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 */
92 cd0acaa7 2018-05-20 stsp static const struct got_error *
93 ea5e7bb5 2018-08-01 stsp show_diff_view(struct tog_view *, struct got_object *, struct got_object *,
94 cd0acaa7 2018-05-20 stsp struct got_repository *);
95 cd0acaa7 2018-05-20 stsp static const struct got_error *
96 ecb28ae0 2018-07-16 stsp show_log_view(struct got_object_id *, struct got_repository *, const char *);
97 a70480e0 2018-06-23 stsp static const struct got_error *
98 a70480e0 2018-06-23 stsp show_blame_view(const char *, struct got_object_id *, struct got_repository *);
99 ffd1d5e5 2018-06-23 stsp static const struct got_error *
100 ffd1d5e5 2018-06-23 stsp show_tree_view(struct got_tree_object *, struct got_object_id *,
101 ffd1d5e5 2018-06-23 stsp struct got_repository *);
103 ea5e7bb5 2018-08-01 stsp static void
104 ea5e7bb5 2018-08-01 stsp close_view(struct tog_view *view)
106 ea5e7bb5 2018-08-01 stsp if (view->panel)
107 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
108 ea5e7bb5 2018-08-01 stsp if (view->window)
109 ea5e7bb5 2018-08-01 stsp delwin(view->window);
110 ea5e7bb5 2018-08-01 stsp free(view);
113 ea5e7bb5 2018-08-01 stsp static struct tog_view *
114 842167bf 2018-08-01 stsp open_view(int nlines, int ncols, int begin_y, int begin_x)
116 ea5e7bb5 2018-08-01 stsp struct tog_view *view = malloc(sizeof(*view));
118 ea5e7bb5 2018-08-01 stsp if (view == NULL)
119 ea5e7bb5 2018-08-01 stsp return NULL;
121 f7d12f7e 2018-08-01 stsp view->lines = LINES;
122 f7d12f7e 2018-08-01 stsp view->cols = COLS;
123 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
124 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
125 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
126 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
127 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
128 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
129 ea5e7bb5 2018-08-01 stsp close_view(view);
130 ea5e7bb5 2018-08-01 stsp return NULL;
132 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
133 ea5e7bb5 2018-08-01 stsp if (view->panel == NULL) {
134 ea5e7bb5 2018-08-01 stsp close_view(view);
135 ea5e7bb5 2018-08-01 stsp return NULL;
138 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
139 ea5e7bb5 2018-08-01 stsp return view;
142 f7d12f7e 2018-08-01 stsp const struct got_error *
143 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
145 f7d12f7e 2018-08-01 stsp int nlines, ncols;
147 f7d12f7e 2018-08-01 stsp if (view->lines > LINES)
148 f7d12f7e 2018-08-01 stsp nlines = view->nlines - (view->lines - LINES);
150 f7d12f7e 2018-08-01 stsp nlines = view->nlines + (LINES - view->lines);
152 f7d12f7e 2018-08-01 stsp if (view->cols > COLS)
153 f7d12f7e 2018-08-01 stsp ncols = view->ncols - (view->cols - COLS);
155 f7d12f7e 2018-08-01 stsp ncols = view->ncols + (COLS - view->cols);
157 f7d12f7e 2018-08-01 stsp if (wresize(view->window, nlines, ncols) == ERR)
158 f7d12f7e 2018-08-01 stsp return got_error_from_errno();
160 f7d12f7e 2018-08-01 stsp view->nlines = nlines;
161 f7d12f7e 2018-08-01 stsp view->ncols = ncols;
162 f7d12f7e 2018-08-01 stsp view->lines = LINES;
163 f7d12f7e 2018-08-01 stsp view->cols = COLS;
164 f7d12f7e 2018-08-01 stsp return NULL;
167 4ed7e80c 2018-05-20 stsp __dead static void
168 9f7d7167 2018-04-29 stsp usage_log(void)
171 ecb28ae0 2018-07-16 stsp fprintf(stderr, "usage: %s log [-c commit] [-r repository-path] [path]\n",
172 9f7d7167 2018-04-29 stsp getprogname());
176 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
177 80ddbec8 2018-04-29 stsp static const struct got_error *
178 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
180 00dfcb92 2018-06-11 stsp char *vis = NULL;
181 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
183 963b370f 2018-05-20 stsp *ws = NULL;
184 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
185 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
186 00dfcb92 2018-06-11 stsp int vislen;
187 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
188 00dfcb92 2018-06-11 stsp return got_error_from_errno();
190 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
191 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
193 00dfcb92 2018-06-11 stsp return err;
194 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
195 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
196 a7f50699 2018-06-11 stsp err = got_error_from_errno(); /* give up */
201 963b370f 2018-05-20 stsp *ws = calloc(*wlen + 1, sizeof(*ws));
202 a7f50699 2018-06-11 stsp if (*ws == NULL) {
203 a7f50699 2018-06-11 stsp err = got_error_from_errno();
207 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
208 963b370f 2018-05-20 stsp err = got_error_from_errno();
213 963b370f 2018-05-20 stsp *ws = NULL;
216 963b370f 2018-05-20 stsp return err;
219 963b370f 2018-05-20 stsp /* Format a line for display, ensuring that it won't overflow a width limit. */
220 963b370f 2018-05-20 stsp static const struct got_error *
221 ffd1d5e5 2018-06-23 stsp format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
223 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
224 963b370f 2018-05-20 stsp int cols = 0;
225 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
226 963b370f 2018-05-20 stsp size_t wlen;
229 963b370f 2018-05-20 stsp *wlinep = NULL;
230 b700b5d6 2018-07-10 stsp *widthp = 0;
232 963b370f 2018-05-20 stsp err = mbs2ws(&wline, &wlen, line);
234 963b370f 2018-05-20 stsp return err;
237 b700b5d6 2018-07-10 stsp while (i < wlen && cols < wlimit) {
238 963b370f 2018-05-20 stsp int width = wcwidth(wline[i]);
239 963b370f 2018-05-20 stsp switch (width) {
245 b700b5d6 2018-07-10 stsp if (cols + width <= wlimit) {
246 b700b5d6 2018-07-10 stsp cols += width;
251 963b370f 2018-05-20 stsp if (wline[i] == L'\t')
252 b700b5d6 2018-07-10 stsp cols += TABSIZE - ((cols + 1) % TABSIZE);
256 963b370f 2018-05-20 stsp err = got_error_from_errno();
260 963b370f 2018-05-20 stsp wline[i] = L'\0';
261 b700b5d6 2018-07-10 stsp if (widthp)
262 b700b5d6 2018-07-10 stsp *widthp = cols;
265 963b370f 2018-05-20 stsp free(wline);
267 963b370f 2018-05-20 stsp *wlinep = wline;
268 963b370f 2018-05-20 stsp return err;
271 963b370f 2018-05-20 stsp static const struct got_error *
272 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
273 2814baeb 2018-08-01 stsp struct got_object_id *id)
275 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
276 b39d25c7 2018-07-10 stsp char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
277 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
278 6d9fbc00 2018-04-29 stsp char *author0 = NULL, *author = NULL;
279 bb737323 2018-05-20 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
280 bb737323 2018-05-20 stsp int author_width, logmsg_width;
281 6d9fbc00 2018-04-29 stsp char *newline, *smallerthan;
282 80ddbec8 2018-04-29 stsp char *line = NULL;
283 bb737323 2018-05-20 stsp int col, limit;
284 b39d25c7 2018-07-10 stsp static const size_t date_display_cols = 9;
285 bb737323 2018-05-20 stsp static const size_t author_display_cols = 16;
286 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
288 b39d25c7 2018-07-10 stsp if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &commit->tm_committer)
289 b39d25c7 2018-07-10 stsp >= sizeof(datebuf))
290 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
292 b39d25c7 2018-07-10 stsp if (avail < date_display_cols)
293 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
295 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
296 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
297 b39d25c7 2018-07-10 stsp col = limit + 1;
298 b39d25c7 2018-07-10 stsp if (col > avail)
301 6d9fbc00 2018-04-29 stsp author0 = strdup(commit->author);
302 6d9fbc00 2018-04-29 stsp if (author0 == NULL) {
303 80ddbec8 2018-04-29 stsp err = got_error_from_errno();
306 6d9fbc00 2018-04-29 stsp author = author0;
307 6d9fbc00 2018-04-29 stsp smallerthan = strchr(author, '<');
308 6d9fbc00 2018-04-29 stsp if (smallerthan)
309 6d9fbc00 2018-04-29 stsp *smallerthan = '\0';
311 6d9fbc00 2018-04-29 stsp char *at = strchr(author, '@');
313 6d9fbc00 2018-04-29 stsp *at = '\0';
315 b39d25c7 2018-07-10 stsp limit = avail - col;
316 bb737323 2018-05-20 stsp err = format_line(&wauthor, &author_width, author, limit);
319 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
320 bb737323 2018-05-20 stsp col += author_width;
321 9c2eaf34 2018-05-20 stsp while (col <= avail && author_width < author_display_cols + 1) {
322 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
324 bb737323 2018-05-20 stsp author_width++;
326 9c2eaf34 2018-05-20 stsp if (col > avail)
329 bb737323 2018-05-20 stsp logmsg0 = strdup(commit->logmsg);
330 bb737323 2018-05-20 stsp if (logmsg0 == NULL) {
331 6d9fbc00 2018-04-29 stsp err = got_error_from_errno();
334 bb737323 2018-05-20 stsp logmsg = logmsg0;
335 bb737323 2018-05-20 stsp while (*logmsg == '\n')
337 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
338 bb737323 2018-05-20 stsp if (newline)
339 bb737323 2018-05-20 stsp *newline = '\0';
340 bb737323 2018-05-20 stsp limit = avail - col;
341 bb737323 2018-05-20 stsp err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
344 2814baeb 2018-08-01 stsp waddwstr(view->window, wlogmsg);
345 bb737323 2018-05-20 stsp col += logmsg_width;
346 bb737323 2018-05-20 stsp while (col <= avail) {
347 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
351 80ddbec8 2018-04-29 stsp free(logmsg0);
352 bb737323 2018-05-20 stsp free(wlogmsg);
353 6d9fbc00 2018-04-29 stsp free(author0);
354 bb737323 2018-05-20 stsp free(wauthor);
355 80ddbec8 2018-04-29 stsp free(line);
356 80ddbec8 2018-04-29 stsp return err;
359 80ddbec8 2018-04-29 stsp struct commit_queue_entry {
360 80ddbec8 2018-04-29 stsp TAILQ_ENTRY(commit_queue_entry) entry;
361 80ddbec8 2018-04-29 stsp struct got_object_id *id;
362 80ddbec8 2018-04-29 stsp struct got_commit_object *commit;
364 ecb28ae0 2018-07-16 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
365 ecb28ae0 2018-07-16 stsp struct commit_queue {
366 ecb28ae0 2018-07-16 stsp int ncommits;
367 ecb28ae0 2018-07-16 stsp struct commit_queue_head head;
370 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
371 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
372 899d86c2 2018-05-10 stsp struct got_object_id *id)
374 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
376 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
377 80ddbec8 2018-04-29 stsp if (entry == NULL)
378 899d86c2 2018-05-10 stsp return NULL;
380 899d86c2 2018-05-10 stsp entry->id = id;
381 99db9666 2018-05-07 stsp entry->commit = commit;
382 899d86c2 2018-05-10 stsp return entry;
385 99db9666 2018-05-07 stsp static void
386 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
388 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
390 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
391 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
392 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
393 ecb28ae0 2018-07-16 stsp commits->ncommits--;
394 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
395 99db9666 2018-05-07 stsp free(entry);
398 99db9666 2018-05-07 stsp static void
399 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
401 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
402 99db9666 2018-05-07 stsp pop_commit(commits);
405 c4972b91 2018-05-07 stsp static const struct got_error *
406 9ba79e04 2018-06-11 stsp queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
407 ecb28ae0 2018-07-16 stsp struct got_object_id *start_id, int minqueue, int init,
408 ecb28ae0 2018-07-16 stsp struct got_repository *repo, const char *path)
410 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
411 899d86c2 2018-05-10 stsp struct got_object_id *id;
412 9ba79e04 2018-06-11 stsp struct commit_queue_entry *entry;
413 ecb28ae0 2018-07-16 stsp int nfetched, nqueued = 0, found_obj = 0;
414 c8f60bff 2018-07-23 stsp int is_root_path = strcmp(path, "/") == 0;
416 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_start(graph, start_id);
418 c4972b91 2018-05-07 stsp return err;
420 ecb28ae0 2018-07-16 stsp entry = TAILQ_LAST(&commits->head, commit_queue_head);
421 9ba79e04 2018-06-11 stsp if (entry && got_object_id_cmp(entry->id, start_id) == 0) {
422 9ba79e04 2018-06-11 stsp int nfetched;
424 9ba79e04 2018-06-11 stsp /* Start ID's commit is already on the queue; skip over it. */
425 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
426 9ba79e04 2018-06-11 stsp if (err && err->code != GOT_ERR_ITER_NEED_MORE)
427 9ba79e04 2018-06-11 stsp return err;
429 9ba79e04 2018-06-11 stsp err = got_commit_graph_fetch_commits(&nfetched, graph, 1, repo);
431 9ba79e04 2018-06-11 stsp return err;
434 9ba79e04 2018-06-11 stsp while (1) {
435 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
437 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
439 ecb28ae0 2018-07-16 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
441 ecb28ae0 2018-07-16 stsp if (nqueued >= minqueue) {
442 9ba79e04 2018-06-11 stsp err = NULL;
445 ecb28ae0 2018-07-16 stsp err = got_commit_graph_fetch_commits(&nfetched,
446 ecb28ae0 2018-07-16 stsp graph, 1, repo);
448 ecb28ae0 2018-07-16 stsp return err;
451 ecb28ae0 2018-07-16 stsp if (id == NULL)
454 9ba79e04 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
458 c8f60bff 2018-07-23 stsp if (!is_root_path) {
459 ecb28ae0 2018-07-16 stsp struct got_object *obj;
460 ecb28ae0 2018-07-16 stsp struct got_object_qid *pid;
461 ecb28ae0 2018-07-16 stsp int changed = 0;
463 ecb28ae0 2018-07-16 stsp err = got_object_open_by_path(&obj, repo, id, path);
465 c8f60bff 2018-07-23 stsp got_object_commit_close(commit);
466 ecb28ae0 2018-07-16 stsp if (err->code == GOT_ERR_NO_OBJ &&
467 ecb28ae0 2018-07-16 stsp (found_obj || !init)) {
468 ecb28ae0 2018-07-16 stsp /* History stops here. */
469 ecb28ae0 2018-07-16 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
473 ecb28ae0 2018-07-16 stsp found_obj = 1;
475 ecb28ae0 2018-07-16 stsp pid = SIMPLEQ_FIRST(&commit->parent_ids);
476 ecb28ae0 2018-07-16 stsp if (pid != NULL) {
477 ecb28ae0 2018-07-16 stsp struct got_object *pobj;
478 ecb28ae0 2018-07-16 stsp err = got_object_open_by_path(&pobj, repo,
479 ecb28ae0 2018-07-16 stsp pid->id, path);
481 ecb28ae0 2018-07-16 stsp if (err->code != GOT_ERR_NO_OBJ) {
482 ecb28ae0 2018-07-16 stsp got_object_close(obj);
483 c8f60bff 2018-07-23 stsp got_object_commit_close(commit);
486 ecb28ae0 2018-07-16 stsp err = NULL;
487 ecb28ae0 2018-07-16 stsp changed = 1;
489 c2f16475 2018-07-23 stsp struct got_object_id *id, *pid;
490 c2f16475 2018-07-23 stsp id = got_object_get_id(obj);
491 c2f16475 2018-07-23 stsp if (id == NULL) {
492 c2f16475 2018-07-23 stsp err = got_error_from_errno();
493 c8f60bff 2018-07-23 stsp got_object_close(obj);
494 c8f60bff 2018-07-23 stsp got_object_close(pobj);
497 c2f16475 2018-07-23 stsp pid = got_object_get_id(pobj);
498 c2f16475 2018-07-23 stsp if (pid == NULL) {
499 c2f16475 2018-07-23 stsp err = got_error_from_errno();
501 c8f60bff 2018-07-23 stsp got_object_close(obj);
502 c8f60bff 2018-07-23 stsp got_object_close(pobj);
506 c2f16475 2018-07-23 stsp (got_object_id_cmp(id, pid) != 0);
507 ecb28ae0 2018-07-16 stsp got_object_close(pobj);
512 c8f60bff 2018-07-23 stsp got_object_close(obj);
513 ecb28ae0 2018-07-16 stsp if (!changed) {
514 ecb28ae0 2018-07-16 stsp got_object_commit_close(commit);
519 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
520 9ba79e04 2018-06-11 stsp if (entry == NULL) {
521 9ba79e04 2018-06-11 stsp err = got_error_from_errno();
524 ecb28ae0 2018-07-16 stsp TAILQ_INSERT_TAIL(&commits->head, entry, entry);
526 ecb28ae0 2018-07-16 stsp commits->ncommits++;
529 9ba79e04 2018-06-11 stsp return err;
532 99db9666 2018-05-07 stsp static const struct got_error *
533 9ba79e04 2018-06-11 stsp fetch_next_commit(struct commit_queue_entry **pentry,
534 9ba79e04 2018-06-11 stsp struct commit_queue_entry *entry, struct commit_queue *commits,
535 ecb28ae0 2018-07-16 stsp struct got_commit_graph *graph, struct got_repository *repo,
536 ecb28ae0 2018-07-16 stsp const char *path)
538 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
540 9ba79e04 2018-06-11 stsp *pentry = NULL;
542 ecb28ae0 2018-07-16 stsp err = queue_commits(graph, commits, entry->id, 1, 0, repo, path);
544 9ba79e04 2018-06-11 stsp return err;
546 9ba79e04 2018-06-11 stsp /* Next entry to display should now be available. */
547 9ba79e04 2018-06-11 stsp *pentry = TAILQ_NEXT(entry, entry);
548 9ba79e04 2018-06-11 stsp if (*pentry == NULL)
549 9ba79e04 2018-06-11 stsp return got_error(GOT_ERR_NO_OBJ);
551 9ba79e04 2018-06-11 stsp return NULL;
554 899d86c2 2018-05-10 stsp static const struct got_error *
555 9ba79e04 2018-06-11 stsp get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
557 9ba79e04 2018-06-11 stsp const struct got_error *err = NULL;
558 9ba79e04 2018-06-11 stsp struct got_reference *head_ref;
560 9ba79e04 2018-06-11 stsp *head_id = NULL;
562 9ba79e04 2018-06-11 stsp err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
564 99db9666 2018-05-07 stsp return err;
566 9ba79e04 2018-06-11 stsp err = got_ref_resolve(head_id, repo, head_ref);
567 9ba79e04 2018-06-11 stsp got_ref_close(head_ref);
569 9ba79e04 2018-06-11 stsp *head_id = NULL;
570 99db9666 2018-05-07 stsp return err;
573 9ba79e04 2018-06-11 stsp return NULL;
576 0553a4e3 2018-04-30 stsp static const struct got_error *
577 2814baeb 2018-08-01 stsp draw_commits(struct tog_view *view, struct commit_queue_entry **last,
578 ecb28ae0 2018-07-16 stsp struct commit_queue_entry **selected, struct commit_queue_entry *first,
579 a7f40148 2018-07-18 stsp struct commit_queue *commits, int selected_idx, int limit,
580 a7f40148 2018-07-18 stsp struct got_commit_graph *graph, struct got_repository *repo,
581 a7f40148 2018-07-18 stsp const char *path)
583 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
584 0553a4e3 2018-04-30 stsp struct commit_queue_entry *entry;
585 ecb28ae0 2018-07-16 stsp int ncommits, width;
586 867c6645 2018-07-10 stsp char *id_str, *header;
587 ecb28ae0 2018-07-16 stsp wchar_t *wline;
589 e0d42f60 2018-07-22 stsp entry = first;
590 e0d42f60 2018-07-22 stsp ncommits = 0;
591 e0d42f60 2018-07-22 stsp while (entry) {
592 e0d42f60 2018-07-22 stsp if (ncommits == selected_idx) {
593 e0d42f60 2018-07-22 stsp *selected = entry;
596 e0d42f60 2018-07-22 stsp entry = TAILQ_NEXT(entry, entry);
597 e0d42f60 2018-07-22 stsp ncommits++;
600 867c6645 2018-07-10 stsp err = got_object_id_str(&id_str, (*selected)->id);
602 867c6645 2018-07-10 stsp return err;
604 ecb28ae0 2018-07-16 stsp if (path) {
605 ecb28ae0 2018-07-16 stsp if (asprintf(&header, "commit: %s [%s]", id_str, path) == -1) {
606 ecb28ae0 2018-07-16 stsp err = got_error_from_errno();
607 ecb28ae0 2018-07-16 stsp free(id_str);
608 ecb28ae0 2018-07-16 stsp return err;
610 ecb28ae0 2018-07-16 stsp } else if (asprintf(&header, "commit: %s", id_str) == -1) {
611 867c6645 2018-07-10 stsp err = got_error_from_errno();
612 867c6645 2018-07-10 stsp free(id_str);
613 867c6645 2018-07-10 stsp return err;
615 ecb28ae0 2018-07-16 stsp free(id_str);
616 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, header, view->ncols);
618 ecb28ae0 2018-07-16 stsp free(header);
619 ecb28ae0 2018-07-16 stsp return err;
621 ecb28ae0 2018-07-16 stsp free(header);
623 2814baeb 2018-08-01 stsp werase(view->window);
625 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
626 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
627 2814baeb 2018-08-01 stsp waddch(view->window, '\n');
628 ecb28ae0 2018-07-16 stsp free(wline);
629 ecb28ae0 2018-07-16 stsp if (limit <= 1)
630 ecb28ae0 2018-07-16 stsp return NULL;
632 899d86c2 2018-05-10 stsp entry = first;
633 899d86c2 2018-05-10 stsp *last = first;
634 867c6645 2018-07-10 stsp ncommits = 0;
635 899d86c2 2018-05-10 stsp while (entry) {
636 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
638 e0d42f60 2018-07-22 stsp if (ncommits == selected_idx)
639 2814baeb 2018-08-01 stsp wstandout(view->window);
640 2814baeb 2018-08-01 stsp err = draw_commit(view, entry->commit, entry->id);
641 cd0acaa7 2018-05-20 stsp if (ncommits == selected_idx)
642 2814baeb 2018-08-01 stsp wstandend(view->window);
645 0553a4e3 2018-04-30 stsp ncommits++;
646 899d86c2 2018-05-10 stsp *last = entry;
647 a7f40148 2018-07-18 stsp if (entry == TAILQ_LAST(&commits->head, commit_queue_head)) {
648 a7f40148 2018-07-18 stsp err = queue_commits(graph, commits, entry->id, 1,
649 a7f40148 2018-07-18 stsp 0, repo, path);
651 a7f40148 2018-07-18 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
652 a7f40148 2018-07-18 stsp return err;
653 a7f40148 2018-07-18 stsp err = NULL;
656 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
659 80ddbec8 2018-04-29 stsp update_panels();
660 80ddbec8 2018-04-29 stsp doupdate();
662 80ddbec8 2018-04-29 stsp return err;
665 07b55e75 2018-05-10 stsp static void
666 16482c3b 2018-05-20 stsp scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
667 07b55e75 2018-05-10 stsp struct commit_queue *commits)
669 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
670 07b55e75 2018-05-10 stsp int nscrolled = 0;
672 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
673 07b55e75 2018-05-10 stsp if (*first_displayed_entry == entry)
676 07b55e75 2018-05-10 stsp entry = *first_displayed_entry;
677 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
678 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
679 07b55e75 2018-05-10 stsp if (entry) {
680 07b55e75 2018-05-10 stsp *first_displayed_entry = entry;
681 07b55e75 2018-05-10 stsp nscrolled++;
686 aa075928 2018-05-10 stsp static const struct got_error *
687 16482c3b 2018-05-20 stsp scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
688 aa075928 2018-05-10 stsp struct commit_queue_entry *last_displayed_entry,
689 9ba79e04 2018-06-11 stsp struct commit_queue *commits, struct got_commit_graph *graph,
690 ecb28ae0 2018-07-16 stsp struct got_repository *repo, const char *path)
692 aa075928 2018-05-10 stsp const struct got_error *err = NULL;
693 dd0a52c1 2018-05-20 stsp struct commit_queue_entry *pentry;
694 aa075928 2018-05-10 stsp int nscrolled = 0;
697 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(last_displayed_entry, entry);
698 aa075928 2018-05-10 stsp if (pentry == NULL) {
699 9ba79e04 2018-06-11 stsp err = fetch_next_commit(&pentry, last_displayed_entry,
700 ecb28ae0 2018-07-16 stsp commits, graph, repo, path);
701 9a6bf2a5 2018-05-20 stsp if (err || pentry == NULL)
704 dd0a52c1 2018-05-20 stsp last_displayed_entry = pentry;
706 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(*first_displayed_entry, entry);
707 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
709 aa075928 2018-05-10 stsp *first_displayed_entry = pentry;
710 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
712 dd0a52c1 2018-05-20 stsp return err;
715 cd0acaa7 2018-05-20 stsp static const struct got_error *
716 cd0acaa7 2018-05-20 stsp show_commit(struct commit_queue_entry *entry, struct got_repository *repo)
718 cd0acaa7 2018-05-20 stsp const struct got_error *err;
719 cd0acaa7 2018-05-20 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
720 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
721 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
723 cd0acaa7 2018-05-20 stsp err = got_object_open(&obj2, repo, entry->id);
725 cd0acaa7 2018-05-20 stsp return err;
727 9ba79e04 2018-06-11 stsp parent_id = SIMPLEQ_FIRST(&entry->commit->parent_ids);
728 9ba79e04 2018-06-11 stsp if (parent_id) {
729 9ba79e04 2018-06-11 stsp err = got_object_open(&obj1, repo, parent_id->id);
734 842167bf 2018-08-01 stsp view = open_view(0, 0, 0, 0);
735 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
736 ea5e7bb5 2018-08-01 stsp err = got_error_from_errno();
740 ea5e7bb5 2018-08-01 stsp err = show_diff_view(view, obj1, obj2, repo);
741 ea5e7bb5 2018-08-01 stsp close_view(view);
744 cd0acaa7 2018-05-20 stsp got_object_close(obj1);
746 cd0acaa7 2018-05-20 stsp got_object_close(obj2);
747 cd0acaa7 2018-05-20 stsp return err;
750 80ddbec8 2018-04-29 stsp static const struct got_error *
751 9343a5fb 2018-06-23 stsp browse_commit(struct commit_queue_entry *entry, struct got_repository *repo)
753 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
754 9343a5fb 2018-06-23 stsp struct got_tree_object *tree;
756 9343a5fb 2018-06-23 stsp err = got_object_open_as_tree(&tree, repo, entry->commit->tree_id);
758 9343a5fb 2018-06-23 stsp return err;
760 9343a5fb 2018-06-23 stsp err = show_tree_view(tree, entry->id, repo);
761 9343a5fb 2018-06-23 stsp got_object_tree_close(tree);
762 9343a5fb 2018-06-23 stsp return err;
765 9343a5fb 2018-06-23 stsp static const struct got_error *
766 ecb28ae0 2018-07-16 stsp show_log_view(struct got_object_id *start_id, struct got_repository *repo,
767 ecb28ae0 2018-07-16 stsp const char *path)
769 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
770 9ba79e04 2018-06-11 stsp struct got_object_id *head_id = NULL;
771 ecb28ae0 2018-07-16 stsp int ch, done = 0, selected = 0, nfetched;
772 ecb28ae0 2018-07-16 stsp struct got_commit_graph *graph = NULL;
773 0553a4e3 2018-04-30 stsp struct commit_queue commits;
774 899d86c2 2018-05-10 stsp struct commit_queue_entry *first_displayed_entry = NULL;
775 899d86c2 2018-05-10 stsp struct commit_queue_entry *last_displayed_entry = NULL;
776 cd0acaa7 2018-05-20 stsp struct commit_queue_entry *selected_entry = NULL;
777 ecb28ae0 2018-07-16 stsp char *in_repo_path = NULL;
778 2814baeb 2018-08-01 stsp struct tog_view *view = NULL;
780 ecb28ae0 2018-07-16 stsp err = got_repo_map_path(&in_repo_path, repo, path);
781 ecb28ae0 2018-07-16 stsp if (err != NULL)
784 9ba79e04 2018-06-11 stsp err = get_head_commit_id(&head_id, repo);
786 9ba79e04 2018-06-11 stsp return err;
788 034e3b69 2018-07-22 stsp /* The graph contains all commits. */
789 5489743f 2018-06-13 stsp err = got_commit_graph_open(&graph, head_id, 0, repo);
792 034e3b69 2018-07-22 stsp /* The commit queue contains a subset of commits filtered by path. */
793 034e3b69 2018-07-22 stsp TAILQ_INIT(&commits.head);
794 034e3b69 2018-07-22 stsp commits.ncommits = 0;
796 9ba79e04 2018-06-11 stsp /* Populate commit graph with a sufficient number of commits. */
797 9ba79e04 2018-06-11 stsp err = got_commit_graph_fetch_commits_up_to(&nfetched, graph, start_id,
802 f7d12f7e 2018-08-01 stsp view = open_view(0, 0, 0, 0);
803 f7d12f7e 2018-08-01 stsp if (view == NULL) {
804 f7d12f7e 2018-08-01 stsp err = got_error_from_errno();
807 f7d12f7e 2018-08-01 stsp show_panel(view->panel);
810 9ba79e04 2018-06-11 stsp * Open the initial batch of commits, sorted in commit graph order.
811 9ba79e04 2018-06-11 stsp * We keep all commits open throughout the lifetime of the log view
812 9ba79e04 2018-06-11 stsp * in order to avoid having to re-fetch commits from disk while
813 9ba79e04 2018-06-11 stsp * updating the display.
815 f7d12f7e 2018-08-01 stsp err = queue_commits(graph, &commits, start_id, view->nlines, 1, repo,
816 ecb28ae0 2018-07-16 stsp in_repo_path);
818 55198a88 2018-07-16 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
820 55198a88 2018-07-16 stsp err = NULL;
823 034e3b69 2018-07-22 stsp first_displayed_entry = TAILQ_FIRST(&commits.head);
824 867c6645 2018-07-10 stsp selected_entry = first_displayed_entry;
825 899d86c2 2018-05-10 stsp while (!done) {
826 2814baeb 2018-08-01 stsp err = draw_commits(view, &last_displayed_entry, &selected_entry,
827 f7d12f7e 2018-08-01 stsp first_displayed_entry, &commits, selected, view->nlines,
828 a7f40148 2018-07-18 stsp graph, repo, in_repo_path);
832 80ddbec8 2018-04-29 stsp nodelay(stdscr, FALSE);
833 2814baeb 2018-08-01 stsp ch = wgetch(view->window);
834 f7182337 2018-05-20 stsp nodelay(stdscr, TRUE);
835 80ddbec8 2018-04-29 stsp switch (ch) {
842 80ddbec8 2018-04-29 stsp case KEY_UP:
843 80ddbec8 2018-04-29 stsp if (selected > 0)
844 80ddbec8 2018-04-29 stsp selected--;
845 8ec9698d 2018-05-10 stsp if (selected > 0)
847 07b55e75 2018-05-10 stsp scroll_up(&first_displayed_entry, 1, &commits);
849 48531068 2018-05-10 stsp case KEY_PPAGE:
850 ecb28ae0 2018-07-16 stsp if (TAILQ_FIRST(&commits.head) ==
851 dfc1d240 2018-05-10 stsp first_displayed_entry) {
852 dfc1d240 2018-05-10 stsp selected = 0;
855 f7d12f7e 2018-08-01 stsp scroll_up(&first_displayed_entry, view->nlines,
859 80ddbec8 2018-04-29 stsp case KEY_DOWN:
860 f7d12f7e 2018-08-01 stsp if (selected < MIN(view->nlines - 2,
861 ecb28ae0 2018-07-16 stsp commits.ncommits - 1)) {
862 15c91275 2018-05-20 stsp selected++;
865 15c91275 2018-05-20 stsp err = scroll_down(&first_displayed_entry, 1,
866 9ba79e04 2018-06-11 stsp last_displayed_entry, &commits, graph,
867 ecb28ae0 2018-07-16 stsp repo, in_repo_path);
869 ecb28ae0 2018-07-16 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
871 ecb28ae0 2018-07-16 stsp err = NULL;
874 ecb28ae0 2018-07-16 stsp case KEY_NPAGE: {
875 ecb28ae0 2018-07-16 stsp struct commit_queue_entry *first = first_displayed_entry;
876 f7d12f7e 2018-08-01 stsp err = scroll_down(&first_displayed_entry, view->nlines,
877 9ba79e04 2018-06-11 stsp last_displayed_entry, &commits, graph,
878 ecb28ae0 2018-07-16 stsp repo, in_repo_path);
880 ecb28ae0 2018-07-16 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
882 ecb28ae0 2018-07-16 stsp /* can't scroll any further; move cursor down */
883 ecb28ae0 2018-07-16 stsp if (first == first_displayed_entry && selected <
884 f7d12f7e 2018-08-01 stsp MIN(view->nlines - 2, commits.ncommits - 1)) {
885 f7d12f7e 2018-08-01 stsp selected = MIN(view->nlines - 2,
886 ecb28ae0 2018-07-16 stsp commits.ncommits - 1);
888 ecb28ae0 2018-07-16 stsp err = NULL;
892 d6df9be4 2018-04-30 stsp case KEY_RESIZE:
893 a41d2007 2018-08-01 stsp err = view_resize(view);
896 f7d12f7e 2018-08-01 stsp if (selected > view->nlines - 2)
897 f7d12f7e 2018-08-01 stsp selected = view->nlines - 2;
898 55198a88 2018-07-16 stsp if (selected > commits.ncommits - 1)
899 55198a88 2018-07-16 stsp selected = commits.ncommits - 1;
901 cd0acaa7 2018-05-20 stsp case KEY_ENTER:
903 cd0acaa7 2018-05-20 stsp err = show_commit(selected_entry, repo);
906 2814baeb 2018-08-01 stsp show_panel(view->panel);
909 9343a5fb 2018-06-23 stsp err = browse_commit(selected_entry, repo);
912 2814baeb 2018-08-01 stsp show_panel(view->panel);
920 2814baeb 2018-08-01 stsp close_view(view);
921 9ba79e04 2018-06-11 stsp free(head_id);
923 9ba79e04 2018-06-11 stsp got_commit_graph_close(graph);
924 0553a4e3 2018-04-30 stsp free_commits(&commits);
925 ecb28ae0 2018-07-16 stsp free(in_repo_path);
926 80ddbec8 2018-04-29 stsp return err;
929 4ed7e80c 2018-05-20 stsp static const struct got_error *
930 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
932 80ddbec8 2018-04-29 stsp const struct got_error *error;
933 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
934 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
935 ecb28ae0 2018-07-16 stsp char *path = NULL, *repo_path = NULL, *cwd = NULL;
936 80ddbec8 2018-04-29 stsp char *start_commit = NULL;
939 80ddbec8 2018-04-29 stsp #ifndef PROFILE
940 80ddbec8 2018-04-29 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
941 80ddbec8 2018-04-29 stsp err(1, "pledge");
944 ecb28ae0 2018-07-16 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
945 80ddbec8 2018-04-29 stsp switch (ch) {
947 80ddbec8 2018-04-29 stsp start_commit = optarg;
950 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
951 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
952 ecb28ae0 2018-07-16 stsp err(1, "-r option");
956 80ddbec8 2018-04-29 stsp /* NOTREACHED */
960 80ddbec8 2018-04-29 stsp argc -= optind;
961 80ddbec8 2018-04-29 stsp argv += optind;
963 ecb28ae0 2018-07-16 stsp if (argc == 0)
964 ecb28ae0 2018-07-16 stsp path = strdup("");
965 ecb28ae0 2018-07-16 stsp else if (argc == 1)
966 ecb28ae0 2018-07-16 stsp path = strdup(argv[0]);
968 80ddbec8 2018-04-29 stsp usage_log();
969 ecb28ae0 2018-07-16 stsp if (path == NULL)
970 ecb28ae0 2018-07-16 stsp return got_error_from_errno();
972 ecb28ae0 2018-07-16 stsp cwd = getcwd(NULL, 0);
973 ecb28ae0 2018-07-16 stsp if (cwd == NULL) {
974 ecb28ae0 2018-07-16 stsp error = got_error_from_errno();
977 ecb28ae0 2018-07-16 stsp if (repo_path == NULL) {
978 ecb28ae0 2018-07-16 stsp repo_path = strdup(cwd);
979 ecb28ae0 2018-07-16 stsp if (repo_path == NULL) {
980 ecb28ae0 2018-07-16 stsp error = got_error_from_errno();
985 80ddbec8 2018-04-29 stsp error = got_repo_open(&repo, repo_path);
986 80ddbec8 2018-04-29 stsp if (error != NULL)
989 80ddbec8 2018-04-29 stsp if (start_commit == NULL) {
990 899d86c2 2018-05-10 stsp error = get_head_commit_id(&start_id, repo);
991 80ddbec8 2018-04-29 stsp if (error != NULL)
994 80ddbec8 2018-04-29 stsp struct got_object *obj;
995 80ddbec8 2018-04-29 stsp error = got_object_open_by_id_str(&obj, repo, start_commit);
996 80ddbec8 2018-04-29 stsp if (error == NULL) {
997 899d86c2 2018-05-10 stsp start_id = got_object_get_id(obj);
998 899d86c2 2018-05-10 stsp if (start_id == NULL)
999 80ddbec8 2018-04-29 stsp error = got_error_from_errno();
1000 ecb28ae0 2018-07-16 stsp goto done;
1003 80ddbec8 2018-04-29 stsp if (error != NULL)
1004 ecb28ae0 2018-07-16 stsp goto done;
1006 ecb28ae0 2018-07-16 stsp error = show_log_view(start_id, repo, path);
1008 ecb28ae0 2018-07-16 stsp free(repo_path);
1009 ecb28ae0 2018-07-16 stsp free(cwd);
1010 ecb28ae0 2018-07-16 stsp free(path);
1011 899d86c2 2018-05-10 stsp free(start_id);
1013 ecb28ae0 2018-07-16 stsp got_repo_close(repo);
1014 80ddbec8 2018-04-29 stsp return error;
1017 4ed7e80c 2018-05-20 stsp __dead static void
1018 9f7d7167 2018-04-29 stsp usage_diff(void)
1021 9f7d7167 2018-04-29 stsp fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1022 9f7d7167 2018-04-29 stsp getprogname());
1026 b304db33 2018-05-20 stsp static char *
1027 b304db33 2018-05-20 stsp parse_next_line(FILE *f, size_t *len)
1029 b304db33 2018-05-20 stsp char *line;
1030 b304db33 2018-05-20 stsp size_t linelen;
1031 b304db33 2018-05-20 stsp size_t lineno;
1032 b304db33 2018-05-20 stsp const char delim[3] = { '\0', '\0', '\0'};
1034 b304db33 2018-05-20 stsp line = fparseln(f, &linelen, &lineno, delim, 0);
1036 b304db33 2018-05-20 stsp *len = linelen;
1037 b304db33 2018-05-20 stsp return line;
1040 4ed7e80c 2018-05-20 stsp static const struct got_error *
1041 f7d12f7e 2018-08-01 stsp draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1042 a70480e0 2018-06-23 stsp int *last_displayed_line, int *eof, int max_lines)
1044 61e69b96 2018-05-20 stsp const struct got_error *err;
1045 26ed57b2 2018-05-19 stsp int nlines = 0, nprinted = 0;
1046 b304db33 2018-05-20 stsp char *line;
1047 b304db33 2018-05-20 stsp size_t len;
1048 61e69b96 2018-05-20 stsp wchar_t *wline;
1049 e0b650dd 2018-05-20 stsp int width;
1051 26ed57b2 2018-05-19 stsp rewind(f);
1052 f7d12f7e 2018-08-01 stsp werase(view->window);
1055 26ed57b2 2018-05-19 stsp while (nprinted < max_lines) {
1056 b304db33 2018-05-20 stsp line = parse_next_line(f, &len);
1057 26ed57b2 2018-05-19 stsp if (line == NULL) {
1061 26ed57b2 2018-05-19 stsp if (++nlines < *first_displayed_line) {
1062 26ed57b2 2018-05-19 stsp free(line);
1066 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
1067 61e69b96 2018-05-20 stsp if (err) {
1068 61e69b96 2018-05-20 stsp free(line);
1069 2550e4c3 2018-07-13 stsp free(wline);
1070 61e69b96 2018-05-20 stsp return err;
1072 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
1073 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
1074 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
1075 26ed57b2 2018-05-19 stsp if (++nprinted == 1)
1076 26ed57b2 2018-05-19 stsp *first_displayed_line = nlines;
1077 26ed57b2 2018-05-19 stsp free(line);
1078 2550e4c3 2018-07-13 stsp free(wline);
1079 2550e4c3 2018-07-13 stsp wline = NULL;
1081 26ed57b2 2018-05-19 stsp *last_displayed_line = nlines;
1083 26ed57b2 2018-05-19 stsp update_panels();
1084 26ed57b2 2018-05-19 stsp doupdate();
1086 26ed57b2 2018-05-19 stsp return NULL;
1089 4ed7e80c 2018-05-20 stsp static const struct got_error *
1090 ea5e7bb5 2018-08-01 stsp show_diff_view(struct tog_view *view, struct got_object *obj1,
1091 ea5e7bb5 2018-08-01 stsp struct got_object *obj2, struct got_repository *repo)
1093 26ed57b2 2018-05-19 stsp const struct got_error *err;
1095 f7d12f7e 2018-08-01 stsp int ch, done = 0;
1096 f7d12f7e 2018-08-01 stsp int first_displayed_line = 1, last_displayed_line = view->nlines;
1097 b304db33 2018-05-20 stsp int eof, i;
1099 cd0acaa7 2018-05-20 stsp if (obj1 != NULL && obj2 != NULL &&
1100 cd0acaa7 2018-05-20 stsp got_object_get_type(obj1) != got_object_get_type(obj2))
1101 26ed57b2 2018-05-19 stsp return got_error(GOT_ERR_OBJ_TYPE);
1103 511a516b 2018-05-19 stsp f = got_opentemp();
1104 26ed57b2 2018-05-19 stsp if (f == NULL)
1105 26ed57b2 2018-05-19 stsp return got_error_from_errno();
1107 cd0acaa7 2018-05-20 stsp switch (got_object_get_type(obj1 ? obj1 : obj2)) {
1108 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
1109 11528a82 2018-05-19 stsp err = got_diff_objects_as_blobs(obj1, obj2, repo, f);
1111 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
1112 11528a82 2018-05-19 stsp err = got_diff_objects_as_trees(obj1, obj2, repo, f);
1114 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_COMMIT:
1115 11528a82 2018-05-19 stsp err = got_diff_objects_as_commits(obj1, obj2, repo, f);
1118 26ed57b2 2018-05-19 stsp return got_error(GOT_ERR_OBJ_TYPE);
1121 26ed57b2 2018-05-19 stsp fflush(f);
1123 ea5e7bb5 2018-08-01 stsp show_panel(view->panel);
1125 26ed57b2 2018-05-19 stsp while (!done) {
1126 f7d12f7e 2018-08-01 stsp err = draw_file(view, f, &first_displayed_line,
1127 f7d12f7e 2018-08-01 stsp &last_displayed_line, &eof, view->nlines);
1130 26ed57b2 2018-05-19 stsp nodelay(stdscr, FALSE);
1131 ea5e7bb5 2018-08-01 stsp ch = wgetch(view->window);
1132 f7182337 2018-05-20 stsp nodelay(stdscr, TRUE);
1133 26ed57b2 2018-05-19 stsp switch (ch) {
1138 26ed57b2 2018-05-19 stsp case KEY_UP:
1139 26ed57b2 2018-05-19 stsp if (first_displayed_line > 1)
1140 b304db33 2018-05-20 stsp first_displayed_line--;
1142 b304db33 2018-05-20 stsp case KEY_PPAGE:
1143 c46b9352 2018-07-12 stsp case KEY_BACKSPACE:
1145 f7d12f7e 2018-08-01 stsp while (i++ < view->nlines - 1 &&
1146 d56caa55 2018-05-20 stsp first_displayed_line > 1)
1147 26ed57b2 2018-05-19 stsp first_displayed_line--;
1150 26ed57b2 2018-05-19 stsp case KEY_DOWN:
1152 26ed57b2 2018-05-19 stsp first_displayed_line++;
1154 b304db33 2018-05-20 stsp case KEY_NPAGE:
1157 f7d12f7e 2018-08-01 stsp while (!eof && i++ < view->nlines - 1) {
1158 b304db33 2018-05-20 stsp char *line = parse_next_line(f, NULL);
1159 b304db33 2018-05-20 stsp first_displayed_line++;
1160 b304db33 2018-05-20 stsp if (line == NULL)
1164 82357c0a 2018-08-01 stsp case KEY_RESIZE:
1165 a41d2007 2018-08-01 stsp err = view_resize(view);
1167 a41d2007 2018-08-01 stsp goto done;
1174 26ed57b2 2018-05-19 stsp fclose(f);
1175 26ed57b2 2018-05-19 stsp return err;
1178 4ed7e80c 2018-05-20 stsp static const struct got_error *
1179 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
1181 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
1182 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
1183 26ed57b2 2018-05-19 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
1184 26ed57b2 2018-05-19 stsp char *repo_path = NULL;
1185 26ed57b2 2018-05-19 stsp char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
1187 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
1189 26ed57b2 2018-05-19 stsp #ifndef PROFILE
1190 26ed57b2 2018-05-19 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1191 26ed57b2 2018-05-19 stsp err(1, "pledge");
1194 26ed57b2 2018-05-19 stsp while ((ch = getopt(argc, argv, "")) != -1) {
1195 26ed57b2 2018-05-19 stsp switch (ch) {
1198 26ed57b2 2018-05-19 stsp /* NOTREACHED */
1202 26ed57b2 2018-05-19 stsp argc -= optind;
1203 26ed57b2 2018-05-19 stsp argv += optind;
1205 26ed57b2 2018-05-19 stsp if (argc == 0) {
1206 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
1207 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
1208 26ed57b2 2018-05-19 stsp repo_path = getcwd(NULL, 0);
1209 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
1210 26ed57b2 2018-05-19 stsp return got_error_from_errno();
1211 26ed57b2 2018-05-19 stsp obj_id_str1 = argv[0];
1212 26ed57b2 2018-05-19 stsp obj_id_str2 = argv[1];
1213 26ed57b2 2018-05-19 stsp } else if (argc == 3) {
1214 26ed57b2 2018-05-19 stsp repo_path = realpath(argv[0], NULL);
1215 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
1216 26ed57b2 2018-05-19 stsp return got_error_from_errno();
1217 26ed57b2 2018-05-19 stsp obj_id_str1 = argv[1];
1218 26ed57b2 2018-05-19 stsp obj_id_str2 = argv[2];
1220 26ed57b2 2018-05-19 stsp usage_diff();
1222 26ed57b2 2018-05-19 stsp error = got_repo_open(&repo, repo_path);
1223 26ed57b2 2018-05-19 stsp free(repo_path);
1224 26ed57b2 2018-05-19 stsp if (error)
1225 26ed57b2 2018-05-19 stsp goto done;
1227 26ed57b2 2018-05-19 stsp error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
1228 26ed57b2 2018-05-19 stsp if (error)
1229 26ed57b2 2018-05-19 stsp goto done;
1231 26ed57b2 2018-05-19 stsp error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1232 26ed57b2 2018-05-19 stsp if (error)
1233 26ed57b2 2018-05-19 stsp goto done;
1235 842167bf 2018-08-01 stsp view = open_view(0, 0, 0, 0);
1236 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
1237 ea5e7bb5 2018-08-01 stsp error = got_error_from_errno();
1238 ea5e7bb5 2018-08-01 stsp goto done;
1240 ea5e7bb5 2018-08-01 stsp error = show_diff_view(view, obj1, obj2, repo);
1241 ea5e7bb5 2018-08-01 stsp close_view(view);
1243 26ed57b2 2018-05-19 stsp got_repo_close(repo);
1245 26ed57b2 2018-05-19 stsp got_object_close(obj1);
1247 26ed57b2 2018-05-19 stsp got_object_close(obj2);
1248 26ed57b2 2018-05-19 stsp return error;
1251 4ed7e80c 2018-05-20 stsp __dead static void
1252 9f7d7167 2018-04-29 stsp usage_blame(void)
1255 a70480e0 2018-06-23 stsp fprintf(stderr, "usage: %s blame [-c commit] [repository-path] path\n",
1256 9f7d7167 2018-04-29 stsp getprogname());
1260 84451b3e 2018-07-10 stsp struct tog_blame_line {
1261 84451b3e 2018-07-10 stsp int annotated;
1262 84451b3e 2018-07-10 stsp struct got_object_id *id;
1265 4ed7e80c 2018-05-20 stsp static const struct got_error *
1266 f7d12f7e 2018-08-01 stsp draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
1267 f7d12f7e 2018-08-01 stsp const char *path, struct tog_blame_line *lines, int nlines,
1268 f7d12f7e 2018-08-01 stsp int blame_complete, int selected_line, int *first_displayed_line,
1269 f7d12f7e 2018-08-01 stsp int *last_displayed_line, int *eof, int max_lines)
1271 84451b3e 2018-07-10 stsp const struct got_error *err;
1272 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
1273 84451b3e 2018-07-10 stsp char *line;
1274 84451b3e 2018-07-10 stsp size_t len;
1275 84451b3e 2018-07-10 stsp wchar_t *wline;
1276 b700b5d6 2018-07-10 stsp int width, wlimit;
1277 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
1278 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
1279 ab089a2a 2018-07-12 stsp char *id_str;
1281 ab089a2a 2018-07-12 stsp err = got_object_id_str(&id_str, id);
1283 ab089a2a 2018-07-12 stsp return err;
1285 84451b3e 2018-07-10 stsp rewind(f);
1286 f7d12f7e 2018-08-01 stsp werase(view->window);
1288 ab089a2a 2018-07-12 stsp if (asprintf(&line, "commit: %s", id_str) == -1) {
1289 ab089a2a 2018-07-12 stsp err = got_error_from_errno();
1290 ab089a2a 2018-07-12 stsp free(id_str);
1291 ab089a2a 2018-07-12 stsp return err;
1294 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
1295 ab089a2a 2018-07-12 stsp free(line);
1296 2550e4c3 2018-07-13 stsp line = NULL;
1297 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
1298 2550e4c3 2018-07-13 stsp free(wline);
1299 2550e4c3 2018-07-13 stsp wline = NULL;
1300 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
1301 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
1303 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
1304 084063cd 2018-07-12 stsp *first_displayed_line - 1 + selected_line, nlines,
1305 12a0b23b 2018-07-12 stsp blame_complete ? "" : "annotating ", path) == -1) {
1306 ab089a2a 2018-07-12 stsp free(id_str);
1307 3f60a8ef 2018-07-10 stsp return got_error_from_errno();
1309 ab089a2a 2018-07-12 stsp free(id_str);
1310 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
1311 3f60a8ef 2018-07-10 stsp free(line);
1312 2550e4c3 2018-07-13 stsp line = NULL;
1314 3f60a8ef 2018-07-10 stsp return err;
1315 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
1316 2550e4c3 2018-07-13 stsp free(wline);
1317 2550e4c3 2018-07-13 stsp wline = NULL;
1318 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
1319 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
1322 ab089a2a 2018-07-12 stsp while (nprinted < max_lines - 2) {
1323 84451b3e 2018-07-10 stsp line = parse_next_line(f, &len);
1324 84451b3e 2018-07-10 stsp if (line == NULL) {
1328 84451b3e 2018-07-10 stsp if (++lineno < *first_displayed_line) {
1329 84451b3e 2018-07-10 stsp free(line);
1333 f7d12f7e 2018-08-01 stsp wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
1334 b700b5d6 2018-07-10 stsp err = format_line(&wline, &width, line, wlimit);
1335 84451b3e 2018-07-10 stsp if (err) {
1336 84451b3e 2018-07-10 stsp free(line);
1337 84451b3e 2018-07-10 stsp return err;
1340 b700b5d6 2018-07-10 stsp if (nprinted == selected_line - 1)
1341 f7d12f7e 2018-08-01 stsp wstandout(view->window);
1343 84451b3e 2018-07-10 stsp blame_line = &lines[lineno - 1];
1344 ee41ec32 2018-07-10 stsp if (blame_line->annotated && prev_id &&
1345 ee41ec32 2018-07-10 stsp got_object_id_cmp(prev_id, blame_line->id) == 0)
1346 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ");
1347 ee41ec32 2018-07-10 stsp else if (blame_line->annotated) {
1348 84451b3e 2018-07-10 stsp char *id_str;
1349 84451b3e 2018-07-10 stsp err = got_object_id_str(&id_str, blame_line->id);
1350 84451b3e 2018-07-10 stsp if (err) {
1351 84451b3e 2018-07-10 stsp free(line);
1352 2550e4c3 2018-07-13 stsp free(wline);
1353 84451b3e 2018-07-10 stsp return err;
1355 f7d12f7e 2018-08-01 stsp wprintw(view->window, "%.8s ", id_str);
1356 84451b3e 2018-07-10 stsp free(id_str);
1357 ee41ec32 2018-07-10 stsp prev_id = blame_line->id;
1359 f7d12f7e 2018-08-01 stsp waddstr(view->window, "........ ");
1360 ee41ec32 2018-07-10 stsp prev_id = NULL;
1363 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
1364 b700b5d6 2018-07-10 stsp while (width < wlimit) {
1365 f7d12f7e 2018-08-01 stsp waddch(view->window, ' ');
1368 b700b5d6 2018-07-10 stsp if (nprinted == selected_line - 1)
1369 f7d12f7e 2018-08-01 stsp wstandend(view->window);
1370 84451b3e 2018-07-10 stsp if (++nprinted == 1)
1371 84451b3e 2018-07-10 stsp *first_displayed_line = lineno;
1372 84451b3e 2018-07-10 stsp free(line);
1373 2550e4c3 2018-07-13 stsp free(wline);
1374 2550e4c3 2018-07-13 stsp wline = NULL;
1376 84451b3e 2018-07-10 stsp *last_displayed_line = lineno;
1378 84451b3e 2018-07-10 stsp update_panels();
1379 84451b3e 2018-07-10 stsp doupdate();
1381 84451b3e 2018-07-10 stsp return NULL;
1384 84451b3e 2018-07-10 stsp struct tog_blame_cb_args {
1385 84451b3e 2018-07-10 stsp pthread_mutex_t *mutex;
1386 84451b3e 2018-07-10 stsp struct tog_blame_line *lines; /* one per line */
1387 84451b3e 2018-07-10 stsp int nlines;
1389 7cc84d77 2018-08-01 stsp struct tog_view *view;
1390 ab089a2a 2018-07-12 stsp struct got_object_id *commit_id;
1392 3f60a8ef 2018-07-10 stsp const char *path;
1393 84451b3e 2018-07-10 stsp int *first_displayed_line;
1394 84451b3e 2018-07-10 stsp int *last_displayed_line;
1395 b700b5d6 2018-07-10 stsp int *selected_line;
1396 18430de3 2018-07-10 stsp int *quit;
1399 84451b3e 2018-07-10 stsp static const struct got_error *
1400 84451b3e 2018-07-10 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
1402 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
1403 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
1404 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
1407 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
1408 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
1409 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
1411 84451b3e 2018-07-10 stsp if (pthread_mutex_lock(a->mutex) != 0)
1412 84451b3e 2018-07-10 stsp return got_error_from_errno();
1414 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
1415 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
1416 d68a0a7d 2018-07-10 stsp goto done;
1419 d68a0a7d 2018-07-10 stsp if (lineno == -1)
1420 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
1422 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
1423 d68a0a7d 2018-07-10 stsp if (line->annotated)
1424 d68a0a7d 2018-07-10 stsp goto done;
1426 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
1427 84451b3e 2018-07-10 stsp if (line->id == NULL) {
1428 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1429 84451b3e 2018-07-10 stsp goto done;
1431 84451b3e 2018-07-10 stsp line->annotated = 1;
1433 f7d12f7e 2018-08-01 stsp err = draw_blame(a->view, a->commit_id, a->f, a->path,
1434 245d91c1 2018-07-12 stsp a->lines, a->nlines, 0, *a->selected_line, a->first_displayed_line,
1435 f7d12f7e 2018-08-01 stsp a->last_displayed_line, &eof, a->view->nlines);
1437 84451b3e 2018-07-10 stsp if (pthread_mutex_unlock(a->mutex) != 0)
1438 84451b3e 2018-07-10 stsp return got_error_from_errno();
1439 84451b3e 2018-07-10 stsp return err;
1442 84451b3e 2018-07-10 stsp struct tog_blame_thread_args {
1443 84451b3e 2018-07-10 stsp const char *path;
1444 84451b3e 2018-07-10 stsp struct got_repository *repo;
1445 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *cb_args;
1446 18430de3 2018-07-10 stsp int *complete;
1449 84451b3e 2018-07-10 stsp static void *
1450 84451b3e 2018-07-10 stsp blame_thread(void *arg)
1452 18430de3 2018-07-10 stsp const struct got_error *err;
1453 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
1454 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
1457 ab089a2a 2018-07-12 stsp err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
1458 245d91c1 2018-07-12 stsp blame_cb, ta->cb_args);
1460 18430de3 2018-07-10 stsp if (pthread_mutex_lock(a->mutex) != 0)
1461 18430de3 2018-07-10 stsp return (void *)got_error_from_errno();
1463 c9beca56 2018-07-22 stsp got_repo_close(ta->repo);
1464 c9beca56 2018-07-22 stsp ta->repo = NULL;
1465 c9beca56 2018-07-22 stsp *ta->complete = 1;
1467 f7d12f7e 2018-08-01 stsp err = draw_blame(a->view, a->commit_id, a->f, a->path,
1468 f7d12f7e 2018-08-01 stsp a->lines, a->nlines, 1, *a->selected_line,
1469 c9beca56 2018-07-22 stsp a->first_displayed_line, a->last_displayed_line, &eof,
1470 f7d12f7e 2018-08-01 stsp a->view->nlines);
1472 18430de3 2018-07-10 stsp if (pthread_mutex_unlock(a->mutex) != 0 && err == NULL)
1473 18430de3 2018-07-10 stsp err = got_error_from_errno();
1475 18430de3 2018-07-10 stsp return (void *)err;
1478 245d91c1 2018-07-12 stsp static struct got_object_id *
1479 245d91c1 2018-07-12 stsp get_selected_commit_id(struct tog_blame_line *lines,
1480 245d91c1 2018-07-12 stsp int first_displayed_line, int selected_line)
1482 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
1484 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
1485 245d91c1 2018-07-12 stsp if (!line->annotated)
1486 245d91c1 2018-07-12 stsp return NULL;
1488 245d91c1 2018-07-12 stsp return line->id;
1491 84451b3e 2018-07-10 stsp static const struct got_error *
1492 245d91c1 2018-07-12 stsp open_selected_commit(struct got_object **pobj, struct got_object **obj,
1493 b880a918 2018-07-10 stsp struct tog_blame_line *lines, int first_displayed_line,
1494 b880a918 2018-07-10 stsp int selected_line, struct got_repository *repo)
1496 b880a918 2018-07-10 stsp const struct got_error *err = NULL;
1497 b880a918 2018-07-10 stsp struct got_commit_object *commit = NULL;
1498 245d91c1 2018-07-12 stsp struct got_object_id *selected_id;
1499 b880a918 2018-07-10 stsp struct got_object_qid *pid;
1501 b880a918 2018-07-10 stsp *pobj = NULL;
1502 b880a918 2018-07-10 stsp *obj = NULL;
1504 245d91c1 2018-07-12 stsp selected_id = get_selected_commit_id(lines,
1505 245d91c1 2018-07-12 stsp first_displayed_line, selected_line);
1506 245d91c1 2018-07-12 stsp if (selected_id == NULL)
1507 b880a918 2018-07-10 stsp return NULL;
1509 245d91c1 2018-07-12 stsp err = got_object_open(obj, repo, selected_id);
1511 b880a918 2018-07-10 stsp goto done;
1513 b880a918 2018-07-10 stsp err = got_object_commit_open(&commit, repo, *obj);
1515 b880a918 2018-07-10 stsp goto done;
1517 b880a918 2018-07-10 stsp pid = SIMPLEQ_FIRST(&commit->parent_ids);
1518 b880a918 2018-07-10 stsp if (pid) {
1519 b880a918 2018-07-10 stsp err = got_object_open(pobj, repo, pid->id);
1521 b880a918 2018-07-10 stsp goto done;
1524 b880a918 2018-07-10 stsp if (commit)
1525 b880a918 2018-07-10 stsp got_object_commit_close(commit);
1526 b880a918 2018-07-10 stsp return err;
1529 245d91c1 2018-07-12 stsp struct tog_blame {
1531 245d91c1 2018-07-12 stsp size_t filesize;
1532 245d91c1 2018-07-12 stsp struct tog_blame_line *lines;
1533 245d91c1 2018-07-12 stsp size_t nlines;
1534 245d91c1 2018-07-12 stsp pthread_t thread;
1535 245d91c1 2018-07-12 stsp struct tog_blame_thread_args thread_args;
1536 245d91c1 2018-07-12 stsp struct tog_blame_cb_args cb_args;
1537 245d91c1 2018-07-12 stsp const char *path;
1540 b880a918 2018-07-10 stsp static const struct got_error *
1541 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
1543 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
1546 245d91c1 2018-07-12 stsp if (blame->thread) {
1547 245d91c1 2018-07-12 stsp if (pthread_join(blame->thread, (void **)&err) != 0)
1548 245d91c1 2018-07-12 stsp err = got_error_from_errno();
1549 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
1550 245d91c1 2018-07-12 stsp err = NULL;
1551 245d91c1 2018-07-12 stsp blame->thread = NULL;
1553 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
1554 245d91c1 2018-07-12 stsp got_repo_close(blame->thread_args.repo);
1555 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
1557 245d91c1 2018-07-12 stsp if (blame->f) {
1558 245d91c1 2018-07-12 stsp fclose(blame->f);
1559 245d91c1 2018-07-12 stsp blame->f = NULL;
1561 245d91c1 2018-07-12 stsp for (i = 0; i < blame->nlines; i++)
1562 245d91c1 2018-07-12 stsp free(blame->lines[i].id);
1563 245d91c1 2018-07-12 stsp free(blame->lines);
1564 245d91c1 2018-07-12 stsp blame->lines = NULL;
1565 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
1566 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
1568 245d91c1 2018-07-12 stsp return err;
1571 245d91c1 2018-07-12 stsp static const struct got_error *
1572 7cc84d77 2018-08-01 stsp run_blame(struct tog_blame *blame, pthread_mutex_t *mutex,
1573 7cc84d77 2018-08-01 stsp struct tog_view *view, int *blame_complete,
1574 245d91c1 2018-07-12 stsp int *first_displayed_line, int *last_displayed_line,
1575 245d91c1 2018-07-12 stsp int *selected_line, int *done, const char *path,
1576 245d91c1 2018-07-12 stsp struct got_object_id *commit_id,
1577 245d91c1 2018-07-12 stsp struct got_repository *repo)
1579 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
1580 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
1581 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
1582 245d91c1 2018-07-12 stsp struct got_object *obj;
1584 84451b3e 2018-07-10 stsp err = got_object_open_by_path(&obj, repo, commit_id, path);
1586 84451b3e 2018-07-10 stsp goto done;
1587 84451b3e 2018-07-10 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
1588 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1589 84451b3e 2018-07-10 stsp goto done;
1592 84451b3e 2018-07-10 stsp err = got_object_blob_open(&blob, repo, obj, 8192);
1594 a70480e0 2018-06-23 stsp goto done;
1595 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
1596 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
1597 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1598 84451b3e 2018-07-10 stsp goto done;
1600 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
1601 245d91c1 2018-07-12 stsp blame->f, blob);
1603 84451b3e 2018-07-10 stsp goto done;
1605 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
1606 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
1607 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1608 84451b3e 2018-07-10 stsp goto done;
1611 245d91c1 2018-07-12 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1613 bd24772e 2018-07-11 stsp goto done;
1615 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
1616 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
1617 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
1618 245d91c1 2018-07-12 stsp blame->cb_args.mutex = mutex;
1619 245d91c1 2018-07-12 stsp blame->cb_args.commit_id = got_object_id_dup(commit_id);
1620 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
1621 245d91c1 2018-07-12 stsp err = got_error_from_errno();
1622 245d91c1 2018-07-12 stsp goto done;
1624 245d91c1 2018-07-12 stsp blame->cb_args.f = blame->f;
1625 245d91c1 2018-07-12 stsp blame->cb_args.path = path;
1626 245d91c1 2018-07-12 stsp blame->cb_args.first_displayed_line = first_displayed_line;
1627 245d91c1 2018-07-12 stsp blame->cb_args.selected_line = selected_line;
1628 245d91c1 2018-07-12 stsp blame->cb_args.last_displayed_line = last_displayed_line;
1629 245d91c1 2018-07-12 stsp blame->cb_args.quit = done;
1631 245d91c1 2018-07-12 stsp blame->thread_args.path = path;
1632 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
1633 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
1634 245d91c1 2018-07-12 stsp blame->thread_args.complete = blame_complete;
1635 245d91c1 2018-07-12 stsp *blame_complete = 0;
1637 245d91c1 2018-07-12 stsp if (pthread_create(&blame->thread, NULL, blame_thread,
1638 245d91c1 2018-07-12 stsp &blame->thread_args) != 0) {
1639 245d91c1 2018-07-12 stsp err = got_error_from_errno();
1640 245d91c1 2018-07-12 stsp goto done;
1645 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
1647 245d91c1 2018-07-12 stsp got_object_close(obj);
1649 245d91c1 2018-07-12 stsp stop_blame(blame);
1650 245d91c1 2018-07-12 stsp return err;
1653 245d91c1 2018-07-12 stsp static const struct got_error *
1654 245d91c1 2018-07-12 stsp show_blame_view(const char *path, struct got_object_id *commit_id,
1655 245d91c1 2018-07-12 stsp struct got_repository *repo)
1657 245d91c1 2018-07-12 stsp const struct got_error *err = NULL, *thread_err = NULL;
1658 f7d12f7e 2018-08-01 stsp int ch, done = 0, first_displayed_line = 1, last_displayed_line;
1659 245d91c1 2018-07-12 stsp int selected_line = first_displayed_line;
1660 245d91c1 2018-07-12 stsp int eof, blame_complete = 0;
1661 245d91c1 2018-07-12 stsp struct got_object *obj = NULL, *pobj = NULL;
1662 245d91c1 2018-07-12 stsp pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
1663 245d91c1 2018-07-12 stsp struct tog_blame blame;
1664 dbc6a6b6 2018-07-12 stsp struct got_object_id_queue blamed_commits;
1665 dbc6a6b6 2018-07-12 stsp struct got_object_qid *blamed_commit = NULL;
1666 7cc84d77 2018-08-01 stsp struct tog_view *view = NULL, *diff_view;
1668 dbc6a6b6 2018-07-12 stsp SIMPLEQ_INIT(&blamed_commits);
1670 245d91c1 2018-07-12 stsp if (pthread_mutex_init(&mutex, NULL) != 0) {
1671 245d91c1 2018-07-12 stsp err = got_error_from_errno();
1672 245d91c1 2018-07-12 stsp goto done;
1675 dbc6a6b6 2018-07-12 stsp err = got_object_qid_alloc(&blamed_commit, commit_id);
1677 245d91c1 2018-07-12 stsp goto done;
1678 dbc6a6b6 2018-07-12 stsp SIMPLEQ_INSERT_HEAD(&blamed_commits, blamed_commit, entry);
1680 842167bf 2018-08-01 stsp view = open_view(0, 0, 0, 0);
1681 7cc84d77 2018-08-01 stsp if (view == NULL) {
1682 7cc84d77 2018-08-01 stsp err = got_error_from_errno();
1683 7cc84d77 2018-08-01 stsp goto done;
1685 7cc84d77 2018-08-01 stsp show_panel(view->panel);
1686 f7d12f7e 2018-08-01 stsp last_displayed_line = view->nlines;
1688 245d91c1 2018-07-12 stsp memset(&blame, 0, sizeof(blame));
1689 7cc84d77 2018-08-01 stsp err = run_blame(&blame, &mutex, view, &blame_complete,
1690 245d91c1 2018-07-12 stsp &first_displayed_line, &last_displayed_line,
1691 dbc6a6b6 2018-07-12 stsp &selected_line, &done, path, blamed_commit->id, repo);
1693 245d91c1 2018-07-12 stsp return err;
1695 a70480e0 2018-06-23 stsp while (!done) {
1696 84451b3e 2018-07-10 stsp if (pthread_mutex_lock(&mutex) != 0) {
1697 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1698 84451b3e 2018-07-10 stsp goto done;
1700 f7d12f7e 2018-08-01 stsp err = draw_blame(view, blamed_commit->id, blame.f, path,
1701 f7d12f7e 2018-08-01 stsp blame.lines, blame.nlines, blame_complete, selected_line,
1702 f7d12f7e 2018-08-01 stsp &first_displayed_line, &last_displayed_line, &eof,
1703 f7d12f7e 2018-08-01 stsp view->nlines);
1704 84451b3e 2018-07-10 stsp if (pthread_mutex_unlock(&mutex) != 0) {
1705 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1706 84451b3e 2018-07-10 stsp goto done;
1710 a70480e0 2018-06-23 stsp nodelay(stdscr, FALSE);
1711 7cc84d77 2018-08-01 stsp ch = wgetch(view->window);
1712 a70480e0 2018-06-23 stsp nodelay(stdscr, TRUE);
1713 84451b3e 2018-07-10 stsp if (pthread_mutex_lock(&mutex) != 0) {
1714 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1715 84451b3e 2018-07-10 stsp goto done;
1717 a70480e0 2018-06-23 stsp switch (ch) {
1722 a70480e0 2018-06-23 stsp case KEY_UP:
1723 b700b5d6 2018-07-10 stsp if (selected_line > 1)
1724 b700b5d6 2018-07-10 stsp selected_line--;
1725 b700b5d6 2018-07-10 stsp else if (selected_line == 1 &&
1726 b700b5d6 2018-07-10 stsp first_displayed_line > 1)
1727 a70480e0 2018-06-23 stsp first_displayed_line--;
1729 a70480e0 2018-06-23 stsp case KEY_PPAGE:
1730 38f94530 2018-07-12 stsp case KEY_BACKSPACE:
1731 b700b5d6 2018-07-10 stsp if (first_displayed_line == 1) {
1732 b700b5d6 2018-07-10 stsp selected_line = 1;
1735 f7d12f7e 2018-08-01 stsp if (first_displayed_line > view->nlines - 2)
1736 f7d12f7e 2018-08-01 stsp first_displayed_line -=
1737 f7d12f7e 2018-08-01 stsp (view->nlines - 2);
1739 b700b5d6 2018-07-10 stsp first_displayed_line = 1;
1742 a70480e0 2018-06-23 stsp case KEY_DOWN:
1743 f7d12f7e 2018-08-01 stsp if (selected_line < view->nlines - 2 &&
1744 a026b947 2018-07-12 stsp first_displayed_line + selected_line <=
1745 a026b947 2018-07-12 stsp blame.nlines)
1746 b700b5d6 2018-07-10 stsp selected_line++;
1747 245d91c1 2018-07-12 stsp else if (last_displayed_line < blame.nlines)
1748 b700b5d6 2018-07-10 stsp first_displayed_line++;
1751 7a2921f9 2018-07-12 stsp case 'p': {
1752 245d91c1 2018-07-12 stsp struct got_object_id *id;
1753 245d91c1 2018-07-12 stsp id = get_selected_commit_id(blame.lines,
1754 245d91c1 2018-07-12 stsp first_displayed_line, selected_line);
1755 245d91c1 2018-07-12 stsp if (id == NULL || got_object_id_cmp(id,
1756 dbc6a6b6 2018-07-12 stsp blamed_commit->id) == 0)
1758 245d91c1 2018-07-12 stsp err = open_selected_commit(&pobj, &obj,
1759 245d91c1 2018-07-12 stsp blame.lines, first_displayed_line,
1760 245d91c1 2018-07-12 stsp selected_line, repo);
1763 245d91c1 2018-07-12 stsp if (pobj == NULL && obj == NULL)
1765 7a2921f9 2018-07-12 stsp if (ch == 'p' && pobj == NULL)
1768 245d91c1 2018-07-12 stsp if (pthread_mutex_unlock(&mutex) != 0) {
1769 245d91c1 2018-07-12 stsp err = got_error_from_errno();
1770 ad6a6c43 2018-07-10 stsp goto done;
1772 245d91c1 2018-07-12 stsp thread_err = stop_blame(&blame);
1774 245d91c1 2018-07-12 stsp if (pthread_mutex_lock(&mutex) != 0) {
1775 245d91c1 2018-07-12 stsp err = got_error_from_errno();
1776 245d91c1 2018-07-12 stsp goto done;
1778 245d91c1 2018-07-12 stsp if (thread_err)
1780 7a2921f9 2018-07-12 stsp id = got_object_get_id(ch == 'b' ? obj : pobj);
1781 1960a6f9 2018-07-12 stsp got_object_close(obj);
1782 1960a6f9 2018-07-12 stsp obj = NULL;
1783 1960a6f9 2018-07-12 stsp if (pobj) {
1784 1960a6f9 2018-07-12 stsp got_object_close(pobj);
1785 14437fb1 2018-07-13 stsp pobj = NULL;
1787 dbc6a6b6 2018-07-12 stsp if (id == NULL) {
1788 245d91c1 2018-07-12 stsp err = got_error_from_errno();
1791 dbc6a6b6 2018-07-12 stsp err = got_object_qid_alloc(&blamed_commit, id);
1794 dbc6a6b6 2018-07-12 stsp goto done;
1795 dbc6a6b6 2018-07-12 stsp SIMPLEQ_INSERT_HEAD(&blamed_commits,
1796 dbc6a6b6 2018-07-12 stsp blamed_commit, entry);
1797 7cc84d77 2018-08-01 stsp err = run_blame(&blame, &mutex, view,
1798 245d91c1 2018-07-12 stsp &blame_complete, &first_displayed_line,
1799 245d91c1 2018-07-12 stsp &last_displayed_line, &selected_line,
1800 dbc6a6b6 2018-07-12 stsp &done, path, blamed_commit->id, repo);
1805 dbc6a6b6 2018-07-12 stsp case 'B': {
1806 dbc6a6b6 2018-07-12 stsp struct got_object_qid *first;
1807 dbc6a6b6 2018-07-12 stsp first = SIMPLEQ_FIRST(&blamed_commits);
1808 dbc6a6b6 2018-07-12 stsp if (!got_object_id_cmp(first->id, commit_id))
1811 dbc6a6b6 2018-07-12 stsp if (pthread_mutex_unlock(&mutex) != 0) {
1812 dbc6a6b6 2018-07-12 stsp err = got_error_from_errno();
1813 dbc6a6b6 2018-07-12 stsp goto done;
1815 dbc6a6b6 2018-07-12 stsp thread_err = stop_blame(&blame);
1817 dbc6a6b6 2018-07-12 stsp if (pthread_mutex_lock(&mutex) != 0) {
1818 dbc6a6b6 2018-07-12 stsp err = got_error_from_errno();
1819 dbc6a6b6 2018-07-12 stsp goto done;
1821 dbc6a6b6 2018-07-12 stsp if (thread_err)
1823 dbc6a6b6 2018-07-12 stsp SIMPLEQ_REMOVE_HEAD(&blamed_commits, entry);
1824 dbc6a6b6 2018-07-12 stsp got_object_qid_free(blamed_commit);
1825 dbc6a6b6 2018-07-12 stsp blamed_commit = SIMPLEQ_FIRST(&blamed_commits);
1826 7cc84d77 2018-08-01 stsp err = run_blame(&blame, &mutex, view,
1827 dbc6a6b6 2018-07-12 stsp &blame_complete, &first_displayed_line,
1828 dbc6a6b6 2018-07-12 stsp &last_displayed_line, &selected_line,
1829 dbc6a6b6 2018-07-12 stsp &done, path, blamed_commit->id, repo);
1834 245d91c1 2018-07-12 stsp case KEY_ENTER:
1835 245d91c1 2018-07-12 stsp case '\r':
1836 245d91c1 2018-07-12 stsp err = open_selected_commit(&pobj, &obj,
1837 245d91c1 2018-07-12 stsp blame.lines, first_displayed_line,
1838 245d91c1 2018-07-12 stsp selected_line, repo);
1841 b880a918 2018-07-10 stsp if (pobj == NULL && obj == NULL)
1843 842167bf 2018-08-01 stsp diff_view = open_view(0, 0, 0, 0);
1844 ea5e7bb5 2018-08-01 stsp if (diff_view == NULL) {
1845 ea5e7bb5 2018-08-01 stsp err = got_error_from_errno();
1848 ea5e7bb5 2018-08-01 stsp err = show_diff_view(diff_view, pobj, obj, repo);
1849 ea5e7bb5 2018-08-01 stsp close_view(diff_view);
1850 b880a918 2018-07-10 stsp if (pobj) {
1851 b880a918 2018-07-10 stsp got_object_close(pobj);
1852 b880a918 2018-07-10 stsp pobj = NULL;
1854 b880a918 2018-07-10 stsp got_object_close(obj);
1855 b880a918 2018-07-10 stsp obj = NULL;
1856 7cc84d77 2018-08-01 stsp show_panel(view->panel);
1860 a70480e0 2018-06-23 stsp case KEY_NPAGE:
1862 245d91c1 2018-07-12 stsp if (last_displayed_line >= blame.nlines &&
1863 f7d12f7e 2018-08-01 stsp selected_line < view->nlines - 2) {
1864 d2dfcfbf 2018-07-12 stsp selected_line = MIN(blame.nlines,
1865 f7d12f7e 2018-08-01 stsp view->nlines - 2);
1868 f7d12f7e 2018-08-01 stsp if (last_displayed_line + view->nlines - 2 <=
1869 245d91c1 2018-07-12 stsp blame.nlines)
1870 f7d12f7e 2018-08-01 stsp first_displayed_line +=
1871 f7d12f7e 2018-08-01 stsp view->nlines - 2;
1873 b700b5d6 2018-07-10 stsp first_displayed_line =
1874 f7d12f7e 2018-08-01 stsp blame.nlines - (view->nlines - 3);
1876 7e5c1fe1 2018-08-01 stsp case KEY_RESIZE:
1877 a41d2007 2018-08-01 stsp err = view_resize(view);
1880 7e5c1fe1 2018-08-01 stsp if (selected_line > view->nlines - 2) {
1881 7e5c1fe1 2018-08-01 stsp selected_line = MIN(blame.nlines,
1882 7e5c1fe1 2018-08-01 stsp view->nlines - 2);
1888 245d91c1 2018-07-12 stsp if (pthread_mutex_unlock(&mutex) != 0)
1889 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1890 245d91c1 2018-07-12 stsp if (err || thread_err)
1895 b880a918 2018-07-10 stsp got_object_close(pobj);
1896 c9beca56 2018-07-22 stsp if (blame.thread)
1897 245d91c1 2018-07-12 stsp thread_err = stop_blame(&blame);
1899 7cc84d77 2018-08-01 stsp close_view(view);
1900 dbc6a6b6 2018-07-12 stsp while (!SIMPLEQ_EMPTY(&blamed_commits)) {
1901 dbc6a6b6 2018-07-12 stsp blamed_commit = SIMPLEQ_FIRST(&blamed_commits);
1902 dbc6a6b6 2018-07-12 stsp SIMPLEQ_REMOVE_HEAD(&blamed_commits, entry);
1903 dbc6a6b6 2018-07-12 stsp got_object_qid_free(blamed_commit);
1905 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
1908 a70480e0 2018-06-23 stsp static const struct got_error *
1909 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
1911 a70480e0 2018-06-23 stsp const struct got_error *error;
1912 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
1913 a70480e0 2018-06-23 stsp char *repo_path = NULL;
1914 a70480e0 2018-06-23 stsp char *path = NULL;
1915 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
1916 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
1919 a70480e0 2018-06-23 stsp #ifndef PROFILE
1920 a70480e0 2018-06-23 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1921 a70480e0 2018-06-23 stsp err(1, "pledge");
1924 a70480e0 2018-06-23 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
1925 a70480e0 2018-06-23 stsp switch (ch) {
1927 a70480e0 2018-06-23 stsp commit_id_str = optarg;
1931 a70480e0 2018-06-23 stsp /* NOTREACHED */
1935 a70480e0 2018-06-23 stsp argc -= optind;
1936 a70480e0 2018-06-23 stsp argv += optind;
1938 a70480e0 2018-06-23 stsp if (argc == 0) {
1939 a70480e0 2018-06-23 stsp usage_blame();
1940 a70480e0 2018-06-23 stsp } else if (argc == 1) {
1941 a70480e0 2018-06-23 stsp repo_path = getcwd(NULL, 0);
1942 a70480e0 2018-06-23 stsp if (repo_path == NULL)
1943 a70480e0 2018-06-23 stsp return got_error_from_errno();
1944 a70480e0 2018-06-23 stsp path = argv[0];
1945 a70480e0 2018-06-23 stsp } else if (argc == 2) {
1946 a70480e0 2018-06-23 stsp repo_path = realpath(argv[0], NULL);
1947 a70480e0 2018-06-23 stsp if (repo_path == NULL)
1948 a70480e0 2018-06-23 stsp return got_error_from_errno();
1949 a70480e0 2018-06-23 stsp path = argv[1];
1951 a70480e0 2018-06-23 stsp usage_blame();
1953 a70480e0 2018-06-23 stsp error = got_repo_open(&repo, repo_path);
1954 a70480e0 2018-06-23 stsp free(repo_path);
1955 a70480e0 2018-06-23 stsp if (error != NULL)
1956 66b4983c 2018-06-23 stsp return error;
1958 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
1959 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
1960 a70480e0 2018-06-23 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1961 a70480e0 2018-06-23 stsp if (error != NULL)
1962 66b4983c 2018-06-23 stsp goto done;
1963 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
1964 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
1966 a70480e0 2018-06-23 stsp struct got_object *obj;
1967 a70480e0 2018-06-23 stsp error = got_object_open_by_id_str(&obj, repo, commit_id_str);
1968 a70480e0 2018-06-23 stsp if (error != NULL)
1969 66b4983c 2018-06-23 stsp goto done;
1970 a70480e0 2018-06-23 stsp commit_id = got_object_get_id(obj);
1971 a19e88aa 2018-06-23 stsp if (commit_id == NULL)
1972 66b4983c 2018-06-23 stsp error = got_error_from_errno();
1973 a19e88aa 2018-06-23 stsp got_object_close(obj);
1975 a19e88aa 2018-06-23 stsp if (error != NULL)
1976 a19e88aa 2018-06-23 stsp goto done;
1978 a70480e0 2018-06-23 stsp error = show_blame_view(path, commit_id, repo);
1980 a70480e0 2018-06-23 stsp free(commit_id);
1982 a70480e0 2018-06-23 stsp got_repo_close(repo);
1983 a70480e0 2018-06-23 stsp return error;
1986 ffd1d5e5 2018-06-23 stsp static const struct got_error *
1987 f7d12f7e 2018-08-01 stsp draw_tree_entries(struct tog_view *view,
1988 f7d12f7e 2018-08-01 stsp struct got_tree_entry **first_displayed_entry,
1989 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **last_displayed_entry,
1990 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **selected_entry, int *ndisplayed,
1991 f7d12f7e 2018-08-01 stsp const char *label, int show_ids, const char *parent_path,
1992 ce52c690 2018-06-23 stsp const struct got_tree_entries *entries, int selected, int limit, int isroot)
1994 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
1995 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
1996 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
1997 ffd1d5e5 2018-06-23 stsp int width, n;
1999 ffd1d5e5 2018-06-23 stsp *ndisplayed = 0;
2001 f7d12f7e 2018-08-01 stsp werase(view->window);
2003 ffd1d5e5 2018-06-23 stsp if (limit == 0)
2004 ffd1d5e5 2018-06-23 stsp return NULL;
2006 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, label, view->ncols);
2008 ffd1d5e5 2018-06-23 stsp return err;
2009 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2010 2550e4c3 2018-07-13 stsp free(wline);
2011 2550e4c3 2018-07-13 stsp wline = NULL;
2012 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2013 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2014 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2015 ffd1d5e5 2018-06-23 stsp return NULL;
2016 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, parent_path, view->ncols);
2018 ce52c690 2018-06-23 stsp return err;
2019 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2020 2550e4c3 2018-07-13 stsp free(wline);
2021 2550e4c3 2018-07-13 stsp wline = NULL;
2022 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2023 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2024 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2025 ffd1d5e5 2018-06-23 stsp return NULL;
2026 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2027 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
2028 a1eca9bb 2018-06-23 stsp return NULL;
2030 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
2031 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL) {
2032 ffd1d5e5 2018-06-23 stsp if (selected == 0) {
2033 f7d12f7e 2018-08-01 stsp wstandout(view->window);
2034 ffd1d5e5 2018-06-23 stsp *selected_entry = NULL;
2036 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
2037 ffd1d5e5 2018-06-23 stsp if (selected == 0)
2038 f7d12f7e 2018-08-01 stsp wstandend(view->window);
2039 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
2040 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2041 ffd1d5e5 2018-06-23 stsp return NULL;
2045 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry)
2046 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
2049 ffd1d5e5 2018-06-23 stsp while (te) {
2050 1d13200f 2018-07-12 stsp char *line = NULL, *id_str = NULL;
2052 1d13200f 2018-07-12 stsp if (show_ids) {
2053 1d13200f 2018-07-12 stsp err = got_object_id_str(&id_str, te->id);
2055 1d13200f 2018-07-12 stsp return got_error_from_errno();
2057 1d13200f 2018-07-12 stsp if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
2058 1d13200f 2018-07-12 stsp te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
2059 1d13200f 2018-07-12 stsp free(id_str);
2060 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
2062 1d13200f 2018-07-12 stsp free(id_str);
2063 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
2064 ffd1d5e5 2018-06-23 stsp if (err) {
2065 ffd1d5e5 2018-06-23 stsp free(line);
2068 ffd1d5e5 2018-06-23 stsp if (n == selected) {
2069 f7d12f7e 2018-08-01 stsp wstandout(view->window);
2070 ffd1d5e5 2018-06-23 stsp *selected_entry = te;
2072 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2073 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2074 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2075 ffd1d5e5 2018-06-23 stsp if (n == selected)
2076 f7d12f7e 2018-08-01 stsp wstandend(view->window);
2077 ffd1d5e5 2018-06-23 stsp free(line);
2078 2550e4c3 2018-07-13 stsp free(wline);
2079 2550e4c3 2018-07-13 stsp wline = NULL;
2081 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
2082 ffd1d5e5 2018-06-23 stsp *last_displayed_entry = te;
2083 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2085 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
2088 ffd1d5e5 2018-06-23 stsp return err;
2091 ffd1d5e5 2018-06-23 stsp static void
2092 ffd1d5e5 2018-06-23 stsp tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
2093 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries, int isroot)
2095 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te, *prev;
2098 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL)
2101 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
2102 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == te) {
2103 ffd1d5e5 2018-06-23 stsp if (!isroot)
2104 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
2108 ffd1d5e5 2018-06-23 stsp /* XXX this is stupid... switch to TAILQ? */
2109 ffd1d5e5 2018-06-23 stsp for (i = 0; i < maxscroll; i++) {
2110 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry) {
2111 ffd1d5e5 2018-06-23 stsp prev = te;
2112 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
2114 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = prev;
2115 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
2117 ffd1d5e5 2018-06-23 stsp if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
2118 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
2121 ffd1d5e5 2018-06-23 stsp static void
2122 ffd1d5e5 2018-06-23 stsp tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
2123 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *last_displayed_entry,
2124 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries)
2126 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *next;
2127 ffd1d5e5 2018-06-23 stsp int n = 0;
2129 ffd1d5e5 2018-06-23 stsp if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
2132 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry)
2133 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
2135 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_FIRST(&entries->head);
2136 ffd1d5e5 2018-06-23 stsp while (next) {
2137 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = next;
2138 ffd1d5e5 2018-06-23 stsp if (++n >= maxscroll)
2140 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(next, entry);
2144 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree {
2145 d9765a41 2018-06-23 stsp TAILQ_ENTRY(tog_parent_tree) entry;
2146 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree;
2147 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *first_displayed_entry;
2148 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *selected_entry;
2149 ffd1d5e5 2018-06-23 stsp int selected;
2152 d9765a41 2018-06-23 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
2154 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2155 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
2156 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
2158 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
2159 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
2160 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
2162 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
2163 ffd1d5e5 2018-06-23 stsp len += strlen(pt->selected_entry->name) + 1 /* slash */;
2165 ce52c690 2018-06-23 stsp len += strlen(te->name);
2167 ce52c690 2018-06-23 stsp *path = calloc(1, len);
2168 ffd1d5e5 2018-06-23 stsp if (path == NULL)
2169 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
2171 ce52c690 2018-06-23 stsp (*path)[0] = '/';
2172 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
2173 d9765a41 2018-06-23 stsp while (pt) {
2174 ce52c690 2018-06-23 stsp if (strlcat(*path, pt->selected_entry->name, len) >= len) {
2175 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
2176 cb2ebc8a 2018-06-23 stsp goto done;
2178 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
2179 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
2180 cb2ebc8a 2018-06-23 stsp goto done;
2182 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
2185 ce52c690 2018-06-23 stsp if (strlcat(*path, te->name, len) >= len) {
2186 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
2187 ce52c690 2018-06-23 stsp goto done;
2191 ce52c690 2018-06-23 stsp if (err) {
2192 ce52c690 2018-06-23 stsp free(*path);
2193 ce52c690 2018-06-23 stsp *path = NULL;
2195 ce52c690 2018-06-23 stsp return err;
2198 ce52c690 2018-06-23 stsp static const struct got_error *
2199 ce52c690 2018-06-23 stsp blame_tree_entry(struct got_tree_entry *te, struct tog_parent_trees *parents,
2200 ce52c690 2018-06-23 stsp struct got_object_id *commit_id, struct got_repository *repo)
2202 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
2203 ce52c690 2018-06-23 stsp char *path;
2205 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
2207 ce52c690 2018-06-23 stsp return err;
2209 cb2ebc8a 2018-06-23 stsp err = show_blame_view(path, commit_id, repo);
2210 69efd4c4 2018-07-18 stsp free(path);
2211 69efd4c4 2018-07-18 stsp return err;
2214 69efd4c4 2018-07-18 stsp static const struct got_error *
2215 69efd4c4 2018-07-18 stsp log_tree_entry(struct got_tree_entry *te, struct tog_parent_trees *parents,
2216 69efd4c4 2018-07-18 stsp struct got_object_id *commit_id, struct got_repository *repo)
2218 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
2219 69efd4c4 2018-07-18 stsp char *path;
2221 69efd4c4 2018-07-18 stsp err = tree_entry_path(&path, parents, te);
2223 69efd4c4 2018-07-18 stsp return err;
2225 69efd4c4 2018-07-18 stsp err = show_log_view(commit_id, repo, path);
2226 cb2ebc8a 2018-06-23 stsp free(path);
2227 cb2ebc8a 2018-06-23 stsp return err;
2230 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2231 ffd1d5e5 2018-06-23 stsp show_tree_view(struct got_tree_object *root, struct got_object_id *commit_id,
2232 ffd1d5e5 2018-06-23 stsp struct got_repository *repo)
2234 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
2235 1d13200f 2018-07-12 stsp int ch, done = 0, selected = 0, show_ids = 0;
2236 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree = root;
2237 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries;
2238 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *first_displayed_entry = NULL;
2239 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *last_displayed_entry = NULL;
2240 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *selected_entry = NULL;
2241 ffd1d5e5 2018-06-23 stsp char *commit_id_str = NULL, *tree_label = NULL;
2242 ffd1d5e5 2018-06-23 stsp int nentries, ndisplayed;
2243 ffd1d5e5 2018-06-23 stsp struct tog_parent_trees parents;
2244 cc3c9aac 2018-08-01 stsp struct tog_view *view = NULL;
2246 d9765a41 2018-06-23 stsp TAILQ_INIT(&parents);
2248 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
2249 ffd1d5e5 2018-06-23 stsp if (err != NULL)
2250 ffd1d5e5 2018-06-23 stsp goto done;
2252 decd3bd1 2018-07-12 stsp if (asprintf(&tree_label, "commit: %s", commit_id_str) == -1) {
2253 ffd1d5e5 2018-06-23 stsp err = got_error_from_errno();
2254 ffd1d5e5 2018-06-23 stsp goto done;
2257 842167bf 2018-08-01 stsp view = open_view(0, 0, 0, 0);
2258 cc3c9aac 2018-08-01 stsp if (view == NULL) {
2259 cc3c9aac 2018-08-01 stsp err = got_error_from_errno();
2260 cc3c9aac 2018-08-01 stsp goto done;
2262 cc3c9aac 2018-08-01 stsp show_panel(view->panel);
2264 ffd1d5e5 2018-06-23 stsp entries = got_object_tree_get_entries(root);
2265 ffd1d5e5 2018-06-23 stsp first_displayed_entry = SIMPLEQ_FIRST(&entries->head);
2266 ffd1d5e5 2018-06-23 stsp while (!done) {
2267 ce52c690 2018-06-23 stsp char *parent_path;
2268 ffd1d5e5 2018-06-23 stsp entries = got_object_tree_get_entries(tree);
2269 ffd1d5e5 2018-06-23 stsp nentries = entries->nentries;
2270 ffd1d5e5 2018-06-23 stsp if (tree != root)
2271 ffd1d5e5 2018-06-23 stsp nentries++; /* '..' directory */
2273 ce52c690 2018-06-23 stsp err = tree_entry_path(&parent_path, &parents, NULL);
2275 ce52c690 2018-06-23 stsp goto done;
2277 f7d12f7e 2018-08-01 stsp err = draw_tree_entries(view, &first_displayed_entry,
2278 ffd1d5e5 2018-06-23 stsp &last_displayed_entry, &selected_entry, &ndisplayed,
2279 f7d12f7e 2018-08-01 stsp tree_label, show_ids, parent_path, entries, selected,
2280 f7d12f7e 2018-08-01 stsp view->nlines, tree == root);
2281 ce52c690 2018-06-23 stsp free(parent_path);
2285 ffd1d5e5 2018-06-23 stsp nodelay(stdscr, FALSE);
2286 cc3c9aac 2018-08-01 stsp ch = wgetch(view->window);
2287 ffd1d5e5 2018-06-23 stsp nodelay(stdscr, TRUE);
2288 ffd1d5e5 2018-06-23 stsp switch (ch) {
2293 1d13200f 2018-07-12 stsp show_ids = !show_ids;
2296 69efd4c4 2018-07-18 stsp if (selected_entry) {
2297 69efd4c4 2018-07-18 stsp err = log_tree_entry(selected_entry,
2298 69efd4c4 2018-07-18 stsp &parents, commit_id, repo);
2300 69efd4c4 2018-07-18 stsp goto done;
2304 ffd1d5e5 2018-06-23 stsp case KEY_UP:
2305 ffd1d5e5 2018-06-23 stsp if (selected > 0)
2306 ffd1d5e5 2018-06-23 stsp selected--;
2307 ffd1d5e5 2018-06-23 stsp if (selected > 0)
2309 ffd1d5e5 2018-06-23 stsp tree_scroll_up(&first_displayed_entry, 1,
2310 ffd1d5e5 2018-06-23 stsp entries, tree == root);
2312 ffd1d5e5 2018-06-23 stsp case KEY_PPAGE:
2313 ffd1d5e5 2018-06-23 stsp if (SIMPLEQ_FIRST(&entries->head) ==
2314 ffd1d5e5 2018-06-23 stsp first_displayed_entry) {
2315 cf8f1261 2018-06-23 stsp if (tree != root)
2316 cf8f1261 2018-06-23 stsp first_displayed_entry = NULL;
2317 ffd1d5e5 2018-06-23 stsp selected = 0;
2320 f7d12f7e 2018-08-01 stsp tree_scroll_up(&first_displayed_entry,
2321 f7d12f7e 2018-08-01 stsp view->nlines, entries, tree == root);
2324 ffd1d5e5 2018-06-23 stsp case KEY_DOWN:
2325 ffd1d5e5 2018-06-23 stsp if (selected < ndisplayed - 1) {
2326 ffd1d5e5 2018-06-23 stsp selected++;
2329 ffd1d5e5 2018-06-23 stsp tree_scroll_down(&first_displayed_entry, 1,
2330 ffd1d5e5 2018-06-23 stsp last_displayed_entry, entries);
2332 ffd1d5e5 2018-06-23 stsp case KEY_NPAGE:
2333 f7d12f7e 2018-08-01 stsp tree_scroll_down(&first_displayed_entry,
2334 f7d12f7e 2018-08-01 stsp view->nlines, last_displayed_entry,
2336 ffd1d5e5 2018-06-23 stsp if (SIMPLEQ_NEXT(last_displayed_entry, entry))
2338 ffd1d5e5 2018-06-23 stsp /* can't scroll any further; move cursor down */
2339 ffd1d5e5 2018-06-23 stsp if (selected < ndisplayed - 1)
2340 ffd1d5e5 2018-06-23 stsp selected = ndisplayed - 1;
2342 ffd1d5e5 2018-06-23 stsp case KEY_ENTER:
2343 ffd1d5e5 2018-06-23 stsp case '\r':
2344 ffd1d5e5 2018-06-23 stsp if (selected_entry == NULL) {
2345 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *parent;
2346 ffd1d5e5 2018-06-23 stsp case KEY_BACKSPACE:
2347 ffd1d5e5 2018-06-23 stsp /* user selected '..' */
2348 ffd1d5e5 2018-06-23 stsp if (tree == root)
2350 d9765a41 2018-06-23 stsp parent = TAILQ_FIRST(&parents);
2351 d9765a41 2018-06-23 stsp TAILQ_REMOVE(&parents, parent, entry);
2352 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
2353 ffd1d5e5 2018-06-23 stsp tree = parent->tree;
2354 ffd1d5e5 2018-06-23 stsp first_displayed_entry =
2355 ffd1d5e5 2018-06-23 stsp parent->first_displayed_entry;
2356 ffd1d5e5 2018-06-23 stsp selected_entry = parent->selected_entry;
2357 ffd1d5e5 2018-06-23 stsp selected = parent->selected;
2358 ffd1d5e5 2018-06-23 stsp free(parent);
2359 ffd1d5e5 2018-06-23 stsp } else if (S_ISDIR(selected_entry->mode)) {
2360 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *parent;
2361 ffd1d5e5 2018-06-23 stsp struct got_tree_object *child;
2362 ffd1d5e5 2018-06-23 stsp err = got_object_open_as_tree(
2363 ffd1d5e5 2018-06-23 stsp &child, repo, selected_entry->id);
2365 ffd1d5e5 2018-06-23 stsp goto done;
2366 ffd1d5e5 2018-06-23 stsp parent = calloc(1, sizeof(*parent));
2367 ffd1d5e5 2018-06-23 stsp if (parent == NULL) {
2368 ffd1d5e5 2018-06-23 stsp err = got_error_from_errno();
2369 ffd1d5e5 2018-06-23 stsp goto done;
2371 ffd1d5e5 2018-06-23 stsp parent->tree = tree;
2372 ffd1d5e5 2018-06-23 stsp parent->first_displayed_entry =
2373 ffd1d5e5 2018-06-23 stsp first_displayed_entry;
2374 ffd1d5e5 2018-06-23 stsp parent->selected_entry = selected_entry;
2375 ffd1d5e5 2018-06-23 stsp parent->selected = selected;
2376 d9765a41 2018-06-23 stsp TAILQ_INSERT_HEAD(&parents, parent,
2378 ffd1d5e5 2018-06-23 stsp tree = child;
2379 ffd1d5e5 2018-06-23 stsp selected = 0;
2380 ffd1d5e5 2018-06-23 stsp first_displayed_entry = NULL;
2381 ffd1d5e5 2018-06-23 stsp } else if (S_ISREG(selected_entry->mode)) {
2382 ffd1d5e5 2018-06-23 stsp err = blame_tree_entry(selected_entry,
2383 ffd1d5e5 2018-06-23 stsp &parents, commit_id, repo);
2385 ffd1d5e5 2018-06-23 stsp goto done;
2388 ffd1d5e5 2018-06-23 stsp case KEY_RESIZE:
2389 a41d2007 2018-08-01 stsp err = view_resize(view);
2391 a41d2007 2018-08-01 stsp goto done;
2392 f7d12f7e 2018-08-01 stsp if (selected > view->nlines)
2393 ffd1d5e5 2018-06-23 stsp selected = ndisplayed - 1;
2401 cc3c9aac 2018-08-01 stsp close_view(view);
2402 ffd1d5e5 2018-06-23 stsp free(tree_label);
2403 ffd1d5e5 2018-06-23 stsp free(commit_id_str);
2404 d9765a41 2018-06-23 stsp while (!TAILQ_EMPTY(&parents)) {
2405 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *parent;
2406 d9765a41 2018-06-23 stsp parent = TAILQ_FIRST(&parents);
2407 d9765a41 2018-06-23 stsp TAILQ_REMOVE(&parents, parent, entry);
2408 ffd1d5e5 2018-06-23 stsp free(parent);
2411 ffd1d5e5 2018-06-23 stsp if (tree != root)
2412 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
2413 ffd1d5e5 2018-06-23 stsp return err;
2416 ffd1d5e5 2018-06-23 stsp __dead static void
2417 ffd1d5e5 2018-06-23 stsp usage_tree(void)
2420 ffd1d5e5 2018-06-23 stsp fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
2421 ffd1d5e5 2018-06-23 stsp getprogname());
2425 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2426 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
2428 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
2429 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
2430 ffd1d5e5 2018-06-23 stsp char *repo_path = NULL;
2431 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
2432 ffd1d5e5 2018-06-23 stsp char *commit_id_arg = NULL;
2433 ffd1d5e5 2018-06-23 stsp struct got_commit_object *commit = NULL;
2434 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree = NULL;
2437 ffd1d5e5 2018-06-23 stsp #ifndef PROFILE
2438 ffd1d5e5 2018-06-23 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
2439 ffd1d5e5 2018-06-23 stsp err(1, "pledge");
2442 ffd1d5e5 2018-06-23 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
2443 ffd1d5e5 2018-06-23 stsp switch (ch) {
2445 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
2449 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
2453 ffd1d5e5 2018-06-23 stsp argc -= optind;
2454 ffd1d5e5 2018-06-23 stsp argv += optind;
2456 ffd1d5e5 2018-06-23 stsp if (argc == 0) {
2457 ffd1d5e5 2018-06-23 stsp repo_path = getcwd(NULL, 0);
2458 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
2459 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
2460 ffd1d5e5 2018-06-23 stsp } else if (argc == 1) {
2461 ffd1d5e5 2018-06-23 stsp repo_path = realpath(argv[0], NULL);
2462 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
2463 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
2465 ffd1d5e5 2018-06-23 stsp usage_log();
2467 ffd1d5e5 2018-06-23 stsp error = got_repo_open(&repo, repo_path);
2468 ffd1d5e5 2018-06-23 stsp free(repo_path);
2469 ffd1d5e5 2018-06-23 stsp if (error != NULL)
2470 ffd1d5e5 2018-06-23 stsp return error;
2472 ffd1d5e5 2018-06-23 stsp if (commit_id_arg == NULL) {
2473 ffd1d5e5 2018-06-23 stsp error = get_head_commit_id(&commit_id, repo);
2474 ffd1d5e5 2018-06-23 stsp if (error != NULL)
2475 ffd1d5e5 2018-06-23 stsp goto done;
2477 ffd1d5e5 2018-06-23 stsp struct got_object *obj;
2478 ffd1d5e5 2018-06-23 stsp error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
2479 ffd1d5e5 2018-06-23 stsp if (error == NULL) {
2480 ffd1d5e5 2018-06-23 stsp commit_id = got_object_get_id(obj);
2481 ffd1d5e5 2018-06-23 stsp if (commit_id == NULL)
2482 ffd1d5e5 2018-06-23 stsp error = got_error_from_errno();
2485 ffd1d5e5 2018-06-23 stsp if (error != NULL)
2486 ffd1d5e5 2018-06-23 stsp goto done;
2488 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
2489 ffd1d5e5 2018-06-23 stsp if (error != NULL)
2490 ffd1d5e5 2018-06-23 stsp goto done;
2492 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_tree(&tree, repo, commit->tree_id);
2493 ffd1d5e5 2018-06-23 stsp if (error != NULL)
2494 ffd1d5e5 2018-06-23 stsp goto done;
2496 ffd1d5e5 2018-06-23 stsp error = show_tree_view(tree, commit_id, repo);
2498 ffd1d5e5 2018-06-23 stsp free(commit_id);
2499 ffd1d5e5 2018-06-23 stsp if (commit)
2500 ffd1d5e5 2018-06-23 stsp got_object_commit_close(commit);
2502 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
2504 ffd1d5e5 2018-06-23 stsp got_repo_close(repo);
2505 ffd1d5e5 2018-06-23 stsp return error;
2507 5c5136c5 2018-05-20 stsp static void
2508 9f7d7167 2018-04-29 stsp init_curses(void)
2510 9f7d7167 2018-04-29 stsp initscr();
2514 9f7d7167 2018-04-29 stsp intrflush(stdscr, FALSE);
2515 9f7d7167 2018-04-29 stsp keypad(stdscr, TRUE);
2516 1f475ad8 2018-05-10 stsp curs_set(0);
2519 4ed7e80c 2018-05-20 stsp __dead static void
2520 9f7d7167 2018-04-29 stsp usage(void)
2524 c2301be8 2018-04-30 stsp fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
2525 9f7d7167 2018-04-29 stsp "Available commands:\n", getprogname());
2526 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
2527 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = &tog_commands[i];
2528 c2301be8 2018-04-30 stsp fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
2533 c2301be8 2018-04-30 stsp static char **
2534 c2301be8 2018-04-30 stsp make_argv(const char *arg0, const char *arg1)
2536 c2301be8 2018-04-30 stsp char **argv;
2537 c2301be8 2018-04-30 stsp int argc = (arg1 == NULL ? 1 : 2);
2539 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
2540 c2301be8 2018-04-30 stsp if (argv == NULL)
2541 c2301be8 2018-04-30 stsp err(1, "calloc");
2542 c2301be8 2018-04-30 stsp argv[0] = strdup(arg0);
2543 c2301be8 2018-04-30 stsp if (argv[0] == NULL)
2544 c2301be8 2018-04-30 stsp err(1, "calloc");
2545 c2301be8 2018-04-30 stsp if (arg1) {
2546 c2301be8 2018-04-30 stsp argv[1] = strdup(arg1);
2547 c2301be8 2018-04-30 stsp if (argv[1] == NULL)
2548 c2301be8 2018-04-30 stsp err(1, "calloc");
2551 c2301be8 2018-04-30 stsp return argv;
2555 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
2557 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
2558 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
2559 9f7d7167 2018-04-29 stsp int ch, hflag = 0;
2560 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
2562 9f7d7167 2018-04-29 stsp setlocale(LC_ALL, "");
2564 9f7d7167 2018-04-29 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
2565 9f7d7167 2018-04-29 stsp switch (ch) {
2567 9f7d7167 2018-04-29 stsp hflag = 1;
2571 9f7d7167 2018-04-29 stsp /* NOTREACHED */
2575 9f7d7167 2018-04-29 stsp argc -= optind;
2576 9f7d7167 2018-04-29 stsp argv += optind;
2577 9f7d7167 2018-04-29 stsp optind = 0;
2578 c2301be8 2018-04-30 stsp optreset = 1;
2580 c2301be8 2018-04-30 stsp if (argc == 0) {
2581 f29d3e89 2018-06-23 stsp if (hflag)
2583 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
2584 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
2585 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, NULL);
2590 c2301be8 2018-04-30 stsp /* Did the user specific a command? */
2591 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
2592 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
2593 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
2594 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
2595 9f7d7167 2018-04-29 stsp if (hflag)
2596 9f7d7167 2018-04-29 stsp tog_commands[i].cmd_usage();
2600 9f7d7167 2018-04-29 stsp if (cmd == NULL) {
2601 c2301be8 2018-04-30 stsp /* Did the user specify a repository? */
2602 c2301be8 2018-04-30 stsp char *repo_path = realpath(argv[0], NULL);
2603 c2301be8 2018-04-30 stsp if (repo_path) {
2604 c2301be8 2018-04-30 stsp struct got_repository *repo;
2605 c2301be8 2018-04-30 stsp error = got_repo_open(&repo, repo_path);
2606 c2301be8 2018-04-30 stsp if (error == NULL)
2607 c2301be8 2018-04-30 stsp got_repo_close(repo);
2609 ad7de8d9 2018-04-30 stsp error = got_error_from_errno();
2610 c2301be8 2018-04-30 stsp if (error) {
2611 f29d3e89 2018-06-23 stsp if (hflag) {
2612 f29d3e89 2018-06-23 stsp fprintf(stderr, "%s: '%s' is not a "
2613 f29d3e89 2018-06-23 stsp "known command\n", getprogname(),
2617 ad7de8d9 2018-04-30 stsp fprintf(stderr, "%s: '%s' is neither a known "
2618 ad7de8d9 2018-04-30 stsp "command nor a path to a repository\n",
2619 c2301be8 2018-04-30 stsp getprogname(), argv[0]);
2620 ad7de8d9 2018-04-30 stsp free(repo_path);
2623 c2301be8 2018-04-30 stsp cmd = &tog_commands[0];
2624 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, repo_path);
2626 c2301be8 2018-04-30 stsp free(repo_path);
2630 5c5136c5 2018-05-20 stsp init_curses();
2632 c2301be8 2018-04-30 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
2633 9f7d7167 2018-04-29 stsp if (error)
2634 9f7d7167 2018-04-29 stsp goto done;
2637 c2301be8 2018-04-30 stsp free(cmd_argv);
2638 9f7d7167 2018-04-29 stsp if (error)
2639 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);