2 9f7d7167 2018-04-29 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
4 9f7d7167 2018-04-29 stsp * Permission to use, copy, modify, and distribute this software for any
5 9f7d7167 2018-04-29 stsp * purpose with or without fee is hereby granted, provided that the above
6 9f7d7167 2018-04-29 stsp * copyright notice and this permission notice appear in all copies.
8 9f7d7167 2018-04-29 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 9f7d7167 2018-04-29 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 9f7d7167 2018-04-29 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 9f7d7167 2018-04-29 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 9f7d7167 2018-04-29 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 9f7d7167 2018-04-29 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 9f7d7167 2018-04-29 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 80ddbec8 2018-04-29 stsp #include <sys/queue.h>
18 ffd1d5e5 2018-06-23 stsp #include <sys/stat.h>
20 31120ada 2018-04-30 stsp #include <errno.h>
21 61e69b96 2018-05-20 stsp #define _XOPEN_SOURCE_EXTENDED
22 9f7d7167 2018-04-29 stsp #include <curses.h>
23 61e69b96 2018-05-20 stsp #undef _XOPEN_SOURCE_EXTENDED
24 9f7d7167 2018-04-29 stsp #include <panel.h>
25 9f7d7167 2018-04-29 stsp #include <locale.h>
26 9f7d7167 2018-04-29 stsp #include <stdlib.h>
27 26ed57b2 2018-05-19 stsp #include <stdio.h>
28 9f7d7167 2018-04-29 stsp #include <getopt.h>
29 9f7d7167 2018-04-29 stsp #include <string.h>
30 9f7d7167 2018-04-29 stsp #include <err.h>
31 80ddbec8 2018-04-29 stsp #include <unistd.h>
32 26ed57b2 2018-05-19 stsp #include <util.h>
33 26ed57b2 2018-05-19 stsp #include <limits.h>
34 61e69b96 2018-05-20 stsp #include <wchar.h>
35 788c352e 2018-06-16 stsp #include <time.h>
36 84451b3e 2018-07-10 stsp #include <pthread.h>
38 9f7d7167 2018-04-29 stsp #include "got_error.h"
39 80ddbec8 2018-04-29 stsp #include "got_object.h"
40 80ddbec8 2018-04-29 stsp #include "got_reference.h"
41 80ddbec8 2018-04-29 stsp #include "got_repository.h"
42 80ddbec8 2018-04-29 stsp #include "got_diff.h"
43 511a516b 2018-05-19 stsp #include "got_opentemp.h"
44 9ba79e04 2018-06-11 stsp #include "got_commit_graph.h"
45 00dfcb92 2018-06-11 stsp #include "got_utf8.h"
46 a70480e0 2018-06-23 stsp #include "got_blame.h"
49 881b2d3e 2018-04-30 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
52 9f7d7167 2018-04-29 stsp #ifndef nitems
53 9f7d7167 2018-04-29 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
56 9f7d7167 2018-04-29 stsp struct tog_cmd {
57 c2301be8 2018-04-30 stsp const char *name;
58 9f7d7167 2018-04-29 stsp const struct got_error *(*cmd_main)(int, char *[]);
59 9f7d7167 2018-04-29 stsp void (*cmd_usage)(void);
60 c2301be8 2018-04-30 stsp const char *descr;
63 4ed7e80c 2018-05-20 stsp __dead static void usage(void);
64 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
65 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
66 4ed7e80c 2018-05-20 stsp __dead static void usage_blame(void);
67 ffd1d5e5 2018-06-23 stsp __dead static void usage_tree(void);
69 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
70 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
71 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_blame(int, char *[]);
72 ffd1d5e5 2018-06-23 stsp static const struct got_error* cmd_tree(int, char *[]);
74 4ed7e80c 2018-05-20 stsp static struct tog_cmd tog_commands[] = {
75 cbb6b58a 2018-05-20 stsp { "log", cmd_log, usage_log,
76 9f7d7167 2018-04-29 stsp "show repository history" },
77 cbb6b58a 2018-05-20 stsp { "diff", cmd_diff, usage_diff,
78 9f7d7167 2018-04-29 stsp "compare files and directories" },
79 cbb6b58a 2018-05-20 stsp { "blame", cmd_blame, usage_blame,
80 9f7d7167 2018-04-29 stsp "show line-by-line file history" },
81 ffd1d5e5 2018-06-23 stsp { "tree", cmd_tree, usage_tree,
82 ffd1d5e5 2018-06-23 stsp "browse trees in repository" },
85 d6b05b5b 2018-08-04 stsp enum tog_view_type {
86 d6b05b5b 2018-08-04 stsp TOG_VIEW_DIFF,
87 d6b05b5b 2018-08-04 stsp TOG_VIEW_LOG,
88 d6b05b5b 2018-08-04 stsp TOG_VIEW_TREE,
89 d6b05b5b 2018-08-04 stsp TOG_VIEW_BLAME
92 ba4f502b 2018-08-04 stsp struct commit_queue_entry {
93 ba4f502b 2018-08-04 stsp TAILQ_ENTRY(commit_queue_entry) entry;
94 ba4f502b 2018-08-04 stsp struct got_object_id *id;
95 ba4f502b 2018-08-04 stsp struct got_commit_object *commit;
97 ba4f502b 2018-08-04 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
98 ba4f502b 2018-08-04 stsp struct commit_queue {
99 ba4f502b 2018-08-04 stsp int ncommits;
100 ba4f502b 2018-08-04 stsp struct commit_queue_head head;
103 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state {
105 b01e7d3b 2018-08-04 stsp int first_displayed_line;
106 b01e7d3b 2018-08-04 stsp int last_displayed_line;
109 b01e7d3b 2018-08-04 stsp struct tog_log_view_state {
110 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
111 b01e7d3b 2018-08-04 stsp struct commit_queue commits;
112 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
113 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
114 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
115 b01e7d3b 2018-08-04 stsp int selected;
116 b01e7d3b 2018-08-04 stsp char *in_repo_path;
117 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
120 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
121 7cbe629d 2018-08-04 stsp int first_displayed_line;
122 7cbe629d 2018-08-04 stsp int last_displayed_line;
123 7cbe629d 2018-08-04 stsp int selected_line;
124 7cbe629d 2018-08-04 stsp int blame_complete;
125 7cbe629d 2018-08-04 stsp pthread_mutex_t mutex;
126 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
127 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
128 7cbe629d 2018-08-04 stsp const char *path;
129 7cbe629d 2018-08-04 stsp struct got_repository *repo;
130 7cbe629d 2018-08-04 stsp struct got_object_id *commit_id;
133 cc3c9aac 2018-08-01 stsp struct tog_view {
134 26ed57b2 2018-05-19 stsp WINDOW *window;
135 26ed57b2 2018-05-19 stsp PANEL *panel;
136 97ddc146 2018-08-01 stsp int nlines, ncols, begin_y, begin_x;
137 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
138 6d0fee91 2018-08-01 stsp struct tog_view *parent;
140 5dc9f4bc 2018-08-04 stsp /* type-specific state */
141 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
143 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
144 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
145 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
149 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
150 ba4f502b 2018-08-04 stsp struct got_object *, struct got_object *, struct got_repository *);
151 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
152 5dc9f4bc 2018-08-04 stsp static void close_diff_view(struct tog_view *);
153 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
154 ba4f502b 2018-08-04 stsp struct got_object_id *, struct got_repository *, const char *);
155 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
156 ba4f502b 2018-08-04 stsp static void close_log_view(struct tog_view *);
157 7cbe629d 2018-08-04 stsp static const struct got_error *open_blame_view(struct tog_view *, const char *,
158 5221c383 2018-08-01 stsp struct got_object_id *, struct got_repository *);
159 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
160 7cbe629d 2018-08-04 stsp static void close_blame_view(struct tog_view *);
161 4fc679ca 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *,
162 4fc679ca 2018-08-04 stsp struct got_tree_object *, struct got_object_id *, struct got_repository *);
164 ea5e7bb5 2018-08-01 stsp static void
165 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
167 ea5e7bb5 2018-08-01 stsp if (view->panel)
168 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
169 ea5e7bb5 2018-08-01 stsp if (view->window)
170 ea5e7bb5 2018-08-01 stsp delwin(view->window);
171 ea5e7bb5 2018-08-01 stsp free(view);
174 ea5e7bb5 2018-08-01 stsp static struct tog_view *
175 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
176 d6b05b5b 2018-08-04 stsp struct tog_view *parent, enum tog_view_type type)
178 ea5e7bb5 2018-08-01 stsp struct tog_view *view = malloc(sizeof(*view));
180 ea5e7bb5 2018-08-01 stsp if (view == NULL)
181 ea5e7bb5 2018-08-01 stsp return NULL;
183 6d0fee91 2018-08-01 stsp view->parent = parent;
184 d6b05b5b 2018-08-04 stsp view->type = type;
185 f7d12f7e 2018-08-01 stsp view->lines = LINES;
186 f7d12f7e 2018-08-01 stsp view->cols = COLS;
187 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
188 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
189 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
190 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
191 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
192 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
193 96a765a8 2018-08-04 stsp view_close(view);
194 ea5e7bb5 2018-08-01 stsp return NULL;
196 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
197 ea5e7bb5 2018-08-01 stsp if (view->panel == NULL) {
198 96a765a8 2018-08-04 stsp view_close(view);
199 ea5e7bb5 2018-08-01 stsp return NULL;
202 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
203 ea5e7bb5 2018-08-01 stsp return view;
207 6acd1bd1 2018-08-04 stsp view_show(struct tog_view *view)
209 cdf1ee82 2018-08-01 stsp show_panel(view->panel);
210 cdf1ee82 2018-08-01 stsp update_panels();
213 f7d12f7e 2018-08-01 stsp const struct got_error *
214 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
216 f7d12f7e 2018-08-01 stsp int nlines, ncols;
218 6d0fee91 2018-08-01 stsp while (view) {
219 6d0fee91 2018-08-01 stsp if (view->lines > LINES)
220 6d0fee91 2018-08-01 stsp nlines = view->nlines - (view->lines - LINES);
222 6d0fee91 2018-08-01 stsp nlines = view->nlines + (LINES - view->lines);
224 6d0fee91 2018-08-01 stsp if (view->cols > COLS)
225 6d0fee91 2018-08-01 stsp ncols = view->ncols - (view->cols - COLS);
227 6d0fee91 2018-08-01 stsp ncols = view->ncols + (COLS - view->cols);
229 6d0fee91 2018-08-01 stsp if (wresize(view->window, nlines, ncols) == ERR)
230 6d0fee91 2018-08-01 stsp return got_error_from_errno();
231 77a567dd 2018-08-01 stsp replace_panel(view->panel, view->window);
233 6d0fee91 2018-08-01 stsp view->nlines = nlines;
234 6d0fee91 2018-08-01 stsp view->ncols = ncols;
235 6d0fee91 2018-08-01 stsp view->lines = LINES;
236 6d0fee91 2018-08-01 stsp view->cols = COLS;
238 6d0fee91 2018-08-01 stsp view = view->parent;
241 f7d12f7e 2018-08-01 stsp return NULL;
244 4ed7e80c 2018-05-20 stsp __dead static void
245 9f7d7167 2018-04-29 stsp usage_log(void)
248 c70c5802 2018-08-01 stsp fprintf(stderr,
249 c70c5802 2018-08-01 stsp "usage: %s log [-c commit] [-r repository-path] [path]\n",
250 9f7d7167 2018-04-29 stsp getprogname());
254 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
255 80ddbec8 2018-04-29 stsp static const struct got_error *
256 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
258 00dfcb92 2018-06-11 stsp char *vis = NULL;
259 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
261 963b370f 2018-05-20 stsp *ws = NULL;
262 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
263 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
264 00dfcb92 2018-06-11 stsp int vislen;
265 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
266 00dfcb92 2018-06-11 stsp return got_error_from_errno();
268 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
269 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
271 00dfcb92 2018-06-11 stsp return err;
272 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
273 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
274 a7f50699 2018-06-11 stsp err = got_error_from_errno(); /* give up */
279 963b370f 2018-05-20 stsp *ws = calloc(*wlen + 1, sizeof(*ws));
280 a7f50699 2018-06-11 stsp if (*ws == NULL) {
281 a7f50699 2018-06-11 stsp err = got_error_from_errno();
285 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
286 963b370f 2018-05-20 stsp err = got_error_from_errno();
291 963b370f 2018-05-20 stsp *ws = NULL;
294 963b370f 2018-05-20 stsp return err;
297 963b370f 2018-05-20 stsp /* Format a line for display, ensuring that it won't overflow a width limit. */
298 963b370f 2018-05-20 stsp static const struct got_error *
299 ffd1d5e5 2018-06-23 stsp format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
301 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
302 963b370f 2018-05-20 stsp int cols = 0;
303 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
304 963b370f 2018-05-20 stsp size_t wlen;
307 963b370f 2018-05-20 stsp *wlinep = NULL;
308 b700b5d6 2018-07-10 stsp *widthp = 0;
310 963b370f 2018-05-20 stsp err = mbs2ws(&wline, &wlen, line);
312 963b370f 2018-05-20 stsp return err;
315 b700b5d6 2018-07-10 stsp while (i < wlen && cols < wlimit) {
316 963b370f 2018-05-20 stsp int width = wcwidth(wline[i]);
317 963b370f 2018-05-20 stsp switch (width) {
323 b700b5d6 2018-07-10 stsp if (cols + width <= wlimit) {
324 b700b5d6 2018-07-10 stsp cols += width;
329 963b370f 2018-05-20 stsp if (wline[i] == L'\t')
330 b700b5d6 2018-07-10 stsp cols += TABSIZE - ((cols + 1) % TABSIZE);
334 963b370f 2018-05-20 stsp err = got_error_from_errno();
338 963b370f 2018-05-20 stsp wline[i] = L'\0';
339 b700b5d6 2018-07-10 stsp if (widthp)
340 b700b5d6 2018-07-10 stsp *widthp = cols;
343 963b370f 2018-05-20 stsp free(wline);
345 963b370f 2018-05-20 stsp *wlinep = wline;
346 963b370f 2018-05-20 stsp return err;
349 963b370f 2018-05-20 stsp static const struct got_error *
350 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
351 2814baeb 2018-08-01 stsp struct got_object_id *id)
353 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
354 b39d25c7 2018-07-10 stsp char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
355 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
356 6d9fbc00 2018-04-29 stsp char *author0 = NULL, *author = NULL;
357 bb737323 2018-05-20 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
358 bb737323 2018-05-20 stsp int author_width, logmsg_width;
359 6d9fbc00 2018-04-29 stsp char *newline, *smallerthan;
360 80ddbec8 2018-04-29 stsp char *line = NULL;
361 bb737323 2018-05-20 stsp int col, limit;
362 b39d25c7 2018-07-10 stsp static const size_t date_display_cols = 9;
363 bb737323 2018-05-20 stsp static const size_t author_display_cols = 16;
364 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
366 c70c5802 2018-08-01 stsp if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ",
367 c70c5802 2018-08-01 stsp &commit->tm_committer) >= sizeof(datebuf))
368 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
370 b39d25c7 2018-07-10 stsp if (avail < date_display_cols)
371 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
373 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
374 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
375 b39d25c7 2018-07-10 stsp col = limit + 1;
376 b39d25c7 2018-07-10 stsp if (col > avail)
379 6d9fbc00 2018-04-29 stsp author0 = strdup(commit->author);
380 6d9fbc00 2018-04-29 stsp if (author0 == NULL) {
381 80ddbec8 2018-04-29 stsp err = got_error_from_errno();
384 6d9fbc00 2018-04-29 stsp author = author0;
385 6d9fbc00 2018-04-29 stsp smallerthan = strchr(author, '<');
386 6d9fbc00 2018-04-29 stsp if (smallerthan)
387 6d9fbc00 2018-04-29 stsp *smallerthan = '\0';
389 6d9fbc00 2018-04-29 stsp char *at = strchr(author, '@');
391 6d9fbc00 2018-04-29 stsp *at = '\0';
393 b39d25c7 2018-07-10 stsp limit = avail - col;
394 bb737323 2018-05-20 stsp err = format_line(&wauthor, &author_width, author, limit);
397 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
398 bb737323 2018-05-20 stsp col += author_width;
399 9c2eaf34 2018-05-20 stsp while (col <= avail && author_width < author_display_cols + 1) {
400 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
402 bb737323 2018-05-20 stsp author_width++;
404 9c2eaf34 2018-05-20 stsp if (col > avail)
407 bb737323 2018-05-20 stsp logmsg0 = strdup(commit->logmsg);
408 bb737323 2018-05-20 stsp if (logmsg0 == NULL) {
409 6d9fbc00 2018-04-29 stsp err = got_error_from_errno();
412 bb737323 2018-05-20 stsp logmsg = logmsg0;
413 bb737323 2018-05-20 stsp while (*logmsg == '\n')
415 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
416 bb737323 2018-05-20 stsp if (newline)
417 bb737323 2018-05-20 stsp *newline = '\0';
418 bb737323 2018-05-20 stsp limit = avail - col;
419 bb737323 2018-05-20 stsp err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
422 2814baeb 2018-08-01 stsp waddwstr(view->window, wlogmsg);
423 bb737323 2018-05-20 stsp col += logmsg_width;
424 bb737323 2018-05-20 stsp while (col <= avail) {
425 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
429 80ddbec8 2018-04-29 stsp free(logmsg0);
430 bb737323 2018-05-20 stsp free(wlogmsg);
431 6d9fbc00 2018-04-29 stsp free(author0);
432 bb737323 2018-05-20 stsp free(wauthor);
433 80ddbec8 2018-04-29 stsp free(line);
434 80ddbec8 2018-04-29 stsp return err;
437 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
438 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
439 899d86c2 2018-05-10 stsp struct got_object_id *id)
441 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
443 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
444 80ddbec8 2018-04-29 stsp if (entry == NULL)
445 899d86c2 2018-05-10 stsp return NULL;
447 899d86c2 2018-05-10 stsp entry->id = id;
448 99db9666 2018-05-07 stsp entry->commit = commit;
449 899d86c2 2018-05-10 stsp return entry;
452 99db9666 2018-05-07 stsp static void
453 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
455 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
457 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
458 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
459 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
460 ecb28ae0 2018-07-16 stsp commits->ncommits--;
461 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
462 99db9666 2018-05-07 stsp free(entry);
465 99db9666 2018-05-07 stsp static void
466 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
468 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
469 99db9666 2018-05-07 stsp pop_commit(commits);
472 c4972b91 2018-05-07 stsp static const struct got_error *
473 9ba79e04 2018-06-11 stsp queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
474 ecb28ae0 2018-07-16 stsp struct got_object_id *start_id, int minqueue, int init,
475 ecb28ae0 2018-07-16 stsp struct got_repository *repo, const char *path)
477 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
478 899d86c2 2018-05-10 stsp struct got_object_id *id;
479 9ba79e04 2018-06-11 stsp struct commit_queue_entry *entry;
480 ecb28ae0 2018-07-16 stsp int nfetched, nqueued = 0, found_obj = 0;
481 c8f60bff 2018-07-23 stsp int is_root_path = strcmp(path, "/") == 0;
483 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_start(graph, start_id);
485 c4972b91 2018-05-07 stsp return err;
487 ecb28ae0 2018-07-16 stsp entry = TAILQ_LAST(&commits->head, commit_queue_head);
488 9ba79e04 2018-06-11 stsp if (entry && got_object_id_cmp(entry->id, start_id) == 0) {
489 9ba79e04 2018-06-11 stsp int nfetched;
491 9ba79e04 2018-06-11 stsp /* Start ID's commit is already on the queue; skip over it. */
492 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
493 9ba79e04 2018-06-11 stsp if (err && err->code != GOT_ERR_ITER_NEED_MORE)
494 9ba79e04 2018-06-11 stsp return err;
496 9ba79e04 2018-06-11 stsp err = got_commit_graph_fetch_commits(&nfetched, graph, 1, repo);
498 9ba79e04 2018-06-11 stsp return err;
501 9ba79e04 2018-06-11 stsp while (1) {
502 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
504 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
506 ecb28ae0 2018-07-16 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
508 ecb28ae0 2018-07-16 stsp if (nqueued >= minqueue) {
509 9ba79e04 2018-06-11 stsp err = NULL;
512 ecb28ae0 2018-07-16 stsp err = got_commit_graph_fetch_commits(&nfetched,
513 ecb28ae0 2018-07-16 stsp graph, 1, repo);
515 ecb28ae0 2018-07-16 stsp return err;
518 ecb28ae0 2018-07-16 stsp if (id == NULL)
521 9ba79e04 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
525 c8f60bff 2018-07-23 stsp if (!is_root_path) {
526 ecb28ae0 2018-07-16 stsp struct got_object *obj;
527 ecb28ae0 2018-07-16 stsp struct got_object_qid *pid;
528 ecb28ae0 2018-07-16 stsp int changed = 0;
530 ecb28ae0 2018-07-16 stsp err = got_object_open_by_path(&obj, repo, id, path);
532 c8f60bff 2018-07-23 stsp got_object_commit_close(commit);
533 ecb28ae0 2018-07-16 stsp if (err->code == GOT_ERR_NO_OBJ &&
534 ecb28ae0 2018-07-16 stsp (found_obj || !init)) {
535 ecb28ae0 2018-07-16 stsp /* History stops here. */
536 ecb28ae0 2018-07-16 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
540 ecb28ae0 2018-07-16 stsp found_obj = 1;
542 ecb28ae0 2018-07-16 stsp pid = SIMPLEQ_FIRST(&commit->parent_ids);
543 ecb28ae0 2018-07-16 stsp if (pid != NULL) {
544 ecb28ae0 2018-07-16 stsp struct got_object *pobj;
545 ecb28ae0 2018-07-16 stsp err = got_object_open_by_path(&pobj, repo,
546 ecb28ae0 2018-07-16 stsp pid->id, path);
548 ecb28ae0 2018-07-16 stsp if (err->code != GOT_ERR_NO_OBJ) {
549 ecb28ae0 2018-07-16 stsp got_object_close(obj);
550 c8f60bff 2018-07-23 stsp got_object_commit_close(commit);
553 ecb28ae0 2018-07-16 stsp err = NULL;
554 ecb28ae0 2018-07-16 stsp changed = 1;
556 c2f16475 2018-07-23 stsp struct got_object_id *id, *pid;
557 c2f16475 2018-07-23 stsp id = got_object_get_id(obj);
558 c2f16475 2018-07-23 stsp if (id == NULL) {
559 c2f16475 2018-07-23 stsp err = got_error_from_errno();
560 c8f60bff 2018-07-23 stsp got_object_close(obj);
561 c8f60bff 2018-07-23 stsp got_object_close(pobj);
564 c2f16475 2018-07-23 stsp pid = got_object_get_id(pobj);
565 c2f16475 2018-07-23 stsp if (pid == NULL) {
566 c2f16475 2018-07-23 stsp err = got_error_from_errno();
568 c8f60bff 2018-07-23 stsp got_object_close(obj);
569 c8f60bff 2018-07-23 stsp got_object_close(pobj);
573 c2f16475 2018-07-23 stsp (got_object_id_cmp(id, pid) != 0);
574 ecb28ae0 2018-07-16 stsp got_object_close(pobj);
579 c8f60bff 2018-07-23 stsp got_object_close(obj);
580 ecb28ae0 2018-07-16 stsp if (!changed) {
581 ecb28ae0 2018-07-16 stsp got_object_commit_close(commit);
586 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
587 9ba79e04 2018-06-11 stsp if (entry == NULL) {
588 9ba79e04 2018-06-11 stsp err = got_error_from_errno();
591 ecb28ae0 2018-07-16 stsp TAILQ_INSERT_TAIL(&commits->head, entry, entry);
593 ecb28ae0 2018-07-16 stsp commits->ncommits++;
596 9ba79e04 2018-06-11 stsp return err;
599 99db9666 2018-05-07 stsp static const struct got_error *
600 9ba79e04 2018-06-11 stsp fetch_next_commit(struct commit_queue_entry **pentry,
601 9ba79e04 2018-06-11 stsp struct commit_queue_entry *entry, struct commit_queue *commits,
602 ecb28ae0 2018-07-16 stsp struct got_commit_graph *graph, struct got_repository *repo,
603 ecb28ae0 2018-07-16 stsp const char *path)
605 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
607 9ba79e04 2018-06-11 stsp *pentry = NULL;
609 ecb28ae0 2018-07-16 stsp err = queue_commits(graph, commits, entry->id, 1, 0, repo, path);
611 9ba79e04 2018-06-11 stsp return err;
613 9ba79e04 2018-06-11 stsp /* Next entry to display should now be available. */
614 9ba79e04 2018-06-11 stsp *pentry = TAILQ_NEXT(entry, entry);
615 9ba79e04 2018-06-11 stsp if (*pentry == NULL)
616 9ba79e04 2018-06-11 stsp return got_error(GOT_ERR_NO_OBJ);
618 9ba79e04 2018-06-11 stsp return NULL;
621 899d86c2 2018-05-10 stsp static const struct got_error *
622 9ba79e04 2018-06-11 stsp get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
624 9ba79e04 2018-06-11 stsp const struct got_error *err = NULL;
625 9ba79e04 2018-06-11 stsp struct got_reference *head_ref;
627 9ba79e04 2018-06-11 stsp *head_id = NULL;
629 9ba79e04 2018-06-11 stsp err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
631 99db9666 2018-05-07 stsp return err;
633 9ba79e04 2018-06-11 stsp err = got_ref_resolve(head_id, repo, head_ref);
634 9ba79e04 2018-06-11 stsp got_ref_close(head_ref);
636 9ba79e04 2018-06-11 stsp *head_id = NULL;
637 99db9666 2018-05-07 stsp return err;
640 9ba79e04 2018-06-11 stsp return NULL;
643 0553a4e3 2018-04-30 stsp static const struct got_error *
644 2814baeb 2018-08-01 stsp draw_commits(struct tog_view *view, struct commit_queue_entry **last,
645 ecb28ae0 2018-07-16 stsp struct commit_queue_entry **selected, struct commit_queue_entry *first,
646 a7f40148 2018-07-18 stsp struct commit_queue *commits, int selected_idx, int limit,
647 a7f40148 2018-07-18 stsp struct got_commit_graph *graph, struct got_repository *repo,
648 a7f40148 2018-07-18 stsp const char *path)
650 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
651 0553a4e3 2018-04-30 stsp struct commit_queue_entry *entry;
652 ecb28ae0 2018-07-16 stsp int ncommits, width;
653 867c6645 2018-07-10 stsp char *id_str, *header;
654 ecb28ae0 2018-07-16 stsp wchar_t *wline;
656 e0d42f60 2018-07-22 stsp entry = first;
657 e0d42f60 2018-07-22 stsp ncommits = 0;
658 e0d42f60 2018-07-22 stsp while (entry) {
659 e0d42f60 2018-07-22 stsp if (ncommits == selected_idx) {
660 e0d42f60 2018-07-22 stsp *selected = entry;
663 e0d42f60 2018-07-22 stsp entry = TAILQ_NEXT(entry, entry);
664 e0d42f60 2018-07-22 stsp ncommits++;
667 867c6645 2018-07-10 stsp err = got_object_id_str(&id_str, (*selected)->id);
669 867c6645 2018-07-10 stsp return err;
671 c3ba6f36 2018-08-01 stsp if (path && strcmp(path, "/") != 0) {
672 ecb28ae0 2018-07-16 stsp if (asprintf(&header, "commit: %s [%s]", id_str, path) == -1) {
673 ecb28ae0 2018-07-16 stsp err = got_error_from_errno();
674 ecb28ae0 2018-07-16 stsp free(id_str);
675 ecb28ae0 2018-07-16 stsp return err;
677 ecb28ae0 2018-07-16 stsp } else if (asprintf(&header, "commit: %s", id_str) == -1) {
678 867c6645 2018-07-10 stsp err = got_error_from_errno();
679 867c6645 2018-07-10 stsp free(id_str);
680 867c6645 2018-07-10 stsp return err;
682 ecb28ae0 2018-07-16 stsp free(id_str);
683 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, header, view->ncols);
685 ecb28ae0 2018-07-16 stsp free(header);
686 ecb28ae0 2018-07-16 stsp return err;
688 ecb28ae0 2018-07-16 stsp free(header);
690 2814baeb 2018-08-01 stsp werase(view->window);
692 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
693 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
694 2814baeb 2018-08-01 stsp waddch(view->window, '\n');
695 ecb28ae0 2018-07-16 stsp free(wline);
696 ecb28ae0 2018-07-16 stsp if (limit <= 1)
697 ecb28ae0 2018-07-16 stsp return NULL;
699 899d86c2 2018-05-10 stsp entry = first;
700 899d86c2 2018-05-10 stsp *last = first;
701 867c6645 2018-07-10 stsp ncommits = 0;
702 899d86c2 2018-05-10 stsp while (entry) {
703 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
705 e0d42f60 2018-07-22 stsp if (ncommits == selected_idx)
706 2814baeb 2018-08-01 stsp wstandout(view->window);
707 2814baeb 2018-08-01 stsp err = draw_commit(view, entry->commit, entry->id);
708 cd0acaa7 2018-05-20 stsp if (ncommits == selected_idx)
709 2814baeb 2018-08-01 stsp wstandend(view->window);
712 0553a4e3 2018-04-30 stsp ncommits++;
713 899d86c2 2018-05-10 stsp *last = entry;
714 a7f40148 2018-07-18 stsp if (entry == TAILQ_LAST(&commits->head, commit_queue_head)) {
715 a7f40148 2018-07-18 stsp err = queue_commits(graph, commits, entry->id, 1,
716 a7f40148 2018-07-18 stsp 0, repo, path);
718 a7f40148 2018-07-18 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
719 a7f40148 2018-07-18 stsp return err;
720 a7f40148 2018-07-18 stsp err = NULL;
723 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
726 80ddbec8 2018-04-29 stsp update_panels();
727 80ddbec8 2018-04-29 stsp doupdate();
729 80ddbec8 2018-04-29 stsp return err;
732 07b55e75 2018-05-10 stsp static void
733 16482c3b 2018-05-20 stsp scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
734 07b55e75 2018-05-10 stsp struct commit_queue *commits)
736 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
737 07b55e75 2018-05-10 stsp int nscrolled = 0;
739 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
740 07b55e75 2018-05-10 stsp if (*first_displayed_entry == entry)
743 07b55e75 2018-05-10 stsp entry = *first_displayed_entry;
744 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
745 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
746 07b55e75 2018-05-10 stsp if (entry) {
747 07b55e75 2018-05-10 stsp *first_displayed_entry = entry;
748 07b55e75 2018-05-10 stsp nscrolled++;
753 aa075928 2018-05-10 stsp static const struct got_error *
754 16482c3b 2018-05-20 stsp scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
755 aa075928 2018-05-10 stsp struct commit_queue_entry *last_displayed_entry,
756 9ba79e04 2018-06-11 stsp struct commit_queue *commits, struct got_commit_graph *graph,
757 ecb28ae0 2018-07-16 stsp struct got_repository *repo, const char *path)
759 aa075928 2018-05-10 stsp const struct got_error *err = NULL;
760 dd0a52c1 2018-05-20 stsp struct commit_queue_entry *pentry;
761 aa075928 2018-05-10 stsp int nscrolled = 0;
764 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(last_displayed_entry, entry);
765 aa075928 2018-05-10 stsp if (pentry == NULL) {
766 9ba79e04 2018-06-11 stsp err = fetch_next_commit(&pentry, last_displayed_entry,
767 ecb28ae0 2018-07-16 stsp commits, graph, repo, path);
768 9a6bf2a5 2018-05-20 stsp if (err || pentry == NULL)
771 dd0a52c1 2018-05-20 stsp last_displayed_entry = pentry;
773 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(*first_displayed_entry, entry);
774 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
776 aa075928 2018-05-10 stsp *first_displayed_entry = pentry;
777 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
779 dd0a52c1 2018-05-20 stsp return err;
782 cd0acaa7 2018-05-20 stsp static const struct got_error *
783 6d0fee91 2018-08-01 stsp show_commit(struct tog_view *parent_view, struct commit_queue_entry *entry,
784 6d0fee91 2018-08-01 stsp struct got_repository *repo)
786 cd0acaa7 2018-05-20 stsp const struct got_error *err;
787 cd0acaa7 2018-05-20 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
788 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
789 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
791 cd0acaa7 2018-05-20 stsp err = got_object_open(&obj2, repo, entry->id);
793 cd0acaa7 2018-05-20 stsp return err;
795 9ba79e04 2018-06-11 stsp parent_id = SIMPLEQ_FIRST(&entry->commit->parent_ids);
796 9ba79e04 2018-06-11 stsp if (parent_id) {
797 9ba79e04 2018-06-11 stsp err = got_object_open(&obj1, repo, parent_id->id);
802 b3665f43 2018-08-04 stsp view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_DIFF);
803 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
804 ea5e7bb5 2018-08-01 stsp err = got_error_from_errno();
808 5dc9f4bc 2018-08-04 stsp err = open_diff_view(view, obj1, obj2, repo);
811 5dc9f4bc 2018-08-04 stsp err = show_diff_view(view);
812 5dc9f4bc 2018-08-04 stsp close_diff_view(view);
813 96a765a8 2018-08-04 stsp view_close(view);
814 6acd1bd1 2018-08-04 stsp view_show(parent_view);
817 cd0acaa7 2018-05-20 stsp got_object_close(obj1);
819 cd0acaa7 2018-05-20 stsp got_object_close(obj2);
820 cd0acaa7 2018-05-20 stsp return err;
823 80ddbec8 2018-04-29 stsp static const struct got_error *
824 6d0fee91 2018-08-01 stsp browse_commit(struct tog_view *parent_view, struct commit_queue_entry *entry,
825 6d0fee91 2018-08-01 stsp struct got_repository *repo)
827 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
828 9343a5fb 2018-06-23 stsp struct got_tree_object *tree;
829 5221c383 2018-08-01 stsp struct tog_view *view;
831 9343a5fb 2018-06-23 stsp err = got_object_open_as_tree(&tree, repo, entry->commit->tree_id);
833 9343a5fb 2018-06-23 stsp return err;
835 b3665f43 2018-08-04 stsp view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_TREE);
836 5221c383 2018-08-01 stsp if (view == NULL) {
837 5221c383 2018-08-01 stsp err = got_error_from_errno();
840 5221c383 2018-08-01 stsp err = show_tree_view(view, tree, entry->id, repo);
841 96a765a8 2018-08-04 stsp view_close(view);
842 6acd1bd1 2018-08-04 stsp view_show(parent_view);
844 9343a5fb 2018-06-23 stsp got_object_tree_close(tree);
845 9343a5fb 2018-06-23 stsp return err;
848 9343a5fb 2018-06-23 stsp static const struct got_error *
849 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
850 04cc582a 2018-08-01 stsp struct got_repository *repo, const char *path)
852 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
853 9ba79e04 2018-06-11 stsp struct got_object_id *head_id = NULL;
854 ba4f502b 2018-08-04 stsp int nfetched;
855 4d7951a5 2018-08-04 stsp struct tog_log_view_state *state = &view->state.log;
857 4d7951a5 2018-08-04 stsp err = got_repo_map_path(&state->in_repo_path, repo, path);
858 ecb28ae0 2018-07-16 stsp if (err != NULL)
861 9ba79e04 2018-06-11 stsp err = get_head_commit_id(&head_id, repo);
863 9ba79e04 2018-06-11 stsp return err;
865 034e3b69 2018-07-22 stsp /* The graph contains all commits. */
866 4d7951a5 2018-08-04 stsp err = got_commit_graph_open(&state->graph, head_id, 0, repo);
869 034e3b69 2018-07-22 stsp /* The commit queue contains a subset of commits filtered by path. */
870 4d7951a5 2018-08-04 stsp TAILQ_INIT(&state->commits.head);
871 4d7951a5 2018-08-04 stsp state->commits.ncommits = 0;
873 9ba79e04 2018-06-11 stsp /* Populate commit graph with a sufficient number of commits. */
874 ba4f502b 2018-08-04 stsp err = got_commit_graph_fetch_commits_up_to(&nfetched,
875 4d7951a5 2018-08-04 stsp state->graph, start_id, repo);
880 9ba79e04 2018-06-11 stsp * Open the initial batch of commits, sorted in commit graph order.
881 9ba79e04 2018-06-11 stsp * We keep all commits open throughout the lifetime of the log view
882 9ba79e04 2018-06-11 stsp * in order to avoid having to re-fetch commits from disk while
883 9ba79e04 2018-06-11 stsp * updating the display.
885 4d7951a5 2018-08-04 stsp err = queue_commits(state->graph, &state->commits, start_id,
886 4d7951a5 2018-08-04 stsp view->nlines, 1, repo, state->in_repo_path);
888 55198a88 2018-07-16 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
890 55198a88 2018-07-16 stsp err = NULL;
893 4d7951a5 2018-08-04 stsp state->repo = repo;
895 ba4f502b 2018-08-04 stsp free(head_id);
896 ba4f502b 2018-08-04 stsp return err;
899 ba4f502b 2018-08-04 stsp static void close_log_view(struct tog_view *view)
901 4d7951a5 2018-08-04 stsp struct tog_log_view_state *state = &view->state.log;
903 4d7951a5 2018-08-04 stsp if (state->graph)
904 4d7951a5 2018-08-04 stsp got_commit_graph_close(state->graph);
905 4d7951a5 2018-08-04 stsp free_commits(&state->commits);
906 4d7951a5 2018-08-04 stsp free(state->in_repo_path);
909 ba4f502b 2018-08-04 stsp static const struct got_error *
910 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
912 ba4f502b 2018-08-04 stsp const struct got_error *err = NULL;
913 ba4f502b 2018-08-04 stsp int ch, done = 0;
914 4d7951a5 2018-08-04 stsp struct tog_log_view_state *state = &view->state.log;
916 6acd1bd1 2018-08-04 stsp view_show(view);
918 4d7951a5 2018-08-04 stsp state->first_displayed_entry =
919 4d7951a5 2018-08-04 stsp TAILQ_FIRST(&state->commits.head);
920 4d7951a5 2018-08-04 stsp state->selected_entry = state->first_displayed_entry;
921 899d86c2 2018-05-10 stsp while (!done) {
922 4d7951a5 2018-08-04 stsp err = draw_commits(view, &state->last_displayed_entry,
923 4d7951a5 2018-08-04 stsp &state->selected_entry, state->first_displayed_entry,
924 4d7951a5 2018-08-04 stsp &state->commits, state->selected, view->nlines,
925 4d7951a5 2018-08-04 stsp state->graph, state->repo, state->in_repo_path);
929 80ddbec8 2018-04-29 stsp nodelay(stdscr, FALSE);
930 2814baeb 2018-08-01 stsp ch = wgetch(view->window);
931 f7182337 2018-05-20 stsp nodelay(stdscr, TRUE);
932 80ddbec8 2018-04-29 stsp switch (ch) {
939 80ddbec8 2018-04-29 stsp case KEY_UP:
940 4d7951a5 2018-08-04 stsp if (state->selected > 0)
941 4d7951a5 2018-08-04 stsp state->selected--;
942 4d7951a5 2018-08-04 stsp if (state->selected > 0)
944 4d7951a5 2018-08-04 stsp scroll_up(&state->first_displayed_entry, 1,
945 4d7951a5 2018-08-04 stsp &state->commits);
947 48531068 2018-05-10 stsp case KEY_PPAGE:
948 4d7951a5 2018-08-04 stsp if (TAILQ_FIRST(&state->commits.head) ==
949 4d7951a5 2018-08-04 stsp state->first_displayed_entry) {
950 4d7951a5 2018-08-04 stsp state->selected = 0;
953 4d7951a5 2018-08-04 stsp scroll_up(&state->first_displayed_entry,
954 4d7951a5 2018-08-04 stsp view->nlines, &state->commits);
957 80ddbec8 2018-04-29 stsp case KEY_DOWN:
958 4d7951a5 2018-08-04 stsp if (state->selected < MIN(view->nlines - 2,
959 4d7951a5 2018-08-04 stsp state->commits.ncommits - 1)) {
960 4d7951a5 2018-08-04 stsp state->selected++;
963 4d7951a5 2018-08-04 stsp err = scroll_down(&state->first_displayed_entry,
964 4d7951a5 2018-08-04 stsp 1, state->last_displayed_entry,
965 4d7951a5 2018-08-04 stsp &state->commits, state->graph, state->repo,
966 4d7951a5 2018-08-04 stsp state->in_repo_path);
968 ecb28ae0 2018-07-16 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
970 ecb28ae0 2018-07-16 stsp err = NULL;
973 ecb28ae0 2018-07-16 stsp case KEY_NPAGE: {
974 c70c5802 2018-08-01 stsp struct commit_queue_entry *first;
975 4d7951a5 2018-08-04 stsp first = state->first_displayed_entry;
976 4d7951a5 2018-08-04 stsp err = scroll_down(&state->first_displayed_entry,
977 4d7951a5 2018-08-04 stsp view->nlines, state->last_displayed_entry,
978 4d7951a5 2018-08-04 stsp &state->commits, state->graph, state->repo,
979 4d7951a5 2018-08-04 stsp state->in_repo_path);
980 4d7951a5 2018-08-04 stsp if (err == NULL)
982 4d7951a5 2018-08-04 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
984 4d7951a5 2018-08-04 stsp if (first == state->first_displayed_entry &&
985 4d7951a5 2018-08-04 stsp state->selected < MIN(view->nlines - 2,
986 4d7951a5 2018-08-04 stsp state->commits.ncommits - 1)) {
987 4d7951a5 2018-08-04 stsp /* can't scroll further down */
988 4d7951a5 2018-08-04 stsp state->selected = MIN(view->nlines - 2,
989 4d7951a5 2018-08-04 stsp state->commits.ncommits - 1);
991 4d7951a5 2018-08-04 stsp err = NULL;
994 d6df9be4 2018-04-30 stsp case KEY_RESIZE:
995 a41d2007 2018-08-01 stsp err = view_resize(view);
998 4d7951a5 2018-08-04 stsp if (state->selected > view->nlines - 2)
999 4d7951a5 2018-08-04 stsp state->selected = view->nlines - 2;
1000 4d7951a5 2018-08-04 stsp if (state->selected >
1001 4d7951a5 2018-08-04 stsp state->commits.ncommits - 1)
1002 4d7951a5 2018-08-04 stsp state->selected =
1003 4d7951a5 2018-08-04 stsp state->commits.ncommits - 1;
1005 cd0acaa7 2018-05-20 stsp case KEY_ENTER:
1006 cd0acaa7 2018-05-20 stsp case '\r':
1007 4d7951a5 2018-08-04 stsp err = show_commit(view, state->selected_entry,
1008 4d7951a5 2018-08-04 stsp state->repo);
1010 9343a5fb 2018-06-23 stsp goto done;
1011 6acd1bd1 2018-08-04 stsp view_show(view);
1014 4d7951a5 2018-08-04 stsp err = browse_commit(view, state->selected_entry,
1015 4d7951a5 2018-08-04 stsp state->repo);
1017 0d4100bb 2018-06-23 stsp goto done;
1018 6acd1bd1 2018-08-04 stsp view_show(view);
1025 80ddbec8 2018-04-29 stsp return err;
1028 4ed7e80c 2018-05-20 stsp static const struct got_error *
1029 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
1031 80ddbec8 2018-04-29 stsp const struct got_error *error;
1032 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
1033 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
1034 ecb28ae0 2018-07-16 stsp char *path = NULL, *repo_path = NULL, *cwd = NULL;
1035 80ddbec8 2018-04-29 stsp char *start_commit = NULL;
1037 04cc582a 2018-08-01 stsp struct tog_view *view;
1039 80ddbec8 2018-04-29 stsp #ifndef PROFILE
1040 80ddbec8 2018-04-29 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1041 80ddbec8 2018-04-29 stsp err(1, "pledge");
1044 ecb28ae0 2018-07-16 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1045 80ddbec8 2018-04-29 stsp switch (ch) {
1047 80ddbec8 2018-04-29 stsp start_commit = optarg;
1050 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
1051 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
1052 ecb28ae0 2018-07-16 stsp err(1, "-r option");
1056 80ddbec8 2018-04-29 stsp /* NOTREACHED */
1060 80ddbec8 2018-04-29 stsp argc -= optind;
1061 80ddbec8 2018-04-29 stsp argv += optind;
1063 ecb28ae0 2018-07-16 stsp if (argc == 0)
1064 ecb28ae0 2018-07-16 stsp path = strdup("");
1065 ecb28ae0 2018-07-16 stsp else if (argc == 1)
1066 ecb28ae0 2018-07-16 stsp path = strdup(argv[0]);
1068 80ddbec8 2018-04-29 stsp usage_log();
1069 ecb28ae0 2018-07-16 stsp if (path == NULL)
1070 ecb28ae0 2018-07-16 stsp return got_error_from_errno();
1072 ecb28ae0 2018-07-16 stsp cwd = getcwd(NULL, 0);
1073 ecb28ae0 2018-07-16 stsp if (cwd == NULL) {
1074 ecb28ae0 2018-07-16 stsp error = got_error_from_errno();
1075 ecb28ae0 2018-07-16 stsp goto done;
1077 ecb28ae0 2018-07-16 stsp if (repo_path == NULL) {
1078 ecb28ae0 2018-07-16 stsp repo_path = strdup(cwd);
1079 ecb28ae0 2018-07-16 stsp if (repo_path == NULL) {
1080 ecb28ae0 2018-07-16 stsp error = got_error_from_errno();
1081 ecb28ae0 2018-07-16 stsp goto done;
1085 80ddbec8 2018-04-29 stsp error = got_repo_open(&repo, repo_path);
1086 80ddbec8 2018-04-29 stsp if (error != NULL)
1087 ecb28ae0 2018-07-16 stsp goto done;
1089 80ddbec8 2018-04-29 stsp if (start_commit == NULL) {
1090 899d86c2 2018-05-10 stsp error = get_head_commit_id(&start_id, repo);
1091 80ddbec8 2018-04-29 stsp if (error != NULL)
1092 ecb28ae0 2018-07-16 stsp goto done;
1094 80ddbec8 2018-04-29 stsp struct got_object *obj;
1095 80ddbec8 2018-04-29 stsp error = got_object_open_by_id_str(&obj, repo, start_commit);
1096 80ddbec8 2018-04-29 stsp if (error == NULL) {
1097 899d86c2 2018-05-10 stsp start_id = got_object_get_id(obj);
1098 899d86c2 2018-05-10 stsp if (start_id == NULL)
1099 80ddbec8 2018-04-29 stsp error = got_error_from_errno();
1100 ecb28ae0 2018-07-16 stsp goto done;
1103 80ddbec8 2018-04-29 stsp if (error != NULL)
1104 ecb28ae0 2018-07-16 stsp goto done;
1106 b3665f43 2018-08-04 stsp view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_LOG);
1107 04cc582a 2018-08-01 stsp if (view == NULL) {
1108 04cc582a 2018-08-01 stsp error = got_error_from_errno();
1109 04cc582a 2018-08-01 stsp goto done;
1111 ba4f502b 2018-08-04 stsp error = open_log_view(view, start_id, repo, path);
1112 ba4f502b 2018-08-04 stsp if (error)
1113 ba4f502b 2018-08-04 stsp goto done;
1114 ba4f502b 2018-08-04 stsp error = show_log_view(view);
1115 ba4f502b 2018-08-04 stsp close_log_view(view);
1116 96a765a8 2018-08-04 stsp view_close(view);
1118 ecb28ae0 2018-07-16 stsp free(repo_path);
1119 ecb28ae0 2018-07-16 stsp free(cwd);
1120 ecb28ae0 2018-07-16 stsp free(path);
1121 899d86c2 2018-05-10 stsp free(start_id);
1123 ecb28ae0 2018-07-16 stsp got_repo_close(repo);
1124 80ddbec8 2018-04-29 stsp return error;
1127 4ed7e80c 2018-05-20 stsp __dead static void
1128 9f7d7167 2018-04-29 stsp usage_diff(void)
1131 9f7d7167 2018-04-29 stsp fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1132 9f7d7167 2018-04-29 stsp getprogname());
1136 b304db33 2018-05-20 stsp static char *
1137 b304db33 2018-05-20 stsp parse_next_line(FILE *f, size_t *len)
1139 b304db33 2018-05-20 stsp char *line;
1140 b304db33 2018-05-20 stsp size_t linelen;
1141 b304db33 2018-05-20 stsp size_t lineno;
1142 b304db33 2018-05-20 stsp const char delim[3] = { '\0', '\0', '\0'};
1144 b304db33 2018-05-20 stsp line = fparseln(f, &linelen, &lineno, delim, 0);
1146 b304db33 2018-05-20 stsp *len = linelen;
1147 b304db33 2018-05-20 stsp return line;
1150 4ed7e80c 2018-05-20 stsp static const struct got_error *
1151 f7d12f7e 2018-08-01 stsp draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1152 a70480e0 2018-06-23 stsp int *last_displayed_line, int *eof, int max_lines)
1154 61e69b96 2018-05-20 stsp const struct got_error *err;
1155 26ed57b2 2018-05-19 stsp int nlines = 0, nprinted = 0;
1156 b304db33 2018-05-20 stsp char *line;
1157 b304db33 2018-05-20 stsp size_t len;
1158 61e69b96 2018-05-20 stsp wchar_t *wline;
1159 e0b650dd 2018-05-20 stsp int width;
1161 26ed57b2 2018-05-19 stsp rewind(f);
1162 f7d12f7e 2018-08-01 stsp werase(view->window);
1165 26ed57b2 2018-05-19 stsp while (nprinted < max_lines) {
1166 b304db33 2018-05-20 stsp line = parse_next_line(f, &len);
1167 26ed57b2 2018-05-19 stsp if (line == NULL) {
1171 26ed57b2 2018-05-19 stsp if (++nlines < *first_displayed_line) {
1172 26ed57b2 2018-05-19 stsp free(line);
1176 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
1177 61e69b96 2018-05-20 stsp if (err) {
1178 61e69b96 2018-05-20 stsp free(line);
1179 2550e4c3 2018-07-13 stsp free(wline);
1180 61e69b96 2018-05-20 stsp return err;
1182 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
1183 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
1184 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
1185 26ed57b2 2018-05-19 stsp if (++nprinted == 1)
1186 26ed57b2 2018-05-19 stsp *first_displayed_line = nlines;
1187 26ed57b2 2018-05-19 stsp free(line);
1188 2550e4c3 2018-07-13 stsp free(wline);
1189 2550e4c3 2018-07-13 stsp wline = NULL;
1191 26ed57b2 2018-05-19 stsp *last_displayed_line = nlines;
1193 26ed57b2 2018-05-19 stsp update_panels();
1194 26ed57b2 2018-05-19 stsp doupdate();
1196 26ed57b2 2018-05-19 stsp return NULL;
1199 4ed7e80c 2018-05-20 stsp static const struct got_error *
1200 5dc9f4bc 2018-08-04 stsp open_diff_view(struct tog_view *view, struct got_object *obj1,
1201 ea5e7bb5 2018-08-01 stsp struct got_object *obj2, struct got_repository *repo)
1203 26ed57b2 2018-05-19 stsp const struct got_error *err;
1206 cd0acaa7 2018-05-20 stsp if (obj1 != NULL && obj2 != NULL &&
1207 cd0acaa7 2018-05-20 stsp got_object_get_type(obj1) != got_object_get_type(obj2))
1208 26ed57b2 2018-05-19 stsp return got_error(GOT_ERR_OBJ_TYPE);
1210 511a516b 2018-05-19 stsp f = got_opentemp();
1211 26ed57b2 2018-05-19 stsp if (f == NULL)
1212 26ed57b2 2018-05-19 stsp return got_error_from_errno();
1214 cd0acaa7 2018-05-20 stsp switch (got_object_get_type(obj1 ? obj1 : obj2)) {
1215 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
1216 11528a82 2018-05-19 stsp err = got_diff_objects_as_blobs(obj1, obj2, repo, f);
1218 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
1219 11528a82 2018-05-19 stsp err = got_diff_objects_as_trees(obj1, obj2, repo, f);
1221 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_COMMIT:
1222 11528a82 2018-05-19 stsp err = got_diff_objects_as_commits(obj1, obj2, repo, f);
1225 26ed57b2 2018-05-19 stsp return got_error(GOT_ERR_OBJ_TYPE);
1228 26ed57b2 2018-05-19 stsp fflush(f);
1230 5dc9f4bc 2018-08-04 stsp view->state.diff.f = f;
1231 5dc9f4bc 2018-08-04 stsp view->state.diff.first_displayed_line = 1;
1232 5dc9f4bc 2018-08-04 stsp view->state.diff.last_displayed_line = view->nlines;
1234 5dc9f4bc 2018-08-04 stsp return NULL;
1237 5dc9f4bc 2018-08-04 stsp static void
1238 5dc9f4bc 2018-08-04 stsp close_diff_view(struct tog_view *view)
1240 5dc9f4bc 2018-08-04 stsp fclose(view->state.diff.f);
1243 5dc9f4bc 2018-08-04 stsp static const struct got_error *
1244 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
1246 5dc9f4bc 2018-08-04 stsp const struct got_error *err = NULL;
1247 5dc9f4bc 2018-08-04 stsp int ch, done = 0;
1248 5dc9f4bc 2018-08-04 stsp int eof, i;
1249 c90732a0 2018-08-04 stsp struct tog_diff_view_state *state = &view->state.diff;
1251 6acd1bd1 2018-08-04 stsp view_show(view);
1253 26ed57b2 2018-05-19 stsp while (!done) {
1254 c90732a0 2018-08-04 stsp err = draw_file(view, state->f, &state->first_displayed_line,
1255 c90732a0 2018-08-04 stsp &state->last_displayed_line, &eof, view->nlines);
1258 26ed57b2 2018-05-19 stsp nodelay(stdscr, FALSE);
1259 ea5e7bb5 2018-08-01 stsp ch = wgetch(view->window);
1260 f7182337 2018-05-20 stsp nodelay(stdscr, TRUE);
1261 26ed57b2 2018-05-19 stsp switch (ch) {
1266 26ed57b2 2018-05-19 stsp case KEY_UP:
1267 c90732a0 2018-08-04 stsp if (state->first_displayed_line > 1)
1268 c90732a0 2018-08-04 stsp state->first_displayed_line--;
1270 b304db33 2018-05-20 stsp case KEY_PPAGE:
1271 c46b9352 2018-07-12 stsp case KEY_BACKSPACE:
1273 f7d12f7e 2018-08-01 stsp while (i++ < view->nlines - 1 &&
1274 c90732a0 2018-08-04 stsp state->first_displayed_line > 1)
1275 c90732a0 2018-08-04 stsp state->first_displayed_line--;
1278 26ed57b2 2018-05-19 stsp case KEY_DOWN:
1280 c90732a0 2018-08-04 stsp state->first_displayed_line++;
1282 b304db33 2018-05-20 stsp case KEY_NPAGE:
1285 f7d12f7e 2018-08-01 stsp while (!eof && i++ < view->nlines - 1) {
1286 5dc9f4bc 2018-08-04 stsp char *line = parse_next_line(
1287 c90732a0 2018-08-04 stsp state->f, NULL);
1288 c90732a0 2018-08-04 stsp state->first_displayed_line++;
1289 b304db33 2018-05-20 stsp if (line == NULL)
1293 82357c0a 2018-08-01 stsp case KEY_RESIZE:
1294 a41d2007 2018-08-01 stsp err = view_resize(view);
1296 a41d2007 2018-08-01 stsp goto done;
1303 26ed57b2 2018-05-19 stsp return err;
1306 4ed7e80c 2018-05-20 stsp static const struct got_error *
1307 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
1309 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
1310 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
1311 26ed57b2 2018-05-19 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
1312 26ed57b2 2018-05-19 stsp char *repo_path = NULL;
1313 26ed57b2 2018-05-19 stsp char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
1315 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
1317 26ed57b2 2018-05-19 stsp #ifndef PROFILE
1318 26ed57b2 2018-05-19 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1319 26ed57b2 2018-05-19 stsp err(1, "pledge");
1322 26ed57b2 2018-05-19 stsp while ((ch = getopt(argc, argv, "")) != -1) {
1323 26ed57b2 2018-05-19 stsp switch (ch) {
1326 26ed57b2 2018-05-19 stsp /* NOTREACHED */
1330 26ed57b2 2018-05-19 stsp argc -= optind;
1331 26ed57b2 2018-05-19 stsp argv += optind;
1333 26ed57b2 2018-05-19 stsp if (argc == 0) {
1334 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
1335 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
1336 26ed57b2 2018-05-19 stsp repo_path = getcwd(NULL, 0);
1337 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
1338 26ed57b2 2018-05-19 stsp return got_error_from_errno();
1339 26ed57b2 2018-05-19 stsp obj_id_str1 = argv[0];
1340 26ed57b2 2018-05-19 stsp obj_id_str2 = argv[1];
1341 26ed57b2 2018-05-19 stsp } else if (argc == 3) {
1342 26ed57b2 2018-05-19 stsp repo_path = realpath(argv[0], NULL);
1343 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
1344 26ed57b2 2018-05-19 stsp return got_error_from_errno();
1345 26ed57b2 2018-05-19 stsp obj_id_str1 = argv[1];
1346 26ed57b2 2018-05-19 stsp obj_id_str2 = argv[2];
1348 26ed57b2 2018-05-19 stsp usage_diff();
1350 26ed57b2 2018-05-19 stsp error = got_repo_open(&repo, repo_path);
1351 26ed57b2 2018-05-19 stsp free(repo_path);
1352 26ed57b2 2018-05-19 stsp if (error)
1353 26ed57b2 2018-05-19 stsp goto done;
1355 26ed57b2 2018-05-19 stsp error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
1356 26ed57b2 2018-05-19 stsp if (error)
1357 26ed57b2 2018-05-19 stsp goto done;
1359 26ed57b2 2018-05-19 stsp error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1360 26ed57b2 2018-05-19 stsp if (error)
1361 26ed57b2 2018-05-19 stsp goto done;
1363 b3665f43 2018-08-04 stsp view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_DIFF);
1364 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
1365 ea5e7bb5 2018-08-01 stsp error = got_error_from_errno();
1366 ea5e7bb5 2018-08-01 stsp goto done;
1368 5dc9f4bc 2018-08-04 stsp error = open_diff_view(view, obj1, obj2, repo);
1369 5dc9f4bc 2018-08-04 stsp if (error)
1370 5dc9f4bc 2018-08-04 stsp goto done;
1371 5dc9f4bc 2018-08-04 stsp error = show_diff_view(view);
1372 5dc9f4bc 2018-08-04 stsp close_diff_view(view);
1373 96a765a8 2018-08-04 stsp view_close(view);
1375 26ed57b2 2018-05-19 stsp got_repo_close(repo);
1377 26ed57b2 2018-05-19 stsp got_object_close(obj1);
1379 26ed57b2 2018-05-19 stsp got_object_close(obj2);
1380 26ed57b2 2018-05-19 stsp return error;
1383 4ed7e80c 2018-05-20 stsp __dead static void
1384 9f7d7167 2018-04-29 stsp usage_blame(void)
1387 69069811 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
1388 9f7d7167 2018-04-29 stsp getprogname());
1392 84451b3e 2018-07-10 stsp struct tog_blame_line {
1393 84451b3e 2018-07-10 stsp int annotated;
1394 84451b3e 2018-07-10 stsp struct got_object_id *id;
1397 4ed7e80c 2018-05-20 stsp static const struct got_error *
1398 f7d12f7e 2018-08-01 stsp draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
1399 f7d12f7e 2018-08-01 stsp const char *path, struct tog_blame_line *lines, int nlines,
1400 f7d12f7e 2018-08-01 stsp int blame_complete, int selected_line, int *first_displayed_line,
1401 f7d12f7e 2018-08-01 stsp int *last_displayed_line, int *eof, int max_lines)
1403 84451b3e 2018-07-10 stsp const struct got_error *err;
1404 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
1405 84451b3e 2018-07-10 stsp char *line;
1406 84451b3e 2018-07-10 stsp size_t len;
1407 84451b3e 2018-07-10 stsp wchar_t *wline;
1408 b700b5d6 2018-07-10 stsp int width, wlimit;
1409 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
1410 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
1411 ab089a2a 2018-07-12 stsp char *id_str;
1413 ab089a2a 2018-07-12 stsp err = got_object_id_str(&id_str, id);
1415 ab089a2a 2018-07-12 stsp return err;
1417 84451b3e 2018-07-10 stsp rewind(f);
1418 f7d12f7e 2018-08-01 stsp werase(view->window);
1420 ab089a2a 2018-07-12 stsp if (asprintf(&line, "commit: %s", id_str) == -1) {
1421 ab089a2a 2018-07-12 stsp err = got_error_from_errno();
1422 ab089a2a 2018-07-12 stsp free(id_str);
1423 ab089a2a 2018-07-12 stsp return err;
1426 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
1427 ab089a2a 2018-07-12 stsp free(line);
1428 2550e4c3 2018-07-13 stsp line = NULL;
1429 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
1430 2550e4c3 2018-07-13 stsp free(wline);
1431 2550e4c3 2018-07-13 stsp wline = NULL;
1432 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
1433 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
1435 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
1436 084063cd 2018-07-12 stsp *first_displayed_line - 1 + selected_line, nlines,
1437 12a0b23b 2018-07-12 stsp blame_complete ? "" : "annotating ", path) == -1) {
1438 ab089a2a 2018-07-12 stsp free(id_str);
1439 3f60a8ef 2018-07-10 stsp return got_error_from_errno();
1441 ab089a2a 2018-07-12 stsp free(id_str);
1442 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
1443 3f60a8ef 2018-07-10 stsp free(line);
1444 2550e4c3 2018-07-13 stsp line = NULL;
1446 3f60a8ef 2018-07-10 stsp return err;
1447 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
1448 2550e4c3 2018-07-13 stsp free(wline);
1449 2550e4c3 2018-07-13 stsp wline = NULL;
1450 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
1451 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
1454 ab089a2a 2018-07-12 stsp while (nprinted < max_lines - 2) {
1455 84451b3e 2018-07-10 stsp line = parse_next_line(f, &len);
1456 84451b3e 2018-07-10 stsp if (line == NULL) {
1460 84451b3e 2018-07-10 stsp if (++lineno < *first_displayed_line) {
1461 84451b3e 2018-07-10 stsp free(line);
1465 f7d12f7e 2018-08-01 stsp wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
1466 b700b5d6 2018-07-10 stsp err = format_line(&wline, &width, line, wlimit);
1467 84451b3e 2018-07-10 stsp if (err) {
1468 84451b3e 2018-07-10 stsp free(line);
1469 84451b3e 2018-07-10 stsp return err;
1472 b700b5d6 2018-07-10 stsp if (nprinted == selected_line - 1)
1473 f7d12f7e 2018-08-01 stsp wstandout(view->window);
1475 84451b3e 2018-07-10 stsp blame_line = &lines[lineno - 1];
1476 ee41ec32 2018-07-10 stsp if (blame_line->annotated && prev_id &&
1477 ee41ec32 2018-07-10 stsp got_object_id_cmp(prev_id, blame_line->id) == 0)
1478 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ");
1479 ee41ec32 2018-07-10 stsp else if (blame_line->annotated) {
1480 84451b3e 2018-07-10 stsp char *id_str;
1481 84451b3e 2018-07-10 stsp err = got_object_id_str(&id_str, blame_line->id);
1482 84451b3e 2018-07-10 stsp if (err) {
1483 84451b3e 2018-07-10 stsp free(line);
1484 2550e4c3 2018-07-13 stsp free(wline);
1485 84451b3e 2018-07-10 stsp return err;
1487 f7d12f7e 2018-08-01 stsp wprintw(view->window, "%.8s ", id_str);
1488 84451b3e 2018-07-10 stsp free(id_str);
1489 ee41ec32 2018-07-10 stsp prev_id = blame_line->id;
1491 f7d12f7e 2018-08-01 stsp waddstr(view->window, "........ ");
1492 ee41ec32 2018-07-10 stsp prev_id = NULL;
1495 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
1496 b700b5d6 2018-07-10 stsp while (width < wlimit) {
1497 f7d12f7e 2018-08-01 stsp waddch(view->window, ' ');
1500 b700b5d6 2018-07-10 stsp if (nprinted == selected_line - 1)
1501 f7d12f7e 2018-08-01 stsp wstandend(view->window);
1502 84451b3e 2018-07-10 stsp if (++nprinted == 1)
1503 84451b3e 2018-07-10 stsp *first_displayed_line = lineno;
1504 84451b3e 2018-07-10 stsp free(line);
1505 2550e4c3 2018-07-13 stsp free(wline);
1506 2550e4c3 2018-07-13 stsp wline = NULL;
1508 84451b3e 2018-07-10 stsp *last_displayed_line = lineno;
1510 84451b3e 2018-07-10 stsp update_panels();
1511 84451b3e 2018-07-10 stsp doupdate();
1513 84451b3e 2018-07-10 stsp return NULL;
1516 84451b3e 2018-07-10 stsp struct tog_blame_cb_args {
1517 84451b3e 2018-07-10 stsp pthread_mutex_t *mutex;
1518 84451b3e 2018-07-10 stsp struct tog_blame_line *lines; /* one per line */
1519 84451b3e 2018-07-10 stsp int nlines;
1521 7cc84d77 2018-08-01 stsp struct tog_view *view;
1522 ab089a2a 2018-07-12 stsp struct got_object_id *commit_id;
1524 3f60a8ef 2018-07-10 stsp const char *path;
1525 84451b3e 2018-07-10 stsp int *first_displayed_line;
1526 84451b3e 2018-07-10 stsp int *last_displayed_line;
1527 b700b5d6 2018-07-10 stsp int *selected_line;
1528 18430de3 2018-07-10 stsp int *quit;
1531 84451b3e 2018-07-10 stsp static const struct got_error *
1532 84451b3e 2018-07-10 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
1534 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
1535 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
1536 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
1539 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
1540 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
1541 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
1543 84451b3e 2018-07-10 stsp if (pthread_mutex_lock(a->mutex) != 0)
1544 84451b3e 2018-07-10 stsp return got_error_from_errno();
1546 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
1547 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
1548 d68a0a7d 2018-07-10 stsp goto done;
1551 d68a0a7d 2018-07-10 stsp if (lineno == -1)
1552 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
1554 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
1555 d68a0a7d 2018-07-10 stsp if (line->annotated)
1556 d68a0a7d 2018-07-10 stsp goto done;
1558 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
1559 84451b3e 2018-07-10 stsp if (line->id == NULL) {
1560 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1561 84451b3e 2018-07-10 stsp goto done;
1563 84451b3e 2018-07-10 stsp line->annotated = 1;
1565 f7d12f7e 2018-08-01 stsp err = draw_blame(a->view, a->commit_id, a->f, a->path,
1566 245d91c1 2018-07-12 stsp a->lines, a->nlines, 0, *a->selected_line, a->first_displayed_line,
1567 f7d12f7e 2018-08-01 stsp a->last_displayed_line, &eof, a->view->nlines);
1569 84451b3e 2018-07-10 stsp if (pthread_mutex_unlock(a->mutex) != 0)
1570 84451b3e 2018-07-10 stsp return got_error_from_errno();
1571 84451b3e 2018-07-10 stsp return err;
1574 84451b3e 2018-07-10 stsp struct tog_blame_thread_args {
1575 84451b3e 2018-07-10 stsp const char *path;
1576 84451b3e 2018-07-10 stsp struct got_repository *repo;
1577 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *cb_args;
1578 18430de3 2018-07-10 stsp int *complete;
1581 84451b3e 2018-07-10 stsp static void *
1582 84451b3e 2018-07-10 stsp blame_thread(void *arg)
1584 18430de3 2018-07-10 stsp const struct got_error *err;
1585 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
1586 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
1589 ab089a2a 2018-07-12 stsp err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
1590 245d91c1 2018-07-12 stsp blame_cb, ta->cb_args);
1592 18430de3 2018-07-10 stsp if (pthread_mutex_lock(a->mutex) != 0)
1593 18430de3 2018-07-10 stsp return (void *)got_error_from_errno();
1595 c9beca56 2018-07-22 stsp got_repo_close(ta->repo);
1596 c9beca56 2018-07-22 stsp ta->repo = NULL;
1597 c9beca56 2018-07-22 stsp *ta->complete = 1;
1599 f7d12f7e 2018-08-01 stsp err = draw_blame(a->view, a->commit_id, a->f, a->path,
1600 f7d12f7e 2018-08-01 stsp a->lines, a->nlines, 1, *a->selected_line,
1601 c9beca56 2018-07-22 stsp a->first_displayed_line, a->last_displayed_line, &eof,
1602 f7d12f7e 2018-08-01 stsp a->view->nlines);
1604 18430de3 2018-07-10 stsp if (pthread_mutex_unlock(a->mutex) != 0 && err == NULL)
1605 18430de3 2018-07-10 stsp err = got_error_from_errno();
1607 18430de3 2018-07-10 stsp return (void *)err;
1610 245d91c1 2018-07-12 stsp static struct got_object_id *
1611 245d91c1 2018-07-12 stsp get_selected_commit_id(struct tog_blame_line *lines,
1612 245d91c1 2018-07-12 stsp int first_displayed_line, int selected_line)
1614 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
1616 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
1617 245d91c1 2018-07-12 stsp if (!line->annotated)
1618 245d91c1 2018-07-12 stsp return NULL;
1620 245d91c1 2018-07-12 stsp return line->id;
1623 84451b3e 2018-07-10 stsp static const struct got_error *
1624 245d91c1 2018-07-12 stsp open_selected_commit(struct got_object **pobj, struct got_object **obj,
1625 b880a918 2018-07-10 stsp struct tog_blame_line *lines, int first_displayed_line,
1626 b880a918 2018-07-10 stsp int selected_line, struct got_repository *repo)
1628 b880a918 2018-07-10 stsp const struct got_error *err = NULL;
1629 b880a918 2018-07-10 stsp struct got_commit_object *commit = NULL;
1630 245d91c1 2018-07-12 stsp struct got_object_id *selected_id;
1631 b880a918 2018-07-10 stsp struct got_object_qid *pid;
1633 b880a918 2018-07-10 stsp *pobj = NULL;
1634 b880a918 2018-07-10 stsp *obj = NULL;
1636 245d91c1 2018-07-12 stsp selected_id = get_selected_commit_id(lines,
1637 245d91c1 2018-07-12 stsp first_displayed_line, selected_line);
1638 245d91c1 2018-07-12 stsp if (selected_id == NULL)
1639 b880a918 2018-07-10 stsp return NULL;
1641 245d91c1 2018-07-12 stsp err = got_object_open(obj, repo, selected_id);
1643 b880a918 2018-07-10 stsp goto done;
1645 b880a918 2018-07-10 stsp err = got_object_commit_open(&commit, repo, *obj);
1647 b880a918 2018-07-10 stsp goto done;
1649 b880a918 2018-07-10 stsp pid = SIMPLEQ_FIRST(&commit->parent_ids);
1650 b880a918 2018-07-10 stsp if (pid) {
1651 b880a918 2018-07-10 stsp err = got_object_open(pobj, repo, pid->id);
1653 b880a918 2018-07-10 stsp goto done;
1656 b880a918 2018-07-10 stsp if (commit)
1657 b880a918 2018-07-10 stsp got_object_commit_close(commit);
1658 b880a918 2018-07-10 stsp return err;
1661 245d91c1 2018-07-12 stsp struct tog_blame {
1663 245d91c1 2018-07-12 stsp size_t filesize;
1664 245d91c1 2018-07-12 stsp struct tog_blame_line *lines;
1665 245d91c1 2018-07-12 stsp size_t nlines;
1666 245d91c1 2018-07-12 stsp pthread_t thread;
1667 245d91c1 2018-07-12 stsp struct tog_blame_thread_args thread_args;
1668 245d91c1 2018-07-12 stsp struct tog_blame_cb_args cb_args;
1669 245d91c1 2018-07-12 stsp const char *path;
1672 b880a918 2018-07-10 stsp static const struct got_error *
1673 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
1675 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
1678 245d91c1 2018-07-12 stsp if (blame->thread) {
1679 245d91c1 2018-07-12 stsp if (pthread_join(blame->thread, (void **)&err) != 0)
1680 245d91c1 2018-07-12 stsp err = got_error_from_errno();
1681 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
1682 245d91c1 2018-07-12 stsp err = NULL;
1683 245d91c1 2018-07-12 stsp blame->thread = NULL;
1685 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
1686 245d91c1 2018-07-12 stsp got_repo_close(blame->thread_args.repo);
1687 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
1689 245d91c1 2018-07-12 stsp if (blame->f) {
1690 245d91c1 2018-07-12 stsp fclose(blame->f);
1691 245d91c1 2018-07-12 stsp blame->f = NULL;
1693 245d91c1 2018-07-12 stsp for (i = 0; i < blame->nlines; i++)
1694 245d91c1 2018-07-12 stsp free(blame->lines[i].id);
1695 245d91c1 2018-07-12 stsp free(blame->lines);
1696 245d91c1 2018-07-12 stsp blame->lines = NULL;
1697 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
1698 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
1700 245d91c1 2018-07-12 stsp return err;
1703 245d91c1 2018-07-12 stsp static const struct got_error *
1704 7cc84d77 2018-08-01 stsp run_blame(struct tog_blame *blame, pthread_mutex_t *mutex,
1705 7cc84d77 2018-08-01 stsp struct tog_view *view, int *blame_complete,
1706 245d91c1 2018-07-12 stsp int *first_displayed_line, int *last_displayed_line,
1707 245d91c1 2018-07-12 stsp int *selected_line, int *done, const char *path,
1708 245d91c1 2018-07-12 stsp struct got_object_id *commit_id,
1709 245d91c1 2018-07-12 stsp struct got_repository *repo)
1711 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
1712 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
1713 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
1714 245d91c1 2018-07-12 stsp struct got_object *obj;
1716 84451b3e 2018-07-10 stsp err = got_object_open_by_path(&obj, repo, commit_id, path);
1718 84451b3e 2018-07-10 stsp goto done;
1719 84451b3e 2018-07-10 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
1720 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1721 84451b3e 2018-07-10 stsp goto done;
1724 84451b3e 2018-07-10 stsp err = got_object_blob_open(&blob, repo, obj, 8192);
1726 a70480e0 2018-06-23 stsp goto done;
1727 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
1728 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
1729 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1730 84451b3e 2018-07-10 stsp goto done;
1732 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
1733 245d91c1 2018-07-12 stsp blame->f, blob);
1735 84451b3e 2018-07-10 stsp goto done;
1737 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
1738 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
1739 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1740 84451b3e 2018-07-10 stsp goto done;
1743 245d91c1 2018-07-12 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1745 bd24772e 2018-07-11 stsp goto done;
1747 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
1748 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
1749 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
1750 245d91c1 2018-07-12 stsp blame->cb_args.mutex = mutex;
1751 245d91c1 2018-07-12 stsp blame->cb_args.commit_id = got_object_id_dup(commit_id);
1752 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
1753 245d91c1 2018-07-12 stsp err = got_error_from_errno();
1754 245d91c1 2018-07-12 stsp goto done;
1756 245d91c1 2018-07-12 stsp blame->cb_args.f = blame->f;
1757 245d91c1 2018-07-12 stsp blame->cb_args.path = path;
1758 245d91c1 2018-07-12 stsp blame->cb_args.first_displayed_line = first_displayed_line;
1759 245d91c1 2018-07-12 stsp blame->cb_args.selected_line = selected_line;
1760 245d91c1 2018-07-12 stsp blame->cb_args.last_displayed_line = last_displayed_line;
1761 245d91c1 2018-07-12 stsp blame->cb_args.quit = done;
1763 245d91c1 2018-07-12 stsp blame->thread_args.path = path;
1764 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
1765 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
1766 245d91c1 2018-07-12 stsp blame->thread_args.complete = blame_complete;
1767 245d91c1 2018-07-12 stsp *blame_complete = 0;
1769 245d91c1 2018-07-12 stsp if (pthread_create(&blame->thread, NULL, blame_thread,
1770 245d91c1 2018-07-12 stsp &blame->thread_args) != 0) {
1771 245d91c1 2018-07-12 stsp err = got_error_from_errno();
1772 245d91c1 2018-07-12 stsp goto done;
1777 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
1779 245d91c1 2018-07-12 stsp got_object_close(obj);
1781 245d91c1 2018-07-12 stsp stop_blame(blame);
1782 245d91c1 2018-07-12 stsp return err;
1785 245d91c1 2018-07-12 stsp static const struct got_error *
1786 7cbe629d 2018-08-04 stsp open_blame_view(struct tog_view *view, const char *path,
1787 e1cd8fed 2018-08-01 stsp struct got_object_id *commit_id, struct got_repository *repo)
1789 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
1790 7cbe629d 2018-08-04 stsp struct tog_blame_view_state *state = &view->state.blame;
1792 7cbe629d 2018-08-04 stsp SIMPLEQ_INIT(&state->blamed_commits);
1794 7cbe629d 2018-08-04 stsp if (pthread_mutex_init(&state->mutex, NULL) != 0)
1795 7cbe629d 2018-08-04 stsp return got_error_from_errno();
1797 7cbe629d 2018-08-04 stsp err = got_object_qid_alloc(&state->blamed_commit, commit_id);
1799 7cbe629d 2018-08-04 stsp return err;
1801 7cbe629d 2018-08-04 stsp SIMPLEQ_INSERT_HEAD(&state->blamed_commits, state->blamed_commit,
1803 7cbe629d 2018-08-04 stsp state->first_displayed_line = 1;
1804 7cbe629d 2018-08-04 stsp state->last_displayed_line = view->nlines;
1805 7cbe629d 2018-08-04 stsp state->selected_line = 1;
1806 7cbe629d 2018-08-04 stsp state->blame_complete = 0;
1807 7cbe629d 2018-08-04 stsp state->path = path;
1808 7cbe629d 2018-08-04 stsp state->repo = repo;
1809 7cbe629d 2018-08-04 stsp state->commit_id = commit_id;
1811 7cbe629d 2018-08-04 stsp return NULL;
1814 7cbe629d 2018-08-04 stsp static void
1815 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
1817 7cbe629d 2018-08-04 stsp struct tog_blame_view_state *state = &view->state.blame;
1819 7cbe629d 2018-08-04 stsp while (!SIMPLEQ_EMPTY(&state->blamed_commits)) {
1820 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
1821 7cbe629d 2018-08-04 stsp blamed_commit = SIMPLEQ_FIRST(&state->blamed_commits);
1822 7cbe629d 2018-08-04 stsp SIMPLEQ_REMOVE_HEAD(&state->blamed_commits, entry);
1823 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
1827 7cbe629d 2018-08-04 stsp static const struct got_error *
1828 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
1830 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
1831 7cbe629d 2018-08-04 stsp int ch, done = 0, eof;
1832 7cbe629d 2018-08-04 stsp struct got_object *obj = NULL, *pobj = NULL;
1833 7cbe629d 2018-08-04 stsp struct tog_blame blame;
1834 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
1835 7cbe629d 2018-08-04 stsp struct tog_blame_view_state *state = &view->state.blame;
1837 6acd1bd1 2018-08-04 stsp view_show(view);
1839 245d91c1 2018-07-12 stsp memset(&blame, 0, sizeof(blame));
1840 7cbe629d 2018-08-04 stsp err = run_blame(&blame, &state->mutex, view, &state->blame_complete,
1841 7cbe629d 2018-08-04 stsp &state->first_displayed_line, &state->last_displayed_line,
1842 7cbe629d 2018-08-04 stsp &state->selected_line, &done, state->path,
1843 7cbe629d 2018-08-04 stsp state->blamed_commit->id, state->repo);
1845 245d91c1 2018-07-12 stsp return err;
1847 a70480e0 2018-06-23 stsp while (!done) {
1848 7cbe629d 2018-08-04 stsp if (pthread_mutex_lock(&state->mutex) != 0) {
1849 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1850 84451b3e 2018-07-10 stsp goto done;
1852 7cbe629d 2018-08-04 stsp err = draw_blame(view, state->blamed_commit->id, blame.f,
1853 7cbe629d 2018-08-04 stsp state->path, blame.lines, blame.nlines,
1854 7cbe629d 2018-08-04 stsp state->blame_complete, state->selected_line,
1855 7cbe629d 2018-08-04 stsp &state->first_displayed_line, &state->last_displayed_line,
1856 7cbe629d 2018-08-04 stsp &eof, view->nlines);
1857 7cbe629d 2018-08-04 stsp if (pthread_mutex_unlock(&state->mutex) != 0) {
1858 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1859 84451b3e 2018-07-10 stsp goto done;
1863 a70480e0 2018-06-23 stsp nodelay(stdscr, FALSE);
1864 7cc84d77 2018-08-01 stsp ch = wgetch(view->window);
1865 a70480e0 2018-06-23 stsp nodelay(stdscr, TRUE);
1866 7cbe629d 2018-08-04 stsp if (pthread_mutex_lock(&state->mutex) != 0) {
1867 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1868 84451b3e 2018-07-10 stsp goto done;
1870 a70480e0 2018-06-23 stsp switch (ch) {
1875 a70480e0 2018-06-23 stsp case KEY_UP:
1876 7cbe629d 2018-08-04 stsp if (state->selected_line > 1)
1877 7cbe629d 2018-08-04 stsp state->selected_line--;
1878 7cbe629d 2018-08-04 stsp else if (state->selected_line == 1 &&
1879 7cbe629d 2018-08-04 stsp state->first_displayed_line > 1)
1880 7cbe629d 2018-08-04 stsp state->first_displayed_line--;
1882 a70480e0 2018-06-23 stsp case KEY_PPAGE:
1883 38f94530 2018-07-12 stsp case KEY_BACKSPACE:
1884 7cbe629d 2018-08-04 stsp if (state->first_displayed_line == 1) {
1885 7cbe629d 2018-08-04 stsp state->selected_line = 1;
1888 7cbe629d 2018-08-04 stsp if (state->first_displayed_line >
1889 7cbe629d 2018-08-04 stsp view->nlines - 2)
1890 7cbe629d 2018-08-04 stsp state->first_displayed_line -=
1891 f7d12f7e 2018-08-01 stsp (view->nlines - 2);
1893 7cbe629d 2018-08-04 stsp state->first_displayed_line = 1;
1896 a70480e0 2018-06-23 stsp case KEY_DOWN:
1897 7cbe629d 2018-08-04 stsp if (state->selected_line < view->nlines - 2 &&
1898 7cbe629d 2018-08-04 stsp state->first_displayed_line +
1899 7cbe629d 2018-08-04 stsp state->selected_line <= blame.nlines)
1900 7cbe629d 2018-08-04 stsp state->selected_line++;
1901 7cbe629d 2018-08-04 stsp else if (state->last_displayed_line <
1902 a026b947 2018-07-12 stsp blame.nlines)
1903 7cbe629d 2018-08-04 stsp state->first_displayed_line++;
1906 7a2921f9 2018-07-12 stsp case 'p': {
1907 245d91c1 2018-07-12 stsp struct got_object_id *id;
1908 245d91c1 2018-07-12 stsp id = get_selected_commit_id(blame.lines,
1909 7cbe629d 2018-08-04 stsp state->first_displayed_line,
1910 7cbe629d 2018-08-04 stsp state->selected_line);
1911 245d91c1 2018-07-12 stsp if (id == NULL || got_object_id_cmp(id,
1912 7cbe629d 2018-08-04 stsp state->blamed_commit->id) == 0)
1914 245d91c1 2018-07-12 stsp err = open_selected_commit(&pobj, &obj,
1915 7cbe629d 2018-08-04 stsp blame.lines, state->first_displayed_line,
1916 7cbe629d 2018-08-04 stsp state->selected_line, state->repo);
1919 245d91c1 2018-07-12 stsp if (pobj == NULL && obj == NULL)
1921 7a2921f9 2018-07-12 stsp if (ch == 'p' && pobj == NULL)
1924 7cbe629d 2018-08-04 stsp if (pthread_mutex_unlock(&state->mutex) != 0) {
1925 245d91c1 2018-07-12 stsp err = got_error_from_errno();
1926 ad6a6c43 2018-07-10 stsp goto done;
1928 245d91c1 2018-07-12 stsp thread_err = stop_blame(&blame);
1930 7cbe629d 2018-08-04 stsp if (pthread_mutex_lock(&state->mutex) != 0) {
1931 245d91c1 2018-07-12 stsp err = got_error_from_errno();
1932 245d91c1 2018-07-12 stsp goto done;
1934 245d91c1 2018-07-12 stsp if (thread_err)
1936 7a2921f9 2018-07-12 stsp id = got_object_get_id(ch == 'b' ? obj : pobj);
1937 1960a6f9 2018-07-12 stsp got_object_close(obj);
1938 1960a6f9 2018-07-12 stsp obj = NULL;
1939 1960a6f9 2018-07-12 stsp if (pobj) {
1940 1960a6f9 2018-07-12 stsp got_object_close(pobj);
1941 14437fb1 2018-07-13 stsp pobj = NULL;
1943 dbc6a6b6 2018-07-12 stsp if (id == NULL) {
1944 245d91c1 2018-07-12 stsp err = got_error_from_errno();
1947 7cbe629d 2018-08-04 stsp err = got_object_qid_alloc(
1948 7cbe629d 2018-08-04 stsp &state->blamed_commit, id);
1951 dbc6a6b6 2018-07-12 stsp goto done;
1952 7cbe629d 2018-08-04 stsp SIMPLEQ_INSERT_HEAD(&state->blamed_commits,
1953 7cbe629d 2018-08-04 stsp state->blamed_commit, entry);
1954 7cbe629d 2018-08-04 stsp err = run_blame(&blame, &state->mutex, view,
1955 7cbe629d 2018-08-04 stsp &state->blame_complete,
1956 7cbe629d 2018-08-04 stsp &state->first_displayed_line,
1957 7cbe629d 2018-08-04 stsp &state->last_displayed_line,
1958 7cbe629d 2018-08-04 stsp &state->selected_line, &done, state->path,
1959 7cbe629d 2018-08-04 stsp state->blamed_commit->id, state->repo);
1964 dbc6a6b6 2018-07-12 stsp case 'B': {
1965 dbc6a6b6 2018-07-12 stsp struct got_object_qid *first;
1966 7cbe629d 2018-08-04 stsp first = SIMPLEQ_FIRST(&state->blamed_commits);
1967 7cbe629d 2018-08-04 stsp if (!got_object_id_cmp(first->id,
1968 7cbe629d 2018-08-04 stsp state->commit_id))
1971 7cbe629d 2018-08-04 stsp if (pthread_mutex_unlock(&state->mutex) != 0) {
1972 dbc6a6b6 2018-07-12 stsp err = got_error_from_errno();
1973 dbc6a6b6 2018-07-12 stsp goto done;
1975 dbc6a6b6 2018-07-12 stsp thread_err = stop_blame(&blame);
1977 7cbe629d 2018-08-04 stsp if (pthread_mutex_lock(&state->mutex) != 0) {
1978 dbc6a6b6 2018-07-12 stsp err = got_error_from_errno();
1979 dbc6a6b6 2018-07-12 stsp goto done;
1981 dbc6a6b6 2018-07-12 stsp if (thread_err)
1983 7cbe629d 2018-08-04 stsp SIMPLEQ_REMOVE_HEAD(&state->blamed_commits,
1985 7cbe629d 2018-08-04 stsp got_object_qid_free(state->blamed_commit);
1986 7cbe629d 2018-08-04 stsp state->blamed_commit =
1987 7cbe629d 2018-08-04 stsp SIMPLEQ_FIRST(&state->blamed_commits);
1988 7cbe629d 2018-08-04 stsp err = run_blame(&blame, &state->mutex, view,
1989 7cbe629d 2018-08-04 stsp &state->blame_complete,
1990 7cbe629d 2018-08-04 stsp &state->first_displayed_line,
1991 7cbe629d 2018-08-04 stsp &state->last_displayed_line,
1992 7cbe629d 2018-08-04 stsp &state->selected_line, &done, state->path,
1993 7cbe629d 2018-08-04 stsp state->blamed_commit->id, state->repo);
1998 245d91c1 2018-07-12 stsp case KEY_ENTER:
1999 245d91c1 2018-07-12 stsp case '\r':
2000 245d91c1 2018-07-12 stsp err = open_selected_commit(&pobj, &obj,
2001 7cbe629d 2018-08-04 stsp blame.lines, state->first_displayed_line,
2002 7cbe629d 2018-08-04 stsp state->selected_line, state->repo);
2005 b880a918 2018-07-10 stsp if (pobj == NULL && obj == NULL)
2007 b3665f43 2018-08-04 stsp diff_view = view_open(0, 0, 0, 0, view,
2008 d6b05b5b 2018-08-04 stsp TOG_VIEW_DIFF);
2009 ea5e7bb5 2018-08-01 stsp if (diff_view == NULL) {
2010 ea5e7bb5 2018-08-01 stsp err = got_error_from_errno();
2013 7cbe629d 2018-08-04 stsp err = open_diff_view(diff_view, pobj, obj,
2014 7cbe629d 2018-08-04 stsp state->repo);
2017 5dc9f4bc 2018-08-04 stsp err = show_diff_view(diff_view);
2018 5dc9f4bc 2018-08-04 stsp close_diff_view(diff_view);
2019 96a765a8 2018-08-04 stsp view_close(diff_view);
2022 6acd1bd1 2018-08-04 stsp view_show(view);
2023 b880a918 2018-07-10 stsp if (pobj) {
2024 b880a918 2018-07-10 stsp got_object_close(pobj);
2025 b880a918 2018-07-10 stsp pobj = NULL;
2027 b880a918 2018-07-10 stsp got_object_close(obj);
2028 b880a918 2018-07-10 stsp obj = NULL;
2032 a70480e0 2018-06-23 stsp case KEY_NPAGE:
2034 7cbe629d 2018-08-04 stsp if (state->last_displayed_line
2035 7cbe629d 2018-08-04 stsp >= blame.nlines &&
2036 7cbe629d 2018-08-04 stsp state->selected_line < view->nlines - 2) {
2037 7cbe629d 2018-08-04 stsp state->selected_line = MIN(blame.nlines,
2038 f7d12f7e 2018-08-01 stsp view->nlines - 2);
2041 7cbe629d 2018-08-04 stsp if (state->last_displayed_line +
2042 7cbe629d 2018-08-04 stsp view->nlines - 2 <= blame.nlines)
2043 7cbe629d 2018-08-04 stsp state->first_displayed_line +=
2044 f7d12f7e 2018-08-01 stsp view->nlines - 2;
2046 7cbe629d 2018-08-04 stsp state->first_displayed_line =
2047 f7d12f7e 2018-08-01 stsp blame.nlines - (view->nlines - 3);
2049 7e5c1fe1 2018-08-01 stsp case KEY_RESIZE:
2050 a41d2007 2018-08-01 stsp err = view_resize(view);
2053 7cbe629d 2018-08-04 stsp if (state->selected_line > view->nlines - 2) {
2054 7cbe629d 2018-08-04 stsp state->selected_line = MIN(blame.nlines,
2055 7e5c1fe1 2018-08-01 stsp view->nlines - 2);
2061 7cbe629d 2018-08-04 stsp if (pthread_mutex_unlock(&state->mutex) != 0)
2062 84451b3e 2018-07-10 stsp err = got_error_from_errno();
2063 245d91c1 2018-07-12 stsp if (err || thread_err)
2068 b880a918 2018-07-10 stsp got_object_close(pobj);
2069 c9beca56 2018-07-22 stsp if (blame.thread)
2070 245d91c1 2018-07-12 stsp thread_err = stop_blame(&blame);
2071 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
2074 a70480e0 2018-06-23 stsp static const struct got_error *
2075 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
2077 a70480e0 2018-06-23 stsp const struct got_error *error;
2078 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
2079 69069811 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2080 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
2081 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
2083 e1cd8fed 2018-08-01 stsp struct tog_view *view;
2085 a70480e0 2018-06-23 stsp #ifndef PROFILE
2086 a70480e0 2018-06-23 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
2087 a70480e0 2018-06-23 stsp err(1, "pledge");
2090 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2091 a70480e0 2018-06-23 stsp switch (ch) {
2093 a70480e0 2018-06-23 stsp commit_id_str = optarg;
2096 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
2097 69069811 2018-08-02 stsp if (repo_path == NULL)
2098 69069811 2018-08-02 stsp err(1, "-r option");
2102 a70480e0 2018-06-23 stsp /* NOTREACHED */
2106 a70480e0 2018-06-23 stsp argc -= optind;
2107 a70480e0 2018-06-23 stsp argv += optind;
2109 69069811 2018-08-02 stsp if (argc == 1)
2110 69069811 2018-08-02 stsp path = argv[0];
2112 a70480e0 2018-06-23 stsp usage_blame();
2114 69069811 2018-08-02 stsp cwd = getcwd(NULL, 0);
2115 69069811 2018-08-02 stsp if (cwd == NULL) {
2116 69069811 2018-08-02 stsp error = got_error_from_errno();
2117 69069811 2018-08-02 stsp goto done;
2119 69069811 2018-08-02 stsp if (repo_path == NULL) {
2120 69069811 2018-08-02 stsp repo_path = strdup(cwd);
2121 69069811 2018-08-02 stsp if (repo_path == NULL) {
2122 69069811 2018-08-02 stsp error = got_error_from_errno();
2123 69069811 2018-08-02 stsp goto done;
2128 69069811 2018-08-02 stsp error = got_repo_open(&repo, repo_path);
2129 a70480e0 2018-06-23 stsp if (error != NULL)
2130 66b4983c 2018-06-23 stsp return error;
2132 69069811 2018-08-02 stsp error = got_repo_map_path(&in_repo_path, repo, path);
2133 69069811 2018-08-02 stsp if (error != NULL)
2134 69069811 2018-08-02 stsp goto done;
2136 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
2137 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
2138 a70480e0 2018-06-23 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
2139 a70480e0 2018-06-23 stsp if (error != NULL)
2140 66b4983c 2018-06-23 stsp goto done;
2141 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2142 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
2144 a70480e0 2018-06-23 stsp struct got_object *obj;
2145 a70480e0 2018-06-23 stsp error = got_object_open_by_id_str(&obj, repo, commit_id_str);
2146 a70480e0 2018-06-23 stsp if (error != NULL)
2147 66b4983c 2018-06-23 stsp goto done;
2148 a70480e0 2018-06-23 stsp commit_id = got_object_get_id(obj);
2149 a19e88aa 2018-06-23 stsp if (commit_id == NULL)
2150 66b4983c 2018-06-23 stsp error = got_error_from_errno();
2151 a19e88aa 2018-06-23 stsp got_object_close(obj);
2153 a19e88aa 2018-06-23 stsp if (error != NULL)
2154 a19e88aa 2018-06-23 stsp goto done;
2156 b3665f43 2018-08-04 stsp view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_BLAME);
2157 e1cd8fed 2018-08-01 stsp if (view == NULL) {
2158 e1cd8fed 2018-08-01 stsp error = got_error_from_errno();
2159 e1cd8fed 2018-08-01 stsp goto done;
2161 7cbe629d 2018-08-04 stsp error = open_blame_view(view, in_repo_path, commit_id, repo);
2162 7cbe629d 2018-08-04 stsp if (error)
2163 7cbe629d 2018-08-04 stsp goto done;
2164 7cbe629d 2018-08-04 stsp error = show_blame_view(view);
2165 7cbe629d 2018-08-04 stsp close_blame_view(view);
2166 96a765a8 2018-08-04 stsp view_close(view);
2168 69069811 2018-08-02 stsp free(in_repo_path);
2169 69069811 2018-08-02 stsp free(repo_path);
2170 69069811 2018-08-02 stsp free(cwd);
2171 a70480e0 2018-06-23 stsp free(commit_id);
2173 a70480e0 2018-06-23 stsp got_repo_close(repo);
2174 a70480e0 2018-06-23 stsp return error;
2177 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2178 f7d12f7e 2018-08-01 stsp draw_tree_entries(struct tog_view *view,
2179 f7d12f7e 2018-08-01 stsp struct got_tree_entry **first_displayed_entry,
2180 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **last_displayed_entry,
2181 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **selected_entry, int *ndisplayed,
2182 f7d12f7e 2018-08-01 stsp const char *label, int show_ids, const char *parent_path,
2183 ce52c690 2018-06-23 stsp const struct got_tree_entries *entries, int selected, int limit, int isroot)
2185 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
2186 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
2187 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
2188 ffd1d5e5 2018-06-23 stsp int width, n;
2190 ffd1d5e5 2018-06-23 stsp *ndisplayed = 0;
2192 f7d12f7e 2018-08-01 stsp werase(view->window);
2194 ffd1d5e5 2018-06-23 stsp if (limit == 0)
2195 ffd1d5e5 2018-06-23 stsp return NULL;
2197 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, label, view->ncols);
2199 ffd1d5e5 2018-06-23 stsp return err;
2200 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2201 2550e4c3 2018-07-13 stsp free(wline);
2202 2550e4c3 2018-07-13 stsp wline = NULL;
2203 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2204 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2205 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2206 ffd1d5e5 2018-06-23 stsp return NULL;
2207 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, parent_path, view->ncols);
2209 ce52c690 2018-06-23 stsp return err;
2210 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2211 2550e4c3 2018-07-13 stsp free(wline);
2212 2550e4c3 2018-07-13 stsp wline = NULL;
2213 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2214 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2215 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2216 ffd1d5e5 2018-06-23 stsp return NULL;
2217 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2218 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
2219 a1eca9bb 2018-06-23 stsp return NULL;
2221 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
2222 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL) {
2223 ffd1d5e5 2018-06-23 stsp if (selected == 0) {
2224 f7d12f7e 2018-08-01 stsp wstandout(view->window);
2225 ffd1d5e5 2018-06-23 stsp *selected_entry = NULL;
2227 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
2228 ffd1d5e5 2018-06-23 stsp if (selected == 0)
2229 f7d12f7e 2018-08-01 stsp wstandend(view->window);
2230 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
2231 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2232 ffd1d5e5 2018-06-23 stsp return NULL;
2236 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry)
2237 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
2240 ffd1d5e5 2018-06-23 stsp while (te) {
2241 1d13200f 2018-07-12 stsp char *line = NULL, *id_str = NULL;
2243 1d13200f 2018-07-12 stsp if (show_ids) {
2244 1d13200f 2018-07-12 stsp err = got_object_id_str(&id_str, te->id);
2246 1d13200f 2018-07-12 stsp return got_error_from_errno();
2248 1d13200f 2018-07-12 stsp if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
2249 1d13200f 2018-07-12 stsp te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
2250 1d13200f 2018-07-12 stsp free(id_str);
2251 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
2253 1d13200f 2018-07-12 stsp free(id_str);
2254 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
2255 ffd1d5e5 2018-06-23 stsp if (err) {
2256 ffd1d5e5 2018-06-23 stsp free(line);
2259 ffd1d5e5 2018-06-23 stsp if (n == selected) {
2260 f7d12f7e 2018-08-01 stsp wstandout(view->window);
2261 ffd1d5e5 2018-06-23 stsp *selected_entry = te;
2263 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2264 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2265 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2266 ffd1d5e5 2018-06-23 stsp if (n == selected)
2267 f7d12f7e 2018-08-01 stsp wstandend(view->window);
2268 ffd1d5e5 2018-06-23 stsp free(line);
2269 2550e4c3 2018-07-13 stsp free(wline);
2270 2550e4c3 2018-07-13 stsp wline = NULL;
2272 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
2273 ffd1d5e5 2018-06-23 stsp *last_displayed_entry = te;
2274 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2276 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
2279 ffd1d5e5 2018-06-23 stsp return err;
2282 ffd1d5e5 2018-06-23 stsp static void
2283 ffd1d5e5 2018-06-23 stsp tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
2284 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries, int isroot)
2286 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te, *prev;
2289 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL)
2292 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
2293 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == te) {
2294 ffd1d5e5 2018-06-23 stsp if (!isroot)
2295 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
2299 ffd1d5e5 2018-06-23 stsp /* XXX this is stupid... switch to TAILQ? */
2300 ffd1d5e5 2018-06-23 stsp for (i = 0; i < maxscroll; i++) {
2301 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry) {
2302 ffd1d5e5 2018-06-23 stsp prev = te;
2303 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
2305 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = prev;
2306 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
2308 ffd1d5e5 2018-06-23 stsp if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
2309 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
2312 ffd1d5e5 2018-06-23 stsp static void
2313 ffd1d5e5 2018-06-23 stsp tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
2314 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *last_displayed_entry,
2315 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries)
2317 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *next;
2318 ffd1d5e5 2018-06-23 stsp int n = 0;
2320 ffd1d5e5 2018-06-23 stsp if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
2323 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry)
2324 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
2326 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_FIRST(&entries->head);
2327 ffd1d5e5 2018-06-23 stsp while (next) {
2328 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = next;
2329 ffd1d5e5 2018-06-23 stsp if (++n >= maxscroll)
2331 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(next, entry);
2335 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree {
2336 d9765a41 2018-06-23 stsp TAILQ_ENTRY(tog_parent_tree) entry;
2337 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree;
2338 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *first_displayed_entry;
2339 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *selected_entry;
2340 ffd1d5e5 2018-06-23 stsp int selected;
2343 d9765a41 2018-06-23 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
2345 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2346 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
2347 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
2349 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
2350 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
2351 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
2353 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
2354 ffd1d5e5 2018-06-23 stsp len += strlen(pt->selected_entry->name) + 1 /* slash */;
2356 ce52c690 2018-06-23 stsp len += strlen(te->name);
2358 ce52c690 2018-06-23 stsp *path = calloc(1, len);
2359 ffd1d5e5 2018-06-23 stsp if (path == NULL)
2360 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
2362 ce52c690 2018-06-23 stsp (*path)[0] = '/';
2363 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
2364 d9765a41 2018-06-23 stsp while (pt) {
2365 ce52c690 2018-06-23 stsp if (strlcat(*path, pt->selected_entry->name, len) >= len) {
2366 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
2367 cb2ebc8a 2018-06-23 stsp goto done;
2369 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
2370 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
2371 cb2ebc8a 2018-06-23 stsp goto done;
2373 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
2376 ce52c690 2018-06-23 stsp if (strlcat(*path, te->name, len) >= len) {
2377 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
2378 ce52c690 2018-06-23 stsp goto done;
2382 ce52c690 2018-06-23 stsp if (err) {
2383 ce52c690 2018-06-23 stsp free(*path);
2384 ce52c690 2018-06-23 stsp *path = NULL;
2386 ce52c690 2018-06-23 stsp return err;
2389 ce52c690 2018-06-23 stsp static const struct got_error *
2390 cdf1ee82 2018-08-01 stsp blame_tree_entry(struct tog_view *parent_view, struct got_tree_entry *te,
2391 e1cd8fed 2018-08-01 stsp struct tog_parent_trees *parents, struct got_object_id *commit_id,
2392 e1cd8fed 2018-08-01 stsp struct got_repository *repo)
2394 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
2395 ce52c690 2018-06-23 stsp char *path;
2396 cdf1ee82 2018-08-01 stsp struct tog_view *view;
2398 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
2400 ce52c690 2018-06-23 stsp return err;
2402 b3665f43 2018-08-04 stsp view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_BLAME);
2403 cdf1ee82 2018-08-01 stsp if (view) {
2404 7cbe629d 2018-08-04 stsp err = open_blame_view(view, path, commit_id, repo);
2406 7cbe629d 2018-08-04 stsp goto done;
2407 7cbe629d 2018-08-04 stsp err = show_blame_view(view);
2408 7cbe629d 2018-08-04 stsp close_blame_view(view);
2409 96a765a8 2018-08-04 stsp view_close(view);
2411 cdf1ee82 2018-08-01 stsp err = got_error_from_errno();
2413 6acd1bd1 2018-08-04 stsp view_show(parent_view);
2415 69efd4c4 2018-07-18 stsp free(path);
2416 69efd4c4 2018-07-18 stsp return err;
2419 69efd4c4 2018-07-18 stsp static const struct got_error *
2420 04cc582a 2018-08-01 stsp log_tree_entry(struct tog_view *view, struct got_tree_entry *te,
2421 04cc582a 2018-08-01 stsp struct tog_parent_trees *parents, struct got_object_id *commit_id,
2422 04cc582a 2018-08-01 stsp struct got_repository *repo)
2424 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
2425 69efd4c4 2018-07-18 stsp char *path;
2427 69efd4c4 2018-07-18 stsp err = tree_entry_path(&path, parents, te);
2429 69efd4c4 2018-07-18 stsp return err;
2431 ba4f502b 2018-08-04 stsp err = open_log_view(view, commit_id, repo, path);
2433 ba4f502b 2018-08-04 stsp goto done;
2434 ba4f502b 2018-08-04 stsp err = show_log_view(view);
2435 ba4f502b 2018-08-04 stsp close_log_view(view);
2437 cb2ebc8a 2018-06-23 stsp free(path);
2438 cb2ebc8a 2018-06-23 stsp return err;
2441 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2442 5221c383 2018-08-01 stsp show_tree_view(struct tog_view *view, struct got_tree_object *root,
2443 5221c383 2018-08-01 stsp struct got_object_id *commit_id, struct got_repository *repo)
2445 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
2446 1d13200f 2018-07-12 stsp int ch, done = 0, selected = 0, show_ids = 0;
2447 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree = root;
2448 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries;
2449 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *first_displayed_entry = NULL;
2450 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *last_displayed_entry = NULL;
2451 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *selected_entry = NULL;
2452 ffd1d5e5 2018-06-23 stsp char *commit_id_str = NULL, *tree_label = NULL;
2453 ffd1d5e5 2018-06-23 stsp int nentries, ndisplayed;
2454 ffd1d5e5 2018-06-23 stsp struct tog_parent_trees parents;
2456 d9765a41 2018-06-23 stsp TAILQ_INIT(&parents);
2458 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
2459 ffd1d5e5 2018-06-23 stsp if (err != NULL)
2460 ffd1d5e5 2018-06-23 stsp goto done;
2462 decd3bd1 2018-07-12 stsp if (asprintf(&tree_label, "commit: %s", commit_id_str) == -1) {
2463 ffd1d5e5 2018-06-23 stsp err = got_error_from_errno();
2464 ffd1d5e5 2018-06-23 stsp goto done;
2467 6acd1bd1 2018-08-04 stsp view_show(view);
2469 ffd1d5e5 2018-06-23 stsp entries = got_object_tree_get_entries(root);
2470 ffd1d5e5 2018-06-23 stsp first_displayed_entry = SIMPLEQ_FIRST(&entries->head);
2471 ffd1d5e5 2018-06-23 stsp while (!done) {
2472 ce52c690 2018-06-23 stsp char *parent_path;
2473 ffd1d5e5 2018-06-23 stsp entries = got_object_tree_get_entries(tree);
2474 ffd1d5e5 2018-06-23 stsp nentries = entries->nentries;
2475 ffd1d5e5 2018-06-23 stsp if (tree != root)
2476 ffd1d5e5 2018-06-23 stsp nentries++; /* '..' directory */
2478 ce52c690 2018-06-23 stsp err = tree_entry_path(&parent_path, &parents, NULL);
2480 ce52c690 2018-06-23 stsp goto done;
2482 f7d12f7e 2018-08-01 stsp err = draw_tree_entries(view, &first_displayed_entry,
2483 ffd1d5e5 2018-06-23 stsp &last_displayed_entry, &selected_entry, &ndisplayed,
2484 f7d12f7e 2018-08-01 stsp tree_label, show_ids, parent_path, entries, selected,
2485 f7d12f7e 2018-08-01 stsp view->nlines, tree == root);
2486 ce52c690 2018-06-23 stsp free(parent_path);
2490 ffd1d5e5 2018-06-23 stsp nodelay(stdscr, FALSE);
2491 cc3c9aac 2018-08-01 stsp ch = wgetch(view->window);
2492 ffd1d5e5 2018-06-23 stsp nodelay(stdscr, TRUE);
2493 ffd1d5e5 2018-06-23 stsp switch (ch) {
2498 1d13200f 2018-07-12 stsp show_ids = !show_ids;
2501 69efd4c4 2018-07-18 stsp if (selected_entry) {
2502 04cc582a 2018-08-01 stsp struct tog_view *log_view;
2503 b3665f43 2018-08-04 stsp log_view = view_open(0, 0, 0, 0, view,
2504 d6b05b5b 2018-08-04 stsp TOG_VIEW_LOG);
2505 04cc582a 2018-08-01 stsp if (log_view == NULL) {
2506 04cc582a 2018-08-01 stsp err = got_error_from_errno();
2507 04cc582a 2018-08-01 stsp goto done;
2509 04cc582a 2018-08-01 stsp err = log_tree_entry(log_view,
2510 04cc582a 2018-08-01 stsp selected_entry, &parents,
2511 04cc582a 2018-08-01 stsp commit_id, repo);
2512 96a765a8 2018-08-04 stsp view_close(log_view);
2513 6acd1bd1 2018-08-04 stsp view_show(view);
2515 69efd4c4 2018-07-18 stsp goto done;
2519 ffd1d5e5 2018-06-23 stsp case KEY_UP:
2520 ffd1d5e5 2018-06-23 stsp if (selected > 0)
2521 ffd1d5e5 2018-06-23 stsp selected--;
2522 ffd1d5e5 2018-06-23 stsp if (selected > 0)
2524 ffd1d5e5 2018-06-23 stsp tree_scroll_up(&first_displayed_entry, 1,
2525 ffd1d5e5 2018-06-23 stsp entries, tree == root);
2527 ffd1d5e5 2018-06-23 stsp case KEY_PPAGE:
2528 ffd1d5e5 2018-06-23 stsp if (SIMPLEQ_FIRST(&entries->head) ==
2529 ffd1d5e5 2018-06-23 stsp first_displayed_entry) {
2530 cf8f1261 2018-06-23 stsp if (tree != root)
2531 cf8f1261 2018-06-23 stsp first_displayed_entry = NULL;
2532 ffd1d5e5 2018-06-23 stsp selected = 0;
2535 f7d12f7e 2018-08-01 stsp tree_scroll_up(&first_displayed_entry,
2536 f7d12f7e 2018-08-01 stsp view->nlines, entries, tree == root);
2539 ffd1d5e5 2018-06-23 stsp case KEY_DOWN:
2540 ffd1d5e5 2018-06-23 stsp if (selected < ndisplayed - 1) {
2541 ffd1d5e5 2018-06-23 stsp selected++;
2544 ffd1d5e5 2018-06-23 stsp tree_scroll_down(&first_displayed_entry, 1,
2545 ffd1d5e5 2018-06-23 stsp last_displayed_entry, entries);
2547 ffd1d5e5 2018-06-23 stsp case KEY_NPAGE:
2548 f7d12f7e 2018-08-01 stsp tree_scroll_down(&first_displayed_entry,
2549 f7d12f7e 2018-08-01 stsp view->nlines, last_displayed_entry,
2551 ffd1d5e5 2018-06-23 stsp if (SIMPLEQ_NEXT(last_displayed_entry, entry))
2553 ffd1d5e5 2018-06-23 stsp /* can't scroll any further; move cursor down */
2554 ffd1d5e5 2018-06-23 stsp if (selected < ndisplayed - 1)
2555 ffd1d5e5 2018-06-23 stsp selected = ndisplayed - 1;
2557 ffd1d5e5 2018-06-23 stsp case KEY_ENTER:
2558 ffd1d5e5 2018-06-23 stsp case '\r':
2559 ffd1d5e5 2018-06-23 stsp if (selected_entry == NULL) {
2560 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *parent;
2561 ffd1d5e5 2018-06-23 stsp case KEY_BACKSPACE:
2562 ffd1d5e5 2018-06-23 stsp /* user selected '..' */
2563 ffd1d5e5 2018-06-23 stsp if (tree == root)
2565 d9765a41 2018-06-23 stsp parent = TAILQ_FIRST(&parents);
2566 d9765a41 2018-06-23 stsp TAILQ_REMOVE(&parents, parent, entry);
2567 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
2568 ffd1d5e5 2018-06-23 stsp tree = parent->tree;
2569 ffd1d5e5 2018-06-23 stsp first_displayed_entry =
2570 ffd1d5e5 2018-06-23 stsp parent->first_displayed_entry;
2571 ffd1d5e5 2018-06-23 stsp selected_entry = parent->selected_entry;
2572 ffd1d5e5 2018-06-23 stsp selected = parent->selected;
2573 ffd1d5e5 2018-06-23 stsp free(parent);
2574 ffd1d5e5 2018-06-23 stsp } else if (S_ISDIR(selected_entry->mode)) {
2575 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *parent;
2576 ffd1d5e5 2018-06-23 stsp struct got_tree_object *child;
2577 ffd1d5e5 2018-06-23 stsp err = got_object_open_as_tree(
2578 ffd1d5e5 2018-06-23 stsp &child, repo, selected_entry->id);
2580 ffd1d5e5 2018-06-23 stsp goto done;
2581 ffd1d5e5 2018-06-23 stsp parent = calloc(1, sizeof(*parent));
2582 ffd1d5e5 2018-06-23 stsp if (parent == NULL) {
2583 ffd1d5e5 2018-06-23 stsp err = got_error_from_errno();
2584 ffd1d5e5 2018-06-23 stsp goto done;
2586 ffd1d5e5 2018-06-23 stsp parent->tree = tree;
2587 ffd1d5e5 2018-06-23 stsp parent->first_displayed_entry =
2588 ffd1d5e5 2018-06-23 stsp first_displayed_entry;
2589 ffd1d5e5 2018-06-23 stsp parent->selected_entry = selected_entry;
2590 ffd1d5e5 2018-06-23 stsp parent->selected = selected;
2591 d9765a41 2018-06-23 stsp TAILQ_INSERT_HEAD(&parents, parent,
2593 ffd1d5e5 2018-06-23 stsp tree = child;
2594 ffd1d5e5 2018-06-23 stsp selected = 0;
2595 ffd1d5e5 2018-06-23 stsp first_displayed_entry = NULL;
2596 ffd1d5e5 2018-06-23 stsp } else if (S_ISREG(selected_entry->mode)) {
2597 cdf1ee82 2018-08-01 stsp err = blame_tree_entry(view,
2598 e1cd8fed 2018-08-01 stsp selected_entry, &parents,
2599 e1cd8fed 2018-08-01 stsp commit_id, repo);
2601 ffd1d5e5 2018-06-23 stsp goto done;
2604 ffd1d5e5 2018-06-23 stsp case KEY_RESIZE:
2605 a41d2007 2018-08-01 stsp err = view_resize(view);
2607 a41d2007 2018-08-01 stsp goto done;
2608 f7d12f7e 2018-08-01 stsp if (selected > view->nlines)
2609 ffd1d5e5 2018-06-23 stsp selected = ndisplayed - 1;
2616 ffd1d5e5 2018-06-23 stsp free(tree_label);
2617 ffd1d5e5 2018-06-23 stsp free(commit_id_str);
2618 d9765a41 2018-06-23 stsp while (!TAILQ_EMPTY(&parents)) {
2619 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *parent;
2620 d9765a41 2018-06-23 stsp parent = TAILQ_FIRST(&parents);
2621 d9765a41 2018-06-23 stsp TAILQ_REMOVE(&parents, parent, entry);
2622 ffd1d5e5 2018-06-23 stsp free(parent);
2625 ffd1d5e5 2018-06-23 stsp if (tree != root)
2626 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
2627 ffd1d5e5 2018-06-23 stsp return err;
2630 ffd1d5e5 2018-06-23 stsp __dead static void
2631 ffd1d5e5 2018-06-23 stsp usage_tree(void)
2634 ffd1d5e5 2018-06-23 stsp fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
2635 ffd1d5e5 2018-06-23 stsp getprogname());
2639 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2640 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
2642 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
2643 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
2644 ffd1d5e5 2018-06-23 stsp char *repo_path = NULL;
2645 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
2646 ffd1d5e5 2018-06-23 stsp char *commit_id_arg = NULL;
2647 ffd1d5e5 2018-06-23 stsp struct got_commit_object *commit = NULL;
2648 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree = NULL;
2650 5221c383 2018-08-01 stsp struct tog_view *view;
2652 ffd1d5e5 2018-06-23 stsp #ifndef PROFILE
2653 ffd1d5e5 2018-06-23 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
2654 ffd1d5e5 2018-06-23 stsp err(1, "pledge");
2657 ffd1d5e5 2018-06-23 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
2658 ffd1d5e5 2018-06-23 stsp switch (ch) {
2660 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
2664 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
2668 ffd1d5e5 2018-06-23 stsp argc -= optind;
2669 ffd1d5e5 2018-06-23 stsp argv += optind;
2671 ffd1d5e5 2018-06-23 stsp if (argc == 0) {
2672 ffd1d5e5 2018-06-23 stsp repo_path = getcwd(NULL, 0);
2673 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
2674 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
2675 ffd1d5e5 2018-06-23 stsp } else if (argc == 1) {
2676 ffd1d5e5 2018-06-23 stsp repo_path = realpath(argv[0], NULL);
2677 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
2678 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
2680 ffd1d5e5 2018-06-23 stsp usage_log();
2682 ffd1d5e5 2018-06-23 stsp error = got_repo_open(&repo, repo_path);
2683 ffd1d5e5 2018-06-23 stsp free(repo_path);
2684 ffd1d5e5 2018-06-23 stsp if (error != NULL)
2685 ffd1d5e5 2018-06-23 stsp return error;
2687 ffd1d5e5 2018-06-23 stsp if (commit_id_arg == NULL) {
2688 ffd1d5e5 2018-06-23 stsp error = get_head_commit_id(&commit_id, repo);
2689 ffd1d5e5 2018-06-23 stsp if (error != NULL)
2690 ffd1d5e5 2018-06-23 stsp goto done;
2692 ffd1d5e5 2018-06-23 stsp struct got_object *obj;
2693 ffd1d5e5 2018-06-23 stsp error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
2694 ffd1d5e5 2018-06-23 stsp if (error == NULL) {
2695 ffd1d5e5 2018-06-23 stsp commit_id = got_object_get_id(obj);
2696 ffd1d5e5 2018-06-23 stsp if (commit_id == NULL)
2697 ffd1d5e5 2018-06-23 stsp error = got_error_from_errno();
2700 ffd1d5e5 2018-06-23 stsp if (error != NULL)
2701 ffd1d5e5 2018-06-23 stsp goto done;
2703 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
2704 ffd1d5e5 2018-06-23 stsp if (error != NULL)
2705 ffd1d5e5 2018-06-23 stsp goto done;
2707 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_tree(&tree, repo, commit->tree_id);
2708 ffd1d5e5 2018-06-23 stsp if (error != NULL)
2709 ffd1d5e5 2018-06-23 stsp goto done;
2711 b3665f43 2018-08-04 stsp view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_TREE);
2712 5221c383 2018-08-01 stsp if (view == NULL) {
2713 5221c383 2018-08-01 stsp error = got_error_from_errno();
2714 5221c383 2018-08-01 stsp goto done;
2716 5221c383 2018-08-01 stsp error = show_tree_view(view, tree, commit_id, repo);
2717 96a765a8 2018-08-04 stsp view_close(view);
2719 ffd1d5e5 2018-06-23 stsp free(commit_id);
2720 ffd1d5e5 2018-06-23 stsp if (commit)
2721 ffd1d5e5 2018-06-23 stsp got_object_commit_close(commit);
2723 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
2725 ffd1d5e5 2018-06-23 stsp got_repo_close(repo);
2726 ffd1d5e5 2018-06-23 stsp return error;
2728 5c5136c5 2018-05-20 stsp static void
2729 9f7d7167 2018-04-29 stsp init_curses(void)
2731 9f7d7167 2018-04-29 stsp initscr();
2735 9f7d7167 2018-04-29 stsp intrflush(stdscr, FALSE);
2736 9f7d7167 2018-04-29 stsp keypad(stdscr, TRUE);
2737 1f475ad8 2018-05-10 stsp curs_set(0);
2740 4ed7e80c 2018-05-20 stsp __dead static void
2741 9f7d7167 2018-04-29 stsp usage(void)
2745 c2301be8 2018-04-30 stsp fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
2746 9f7d7167 2018-04-29 stsp "Available commands:\n", getprogname());
2747 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
2748 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = &tog_commands[i];
2749 c2301be8 2018-04-30 stsp fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
2754 c2301be8 2018-04-30 stsp static char **
2755 c2301be8 2018-04-30 stsp make_argv(const char *arg0, const char *arg1)
2757 c2301be8 2018-04-30 stsp char **argv;
2758 c2301be8 2018-04-30 stsp int argc = (arg1 == NULL ? 1 : 2);
2760 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
2761 c2301be8 2018-04-30 stsp if (argv == NULL)
2762 c2301be8 2018-04-30 stsp err(1, "calloc");
2763 c2301be8 2018-04-30 stsp argv[0] = strdup(arg0);
2764 c2301be8 2018-04-30 stsp if (argv[0] == NULL)
2765 c2301be8 2018-04-30 stsp err(1, "calloc");
2766 c2301be8 2018-04-30 stsp if (arg1) {
2767 c2301be8 2018-04-30 stsp argv[1] = strdup(arg1);
2768 c2301be8 2018-04-30 stsp if (argv[1] == NULL)
2769 c2301be8 2018-04-30 stsp err(1, "calloc");
2772 c2301be8 2018-04-30 stsp return argv;
2776 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
2778 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
2779 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
2780 9f7d7167 2018-04-29 stsp int ch, hflag = 0;
2781 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
2783 9f7d7167 2018-04-29 stsp setlocale(LC_ALL, "");
2785 9f7d7167 2018-04-29 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
2786 9f7d7167 2018-04-29 stsp switch (ch) {
2788 9f7d7167 2018-04-29 stsp hflag = 1;
2792 9f7d7167 2018-04-29 stsp /* NOTREACHED */
2796 9f7d7167 2018-04-29 stsp argc -= optind;
2797 9f7d7167 2018-04-29 stsp argv += optind;
2798 9f7d7167 2018-04-29 stsp optind = 0;
2799 c2301be8 2018-04-30 stsp optreset = 1;
2801 c2301be8 2018-04-30 stsp if (argc == 0) {
2802 f29d3e89 2018-06-23 stsp if (hflag)
2804 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
2805 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
2806 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, NULL);
2811 c2301be8 2018-04-30 stsp /* Did the user specific a command? */
2812 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
2813 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
2814 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
2815 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
2816 9f7d7167 2018-04-29 stsp if (hflag)
2817 9f7d7167 2018-04-29 stsp tog_commands[i].cmd_usage();
2821 9f7d7167 2018-04-29 stsp if (cmd == NULL) {
2822 c2301be8 2018-04-30 stsp /* Did the user specify a repository? */
2823 c2301be8 2018-04-30 stsp char *repo_path = realpath(argv[0], NULL);
2824 c2301be8 2018-04-30 stsp if (repo_path) {
2825 c2301be8 2018-04-30 stsp struct got_repository *repo;
2826 c2301be8 2018-04-30 stsp error = got_repo_open(&repo, repo_path);
2827 c2301be8 2018-04-30 stsp if (error == NULL)
2828 c2301be8 2018-04-30 stsp got_repo_close(repo);
2830 ad7de8d9 2018-04-30 stsp error = got_error_from_errno();
2831 c2301be8 2018-04-30 stsp if (error) {
2832 f29d3e89 2018-06-23 stsp if (hflag) {
2833 f29d3e89 2018-06-23 stsp fprintf(stderr, "%s: '%s' is not a "
2834 f29d3e89 2018-06-23 stsp "known command\n", getprogname(),
2838 ad7de8d9 2018-04-30 stsp fprintf(stderr, "%s: '%s' is neither a known "
2839 ad7de8d9 2018-04-30 stsp "command nor a path to a repository\n",
2840 c2301be8 2018-04-30 stsp getprogname(), argv[0]);
2841 ad7de8d9 2018-04-30 stsp free(repo_path);
2844 c2301be8 2018-04-30 stsp cmd = &tog_commands[0];
2845 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, repo_path);
2847 c2301be8 2018-04-30 stsp free(repo_path);
2851 5c5136c5 2018-05-20 stsp init_curses();
2853 c2301be8 2018-04-30 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
2854 9f7d7167 2018-04-29 stsp if (error)
2855 9f7d7167 2018-04-29 stsp goto done;
2858 c2301be8 2018-04-30 stsp free(cmd_argv);
2859 9f7d7167 2018-04-29 stsp if (error)
2860 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);