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 d6b05b5b 2018-08-04 stsp TOG_VIEW_TREE,
89 d6b05b5b 2018-08-04 stsp TOG_VIEW_BLAME
92 ba4f502b 2018-08-04 stsp struct commit_queue_entry {
93 ba4f502b 2018-08-04 stsp TAILQ_ENTRY(commit_queue_entry) entry;
94 ba4f502b 2018-08-04 stsp struct got_object_id *id;
95 ba4f502b 2018-08-04 stsp struct got_commit_object *commit;
97 ba4f502b 2018-08-04 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
98 ba4f502b 2018-08-04 stsp struct commit_queue {
99 ba4f502b 2018-08-04 stsp int ncommits;
100 ba4f502b 2018-08-04 stsp struct commit_queue_head head;
103 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state {
105 b01e7d3b 2018-08-04 stsp int first_displayed_line;
106 b01e7d3b 2018-08-04 stsp int last_displayed_line;
109 b01e7d3b 2018-08-04 stsp struct tog_log_view_state {
110 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
111 b01e7d3b 2018-08-04 stsp struct commit_queue commits;
112 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
113 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
114 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
115 b01e7d3b 2018-08-04 stsp int selected;
116 b01e7d3b 2018-08-04 stsp char *in_repo_path;
117 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
120 cc3c9aac 2018-08-01 stsp struct tog_view {
121 26ed57b2 2018-05-19 stsp WINDOW *window;
122 26ed57b2 2018-05-19 stsp PANEL *panel;
123 97ddc146 2018-08-01 stsp int nlines, ncols, begin_y, begin_x;
124 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
125 6d0fee91 2018-08-01 stsp struct tog_view *parent;
127 5dc9f4bc 2018-08-04 stsp /* type-specific state */
128 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
130 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
131 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
135 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
136 ba4f502b 2018-08-04 stsp struct got_object *, struct got_object *, struct got_repository *);
137 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
138 5dc9f4bc 2018-08-04 stsp static void close_diff_view(struct tog_view *);
139 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
140 ba4f502b 2018-08-04 stsp struct got_object_id *, struct got_repository *, const char *);
141 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
142 ba4f502b 2018-08-04 stsp static void close_log_view(struct tog_view *);
143 4fc679ca 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *, const char *,
144 5221c383 2018-08-01 stsp struct got_object_id *, struct got_repository *);
145 4fc679ca 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *,
146 4fc679ca 2018-08-04 stsp struct got_tree_object *, struct got_object_id *, struct got_repository *);
148 ea5e7bb5 2018-08-01 stsp static void
149 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
151 ea5e7bb5 2018-08-01 stsp if (view->panel)
152 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
153 ea5e7bb5 2018-08-01 stsp if (view->window)
154 ea5e7bb5 2018-08-01 stsp delwin(view->window);
155 ea5e7bb5 2018-08-01 stsp free(view);
158 ea5e7bb5 2018-08-01 stsp static struct tog_view *
159 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
160 d6b05b5b 2018-08-04 stsp struct tog_view *parent, enum tog_view_type type)
162 ea5e7bb5 2018-08-01 stsp struct tog_view *view = malloc(sizeof(*view));
164 ea5e7bb5 2018-08-01 stsp if (view == NULL)
165 ea5e7bb5 2018-08-01 stsp return NULL;
167 6d0fee91 2018-08-01 stsp view->parent = parent;
168 d6b05b5b 2018-08-04 stsp view->type = type;
169 f7d12f7e 2018-08-01 stsp view->lines = LINES;
170 f7d12f7e 2018-08-01 stsp view->cols = COLS;
171 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
172 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
173 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
174 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
175 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
176 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
177 96a765a8 2018-08-04 stsp view_close(view);
178 ea5e7bb5 2018-08-01 stsp return NULL;
180 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
181 ea5e7bb5 2018-08-01 stsp if (view->panel == NULL) {
182 96a765a8 2018-08-04 stsp view_close(view);
183 ea5e7bb5 2018-08-01 stsp return NULL;
186 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
187 ea5e7bb5 2018-08-01 stsp return view;
191 6acd1bd1 2018-08-04 stsp view_show(struct tog_view *view)
193 cdf1ee82 2018-08-01 stsp show_panel(view->panel);
194 cdf1ee82 2018-08-01 stsp update_panels();
197 f7d12f7e 2018-08-01 stsp const struct got_error *
198 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
200 f7d12f7e 2018-08-01 stsp int nlines, ncols;
202 6d0fee91 2018-08-01 stsp while (view) {
203 6d0fee91 2018-08-01 stsp if (view->lines > LINES)
204 6d0fee91 2018-08-01 stsp nlines = view->nlines - (view->lines - LINES);
206 6d0fee91 2018-08-01 stsp nlines = view->nlines + (LINES - view->lines);
208 6d0fee91 2018-08-01 stsp if (view->cols > COLS)
209 6d0fee91 2018-08-01 stsp ncols = view->ncols - (view->cols - COLS);
211 6d0fee91 2018-08-01 stsp ncols = view->ncols + (COLS - view->cols);
213 6d0fee91 2018-08-01 stsp if (wresize(view->window, nlines, ncols) == ERR)
214 6d0fee91 2018-08-01 stsp return got_error_from_errno();
215 77a567dd 2018-08-01 stsp replace_panel(view->panel, view->window);
217 6d0fee91 2018-08-01 stsp view->nlines = nlines;
218 6d0fee91 2018-08-01 stsp view->ncols = ncols;
219 6d0fee91 2018-08-01 stsp view->lines = LINES;
220 6d0fee91 2018-08-01 stsp view->cols = COLS;
222 6d0fee91 2018-08-01 stsp view = view->parent;
225 f7d12f7e 2018-08-01 stsp return NULL;
228 4ed7e80c 2018-05-20 stsp __dead static void
229 9f7d7167 2018-04-29 stsp usage_log(void)
232 c70c5802 2018-08-01 stsp fprintf(stderr,
233 c70c5802 2018-08-01 stsp "usage: %s log [-c commit] [-r repository-path] [path]\n",
234 9f7d7167 2018-04-29 stsp getprogname());
238 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
239 80ddbec8 2018-04-29 stsp static const struct got_error *
240 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
242 00dfcb92 2018-06-11 stsp char *vis = NULL;
243 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
245 963b370f 2018-05-20 stsp *ws = NULL;
246 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
247 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
248 00dfcb92 2018-06-11 stsp int vislen;
249 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
250 00dfcb92 2018-06-11 stsp return got_error_from_errno();
252 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
253 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
255 00dfcb92 2018-06-11 stsp return err;
256 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
257 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
258 a7f50699 2018-06-11 stsp err = got_error_from_errno(); /* give up */
263 963b370f 2018-05-20 stsp *ws = calloc(*wlen + 1, sizeof(*ws));
264 a7f50699 2018-06-11 stsp if (*ws == NULL) {
265 a7f50699 2018-06-11 stsp err = got_error_from_errno();
269 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
270 963b370f 2018-05-20 stsp err = got_error_from_errno();
275 963b370f 2018-05-20 stsp *ws = NULL;
278 963b370f 2018-05-20 stsp return err;
281 963b370f 2018-05-20 stsp /* Format a line for display, ensuring that it won't overflow a width limit. */
282 963b370f 2018-05-20 stsp static const struct got_error *
283 ffd1d5e5 2018-06-23 stsp format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
285 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
286 963b370f 2018-05-20 stsp int cols = 0;
287 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
288 963b370f 2018-05-20 stsp size_t wlen;
291 963b370f 2018-05-20 stsp *wlinep = NULL;
292 b700b5d6 2018-07-10 stsp *widthp = 0;
294 963b370f 2018-05-20 stsp err = mbs2ws(&wline, &wlen, line);
296 963b370f 2018-05-20 stsp return err;
299 b700b5d6 2018-07-10 stsp while (i < wlen && cols < wlimit) {
300 963b370f 2018-05-20 stsp int width = wcwidth(wline[i]);
301 963b370f 2018-05-20 stsp switch (width) {
307 b700b5d6 2018-07-10 stsp if (cols + width <= wlimit) {
308 b700b5d6 2018-07-10 stsp cols += width;
313 963b370f 2018-05-20 stsp if (wline[i] == L'\t')
314 b700b5d6 2018-07-10 stsp cols += TABSIZE - ((cols + 1) % TABSIZE);
318 963b370f 2018-05-20 stsp err = got_error_from_errno();
322 963b370f 2018-05-20 stsp wline[i] = L'\0';
323 b700b5d6 2018-07-10 stsp if (widthp)
324 b700b5d6 2018-07-10 stsp *widthp = cols;
327 963b370f 2018-05-20 stsp free(wline);
329 963b370f 2018-05-20 stsp *wlinep = wline;
330 963b370f 2018-05-20 stsp return err;
333 963b370f 2018-05-20 stsp static const struct got_error *
334 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
335 2814baeb 2018-08-01 stsp struct got_object_id *id)
337 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
338 b39d25c7 2018-07-10 stsp char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
339 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
340 6d9fbc00 2018-04-29 stsp char *author0 = NULL, *author = NULL;
341 bb737323 2018-05-20 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
342 bb737323 2018-05-20 stsp int author_width, logmsg_width;
343 6d9fbc00 2018-04-29 stsp char *newline, *smallerthan;
344 80ddbec8 2018-04-29 stsp char *line = NULL;
345 bb737323 2018-05-20 stsp int col, limit;
346 b39d25c7 2018-07-10 stsp static const size_t date_display_cols = 9;
347 bb737323 2018-05-20 stsp static const size_t author_display_cols = 16;
348 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
350 c70c5802 2018-08-01 stsp if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ",
351 c70c5802 2018-08-01 stsp &commit->tm_committer) >= sizeof(datebuf))
352 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
354 b39d25c7 2018-07-10 stsp if (avail < date_display_cols)
355 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
357 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
358 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
359 b39d25c7 2018-07-10 stsp col = limit + 1;
360 b39d25c7 2018-07-10 stsp if (col > avail)
363 6d9fbc00 2018-04-29 stsp author0 = strdup(commit->author);
364 6d9fbc00 2018-04-29 stsp if (author0 == NULL) {
365 80ddbec8 2018-04-29 stsp err = got_error_from_errno();
368 6d9fbc00 2018-04-29 stsp author = author0;
369 6d9fbc00 2018-04-29 stsp smallerthan = strchr(author, '<');
370 6d9fbc00 2018-04-29 stsp if (smallerthan)
371 6d9fbc00 2018-04-29 stsp *smallerthan = '\0';
373 6d9fbc00 2018-04-29 stsp char *at = strchr(author, '@');
375 6d9fbc00 2018-04-29 stsp *at = '\0';
377 b39d25c7 2018-07-10 stsp limit = avail - col;
378 bb737323 2018-05-20 stsp err = format_line(&wauthor, &author_width, author, limit);
381 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
382 bb737323 2018-05-20 stsp col += author_width;
383 9c2eaf34 2018-05-20 stsp while (col <= avail && author_width < author_display_cols + 1) {
384 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
386 bb737323 2018-05-20 stsp author_width++;
388 9c2eaf34 2018-05-20 stsp if (col > avail)
391 bb737323 2018-05-20 stsp logmsg0 = strdup(commit->logmsg);
392 bb737323 2018-05-20 stsp if (logmsg0 == NULL) {
393 6d9fbc00 2018-04-29 stsp err = got_error_from_errno();
396 bb737323 2018-05-20 stsp logmsg = logmsg0;
397 bb737323 2018-05-20 stsp while (*logmsg == '\n')
399 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
400 bb737323 2018-05-20 stsp if (newline)
401 bb737323 2018-05-20 stsp *newline = '\0';
402 bb737323 2018-05-20 stsp limit = avail - col;
403 bb737323 2018-05-20 stsp err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
406 2814baeb 2018-08-01 stsp waddwstr(view->window, wlogmsg);
407 bb737323 2018-05-20 stsp col += logmsg_width;
408 bb737323 2018-05-20 stsp while (col <= avail) {
409 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
413 80ddbec8 2018-04-29 stsp free(logmsg0);
414 bb737323 2018-05-20 stsp free(wlogmsg);
415 6d9fbc00 2018-04-29 stsp free(author0);
416 bb737323 2018-05-20 stsp free(wauthor);
417 80ddbec8 2018-04-29 stsp free(line);
418 80ddbec8 2018-04-29 stsp return err;
421 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
422 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
423 899d86c2 2018-05-10 stsp struct got_object_id *id)
425 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
427 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
428 80ddbec8 2018-04-29 stsp if (entry == NULL)
429 899d86c2 2018-05-10 stsp return NULL;
431 899d86c2 2018-05-10 stsp entry->id = id;
432 99db9666 2018-05-07 stsp entry->commit = commit;
433 899d86c2 2018-05-10 stsp return entry;
436 99db9666 2018-05-07 stsp static void
437 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
439 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
441 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
442 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
443 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
444 ecb28ae0 2018-07-16 stsp commits->ncommits--;
445 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
446 99db9666 2018-05-07 stsp free(entry);
449 99db9666 2018-05-07 stsp static void
450 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
452 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
453 99db9666 2018-05-07 stsp pop_commit(commits);
456 c4972b91 2018-05-07 stsp static const struct got_error *
457 9ba79e04 2018-06-11 stsp queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
458 ecb28ae0 2018-07-16 stsp struct got_object_id *start_id, int minqueue, int init,
459 ecb28ae0 2018-07-16 stsp struct got_repository *repo, const char *path)
461 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
462 899d86c2 2018-05-10 stsp struct got_object_id *id;
463 9ba79e04 2018-06-11 stsp struct commit_queue_entry *entry;
464 ecb28ae0 2018-07-16 stsp int nfetched, nqueued = 0, found_obj = 0;
465 c8f60bff 2018-07-23 stsp int is_root_path = strcmp(path, "/") == 0;
467 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_start(graph, start_id);
469 c4972b91 2018-05-07 stsp return err;
471 ecb28ae0 2018-07-16 stsp entry = TAILQ_LAST(&commits->head, commit_queue_head);
472 9ba79e04 2018-06-11 stsp if (entry && got_object_id_cmp(entry->id, start_id) == 0) {
473 9ba79e04 2018-06-11 stsp int nfetched;
475 9ba79e04 2018-06-11 stsp /* Start ID's commit is already on the queue; skip over it. */
476 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
477 9ba79e04 2018-06-11 stsp if (err && err->code != GOT_ERR_ITER_NEED_MORE)
478 9ba79e04 2018-06-11 stsp return err;
480 9ba79e04 2018-06-11 stsp err = got_commit_graph_fetch_commits(&nfetched, graph, 1, repo);
482 9ba79e04 2018-06-11 stsp return err;
485 9ba79e04 2018-06-11 stsp while (1) {
486 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
488 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
490 ecb28ae0 2018-07-16 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
492 ecb28ae0 2018-07-16 stsp if (nqueued >= minqueue) {
493 9ba79e04 2018-06-11 stsp err = NULL;
496 ecb28ae0 2018-07-16 stsp err = got_commit_graph_fetch_commits(&nfetched,
497 ecb28ae0 2018-07-16 stsp graph, 1, repo);
499 ecb28ae0 2018-07-16 stsp return err;
502 ecb28ae0 2018-07-16 stsp if (id == NULL)
505 9ba79e04 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
509 c8f60bff 2018-07-23 stsp if (!is_root_path) {
510 ecb28ae0 2018-07-16 stsp struct got_object *obj;
511 ecb28ae0 2018-07-16 stsp struct got_object_qid *pid;
512 ecb28ae0 2018-07-16 stsp int changed = 0;
514 ecb28ae0 2018-07-16 stsp err = got_object_open_by_path(&obj, repo, id, path);
516 c8f60bff 2018-07-23 stsp got_object_commit_close(commit);
517 ecb28ae0 2018-07-16 stsp if (err->code == GOT_ERR_NO_OBJ &&
518 ecb28ae0 2018-07-16 stsp (found_obj || !init)) {
519 ecb28ae0 2018-07-16 stsp /* History stops here. */
520 ecb28ae0 2018-07-16 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
524 ecb28ae0 2018-07-16 stsp found_obj = 1;
526 ecb28ae0 2018-07-16 stsp pid = SIMPLEQ_FIRST(&commit->parent_ids);
527 ecb28ae0 2018-07-16 stsp if (pid != NULL) {
528 ecb28ae0 2018-07-16 stsp struct got_object *pobj;
529 ecb28ae0 2018-07-16 stsp err = got_object_open_by_path(&pobj, repo,
530 ecb28ae0 2018-07-16 stsp pid->id, path);
532 ecb28ae0 2018-07-16 stsp if (err->code != GOT_ERR_NO_OBJ) {
533 ecb28ae0 2018-07-16 stsp got_object_close(obj);
534 c8f60bff 2018-07-23 stsp got_object_commit_close(commit);
537 ecb28ae0 2018-07-16 stsp err = NULL;
538 ecb28ae0 2018-07-16 stsp changed = 1;
540 c2f16475 2018-07-23 stsp struct got_object_id *id, *pid;
541 c2f16475 2018-07-23 stsp id = got_object_get_id(obj);
542 c2f16475 2018-07-23 stsp if (id == NULL) {
543 c2f16475 2018-07-23 stsp err = got_error_from_errno();
544 c8f60bff 2018-07-23 stsp got_object_close(obj);
545 c8f60bff 2018-07-23 stsp got_object_close(pobj);
548 c2f16475 2018-07-23 stsp pid = got_object_get_id(pobj);
549 c2f16475 2018-07-23 stsp if (pid == NULL) {
550 c2f16475 2018-07-23 stsp err = got_error_from_errno();
552 c8f60bff 2018-07-23 stsp got_object_close(obj);
553 c8f60bff 2018-07-23 stsp got_object_close(pobj);
557 c2f16475 2018-07-23 stsp (got_object_id_cmp(id, pid) != 0);
558 ecb28ae0 2018-07-16 stsp got_object_close(pobj);
563 c8f60bff 2018-07-23 stsp got_object_close(obj);
564 ecb28ae0 2018-07-16 stsp if (!changed) {
565 ecb28ae0 2018-07-16 stsp got_object_commit_close(commit);
570 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
571 9ba79e04 2018-06-11 stsp if (entry == NULL) {
572 9ba79e04 2018-06-11 stsp err = got_error_from_errno();
575 ecb28ae0 2018-07-16 stsp TAILQ_INSERT_TAIL(&commits->head, entry, entry);
577 ecb28ae0 2018-07-16 stsp commits->ncommits++;
580 9ba79e04 2018-06-11 stsp return err;
583 99db9666 2018-05-07 stsp static const struct got_error *
584 9ba79e04 2018-06-11 stsp fetch_next_commit(struct commit_queue_entry **pentry,
585 9ba79e04 2018-06-11 stsp struct commit_queue_entry *entry, struct commit_queue *commits,
586 ecb28ae0 2018-07-16 stsp struct got_commit_graph *graph, struct got_repository *repo,
587 ecb28ae0 2018-07-16 stsp const char *path)
589 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
591 9ba79e04 2018-06-11 stsp *pentry = NULL;
593 ecb28ae0 2018-07-16 stsp err = queue_commits(graph, commits, entry->id, 1, 0, repo, path);
595 9ba79e04 2018-06-11 stsp return err;
597 9ba79e04 2018-06-11 stsp /* Next entry to display should now be available. */
598 9ba79e04 2018-06-11 stsp *pentry = TAILQ_NEXT(entry, entry);
599 9ba79e04 2018-06-11 stsp if (*pentry == NULL)
600 9ba79e04 2018-06-11 stsp return got_error(GOT_ERR_NO_OBJ);
602 9ba79e04 2018-06-11 stsp return NULL;
605 899d86c2 2018-05-10 stsp static const struct got_error *
606 9ba79e04 2018-06-11 stsp get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
608 9ba79e04 2018-06-11 stsp const struct got_error *err = NULL;
609 9ba79e04 2018-06-11 stsp struct got_reference *head_ref;
611 9ba79e04 2018-06-11 stsp *head_id = NULL;
613 9ba79e04 2018-06-11 stsp err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
615 99db9666 2018-05-07 stsp return err;
617 9ba79e04 2018-06-11 stsp err = got_ref_resolve(head_id, repo, head_ref);
618 9ba79e04 2018-06-11 stsp got_ref_close(head_ref);
620 9ba79e04 2018-06-11 stsp *head_id = NULL;
621 99db9666 2018-05-07 stsp return err;
624 9ba79e04 2018-06-11 stsp return NULL;
627 0553a4e3 2018-04-30 stsp static const struct got_error *
628 2814baeb 2018-08-01 stsp draw_commits(struct tog_view *view, struct commit_queue_entry **last,
629 ecb28ae0 2018-07-16 stsp struct commit_queue_entry **selected, struct commit_queue_entry *first,
630 a7f40148 2018-07-18 stsp struct commit_queue *commits, int selected_idx, int limit,
631 a7f40148 2018-07-18 stsp struct got_commit_graph *graph, struct got_repository *repo,
632 a7f40148 2018-07-18 stsp const char *path)
634 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
635 0553a4e3 2018-04-30 stsp struct commit_queue_entry *entry;
636 ecb28ae0 2018-07-16 stsp int ncommits, width;
637 867c6645 2018-07-10 stsp char *id_str, *header;
638 ecb28ae0 2018-07-16 stsp wchar_t *wline;
640 e0d42f60 2018-07-22 stsp entry = first;
641 e0d42f60 2018-07-22 stsp ncommits = 0;
642 e0d42f60 2018-07-22 stsp while (entry) {
643 e0d42f60 2018-07-22 stsp if (ncommits == selected_idx) {
644 e0d42f60 2018-07-22 stsp *selected = entry;
647 e0d42f60 2018-07-22 stsp entry = TAILQ_NEXT(entry, entry);
648 e0d42f60 2018-07-22 stsp ncommits++;
651 867c6645 2018-07-10 stsp err = got_object_id_str(&id_str, (*selected)->id);
653 867c6645 2018-07-10 stsp return err;
655 c3ba6f36 2018-08-01 stsp if (path && strcmp(path, "/") != 0) {
656 ecb28ae0 2018-07-16 stsp if (asprintf(&header, "commit: %s [%s]", id_str, path) == -1) {
657 ecb28ae0 2018-07-16 stsp err = got_error_from_errno();
658 ecb28ae0 2018-07-16 stsp free(id_str);
659 ecb28ae0 2018-07-16 stsp return err;
661 ecb28ae0 2018-07-16 stsp } else if (asprintf(&header, "commit: %s", id_str) == -1) {
662 867c6645 2018-07-10 stsp err = got_error_from_errno();
663 867c6645 2018-07-10 stsp free(id_str);
664 867c6645 2018-07-10 stsp return err;
666 ecb28ae0 2018-07-16 stsp free(id_str);
667 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, header, view->ncols);
669 ecb28ae0 2018-07-16 stsp free(header);
670 ecb28ae0 2018-07-16 stsp return err;
672 ecb28ae0 2018-07-16 stsp free(header);
674 2814baeb 2018-08-01 stsp werase(view->window);
676 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
677 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
678 2814baeb 2018-08-01 stsp waddch(view->window, '\n');
679 ecb28ae0 2018-07-16 stsp free(wline);
680 ecb28ae0 2018-07-16 stsp if (limit <= 1)
681 ecb28ae0 2018-07-16 stsp return NULL;
683 899d86c2 2018-05-10 stsp entry = first;
684 899d86c2 2018-05-10 stsp *last = first;
685 867c6645 2018-07-10 stsp ncommits = 0;
686 899d86c2 2018-05-10 stsp while (entry) {
687 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
689 e0d42f60 2018-07-22 stsp if (ncommits == selected_idx)
690 2814baeb 2018-08-01 stsp wstandout(view->window);
691 2814baeb 2018-08-01 stsp err = draw_commit(view, entry->commit, entry->id);
692 cd0acaa7 2018-05-20 stsp if (ncommits == selected_idx)
693 2814baeb 2018-08-01 stsp wstandend(view->window);
696 0553a4e3 2018-04-30 stsp ncommits++;
697 899d86c2 2018-05-10 stsp *last = entry;
698 a7f40148 2018-07-18 stsp if (entry == TAILQ_LAST(&commits->head, commit_queue_head)) {
699 a7f40148 2018-07-18 stsp err = queue_commits(graph, commits, entry->id, 1,
700 a7f40148 2018-07-18 stsp 0, repo, path);
702 a7f40148 2018-07-18 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
703 a7f40148 2018-07-18 stsp return err;
704 a7f40148 2018-07-18 stsp err = NULL;
707 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
710 80ddbec8 2018-04-29 stsp update_panels();
711 80ddbec8 2018-04-29 stsp doupdate();
713 80ddbec8 2018-04-29 stsp return err;
716 07b55e75 2018-05-10 stsp static void
717 16482c3b 2018-05-20 stsp scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
718 07b55e75 2018-05-10 stsp struct commit_queue *commits)
720 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
721 07b55e75 2018-05-10 stsp int nscrolled = 0;
723 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
724 07b55e75 2018-05-10 stsp if (*first_displayed_entry == entry)
727 07b55e75 2018-05-10 stsp entry = *first_displayed_entry;
728 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
729 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
730 07b55e75 2018-05-10 stsp if (entry) {
731 07b55e75 2018-05-10 stsp *first_displayed_entry = entry;
732 07b55e75 2018-05-10 stsp nscrolled++;
737 aa075928 2018-05-10 stsp static const struct got_error *
738 16482c3b 2018-05-20 stsp scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
739 aa075928 2018-05-10 stsp struct commit_queue_entry *last_displayed_entry,
740 9ba79e04 2018-06-11 stsp struct commit_queue *commits, struct got_commit_graph *graph,
741 ecb28ae0 2018-07-16 stsp struct got_repository *repo, const char *path)
743 aa075928 2018-05-10 stsp const struct got_error *err = NULL;
744 dd0a52c1 2018-05-20 stsp struct commit_queue_entry *pentry;
745 aa075928 2018-05-10 stsp int nscrolled = 0;
748 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(last_displayed_entry, entry);
749 aa075928 2018-05-10 stsp if (pentry == NULL) {
750 9ba79e04 2018-06-11 stsp err = fetch_next_commit(&pentry, last_displayed_entry,
751 ecb28ae0 2018-07-16 stsp commits, graph, repo, path);
752 9a6bf2a5 2018-05-20 stsp if (err || pentry == NULL)
755 dd0a52c1 2018-05-20 stsp last_displayed_entry = pentry;
757 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(*first_displayed_entry, entry);
758 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
760 aa075928 2018-05-10 stsp *first_displayed_entry = pentry;
761 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
763 dd0a52c1 2018-05-20 stsp return err;
766 cd0acaa7 2018-05-20 stsp static const struct got_error *
767 6d0fee91 2018-08-01 stsp show_commit(struct tog_view *parent_view, struct commit_queue_entry *entry,
768 6d0fee91 2018-08-01 stsp struct got_repository *repo)
770 cd0acaa7 2018-05-20 stsp const struct got_error *err;
771 cd0acaa7 2018-05-20 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
772 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
773 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
775 cd0acaa7 2018-05-20 stsp err = got_object_open(&obj2, repo, entry->id);
777 cd0acaa7 2018-05-20 stsp return err;
779 9ba79e04 2018-06-11 stsp parent_id = SIMPLEQ_FIRST(&entry->commit->parent_ids);
780 9ba79e04 2018-06-11 stsp if (parent_id) {
781 9ba79e04 2018-06-11 stsp err = got_object_open(&obj1, repo, parent_id->id);
786 b3665f43 2018-08-04 stsp view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_DIFF);
787 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
788 ea5e7bb5 2018-08-01 stsp err = got_error_from_errno();
792 5dc9f4bc 2018-08-04 stsp err = open_diff_view(view, obj1, obj2, repo);
795 5dc9f4bc 2018-08-04 stsp err = show_diff_view(view);
796 5dc9f4bc 2018-08-04 stsp close_diff_view(view);
797 96a765a8 2018-08-04 stsp view_close(view);
798 6acd1bd1 2018-08-04 stsp view_show(parent_view);
801 cd0acaa7 2018-05-20 stsp got_object_close(obj1);
803 cd0acaa7 2018-05-20 stsp got_object_close(obj2);
804 cd0acaa7 2018-05-20 stsp return err;
807 80ddbec8 2018-04-29 stsp static const struct got_error *
808 6d0fee91 2018-08-01 stsp browse_commit(struct tog_view *parent_view, struct commit_queue_entry *entry,
809 6d0fee91 2018-08-01 stsp struct got_repository *repo)
811 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
812 9343a5fb 2018-06-23 stsp struct got_tree_object *tree;
813 5221c383 2018-08-01 stsp struct tog_view *view;
815 9343a5fb 2018-06-23 stsp err = got_object_open_as_tree(&tree, repo, entry->commit->tree_id);
817 9343a5fb 2018-06-23 stsp return err;
819 b3665f43 2018-08-04 stsp view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_TREE);
820 5221c383 2018-08-01 stsp if (view == NULL) {
821 5221c383 2018-08-01 stsp err = got_error_from_errno();
824 5221c383 2018-08-01 stsp err = show_tree_view(view, tree, entry->id, repo);
825 96a765a8 2018-08-04 stsp view_close(view);
826 6acd1bd1 2018-08-04 stsp view_show(parent_view);
828 9343a5fb 2018-06-23 stsp got_object_tree_close(tree);
829 9343a5fb 2018-06-23 stsp return err;
832 9343a5fb 2018-06-23 stsp static const struct got_error *
833 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
834 04cc582a 2018-08-01 stsp struct got_repository *repo, const char *path)
836 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
837 9ba79e04 2018-06-11 stsp struct got_object_id *head_id = NULL;
838 ba4f502b 2018-08-04 stsp int nfetched;
839 4d7951a5 2018-08-04 stsp struct tog_log_view_state *state = &view->state.log;
841 4d7951a5 2018-08-04 stsp err = got_repo_map_path(&state->in_repo_path, repo, path);
842 ecb28ae0 2018-07-16 stsp if (err != NULL)
845 9ba79e04 2018-06-11 stsp err = get_head_commit_id(&head_id, repo);
847 9ba79e04 2018-06-11 stsp return err;
849 034e3b69 2018-07-22 stsp /* The graph contains all commits. */
850 4d7951a5 2018-08-04 stsp err = got_commit_graph_open(&state->graph, head_id, 0, repo);
853 034e3b69 2018-07-22 stsp /* The commit queue contains a subset of commits filtered by path. */
854 4d7951a5 2018-08-04 stsp TAILQ_INIT(&state->commits.head);
855 4d7951a5 2018-08-04 stsp state->commits.ncommits = 0;
857 9ba79e04 2018-06-11 stsp /* Populate commit graph with a sufficient number of commits. */
858 ba4f502b 2018-08-04 stsp err = got_commit_graph_fetch_commits_up_to(&nfetched,
859 4d7951a5 2018-08-04 stsp state->graph, start_id, repo);
864 9ba79e04 2018-06-11 stsp * Open the initial batch of commits, sorted in commit graph order.
865 9ba79e04 2018-06-11 stsp * We keep all commits open throughout the lifetime of the log view
866 9ba79e04 2018-06-11 stsp * in order to avoid having to re-fetch commits from disk while
867 9ba79e04 2018-06-11 stsp * updating the display.
869 4d7951a5 2018-08-04 stsp err = queue_commits(state->graph, &state->commits, start_id,
870 4d7951a5 2018-08-04 stsp view->nlines, 1, repo, state->in_repo_path);
872 55198a88 2018-07-16 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
874 55198a88 2018-07-16 stsp err = NULL;
877 4d7951a5 2018-08-04 stsp state->repo = repo;
879 ba4f502b 2018-08-04 stsp free(head_id);
880 ba4f502b 2018-08-04 stsp return err;
883 ba4f502b 2018-08-04 stsp static void close_log_view(struct tog_view *view)
885 4d7951a5 2018-08-04 stsp struct tog_log_view_state *state = &view->state.log;
887 4d7951a5 2018-08-04 stsp if (state->graph)
888 4d7951a5 2018-08-04 stsp got_commit_graph_close(state->graph);
889 4d7951a5 2018-08-04 stsp free_commits(&state->commits);
890 4d7951a5 2018-08-04 stsp free(state->in_repo_path);
893 ba4f502b 2018-08-04 stsp static const struct got_error *
894 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
896 ba4f502b 2018-08-04 stsp const struct got_error *err = NULL;
897 ba4f502b 2018-08-04 stsp int ch, done = 0;
898 4d7951a5 2018-08-04 stsp struct tog_log_view_state *state = &view->state.log;
900 6acd1bd1 2018-08-04 stsp view_show(view);
902 4d7951a5 2018-08-04 stsp state->first_displayed_entry =
903 4d7951a5 2018-08-04 stsp TAILQ_FIRST(&state->commits.head);
904 4d7951a5 2018-08-04 stsp state->selected_entry = state->first_displayed_entry;
905 899d86c2 2018-05-10 stsp while (!done) {
906 4d7951a5 2018-08-04 stsp err = draw_commits(view, &state->last_displayed_entry,
907 4d7951a5 2018-08-04 stsp &state->selected_entry, state->first_displayed_entry,
908 4d7951a5 2018-08-04 stsp &state->commits, state->selected, view->nlines,
909 4d7951a5 2018-08-04 stsp state->graph, state->repo, state->in_repo_path);
913 80ddbec8 2018-04-29 stsp nodelay(stdscr, FALSE);
914 2814baeb 2018-08-01 stsp ch = wgetch(view->window);
915 f7182337 2018-05-20 stsp nodelay(stdscr, TRUE);
916 80ddbec8 2018-04-29 stsp switch (ch) {
923 80ddbec8 2018-04-29 stsp case KEY_UP:
924 4d7951a5 2018-08-04 stsp if (state->selected > 0)
925 4d7951a5 2018-08-04 stsp state->selected--;
926 4d7951a5 2018-08-04 stsp if (state->selected > 0)
928 4d7951a5 2018-08-04 stsp scroll_up(&state->first_displayed_entry, 1,
929 4d7951a5 2018-08-04 stsp &state->commits);
931 48531068 2018-05-10 stsp case KEY_PPAGE:
932 4d7951a5 2018-08-04 stsp if (TAILQ_FIRST(&state->commits.head) ==
933 4d7951a5 2018-08-04 stsp state->first_displayed_entry) {
934 4d7951a5 2018-08-04 stsp state->selected = 0;
937 4d7951a5 2018-08-04 stsp scroll_up(&state->first_displayed_entry,
938 4d7951a5 2018-08-04 stsp view->nlines, &state->commits);
941 80ddbec8 2018-04-29 stsp case KEY_DOWN:
942 4d7951a5 2018-08-04 stsp if (state->selected < MIN(view->nlines - 2,
943 4d7951a5 2018-08-04 stsp state->commits.ncommits - 1)) {
944 4d7951a5 2018-08-04 stsp state->selected++;
947 4d7951a5 2018-08-04 stsp err = scroll_down(&state->first_displayed_entry,
948 4d7951a5 2018-08-04 stsp 1, state->last_displayed_entry,
949 4d7951a5 2018-08-04 stsp &state->commits, state->graph, state->repo,
950 4d7951a5 2018-08-04 stsp state->in_repo_path);
952 ecb28ae0 2018-07-16 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
954 ecb28ae0 2018-07-16 stsp err = NULL;
957 ecb28ae0 2018-07-16 stsp case KEY_NPAGE: {
958 c70c5802 2018-08-01 stsp struct commit_queue_entry *first;
959 4d7951a5 2018-08-04 stsp first = state->first_displayed_entry;
960 4d7951a5 2018-08-04 stsp err = scroll_down(&state->first_displayed_entry,
961 4d7951a5 2018-08-04 stsp view->nlines, state->last_displayed_entry,
962 4d7951a5 2018-08-04 stsp &state->commits, state->graph, state->repo,
963 4d7951a5 2018-08-04 stsp state->in_repo_path);
964 4d7951a5 2018-08-04 stsp if (err == NULL)
966 4d7951a5 2018-08-04 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
968 4d7951a5 2018-08-04 stsp if (first == state->first_displayed_entry &&
969 4d7951a5 2018-08-04 stsp state->selected < MIN(view->nlines - 2,
970 4d7951a5 2018-08-04 stsp state->commits.ncommits - 1)) {
971 4d7951a5 2018-08-04 stsp /* can't scroll further down */
972 4d7951a5 2018-08-04 stsp state->selected = MIN(view->nlines - 2,
973 4d7951a5 2018-08-04 stsp state->commits.ncommits - 1);
975 4d7951a5 2018-08-04 stsp err = NULL;
978 d6df9be4 2018-04-30 stsp case KEY_RESIZE:
979 a41d2007 2018-08-01 stsp err = view_resize(view);
982 4d7951a5 2018-08-04 stsp if (state->selected > view->nlines - 2)
983 4d7951a5 2018-08-04 stsp state->selected = view->nlines - 2;
984 4d7951a5 2018-08-04 stsp if (state->selected >
985 4d7951a5 2018-08-04 stsp state->commits.ncommits - 1)
986 4d7951a5 2018-08-04 stsp state->selected =
987 4d7951a5 2018-08-04 stsp state->commits.ncommits - 1;
989 cd0acaa7 2018-05-20 stsp case KEY_ENTER:
991 4d7951a5 2018-08-04 stsp err = show_commit(view, state->selected_entry,
992 4d7951a5 2018-08-04 stsp state->repo);
995 6acd1bd1 2018-08-04 stsp view_show(view);
998 4d7951a5 2018-08-04 stsp err = browse_commit(view, state->selected_entry,
999 4d7951a5 2018-08-04 stsp state->repo);
1001 0d4100bb 2018-06-23 stsp goto done;
1002 6acd1bd1 2018-08-04 stsp view_show(view);
1009 80ddbec8 2018-04-29 stsp return err;
1012 4ed7e80c 2018-05-20 stsp static const struct got_error *
1013 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
1015 80ddbec8 2018-04-29 stsp const struct got_error *error;
1016 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
1017 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
1018 ecb28ae0 2018-07-16 stsp char *path = NULL, *repo_path = NULL, *cwd = NULL;
1019 80ddbec8 2018-04-29 stsp char *start_commit = NULL;
1021 04cc582a 2018-08-01 stsp struct tog_view *view;
1023 80ddbec8 2018-04-29 stsp #ifndef PROFILE
1024 80ddbec8 2018-04-29 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1025 80ddbec8 2018-04-29 stsp err(1, "pledge");
1028 ecb28ae0 2018-07-16 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1029 80ddbec8 2018-04-29 stsp switch (ch) {
1031 80ddbec8 2018-04-29 stsp start_commit = optarg;
1034 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
1035 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
1036 ecb28ae0 2018-07-16 stsp err(1, "-r option");
1040 80ddbec8 2018-04-29 stsp /* NOTREACHED */
1044 80ddbec8 2018-04-29 stsp argc -= optind;
1045 80ddbec8 2018-04-29 stsp argv += optind;
1047 ecb28ae0 2018-07-16 stsp if (argc == 0)
1048 ecb28ae0 2018-07-16 stsp path = strdup("");
1049 ecb28ae0 2018-07-16 stsp else if (argc == 1)
1050 ecb28ae0 2018-07-16 stsp path = strdup(argv[0]);
1052 80ddbec8 2018-04-29 stsp usage_log();
1053 ecb28ae0 2018-07-16 stsp if (path == NULL)
1054 ecb28ae0 2018-07-16 stsp return got_error_from_errno();
1056 ecb28ae0 2018-07-16 stsp cwd = getcwd(NULL, 0);
1057 ecb28ae0 2018-07-16 stsp if (cwd == NULL) {
1058 ecb28ae0 2018-07-16 stsp error = got_error_from_errno();
1059 ecb28ae0 2018-07-16 stsp goto done;
1061 ecb28ae0 2018-07-16 stsp if (repo_path == NULL) {
1062 ecb28ae0 2018-07-16 stsp repo_path = strdup(cwd);
1063 ecb28ae0 2018-07-16 stsp if (repo_path == NULL) {
1064 ecb28ae0 2018-07-16 stsp error = got_error_from_errno();
1065 ecb28ae0 2018-07-16 stsp goto done;
1069 80ddbec8 2018-04-29 stsp error = got_repo_open(&repo, repo_path);
1070 80ddbec8 2018-04-29 stsp if (error != NULL)
1071 ecb28ae0 2018-07-16 stsp goto done;
1073 80ddbec8 2018-04-29 stsp if (start_commit == NULL) {
1074 899d86c2 2018-05-10 stsp error = get_head_commit_id(&start_id, repo);
1075 80ddbec8 2018-04-29 stsp if (error != NULL)
1076 ecb28ae0 2018-07-16 stsp goto done;
1078 80ddbec8 2018-04-29 stsp struct got_object *obj;
1079 80ddbec8 2018-04-29 stsp error = got_object_open_by_id_str(&obj, repo, start_commit);
1080 80ddbec8 2018-04-29 stsp if (error == NULL) {
1081 899d86c2 2018-05-10 stsp start_id = got_object_get_id(obj);
1082 899d86c2 2018-05-10 stsp if (start_id == NULL)
1083 80ddbec8 2018-04-29 stsp error = got_error_from_errno();
1084 ecb28ae0 2018-07-16 stsp goto done;
1087 80ddbec8 2018-04-29 stsp if (error != NULL)
1088 ecb28ae0 2018-07-16 stsp goto done;
1090 b3665f43 2018-08-04 stsp view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_LOG);
1091 04cc582a 2018-08-01 stsp if (view == NULL) {
1092 04cc582a 2018-08-01 stsp error = got_error_from_errno();
1093 04cc582a 2018-08-01 stsp goto done;
1095 ba4f502b 2018-08-04 stsp error = open_log_view(view, start_id, repo, path);
1096 ba4f502b 2018-08-04 stsp if (error)
1097 ba4f502b 2018-08-04 stsp goto done;
1098 ba4f502b 2018-08-04 stsp error = show_log_view(view);
1099 ba4f502b 2018-08-04 stsp close_log_view(view);
1100 96a765a8 2018-08-04 stsp view_close(view);
1102 ecb28ae0 2018-07-16 stsp free(repo_path);
1103 ecb28ae0 2018-07-16 stsp free(cwd);
1104 ecb28ae0 2018-07-16 stsp free(path);
1105 899d86c2 2018-05-10 stsp free(start_id);
1107 ecb28ae0 2018-07-16 stsp got_repo_close(repo);
1108 80ddbec8 2018-04-29 stsp return error;
1111 4ed7e80c 2018-05-20 stsp __dead static void
1112 9f7d7167 2018-04-29 stsp usage_diff(void)
1115 9f7d7167 2018-04-29 stsp fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1116 9f7d7167 2018-04-29 stsp getprogname());
1120 b304db33 2018-05-20 stsp static char *
1121 b304db33 2018-05-20 stsp parse_next_line(FILE *f, size_t *len)
1123 b304db33 2018-05-20 stsp char *line;
1124 b304db33 2018-05-20 stsp size_t linelen;
1125 b304db33 2018-05-20 stsp size_t lineno;
1126 b304db33 2018-05-20 stsp const char delim[3] = { '\0', '\0', '\0'};
1128 b304db33 2018-05-20 stsp line = fparseln(f, &linelen, &lineno, delim, 0);
1130 b304db33 2018-05-20 stsp *len = linelen;
1131 b304db33 2018-05-20 stsp return line;
1134 4ed7e80c 2018-05-20 stsp static const struct got_error *
1135 f7d12f7e 2018-08-01 stsp draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1136 a70480e0 2018-06-23 stsp int *last_displayed_line, int *eof, int max_lines)
1138 61e69b96 2018-05-20 stsp const struct got_error *err;
1139 26ed57b2 2018-05-19 stsp int nlines = 0, nprinted = 0;
1140 b304db33 2018-05-20 stsp char *line;
1141 b304db33 2018-05-20 stsp size_t len;
1142 61e69b96 2018-05-20 stsp wchar_t *wline;
1143 e0b650dd 2018-05-20 stsp int width;
1145 26ed57b2 2018-05-19 stsp rewind(f);
1146 f7d12f7e 2018-08-01 stsp werase(view->window);
1149 26ed57b2 2018-05-19 stsp while (nprinted < max_lines) {
1150 b304db33 2018-05-20 stsp line = parse_next_line(f, &len);
1151 26ed57b2 2018-05-19 stsp if (line == NULL) {
1155 26ed57b2 2018-05-19 stsp if (++nlines < *first_displayed_line) {
1156 26ed57b2 2018-05-19 stsp free(line);
1160 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
1161 61e69b96 2018-05-20 stsp if (err) {
1162 61e69b96 2018-05-20 stsp free(line);
1163 2550e4c3 2018-07-13 stsp free(wline);
1164 61e69b96 2018-05-20 stsp return err;
1166 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
1167 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
1168 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
1169 26ed57b2 2018-05-19 stsp if (++nprinted == 1)
1170 26ed57b2 2018-05-19 stsp *first_displayed_line = nlines;
1171 26ed57b2 2018-05-19 stsp free(line);
1172 2550e4c3 2018-07-13 stsp free(wline);
1173 2550e4c3 2018-07-13 stsp wline = NULL;
1175 26ed57b2 2018-05-19 stsp *last_displayed_line = nlines;
1177 26ed57b2 2018-05-19 stsp update_panels();
1178 26ed57b2 2018-05-19 stsp doupdate();
1180 26ed57b2 2018-05-19 stsp return NULL;
1183 4ed7e80c 2018-05-20 stsp static const struct got_error *
1184 5dc9f4bc 2018-08-04 stsp open_diff_view(struct tog_view *view, struct got_object *obj1,
1185 ea5e7bb5 2018-08-01 stsp struct got_object *obj2, struct got_repository *repo)
1187 26ed57b2 2018-05-19 stsp const struct got_error *err;
1190 cd0acaa7 2018-05-20 stsp if (obj1 != NULL && obj2 != NULL &&
1191 cd0acaa7 2018-05-20 stsp got_object_get_type(obj1) != got_object_get_type(obj2))
1192 26ed57b2 2018-05-19 stsp return got_error(GOT_ERR_OBJ_TYPE);
1194 511a516b 2018-05-19 stsp f = got_opentemp();
1195 26ed57b2 2018-05-19 stsp if (f == NULL)
1196 26ed57b2 2018-05-19 stsp return got_error_from_errno();
1198 cd0acaa7 2018-05-20 stsp switch (got_object_get_type(obj1 ? obj1 : obj2)) {
1199 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
1200 11528a82 2018-05-19 stsp err = got_diff_objects_as_blobs(obj1, obj2, repo, f);
1202 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
1203 11528a82 2018-05-19 stsp err = got_diff_objects_as_trees(obj1, obj2, repo, f);
1205 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_COMMIT:
1206 11528a82 2018-05-19 stsp err = got_diff_objects_as_commits(obj1, obj2, repo, f);
1209 26ed57b2 2018-05-19 stsp return got_error(GOT_ERR_OBJ_TYPE);
1212 26ed57b2 2018-05-19 stsp fflush(f);
1214 5dc9f4bc 2018-08-04 stsp view->state.diff.f = f;
1215 5dc9f4bc 2018-08-04 stsp view->state.diff.first_displayed_line = 1;
1216 5dc9f4bc 2018-08-04 stsp view->state.diff.last_displayed_line = view->nlines;
1218 5dc9f4bc 2018-08-04 stsp return NULL;
1221 5dc9f4bc 2018-08-04 stsp static void
1222 5dc9f4bc 2018-08-04 stsp close_diff_view(struct tog_view *view)
1224 5dc9f4bc 2018-08-04 stsp fclose(view->state.diff.f);
1227 5dc9f4bc 2018-08-04 stsp static const struct got_error *
1228 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
1230 5dc9f4bc 2018-08-04 stsp const struct got_error *err = NULL;
1231 5dc9f4bc 2018-08-04 stsp int ch, done = 0;
1232 5dc9f4bc 2018-08-04 stsp int eof, i;
1233 c90732a0 2018-08-04 stsp struct tog_diff_view_state *state = &view->state.diff;
1235 6acd1bd1 2018-08-04 stsp view_show(view);
1237 26ed57b2 2018-05-19 stsp while (!done) {
1238 c90732a0 2018-08-04 stsp err = draw_file(view, state->f, &state->first_displayed_line,
1239 c90732a0 2018-08-04 stsp &state->last_displayed_line, &eof, view->nlines);
1242 26ed57b2 2018-05-19 stsp nodelay(stdscr, FALSE);
1243 ea5e7bb5 2018-08-01 stsp ch = wgetch(view->window);
1244 f7182337 2018-05-20 stsp nodelay(stdscr, TRUE);
1245 26ed57b2 2018-05-19 stsp switch (ch) {
1250 26ed57b2 2018-05-19 stsp case KEY_UP:
1251 c90732a0 2018-08-04 stsp if (state->first_displayed_line > 1)
1252 c90732a0 2018-08-04 stsp state->first_displayed_line--;
1254 b304db33 2018-05-20 stsp case KEY_PPAGE:
1255 c46b9352 2018-07-12 stsp case KEY_BACKSPACE:
1257 f7d12f7e 2018-08-01 stsp while (i++ < view->nlines - 1 &&
1258 c90732a0 2018-08-04 stsp state->first_displayed_line > 1)
1259 c90732a0 2018-08-04 stsp state->first_displayed_line--;
1262 26ed57b2 2018-05-19 stsp case KEY_DOWN:
1264 c90732a0 2018-08-04 stsp state->first_displayed_line++;
1266 b304db33 2018-05-20 stsp case KEY_NPAGE:
1269 f7d12f7e 2018-08-01 stsp while (!eof && i++ < view->nlines - 1) {
1270 5dc9f4bc 2018-08-04 stsp char *line = parse_next_line(
1271 c90732a0 2018-08-04 stsp state->f, NULL);
1272 c90732a0 2018-08-04 stsp state->first_displayed_line++;
1273 b304db33 2018-05-20 stsp if (line == NULL)
1277 82357c0a 2018-08-01 stsp case KEY_RESIZE:
1278 a41d2007 2018-08-01 stsp err = view_resize(view);
1280 a41d2007 2018-08-01 stsp goto done;
1287 26ed57b2 2018-05-19 stsp return err;
1290 4ed7e80c 2018-05-20 stsp static const struct got_error *
1291 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
1293 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
1294 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
1295 26ed57b2 2018-05-19 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
1296 26ed57b2 2018-05-19 stsp char *repo_path = NULL;
1297 26ed57b2 2018-05-19 stsp char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
1299 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
1301 26ed57b2 2018-05-19 stsp #ifndef PROFILE
1302 26ed57b2 2018-05-19 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1303 26ed57b2 2018-05-19 stsp err(1, "pledge");
1306 26ed57b2 2018-05-19 stsp while ((ch = getopt(argc, argv, "")) != -1) {
1307 26ed57b2 2018-05-19 stsp switch (ch) {
1310 26ed57b2 2018-05-19 stsp /* NOTREACHED */
1314 26ed57b2 2018-05-19 stsp argc -= optind;
1315 26ed57b2 2018-05-19 stsp argv += optind;
1317 26ed57b2 2018-05-19 stsp if (argc == 0) {
1318 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
1319 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
1320 26ed57b2 2018-05-19 stsp repo_path = getcwd(NULL, 0);
1321 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
1322 26ed57b2 2018-05-19 stsp return got_error_from_errno();
1323 26ed57b2 2018-05-19 stsp obj_id_str1 = argv[0];
1324 26ed57b2 2018-05-19 stsp obj_id_str2 = argv[1];
1325 26ed57b2 2018-05-19 stsp } else if (argc == 3) {
1326 26ed57b2 2018-05-19 stsp repo_path = realpath(argv[0], NULL);
1327 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
1328 26ed57b2 2018-05-19 stsp return got_error_from_errno();
1329 26ed57b2 2018-05-19 stsp obj_id_str1 = argv[1];
1330 26ed57b2 2018-05-19 stsp obj_id_str2 = argv[2];
1332 26ed57b2 2018-05-19 stsp usage_diff();
1334 26ed57b2 2018-05-19 stsp error = got_repo_open(&repo, repo_path);
1335 26ed57b2 2018-05-19 stsp free(repo_path);
1336 26ed57b2 2018-05-19 stsp if (error)
1337 26ed57b2 2018-05-19 stsp goto done;
1339 26ed57b2 2018-05-19 stsp error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
1340 26ed57b2 2018-05-19 stsp if (error)
1341 26ed57b2 2018-05-19 stsp goto done;
1343 26ed57b2 2018-05-19 stsp error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1344 26ed57b2 2018-05-19 stsp if (error)
1345 26ed57b2 2018-05-19 stsp goto done;
1347 b3665f43 2018-08-04 stsp view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_DIFF);
1348 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
1349 ea5e7bb5 2018-08-01 stsp error = got_error_from_errno();
1350 ea5e7bb5 2018-08-01 stsp goto done;
1352 5dc9f4bc 2018-08-04 stsp error = open_diff_view(view, obj1, obj2, repo);
1353 5dc9f4bc 2018-08-04 stsp if (error)
1354 5dc9f4bc 2018-08-04 stsp goto done;
1355 5dc9f4bc 2018-08-04 stsp error = show_diff_view(view);
1356 5dc9f4bc 2018-08-04 stsp close_diff_view(view);
1357 96a765a8 2018-08-04 stsp view_close(view);
1359 26ed57b2 2018-05-19 stsp got_repo_close(repo);
1361 26ed57b2 2018-05-19 stsp got_object_close(obj1);
1363 26ed57b2 2018-05-19 stsp got_object_close(obj2);
1364 26ed57b2 2018-05-19 stsp return error;
1367 4ed7e80c 2018-05-20 stsp __dead static void
1368 9f7d7167 2018-04-29 stsp usage_blame(void)
1371 69069811 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
1372 9f7d7167 2018-04-29 stsp getprogname());
1376 84451b3e 2018-07-10 stsp struct tog_blame_line {
1377 84451b3e 2018-07-10 stsp int annotated;
1378 84451b3e 2018-07-10 stsp struct got_object_id *id;
1381 4ed7e80c 2018-05-20 stsp static const struct got_error *
1382 f7d12f7e 2018-08-01 stsp draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
1383 f7d12f7e 2018-08-01 stsp const char *path, struct tog_blame_line *lines, int nlines,
1384 f7d12f7e 2018-08-01 stsp int blame_complete, int selected_line, int *first_displayed_line,
1385 f7d12f7e 2018-08-01 stsp int *last_displayed_line, int *eof, int max_lines)
1387 84451b3e 2018-07-10 stsp const struct got_error *err;
1388 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
1389 84451b3e 2018-07-10 stsp char *line;
1390 84451b3e 2018-07-10 stsp size_t len;
1391 84451b3e 2018-07-10 stsp wchar_t *wline;
1392 b700b5d6 2018-07-10 stsp int width, wlimit;
1393 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
1394 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
1395 ab089a2a 2018-07-12 stsp char *id_str;
1397 ab089a2a 2018-07-12 stsp err = got_object_id_str(&id_str, id);
1399 ab089a2a 2018-07-12 stsp return err;
1401 84451b3e 2018-07-10 stsp rewind(f);
1402 f7d12f7e 2018-08-01 stsp werase(view->window);
1404 ab089a2a 2018-07-12 stsp if (asprintf(&line, "commit: %s", id_str) == -1) {
1405 ab089a2a 2018-07-12 stsp err = got_error_from_errno();
1406 ab089a2a 2018-07-12 stsp free(id_str);
1407 ab089a2a 2018-07-12 stsp return err;
1410 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
1411 ab089a2a 2018-07-12 stsp free(line);
1412 2550e4c3 2018-07-13 stsp line = NULL;
1413 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
1414 2550e4c3 2018-07-13 stsp free(wline);
1415 2550e4c3 2018-07-13 stsp wline = NULL;
1416 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
1417 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
1419 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
1420 084063cd 2018-07-12 stsp *first_displayed_line - 1 + selected_line, nlines,
1421 12a0b23b 2018-07-12 stsp blame_complete ? "" : "annotating ", path) == -1) {
1422 ab089a2a 2018-07-12 stsp free(id_str);
1423 3f60a8ef 2018-07-10 stsp return got_error_from_errno();
1425 ab089a2a 2018-07-12 stsp free(id_str);
1426 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
1427 3f60a8ef 2018-07-10 stsp free(line);
1428 2550e4c3 2018-07-13 stsp line = NULL;
1430 3f60a8ef 2018-07-10 stsp return err;
1431 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
1432 2550e4c3 2018-07-13 stsp free(wline);
1433 2550e4c3 2018-07-13 stsp wline = NULL;
1434 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
1435 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
1438 ab089a2a 2018-07-12 stsp while (nprinted < max_lines - 2) {
1439 84451b3e 2018-07-10 stsp line = parse_next_line(f, &len);
1440 84451b3e 2018-07-10 stsp if (line == NULL) {
1444 84451b3e 2018-07-10 stsp if (++lineno < *first_displayed_line) {
1445 84451b3e 2018-07-10 stsp free(line);
1449 f7d12f7e 2018-08-01 stsp wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
1450 b700b5d6 2018-07-10 stsp err = format_line(&wline, &width, line, wlimit);
1451 84451b3e 2018-07-10 stsp if (err) {
1452 84451b3e 2018-07-10 stsp free(line);
1453 84451b3e 2018-07-10 stsp return err;
1456 b700b5d6 2018-07-10 stsp if (nprinted == selected_line - 1)
1457 f7d12f7e 2018-08-01 stsp wstandout(view->window);
1459 84451b3e 2018-07-10 stsp blame_line = &lines[lineno - 1];
1460 ee41ec32 2018-07-10 stsp if (blame_line->annotated && prev_id &&
1461 ee41ec32 2018-07-10 stsp got_object_id_cmp(prev_id, blame_line->id) == 0)
1462 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ");
1463 ee41ec32 2018-07-10 stsp else if (blame_line->annotated) {
1464 84451b3e 2018-07-10 stsp char *id_str;
1465 84451b3e 2018-07-10 stsp err = got_object_id_str(&id_str, blame_line->id);
1466 84451b3e 2018-07-10 stsp if (err) {
1467 84451b3e 2018-07-10 stsp free(line);
1468 2550e4c3 2018-07-13 stsp free(wline);
1469 84451b3e 2018-07-10 stsp return err;
1471 f7d12f7e 2018-08-01 stsp wprintw(view->window, "%.8s ", id_str);
1472 84451b3e 2018-07-10 stsp free(id_str);
1473 ee41ec32 2018-07-10 stsp prev_id = blame_line->id;
1475 f7d12f7e 2018-08-01 stsp waddstr(view->window, "........ ");
1476 ee41ec32 2018-07-10 stsp prev_id = NULL;
1479 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
1480 b700b5d6 2018-07-10 stsp while (width < wlimit) {
1481 f7d12f7e 2018-08-01 stsp waddch(view->window, ' ');
1484 b700b5d6 2018-07-10 stsp if (nprinted == selected_line - 1)
1485 f7d12f7e 2018-08-01 stsp wstandend(view->window);
1486 84451b3e 2018-07-10 stsp if (++nprinted == 1)
1487 84451b3e 2018-07-10 stsp *first_displayed_line = lineno;
1488 84451b3e 2018-07-10 stsp free(line);
1489 2550e4c3 2018-07-13 stsp free(wline);
1490 2550e4c3 2018-07-13 stsp wline = NULL;
1492 84451b3e 2018-07-10 stsp *last_displayed_line = lineno;
1494 84451b3e 2018-07-10 stsp update_panels();
1495 84451b3e 2018-07-10 stsp doupdate();
1497 84451b3e 2018-07-10 stsp return NULL;
1500 84451b3e 2018-07-10 stsp struct tog_blame_cb_args {
1501 84451b3e 2018-07-10 stsp pthread_mutex_t *mutex;
1502 84451b3e 2018-07-10 stsp struct tog_blame_line *lines; /* one per line */
1503 84451b3e 2018-07-10 stsp int nlines;
1505 7cc84d77 2018-08-01 stsp struct tog_view *view;
1506 ab089a2a 2018-07-12 stsp struct got_object_id *commit_id;
1508 3f60a8ef 2018-07-10 stsp const char *path;
1509 84451b3e 2018-07-10 stsp int *first_displayed_line;
1510 84451b3e 2018-07-10 stsp int *last_displayed_line;
1511 b700b5d6 2018-07-10 stsp int *selected_line;
1512 18430de3 2018-07-10 stsp int *quit;
1515 84451b3e 2018-07-10 stsp static const struct got_error *
1516 84451b3e 2018-07-10 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
1518 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
1519 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
1520 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
1523 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
1524 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
1525 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
1527 84451b3e 2018-07-10 stsp if (pthread_mutex_lock(a->mutex) != 0)
1528 84451b3e 2018-07-10 stsp return got_error_from_errno();
1530 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
1531 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
1532 d68a0a7d 2018-07-10 stsp goto done;
1535 d68a0a7d 2018-07-10 stsp if (lineno == -1)
1536 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
1538 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
1539 d68a0a7d 2018-07-10 stsp if (line->annotated)
1540 d68a0a7d 2018-07-10 stsp goto done;
1542 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
1543 84451b3e 2018-07-10 stsp if (line->id == NULL) {
1544 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1545 84451b3e 2018-07-10 stsp goto done;
1547 84451b3e 2018-07-10 stsp line->annotated = 1;
1549 f7d12f7e 2018-08-01 stsp err = draw_blame(a->view, a->commit_id, a->f, a->path,
1550 245d91c1 2018-07-12 stsp a->lines, a->nlines, 0, *a->selected_line, a->first_displayed_line,
1551 f7d12f7e 2018-08-01 stsp a->last_displayed_line, &eof, a->view->nlines);
1553 84451b3e 2018-07-10 stsp if (pthread_mutex_unlock(a->mutex) != 0)
1554 84451b3e 2018-07-10 stsp return got_error_from_errno();
1555 84451b3e 2018-07-10 stsp return err;
1558 84451b3e 2018-07-10 stsp struct tog_blame_thread_args {
1559 84451b3e 2018-07-10 stsp const char *path;
1560 84451b3e 2018-07-10 stsp struct got_repository *repo;
1561 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *cb_args;
1562 18430de3 2018-07-10 stsp int *complete;
1565 84451b3e 2018-07-10 stsp static void *
1566 84451b3e 2018-07-10 stsp blame_thread(void *arg)
1568 18430de3 2018-07-10 stsp const struct got_error *err;
1569 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
1570 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
1573 ab089a2a 2018-07-12 stsp err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
1574 245d91c1 2018-07-12 stsp blame_cb, ta->cb_args);
1576 18430de3 2018-07-10 stsp if (pthread_mutex_lock(a->mutex) != 0)
1577 18430de3 2018-07-10 stsp return (void *)got_error_from_errno();
1579 c9beca56 2018-07-22 stsp got_repo_close(ta->repo);
1580 c9beca56 2018-07-22 stsp ta->repo = NULL;
1581 c9beca56 2018-07-22 stsp *ta->complete = 1;
1583 f7d12f7e 2018-08-01 stsp err = draw_blame(a->view, a->commit_id, a->f, a->path,
1584 f7d12f7e 2018-08-01 stsp a->lines, a->nlines, 1, *a->selected_line,
1585 c9beca56 2018-07-22 stsp a->first_displayed_line, a->last_displayed_line, &eof,
1586 f7d12f7e 2018-08-01 stsp a->view->nlines);
1588 18430de3 2018-07-10 stsp if (pthread_mutex_unlock(a->mutex) != 0 && err == NULL)
1589 18430de3 2018-07-10 stsp err = got_error_from_errno();
1591 18430de3 2018-07-10 stsp return (void *)err;
1594 245d91c1 2018-07-12 stsp static struct got_object_id *
1595 245d91c1 2018-07-12 stsp get_selected_commit_id(struct tog_blame_line *lines,
1596 245d91c1 2018-07-12 stsp int first_displayed_line, int selected_line)
1598 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
1600 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
1601 245d91c1 2018-07-12 stsp if (!line->annotated)
1602 245d91c1 2018-07-12 stsp return NULL;
1604 245d91c1 2018-07-12 stsp return line->id;
1607 84451b3e 2018-07-10 stsp static const struct got_error *
1608 245d91c1 2018-07-12 stsp open_selected_commit(struct got_object **pobj, struct got_object **obj,
1609 b880a918 2018-07-10 stsp struct tog_blame_line *lines, int first_displayed_line,
1610 b880a918 2018-07-10 stsp int selected_line, struct got_repository *repo)
1612 b880a918 2018-07-10 stsp const struct got_error *err = NULL;
1613 b880a918 2018-07-10 stsp struct got_commit_object *commit = NULL;
1614 245d91c1 2018-07-12 stsp struct got_object_id *selected_id;
1615 b880a918 2018-07-10 stsp struct got_object_qid *pid;
1617 b880a918 2018-07-10 stsp *pobj = NULL;
1618 b880a918 2018-07-10 stsp *obj = NULL;
1620 245d91c1 2018-07-12 stsp selected_id = get_selected_commit_id(lines,
1621 245d91c1 2018-07-12 stsp first_displayed_line, selected_line);
1622 245d91c1 2018-07-12 stsp if (selected_id == NULL)
1623 b880a918 2018-07-10 stsp return NULL;
1625 245d91c1 2018-07-12 stsp err = got_object_open(obj, repo, selected_id);
1627 b880a918 2018-07-10 stsp goto done;
1629 b880a918 2018-07-10 stsp err = got_object_commit_open(&commit, repo, *obj);
1631 b880a918 2018-07-10 stsp goto done;
1633 b880a918 2018-07-10 stsp pid = SIMPLEQ_FIRST(&commit->parent_ids);
1634 b880a918 2018-07-10 stsp if (pid) {
1635 b880a918 2018-07-10 stsp err = got_object_open(pobj, repo, pid->id);
1637 b880a918 2018-07-10 stsp goto done;
1640 b880a918 2018-07-10 stsp if (commit)
1641 b880a918 2018-07-10 stsp got_object_commit_close(commit);
1642 b880a918 2018-07-10 stsp return err;
1645 245d91c1 2018-07-12 stsp struct tog_blame {
1647 245d91c1 2018-07-12 stsp size_t filesize;
1648 245d91c1 2018-07-12 stsp struct tog_blame_line *lines;
1649 245d91c1 2018-07-12 stsp size_t nlines;
1650 245d91c1 2018-07-12 stsp pthread_t thread;
1651 245d91c1 2018-07-12 stsp struct tog_blame_thread_args thread_args;
1652 245d91c1 2018-07-12 stsp struct tog_blame_cb_args cb_args;
1653 245d91c1 2018-07-12 stsp const char *path;
1656 b880a918 2018-07-10 stsp static const struct got_error *
1657 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
1659 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
1662 245d91c1 2018-07-12 stsp if (blame->thread) {
1663 245d91c1 2018-07-12 stsp if (pthread_join(blame->thread, (void **)&err) != 0)
1664 245d91c1 2018-07-12 stsp err = got_error_from_errno();
1665 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
1666 245d91c1 2018-07-12 stsp err = NULL;
1667 245d91c1 2018-07-12 stsp blame->thread = NULL;
1669 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
1670 245d91c1 2018-07-12 stsp got_repo_close(blame->thread_args.repo);
1671 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
1673 245d91c1 2018-07-12 stsp if (blame->f) {
1674 245d91c1 2018-07-12 stsp fclose(blame->f);
1675 245d91c1 2018-07-12 stsp blame->f = NULL;
1677 245d91c1 2018-07-12 stsp for (i = 0; i < blame->nlines; i++)
1678 245d91c1 2018-07-12 stsp free(blame->lines[i].id);
1679 245d91c1 2018-07-12 stsp free(blame->lines);
1680 245d91c1 2018-07-12 stsp blame->lines = NULL;
1681 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
1682 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
1684 245d91c1 2018-07-12 stsp return err;
1687 245d91c1 2018-07-12 stsp static const struct got_error *
1688 7cc84d77 2018-08-01 stsp run_blame(struct tog_blame *blame, pthread_mutex_t *mutex,
1689 7cc84d77 2018-08-01 stsp struct tog_view *view, int *blame_complete,
1690 245d91c1 2018-07-12 stsp int *first_displayed_line, int *last_displayed_line,
1691 245d91c1 2018-07-12 stsp int *selected_line, int *done, const char *path,
1692 245d91c1 2018-07-12 stsp struct got_object_id *commit_id,
1693 245d91c1 2018-07-12 stsp struct got_repository *repo)
1695 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
1696 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
1697 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
1698 245d91c1 2018-07-12 stsp struct got_object *obj;
1700 84451b3e 2018-07-10 stsp err = got_object_open_by_path(&obj, repo, commit_id, path);
1702 84451b3e 2018-07-10 stsp goto done;
1703 84451b3e 2018-07-10 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
1704 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1705 84451b3e 2018-07-10 stsp goto done;
1708 84451b3e 2018-07-10 stsp err = got_object_blob_open(&blob, repo, obj, 8192);
1710 a70480e0 2018-06-23 stsp goto done;
1711 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
1712 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
1713 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1714 84451b3e 2018-07-10 stsp goto done;
1716 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
1717 245d91c1 2018-07-12 stsp blame->f, blob);
1719 84451b3e 2018-07-10 stsp goto done;
1721 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
1722 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
1723 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1724 84451b3e 2018-07-10 stsp goto done;
1727 245d91c1 2018-07-12 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1729 bd24772e 2018-07-11 stsp goto done;
1731 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
1732 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
1733 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
1734 245d91c1 2018-07-12 stsp blame->cb_args.mutex = mutex;
1735 245d91c1 2018-07-12 stsp blame->cb_args.commit_id = got_object_id_dup(commit_id);
1736 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
1737 245d91c1 2018-07-12 stsp err = got_error_from_errno();
1738 245d91c1 2018-07-12 stsp goto done;
1740 245d91c1 2018-07-12 stsp blame->cb_args.f = blame->f;
1741 245d91c1 2018-07-12 stsp blame->cb_args.path = path;
1742 245d91c1 2018-07-12 stsp blame->cb_args.first_displayed_line = first_displayed_line;
1743 245d91c1 2018-07-12 stsp blame->cb_args.selected_line = selected_line;
1744 245d91c1 2018-07-12 stsp blame->cb_args.last_displayed_line = last_displayed_line;
1745 245d91c1 2018-07-12 stsp blame->cb_args.quit = done;
1747 245d91c1 2018-07-12 stsp blame->thread_args.path = path;
1748 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
1749 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
1750 245d91c1 2018-07-12 stsp blame->thread_args.complete = blame_complete;
1751 245d91c1 2018-07-12 stsp *blame_complete = 0;
1753 245d91c1 2018-07-12 stsp if (pthread_create(&blame->thread, NULL, blame_thread,
1754 245d91c1 2018-07-12 stsp &blame->thread_args) != 0) {
1755 245d91c1 2018-07-12 stsp err = got_error_from_errno();
1756 245d91c1 2018-07-12 stsp goto done;
1761 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
1763 245d91c1 2018-07-12 stsp got_object_close(obj);
1765 245d91c1 2018-07-12 stsp stop_blame(blame);
1766 245d91c1 2018-07-12 stsp return err;
1769 245d91c1 2018-07-12 stsp static const struct got_error *
1770 e1cd8fed 2018-08-01 stsp show_blame_view(struct tog_view *view, const char *path,
1771 e1cd8fed 2018-08-01 stsp struct got_object_id *commit_id, struct got_repository *repo)
1773 245d91c1 2018-07-12 stsp const struct got_error *err = NULL, *thread_err = NULL;
1774 f7d12f7e 2018-08-01 stsp int ch, done = 0, first_displayed_line = 1, last_displayed_line;
1775 245d91c1 2018-07-12 stsp int selected_line = first_displayed_line;
1776 245d91c1 2018-07-12 stsp int eof, blame_complete = 0;
1777 245d91c1 2018-07-12 stsp struct got_object *obj = NULL, *pobj = NULL;
1778 245d91c1 2018-07-12 stsp pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
1779 245d91c1 2018-07-12 stsp struct tog_blame blame;
1780 dbc6a6b6 2018-07-12 stsp struct got_object_id_queue blamed_commits;
1781 dbc6a6b6 2018-07-12 stsp struct got_object_qid *blamed_commit = NULL;
1782 e1cd8fed 2018-08-01 stsp struct tog_view *diff_view;
1784 dbc6a6b6 2018-07-12 stsp SIMPLEQ_INIT(&blamed_commits);
1786 245d91c1 2018-07-12 stsp if (pthread_mutex_init(&mutex, NULL) != 0) {
1787 245d91c1 2018-07-12 stsp err = got_error_from_errno();
1788 245d91c1 2018-07-12 stsp goto done;
1791 dbc6a6b6 2018-07-12 stsp err = got_object_qid_alloc(&blamed_commit, commit_id);
1793 245d91c1 2018-07-12 stsp goto done;
1794 dbc6a6b6 2018-07-12 stsp SIMPLEQ_INSERT_HEAD(&blamed_commits, blamed_commit, entry);
1796 6acd1bd1 2018-08-04 stsp view_show(view);
1797 f7d12f7e 2018-08-01 stsp last_displayed_line = view->nlines;
1799 245d91c1 2018-07-12 stsp memset(&blame, 0, sizeof(blame));
1800 7cc84d77 2018-08-01 stsp err = run_blame(&blame, &mutex, view, &blame_complete,
1801 245d91c1 2018-07-12 stsp &first_displayed_line, &last_displayed_line,
1802 dbc6a6b6 2018-07-12 stsp &selected_line, &done, path, blamed_commit->id, repo);
1804 245d91c1 2018-07-12 stsp return err;
1806 a70480e0 2018-06-23 stsp while (!done) {
1807 84451b3e 2018-07-10 stsp if (pthread_mutex_lock(&mutex) != 0) {
1808 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1809 84451b3e 2018-07-10 stsp goto done;
1811 f7d12f7e 2018-08-01 stsp err = draw_blame(view, blamed_commit->id, blame.f, path,
1812 f7d12f7e 2018-08-01 stsp blame.lines, blame.nlines, blame_complete, selected_line,
1813 f7d12f7e 2018-08-01 stsp &first_displayed_line, &last_displayed_line, &eof,
1814 f7d12f7e 2018-08-01 stsp view->nlines);
1815 84451b3e 2018-07-10 stsp if (pthread_mutex_unlock(&mutex) != 0) {
1816 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1817 84451b3e 2018-07-10 stsp goto done;
1821 a70480e0 2018-06-23 stsp nodelay(stdscr, FALSE);
1822 7cc84d77 2018-08-01 stsp ch = wgetch(view->window);
1823 a70480e0 2018-06-23 stsp nodelay(stdscr, TRUE);
1824 84451b3e 2018-07-10 stsp if (pthread_mutex_lock(&mutex) != 0) {
1825 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1826 84451b3e 2018-07-10 stsp goto done;
1828 a70480e0 2018-06-23 stsp switch (ch) {
1833 a70480e0 2018-06-23 stsp case KEY_UP:
1834 b700b5d6 2018-07-10 stsp if (selected_line > 1)
1835 b700b5d6 2018-07-10 stsp selected_line--;
1836 b700b5d6 2018-07-10 stsp else if (selected_line == 1 &&
1837 b700b5d6 2018-07-10 stsp first_displayed_line > 1)
1838 a70480e0 2018-06-23 stsp first_displayed_line--;
1840 a70480e0 2018-06-23 stsp case KEY_PPAGE:
1841 38f94530 2018-07-12 stsp case KEY_BACKSPACE:
1842 b700b5d6 2018-07-10 stsp if (first_displayed_line == 1) {
1843 b700b5d6 2018-07-10 stsp selected_line = 1;
1846 f7d12f7e 2018-08-01 stsp if (first_displayed_line > view->nlines - 2)
1847 f7d12f7e 2018-08-01 stsp first_displayed_line -=
1848 f7d12f7e 2018-08-01 stsp (view->nlines - 2);
1850 b700b5d6 2018-07-10 stsp first_displayed_line = 1;
1853 a70480e0 2018-06-23 stsp case KEY_DOWN:
1854 f7d12f7e 2018-08-01 stsp if (selected_line < view->nlines - 2 &&
1855 a026b947 2018-07-12 stsp first_displayed_line + selected_line <=
1856 a026b947 2018-07-12 stsp blame.nlines)
1857 b700b5d6 2018-07-10 stsp selected_line++;
1858 245d91c1 2018-07-12 stsp else if (last_displayed_line < blame.nlines)
1859 b700b5d6 2018-07-10 stsp first_displayed_line++;
1862 7a2921f9 2018-07-12 stsp case 'p': {
1863 245d91c1 2018-07-12 stsp struct got_object_id *id;
1864 245d91c1 2018-07-12 stsp id = get_selected_commit_id(blame.lines,
1865 245d91c1 2018-07-12 stsp first_displayed_line, selected_line);
1866 245d91c1 2018-07-12 stsp if (id == NULL || got_object_id_cmp(id,
1867 dbc6a6b6 2018-07-12 stsp blamed_commit->id) == 0)
1869 245d91c1 2018-07-12 stsp err = open_selected_commit(&pobj, &obj,
1870 245d91c1 2018-07-12 stsp blame.lines, first_displayed_line,
1871 245d91c1 2018-07-12 stsp selected_line, repo);
1874 245d91c1 2018-07-12 stsp if (pobj == NULL && obj == NULL)
1876 7a2921f9 2018-07-12 stsp if (ch == 'p' && pobj == NULL)
1879 245d91c1 2018-07-12 stsp if (pthread_mutex_unlock(&mutex) != 0) {
1880 245d91c1 2018-07-12 stsp err = got_error_from_errno();
1881 ad6a6c43 2018-07-10 stsp goto done;
1883 245d91c1 2018-07-12 stsp thread_err = stop_blame(&blame);
1885 245d91c1 2018-07-12 stsp if (pthread_mutex_lock(&mutex) != 0) {
1886 245d91c1 2018-07-12 stsp err = got_error_from_errno();
1887 245d91c1 2018-07-12 stsp goto done;
1889 245d91c1 2018-07-12 stsp if (thread_err)
1891 7a2921f9 2018-07-12 stsp id = got_object_get_id(ch == 'b' ? obj : pobj);
1892 1960a6f9 2018-07-12 stsp got_object_close(obj);
1893 1960a6f9 2018-07-12 stsp obj = NULL;
1894 1960a6f9 2018-07-12 stsp if (pobj) {
1895 1960a6f9 2018-07-12 stsp got_object_close(pobj);
1896 14437fb1 2018-07-13 stsp pobj = NULL;
1898 dbc6a6b6 2018-07-12 stsp if (id == NULL) {
1899 245d91c1 2018-07-12 stsp err = got_error_from_errno();
1902 dbc6a6b6 2018-07-12 stsp err = got_object_qid_alloc(&blamed_commit, id);
1905 dbc6a6b6 2018-07-12 stsp goto done;
1906 dbc6a6b6 2018-07-12 stsp SIMPLEQ_INSERT_HEAD(&blamed_commits,
1907 dbc6a6b6 2018-07-12 stsp blamed_commit, entry);
1908 7cc84d77 2018-08-01 stsp err = run_blame(&blame, &mutex, view,
1909 245d91c1 2018-07-12 stsp &blame_complete, &first_displayed_line,
1910 245d91c1 2018-07-12 stsp &last_displayed_line, &selected_line,
1911 dbc6a6b6 2018-07-12 stsp &done, path, blamed_commit->id, repo);
1916 dbc6a6b6 2018-07-12 stsp case 'B': {
1917 dbc6a6b6 2018-07-12 stsp struct got_object_qid *first;
1918 dbc6a6b6 2018-07-12 stsp first = SIMPLEQ_FIRST(&blamed_commits);
1919 dbc6a6b6 2018-07-12 stsp if (!got_object_id_cmp(first->id, commit_id))
1922 dbc6a6b6 2018-07-12 stsp if (pthread_mutex_unlock(&mutex) != 0) {
1923 dbc6a6b6 2018-07-12 stsp err = got_error_from_errno();
1924 dbc6a6b6 2018-07-12 stsp goto done;
1926 dbc6a6b6 2018-07-12 stsp thread_err = stop_blame(&blame);
1928 dbc6a6b6 2018-07-12 stsp if (pthread_mutex_lock(&mutex) != 0) {
1929 dbc6a6b6 2018-07-12 stsp err = got_error_from_errno();
1930 dbc6a6b6 2018-07-12 stsp goto done;
1932 dbc6a6b6 2018-07-12 stsp if (thread_err)
1934 dbc6a6b6 2018-07-12 stsp SIMPLEQ_REMOVE_HEAD(&blamed_commits, entry);
1935 dbc6a6b6 2018-07-12 stsp got_object_qid_free(blamed_commit);
1936 dbc6a6b6 2018-07-12 stsp blamed_commit = SIMPLEQ_FIRST(&blamed_commits);
1937 7cc84d77 2018-08-01 stsp err = run_blame(&blame, &mutex, view,
1938 dbc6a6b6 2018-07-12 stsp &blame_complete, &first_displayed_line,
1939 dbc6a6b6 2018-07-12 stsp &last_displayed_line, &selected_line,
1940 dbc6a6b6 2018-07-12 stsp &done, path, blamed_commit->id, repo);
1945 245d91c1 2018-07-12 stsp case KEY_ENTER:
1946 245d91c1 2018-07-12 stsp case '\r':
1947 245d91c1 2018-07-12 stsp err = open_selected_commit(&pobj, &obj,
1948 245d91c1 2018-07-12 stsp blame.lines, first_displayed_line,
1949 245d91c1 2018-07-12 stsp selected_line, repo);
1952 b880a918 2018-07-10 stsp if (pobj == NULL && obj == NULL)
1954 b3665f43 2018-08-04 stsp diff_view = view_open(0, 0, 0, 0, view,
1955 d6b05b5b 2018-08-04 stsp TOG_VIEW_DIFF);
1956 ea5e7bb5 2018-08-01 stsp if (diff_view == NULL) {
1957 ea5e7bb5 2018-08-01 stsp err = got_error_from_errno();
1960 5dc9f4bc 2018-08-04 stsp err = open_diff_view(diff_view, pobj, obj, repo);
1963 5dc9f4bc 2018-08-04 stsp err = show_diff_view(diff_view);
1964 5dc9f4bc 2018-08-04 stsp close_diff_view(diff_view);
1965 96a765a8 2018-08-04 stsp view_close(diff_view);
1968 6acd1bd1 2018-08-04 stsp view_show(view);
1969 b880a918 2018-07-10 stsp if (pobj) {
1970 b880a918 2018-07-10 stsp got_object_close(pobj);
1971 b880a918 2018-07-10 stsp pobj = NULL;
1973 b880a918 2018-07-10 stsp got_object_close(obj);
1974 b880a918 2018-07-10 stsp obj = NULL;
1978 a70480e0 2018-06-23 stsp case KEY_NPAGE:
1980 245d91c1 2018-07-12 stsp if (last_displayed_line >= blame.nlines &&
1981 f7d12f7e 2018-08-01 stsp selected_line < view->nlines - 2) {
1982 d2dfcfbf 2018-07-12 stsp selected_line = MIN(blame.nlines,
1983 f7d12f7e 2018-08-01 stsp view->nlines - 2);
1986 f7d12f7e 2018-08-01 stsp if (last_displayed_line + view->nlines - 2 <=
1987 245d91c1 2018-07-12 stsp blame.nlines)
1988 f7d12f7e 2018-08-01 stsp first_displayed_line +=
1989 f7d12f7e 2018-08-01 stsp view->nlines - 2;
1991 b700b5d6 2018-07-10 stsp first_displayed_line =
1992 f7d12f7e 2018-08-01 stsp blame.nlines - (view->nlines - 3);
1994 7e5c1fe1 2018-08-01 stsp case KEY_RESIZE:
1995 a41d2007 2018-08-01 stsp err = view_resize(view);
1998 7e5c1fe1 2018-08-01 stsp if (selected_line > view->nlines - 2) {
1999 7e5c1fe1 2018-08-01 stsp selected_line = MIN(blame.nlines,
2000 7e5c1fe1 2018-08-01 stsp view->nlines - 2);
2006 245d91c1 2018-07-12 stsp if (pthread_mutex_unlock(&mutex) != 0)
2007 84451b3e 2018-07-10 stsp err = got_error_from_errno();
2008 245d91c1 2018-07-12 stsp if (err || thread_err)
2013 b880a918 2018-07-10 stsp got_object_close(pobj);
2014 c9beca56 2018-07-22 stsp if (blame.thread)
2015 245d91c1 2018-07-12 stsp thread_err = stop_blame(&blame);
2016 dbc6a6b6 2018-07-12 stsp while (!SIMPLEQ_EMPTY(&blamed_commits)) {
2017 dbc6a6b6 2018-07-12 stsp blamed_commit = SIMPLEQ_FIRST(&blamed_commits);
2018 dbc6a6b6 2018-07-12 stsp SIMPLEQ_REMOVE_HEAD(&blamed_commits, entry);
2019 dbc6a6b6 2018-07-12 stsp got_object_qid_free(blamed_commit);
2021 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
2024 a70480e0 2018-06-23 stsp static const struct got_error *
2025 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
2027 a70480e0 2018-06-23 stsp const struct got_error *error;
2028 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
2029 69069811 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2030 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
2031 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
2033 e1cd8fed 2018-08-01 stsp struct tog_view *view;
2035 a70480e0 2018-06-23 stsp #ifndef PROFILE
2036 a70480e0 2018-06-23 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
2037 a70480e0 2018-06-23 stsp err(1, "pledge");
2040 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2041 a70480e0 2018-06-23 stsp switch (ch) {
2043 a70480e0 2018-06-23 stsp commit_id_str = optarg;
2046 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
2047 69069811 2018-08-02 stsp if (repo_path == NULL)
2048 69069811 2018-08-02 stsp err(1, "-r option");
2052 a70480e0 2018-06-23 stsp /* NOTREACHED */
2056 a70480e0 2018-06-23 stsp argc -= optind;
2057 a70480e0 2018-06-23 stsp argv += optind;
2059 69069811 2018-08-02 stsp if (argc == 1)
2060 69069811 2018-08-02 stsp path = argv[0];
2062 a70480e0 2018-06-23 stsp usage_blame();
2064 69069811 2018-08-02 stsp cwd = getcwd(NULL, 0);
2065 69069811 2018-08-02 stsp if (cwd == NULL) {
2066 69069811 2018-08-02 stsp error = got_error_from_errno();
2067 69069811 2018-08-02 stsp goto done;
2069 69069811 2018-08-02 stsp if (repo_path == NULL) {
2070 69069811 2018-08-02 stsp repo_path = strdup(cwd);
2071 69069811 2018-08-02 stsp if (repo_path == NULL) {
2072 69069811 2018-08-02 stsp error = got_error_from_errno();
2073 69069811 2018-08-02 stsp goto done;
2078 69069811 2018-08-02 stsp error = got_repo_open(&repo, repo_path);
2079 a70480e0 2018-06-23 stsp if (error != NULL)
2080 66b4983c 2018-06-23 stsp return error;
2082 69069811 2018-08-02 stsp error = got_repo_map_path(&in_repo_path, repo, path);
2083 69069811 2018-08-02 stsp if (error != NULL)
2084 69069811 2018-08-02 stsp goto done;
2086 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
2087 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
2088 a70480e0 2018-06-23 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
2089 a70480e0 2018-06-23 stsp if (error != NULL)
2090 66b4983c 2018-06-23 stsp goto done;
2091 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2092 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
2094 a70480e0 2018-06-23 stsp struct got_object *obj;
2095 a70480e0 2018-06-23 stsp error = got_object_open_by_id_str(&obj, repo, commit_id_str);
2096 a70480e0 2018-06-23 stsp if (error != NULL)
2097 66b4983c 2018-06-23 stsp goto done;
2098 a70480e0 2018-06-23 stsp commit_id = got_object_get_id(obj);
2099 a19e88aa 2018-06-23 stsp if (commit_id == NULL)
2100 66b4983c 2018-06-23 stsp error = got_error_from_errno();
2101 a19e88aa 2018-06-23 stsp got_object_close(obj);
2103 a19e88aa 2018-06-23 stsp if (error != NULL)
2104 a19e88aa 2018-06-23 stsp goto done;
2106 b3665f43 2018-08-04 stsp view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_BLAME);
2107 e1cd8fed 2018-08-01 stsp if (view == NULL) {
2108 e1cd8fed 2018-08-01 stsp error = got_error_from_errno();
2109 e1cd8fed 2018-08-01 stsp goto done;
2111 69069811 2018-08-02 stsp error = show_blame_view(view, in_repo_path, commit_id, repo);
2112 96a765a8 2018-08-04 stsp view_close(view);
2114 69069811 2018-08-02 stsp free(in_repo_path);
2115 69069811 2018-08-02 stsp free(repo_path);
2116 69069811 2018-08-02 stsp free(cwd);
2117 a70480e0 2018-06-23 stsp free(commit_id);
2119 a70480e0 2018-06-23 stsp got_repo_close(repo);
2120 a70480e0 2018-06-23 stsp return error;
2123 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2124 f7d12f7e 2018-08-01 stsp draw_tree_entries(struct tog_view *view,
2125 f7d12f7e 2018-08-01 stsp struct got_tree_entry **first_displayed_entry,
2126 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **last_displayed_entry,
2127 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **selected_entry, int *ndisplayed,
2128 f7d12f7e 2018-08-01 stsp const char *label, int show_ids, const char *parent_path,
2129 ce52c690 2018-06-23 stsp const struct got_tree_entries *entries, int selected, int limit, int isroot)
2131 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
2132 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
2133 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
2134 ffd1d5e5 2018-06-23 stsp int width, n;
2136 ffd1d5e5 2018-06-23 stsp *ndisplayed = 0;
2138 f7d12f7e 2018-08-01 stsp werase(view->window);
2140 ffd1d5e5 2018-06-23 stsp if (limit == 0)
2141 ffd1d5e5 2018-06-23 stsp return NULL;
2143 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, label, view->ncols);
2145 ffd1d5e5 2018-06-23 stsp return err;
2146 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2147 2550e4c3 2018-07-13 stsp free(wline);
2148 2550e4c3 2018-07-13 stsp wline = NULL;
2149 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2150 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2151 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2152 ffd1d5e5 2018-06-23 stsp return NULL;
2153 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, parent_path, view->ncols);
2155 ce52c690 2018-06-23 stsp return err;
2156 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2157 2550e4c3 2018-07-13 stsp free(wline);
2158 2550e4c3 2018-07-13 stsp wline = NULL;
2159 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2160 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2161 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2162 ffd1d5e5 2018-06-23 stsp return NULL;
2163 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2164 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
2165 a1eca9bb 2018-06-23 stsp return NULL;
2167 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
2168 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL) {
2169 ffd1d5e5 2018-06-23 stsp if (selected == 0) {
2170 f7d12f7e 2018-08-01 stsp wstandout(view->window);
2171 ffd1d5e5 2018-06-23 stsp *selected_entry = NULL;
2173 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
2174 ffd1d5e5 2018-06-23 stsp if (selected == 0)
2175 f7d12f7e 2018-08-01 stsp wstandend(view->window);
2176 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
2177 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2178 ffd1d5e5 2018-06-23 stsp return NULL;
2182 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry)
2183 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
2186 ffd1d5e5 2018-06-23 stsp while (te) {
2187 1d13200f 2018-07-12 stsp char *line = NULL, *id_str = NULL;
2189 1d13200f 2018-07-12 stsp if (show_ids) {
2190 1d13200f 2018-07-12 stsp err = got_object_id_str(&id_str, te->id);
2192 1d13200f 2018-07-12 stsp return got_error_from_errno();
2194 1d13200f 2018-07-12 stsp if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
2195 1d13200f 2018-07-12 stsp te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
2196 1d13200f 2018-07-12 stsp free(id_str);
2197 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
2199 1d13200f 2018-07-12 stsp free(id_str);
2200 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
2201 ffd1d5e5 2018-06-23 stsp if (err) {
2202 ffd1d5e5 2018-06-23 stsp free(line);
2205 ffd1d5e5 2018-06-23 stsp if (n == selected) {
2206 f7d12f7e 2018-08-01 stsp wstandout(view->window);
2207 ffd1d5e5 2018-06-23 stsp *selected_entry = te;
2209 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2210 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2211 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2212 ffd1d5e5 2018-06-23 stsp if (n == selected)
2213 f7d12f7e 2018-08-01 stsp wstandend(view->window);
2214 ffd1d5e5 2018-06-23 stsp free(line);
2215 2550e4c3 2018-07-13 stsp free(wline);
2216 2550e4c3 2018-07-13 stsp wline = NULL;
2218 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
2219 ffd1d5e5 2018-06-23 stsp *last_displayed_entry = te;
2220 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2222 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
2225 ffd1d5e5 2018-06-23 stsp return err;
2228 ffd1d5e5 2018-06-23 stsp static void
2229 ffd1d5e5 2018-06-23 stsp tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
2230 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries, int isroot)
2232 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te, *prev;
2235 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL)
2238 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
2239 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == te) {
2240 ffd1d5e5 2018-06-23 stsp if (!isroot)
2241 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
2245 ffd1d5e5 2018-06-23 stsp /* XXX this is stupid... switch to TAILQ? */
2246 ffd1d5e5 2018-06-23 stsp for (i = 0; i < maxscroll; i++) {
2247 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry) {
2248 ffd1d5e5 2018-06-23 stsp prev = te;
2249 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
2251 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = prev;
2252 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
2254 ffd1d5e5 2018-06-23 stsp if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
2255 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
2258 ffd1d5e5 2018-06-23 stsp static void
2259 ffd1d5e5 2018-06-23 stsp tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
2260 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *last_displayed_entry,
2261 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries)
2263 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *next;
2264 ffd1d5e5 2018-06-23 stsp int n = 0;
2266 ffd1d5e5 2018-06-23 stsp if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
2269 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry)
2270 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
2272 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_FIRST(&entries->head);
2273 ffd1d5e5 2018-06-23 stsp while (next) {
2274 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = next;
2275 ffd1d5e5 2018-06-23 stsp if (++n >= maxscroll)
2277 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(next, entry);
2281 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree {
2282 d9765a41 2018-06-23 stsp TAILQ_ENTRY(tog_parent_tree) entry;
2283 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree;
2284 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *first_displayed_entry;
2285 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *selected_entry;
2286 ffd1d5e5 2018-06-23 stsp int selected;
2289 d9765a41 2018-06-23 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
2291 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2292 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
2293 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
2295 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
2296 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
2297 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
2299 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
2300 ffd1d5e5 2018-06-23 stsp len += strlen(pt->selected_entry->name) + 1 /* slash */;
2302 ce52c690 2018-06-23 stsp len += strlen(te->name);
2304 ce52c690 2018-06-23 stsp *path = calloc(1, len);
2305 ffd1d5e5 2018-06-23 stsp if (path == NULL)
2306 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
2308 ce52c690 2018-06-23 stsp (*path)[0] = '/';
2309 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
2310 d9765a41 2018-06-23 stsp while (pt) {
2311 ce52c690 2018-06-23 stsp if (strlcat(*path, pt->selected_entry->name, len) >= len) {
2312 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
2313 cb2ebc8a 2018-06-23 stsp goto done;
2315 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
2316 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
2317 cb2ebc8a 2018-06-23 stsp goto done;
2319 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
2322 ce52c690 2018-06-23 stsp if (strlcat(*path, te->name, len) >= len) {
2323 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
2324 ce52c690 2018-06-23 stsp goto done;
2328 ce52c690 2018-06-23 stsp if (err) {
2329 ce52c690 2018-06-23 stsp free(*path);
2330 ce52c690 2018-06-23 stsp *path = NULL;
2332 ce52c690 2018-06-23 stsp return err;
2335 ce52c690 2018-06-23 stsp static const struct got_error *
2336 cdf1ee82 2018-08-01 stsp blame_tree_entry(struct tog_view *parent_view, struct got_tree_entry *te,
2337 e1cd8fed 2018-08-01 stsp struct tog_parent_trees *parents, struct got_object_id *commit_id,
2338 e1cd8fed 2018-08-01 stsp struct got_repository *repo)
2340 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
2341 ce52c690 2018-06-23 stsp char *path;
2342 cdf1ee82 2018-08-01 stsp struct tog_view *view;
2344 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
2346 ce52c690 2018-06-23 stsp return err;
2348 b3665f43 2018-08-04 stsp view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_BLAME);
2349 cdf1ee82 2018-08-01 stsp if (view) {
2350 cdf1ee82 2018-08-01 stsp err = show_blame_view(view, path, commit_id, repo);
2351 96a765a8 2018-08-04 stsp view_close(view);
2353 cdf1ee82 2018-08-01 stsp err = got_error_from_errno();
2355 6acd1bd1 2018-08-04 stsp view_show(parent_view);
2356 69efd4c4 2018-07-18 stsp free(path);
2357 69efd4c4 2018-07-18 stsp return err;
2360 69efd4c4 2018-07-18 stsp static const struct got_error *
2361 04cc582a 2018-08-01 stsp log_tree_entry(struct tog_view *view, struct got_tree_entry *te,
2362 04cc582a 2018-08-01 stsp struct tog_parent_trees *parents, struct got_object_id *commit_id,
2363 04cc582a 2018-08-01 stsp struct got_repository *repo)
2365 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
2366 69efd4c4 2018-07-18 stsp char *path;
2368 69efd4c4 2018-07-18 stsp err = tree_entry_path(&path, parents, te);
2370 69efd4c4 2018-07-18 stsp return err;
2372 ba4f502b 2018-08-04 stsp err = open_log_view(view, commit_id, repo, path);
2374 ba4f502b 2018-08-04 stsp goto done;
2375 ba4f502b 2018-08-04 stsp err = show_log_view(view);
2376 ba4f502b 2018-08-04 stsp close_log_view(view);
2378 cb2ebc8a 2018-06-23 stsp free(path);
2379 cb2ebc8a 2018-06-23 stsp return err;
2382 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2383 5221c383 2018-08-01 stsp show_tree_view(struct tog_view *view, struct got_tree_object *root,
2384 5221c383 2018-08-01 stsp struct got_object_id *commit_id, struct got_repository *repo)
2386 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
2387 1d13200f 2018-07-12 stsp int ch, done = 0, selected = 0, show_ids = 0;
2388 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree = root;
2389 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries;
2390 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *first_displayed_entry = NULL;
2391 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *last_displayed_entry = NULL;
2392 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *selected_entry = NULL;
2393 ffd1d5e5 2018-06-23 stsp char *commit_id_str = NULL, *tree_label = NULL;
2394 ffd1d5e5 2018-06-23 stsp int nentries, ndisplayed;
2395 ffd1d5e5 2018-06-23 stsp struct tog_parent_trees parents;
2397 d9765a41 2018-06-23 stsp TAILQ_INIT(&parents);
2399 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
2400 ffd1d5e5 2018-06-23 stsp if (err != NULL)
2401 ffd1d5e5 2018-06-23 stsp goto done;
2403 decd3bd1 2018-07-12 stsp if (asprintf(&tree_label, "commit: %s", commit_id_str) == -1) {
2404 ffd1d5e5 2018-06-23 stsp err = got_error_from_errno();
2405 ffd1d5e5 2018-06-23 stsp goto done;
2408 6acd1bd1 2018-08-04 stsp view_show(view);
2410 ffd1d5e5 2018-06-23 stsp entries = got_object_tree_get_entries(root);
2411 ffd1d5e5 2018-06-23 stsp first_displayed_entry = SIMPLEQ_FIRST(&entries->head);
2412 ffd1d5e5 2018-06-23 stsp while (!done) {
2413 ce52c690 2018-06-23 stsp char *parent_path;
2414 ffd1d5e5 2018-06-23 stsp entries = got_object_tree_get_entries(tree);
2415 ffd1d5e5 2018-06-23 stsp nentries = entries->nentries;
2416 ffd1d5e5 2018-06-23 stsp if (tree != root)
2417 ffd1d5e5 2018-06-23 stsp nentries++; /* '..' directory */
2419 ce52c690 2018-06-23 stsp err = tree_entry_path(&parent_path, &parents, NULL);
2421 ce52c690 2018-06-23 stsp goto done;
2423 f7d12f7e 2018-08-01 stsp err = draw_tree_entries(view, &first_displayed_entry,
2424 ffd1d5e5 2018-06-23 stsp &last_displayed_entry, &selected_entry, &ndisplayed,
2425 f7d12f7e 2018-08-01 stsp tree_label, show_ids, parent_path, entries, selected,
2426 f7d12f7e 2018-08-01 stsp view->nlines, tree == root);
2427 ce52c690 2018-06-23 stsp free(parent_path);
2431 ffd1d5e5 2018-06-23 stsp nodelay(stdscr, FALSE);
2432 cc3c9aac 2018-08-01 stsp ch = wgetch(view->window);
2433 ffd1d5e5 2018-06-23 stsp nodelay(stdscr, TRUE);
2434 ffd1d5e5 2018-06-23 stsp switch (ch) {
2439 1d13200f 2018-07-12 stsp show_ids = !show_ids;
2442 69efd4c4 2018-07-18 stsp if (selected_entry) {
2443 04cc582a 2018-08-01 stsp struct tog_view *log_view;
2444 b3665f43 2018-08-04 stsp log_view = view_open(0, 0, 0, 0, view,
2445 d6b05b5b 2018-08-04 stsp TOG_VIEW_LOG);
2446 04cc582a 2018-08-01 stsp if (log_view == NULL) {
2447 04cc582a 2018-08-01 stsp err = got_error_from_errno();
2448 04cc582a 2018-08-01 stsp goto done;
2450 04cc582a 2018-08-01 stsp err = log_tree_entry(log_view,
2451 04cc582a 2018-08-01 stsp selected_entry, &parents,
2452 04cc582a 2018-08-01 stsp commit_id, repo);
2453 96a765a8 2018-08-04 stsp view_close(log_view);
2454 6acd1bd1 2018-08-04 stsp view_show(view);
2456 69efd4c4 2018-07-18 stsp goto done;
2460 ffd1d5e5 2018-06-23 stsp case KEY_UP:
2461 ffd1d5e5 2018-06-23 stsp if (selected > 0)
2462 ffd1d5e5 2018-06-23 stsp selected--;
2463 ffd1d5e5 2018-06-23 stsp if (selected > 0)
2465 ffd1d5e5 2018-06-23 stsp tree_scroll_up(&first_displayed_entry, 1,
2466 ffd1d5e5 2018-06-23 stsp entries, tree == root);
2468 ffd1d5e5 2018-06-23 stsp case KEY_PPAGE:
2469 ffd1d5e5 2018-06-23 stsp if (SIMPLEQ_FIRST(&entries->head) ==
2470 ffd1d5e5 2018-06-23 stsp first_displayed_entry) {
2471 cf8f1261 2018-06-23 stsp if (tree != root)
2472 cf8f1261 2018-06-23 stsp first_displayed_entry = NULL;
2473 ffd1d5e5 2018-06-23 stsp selected = 0;
2476 f7d12f7e 2018-08-01 stsp tree_scroll_up(&first_displayed_entry,
2477 f7d12f7e 2018-08-01 stsp view->nlines, entries, tree == root);
2480 ffd1d5e5 2018-06-23 stsp case KEY_DOWN:
2481 ffd1d5e5 2018-06-23 stsp if (selected < ndisplayed - 1) {
2482 ffd1d5e5 2018-06-23 stsp selected++;
2485 ffd1d5e5 2018-06-23 stsp tree_scroll_down(&first_displayed_entry, 1,
2486 ffd1d5e5 2018-06-23 stsp last_displayed_entry, entries);
2488 ffd1d5e5 2018-06-23 stsp case KEY_NPAGE:
2489 f7d12f7e 2018-08-01 stsp tree_scroll_down(&first_displayed_entry,
2490 f7d12f7e 2018-08-01 stsp view->nlines, last_displayed_entry,
2492 ffd1d5e5 2018-06-23 stsp if (SIMPLEQ_NEXT(last_displayed_entry, entry))
2494 ffd1d5e5 2018-06-23 stsp /* can't scroll any further; move cursor down */
2495 ffd1d5e5 2018-06-23 stsp if (selected < ndisplayed - 1)
2496 ffd1d5e5 2018-06-23 stsp selected = ndisplayed - 1;
2498 ffd1d5e5 2018-06-23 stsp case KEY_ENTER:
2499 ffd1d5e5 2018-06-23 stsp case '\r':
2500 ffd1d5e5 2018-06-23 stsp if (selected_entry == NULL) {
2501 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *parent;
2502 ffd1d5e5 2018-06-23 stsp case KEY_BACKSPACE:
2503 ffd1d5e5 2018-06-23 stsp /* user selected '..' */
2504 ffd1d5e5 2018-06-23 stsp if (tree == root)
2506 d9765a41 2018-06-23 stsp parent = TAILQ_FIRST(&parents);
2507 d9765a41 2018-06-23 stsp TAILQ_REMOVE(&parents, parent, entry);
2508 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
2509 ffd1d5e5 2018-06-23 stsp tree = parent->tree;
2510 ffd1d5e5 2018-06-23 stsp first_displayed_entry =
2511 ffd1d5e5 2018-06-23 stsp parent->first_displayed_entry;
2512 ffd1d5e5 2018-06-23 stsp selected_entry = parent->selected_entry;
2513 ffd1d5e5 2018-06-23 stsp selected = parent->selected;
2514 ffd1d5e5 2018-06-23 stsp free(parent);
2515 ffd1d5e5 2018-06-23 stsp } else if (S_ISDIR(selected_entry->mode)) {
2516 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *parent;
2517 ffd1d5e5 2018-06-23 stsp struct got_tree_object *child;
2518 ffd1d5e5 2018-06-23 stsp err = got_object_open_as_tree(
2519 ffd1d5e5 2018-06-23 stsp &child, repo, selected_entry->id);
2521 ffd1d5e5 2018-06-23 stsp goto done;
2522 ffd1d5e5 2018-06-23 stsp parent = calloc(1, sizeof(*parent));
2523 ffd1d5e5 2018-06-23 stsp if (parent == NULL) {
2524 ffd1d5e5 2018-06-23 stsp err = got_error_from_errno();
2525 ffd1d5e5 2018-06-23 stsp goto done;
2527 ffd1d5e5 2018-06-23 stsp parent->tree = tree;
2528 ffd1d5e5 2018-06-23 stsp parent->first_displayed_entry =
2529 ffd1d5e5 2018-06-23 stsp first_displayed_entry;
2530 ffd1d5e5 2018-06-23 stsp parent->selected_entry = selected_entry;
2531 ffd1d5e5 2018-06-23 stsp parent->selected = selected;
2532 d9765a41 2018-06-23 stsp TAILQ_INSERT_HEAD(&parents, parent,
2534 ffd1d5e5 2018-06-23 stsp tree = child;
2535 ffd1d5e5 2018-06-23 stsp selected = 0;
2536 ffd1d5e5 2018-06-23 stsp first_displayed_entry = NULL;
2537 ffd1d5e5 2018-06-23 stsp } else if (S_ISREG(selected_entry->mode)) {
2538 cdf1ee82 2018-08-01 stsp err = blame_tree_entry(view,
2539 e1cd8fed 2018-08-01 stsp selected_entry, &parents,
2540 e1cd8fed 2018-08-01 stsp commit_id, repo);
2542 ffd1d5e5 2018-06-23 stsp goto done;
2545 ffd1d5e5 2018-06-23 stsp case KEY_RESIZE:
2546 a41d2007 2018-08-01 stsp err = view_resize(view);
2548 a41d2007 2018-08-01 stsp goto done;
2549 f7d12f7e 2018-08-01 stsp if (selected > view->nlines)
2550 ffd1d5e5 2018-06-23 stsp selected = ndisplayed - 1;
2557 ffd1d5e5 2018-06-23 stsp free(tree_label);
2558 ffd1d5e5 2018-06-23 stsp free(commit_id_str);
2559 d9765a41 2018-06-23 stsp while (!TAILQ_EMPTY(&parents)) {
2560 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *parent;
2561 d9765a41 2018-06-23 stsp parent = TAILQ_FIRST(&parents);
2562 d9765a41 2018-06-23 stsp TAILQ_REMOVE(&parents, parent, entry);
2563 ffd1d5e5 2018-06-23 stsp free(parent);
2566 ffd1d5e5 2018-06-23 stsp if (tree != root)
2567 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
2568 ffd1d5e5 2018-06-23 stsp return err;
2571 ffd1d5e5 2018-06-23 stsp __dead static void
2572 ffd1d5e5 2018-06-23 stsp usage_tree(void)
2575 ffd1d5e5 2018-06-23 stsp fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
2576 ffd1d5e5 2018-06-23 stsp getprogname());
2580 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2581 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
2583 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
2584 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
2585 ffd1d5e5 2018-06-23 stsp char *repo_path = NULL;
2586 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
2587 ffd1d5e5 2018-06-23 stsp char *commit_id_arg = NULL;
2588 ffd1d5e5 2018-06-23 stsp struct got_commit_object *commit = NULL;
2589 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree = NULL;
2591 5221c383 2018-08-01 stsp struct tog_view *view;
2593 ffd1d5e5 2018-06-23 stsp #ifndef PROFILE
2594 ffd1d5e5 2018-06-23 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
2595 ffd1d5e5 2018-06-23 stsp err(1, "pledge");
2598 ffd1d5e5 2018-06-23 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
2599 ffd1d5e5 2018-06-23 stsp switch (ch) {
2601 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
2605 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
2609 ffd1d5e5 2018-06-23 stsp argc -= optind;
2610 ffd1d5e5 2018-06-23 stsp argv += optind;
2612 ffd1d5e5 2018-06-23 stsp if (argc == 0) {
2613 ffd1d5e5 2018-06-23 stsp repo_path = getcwd(NULL, 0);
2614 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
2615 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
2616 ffd1d5e5 2018-06-23 stsp } else if (argc == 1) {
2617 ffd1d5e5 2018-06-23 stsp repo_path = realpath(argv[0], NULL);
2618 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
2619 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
2621 ffd1d5e5 2018-06-23 stsp usage_log();
2623 ffd1d5e5 2018-06-23 stsp error = got_repo_open(&repo, repo_path);
2624 ffd1d5e5 2018-06-23 stsp free(repo_path);
2625 ffd1d5e5 2018-06-23 stsp if (error != NULL)
2626 ffd1d5e5 2018-06-23 stsp return error;
2628 ffd1d5e5 2018-06-23 stsp if (commit_id_arg == NULL) {
2629 ffd1d5e5 2018-06-23 stsp error = get_head_commit_id(&commit_id, repo);
2630 ffd1d5e5 2018-06-23 stsp if (error != NULL)
2631 ffd1d5e5 2018-06-23 stsp goto done;
2633 ffd1d5e5 2018-06-23 stsp struct got_object *obj;
2634 ffd1d5e5 2018-06-23 stsp error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
2635 ffd1d5e5 2018-06-23 stsp if (error == NULL) {
2636 ffd1d5e5 2018-06-23 stsp commit_id = got_object_get_id(obj);
2637 ffd1d5e5 2018-06-23 stsp if (commit_id == NULL)
2638 ffd1d5e5 2018-06-23 stsp error = got_error_from_errno();
2641 ffd1d5e5 2018-06-23 stsp if (error != NULL)
2642 ffd1d5e5 2018-06-23 stsp goto done;
2644 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
2645 ffd1d5e5 2018-06-23 stsp if (error != NULL)
2646 ffd1d5e5 2018-06-23 stsp goto done;
2648 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_tree(&tree, repo, commit->tree_id);
2649 ffd1d5e5 2018-06-23 stsp if (error != NULL)
2650 ffd1d5e5 2018-06-23 stsp goto done;
2652 b3665f43 2018-08-04 stsp view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_TREE);
2653 5221c383 2018-08-01 stsp if (view == NULL) {
2654 5221c383 2018-08-01 stsp error = got_error_from_errno();
2655 5221c383 2018-08-01 stsp goto done;
2657 5221c383 2018-08-01 stsp error = show_tree_view(view, tree, commit_id, repo);
2658 96a765a8 2018-08-04 stsp view_close(view);
2660 ffd1d5e5 2018-06-23 stsp free(commit_id);
2661 ffd1d5e5 2018-06-23 stsp if (commit)
2662 ffd1d5e5 2018-06-23 stsp got_object_commit_close(commit);
2664 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
2666 ffd1d5e5 2018-06-23 stsp got_repo_close(repo);
2667 ffd1d5e5 2018-06-23 stsp return error;
2669 5c5136c5 2018-05-20 stsp static void
2670 9f7d7167 2018-04-29 stsp init_curses(void)
2672 9f7d7167 2018-04-29 stsp initscr();
2676 9f7d7167 2018-04-29 stsp intrflush(stdscr, FALSE);
2677 9f7d7167 2018-04-29 stsp keypad(stdscr, TRUE);
2678 1f475ad8 2018-05-10 stsp curs_set(0);
2681 4ed7e80c 2018-05-20 stsp __dead static void
2682 9f7d7167 2018-04-29 stsp usage(void)
2686 c2301be8 2018-04-30 stsp fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
2687 9f7d7167 2018-04-29 stsp "Available commands:\n", getprogname());
2688 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
2689 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = &tog_commands[i];
2690 c2301be8 2018-04-30 stsp fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
2695 c2301be8 2018-04-30 stsp static char **
2696 c2301be8 2018-04-30 stsp make_argv(const char *arg0, const char *arg1)
2698 c2301be8 2018-04-30 stsp char **argv;
2699 c2301be8 2018-04-30 stsp int argc = (arg1 == NULL ? 1 : 2);
2701 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
2702 c2301be8 2018-04-30 stsp if (argv == NULL)
2703 c2301be8 2018-04-30 stsp err(1, "calloc");
2704 c2301be8 2018-04-30 stsp argv[0] = strdup(arg0);
2705 c2301be8 2018-04-30 stsp if (argv[0] == NULL)
2706 c2301be8 2018-04-30 stsp err(1, "calloc");
2707 c2301be8 2018-04-30 stsp if (arg1) {
2708 c2301be8 2018-04-30 stsp argv[1] = strdup(arg1);
2709 c2301be8 2018-04-30 stsp if (argv[1] == NULL)
2710 c2301be8 2018-04-30 stsp err(1, "calloc");
2713 c2301be8 2018-04-30 stsp return argv;
2717 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
2719 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
2720 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
2721 9f7d7167 2018-04-29 stsp int ch, hflag = 0;
2722 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
2724 9f7d7167 2018-04-29 stsp setlocale(LC_ALL, "");
2726 9f7d7167 2018-04-29 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
2727 9f7d7167 2018-04-29 stsp switch (ch) {
2729 9f7d7167 2018-04-29 stsp hflag = 1;
2733 9f7d7167 2018-04-29 stsp /* NOTREACHED */
2737 9f7d7167 2018-04-29 stsp argc -= optind;
2738 9f7d7167 2018-04-29 stsp argv += optind;
2739 9f7d7167 2018-04-29 stsp optind = 0;
2740 c2301be8 2018-04-30 stsp optreset = 1;
2742 c2301be8 2018-04-30 stsp if (argc == 0) {
2743 f29d3e89 2018-06-23 stsp if (hflag)
2745 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
2746 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
2747 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, NULL);
2752 c2301be8 2018-04-30 stsp /* Did the user specific a command? */
2753 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
2754 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
2755 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
2756 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
2757 9f7d7167 2018-04-29 stsp if (hflag)
2758 9f7d7167 2018-04-29 stsp tog_commands[i].cmd_usage();
2762 9f7d7167 2018-04-29 stsp if (cmd == NULL) {
2763 c2301be8 2018-04-30 stsp /* Did the user specify a repository? */
2764 c2301be8 2018-04-30 stsp char *repo_path = realpath(argv[0], NULL);
2765 c2301be8 2018-04-30 stsp if (repo_path) {
2766 c2301be8 2018-04-30 stsp struct got_repository *repo;
2767 c2301be8 2018-04-30 stsp error = got_repo_open(&repo, repo_path);
2768 c2301be8 2018-04-30 stsp if (error == NULL)
2769 c2301be8 2018-04-30 stsp got_repo_close(repo);
2771 ad7de8d9 2018-04-30 stsp error = got_error_from_errno();
2772 c2301be8 2018-04-30 stsp if (error) {
2773 f29d3e89 2018-06-23 stsp if (hflag) {
2774 f29d3e89 2018-06-23 stsp fprintf(stderr, "%s: '%s' is not a "
2775 f29d3e89 2018-06-23 stsp "known command\n", getprogname(),
2779 ad7de8d9 2018-04-30 stsp fprintf(stderr, "%s: '%s' is neither a known "
2780 ad7de8d9 2018-04-30 stsp "command nor a path to a repository\n",
2781 c2301be8 2018-04-30 stsp getprogname(), argv[0]);
2782 ad7de8d9 2018-04-30 stsp free(repo_path);
2785 c2301be8 2018-04-30 stsp cmd = &tog_commands[0];
2786 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, repo_path);
2788 c2301be8 2018-04-30 stsp free(repo_path);
2792 5c5136c5 2018-05-20 stsp init_curses();
2794 c2301be8 2018-04-30 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
2795 9f7d7167 2018-04-29 stsp if (error)
2796 9f7d7167 2018-04-29 stsp goto done;
2799 c2301be8 2018-04-30 stsp free(cmd_argv);
2800 9f7d7167 2018-04-29 stsp if (error)
2801 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);