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