2 9f7d7167 2018-04-29 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
4 9f7d7167 2018-04-29 stsp * Permission to use, copy, modify, and distribute this software for any
5 9f7d7167 2018-04-29 stsp * purpose with or without fee is hereby granted, provided that the above
6 9f7d7167 2018-04-29 stsp * copyright notice and this permission notice appear in all copies.
8 9f7d7167 2018-04-29 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 9f7d7167 2018-04-29 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 9f7d7167 2018-04-29 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 9f7d7167 2018-04-29 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 9f7d7167 2018-04-29 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 9f7d7167 2018-04-29 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 9f7d7167 2018-04-29 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 80ddbec8 2018-04-29 stsp #include <sys/queue.h>
18 ffd1d5e5 2018-06-23 stsp #include <sys/stat.h>
20 31120ada 2018-04-30 stsp #include <errno.h>
21 61e69b96 2018-05-20 stsp #define _XOPEN_SOURCE_EXTENDED
22 9f7d7167 2018-04-29 stsp #include <curses.h>
23 61e69b96 2018-05-20 stsp #undef _XOPEN_SOURCE_EXTENDED
24 9f7d7167 2018-04-29 stsp #include <panel.h>
25 9f7d7167 2018-04-29 stsp #include <locale.h>
26 9f7d7167 2018-04-29 stsp #include <stdlib.h>
27 26ed57b2 2018-05-19 stsp #include <stdio.h>
28 9f7d7167 2018-04-29 stsp #include <getopt.h>
29 9f7d7167 2018-04-29 stsp #include <string.h>
30 9f7d7167 2018-04-29 stsp #include <err.h>
31 80ddbec8 2018-04-29 stsp #include <unistd.h>
32 26ed57b2 2018-05-19 stsp #include <util.h>
33 26ed57b2 2018-05-19 stsp #include <limits.h>
34 61e69b96 2018-05-20 stsp #include <wchar.h>
35 788c352e 2018-06-16 stsp #include <time.h>
36 84451b3e 2018-07-10 stsp #include <pthread.h>
38 9f7d7167 2018-04-29 stsp #include "got_error.h"
39 80ddbec8 2018-04-29 stsp #include "got_object.h"
40 80ddbec8 2018-04-29 stsp #include "got_reference.h"
41 80ddbec8 2018-04-29 stsp #include "got_repository.h"
42 80ddbec8 2018-04-29 stsp #include "got_diff.h"
43 511a516b 2018-05-19 stsp #include "got_opentemp.h"
44 9ba79e04 2018-06-11 stsp #include "got_commit_graph.h"
45 00dfcb92 2018-06-11 stsp #include "got_utf8.h"
46 a70480e0 2018-06-23 stsp #include "got_blame.h"
49 881b2d3e 2018-04-30 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
52 9f7d7167 2018-04-29 stsp #ifndef nitems
53 9f7d7167 2018-04-29 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
56 9f7d7167 2018-04-29 stsp struct tog_cmd {
57 c2301be8 2018-04-30 stsp const char *name;
58 9f7d7167 2018-04-29 stsp const struct got_error *(*cmd_main)(int, char *[]);
59 9f7d7167 2018-04-29 stsp void (*cmd_usage)(void);
60 c2301be8 2018-04-30 stsp const char *descr;
63 4ed7e80c 2018-05-20 stsp __dead static void usage(void);
64 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
65 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
66 4ed7e80c 2018-05-20 stsp __dead static void usage_blame(void);
67 ffd1d5e5 2018-06-23 stsp __dead static void usage_tree(void);
69 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
70 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
71 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_blame(int, char *[]);
72 ffd1d5e5 2018-06-23 stsp static const struct got_error* cmd_tree(int, char *[]);
74 4ed7e80c 2018-05-20 stsp static struct tog_cmd tog_commands[] = {
75 cbb6b58a 2018-05-20 stsp { "log", cmd_log, usage_log,
76 9f7d7167 2018-04-29 stsp "show repository history" },
77 cbb6b58a 2018-05-20 stsp { "diff", cmd_diff, usage_diff,
78 9f7d7167 2018-04-29 stsp "compare files and directories" },
79 cbb6b58a 2018-05-20 stsp { "blame", cmd_blame, usage_blame,
80 9f7d7167 2018-04-29 stsp "show line-by-line file history" },
81 ffd1d5e5 2018-06-23 stsp { "tree", cmd_tree, usage_tree,
82 ffd1d5e5 2018-06-23 stsp "browse trees in repository" },
85 d6b05b5b 2018-08-04 stsp enum tog_view_type {
86 d6b05b5b 2018-08-04 stsp TOG_VIEW_DIFF,
87 d6b05b5b 2018-08-04 stsp TOG_VIEW_LOG,
88 ad80ab7b 2018-08-04 stsp TOG_VIEW_BLAME,
89 ad80ab7b 2018-08-04 stsp TOG_VIEW_TREE
92 ad80ab7b 2018-08-04 stsp struct tog_diff_view_state {
93 bcbd79e2 2018-08-19 stsp struct got_object_id *id;
95 ad80ab7b 2018-08-04 stsp int first_displayed_line;
96 ad80ab7b 2018-08-04 stsp int last_displayed_line;
100 ba4f502b 2018-08-04 stsp struct commit_queue_entry {
101 ba4f502b 2018-08-04 stsp TAILQ_ENTRY(commit_queue_entry) entry;
102 ba4f502b 2018-08-04 stsp struct got_object_id *id;
103 ba4f502b 2018-08-04 stsp struct got_commit_object *commit;
105 ba4f502b 2018-08-04 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
106 ba4f502b 2018-08-04 stsp struct commit_queue {
107 ba4f502b 2018-08-04 stsp int ncommits;
108 ba4f502b 2018-08-04 stsp struct commit_queue_head head;
111 b01e7d3b 2018-08-04 stsp struct tog_log_view_state {
112 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
113 b01e7d3b 2018-08-04 stsp struct commit_queue commits;
114 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
115 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
116 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
117 b01e7d3b 2018-08-04 stsp int selected;
118 b01e7d3b 2018-08-04 stsp char *in_repo_path;
119 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
122 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
123 e9424729 2018-08-04 stsp pthread_mutex_t *mutex;
124 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
125 e9424729 2018-08-04 stsp int nlines;
127 e9424729 2018-08-04 stsp struct tog_view *view;
128 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
130 e9424729 2018-08-04 stsp const char *path;
131 e9424729 2018-08-04 stsp int *first_displayed_line;
132 e9424729 2018-08-04 stsp int *last_displayed_line;
133 e9424729 2018-08-04 stsp int *selected_line;
138 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
139 e9424729 2018-08-04 stsp const char *path;
140 e9424729 2018-08-04 stsp struct got_repository *repo;
141 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
142 e9424729 2018-08-04 stsp int *complete;
145 e9424729 2018-08-04 stsp struct tog_blame {
147 e9424729 2018-08-04 stsp size_t filesize;
148 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
149 e9424729 2018-08-04 stsp size_t nlines;
150 e9424729 2018-08-04 stsp pthread_t thread;
151 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
152 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
153 e9424729 2018-08-04 stsp const char *path;
156 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
157 7cbe629d 2018-08-04 stsp int first_displayed_line;
158 7cbe629d 2018-08-04 stsp int last_displayed_line;
159 7cbe629d 2018-08-04 stsp int selected_line;
160 7cbe629d 2018-08-04 stsp int blame_complete;
163 7cbe629d 2018-08-04 stsp pthread_mutex_t mutex;
164 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
165 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
166 e5a0f69f 2018-08-18 stsp char *path;
167 7cbe629d 2018-08-04 stsp struct got_repository *repo;
168 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
169 e9424729 2018-08-04 stsp struct tog_blame blame;
172 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
173 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
174 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
175 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
176 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
177 ad80ab7b 2018-08-04 stsp int selected;
180 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
182 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
183 ad80ab7b 2018-08-04 stsp char *tree_label;
184 ad80ab7b 2018-08-04 stsp struct got_tree_object *root;
185 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
186 ad80ab7b 2018-08-04 stsp const struct got_tree_entries *entries;
187 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
188 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
189 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
190 ad80ab7b 2018-08-04 stsp int nentries, ndisplayed, selected, show_ids;
191 ad80ab7b 2018-08-04 stsp struct tog_parent_trees parents;
192 7cbe629d 2018-08-04 stsp struct got_object_id *commit_id;
193 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
196 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
197 cc3c9aac 2018-08-01 stsp struct tog_view {
198 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
199 26ed57b2 2018-05-19 stsp WINDOW *window;
200 26ed57b2 2018-05-19 stsp PANEL *panel;
201 97ddc146 2018-08-01 stsp int nlines, ncols, begin_y, begin_x;
202 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
203 6d0fee91 2018-08-01 stsp struct tog_view *parent;
204 bcbd79e2 2018-08-19 stsp struct tog_view *child;
206 5dc9f4bc 2018-08-04 stsp /* type-specific state */
207 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
209 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
210 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
211 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
212 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
215 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
216 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
217 e5a0f69f 2018-08-18 stsp struct tog_view **, struct tog_view *, int);
218 bcbd79e2 2018-08-19 stsp const struct got_error *(*set_child)(struct tog_view *,
219 bcbd79e2 2018-08-19 stsp struct tog_view *);
220 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
223 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
224 ba4f502b 2018-08-04 stsp struct got_object *, struct got_object *, struct got_repository *);
225 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
226 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
227 e5a0f69f 2018-08-18 stsp struct tog_view **, struct tog_view *, int);
228 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
230 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
231 ba4f502b 2018-08-04 stsp struct got_object_id *, struct got_repository *, const char *);
232 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
233 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
234 e5a0f69f 2018-08-18 stsp struct tog_view **, struct tog_view *, int);
235 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
236 bcbd79e2 2018-08-19 stsp static const struct got_error* set_child_log_view(struct tog_view *,
237 bcbd79e2 2018-08-19 stsp struct tog_view *);
239 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
240 5221c383 2018-08-01 stsp struct got_object_id *, struct got_repository *);
241 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
242 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
243 e5a0f69f 2018-08-18 stsp struct tog_view **, struct tog_view *, int);
244 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
246 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
247 4fc679ca 2018-08-04 stsp struct got_tree_object *, struct got_object_id *, struct got_repository *);
248 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
249 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
250 e5a0f69f 2018-08-18 stsp struct tog_view **, struct tog_view *, int);
251 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
253 e5a0f69f 2018-08-18 stsp static const struct got_error *
254 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
256 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
258 bcbd79e2 2018-08-19 stsp if (view->child)
259 bcbd79e2 2018-08-19 stsp view->child->parent = NULL;
260 bcbd79e2 2018-08-19 stsp if (view->parent)
261 bcbd79e2 2018-08-19 stsp view->parent->child = NULL;
262 e5a0f69f 2018-08-18 stsp if (view->close)
263 e5a0f69f 2018-08-18 stsp err = view->close(view);
264 ea5e7bb5 2018-08-01 stsp if (view->panel)
265 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
266 ea5e7bb5 2018-08-01 stsp if (view->window)
267 ea5e7bb5 2018-08-01 stsp delwin(view->window);
268 ea5e7bb5 2018-08-01 stsp free(view);
269 e5a0f69f 2018-08-18 stsp return err;
272 ea5e7bb5 2018-08-01 stsp static struct tog_view *
273 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
274 d6b05b5b 2018-08-04 stsp struct tog_view *parent, enum tog_view_type type)
276 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
278 ea5e7bb5 2018-08-01 stsp if (view == NULL)
279 ea5e7bb5 2018-08-01 stsp return NULL;
281 dd04dd3f 2018-08-19 stsp if (begin_x == 0 && parent && parent->ncols - 80 > 10)
282 dd04dd3f 2018-08-19 stsp begin_x = parent->ncols - 80;
284 6d0fee91 2018-08-01 stsp view->parent = parent;
285 bcbd79e2 2018-08-19 stsp if (parent)
286 bcbd79e2 2018-08-19 stsp parent->child = view;
287 d6b05b5b 2018-08-04 stsp view->type = type;
288 f7d12f7e 2018-08-01 stsp view->lines = LINES;
289 f7d12f7e 2018-08-01 stsp view->cols = COLS;
290 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
291 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
292 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
293 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
294 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
295 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
296 96a765a8 2018-08-04 stsp view_close(view);
297 ea5e7bb5 2018-08-01 stsp return NULL;
299 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
300 ea5e7bb5 2018-08-01 stsp if (view->panel == NULL) {
301 96a765a8 2018-08-04 stsp view_close(view);
302 ea5e7bb5 2018-08-01 stsp return NULL;
305 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
306 ea5e7bb5 2018-08-01 stsp return view;
309 4d8c2215 2018-08-19 stsp static const struct got_error *
310 6acd1bd1 2018-08-04 stsp view_show(struct tog_view *view)
312 e5a0f69f 2018-08-18 stsp const struct got_error *err;
314 bcbd79e2 2018-08-19 stsp if (view->parent) {
315 bcbd79e2 2018-08-19 stsp err = view->parent->show(view->parent);
317 bcbd79e2 2018-08-19 stsp return err;
318 bcbd79e2 2018-08-19 stsp show_panel(view->parent->panel);
321 e5a0f69f 2018-08-18 stsp err = view->show(view);
323 e5a0f69f 2018-08-18 stsp return err;
324 cdf1ee82 2018-08-01 stsp show_panel(view->panel);
326 bcbd79e2 2018-08-19 stsp if (view->child) {
327 bcbd79e2 2018-08-19 stsp err = view->child->show(view->child);
329 bcbd79e2 2018-08-19 stsp return err;
330 bcbd79e2 2018-08-19 stsp show_panel(view->child->panel);
333 cdf1ee82 2018-08-01 stsp update_panels();
334 e5a0f69f 2018-08-18 stsp doupdate();
336 e5a0f69f 2018-08-18 stsp return err;
339 4d8c2215 2018-08-19 stsp static const struct got_error *
340 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
342 f7d12f7e 2018-08-01 stsp int nlines, ncols;
344 6d0fee91 2018-08-01 stsp while (view) {
345 6d0fee91 2018-08-01 stsp if (view->lines > LINES)
346 6d0fee91 2018-08-01 stsp nlines = view->nlines - (view->lines - LINES);
348 6d0fee91 2018-08-01 stsp nlines = view->nlines + (LINES - view->lines);
350 6d0fee91 2018-08-01 stsp if (view->cols > COLS)
351 6d0fee91 2018-08-01 stsp ncols = view->ncols - (view->cols - COLS);
353 6d0fee91 2018-08-01 stsp ncols = view->ncols + (COLS - view->cols);
355 6d0fee91 2018-08-01 stsp if (wresize(view->window, nlines, ncols) == ERR)
356 6d0fee91 2018-08-01 stsp return got_error_from_errno();
357 77a567dd 2018-08-01 stsp replace_panel(view->panel, view->window);
359 6d0fee91 2018-08-01 stsp view->nlines = nlines;
360 6d0fee91 2018-08-01 stsp view->ncols = ncols;
361 6d0fee91 2018-08-01 stsp view->lines = LINES;
362 6d0fee91 2018-08-01 stsp view->cols = COLS;
364 6d0fee91 2018-08-01 stsp view = view->parent;
367 f7d12f7e 2018-08-01 stsp return NULL;
370 e5a0f69f 2018-08-18 stsp static const struct got_error *
371 48fcc512 2018-08-18 stsp view_input(struct tog_view **new, struct tog_view **dead,
372 e4197bf9 2018-08-18 stsp struct tog_view **focus, int *done, struct tog_view *view,
373 48fcc512 2018-08-18 stsp struct tog_view_list_head *views)
375 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
376 31607d6c 2018-08-18 stsp struct tog_view *next, *prev;
379 e5a0f69f 2018-08-18 stsp *new = NULL;
380 e5a0f69f 2018-08-18 stsp *dead = NULL;
382 e5a0f69f 2018-08-18 stsp nodelay(stdscr, FALSE);
383 e5a0f69f 2018-08-18 stsp ch = wgetch(view->window);
384 e5a0f69f 2018-08-18 stsp nodelay(stdscr, TRUE);
385 e5a0f69f 2018-08-18 stsp switch (ch) {
389 48fcc512 2018-08-18 stsp next = TAILQ_NEXT(view, entry);
391 48fcc512 2018-08-18 stsp *focus = next;
393 48fcc512 2018-08-18 stsp *focus = TAILQ_FIRST(views);
395 31607d6c 2018-08-18 stsp case KEY_BACKSPACE:
396 31607d6c 2018-08-18 stsp prev = TAILQ_PREV(view, tog_view_list_head, entry);
398 31607d6c 2018-08-18 stsp *focus = prev;
400 31607d6c 2018-08-18 stsp *focus = TAILQ_LAST(views, tog_view_list_head);
403 e5a0f69f 2018-08-18 stsp err = view->input(new, dead, view, ch);
404 e5a0f69f 2018-08-18 stsp *dead = view;
409 e5a0f69f 2018-08-18 stsp case KEY_RESIZE:
410 e5a0f69f 2018-08-18 stsp err = view_resize(view);
412 e5a0f69f 2018-08-18 stsp return err;
413 e5a0f69f 2018-08-18 stsp err = view->input(new, dead, view, ch);
416 e5a0f69f 2018-08-18 stsp err = view->input(new, dead, view, ch);
420 e5a0f69f 2018-08-18 stsp return err;
423 e5a0f69f 2018-08-18 stsp static const struct got_error *
424 bcbd79e2 2018-08-19 stsp view_set_child(struct tog_view *view, struct tog_view *child)
426 bcbd79e2 2018-08-19 stsp const struct got_error *err;
428 bcbd79e2 2018-08-19 stsp if (view->set_child) {
429 bcbd79e2 2018-08-19 stsp err = view->set_child(view, child);
431 bcbd79e2 2018-08-19 stsp return err;
434 bcbd79e2 2018-08-19 stsp view->child = child;
435 bcbd79e2 2018-08-19 stsp return NULL;
438 bcbd79e2 2018-08-19 stsp static const struct got_error *
439 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
441 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
442 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
443 e5a0f69f 2018-08-18 stsp struct tog_view *new_view, *dead_view;
444 e4197bf9 2018-08-18 stsp int done = 0;
446 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
447 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
449 e4197bf9 2018-08-18 stsp while (!TAILQ_EMPTY(&views) && !done) {
450 e5a0f69f 2018-08-18 stsp err = view_show(view);
453 e4197bf9 2018-08-18 stsp err = view_input(&new_view, &dead_view, &view, &done,
454 e4197bf9 2018-08-18 stsp view, &views);
457 e5a0f69f 2018-08-18 stsp if (dead_view) {
458 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, dead_view, entry);
459 e5a0f69f 2018-08-18 stsp if (dead_view->parent)
460 e5a0f69f 2018-08-18 stsp view = dead_view->parent;
462 e5a0f69f 2018-08-18 stsp view = TAILQ_LAST(&views, tog_view_list_head);
463 bcbd79e2 2018-08-19 stsp if (dead_view->child) {
464 bcbd79e2 2018-08-19 stsp TAILQ_REMOVE(&views, dead_view->child, entry);
465 bcbd79e2 2018-08-19 stsp err = view_close(dead_view->child);
469 e5a0f69f 2018-08-18 stsp err = view_close(dead_view);
473 bcbd79e2 2018-08-19 stsp if (new_view) {
474 bcbd79e2 2018-08-19 stsp /* TODO: de-duplicate! */
475 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
476 bcbd79e2 2018-08-19 stsp if (new_view->parent) {
477 bcbd79e2 2018-08-19 stsp err = view_set_child(new_view->parent, new_view);
481 bcbd79e2 2018-08-19 stsp view = new_view;
485 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
486 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
487 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
488 e5a0f69f 2018-08-18 stsp view_close(view);
490 e5a0f69f 2018-08-18 stsp return err;
493 4ed7e80c 2018-05-20 stsp __dead static void
494 9f7d7167 2018-04-29 stsp usage_log(void)
497 c70c5802 2018-08-01 stsp fprintf(stderr,
498 c70c5802 2018-08-01 stsp "usage: %s log [-c commit] [-r repository-path] [path]\n",
499 9f7d7167 2018-04-29 stsp getprogname());
503 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
504 80ddbec8 2018-04-29 stsp static const struct got_error *
505 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
507 00dfcb92 2018-06-11 stsp char *vis = NULL;
508 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
510 963b370f 2018-05-20 stsp *ws = NULL;
511 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
512 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
513 00dfcb92 2018-06-11 stsp int vislen;
514 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
515 00dfcb92 2018-06-11 stsp return got_error_from_errno();
517 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
518 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
520 00dfcb92 2018-06-11 stsp return err;
521 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
522 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
523 a7f50699 2018-06-11 stsp err = got_error_from_errno(); /* give up */
528 963b370f 2018-05-20 stsp *ws = calloc(*wlen + 1, sizeof(*ws));
529 a7f50699 2018-06-11 stsp if (*ws == NULL) {
530 a7f50699 2018-06-11 stsp err = got_error_from_errno();
534 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
535 963b370f 2018-05-20 stsp err = got_error_from_errno();
540 963b370f 2018-05-20 stsp *ws = NULL;
543 963b370f 2018-05-20 stsp return err;
546 963b370f 2018-05-20 stsp /* Format a line for display, ensuring that it won't overflow a width limit. */
547 963b370f 2018-05-20 stsp static const struct got_error *
548 ffd1d5e5 2018-06-23 stsp format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
550 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
551 963b370f 2018-05-20 stsp int cols = 0;
552 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
553 963b370f 2018-05-20 stsp size_t wlen;
556 963b370f 2018-05-20 stsp *wlinep = NULL;
557 b700b5d6 2018-07-10 stsp *widthp = 0;
559 963b370f 2018-05-20 stsp err = mbs2ws(&wline, &wlen, line);
561 963b370f 2018-05-20 stsp return err;
564 b700b5d6 2018-07-10 stsp while (i < wlen && cols < wlimit) {
565 963b370f 2018-05-20 stsp int width = wcwidth(wline[i]);
566 963b370f 2018-05-20 stsp switch (width) {
572 b700b5d6 2018-07-10 stsp if (cols + width <= wlimit) {
573 b700b5d6 2018-07-10 stsp cols += width;
578 963b370f 2018-05-20 stsp if (wline[i] == L'\t')
579 b700b5d6 2018-07-10 stsp cols += TABSIZE - ((cols + 1) % TABSIZE);
583 963b370f 2018-05-20 stsp err = got_error_from_errno();
587 963b370f 2018-05-20 stsp wline[i] = L'\0';
588 b700b5d6 2018-07-10 stsp if (widthp)
589 b700b5d6 2018-07-10 stsp *widthp = cols;
592 963b370f 2018-05-20 stsp free(wline);
594 963b370f 2018-05-20 stsp *wlinep = wline;
595 963b370f 2018-05-20 stsp return err;
598 963b370f 2018-05-20 stsp static const struct got_error *
599 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
600 2814baeb 2018-08-01 stsp struct got_object_id *id)
602 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
603 b39d25c7 2018-07-10 stsp char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
604 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
605 6d9fbc00 2018-04-29 stsp char *author0 = NULL, *author = NULL;
606 bb737323 2018-05-20 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
607 bb737323 2018-05-20 stsp int author_width, logmsg_width;
608 6d9fbc00 2018-04-29 stsp char *newline, *smallerthan;
609 80ddbec8 2018-04-29 stsp char *line = NULL;
610 bb737323 2018-05-20 stsp int col, limit;
611 b39d25c7 2018-07-10 stsp static const size_t date_display_cols = 9;
612 bb737323 2018-05-20 stsp static const size_t author_display_cols = 16;
613 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
615 c70c5802 2018-08-01 stsp if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ",
616 c70c5802 2018-08-01 stsp &commit->tm_committer) >= sizeof(datebuf))
617 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
619 b39d25c7 2018-07-10 stsp if (avail < date_display_cols)
620 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
622 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
623 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
624 b39d25c7 2018-07-10 stsp col = limit + 1;
625 b39d25c7 2018-07-10 stsp if (col > avail)
628 6d9fbc00 2018-04-29 stsp author0 = strdup(commit->author);
629 6d9fbc00 2018-04-29 stsp if (author0 == NULL) {
630 80ddbec8 2018-04-29 stsp err = got_error_from_errno();
633 6d9fbc00 2018-04-29 stsp author = author0;
634 6d9fbc00 2018-04-29 stsp smallerthan = strchr(author, '<');
635 6d9fbc00 2018-04-29 stsp if (smallerthan)
636 6d9fbc00 2018-04-29 stsp *smallerthan = '\0';
638 6d9fbc00 2018-04-29 stsp char *at = strchr(author, '@');
640 6d9fbc00 2018-04-29 stsp *at = '\0';
642 b39d25c7 2018-07-10 stsp limit = avail - col;
643 bb737323 2018-05-20 stsp err = format_line(&wauthor, &author_width, author, limit);
646 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
647 bb737323 2018-05-20 stsp col += author_width;
648 9c2eaf34 2018-05-20 stsp while (col <= avail && author_width < author_display_cols + 1) {
649 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
651 bb737323 2018-05-20 stsp author_width++;
653 9c2eaf34 2018-05-20 stsp if (col > avail)
656 bb737323 2018-05-20 stsp logmsg0 = strdup(commit->logmsg);
657 bb737323 2018-05-20 stsp if (logmsg0 == NULL) {
658 6d9fbc00 2018-04-29 stsp err = got_error_from_errno();
661 bb737323 2018-05-20 stsp logmsg = logmsg0;
662 bb737323 2018-05-20 stsp while (*logmsg == '\n')
664 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
665 bb737323 2018-05-20 stsp if (newline)
666 bb737323 2018-05-20 stsp *newline = '\0';
667 bb737323 2018-05-20 stsp limit = avail - col;
668 bb737323 2018-05-20 stsp err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
671 2814baeb 2018-08-01 stsp waddwstr(view->window, wlogmsg);
672 bb737323 2018-05-20 stsp col += logmsg_width;
673 bb737323 2018-05-20 stsp while (col <= avail) {
674 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
678 80ddbec8 2018-04-29 stsp free(logmsg0);
679 bb737323 2018-05-20 stsp free(wlogmsg);
680 6d9fbc00 2018-04-29 stsp free(author0);
681 bb737323 2018-05-20 stsp free(wauthor);
682 80ddbec8 2018-04-29 stsp free(line);
683 80ddbec8 2018-04-29 stsp return err;
686 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
687 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
688 899d86c2 2018-05-10 stsp struct got_object_id *id)
690 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
692 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
693 80ddbec8 2018-04-29 stsp if (entry == NULL)
694 899d86c2 2018-05-10 stsp return NULL;
696 899d86c2 2018-05-10 stsp entry->id = id;
697 99db9666 2018-05-07 stsp entry->commit = commit;
698 899d86c2 2018-05-10 stsp return entry;
701 99db9666 2018-05-07 stsp static void
702 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
704 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
706 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
707 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
708 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
709 ecb28ae0 2018-07-16 stsp commits->ncommits--;
710 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
711 99db9666 2018-05-07 stsp free(entry);
714 99db9666 2018-05-07 stsp static void
715 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
717 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
718 99db9666 2018-05-07 stsp pop_commit(commits);
721 c4972b91 2018-05-07 stsp static const struct got_error *
722 9ba79e04 2018-06-11 stsp queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
723 ecb28ae0 2018-07-16 stsp struct got_object_id *start_id, int minqueue, int init,
724 ecb28ae0 2018-07-16 stsp struct got_repository *repo, const char *path)
726 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
727 899d86c2 2018-05-10 stsp struct got_object_id *id;
728 9ba79e04 2018-06-11 stsp struct commit_queue_entry *entry;
729 ecb28ae0 2018-07-16 stsp int nfetched, nqueued = 0, found_obj = 0;
730 c8f60bff 2018-07-23 stsp int is_root_path = strcmp(path, "/") == 0;
732 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_start(graph, start_id);
734 c4972b91 2018-05-07 stsp return err;
736 ecb28ae0 2018-07-16 stsp entry = TAILQ_LAST(&commits->head, commit_queue_head);
737 9ba79e04 2018-06-11 stsp if (entry && got_object_id_cmp(entry->id, start_id) == 0) {
738 9ba79e04 2018-06-11 stsp int nfetched;
740 9ba79e04 2018-06-11 stsp /* Start ID's commit is already on the queue; skip over it. */
741 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
742 9ba79e04 2018-06-11 stsp if (err && err->code != GOT_ERR_ITER_NEED_MORE)
743 9ba79e04 2018-06-11 stsp return err;
745 9ba79e04 2018-06-11 stsp err = got_commit_graph_fetch_commits(&nfetched, graph, 1, repo);
747 9ba79e04 2018-06-11 stsp return err;
750 9ba79e04 2018-06-11 stsp while (1) {
751 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
753 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
755 ecb28ae0 2018-07-16 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
757 ecb28ae0 2018-07-16 stsp if (nqueued >= minqueue) {
758 9ba79e04 2018-06-11 stsp err = NULL;
761 ecb28ae0 2018-07-16 stsp err = got_commit_graph_fetch_commits(&nfetched,
762 ecb28ae0 2018-07-16 stsp graph, 1, repo);
764 ecb28ae0 2018-07-16 stsp return err;
767 ecb28ae0 2018-07-16 stsp if (id == NULL)
770 9ba79e04 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
774 c8f60bff 2018-07-23 stsp if (!is_root_path) {
775 ecb28ae0 2018-07-16 stsp struct got_object *obj;
776 ecb28ae0 2018-07-16 stsp struct got_object_qid *pid;
777 ecb28ae0 2018-07-16 stsp int changed = 0;
779 ecb28ae0 2018-07-16 stsp err = got_object_open_by_path(&obj, repo, id, path);
781 c8f60bff 2018-07-23 stsp got_object_commit_close(commit);
782 ecb28ae0 2018-07-16 stsp if (err->code == GOT_ERR_NO_OBJ &&
783 ecb28ae0 2018-07-16 stsp (found_obj || !init)) {
784 ecb28ae0 2018-07-16 stsp /* History stops here. */
785 ecb28ae0 2018-07-16 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
789 ecb28ae0 2018-07-16 stsp found_obj = 1;
791 ecb28ae0 2018-07-16 stsp pid = SIMPLEQ_FIRST(&commit->parent_ids);
792 ecb28ae0 2018-07-16 stsp if (pid != NULL) {
793 ecb28ae0 2018-07-16 stsp struct got_object *pobj;
794 ecb28ae0 2018-07-16 stsp err = got_object_open_by_path(&pobj, repo,
795 ecb28ae0 2018-07-16 stsp pid->id, path);
797 ecb28ae0 2018-07-16 stsp if (err->code != GOT_ERR_NO_OBJ) {
798 ecb28ae0 2018-07-16 stsp got_object_close(obj);
799 c8f60bff 2018-07-23 stsp got_object_commit_close(commit);
802 ecb28ae0 2018-07-16 stsp err = NULL;
803 ecb28ae0 2018-07-16 stsp changed = 1;
805 c2f16475 2018-07-23 stsp struct got_object_id *id, *pid;
806 c2f16475 2018-07-23 stsp id = got_object_get_id(obj);
807 c2f16475 2018-07-23 stsp if (id == NULL) {
808 c2f16475 2018-07-23 stsp err = got_error_from_errno();
809 c8f60bff 2018-07-23 stsp got_object_close(obj);
810 c8f60bff 2018-07-23 stsp got_object_close(pobj);
813 c2f16475 2018-07-23 stsp pid = got_object_get_id(pobj);
814 c2f16475 2018-07-23 stsp if (pid == NULL) {
815 c2f16475 2018-07-23 stsp err = got_error_from_errno();
817 c8f60bff 2018-07-23 stsp got_object_close(obj);
818 c8f60bff 2018-07-23 stsp got_object_close(pobj);
822 c2f16475 2018-07-23 stsp (got_object_id_cmp(id, pid) != 0);
823 ecb28ae0 2018-07-16 stsp got_object_close(pobj);
828 c8f60bff 2018-07-23 stsp got_object_close(obj);
829 ecb28ae0 2018-07-16 stsp if (!changed) {
830 ecb28ae0 2018-07-16 stsp got_object_commit_close(commit);
835 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
836 9ba79e04 2018-06-11 stsp if (entry == NULL) {
837 9ba79e04 2018-06-11 stsp err = got_error_from_errno();
840 ecb28ae0 2018-07-16 stsp TAILQ_INSERT_TAIL(&commits->head, entry, entry);
842 ecb28ae0 2018-07-16 stsp commits->ncommits++;
845 9ba79e04 2018-06-11 stsp return err;
848 99db9666 2018-05-07 stsp static const struct got_error *
849 9ba79e04 2018-06-11 stsp fetch_next_commit(struct commit_queue_entry **pentry,
850 9ba79e04 2018-06-11 stsp struct commit_queue_entry *entry, struct commit_queue *commits,
851 ecb28ae0 2018-07-16 stsp struct got_commit_graph *graph, struct got_repository *repo,
852 ecb28ae0 2018-07-16 stsp const char *path)
854 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
856 9ba79e04 2018-06-11 stsp *pentry = NULL;
858 ecb28ae0 2018-07-16 stsp err = queue_commits(graph, commits, entry->id, 1, 0, repo, path);
860 9ba79e04 2018-06-11 stsp return err;
862 9ba79e04 2018-06-11 stsp /* Next entry to display should now be available. */
863 9ba79e04 2018-06-11 stsp *pentry = TAILQ_NEXT(entry, entry);
864 9ba79e04 2018-06-11 stsp if (*pentry == NULL)
865 9ba79e04 2018-06-11 stsp return got_error(GOT_ERR_NO_OBJ);
867 9ba79e04 2018-06-11 stsp return NULL;
870 899d86c2 2018-05-10 stsp static const struct got_error *
871 9ba79e04 2018-06-11 stsp get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
873 9ba79e04 2018-06-11 stsp const struct got_error *err = NULL;
874 9ba79e04 2018-06-11 stsp struct got_reference *head_ref;
876 9ba79e04 2018-06-11 stsp *head_id = NULL;
878 9ba79e04 2018-06-11 stsp err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
880 99db9666 2018-05-07 stsp return err;
882 9ba79e04 2018-06-11 stsp err = got_ref_resolve(head_id, repo, head_ref);
883 9ba79e04 2018-06-11 stsp got_ref_close(head_ref);
885 9ba79e04 2018-06-11 stsp *head_id = NULL;
886 99db9666 2018-05-07 stsp return err;
889 9ba79e04 2018-06-11 stsp return NULL;
892 0553a4e3 2018-04-30 stsp static const struct got_error *
893 2814baeb 2018-08-01 stsp draw_commits(struct tog_view *view, struct commit_queue_entry **last,
894 ecb28ae0 2018-07-16 stsp struct commit_queue_entry **selected, struct commit_queue_entry *first,
895 a7f40148 2018-07-18 stsp struct commit_queue *commits, int selected_idx, int limit,
896 a7f40148 2018-07-18 stsp struct got_commit_graph *graph, struct got_repository *repo,
897 a7f40148 2018-07-18 stsp const char *path)
899 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
900 0553a4e3 2018-04-30 stsp struct commit_queue_entry *entry;
901 ecb28ae0 2018-07-16 stsp int ncommits, width;
902 867c6645 2018-07-10 stsp char *id_str, *header;
903 ecb28ae0 2018-07-16 stsp wchar_t *wline;
905 e0d42f60 2018-07-22 stsp entry = first;
906 e0d42f60 2018-07-22 stsp ncommits = 0;
907 e0d42f60 2018-07-22 stsp while (entry) {
908 e0d42f60 2018-07-22 stsp if (ncommits == selected_idx) {
909 e0d42f60 2018-07-22 stsp *selected = entry;
912 e0d42f60 2018-07-22 stsp entry = TAILQ_NEXT(entry, entry);
913 e0d42f60 2018-07-22 stsp ncommits++;
916 867c6645 2018-07-10 stsp err = got_object_id_str(&id_str, (*selected)->id);
918 867c6645 2018-07-10 stsp return err;
920 c3ba6f36 2018-08-01 stsp if (path && strcmp(path, "/") != 0) {
921 ecb28ae0 2018-07-16 stsp if (asprintf(&header, "commit: %s [%s]", id_str, path) == -1) {
922 ecb28ae0 2018-07-16 stsp err = got_error_from_errno();
923 ecb28ae0 2018-07-16 stsp free(id_str);
924 ecb28ae0 2018-07-16 stsp return err;
926 ecb28ae0 2018-07-16 stsp } else if (asprintf(&header, "commit: %s", id_str) == -1) {
927 867c6645 2018-07-10 stsp err = got_error_from_errno();
928 867c6645 2018-07-10 stsp free(id_str);
929 867c6645 2018-07-10 stsp return err;
931 ecb28ae0 2018-07-16 stsp free(id_str);
932 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, header, view->ncols);
934 ecb28ae0 2018-07-16 stsp free(header);
935 ecb28ae0 2018-07-16 stsp return err;
937 ecb28ae0 2018-07-16 stsp free(header);
939 2814baeb 2018-08-01 stsp werase(view->window);
941 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
942 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
943 2814baeb 2018-08-01 stsp waddch(view->window, '\n');
944 ecb28ae0 2018-07-16 stsp free(wline);
945 ecb28ae0 2018-07-16 stsp if (limit <= 1)
946 ecb28ae0 2018-07-16 stsp return NULL;
948 899d86c2 2018-05-10 stsp entry = first;
949 899d86c2 2018-05-10 stsp *last = first;
950 867c6645 2018-07-10 stsp ncommits = 0;
951 899d86c2 2018-05-10 stsp while (entry) {
952 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
954 e0d42f60 2018-07-22 stsp if (ncommits == selected_idx)
955 2814baeb 2018-08-01 stsp wstandout(view->window);
956 2814baeb 2018-08-01 stsp err = draw_commit(view, entry->commit, entry->id);
957 cd0acaa7 2018-05-20 stsp if (ncommits == selected_idx)
958 2814baeb 2018-08-01 stsp wstandend(view->window);
961 0553a4e3 2018-04-30 stsp ncommits++;
962 899d86c2 2018-05-10 stsp *last = entry;
963 a7f40148 2018-07-18 stsp if (entry == TAILQ_LAST(&commits->head, commit_queue_head)) {
964 a7f40148 2018-07-18 stsp err = queue_commits(graph, commits, entry->id, 1,
965 a7f40148 2018-07-18 stsp 0, repo, path);
967 a7f40148 2018-07-18 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
968 a7f40148 2018-07-18 stsp return err;
969 a7f40148 2018-07-18 stsp err = NULL;
972 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
975 80ddbec8 2018-04-29 stsp update_panels();
977 80ddbec8 2018-04-29 stsp return err;
980 07b55e75 2018-05-10 stsp static void
981 16482c3b 2018-05-20 stsp scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
982 07b55e75 2018-05-10 stsp struct commit_queue *commits)
984 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
985 07b55e75 2018-05-10 stsp int nscrolled = 0;
987 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
988 07b55e75 2018-05-10 stsp if (*first_displayed_entry == entry)
991 07b55e75 2018-05-10 stsp entry = *first_displayed_entry;
992 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
993 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
994 07b55e75 2018-05-10 stsp if (entry) {
995 07b55e75 2018-05-10 stsp *first_displayed_entry = entry;
996 07b55e75 2018-05-10 stsp nscrolled++;
1001 aa075928 2018-05-10 stsp static const struct got_error *
1002 16482c3b 2018-05-20 stsp scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1003 aa075928 2018-05-10 stsp struct commit_queue_entry *last_displayed_entry,
1004 9ba79e04 2018-06-11 stsp struct commit_queue *commits, struct got_commit_graph *graph,
1005 ecb28ae0 2018-07-16 stsp struct got_repository *repo, const char *path)
1007 aa075928 2018-05-10 stsp const struct got_error *err = NULL;
1008 dd0a52c1 2018-05-20 stsp struct commit_queue_entry *pentry;
1009 aa075928 2018-05-10 stsp int nscrolled = 0;
1012 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(last_displayed_entry, entry);
1013 aa075928 2018-05-10 stsp if (pentry == NULL) {
1014 9ba79e04 2018-06-11 stsp err = fetch_next_commit(&pentry, last_displayed_entry,
1015 ecb28ae0 2018-07-16 stsp commits, graph, repo, path);
1016 9a6bf2a5 2018-05-20 stsp if (err || pentry == NULL)
1019 dd0a52c1 2018-05-20 stsp last_displayed_entry = pentry;
1021 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1022 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
1024 aa075928 2018-05-10 stsp *first_displayed_entry = pentry;
1025 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
1027 dd0a52c1 2018-05-20 stsp return err;
1030 cd0acaa7 2018-05-20 stsp static const struct got_error *
1031 e5a0f69f 2018-08-18 stsp show_commit(struct tog_view **new_view, struct tog_view *parent_view,
1032 e5a0f69f 2018-08-18 stsp struct commit_queue_entry *entry, struct got_repository *repo)
1034 cd0acaa7 2018-05-20 stsp const struct got_error *err;
1035 cd0acaa7 2018-05-20 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
1036 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
1037 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
1039 cd0acaa7 2018-05-20 stsp err = got_object_open(&obj2, repo, entry->id);
1041 cd0acaa7 2018-05-20 stsp return err;
1043 9ba79e04 2018-06-11 stsp parent_id = SIMPLEQ_FIRST(&entry->commit->parent_ids);
1044 9ba79e04 2018-06-11 stsp if (parent_id) {
1045 9ba79e04 2018-06-11 stsp err = got_object_open(&obj1, repo, parent_id->id);
1047 cd0acaa7 2018-05-20 stsp goto done;
1050 e5a0f69f 2018-08-18 stsp diff_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_DIFF);
1051 e5a0f69f 2018-08-18 stsp if (diff_view == NULL) {
1052 ea5e7bb5 2018-08-01 stsp err = got_error_from_errno();
1053 ea5e7bb5 2018-08-01 stsp goto done;
1056 e5a0f69f 2018-08-18 stsp err = open_diff_view(diff_view, obj1, obj2, repo);
1057 e5a0f69f 2018-08-18 stsp if (err == NULL)
1058 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
1061 cd0acaa7 2018-05-20 stsp got_object_close(obj1);
1063 cd0acaa7 2018-05-20 stsp got_object_close(obj2);
1064 cd0acaa7 2018-05-20 stsp return err;
1067 80ddbec8 2018-04-29 stsp static const struct got_error *
1068 e5a0f69f 2018-08-18 stsp browse_commit(struct tog_view **new_view, struct tog_view *parent_view,
1069 e5a0f69f 2018-08-18 stsp struct commit_queue_entry *entry, struct got_repository *repo)
1071 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
1072 9343a5fb 2018-06-23 stsp struct got_tree_object *tree;
1073 e5a0f69f 2018-08-18 stsp struct tog_view *tree_view;
1075 9343a5fb 2018-06-23 stsp err = got_object_open_as_tree(&tree, repo, entry->commit->tree_id);
1077 9343a5fb 2018-06-23 stsp return err;
1079 e5a0f69f 2018-08-18 stsp tree_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_TREE);
1080 e5a0f69f 2018-08-18 stsp if (tree_view == NULL)
1081 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
1083 e5a0f69f 2018-08-18 stsp err = open_tree_view(tree_view, tree, entry->id, repo);
1085 e5a0f69f 2018-08-18 stsp got_object_tree_close(tree);
1087 e5a0f69f 2018-08-18 stsp *new_view = tree_view;
1088 9343a5fb 2018-06-23 stsp return err;
1091 bcbd79e2 2018-08-19 stsp static const struct got_error *
1092 bcbd79e2 2018-08-19 stsp set_child_log_view(struct tog_view *view, struct tog_view *child)
1094 bcbd79e2 2018-08-19 stsp struct tog_log_view_state *s = &view->state.log;
1095 bcbd79e2 2018-08-19 stsp struct tog_diff_view_state *ds;
1096 bcbd79e2 2018-08-19 stsp struct commit_queue_entry *commit, *child_entry = NULL;
1097 bcbd79e2 2018-08-19 stsp int selected_idx = 0;
1099 bcbd79e2 2018-08-19 stsp if (child->type != TOG_VIEW_DIFF)
1100 bcbd79e2 2018-08-19 stsp return NULL;
1101 bcbd79e2 2018-08-19 stsp ds = &child->state.diff;
1103 bcbd79e2 2018-08-19 stsp TAILQ_FOREACH(commit, &s->commits.head, entry) {
1104 bcbd79e2 2018-08-19 stsp if (got_object_id_cmp(commit->id, ds->id) == 0) {
1105 bcbd79e2 2018-08-19 stsp child_entry = commit;
1109 bcbd79e2 2018-08-19 stsp if (child_entry == NULL)
1110 bcbd79e2 2018-08-19 stsp return NULL;
1112 bcbd79e2 2018-08-19 stsp commit = s->first_displayed_entry;
1113 bcbd79e2 2018-08-19 stsp while (commit) {
1114 bcbd79e2 2018-08-19 stsp if (got_object_id_cmp(commit->id, child_entry->id) == 0) {
1115 bcbd79e2 2018-08-19 stsp s->selected_entry = child_entry;
1116 bcbd79e2 2018-08-19 stsp s->selected = selected_idx;
1119 bcbd79e2 2018-08-19 stsp if (commit == s->last_displayed_entry)
1121 bcbd79e2 2018-08-19 stsp selected_idx++;
1122 bcbd79e2 2018-08-19 stsp commit = TAILQ_NEXT(commit, entry);
1125 bcbd79e2 2018-08-19 stsp return show_log_view(view);
1128 9343a5fb 2018-06-23 stsp static const struct got_error *
1129 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
1130 04cc582a 2018-08-01 stsp struct got_repository *repo, const char *path)
1132 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1133 9ba79e04 2018-06-11 stsp struct got_object_id *head_id = NULL;
1134 ba4f502b 2018-08-04 stsp int nfetched;
1135 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
1137 fb2756b9 2018-08-04 stsp err = got_repo_map_path(&s->in_repo_path, repo, path);
1138 ecb28ae0 2018-07-16 stsp if (err != NULL)
1139 ecb28ae0 2018-07-16 stsp goto done;
1141 9ba79e04 2018-06-11 stsp err = get_head_commit_id(&head_id, repo);
1143 9ba79e04 2018-06-11 stsp return err;
1145 034e3b69 2018-07-22 stsp /* The graph contains all commits. */
1146 fb2756b9 2018-08-04 stsp err = got_commit_graph_open(&s->graph, head_id, 0, repo);
1148 9ba79e04 2018-06-11 stsp goto done;
1149 034e3b69 2018-07-22 stsp /* The commit queue contains a subset of commits filtered by path. */
1150 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
1151 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
1153 9ba79e04 2018-06-11 stsp /* Populate commit graph with a sufficient number of commits. */
1154 fb2756b9 2018-08-04 stsp err = got_commit_graph_fetch_commits_up_to(&nfetched, s->graph,
1155 fb2756b9 2018-08-04 stsp start_id, repo);
1157 9ba79e04 2018-06-11 stsp goto done;
1160 9ba79e04 2018-06-11 stsp * Open the initial batch of commits, sorted in commit graph order.
1161 9ba79e04 2018-06-11 stsp * We keep all commits open throughout the lifetime of the log view
1162 9ba79e04 2018-06-11 stsp * in order to avoid having to re-fetch commits from disk while
1163 9ba79e04 2018-06-11 stsp * updating the display.
1165 fb2756b9 2018-08-04 stsp err = queue_commits(s->graph, &s->commits, start_id, view->nlines, 1,
1166 fb2756b9 2018-08-04 stsp repo, s->in_repo_path);
1167 55198a88 2018-07-16 stsp if (err) {
1168 55198a88 2018-07-16 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
1169 55198a88 2018-07-16 stsp goto done;
1170 55198a88 2018-07-16 stsp err = NULL;
1173 1bfa490b 2018-08-18 stsp s->first_displayed_entry =
1174 1bfa490b 2018-08-18 stsp TAILQ_FIRST(&s->commits.head);
1175 1bfa490b 2018-08-18 stsp s->selected_entry = s->first_displayed_entry;
1176 fb2756b9 2018-08-04 stsp s->repo = repo;
1178 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
1179 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
1180 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
1181 bcbd79e2 2018-08-19 stsp view->set_child = set_child_log_view;
1183 ba4f502b 2018-08-04 stsp free(head_id);
1184 ba4f502b 2018-08-04 stsp return err;
1187 e5a0f69f 2018-08-18 stsp static const struct got_error *
1188 e5a0f69f 2018-08-18 stsp close_log_view(struct tog_view *view)
1190 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
1192 fb2756b9 2018-08-04 stsp if (s->graph)
1193 fb2756b9 2018-08-04 stsp got_commit_graph_close(s->graph);
1194 fb2756b9 2018-08-04 stsp free_commits(&s->commits);
1195 fb2756b9 2018-08-04 stsp free(s->in_repo_path);
1196 e5a0f69f 2018-08-18 stsp return NULL;
1199 ba4f502b 2018-08-04 stsp static const struct got_error *
1200 bcbd79e2 2018-08-19 stsp update_diff_child_view(struct tog_view *parent,
1201 bcbd79e2 2018-08-19 stsp struct commit_queue_entry *selected_entry, struct got_repository *repo)
1203 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
1204 bcbd79e2 2018-08-19 stsp struct tog_diff_view_state *ds;
1205 bcbd79e2 2018-08-19 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
1206 bcbd79e2 2018-08-19 stsp struct got_object_qid *parent_id;
1207 bcbd79e2 2018-08-19 stsp struct tog_view *child_view = parent->child;
1209 bcbd79e2 2018-08-19 stsp if (child_view == NULL)
1210 bcbd79e2 2018-08-19 stsp return NULL;
1211 bcbd79e2 2018-08-19 stsp if (child_view->type != TOG_VIEW_DIFF)
1212 bcbd79e2 2018-08-19 stsp return NULL;
1213 bcbd79e2 2018-08-19 stsp ds = &child_view->state.diff;
1214 bcbd79e2 2018-08-19 stsp if (got_object_id_cmp(ds->id, selected_entry->id) == 0)
1215 bcbd79e2 2018-08-19 stsp return NULL;
1217 bcbd79e2 2018-08-19 stsp err = got_object_open(&obj2, repo, selected_entry->id);
1219 bcbd79e2 2018-08-19 stsp return err;
1221 bcbd79e2 2018-08-19 stsp parent_id = SIMPLEQ_FIRST(&selected_entry->commit->parent_ids);
1222 bcbd79e2 2018-08-19 stsp if (parent_id) {
1223 bcbd79e2 2018-08-19 stsp err = got_object_open(&obj1, repo, parent_id->id);
1225 bcbd79e2 2018-08-19 stsp goto done;
1228 bcbd79e2 2018-08-19 stsp err = close_diff_view(child_view);
1230 bcbd79e2 2018-08-19 stsp goto done;
1232 bcbd79e2 2018-08-19 stsp err = open_diff_view(child_view, obj1, obj2, repo);
1234 bcbd79e2 2018-08-19 stsp goto done;
1237 bcbd79e2 2018-08-19 stsp got_object_close(obj1);
1239 bcbd79e2 2018-08-19 stsp got_object_close(obj2);
1240 bcbd79e2 2018-08-19 stsp return err;
1243 bcbd79e2 2018-08-19 stsp static const struct got_error *
1244 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
1246 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
1247 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
1249 bcbd79e2 2018-08-19 stsp err = draw_commits(view, &s->last_displayed_entry,
1250 e5a0f69f 2018-08-18 stsp &s->selected_entry, s->first_displayed_entry,
1251 e5a0f69f 2018-08-18 stsp &s->commits, s->selected, view->nlines, s->graph,
1252 e5a0f69f 2018-08-18 stsp s->repo, s->in_repo_path);
1254 bcbd79e2 2018-08-19 stsp return err;
1256 bcbd79e2 2018-08-19 stsp return update_diff_child_view(view, s->selected_entry, s->repo);
1259 e5a0f69f 2018-08-18 stsp static const struct got_error *
1260 e5a0f69f 2018-08-18 stsp input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1261 e5a0f69f 2018-08-18 stsp struct tog_view *view, int ch)
1263 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1264 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
1266 e5a0f69f 2018-08-18 stsp switch (ch) {
1268 e5a0f69f 2018-08-18 stsp case KEY_UP:
1270 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
1271 e5a0f69f 2018-08-18 stsp s->selected--;
1272 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
1274 e5a0f69f 2018-08-18 stsp scroll_up(&s->first_displayed_entry, 1,
1275 e5a0f69f 2018-08-18 stsp &s->commits);
1277 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
1278 e5a0f69f 2018-08-18 stsp if (TAILQ_FIRST(&s->commits.head) ==
1279 e5a0f69f 2018-08-18 stsp s->first_displayed_entry) {
1280 e5a0f69f 2018-08-18 stsp s->selected = 0;
1283 e5a0f69f 2018-08-18 stsp scroll_up(&s->first_displayed_entry,
1284 e5a0f69f 2018-08-18 stsp view->nlines, &s->commits);
1287 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
1289 e5a0f69f 2018-08-18 stsp if (s->selected < MIN(view->nlines - 2,
1290 e5a0f69f 2018-08-18 stsp s->commits.ncommits - 1)) {
1291 e5a0f69f 2018-08-18 stsp s->selected++;
1294 e5a0f69f 2018-08-18 stsp err = scroll_down(&s->first_displayed_entry, 1,
1295 e5a0f69f 2018-08-18 stsp s->last_displayed_entry, &s->commits,
1296 e5a0f69f 2018-08-18 stsp s->graph, s->repo, s->in_repo_path);
1297 e5a0f69f 2018-08-18 stsp if (err) {
1298 4d7951a5 2018-08-04 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
1300 4d7951a5 2018-08-04 stsp err = NULL;
1303 e5a0f69f 2018-08-18 stsp case KEY_NPAGE: {
1304 e5a0f69f 2018-08-18 stsp struct commit_queue_entry *first;
1305 e5a0f69f 2018-08-18 stsp first = s->first_displayed_entry;
1306 e5a0f69f 2018-08-18 stsp err = scroll_down(&s->first_displayed_entry,
1307 e5a0f69f 2018-08-18 stsp view->nlines, s->last_displayed_entry,
1308 e5a0f69f 2018-08-18 stsp &s->commits, s->graph, s->repo,
1309 e5a0f69f 2018-08-18 stsp s->in_repo_path);
1310 e5a0f69f 2018-08-18 stsp if (err == NULL)
1312 e5a0f69f 2018-08-18 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
1314 e5a0f69f 2018-08-18 stsp if (first == s->first_displayed_entry &&
1315 e5a0f69f 2018-08-18 stsp s->selected < MIN(view->nlines - 2,
1316 e5a0f69f 2018-08-18 stsp s->commits.ncommits - 1)) {
1317 e5a0f69f 2018-08-18 stsp /* can't scroll further down */
1318 e5a0f69f 2018-08-18 stsp s->selected = MIN(view->nlines - 2,
1319 e5a0f69f 2018-08-18 stsp s->commits.ncommits - 1);
1321 e5a0f69f 2018-08-18 stsp err = NULL;
1324 e5a0f69f 2018-08-18 stsp case KEY_RESIZE:
1325 e5a0f69f 2018-08-18 stsp if (s->selected > view->nlines - 2)
1326 e5a0f69f 2018-08-18 stsp s->selected = view->nlines - 2;
1327 e5a0f69f 2018-08-18 stsp if (s->selected > s->commits.ncommits - 1)
1328 e5a0f69f 2018-08-18 stsp s->selected = s->commits.ncommits - 1;
1330 e5a0f69f 2018-08-18 stsp case KEY_ENTER:
1331 e5a0f69f 2018-08-18 stsp case '\r':
1332 e5a0f69f 2018-08-18 stsp err = show_commit(new_view, view, s->selected_entry,
1336 e5a0f69f 2018-08-18 stsp err = browse_commit(new_view, view, s->selected_entry,
1343 80ddbec8 2018-04-29 stsp return err;
1346 4ed7e80c 2018-05-20 stsp static const struct got_error *
1347 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
1349 80ddbec8 2018-04-29 stsp const struct got_error *error;
1350 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
1351 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
1352 ecb28ae0 2018-07-16 stsp char *path = NULL, *repo_path = NULL, *cwd = NULL;
1353 80ddbec8 2018-04-29 stsp char *start_commit = NULL;
1355 04cc582a 2018-08-01 stsp struct tog_view *view;
1357 80ddbec8 2018-04-29 stsp #ifndef PROFILE
1358 80ddbec8 2018-04-29 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1359 80ddbec8 2018-04-29 stsp err(1, "pledge");
1362 ecb28ae0 2018-07-16 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1363 80ddbec8 2018-04-29 stsp switch (ch) {
1365 80ddbec8 2018-04-29 stsp start_commit = optarg;
1368 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
1369 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
1370 ecb28ae0 2018-07-16 stsp err(1, "-r option");
1374 80ddbec8 2018-04-29 stsp /* NOTREACHED */
1378 80ddbec8 2018-04-29 stsp argc -= optind;
1379 80ddbec8 2018-04-29 stsp argv += optind;
1381 ecb28ae0 2018-07-16 stsp if (argc == 0)
1382 ecb28ae0 2018-07-16 stsp path = strdup("");
1383 ecb28ae0 2018-07-16 stsp else if (argc == 1)
1384 ecb28ae0 2018-07-16 stsp path = strdup(argv[0]);
1386 80ddbec8 2018-04-29 stsp usage_log();
1387 ecb28ae0 2018-07-16 stsp if (path == NULL)
1388 ecb28ae0 2018-07-16 stsp return got_error_from_errno();
1390 ecb28ae0 2018-07-16 stsp cwd = getcwd(NULL, 0);
1391 ecb28ae0 2018-07-16 stsp if (cwd == NULL) {
1392 ecb28ae0 2018-07-16 stsp error = got_error_from_errno();
1393 ecb28ae0 2018-07-16 stsp goto done;
1395 ecb28ae0 2018-07-16 stsp if (repo_path == NULL) {
1396 ecb28ae0 2018-07-16 stsp repo_path = strdup(cwd);
1397 ecb28ae0 2018-07-16 stsp if (repo_path == NULL) {
1398 ecb28ae0 2018-07-16 stsp error = got_error_from_errno();
1399 ecb28ae0 2018-07-16 stsp goto done;
1403 80ddbec8 2018-04-29 stsp error = got_repo_open(&repo, repo_path);
1404 80ddbec8 2018-04-29 stsp if (error != NULL)
1405 ecb28ae0 2018-07-16 stsp goto done;
1407 80ddbec8 2018-04-29 stsp if (start_commit == NULL) {
1408 899d86c2 2018-05-10 stsp error = get_head_commit_id(&start_id, repo);
1409 80ddbec8 2018-04-29 stsp if (error != NULL)
1410 ecb28ae0 2018-07-16 stsp goto done;
1412 80ddbec8 2018-04-29 stsp struct got_object *obj;
1413 80ddbec8 2018-04-29 stsp error = got_object_open_by_id_str(&obj, repo, start_commit);
1414 80ddbec8 2018-04-29 stsp if (error == NULL) {
1415 899d86c2 2018-05-10 stsp start_id = got_object_get_id(obj);
1416 899d86c2 2018-05-10 stsp if (start_id == NULL)
1417 80ddbec8 2018-04-29 stsp error = got_error_from_errno();
1418 ecb28ae0 2018-07-16 stsp goto done;
1421 80ddbec8 2018-04-29 stsp if (error != NULL)
1422 ecb28ae0 2018-07-16 stsp goto done;
1424 b3665f43 2018-08-04 stsp view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_LOG);
1425 04cc582a 2018-08-01 stsp if (view == NULL) {
1426 04cc582a 2018-08-01 stsp error = got_error_from_errno();
1427 04cc582a 2018-08-01 stsp goto done;
1429 ba4f502b 2018-08-04 stsp error = open_log_view(view, start_id, repo, path);
1430 ba4f502b 2018-08-04 stsp if (error)
1431 ba4f502b 2018-08-04 stsp goto done;
1432 e5a0f69f 2018-08-18 stsp error = view_loop(view);
1434 ecb28ae0 2018-07-16 stsp free(repo_path);
1435 ecb28ae0 2018-07-16 stsp free(cwd);
1436 ecb28ae0 2018-07-16 stsp free(path);
1437 899d86c2 2018-05-10 stsp free(start_id);
1439 ecb28ae0 2018-07-16 stsp got_repo_close(repo);
1440 80ddbec8 2018-04-29 stsp return error;
1443 4ed7e80c 2018-05-20 stsp __dead static void
1444 9f7d7167 2018-04-29 stsp usage_diff(void)
1447 9f7d7167 2018-04-29 stsp fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1448 9f7d7167 2018-04-29 stsp getprogname());
1452 b304db33 2018-05-20 stsp static char *
1453 b304db33 2018-05-20 stsp parse_next_line(FILE *f, size_t *len)
1455 b304db33 2018-05-20 stsp char *line;
1456 b304db33 2018-05-20 stsp size_t linelen;
1457 b304db33 2018-05-20 stsp size_t lineno;
1458 b304db33 2018-05-20 stsp const char delim[3] = { '\0', '\0', '\0'};
1460 b304db33 2018-05-20 stsp line = fparseln(f, &linelen, &lineno, delim, 0);
1462 b304db33 2018-05-20 stsp *len = linelen;
1463 b304db33 2018-05-20 stsp return line;
1466 4ed7e80c 2018-05-20 stsp static const struct got_error *
1467 f7d12f7e 2018-08-01 stsp draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1468 a70480e0 2018-06-23 stsp int *last_displayed_line, int *eof, int max_lines)
1470 61e69b96 2018-05-20 stsp const struct got_error *err;
1471 26ed57b2 2018-05-19 stsp int nlines = 0, nprinted = 0;
1472 b304db33 2018-05-20 stsp char *line;
1473 b304db33 2018-05-20 stsp size_t len;
1474 61e69b96 2018-05-20 stsp wchar_t *wline;
1475 e0b650dd 2018-05-20 stsp int width;
1477 26ed57b2 2018-05-19 stsp rewind(f);
1478 f7d12f7e 2018-08-01 stsp werase(view->window);
1481 26ed57b2 2018-05-19 stsp while (nprinted < max_lines) {
1482 b304db33 2018-05-20 stsp line = parse_next_line(f, &len);
1483 26ed57b2 2018-05-19 stsp if (line == NULL) {
1487 26ed57b2 2018-05-19 stsp if (++nlines < *first_displayed_line) {
1488 26ed57b2 2018-05-19 stsp free(line);
1492 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
1493 61e69b96 2018-05-20 stsp if (err) {
1494 61e69b96 2018-05-20 stsp free(line);
1495 2550e4c3 2018-07-13 stsp free(wline);
1496 61e69b96 2018-05-20 stsp return err;
1498 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
1499 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
1500 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
1501 26ed57b2 2018-05-19 stsp if (++nprinted == 1)
1502 26ed57b2 2018-05-19 stsp *first_displayed_line = nlines;
1503 26ed57b2 2018-05-19 stsp free(line);
1504 2550e4c3 2018-07-13 stsp free(wline);
1505 2550e4c3 2018-07-13 stsp wline = NULL;
1507 26ed57b2 2018-05-19 stsp *last_displayed_line = nlines;
1509 26ed57b2 2018-05-19 stsp update_panels();
1511 26ed57b2 2018-05-19 stsp return NULL;
1514 4ed7e80c 2018-05-20 stsp static const struct got_error *
1515 5dc9f4bc 2018-08-04 stsp open_diff_view(struct tog_view *view, struct got_object *obj1,
1516 ea5e7bb5 2018-08-01 stsp struct got_object *obj2, struct got_repository *repo)
1518 26ed57b2 2018-05-19 stsp const struct got_error *err;
1521 cd0acaa7 2018-05-20 stsp if (obj1 != NULL && obj2 != NULL &&
1522 cd0acaa7 2018-05-20 stsp got_object_get_type(obj1) != got_object_get_type(obj2))
1523 26ed57b2 2018-05-19 stsp return got_error(GOT_ERR_OBJ_TYPE);
1525 511a516b 2018-05-19 stsp f = got_opentemp();
1526 26ed57b2 2018-05-19 stsp if (f == NULL)
1527 26ed57b2 2018-05-19 stsp return got_error_from_errno();
1529 cd0acaa7 2018-05-20 stsp switch (got_object_get_type(obj1 ? obj1 : obj2)) {
1530 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
1531 11528a82 2018-05-19 stsp err = got_diff_objects_as_blobs(obj1, obj2, repo, f);
1533 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
1534 11528a82 2018-05-19 stsp err = got_diff_objects_as_trees(obj1, obj2, repo, f);
1536 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_COMMIT:
1537 11528a82 2018-05-19 stsp err = got_diff_objects_as_commits(obj1, obj2, repo, f);
1540 26ed57b2 2018-05-19 stsp return got_error(GOT_ERR_OBJ_TYPE);
1543 26ed57b2 2018-05-19 stsp fflush(f);
1545 bcbd79e2 2018-08-19 stsp view->state.diff.id = got_object_get_id(obj2);
1546 bcbd79e2 2018-08-19 stsp if (view->state.diff.id == NULL)
1547 bcbd79e2 2018-08-19 stsp return got_error_from_errno();
1548 5dc9f4bc 2018-08-04 stsp view->state.diff.f = f;
1549 5dc9f4bc 2018-08-04 stsp view->state.diff.first_displayed_line = 1;
1550 5dc9f4bc 2018-08-04 stsp view->state.diff.last_displayed_line = view->nlines;
1552 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
1553 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
1554 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
1556 5dc9f4bc 2018-08-04 stsp return NULL;
1559 e5a0f69f 2018-08-18 stsp static const struct got_error *
1560 5dc9f4bc 2018-08-04 stsp close_diff_view(struct tog_view *view)
1562 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1564 e5a0f69f 2018-08-18 stsp if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
1565 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
1566 bcbd79e2 2018-08-19 stsp free(view->state.diff.id);
1567 e5a0f69f 2018-08-18 stsp return err;
1570 5dc9f4bc 2018-08-04 stsp static const struct got_error *
1571 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
1573 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
1575 e5a0f69f 2018-08-18 stsp return draw_file(view, s->f, &s->first_displayed_line,
1576 e5a0f69f 2018-08-18 stsp &s->last_displayed_line, &s->eof, view->nlines);
1579 e5a0f69f 2018-08-18 stsp static const struct got_error *
1580 bcbd79e2 2018-08-19 stsp input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
1581 e5a0f69f 2018-08-18 stsp struct tog_view *view, int ch)
1583 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
1584 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
1587 e5a0f69f 2018-08-18 stsp switch (ch) {
1589 e5a0f69f 2018-08-18 stsp case KEY_UP:
1590 e5a0f69f 2018-08-18 stsp if (s->first_displayed_line > 1)
1591 e5a0f69f 2018-08-18 stsp s->first_displayed_line--;
1593 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
1595 e5a0f69f 2018-08-18 stsp while (i++ < view->nlines - 1 &&
1596 e5a0f69f 2018-08-18 stsp s->first_displayed_line > 1)
1597 e5a0f69f 2018-08-18 stsp s->first_displayed_line--;
1600 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
1601 e5a0f69f 2018-08-18 stsp if (!s->eof)
1602 e5a0f69f 2018-08-18 stsp s->first_displayed_line++;
1604 e5a0f69f 2018-08-18 stsp case KEY_NPAGE:
1607 e5a0f69f 2018-08-18 stsp while (!s->eof && i++ < view->nlines - 1) {
1608 e5a0f69f 2018-08-18 stsp char *line;
1609 e5a0f69f 2018-08-18 stsp line = parse_next_line(s->f, NULL);
1610 e5a0f69f 2018-08-18 stsp s->first_displayed_line++;
1611 e5a0f69f 2018-08-18 stsp if (line == NULL)
1616 bcbd79e2 2018-08-19 stsp case ']': {
1617 bcbd79e2 2018-08-19 stsp struct tog_log_view_state *ls;
1618 bcbd79e2 2018-08-19 stsp struct commit_queue_entry *entry;
1619 bcbd79e2 2018-08-19 stsp struct tog_view *diff_view;
1621 bcbd79e2 2018-08-19 stsp if (view->parent == NULL)
1623 bcbd79e2 2018-08-19 stsp if (view->parent->type != TOG_VIEW_LOG)
1625 bcbd79e2 2018-08-19 stsp ls = &view->parent->state.log;
1627 bcbd79e2 2018-08-19 stsp if (ch == '[') {
1628 bcbd79e2 2018-08-19 stsp entry = TAILQ_PREV(ls->selected_entry,
1629 bcbd79e2 2018-08-19 stsp commit_queue_head, entry);
1631 bcbd79e2 2018-08-19 stsp entry = TAILQ_NEXT(ls->selected_entry, entry);
1632 bcbd79e2 2018-08-19 stsp if (entry == NULL) {
1633 bcbd79e2 2018-08-19 stsp err = fetch_next_commit(&entry,
1634 bcbd79e2 2018-08-19 stsp ls->selected_entry,
1635 bcbd79e2 2018-08-19 stsp &ls->commits, ls->graph,
1636 bcbd79e2 2018-08-19 stsp ls->repo, ls->in_repo_path);
1641 bcbd79e2 2018-08-19 stsp if (entry == NULL)
1643 bcbd79e2 2018-08-19 stsp err = show_commit(&diff_view, view->parent,
1644 bcbd79e2 2018-08-19 stsp entry, ls->repo);
1647 bcbd79e2 2018-08-19 stsp *new_view = diff_view;
1648 bcbd79e2 2018-08-19 stsp *dead_view = view;
1655 bcbd79e2 2018-08-19 stsp return err;
1658 4ed7e80c 2018-05-20 stsp static const struct got_error *
1659 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
1661 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
1662 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
1663 26ed57b2 2018-05-19 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
1664 26ed57b2 2018-05-19 stsp char *repo_path = NULL;
1665 26ed57b2 2018-05-19 stsp char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
1667 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
1669 26ed57b2 2018-05-19 stsp #ifndef PROFILE
1670 26ed57b2 2018-05-19 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1671 26ed57b2 2018-05-19 stsp err(1, "pledge");
1674 26ed57b2 2018-05-19 stsp while ((ch = getopt(argc, argv, "")) != -1) {
1675 26ed57b2 2018-05-19 stsp switch (ch) {
1678 26ed57b2 2018-05-19 stsp /* NOTREACHED */
1682 26ed57b2 2018-05-19 stsp argc -= optind;
1683 26ed57b2 2018-05-19 stsp argv += optind;
1685 26ed57b2 2018-05-19 stsp if (argc == 0) {
1686 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
1687 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
1688 26ed57b2 2018-05-19 stsp repo_path = getcwd(NULL, 0);
1689 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
1690 26ed57b2 2018-05-19 stsp return got_error_from_errno();
1691 26ed57b2 2018-05-19 stsp obj_id_str1 = argv[0];
1692 26ed57b2 2018-05-19 stsp obj_id_str2 = argv[1];
1693 26ed57b2 2018-05-19 stsp } else if (argc == 3) {
1694 26ed57b2 2018-05-19 stsp repo_path = realpath(argv[0], NULL);
1695 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
1696 26ed57b2 2018-05-19 stsp return got_error_from_errno();
1697 26ed57b2 2018-05-19 stsp obj_id_str1 = argv[1];
1698 26ed57b2 2018-05-19 stsp obj_id_str2 = argv[2];
1700 26ed57b2 2018-05-19 stsp usage_diff();
1702 26ed57b2 2018-05-19 stsp error = got_repo_open(&repo, repo_path);
1703 26ed57b2 2018-05-19 stsp free(repo_path);
1704 26ed57b2 2018-05-19 stsp if (error)
1705 26ed57b2 2018-05-19 stsp goto done;
1707 26ed57b2 2018-05-19 stsp error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
1708 26ed57b2 2018-05-19 stsp if (error)
1709 26ed57b2 2018-05-19 stsp goto done;
1711 26ed57b2 2018-05-19 stsp error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1712 26ed57b2 2018-05-19 stsp if (error)
1713 26ed57b2 2018-05-19 stsp goto done;
1715 b3665f43 2018-08-04 stsp view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_DIFF);
1716 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
1717 ea5e7bb5 2018-08-01 stsp error = got_error_from_errno();
1718 ea5e7bb5 2018-08-01 stsp goto done;
1720 5dc9f4bc 2018-08-04 stsp error = open_diff_view(view, obj1, obj2, repo);
1721 5dc9f4bc 2018-08-04 stsp if (error)
1722 5dc9f4bc 2018-08-04 stsp goto done;
1723 e5a0f69f 2018-08-18 stsp error = view_loop(view);
1725 26ed57b2 2018-05-19 stsp got_repo_close(repo);
1727 26ed57b2 2018-05-19 stsp got_object_close(obj1);
1729 26ed57b2 2018-05-19 stsp got_object_close(obj2);
1730 26ed57b2 2018-05-19 stsp return error;
1733 4ed7e80c 2018-05-20 stsp __dead static void
1734 9f7d7167 2018-04-29 stsp usage_blame(void)
1737 69069811 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
1738 9f7d7167 2018-04-29 stsp getprogname());
1742 84451b3e 2018-07-10 stsp struct tog_blame_line {
1743 84451b3e 2018-07-10 stsp int annotated;
1744 84451b3e 2018-07-10 stsp struct got_object_id *id;
1747 4ed7e80c 2018-05-20 stsp static const struct got_error *
1748 f7d12f7e 2018-08-01 stsp draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
1749 f7d12f7e 2018-08-01 stsp const char *path, struct tog_blame_line *lines, int nlines,
1750 f7d12f7e 2018-08-01 stsp int blame_complete, int selected_line, int *first_displayed_line,
1751 f7d12f7e 2018-08-01 stsp int *last_displayed_line, int *eof, int max_lines)
1753 84451b3e 2018-07-10 stsp const struct got_error *err;
1754 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
1755 84451b3e 2018-07-10 stsp char *line;
1756 84451b3e 2018-07-10 stsp size_t len;
1757 84451b3e 2018-07-10 stsp wchar_t *wline;
1758 b700b5d6 2018-07-10 stsp int width, wlimit;
1759 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
1760 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
1761 ab089a2a 2018-07-12 stsp char *id_str;
1763 ab089a2a 2018-07-12 stsp err = got_object_id_str(&id_str, id);
1765 ab089a2a 2018-07-12 stsp return err;
1767 84451b3e 2018-07-10 stsp rewind(f);
1768 f7d12f7e 2018-08-01 stsp werase(view->window);
1770 ab089a2a 2018-07-12 stsp if (asprintf(&line, "commit: %s", id_str) == -1) {
1771 ab089a2a 2018-07-12 stsp err = got_error_from_errno();
1772 ab089a2a 2018-07-12 stsp free(id_str);
1773 ab089a2a 2018-07-12 stsp return err;
1776 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
1777 ab089a2a 2018-07-12 stsp free(line);
1778 2550e4c3 2018-07-13 stsp line = NULL;
1779 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
1780 2550e4c3 2018-07-13 stsp free(wline);
1781 2550e4c3 2018-07-13 stsp wline = NULL;
1782 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
1783 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
1785 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
1786 084063cd 2018-07-12 stsp *first_displayed_line - 1 + selected_line, nlines,
1787 12a0b23b 2018-07-12 stsp blame_complete ? "" : "annotating ", path) == -1) {
1788 ab089a2a 2018-07-12 stsp free(id_str);
1789 3f60a8ef 2018-07-10 stsp return got_error_from_errno();
1791 ab089a2a 2018-07-12 stsp free(id_str);
1792 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
1793 3f60a8ef 2018-07-10 stsp free(line);
1794 2550e4c3 2018-07-13 stsp line = NULL;
1796 3f60a8ef 2018-07-10 stsp return err;
1797 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
1798 2550e4c3 2018-07-13 stsp free(wline);
1799 2550e4c3 2018-07-13 stsp wline = NULL;
1800 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
1801 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
1804 ab089a2a 2018-07-12 stsp while (nprinted < max_lines - 2) {
1805 84451b3e 2018-07-10 stsp line = parse_next_line(f, &len);
1806 84451b3e 2018-07-10 stsp if (line == NULL) {
1810 84451b3e 2018-07-10 stsp if (++lineno < *first_displayed_line) {
1811 84451b3e 2018-07-10 stsp free(line);
1815 f7d12f7e 2018-08-01 stsp wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
1816 b700b5d6 2018-07-10 stsp err = format_line(&wline, &width, line, wlimit);
1817 84451b3e 2018-07-10 stsp if (err) {
1818 84451b3e 2018-07-10 stsp free(line);
1819 84451b3e 2018-07-10 stsp return err;
1822 b700b5d6 2018-07-10 stsp if (nprinted == selected_line - 1)
1823 f7d12f7e 2018-08-01 stsp wstandout(view->window);
1825 84451b3e 2018-07-10 stsp blame_line = &lines[lineno - 1];
1826 ee41ec32 2018-07-10 stsp if (blame_line->annotated && prev_id &&
1827 ee41ec32 2018-07-10 stsp got_object_id_cmp(prev_id, blame_line->id) == 0)
1828 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ");
1829 ee41ec32 2018-07-10 stsp else if (blame_line->annotated) {
1830 84451b3e 2018-07-10 stsp char *id_str;
1831 84451b3e 2018-07-10 stsp err = got_object_id_str(&id_str, blame_line->id);
1832 84451b3e 2018-07-10 stsp if (err) {
1833 84451b3e 2018-07-10 stsp free(line);
1834 2550e4c3 2018-07-13 stsp free(wline);
1835 84451b3e 2018-07-10 stsp return err;
1837 f7d12f7e 2018-08-01 stsp wprintw(view->window, "%.8s ", id_str);
1838 84451b3e 2018-07-10 stsp free(id_str);
1839 ee41ec32 2018-07-10 stsp prev_id = blame_line->id;
1841 f7d12f7e 2018-08-01 stsp waddstr(view->window, "........ ");
1842 ee41ec32 2018-07-10 stsp prev_id = NULL;
1845 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
1846 b700b5d6 2018-07-10 stsp while (width < wlimit) {
1847 f7d12f7e 2018-08-01 stsp waddch(view->window, ' ');
1850 b700b5d6 2018-07-10 stsp if (nprinted == selected_line - 1)
1851 f7d12f7e 2018-08-01 stsp wstandend(view->window);
1852 84451b3e 2018-07-10 stsp if (++nprinted == 1)
1853 84451b3e 2018-07-10 stsp *first_displayed_line = lineno;
1854 84451b3e 2018-07-10 stsp free(line);
1855 2550e4c3 2018-07-13 stsp free(wline);
1856 2550e4c3 2018-07-13 stsp wline = NULL;
1858 84451b3e 2018-07-10 stsp *last_displayed_line = lineno;
1860 84451b3e 2018-07-10 stsp update_panels();
1862 84451b3e 2018-07-10 stsp return NULL;
1865 84451b3e 2018-07-10 stsp static const struct got_error *
1866 84451b3e 2018-07-10 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
1868 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
1869 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
1870 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
1872 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
1873 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
1874 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
1876 84451b3e 2018-07-10 stsp if (pthread_mutex_lock(a->mutex) != 0)
1877 84451b3e 2018-07-10 stsp return got_error_from_errno();
1879 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
1880 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
1881 d68a0a7d 2018-07-10 stsp goto done;
1884 d68a0a7d 2018-07-10 stsp if (lineno == -1)
1885 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
1887 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
1888 d68a0a7d 2018-07-10 stsp if (line->annotated)
1889 d68a0a7d 2018-07-10 stsp goto done;
1891 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
1892 84451b3e 2018-07-10 stsp if (line->id == NULL) {
1893 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1894 84451b3e 2018-07-10 stsp goto done;
1896 84451b3e 2018-07-10 stsp line->annotated = 1;
1898 f7d12f7e 2018-08-01 stsp err = draw_blame(a->view, a->commit_id, a->f, a->path,
1899 245d91c1 2018-07-12 stsp a->lines, a->nlines, 0, *a->selected_line, a->first_displayed_line,
1900 e5a0f69f 2018-08-18 stsp a->last_displayed_line, a->eof, a->view->nlines);
1902 84451b3e 2018-07-10 stsp if (pthread_mutex_unlock(a->mutex) != 0)
1903 84451b3e 2018-07-10 stsp return got_error_from_errno();
1904 84451b3e 2018-07-10 stsp return err;
1907 84451b3e 2018-07-10 stsp static void *
1908 84451b3e 2018-07-10 stsp blame_thread(void *arg)
1910 18430de3 2018-07-10 stsp const struct got_error *err;
1911 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
1912 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
1914 ab089a2a 2018-07-12 stsp err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
1915 245d91c1 2018-07-12 stsp blame_cb, ta->cb_args);
1917 18430de3 2018-07-10 stsp if (pthread_mutex_lock(a->mutex) != 0)
1918 18430de3 2018-07-10 stsp return (void *)got_error_from_errno();
1920 c9beca56 2018-07-22 stsp got_repo_close(ta->repo);
1921 c9beca56 2018-07-22 stsp ta->repo = NULL;
1922 c9beca56 2018-07-22 stsp *ta->complete = 1;
1924 f7d12f7e 2018-08-01 stsp err = draw_blame(a->view, a->commit_id, a->f, a->path,
1925 f7d12f7e 2018-08-01 stsp a->lines, a->nlines, 1, *a->selected_line,
1926 e5a0f69f 2018-08-18 stsp a->first_displayed_line, a->last_displayed_line, a->eof,
1927 f7d12f7e 2018-08-01 stsp a->view->nlines);
1929 18430de3 2018-07-10 stsp if (pthread_mutex_unlock(a->mutex) != 0 && err == NULL)
1930 18430de3 2018-07-10 stsp err = got_error_from_errno();
1932 18430de3 2018-07-10 stsp return (void *)err;
1935 245d91c1 2018-07-12 stsp static struct got_object_id *
1936 245d91c1 2018-07-12 stsp get_selected_commit_id(struct tog_blame_line *lines,
1937 245d91c1 2018-07-12 stsp int first_displayed_line, int selected_line)
1939 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
1941 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
1942 245d91c1 2018-07-12 stsp if (!line->annotated)
1943 245d91c1 2018-07-12 stsp return NULL;
1945 245d91c1 2018-07-12 stsp return line->id;
1948 84451b3e 2018-07-10 stsp static const struct got_error *
1949 245d91c1 2018-07-12 stsp open_selected_commit(struct got_object **pobj, struct got_object **obj,
1950 b880a918 2018-07-10 stsp struct tog_blame_line *lines, int first_displayed_line,
1951 b880a918 2018-07-10 stsp int selected_line, struct got_repository *repo)
1953 b880a918 2018-07-10 stsp const struct got_error *err = NULL;
1954 b880a918 2018-07-10 stsp struct got_commit_object *commit = NULL;
1955 245d91c1 2018-07-12 stsp struct got_object_id *selected_id;
1956 b880a918 2018-07-10 stsp struct got_object_qid *pid;
1958 b880a918 2018-07-10 stsp *pobj = NULL;
1959 b880a918 2018-07-10 stsp *obj = NULL;
1961 245d91c1 2018-07-12 stsp selected_id = get_selected_commit_id(lines,
1962 245d91c1 2018-07-12 stsp first_displayed_line, selected_line);
1963 245d91c1 2018-07-12 stsp if (selected_id == NULL)
1964 b880a918 2018-07-10 stsp return NULL;
1966 245d91c1 2018-07-12 stsp err = got_object_open(obj, repo, selected_id);
1968 b880a918 2018-07-10 stsp goto done;
1970 b880a918 2018-07-10 stsp err = got_object_commit_open(&commit, repo, *obj);
1972 b880a918 2018-07-10 stsp goto done;
1974 b880a918 2018-07-10 stsp pid = SIMPLEQ_FIRST(&commit->parent_ids);
1975 b880a918 2018-07-10 stsp if (pid) {
1976 b880a918 2018-07-10 stsp err = got_object_open(pobj, repo, pid->id);
1978 b880a918 2018-07-10 stsp goto done;
1981 b880a918 2018-07-10 stsp if (commit)
1982 b880a918 2018-07-10 stsp got_object_commit_close(commit);
1983 b880a918 2018-07-10 stsp return err;
1986 b880a918 2018-07-10 stsp static const struct got_error *
1987 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
1989 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
1992 245d91c1 2018-07-12 stsp if (blame->thread) {
1993 245d91c1 2018-07-12 stsp if (pthread_join(blame->thread, (void **)&err) != 0)
1994 245d91c1 2018-07-12 stsp err = got_error_from_errno();
1995 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
1996 245d91c1 2018-07-12 stsp err = NULL;
1997 245d91c1 2018-07-12 stsp blame->thread = NULL;
1999 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
2000 245d91c1 2018-07-12 stsp got_repo_close(blame->thread_args.repo);
2001 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
2003 245d91c1 2018-07-12 stsp if (blame->f) {
2004 245d91c1 2018-07-12 stsp fclose(blame->f);
2005 245d91c1 2018-07-12 stsp blame->f = NULL;
2007 245d91c1 2018-07-12 stsp for (i = 0; i < blame->nlines; i++)
2008 245d91c1 2018-07-12 stsp free(blame->lines[i].id);
2009 245d91c1 2018-07-12 stsp free(blame->lines);
2010 245d91c1 2018-07-12 stsp blame->lines = NULL;
2011 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
2012 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
2014 245d91c1 2018-07-12 stsp return err;
2017 245d91c1 2018-07-12 stsp static const struct got_error *
2018 7cc84d77 2018-08-01 stsp run_blame(struct tog_blame *blame, pthread_mutex_t *mutex,
2019 7cc84d77 2018-08-01 stsp struct tog_view *view, int *blame_complete,
2020 245d91c1 2018-07-12 stsp int *first_displayed_line, int *last_displayed_line,
2021 e5a0f69f 2018-08-18 stsp int *selected_line, int *done, int *eof, const char *path,
2022 245d91c1 2018-07-12 stsp struct got_object_id *commit_id,
2023 245d91c1 2018-07-12 stsp struct got_repository *repo)
2025 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
2026 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
2027 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
2028 245d91c1 2018-07-12 stsp struct got_object *obj;
2030 84451b3e 2018-07-10 stsp err = got_object_open_by_path(&obj, repo, commit_id, path);
2032 84451b3e 2018-07-10 stsp goto done;
2033 84451b3e 2018-07-10 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
2034 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
2035 84451b3e 2018-07-10 stsp goto done;
2038 84451b3e 2018-07-10 stsp err = got_object_blob_open(&blob, repo, obj, 8192);
2040 a70480e0 2018-06-23 stsp goto done;
2041 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
2042 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
2043 84451b3e 2018-07-10 stsp err = got_error_from_errno();
2044 84451b3e 2018-07-10 stsp goto done;
2046 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2047 245d91c1 2018-07-12 stsp blame->f, blob);
2049 84451b3e 2018-07-10 stsp goto done;
2051 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2052 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
2053 84451b3e 2018-07-10 stsp err = got_error_from_errno();
2054 84451b3e 2018-07-10 stsp goto done;
2057 245d91c1 2018-07-12 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2059 bd24772e 2018-07-11 stsp goto done;
2061 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
2062 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
2063 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
2064 245d91c1 2018-07-12 stsp blame->cb_args.mutex = mutex;
2065 245d91c1 2018-07-12 stsp blame->cb_args.commit_id = got_object_id_dup(commit_id);
2066 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
2067 245d91c1 2018-07-12 stsp err = got_error_from_errno();
2068 245d91c1 2018-07-12 stsp goto done;
2070 245d91c1 2018-07-12 stsp blame->cb_args.f = blame->f;
2071 245d91c1 2018-07-12 stsp blame->cb_args.path = path;
2072 245d91c1 2018-07-12 stsp blame->cb_args.first_displayed_line = first_displayed_line;
2073 245d91c1 2018-07-12 stsp blame->cb_args.selected_line = selected_line;
2074 245d91c1 2018-07-12 stsp blame->cb_args.last_displayed_line = last_displayed_line;
2075 245d91c1 2018-07-12 stsp blame->cb_args.quit = done;
2076 e5a0f69f 2018-08-18 stsp blame->cb_args.eof = eof;
2078 245d91c1 2018-07-12 stsp blame->thread_args.path = path;
2079 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
2080 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
2081 245d91c1 2018-07-12 stsp blame->thread_args.complete = blame_complete;
2082 245d91c1 2018-07-12 stsp *blame_complete = 0;
2084 245d91c1 2018-07-12 stsp if (pthread_create(&blame->thread, NULL, blame_thread,
2085 245d91c1 2018-07-12 stsp &blame->thread_args) != 0) {
2086 245d91c1 2018-07-12 stsp err = got_error_from_errno();
2087 245d91c1 2018-07-12 stsp goto done;
2092 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
2094 245d91c1 2018-07-12 stsp got_object_close(obj);
2096 245d91c1 2018-07-12 stsp stop_blame(blame);
2097 245d91c1 2018-07-12 stsp return err;
2100 245d91c1 2018-07-12 stsp static const struct got_error *
2101 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
2102 e1cd8fed 2018-08-01 stsp struct got_object_id *commit_id, struct got_repository *repo)
2104 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
2105 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
2107 fb2756b9 2018-08-04 stsp SIMPLEQ_INIT(&s->blamed_commits);
2109 fb2756b9 2018-08-04 stsp if (pthread_mutex_init(&s->mutex, NULL) != 0)
2110 7cbe629d 2018-08-04 stsp return got_error_from_errno();
2112 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2114 7cbe629d 2018-08-04 stsp return err;
2116 fb2756b9 2018-08-04 stsp SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2117 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
2118 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
2119 fb2756b9 2018-08-04 stsp s->selected_line = 1;
2120 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
2121 fb2756b9 2018-08-04 stsp s->path = path;
2122 e5a0f69f 2018-08-18 stsp if (s->path == NULL)
2123 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
2124 fb2756b9 2018-08-04 stsp s->repo = repo;
2125 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
2126 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
2128 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
2129 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
2130 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
2132 e5a0f69f 2018-08-18 stsp return run_blame(&s->blame, &s->mutex, view, &s->blame_complete,
2133 e5a0f69f 2018-08-18 stsp &s->first_displayed_line, &s->last_displayed_line,
2134 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof, s->path,
2135 e5a0f69f 2018-08-18 stsp s->blamed_commit->id, s->repo);
2138 e5a0f69f 2018-08-18 stsp static const struct got_error *
2139 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
2141 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2142 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
2144 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
2145 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
2147 fb2756b9 2018-08-04 stsp while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2148 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
2149 fb2756b9 2018-08-04 stsp blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2150 fb2756b9 2018-08-04 stsp SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2151 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
2154 e5a0f69f 2018-08-18 stsp free(s->path);
2156 e5a0f69f 2018-08-18 stsp return err;
2159 7cbe629d 2018-08-04 stsp static const struct got_error *
2160 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
2162 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2163 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
2165 e5a0f69f 2018-08-18 stsp if (pthread_mutex_lock(&s->mutex) != 0)
2166 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
2168 e5a0f69f 2018-08-18 stsp err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2169 e5a0f69f 2018-08-18 stsp s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2170 e5a0f69f 2018-08-18 stsp s->selected_line, &s->first_displayed_line,
2171 e5a0f69f 2018-08-18 stsp &s->last_displayed_line, &s->eof, view->nlines);
2173 e5a0f69f 2018-08-18 stsp if (pthread_mutex_unlock(&s->mutex) != 0 && err == NULL)
2174 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2176 e5a0f69f 2018-08-18 stsp return err;
2179 e5a0f69f 2018-08-18 stsp static const struct got_error *
2180 e5a0f69f 2018-08-18 stsp input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2181 e5a0f69f 2018-08-18 stsp struct tog_view *view, int ch)
2183 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
2184 7cbe629d 2018-08-04 stsp struct got_object *obj = NULL, *pobj = NULL;
2185 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
2186 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
2188 e5a0f69f 2018-08-18 stsp if (pthread_mutex_lock(&s->mutex) != 0) {
2189 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2190 e5a0f69f 2018-08-18 stsp goto done;
2193 e5a0f69f 2018-08-18 stsp switch (ch) {
2195 e5a0f69f 2018-08-18 stsp s->done = 1;
2196 e5a0f69f 2018-08-18 stsp if (pthread_mutex_unlock(&s->mutex) != 0) {
2197 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2198 e5a0f69f 2018-08-18 stsp goto done;
2200 e5a0f69f 2018-08-18 stsp return stop_blame(&s->blame);
2202 e5a0f69f 2018-08-18 stsp case KEY_UP:
2203 e5a0f69f 2018-08-18 stsp if (s->selected_line > 1)
2204 e5a0f69f 2018-08-18 stsp s->selected_line--;
2205 e5a0f69f 2018-08-18 stsp else if (s->selected_line == 1 &&
2206 e5a0f69f 2018-08-18 stsp s->first_displayed_line > 1)
2207 e5a0f69f 2018-08-18 stsp s->first_displayed_line--;
2209 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
2210 e5a0f69f 2018-08-18 stsp if (s->first_displayed_line == 1) {
2211 e5a0f69f 2018-08-18 stsp s->selected_line = 1;
2214 e5a0f69f 2018-08-18 stsp if (s->first_displayed_line > view->nlines - 2)
2215 e5a0f69f 2018-08-18 stsp s->first_displayed_line -=
2216 e5a0f69f 2018-08-18 stsp (view->nlines - 2);
2218 e5a0f69f 2018-08-18 stsp s->first_displayed_line = 1;
2221 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
2222 e5a0f69f 2018-08-18 stsp if (s->selected_line < view->nlines - 2 &&
2223 e5a0f69f 2018-08-18 stsp s->first_displayed_line +
2224 e5a0f69f 2018-08-18 stsp s->selected_line <= s->blame.nlines)
2225 e5a0f69f 2018-08-18 stsp s->selected_line++;
2226 e5a0f69f 2018-08-18 stsp else if (s->last_displayed_line <
2227 e5a0f69f 2018-08-18 stsp s->blame.nlines)
2228 e5a0f69f 2018-08-18 stsp s->first_displayed_line++;
2231 e5a0f69f 2018-08-18 stsp case 'p': {
2232 e5a0f69f 2018-08-18 stsp struct got_object_id *id;
2233 e5a0f69f 2018-08-18 stsp id = get_selected_commit_id(s->blame.lines,
2234 e5a0f69f 2018-08-18 stsp s->first_displayed_line, s->selected_line);
2235 e5a0f69f 2018-08-18 stsp if (id == NULL || got_object_id_cmp(id,
2236 e5a0f69f 2018-08-18 stsp s->blamed_commit->id) == 0)
2238 e5a0f69f 2018-08-18 stsp err = open_selected_commit(&pobj, &obj,
2239 e5a0f69f 2018-08-18 stsp s->blame.lines, s->first_displayed_line,
2240 e5a0f69f 2018-08-18 stsp s->selected_line, s->repo);
2243 e5a0f69f 2018-08-18 stsp if (pobj == NULL && obj == NULL)
2245 e5a0f69f 2018-08-18 stsp if (ch == 'p' && pobj == NULL)
2247 e5a0f69f 2018-08-18 stsp s->done = 1;
2248 e5a0f69f 2018-08-18 stsp if (pthread_mutex_unlock(&s->mutex) != 0) {
2249 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2250 e5a0f69f 2018-08-18 stsp goto done;
2252 e5a0f69f 2018-08-18 stsp thread_err = stop_blame(&s->blame);
2253 e5a0f69f 2018-08-18 stsp s->done = 0;
2254 e5a0f69f 2018-08-18 stsp if (pthread_mutex_lock(&s->mutex) != 0) {
2255 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2256 e5a0f69f 2018-08-18 stsp goto done;
2258 e5a0f69f 2018-08-18 stsp if (thread_err)
2260 e5a0f69f 2018-08-18 stsp id = got_object_get_id(ch == 'b' ? obj : pobj);
2261 e5a0f69f 2018-08-18 stsp got_object_close(obj);
2262 e5a0f69f 2018-08-18 stsp obj = NULL;
2263 e5a0f69f 2018-08-18 stsp if (pobj) {
2264 e5a0f69f 2018-08-18 stsp got_object_close(pobj);
2265 e5a0f69f 2018-08-18 stsp pobj = NULL;
2267 e5a0f69f 2018-08-18 stsp if (id == NULL) {
2268 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2271 e5a0f69f 2018-08-18 stsp err = got_object_qid_alloc(
2272 e5a0f69f 2018-08-18 stsp &s->blamed_commit, id);
2275 e5a0f69f 2018-08-18 stsp goto done;
2276 e5a0f69f 2018-08-18 stsp SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
2277 e5a0f69f 2018-08-18 stsp s->blamed_commit, entry);
2278 e5a0f69f 2018-08-18 stsp err = run_blame(&s->blame, &s->mutex, view,
2279 e5a0f69f 2018-08-18 stsp &s->blame_complete,
2280 e5a0f69f 2018-08-18 stsp &s->first_displayed_line,
2281 e5a0f69f 2018-08-18 stsp &s->last_displayed_line,
2282 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof,
2283 e5a0f69f 2018-08-18 stsp s->path, s->blamed_commit->id, s->repo);
2288 e5a0f69f 2018-08-18 stsp case 'B': {
2289 e5a0f69f 2018-08-18 stsp struct got_object_qid *first;
2290 e5a0f69f 2018-08-18 stsp first = SIMPLEQ_FIRST(&s->blamed_commits);
2291 e5a0f69f 2018-08-18 stsp if (!got_object_id_cmp(first->id, s->commit_id))
2293 e5a0f69f 2018-08-18 stsp s->done = 1;
2294 e5a0f69f 2018-08-18 stsp if (pthread_mutex_unlock(&s->mutex) != 0) {
2295 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2296 e5a0f69f 2018-08-18 stsp goto done;
2298 e5a0f69f 2018-08-18 stsp thread_err = stop_blame(&s->blame);
2299 e5a0f69f 2018-08-18 stsp s->done = 0;
2300 e5a0f69f 2018-08-18 stsp if (pthread_mutex_lock(&s->mutex) != 0) {
2301 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2302 e5a0f69f 2018-08-18 stsp goto done;
2304 e5a0f69f 2018-08-18 stsp if (thread_err)
2306 e5a0f69f 2018-08-18 stsp SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2307 e5a0f69f 2018-08-18 stsp got_object_qid_free(s->blamed_commit);
2308 e5a0f69f 2018-08-18 stsp s->blamed_commit =
2309 e5a0f69f 2018-08-18 stsp SIMPLEQ_FIRST(&s->blamed_commits);
2310 e5a0f69f 2018-08-18 stsp err = run_blame(&s->blame, &s->mutex, view,
2311 e5a0f69f 2018-08-18 stsp &s->blame_complete,
2312 e5a0f69f 2018-08-18 stsp &s->first_displayed_line,
2313 e5a0f69f 2018-08-18 stsp &s->last_displayed_line,
2314 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof, s->path,
2315 e5a0f69f 2018-08-18 stsp s->blamed_commit->id, s->repo);
2320 e5a0f69f 2018-08-18 stsp case KEY_ENTER:
2321 e5a0f69f 2018-08-18 stsp case '\r':
2322 e5a0f69f 2018-08-18 stsp err = open_selected_commit(&pobj, &obj,
2323 e5a0f69f 2018-08-18 stsp s->blame.lines, s->first_displayed_line,
2324 e5a0f69f 2018-08-18 stsp s->selected_line, s->repo);
2327 e5a0f69f 2018-08-18 stsp if (pobj == NULL && obj == NULL)
2329 e5a0f69f 2018-08-18 stsp diff_view = view_open(0, 0, 0, 0, view,
2330 e5a0f69f 2018-08-18 stsp TOG_VIEW_DIFF);
2331 e5a0f69f 2018-08-18 stsp if (diff_view == NULL) {
2332 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2335 e5a0f69f 2018-08-18 stsp err = open_diff_view(diff_view, pobj, obj,
2337 e5a0f69f 2018-08-18 stsp if (err) {
2338 e5a0f69f 2018-08-18 stsp view_close(diff_view);
2341 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
2342 e5a0f69f 2018-08-18 stsp if (pobj) {
2343 e5a0f69f 2018-08-18 stsp got_object_close(pobj);
2344 e5a0f69f 2018-08-18 stsp pobj = NULL;
2346 e5a0f69f 2018-08-18 stsp got_object_close(obj);
2347 e5a0f69f 2018-08-18 stsp obj = NULL;
2351 e5a0f69f 2018-08-18 stsp case KEY_NPAGE:
2353 e5a0f69f 2018-08-18 stsp if (s->last_displayed_line >= s->blame.nlines &&
2354 e5a0f69f 2018-08-18 stsp s->selected_line < view->nlines - 2) {
2355 e5a0f69f 2018-08-18 stsp s->selected_line = MIN(s->blame.nlines,
2356 e5a0f69f 2018-08-18 stsp view->nlines - 2);
2359 e5a0f69f 2018-08-18 stsp if (s->last_displayed_line + view->nlines - 2
2360 e5a0f69f 2018-08-18 stsp <= s->blame.nlines)
2361 e5a0f69f 2018-08-18 stsp s->first_displayed_line +=
2362 e5a0f69f 2018-08-18 stsp view->nlines - 2;
2364 e5a0f69f 2018-08-18 stsp s->first_displayed_line =
2365 e5a0f69f 2018-08-18 stsp s->blame.nlines -
2366 e5a0f69f 2018-08-18 stsp (view->nlines - 3);
2368 e5a0f69f 2018-08-18 stsp case KEY_RESIZE:
2369 e5a0f69f 2018-08-18 stsp if (s->selected_line > view->nlines - 2) {
2370 e5a0f69f 2018-08-18 stsp s->selected_line = MIN(s->blame.nlines,
2371 e5a0f69f 2018-08-18 stsp view->nlines - 2);
2378 e5a0f69f 2018-08-18 stsp if (pthread_mutex_unlock(&s->mutex) != 0)
2379 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2382 b880a918 2018-07-10 stsp got_object_close(pobj);
2383 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
2386 a70480e0 2018-06-23 stsp static const struct got_error *
2387 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
2389 a70480e0 2018-06-23 stsp const struct got_error *error;
2390 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
2391 69069811 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2392 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
2393 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
2395 e1cd8fed 2018-08-01 stsp struct tog_view *view;
2397 a70480e0 2018-06-23 stsp #ifndef PROFILE
2398 a70480e0 2018-06-23 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
2399 a70480e0 2018-06-23 stsp err(1, "pledge");
2402 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2403 a70480e0 2018-06-23 stsp switch (ch) {
2405 a70480e0 2018-06-23 stsp commit_id_str = optarg;
2408 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
2409 69069811 2018-08-02 stsp if (repo_path == NULL)
2410 69069811 2018-08-02 stsp err(1, "-r option");
2414 a70480e0 2018-06-23 stsp /* NOTREACHED */
2418 a70480e0 2018-06-23 stsp argc -= optind;
2419 a70480e0 2018-06-23 stsp argv += optind;
2421 69069811 2018-08-02 stsp if (argc == 1)
2422 69069811 2018-08-02 stsp path = argv[0];
2424 a70480e0 2018-06-23 stsp usage_blame();
2426 69069811 2018-08-02 stsp cwd = getcwd(NULL, 0);
2427 69069811 2018-08-02 stsp if (cwd == NULL) {
2428 69069811 2018-08-02 stsp error = got_error_from_errno();
2429 69069811 2018-08-02 stsp goto done;
2431 69069811 2018-08-02 stsp if (repo_path == NULL) {
2432 69069811 2018-08-02 stsp repo_path = strdup(cwd);
2433 69069811 2018-08-02 stsp if (repo_path == NULL) {
2434 69069811 2018-08-02 stsp error = got_error_from_errno();
2435 69069811 2018-08-02 stsp goto done;
2440 69069811 2018-08-02 stsp error = got_repo_open(&repo, repo_path);
2441 a70480e0 2018-06-23 stsp if (error != NULL)
2442 66b4983c 2018-06-23 stsp return error;
2444 69069811 2018-08-02 stsp error = got_repo_map_path(&in_repo_path, repo, path);
2445 69069811 2018-08-02 stsp if (error != NULL)
2446 69069811 2018-08-02 stsp goto done;
2448 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
2449 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
2450 a70480e0 2018-06-23 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
2451 a70480e0 2018-06-23 stsp if (error != NULL)
2452 66b4983c 2018-06-23 stsp goto done;
2453 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2454 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
2456 a70480e0 2018-06-23 stsp struct got_object *obj;
2457 a70480e0 2018-06-23 stsp error = got_object_open_by_id_str(&obj, repo, commit_id_str);
2458 a70480e0 2018-06-23 stsp if (error != NULL)
2459 66b4983c 2018-06-23 stsp goto done;
2460 a70480e0 2018-06-23 stsp commit_id = got_object_get_id(obj);
2461 a19e88aa 2018-06-23 stsp if (commit_id == NULL)
2462 66b4983c 2018-06-23 stsp error = got_error_from_errno();
2463 a19e88aa 2018-06-23 stsp got_object_close(obj);
2465 a19e88aa 2018-06-23 stsp if (error != NULL)
2466 a19e88aa 2018-06-23 stsp goto done;
2468 b3665f43 2018-08-04 stsp view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_BLAME);
2469 e1cd8fed 2018-08-01 stsp if (view == NULL) {
2470 e1cd8fed 2018-08-01 stsp error = got_error_from_errno();
2471 e1cd8fed 2018-08-01 stsp goto done;
2473 7cbe629d 2018-08-04 stsp error = open_blame_view(view, in_repo_path, commit_id, repo);
2474 7cbe629d 2018-08-04 stsp if (error)
2475 7cbe629d 2018-08-04 stsp goto done;
2476 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2478 69069811 2018-08-02 stsp free(repo_path);
2479 69069811 2018-08-02 stsp free(cwd);
2480 a70480e0 2018-06-23 stsp free(commit_id);
2482 a70480e0 2018-06-23 stsp got_repo_close(repo);
2483 a70480e0 2018-06-23 stsp return error;
2486 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2487 f7d12f7e 2018-08-01 stsp draw_tree_entries(struct tog_view *view,
2488 f7d12f7e 2018-08-01 stsp struct got_tree_entry **first_displayed_entry,
2489 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **last_displayed_entry,
2490 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **selected_entry, int *ndisplayed,
2491 f7d12f7e 2018-08-01 stsp const char *label, int show_ids, const char *parent_path,
2492 ce52c690 2018-06-23 stsp const struct got_tree_entries *entries, int selected, int limit, int isroot)
2494 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
2495 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
2496 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
2497 ffd1d5e5 2018-06-23 stsp int width, n;
2499 ffd1d5e5 2018-06-23 stsp *ndisplayed = 0;
2501 f7d12f7e 2018-08-01 stsp werase(view->window);
2503 ffd1d5e5 2018-06-23 stsp if (limit == 0)
2504 ffd1d5e5 2018-06-23 stsp return NULL;
2506 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, label, view->ncols);
2508 ffd1d5e5 2018-06-23 stsp return err;
2509 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2510 2550e4c3 2018-07-13 stsp free(wline);
2511 2550e4c3 2018-07-13 stsp wline = NULL;
2512 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2513 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2514 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2515 ffd1d5e5 2018-06-23 stsp return NULL;
2516 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, parent_path, view->ncols);
2518 ce52c690 2018-06-23 stsp return err;
2519 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2520 2550e4c3 2018-07-13 stsp free(wline);
2521 2550e4c3 2018-07-13 stsp wline = NULL;
2522 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2523 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2524 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2525 ffd1d5e5 2018-06-23 stsp return NULL;
2526 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2527 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
2528 a1eca9bb 2018-06-23 stsp return NULL;
2530 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
2531 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL) {
2532 ffd1d5e5 2018-06-23 stsp if (selected == 0) {
2533 f7d12f7e 2018-08-01 stsp wstandout(view->window);
2534 ffd1d5e5 2018-06-23 stsp *selected_entry = NULL;
2536 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
2537 ffd1d5e5 2018-06-23 stsp if (selected == 0)
2538 f7d12f7e 2018-08-01 stsp wstandend(view->window);
2539 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
2540 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2541 ffd1d5e5 2018-06-23 stsp return NULL;
2545 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry)
2546 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
2549 ffd1d5e5 2018-06-23 stsp while (te) {
2550 1d13200f 2018-07-12 stsp char *line = NULL, *id_str = NULL;
2552 1d13200f 2018-07-12 stsp if (show_ids) {
2553 1d13200f 2018-07-12 stsp err = got_object_id_str(&id_str, te->id);
2555 1d13200f 2018-07-12 stsp return got_error_from_errno();
2557 1d13200f 2018-07-12 stsp if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
2558 1d13200f 2018-07-12 stsp te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
2559 1d13200f 2018-07-12 stsp free(id_str);
2560 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
2562 1d13200f 2018-07-12 stsp free(id_str);
2563 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
2564 ffd1d5e5 2018-06-23 stsp if (err) {
2565 ffd1d5e5 2018-06-23 stsp free(line);
2568 ffd1d5e5 2018-06-23 stsp if (n == selected) {
2569 f7d12f7e 2018-08-01 stsp wstandout(view->window);
2570 ffd1d5e5 2018-06-23 stsp *selected_entry = te;
2572 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2573 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2574 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2575 ffd1d5e5 2018-06-23 stsp if (n == selected)
2576 f7d12f7e 2018-08-01 stsp wstandend(view->window);
2577 ffd1d5e5 2018-06-23 stsp free(line);
2578 2550e4c3 2018-07-13 stsp free(wline);
2579 2550e4c3 2018-07-13 stsp wline = NULL;
2581 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
2582 ffd1d5e5 2018-06-23 stsp *last_displayed_entry = te;
2583 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2585 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
2588 ffd1d5e5 2018-06-23 stsp return err;
2591 ffd1d5e5 2018-06-23 stsp static void
2592 ffd1d5e5 2018-06-23 stsp tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
2593 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries, int isroot)
2595 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te, *prev;
2598 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL)
2601 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
2602 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == te) {
2603 ffd1d5e5 2018-06-23 stsp if (!isroot)
2604 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
2608 ffd1d5e5 2018-06-23 stsp /* XXX this is stupid... switch to TAILQ? */
2609 ffd1d5e5 2018-06-23 stsp for (i = 0; i < maxscroll; i++) {
2610 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry) {
2611 ffd1d5e5 2018-06-23 stsp prev = te;
2612 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
2614 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = prev;
2615 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
2617 ffd1d5e5 2018-06-23 stsp if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
2618 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
2621 ffd1d5e5 2018-06-23 stsp static void
2622 ffd1d5e5 2018-06-23 stsp tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
2623 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *last_displayed_entry,
2624 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries)
2626 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *next;
2627 ffd1d5e5 2018-06-23 stsp int n = 0;
2629 ffd1d5e5 2018-06-23 stsp if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
2632 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry)
2633 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
2635 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_FIRST(&entries->head);
2636 ffd1d5e5 2018-06-23 stsp while (next) {
2637 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = next;
2638 ffd1d5e5 2018-06-23 stsp if (++n >= maxscroll)
2640 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(next, entry);
2644 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2645 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
2646 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
2648 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
2649 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
2650 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
2652 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
2653 ffd1d5e5 2018-06-23 stsp len += strlen(pt->selected_entry->name) + 1 /* slash */;
2655 ce52c690 2018-06-23 stsp len += strlen(te->name);
2657 ce52c690 2018-06-23 stsp *path = calloc(1, len);
2658 ffd1d5e5 2018-06-23 stsp if (path == NULL)
2659 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
2661 ce52c690 2018-06-23 stsp (*path)[0] = '/';
2662 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
2663 d9765a41 2018-06-23 stsp while (pt) {
2664 ce52c690 2018-06-23 stsp if (strlcat(*path, pt->selected_entry->name, len) >= len) {
2665 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
2666 cb2ebc8a 2018-06-23 stsp goto done;
2668 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
2669 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
2670 cb2ebc8a 2018-06-23 stsp goto done;
2672 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
2675 ce52c690 2018-06-23 stsp if (strlcat(*path, te->name, len) >= len) {
2676 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
2677 ce52c690 2018-06-23 stsp goto done;
2681 ce52c690 2018-06-23 stsp if (err) {
2682 ce52c690 2018-06-23 stsp free(*path);
2683 ce52c690 2018-06-23 stsp *path = NULL;
2685 ce52c690 2018-06-23 stsp return err;
2688 ce52c690 2018-06-23 stsp static const struct got_error *
2689 e5a0f69f 2018-08-18 stsp blame_tree_entry(struct tog_view **new_view, struct tog_view *parent_view,
2690 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
2691 e5a0f69f 2018-08-18 stsp struct got_object_id *commit_id, struct got_repository *repo)
2693 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
2694 ce52c690 2018-06-23 stsp char *path;
2695 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
2697 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
2699 ce52c690 2018-06-23 stsp return err;
2701 e5a0f69f 2018-08-18 stsp blame_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_BLAME);
2702 e5a0f69f 2018-08-18 stsp if (blame_view == NULL)
2703 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
2705 e5a0f69f 2018-08-18 stsp err = open_blame_view(blame_view, path, commit_id, repo);
2706 e5a0f69f 2018-08-18 stsp if (err) {
2707 e5a0f69f 2018-08-18 stsp view_close(blame_view);
2708 e5a0f69f 2018-08-18 stsp free(path);
2710 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
2711 69efd4c4 2018-07-18 stsp return err;
2714 69efd4c4 2018-07-18 stsp static const struct got_error *
2715 e5a0f69f 2018-08-18 stsp log_tree_entry(struct tog_view **new_view, struct tog_view *parent_view,
2716 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
2717 e5a0f69f 2018-08-18 stsp struct got_object_id *commit_id, struct got_repository *repo)
2719 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
2720 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
2721 69efd4c4 2018-07-18 stsp char *path;
2723 e5a0f69f 2018-08-18 stsp log_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_LOG);
2724 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
2725 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
2727 69efd4c4 2018-07-18 stsp err = tree_entry_path(&path, parents, te);
2729 69efd4c4 2018-07-18 stsp return err;
2731 e5a0f69f 2018-08-18 stsp err = open_log_view(log_view, commit_id, repo, path);
2733 e5a0f69f 2018-08-18 stsp view_close(log_view);
2735 e5a0f69f 2018-08-18 stsp *new_view = log_view;
2736 cb2ebc8a 2018-06-23 stsp free(path);
2737 cb2ebc8a 2018-06-23 stsp return err;
2740 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2741 ad80ab7b 2018-08-04 stsp open_tree_view(struct tog_view *view, struct got_tree_object *root,
2742 5221c383 2018-08-01 stsp struct got_object_id *commit_id, struct got_repository *repo)
2744 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
2745 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
2746 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
2748 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
2750 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
2751 ffd1d5e5 2018-06-23 stsp if (err != NULL)
2752 ffd1d5e5 2018-06-23 stsp goto done;
2754 fb2756b9 2018-08-04 stsp if (asprintf(&s->tree_label, "commit: %s", commit_id_str) == -1) {
2755 ffd1d5e5 2018-06-23 stsp err = got_error_from_errno();
2756 ffd1d5e5 2018-06-23 stsp goto done;
2759 fb2756b9 2018-08-04 stsp s->root = s->tree = root;
2760 fb2756b9 2018-08-04 stsp s->entries = got_object_tree_get_entries(root);
2761 fb2756b9 2018-08-04 stsp s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
2762 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
2763 fb2756b9 2018-08-04 stsp s->repo = repo;
2765 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
2766 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
2767 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
2769 ad80ab7b 2018-08-04 stsp free(commit_id_str);
2771 fb2756b9 2018-08-04 stsp free(s->tree_label);
2772 ad80ab7b 2018-08-04 stsp return err;
2775 e5a0f69f 2018-08-18 stsp static const struct got_error *
2776 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
2778 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
2780 fb2756b9 2018-08-04 stsp free(s->tree_label);
2781 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
2782 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
2783 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
2784 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
2785 ad80ab7b 2018-08-04 stsp free(parent);
2788 fb2756b9 2018-08-04 stsp if (s->tree != s->root)
2789 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
2790 e5a0f69f 2018-08-18 stsp got_object_tree_close(s->root);
2792 e5a0f69f 2018-08-18 stsp return NULL;
2795 ad80ab7b 2018-08-04 stsp static const struct got_error *
2796 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
2798 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
2799 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
2800 e5a0f69f 2018-08-18 stsp char *parent_path;
2802 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
2804 e5a0f69f 2018-08-18 stsp return err;
2806 e5a0f69f 2018-08-18 stsp err = draw_tree_entries(view, &s->first_displayed_entry,
2807 e5a0f69f 2018-08-18 stsp &s->last_displayed_entry, &s->selected_entry,
2808 e5a0f69f 2018-08-18 stsp &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
2809 e5a0f69f 2018-08-18 stsp s->entries, s->selected, view->nlines, s->tree == s->root);
2810 e5a0f69f 2018-08-18 stsp free(parent_path);
2811 e5a0f69f 2018-08-18 stsp return err;
2814 e5a0f69f 2018-08-18 stsp static const struct got_error *
2815 e5a0f69f 2018-08-18 stsp input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
2816 e5a0f69f 2018-08-18 stsp struct tog_view *view, int ch)
2818 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2819 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
2821 e5a0f69f 2018-08-18 stsp switch (ch) {
2823 e5a0f69f 2018-08-18 stsp s->show_ids = !s->show_ids;
2826 e5a0f69f 2018-08-18 stsp if (s->selected_entry) {
2827 e5a0f69f 2018-08-18 stsp err = log_tree_entry(new_view, view,
2828 e5a0f69f 2018-08-18 stsp s->selected_entry, &s->parents,
2829 e5a0f69f 2018-08-18 stsp s->commit_id, s->repo);
2833 e5a0f69f 2018-08-18 stsp case KEY_UP:
2834 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
2835 e5a0f69f 2018-08-18 stsp s->selected--;
2836 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
2838 e5a0f69f 2018-08-18 stsp tree_scroll_up(&s->first_displayed_entry, 1,
2839 e5a0f69f 2018-08-18 stsp s->entries, s->tree == s->root);
2841 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
2842 e5a0f69f 2018-08-18 stsp if (SIMPLEQ_FIRST(&s->entries->head) ==
2843 e5a0f69f 2018-08-18 stsp s->first_displayed_entry) {
2844 e5a0f69f 2018-08-18 stsp if (s->tree != s->root)
2845 e5a0f69f 2018-08-18 stsp s->first_displayed_entry = NULL;
2846 e5a0f69f 2018-08-18 stsp s->selected = 0;
2849 e5a0f69f 2018-08-18 stsp tree_scroll_up(&s->first_displayed_entry,
2850 e5a0f69f 2018-08-18 stsp view->nlines, s->entries,
2851 e5a0f69f 2018-08-18 stsp s->tree == s->root);
2854 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
2855 e5a0f69f 2018-08-18 stsp if (s->selected < s->ndisplayed - 1) {
2856 e5a0f69f 2018-08-18 stsp s->selected++;
2859 e5a0f69f 2018-08-18 stsp tree_scroll_down(&s->first_displayed_entry, 1,
2860 e5a0f69f 2018-08-18 stsp s->last_displayed_entry, s->entries);
2862 e5a0f69f 2018-08-18 stsp case KEY_NPAGE:
2863 e5a0f69f 2018-08-18 stsp tree_scroll_down(&s->first_displayed_entry,
2864 e5a0f69f 2018-08-18 stsp view->nlines, s->last_displayed_entry,
2865 e5a0f69f 2018-08-18 stsp s->entries);
2866 e5a0f69f 2018-08-18 stsp if (SIMPLEQ_NEXT(s->last_displayed_entry,
2869 e5a0f69f 2018-08-18 stsp /* can't scroll any further; move cursor down */
2870 e5a0f69f 2018-08-18 stsp if (s->selected < s->ndisplayed - 1)
2871 e5a0f69f 2018-08-18 stsp s->selected = s->ndisplayed - 1;
2873 e5a0f69f 2018-08-18 stsp case KEY_ENTER:
2874 e5a0f69f 2018-08-18 stsp case '\r':
2875 e5a0f69f 2018-08-18 stsp if (s->selected_entry == NULL) {
2876 e5a0f69f 2018-08-18 stsp struct tog_parent_tree *parent;
2877 31607d6c 2018-08-18 stsp case KEY_LEFT:
2878 e5a0f69f 2018-08-18 stsp /* user selected '..' */
2879 e5a0f69f 2018-08-18 stsp if (s->tree == s->root)
2881 e5a0f69f 2018-08-18 stsp parent = TAILQ_FIRST(&s->parents);
2882 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&s->parents, parent,
2884 e5a0f69f 2018-08-18 stsp got_object_tree_close(s->tree);
2885 e5a0f69f 2018-08-18 stsp s->tree = parent->tree;
2886 e5a0f69f 2018-08-18 stsp s->entries =
2887 e5a0f69f 2018-08-18 stsp got_object_tree_get_entries(s->tree);
2888 e5a0f69f 2018-08-18 stsp s->first_displayed_entry =
2889 e5a0f69f 2018-08-18 stsp parent->first_displayed_entry;
2890 e5a0f69f 2018-08-18 stsp s->selected_entry =
2891 e5a0f69f 2018-08-18 stsp parent->selected_entry;
2892 e5a0f69f 2018-08-18 stsp s->selected = parent->selected;
2893 e5a0f69f 2018-08-18 stsp free(parent);
2894 e5a0f69f 2018-08-18 stsp } else if (S_ISDIR(s->selected_entry->mode)) {
2895 e5a0f69f 2018-08-18 stsp struct tog_parent_tree *parent;
2896 e5a0f69f 2018-08-18 stsp struct got_tree_object *child;
2897 e5a0f69f 2018-08-18 stsp err = got_object_open_as_tree(&child,
2898 e5a0f69f 2018-08-18 stsp s->repo, s->selected_entry->id);
2901 e5a0f69f 2018-08-18 stsp parent = calloc(1, sizeof(*parent));
2902 e5a0f69f 2018-08-18 stsp if (parent == NULL) {
2903 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2906 e5a0f69f 2018-08-18 stsp parent->tree = s->tree;
2907 e5a0f69f 2018-08-18 stsp parent->first_displayed_entry =
2908 e5a0f69f 2018-08-18 stsp s->first_displayed_entry;
2909 e5a0f69f 2018-08-18 stsp parent->selected_entry = s->selected_entry;
2910 e5a0f69f 2018-08-18 stsp parent->selected = s->selected;
2911 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2912 e5a0f69f 2018-08-18 stsp s->tree = child;
2913 e5a0f69f 2018-08-18 stsp s->entries =
2914 e5a0f69f 2018-08-18 stsp got_object_tree_get_entries(s->tree);
2915 e5a0f69f 2018-08-18 stsp s->selected = 0;
2916 e5a0f69f 2018-08-18 stsp s->first_displayed_entry = NULL;
2917 e5a0f69f 2018-08-18 stsp } else if (S_ISREG(s->selected_entry->mode)) {
2918 e5a0f69f 2018-08-18 stsp err = blame_tree_entry(new_view, view,
2919 e5a0f69f 2018-08-18 stsp s->selected_entry, &s->parents,
2920 e5a0f69f 2018-08-18 stsp s->commit_id, s->repo);
2925 e5a0f69f 2018-08-18 stsp case KEY_RESIZE:
2926 e5a0f69f 2018-08-18 stsp if (s->selected > view->nlines)
2927 e5a0f69f 2018-08-18 stsp s->selected = s->ndisplayed - 1;
2933 ffd1d5e5 2018-06-23 stsp return err;
2936 ffd1d5e5 2018-06-23 stsp __dead static void
2937 ffd1d5e5 2018-06-23 stsp usage_tree(void)
2940 ffd1d5e5 2018-06-23 stsp fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
2941 ffd1d5e5 2018-06-23 stsp getprogname());
2945 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2946 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
2948 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
2949 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
2950 ffd1d5e5 2018-06-23 stsp char *repo_path = NULL;
2951 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
2952 ffd1d5e5 2018-06-23 stsp char *commit_id_arg = NULL;
2953 ffd1d5e5 2018-06-23 stsp struct got_commit_object *commit = NULL;
2954 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree = NULL;
2956 5221c383 2018-08-01 stsp struct tog_view *view;
2958 ffd1d5e5 2018-06-23 stsp #ifndef PROFILE
2959 ffd1d5e5 2018-06-23 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
2960 ffd1d5e5 2018-06-23 stsp err(1, "pledge");
2963 ffd1d5e5 2018-06-23 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
2964 ffd1d5e5 2018-06-23 stsp switch (ch) {
2966 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
2970 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
2974 ffd1d5e5 2018-06-23 stsp argc -= optind;
2975 ffd1d5e5 2018-06-23 stsp argv += optind;
2977 ffd1d5e5 2018-06-23 stsp if (argc == 0) {
2978 ffd1d5e5 2018-06-23 stsp repo_path = getcwd(NULL, 0);
2979 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
2980 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
2981 ffd1d5e5 2018-06-23 stsp } else if (argc == 1) {
2982 ffd1d5e5 2018-06-23 stsp repo_path = realpath(argv[0], NULL);
2983 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
2984 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
2986 ffd1d5e5 2018-06-23 stsp usage_log();
2988 ffd1d5e5 2018-06-23 stsp error = got_repo_open(&repo, repo_path);
2989 ffd1d5e5 2018-06-23 stsp free(repo_path);
2990 ffd1d5e5 2018-06-23 stsp if (error != NULL)
2991 ffd1d5e5 2018-06-23 stsp return error;
2993 ffd1d5e5 2018-06-23 stsp if (commit_id_arg == NULL) {
2994 ffd1d5e5 2018-06-23 stsp error = get_head_commit_id(&commit_id, repo);
2995 ffd1d5e5 2018-06-23 stsp if (error != NULL)
2996 ffd1d5e5 2018-06-23 stsp goto done;
2998 ffd1d5e5 2018-06-23 stsp struct got_object *obj;
2999 ffd1d5e5 2018-06-23 stsp error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
3000 ffd1d5e5 2018-06-23 stsp if (error == NULL) {
3001 ffd1d5e5 2018-06-23 stsp commit_id = got_object_get_id(obj);
3002 ffd1d5e5 2018-06-23 stsp if (commit_id == NULL)
3003 ffd1d5e5 2018-06-23 stsp error = got_error_from_errno();
3006 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3007 ffd1d5e5 2018-06-23 stsp goto done;
3009 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
3010 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3011 ffd1d5e5 2018-06-23 stsp goto done;
3013 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_tree(&tree, repo, commit->tree_id);
3014 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3015 ffd1d5e5 2018-06-23 stsp goto done;
3017 b3665f43 2018-08-04 stsp view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_TREE);
3018 5221c383 2018-08-01 stsp if (view == NULL) {
3019 5221c383 2018-08-01 stsp error = got_error_from_errno();
3020 5221c383 2018-08-01 stsp goto done;
3022 ad80ab7b 2018-08-04 stsp error = open_tree_view(view, tree, commit_id, repo);
3023 ad80ab7b 2018-08-04 stsp if (error)
3024 ad80ab7b 2018-08-04 stsp goto done;
3025 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3027 ffd1d5e5 2018-06-23 stsp free(commit_id);
3028 ffd1d5e5 2018-06-23 stsp if (commit)
3029 ffd1d5e5 2018-06-23 stsp got_object_commit_close(commit);
3031 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
3033 ffd1d5e5 2018-06-23 stsp got_repo_close(repo);
3034 ffd1d5e5 2018-06-23 stsp return error;
3036 5c5136c5 2018-05-20 stsp static void
3037 9f7d7167 2018-04-29 stsp init_curses(void)
3039 9f7d7167 2018-04-29 stsp initscr();
3043 9f7d7167 2018-04-29 stsp intrflush(stdscr, FALSE);
3044 9f7d7167 2018-04-29 stsp keypad(stdscr, TRUE);
3045 1f475ad8 2018-05-10 stsp curs_set(0);
3048 4ed7e80c 2018-05-20 stsp __dead static void
3049 9f7d7167 2018-04-29 stsp usage(void)
3053 c2301be8 2018-04-30 stsp fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3054 9f7d7167 2018-04-29 stsp "Available commands:\n", getprogname());
3055 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
3056 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = &tog_commands[i];
3057 c2301be8 2018-04-30 stsp fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3062 c2301be8 2018-04-30 stsp static char **
3063 c2301be8 2018-04-30 stsp make_argv(const char *arg0, const char *arg1)
3065 c2301be8 2018-04-30 stsp char **argv;
3066 c2301be8 2018-04-30 stsp int argc = (arg1 == NULL ? 1 : 2);
3068 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
3069 c2301be8 2018-04-30 stsp if (argv == NULL)
3070 c2301be8 2018-04-30 stsp err(1, "calloc");
3071 c2301be8 2018-04-30 stsp argv[0] = strdup(arg0);
3072 c2301be8 2018-04-30 stsp if (argv[0] == NULL)
3073 c2301be8 2018-04-30 stsp err(1, "calloc");
3074 c2301be8 2018-04-30 stsp if (arg1) {
3075 c2301be8 2018-04-30 stsp argv[1] = strdup(arg1);
3076 c2301be8 2018-04-30 stsp if (argv[1] == NULL)
3077 c2301be8 2018-04-30 stsp err(1, "calloc");
3080 c2301be8 2018-04-30 stsp return argv;
3084 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
3086 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
3087 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
3088 9f7d7167 2018-04-29 stsp int ch, hflag = 0;
3089 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
3091 9f7d7167 2018-04-29 stsp setlocale(LC_ALL, "");
3093 9f7d7167 2018-04-29 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
3094 9f7d7167 2018-04-29 stsp switch (ch) {
3096 9f7d7167 2018-04-29 stsp hflag = 1;
3100 9f7d7167 2018-04-29 stsp /* NOTREACHED */
3104 9f7d7167 2018-04-29 stsp argc -= optind;
3105 9f7d7167 2018-04-29 stsp argv += optind;
3106 9f7d7167 2018-04-29 stsp optind = 0;
3107 c2301be8 2018-04-30 stsp optreset = 1;
3109 c2301be8 2018-04-30 stsp if (argc == 0) {
3110 f29d3e89 2018-06-23 stsp if (hflag)
3112 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
3113 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
3114 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, NULL);
3119 c2301be8 2018-04-30 stsp /* Did the user specific a command? */
3120 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
3121 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
3122 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
3123 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
3124 9f7d7167 2018-04-29 stsp if (hflag)
3125 9f7d7167 2018-04-29 stsp tog_commands[i].cmd_usage();
3129 9f7d7167 2018-04-29 stsp if (cmd == NULL) {
3130 c2301be8 2018-04-30 stsp /* Did the user specify a repository? */
3131 c2301be8 2018-04-30 stsp char *repo_path = realpath(argv[0], NULL);
3132 c2301be8 2018-04-30 stsp if (repo_path) {
3133 c2301be8 2018-04-30 stsp struct got_repository *repo;
3134 c2301be8 2018-04-30 stsp error = got_repo_open(&repo, repo_path);
3135 c2301be8 2018-04-30 stsp if (error == NULL)
3136 c2301be8 2018-04-30 stsp got_repo_close(repo);
3138 ad7de8d9 2018-04-30 stsp error = got_error_from_errno();
3139 c2301be8 2018-04-30 stsp if (error) {
3140 f29d3e89 2018-06-23 stsp if (hflag) {
3141 f29d3e89 2018-06-23 stsp fprintf(stderr, "%s: '%s' is not a "
3142 f29d3e89 2018-06-23 stsp "known command\n", getprogname(),
3146 ad7de8d9 2018-04-30 stsp fprintf(stderr, "%s: '%s' is neither a known "
3147 ad7de8d9 2018-04-30 stsp "command nor a path to a repository\n",
3148 c2301be8 2018-04-30 stsp getprogname(), argv[0]);
3149 ad7de8d9 2018-04-30 stsp free(repo_path);
3152 c2301be8 2018-04-30 stsp cmd = &tog_commands[0];
3153 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, repo_path);
3155 c2301be8 2018-04-30 stsp free(repo_path);
3159 5c5136c5 2018-05-20 stsp init_curses();
3161 c2301be8 2018-04-30 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
3162 9f7d7167 2018-04-29 stsp if (error)
3163 9f7d7167 2018-04-29 stsp goto done;
3166 c2301be8 2018-04-30 stsp free(cmd_argv);
3167 9f7d7167 2018-04-29 stsp if (error)
3168 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);