Blame


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