Blame


1 9f7d7167 2018-04-29 stsp /*
2 9f7d7167 2018-04-29 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 9f7d7167 2018-04-29 stsp *
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.
7 9f7d7167 2018-04-29 stsp *
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.
15 9f7d7167 2018-04-29 stsp */
16 9f7d7167 2018-04-29 stsp
17 80ddbec8 2018-04-29 stsp #include <sys/queue.h>
18 ffd1d5e5 2018-06-23 stsp #include <sys/stat.h>
19 80ddbec8 2018-04-29 stsp
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>
37 9f7d7167 2018-04-29 stsp
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"
47 9f7d7167 2018-04-29 stsp
48 881b2d3e 2018-04-30 stsp #ifndef MIN
49 881b2d3e 2018-04-30 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
50 881b2d3e 2018-04-30 stsp #endif
51 881b2d3e 2018-04-30 stsp
52 9f7d7167 2018-04-29 stsp #ifndef nitems
53 9f7d7167 2018-04-29 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
54 9f7d7167 2018-04-29 stsp #endif
55 9f7d7167 2018-04-29 stsp
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;
61 9f7d7167 2018-04-29 stsp };
62 9f7d7167 2018-04-29 stsp
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);
68 9f7d7167 2018-04-29 stsp
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 *[]);
73 9f7d7167 2018-04-29 stsp
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" },
83 9f7d7167 2018-04-29 stsp };
84 9f7d7167 2018-04-29 stsp
85 cc3c9aac 2018-08-01 stsp struct tog_view {
86 26ed57b2 2018-05-19 stsp WINDOW *window;
87 26ed57b2 2018-05-19 stsp PANEL *panel;
88 97ddc146 2018-08-01 stsp int nlines, ncols, begin_y, begin_x;
89 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
90 6d0fee91 2018-08-01 stsp struct tog_view *parent;
91 cc3c9aac 2018-08-01 stsp };
92 cd0acaa7 2018-05-20 stsp
93 cd0acaa7 2018-05-20 stsp static const struct got_error *
94 ea5e7bb5 2018-08-01 stsp show_diff_view(struct tog_view *, struct got_object *, struct got_object *,
95 cd0acaa7 2018-05-20 stsp struct got_repository *);
96 cd0acaa7 2018-05-20 stsp static const struct got_error *
97 04cc582a 2018-08-01 stsp show_log_view(struct tog_view *, struct got_object_id *,
98 04cc582a 2018-08-01 stsp struct got_repository *, const char *);
99 a70480e0 2018-06-23 stsp static const struct got_error *
100 e1cd8fed 2018-08-01 stsp show_blame_view(struct tog_view *, const char *, struct got_object_id *,
101 e1cd8fed 2018-08-01 stsp struct got_repository *);
102 ffd1d5e5 2018-06-23 stsp static const struct got_error *
103 5221c383 2018-08-01 stsp show_tree_view(struct tog_view *, struct got_tree_object *,
104 5221c383 2018-08-01 stsp struct got_object_id *, struct got_repository *);
105 26ed57b2 2018-05-19 stsp
106 ea5e7bb5 2018-08-01 stsp static void
107 ea5e7bb5 2018-08-01 stsp close_view(struct tog_view *view)
108 ea5e7bb5 2018-08-01 stsp {
109 ea5e7bb5 2018-08-01 stsp if (view->panel)
110 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
111 ea5e7bb5 2018-08-01 stsp if (view->window)
112 ea5e7bb5 2018-08-01 stsp delwin(view->window);
113 ea5e7bb5 2018-08-01 stsp free(view);
114 ea5e7bb5 2018-08-01 stsp }
115 ea5e7bb5 2018-08-01 stsp
116 ea5e7bb5 2018-08-01 stsp static struct tog_view *
117 6d0fee91 2018-08-01 stsp open_view(int nlines, int ncols, int begin_y, int begin_x,
118 6d0fee91 2018-08-01 stsp struct tog_view *parent)
119 ea5e7bb5 2018-08-01 stsp {
120 ea5e7bb5 2018-08-01 stsp struct tog_view *view = malloc(sizeof(*view));
121 ea5e7bb5 2018-08-01 stsp
122 ea5e7bb5 2018-08-01 stsp if (view == NULL)
123 ea5e7bb5 2018-08-01 stsp return NULL;
124 ea5e7bb5 2018-08-01 stsp
125 6d0fee91 2018-08-01 stsp view->parent = parent;
126 f7d12f7e 2018-08-01 stsp view->lines = LINES;
127 f7d12f7e 2018-08-01 stsp view->cols = COLS;
128 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
129 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
130 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
131 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
132 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
133 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
134 ea5e7bb5 2018-08-01 stsp close_view(view);
135 ea5e7bb5 2018-08-01 stsp return NULL;
136 ea5e7bb5 2018-08-01 stsp }
137 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
138 ea5e7bb5 2018-08-01 stsp if (view->panel == NULL) {
139 ea5e7bb5 2018-08-01 stsp close_view(view);
140 ea5e7bb5 2018-08-01 stsp return NULL;
141 ea5e7bb5 2018-08-01 stsp }
142 ea5e7bb5 2018-08-01 stsp
143 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
144 ea5e7bb5 2018-08-01 stsp return view;
145 f7d12f7e 2018-08-01 stsp }
146 f7d12f7e 2018-08-01 stsp
147 f7d12f7e 2018-08-01 stsp const struct got_error *
148 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
149 f7d12f7e 2018-08-01 stsp {
150 f7d12f7e 2018-08-01 stsp int nlines, ncols;
151 f7d12f7e 2018-08-01 stsp
152 6d0fee91 2018-08-01 stsp while (view) {
153 6d0fee91 2018-08-01 stsp if (view->lines > LINES)
154 6d0fee91 2018-08-01 stsp nlines = view->nlines - (view->lines - LINES);
155 6d0fee91 2018-08-01 stsp else
156 6d0fee91 2018-08-01 stsp nlines = view->nlines + (LINES - view->lines);
157 f7d12f7e 2018-08-01 stsp
158 6d0fee91 2018-08-01 stsp if (view->cols > COLS)
159 6d0fee91 2018-08-01 stsp ncols = view->ncols - (view->cols - COLS);
160 6d0fee91 2018-08-01 stsp else
161 6d0fee91 2018-08-01 stsp ncols = view->ncols + (COLS - view->cols);
162 f7d12f7e 2018-08-01 stsp
163 6d0fee91 2018-08-01 stsp if (wresize(view->window, nlines, ncols) == ERR)
164 6d0fee91 2018-08-01 stsp return got_error_from_errno();
165 f7d12f7e 2018-08-01 stsp
166 6d0fee91 2018-08-01 stsp view->nlines = nlines;
167 6d0fee91 2018-08-01 stsp view->ncols = ncols;
168 6d0fee91 2018-08-01 stsp view->lines = LINES;
169 6d0fee91 2018-08-01 stsp view->cols = COLS;
170 6d0fee91 2018-08-01 stsp
171 6d0fee91 2018-08-01 stsp view = view->parent;
172 6d0fee91 2018-08-01 stsp }
173 6d0fee91 2018-08-01 stsp
174 f7d12f7e 2018-08-01 stsp return NULL;
175 ea5e7bb5 2018-08-01 stsp }
176 ea5e7bb5 2018-08-01 stsp
177 4ed7e80c 2018-05-20 stsp __dead static void
178 9f7d7167 2018-04-29 stsp usage_log(void)
179 9f7d7167 2018-04-29 stsp {
180 80ddbec8 2018-04-29 stsp endwin();
181 ecb28ae0 2018-07-16 stsp fprintf(stderr, "usage: %s log [-c commit] [-r repository-path] [path]\n",
182 9f7d7167 2018-04-29 stsp getprogname());
183 9f7d7167 2018-04-29 stsp exit(1);
184 80ddbec8 2018-04-29 stsp }
185 80ddbec8 2018-04-29 stsp
186 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
187 80ddbec8 2018-04-29 stsp static const struct got_error *
188 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
189 963b370f 2018-05-20 stsp {
190 00dfcb92 2018-06-11 stsp char *vis = NULL;
191 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
192 963b370f 2018-05-20 stsp
193 963b370f 2018-05-20 stsp *ws = NULL;
194 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
195 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
196 00dfcb92 2018-06-11 stsp int vislen;
197 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
198 00dfcb92 2018-06-11 stsp return got_error_from_errno();
199 00dfcb92 2018-06-11 stsp
200 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
201 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
202 00dfcb92 2018-06-11 stsp if (err)
203 00dfcb92 2018-06-11 stsp return err;
204 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
205 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
206 a7f50699 2018-06-11 stsp err = got_error_from_errno(); /* give up */
207 a7f50699 2018-06-11 stsp goto done;
208 a7f50699 2018-06-11 stsp }
209 00dfcb92 2018-06-11 stsp }
210 963b370f 2018-05-20 stsp
211 963b370f 2018-05-20 stsp *ws = calloc(*wlen + 1, sizeof(*ws));
212 a7f50699 2018-06-11 stsp if (*ws == NULL) {
213 a7f50699 2018-06-11 stsp err = got_error_from_errno();
214 a7f50699 2018-06-11 stsp goto done;
215 a7f50699 2018-06-11 stsp }
216 963b370f 2018-05-20 stsp
217 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
218 963b370f 2018-05-20 stsp err = got_error_from_errno();
219 a7f50699 2018-06-11 stsp done:
220 00dfcb92 2018-06-11 stsp free(vis);
221 963b370f 2018-05-20 stsp if (err) {
222 963b370f 2018-05-20 stsp free(*ws);
223 963b370f 2018-05-20 stsp *ws = NULL;
224 963b370f 2018-05-20 stsp *wlen = 0;
225 963b370f 2018-05-20 stsp }
226 963b370f 2018-05-20 stsp return err;
227 963b370f 2018-05-20 stsp }
228 963b370f 2018-05-20 stsp
229 963b370f 2018-05-20 stsp /* Format a line for display, ensuring that it won't overflow a width limit. */
230 963b370f 2018-05-20 stsp static const struct got_error *
231 ffd1d5e5 2018-06-23 stsp format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
232 963b370f 2018-05-20 stsp {
233 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
234 963b370f 2018-05-20 stsp int cols = 0;
235 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
236 963b370f 2018-05-20 stsp size_t wlen;
237 963b370f 2018-05-20 stsp int i;
238 963b370f 2018-05-20 stsp
239 963b370f 2018-05-20 stsp *wlinep = NULL;
240 b700b5d6 2018-07-10 stsp *widthp = 0;
241 963b370f 2018-05-20 stsp
242 963b370f 2018-05-20 stsp err = mbs2ws(&wline, &wlen, line);
243 963b370f 2018-05-20 stsp if (err)
244 963b370f 2018-05-20 stsp return err;
245 963b370f 2018-05-20 stsp
246 963b370f 2018-05-20 stsp i = 0;
247 b700b5d6 2018-07-10 stsp while (i < wlen && cols < wlimit) {
248 963b370f 2018-05-20 stsp int width = wcwidth(wline[i]);
249 963b370f 2018-05-20 stsp switch (width) {
250 963b370f 2018-05-20 stsp case 0:
251 b700b5d6 2018-07-10 stsp i++;
252 963b370f 2018-05-20 stsp break;
253 963b370f 2018-05-20 stsp case 1:
254 963b370f 2018-05-20 stsp case 2:
255 b700b5d6 2018-07-10 stsp if (cols + width <= wlimit) {
256 b700b5d6 2018-07-10 stsp cols += width;
257 b700b5d6 2018-07-10 stsp i++;
258 b700b5d6 2018-07-10 stsp }
259 963b370f 2018-05-20 stsp break;
260 963b370f 2018-05-20 stsp case -1:
261 963b370f 2018-05-20 stsp if (wline[i] == L'\t')
262 b700b5d6 2018-07-10 stsp cols += TABSIZE - ((cols + 1) % TABSIZE);
263 b700b5d6 2018-07-10 stsp i++;
264 963b370f 2018-05-20 stsp break;
265 963b370f 2018-05-20 stsp default:
266 963b370f 2018-05-20 stsp err = got_error_from_errno();
267 963b370f 2018-05-20 stsp goto done;
268 963b370f 2018-05-20 stsp }
269 963b370f 2018-05-20 stsp }
270 963b370f 2018-05-20 stsp wline[i] = L'\0';
271 b700b5d6 2018-07-10 stsp if (widthp)
272 b700b5d6 2018-07-10 stsp *widthp = cols;
273 963b370f 2018-05-20 stsp done:
274 963b370f 2018-05-20 stsp if (err)
275 963b370f 2018-05-20 stsp free(wline);
276 963b370f 2018-05-20 stsp else
277 963b370f 2018-05-20 stsp *wlinep = wline;
278 963b370f 2018-05-20 stsp return err;
279 963b370f 2018-05-20 stsp }
280 963b370f 2018-05-20 stsp
281 963b370f 2018-05-20 stsp static const struct got_error *
282 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
283 2814baeb 2018-08-01 stsp struct got_object_id *id)
284 80ddbec8 2018-04-29 stsp {
285 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
286 b39d25c7 2018-07-10 stsp char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
287 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
288 6d9fbc00 2018-04-29 stsp char *author0 = NULL, *author = NULL;
289 bb737323 2018-05-20 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
290 bb737323 2018-05-20 stsp int author_width, logmsg_width;
291 6d9fbc00 2018-04-29 stsp char *newline, *smallerthan;
292 80ddbec8 2018-04-29 stsp char *line = NULL;
293 bb737323 2018-05-20 stsp int col, limit;
294 b39d25c7 2018-07-10 stsp static const size_t date_display_cols = 9;
295 bb737323 2018-05-20 stsp static const size_t author_display_cols = 16;
296 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
297 80ddbec8 2018-04-29 stsp
298 b39d25c7 2018-07-10 stsp if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &commit->tm_committer)
299 b39d25c7 2018-07-10 stsp >= sizeof(datebuf))
300 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
301 b39d25c7 2018-07-10 stsp
302 b39d25c7 2018-07-10 stsp if (avail < date_display_cols)
303 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
304 b39d25c7 2018-07-10 stsp else
305 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
306 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
307 b39d25c7 2018-07-10 stsp col = limit + 1;
308 b39d25c7 2018-07-10 stsp if (col > avail)
309 b39d25c7 2018-07-10 stsp goto done;
310 b39d25c7 2018-07-10 stsp
311 6d9fbc00 2018-04-29 stsp author0 = strdup(commit->author);
312 6d9fbc00 2018-04-29 stsp if (author0 == NULL) {
313 80ddbec8 2018-04-29 stsp err = got_error_from_errno();
314 80ddbec8 2018-04-29 stsp goto done;
315 80ddbec8 2018-04-29 stsp }
316 6d9fbc00 2018-04-29 stsp author = author0;
317 6d9fbc00 2018-04-29 stsp smallerthan = strchr(author, '<');
318 6d9fbc00 2018-04-29 stsp if (smallerthan)
319 6d9fbc00 2018-04-29 stsp *smallerthan = '\0';
320 6d9fbc00 2018-04-29 stsp else {
321 6d9fbc00 2018-04-29 stsp char *at = strchr(author, '@');
322 6d9fbc00 2018-04-29 stsp if (at)
323 6d9fbc00 2018-04-29 stsp *at = '\0';
324 6d9fbc00 2018-04-29 stsp }
325 b39d25c7 2018-07-10 stsp limit = avail - col;
326 bb737323 2018-05-20 stsp err = format_line(&wauthor, &author_width, author, limit);
327 bb737323 2018-05-20 stsp if (err)
328 bb737323 2018-05-20 stsp goto done;
329 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
330 bb737323 2018-05-20 stsp col += author_width;
331 9c2eaf34 2018-05-20 stsp while (col <= avail && author_width < author_display_cols + 1) {
332 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
333 bb737323 2018-05-20 stsp col++;
334 bb737323 2018-05-20 stsp author_width++;
335 bb737323 2018-05-20 stsp }
336 9c2eaf34 2018-05-20 stsp if (col > avail)
337 9c2eaf34 2018-05-20 stsp goto done;
338 80ddbec8 2018-04-29 stsp
339 bb737323 2018-05-20 stsp logmsg0 = strdup(commit->logmsg);
340 bb737323 2018-05-20 stsp if (logmsg0 == NULL) {
341 6d9fbc00 2018-04-29 stsp err = got_error_from_errno();
342 6d9fbc00 2018-04-29 stsp goto done;
343 6d9fbc00 2018-04-29 stsp }
344 bb737323 2018-05-20 stsp logmsg = logmsg0;
345 bb737323 2018-05-20 stsp while (*logmsg == '\n')
346 bb737323 2018-05-20 stsp logmsg++;
347 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
348 bb737323 2018-05-20 stsp if (newline)
349 bb737323 2018-05-20 stsp *newline = '\0';
350 bb737323 2018-05-20 stsp limit = avail - col;
351 bb737323 2018-05-20 stsp err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
352 bb737323 2018-05-20 stsp if (err)
353 bb737323 2018-05-20 stsp goto done;
354 2814baeb 2018-08-01 stsp waddwstr(view->window, wlogmsg);
355 bb737323 2018-05-20 stsp col += logmsg_width;
356 bb737323 2018-05-20 stsp while (col <= avail) {
357 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
358 bb737323 2018-05-20 stsp col++;
359 881b2d3e 2018-04-30 stsp }
360 80ddbec8 2018-04-29 stsp done:
361 80ddbec8 2018-04-29 stsp free(logmsg0);
362 bb737323 2018-05-20 stsp free(wlogmsg);
363 6d9fbc00 2018-04-29 stsp free(author0);
364 bb737323 2018-05-20 stsp free(wauthor);
365 80ddbec8 2018-04-29 stsp free(line);
366 80ddbec8 2018-04-29 stsp return err;
367 80ddbec8 2018-04-29 stsp }
368 26ed57b2 2018-05-19 stsp
369 80ddbec8 2018-04-29 stsp struct commit_queue_entry {
370 80ddbec8 2018-04-29 stsp TAILQ_ENTRY(commit_queue_entry) entry;
371 80ddbec8 2018-04-29 stsp struct got_object_id *id;
372 80ddbec8 2018-04-29 stsp struct got_commit_object *commit;
373 80ddbec8 2018-04-29 stsp };
374 ecb28ae0 2018-07-16 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
375 ecb28ae0 2018-07-16 stsp struct commit_queue {
376 ecb28ae0 2018-07-16 stsp int ncommits;
377 ecb28ae0 2018-07-16 stsp struct commit_queue_head head;
378 ecb28ae0 2018-07-16 stsp };
379 80ddbec8 2018-04-29 stsp
380 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
381 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
382 899d86c2 2018-05-10 stsp struct got_object_id *id)
383 80ddbec8 2018-04-29 stsp {
384 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
385 80ddbec8 2018-04-29 stsp
386 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
387 80ddbec8 2018-04-29 stsp if (entry == NULL)
388 899d86c2 2018-05-10 stsp return NULL;
389 99db9666 2018-05-07 stsp
390 899d86c2 2018-05-10 stsp entry->id = id;
391 99db9666 2018-05-07 stsp entry->commit = commit;
392 899d86c2 2018-05-10 stsp return entry;
393 99db9666 2018-05-07 stsp }
394 80ddbec8 2018-04-29 stsp
395 99db9666 2018-05-07 stsp static void
396 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
397 99db9666 2018-05-07 stsp {
398 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
399 99db9666 2018-05-07 stsp
400 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
401 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
402 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
403 ecb28ae0 2018-07-16 stsp commits->ncommits--;
404 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
405 99db9666 2018-05-07 stsp free(entry);
406 99db9666 2018-05-07 stsp }
407 99db9666 2018-05-07 stsp
408 99db9666 2018-05-07 stsp static void
409 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
410 99db9666 2018-05-07 stsp {
411 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
412 99db9666 2018-05-07 stsp pop_commit(commits);
413 c4972b91 2018-05-07 stsp }
414 c4972b91 2018-05-07 stsp
415 c4972b91 2018-05-07 stsp static const struct got_error *
416 9ba79e04 2018-06-11 stsp queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
417 ecb28ae0 2018-07-16 stsp struct got_object_id *start_id, int minqueue, int init,
418 ecb28ae0 2018-07-16 stsp struct got_repository *repo, const char *path)
419 c4972b91 2018-05-07 stsp {
420 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
421 899d86c2 2018-05-10 stsp struct got_object_id *id;
422 9ba79e04 2018-06-11 stsp struct commit_queue_entry *entry;
423 ecb28ae0 2018-07-16 stsp int nfetched, nqueued = 0, found_obj = 0;
424 c8f60bff 2018-07-23 stsp int is_root_path = strcmp(path, "/") == 0;
425 c4972b91 2018-05-07 stsp
426 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_start(graph, start_id);
427 c4972b91 2018-05-07 stsp if (err)
428 c4972b91 2018-05-07 stsp return err;
429 c4972b91 2018-05-07 stsp
430 ecb28ae0 2018-07-16 stsp entry = TAILQ_LAST(&commits->head, commit_queue_head);
431 9ba79e04 2018-06-11 stsp if (entry && got_object_id_cmp(entry->id, start_id) == 0) {
432 9ba79e04 2018-06-11 stsp int nfetched;
433 899d86c2 2018-05-10 stsp
434 9ba79e04 2018-06-11 stsp /* Start ID's commit is already on the queue; skip over it. */
435 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
436 9ba79e04 2018-06-11 stsp if (err && err->code != GOT_ERR_ITER_NEED_MORE)
437 9ba79e04 2018-06-11 stsp return err;
438 9ba79e04 2018-06-11 stsp
439 9ba79e04 2018-06-11 stsp err = got_commit_graph_fetch_commits(&nfetched, graph, 1, repo);
440 9ba79e04 2018-06-11 stsp if (err)
441 9ba79e04 2018-06-11 stsp return err;
442 c4972b91 2018-05-07 stsp }
443 c4972b91 2018-05-07 stsp
444 9ba79e04 2018-06-11 stsp while (1) {
445 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
446 899d86c2 2018-05-10 stsp
447 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
448 9ba79e04 2018-06-11 stsp if (err) {
449 ecb28ae0 2018-07-16 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
450 ecb28ae0 2018-07-16 stsp break;
451 ecb28ae0 2018-07-16 stsp if (nqueued >= minqueue) {
452 9ba79e04 2018-06-11 stsp err = NULL;
453 ecb28ae0 2018-07-16 stsp break;
454 ecb28ae0 2018-07-16 stsp }
455 ecb28ae0 2018-07-16 stsp err = got_commit_graph_fetch_commits(&nfetched,
456 ecb28ae0 2018-07-16 stsp graph, 1, repo);
457 ecb28ae0 2018-07-16 stsp if (err)
458 ecb28ae0 2018-07-16 stsp return err;
459 ecb28ae0 2018-07-16 stsp continue;
460 9ba79e04 2018-06-11 stsp }
461 ecb28ae0 2018-07-16 stsp if (id == NULL)
462 ecb28ae0 2018-07-16 stsp break;
463 899d86c2 2018-05-10 stsp
464 9ba79e04 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
465 9ba79e04 2018-06-11 stsp if (err)
466 9ba79e04 2018-06-11 stsp break;
467 899d86c2 2018-05-10 stsp
468 c8f60bff 2018-07-23 stsp if (!is_root_path) {
469 ecb28ae0 2018-07-16 stsp struct got_object *obj;
470 ecb28ae0 2018-07-16 stsp struct got_object_qid *pid;
471 ecb28ae0 2018-07-16 stsp int changed = 0;
472 ecb28ae0 2018-07-16 stsp
473 ecb28ae0 2018-07-16 stsp err = got_object_open_by_path(&obj, repo, id, path);
474 ecb28ae0 2018-07-16 stsp if (err) {
475 c8f60bff 2018-07-23 stsp got_object_commit_close(commit);
476 ecb28ae0 2018-07-16 stsp if (err->code == GOT_ERR_NO_OBJ &&
477 ecb28ae0 2018-07-16 stsp (found_obj || !init)) {
478 ecb28ae0 2018-07-16 stsp /* History stops here. */
479 ecb28ae0 2018-07-16 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
480 ecb28ae0 2018-07-16 stsp }
481 ecb28ae0 2018-07-16 stsp break;
482 ecb28ae0 2018-07-16 stsp }
483 ecb28ae0 2018-07-16 stsp found_obj = 1;
484 ecb28ae0 2018-07-16 stsp
485 ecb28ae0 2018-07-16 stsp pid = SIMPLEQ_FIRST(&commit->parent_ids);
486 ecb28ae0 2018-07-16 stsp if (pid != NULL) {
487 ecb28ae0 2018-07-16 stsp struct got_object *pobj;
488 ecb28ae0 2018-07-16 stsp err = got_object_open_by_path(&pobj, repo,
489 ecb28ae0 2018-07-16 stsp pid->id, path);
490 ecb28ae0 2018-07-16 stsp if (err) {
491 ecb28ae0 2018-07-16 stsp if (err->code != GOT_ERR_NO_OBJ) {
492 ecb28ae0 2018-07-16 stsp got_object_close(obj);
493 c8f60bff 2018-07-23 stsp got_object_commit_close(commit);
494 ecb28ae0 2018-07-16 stsp break;
495 ecb28ae0 2018-07-16 stsp }
496 ecb28ae0 2018-07-16 stsp err = NULL;
497 ecb28ae0 2018-07-16 stsp changed = 1;
498 ecb28ae0 2018-07-16 stsp } else {
499 c2f16475 2018-07-23 stsp struct got_object_id *id, *pid;
500 c2f16475 2018-07-23 stsp id = got_object_get_id(obj);
501 c2f16475 2018-07-23 stsp if (id == NULL) {
502 c2f16475 2018-07-23 stsp err = got_error_from_errno();
503 c8f60bff 2018-07-23 stsp got_object_close(obj);
504 c8f60bff 2018-07-23 stsp got_object_close(pobj);
505 c2f16475 2018-07-23 stsp break;
506 c2f16475 2018-07-23 stsp }
507 c2f16475 2018-07-23 stsp pid = got_object_get_id(pobj);
508 c2f16475 2018-07-23 stsp if (pid == NULL) {
509 c2f16475 2018-07-23 stsp err = got_error_from_errno();
510 c2f16475 2018-07-23 stsp free(id);
511 c8f60bff 2018-07-23 stsp got_object_close(obj);
512 c8f60bff 2018-07-23 stsp got_object_close(pobj);
513 c2f16475 2018-07-23 stsp break;
514 c2f16475 2018-07-23 stsp }
515 c2f16475 2018-07-23 stsp changed =
516 c2f16475 2018-07-23 stsp (got_object_id_cmp(id, pid) != 0);
517 ecb28ae0 2018-07-16 stsp got_object_close(pobj);
518 c2f16475 2018-07-23 stsp free(id);
519 c2f16475 2018-07-23 stsp free(pid);
520 ecb28ae0 2018-07-16 stsp }
521 ecb28ae0 2018-07-16 stsp }
522 c8f60bff 2018-07-23 stsp got_object_close(obj);
523 ecb28ae0 2018-07-16 stsp if (!changed) {
524 ecb28ae0 2018-07-16 stsp got_object_commit_close(commit);
525 ecb28ae0 2018-07-16 stsp continue;
526 ecb28ae0 2018-07-16 stsp }
527 ecb28ae0 2018-07-16 stsp }
528 ecb28ae0 2018-07-16 stsp
529 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
530 9ba79e04 2018-06-11 stsp if (entry == NULL) {
531 9ba79e04 2018-06-11 stsp err = got_error_from_errno();
532 9ba79e04 2018-06-11 stsp break;
533 9ba79e04 2018-06-11 stsp }
534 ecb28ae0 2018-07-16 stsp TAILQ_INSERT_TAIL(&commits->head, entry, entry);
535 ecb28ae0 2018-07-16 stsp nqueued++;
536 ecb28ae0 2018-07-16 stsp commits->ncommits++;
537 899d86c2 2018-05-10 stsp }
538 899d86c2 2018-05-10 stsp
539 9ba79e04 2018-06-11 stsp return err;
540 99db9666 2018-05-07 stsp }
541 99db9666 2018-05-07 stsp
542 99db9666 2018-05-07 stsp static const struct got_error *
543 9ba79e04 2018-06-11 stsp fetch_next_commit(struct commit_queue_entry **pentry,
544 9ba79e04 2018-06-11 stsp struct commit_queue_entry *entry, struct commit_queue *commits,
545 ecb28ae0 2018-07-16 stsp struct got_commit_graph *graph, struct got_repository *repo,
546 ecb28ae0 2018-07-16 stsp const char *path)
547 899d86c2 2018-05-10 stsp {
548 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
549 899d86c2 2018-05-10 stsp
550 9ba79e04 2018-06-11 stsp *pentry = NULL;
551 899d86c2 2018-05-10 stsp
552 ecb28ae0 2018-07-16 stsp err = queue_commits(graph, commits, entry->id, 1, 0, repo, path);
553 ecb28ae0 2018-07-16 stsp if (err)
554 9ba79e04 2018-06-11 stsp return err;
555 899d86c2 2018-05-10 stsp
556 9ba79e04 2018-06-11 stsp /* Next entry to display should now be available. */
557 9ba79e04 2018-06-11 stsp *pentry = TAILQ_NEXT(entry, entry);
558 9ba79e04 2018-06-11 stsp if (*pentry == NULL)
559 9ba79e04 2018-06-11 stsp return got_error(GOT_ERR_NO_OBJ);
560 899d86c2 2018-05-10 stsp
561 9ba79e04 2018-06-11 stsp return NULL;
562 899d86c2 2018-05-10 stsp }
563 899d86c2 2018-05-10 stsp
564 899d86c2 2018-05-10 stsp static const struct got_error *
565 9ba79e04 2018-06-11 stsp get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
566 99db9666 2018-05-07 stsp {
567 9ba79e04 2018-06-11 stsp const struct got_error *err = NULL;
568 9ba79e04 2018-06-11 stsp struct got_reference *head_ref;
569 99db9666 2018-05-07 stsp
570 9ba79e04 2018-06-11 stsp *head_id = NULL;
571 899d86c2 2018-05-10 stsp
572 9ba79e04 2018-06-11 stsp err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
573 99db9666 2018-05-07 stsp if (err)
574 99db9666 2018-05-07 stsp return err;
575 99db9666 2018-05-07 stsp
576 9ba79e04 2018-06-11 stsp err = got_ref_resolve(head_id, repo, head_ref);
577 9ba79e04 2018-06-11 stsp got_ref_close(head_ref);
578 9ba79e04 2018-06-11 stsp if (err) {
579 9ba79e04 2018-06-11 stsp *head_id = NULL;
580 99db9666 2018-05-07 stsp return err;
581 0553a4e3 2018-04-30 stsp }
582 80ddbec8 2018-04-29 stsp
583 9ba79e04 2018-06-11 stsp return NULL;
584 0553a4e3 2018-04-30 stsp }
585 0553a4e3 2018-04-30 stsp
586 0553a4e3 2018-04-30 stsp static const struct got_error *
587 2814baeb 2018-08-01 stsp draw_commits(struct tog_view *view, struct commit_queue_entry **last,
588 ecb28ae0 2018-07-16 stsp struct commit_queue_entry **selected, struct commit_queue_entry *first,
589 a7f40148 2018-07-18 stsp struct commit_queue *commits, int selected_idx, int limit,
590 a7f40148 2018-07-18 stsp struct got_commit_graph *graph, struct got_repository *repo,
591 a7f40148 2018-07-18 stsp const char *path)
592 0553a4e3 2018-04-30 stsp {
593 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
594 0553a4e3 2018-04-30 stsp struct commit_queue_entry *entry;
595 ecb28ae0 2018-07-16 stsp int ncommits, width;
596 867c6645 2018-07-10 stsp char *id_str, *header;
597 ecb28ae0 2018-07-16 stsp wchar_t *wline;
598 0553a4e3 2018-04-30 stsp
599 e0d42f60 2018-07-22 stsp entry = first;
600 e0d42f60 2018-07-22 stsp ncommits = 0;
601 e0d42f60 2018-07-22 stsp while (entry) {
602 e0d42f60 2018-07-22 stsp if (ncommits == selected_idx) {
603 e0d42f60 2018-07-22 stsp *selected = entry;
604 e0d42f60 2018-07-22 stsp break;
605 e0d42f60 2018-07-22 stsp }
606 e0d42f60 2018-07-22 stsp entry = TAILQ_NEXT(entry, entry);
607 e0d42f60 2018-07-22 stsp ncommits++;
608 e0d42f60 2018-07-22 stsp }
609 e0d42f60 2018-07-22 stsp
610 867c6645 2018-07-10 stsp err = got_object_id_str(&id_str, (*selected)->id);
611 867c6645 2018-07-10 stsp if (err)
612 867c6645 2018-07-10 stsp return err;
613 867c6645 2018-07-10 stsp
614 ecb28ae0 2018-07-16 stsp if (path) {
615 ecb28ae0 2018-07-16 stsp if (asprintf(&header, "commit: %s [%s]", id_str, path) == -1) {
616 ecb28ae0 2018-07-16 stsp err = got_error_from_errno();
617 ecb28ae0 2018-07-16 stsp free(id_str);
618 ecb28ae0 2018-07-16 stsp return err;
619 ecb28ae0 2018-07-16 stsp }
620 ecb28ae0 2018-07-16 stsp } else if (asprintf(&header, "commit: %s", id_str) == -1) {
621 867c6645 2018-07-10 stsp err = got_error_from_errno();
622 867c6645 2018-07-10 stsp free(id_str);
623 867c6645 2018-07-10 stsp return err;
624 867c6645 2018-07-10 stsp }
625 ecb28ae0 2018-07-16 stsp free(id_str);
626 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, header, view->ncols);
627 ecb28ae0 2018-07-16 stsp if (err) {
628 ecb28ae0 2018-07-16 stsp free(header);
629 ecb28ae0 2018-07-16 stsp return err;
630 ecb28ae0 2018-07-16 stsp }
631 ecb28ae0 2018-07-16 stsp free(header);
632 867c6645 2018-07-10 stsp
633 2814baeb 2018-08-01 stsp werase(view->window);
634 867c6645 2018-07-10 stsp
635 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
636 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
637 2814baeb 2018-08-01 stsp waddch(view->window, '\n');
638 ecb28ae0 2018-07-16 stsp free(wline);
639 ecb28ae0 2018-07-16 stsp if (limit <= 1)
640 ecb28ae0 2018-07-16 stsp return NULL;
641 0553a4e3 2018-04-30 stsp
642 899d86c2 2018-05-10 stsp entry = first;
643 899d86c2 2018-05-10 stsp *last = first;
644 867c6645 2018-07-10 stsp ncommits = 0;
645 899d86c2 2018-05-10 stsp while (entry) {
646 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
647 899d86c2 2018-05-10 stsp break;
648 e0d42f60 2018-07-22 stsp if (ncommits == selected_idx)
649 2814baeb 2018-08-01 stsp wstandout(view->window);
650 2814baeb 2018-08-01 stsp err = draw_commit(view, entry->commit, entry->id);
651 cd0acaa7 2018-05-20 stsp if (ncommits == selected_idx)
652 2814baeb 2018-08-01 stsp wstandend(view->window);
653 0553a4e3 2018-04-30 stsp if (err)
654 0553a4e3 2018-04-30 stsp break;
655 0553a4e3 2018-04-30 stsp ncommits++;
656 899d86c2 2018-05-10 stsp *last = entry;
657 a7f40148 2018-07-18 stsp if (entry == TAILQ_LAST(&commits->head, commit_queue_head)) {
658 a7f40148 2018-07-18 stsp err = queue_commits(graph, commits, entry->id, 1,
659 a7f40148 2018-07-18 stsp 0, repo, path);
660 a7f40148 2018-07-18 stsp if (err) {
661 a7f40148 2018-07-18 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
662 a7f40148 2018-07-18 stsp return err;
663 a7f40148 2018-07-18 stsp err = NULL;
664 a7f40148 2018-07-18 stsp }
665 a7f40148 2018-07-18 stsp }
666 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
667 80ddbec8 2018-04-29 stsp }
668 80ddbec8 2018-04-29 stsp
669 80ddbec8 2018-04-29 stsp update_panels();
670 80ddbec8 2018-04-29 stsp doupdate();
671 0553a4e3 2018-04-30 stsp
672 80ddbec8 2018-04-29 stsp return err;
673 9f7d7167 2018-04-29 stsp }
674 07b55e75 2018-05-10 stsp
675 07b55e75 2018-05-10 stsp static void
676 16482c3b 2018-05-20 stsp scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
677 07b55e75 2018-05-10 stsp struct commit_queue *commits)
678 07b55e75 2018-05-10 stsp {
679 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
680 07b55e75 2018-05-10 stsp int nscrolled = 0;
681 07b55e75 2018-05-10 stsp
682 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
683 07b55e75 2018-05-10 stsp if (*first_displayed_entry == entry)
684 07b55e75 2018-05-10 stsp return;
685 9f7d7167 2018-04-29 stsp
686 07b55e75 2018-05-10 stsp entry = *first_displayed_entry;
687 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
688 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
689 07b55e75 2018-05-10 stsp if (entry) {
690 07b55e75 2018-05-10 stsp *first_displayed_entry = entry;
691 07b55e75 2018-05-10 stsp nscrolled++;
692 07b55e75 2018-05-10 stsp }
693 07b55e75 2018-05-10 stsp }
694 aa075928 2018-05-10 stsp }
695 aa075928 2018-05-10 stsp
696 aa075928 2018-05-10 stsp static const struct got_error *
697 16482c3b 2018-05-20 stsp scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
698 aa075928 2018-05-10 stsp struct commit_queue_entry *last_displayed_entry,
699 9ba79e04 2018-06-11 stsp struct commit_queue *commits, struct got_commit_graph *graph,
700 ecb28ae0 2018-07-16 stsp struct got_repository *repo, const char *path)
701 aa075928 2018-05-10 stsp {
702 aa075928 2018-05-10 stsp const struct got_error *err = NULL;
703 dd0a52c1 2018-05-20 stsp struct commit_queue_entry *pentry;
704 aa075928 2018-05-10 stsp int nscrolled = 0;
705 aa075928 2018-05-10 stsp
706 aa075928 2018-05-10 stsp do {
707 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(last_displayed_entry, entry);
708 aa075928 2018-05-10 stsp if (pentry == NULL) {
709 9ba79e04 2018-06-11 stsp err = fetch_next_commit(&pentry, last_displayed_entry,
710 ecb28ae0 2018-07-16 stsp commits, graph, repo, path);
711 9a6bf2a5 2018-05-20 stsp if (err || pentry == NULL)
712 aa075928 2018-05-10 stsp break;
713 aa075928 2018-05-10 stsp }
714 dd0a52c1 2018-05-20 stsp last_displayed_entry = pentry;
715 aa075928 2018-05-10 stsp
716 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(*first_displayed_entry, entry);
717 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
718 dd0a52c1 2018-05-20 stsp break;
719 aa075928 2018-05-10 stsp *first_displayed_entry = pentry;
720 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
721 aa075928 2018-05-10 stsp
722 dd0a52c1 2018-05-20 stsp return err;
723 07b55e75 2018-05-10 stsp }
724 4a7f7875 2018-05-10 stsp
725 cd0acaa7 2018-05-20 stsp static const struct got_error *
726 6d0fee91 2018-08-01 stsp show_commit(struct tog_view *parent_view, struct commit_queue_entry *entry,
727 6d0fee91 2018-08-01 stsp struct got_repository *repo)
728 cd0acaa7 2018-05-20 stsp {
729 cd0acaa7 2018-05-20 stsp const struct got_error *err;
730 cd0acaa7 2018-05-20 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
731 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
732 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
733 cd0acaa7 2018-05-20 stsp
734 cd0acaa7 2018-05-20 stsp err = got_object_open(&obj2, repo, entry->id);
735 cd0acaa7 2018-05-20 stsp if (err)
736 cd0acaa7 2018-05-20 stsp return err;
737 cd0acaa7 2018-05-20 stsp
738 9ba79e04 2018-06-11 stsp parent_id = SIMPLEQ_FIRST(&entry->commit->parent_ids);
739 9ba79e04 2018-06-11 stsp if (parent_id) {
740 9ba79e04 2018-06-11 stsp err = got_object_open(&obj1, repo, parent_id->id);
741 cd0acaa7 2018-05-20 stsp if (err)
742 cd0acaa7 2018-05-20 stsp goto done;
743 cd0acaa7 2018-05-20 stsp }
744 cd0acaa7 2018-05-20 stsp
745 6d0fee91 2018-08-01 stsp view = open_view(0, 0, 0, 0, parent_view);
746 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
747 ea5e7bb5 2018-08-01 stsp err = got_error_from_errno();
748 ea5e7bb5 2018-08-01 stsp goto done;
749 ea5e7bb5 2018-08-01 stsp }
750 ea5e7bb5 2018-08-01 stsp
751 ea5e7bb5 2018-08-01 stsp err = show_diff_view(view, obj1, obj2, repo);
752 ea5e7bb5 2018-08-01 stsp close_view(view);
753 cd0acaa7 2018-05-20 stsp done:
754 cd0acaa7 2018-05-20 stsp if (obj1)
755 cd0acaa7 2018-05-20 stsp got_object_close(obj1);
756 cd0acaa7 2018-05-20 stsp if (obj2)
757 cd0acaa7 2018-05-20 stsp got_object_close(obj2);
758 cd0acaa7 2018-05-20 stsp return err;
759 4a7f7875 2018-05-10 stsp }
760 4a7f7875 2018-05-10 stsp
761 80ddbec8 2018-04-29 stsp static const struct got_error *
762 6d0fee91 2018-08-01 stsp browse_commit(struct tog_view *parent_view, struct commit_queue_entry *entry,
763 6d0fee91 2018-08-01 stsp struct got_repository *repo)
764 9343a5fb 2018-06-23 stsp {
765 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
766 9343a5fb 2018-06-23 stsp struct got_tree_object *tree;
767 5221c383 2018-08-01 stsp struct tog_view *view;
768 9343a5fb 2018-06-23 stsp
769 9343a5fb 2018-06-23 stsp err = got_object_open_as_tree(&tree, repo, entry->commit->tree_id);
770 9343a5fb 2018-06-23 stsp if (err)
771 9343a5fb 2018-06-23 stsp return err;
772 9343a5fb 2018-06-23 stsp
773 6d0fee91 2018-08-01 stsp view = open_view(0, 0, 0, 0, parent_view);
774 5221c383 2018-08-01 stsp if (view == NULL) {
775 5221c383 2018-08-01 stsp err = got_error_from_errno();
776 5221c383 2018-08-01 stsp goto done;
777 5221c383 2018-08-01 stsp }
778 5221c383 2018-08-01 stsp err = show_tree_view(view, tree, entry->id, repo);
779 5221c383 2018-08-01 stsp close_view(view);
780 5221c383 2018-08-01 stsp done:
781 9343a5fb 2018-06-23 stsp got_object_tree_close(tree);
782 9343a5fb 2018-06-23 stsp return err;
783 9343a5fb 2018-06-23 stsp }
784 9343a5fb 2018-06-23 stsp
785 9343a5fb 2018-06-23 stsp static const struct got_error *
786 04cc582a 2018-08-01 stsp show_log_view(struct tog_view *view, struct got_object_id *start_id,
787 04cc582a 2018-08-01 stsp struct got_repository *repo, const char *path)
788 80ddbec8 2018-04-29 stsp {
789 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
790 9ba79e04 2018-06-11 stsp struct got_object_id *head_id = NULL;
791 ecb28ae0 2018-07-16 stsp int ch, done = 0, selected = 0, nfetched;
792 ecb28ae0 2018-07-16 stsp struct got_commit_graph *graph = NULL;
793 0553a4e3 2018-04-30 stsp struct commit_queue commits;
794 899d86c2 2018-05-10 stsp struct commit_queue_entry *first_displayed_entry = NULL;
795 899d86c2 2018-05-10 stsp struct commit_queue_entry *last_displayed_entry = NULL;
796 cd0acaa7 2018-05-20 stsp struct commit_queue_entry *selected_entry = NULL;
797 ecb28ae0 2018-07-16 stsp char *in_repo_path = NULL;
798 80ddbec8 2018-04-29 stsp
799 ecb28ae0 2018-07-16 stsp err = got_repo_map_path(&in_repo_path, repo, path);
800 ecb28ae0 2018-07-16 stsp if (err != NULL)
801 ecb28ae0 2018-07-16 stsp goto done;
802 ecb28ae0 2018-07-16 stsp
803 9ba79e04 2018-06-11 stsp err = get_head_commit_id(&head_id, repo);
804 9ba79e04 2018-06-11 stsp if (err)
805 9ba79e04 2018-06-11 stsp return err;
806 9ba79e04 2018-06-11 stsp
807 034e3b69 2018-07-22 stsp /* The graph contains all commits. */
808 5489743f 2018-06-13 stsp err = got_commit_graph_open(&graph, head_id, 0, repo);
809 9ba79e04 2018-06-11 stsp if (err)
810 9ba79e04 2018-06-11 stsp goto done;
811 034e3b69 2018-07-22 stsp /* The commit queue contains a subset of commits filtered by path. */
812 034e3b69 2018-07-22 stsp TAILQ_INIT(&commits.head);
813 034e3b69 2018-07-22 stsp commits.ncommits = 0;
814 9ba79e04 2018-06-11 stsp
815 9ba79e04 2018-06-11 stsp /* Populate commit graph with a sufficient number of commits. */
816 9ba79e04 2018-06-11 stsp err = got_commit_graph_fetch_commits_up_to(&nfetched, graph, start_id,
817 9ba79e04 2018-06-11 stsp repo);
818 9ba79e04 2018-06-11 stsp if (err)
819 9ba79e04 2018-06-11 stsp goto done;
820 9ba79e04 2018-06-11 stsp
821 9ba79e04 2018-06-11 stsp /*
822 9ba79e04 2018-06-11 stsp * Open the initial batch of commits, sorted in commit graph order.
823 9ba79e04 2018-06-11 stsp * We keep all commits open throughout the lifetime of the log view
824 9ba79e04 2018-06-11 stsp * in order to avoid having to re-fetch commits from disk while
825 9ba79e04 2018-06-11 stsp * updating the display.
826 9ba79e04 2018-06-11 stsp */
827 f7d12f7e 2018-08-01 stsp err = queue_commits(graph, &commits, start_id, view->nlines, 1, repo,
828 ecb28ae0 2018-07-16 stsp in_repo_path);
829 55198a88 2018-07-16 stsp if (err) {
830 55198a88 2018-07-16 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
831 55198a88 2018-07-16 stsp goto done;
832 55198a88 2018-07-16 stsp err = NULL;
833 2814baeb 2018-08-01 stsp }
834 2814baeb 2018-08-01 stsp
835 04cc582a 2018-08-01 stsp show_panel(view->panel);
836 04cc582a 2018-08-01 stsp
837 034e3b69 2018-07-22 stsp first_displayed_entry = TAILQ_FIRST(&commits.head);
838 867c6645 2018-07-10 stsp selected_entry = first_displayed_entry;
839 899d86c2 2018-05-10 stsp while (!done) {
840 2814baeb 2018-08-01 stsp err = draw_commits(view, &last_displayed_entry, &selected_entry,
841 f7d12f7e 2018-08-01 stsp first_displayed_entry, &commits, selected, view->nlines,
842 a7f40148 2018-07-18 stsp graph, repo, in_repo_path);
843 80ddbec8 2018-04-29 stsp if (err)
844 d0f709cb 2018-04-30 stsp goto done;
845 80ddbec8 2018-04-29 stsp
846 80ddbec8 2018-04-29 stsp nodelay(stdscr, FALSE);
847 2814baeb 2018-08-01 stsp ch = wgetch(view->window);
848 f7182337 2018-05-20 stsp nodelay(stdscr, TRUE);
849 80ddbec8 2018-04-29 stsp switch (ch) {
850 31120ada 2018-04-30 stsp case ERR:
851 31120ada 2018-04-30 stsp break;
852 80ddbec8 2018-04-29 stsp case 'q':
853 80ddbec8 2018-04-29 stsp done = 1;
854 80ddbec8 2018-04-29 stsp break;
855 80ddbec8 2018-04-29 stsp case 'k':
856 80ddbec8 2018-04-29 stsp case KEY_UP:
857 80ddbec8 2018-04-29 stsp if (selected > 0)
858 80ddbec8 2018-04-29 stsp selected--;
859 8ec9698d 2018-05-10 stsp if (selected > 0)
860 d91e25cb 2018-05-10 stsp break;
861 07b55e75 2018-05-10 stsp scroll_up(&first_displayed_entry, 1, &commits);
862 80ddbec8 2018-04-29 stsp break;
863 48531068 2018-05-10 stsp case KEY_PPAGE:
864 ecb28ae0 2018-07-16 stsp if (TAILQ_FIRST(&commits.head) ==
865 dfc1d240 2018-05-10 stsp first_displayed_entry) {
866 dfc1d240 2018-05-10 stsp selected = 0;
867 dfc1d240 2018-05-10 stsp break;
868 dfc1d240 2018-05-10 stsp }
869 f7d12f7e 2018-08-01 stsp scroll_up(&first_displayed_entry, view->nlines,
870 48531068 2018-05-10 stsp &commits);
871 48531068 2018-05-10 stsp break;
872 80ddbec8 2018-04-29 stsp case 'j':
873 80ddbec8 2018-04-29 stsp case KEY_DOWN:
874 f7d12f7e 2018-08-01 stsp if (selected < MIN(view->nlines - 2,
875 ecb28ae0 2018-07-16 stsp commits.ncommits - 1)) {
876 15c91275 2018-05-20 stsp selected++;
877 15c91275 2018-05-20 stsp break;
878 8df4052c 2018-05-20 stsp }
879 15c91275 2018-05-20 stsp err = scroll_down(&first_displayed_entry, 1,
880 9ba79e04 2018-06-11 stsp last_displayed_entry, &commits, graph,
881 ecb28ae0 2018-07-16 stsp repo, in_repo_path);
882 ecb28ae0 2018-07-16 stsp if (err) {
883 ecb28ae0 2018-07-16 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
884 ecb28ae0 2018-07-16 stsp goto done;
885 ecb28ae0 2018-07-16 stsp err = NULL;
886 ecb28ae0 2018-07-16 stsp }
887 4a7f7875 2018-05-10 stsp break;
888 ecb28ae0 2018-07-16 stsp case KEY_NPAGE: {
889 ecb28ae0 2018-07-16 stsp struct commit_queue_entry *first = first_displayed_entry;
890 f7d12f7e 2018-08-01 stsp err = scroll_down(&first_displayed_entry, view->nlines,
891 9ba79e04 2018-06-11 stsp last_displayed_entry, &commits, graph,
892 ecb28ae0 2018-07-16 stsp repo, in_repo_path);
893 ecb28ae0 2018-07-16 stsp if (err) {
894 ecb28ae0 2018-07-16 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
895 ecb28ae0 2018-07-16 stsp goto done;
896 ecb28ae0 2018-07-16 stsp /* can't scroll any further; move cursor down */
897 ecb28ae0 2018-07-16 stsp if (first == first_displayed_entry && selected <
898 f7d12f7e 2018-08-01 stsp MIN(view->nlines - 2, commits.ncommits - 1)) {
899 f7d12f7e 2018-08-01 stsp selected = MIN(view->nlines - 2,
900 ecb28ae0 2018-07-16 stsp commits.ncommits - 1);
901 ecb28ae0 2018-07-16 stsp }
902 ecb28ae0 2018-07-16 stsp err = NULL;
903 ecb28ae0 2018-07-16 stsp }
904 80ddbec8 2018-04-29 stsp break;
905 ecb28ae0 2018-07-16 stsp }
906 d6df9be4 2018-04-30 stsp case KEY_RESIZE:
907 a41d2007 2018-08-01 stsp err = view_resize(view);
908 a41d2007 2018-08-01 stsp if (err)
909 a41d2007 2018-08-01 stsp goto done;
910 f7d12f7e 2018-08-01 stsp if (selected > view->nlines - 2)
911 f7d12f7e 2018-08-01 stsp selected = view->nlines - 2;
912 55198a88 2018-07-16 stsp if (selected > commits.ncommits - 1)
913 55198a88 2018-07-16 stsp selected = commits.ncommits - 1;
914 cd0acaa7 2018-05-20 stsp break;
915 cd0acaa7 2018-05-20 stsp case KEY_ENTER:
916 cd0acaa7 2018-05-20 stsp case '\r':
917 6d0fee91 2018-08-01 stsp err = show_commit(view, selected_entry, repo);
918 9343a5fb 2018-06-23 stsp if (err)
919 9343a5fb 2018-06-23 stsp goto done;
920 2814baeb 2018-08-01 stsp show_panel(view->panel);
921 9343a5fb 2018-06-23 stsp break;
922 9343a5fb 2018-06-23 stsp case 't':
923 6d0fee91 2018-08-01 stsp err = browse_commit(view, selected_entry, repo);
924 cd0acaa7 2018-05-20 stsp if (err)
925 0d4100bb 2018-06-23 stsp goto done;
926 2814baeb 2018-08-01 stsp show_panel(view->panel);
927 31120ada 2018-04-30 stsp break;
928 80ddbec8 2018-04-29 stsp default:
929 80ddbec8 2018-04-29 stsp break;
930 80ddbec8 2018-04-29 stsp }
931 899d86c2 2018-05-10 stsp }
932 80ddbec8 2018-04-29 stsp done:
933 9ba79e04 2018-06-11 stsp free(head_id);
934 9ba79e04 2018-06-11 stsp if (graph)
935 9ba79e04 2018-06-11 stsp got_commit_graph_close(graph);
936 0553a4e3 2018-04-30 stsp free_commits(&commits);
937 ecb28ae0 2018-07-16 stsp free(in_repo_path);
938 80ddbec8 2018-04-29 stsp return err;
939 80ddbec8 2018-04-29 stsp }
940 80ddbec8 2018-04-29 stsp
941 4ed7e80c 2018-05-20 stsp static const struct got_error *
942 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
943 9f7d7167 2018-04-29 stsp {
944 80ddbec8 2018-04-29 stsp const struct got_error *error;
945 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
946 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
947 ecb28ae0 2018-07-16 stsp char *path = NULL, *repo_path = NULL, *cwd = NULL;
948 80ddbec8 2018-04-29 stsp char *start_commit = NULL;
949 80ddbec8 2018-04-29 stsp int ch;
950 04cc582a 2018-08-01 stsp struct tog_view *view;
951 80ddbec8 2018-04-29 stsp
952 80ddbec8 2018-04-29 stsp #ifndef PROFILE
953 80ddbec8 2018-04-29 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
954 80ddbec8 2018-04-29 stsp err(1, "pledge");
955 80ddbec8 2018-04-29 stsp #endif
956 80ddbec8 2018-04-29 stsp
957 ecb28ae0 2018-07-16 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
958 80ddbec8 2018-04-29 stsp switch (ch) {
959 80ddbec8 2018-04-29 stsp case 'c':
960 80ddbec8 2018-04-29 stsp start_commit = optarg;
961 80ddbec8 2018-04-29 stsp break;
962 ecb28ae0 2018-07-16 stsp case 'r':
963 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
964 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
965 ecb28ae0 2018-07-16 stsp err(1, "-r option");
966 ecb28ae0 2018-07-16 stsp break;
967 80ddbec8 2018-04-29 stsp default:
968 80ddbec8 2018-04-29 stsp usage();
969 80ddbec8 2018-04-29 stsp /* NOTREACHED */
970 80ddbec8 2018-04-29 stsp }
971 80ddbec8 2018-04-29 stsp }
972 80ddbec8 2018-04-29 stsp
973 80ddbec8 2018-04-29 stsp argc -= optind;
974 80ddbec8 2018-04-29 stsp argv += optind;
975 80ddbec8 2018-04-29 stsp
976 ecb28ae0 2018-07-16 stsp if (argc == 0)
977 ecb28ae0 2018-07-16 stsp path = strdup("");
978 ecb28ae0 2018-07-16 stsp else if (argc == 1)
979 ecb28ae0 2018-07-16 stsp path = strdup(argv[0]);
980 ecb28ae0 2018-07-16 stsp else
981 80ddbec8 2018-04-29 stsp usage_log();
982 ecb28ae0 2018-07-16 stsp if (path == NULL)
983 ecb28ae0 2018-07-16 stsp return got_error_from_errno();
984 80ddbec8 2018-04-29 stsp
985 ecb28ae0 2018-07-16 stsp cwd = getcwd(NULL, 0);
986 ecb28ae0 2018-07-16 stsp if (cwd == NULL) {
987 ecb28ae0 2018-07-16 stsp error = got_error_from_errno();
988 ecb28ae0 2018-07-16 stsp goto done;
989 ecb28ae0 2018-07-16 stsp }
990 ecb28ae0 2018-07-16 stsp if (repo_path == NULL) {
991 ecb28ae0 2018-07-16 stsp repo_path = strdup(cwd);
992 ecb28ae0 2018-07-16 stsp if (repo_path == NULL) {
993 ecb28ae0 2018-07-16 stsp error = got_error_from_errno();
994 ecb28ae0 2018-07-16 stsp goto done;
995 ecb28ae0 2018-07-16 stsp }
996 ecb28ae0 2018-07-16 stsp }
997 ecb28ae0 2018-07-16 stsp
998 80ddbec8 2018-04-29 stsp error = got_repo_open(&repo, repo_path);
999 80ddbec8 2018-04-29 stsp if (error != NULL)
1000 ecb28ae0 2018-07-16 stsp goto done;
1001 80ddbec8 2018-04-29 stsp
1002 80ddbec8 2018-04-29 stsp if (start_commit == NULL) {
1003 899d86c2 2018-05-10 stsp error = get_head_commit_id(&start_id, repo);
1004 80ddbec8 2018-04-29 stsp if (error != NULL)
1005 ecb28ae0 2018-07-16 stsp goto done;
1006 80ddbec8 2018-04-29 stsp } else {
1007 80ddbec8 2018-04-29 stsp struct got_object *obj;
1008 80ddbec8 2018-04-29 stsp error = got_object_open_by_id_str(&obj, repo, start_commit);
1009 80ddbec8 2018-04-29 stsp if (error == NULL) {
1010 899d86c2 2018-05-10 stsp start_id = got_object_get_id(obj);
1011 899d86c2 2018-05-10 stsp if (start_id == NULL)
1012 80ddbec8 2018-04-29 stsp error = got_error_from_errno();
1013 ecb28ae0 2018-07-16 stsp goto done;
1014 80ddbec8 2018-04-29 stsp }
1015 80ddbec8 2018-04-29 stsp }
1016 80ddbec8 2018-04-29 stsp if (error != NULL)
1017 ecb28ae0 2018-07-16 stsp goto done;
1018 ecb28ae0 2018-07-16 stsp
1019 6d0fee91 2018-08-01 stsp view = open_view(0, 0, 0, 0, NULL);
1020 04cc582a 2018-08-01 stsp if (view == NULL) {
1021 04cc582a 2018-08-01 stsp error = got_error_from_errno();
1022 04cc582a 2018-08-01 stsp goto done;
1023 04cc582a 2018-08-01 stsp }
1024 04cc582a 2018-08-01 stsp error = show_log_view(view, start_id, repo, path);
1025 04cc582a 2018-08-01 stsp close_view(view);
1026 ecb28ae0 2018-07-16 stsp done:
1027 ecb28ae0 2018-07-16 stsp free(repo_path);
1028 ecb28ae0 2018-07-16 stsp free(cwd);
1029 ecb28ae0 2018-07-16 stsp free(path);
1030 899d86c2 2018-05-10 stsp free(start_id);
1031 ecb28ae0 2018-07-16 stsp if (repo)
1032 ecb28ae0 2018-07-16 stsp got_repo_close(repo);
1033 80ddbec8 2018-04-29 stsp return error;
1034 9f7d7167 2018-04-29 stsp }
1035 9f7d7167 2018-04-29 stsp
1036 4ed7e80c 2018-05-20 stsp __dead static void
1037 9f7d7167 2018-04-29 stsp usage_diff(void)
1038 9f7d7167 2018-04-29 stsp {
1039 80ddbec8 2018-04-29 stsp endwin();
1040 9f7d7167 2018-04-29 stsp fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1041 9f7d7167 2018-04-29 stsp getprogname());
1042 9f7d7167 2018-04-29 stsp exit(1);
1043 b304db33 2018-05-20 stsp }
1044 b304db33 2018-05-20 stsp
1045 b304db33 2018-05-20 stsp static char *
1046 b304db33 2018-05-20 stsp parse_next_line(FILE *f, size_t *len)
1047 b304db33 2018-05-20 stsp {
1048 b304db33 2018-05-20 stsp char *line;
1049 b304db33 2018-05-20 stsp size_t linelen;
1050 b304db33 2018-05-20 stsp size_t lineno;
1051 b304db33 2018-05-20 stsp const char delim[3] = { '\0', '\0', '\0'};
1052 b304db33 2018-05-20 stsp
1053 b304db33 2018-05-20 stsp line = fparseln(f, &linelen, &lineno, delim, 0);
1054 b304db33 2018-05-20 stsp if (len)
1055 b304db33 2018-05-20 stsp *len = linelen;
1056 b304db33 2018-05-20 stsp return line;
1057 26ed57b2 2018-05-19 stsp }
1058 26ed57b2 2018-05-19 stsp
1059 4ed7e80c 2018-05-20 stsp static const struct got_error *
1060 f7d12f7e 2018-08-01 stsp draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1061 a70480e0 2018-06-23 stsp int *last_displayed_line, int *eof, int max_lines)
1062 26ed57b2 2018-05-19 stsp {
1063 61e69b96 2018-05-20 stsp const struct got_error *err;
1064 26ed57b2 2018-05-19 stsp int nlines = 0, nprinted = 0;
1065 b304db33 2018-05-20 stsp char *line;
1066 b304db33 2018-05-20 stsp size_t len;
1067 61e69b96 2018-05-20 stsp wchar_t *wline;
1068 e0b650dd 2018-05-20 stsp int width;
1069 26ed57b2 2018-05-19 stsp
1070 26ed57b2 2018-05-19 stsp rewind(f);
1071 f7d12f7e 2018-08-01 stsp werase(view->window);
1072 26ed57b2 2018-05-19 stsp
1073 26ed57b2 2018-05-19 stsp *eof = 0;
1074 26ed57b2 2018-05-19 stsp while (nprinted < max_lines) {
1075 b304db33 2018-05-20 stsp line = parse_next_line(f, &len);
1076 26ed57b2 2018-05-19 stsp if (line == NULL) {
1077 26ed57b2 2018-05-19 stsp *eof = 1;
1078 26ed57b2 2018-05-19 stsp break;
1079 26ed57b2 2018-05-19 stsp }
1080 26ed57b2 2018-05-19 stsp if (++nlines < *first_displayed_line) {
1081 26ed57b2 2018-05-19 stsp free(line);
1082 26ed57b2 2018-05-19 stsp continue;
1083 26ed57b2 2018-05-19 stsp }
1084 26ed57b2 2018-05-19 stsp
1085 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
1086 61e69b96 2018-05-20 stsp if (err) {
1087 61e69b96 2018-05-20 stsp free(line);
1088 2550e4c3 2018-07-13 stsp free(wline);
1089 61e69b96 2018-05-20 stsp return err;
1090 61e69b96 2018-05-20 stsp }
1091 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
1092 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
1093 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
1094 26ed57b2 2018-05-19 stsp if (++nprinted == 1)
1095 26ed57b2 2018-05-19 stsp *first_displayed_line = nlines;
1096 26ed57b2 2018-05-19 stsp free(line);
1097 2550e4c3 2018-07-13 stsp free(wline);
1098 2550e4c3 2018-07-13 stsp wline = NULL;
1099 26ed57b2 2018-05-19 stsp }
1100 26ed57b2 2018-05-19 stsp *last_displayed_line = nlines;
1101 26ed57b2 2018-05-19 stsp
1102 26ed57b2 2018-05-19 stsp update_panels();
1103 26ed57b2 2018-05-19 stsp doupdate();
1104 26ed57b2 2018-05-19 stsp
1105 26ed57b2 2018-05-19 stsp return NULL;
1106 9f7d7167 2018-04-29 stsp }
1107 9f7d7167 2018-04-29 stsp
1108 4ed7e80c 2018-05-20 stsp static const struct got_error *
1109 ea5e7bb5 2018-08-01 stsp show_diff_view(struct tog_view *view, struct got_object *obj1,
1110 ea5e7bb5 2018-08-01 stsp struct got_object *obj2, struct got_repository *repo)
1111 26ed57b2 2018-05-19 stsp {
1112 26ed57b2 2018-05-19 stsp const struct got_error *err;
1113 26ed57b2 2018-05-19 stsp FILE *f;
1114 f7d12f7e 2018-08-01 stsp int ch, done = 0;
1115 f7d12f7e 2018-08-01 stsp int first_displayed_line = 1, last_displayed_line = view->nlines;
1116 b304db33 2018-05-20 stsp int eof, i;
1117 26ed57b2 2018-05-19 stsp
1118 cd0acaa7 2018-05-20 stsp if (obj1 != NULL && obj2 != NULL &&
1119 cd0acaa7 2018-05-20 stsp got_object_get_type(obj1) != got_object_get_type(obj2))
1120 26ed57b2 2018-05-19 stsp return got_error(GOT_ERR_OBJ_TYPE);
1121 26ed57b2 2018-05-19 stsp
1122 511a516b 2018-05-19 stsp f = got_opentemp();
1123 26ed57b2 2018-05-19 stsp if (f == NULL)
1124 26ed57b2 2018-05-19 stsp return got_error_from_errno();
1125 26ed57b2 2018-05-19 stsp
1126 cd0acaa7 2018-05-20 stsp switch (got_object_get_type(obj1 ? obj1 : obj2)) {
1127 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
1128 11528a82 2018-05-19 stsp err = got_diff_objects_as_blobs(obj1, obj2, repo, f);
1129 26ed57b2 2018-05-19 stsp break;
1130 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
1131 11528a82 2018-05-19 stsp err = got_diff_objects_as_trees(obj1, obj2, repo, f);
1132 26ed57b2 2018-05-19 stsp break;
1133 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_COMMIT:
1134 11528a82 2018-05-19 stsp err = got_diff_objects_as_commits(obj1, obj2, repo, f);
1135 26ed57b2 2018-05-19 stsp break;
1136 26ed57b2 2018-05-19 stsp default:
1137 26ed57b2 2018-05-19 stsp return got_error(GOT_ERR_OBJ_TYPE);
1138 26ed57b2 2018-05-19 stsp }
1139 26ed57b2 2018-05-19 stsp
1140 26ed57b2 2018-05-19 stsp fflush(f);
1141 26ed57b2 2018-05-19 stsp
1142 ea5e7bb5 2018-08-01 stsp show_panel(view->panel);
1143 26ed57b2 2018-05-19 stsp
1144 26ed57b2 2018-05-19 stsp while (!done) {
1145 f7d12f7e 2018-08-01 stsp err = draw_file(view, f, &first_displayed_line,
1146 f7d12f7e 2018-08-01 stsp &last_displayed_line, &eof, view->nlines);
1147 26ed57b2 2018-05-19 stsp if (err)
1148 26ed57b2 2018-05-19 stsp break;
1149 26ed57b2 2018-05-19 stsp nodelay(stdscr, FALSE);
1150 ea5e7bb5 2018-08-01 stsp ch = wgetch(view->window);
1151 f7182337 2018-05-20 stsp nodelay(stdscr, TRUE);
1152 26ed57b2 2018-05-19 stsp switch (ch) {
1153 26ed57b2 2018-05-19 stsp case 'q':
1154 26ed57b2 2018-05-19 stsp done = 1;
1155 26ed57b2 2018-05-19 stsp break;
1156 26ed57b2 2018-05-19 stsp case 'k':
1157 26ed57b2 2018-05-19 stsp case KEY_UP:
1158 26ed57b2 2018-05-19 stsp if (first_displayed_line > 1)
1159 b304db33 2018-05-20 stsp first_displayed_line--;
1160 b304db33 2018-05-20 stsp break;
1161 b304db33 2018-05-20 stsp case KEY_PPAGE:
1162 c46b9352 2018-07-12 stsp case KEY_BACKSPACE:
1163 b304db33 2018-05-20 stsp i = 0;
1164 f7d12f7e 2018-08-01 stsp while (i++ < view->nlines - 1 &&
1165 d56caa55 2018-05-20 stsp first_displayed_line > 1)
1166 26ed57b2 2018-05-19 stsp first_displayed_line--;
1167 26ed57b2 2018-05-19 stsp break;
1168 26ed57b2 2018-05-19 stsp case 'j':
1169 26ed57b2 2018-05-19 stsp case KEY_DOWN:
1170 26ed57b2 2018-05-19 stsp if (!eof)
1171 26ed57b2 2018-05-19 stsp first_displayed_line++;
1172 26ed57b2 2018-05-19 stsp break;
1173 b304db33 2018-05-20 stsp case KEY_NPAGE:
1174 75e48879 2018-05-20 stsp case ' ':
1175 b304db33 2018-05-20 stsp i = 0;
1176 f7d12f7e 2018-08-01 stsp while (!eof && i++ < view->nlines - 1) {
1177 b304db33 2018-05-20 stsp char *line = parse_next_line(f, NULL);
1178 b304db33 2018-05-20 stsp first_displayed_line++;
1179 b304db33 2018-05-20 stsp if (line == NULL)
1180 b304db33 2018-05-20 stsp break;
1181 b304db33 2018-05-20 stsp }
1182 b304db33 2018-05-20 stsp break;
1183 82357c0a 2018-08-01 stsp case KEY_RESIZE:
1184 a41d2007 2018-08-01 stsp err = view_resize(view);
1185 a41d2007 2018-08-01 stsp if (err)
1186 a41d2007 2018-08-01 stsp goto done;
1187 82357c0a 2018-08-01 stsp break;
1188 26ed57b2 2018-05-19 stsp default:
1189 26ed57b2 2018-05-19 stsp break;
1190 26ed57b2 2018-05-19 stsp }
1191 26ed57b2 2018-05-19 stsp }
1192 a41d2007 2018-08-01 stsp done:
1193 26ed57b2 2018-05-19 stsp fclose(f);
1194 26ed57b2 2018-05-19 stsp return err;
1195 26ed57b2 2018-05-19 stsp }
1196 26ed57b2 2018-05-19 stsp
1197 4ed7e80c 2018-05-20 stsp static const struct got_error *
1198 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
1199 9f7d7167 2018-04-29 stsp {
1200 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
1201 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
1202 26ed57b2 2018-05-19 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
1203 26ed57b2 2018-05-19 stsp char *repo_path = NULL;
1204 26ed57b2 2018-05-19 stsp char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
1205 26ed57b2 2018-05-19 stsp int ch;
1206 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
1207 26ed57b2 2018-05-19 stsp
1208 26ed57b2 2018-05-19 stsp #ifndef PROFILE
1209 26ed57b2 2018-05-19 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1210 26ed57b2 2018-05-19 stsp err(1, "pledge");
1211 26ed57b2 2018-05-19 stsp #endif
1212 26ed57b2 2018-05-19 stsp
1213 26ed57b2 2018-05-19 stsp while ((ch = getopt(argc, argv, "")) != -1) {
1214 26ed57b2 2018-05-19 stsp switch (ch) {
1215 26ed57b2 2018-05-19 stsp default:
1216 26ed57b2 2018-05-19 stsp usage();
1217 26ed57b2 2018-05-19 stsp /* NOTREACHED */
1218 26ed57b2 2018-05-19 stsp }
1219 26ed57b2 2018-05-19 stsp }
1220 26ed57b2 2018-05-19 stsp
1221 26ed57b2 2018-05-19 stsp argc -= optind;
1222 26ed57b2 2018-05-19 stsp argv += optind;
1223 26ed57b2 2018-05-19 stsp
1224 26ed57b2 2018-05-19 stsp if (argc == 0) {
1225 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
1226 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
1227 26ed57b2 2018-05-19 stsp repo_path = getcwd(NULL, 0);
1228 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
1229 26ed57b2 2018-05-19 stsp return got_error_from_errno();
1230 26ed57b2 2018-05-19 stsp obj_id_str1 = argv[0];
1231 26ed57b2 2018-05-19 stsp obj_id_str2 = argv[1];
1232 26ed57b2 2018-05-19 stsp } else if (argc == 3) {
1233 26ed57b2 2018-05-19 stsp repo_path = realpath(argv[0], NULL);
1234 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
1235 26ed57b2 2018-05-19 stsp return got_error_from_errno();
1236 26ed57b2 2018-05-19 stsp obj_id_str1 = argv[1];
1237 26ed57b2 2018-05-19 stsp obj_id_str2 = argv[2];
1238 26ed57b2 2018-05-19 stsp } else
1239 26ed57b2 2018-05-19 stsp usage_diff();
1240 26ed57b2 2018-05-19 stsp
1241 26ed57b2 2018-05-19 stsp error = got_repo_open(&repo, repo_path);
1242 26ed57b2 2018-05-19 stsp free(repo_path);
1243 26ed57b2 2018-05-19 stsp if (error)
1244 26ed57b2 2018-05-19 stsp goto done;
1245 26ed57b2 2018-05-19 stsp
1246 26ed57b2 2018-05-19 stsp error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
1247 26ed57b2 2018-05-19 stsp if (error)
1248 26ed57b2 2018-05-19 stsp goto done;
1249 26ed57b2 2018-05-19 stsp
1250 26ed57b2 2018-05-19 stsp error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1251 26ed57b2 2018-05-19 stsp if (error)
1252 26ed57b2 2018-05-19 stsp goto done;
1253 26ed57b2 2018-05-19 stsp
1254 6d0fee91 2018-08-01 stsp view = open_view(0, 0, 0, 0, NULL);
1255 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
1256 ea5e7bb5 2018-08-01 stsp error = got_error_from_errno();
1257 ea5e7bb5 2018-08-01 stsp goto done;
1258 ea5e7bb5 2018-08-01 stsp }
1259 ea5e7bb5 2018-08-01 stsp error = show_diff_view(view, obj1, obj2, repo);
1260 ea5e7bb5 2018-08-01 stsp close_view(view);
1261 26ed57b2 2018-05-19 stsp done:
1262 26ed57b2 2018-05-19 stsp got_repo_close(repo);
1263 26ed57b2 2018-05-19 stsp if (obj1)
1264 26ed57b2 2018-05-19 stsp got_object_close(obj1);
1265 26ed57b2 2018-05-19 stsp if (obj2)
1266 26ed57b2 2018-05-19 stsp got_object_close(obj2);
1267 26ed57b2 2018-05-19 stsp return error;
1268 9f7d7167 2018-04-29 stsp }
1269 9f7d7167 2018-04-29 stsp
1270 4ed7e80c 2018-05-20 stsp __dead static void
1271 9f7d7167 2018-04-29 stsp usage_blame(void)
1272 9f7d7167 2018-04-29 stsp {
1273 80ddbec8 2018-04-29 stsp endwin();
1274 a70480e0 2018-06-23 stsp fprintf(stderr, "usage: %s blame [-c commit] [repository-path] path\n",
1275 9f7d7167 2018-04-29 stsp getprogname());
1276 9f7d7167 2018-04-29 stsp exit(1);
1277 9f7d7167 2018-04-29 stsp }
1278 84451b3e 2018-07-10 stsp
1279 84451b3e 2018-07-10 stsp struct tog_blame_line {
1280 84451b3e 2018-07-10 stsp int annotated;
1281 84451b3e 2018-07-10 stsp struct got_object_id *id;
1282 84451b3e 2018-07-10 stsp };
1283 9f7d7167 2018-04-29 stsp
1284 4ed7e80c 2018-05-20 stsp static const struct got_error *
1285 f7d12f7e 2018-08-01 stsp draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
1286 f7d12f7e 2018-08-01 stsp const char *path, struct tog_blame_line *lines, int nlines,
1287 f7d12f7e 2018-08-01 stsp int blame_complete, int selected_line, int *first_displayed_line,
1288 f7d12f7e 2018-08-01 stsp int *last_displayed_line, int *eof, int max_lines)
1289 84451b3e 2018-07-10 stsp {
1290 84451b3e 2018-07-10 stsp const struct got_error *err;
1291 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
1292 84451b3e 2018-07-10 stsp char *line;
1293 84451b3e 2018-07-10 stsp size_t len;
1294 84451b3e 2018-07-10 stsp wchar_t *wline;
1295 b700b5d6 2018-07-10 stsp int width, wlimit;
1296 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
1297 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
1298 ab089a2a 2018-07-12 stsp char *id_str;
1299 ab089a2a 2018-07-12 stsp
1300 ab089a2a 2018-07-12 stsp err = got_object_id_str(&id_str, id);
1301 ab089a2a 2018-07-12 stsp if (err)
1302 ab089a2a 2018-07-12 stsp return err;
1303 84451b3e 2018-07-10 stsp
1304 84451b3e 2018-07-10 stsp rewind(f);
1305 f7d12f7e 2018-08-01 stsp werase(view->window);
1306 84451b3e 2018-07-10 stsp
1307 ab089a2a 2018-07-12 stsp if (asprintf(&line, "commit: %s", id_str) == -1) {
1308 ab089a2a 2018-07-12 stsp err = got_error_from_errno();
1309 ab089a2a 2018-07-12 stsp free(id_str);
1310 ab089a2a 2018-07-12 stsp return err;
1311 ab089a2a 2018-07-12 stsp }
1312 ab089a2a 2018-07-12 stsp
1313 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
1314 ab089a2a 2018-07-12 stsp free(line);
1315 2550e4c3 2018-07-13 stsp line = NULL;
1316 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
1317 2550e4c3 2018-07-13 stsp free(wline);
1318 2550e4c3 2018-07-13 stsp wline = NULL;
1319 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
1320 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
1321 ab089a2a 2018-07-12 stsp
1322 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
1323 084063cd 2018-07-12 stsp *first_displayed_line - 1 + selected_line, nlines,
1324 12a0b23b 2018-07-12 stsp blame_complete ? "" : "annotating ", path) == -1) {
1325 ab089a2a 2018-07-12 stsp free(id_str);
1326 3f60a8ef 2018-07-10 stsp return got_error_from_errno();
1327 ab089a2a 2018-07-12 stsp }
1328 ab089a2a 2018-07-12 stsp free(id_str);
1329 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
1330 3f60a8ef 2018-07-10 stsp free(line);
1331 2550e4c3 2018-07-13 stsp line = NULL;
1332 3f60a8ef 2018-07-10 stsp if (err)
1333 3f60a8ef 2018-07-10 stsp return err;
1334 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
1335 2550e4c3 2018-07-13 stsp free(wline);
1336 2550e4c3 2018-07-13 stsp wline = NULL;
1337 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
1338 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
1339 3f60a8ef 2018-07-10 stsp
1340 84451b3e 2018-07-10 stsp *eof = 0;
1341 ab089a2a 2018-07-12 stsp while (nprinted < max_lines - 2) {
1342 84451b3e 2018-07-10 stsp line = parse_next_line(f, &len);
1343 84451b3e 2018-07-10 stsp if (line == NULL) {
1344 84451b3e 2018-07-10 stsp *eof = 1;
1345 84451b3e 2018-07-10 stsp break;
1346 84451b3e 2018-07-10 stsp }
1347 84451b3e 2018-07-10 stsp if (++lineno < *first_displayed_line) {
1348 84451b3e 2018-07-10 stsp free(line);
1349 84451b3e 2018-07-10 stsp continue;
1350 84451b3e 2018-07-10 stsp }
1351 84451b3e 2018-07-10 stsp
1352 f7d12f7e 2018-08-01 stsp wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
1353 b700b5d6 2018-07-10 stsp err = format_line(&wline, &width, line, wlimit);
1354 84451b3e 2018-07-10 stsp if (err) {
1355 84451b3e 2018-07-10 stsp free(line);
1356 84451b3e 2018-07-10 stsp return err;
1357 84451b3e 2018-07-10 stsp }
1358 84451b3e 2018-07-10 stsp
1359 b700b5d6 2018-07-10 stsp if (nprinted == selected_line - 1)
1360 f7d12f7e 2018-08-01 stsp wstandout(view->window);
1361 b700b5d6 2018-07-10 stsp
1362 84451b3e 2018-07-10 stsp blame_line = &lines[lineno - 1];
1363 ee41ec32 2018-07-10 stsp if (blame_line->annotated && prev_id &&
1364 ee41ec32 2018-07-10 stsp got_object_id_cmp(prev_id, blame_line->id) == 0)
1365 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ");
1366 ee41ec32 2018-07-10 stsp else if (blame_line->annotated) {
1367 84451b3e 2018-07-10 stsp char *id_str;
1368 84451b3e 2018-07-10 stsp err = got_object_id_str(&id_str, blame_line->id);
1369 84451b3e 2018-07-10 stsp if (err) {
1370 84451b3e 2018-07-10 stsp free(line);
1371 2550e4c3 2018-07-13 stsp free(wline);
1372 84451b3e 2018-07-10 stsp return err;
1373 84451b3e 2018-07-10 stsp }
1374 f7d12f7e 2018-08-01 stsp wprintw(view->window, "%.8s ", id_str);
1375 84451b3e 2018-07-10 stsp free(id_str);
1376 ee41ec32 2018-07-10 stsp prev_id = blame_line->id;
1377 ee41ec32 2018-07-10 stsp } else {
1378 f7d12f7e 2018-08-01 stsp waddstr(view->window, "........ ");
1379 ee41ec32 2018-07-10 stsp prev_id = NULL;
1380 ee41ec32 2018-07-10 stsp }
1381 84451b3e 2018-07-10 stsp
1382 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
1383 b700b5d6 2018-07-10 stsp while (width < wlimit) {
1384 f7d12f7e 2018-08-01 stsp waddch(view->window, ' ');
1385 b700b5d6 2018-07-10 stsp width++;
1386 b700b5d6 2018-07-10 stsp }
1387 b700b5d6 2018-07-10 stsp if (nprinted == selected_line - 1)
1388 f7d12f7e 2018-08-01 stsp wstandend(view->window);
1389 84451b3e 2018-07-10 stsp if (++nprinted == 1)
1390 84451b3e 2018-07-10 stsp *first_displayed_line = lineno;
1391 84451b3e 2018-07-10 stsp free(line);
1392 2550e4c3 2018-07-13 stsp free(wline);
1393 2550e4c3 2018-07-13 stsp wline = NULL;
1394 84451b3e 2018-07-10 stsp }
1395 84451b3e 2018-07-10 stsp *last_displayed_line = lineno;
1396 84451b3e 2018-07-10 stsp
1397 84451b3e 2018-07-10 stsp update_panels();
1398 84451b3e 2018-07-10 stsp doupdate();
1399 84451b3e 2018-07-10 stsp
1400 84451b3e 2018-07-10 stsp return NULL;
1401 84451b3e 2018-07-10 stsp }
1402 84451b3e 2018-07-10 stsp
1403 84451b3e 2018-07-10 stsp struct tog_blame_cb_args {
1404 84451b3e 2018-07-10 stsp pthread_mutex_t *mutex;
1405 84451b3e 2018-07-10 stsp struct tog_blame_line *lines; /* one per line */
1406 84451b3e 2018-07-10 stsp int nlines;
1407 84451b3e 2018-07-10 stsp
1408 7cc84d77 2018-08-01 stsp struct tog_view *view;
1409 ab089a2a 2018-07-12 stsp struct got_object_id *commit_id;
1410 84451b3e 2018-07-10 stsp FILE *f;
1411 3f60a8ef 2018-07-10 stsp const char *path;
1412 84451b3e 2018-07-10 stsp int *first_displayed_line;
1413 84451b3e 2018-07-10 stsp int *last_displayed_line;
1414 b700b5d6 2018-07-10 stsp int *selected_line;
1415 18430de3 2018-07-10 stsp int *quit;
1416 84451b3e 2018-07-10 stsp };
1417 84451b3e 2018-07-10 stsp
1418 84451b3e 2018-07-10 stsp static const struct got_error *
1419 84451b3e 2018-07-10 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
1420 84451b3e 2018-07-10 stsp {
1421 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
1422 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
1423 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
1424 84451b3e 2018-07-10 stsp int eof;
1425 84451b3e 2018-07-10 stsp
1426 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
1427 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
1428 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
1429 84451b3e 2018-07-10 stsp
1430 84451b3e 2018-07-10 stsp if (pthread_mutex_lock(a->mutex) != 0)
1431 84451b3e 2018-07-10 stsp return got_error_from_errno();
1432 84451b3e 2018-07-10 stsp
1433 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
1434 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
1435 d68a0a7d 2018-07-10 stsp goto done;
1436 d68a0a7d 2018-07-10 stsp }
1437 d68a0a7d 2018-07-10 stsp
1438 d68a0a7d 2018-07-10 stsp if (lineno == -1)
1439 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
1440 d68a0a7d 2018-07-10 stsp
1441 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
1442 d68a0a7d 2018-07-10 stsp if (line->annotated)
1443 d68a0a7d 2018-07-10 stsp goto done;
1444 d68a0a7d 2018-07-10 stsp
1445 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
1446 84451b3e 2018-07-10 stsp if (line->id == NULL) {
1447 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1448 84451b3e 2018-07-10 stsp goto done;
1449 84451b3e 2018-07-10 stsp }
1450 84451b3e 2018-07-10 stsp line->annotated = 1;
1451 84451b3e 2018-07-10 stsp
1452 f7d12f7e 2018-08-01 stsp err = draw_blame(a->view, a->commit_id, a->f, a->path,
1453 245d91c1 2018-07-12 stsp a->lines, a->nlines, 0, *a->selected_line, a->first_displayed_line,
1454 f7d12f7e 2018-08-01 stsp a->last_displayed_line, &eof, a->view->nlines);
1455 84451b3e 2018-07-10 stsp done:
1456 84451b3e 2018-07-10 stsp if (pthread_mutex_unlock(a->mutex) != 0)
1457 84451b3e 2018-07-10 stsp return got_error_from_errno();
1458 84451b3e 2018-07-10 stsp return err;
1459 84451b3e 2018-07-10 stsp }
1460 84451b3e 2018-07-10 stsp
1461 84451b3e 2018-07-10 stsp struct tog_blame_thread_args {
1462 84451b3e 2018-07-10 stsp const char *path;
1463 84451b3e 2018-07-10 stsp struct got_repository *repo;
1464 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *cb_args;
1465 18430de3 2018-07-10 stsp int *complete;
1466 84451b3e 2018-07-10 stsp };
1467 84451b3e 2018-07-10 stsp
1468 84451b3e 2018-07-10 stsp static void *
1469 84451b3e 2018-07-10 stsp blame_thread(void *arg)
1470 84451b3e 2018-07-10 stsp {
1471 18430de3 2018-07-10 stsp const struct got_error *err;
1472 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
1473 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
1474 18430de3 2018-07-10 stsp int eof;
1475 18430de3 2018-07-10 stsp
1476 ab089a2a 2018-07-12 stsp err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
1477 245d91c1 2018-07-12 stsp blame_cb, ta->cb_args);
1478 18430de3 2018-07-10 stsp
1479 18430de3 2018-07-10 stsp if (pthread_mutex_lock(a->mutex) != 0)
1480 18430de3 2018-07-10 stsp return (void *)got_error_from_errno();
1481 18430de3 2018-07-10 stsp
1482 c9beca56 2018-07-22 stsp got_repo_close(ta->repo);
1483 c9beca56 2018-07-22 stsp ta->repo = NULL;
1484 c9beca56 2018-07-22 stsp *ta->complete = 1;
1485 c9beca56 2018-07-22 stsp if (!err)
1486 f7d12f7e 2018-08-01 stsp err = draw_blame(a->view, a->commit_id, a->f, a->path,
1487 f7d12f7e 2018-08-01 stsp a->lines, a->nlines, 1, *a->selected_line,
1488 c9beca56 2018-07-22 stsp a->first_displayed_line, a->last_displayed_line, &eof,
1489 f7d12f7e 2018-08-01 stsp a->view->nlines);
1490 18430de3 2018-07-10 stsp
1491 18430de3 2018-07-10 stsp if (pthread_mutex_unlock(a->mutex) != 0 && err == NULL)
1492 18430de3 2018-07-10 stsp err = got_error_from_errno();
1493 18430de3 2018-07-10 stsp
1494 18430de3 2018-07-10 stsp return (void *)err;
1495 84451b3e 2018-07-10 stsp }
1496 84451b3e 2018-07-10 stsp
1497 245d91c1 2018-07-12 stsp static struct got_object_id *
1498 245d91c1 2018-07-12 stsp get_selected_commit_id(struct tog_blame_line *lines,
1499 245d91c1 2018-07-12 stsp int first_displayed_line, int selected_line)
1500 245d91c1 2018-07-12 stsp {
1501 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
1502 b880a918 2018-07-10 stsp
1503 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
1504 245d91c1 2018-07-12 stsp if (!line->annotated)
1505 245d91c1 2018-07-12 stsp return NULL;
1506 245d91c1 2018-07-12 stsp
1507 245d91c1 2018-07-12 stsp return line->id;
1508 245d91c1 2018-07-12 stsp }
1509 245d91c1 2018-07-12 stsp
1510 84451b3e 2018-07-10 stsp static const struct got_error *
1511 245d91c1 2018-07-12 stsp open_selected_commit(struct got_object **pobj, struct got_object **obj,
1512 b880a918 2018-07-10 stsp struct tog_blame_line *lines, int first_displayed_line,
1513 b880a918 2018-07-10 stsp int selected_line, struct got_repository *repo)
1514 b880a918 2018-07-10 stsp {
1515 b880a918 2018-07-10 stsp const struct got_error *err = NULL;
1516 b880a918 2018-07-10 stsp struct got_commit_object *commit = NULL;
1517 245d91c1 2018-07-12 stsp struct got_object_id *selected_id;
1518 b880a918 2018-07-10 stsp struct got_object_qid *pid;
1519 b880a918 2018-07-10 stsp
1520 b880a918 2018-07-10 stsp *pobj = NULL;
1521 b880a918 2018-07-10 stsp *obj = NULL;
1522 b880a918 2018-07-10 stsp
1523 245d91c1 2018-07-12 stsp selected_id = get_selected_commit_id(lines,
1524 245d91c1 2018-07-12 stsp first_displayed_line, selected_line);
1525 245d91c1 2018-07-12 stsp if (selected_id == NULL)
1526 b880a918 2018-07-10 stsp return NULL;
1527 b880a918 2018-07-10 stsp
1528 245d91c1 2018-07-12 stsp err = got_object_open(obj, repo, selected_id);
1529 b880a918 2018-07-10 stsp if (err)
1530 b880a918 2018-07-10 stsp goto done;
1531 b880a918 2018-07-10 stsp
1532 b880a918 2018-07-10 stsp err = got_object_commit_open(&commit, repo, *obj);
1533 b880a918 2018-07-10 stsp if (err)
1534 b880a918 2018-07-10 stsp goto done;
1535 b880a918 2018-07-10 stsp
1536 b880a918 2018-07-10 stsp pid = SIMPLEQ_FIRST(&commit->parent_ids);
1537 b880a918 2018-07-10 stsp if (pid) {
1538 b880a918 2018-07-10 stsp err = got_object_open(pobj, repo, pid->id);
1539 b880a918 2018-07-10 stsp if (err)
1540 b880a918 2018-07-10 stsp goto done;
1541 b880a918 2018-07-10 stsp }
1542 b880a918 2018-07-10 stsp done:
1543 b880a918 2018-07-10 stsp if (commit)
1544 b880a918 2018-07-10 stsp got_object_commit_close(commit);
1545 b880a918 2018-07-10 stsp return err;
1546 b880a918 2018-07-10 stsp }
1547 b880a918 2018-07-10 stsp
1548 245d91c1 2018-07-12 stsp struct tog_blame {
1549 245d91c1 2018-07-12 stsp FILE *f;
1550 245d91c1 2018-07-12 stsp size_t filesize;
1551 245d91c1 2018-07-12 stsp struct tog_blame_line *lines;
1552 245d91c1 2018-07-12 stsp size_t nlines;
1553 245d91c1 2018-07-12 stsp pthread_t thread;
1554 245d91c1 2018-07-12 stsp struct tog_blame_thread_args thread_args;
1555 245d91c1 2018-07-12 stsp struct tog_blame_cb_args cb_args;
1556 245d91c1 2018-07-12 stsp const char *path;
1557 245d91c1 2018-07-12 stsp };
1558 245d91c1 2018-07-12 stsp
1559 b880a918 2018-07-10 stsp static const struct got_error *
1560 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
1561 a70480e0 2018-06-23 stsp {
1562 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
1563 245d91c1 2018-07-12 stsp int i;
1564 245d91c1 2018-07-12 stsp
1565 245d91c1 2018-07-12 stsp if (blame->thread) {
1566 245d91c1 2018-07-12 stsp if (pthread_join(blame->thread, (void **)&err) != 0)
1567 245d91c1 2018-07-12 stsp err = got_error_from_errno();
1568 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
1569 245d91c1 2018-07-12 stsp err = NULL;
1570 245d91c1 2018-07-12 stsp blame->thread = NULL;
1571 245d91c1 2018-07-12 stsp }
1572 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
1573 245d91c1 2018-07-12 stsp got_repo_close(blame->thread_args.repo);
1574 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
1575 245d91c1 2018-07-12 stsp }
1576 245d91c1 2018-07-12 stsp if (blame->f) {
1577 245d91c1 2018-07-12 stsp fclose(blame->f);
1578 245d91c1 2018-07-12 stsp blame->f = NULL;
1579 245d91c1 2018-07-12 stsp }
1580 245d91c1 2018-07-12 stsp for (i = 0; i < blame->nlines; i++)
1581 245d91c1 2018-07-12 stsp free(blame->lines[i].id);
1582 245d91c1 2018-07-12 stsp free(blame->lines);
1583 245d91c1 2018-07-12 stsp blame->lines = NULL;
1584 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
1585 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
1586 245d91c1 2018-07-12 stsp
1587 245d91c1 2018-07-12 stsp return err;
1588 245d91c1 2018-07-12 stsp }
1589 245d91c1 2018-07-12 stsp
1590 245d91c1 2018-07-12 stsp static const struct got_error *
1591 7cc84d77 2018-08-01 stsp run_blame(struct tog_blame *blame, pthread_mutex_t *mutex,
1592 7cc84d77 2018-08-01 stsp struct tog_view *view, int *blame_complete,
1593 245d91c1 2018-07-12 stsp int *first_displayed_line, int *last_displayed_line,
1594 245d91c1 2018-07-12 stsp int *selected_line, int *done, const char *path,
1595 245d91c1 2018-07-12 stsp struct got_object_id *commit_id,
1596 245d91c1 2018-07-12 stsp struct got_repository *repo)
1597 245d91c1 2018-07-12 stsp {
1598 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
1599 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
1600 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
1601 245d91c1 2018-07-12 stsp struct got_object *obj;
1602 a70480e0 2018-06-23 stsp
1603 84451b3e 2018-07-10 stsp err = got_object_open_by_path(&obj, repo, commit_id, path);
1604 84451b3e 2018-07-10 stsp if (err)
1605 84451b3e 2018-07-10 stsp goto done;
1606 84451b3e 2018-07-10 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
1607 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1608 84451b3e 2018-07-10 stsp goto done;
1609 84451b3e 2018-07-10 stsp }
1610 a70480e0 2018-06-23 stsp
1611 84451b3e 2018-07-10 stsp err = got_object_blob_open(&blob, repo, obj, 8192);
1612 a70480e0 2018-06-23 stsp if (err)
1613 a70480e0 2018-06-23 stsp goto done;
1614 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
1615 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
1616 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1617 84451b3e 2018-07-10 stsp goto done;
1618 84451b3e 2018-07-10 stsp }
1619 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
1620 245d91c1 2018-07-12 stsp blame->f, blob);
1621 84451b3e 2018-07-10 stsp if (err)
1622 84451b3e 2018-07-10 stsp goto done;
1623 a70480e0 2018-06-23 stsp
1624 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
1625 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
1626 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1627 84451b3e 2018-07-10 stsp goto done;
1628 84451b3e 2018-07-10 stsp }
1629 a70480e0 2018-06-23 stsp
1630 245d91c1 2018-07-12 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1631 bd24772e 2018-07-11 stsp if (err)
1632 bd24772e 2018-07-11 stsp goto done;
1633 bd24772e 2018-07-11 stsp
1634 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
1635 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
1636 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
1637 245d91c1 2018-07-12 stsp blame->cb_args.mutex = mutex;
1638 245d91c1 2018-07-12 stsp blame->cb_args.commit_id = got_object_id_dup(commit_id);
1639 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
1640 245d91c1 2018-07-12 stsp err = got_error_from_errno();
1641 245d91c1 2018-07-12 stsp goto done;
1642 245d91c1 2018-07-12 stsp }
1643 245d91c1 2018-07-12 stsp blame->cb_args.f = blame->f;
1644 245d91c1 2018-07-12 stsp blame->cb_args.path = path;
1645 245d91c1 2018-07-12 stsp blame->cb_args.first_displayed_line = first_displayed_line;
1646 245d91c1 2018-07-12 stsp blame->cb_args.selected_line = selected_line;
1647 245d91c1 2018-07-12 stsp blame->cb_args.last_displayed_line = last_displayed_line;
1648 245d91c1 2018-07-12 stsp blame->cb_args.quit = done;
1649 245d91c1 2018-07-12 stsp
1650 245d91c1 2018-07-12 stsp blame->thread_args.path = path;
1651 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
1652 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
1653 245d91c1 2018-07-12 stsp blame->thread_args.complete = blame_complete;
1654 245d91c1 2018-07-12 stsp *blame_complete = 0;
1655 245d91c1 2018-07-12 stsp
1656 245d91c1 2018-07-12 stsp if (pthread_create(&blame->thread, NULL, blame_thread,
1657 245d91c1 2018-07-12 stsp &blame->thread_args) != 0) {
1658 245d91c1 2018-07-12 stsp err = got_error_from_errno();
1659 245d91c1 2018-07-12 stsp goto done;
1660 245d91c1 2018-07-12 stsp }
1661 245d91c1 2018-07-12 stsp
1662 245d91c1 2018-07-12 stsp done:
1663 245d91c1 2018-07-12 stsp if (blob)
1664 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
1665 245d91c1 2018-07-12 stsp if (obj)
1666 245d91c1 2018-07-12 stsp got_object_close(obj);
1667 245d91c1 2018-07-12 stsp if (err)
1668 245d91c1 2018-07-12 stsp stop_blame(blame);
1669 245d91c1 2018-07-12 stsp return err;
1670 245d91c1 2018-07-12 stsp }
1671 245d91c1 2018-07-12 stsp
1672 245d91c1 2018-07-12 stsp static const struct got_error *
1673 e1cd8fed 2018-08-01 stsp show_blame_view(struct tog_view *view, const char *path,
1674 e1cd8fed 2018-08-01 stsp struct got_object_id *commit_id, struct got_repository *repo)
1675 245d91c1 2018-07-12 stsp {
1676 245d91c1 2018-07-12 stsp const struct got_error *err = NULL, *thread_err = NULL;
1677 f7d12f7e 2018-08-01 stsp int ch, done = 0, first_displayed_line = 1, last_displayed_line;
1678 245d91c1 2018-07-12 stsp int selected_line = first_displayed_line;
1679 245d91c1 2018-07-12 stsp int eof, blame_complete = 0;
1680 245d91c1 2018-07-12 stsp struct got_object *obj = NULL, *pobj = NULL;
1681 245d91c1 2018-07-12 stsp pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
1682 245d91c1 2018-07-12 stsp struct tog_blame blame;
1683 dbc6a6b6 2018-07-12 stsp struct got_object_id_queue blamed_commits;
1684 dbc6a6b6 2018-07-12 stsp struct got_object_qid *blamed_commit = NULL;
1685 e1cd8fed 2018-08-01 stsp struct tog_view *diff_view;
1686 dbc6a6b6 2018-07-12 stsp
1687 dbc6a6b6 2018-07-12 stsp SIMPLEQ_INIT(&blamed_commits);
1688 245d91c1 2018-07-12 stsp
1689 245d91c1 2018-07-12 stsp if (pthread_mutex_init(&mutex, NULL) != 0) {
1690 245d91c1 2018-07-12 stsp err = got_error_from_errno();
1691 245d91c1 2018-07-12 stsp goto done;
1692 245d91c1 2018-07-12 stsp }
1693 245d91c1 2018-07-12 stsp
1694 dbc6a6b6 2018-07-12 stsp err = got_object_qid_alloc(&blamed_commit, commit_id);
1695 dbc6a6b6 2018-07-12 stsp if (err)
1696 245d91c1 2018-07-12 stsp goto done;
1697 dbc6a6b6 2018-07-12 stsp SIMPLEQ_INSERT_HEAD(&blamed_commits, blamed_commit, entry);
1698 245d91c1 2018-07-12 stsp
1699 7cc84d77 2018-08-01 stsp show_panel(view->panel);
1700 f7d12f7e 2018-08-01 stsp last_displayed_line = view->nlines;
1701 a70480e0 2018-06-23 stsp
1702 245d91c1 2018-07-12 stsp memset(&blame, 0, sizeof(blame));
1703 7cc84d77 2018-08-01 stsp err = run_blame(&blame, &mutex, view, &blame_complete,
1704 245d91c1 2018-07-12 stsp &first_displayed_line, &last_displayed_line,
1705 dbc6a6b6 2018-07-12 stsp &selected_line, &done, path, blamed_commit->id, repo);
1706 245d91c1 2018-07-12 stsp if (err)
1707 245d91c1 2018-07-12 stsp return err;
1708 84451b3e 2018-07-10 stsp
1709 a70480e0 2018-06-23 stsp while (!done) {
1710 84451b3e 2018-07-10 stsp if (pthread_mutex_lock(&mutex) != 0) {
1711 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1712 84451b3e 2018-07-10 stsp goto done;
1713 84451b3e 2018-07-10 stsp }
1714 f7d12f7e 2018-08-01 stsp err = draw_blame(view, blamed_commit->id, blame.f, path,
1715 f7d12f7e 2018-08-01 stsp blame.lines, blame.nlines, blame_complete, selected_line,
1716 f7d12f7e 2018-08-01 stsp &first_displayed_line, &last_displayed_line, &eof,
1717 f7d12f7e 2018-08-01 stsp view->nlines);
1718 84451b3e 2018-07-10 stsp if (pthread_mutex_unlock(&mutex) != 0) {
1719 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1720 84451b3e 2018-07-10 stsp goto done;
1721 84451b3e 2018-07-10 stsp }
1722 a70480e0 2018-06-23 stsp if (err)
1723 a70480e0 2018-06-23 stsp break;
1724 a70480e0 2018-06-23 stsp nodelay(stdscr, FALSE);
1725 7cc84d77 2018-08-01 stsp ch = wgetch(view->window);
1726 a70480e0 2018-06-23 stsp nodelay(stdscr, TRUE);
1727 84451b3e 2018-07-10 stsp if (pthread_mutex_lock(&mutex) != 0) {
1728 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1729 84451b3e 2018-07-10 stsp goto done;
1730 84451b3e 2018-07-10 stsp }
1731 a70480e0 2018-06-23 stsp switch (ch) {
1732 a70480e0 2018-06-23 stsp case 'q':
1733 a70480e0 2018-06-23 stsp done = 1;
1734 a70480e0 2018-06-23 stsp break;
1735 a70480e0 2018-06-23 stsp case 'k':
1736 a70480e0 2018-06-23 stsp case KEY_UP:
1737 b700b5d6 2018-07-10 stsp if (selected_line > 1)
1738 b700b5d6 2018-07-10 stsp selected_line--;
1739 b700b5d6 2018-07-10 stsp else if (selected_line == 1 &&
1740 b700b5d6 2018-07-10 stsp first_displayed_line > 1)
1741 a70480e0 2018-06-23 stsp first_displayed_line--;
1742 a70480e0 2018-06-23 stsp break;
1743 a70480e0 2018-06-23 stsp case KEY_PPAGE:
1744 38f94530 2018-07-12 stsp case KEY_BACKSPACE:
1745 b700b5d6 2018-07-10 stsp if (first_displayed_line == 1) {
1746 b700b5d6 2018-07-10 stsp selected_line = 1;
1747 b700b5d6 2018-07-10 stsp break;
1748 b700b5d6 2018-07-10 stsp }
1749 f7d12f7e 2018-08-01 stsp if (first_displayed_line > view->nlines - 2)
1750 f7d12f7e 2018-08-01 stsp first_displayed_line -=
1751 f7d12f7e 2018-08-01 stsp (view->nlines - 2);
1752 b700b5d6 2018-07-10 stsp else
1753 b700b5d6 2018-07-10 stsp first_displayed_line = 1;
1754 a70480e0 2018-06-23 stsp break;
1755 a70480e0 2018-06-23 stsp case 'j':
1756 a70480e0 2018-06-23 stsp case KEY_DOWN:
1757 f7d12f7e 2018-08-01 stsp if (selected_line < view->nlines - 2 &&
1758 a026b947 2018-07-12 stsp first_displayed_line + selected_line <=
1759 a026b947 2018-07-12 stsp blame.nlines)
1760 b700b5d6 2018-07-10 stsp selected_line++;
1761 245d91c1 2018-07-12 stsp else if (last_displayed_line < blame.nlines)
1762 b700b5d6 2018-07-10 stsp first_displayed_line++;
1763 b700b5d6 2018-07-10 stsp break;
1764 7a2921f9 2018-07-12 stsp case 'b':
1765 7a2921f9 2018-07-12 stsp case 'p': {
1766 245d91c1 2018-07-12 stsp struct got_object_id *id;
1767 245d91c1 2018-07-12 stsp id = get_selected_commit_id(blame.lines,
1768 245d91c1 2018-07-12 stsp first_displayed_line, selected_line);
1769 245d91c1 2018-07-12 stsp if (id == NULL || got_object_id_cmp(id,
1770 dbc6a6b6 2018-07-12 stsp blamed_commit->id) == 0)
1771 245d91c1 2018-07-12 stsp break;
1772 245d91c1 2018-07-12 stsp err = open_selected_commit(&pobj, &obj,
1773 245d91c1 2018-07-12 stsp blame.lines, first_displayed_line,
1774 245d91c1 2018-07-12 stsp selected_line, repo);
1775 ad6a6c43 2018-07-10 stsp if (err)
1776 245d91c1 2018-07-12 stsp break;
1777 245d91c1 2018-07-12 stsp if (pobj == NULL && obj == NULL)
1778 245d91c1 2018-07-12 stsp break;
1779 7a2921f9 2018-07-12 stsp if (ch == 'p' && pobj == NULL)
1780 7a2921f9 2018-07-12 stsp break;
1781 245d91c1 2018-07-12 stsp done = 1;
1782 245d91c1 2018-07-12 stsp if (pthread_mutex_unlock(&mutex) != 0) {
1783 245d91c1 2018-07-12 stsp err = got_error_from_errno();
1784 ad6a6c43 2018-07-10 stsp goto done;
1785 245d91c1 2018-07-12 stsp }
1786 245d91c1 2018-07-12 stsp thread_err = stop_blame(&blame);
1787 245d91c1 2018-07-12 stsp done = 0;
1788 245d91c1 2018-07-12 stsp if (pthread_mutex_lock(&mutex) != 0) {
1789 245d91c1 2018-07-12 stsp err = got_error_from_errno();
1790 245d91c1 2018-07-12 stsp goto done;
1791 245d91c1 2018-07-12 stsp }
1792 245d91c1 2018-07-12 stsp if (thread_err)
1793 245d91c1 2018-07-12 stsp break;
1794 7a2921f9 2018-07-12 stsp id = got_object_get_id(ch == 'b' ? obj : pobj);
1795 1960a6f9 2018-07-12 stsp got_object_close(obj);
1796 1960a6f9 2018-07-12 stsp obj = NULL;
1797 1960a6f9 2018-07-12 stsp if (pobj) {
1798 1960a6f9 2018-07-12 stsp got_object_close(pobj);
1799 14437fb1 2018-07-13 stsp pobj = NULL;
1800 1960a6f9 2018-07-12 stsp }
1801 dbc6a6b6 2018-07-12 stsp if (id == NULL) {
1802 245d91c1 2018-07-12 stsp err = got_error_from_errno();
1803 245d91c1 2018-07-12 stsp break;
1804 245d91c1 2018-07-12 stsp }
1805 dbc6a6b6 2018-07-12 stsp err = got_object_qid_alloc(&blamed_commit, id);
1806 dbc6a6b6 2018-07-12 stsp free(id);
1807 dbc6a6b6 2018-07-12 stsp if (err)
1808 dbc6a6b6 2018-07-12 stsp goto done;
1809 dbc6a6b6 2018-07-12 stsp SIMPLEQ_INSERT_HEAD(&blamed_commits,
1810 dbc6a6b6 2018-07-12 stsp blamed_commit, entry);
1811 7cc84d77 2018-08-01 stsp err = run_blame(&blame, &mutex, view,
1812 245d91c1 2018-07-12 stsp &blame_complete, &first_displayed_line,
1813 245d91c1 2018-07-12 stsp &last_displayed_line, &selected_line,
1814 dbc6a6b6 2018-07-12 stsp &done, path, blamed_commit->id, repo);
1815 dbc6a6b6 2018-07-12 stsp if (err)
1816 dbc6a6b6 2018-07-12 stsp break;
1817 dbc6a6b6 2018-07-12 stsp break;
1818 dbc6a6b6 2018-07-12 stsp }
1819 dbc6a6b6 2018-07-12 stsp case 'B': {
1820 dbc6a6b6 2018-07-12 stsp struct got_object_qid *first;
1821 dbc6a6b6 2018-07-12 stsp first = SIMPLEQ_FIRST(&blamed_commits);
1822 dbc6a6b6 2018-07-12 stsp if (!got_object_id_cmp(first->id, commit_id))
1823 dbc6a6b6 2018-07-12 stsp break;
1824 dbc6a6b6 2018-07-12 stsp done = 1;
1825 dbc6a6b6 2018-07-12 stsp if (pthread_mutex_unlock(&mutex) != 0) {
1826 dbc6a6b6 2018-07-12 stsp err = got_error_from_errno();
1827 dbc6a6b6 2018-07-12 stsp goto done;
1828 dbc6a6b6 2018-07-12 stsp }
1829 dbc6a6b6 2018-07-12 stsp thread_err = stop_blame(&blame);
1830 dbc6a6b6 2018-07-12 stsp done = 0;
1831 dbc6a6b6 2018-07-12 stsp if (pthread_mutex_lock(&mutex) != 0) {
1832 dbc6a6b6 2018-07-12 stsp err = got_error_from_errno();
1833 dbc6a6b6 2018-07-12 stsp goto done;
1834 dbc6a6b6 2018-07-12 stsp }
1835 dbc6a6b6 2018-07-12 stsp if (thread_err)
1836 dbc6a6b6 2018-07-12 stsp break;
1837 dbc6a6b6 2018-07-12 stsp SIMPLEQ_REMOVE_HEAD(&blamed_commits, entry);
1838 dbc6a6b6 2018-07-12 stsp got_object_qid_free(blamed_commit);
1839 dbc6a6b6 2018-07-12 stsp blamed_commit = SIMPLEQ_FIRST(&blamed_commits);
1840 7cc84d77 2018-08-01 stsp err = run_blame(&blame, &mutex, view,
1841 dbc6a6b6 2018-07-12 stsp &blame_complete, &first_displayed_line,
1842 dbc6a6b6 2018-07-12 stsp &last_displayed_line, &selected_line,
1843 dbc6a6b6 2018-07-12 stsp &done, path, blamed_commit->id, repo);
1844 245d91c1 2018-07-12 stsp if (err)
1845 245d91c1 2018-07-12 stsp break;
1846 245d91c1 2018-07-12 stsp break;
1847 245d91c1 2018-07-12 stsp }
1848 245d91c1 2018-07-12 stsp case KEY_ENTER:
1849 245d91c1 2018-07-12 stsp case '\r':
1850 245d91c1 2018-07-12 stsp err = open_selected_commit(&pobj, &obj,
1851 245d91c1 2018-07-12 stsp blame.lines, first_displayed_line,
1852 245d91c1 2018-07-12 stsp selected_line, repo);
1853 245d91c1 2018-07-12 stsp if (err)
1854 245d91c1 2018-07-12 stsp break;
1855 b880a918 2018-07-10 stsp if (pobj == NULL && obj == NULL)
1856 b880a918 2018-07-10 stsp break;
1857 6d0fee91 2018-08-01 stsp diff_view = open_view(0, 0, 0, 0, view);
1858 ea5e7bb5 2018-08-01 stsp if (diff_view == NULL) {
1859 ea5e7bb5 2018-08-01 stsp err = got_error_from_errno();
1860 ea5e7bb5 2018-08-01 stsp break;
1861 ea5e7bb5 2018-08-01 stsp }
1862 ea5e7bb5 2018-08-01 stsp err = show_diff_view(diff_view, pobj, obj, repo);
1863 ea5e7bb5 2018-08-01 stsp close_view(diff_view);
1864 b880a918 2018-07-10 stsp if (pobj) {
1865 b880a918 2018-07-10 stsp got_object_close(pobj);
1866 b880a918 2018-07-10 stsp pobj = NULL;
1867 fb311a66 2018-07-10 stsp }
1868 b880a918 2018-07-10 stsp got_object_close(obj);
1869 b880a918 2018-07-10 stsp obj = NULL;
1870 7cc84d77 2018-08-01 stsp show_panel(view->panel);
1871 ad6a6c43 2018-07-10 stsp if (err)
1872 245d91c1 2018-07-12 stsp break;
1873 a70480e0 2018-06-23 stsp break;
1874 a70480e0 2018-06-23 stsp case KEY_NPAGE:
1875 a70480e0 2018-06-23 stsp case ' ':
1876 245d91c1 2018-07-12 stsp if (last_displayed_line >= blame.nlines &&
1877 f7d12f7e 2018-08-01 stsp selected_line < view->nlines - 2) {
1878 d2dfcfbf 2018-07-12 stsp selected_line = MIN(blame.nlines,
1879 f7d12f7e 2018-08-01 stsp view->nlines - 2);
1880 b700b5d6 2018-07-10 stsp break;
1881 a70480e0 2018-06-23 stsp }
1882 f7d12f7e 2018-08-01 stsp if (last_displayed_line + view->nlines - 2 <=
1883 245d91c1 2018-07-12 stsp blame.nlines)
1884 f7d12f7e 2018-08-01 stsp first_displayed_line +=
1885 f7d12f7e 2018-08-01 stsp view->nlines - 2;
1886 b700b5d6 2018-07-10 stsp else
1887 b700b5d6 2018-07-10 stsp first_displayed_line =
1888 f7d12f7e 2018-08-01 stsp blame.nlines - (view->nlines - 3);
1889 a70480e0 2018-06-23 stsp break;
1890 7e5c1fe1 2018-08-01 stsp case KEY_RESIZE:
1891 a41d2007 2018-08-01 stsp err = view_resize(view);
1892 a41d2007 2018-08-01 stsp if (err)
1893 a41d2007 2018-08-01 stsp break;
1894 7e5c1fe1 2018-08-01 stsp if (selected_line > view->nlines - 2) {
1895 7e5c1fe1 2018-08-01 stsp selected_line = MIN(blame.nlines,
1896 7e5c1fe1 2018-08-01 stsp view->nlines - 2);
1897 7e5c1fe1 2018-08-01 stsp }
1898 7e5c1fe1 2018-08-01 stsp break;
1899 a70480e0 2018-06-23 stsp default:
1900 a70480e0 2018-06-23 stsp break;
1901 84451b3e 2018-07-10 stsp }
1902 245d91c1 2018-07-12 stsp if (pthread_mutex_unlock(&mutex) != 0)
1903 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1904 245d91c1 2018-07-12 stsp if (err || thread_err)
1905 245d91c1 2018-07-12 stsp break;
1906 a70480e0 2018-06-23 stsp }
1907 a70480e0 2018-06-23 stsp done:
1908 b880a918 2018-07-10 stsp if (pobj)
1909 b880a918 2018-07-10 stsp got_object_close(pobj);
1910 c9beca56 2018-07-22 stsp if (blame.thread)
1911 245d91c1 2018-07-12 stsp thread_err = stop_blame(&blame);
1912 dbc6a6b6 2018-07-12 stsp while (!SIMPLEQ_EMPTY(&blamed_commits)) {
1913 dbc6a6b6 2018-07-12 stsp blamed_commit = SIMPLEQ_FIRST(&blamed_commits);
1914 dbc6a6b6 2018-07-12 stsp SIMPLEQ_REMOVE_HEAD(&blamed_commits, entry);
1915 dbc6a6b6 2018-07-12 stsp got_object_qid_free(blamed_commit);
1916 dbc6a6b6 2018-07-12 stsp }
1917 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
1918 a70480e0 2018-06-23 stsp }
1919 a70480e0 2018-06-23 stsp
1920 a70480e0 2018-06-23 stsp static const struct got_error *
1921 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
1922 9f7d7167 2018-04-29 stsp {
1923 a70480e0 2018-06-23 stsp const struct got_error *error;
1924 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
1925 a70480e0 2018-06-23 stsp char *repo_path = NULL;
1926 a70480e0 2018-06-23 stsp char *path = NULL;
1927 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
1928 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
1929 a70480e0 2018-06-23 stsp int ch;
1930 e1cd8fed 2018-08-01 stsp struct tog_view *view;
1931 a70480e0 2018-06-23 stsp
1932 a70480e0 2018-06-23 stsp #ifndef PROFILE
1933 a70480e0 2018-06-23 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1934 a70480e0 2018-06-23 stsp err(1, "pledge");
1935 a70480e0 2018-06-23 stsp #endif
1936 a70480e0 2018-06-23 stsp
1937 a70480e0 2018-06-23 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
1938 a70480e0 2018-06-23 stsp switch (ch) {
1939 a70480e0 2018-06-23 stsp case 'c':
1940 a70480e0 2018-06-23 stsp commit_id_str = optarg;
1941 a70480e0 2018-06-23 stsp break;
1942 a70480e0 2018-06-23 stsp default:
1943 a70480e0 2018-06-23 stsp usage();
1944 a70480e0 2018-06-23 stsp /* NOTREACHED */
1945 a70480e0 2018-06-23 stsp }
1946 a70480e0 2018-06-23 stsp }
1947 a70480e0 2018-06-23 stsp
1948 a70480e0 2018-06-23 stsp argc -= optind;
1949 a70480e0 2018-06-23 stsp argv += optind;
1950 a70480e0 2018-06-23 stsp
1951 a70480e0 2018-06-23 stsp if (argc == 0) {
1952 a70480e0 2018-06-23 stsp usage_blame();
1953 a70480e0 2018-06-23 stsp } else if (argc == 1) {
1954 a70480e0 2018-06-23 stsp repo_path = getcwd(NULL, 0);
1955 a70480e0 2018-06-23 stsp if (repo_path == NULL)
1956 a70480e0 2018-06-23 stsp return got_error_from_errno();
1957 a70480e0 2018-06-23 stsp path = argv[0];
1958 a70480e0 2018-06-23 stsp } else if (argc == 2) {
1959 a70480e0 2018-06-23 stsp repo_path = realpath(argv[0], NULL);
1960 a70480e0 2018-06-23 stsp if (repo_path == NULL)
1961 a70480e0 2018-06-23 stsp return got_error_from_errno();
1962 a70480e0 2018-06-23 stsp path = argv[1];
1963 a70480e0 2018-06-23 stsp } else
1964 a70480e0 2018-06-23 stsp usage_blame();
1965 a70480e0 2018-06-23 stsp
1966 a70480e0 2018-06-23 stsp error = got_repo_open(&repo, repo_path);
1967 a70480e0 2018-06-23 stsp free(repo_path);
1968 a70480e0 2018-06-23 stsp if (error != NULL)
1969 66b4983c 2018-06-23 stsp return error;
1970 a70480e0 2018-06-23 stsp
1971 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
1972 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
1973 a70480e0 2018-06-23 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1974 a70480e0 2018-06-23 stsp if (error != NULL)
1975 66b4983c 2018-06-23 stsp goto done;
1976 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
1977 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
1978 a70480e0 2018-06-23 stsp } else {
1979 a70480e0 2018-06-23 stsp struct got_object *obj;
1980 a70480e0 2018-06-23 stsp error = got_object_open_by_id_str(&obj, repo, commit_id_str);
1981 a70480e0 2018-06-23 stsp if (error != NULL)
1982 66b4983c 2018-06-23 stsp goto done;
1983 a70480e0 2018-06-23 stsp commit_id = got_object_get_id(obj);
1984 a19e88aa 2018-06-23 stsp if (commit_id == NULL)
1985 66b4983c 2018-06-23 stsp error = got_error_from_errno();
1986 a19e88aa 2018-06-23 stsp got_object_close(obj);
1987 a70480e0 2018-06-23 stsp }
1988 a19e88aa 2018-06-23 stsp if (error != NULL)
1989 a19e88aa 2018-06-23 stsp goto done;
1990 a70480e0 2018-06-23 stsp
1991 6d0fee91 2018-08-01 stsp view = open_view(0, 0, 0, 0, NULL);
1992 e1cd8fed 2018-08-01 stsp if (view == NULL) {
1993 e1cd8fed 2018-08-01 stsp error = got_error_from_errno();
1994 e1cd8fed 2018-08-01 stsp goto done;
1995 e1cd8fed 2018-08-01 stsp }
1996 e1cd8fed 2018-08-01 stsp error = show_blame_view(view, path, commit_id, repo);
1997 e1cd8fed 2018-08-01 stsp close_view(view);
1998 a70480e0 2018-06-23 stsp done:
1999 a70480e0 2018-06-23 stsp free(commit_id);
2000 a70480e0 2018-06-23 stsp if (repo)
2001 a70480e0 2018-06-23 stsp got_repo_close(repo);
2002 a70480e0 2018-06-23 stsp return error;
2003 ffd1d5e5 2018-06-23 stsp }
2004 ffd1d5e5 2018-06-23 stsp
2005 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2006 f7d12f7e 2018-08-01 stsp draw_tree_entries(struct tog_view *view,
2007 f7d12f7e 2018-08-01 stsp struct got_tree_entry **first_displayed_entry,
2008 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **last_displayed_entry,
2009 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **selected_entry, int *ndisplayed,
2010 f7d12f7e 2018-08-01 stsp const char *label, int show_ids, const char *parent_path,
2011 ce52c690 2018-06-23 stsp const struct got_tree_entries *entries, int selected, int limit, int isroot)
2012 ffd1d5e5 2018-06-23 stsp {
2013 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
2014 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
2015 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
2016 ffd1d5e5 2018-06-23 stsp int width, n;
2017 ffd1d5e5 2018-06-23 stsp
2018 ffd1d5e5 2018-06-23 stsp *ndisplayed = 0;
2019 ffd1d5e5 2018-06-23 stsp
2020 f7d12f7e 2018-08-01 stsp werase(view->window);
2021 ffd1d5e5 2018-06-23 stsp
2022 ffd1d5e5 2018-06-23 stsp if (limit == 0)
2023 ffd1d5e5 2018-06-23 stsp return NULL;
2024 ffd1d5e5 2018-06-23 stsp
2025 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, label, view->ncols);
2026 ffd1d5e5 2018-06-23 stsp if (err)
2027 ffd1d5e5 2018-06-23 stsp return err;
2028 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2029 2550e4c3 2018-07-13 stsp free(wline);
2030 2550e4c3 2018-07-13 stsp wline = NULL;
2031 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2032 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2033 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2034 ffd1d5e5 2018-06-23 stsp return NULL;
2035 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, parent_path, view->ncols);
2036 ce52c690 2018-06-23 stsp if (err)
2037 ce52c690 2018-06-23 stsp return err;
2038 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2039 2550e4c3 2018-07-13 stsp free(wline);
2040 2550e4c3 2018-07-13 stsp wline = NULL;
2041 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2042 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2043 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2044 ffd1d5e5 2018-06-23 stsp return NULL;
2045 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2046 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
2047 a1eca9bb 2018-06-23 stsp return NULL;
2048 ffd1d5e5 2018-06-23 stsp
2049 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
2050 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL) {
2051 ffd1d5e5 2018-06-23 stsp if (selected == 0) {
2052 f7d12f7e 2018-08-01 stsp wstandout(view->window);
2053 ffd1d5e5 2018-06-23 stsp *selected_entry = NULL;
2054 ffd1d5e5 2018-06-23 stsp }
2055 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
2056 ffd1d5e5 2018-06-23 stsp if (selected == 0)
2057 f7d12f7e 2018-08-01 stsp wstandend(view->window);
2058 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
2059 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2060 ffd1d5e5 2018-06-23 stsp return NULL;
2061 ffd1d5e5 2018-06-23 stsp n = 1;
2062 ffd1d5e5 2018-06-23 stsp } else {
2063 ffd1d5e5 2018-06-23 stsp n = 0;
2064 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry)
2065 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
2066 ffd1d5e5 2018-06-23 stsp }
2067 ffd1d5e5 2018-06-23 stsp
2068 ffd1d5e5 2018-06-23 stsp while (te) {
2069 1d13200f 2018-07-12 stsp char *line = NULL, *id_str = NULL;
2070 1d13200f 2018-07-12 stsp
2071 1d13200f 2018-07-12 stsp if (show_ids) {
2072 1d13200f 2018-07-12 stsp err = got_object_id_str(&id_str, te->id);
2073 1d13200f 2018-07-12 stsp if (err)
2074 1d13200f 2018-07-12 stsp return got_error_from_errno();
2075 1d13200f 2018-07-12 stsp }
2076 1d13200f 2018-07-12 stsp if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
2077 1d13200f 2018-07-12 stsp te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
2078 1d13200f 2018-07-12 stsp free(id_str);
2079 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
2080 1d13200f 2018-07-12 stsp }
2081 1d13200f 2018-07-12 stsp free(id_str);
2082 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
2083 ffd1d5e5 2018-06-23 stsp if (err) {
2084 ffd1d5e5 2018-06-23 stsp free(line);
2085 ffd1d5e5 2018-06-23 stsp break;
2086 ffd1d5e5 2018-06-23 stsp }
2087 ffd1d5e5 2018-06-23 stsp if (n == selected) {
2088 f7d12f7e 2018-08-01 stsp wstandout(view->window);
2089 ffd1d5e5 2018-06-23 stsp *selected_entry = te;
2090 ffd1d5e5 2018-06-23 stsp }
2091 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2092 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2093 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2094 ffd1d5e5 2018-06-23 stsp if (n == selected)
2095 f7d12f7e 2018-08-01 stsp wstandend(view->window);
2096 ffd1d5e5 2018-06-23 stsp free(line);
2097 2550e4c3 2018-07-13 stsp free(wline);
2098 2550e4c3 2018-07-13 stsp wline = NULL;
2099 ffd1d5e5 2018-06-23 stsp n++;
2100 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
2101 ffd1d5e5 2018-06-23 stsp *last_displayed_entry = te;
2102 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2103 ffd1d5e5 2018-06-23 stsp break;
2104 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
2105 ffd1d5e5 2018-06-23 stsp }
2106 ffd1d5e5 2018-06-23 stsp
2107 ffd1d5e5 2018-06-23 stsp return err;
2108 ffd1d5e5 2018-06-23 stsp }
2109 ffd1d5e5 2018-06-23 stsp
2110 ffd1d5e5 2018-06-23 stsp static void
2111 ffd1d5e5 2018-06-23 stsp tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
2112 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries, int isroot)
2113 ffd1d5e5 2018-06-23 stsp {
2114 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te, *prev;
2115 ffd1d5e5 2018-06-23 stsp int i;
2116 ffd1d5e5 2018-06-23 stsp
2117 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL)
2118 ffd1d5e5 2018-06-23 stsp return;
2119 ffd1d5e5 2018-06-23 stsp
2120 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
2121 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == te) {
2122 ffd1d5e5 2018-06-23 stsp if (!isroot)
2123 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
2124 ffd1d5e5 2018-06-23 stsp return;
2125 ffd1d5e5 2018-06-23 stsp }
2126 ffd1d5e5 2018-06-23 stsp
2127 ffd1d5e5 2018-06-23 stsp /* XXX this is stupid... switch to TAILQ? */
2128 ffd1d5e5 2018-06-23 stsp for (i = 0; i < maxscroll; i++) {
2129 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry) {
2130 ffd1d5e5 2018-06-23 stsp prev = te;
2131 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
2132 ffd1d5e5 2018-06-23 stsp }
2133 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = prev;
2134 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
2135 ffd1d5e5 2018-06-23 stsp }
2136 ffd1d5e5 2018-06-23 stsp if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
2137 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
2138 ffd1d5e5 2018-06-23 stsp }
2139 ffd1d5e5 2018-06-23 stsp
2140 ffd1d5e5 2018-06-23 stsp static void
2141 ffd1d5e5 2018-06-23 stsp tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
2142 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *last_displayed_entry,
2143 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries)
2144 ffd1d5e5 2018-06-23 stsp {
2145 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *next;
2146 ffd1d5e5 2018-06-23 stsp int n = 0;
2147 ffd1d5e5 2018-06-23 stsp
2148 ffd1d5e5 2018-06-23 stsp if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
2149 ffd1d5e5 2018-06-23 stsp return;
2150 ffd1d5e5 2018-06-23 stsp
2151 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry)
2152 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
2153 ffd1d5e5 2018-06-23 stsp else
2154 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_FIRST(&entries->head);
2155 ffd1d5e5 2018-06-23 stsp while (next) {
2156 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = next;
2157 ffd1d5e5 2018-06-23 stsp if (++n >= maxscroll)
2158 ffd1d5e5 2018-06-23 stsp break;
2159 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(next, entry);
2160 ffd1d5e5 2018-06-23 stsp }
2161 ffd1d5e5 2018-06-23 stsp }
2162 ffd1d5e5 2018-06-23 stsp
2163 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree {
2164 d9765a41 2018-06-23 stsp TAILQ_ENTRY(tog_parent_tree) entry;
2165 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree;
2166 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *first_displayed_entry;
2167 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *selected_entry;
2168 ffd1d5e5 2018-06-23 stsp int selected;
2169 ffd1d5e5 2018-06-23 stsp };
2170 ffd1d5e5 2018-06-23 stsp
2171 d9765a41 2018-06-23 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
2172 ffd1d5e5 2018-06-23 stsp
2173 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2174 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
2175 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
2176 ffd1d5e5 2018-06-23 stsp {
2177 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
2178 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
2179 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
2180 ffd1d5e5 2018-06-23 stsp
2181 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
2182 ffd1d5e5 2018-06-23 stsp len += strlen(pt->selected_entry->name) + 1 /* slash */;
2183 ce52c690 2018-06-23 stsp if (te)
2184 ce52c690 2018-06-23 stsp len += strlen(te->name);
2185 ce52c690 2018-06-23 stsp
2186 ce52c690 2018-06-23 stsp *path = calloc(1, len);
2187 ffd1d5e5 2018-06-23 stsp if (path == NULL)
2188 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
2189 ffd1d5e5 2018-06-23 stsp
2190 ce52c690 2018-06-23 stsp (*path)[0] = '/';
2191 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
2192 d9765a41 2018-06-23 stsp while (pt) {
2193 ce52c690 2018-06-23 stsp if (strlcat(*path, pt->selected_entry->name, len) >= len) {
2194 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
2195 cb2ebc8a 2018-06-23 stsp goto done;
2196 cb2ebc8a 2018-06-23 stsp }
2197 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
2198 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
2199 cb2ebc8a 2018-06-23 stsp goto done;
2200 cb2ebc8a 2018-06-23 stsp }
2201 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
2202 ffd1d5e5 2018-06-23 stsp }
2203 ce52c690 2018-06-23 stsp if (te) {
2204 ce52c690 2018-06-23 stsp if (strlcat(*path, te->name, len) >= len) {
2205 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
2206 ce52c690 2018-06-23 stsp goto done;
2207 ce52c690 2018-06-23 stsp }
2208 cb2ebc8a 2018-06-23 stsp }
2209 ce52c690 2018-06-23 stsp done:
2210 ce52c690 2018-06-23 stsp if (err) {
2211 ce52c690 2018-06-23 stsp free(*path);
2212 ce52c690 2018-06-23 stsp *path = NULL;
2213 ce52c690 2018-06-23 stsp }
2214 ce52c690 2018-06-23 stsp return err;
2215 ce52c690 2018-06-23 stsp }
2216 ce52c690 2018-06-23 stsp
2217 ce52c690 2018-06-23 stsp static const struct got_error *
2218 e1cd8fed 2018-08-01 stsp blame_tree_entry(struct tog_view *view, struct got_tree_entry *te,
2219 e1cd8fed 2018-08-01 stsp struct tog_parent_trees *parents, struct got_object_id *commit_id,
2220 e1cd8fed 2018-08-01 stsp struct got_repository *repo)
2221 ce52c690 2018-06-23 stsp {
2222 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
2223 ce52c690 2018-06-23 stsp char *path;
2224 69efd4c4 2018-07-18 stsp
2225 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
2226 ce52c690 2018-06-23 stsp if (err)
2227 ce52c690 2018-06-23 stsp return err;
2228 ffd1d5e5 2018-06-23 stsp
2229 e1cd8fed 2018-08-01 stsp err = show_blame_view(view, path, commit_id, repo);
2230 69efd4c4 2018-07-18 stsp free(path);
2231 69efd4c4 2018-07-18 stsp return err;
2232 69efd4c4 2018-07-18 stsp }
2233 69efd4c4 2018-07-18 stsp
2234 69efd4c4 2018-07-18 stsp static const struct got_error *
2235 04cc582a 2018-08-01 stsp log_tree_entry(struct tog_view *view, struct got_tree_entry *te,
2236 04cc582a 2018-08-01 stsp struct tog_parent_trees *parents, struct got_object_id *commit_id,
2237 04cc582a 2018-08-01 stsp struct got_repository *repo)
2238 69efd4c4 2018-07-18 stsp {
2239 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
2240 69efd4c4 2018-07-18 stsp char *path;
2241 69efd4c4 2018-07-18 stsp
2242 69efd4c4 2018-07-18 stsp err = tree_entry_path(&path, parents, te);
2243 69efd4c4 2018-07-18 stsp if (err)
2244 69efd4c4 2018-07-18 stsp return err;
2245 69efd4c4 2018-07-18 stsp
2246 04cc582a 2018-08-01 stsp err = show_log_view(view, commit_id, repo, path);
2247 cb2ebc8a 2018-06-23 stsp free(path);
2248 cb2ebc8a 2018-06-23 stsp return err;
2249 ffd1d5e5 2018-06-23 stsp }
2250 ffd1d5e5 2018-06-23 stsp
2251 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2252 5221c383 2018-08-01 stsp show_tree_view(struct tog_view *view, struct got_tree_object *root,
2253 5221c383 2018-08-01 stsp struct got_object_id *commit_id, struct got_repository *repo)
2254 ffd1d5e5 2018-06-23 stsp {
2255 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
2256 1d13200f 2018-07-12 stsp int ch, done = 0, selected = 0, show_ids = 0;
2257 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree = root;
2258 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries;
2259 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *first_displayed_entry = NULL;
2260 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *last_displayed_entry = NULL;
2261 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *selected_entry = NULL;
2262 ffd1d5e5 2018-06-23 stsp char *commit_id_str = NULL, *tree_label = NULL;
2263 ffd1d5e5 2018-06-23 stsp int nentries, ndisplayed;
2264 ffd1d5e5 2018-06-23 stsp struct tog_parent_trees parents;
2265 ffd1d5e5 2018-06-23 stsp
2266 d9765a41 2018-06-23 stsp TAILQ_INIT(&parents);
2267 ffd1d5e5 2018-06-23 stsp
2268 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
2269 ffd1d5e5 2018-06-23 stsp if (err != NULL)
2270 ffd1d5e5 2018-06-23 stsp goto done;
2271 ffd1d5e5 2018-06-23 stsp
2272 decd3bd1 2018-07-12 stsp if (asprintf(&tree_label, "commit: %s", commit_id_str) == -1) {
2273 ffd1d5e5 2018-06-23 stsp err = got_error_from_errno();
2274 ffd1d5e5 2018-06-23 stsp goto done;
2275 ffd1d5e5 2018-06-23 stsp }
2276 ffd1d5e5 2018-06-23 stsp
2277 cc3c9aac 2018-08-01 stsp show_panel(view->panel);
2278 ffd1d5e5 2018-06-23 stsp
2279 ffd1d5e5 2018-06-23 stsp entries = got_object_tree_get_entries(root);
2280 ffd1d5e5 2018-06-23 stsp first_displayed_entry = SIMPLEQ_FIRST(&entries->head);
2281 ffd1d5e5 2018-06-23 stsp while (!done) {
2282 ce52c690 2018-06-23 stsp char *parent_path;
2283 ffd1d5e5 2018-06-23 stsp entries = got_object_tree_get_entries(tree);
2284 ffd1d5e5 2018-06-23 stsp nentries = entries->nentries;
2285 ffd1d5e5 2018-06-23 stsp if (tree != root)
2286 ffd1d5e5 2018-06-23 stsp nentries++; /* '..' directory */
2287 ce52c690 2018-06-23 stsp
2288 ce52c690 2018-06-23 stsp err = tree_entry_path(&parent_path, &parents, NULL);
2289 ce52c690 2018-06-23 stsp if (err)
2290 ce52c690 2018-06-23 stsp goto done;
2291 ffd1d5e5 2018-06-23 stsp
2292 f7d12f7e 2018-08-01 stsp err = draw_tree_entries(view, &first_displayed_entry,
2293 ffd1d5e5 2018-06-23 stsp &last_displayed_entry, &selected_entry, &ndisplayed,
2294 f7d12f7e 2018-08-01 stsp tree_label, show_ids, parent_path, entries, selected,
2295 f7d12f7e 2018-08-01 stsp view->nlines, tree == root);
2296 ce52c690 2018-06-23 stsp free(parent_path);
2297 ffd1d5e5 2018-06-23 stsp if (err)
2298 ffd1d5e5 2018-06-23 stsp break;
2299 ffd1d5e5 2018-06-23 stsp
2300 ffd1d5e5 2018-06-23 stsp nodelay(stdscr, FALSE);
2301 cc3c9aac 2018-08-01 stsp ch = wgetch(view->window);
2302 ffd1d5e5 2018-06-23 stsp nodelay(stdscr, TRUE);
2303 ffd1d5e5 2018-06-23 stsp switch (ch) {
2304 ffd1d5e5 2018-06-23 stsp case 'q':
2305 ffd1d5e5 2018-06-23 stsp done = 1;
2306 ffd1d5e5 2018-06-23 stsp break;
2307 1d13200f 2018-07-12 stsp case 'i':
2308 1d13200f 2018-07-12 stsp show_ids = !show_ids;
2309 69efd4c4 2018-07-18 stsp break;
2310 69efd4c4 2018-07-18 stsp case 'l':
2311 69efd4c4 2018-07-18 stsp if (selected_entry) {
2312 04cc582a 2018-08-01 stsp struct tog_view *log_view;
2313 6d0fee91 2018-08-01 stsp log_view = open_view(0, 0, 0, 0, view);
2314 04cc582a 2018-08-01 stsp if (log_view == NULL) {
2315 04cc582a 2018-08-01 stsp err = got_error_from_errno();
2316 04cc582a 2018-08-01 stsp goto done;
2317 04cc582a 2018-08-01 stsp }
2318 04cc582a 2018-08-01 stsp err = log_tree_entry(log_view,
2319 04cc582a 2018-08-01 stsp selected_entry, &parents,
2320 04cc582a 2018-08-01 stsp commit_id, repo);
2321 04cc582a 2018-08-01 stsp close_view(log_view);
2322 69efd4c4 2018-07-18 stsp if (err)
2323 69efd4c4 2018-07-18 stsp goto done;
2324 69efd4c4 2018-07-18 stsp }
2325 1d13200f 2018-07-12 stsp break;
2326 ffd1d5e5 2018-06-23 stsp case 'k':
2327 ffd1d5e5 2018-06-23 stsp case KEY_UP:
2328 ffd1d5e5 2018-06-23 stsp if (selected > 0)
2329 ffd1d5e5 2018-06-23 stsp selected--;
2330 ffd1d5e5 2018-06-23 stsp if (selected > 0)
2331 ffd1d5e5 2018-06-23 stsp break;
2332 ffd1d5e5 2018-06-23 stsp tree_scroll_up(&first_displayed_entry, 1,
2333 ffd1d5e5 2018-06-23 stsp entries, tree == root);
2334 ffd1d5e5 2018-06-23 stsp break;
2335 ffd1d5e5 2018-06-23 stsp case KEY_PPAGE:
2336 ffd1d5e5 2018-06-23 stsp if (SIMPLEQ_FIRST(&entries->head) ==
2337 ffd1d5e5 2018-06-23 stsp first_displayed_entry) {
2338 cf8f1261 2018-06-23 stsp if (tree != root)
2339 cf8f1261 2018-06-23 stsp first_displayed_entry = NULL;
2340 ffd1d5e5 2018-06-23 stsp selected = 0;
2341 ffd1d5e5 2018-06-23 stsp break;
2342 ffd1d5e5 2018-06-23 stsp }
2343 f7d12f7e 2018-08-01 stsp tree_scroll_up(&first_displayed_entry,
2344 f7d12f7e 2018-08-01 stsp view->nlines, entries, tree == root);
2345 ffd1d5e5 2018-06-23 stsp break;
2346 ffd1d5e5 2018-06-23 stsp case 'j':
2347 ffd1d5e5 2018-06-23 stsp case KEY_DOWN:
2348 ffd1d5e5 2018-06-23 stsp if (selected < ndisplayed - 1) {
2349 ffd1d5e5 2018-06-23 stsp selected++;
2350 ffd1d5e5 2018-06-23 stsp break;
2351 ffd1d5e5 2018-06-23 stsp }
2352 ffd1d5e5 2018-06-23 stsp tree_scroll_down(&first_displayed_entry, 1,
2353 ffd1d5e5 2018-06-23 stsp last_displayed_entry, entries);
2354 ffd1d5e5 2018-06-23 stsp break;
2355 ffd1d5e5 2018-06-23 stsp case KEY_NPAGE:
2356 f7d12f7e 2018-08-01 stsp tree_scroll_down(&first_displayed_entry,
2357 f7d12f7e 2018-08-01 stsp view->nlines, last_displayed_entry,
2358 f7d12f7e 2018-08-01 stsp entries);
2359 ffd1d5e5 2018-06-23 stsp if (SIMPLEQ_NEXT(last_displayed_entry, entry))
2360 ffd1d5e5 2018-06-23 stsp break;
2361 ffd1d5e5 2018-06-23 stsp /* can't scroll any further; move cursor down */
2362 ffd1d5e5 2018-06-23 stsp if (selected < ndisplayed - 1)
2363 ffd1d5e5 2018-06-23 stsp selected = ndisplayed - 1;
2364 ffd1d5e5 2018-06-23 stsp break;
2365 ffd1d5e5 2018-06-23 stsp case KEY_ENTER:
2366 ffd1d5e5 2018-06-23 stsp case '\r':
2367 ffd1d5e5 2018-06-23 stsp if (selected_entry == NULL) {
2368 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *parent;
2369 ffd1d5e5 2018-06-23 stsp case KEY_BACKSPACE:
2370 ffd1d5e5 2018-06-23 stsp /* user selected '..' */
2371 ffd1d5e5 2018-06-23 stsp if (tree == root)
2372 ffd1d5e5 2018-06-23 stsp break;
2373 d9765a41 2018-06-23 stsp parent = TAILQ_FIRST(&parents);
2374 d9765a41 2018-06-23 stsp TAILQ_REMOVE(&parents, parent, entry);
2375 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
2376 ffd1d5e5 2018-06-23 stsp tree = parent->tree;
2377 ffd1d5e5 2018-06-23 stsp first_displayed_entry =
2378 ffd1d5e5 2018-06-23 stsp parent->first_displayed_entry;
2379 ffd1d5e5 2018-06-23 stsp selected_entry = parent->selected_entry;
2380 ffd1d5e5 2018-06-23 stsp selected = parent->selected;
2381 ffd1d5e5 2018-06-23 stsp free(parent);
2382 ffd1d5e5 2018-06-23 stsp } else if (S_ISDIR(selected_entry->mode)) {
2383 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *parent;
2384 ffd1d5e5 2018-06-23 stsp struct got_tree_object *child;
2385 ffd1d5e5 2018-06-23 stsp err = got_object_open_as_tree(
2386 ffd1d5e5 2018-06-23 stsp &child, repo, selected_entry->id);
2387 ffd1d5e5 2018-06-23 stsp if (err)
2388 ffd1d5e5 2018-06-23 stsp goto done;
2389 ffd1d5e5 2018-06-23 stsp parent = calloc(1, sizeof(*parent));
2390 ffd1d5e5 2018-06-23 stsp if (parent == NULL) {
2391 ffd1d5e5 2018-06-23 stsp err = got_error_from_errno();
2392 ffd1d5e5 2018-06-23 stsp goto done;
2393 ffd1d5e5 2018-06-23 stsp }
2394 ffd1d5e5 2018-06-23 stsp parent->tree = tree;
2395 ffd1d5e5 2018-06-23 stsp parent->first_displayed_entry =
2396 ffd1d5e5 2018-06-23 stsp first_displayed_entry;
2397 ffd1d5e5 2018-06-23 stsp parent->selected_entry = selected_entry;
2398 ffd1d5e5 2018-06-23 stsp parent->selected = selected;
2399 d9765a41 2018-06-23 stsp TAILQ_INSERT_HEAD(&parents, parent,
2400 ffd1d5e5 2018-06-23 stsp entry);
2401 ffd1d5e5 2018-06-23 stsp tree = child;
2402 ffd1d5e5 2018-06-23 stsp selected = 0;
2403 ffd1d5e5 2018-06-23 stsp first_displayed_entry = NULL;
2404 ffd1d5e5 2018-06-23 stsp } else if (S_ISREG(selected_entry->mode)) {
2405 6d0fee91 2018-08-01 stsp struct tog_view *blame_view =
2406 6d0fee91 2018-08-01 stsp open_view(0, 0, 0, 0, view);
2407 e1cd8fed 2018-08-01 stsp if (blame_view == NULL) {
2408 e1cd8fed 2018-08-01 stsp err = got_error_from_errno();
2409 e1cd8fed 2018-08-01 stsp goto done;
2410 e1cd8fed 2018-08-01 stsp }
2411 e1cd8fed 2018-08-01 stsp err = blame_tree_entry(blame_view,
2412 e1cd8fed 2018-08-01 stsp selected_entry, &parents,
2413 e1cd8fed 2018-08-01 stsp commit_id, repo);
2414 e1cd8fed 2018-08-01 stsp close_view(blame_view);
2415 ffd1d5e5 2018-06-23 stsp if (err)
2416 ffd1d5e5 2018-06-23 stsp goto done;
2417 ffd1d5e5 2018-06-23 stsp }
2418 ffd1d5e5 2018-06-23 stsp break;
2419 ffd1d5e5 2018-06-23 stsp case KEY_RESIZE:
2420 a41d2007 2018-08-01 stsp err = view_resize(view);
2421 a41d2007 2018-08-01 stsp if (err)
2422 a41d2007 2018-08-01 stsp goto done;
2423 f7d12f7e 2018-08-01 stsp if (selected > view->nlines)
2424 ffd1d5e5 2018-06-23 stsp selected = ndisplayed - 1;
2425 ffd1d5e5 2018-06-23 stsp break;
2426 ffd1d5e5 2018-06-23 stsp default:
2427 ffd1d5e5 2018-06-23 stsp break;
2428 ffd1d5e5 2018-06-23 stsp }
2429 ffd1d5e5 2018-06-23 stsp }
2430 ffd1d5e5 2018-06-23 stsp done:
2431 ffd1d5e5 2018-06-23 stsp free(tree_label);
2432 ffd1d5e5 2018-06-23 stsp free(commit_id_str);
2433 d9765a41 2018-06-23 stsp while (!TAILQ_EMPTY(&parents)) {
2434 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *parent;
2435 d9765a41 2018-06-23 stsp parent = TAILQ_FIRST(&parents);
2436 d9765a41 2018-06-23 stsp TAILQ_REMOVE(&parents, parent, entry);
2437 ffd1d5e5 2018-06-23 stsp free(parent);
2438 ffd1d5e5 2018-06-23 stsp
2439 ffd1d5e5 2018-06-23 stsp }
2440 ffd1d5e5 2018-06-23 stsp if (tree != root)
2441 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
2442 ffd1d5e5 2018-06-23 stsp return err;
2443 9f7d7167 2018-04-29 stsp }
2444 9f7d7167 2018-04-29 stsp
2445 ffd1d5e5 2018-06-23 stsp __dead static void
2446 ffd1d5e5 2018-06-23 stsp usage_tree(void)
2447 ffd1d5e5 2018-06-23 stsp {
2448 ffd1d5e5 2018-06-23 stsp endwin();
2449 ffd1d5e5 2018-06-23 stsp fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
2450 ffd1d5e5 2018-06-23 stsp getprogname());
2451 ffd1d5e5 2018-06-23 stsp exit(1);
2452 ffd1d5e5 2018-06-23 stsp }
2453 ffd1d5e5 2018-06-23 stsp
2454 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2455 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
2456 ffd1d5e5 2018-06-23 stsp {
2457 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
2458 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
2459 ffd1d5e5 2018-06-23 stsp char *repo_path = NULL;
2460 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
2461 ffd1d5e5 2018-06-23 stsp char *commit_id_arg = NULL;
2462 ffd1d5e5 2018-06-23 stsp struct got_commit_object *commit = NULL;
2463 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree = NULL;
2464 ffd1d5e5 2018-06-23 stsp int ch;
2465 5221c383 2018-08-01 stsp struct tog_view *view;
2466 ffd1d5e5 2018-06-23 stsp
2467 ffd1d5e5 2018-06-23 stsp #ifndef PROFILE
2468 ffd1d5e5 2018-06-23 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
2469 ffd1d5e5 2018-06-23 stsp err(1, "pledge");
2470 ffd1d5e5 2018-06-23 stsp #endif
2471 ffd1d5e5 2018-06-23 stsp
2472 ffd1d5e5 2018-06-23 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
2473 ffd1d5e5 2018-06-23 stsp switch (ch) {
2474 ffd1d5e5 2018-06-23 stsp case 'c':
2475 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
2476 ffd1d5e5 2018-06-23 stsp break;
2477 ffd1d5e5 2018-06-23 stsp default:
2478 ffd1d5e5 2018-06-23 stsp usage();
2479 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
2480 ffd1d5e5 2018-06-23 stsp }
2481 ffd1d5e5 2018-06-23 stsp }
2482 ffd1d5e5 2018-06-23 stsp
2483 ffd1d5e5 2018-06-23 stsp argc -= optind;
2484 ffd1d5e5 2018-06-23 stsp argv += optind;
2485 ffd1d5e5 2018-06-23 stsp
2486 ffd1d5e5 2018-06-23 stsp if (argc == 0) {
2487 ffd1d5e5 2018-06-23 stsp repo_path = getcwd(NULL, 0);
2488 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
2489 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
2490 ffd1d5e5 2018-06-23 stsp } else if (argc == 1) {
2491 ffd1d5e5 2018-06-23 stsp repo_path = realpath(argv[0], NULL);
2492 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
2493 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
2494 ffd1d5e5 2018-06-23 stsp } else
2495 ffd1d5e5 2018-06-23 stsp usage_log();
2496 ffd1d5e5 2018-06-23 stsp
2497 ffd1d5e5 2018-06-23 stsp error = got_repo_open(&repo, repo_path);
2498 ffd1d5e5 2018-06-23 stsp free(repo_path);
2499 ffd1d5e5 2018-06-23 stsp if (error != NULL)
2500 ffd1d5e5 2018-06-23 stsp return error;
2501 ffd1d5e5 2018-06-23 stsp
2502 ffd1d5e5 2018-06-23 stsp if (commit_id_arg == NULL) {
2503 ffd1d5e5 2018-06-23 stsp error = get_head_commit_id(&commit_id, repo);
2504 ffd1d5e5 2018-06-23 stsp if (error != NULL)
2505 ffd1d5e5 2018-06-23 stsp goto done;
2506 ffd1d5e5 2018-06-23 stsp } else {
2507 ffd1d5e5 2018-06-23 stsp struct got_object *obj;
2508 ffd1d5e5 2018-06-23 stsp error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
2509 ffd1d5e5 2018-06-23 stsp if (error == NULL) {
2510 ffd1d5e5 2018-06-23 stsp commit_id = got_object_get_id(obj);
2511 ffd1d5e5 2018-06-23 stsp if (commit_id == NULL)
2512 ffd1d5e5 2018-06-23 stsp error = got_error_from_errno();
2513 ffd1d5e5 2018-06-23 stsp }
2514 ffd1d5e5 2018-06-23 stsp }
2515 ffd1d5e5 2018-06-23 stsp if (error != NULL)
2516 ffd1d5e5 2018-06-23 stsp goto done;
2517 ffd1d5e5 2018-06-23 stsp
2518 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
2519 ffd1d5e5 2018-06-23 stsp if (error != NULL)
2520 ffd1d5e5 2018-06-23 stsp goto done;
2521 ffd1d5e5 2018-06-23 stsp
2522 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_tree(&tree, repo, commit->tree_id);
2523 ffd1d5e5 2018-06-23 stsp if (error != NULL)
2524 ffd1d5e5 2018-06-23 stsp goto done;
2525 ffd1d5e5 2018-06-23 stsp
2526 6d0fee91 2018-08-01 stsp view = open_view(0, 0, 0, 0, NULL);
2527 5221c383 2018-08-01 stsp if (view == NULL) {
2528 5221c383 2018-08-01 stsp error = got_error_from_errno();
2529 5221c383 2018-08-01 stsp goto done;
2530 5221c383 2018-08-01 stsp }
2531 5221c383 2018-08-01 stsp error = show_tree_view(view, tree, commit_id, repo);
2532 5221c383 2018-08-01 stsp close_view(view);
2533 ffd1d5e5 2018-06-23 stsp done:
2534 ffd1d5e5 2018-06-23 stsp free(commit_id);
2535 ffd1d5e5 2018-06-23 stsp if (commit)
2536 ffd1d5e5 2018-06-23 stsp got_object_commit_close(commit);
2537 ffd1d5e5 2018-06-23 stsp if (tree)
2538 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
2539 ffd1d5e5 2018-06-23 stsp if (repo)
2540 ffd1d5e5 2018-06-23 stsp got_repo_close(repo);
2541 ffd1d5e5 2018-06-23 stsp return error;
2542 ffd1d5e5 2018-06-23 stsp }
2543 5c5136c5 2018-05-20 stsp static void
2544 9f7d7167 2018-04-29 stsp init_curses(void)
2545 9f7d7167 2018-04-29 stsp {
2546 9f7d7167 2018-04-29 stsp initscr();
2547 9f7d7167 2018-04-29 stsp cbreak();
2548 9f7d7167 2018-04-29 stsp noecho();
2549 9f7d7167 2018-04-29 stsp nonl();
2550 9f7d7167 2018-04-29 stsp intrflush(stdscr, FALSE);
2551 9f7d7167 2018-04-29 stsp keypad(stdscr, TRUE);
2552 1f475ad8 2018-05-10 stsp curs_set(0);
2553 9f7d7167 2018-04-29 stsp }
2554 9f7d7167 2018-04-29 stsp
2555 4ed7e80c 2018-05-20 stsp __dead static void
2556 9f7d7167 2018-04-29 stsp usage(void)
2557 9f7d7167 2018-04-29 stsp {
2558 9f7d7167 2018-04-29 stsp int i;
2559 9f7d7167 2018-04-29 stsp
2560 c2301be8 2018-04-30 stsp fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
2561 9f7d7167 2018-04-29 stsp "Available commands:\n", getprogname());
2562 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
2563 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = &tog_commands[i];
2564 c2301be8 2018-04-30 stsp fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
2565 9f7d7167 2018-04-29 stsp }
2566 9f7d7167 2018-04-29 stsp exit(1);
2567 9f7d7167 2018-04-29 stsp }
2568 9f7d7167 2018-04-29 stsp
2569 c2301be8 2018-04-30 stsp static char **
2570 c2301be8 2018-04-30 stsp make_argv(const char *arg0, const char *arg1)
2571 c2301be8 2018-04-30 stsp {
2572 c2301be8 2018-04-30 stsp char **argv;
2573 c2301be8 2018-04-30 stsp int argc = (arg1 == NULL ? 1 : 2);
2574 c2301be8 2018-04-30 stsp
2575 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
2576 c2301be8 2018-04-30 stsp if (argv == NULL)
2577 c2301be8 2018-04-30 stsp err(1, "calloc");
2578 c2301be8 2018-04-30 stsp argv[0] = strdup(arg0);
2579 c2301be8 2018-04-30 stsp if (argv[0] == NULL)
2580 c2301be8 2018-04-30 stsp err(1, "calloc");
2581 c2301be8 2018-04-30 stsp if (arg1) {
2582 c2301be8 2018-04-30 stsp argv[1] = strdup(arg1);
2583 c2301be8 2018-04-30 stsp if (argv[1] == NULL)
2584 c2301be8 2018-04-30 stsp err(1, "calloc");
2585 c2301be8 2018-04-30 stsp }
2586 c2301be8 2018-04-30 stsp
2587 c2301be8 2018-04-30 stsp return argv;
2588 c2301be8 2018-04-30 stsp }
2589 c2301be8 2018-04-30 stsp
2590 9f7d7167 2018-04-29 stsp int
2591 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
2592 9f7d7167 2018-04-29 stsp {
2593 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
2594 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
2595 9f7d7167 2018-04-29 stsp int ch, hflag = 0;
2596 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
2597 9f7d7167 2018-04-29 stsp
2598 9f7d7167 2018-04-29 stsp setlocale(LC_ALL, "");
2599 9f7d7167 2018-04-29 stsp
2600 9f7d7167 2018-04-29 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
2601 9f7d7167 2018-04-29 stsp switch (ch) {
2602 9f7d7167 2018-04-29 stsp case 'h':
2603 9f7d7167 2018-04-29 stsp hflag = 1;
2604 9f7d7167 2018-04-29 stsp break;
2605 9f7d7167 2018-04-29 stsp default:
2606 9f7d7167 2018-04-29 stsp usage();
2607 9f7d7167 2018-04-29 stsp /* NOTREACHED */
2608 9f7d7167 2018-04-29 stsp }
2609 9f7d7167 2018-04-29 stsp }
2610 9f7d7167 2018-04-29 stsp
2611 9f7d7167 2018-04-29 stsp argc -= optind;
2612 9f7d7167 2018-04-29 stsp argv += optind;
2613 9f7d7167 2018-04-29 stsp optind = 0;
2614 c2301be8 2018-04-30 stsp optreset = 1;
2615 9f7d7167 2018-04-29 stsp
2616 c2301be8 2018-04-30 stsp if (argc == 0) {
2617 f29d3e89 2018-06-23 stsp if (hflag)
2618 f29d3e89 2018-06-23 stsp usage();
2619 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
2620 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
2621 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, NULL);
2622 c2301be8 2018-04-30 stsp argc = 1;
2623 c2301be8 2018-04-30 stsp } else {
2624 9f7d7167 2018-04-29 stsp int i;
2625 9f7d7167 2018-04-29 stsp
2626 c2301be8 2018-04-30 stsp /* Did the user specific a command? */
2627 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
2628 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
2629 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
2630 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
2631 9f7d7167 2018-04-29 stsp if (hflag)
2632 9f7d7167 2018-04-29 stsp tog_commands[i].cmd_usage();
2633 9f7d7167 2018-04-29 stsp break;
2634 9f7d7167 2018-04-29 stsp }
2635 9f7d7167 2018-04-29 stsp }
2636 9f7d7167 2018-04-29 stsp if (cmd == NULL) {
2637 c2301be8 2018-04-30 stsp /* Did the user specify a repository? */
2638 c2301be8 2018-04-30 stsp char *repo_path = realpath(argv[0], NULL);
2639 c2301be8 2018-04-30 stsp if (repo_path) {
2640 c2301be8 2018-04-30 stsp struct got_repository *repo;
2641 c2301be8 2018-04-30 stsp error = got_repo_open(&repo, repo_path);
2642 c2301be8 2018-04-30 stsp if (error == NULL)
2643 c2301be8 2018-04-30 stsp got_repo_close(repo);
2644 c2301be8 2018-04-30 stsp } else
2645 ad7de8d9 2018-04-30 stsp error = got_error_from_errno();
2646 c2301be8 2018-04-30 stsp if (error) {
2647 f29d3e89 2018-06-23 stsp if (hflag) {
2648 f29d3e89 2018-06-23 stsp fprintf(stderr, "%s: '%s' is not a "
2649 f29d3e89 2018-06-23 stsp "known command\n", getprogname(),
2650 f29d3e89 2018-06-23 stsp argv[0]);
2651 f29d3e89 2018-06-23 stsp usage();
2652 f29d3e89 2018-06-23 stsp }
2653 ad7de8d9 2018-04-30 stsp fprintf(stderr, "%s: '%s' is neither a known "
2654 ad7de8d9 2018-04-30 stsp "command nor a path to a repository\n",
2655 c2301be8 2018-04-30 stsp getprogname(), argv[0]);
2656 ad7de8d9 2018-04-30 stsp free(repo_path);
2657 c2301be8 2018-04-30 stsp return 1;
2658 c2301be8 2018-04-30 stsp }
2659 c2301be8 2018-04-30 stsp cmd = &tog_commands[0];
2660 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, repo_path);
2661 c2301be8 2018-04-30 stsp argc = 2;
2662 c2301be8 2018-04-30 stsp free(repo_path);
2663 9f7d7167 2018-04-29 stsp }
2664 9f7d7167 2018-04-29 stsp }
2665 9f7d7167 2018-04-29 stsp
2666 5c5136c5 2018-05-20 stsp init_curses();
2667 9f7d7167 2018-04-29 stsp
2668 c2301be8 2018-04-30 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
2669 9f7d7167 2018-04-29 stsp if (error)
2670 9f7d7167 2018-04-29 stsp goto done;
2671 9f7d7167 2018-04-29 stsp done:
2672 9f7d7167 2018-04-29 stsp endwin();
2673 c2301be8 2018-04-30 stsp free(cmd_argv);
2674 9f7d7167 2018-04-29 stsp if (error)
2675 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
2676 9f7d7167 2018-04-29 stsp return 0;
2677 9f7d7167 2018-04-29 stsp }