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