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 cc3c9aac 2018-08-01 stsp struct tog_view {
86 26ed57b2 2018-05-19 stsp WINDOW *window;
87 26ed57b2 2018-05-19 stsp PANEL *panel;
88 97ddc146 2018-08-01 stsp int nlines, ncols, begin_y, begin_x;
89 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
90 6d0fee91 2018-08-01 stsp struct tog_view *parent;
93 cd0acaa7 2018-05-20 stsp static const struct got_error *
94 ea5e7bb5 2018-08-01 stsp show_diff_view(struct tog_view *, struct got_object *, struct got_object *,
95 cd0acaa7 2018-05-20 stsp struct got_repository *);
96 cd0acaa7 2018-05-20 stsp static const struct got_error *
97 04cc582a 2018-08-01 stsp show_log_view(struct tog_view *, struct got_object_id *,
98 04cc582a 2018-08-01 stsp struct got_repository *, const char *);
99 a70480e0 2018-06-23 stsp static const struct got_error *
100 e1cd8fed 2018-08-01 stsp show_blame_view(struct tog_view *, const char *, struct got_object_id *,
101 e1cd8fed 2018-08-01 stsp struct got_repository *);
102 ffd1d5e5 2018-06-23 stsp static const struct got_error *
103 5221c383 2018-08-01 stsp show_tree_view(struct tog_view *, struct got_tree_object *,
104 5221c383 2018-08-01 stsp struct got_object_id *, struct got_repository *);
106 ea5e7bb5 2018-08-01 stsp static void
107 ea5e7bb5 2018-08-01 stsp close_view(struct tog_view *view)
109 ea5e7bb5 2018-08-01 stsp if (view->panel)
110 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
111 ea5e7bb5 2018-08-01 stsp if (view->window)
112 ea5e7bb5 2018-08-01 stsp delwin(view->window);
113 ea5e7bb5 2018-08-01 stsp free(view);
116 ea5e7bb5 2018-08-01 stsp static struct tog_view *
117 6d0fee91 2018-08-01 stsp open_view(int nlines, int ncols, int begin_y, int begin_x,
118 6d0fee91 2018-08-01 stsp struct tog_view *parent)
120 ea5e7bb5 2018-08-01 stsp struct tog_view *view = malloc(sizeof(*view));
122 ea5e7bb5 2018-08-01 stsp if (view == NULL)
123 ea5e7bb5 2018-08-01 stsp return NULL;
125 6d0fee91 2018-08-01 stsp view->parent = parent;
126 f7d12f7e 2018-08-01 stsp view->lines = LINES;
127 f7d12f7e 2018-08-01 stsp view->cols = COLS;
128 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
129 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
130 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
131 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
132 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
133 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
134 ea5e7bb5 2018-08-01 stsp close_view(view);
135 ea5e7bb5 2018-08-01 stsp return NULL;
137 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
138 ea5e7bb5 2018-08-01 stsp if (view->panel == NULL) {
139 ea5e7bb5 2018-08-01 stsp close_view(view);
140 ea5e7bb5 2018-08-01 stsp return NULL;
143 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
144 ea5e7bb5 2018-08-01 stsp return view;
147 f7d12f7e 2018-08-01 stsp const struct got_error *
148 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
150 f7d12f7e 2018-08-01 stsp int nlines, ncols;
152 6d0fee91 2018-08-01 stsp while (view) {
153 6d0fee91 2018-08-01 stsp if (view->lines > LINES)
154 6d0fee91 2018-08-01 stsp nlines = view->nlines - (view->lines - LINES);
156 6d0fee91 2018-08-01 stsp nlines = view->nlines + (LINES - view->lines);
158 6d0fee91 2018-08-01 stsp if (view->cols > COLS)
159 6d0fee91 2018-08-01 stsp ncols = view->ncols - (view->cols - COLS);
161 6d0fee91 2018-08-01 stsp ncols = view->ncols + (COLS - view->cols);
163 6d0fee91 2018-08-01 stsp if (wresize(view->window, nlines, ncols) == ERR)
164 6d0fee91 2018-08-01 stsp return got_error_from_errno();
166 6d0fee91 2018-08-01 stsp view->nlines = nlines;
167 6d0fee91 2018-08-01 stsp view->ncols = ncols;
168 6d0fee91 2018-08-01 stsp view->lines = LINES;
169 6d0fee91 2018-08-01 stsp view->cols = COLS;
171 6d0fee91 2018-08-01 stsp view = view->parent;
174 f7d12f7e 2018-08-01 stsp return NULL;
177 4ed7e80c 2018-05-20 stsp __dead static void
178 9f7d7167 2018-04-29 stsp usage_log(void)
181 ecb28ae0 2018-07-16 stsp fprintf(stderr, "usage: %s log [-c commit] [-r repository-path] [path]\n",
182 9f7d7167 2018-04-29 stsp getprogname());
186 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
187 80ddbec8 2018-04-29 stsp static const struct got_error *
188 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
190 00dfcb92 2018-06-11 stsp char *vis = NULL;
191 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
193 963b370f 2018-05-20 stsp *ws = NULL;
194 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
195 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
196 00dfcb92 2018-06-11 stsp int vislen;
197 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
198 00dfcb92 2018-06-11 stsp return got_error_from_errno();
200 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
201 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
203 00dfcb92 2018-06-11 stsp return err;
204 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
205 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
206 a7f50699 2018-06-11 stsp err = got_error_from_errno(); /* give up */
211 963b370f 2018-05-20 stsp *ws = calloc(*wlen + 1, sizeof(*ws));
212 a7f50699 2018-06-11 stsp if (*ws == NULL) {
213 a7f50699 2018-06-11 stsp err = got_error_from_errno();
217 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
218 963b370f 2018-05-20 stsp err = got_error_from_errno();
223 963b370f 2018-05-20 stsp *ws = NULL;
226 963b370f 2018-05-20 stsp return err;
229 963b370f 2018-05-20 stsp /* Format a line for display, ensuring that it won't overflow a width limit. */
230 963b370f 2018-05-20 stsp static const struct got_error *
231 ffd1d5e5 2018-06-23 stsp format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
233 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
234 963b370f 2018-05-20 stsp int cols = 0;
235 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
236 963b370f 2018-05-20 stsp size_t wlen;
239 963b370f 2018-05-20 stsp *wlinep = NULL;
240 b700b5d6 2018-07-10 stsp *widthp = 0;
242 963b370f 2018-05-20 stsp err = mbs2ws(&wline, &wlen, line);
244 963b370f 2018-05-20 stsp return err;
247 b700b5d6 2018-07-10 stsp while (i < wlen && cols < wlimit) {
248 963b370f 2018-05-20 stsp int width = wcwidth(wline[i]);
249 963b370f 2018-05-20 stsp switch (width) {
255 b700b5d6 2018-07-10 stsp if (cols + width <= wlimit) {
256 b700b5d6 2018-07-10 stsp cols += width;
261 963b370f 2018-05-20 stsp if (wline[i] == L'\t')
262 b700b5d6 2018-07-10 stsp cols += TABSIZE - ((cols + 1) % TABSIZE);
266 963b370f 2018-05-20 stsp err = got_error_from_errno();
270 963b370f 2018-05-20 stsp wline[i] = L'\0';
271 b700b5d6 2018-07-10 stsp if (widthp)
272 b700b5d6 2018-07-10 stsp *widthp = cols;
275 963b370f 2018-05-20 stsp free(wline);
277 963b370f 2018-05-20 stsp *wlinep = wline;
278 963b370f 2018-05-20 stsp return err;
281 963b370f 2018-05-20 stsp static const struct got_error *
282 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
283 2814baeb 2018-08-01 stsp struct got_object_id *id)
285 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
286 b39d25c7 2018-07-10 stsp char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
287 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
288 6d9fbc00 2018-04-29 stsp char *author0 = NULL, *author = NULL;
289 bb737323 2018-05-20 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
290 bb737323 2018-05-20 stsp int author_width, logmsg_width;
291 6d9fbc00 2018-04-29 stsp char *newline, *smallerthan;
292 80ddbec8 2018-04-29 stsp char *line = NULL;
293 bb737323 2018-05-20 stsp int col, limit;
294 b39d25c7 2018-07-10 stsp static const size_t date_display_cols = 9;
295 bb737323 2018-05-20 stsp static const size_t author_display_cols = 16;
296 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
298 b39d25c7 2018-07-10 stsp if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &commit->tm_committer)
299 b39d25c7 2018-07-10 stsp >= sizeof(datebuf))
300 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
302 b39d25c7 2018-07-10 stsp if (avail < date_display_cols)
303 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
305 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
306 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
307 b39d25c7 2018-07-10 stsp col = limit + 1;
308 b39d25c7 2018-07-10 stsp if (col > avail)
311 6d9fbc00 2018-04-29 stsp author0 = strdup(commit->author);
312 6d9fbc00 2018-04-29 stsp if (author0 == NULL) {
313 80ddbec8 2018-04-29 stsp err = got_error_from_errno();
316 6d9fbc00 2018-04-29 stsp author = author0;
317 6d9fbc00 2018-04-29 stsp smallerthan = strchr(author, '<');
318 6d9fbc00 2018-04-29 stsp if (smallerthan)
319 6d9fbc00 2018-04-29 stsp *smallerthan = '\0';
321 6d9fbc00 2018-04-29 stsp char *at = strchr(author, '@');
323 6d9fbc00 2018-04-29 stsp *at = '\0';
325 b39d25c7 2018-07-10 stsp limit = avail - col;
326 bb737323 2018-05-20 stsp err = format_line(&wauthor, &author_width, author, limit);
329 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
330 bb737323 2018-05-20 stsp col += author_width;
331 9c2eaf34 2018-05-20 stsp while (col <= avail && author_width < author_display_cols + 1) {
332 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
334 bb737323 2018-05-20 stsp author_width++;
336 9c2eaf34 2018-05-20 stsp if (col > avail)
339 bb737323 2018-05-20 stsp logmsg0 = strdup(commit->logmsg);
340 bb737323 2018-05-20 stsp if (logmsg0 == NULL) {
341 6d9fbc00 2018-04-29 stsp err = got_error_from_errno();
344 bb737323 2018-05-20 stsp logmsg = logmsg0;
345 bb737323 2018-05-20 stsp while (*logmsg == '\n')
347 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
348 bb737323 2018-05-20 stsp if (newline)
349 bb737323 2018-05-20 stsp *newline = '\0';
350 bb737323 2018-05-20 stsp limit = avail - col;
351 bb737323 2018-05-20 stsp err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
354 2814baeb 2018-08-01 stsp waddwstr(view->window, wlogmsg);
355 bb737323 2018-05-20 stsp col += logmsg_width;
356 bb737323 2018-05-20 stsp while (col <= avail) {
357 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
361 80ddbec8 2018-04-29 stsp free(logmsg0);
362 bb737323 2018-05-20 stsp free(wlogmsg);
363 6d9fbc00 2018-04-29 stsp free(author0);
364 bb737323 2018-05-20 stsp free(wauthor);
365 80ddbec8 2018-04-29 stsp free(line);
366 80ddbec8 2018-04-29 stsp return err;
369 80ddbec8 2018-04-29 stsp struct commit_queue_entry {
370 80ddbec8 2018-04-29 stsp TAILQ_ENTRY(commit_queue_entry) entry;
371 80ddbec8 2018-04-29 stsp struct got_object_id *id;
372 80ddbec8 2018-04-29 stsp struct got_commit_object *commit;
374 ecb28ae0 2018-07-16 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
375 ecb28ae0 2018-07-16 stsp struct commit_queue {
376 ecb28ae0 2018-07-16 stsp int ncommits;
377 ecb28ae0 2018-07-16 stsp struct commit_queue_head head;
380 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
381 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
382 899d86c2 2018-05-10 stsp struct got_object_id *id)
384 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
386 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
387 80ddbec8 2018-04-29 stsp if (entry == NULL)
388 899d86c2 2018-05-10 stsp return NULL;
390 899d86c2 2018-05-10 stsp entry->id = id;
391 99db9666 2018-05-07 stsp entry->commit = commit;
392 899d86c2 2018-05-10 stsp return entry;
395 99db9666 2018-05-07 stsp static void
396 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
398 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
400 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
401 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
402 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
403 ecb28ae0 2018-07-16 stsp commits->ncommits--;
404 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
405 99db9666 2018-05-07 stsp free(entry);
408 99db9666 2018-05-07 stsp static void
409 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
411 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
412 99db9666 2018-05-07 stsp pop_commit(commits);
415 c4972b91 2018-05-07 stsp static const struct got_error *
416 9ba79e04 2018-06-11 stsp queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
417 ecb28ae0 2018-07-16 stsp struct got_object_id *start_id, int minqueue, int init,
418 ecb28ae0 2018-07-16 stsp struct got_repository *repo, const char *path)
420 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
421 899d86c2 2018-05-10 stsp struct got_object_id *id;
422 9ba79e04 2018-06-11 stsp struct commit_queue_entry *entry;
423 ecb28ae0 2018-07-16 stsp int nfetched, nqueued = 0, found_obj = 0;
424 c8f60bff 2018-07-23 stsp int is_root_path = strcmp(path, "/") == 0;
426 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_start(graph, start_id);
428 c4972b91 2018-05-07 stsp return err;
430 ecb28ae0 2018-07-16 stsp entry = TAILQ_LAST(&commits->head, commit_queue_head);
431 9ba79e04 2018-06-11 stsp if (entry && got_object_id_cmp(entry->id, start_id) == 0) {
432 9ba79e04 2018-06-11 stsp int nfetched;
434 9ba79e04 2018-06-11 stsp /* Start ID's commit is already on the queue; skip over it. */
435 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
436 9ba79e04 2018-06-11 stsp if (err && err->code != GOT_ERR_ITER_NEED_MORE)
437 9ba79e04 2018-06-11 stsp return err;
439 9ba79e04 2018-06-11 stsp err = got_commit_graph_fetch_commits(&nfetched, graph, 1, repo);
441 9ba79e04 2018-06-11 stsp return err;
444 9ba79e04 2018-06-11 stsp while (1) {
445 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
447 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
449 ecb28ae0 2018-07-16 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
451 ecb28ae0 2018-07-16 stsp if (nqueued >= minqueue) {
452 9ba79e04 2018-06-11 stsp err = NULL;
455 ecb28ae0 2018-07-16 stsp err = got_commit_graph_fetch_commits(&nfetched,
456 ecb28ae0 2018-07-16 stsp graph, 1, repo);
458 ecb28ae0 2018-07-16 stsp return err;
461 ecb28ae0 2018-07-16 stsp if (id == NULL)
464 9ba79e04 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
468 c8f60bff 2018-07-23 stsp if (!is_root_path) {
469 ecb28ae0 2018-07-16 stsp struct got_object *obj;
470 ecb28ae0 2018-07-16 stsp struct got_object_qid *pid;
471 ecb28ae0 2018-07-16 stsp int changed = 0;
473 ecb28ae0 2018-07-16 stsp err = got_object_open_by_path(&obj, repo, id, path);
475 c8f60bff 2018-07-23 stsp got_object_commit_close(commit);
476 ecb28ae0 2018-07-16 stsp if (err->code == GOT_ERR_NO_OBJ &&
477 ecb28ae0 2018-07-16 stsp (found_obj || !init)) {
478 ecb28ae0 2018-07-16 stsp /* History stops here. */
479 ecb28ae0 2018-07-16 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
483 ecb28ae0 2018-07-16 stsp found_obj = 1;
485 ecb28ae0 2018-07-16 stsp pid = SIMPLEQ_FIRST(&commit->parent_ids);
486 ecb28ae0 2018-07-16 stsp if (pid != NULL) {
487 ecb28ae0 2018-07-16 stsp struct got_object *pobj;
488 ecb28ae0 2018-07-16 stsp err = got_object_open_by_path(&pobj, repo,
489 ecb28ae0 2018-07-16 stsp pid->id, path);
491 ecb28ae0 2018-07-16 stsp if (err->code != GOT_ERR_NO_OBJ) {
492 ecb28ae0 2018-07-16 stsp got_object_close(obj);
493 c8f60bff 2018-07-23 stsp got_object_commit_close(commit);
496 ecb28ae0 2018-07-16 stsp err = NULL;
497 ecb28ae0 2018-07-16 stsp changed = 1;
499 c2f16475 2018-07-23 stsp struct got_object_id *id, *pid;
500 c2f16475 2018-07-23 stsp id = got_object_get_id(obj);
501 c2f16475 2018-07-23 stsp if (id == NULL) {
502 c2f16475 2018-07-23 stsp err = got_error_from_errno();
503 c8f60bff 2018-07-23 stsp got_object_close(obj);
504 c8f60bff 2018-07-23 stsp got_object_close(pobj);
507 c2f16475 2018-07-23 stsp pid = got_object_get_id(pobj);
508 c2f16475 2018-07-23 stsp if (pid == NULL) {
509 c2f16475 2018-07-23 stsp err = got_error_from_errno();
511 c8f60bff 2018-07-23 stsp got_object_close(obj);
512 c8f60bff 2018-07-23 stsp got_object_close(pobj);
516 c2f16475 2018-07-23 stsp (got_object_id_cmp(id, pid) != 0);
517 ecb28ae0 2018-07-16 stsp got_object_close(pobj);
522 c8f60bff 2018-07-23 stsp got_object_close(obj);
523 ecb28ae0 2018-07-16 stsp if (!changed) {
524 ecb28ae0 2018-07-16 stsp got_object_commit_close(commit);
529 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
530 9ba79e04 2018-06-11 stsp if (entry == NULL) {
531 9ba79e04 2018-06-11 stsp err = got_error_from_errno();
534 ecb28ae0 2018-07-16 stsp TAILQ_INSERT_TAIL(&commits->head, entry, entry);
536 ecb28ae0 2018-07-16 stsp commits->ncommits++;
539 9ba79e04 2018-06-11 stsp return err;
542 99db9666 2018-05-07 stsp static const struct got_error *
543 9ba79e04 2018-06-11 stsp fetch_next_commit(struct commit_queue_entry **pentry,
544 9ba79e04 2018-06-11 stsp struct commit_queue_entry *entry, struct commit_queue *commits,
545 ecb28ae0 2018-07-16 stsp struct got_commit_graph *graph, struct got_repository *repo,
546 ecb28ae0 2018-07-16 stsp const char *path)
548 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
550 9ba79e04 2018-06-11 stsp *pentry = NULL;
552 ecb28ae0 2018-07-16 stsp err = queue_commits(graph, commits, entry->id, 1, 0, repo, path);
554 9ba79e04 2018-06-11 stsp return err;
556 9ba79e04 2018-06-11 stsp /* Next entry to display should now be available. */
557 9ba79e04 2018-06-11 stsp *pentry = TAILQ_NEXT(entry, entry);
558 9ba79e04 2018-06-11 stsp if (*pentry == NULL)
559 9ba79e04 2018-06-11 stsp return got_error(GOT_ERR_NO_OBJ);
561 9ba79e04 2018-06-11 stsp return NULL;
564 899d86c2 2018-05-10 stsp static const struct got_error *
565 9ba79e04 2018-06-11 stsp get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
567 9ba79e04 2018-06-11 stsp const struct got_error *err = NULL;
568 9ba79e04 2018-06-11 stsp struct got_reference *head_ref;
570 9ba79e04 2018-06-11 stsp *head_id = NULL;
572 9ba79e04 2018-06-11 stsp err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
574 99db9666 2018-05-07 stsp return err;
576 9ba79e04 2018-06-11 stsp err = got_ref_resolve(head_id, repo, head_ref);
577 9ba79e04 2018-06-11 stsp got_ref_close(head_ref);
579 9ba79e04 2018-06-11 stsp *head_id = NULL;
580 99db9666 2018-05-07 stsp return err;
583 9ba79e04 2018-06-11 stsp return NULL;
586 0553a4e3 2018-04-30 stsp static const struct got_error *
587 2814baeb 2018-08-01 stsp draw_commits(struct tog_view *view, struct commit_queue_entry **last,
588 ecb28ae0 2018-07-16 stsp struct commit_queue_entry **selected, struct commit_queue_entry *first,
589 a7f40148 2018-07-18 stsp struct commit_queue *commits, int selected_idx, int limit,
590 a7f40148 2018-07-18 stsp struct got_commit_graph *graph, struct got_repository *repo,
591 a7f40148 2018-07-18 stsp const char *path)
593 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
594 0553a4e3 2018-04-30 stsp struct commit_queue_entry *entry;
595 ecb28ae0 2018-07-16 stsp int ncommits, width;
596 867c6645 2018-07-10 stsp char *id_str, *header;
597 ecb28ae0 2018-07-16 stsp wchar_t *wline;
599 e0d42f60 2018-07-22 stsp entry = first;
600 e0d42f60 2018-07-22 stsp ncommits = 0;
601 e0d42f60 2018-07-22 stsp while (entry) {
602 e0d42f60 2018-07-22 stsp if (ncommits == selected_idx) {
603 e0d42f60 2018-07-22 stsp *selected = entry;
606 e0d42f60 2018-07-22 stsp entry = TAILQ_NEXT(entry, entry);
607 e0d42f60 2018-07-22 stsp ncommits++;
610 867c6645 2018-07-10 stsp err = got_object_id_str(&id_str, (*selected)->id);
612 867c6645 2018-07-10 stsp return err;
614 ecb28ae0 2018-07-16 stsp if (path) {
615 ecb28ae0 2018-07-16 stsp if (asprintf(&header, "commit: %s [%s]", id_str, path) == -1) {
616 ecb28ae0 2018-07-16 stsp err = got_error_from_errno();
617 ecb28ae0 2018-07-16 stsp free(id_str);
618 ecb28ae0 2018-07-16 stsp return err;
620 ecb28ae0 2018-07-16 stsp } else if (asprintf(&header, "commit: %s", id_str) == -1) {
621 867c6645 2018-07-10 stsp err = got_error_from_errno();
622 867c6645 2018-07-10 stsp free(id_str);
623 867c6645 2018-07-10 stsp return err;
625 ecb28ae0 2018-07-16 stsp free(id_str);
626 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, header, view->ncols);
628 ecb28ae0 2018-07-16 stsp free(header);
629 ecb28ae0 2018-07-16 stsp return err;
631 ecb28ae0 2018-07-16 stsp free(header);
633 2814baeb 2018-08-01 stsp werase(view->window);
635 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
636 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
637 2814baeb 2018-08-01 stsp waddch(view->window, '\n');
638 ecb28ae0 2018-07-16 stsp free(wline);
639 ecb28ae0 2018-07-16 stsp if (limit <= 1)
640 ecb28ae0 2018-07-16 stsp return NULL;
642 899d86c2 2018-05-10 stsp entry = first;
643 899d86c2 2018-05-10 stsp *last = first;
644 867c6645 2018-07-10 stsp ncommits = 0;
645 899d86c2 2018-05-10 stsp while (entry) {
646 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
648 e0d42f60 2018-07-22 stsp if (ncommits == selected_idx)
649 2814baeb 2018-08-01 stsp wstandout(view->window);
650 2814baeb 2018-08-01 stsp err = draw_commit(view, entry->commit, entry->id);
651 cd0acaa7 2018-05-20 stsp if (ncommits == selected_idx)
652 2814baeb 2018-08-01 stsp wstandend(view->window);
655 0553a4e3 2018-04-30 stsp ncommits++;
656 899d86c2 2018-05-10 stsp *last = entry;
657 a7f40148 2018-07-18 stsp if (entry == TAILQ_LAST(&commits->head, commit_queue_head)) {
658 a7f40148 2018-07-18 stsp err = queue_commits(graph, commits, entry->id, 1,
659 a7f40148 2018-07-18 stsp 0, repo, path);
661 a7f40148 2018-07-18 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
662 a7f40148 2018-07-18 stsp return err;
663 a7f40148 2018-07-18 stsp err = NULL;
666 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
669 80ddbec8 2018-04-29 stsp update_panels();
670 80ddbec8 2018-04-29 stsp doupdate();
672 80ddbec8 2018-04-29 stsp return err;
675 07b55e75 2018-05-10 stsp static void
676 16482c3b 2018-05-20 stsp scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
677 07b55e75 2018-05-10 stsp struct commit_queue *commits)
679 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
680 07b55e75 2018-05-10 stsp int nscrolled = 0;
682 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
683 07b55e75 2018-05-10 stsp if (*first_displayed_entry == entry)
686 07b55e75 2018-05-10 stsp entry = *first_displayed_entry;
687 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
688 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
689 07b55e75 2018-05-10 stsp if (entry) {
690 07b55e75 2018-05-10 stsp *first_displayed_entry = entry;
691 07b55e75 2018-05-10 stsp nscrolled++;
696 aa075928 2018-05-10 stsp static const struct got_error *
697 16482c3b 2018-05-20 stsp scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
698 aa075928 2018-05-10 stsp struct commit_queue_entry *last_displayed_entry,
699 9ba79e04 2018-06-11 stsp struct commit_queue *commits, struct got_commit_graph *graph,
700 ecb28ae0 2018-07-16 stsp struct got_repository *repo, const char *path)
702 aa075928 2018-05-10 stsp const struct got_error *err = NULL;
703 dd0a52c1 2018-05-20 stsp struct commit_queue_entry *pentry;
704 aa075928 2018-05-10 stsp int nscrolled = 0;
707 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(last_displayed_entry, entry);
708 aa075928 2018-05-10 stsp if (pentry == NULL) {
709 9ba79e04 2018-06-11 stsp err = fetch_next_commit(&pentry, last_displayed_entry,
710 ecb28ae0 2018-07-16 stsp commits, graph, repo, path);
711 9a6bf2a5 2018-05-20 stsp if (err || pentry == NULL)
714 dd0a52c1 2018-05-20 stsp last_displayed_entry = pentry;
716 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(*first_displayed_entry, entry);
717 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
719 aa075928 2018-05-10 stsp *first_displayed_entry = pentry;
720 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
722 dd0a52c1 2018-05-20 stsp return err;
725 cd0acaa7 2018-05-20 stsp static const struct got_error *
726 6d0fee91 2018-08-01 stsp show_commit(struct tog_view *parent_view, struct commit_queue_entry *entry,
727 6d0fee91 2018-08-01 stsp struct got_repository *repo)
729 cd0acaa7 2018-05-20 stsp const struct got_error *err;
730 cd0acaa7 2018-05-20 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
731 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
732 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
734 cd0acaa7 2018-05-20 stsp err = got_object_open(&obj2, repo, entry->id);
736 cd0acaa7 2018-05-20 stsp return err;
738 9ba79e04 2018-06-11 stsp parent_id = SIMPLEQ_FIRST(&entry->commit->parent_ids);
739 9ba79e04 2018-06-11 stsp if (parent_id) {
740 9ba79e04 2018-06-11 stsp err = got_object_open(&obj1, repo, parent_id->id);
745 6d0fee91 2018-08-01 stsp view = open_view(0, 0, 0, 0, parent_view);
746 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
747 ea5e7bb5 2018-08-01 stsp err = got_error_from_errno();
751 ea5e7bb5 2018-08-01 stsp err = show_diff_view(view, obj1, obj2, repo);
752 ea5e7bb5 2018-08-01 stsp close_view(view);
755 cd0acaa7 2018-05-20 stsp got_object_close(obj1);
757 cd0acaa7 2018-05-20 stsp got_object_close(obj2);
758 cd0acaa7 2018-05-20 stsp return err;
761 80ddbec8 2018-04-29 stsp static const struct got_error *
762 6d0fee91 2018-08-01 stsp browse_commit(struct tog_view *parent_view, struct commit_queue_entry *entry,
763 6d0fee91 2018-08-01 stsp struct got_repository *repo)
765 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
766 9343a5fb 2018-06-23 stsp struct got_tree_object *tree;
767 5221c383 2018-08-01 stsp struct tog_view *view;
769 9343a5fb 2018-06-23 stsp err = got_object_open_as_tree(&tree, repo, entry->commit->tree_id);
771 9343a5fb 2018-06-23 stsp return err;
773 6d0fee91 2018-08-01 stsp view = open_view(0, 0, 0, 0, parent_view);
774 5221c383 2018-08-01 stsp if (view == NULL) {
775 5221c383 2018-08-01 stsp err = got_error_from_errno();
778 5221c383 2018-08-01 stsp err = show_tree_view(view, tree, entry->id, repo);
779 5221c383 2018-08-01 stsp close_view(view);
781 9343a5fb 2018-06-23 stsp got_object_tree_close(tree);
782 9343a5fb 2018-06-23 stsp return err;
785 9343a5fb 2018-06-23 stsp static const struct got_error *
786 04cc582a 2018-08-01 stsp show_log_view(struct tog_view *view, struct got_object_id *start_id,
787 04cc582a 2018-08-01 stsp struct got_repository *repo, const char *path)
789 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
790 9ba79e04 2018-06-11 stsp struct got_object_id *head_id = NULL;
791 ecb28ae0 2018-07-16 stsp int ch, done = 0, selected = 0, nfetched;
792 ecb28ae0 2018-07-16 stsp struct got_commit_graph *graph = NULL;
793 0553a4e3 2018-04-30 stsp struct commit_queue commits;
794 899d86c2 2018-05-10 stsp struct commit_queue_entry *first_displayed_entry = NULL;
795 899d86c2 2018-05-10 stsp struct commit_queue_entry *last_displayed_entry = NULL;
796 cd0acaa7 2018-05-20 stsp struct commit_queue_entry *selected_entry = NULL;
797 ecb28ae0 2018-07-16 stsp char *in_repo_path = NULL;
799 ecb28ae0 2018-07-16 stsp err = got_repo_map_path(&in_repo_path, repo, path);
800 ecb28ae0 2018-07-16 stsp if (err != NULL)
803 9ba79e04 2018-06-11 stsp err = get_head_commit_id(&head_id, repo);
805 9ba79e04 2018-06-11 stsp return err;
807 034e3b69 2018-07-22 stsp /* The graph contains all commits. */
808 5489743f 2018-06-13 stsp err = got_commit_graph_open(&graph, head_id, 0, repo);
811 034e3b69 2018-07-22 stsp /* The commit queue contains a subset of commits filtered by path. */
812 034e3b69 2018-07-22 stsp TAILQ_INIT(&commits.head);
813 034e3b69 2018-07-22 stsp commits.ncommits = 0;
815 9ba79e04 2018-06-11 stsp /* Populate commit graph with a sufficient number of commits. */
816 9ba79e04 2018-06-11 stsp err = got_commit_graph_fetch_commits_up_to(&nfetched, graph, start_id,
822 9ba79e04 2018-06-11 stsp * Open the initial batch of commits, sorted in commit graph order.
823 9ba79e04 2018-06-11 stsp * We keep all commits open throughout the lifetime of the log view
824 9ba79e04 2018-06-11 stsp * in order to avoid having to re-fetch commits from disk while
825 9ba79e04 2018-06-11 stsp * updating the display.
827 f7d12f7e 2018-08-01 stsp err = queue_commits(graph, &commits, start_id, view->nlines, 1, repo,
828 ecb28ae0 2018-07-16 stsp in_repo_path);
830 55198a88 2018-07-16 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
832 55198a88 2018-07-16 stsp err = NULL;
835 04cc582a 2018-08-01 stsp show_panel(view->panel);
837 034e3b69 2018-07-22 stsp first_displayed_entry = TAILQ_FIRST(&commits.head);
838 867c6645 2018-07-10 stsp selected_entry = first_displayed_entry;
839 899d86c2 2018-05-10 stsp while (!done) {
840 2814baeb 2018-08-01 stsp err = draw_commits(view, &last_displayed_entry, &selected_entry,
841 f7d12f7e 2018-08-01 stsp first_displayed_entry, &commits, selected, view->nlines,
842 a7f40148 2018-07-18 stsp graph, repo, in_repo_path);
846 80ddbec8 2018-04-29 stsp nodelay(stdscr, FALSE);
847 2814baeb 2018-08-01 stsp ch = wgetch(view->window);
848 f7182337 2018-05-20 stsp nodelay(stdscr, TRUE);
849 80ddbec8 2018-04-29 stsp switch (ch) {
856 80ddbec8 2018-04-29 stsp case KEY_UP:
857 80ddbec8 2018-04-29 stsp if (selected > 0)
858 80ddbec8 2018-04-29 stsp selected--;
859 8ec9698d 2018-05-10 stsp if (selected > 0)
861 07b55e75 2018-05-10 stsp scroll_up(&first_displayed_entry, 1, &commits);
863 48531068 2018-05-10 stsp case KEY_PPAGE:
864 ecb28ae0 2018-07-16 stsp if (TAILQ_FIRST(&commits.head) ==
865 dfc1d240 2018-05-10 stsp first_displayed_entry) {
866 dfc1d240 2018-05-10 stsp selected = 0;
869 f7d12f7e 2018-08-01 stsp scroll_up(&first_displayed_entry, view->nlines,
873 80ddbec8 2018-04-29 stsp case KEY_DOWN:
874 f7d12f7e 2018-08-01 stsp if (selected < MIN(view->nlines - 2,
875 ecb28ae0 2018-07-16 stsp commits.ncommits - 1)) {
876 15c91275 2018-05-20 stsp selected++;
879 15c91275 2018-05-20 stsp err = scroll_down(&first_displayed_entry, 1,
880 9ba79e04 2018-06-11 stsp last_displayed_entry, &commits, graph,
881 ecb28ae0 2018-07-16 stsp repo, in_repo_path);
883 ecb28ae0 2018-07-16 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
885 ecb28ae0 2018-07-16 stsp err = NULL;
888 ecb28ae0 2018-07-16 stsp case KEY_NPAGE: {
889 ecb28ae0 2018-07-16 stsp struct commit_queue_entry *first = first_displayed_entry;
890 f7d12f7e 2018-08-01 stsp err = scroll_down(&first_displayed_entry, view->nlines,
891 9ba79e04 2018-06-11 stsp last_displayed_entry, &commits, graph,
892 ecb28ae0 2018-07-16 stsp repo, in_repo_path);
894 ecb28ae0 2018-07-16 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
896 ecb28ae0 2018-07-16 stsp /* can't scroll any further; move cursor down */
897 ecb28ae0 2018-07-16 stsp if (first == first_displayed_entry && selected <
898 f7d12f7e 2018-08-01 stsp MIN(view->nlines - 2, commits.ncommits - 1)) {
899 f7d12f7e 2018-08-01 stsp selected = MIN(view->nlines - 2,
900 ecb28ae0 2018-07-16 stsp commits.ncommits - 1);
902 ecb28ae0 2018-07-16 stsp err = NULL;
906 d6df9be4 2018-04-30 stsp case KEY_RESIZE:
907 a41d2007 2018-08-01 stsp err = view_resize(view);
910 f7d12f7e 2018-08-01 stsp if (selected > view->nlines - 2)
911 f7d12f7e 2018-08-01 stsp selected = view->nlines - 2;
912 55198a88 2018-07-16 stsp if (selected > commits.ncommits - 1)
913 55198a88 2018-07-16 stsp selected = commits.ncommits - 1;
915 cd0acaa7 2018-05-20 stsp case KEY_ENTER:
917 6d0fee91 2018-08-01 stsp err = show_commit(view, selected_entry, repo);
920 2814baeb 2018-08-01 stsp show_panel(view->panel);
923 6d0fee91 2018-08-01 stsp err = browse_commit(view, selected_entry, repo);
926 2814baeb 2018-08-01 stsp show_panel(view->panel);
933 9ba79e04 2018-06-11 stsp free(head_id);
935 9ba79e04 2018-06-11 stsp got_commit_graph_close(graph);
936 0553a4e3 2018-04-30 stsp free_commits(&commits);
937 ecb28ae0 2018-07-16 stsp free(in_repo_path);
938 80ddbec8 2018-04-29 stsp return err;
941 4ed7e80c 2018-05-20 stsp static const struct got_error *
942 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
944 80ddbec8 2018-04-29 stsp const struct got_error *error;
945 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
946 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
947 ecb28ae0 2018-07-16 stsp char *path = NULL, *repo_path = NULL, *cwd = NULL;
948 80ddbec8 2018-04-29 stsp char *start_commit = NULL;
950 04cc582a 2018-08-01 stsp struct tog_view *view;
952 80ddbec8 2018-04-29 stsp #ifndef PROFILE
953 80ddbec8 2018-04-29 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
954 80ddbec8 2018-04-29 stsp err(1, "pledge");
957 ecb28ae0 2018-07-16 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
958 80ddbec8 2018-04-29 stsp switch (ch) {
960 80ddbec8 2018-04-29 stsp start_commit = optarg;
963 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
964 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
965 ecb28ae0 2018-07-16 stsp err(1, "-r option");
969 80ddbec8 2018-04-29 stsp /* NOTREACHED */
973 80ddbec8 2018-04-29 stsp argc -= optind;
974 80ddbec8 2018-04-29 stsp argv += optind;
976 ecb28ae0 2018-07-16 stsp if (argc == 0)
977 ecb28ae0 2018-07-16 stsp path = strdup("");
978 ecb28ae0 2018-07-16 stsp else if (argc == 1)
979 ecb28ae0 2018-07-16 stsp path = strdup(argv[0]);
981 80ddbec8 2018-04-29 stsp usage_log();
982 ecb28ae0 2018-07-16 stsp if (path == NULL)
983 ecb28ae0 2018-07-16 stsp return got_error_from_errno();
985 ecb28ae0 2018-07-16 stsp cwd = getcwd(NULL, 0);
986 ecb28ae0 2018-07-16 stsp if (cwd == NULL) {
987 ecb28ae0 2018-07-16 stsp error = got_error_from_errno();
990 ecb28ae0 2018-07-16 stsp if (repo_path == NULL) {
991 ecb28ae0 2018-07-16 stsp repo_path = strdup(cwd);
992 ecb28ae0 2018-07-16 stsp if (repo_path == NULL) {
993 ecb28ae0 2018-07-16 stsp error = got_error_from_errno();
998 80ddbec8 2018-04-29 stsp error = got_repo_open(&repo, repo_path);
999 80ddbec8 2018-04-29 stsp if (error != NULL)
1000 ecb28ae0 2018-07-16 stsp goto done;
1002 80ddbec8 2018-04-29 stsp if (start_commit == NULL) {
1003 899d86c2 2018-05-10 stsp error = get_head_commit_id(&start_id, repo);
1004 80ddbec8 2018-04-29 stsp if (error != NULL)
1005 ecb28ae0 2018-07-16 stsp goto done;
1007 80ddbec8 2018-04-29 stsp struct got_object *obj;
1008 80ddbec8 2018-04-29 stsp error = got_object_open_by_id_str(&obj, repo, start_commit);
1009 80ddbec8 2018-04-29 stsp if (error == NULL) {
1010 899d86c2 2018-05-10 stsp start_id = got_object_get_id(obj);
1011 899d86c2 2018-05-10 stsp if (start_id == NULL)
1012 80ddbec8 2018-04-29 stsp error = got_error_from_errno();
1013 ecb28ae0 2018-07-16 stsp goto done;
1016 80ddbec8 2018-04-29 stsp if (error != NULL)
1017 ecb28ae0 2018-07-16 stsp goto done;
1019 6d0fee91 2018-08-01 stsp view = open_view(0, 0, 0, 0, NULL);
1020 04cc582a 2018-08-01 stsp if (view == NULL) {
1021 04cc582a 2018-08-01 stsp error = got_error_from_errno();
1022 04cc582a 2018-08-01 stsp goto done;
1024 04cc582a 2018-08-01 stsp error = show_log_view(view, start_id, repo, path);
1025 04cc582a 2018-08-01 stsp close_view(view);
1027 ecb28ae0 2018-07-16 stsp free(repo_path);
1028 ecb28ae0 2018-07-16 stsp free(cwd);
1029 ecb28ae0 2018-07-16 stsp free(path);
1030 899d86c2 2018-05-10 stsp free(start_id);
1032 ecb28ae0 2018-07-16 stsp got_repo_close(repo);
1033 80ddbec8 2018-04-29 stsp return error;
1036 4ed7e80c 2018-05-20 stsp __dead static void
1037 9f7d7167 2018-04-29 stsp usage_diff(void)
1040 9f7d7167 2018-04-29 stsp fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1041 9f7d7167 2018-04-29 stsp getprogname());
1045 b304db33 2018-05-20 stsp static char *
1046 b304db33 2018-05-20 stsp parse_next_line(FILE *f, size_t *len)
1048 b304db33 2018-05-20 stsp char *line;
1049 b304db33 2018-05-20 stsp size_t linelen;
1050 b304db33 2018-05-20 stsp size_t lineno;
1051 b304db33 2018-05-20 stsp const char delim[3] = { '\0', '\0', '\0'};
1053 b304db33 2018-05-20 stsp line = fparseln(f, &linelen, &lineno, delim, 0);
1055 b304db33 2018-05-20 stsp *len = linelen;
1056 b304db33 2018-05-20 stsp return line;
1059 4ed7e80c 2018-05-20 stsp static const struct got_error *
1060 f7d12f7e 2018-08-01 stsp draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1061 a70480e0 2018-06-23 stsp int *last_displayed_line, int *eof, int max_lines)
1063 61e69b96 2018-05-20 stsp const struct got_error *err;
1064 26ed57b2 2018-05-19 stsp int nlines = 0, nprinted = 0;
1065 b304db33 2018-05-20 stsp char *line;
1066 b304db33 2018-05-20 stsp size_t len;
1067 61e69b96 2018-05-20 stsp wchar_t *wline;
1068 e0b650dd 2018-05-20 stsp int width;
1070 26ed57b2 2018-05-19 stsp rewind(f);
1071 f7d12f7e 2018-08-01 stsp werase(view->window);
1074 26ed57b2 2018-05-19 stsp while (nprinted < max_lines) {
1075 b304db33 2018-05-20 stsp line = parse_next_line(f, &len);
1076 26ed57b2 2018-05-19 stsp if (line == NULL) {
1080 26ed57b2 2018-05-19 stsp if (++nlines < *first_displayed_line) {
1081 26ed57b2 2018-05-19 stsp free(line);
1085 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
1086 61e69b96 2018-05-20 stsp if (err) {
1087 61e69b96 2018-05-20 stsp free(line);
1088 2550e4c3 2018-07-13 stsp free(wline);
1089 61e69b96 2018-05-20 stsp return err;
1091 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
1092 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
1093 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
1094 26ed57b2 2018-05-19 stsp if (++nprinted == 1)
1095 26ed57b2 2018-05-19 stsp *first_displayed_line = nlines;
1096 26ed57b2 2018-05-19 stsp free(line);
1097 2550e4c3 2018-07-13 stsp free(wline);
1098 2550e4c3 2018-07-13 stsp wline = NULL;
1100 26ed57b2 2018-05-19 stsp *last_displayed_line = nlines;
1102 26ed57b2 2018-05-19 stsp update_panels();
1103 26ed57b2 2018-05-19 stsp doupdate();
1105 26ed57b2 2018-05-19 stsp return NULL;
1108 4ed7e80c 2018-05-20 stsp static const struct got_error *
1109 ea5e7bb5 2018-08-01 stsp show_diff_view(struct tog_view *view, struct got_object *obj1,
1110 ea5e7bb5 2018-08-01 stsp struct got_object *obj2, struct got_repository *repo)
1112 26ed57b2 2018-05-19 stsp const struct got_error *err;
1114 f7d12f7e 2018-08-01 stsp int ch, done = 0;
1115 f7d12f7e 2018-08-01 stsp int first_displayed_line = 1, last_displayed_line = view->nlines;
1116 b304db33 2018-05-20 stsp int eof, i;
1118 cd0acaa7 2018-05-20 stsp if (obj1 != NULL && obj2 != NULL &&
1119 cd0acaa7 2018-05-20 stsp got_object_get_type(obj1) != got_object_get_type(obj2))
1120 26ed57b2 2018-05-19 stsp return got_error(GOT_ERR_OBJ_TYPE);
1122 511a516b 2018-05-19 stsp f = got_opentemp();
1123 26ed57b2 2018-05-19 stsp if (f == NULL)
1124 26ed57b2 2018-05-19 stsp return got_error_from_errno();
1126 cd0acaa7 2018-05-20 stsp switch (got_object_get_type(obj1 ? obj1 : obj2)) {
1127 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
1128 11528a82 2018-05-19 stsp err = got_diff_objects_as_blobs(obj1, obj2, repo, f);
1130 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
1131 11528a82 2018-05-19 stsp err = got_diff_objects_as_trees(obj1, obj2, repo, f);
1133 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_COMMIT:
1134 11528a82 2018-05-19 stsp err = got_diff_objects_as_commits(obj1, obj2, repo, f);
1137 26ed57b2 2018-05-19 stsp return got_error(GOT_ERR_OBJ_TYPE);
1140 26ed57b2 2018-05-19 stsp fflush(f);
1142 ea5e7bb5 2018-08-01 stsp show_panel(view->panel);
1144 26ed57b2 2018-05-19 stsp while (!done) {
1145 f7d12f7e 2018-08-01 stsp err = draw_file(view, f, &first_displayed_line,
1146 f7d12f7e 2018-08-01 stsp &last_displayed_line, &eof, view->nlines);
1149 26ed57b2 2018-05-19 stsp nodelay(stdscr, FALSE);
1150 ea5e7bb5 2018-08-01 stsp ch = wgetch(view->window);
1151 f7182337 2018-05-20 stsp nodelay(stdscr, TRUE);
1152 26ed57b2 2018-05-19 stsp switch (ch) {
1157 26ed57b2 2018-05-19 stsp case KEY_UP:
1158 26ed57b2 2018-05-19 stsp if (first_displayed_line > 1)
1159 b304db33 2018-05-20 stsp first_displayed_line--;
1161 b304db33 2018-05-20 stsp case KEY_PPAGE:
1162 c46b9352 2018-07-12 stsp case KEY_BACKSPACE:
1164 f7d12f7e 2018-08-01 stsp while (i++ < view->nlines - 1 &&
1165 d56caa55 2018-05-20 stsp first_displayed_line > 1)
1166 26ed57b2 2018-05-19 stsp first_displayed_line--;
1169 26ed57b2 2018-05-19 stsp case KEY_DOWN:
1171 26ed57b2 2018-05-19 stsp first_displayed_line++;
1173 b304db33 2018-05-20 stsp case KEY_NPAGE:
1176 f7d12f7e 2018-08-01 stsp while (!eof && i++ < view->nlines - 1) {
1177 b304db33 2018-05-20 stsp char *line = parse_next_line(f, NULL);
1178 b304db33 2018-05-20 stsp first_displayed_line++;
1179 b304db33 2018-05-20 stsp if (line == NULL)
1183 82357c0a 2018-08-01 stsp case KEY_RESIZE:
1184 a41d2007 2018-08-01 stsp err = view_resize(view);
1186 a41d2007 2018-08-01 stsp goto done;
1193 26ed57b2 2018-05-19 stsp fclose(f);
1194 26ed57b2 2018-05-19 stsp return err;
1197 4ed7e80c 2018-05-20 stsp static const struct got_error *
1198 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
1200 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
1201 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
1202 26ed57b2 2018-05-19 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
1203 26ed57b2 2018-05-19 stsp char *repo_path = NULL;
1204 26ed57b2 2018-05-19 stsp char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
1206 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
1208 26ed57b2 2018-05-19 stsp #ifndef PROFILE
1209 26ed57b2 2018-05-19 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1210 26ed57b2 2018-05-19 stsp err(1, "pledge");
1213 26ed57b2 2018-05-19 stsp while ((ch = getopt(argc, argv, "")) != -1) {
1214 26ed57b2 2018-05-19 stsp switch (ch) {
1217 26ed57b2 2018-05-19 stsp /* NOTREACHED */
1221 26ed57b2 2018-05-19 stsp argc -= optind;
1222 26ed57b2 2018-05-19 stsp argv += optind;
1224 26ed57b2 2018-05-19 stsp if (argc == 0) {
1225 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
1226 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
1227 26ed57b2 2018-05-19 stsp repo_path = getcwd(NULL, 0);
1228 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
1229 26ed57b2 2018-05-19 stsp return got_error_from_errno();
1230 26ed57b2 2018-05-19 stsp obj_id_str1 = argv[0];
1231 26ed57b2 2018-05-19 stsp obj_id_str2 = argv[1];
1232 26ed57b2 2018-05-19 stsp } else if (argc == 3) {
1233 26ed57b2 2018-05-19 stsp repo_path = realpath(argv[0], NULL);
1234 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
1235 26ed57b2 2018-05-19 stsp return got_error_from_errno();
1236 26ed57b2 2018-05-19 stsp obj_id_str1 = argv[1];
1237 26ed57b2 2018-05-19 stsp obj_id_str2 = argv[2];
1239 26ed57b2 2018-05-19 stsp usage_diff();
1241 26ed57b2 2018-05-19 stsp error = got_repo_open(&repo, repo_path);
1242 26ed57b2 2018-05-19 stsp free(repo_path);
1243 26ed57b2 2018-05-19 stsp if (error)
1244 26ed57b2 2018-05-19 stsp goto done;
1246 26ed57b2 2018-05-19 stsp error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
1247 26ed57b2 2018-05-19 stsp if (error)
1248 26ed57b2 2018-05-19 stsp goto done;
1250 26ed57b2 2018-05-19 stsp error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1251 26ed57b2 2018-05-19 stsp if (error)
1252 26ed57b2 2018-05-19 stsp goto done;
1254 6d0fee91 2018-08-01 stsp view = open_view(0, 0, 0, 0, NULL);
1255 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
1256 ea5e7bb5 2018-08-01 stsp error = got_error_from_errno();
1257 ea5e7bb5 2018-08-01 stsp goto done;
1259 ea5e7bb5 2018-08-01 stsp error = show_diff_view(view, obj1, obj2, repo);
1260 ea5e7bb5 2018-08-01 stsp close_view(view);
1262 26ed57b2 2018-05-19 stsp got_repo_close(repo);
1264 26ed57b2 2018-05-19 stsp got_object_close(obj1);
1266 26ed57b2 2018-05-19 stsp got_object_close(obj2);
1267 26ed57b2 2018-05-19 stsp return error;
1270 4ed7e80c 2018-05-20 stsp __dead static void
1271 9f7d7167 2018-04-29 stsp usage_blame(void)
1274 a70480e0 2018-06-23 stsp fprintf(stderr, "usage: %s blame [-c commit] [repository-path] path\n",
1275 9f7d7167 2018-04-29 stsp getprogname());
1279 84451b3e 2018-07-10 stsp struct tog_blame_line {
1280 84451b3e 2018-07-10 stsp int annotated;
1281 84451b3e 2018-07-10 stsp struct got_object_id *id;
1284 4ed7e80c 2018-05-20 stsp static const struct got_error *
1285 f7d12f7e 2018-08-01 stsp draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
1286 f7d12f7e 2018-08-01 stsp const char *path, struct tog_blame_line *lines, int nlines,
1287 f7d12f7e 2018-08-01 stsp int blame_complete, int selected_line, int *first_displayed_line,
1288 f7d12f7e 2018-08-01 stsp int *last_displayed_line, int *eof, int max_lines)
1290 84451b3e 2018-07-10 stsp const struct got_error *err;
1291 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
1292 84451b3e 2018-07-10 stsp char *line;
1293 84451b3e 2018-07-10 stsp size_t len;
1294 84451b3e 2018-07-10 stsp wchar_t *wline;
1295 b700b5d6 2018-07-10 stsp int width, wlimit;
1296 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
1297 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
1298 ab089a2a 2018-07-12 stsp char *id_str;
1300 ab089a2a 2018-07-12 stsp err = got_object_id_str(&id_str, id);
1302 ab089a2a 2018-07-12 stsp return err;
1304 84451b3e 2018-07-10 stsp rewind(f);
1305 f7d12f7e 2018-08-01 stsp werase(view->window);
1307 ab089a2a 2018-07-12 stsp if (asprintf(&line, "commit: %s", id_str) == -1) {
1308 ab089a2a 2018-07-12 stsp err = got_error_from_errno();
1309 ab089a2a 2018-07-12 stsp free(id_str);
1310 ab089a2a 2018-07-12 stsp return err;
1313 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
1314 ab089a2a 2018-07-12 stsp free(line);
1315 2550e4c3 2018-07-13 stsp line = NULL;
1316 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
1317 2550e4c3 2018-07-13 stsp free(wline);
1318 2550e4c3 2018-07-13 stsp wline = NULL;
1319 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
1320 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
1322 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
1323 084063cd 2018-07-12 stsp *first_displayed_line - 1 + selected_line, nlines,
1324 12a0b23b 2018-07-12 stsp blame_complete ? "" : "annotating ", path) == -1) {
1325 ab089a2a 2018-07-12 stsp free(id_str);
1326 3f60a8ef 2018-07-10 stsp return got_error_from_errno();
1328 ab089a2a 2018-07-12 stsp free(id_str);
1329 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
1330 3f60a8ef 2018-07-10 stsp free(line);
1331 2550e4c3 2018-07-13 stsp line = NULL;
1333 3f60a8ef 2018-07-10 stsp return err;
1334 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
1335 2550e4c3 2018-07-13 stsp free(wline);
1336 2550e4c3 2018-07-13 stsp wline = NULL;
1337 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
1338 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
1341 ab089a2a 2018-07-12 stsp while (nprinted < max_lines - 2) {
1342 84451b3e 2018-07-10 stsp line = parse_next_line(f, &len);
1343 84451b3e 2018-07-10 stsp if (line == NULL) {
1347 84451b3e 2018-07-10 stsp if (++lineno < *first_displayed_line) {
1348 84451b3e 2018-07-10 stsp free(line);
1352 f7d12f7e 2018-08-01 stsp wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
1353 b700b5d6 2018-07-10 stsp err = format_line(&wline, &width, line, wlimit);
1354 84451b3e 2018-07-10 stsp if (err) {
1355 84451b3e 2018-07-10 stsp free(line);
1356 84451b3e 2018-07-10 stsp return err;
1359 b700b5d6 2018-07-10 stsp if (nprinted == selected_line - 1)
1360 f7d12f7e 2018-08-01 stsp wstandout(view->window);
1362 84451b3e 2018-07-10 stsp blame_line = &lines[lineno - 1];
1363 ee41ec32 2018-07-10 stsp if (blame_line->annotated && prev_id &&
1364 ee41ec32 2018-07-10 stsp got_object_id_cmp(prev_id, blame_line->id) == 0)
1365 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ");
1366 ee41ec32 2018-07-10 stsp else if (blame_line->annotated) {
1367 84451b3e 2018-07-10 stsp char *id_str;
1368 84451b3e 2018-07-10 stsp err = got_object_id_str(&id_str, blame_line->id);
1369 84451b3e 2018-07-10 stsp if (err) {
1370 84451b3e 2018-07-10 stsp free(line);
1371 2550e4c3 2018-07-13 stsp free(wline);
1372 84451b3e 2018-07-10 stsp return err;
1374 f7d12f7e 2018-08-01 stsp wprintw(view->window, "%.8s ", id_str);
1375 84451b3e 2018-07-10 stsp free(id_str);
1376 ee41ec32 2018-07-10 stsp prev_id = blame_line->id;
1378 f7d12f7e 2018-08-01 stsp waddstr(view->window, "........ ");
1379 ee41ec32 2018-07-10 stsp prev_id = NULL;
1382 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
1383 b700b5d6 2018-07-10 stsp while (width < wlimit) {
1384 f7d12f7e 2018-08-01 stsp waddch(view->window, ' ');
1387 b700b5d6 2018-07-10 stsp if (nprinted == selected_line - 1)
1388 f7d12f7e 2018-08-01 stsp wstandend(view->window);
1389 84451b3e 2018-07-10 stsp if (++nprinted == 1)
1390 84451b3e 2018-07-10 stsp *first_displayed_line = lineno;
1391 84451b3e 2018-07-10 stsp free(line);
1392 2550e4c3 2018-07-13 stsp free(wline);
1393 2550e4c3 2018-07-13 stsp wline = NULL;
1395 84451b3e 2018-07-10 stsp *last_displayed_line = lineno;
1397 84451b3e 2018-07-10 stsp update_panels();
1398 84451b3e 2018-07-10 stsp doupdate();
1400 84451b3e 2018-07-10 stsp return NULL;
1403 84451b3e 2018-07-10 stsp struct tog_blame_cb_args {
1404 84451b3e 2018-07-10 stsp pthread_mutex_t *mutex;
1405 84451b3e 2018-07-10 stsp struct tog_blame_line *lines; /* one per line */
1406 84451b3e 2018-07-10 stsp int nlines;
1408 7cc84d77 2018-08-01 stsp struct tog_view *view;
1409 ab089a2a 2018-07-12 stsp struct got_object_id *commit_id;
1411 3f60a8ef 2018-07-10 stsp const char *path;
1412 84451b3e 2018-07-10 stsp int *first_displayed_line;
1413 84451b3e 2018-07-10 stsp int *last_displayed_line;
1414 b700b5d6 2018-07-10 stsp int *selected_line;
1415 18430de3 2018-07-10 stsp int *quit;
1418 84451b3e 2018-07-10 stsp static const struct got_error *
1419 84451b3e 2018-07-10 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
1421 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
1422 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
1423 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
1426 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
1427 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
1428 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
1430 84451b3e 2018-07-10 stsp if (pthread_mutex_lock(a->mutex) != 0)
1431 84451b3e 2018-07-10 stsp return got_error_from_errno();
1433 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
1434 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
1435 d68a0a7d 2018-07-10 stsp goto done;
1438 d68a0a7d 2018-07-10 stsp if (lineno == -1)
1439 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
1441 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
1442 d68a0a7d 2018-07-10 stsp if (line->annotated)
1443 d68a0a7d 2018-07-10 stsp goto done;
1445 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
1446 84451b3e 2018-07-10 stsp if (line->id == NULL) {
1447 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1448 84451b3e 2018-07-10 stsp goto done;
1450 84451b3e 2018-07-10 stsp line->annotated = 1;
1452 f7d12f7e 2018-08-01 stsp err = draw_blame(a->view, a->commit_id, a->f, a->path,
1453 245d91c1 2018-07-12 stsp a->lines, a->nlines, 0, *a->selected_line, a->first_displayed_line,
1454 f7d12f7e 2018-08-01 stsp a->last_displayed_line, &eof, a->view->nlines);
1456 84451b3e 2018-07-10 stsp if (pthread_mutex_unlock(a->mutex) != 0)
1457 84451b3e 2018-07-10 stsp return got_error_from_errno();
1458 84451b3e 2018-07-10 stsp return err;
1461 84451b3e 2018-07-10 stsp struct tog_blame_thread_args {
1462 84451b3e 2018-07-10 stsp const char *path;
1463 84451b3e 2018-07-10 stsp struct got_repository *repo;
1464 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *cb_args;
1465 18430de3 2018-07-10 stsp int *complete;
1468 84451b3e 2018-07-10 stsp static void *
1469 84451b3e 2018-07-10 stsp blame_thread(void *arg)
1471 18430de3 2018-07-10 stsp const struct got_error *err;
1472 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
1473 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
1476 ab089a2a 2018-07-12 stsp err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
1477 245d91c1 2018-07-12 stsp blame_cb, ta->cb_args);
1479 18430de3 2018-07-10 stsp if (pthread_mutex_lock(a->mutex) != 0)
1480 18430de3 2018-07-10 stsp return (void *)got_error_from_errno();
1482 c9beca56 2018-07-22 stsp got_repo_close(ta->repo);
1483 c9beca56 2018-07-22 stsp ta->repo = NULL;
1484 c9beca56 2018-07-22 stsp *ta->complete = 1;
1486 f7d12f7e 2018-08-01 stsp err = draw_blame(a->view, a->commit_id, a->f, a->path,
1487 f7d12f7e 2018-08-01 stsp a->lines, a->nlines, 1, *a->selected_line,
1488 c9beca56 2018-07-22 stsp a->first_displayed_line, a->last_displayed_line, &eof,
1489 f7d12f7e 2018-08-01 stsp a->view->nlines);
1491 18430de3 2018-07-10 stsp if (pthread_mutex_unlock(a->mutex) != 0 && err == NULL)
1492 18430de3 2018-07-10 stsp err = got_error_from_errno();
1494 18430de3 2018-07-10 stsp return (void *)err;
1497 245d91c1 2018-07-12 stsp static struct got_object_id *
1498 245d91c1 2018-07-12 stsp get_selected_commit_id(struct tog_blame_line *lines,
1499 245d91c1 2018-07-12 stsp int first_displayed_line, int selected_line)
1501 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
1503 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
1504 245d91c1 2018-07-12 stsp if (!line->annotated)
1505 245d91c1 2018-07-12 stsp return NULL;
1507 245d91c1 2018-07-12 stsp return line->id;
1510 84451b3e 2018-07-10 stsp static const struct got_error *
1511 245d91c1 2018-07-12 stsp open_selected_commit(struct got_object **pobj, struct got_object **obj,
1512 b880a918 2018-07-10 stsp struct tog_blame_line *lines, int first_displayed_line,
1513 b880a918 2018-07-10 stsp int selected_line, struct got_repository *repo)
1515 b880a918 2018-07-10 stsp const struct got_error *err = NULL;
1516 b880a918 2018-07-10 stsp struct got_commit_object *commit = NULL;
1517 245d91c1 2018-07-12 stsp struct got_object_id *selected_id;
1518 b880a918 2018-07-10 stsp struct got_object_qid *pid;
1520 b880a918 2018-07-10 stsp *pobj = NULL;
1521 b880a918 2018-07-10 stsp *obj = NULL;
1523 245d91c1 2018-07-12 stsp selected_id = get_selected_commit_id(lines,
1524 245d91c1 2018-07-12 stsp first_displayed_line, selected_line);
1525 245d91c1 2018-07-12 stsp if (selected_id == NULL)
1526 b880a918 2018-07-10 stsp return NULL;
1528 245d91c1 2018-07-12 stsp err = got_object_open(obj, repo, selected_id);
1530 b880a918 2018-07-10 stsp goto done;
1532 b880a918 2018-07-10 stsp err = got_object_commit_open(&commit, repo, *obj);
1534 b880a918 2018-07-10 stsp goto done;
1536 b880a918 2018-07-10 stsp pid = SIMPLEQ_FIRST(&commit->parent_ids);
1537 b880a918 2018-07-10 stsp if (pid) {
1538 b880a918 2018-07-10 stsp err = got_object_open(pobj, repo, pid->id);
1540 b880a918 2018-07-10 stsp goto done;
1543 b880a918 2018-07-10 stsp if (commit)
1544 b880a918 2018-07-10 stsp got_object_commit_close(commit);
1545 b880a918 2018-07-10 stsp return err;
1548 245d91c1 2018-07-12 stsp struct tog_blame {
1550 245d91c1 2018-07-12 stsp size_t filesize;
1551 245d91c1 2018-07-12 stsp struct tog_blame_line *lines;
1552 245d91c1 2018-07-12 stsp size_t nlines;
1553 245d91c1 2018-07-12 stsp pthread_t thread;
1554 245d91c1 2018-07-12 stsp struct tog_blame_thread_args thread_args;
1555 245d91c1 2018-07-12 stsp struct tog_blame_cb_args cb_args;
1556 245d91c1 2018-07-12 stsp const char *path;
1559 b880a918 2018-07-10 stsp static const struct got_error *
1560 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
1562 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
1565 245d91c1 2018-07-12 stsp if (blame->thread) {
1566 245d91c1 2018-07-12 stsp if (pthread_join(blame->thread, (void **)&err) != 0)
1567 245d91c1 2018-07-12 stsp err = got_error_from_errno();
1568 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
1569 245d91c1 2018-07-12 stsp err = NULL;
1570 245d91c1 2018-07-12 stsp blame->thread = NULL;
1572 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
1573 245d91c1 2018-07-12 stsp got_repo_close(blame->thread_args.repo);
1574 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
1576 245d91c1 2018-07-12 stsp if (blame->f) {
1577 245d91c1 2018-07-12 stsp fclose(blame->f);
1578 245d91c1 2018-07-12 stsp blame->f = NULL;
1580 245d91c1 2018-07-12 stsp for (i = 0; i < blame->nlines; i++)
1581 245d91c1 2018-07-12 stsp free(blame->lines[i].id);
1582 245d91c1 2018-07-12 stsp free(blame->lines);
1583 245d91c1 2018-07-12 stsp blame->lines = NULL;
1584 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
1585 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
1587 245d91c1 2018-07-12 stsp return err;
1590 245d91c1 2018-07-12 stsp static const struct got_error *
1591 7cc84d77 2018-08-01 stsp run_blame(struct tog_blame *blame, pthread_mutex_t *mutex,
1592 7cc84d77 2018-08-01 stsp struct tog_view *view, int *blame_complete,
1593 245d91c1 2018-07-12 stsp int *first_displayed_line, int *last_displayed_line,
1594 245d91c1 2018-07-12 stsp int *selected_line, int *done, const char *path,
1595 245d91c1 2018-07-12 stsp struct got_object_id *commit_id,
1596 245d91c1 2018-07-12 stsp struct got_repository *repo)
1598 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
1599 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
1600 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
1601 245d91c1 2018-07-12 stsp struct got_object *obj;
1603 84451b3e 2018-07-10 stsp err = got_object_open_by_path(&obj, repo, commit_id, path);
1605 84451b3e 2018-07-10 stsp goto done;
1606 84451b3e 2018-07-10 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
1607 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1608 84451b3e 2018-07-10 stsp goto done;
1611 84451b3e 2018-07-10 stsp err = got_object_blob_open(&blob, repo, obj, 8192);
1613 a70480e0 2018-06-23 stsp goto done;
1614 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
1615 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
1616 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1617 84451b3e 2018-07-10 stsp goto done;
1619 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
1620 245d91c1 2018-07-12 stsp blame->f, blob);
1622 84451b3e 2018-07-10 stsp goto done;
1624 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
1625 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
1626 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1627 84451b3e 2018-07-10 stsp goto done;
1630 245d91c1 2018-07-12 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1632 bd24772e 2018-07-11 stsp goto done;
1634 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
1635 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
1636 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
1637 245d91c1 2018-07-12 stsp blame->cb_args.mutex = mutex;
1638 245d91c1 2018-07-12 stsp blame->cb_args.commit_id = got_object_id_dup(commit_id);
1639 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
1640 245d91c1 2018-07-12 stsp err = got_error_from_errno();
1641 245d91c1 2018-07-12 stsp goto done;
1643 245d91c1 2018-07-12 stsp blame->cb_args.f = blame->f;
1644 245d91c1 2018-07-12 stsp blame->cb_args.path = path;
1645 245d91c1 2018-07-12 stsp blame->cb_args.first_displayed_line = first_displayed_line;
1646 245d91c1 2018-07-12 stsp blame->cb_args.selected_line = selected_line;
1647 245d91c1 2018-07-12 stsp blame->cb_args.last_displayed_line = last_displayed_line;
1648 245d91c1 2018-07-12 stsp blame->cb_args.quit = done;
1650 245d91c1 2018-07-12 stsp blame->thread_args.path = path;
1651 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
1652 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
1653 245d91c1 2018-07-12 stsp blame->thread_args.complete = blame_complete;
1654 245d91c1 2018-07-12 stsp *blame_complete = 0;
1656 245d91c1 2018-07-12 stsp if (pthread_create(&blame->thread, NULL, blame_thread,
1657 245d91c1 2018-07-12 stsp &blame->thread_args) != 0) {
1658 245d91c1 2018-07-12 stsp err = got_error_from_errno();
1659 245d91c1 2018-07-12 stsp goto done;
1664 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
1666 245d91c1 2018-07-12 stsp got_object_close(obj);
1668 245d91c1 2018-07-12 stsp stop_blame(blame);
1669 245d91c1 2018-07-12 stsp return err;
1672 245d91c1 2018-07-12 stsp static const struct got_error *
1673 e1cd8fed 2018-08-01 stsp show_blame_view(struct tog_view *view, const char *path,
1674 e1cd8fed 2018-08-01 stsp struct got_object_id *commit_id, struct got_repository *repo)
1676 245d91c1 2018-07-12 stsp const struct got_error *err = NULL, *thread_err = NULL;
1677 f7d12f7e 2018-08-01 stsp int ch, done = 0, first_displayed_line = 1, last_displayed_line;
1678 245d91c1 2018-07-12 stsp int selected_line = first_displayed_line;
1679 245d91c1 2018-07-12 stsp int eof, blame_complete = 0;
1680 245d91c1 2018-07-12 stsp struct got_object *obj = NULL, *pobj = NULL;
1681 245d91c1 2018-07-12 stsp pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
1682 245d91c1 2018-07-12 stsp struct tog_blame blame;
1683 dbc6a6b6 2018-07-12 stsp struct got_object_id_queue blamed_commits;
1684 dbc6a6b6 2018-07-12 stsp struct got_object_qid *blamed_commit = NULL;
1685 e1cd8fed 2018-08-01 stsp struct tog_view *diff_view;
1687 dbc6a6b6 2018-07-12 stsp SIMPLEQ_INIT(&blamed_commits);
1689 245d91c1 2018-07-12 stsp if (pthread_mutex_init(&mutex, NULL) != 0) {
1690 245d91c1 2018-07-12 stsp err = got_error_from_errno();
1691 245d91c1 2018-07-12 stsp goto done;
1694 dbc6a6b6 2018-07-12 stsp err = got_object_qid_alloc(&blamed_commit, commit_id);
1696 245d91c1 2018-07-12 stsp goto done;
1697 dbc6a6b6 2018-07-12 stsp SIMPLEQ_INSERT_HEAD(&blamed_commits, blamed_commit, entry);
1699 7cc84d77 2018-08-01 stsp show_panel(view->panel);
1700 f7d12f7e 2018-08-01 stsp last_displayed_line = view->nlines;
1702 245d91c1 2018-07-12 stsp memset(&blame, 0, sizeof(blame));
1703 7cc84d77 2018-08-01 stsp err = run_blame(&blame, &mutex, view, &blame_complete,
1704 245d91c1 2018-07-12 stsp &first_displayed_line, &last_displayed_line,
1705 dbc6a6b6 2018-07-12 stsp &selected_line, &done, path, blamed_commit->id, repo);
1707 245d91c1 2018-07-12 stsp return err;
1709 a70480e0 2018-06-23 stsp while (!done) {
1710 84451b3e 2018-07-10 stsp if (pthread_mutex_lock(&mutex) != 0) {
1711 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1712 84451b3e 2018-07-10 stsp goto done;
1714 f7d12f7e 2018-08-01 stsp err = draw_blame(view, blamed_commit->id, blame.f, path,
1715 f7d12f7e 2018-08-01 stsp blame.lines, blame.nlines, blame_complete, selected_line,
1716 f7d12f7e 2018-08-01 stsp &first_displayed_line, &last_displayed_line, &eof,
1717 f7d12f7e 2018-08-01 stsp view->nlines);
1718 84451b3e 2018-07-10 stsp if (pthread_mutex_unlock(&mutex) != 0) {
1719 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1720 84451b3e 2018-07-10 stsp goto done;
1724 a70480e0 2018-06-23 stsp nodelay(stdscr, FALSE);
1725 7cc84d77 2018-08-01 stsp ch = wgetch(view->window);
1726 a70480e0 2018-06-23 stsp nodelay(stdscr, TRUE);
1727 84451b3e 2018-07-10 stsp if (pthread_mutex_lock(&mutex) != 0) {
1728 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1729 84451b3e 2018-07-10 stsp goto done;
1731 a70480e0 2018-06-23 stsp switch (ch) {
1736 a70480e0 2018-06-23 stsp case KEY_UP:
1737 b700b5d6 2018-07-10 stsp if (selected_line > 1)
1738 b700b5d6 2018-07-10 stsp selected_line--;
1739 b700b5d6 2018-07-10 stsp else if (selected_line == 1 &&
1740 b700b5d6 2018-07-10 stsp first_displayed_line > 1)
1741 a70480e0 2018-06-23 stsp first_displayed_line--;
1743 a70480e0 2018-06-23 stsp case KEY_PPAGE:
1744 38f94530 2018-07-12 stsp case KEY_BACKSPACE:
1745 b700b5d6 2018-07-10 stsp if (first_displayed_line == 1) {
1746 b700b5d6 2018-07-10 stsp selected_line = 1;
1749 f7d12f7e 2018-08-01 stsp if (first_displayed_line > view->nlines - 2)
1750 f7d12f7e 2018-08-01 stsp first_displayed_line -=
1751 f7d12f7e 2018-08-01 stsp (view->nlines - 2);
1753 b700b5d6 2018-07-10 stsp first_displayed_line = 1;
1756 a70480e0 2018-06-23 stsp case KEY_DOWN:
1757 f7d12f7e 2018-08-01 stsp if (selected_line < view->nlines - 2 &&
1758 a026b947 2018-07-12 stsp first_displayed_line + selected_line <=
1759 a026b947 2018-07-12 stsp blame.nlines)
1760 b700b5d6 2018-07-10 stsp selected_line++;
1761 245d91c1 2018-07-12 stsp else if (last_displayed_line < blame.nlines)
1762 b700b5d6 2018-07-10 stsp first_displayed_line++;
1765 7a2921f9 2018-07-12 stsp case 'p': {
1766 245d91c1 2018-07-12 stsp struct got_object_id *id;
1767 245d91c1 2018-07-12 stsp id = get_selected_commit_id(blame.lines,
1768 245d91c1 2018-07-12 stsp first_displayed_line, selected_line);
1769 245d91c1 2018-07-12 stsp if (id == NULL || got_object_id_cmp(id,
1770 dbc6a6b6 2018-07-12 stsp blamed_commit->id) == 0)
1772 245d91c1 2018-07-12 stsp err = open_selected_commit(&pobj, &obj,
1773 245d91c1 2018-07-12 stsp blame.lines, first_displayed_line,
1774 245d91c1 2018-07-12 stsp selected_line, repo);
1777 245d91c1 2018-07-12 stsp if (pobj == NULL && obj == NULL)
1779 7a2921f9 2018-07-12 stsp if (ch == 'p' && pobj == NULL)
1782 245d91c1 2018-07-12 stsp if (pthread_mutex_unlock(&mutex) != 0) {
1783 245d91c1 2018-07-12 stsp err = got_error_from_errno();
1784 ad6a6c43 2018-07-10 stsp goto done;
1786 245d91c1 2018-07-12 stsp thread_err = stop_blame(&blame);
1788 245d91c1 2018-07-12 stsp if (pthread_mutex_lock(&mutex) != 0) {
1789 245d91c1 2018-07-12 stsp err = got_error_from_errno();
1790 245d91c1 2018-07-12 stsp goto done;
1792 245d91c1 2018-07-12 stsp if (thread_err)
1794 7a2921f9 2018-07-12 stsp id = got_object_get_id(ch == 'b' ? obj : pobj);
1795 1960a6f9 2018-07-12 stsp got_object_close(obj);
1796 1960a6f9 2018-07-12 stsp obj = NULL;
1797 1960a6f9 2018-07-12 stsp if (pobj) {
1798 1960a6f9 2018-07-12 stsp got_object_close(pobj);
1799 14437fb1 2018-07-13 stsp pobj = NULL;
1801 dbc6a6b6 2018-07-12 stsp if (id == NULL) {
1802 245d91c1 2018-07-12 stsp err = got_error_from_errno();
1805 dbc6a6b6 2018-07-12 stsp err = got_object_qid_alloc(&blamed_commit, id);
1808 dbc6a6b6 2018-07-12 stsp goto done;
1809 dbc6a6b6 2018-07-12 stsp SIMPLEQ_INSERT_HEAD(&blamed_commits,
1810 dbc6a6b6 2018-07-12 stsp blamed_commit, entry);
1811 7cc84d77 2018-08-01 stsp err = run_blame(&blame, &mutex, view,
1812 245d91c1 2018-07-12 stsp &blame_complete, &first_displayed_line,
1813 245d91c1 2018-07-12 stsp &last_displayed_line, &selected_line,
1814 dbc6a6b6 2018-07-12 stsp &done, path, blamed_commit->id, repo);
1819 dbc6a6b6 2018-07-12 stsp case 'B': {
1820 dbc6a6b6 2018-07-12 stsp struct got_object_qid *first;
1821 dbc6a6b6 2018-07-12 stsp first = SIMPLEQ_FIRST(&blamed_commits);
1822 dbc6a6b6 2018-07-12 stsp if (!got_object_id_cmp(first->id, commit_id))
1825 dbc6a6b6 2018-07-12 stsp if (pthread_mutex_unlock(&mutex) != 0) {
1826 dbc6a6b6 2018-07-12 stsp err = got_error_from_errno();
1827 dbc6a6b6 2018-07-12 stsp goto done;
1829 dbc6a6b6 2018-07-12 stsp thread_err = stop_blame(&blame);
1831 dbc6a6b6 2018-07-12 stsp if (pthread_mutex_lock(&mutex) != 0) {
1832 dbc6a6b6 2018-07-12 stsp err = got_error_from_errno();
1833 dbc6a6b6 2018-07-12 stsp goto done;
1835 dbc6a6b6 2018-07-12 stsp if (thread_err)
1837 dbc6a6b6 2018-07-12 stsp SIMPLEQ_REMOVE_HEAD(&blamed_commits, entry);
1838 dbc6a6b6 2018-07-12 stsp got_object_qid_free(blamed_commit);
1839 dbc6a6b6 2018-07-12 stsp blamed_commit = SIMPLEQ_FIRST(&blamed_commits);
1840 7cc84d77 2018-08-01 stsp err = run_blame(&blame, &mutex, view,
1841 dbc6a6b6 2018-07-12 stsp &blame_complete, &first_displayed_line,
1842 dbc6a6b6 2018-07-12 stsp &last_displayed_line, &selected_line,
1843 dbc6a6b6 2018-07-12 stsp &done, path, blamed_commit->id, repo);
1848 245d91c1 2018-07-12 stsp case KEY_ENTER:
1849 245d91c1 2018-07-12 stsp case '\r':
1850 245d91c1 2018-07-12 stsp err = open_selected_commit(&pobj, &obj,
1851 245d91c1 2018-07-12 stsp blame.lines, first_displayed_line,
1852 245d91c1 2018-07-12 stsp selected_line, repo);
1855 b880a918 2018-07-10 stsp if (pobj == NULL && obj == NULL)
1857 6d0fee91 2018-08-01 stsp diff_view = open_view(0, 0, 0, 0, view);
1858 ea5e7bb5 2018-08-01 stsp if (diff_view == NULL) {
1859 ea5e7bb5 2018-08-01 stsp err = got_error_from_errno();
1862 ea5e7bb5 2018-08-01 stsp err = show_diff_view(diff_view, pobj, obj, repo);
1863 ea5e7bb5 2018-08-01 stsp close_view(diff_view);
1864 b880a918 2018-07-10 stsp if (pobj) {
1865 b880a918 2018-07-10 stsp got_object_close(pobj);
1866 b880a918 2018-07-10 stsp pobj = NULL;
1868 b880a918 2018-07-10 stsp got_object_close(obj);
1869 b880a918 2018-07-10 stsp obj = NULL;
1870 7cc84d77 2018-08-01 stsp show_panel(view->panel);
1874 a70480e0 2018-06-23 stsp case KEY_NPAGE:
1876 245d91c1 2018-07-12 stsp if (last_displayed_line >= blame.nlines &&
1877 f7d12f7e 2018-08-01 stsp selected_line < view->nlines - 2) {
1878 d2dfcfbf 2018-07-12 stsp selected_line = MIN(blame.nlines,
1879 f7d12f7e 2018-08-01 stsp view->nlines - 2);
1882 f7d12f7e 2018-08-01 stsp if (last_displayed_line + view->nlines - 2 <=
1883 245d91c1 2018-07-12 stsp blame.nlines)
1884 f7d12f7e 2018-08-01 stsp first_displayed_line +=
1885 f7d12f7e 2018-08-01 stsp view->nlines - 2;
1887 b700b5d6 2018-07-10 stsp first_displayed_line =
1888 f7d12f7e 2018-08-01 stsp blame.nlines - (view->nlines - 3);
1890 7e5c1fe1 2018-08-01 stsp case KEY_RESIZE:
1891 a41d2007 2018-08-01 stsp err = view_resize(view);
1894 7e5c1fe1 2018-08-01 stsp if (selected_line > view->nlines - 2) {
1895 7e5c1fe1 2018-08-01 stsp selected_line = MIN(blame.nlines,
1896 7e5c1fe1 2018-08-01 stsp view->nlines - 2);
1902 245d91c1 2018-07-12 stsp if (pthread_mutex_unlock(&mutex) != 0)
1903 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1904 245d91c1 2018-07-12 stsp if (err || thread_err)
1909 b880a918 2018-07-10 stsp got_object_close(pobj);
1910 c9beca56 2018-07-22 stsp if (blame.thread)
1911 245d91c1 2018-07-12 stsp thread_err = stop_blame(&blame);
1912 dbc6a6b6 2018-07-12 stsp while (!SIMPLEQ_EMPTY(&blamed_commits)) {
1913 dbc6a6b6 2018-07-12 stsp blamed_commit = SIMPLEQ_FIRST(&blamed_commits);
1914 dbc6a6b6 2018-07-12 stsp SIMPLEQ_REMOVE_HEAD(&blamed_commits, entry);
1915 dbc6a6b6 2018-07-12 stsp got_object_qid_free(blamed_commit);
1917 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
1920 a70480e0 2018-06-23 stsp static const struct got_error *
1921 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
1923 a70480e0 2018-06-23 stsp const struct got_error *error;
1924 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
1925 a70480e0 2018-06-23 stsp char *repo_path = NULL;
1926 a70480e0 2018-06-23 stsp char *path = NULL;
1927 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
1928 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
1930 e1cd8fed 2018-08-01 stsp struct tog_view *view;
1932 a70480e0 2018-06-23 stsp #ifndef PROFILE
1933 a70480e0 2018-06-23 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1934 a70480e0 2018-06-23 stsp err(1, "pledge");
1937 a70480e0 2018-06-23 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
1938 a70480e0 2018-06-23 stsp switch (ch) {
1940 a70480e0 2018-06-23 stsp commit_id_str = optarg;
1944 a70480e0 2018-06-23 stsp /* NOTREACHED */
1948 a70480e0 2018-06-23 stsp argc -= optind;
1949 a70480e0 2018-06-23 stsp argv += optind;
1951 a70480e0 2018-06-23 stsp if (argc == 0) {
1952 a70480e0 2018-06-23 stsp usage_blame();
1953 a70480e0 2018-06-23 stsp } else if (argc == 1) {
1954 a70480e0 2018-06-23 stsp repo_path = getcwd(NULL, 0);
1955 a70480e0 2018-06-23 stsp if (repo_path == NULL)
1956 a70480e0 2018-06-23 stsp return got_error_from_errno();
1957 a70480e0 2018-06-23 stsp path = argv[0];
1958 a70480e0 2018-06-23 stsp } else if (argc == 2) {
1959 a70480e0 2018-06-23 stsp repo_path = realpath(argv[0], NULL);
1960 a70480e0 2018-06-23 stsp if (repo_path == NULL)
1961 a70480e0 2018-06-23 stsp return got_error_from_errno();
1962 a70480e0 2018-06-23 stsp path = argv[1];
1964 a70480e0 2018-06-23 stsp usage_blame();
1966 a70480e0 2018-06-23 stsp error = got_repo_open(&repo, repo_path);
1967 a70480e0 2018-06-23 stsp free(repo_path);
1968 a70480e0 2018-06-23 stsp if (error != NULL)
1969 66b4983c 2018-06-23 stsp return error;
1971 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
1972 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
1973 a70480e0 2018-06-23 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1974 a70480e0 2018-06-23 stsp if (error != NULL)
1975 66b4983c 2018-06-23 stsp goto done;
1976 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
1977 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
1979 a70480e0 2018-06-23 stsp struct got_object *obj;
1980 a70480e0 2018-06-23 stsp error = got_object_open_by_id_str(&obj, repo, commit_id_str);
1981 a70480e0 2018-06-23 stsp if (error != NULL)
1982 66b4983c 2018-06-23 stsp goto done;
1983 a70480e0 2018-06-23 stsp commit_id = got_object_get_id(obj);
1984 a19e88aa 2018-06-23 stsp if (commit_id == NULL)
1985 66b4983c 2018-06-23 stsp error = got_error_from_errno();
1986 a19e88aa 2018-06-23 stsp got_object_close(obj);
1988 a19e88aa 2018-06-23 stsp if (error != NULL)
1989 a19e88aa 2018-06-23 stsp goto done;
1991 6d0fee91 2018-08-01 stsp view = open_view(0, 0, 0, 0, NULL);
1992 e1cd8fed 2018-08-01 stsp if (view == NULL) {
1993 e1cd8fed 2018-08-01 stsp error = got_error_from_errno();
1994 e1cd8fed 2018-08-01 stsp goto done;
1996 e1cd8fed 2018-08-01 stsp error = show_blame_view(view, path, commit_id, repo);
1997 e1cd8fed 2018-08-01 stsp close_view(view);
1999 a70480e0 2018-06-23 stsp free(commit_id);
2001 a70480e0 2018-06-23 stsp got_repo_close(repo);
2002 a70480e0 2018-06-23 stsp return error;
2005 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2006 f7d12f7e 2018-08-01 stsp draw_tree_entries(struct tog_view *view,
2007 f7d12f7e 2018-08-01 stsp struct got_tree_entry **first_displayed_entry,
2008 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **last_displayed_entry,
2009 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **selected_entry, int *ndisplayed,
2010 f7d12f7e 2018-08-01 stsp const char *label, int show_ids, const char *parent_path,
2011 ce52c690 2018-06-23 stsp const struct got_tree_entries *entries, int selected, int limit, int isroot)
2013 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
2014 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
2015 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
2016 ffd1d5e5 2018-06-23 stsp int width, n;
2018 ffd1d5e5 2018-06-23 stsp *ndisplayed = 0;
2020 f7d12f7e 2018-08-01 stsp werase(view->window);
2022 ffd1d5e5 2018-06-23 stsp if (limit == 0)
2023 ffd1d5e5 2018-06-23 stsp return NULL;
2025 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, label, view->ncols);
2027 ffd1d5e5 2018-06-23 stsp return err;
2028 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2029 2550e4c3 2018-07-13 stsp free(wline);
2030 2550e4c3 2018-07-13 stsp wline = NULL;
2031 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2032 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2033 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2034 ffd1d5e5 2018-06-23 stsp return NULL;
2035 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, parent_path, view->ncols);
2037 ce52c690 2018-06-23 stsp return err;
2038 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2039 2550e4c3 2018-07-13 stsp free(wline);
2040 2550e4c3 2018-07-13 stsp wline = NULL;
2041 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2042 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2043 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2044 ffd1d5e5 2018-06-23 stsp return NULL;
2045 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2046 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
2047 a1eca9bb 2018-06-23 stsp return NULL;
2049 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
2050 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL) {
2051 ffd1d5e5 2018-06-23 stsp if (selected == 0) {
2052 f7d12f7e 2018-08-01 stsp wstandout(view->window);
2053 ffd1d5e5 2018-06-23 stsp *selected_entry = NULL;
2055 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
2056 ffd1d5e5 2018-06-23 stsp if (selected == 0)
2057 f7d12f7e 2018-08-01 stsp wstandend(view->window);
2058 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
2059 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2060 ffd1d5e5 2018-06-23 stsp return NULL;
2064 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry)
2065 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
2068 ffd1d5e5 2018-06-23 stsp while (te) {
2069 1d13200f 2018-07-12 stsp char *line = NULL, *id_str = NULL;
2071 1d13200f 2018-07-12 stsp if (show_ids) {
2072 1d13200f 2018-07-12 stsp err = got_object_id_str(&id_str, te->id);
2074 1d13200f 2018-07-12 stsp return got_error_from_errno();
2076 1d13200f 2018-07-12 stsp if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
2077 1d13200f 2018-07-12 stsp te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
2078 1d13200f 2018-07-12 stsp free(id_str);
2079 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
2081 1d13200f 2018-07-12 stsp free(id_str);
2082 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
2083 ffd1d5e5 2018-06-23 stsp if (err) {
2084 ffd1d5e5 2018-06-23 stsp free(line);
2087 ffd1d5e5 2018-06-23 stsp if (n == selected) {
2088 f7d12f7e 2018-08-01 stsp wstandout(view->window);
2089 ffd1d5e5 2018-06-23 stsp *selected_entry = te;
2091 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2092 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2093 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2094 ffd1d5e5 2018-06-23 stsp if (n == selected)
2095 f7d12f7e 2018-08-01 stsp wstandend(view->window);
2096 ffd1d5e5 2018-06-23 stsp free(line);
2097 2550e4c3 2018-07-13 stsp free(wline);
2098 2550e4c3 2018-07-13 stsp wline = NULL;
2100 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
2101 ffd1d5e5 2018-06-23 stsp *last_displayed_entry = te;
2102 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2104 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
2107 ffd1d5e5 2018-06-23 stsp return err;
2110 ffd1d5e5 2018-06-23 stsp static void
2111 ffd1d5e5 2018-06-23 stsp tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
2112 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries, int isroot)
2114 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te, *prev;
2117 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL)
2120 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
2121 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == te) {
2122 ffd1d5e5 2018-06-23 stsp if (!isroot)
2123 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
2127 ffd1d5e5 2018-06-23 stsp /* XXX this is stupid... switch to TAILQ? */
2128 ffd1d5e5 2018-06-23 stsp for (i = 0; i < maxscroll; i++) {
2129 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry) {
2130 ffd1d5e5 2018-06-23 stsp prev = te;
2131 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
2133 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = prev;
2134 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
2136 ffd1d5e5 2018-06-23 stsp if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
2137 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
2140 ffd1d5e5 2018-06-23 stsp static void
2141 ffd1d5e5 2018-06-23 stsp tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
2142 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *last_displayed_entry,
2143 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries)
2145 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *next;
2146 ffd1d5e5 2018-06-23 stsp int n = 0;
2148 ffd1d5e5 2018-06-23 stsp if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
2151 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry)
2152 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
2154 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_FIRST(&entries->head);
2155 ffd1d5e5 2018-06-23 stsp while (next) {
2156 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = next;
2157 ffd1d5e5 2018-06-23 stsp if (++n >= maxscroll)
2159 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(next, entry);
2163 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree {
2164 d9765a41 2018-06-23 stsp TAILQ_ENTRY(tog_parent_tree) entry;
2165 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree;
2166 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *first_displayed_entry;
2167 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *selected_entry;
2168 ffd1d5e5 2018-06-23 stsp int selected;
2171 d9765a41 2018-06-23 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
2173 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2174 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
2175 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
2177 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
2178 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
2179 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
2181 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
2182 ffd1d5e5 2018-06-23 stsp len += strlen(pt->selected_entry->name) + 1 /* slash */;
2184 ce52c690 2018-06-23 stsp len += strlen(te->name);
2186 ce52c690 2018-06-23 stsp *path = calloc(1, len);
2187 ffd1d5e5 2018-06-23 stsp if (path == NULL)
2188 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
2190 ce52c690 2018-06-23 stsp (*path)[0] = '/';
2191 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
2192 d9765a41 2018-06-23 stsp while (pt) {
2193 ce52c690 2018-06-23 stsp if (strlcat(*path, pt->selected_entry->name, len) >= len) {
2194 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
2195 cb2ebc8a 2018-06-23 stsp goto done;
2197 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
2198 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
2199 cb2ebc8a 2018-06-23 stsp goto done;
2201 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
2204 ce52c690 2018-06-23 stsp if (strlcat(*path, te->name, len) >= len) {
2205 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
2206 ce52c690 2018-06-23 stsp goto done;
2210 ce52c690 2018-06-23 stsp if (err) {
2211 ce52c690 2018-06-23 stsp free(*path);
2212 ce52c690 2018-06-23 stsp *path = NULL;
2214 ce52c690 2018-06-23 stsp return err;
2217 ce52c690 2018-06-23 stsp static const struct got_error *
2218 e1cd8fed 2018-08-01 stsp blame_tree_entry(struct tog_view *view, struct got_tree_entry *te,
2219 e1cd8fed 2018-08-01 stsp struct tog_parent_trees *parents, struct got_object_id *commit_id,
2220 e1cd8fed 2018-08-01 stsp struct got_repository *repo)
2222 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
2223 ce52c690 2018-06-23 stsp char *path;
2225 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
2227 ce52c690 2018-06-23 stsp return err;
2229 e1cd8fed 2018-08-01 stsp err = show_blame_view(view, path, commit_id, repo);
2230 69efd4c4 2018-07-18 stsp free(path);
2231 69efd4c4 2018-07-18 stsp return err;
2234 69efd4c4 2018-07-18 stsp static const struct got_error *
2235 04cc582a 2018-08-01 stsp log_tree_entry(struct tog_view *view, struct got_tree_entry *te,
2236 04cc582a 2018-08-01 stsp struct tog_parent_trees *parents, struct got_object_id *commit_id,
2237 04cc582a 2018-08-01 stsp struct got_repository *repo)
2239 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
2240 69efd4c4 2018-07-18 stsp char *path;
2242 69efd4c4 2018-07-18 stsp err = tree_entry_path(&path, parents, te);
2244 69efd4c4 2018-07-18 stsp return err;
2246 04cc582a 2018-08-01 stsp err = show_log_view(view, commit_id, repo, path);
2247 cb2ebc8a 2018-06-23 stsp free(path);
2248 cb2ebc8a 2018-06-23 stsp return err;
2251 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2252 5221c383 2018-08-01 stsp show_tree_view(struct tog_view *view, struct got_tree_object *root,
2253 5221c383 2018-08-01 stsp struct got_object_id *commit_id, struct got_repository *repo)
2255 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
2256 1d13200f 2018-07-12 stsp int ch, done = 0, selected = 0, show_ids = 0;
2257 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree = root;
2258 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries;
2259 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *first_displayed_entry = NULL;
2260 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *last_displayed_entry = NULL;
2261 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *selected_entry = NULL;
2262 ffd1d5e5 2018-06-23 stsp char *commit_id_str = NULL, *tree_label = NULL;
2263 ffd1d5e5 2018-06-23 stsp int nentries, ndisplayed;
2264 ffd1d5e5 2018-06-23 stsp struct tog_parent_trees parents;
2266 d9765a41 2018-06-23 stsp TAILQ_INIT(&parents);
2268 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
2269 ffd1d5e5 2018-06-23 stsp if (err != NULL)
2270 ffd1d5e5 2018-06-23 stsp goto done;
2272 decd3bd1 2018-07-12 stsp if (asprintf(&tree_label, "commit: %s", commit_id_str) == -1) {
2273 ffd1d5e5 2018-06-23 stsp err = got_error_from_errno();
2274 ffd1d5e5 2018-06-23 stsp goto done;
2277 cc3c9aac 2018-08-01 stsp show_panel(view->panel);
2279 ffd1d5e5 2018-06-23 stsp entries = got_object_tree_get_entries(root);
2280 ffd1d5e5 2018-06-23 stsp first_displayed_entry = SIMPLEQ_FIRST(&entries->head);
2281 ffd1d5e5 2018-06-23 stsp while (!done) {
2282 ce52c690 2018-06-23 stsp char *parent_path;
2283 ffd1d5e5 2018-06-23 stsp entries = got_object_tree_get_entries(tree);
2284 ffd1d5e5 2018-06-23 stsp nentries = entries->nentries;
2285 ffd1d5e5 2018-06-23 stsp if (tree != root)
2286 ffd1d5e5 2018-06-23 stsp nentries++; /* '..' directory */
2288 ce52c690 2018-06-23 stsp err = tree_entry_path(&parent_path, &parents, NULL);
2290 ce52c690 2018-06-23 stsp goto done;
2292 f7d12f7e 2018-08-01 stsp err = draw_tree_entries(view, &first_displayed_entry,
2293 ffd1d5e5 2018-06-23 stsp &last_displayed_entry, &selected_entry, &ndisplayed,
2294 f7d12f7e 2018-08-01 stsp tree_label, show_ids, parent_path, entries, selected,
2295 f7d12f7e 2018-08-01 stsp view->nlines, tree == root);
2296 ce52c690 2018-06-23 stsp free(parent_path);
2300 ffd1d5e5 2018-06-23 stsp nodelay(stdscr, FALSE);
2301 cc3c9aac 2018-08-01 stsp ch = wgetch(view->window);
2302 ffd1d5e5 2018-06-23 stsp nodelay(stdscr, TRUE);
2303 ffd1d5e5 2018-06-23 stsp switch (ch) {
2308 1d13200f 2018-07-12 stsp show_ids = !show_ids;
2311 69efd4c4 2018-07-18 stsp if (selected_entry) {
2312 04cc582a 2018-08-01 stsp struct tog_view *log_view;
2313 6d0fee91 2018-08-01 stsp log_view = open_view(0, 0, 0, 0, view);
2314 04cc582a 2018-08-01 stsp if (log_view == NULL) {
2315 04cc582a 2018-08-01 stsp err = got_error_from_errno();
2316 04cc582a 2018-08-01 stsp goto done;
2318 04cc582a 2018-08-01 stsp err = log_tree_entry(log_view,
2319 04cc582a 2018-08-01 stsp selected_entry, &parents,
2320 04cc582a 2018-08-01 stsp commit_id, repo);
2321 04cc582a 2018-08-01 stsp close_view(log_view);
2323 69efd4c4 2018-07-18 stsp goto done;
2327 ffd1d5e5 2018-06-23 stsp case KEY_UP:
2328 ffd1d5e5 2018-06-23 stsp if (selected > 0)
2329 ffd1d5e5 2018-06-23 stsp selected--;
2330 ffd1d5e5 2018-06-23 stsp if (selected > 0)
2332 ffd1d5e5 2018-06-23 stsp tree_scroll_up(&first_displayed_entry, 1,
2333 ffd1d5e5 2018-06-23 stsp entries, tree == root);
2335 ffd1d5e5 2018-06-23 stsp case KEY_PPAGE:
2336 ffd1d5e5 2018-06-23 stsp if (SIMPLEQ_FIRST(&entries->head) ==
2337 ffd1d5e5 2018-06-23 stsp first_displayed_entry) {
2338 cf8f1261 2018-06-23 stsp if (tree != root)
2339 cf8f1261 2018-06-23 stsp first_displayed_entry = NULL;
2340 ffd1d5e5 2018-06-23 stsp selected = 0;
2343 f7d12f7e 2018-08-01 stsp tree_scroll_up(&first_displayed_entry,
2344 f7d12f7e 2018-08-01 stsp view->nlines, entries, tree == root);
2347 ffd1d5e5 2018-06-23 stsp case KEY_DOWN:
2348 ffd1d5e5 2018-06-23 stsp if (selected < ndisplayed - 1) {
2349 ffd1d5e5 2018-06-23 stsp selected++;
2352 ffd1d5e5 2018-06-23 stsp tree_scroll_down(&first_displayed_entry, 1,
2353 ffd1d5e5 2018-06-23 stsp last_displayed_entry, entries);
2355 ffd1d5e5 2018-06-23 stsp case KEY_NPAGE:
2356 f7d12f7e 2018-08-01 stsp tree_scroll_down(&first_displayed_entry,
2357 f7d12f7e 2018-08-01 stsp view->nlines, last_displayed_entry,
2359 ffd1d5e5 2018-06-23 stsp if (SIMPLEQ_NEXT(last_displayed_entry, entry))
2361 ffd1d5e5 2018-06-23 stsp /* can't scroll any further; move cursor down */
2362 ffd1d5e5 2018-06-23 stsp if (selected < ndisplayed - 1)
2363 ffd1d5e5 2018-06-23 stsp selected = ndisplayed - 1;
2365 ffd1d5e5 2018-06-23 stsp case KEY_ENTER:
2366 ffd1d5e5 2018-06-23 stsp case '\r':
2367 ffd1d5e5 2018-06-23 stsp if (selected_entry == NULL) {
2368 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *parent;
2369 ffd1d5e5 2018-06-23 stsp case KEY_BACKSPACE:
2370 ffd1d5e5 2018-06-23 stsp /* user selected '..' */
2371 ffd1d5e5 2018-06-23 stsp if (tree == root)
2373 d9765a41 2018-06-23 stsp parent = TAILQ_FIRST(&parents);
2374 d9765a41 2018-06-23 stsp TAILQ_REMOVE(&parents, parent, entry);
2375 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
2376 ffd1d5e5 2018-06-23 stsp tree = parent->tree;
2377 ffd1d5e5 2018-06-23 stsp first_displayed_entry =
2378 ffd1d5e5 2018-06-23 stsp parent->first_displayed_entry;
2379 ffd1d5e5 2018-06-23 stsp selected_entry = parent->selected_entry;
2380 ffd1d5e5 2018-06-23 stsp selected = parent->selected;
2381 ffd1d5e5 2018-06-23 stsp free(parent);
2382 ffd1d5e5 2018-06-23 stsp } else if (S_ISDIR(selected_entry->mode)) {
2383 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *parent;
2384 ffd1d5e5 2018-06-23 stsp struct got_tree_object *child;
2385 ffd1d5e5 2018-06-23 stsp err = got_object_open_as_tree(
2386 ffd1d5e5 2018-06-23 stsp &child, repo, selected_entry->id);
2388 ffd1d5e5 2018-06-23 stsp goto done;
2389 ffd1d5e5 2018-06-23 stsp parent = calloc(1, sizeof(*parent));
2390 ffd1d5e5 2018-06-23 stsp if (parent == NULL) {
2391 ffd1d5e5 2018-06-23 stsp err = got_error_from_errno();
2392 ffd1d5e5 2018-06-23 stsp goto done;
2394 ffd1d5e5 2018-06-23 stsp parent->tree = tree;
2395 ffd1d5e5 2018-06-23 stsp parent->first_displayed_entry =
2396 ffd1d5e5 2018-06-23 stsp first_displayed_entry;
2397 ffd1d5e5 2018-06-23 stsp parent->selected_entry = selected_entry;
2398 ffd1d5e5 2018-06-23 stsp parent->selected = selected;
2399 d9765a41 2018-06-23 stsp TAILQ_INSERT_HEAD(&parents, parent,
2401 ffd1d5e5 2018-06-23 stsp tree = child;
2402 ffd1d5e5 2018-06-23 stsp selected = 0;
2403 ffd1d5e5 2018-06-23 stsp first_displayed_entry = NULL;
2404 ffd1d5e5 2018-06-23 stsp } else if (S_ISREG(selected_entry->mode)) {
2405 6d0fee91 2018-08-01 stsp struct tog_view *blame_view =
2406 6d0fee91 2018-08-01 stsp open_view(0, 0, 0, 0, view);
2407 e1cd8fed 2018-08-01 stsp if (blame_view == NULL) {
2408 e1cd8fed 2018-08-01 stsp err = got_error_from_errno();
2409 e1cd8fed 2018-08-01 stsp goto done;
2411 e1cd8fed 2018-08-01 stsp err = blame_tree_entry(blame_view,
2412 e1cd8fed 2018-08-01 stsp selected_entry, &parents,
2413 e1cd8fed 2018-08-01 stsp commit_id, repo);
2414 e1cd8fed 2018-08-01 stsp close_view(blame_view);
2416 ffd1d5e5 2018-06-23 stsp goto done;
2419 ffd1d5e5 2018-06-23 stsp case KEY_RESIZE:
2420 a41d2007 2018-08-01 stsp err = view_resize(view);
2422 a41d2007 2018-08-01 stsp goto done;
2423 f7d12f7e 2018-08-01 stsp if (selected > view->nlines)
2424 ffd1d5e5 2018-06-23 stsp selected = ndisplayed - 1;
2431 ffd1d5e5 2018-06-23 stsp free(tree_label);
2432 ffd1d5e5 2018-06-23 stsp free(commit_id_str);
2433 d9765a41 2018-06-23 stsp while (!TAILQ_EMPTY(&parents)) {
2434 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *parent;
2435 d9765a41 2018-06-23 stsp parent = TAILQ_FIRST(&parents);
2436 d9765a41 2018-06-23 stsp TAILQ_REMOVE(&parents, parent, entry);
2437 ffd1d5e5 2018-06-23 stsp free(parent);
2440 ffd1d5e5 2018-06-23 stsp if (tree != root)
2441 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
2442 ffd1d5e5 2018-06-23 stsp return err;
2445 ffd1d5e5 2018-06-23 stsp __dead static void
2446 ffd1d5e5 2018-06-23 stsp usage_tree(void)
2449 ffd1d5e5 2018-06-23 stsp fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
2450 ffd1d5e5 2018-06-23 stsp getprogname());
2454 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2455 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
2457 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
2458 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
2459 ffd1d5e5 2018-06-23 stsp char *repo_path = NULL;
2460 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
2461 ffd1d5e5 2018-06-23 stsp char *commit_id_arg = NULL;
2462 ffd1d5e5 2018-06-23 stsp struct got_commit_object *commit = NULL;
2463 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree = NULL;
2465 5221c383 2018-08-01 stsp struct tog_view *view;
2467 ffd1d5e5 2018-06-23 stsp #ifndef PROFILE
2468 ffd1d5e5 2018-06-23 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
2469 ffd1d5e5 2018-06-23 stsp err(1, "pledge");
2472 ffd1d5e5 2018-06-23 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
2473 ffd1d5e5 2018-06-23 stsp switch (ch) {
2475 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
2479 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
2483 ffd1d5e5 2018-06-23 stsp argc -= optind;
2484 ffd1d5e5 2018-06-23 stsp argv += optind;
2486 ffd1d5e5 2018-06-23 stsp if (argc == 0) {
2487 ffd1d5e5 2018-06-23 stsp repo_path = getcwd(NULL, 0);
2488 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
2489 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
2490 ffd1d5e5 2018-06-23 stsp } else if (argc == 1) {
2491 ffd1d5e5 2018-06-23 stsp repo_path = realpath(argv[0], NULL);
2492 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
2493 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
2495 ffd1d5e5 2018-06-23 stsp usage_log();
2497 ffd1d5e5 2018-06-23 stsp error = got_repo_open(&repo, repo_path);
2498 ffd1d5e5 2018-06-23 stsp free(repo_path);
2499 ffd1d5e5 2018-06-23 stsp if (error != NULL)
2500 ffd1d5e5 2018-06-23 stsp return error;
2502 ffd1d5e5 2018-06-23 stsp if (commit_id_arg == NULL) {
2503 ffd1d5e5 2018-06-23 stsp error = get_head_commit_id(&commit_id, repo);
2504 ffd1d5e5 2018-06-23 stsp if (error != NULL)
2505 ffd1d5e5 2018-06-23 stsp goto done;
2507 ffd1d5e5 2018-06-23 stsp struct got_object *obj;
2508 ffd1d5e5 2018-06-23 stsp error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
2509 ffd1d5e5 2018-06-23 stsp if (error == NULL) {
2510 ffd1d5e5 2018-06-23 stsp commit_id = got_object_get_id(obj);
2511 ffd1d5e5 2018-06-23 stsp if (commit_id == NULL)
2512 ffd1d5e5 2018-06-23 stsp error = got_error_from_errno();
2515 ffd1d5e5 2018-06-23 stsp if (error != NULL)
2516 ffd1d5e5 2018-06-23 stsp goto done;
2518 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
2519 ffd1d5e5 2018-06-23 stsp if (error != NULL)
2520 ffd1d5e5 2018-06-23 stsp goto done;
2522 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_tree(&tree, repo, commit->tree_id);
2523 ffd1d5e5 2018-06-23 stsp if (error != NULL)
2524 ffd1d5e5 2018-06-23 stsp goto done;
2526 6d0fee91 2018-08-01 stsp view = open_view(0, 0, 0, 0, NULL);
2527 5221c383 2018-08-01 stsp if (view == NULL) {
2528 5221c383 2018-08-01 stsp error = got_error_from_errno();
2529 5221c383 2018-08-01 stsp goto done;
2531 5221c383 2018-08-01 stsp error = show_tree_view(view, tree, commit_id, repo);
2532 5221c383 2018-08-01 stsp close_view(view);
2534 ffd1d5e5 2018-06-23 stsp free(commit_id);
2535 ffd1d5e5 2018-06-23 stsp if (commit)
2536 ffd1d5e5 2018-06-23 stsp got_object_commit_close(commit);
2538 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
2540 ffd1d5e5 2018-06-23 stsp got_repo_close(repo);
2541 ffd1d5e5 2018-06-23 stsp return error;
2543 5c5136c5 2018-05-20 stsp static void
2544 9f7d7167 2018-04-29 stsp init_curses(void)
2546 9f7d7167 2018-04-29 stsp initscr();
2550 9f7d7167 2018-04-29 stsp intrflush(stdscr, FALSE);
2551 9f7d7167 2018-04-29 stsp keypad(stdscr, TRUE);
2552 1f475ad8 2018-05-10 stsp curs_set(0);
2555 4ed7e80c 2018-05-20 stsp __dead static void
2556 9f7d7167 2018-04-29 stsp usage(void)
2560 c2301be8 2018-04-30 stsp fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
2561 9f7d7167 2018-04-29 stsp "Available commands:\n", getprogname());
2562 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
2563 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = &tog_commands[i];
2564 c2301be8 2018-04-30 stsp fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
2569 c2301be8 2018-04-30 stsp static char **
2570 c2301be8 2018-04-30 stsp make_argv(const char *arg0, const char *arg1)
2572 c2301be8 2018-04-30 stsp char **argv;
2573 c2301be8 2018-04-30 stsp int argc = (arg1 == NULL ? 1 : 2);
2575 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
2576 c2301be8 2018-04-30 stsp if (argv == NULL)
2577 c2301be8 2018-04-30 stsp err(1, "calloc");
2578 c2301be8 2018-04-30 stsp argv[0] = strdup(arg0);
2579 c2301be8 2018-04-30 stsp if (argv[0] == NULL)
2580 c2301be8 2018-04-30 stsp err(1, "calloc");
2581 c2301be8 2018-04-30 stsp if (arg1) {
2582 c2301be8 2018-04-30 stsp argv[1] = strdup(arg1);
2583 c2301be8 2018-04-30 stsp if (argv[1] == NULL)
2584 c2301be8 2018-04-30 stsp err(1, "calloc");
2587 c2301be8 2018-04-30 stsp return argv;
2591 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
2593 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
2594 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
2595 9f7d7167 2018-04-29 stsp int ch, hflag = 0;
2596 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
2598 9f7d7167 2018-04-29 stsp setlocale(LC_ALL, "");
2600 9f7d7167 2018-04-29 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
2601 9f7d7167 2018-04-29 stsp switch (ch) {
2603 9f7d7167 2018-04-29 stsp hflag = 1;
2607 9f7d7167 2018-04-29 stsp /* NOTREACHED */
2611 9f7d7167 2018-04-29 stsp argc -= optind;
2612 9f7d7167 2018-04-29 stsp argv += optind;
2613 9f7d7167 2018-04-29 stsp optind = 0;
2614 c2301be8 2018-04-30 stsp optreset = 1;
2616 c2301be8 2018-04-30 stsp if (argc == 0) {
2617 f29d3e89 2018-06-23 stsp if (hflag)
2619 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
2620 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
2621 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, NULL);
2626 c2301be8 2018-04-30 stsp /* Did the user specific a command? */
2627 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
2628 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
2629 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
2630 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
2631 9f7d7167 2018-04-29 stsp if (hflag)
2632 9f7d7167 2018-04-29 stsp tog_commands[i].cmd_usage();
2636 9f7d7167 2018-04-29 stsp if (cmd == NULL) {
2637 c2301be8 2018-04-30 stsp /* Did the user specify a repository? */
2638 c2301be8 2018-04-30 stsp char *repo_path = realpath(argv[0], NULL);
2639 c2301be8 2018-04-30 stsp if (repo_path) {
2640 c2301be8 2018-04-30 stsp struct got_repository *repo;
2641 c2301be8 2018-04-30 stsp error = got_repo_open(&repo, repo_path);
2642 c2301be8 2018-04-30 stsp if (error == NULL)
2643 c2301be8 2018-04-30 stsp got_repo_close(repo);
2645 ad7de8d9 2018-04-30 stsp error = got_error_from_errno();
2646 c2301be8 2018-04-30 stsp if (error) {
2647 f29d3e89 2018-06-23 stsp if (hflag) {
2648 f29d3e89 2018-06-23 stsp fprintf(stderr, "%s: '%s' is not a "
2649 f29d3e89 2018-06-23 stsp "known command\n", getprogname(),
2653 ad7de8d9 2018-04-30 stsp fprintf(stderr, "%s: '%s' is neither a known "
2654 ad7de8d9 2018-04-30 stsp "command nor a path to a repository\n",
2655 c2301be8 2018-04-30 stsp getprogname(), argv[0]);
2656 ad7de8d9 2018-04-30 stsp free(repo_path);
2659 c2301be8 2018-04-30 stsp cmd = &tog_commands[0];
2660 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, repo_path);
2662 c2301be8 2018-04-30 stsp free(repo_path);
2666 5c5136c5 2018-05-20 stsp init_curses();
2668 c2301be8 2018-04-30 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
2669 9f7d7167 2018-04-29 stsp if (error)
2670 9f7d7167 2018-04-29 stsp goto done;
2673 c2301be8 2018-04-30 stsp free(cmd_argv);
2674 9f7d7167 2018-04-29 stsp if (error)
2675 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);