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 fed328cc 2018-05-20 stsp static struct tog_view {
86 26ed57b2 2018-05-19 stsp WINDOW *window;
87 26ed57b2 2018-05-19 stsp PANEL *panel;
88 ffd1d5e5 2018-06-23 stsp } tog_log_view, tog_diff_view, tog_blame_view, tog_tree_view;
89 cd0acaa7 2018-05-20 stsp
90 cd0acaa7 2018-05-20 stsp static const struct got_error *
91 cd0acaa7 2018-05-20 stsp show_diff_view(struct got_object *, struct got_object *,
92 cd0acaa7 2018-05-20 stsp struct got_repository *);
93 cd0acaa7 2018-05-20 stsp static const struct got_error *
94 cd0acaa7 2018-05-20 stsp show_log_view(struct got_object_id *, struct got_repository *);
95 a70480e0 2018-06-23 stsp static const struct got_error *
96 a70480e0 2018-06-23 stsp show_blame_view(const char *, struct got_object_id *, struct got_repository *);
97 ffd1d5e5 2018-06-23 stsp static const struct got_error *
98 ffd1d5e5 2018-06-23 stsp show_tree_view(struct got_tree_object *, struct got_object_id *,
99 ffd1d5e5 2018-06-23 stsp struct got_repository *);
100 26ed57b2 2018-05-19 stsp
101 4ed7e80c 2018-05-20 stsp __dead static void
102 9f7d7167 2018-04-29 stsp usage_log(void)
103 9f7d7167 2018-04-29 stsp {
104 80ddbec8 2018-04-29 stsp endwin();
105 80ddbec8 2018-04-29 stsp fprintf(stderr, "usage: %s log [-c commit] [repository-path]\n",
106 9f7d7167 2018-04-29 stsp getprogname());
107 9f7d7167 2018-04-29 stsp exit(1);
108 80ddbec8 2018-04-29 stsp }
109 80ddbec8 2018-04-29 stsp
110 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
111 80ddbec8 2018-04-29 stsp static const struct got_error *
112 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
113 963b370f 2018-05-20 stsp {
114 00dfcb92 2018-06-11 stsp char *vis = NULL;
115 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
116 963b370f 2018-05-20 stsp
117 963b370f 2018-05-20 stsp *ws = NULL;
118 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
119 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
120 00dfcb92 2018-06-11 stsp int vislen;
121 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
122 00dfcb92 2018-06-11 stsp return got_error_from_errno();
123 00dfcb92 2018-06-11 stsp
124 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
125 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
126 00dfcb92 2018-06-11 stsp if (err)
127 00dfcb92 2018-06-11 stsp return err;
128 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
129 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
130 a7f50699 2018-06-11 stsp err = got_error_from_errno(); /* give up */
131 a7f50699 2018-06-11 stsp goto done;
132 a7f50699 2018-06-11 stsp }
133 00dfcb92 2018-06-11 stsp }
134 963b370f 2018-05-20 stsp
135 963b370f 2018-05-20 stsp *ws = calloc(*wlen + 1, sizeof(*ws));
136 a7f50699 2018-06-11 stsp if (*ws == NULL) {
137 a7f50699 2018-06-11 stsp err = got_error_from_errno();
138 a7f50699 2018-06-11 stsp goto done;
139 a7f50699 2018-06-11 stsp }
140 963b370f 2018-05-20 stsp
141 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
142 963b370f 2018-05-20 stsp err = got_error_from_errno();
143 a7f50699 2018-06-11 stsp done:
144 00dfcb92 2018-06-11 stsp free(vis);
145 963b370f 2018-05-20 stsp if (err) {
146 963b370f 2018-05-20 stsp free(*ws);
147 963b370f 2018-05-20 stsp *ws = NULL;
148 963b370f 2018-05-20 stsp *wlen = 0;
149 963b370f 2018-05-20 stsp }
150 963b370f 2018-05-20 stsp return err;
151 963b370f 2018-05-20 stsp }
152 963b370f 2018-05-20 stsp
153 963b370f 2018-05-20 stsp /* Format a line for display, ensuring that it won't overflow a width limit. */
154 963b370f 2018-05-20 stsp static const struct got_error *
155 ffd1d5e5 2018-06-23 stsp format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
156 963b370f 2018-05-20 stsp {
157 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
158 963b370f 2018-05-20 stsp int cols = 0;
159 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
160 963b370f 2018-05-20 stsp size_t wlen;
161 963b370f 2018-05-20 stsp int i;
162 963b370f 2018-05-20 stsp
163 963b370f 2018-05-20 stsp *wlinep = NULL;
164 963b370f 2018-05-20 stsp
165 963b370f 2018-05-20 stsp err = mbs2ws(&wline, &wlen, line);
166 963b370f 2018-05-20 stsp if (err)
167 963b370f 2018-05-20 stsp return err;
168 963b370f 2018-05-20 stsp
169 963b370f 2018-05-20 stsp i = 0;
170 963b370f 2018-05-20 stsp while (i < wlen && cols <= wlimit) {
171 963b370f 2018-05-20 stsp int width = wcwidth(wline[i]);
172 963b370f 2018-05-20 stsp switch (width) {
173 963b370f 2018-05-20 stsp case 0:
174 963b370f 2018-05-20 stsp break;
175 963b370f 2018-05-20 stsp case 1:
176 963b370f 2018-05-20 stsp case 2:
177 963b370f 2018-05-20 stsp cols += width;
178 963b370f 2018-05-20 stsp break;
179 963b370f 2018-05-20 stsp case -1:
180 963b370f 2018-05-20 stsp if (wline[i] == L'\t')
181 e7ea91f8 2018-07-08 stsp cols += TABSIZE - (cols % TABSIZE);
182 963b370f 2018-05-20 stsp break;
183 963b370f 2018-05-20 stsp default:
184 963b370f 2018-05-20 stsp err = got_error_from_errno();
185 963b370f 2018-05-20 stsp goto done;
186 963b370f 2018-05-20 stsp }
187 963b370f 2018-05-20 stsp if (cols <= COLS) {
188 963b370f 2018-05-20 stsp i++;
189 963b370f 2018-05-20 stsp if (widthp)
190 963b370f 2018-05-20 stsp *widthp = cols;
191 963b370f 2018-05-20 stsp }
192 963b370f 2018-05-20 stsp }
193 963b370f 2018-05-20 stsp wline[i] = L'\0';
194 963b370f 2018-05-20 stsp done:
195 963b370f 2018-05-20 stsp if (err)
196 963b370f 2018-05-20 stsp free(wline);
197 963b370f 2018-05-20 stsp else
198 963b370f 2018-05-20 stsp *wlinep = wline;
199 963b370f 2018-05-20 stsp return err;
200 963b370f 2018-05-20 stsp }
201 963b370f 2018-05-20 stsp
202 963b370f 2018-05-20 stsp static const struct got_error *
203 0553a4e3 2018-04-30 stsp draw_commit(struct got_commit_object *commit, struct got_object_id *id)
204 80ddbec8 2018-04-29 stsp {
205 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
206 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
207 6d9fbc00 2018-04-29 stsp char *author0 = NULL, *author = NULL;
208 bb737323 2018-05-20 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
209 bb737323 2018-05-20 stsp int author_width, logmsg_width;
210 6d9fbc00 2018-04-29 stsp char *newline, *smallerthan;
211 80ddbec8 2018-04-29 stsp char *line = NULL;
212 6d9fbc00 2018-04-29 stsp char *id_str = NULL;
213 bb737323 2018-05-20 stsp size_t id_len;
214 bb737323 2018-05-20 stsp int col, limit;
215 bb737323 2018-05-20 stsp static const size_t id_display_cols = 8;
216 bb737323 2018-05-20 stsp static const size_t author_display_cols = 16;
217 9c2eaf34 2018-05-20 stsp const int avail = COLS;
218 80ddbec8 2018-04-29 stsp
219 80ddbec8 2018-04-29 stsp err = got_object_id_str(&id_str, id);
220 80ddbec8 2018-04-29 stsp if (err)
221 80ddbec8 2018-04-29 stsp return err;
222 881b2d3e 2018-04-30 stsp id_len = strlen(id_str);
223 bb737323 2018-05-20 stsp if (avail < id_display_cols) {
224 bb737323 2018-05-20 stsp limit = MIN(id_len, avail);
225 bb737323 2018-05-20 stsp waddnstr(tog_log_view.window, id_str, limit);
226 bb737323 2018-05-20 stsp } else {
227 bb737323 2018-05-20 stsp limit = MIN(id_display_cols, id_len);
228 bb737323 2018-05-20 stsp waddnstr(tog_log_view.window, id_str, limit);
229 6d9fbc00 2018-04-29 stsp }
230 bb737323 2018-05-20 stsp col = limit + 1;
231 9c2eaf34 2018-05-20 stsp while (col <= avail && col < id_display_cols + 2) {
232 bb737323 2018-05-20 stsp waddch(tog_log_view.window, ' ');
233 bb737323 2018-05-20 stsp col++;
234 bb737323 2018-05-20 stsp }
235 9c2eaf34 2018-05-20 stsp if (col > avail)
236 9c2eaf34 2018-05-20 stsp goto done;
237 80ddbec8 2018-04-29 stsp
238 6d9fbc00 2018-04-29 stsp author0 = strdup(commit->author);
239 6d9fbc00 2018-04-29 stsp if (author0 == NULL) {
240 80ddbec8 2018-04-29 stsp err = got_error_from_errno();
241 80ddbec8 2018-04-29 stsp goto done;
242 80ddbec8 2018-04-29 stsp }
243 6d9fbc00 2018-04-29 stsp author = author0;
244 6d9fbc00 2018-04-29 stsp smallerthan = strchr(author, '<');
245 6d9fbc00 2018-04-29 stsp if (smallerthan)
246 6d9fbc00 2018-04-29 stsp *smallerthan = '\0';
247 6d9fbc00 2018-04-29 stsp else {
248 6d9fbc00 2018-04-29 stsp char *at = strchr(author, '@');
249 6d9fbc00 2018-04-29 stsp if (at)
250 6d9fbc00 2018-04-29 stsp *at = '\0';
251 6d9fbc00 2018-04-29 stsp }
252 bb737323 2018-05-20 stsp limit = MIN(avail, author_display_cols);
253 bb737323 2018-05-20 stsp err = format_line(&wauthor, &author_width, author, limit);
254 bb737323 2018-05-20 stsp if (err)
255 bb737323 2018-05-20 stsp goto done;
256 bb737323 2018-05-20 stsp waddwstr(tog_log_view.window, wauthor);
257 bb737323 2018-05-20 stsp col += author_width;
258 9c2eaf34 2018-05-20 stsp while (col <= avail && author_width < author_display_cols + 1) {
259 bb737323 2018-05-20 stsp waddch(tog_log_view.window, ' ');
260 bb737323 2018-05-20 stsp col++;
261 bb737323 2018-05-20 stsp author_width++;
262 bb737323 2018-05-20 stsp }
263 9c2eaf34 2018-05-20 stsp if (col > avail)
264 9c2eaf34 2018-05-20 stsp goto done;
265 80ddbec8 2018-04-29 stsp
266 bb737323 2018-05-20 stsp logmsg0 = strdup(commit->logmsg);
267 bb737323 2018-05-20 stsp if (logmsg0 == NULL) {
268 6d9fbc00 2018-04-29 stsp err = got_error_from_errno();
269 6d9fbc00 2018-04-29 stsp goto done;
270 6d9fbc00 2018-04-29 stsp }
271 bb737323 2018-05-20 stsp logmsg = logmsg0;
272 bb737323 2018-05-20 stsp while (*logmsg == '\n')
273 bb737323 2018-05-20 stsp logmsg++;
274 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
275 bb737323 2018-05-20 stsp if (newline)
276 bb737323 2018-05-20 stsp *newline = '\0';
277 bb737323 2018-05-20 stsp limit = avail - col;
278 bb737323 2018-05-20 stsp err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
279 bb737323 2018-05-20 stsp if (err)
280 bb737323 2018-05-20 stsp goto done;
281 bb737323 2018-05-20 stsp waddwstr(tog_log_view.window, wlogmsg);
282 bb737323 2018-05-20 stsp col += logmsg_width;
283 bb737323 2018-05-20 stsp while (col <= avail) {
284 bb737323 2018-05-20 stsp waddch(tog_log_view.window, ' ');
285 bb737323 2018-05-20 stsp col++;
286 881b2d3e 2018-04-30 stsp }
287 80ddbec8 2018-04-29 stsp done:
288 80ddbec8 2018-04-29 stsp free(logmsg0);
289 bb737323 2018-05-20 stsp free(wlogmsg);
290 6d9fbc00 2018-04-29 stsp free(author0);
291 bb737323 2018-05-20 stsp free(wauthor);
292 80ddbec8 2018-04-29 stsp free(line);
293 6d9fbc00 2018-04-29 stsp free(id_str);
294 80ddbec8 2018-04-29 stsp return err;
295 80ddbec8 2018-04-29 stsp }
296 26ed57b2 2018-05-19 stsp
297 80ddbec8 2018-04-29 stsp struct commit_queue_entry {
298 80ddbec8 2018-04-29 stsp TAILQ_ENTRY(commit_queue_entry) entry;
299 80ddbec8 2018-04-29 stsp struct got_object_id *id;
300 80ddbec8 2018-04-29 stsp struct got_commit_object *commit;
301 80ddbec8 2018-04-29 stsp };
302 0553a4e3 2018-04-30 stsp TAILQ_HEAD(commit_queue, commit_queue_entry);
303 80ddbec8 2018-04-29 stsp
304 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
305 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
306 899d86c2 2018-05-10 stsp struct got_object_id *id)
307 80ddbec8 2018-04-29 stsp {
308 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
309 80ddbec8 2018-04-29 stsp
310 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
311 80ddbec8 2018-04-29 stsp if (entry == NULL)
312 899d86c2 2018-05-10 stsp return NULL;
313 99db9666 2018-05-07 stsp
314 899d86c2 2018-05-10 stsp entry->id = id;
315 99db9666 2018-05-07 stsp entry->commit = commit;
316 899d86c2 2018-05-10 stsp return entry;
317 99db9666 2018-05-07 stsp }
318 80ddbec8 2018-04-29 stsp
319 99db9666 2018-05-07 stsp static void
320 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
321 99db9666 2018-05-07 stsp {
322 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
323 99db9666 2018-05-07 stsp
324 99db9666 2018-05-07 stsp entry = TAILQ_FIRST(commits);
325 99db9666 2018-05-07 stsp TAILQ_REMOVE(commits, entry, entry);
326 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
327 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
328 99db9666 2018-05-07 stsp free(entry);
329 99db9666 2018-05-07 stsp }
330 99db9666 2018-05-07 stsp
331 99db9666 2018-05-07 stsp static void
332 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
333 99db9666 2018-05-07 stsp {
334 99db9666 2018-05-07 stsp while (!TAILQ_EMPTY(commits))
335 99db9666 2018-05-07 stsp pop_commit(commits);
336 c4972b91 2018-05-07 stsp }
337 c4972b91 2018-05-07 stsp
338 c4972b91 2018-05-07 stsp static const struct got_error *
339 9ba79e04 2018-06-11 stsp queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
340 9ba79e04 2018-06-11 stsp struct got_object_id *start_id, struct got_repository *repo)
341 c4972b91 2018-05-07 stsp {
342 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
343 899d86c2 2018-05-10 stsp struct got_object_id *id;
344 9ba79e04 2018-06-11 stsp struct commit_queue_entry *entry;
345 c4972b91 2018-05-07 stsp
346 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_start(graph, start_id);
347 c4972b91 2018-05-07 stsp if (err)
348 c4972b91 2018-05-07 stsp return err;
349 c4972b91 2018-05-07 stsp
350 9ba79e04 2018-06-11 stsp entry = TAILQ_LAST(commits, commit_queue);
351 9ba79e04 2018-06-11 stsp if (entry && got_object_id_cmp(entry->id, start_id) == 0) {
352 9ba79e04 2018-06-11 stsp int nfetched;
353 899d86c2 2018-05-10 stsp
354 9ba79e04 2018-06-11 stsp /* Start ID's commit is already on the queue; skip over it. */
355 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
356 9ba79e04 2018-06-11 stsp if (err && err->code != GOT_ERR_ITER_NEED_MORE)
357 9ba79e04 2018-06-11 stsp return err;
358 9ba79e04 2018-06-11 stsp
359 9ba79e04 2018-06-11 stsp err = got_commit_graph_fetch_commits(&nfetched, graph, 1, repo);
360 9ba79e04 2018-06-11 stsp if (err)
361 9ba79e04 2018-06-11 stsp return err;
362 c4972b91 2018-05-07 stsp }
363 c4972b91 2018-05-07 stsp
364 9ba79e04 2018-06-11 stsp while (1) {
365 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
366 899d86c2 2018-05-10 stsp
367 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
368 9ba79e04 2018-06-11 stsp if (err) {
369 9ba79e04 2018-06-11 stsp if (err->code == GOT_ERR_ITER_NEED_MORE)
370 9ba79e04 2018-06-11 stsp err = NULL;
371 9ba79e04 2018-06-11 stsp break;
372 9ba79e04 2018-06-11 stsp }
373 899d86c2 2018-05-10 stsp
374 9ba79e04 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
375 9ba79e04 2018-06-11 stsp if (err)
376 9ba79e04 2018-06-11 stsp break;
377 899d86c2 2018-05-10 stsp
378 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
379 9ba79e04 2018-06-11 stsp if (entry == NULL) {
380 9ba79e04 2018-06-11 stsp err = got_error_from_errno();
381 9ba79e04 2018-06-11 stsp break;
382 9ba79e04 2018-06-11 stsp }
383 899d86c2 2018-05-10 stsp
384 9ba79e04 2018-06-11 stsp TAILQ_INSERT_TAIL(commits, entry, entry);
385 899d86c2 2018-05-10 stsp }
386 899d86c2 2018-05-10 stsp
387 9ba79e04 2018-06-11 stsp return err;
388 99db9666 2018-05-07 stsp }
389 99db9666 2018-05-07 stsp
390 99db9666 2018-05-07 stsp static const struct got_error *
391 9ba79e04 2018-06-11 stsp fetch_next_commit(struct commit_queue_entry **pentry,
392 9ba79e04 2018-06-11 stsp struct commit_queue_entry *entry, struct commit_queue *commits,
393 9ba79e04 2018-06-11 stsp struct got_commit_graph *graph, struct got_repository *repo)
394 899d86c2 2018-05-10 stsp {
395 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
396 9ba79e04 2018-06-11 stsp struct got_object_qid *qid;
397 899d86c2 2018-05-10 stsp
398 9ba79e04 2018-06-11 stsp *pentry = NULL;
399 899d86c2 2018-05-10 stsp
400 9ba79e04 2018-06-11 stsp /* Populate commit graph with entry's parent commits. */
401 9ba79e04 2018-06-11 stsp SIMPLEQ_FOREACH(qid, &entry->commit->parent_ids, entry) {
402 9ba79e04 2018-06-11 stsp int nfetched;
403 9ba79e04 2018-06-11 stsp err = got_commit_graph_fetch_commits_up_to(&nfetched,
404 9ba79e04 2018-06-11 stsp graph, qid->id, repo);
405 9ba79e04 2018-06-11 stsp if (err)
406 9ba79e04 2018-06-11 stsp return err;
407 899d86c2 2018-05-10 stsp }
408 899d86c2 2018-05-10 stsp
409 9ba79e04 2018-06-11 stsp /* Append outstanding commits to queue in graph sort order. */
410 9ba79e04 2018-06-11 stsp err = queue_commits(graph, commits, entry->id, repo);
411 9ba79e04 2018-06-11 stsp if (err) {
412 9ba79e04 2018-06-11 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
413 9ba79e04 2018-06-11 stsp err = NULL;
414 9ba79e04 2018-06-11 stsp return err;
415 899d86c2 2018-05-10 stsp }
416 899d86c2 2018-05-10 stsp
417 9ba79e04 2018-06-11 stsp /* Next entry to display should now be available. */
418 9ba79e04 2018-06-11 stsp *pentry = TAILQ_NEXT(entry, entry);
419 9ba79e04 2018-06-11 stsp if (*pentry == NULL)
420 9ba79e04 2018-06-11 stsp return got_error(GOT_ERR_NO_OBJ);
421 899d86c2 2018-05-10 stsp
422 9ba79e04 2018-06-11 stsp return NULL;
423 899d86c2 2018-05-10 stsp }
424 899d86c2 2018-05-10 stsp
425 899d86c2 2018-05-10 stsp static const struct got_error *
426 9ba79e04 2018-06-11 stsp get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
427 99db9666 2018-05-07 stsp {
428 9ba79e04 2018-06-11 stsp const struct got_error *err = NULL;
429 9ba79e04 2018-06-11 stsp struct got_reference *head_ref;
430 99db9666 2018-05-07 stsp
431 9ba79e04 2018-06-11 stsp *head_id = NULL;
432 899d86c2 2018-05-10 stsp
433 9ba79e04 2018-06-11 stsp err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
434 99db9666 2018-05-07 stsp if (err)
435 99db9666 2018-05-07 stsp return err;
436 99db9666 2018-05-07 stsp
437 9ba79e04 2018-06-11 stsp err = got_ref_resolve(head_id, repo, head_ref);
438 9ba79e04 2018-06-11 stsp got_ref_close(head_ref);
439 9ba79e04 2018-06-11 stsp if (err) {
440 9ba79e04 2018-06-11 stsp *head_id = NULL;
441 99db9666 2018-05-07 stsp return err;
442 0553a4e3 2018-04-30 stsp }
443 80ddbec8 2018-04-29 stsp
444 9ba79e04 2018-06-11 stsp return NULL;
445 0553a4e3 2018-04-30 stsp }
446 0553a4e3 2018-04-30 stsp
447 0553a4e3 2018-04-30 stsp static const struct got_error *
448 cd0acaa7 2018-05-20 stsp draw_commits(struct commit_queue_entry **last, struct commit_queue_entry **selected,
449 cd0acaa7 2018-05-20 stsp struct commit_queue_entry *first, int selected_idx, int limit)
450 0553a4e3 2018-04-30 stsp {
451 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
452 0553a4e3 2018-04-30 stsp struct commit_queue_entry *entry;
453 0553a4e3 2018-04-30 stsp int ncommits = 0;
454 0553a4e3 2018-04-30 stsp
455 9c2de6ef 2018-05-20 stsp werase(tog_log_view.window);
456 0553a4e3 2018-04-30 stsp
457 899d86c2 2018-05-10 stsp entry = first;
458 899d86c2 2018-05-10 stsp *last = first;
459 899d86c2 2018-05-10 stsp while (entry) {
460 899d86c2 2018-05-10 stsp if (ncommits == limit)
461 899d86c2 2018-05-10 stsp break;
462 cd0acaa7 2018-05-20 stsp if (ncommits == selected_idx) {
463 0553a4e3 2018-04-30 stsp wstandout(tog_log_view.window);
464 cd0acaa7 2018-05-20 stsp *selected = entry;
465 cd0acaa7 2018-05-20 stsp }
466 0553a4e3 2018-04-30 stsp err = draw_commit(entry->commit, entry->id);
467 cd0acaa7 2018-05-20 stsp if (ncommits == selected_idx)
468 0553a4e3 2018-04-30 stsp wstandend(tog_log_view.window);
469 0553a4e3 2018-04-30 stsp if (err)
470 0553a4e3 2018-04-30 stsp break;
471 0553a4e3 2018-04-30 stsp ncommits++;
472 899d86c2 2018-05-10 stsp *last = entry;
473 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
474 80ddbec8 2018-04-29 stsp }
475 80ddbec8 2018-04-29 stsp
476 80ddbec8 2018-04-29 stsp update_panels();
477 80ddbec8 2018-04-29 stsp doupdate();
478 0553a4e3 2018-04-30 stsp
479 80ddbec8 2018-04-29 stsp return err;
480 9f7d7167 2018-04-29 stsp }
481 07b55e75 2018-05-10 stsp
482 07b55e75 2018-05-10 stsp static void
483 16482c3b 2018-05-20 stsp scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
484 07b55e75 2018-05-10 stsp struct commit_queue *commits)
485 07b55e75 2018-05-10 stsp {
486 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
487 07b55e75 2018-05-10 stsp int nscrolled = 0;
488 07b55e75 2018-05-10 stsp
489 07b55e75 2018-05-10 stsp entry = TAILQ_FIRST(commits);
490 07b55e75 2018-05-10 stsp if (*first_displayed_entry == entry)
491 07b55e75 2018-05-10 stsp return;
492 9f7d7167 2018-04-29 stsp
493 07b55e75 2018-05-10 stsp entry = *first_displayed_entry;
494 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
495 07b55e75 2018-05-10 stsp entry = TAILQ_PREV(entry, commit_queue, entry);
496 07b55e75 2018-05-10 stsp if (entry) {
497 07b55e75 2018-05-10 stsp *first_displayed_entry = entry;
498 07b55e75 2018-05-10 stsp nscrolled++;
499 07b55e75 2018-05-10 stsp }
500 07b55e75 2018-05-10 stsp }
501 aa075928 2018-05-10 stsp }
502 aa075928 2018-05-10 stsp
503 aa075928 2018-05-10 stsp static const struct got_error *
504 16482c3b 2018-05-20 stsp scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
505 aa075928 2018-05-10 stsp struct commit_queue_entry *last_displayed_entry,
506 9ba79e04 2018-06-11 stsp struct commit_queue *commits, struct got_commit_graph *graph,
507 9ba79e04 2018-06-11 stsp struct got_repository *repo)
508 aa075928 2018-05-10 stsp {
509 aa075928 2018-05-10 stsp const struct got_error *err = NULL;
510 dd0a52c1 2018-05-20 stsp struct commit_queue_entry *pentry;
511 aa075928 2018-05-10 stsp int nscrolled = 0;
512 aa075928 2018-05-10 stsp
513 aa075928 2018-05-10 stsp do {
514 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(last_displayed_entry, entry);
515 aa075928 2018-05-10 stsp if (pentry == NULL) {
516 9ba79e04 2018-06-11 stsp err = fetch_next_commit(&pentry, last_displayed_entry,
517 9ba79e04 2018-06-11 stsp commits, graph, repo);
518 9a6bf2a5 2018-05-20 stsp if (err || pentry == NULL)
519 aa075928 2018-05-10 stsp break;
520 aa075928 2018-05-10 stsp }
521 dd0a52c1 2018-05-20 stsp last_displayed_entry = pentry;
522 aa075928 2018-05-10 stsp
523 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(*first_displayed_entry, entry);
524 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
525 dd0a52c1 2018-05-20 stsp break;
526 aa075928 2018-05-10 stsp *first_displayed_entry = pentry;
527 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
528 aa075928 2018-05-10 stsp
529 dd0a52c1 2018-05-20 stsp return err;
530 07b55e75 2018-05-10 stsp }
531 4a7f7875 2018-05-10 stsp
532 4a7f7875 2018-05-10 stsp static int
533 4a7f7875 2018-05-10 stsp num_parents(struct commit_queue_entry *entry)
534 4a7f7875 2018-05-10 stsp {
535 4a7f7875 2018-05-10 stsp int nparents = 0;
536 07b55e75 2018-05-10 stsp
537 4a7f7875 2018-05-10 stsp while (entry) {
538 4a7f7875 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
539 4a7f7875 2018-05-10 stsp nparents++;
540 4a7f7875 2018-05-10 stsp }
541 4a7f7875 2018-05-10 stsp
542 4a7f7875 2018-05-10 stsp return nparents;
543 cd0acaa7 2018-05-20 stsp }
544 cd0acaa7 2018-05-20 stsp
545 cd0acaa7 2018-05-20 stsp static const struct got_error *
546 cd0acaa7 2018-05-20 stsp show_commit(struct commit_queue_entry *entry, struct got_repository *repo)
547 cd0acaa7 2018-05-20 stsp {
548 cd0acaa7 2018-05-20 stsp const struct got_error *err;
549 cd0acaa7 2018-05-20 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
550 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
551 cd0acaa7 2018-05-20 stsp
552 cd0acaa7 2018-05-20 stsp err = got_object_open(&obj2, repo, entry->id);
553 cd0acaa7 2018-05-20 stsp if (err)
554 cd0acaa7 2018-05-20 stsp return err;
555 cd0acaa7 2018-05-20 stsp
556 9ba79e04 2018-06-11 stsp parent_id = SIMPLEQ_FIRST(&entry->commit->parent_ids);
557 9ba79e04 2018-06-11 stsp if (parent_id) {
558 9ba79e04 2018-06-11 stsp err = got_object_open(&obj1, repo, parent_id->id);
559 cd0acaa7 2018-05-20 stsp if (err)
560 cd0acaa7 2018-05-20 stsp goto done;
561 cd0acaa7 2018-05-20 stsp }
562 cd0acaa7 2018-05-20 stsp
563 cd0acaa7 2018-05-20 stsp err = show_diff_view(obj1, obj2, repo);
564 cd0acaa7 2018-05-20 stsp done:
565 cd0acaa7 2018-05-20 stsp if (obj1)
566 cd0acaa7 2018-05-20 stsp got_object_close(obj1);
567 cd0acaa7 2018-05-20 stsp if (obj2)
568 cd0acaa7 2018-05-20 stsp got_object_close(obj2);
569 cd0acaa7 2018-05-20 stsp return err;
570 4a7f7875 2018-05-10 stsp }
571 4a7f7875 2018-05-10 stsp
572 80ddbec8 2018-04-29 stsp static const struct got_error *
573 9343a5fb 2018-06-23 stsp browse_commit(struct commit_queue_entry *entry, struct got_repository *repo)
574 9343a5fb 2018-06-23 stsp {
575 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
576 9343a5fb 2018-06-23 stsp struct got_tree_object *tree;
577 9343a5fb 2018-06-23 stsp
578 9343a5fb 2018-06-23 stsp err = got_object_open_as_tree(&tree, repo, entry->commit->tree_id);
579 9343a5fb 2018-06-23 stsp if (err)
580 9343a5fb 2018-06-23 stsp return err;
581 9343a5fb 2018-06-23 stsp
582 9343a5fb 2018-06-23 stsp err = show_tree_view(tree, entry->id, repo);
583 9343a5fb 2018-06-23 stsp got_object_tree_close(tree);
584 9343a5fb 2018-06-23 stsp return err;
585 9343a5fb 2018-06-23 stsp }
586 9343a5fb 2018-06-23 stsp
587 9343a5fb 2018-06-23 stsp static const struct got_error *
588 80ddbec8 2018-04-29 stsp show_log_view(struct got_object_id *start_id, struct got_repository *repo)
589 80ddbec8 2018-04-29 stsp {
590 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
591 9ba79e04 2018-06-11 stsp struct got_object_id *head_id = NULL;
592 9ba79e04 2018-06-11 stsp int ch, done = 0, selected = 0, nparents, nfetched;
593 9ba79e04 2018-06-11 stsp struct got_commit_graph *graph;
594 0553a4e3 2018-04-30 stsp struct commit_queue commits;
595 9ba79e04 2018-06-11 stsp struct commit_queue_entry *entry = NULL;
596 899d86c2 2018-05-10 stsp struct commit_queue_entry *first_displayed_entry = NULL;
597 899d86c2 2018-05-10 stsp struct commit_queue_entry *last_displayed_entry = NULL;
598 cd0acaa7 2018-05-20 stsp struct commit_queue_entry *selected_entry = NULL;
599 80ddbec8 2018-04-29 stsp
600 80ddbec8 2018-04-29 stsp if (tog_log_view.window == NULL) {
601 80ddbec8 2018-04-29 stsp tog_log_view.window = newwin(0, 0, 0, 0);
602 80ddbec8 2018-04-29 stsp if (tog_log_view.window == NULL)
603 80ddbec8 2018-04-29 stsp return got_error_from_errno();
604 00a838fa 2018-04-29 stsp keypad(tog_log_view.window, TRUE);
605 80ddbec8 2018-04-29 stsp }
606 80ddbec8 2018-04-29 stsp if (tog_log_view.panel == NULL) {
607 80ddbec8 2018-04-29 stsp tog_log_view.panel = new_panel(tog_log_view.window);
608 80ddbec8 2018-04-29 stsp if (tog_log_view.panel == NULL)
609 80ddbec8 2018-04-29 stsp return got_error_from_errno();
610 cd0acaa7 2018-05-20 stsp } else
611 cd0acaa7 2018-05-20 stsp show_panel(tog_log_view.panel);
612 80ddbec8 2018-04-29 stsp
613 9ba79e04 2018-06-11 stsp err = get_head_commit_id(&head_id, repo);
614 9ba79e04 2018-06-11 stsp if (err)
615 9ba79e04 2018-06-11 stsp return err;
616 9ba79e04 2018-06-11 stsp
617 899d86c2 2018-05-10 stsp TAILQ_INIT(&commits);
618 9ba79e04 2018-06-11 stsp
619 5489743f 2018-06-13 stsp err = got_commit_graph_open(&graph, head_id, 0, repo);
620 9ba79e04 2018-06-11 stsp if (err)
621 9ba79e04 2018-06-11 stsp goto done;
622 9ba79e04 2018-06-11 stsp
623 9ba79e04 2018-06-11 stsp /* Populate commit graph with a sufficient number of commits. */
624 9ba79e04 2018-06-11 stsp err = got_commit_graph_fetch_commits_up_to(&nfetched, graph, start_id,
625 9ba79e04 2018-06-11 stsp repo);
626 9ba79e04 2018-06-11 stsp if (err)
627 9ba79e04 2018-06-11 stsp goto done;
628 9ba79e04 2018-06-11 stsp err = got_commit_graph_fetch_commits(&nfetched, graph, LINES, repo);
629 80ddbec8 2018-04-29 stsp if (err)
630 9ba79e04 2018-06-11 stsp goto done;
631 9ba79e04 2018-06-11 stsp
632 9ba79e04 2018-06-11 stsp /*
633 9ba79e04 2018-06-11 stsp * Open the initial batch of commits, sorted in commit graph order.
634 9ba79e04 2018-06-11 stsp * We keep all commits open throughout the lifetime of the log view
635 9ba79e04 2018-06-11 stsp * in order to avoid having to re-fetch commits from disk while
636 9ba79e04 2018-06-11 stsp * updating the display.
637 9ba79e04 2018-06-11 stsp */
638 9ba79e04 2018-06-11 stsp err = queue_commits(graph, &commits, head_id, repo);
639 9ba79e04 2018-06-11 stsp if (err && err->code != GOT_ERR_ITER_COMPLETED)
640 9ba79e04 2018-06-11 stsp goto done;
641 9ba79e04 2018-06-11 stsp
642 9ba79e04 2018-06-11 stsp /* Find entry corresponding to the first commit to display. */
643 9ba79e04 2018-06-11 stsp TAILQ_FOREACH(entry, &commits, entry) {
644 9ba79e04 2018-06-11 stsp if (got_object_id_cmp(entry->id, start_id) == 0) {
645 9ba79e04 2018-06-11 stsp first_displayed_entry = entry;
646 9ba79e04 2018-06-11 stsp break;
647 9ba79e04 2018-06-11 stsp }
648 9ba79e04 2018-06-11 stsp }
649 9ba79e04 2018-06-11 stsp if (first_displayed_entry == NULL) {
650 9ba79e04 2018-06-11 stsp err = got_error(GOT_ERR_NO_OBJ);
651 80ddbec8 2018-04-29 stsp goto done;
652 9ba79e04 2018-06-11 stsp }
653 9ba79e04 2018-06-11 stsp
654 899d86c2 2018-05-10 stsp while (!done) {
655 cd0acaa7 2018-05-20 stsp err = draw_commits(&last_displayed_entry, &selected_entry,
656 cd0acaa7 2018-05-20 stsp first_displayed_entry, selected, LINES);
657 80ddbec8 2018-04-29 stsp if (err)
658 d0f709cb 2018-04-30 stsp goto done;
659 80ddbec8 2018-04-29 stsp
660 80ddbec8 2018-04-29 stsp nodelay(stdscr, FALSE);
661 80ddbec8 2018-04-29 stsp ch = wgetch(tog_log_view.window);
662 f7182337 2018-05-20 stsp nodelay(stdscr, TRUE);
663 80ddbec8 2018-04-29 stsp switch (ch) {
664 31120ada 2018-04-30 stsp case ERR:
665 31120ada 2018-04-30 stsp if (errno) {
666 31120ada 2018-04-30 stsp err = got_error_from_errno();
667 31120ada 2018-04-30 stsp goto done;
668 31120ada 2018-04-30 stsp }
669 31120ada 2018-04-30 stsp break;
670 80ddbec8 2018-04-29 stsp case 'q':
671 80ddbec8 2018-04-29 stsp done = 1;
672 80ddbec8 2018-04-29 stsp break;
673 80ddbec8 2018-04-29 stsp case 'k':
674 80ddbec8 2018-04-29 stsp case KEY_UP:
675 80ddbec8 2018-04-29 stsp if (selected > 0)
676 80ddbec8 2018-04-29 stsp selected--;
677 8ec9698d 2018-05-10 stsp if (selected > 0)
678 d91e25cb 2018-05-10 stsp break;
679 07b55e75 2018-05-10 stsp scroll_up(&first_displayed_entry, 1, &commits);
680 80ddbec8 2018-04-29 stsp break;
681 48531068 2018-05-10 stsp case KEY_PPAGE:
682 dfc1d240 2018-05-10 stsp if (TAILQ_FIRST(&commits) ==
683 dfc1d240 2018-05-10 stsp first_displayed_entry) {
684 dfc1d240 2018-05-10 stsp selected = 0;
685 dfc1d240 2018-05-10 stsp break;
686 dfc1d240 2018-05-10 stsp }
687 48531068 2018-05-10 stsp scroll_up(&first_displayed_entry, LINES,
688 48531068 2018-05-10 stsp &commits);
689 48531068 2018-05-10 stsp break;
690 80ddbec8 2018-04-29 stsp case 'j':
691 80ddbec8 2018-04-29 stsp case KEY_DOWN:
692 80ee4603 2018-05-10 stsp nparents = num_parents(first_displayed_entry);
693 06abe2cd 2018-05-10 stsp if (selected < LINES - 1 &&
694 15c91275 2018-05-20 stsp selected < nparents - 1) {
695 15c91275 2018-05-20 stsp selected++;
696 15c91275 2018-05-20 stsp break;
697 8df4052c 2018-05-20 stsp }
698 15c91275 2018-05-20 stsp err = scroll_down(&first_displayed_entry, 1,
699 9ba79e04 2018-06-11 stsp last_displayed_entry, &commits, graph,
700 9ba79e04 2018-06-11 stsp repo);
701 15c91275 2018-05-20 stsp if (err)
702 15c91275 2018-05-20 stsp goto done;
703 4a7f7875 2018-05-10 stsp break;
704 4a7f7875 2018-05-10 stsp case KEY_NPAGE:
705 e50ee4f1 2018-05-10 stsp err = scroll_down(&first_displayed_entry, LINES,
706 9ba79e04 2018-06-11 stsp last_displayed_entry, &commits, graph,
707 9ba79e04 2018-06-11 stsp repo);
708 899d86c2 2018-05-10 stsp if (err)
709 aa075928 2018-05-10 stsp goto done;
710 dd0a52c1 2018-05-20 stsp if (last_displayed_entry->commit->nparents > 0)
711 dd0a52c1 2018-05-20 stsp break;
712 dd0a52c1 2018-05-20 stsp /* can't scroll any further; move cursor down */
713 4a7f7875 2018-05-10 stsp nparents = num_parents(first_displayed_entry);
714 dd0a52c1 2018-05-20 stsp if (selected < LINES - 1 ||
715 dd0a52c1 2018-05-20 stsp selected < nparents - 1)
716 dd0a52c1 2018-05-20 stsp selected = MIN(LINES - 1, nparents - 1);
717 80ddbec8 2018-04-29 stsp break;
718 d6df9be4 2018-04-30 stsp case KEY_RESIZE:
719 31120ada 2018-04-30 stsp if (selected > LINES)
720 31120ada 2018-04-30 stsp selected = LINES - 1;
721 cd0acaa7 2018-05-20 stsp break;
722 cd0acaa7 2018-05-20 stsp case KEY_ENTER:
723 cd0acaa7 2018-05-20 stsp case '\r':
724 cd0acaa7 2018-05-20 stsp err = show_commit(selected_entry, repo);
725 9343a5fb 2018-06-23 stsp if (err)
726 9343a5fb 2018-06-23 stsp goto done;
727 9343a5fb 2018-06-23 stsp show_panel(tog_log_view.panel);
728 9343a5fb 2018-06-23 stsp break;
729 9343a5fb 2018-06-23 stsp case 't':
730 9343a5fb 2018-06-23 stsp err = browse_commit(selected_entry, repo);
731 cd0acaa7 2018-05-20 stsp if (err)
732 0d4100bb 2018-06-23 stsp goto done;
733 cd0acaa7 2018-05-20 stsp show_panel(tog_log_view.panel);
734 31120ada 2018-04-30 stsp break;
735 80ddbec8 2018-04-29 stsp default:
736 80ddbec8 2018-04-29 stsp break;
737 80ddbec8 2018-04-29 stsp }
738 899d86c2 2018-05-10 stsp }
739 80ddbec8 2018-04-29 stsp done:
740 9ba79e04 2018-06-11 stsp free(head_id);
741 9ba79e04 2018-06-11 stsp if (graph)
742 9ba79e04 2018-06-11 stsp got_commit_graph_close(graph);
743 0553a4e3 2018-04-30 stsp free_commits(&commits);
744 80ddbec8 2018-04-29 stsp return err;
745 80ddbec8 2018-04-29 stsp }
746 80ddbec8 2018-04-29 stsp
747 4ed7e80c 2018-05-20 stsp static const struct got_error *
748 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
749 9f7d7167 2018-04-29 stsp {
750 80ddbec8 2018-04-29 stsp const struct got_error *error;
751 80ddbec8 2018-04-29 stsp struct got_repository *repo;
752 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
753 80ddbec8 2018-04-29 stsp char *repo_path = NULL;
754 80ddbec8 2018-04-29 stsp char *start_commit = NULL;
755 80ddbec8 2018-04-29 stsp int ch;
756 80ddbec8 2018-04-29 stsp
757 80ddbec8 2018-04-29 stsp #ifndef PROFILE
758 80ddbec8 2018-04-29 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
759 80ddbec8 2018-04-29 stsp err(1, "pledge");
760 80ddbec8 2018-04-29 stsp #endif
761 80ddbec8 2018-04-29 stsp
762 80ddbec8 2018-04-29 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
763 80ddbec8 2018-04-29 stsp switch (ch) {
764 80ddbec8 2018-04-29 stsp case 'c':
765 80ddbec8 2018-04-29 stsp start_commit = optarg;
766 80ddbec8 2018-04-29 stsp break;
767 80ddbec8 2018-04-29 stsp default:
768 80ddbec8 2018-04-29 stsp usage();
769 80ddbec8 2018-04-29 stsp /* NOTREACHED */
770 80ddbec8 2018-04-29 stsp }
771 80ddbec8 2018-04-29 stsp }
772 80ddbec8 2018-04-29 stsp
773 80ddbec8 2018-04-29 stsp argc -= optind;
774 80ddbec8 2018-04-29 stsp argv += optind;
775 80ddbec8 2018-04-29 stsp
776 80ddbec8 2018-04-29 stsp if (argc == 0) {
777 80ddbec8 2018-04-29 stsp repo_path = getcwd(NULL, 0);
778 80ddbec8 2018-04-29 stsp if (repo_path == NULL)
779 80ddbec8 2018-04-29 stsp return got_error_from_errno();
780 80ddbec8 2018-04-29 stsp } else if (argc == 1) {
781 80ddbec8 2018-04-29 stsp repo_path = realpath(argv[0], NULL);
782 80ddbec8 2018-04-29 stsp if (repo_path == NULL)
783 80ddbec8 2018-04-29 stsp return got_error_from_errno();
784 80ddbec8 2018-04-29 stsp } else
785 80ddbec8 2018-04-29 stsp usage_log();
786 80ddbec8 2018-04-29 stsp
787 80ddbec8 2018-04-29 stsp error = got_repo_open(&repo, repo_path);
788 80ddbec8 2018-04-29 stsp free(repo_path);
789 80ddbec8 2018-04-29 stsp if (error != NULL)
790 80ddbec8 2018-04-29 stsp return error;
791 80ddbec8 2018-04-29 stsp
792 80ddbec8 2018-04-29 stsp if (start_commit == NULL) {
793 899d86c2 2018-05-10 stsp error = get_head_commit_id(&start_id, repo);
794 80ddbec8 2018-04-29 stsp if (error != NULL)
795 80ddbec8 2018-04-29 stsp return error;
796 80ddbec8 2018-04-29 stsp } else {
797 80ddbec8 2018-04-29 stsp struct got_object *obj;
798 80ddbec8 2018-04-29 stsp error = got_object_open_by_id_str(&obj, repo, start_commit);
799 80ddbec8 2018-04-29 stsp if (error == NULL) {
800 899d86c2 2018-05-10 stsp start_id = got_object_get_id(obj);
801 899d86c2 2018-05-10 stsp if (start_id == NULL)
802 80ddbec8 2018-04-29 stsp error = got_error_from_errno();
803 80ddbec8 2018-04-29 stsp }
804 80ddbec8 2018-04-29 stsp }
805 80ddbec8 2018-04-29 stsp if (error != NULL)
806 80ddbec8 2018-04-29 stsp return error;
807 899d86c2 2018-05-10 stsp error = show_log_view(start_id, repo);
808 899d86c2 2018-05-10 stsp free(start_id);
809 80ddbec8 2018-04-29 stsp got_repo_close(repo);
810 80ddbec8 2018-04-29 stsp return error;
811 9f7d7167 2018-04-29 stsp }
812 9f7d7167 2018-04-29 stsp
813 4ed7e80c 2018-05-20 stsp __dead static void
814 9f7d7167 2018-04-29 stsp usage_diff(void)
815 9f7d7167 2018-04-29 stsp {
816 80ddbec8 2018-04-29 stsp endwin();
817 9f7d7167 2018-04-29 stsp fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
818 9f7d7167 2018-04-29 stsp getprogname());
819 9f7d7167 2018-04-29 stsp exit(1);
820 b304db33 2018-05-20 stsp }
821 b304db33 2018-05-20 stsp
822 b304db33 2018-05-20 stsp static char *
823 b304db33 2018-05-20 stsp parse_next_line(FILE *f, size_t *len)
824 b304db33 2018-05-20 stsp {
825 b304db33 2018-05-20 stsp char *line;
826 b304db33 2018-05-20 stsp size_t linelen;
827 b304db33 2018-05-20 stsp size_t lineno;
828 b304db33 2018-05-20 stsp const char delim[3] = { '\0', '\0', '\0'};
829 b304db33 2018-05-20 stsp
830 b304db33 2018-05-20 stsp line = fparseln(f, &linelen, &lineno, delim, 0);
831 b304db33 2018-05-20 stsp if (len)
832 b304db33 2018-05-20 stsp *len = linelen;
833 b304db33 2018-05-20 stsp return line;
834 26ed57b2 2018-05-19 stsp }
835 26ed57b2 2018-05-19 stsp
836 4ed7e80c 2018-05-20 stsp static const struct got_error *
837 a70480e0 2018-06-23 stsp draw_file(WINDOW *window, FILE *f, int *first_displayed_line,
838 a70480e0 2018-06-23 stsp int *last_displayed_line, int *eof, int max_lines)
839 26ed57b2 2018-05-19 stsp {
840 61e69b96 2018-05-20 stsp const struct got_error *err;
841 26ed57b2 2018-05-19 stsp int nlines = 0, nprinted = 0;
842 b304db33 2018-05-20 stsp char *line;
843 b304db33 2018-05-20 stsp size_t len;
844 61e69b96 2018-05-20 stsp wchar_t *wline;
845 e0b650dd 2018-05-20 stsp int width;
846 26ed57b2 2018-05-19 stsp
847 26ed57b2 2018-05-19 stsp rewind(f);
848 a70480e0 2018-06-23 stsp werase(window);
849 26ed57b2 2018-05-19 stsp
850 26ed57b2 2018-05-19 stsp *eof = 0;
851 26ed57b2 2018-05-19 stsp while (nprinted < max_lines) {
852 b304db33 2018-05-20 stsp line = parse_next_line(f, &len);
853 26ed57b2 2018-05-19 stsp if (line == NULL) {
854 26ed57b2 2018-05-19 stsp *eof = 1;
855 26ed57b2 2018-05-19 stsp break;
856 26ed57b2 2018-05-19 stsp }
857 26ed57b2 2018-05-19 stsp if (++nlines < *first_displayed_line) {
858 26ed57b2 2018-05-19 stsp free(line);
859 26ed57b2 2018-05-19 stsp continue;
860 26ed57b2 2018-05-19 stsp }
861 26ed57b2 2018-05-19 stsp
862 e0b650dd 2018-05-20 stsp err = format_line(&wline, &width, line, COLS);
863 61e69b96 2018-05-20 stsp if (err) {
864 61e69b96 2018-05-20 stsp free(line);
865 61e69b96 2018-05-20 stsp return err;
866 61e69b96 2018-05-20 stsp }
867 a70480e0 2018-06-23 stsp waddwstr(window, wline);
868 e0b650dd 2018-05-20 stsp if (width < COLS)
869 a70480e0 2018-06-23 stsp waddch(window, '\n');
870 26ed57b2 2018-05-19 stsp if (++nprinted == 1)
871 26ed57b2 2018-05-19 stsp *first_displayed_line = nlines;
872 26ed57b2 2018-05-19 stsp free(line);
873 26ed57b2 2018-05-19 stsp }
874 26ed57b2 2018-05-19 stsp *last_displayed_line = nlines;
875 26ed57b2 2018-05-19 stsp
876 26ed57b2 2018-05-19 stsp update_panels();
877 26ed57b2 2018-05-19 stsp doupdate();
878 26ed57b2 2018-05-19 stsp
879 26ed57b2 2018-05-19 stsp return NULL;
880 9f7d7167 2018-04-29 stsp }
881 9f7d7167 2018-04-29 stsp
882 4ed7e80c 2018-05-20 stsp static const struct got_error *
883 26ed57b2 2018-05-19 stsp show_diff_view(struct got_object *obj1, struct got_object *obj2,
884 26ed57b2 2018-05-19 stsp struct got_repository *repo)
885 26ed57b2 2018-05-19 stsp {
886 26ed57b2 2018-05-19 stsp const struct got_error *err;
887 26ed57b2 2018-05-19 stsp FILE *f;
888 26ed57b2 2018-05-19 stsp int ch, done = 0, first_displayed_line = 1, last_displayed_line = LINES;
889 b304db33 2018-05-20 stsp int eof, i;
890 26ed57b2 2018-05-19 stsp
891 cd0acaa7 2018-05-20 stsp if (obj1 != NULL && obj2 != NULL &&
892 cd0acaa7 2018-05-20 stsp got_object_get_type(obj1) != got_object_get_type(obj2))
893 26ed57b2 2018-05-19 stsp return got_error(GOT_ERR_OBJ_TYPE);
894 26ed57b2 2018-05-19 stsp
895 511a516b 2018-05-19 stsp f = got_opentemp();
896 26ed57b2 2018-05-19 stsp if (f == NULL)
897 26ed57b2 2018-05-19 stsp return got_error_from_errno();
898 26ed57b2 2018-05-19 stsp
899 cd0acaa7 2018-05-20 stsp switch (got_object_get_type(obj1 ? obj1 : obj2)) {
900 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
901 11528a82 2018-05-19 stsp err = got_diff_objects_as_blobs(obj1, obj2, repo, f);
902 26ed57b2 2018-05-19 stsp break;
903 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
904 11528a82 2018-05-19 stsp err = got_diff_objects_as_trees(obj1, obj2, repo, f);
905 26ed57b2 2018-05-19 stsp break;
906 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_COMMIT:
907 11528a82 2018-05-19 stsp err = got_diff_objects_as_commits(obj1, obj2, repo, f);
908 26ed57b2 2018-05-19 stsp break;
909 26ed57b2 2018-05-19 stsp default:
910 26ed57b2 2018-05-19 stsp return got_error(GOT_ERR_OBJ_TYPE);
911 26ed57b2 2018-05-19 stsp }
912 26ed57b2 2018-05-19 stsp
913 26ed57b2 2018-05-19 stsp fflush(f);
914 26ed57b2 2018-05-19 stsp
915 26ed57b2 2018-05-19 stsp if (tog_diff_view.window == NULL) {
916 26ed57b2 2018-05-19 stsp tog_diff_view.window = newwin(0, 0, 0, 0);
917 26ed57b2 2018-05-19 stsp if (tog_diff_view.window == NULL)
918 26ed57b2 2018-05-19 stsp return got_error_from_errno();
919 26ed57b2 2018-05-19 stsp keypad(tog_diff_view.window, TRUE);
920 26ed57b2 2018-05-19 stsp }
921 26ed57b2 2018-05-19 stsp if (tog_diff_view.panel == NULL) {
922 26ed57b2 2018-05-19 stsp tog_diff_view.panel = new_panel(tog_diff_view.window);
923 26ed57b2 2018-05-19 stsp if (tog_diff_view.panel == NULL)
924 26ed57b2 2018-05-19 stsp return got_error_from_errno();
925 26ed57b2 2018-05-19 stsp } else
926 26ed57b2 2018-05-19 stsp show_panel(tog_diff_view.panel);
927 26ed57b2 2018-05-19 stsp
928 26ed57b2 2018-05-19 stsp while (!done) {
929 a70480e0 2018-06-23 stsp err = draw_file(tog_diff_view.window, f, &first_displayed_line,
930 a70480e0 2018-06-23 stsp &last_displayed_line, &eof, LINES);
931 26ed57b2 2018-05-19 stsp if (err)
932 26ed57b2 2018-05-19 stsp break;
933 26ed57b2 2018-05-19 stsp nodelay(stdscr, FALSE);
934 26ed57b2 2018-05-19 stsp ch = wgetch(tog_diff_view.window);
935 f7182337 2018-05-20 stsp nodelay(stdscr, TRUE);
936 26ed57b2 2018-05-19 stsp switch (ch) {
937 26ed57b2 2018-05-19 stsp case 'q':
938 26ed57b2 2018-05-19 stsp done = 1;
939 26ed57b2 2018-05-19 stsp break;
940 26ed57b2 2018-05-19 stsp case 'k':
941 26ed57b2 2018-05-19 stsp case KEY_UP:
942 925e6f23 2018-05-20 stsp case KEY_BACKSPACE:
943 26ed57b2 2018-05-19 stsp if (first_displayed_line > 1)
944 b304db33 2018-05-20 stsp first_displayed_line--;
945 b304db33 2018-05-20 stsp break;
946 b304db33 2018-05-20 stsp case KEY_PPAGE:
947 b304db33 2018-05-20 stsp i = 0;
948 d56caa55 2018-05-20 stsp while (i++ < LINES - 1 &&
949 d56caa55 2018-05-20 stsp first_displayed_line > 1)
950 26ed57b2 2018-05-19 stsp first_displayed_line--;
951 26ed57b2 2018-05-19 stsp break;
952 26ed57b2 2018-05-19 stsp case 'j':
953 26ed57b2 2018-05-19 stsp case KEY_DOWN:
954 925e6f23 2018-05-20 stsp case KEY_ENTER:
955 925e6f23 2018-05-20 stsp case '\r':
956 26ed57b2 2018-05-19 stsp if (!eof)
957 26ed57b2 2018-05-19 stsp first_displayed_line++;
958 26ed57b2 2018-05-19 stsp break;
959 b304db33 2018-05-20 stsp case KEY_NPAGE:
960 75e48879 2018-05-20 stsp case ' ':
961 b304db33 2018-05-20 stsp i = 0;
962 b304db33 2018-05-20 stsp while (!eof && i++ < LINES - 1) {
963 b304db33 2018-05-20 stsp char *line = parse_next_line(f, NULL);
964 b304db33 2018-05-20 stsp first_displayed_line++;
965 b304db33 2018-05-20 stsp if (line == NULL)
966 b304db33 2018-05-20 stsp break;
967 b304db33 2018-05-20 stsp }
968 b304db33 2018-05-20 stsp break;
969 26ed57b2 2018-05-19 stsp default:
970 26ed57b2 2018-05-19 stsp break;
971 26ed57b2 2018-05-19 stsp }
972 26ed57b2 2018-05-19 stsp }
973 26ed57b2 2018-05-19 stsp fclose(f);
974 26ed57b2 2018-05-19 stsp return err;
975 26ed57b2 2018-05-19 stsp }
976 26ed57b2 2018-05-19 stsp
977 4ed7e80c 2018-05-20 stsp static const struct got_error *
978 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
979 9f7d7167 2018-04-29 stsp {
980 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
981 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
982 26ed57b2 2018-05-19 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
983 26ed57b2 2018-05-19 stsp char *repo_path = NULL;
984 26ed57b2 2018-05-19 stsp char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
985 26ed57b2 2018-05-19 stsp int ch;
986 26ed57b2 2018-05-19 stsp
987 26ed57b2 2018-05-19 stsp #ifndef PROFILE
988 26ed57b2 2018-05-19 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
989 26ed57b2 2018-05-19 stsp err(1, "pledge");
990 26ed57b2 2018-05-19 stsp #endif
991 26ed57b2 2018-05-19 stsp
992 26ed57b2 2018-05-19 stsp while ((ch = getopt(argc, argv, "")) != -1) {
993 26ed57b2 2018-05-19 stsp switch (ch) {
994 26ed57b2 2018-05-19 stsp default:
995 26ed57b2 2018-05-19 stsp usage();
996 26ed57b2 2018-05-19 stsp /* NOTREACHED */
997 26ed57b2 2018-05-19 stsp }
998 26ed57b2 2018-05-19 stsp }
999 26ed57b2 2018-05-19 stsp
1000 26ed57b2 2018-05-19 stsp argc -= optind;
1001 26ed57b2 2018-05-19 stsp argv += optind;
1002 26ed57b2 2018-05-19 stsp
1003 26ed57b2 2018-05-19 stsp if (argc == 0) {
1004 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
1005 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
1006 26ed57b2 2018-05-19 stsp repo_path = getcwd(NULL, 0);
1007 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
1008 26ed57b2 2018-05-19 stsp return got_error_from_errno();
1009 26ed57b2 2018-05-19 stsp obj_id_str1 = argv[0];
1010 26ed57b2 2018-05-19 stsp obj_id_str2 = argv[1];
1011 26ed57b2 2018-05-19 stsp } else if (argc == 3) {
1012 26ed57b2 2018-05-19 stsp repo_path = realpath(argv[0], NULL);
1013 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
1014 26ed57b2 2018-05-19 stsp return got_error_from_errno();
1015 26ed57b2 2018-05-19 stsp obj_id_str1 = argv[1];
1016 26ed57b2 2018-05-19 stsp obj_id_str2 = argv[2];
1017 26ed57b2 2018-05-19 stsp } else
1018 26ed57b2 2018-05-19 stsp usage_diff();
1019 26ed57b2 2018-05-19 stsp
1020 26ed57b2 2018-05-19 stsp error = got_repo_open(&repo, repo_path);
1021 26ed57b2 2018-05-19 stsp free(repo_path);
1022 26ed57b2 2018-05-19 stsp if (error)
1023 26ed57b2 2018-05-19 stsp goto done;
1024 26ed57b2 2018-05-19 stsp
1025 26ed57b2 2018-05-19 stsp error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
1026 26ed57b2 2018-05-19 stsp if (error)
1027 26ed57b2 2018-05-19 stsp goto done;
1028 26ed57b2 2018-05-19 stsp
1029 26ed57b2 2018-05-19 stsp error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1030 26ed57b2 2018-05-19 stsp if (error)
1031 26ed57b2 2018-05-19 stsp goto done;
1032 26ed57b2 2018-05-19 stsp
1033 26ed57b2 2018-05-19 stsp error = show_diff_view(obj1, obj2, repo);
1034 26ed57b2 2018-05-19 stsp done:
1035 26ed57b2 2018-05-19 stsp got_repo_close(repo);
1036 26ed57b2 2018-05-19 stsp if (obj1)
1037 26ed57b2 2018-05-19 stsp got_object_close(obj1);
1038 26ed57b2 2018-05-19 stsp if (obj2)
1039 26ed57b2 2018-05-19 stsp got_object_close(obj2);
1040 26ed57b2 2018-05-19 stsp return error;
1041 9f7d7167 2018-04-29 stsp }
1042 9f7d7167 2018-04-29 stsp
1043 4ed7e80c 2018-05-20 stsp __dead static void
1044 9f7d7167 2018-04-29 stsp usage_blame(void)
1045 9f7d7167 2018-04-29 stsp {
1046 80ddbec8 2018-04-29 stsp endwin();
1047 a70480e0 2018-06-23 stsp fprintf(stderr, "usage: %s blame [-c commit] [repository-path] path\n",
1048 9f7d7167 2018-04-29 stsp getprogname());
1049 9f7d7167 2018-04-29 stsp exit(1);
1050 9f7d7167 2018-04-29 stsp }
1051 84451b3e 2018-07-10 stsp
1052 84451b3e 2018-07-10 stsp struct tog_blame_line {
1053 84451b3e 2018-07-10 stsp int annotated;
1054 84451b3e 2018-07-10 stsp struct got_object_id *id;
1055 84451b3e 2018-07-10 stsp };
1056 9f7d7167 2018-04-29 stsp
1057 4ed7e80c 2018-05-20 stsp static const struct got_error *
1058 84451b3e 2018-07-10 stsp draw_blame(WINDOW *window, FILE *f, struct tog_blame_line *lines, int nlines,
1059 84451b3e 2018-07-10 stsp int *first_displayed_line, int *last_displayed_line, int *eof,
1060 84451b3e 2018-07-10 stsp int max_lines)
1061 84451b3e 2018-07-10 stsp {
1062 84451b3e 2018-07-10 stsp const struct got_error *err;
1063 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
1064 84451b3e 2018-07-10 stsp char *line;
1065 84451b3e 2018-07-10 stsp size_t len;
1066 84451b3e 2018-07-10 stsp wchar_t *wline;
1067 84451b3e 2018-07-10 stsp int width;
1068 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
1069 84451b3e 2018-07-10 stsp
1070 84451b3e 2018-07-10 stsp rewind(f);
1071 84451b3e 2018-07-10 stsp werase(window);
1072 84451b3e 2018-07-10 stsp
1073 84451b3e 2018-07-10 stsp *eof = 0;
1074 84451b3e 2018-07-10 stsp while (nprinted < max_lines) {
1075 84451b3e 2018-07-10 stsp line = parse_next_line(f, &len);
1076 84451b3e 2018-07-10 stsp if (line == NULL) {
1077 84451b3e 2018-07-10 stsp *eof = 1;
1078 84451b3e 2018-07-10 stsp break;
1079 84451b3e 2018-07-10 stsp }
1080 84451b3e 2018-07-10 stsp if (++lineno < *first_displayed_line) {
1081 84451b3e 2018-07-10 stsp free(line);
1082 84451b3e 2018-07-10 stsp continue;
1083 84451b3e 2018-07-10 stsp }
1084 84451b3e 2018-07-10 stsp
1085 84451b3e 2018-07-10 stsp err = format_line(&wline, &width, line, COLS - 9);
1086 84451b3e 2018-07-10 stsp if (err) {
1087 84451b3e 2018-07-10 stsp free(line);
1088 84451b3e 2018-07-10 stsp return err;
1089 84451b3e 2018-07-10 stsp }
1090 84451b3e 2018-07-10 stsp
1091 84451b3e 2018-07-10 stsp blame_line = &lines[lineno - 1];
1092 84451b3e 2018-07-10 stsp if (blame_line->annotated) {
1093 84451b3e 2018-07-10 stsp char *id_str;
1094 84451b3e 2018-07-10 stsp err = got_object_id_str(&id_str, blame_line->id);
1095 84451b3e 2018-07-10 stsp if (err) {
1096 84451b3e 2018-07-10 stsp free(line);
1097 84451b3e 2018-07-10 stsp return err;
1098 84451b3e 2018-07-10 stsp }
1099 84451b3e 2018-07-10 stsp wprintw(window, "%.8s ", id_str);
1100 84451b3e 2018-07-10 stsp free(id_str);
1101 84451b3e 2018-07-10 stsp } else
1102 84451b3e 2018-07-10 stsp waddstr(window, " ");
1103 84451b3e 2018-07-10 stsp
1104 84451b3e 2018-07-10 stsp waddwstr(window, wline);
1105 84451b3e 2018-07-10 stsp if (width < COLS - 9)
1106 84451b3e 2018-07-10 stsp waddch(window, '\n');
1107 84451b3e 2018-07-10 stsp if (++nprinted == 1)
1108 84451b3e 2018-07-10 stsp *first_displayed_line = lineno;
1109 84451b3e 2018-07-10 stsp free(line);
1110 84451b3e 2018-07-10 stsp }
1111 84451b3e 2018-07-10 stsp *last_displayed_line = lineno;
1112 84451b3e 2018-07-10 stsp
1113 84451b3e 2018-07-10 stsp update_panels();
1114 84451b3e 2018-07-10 stsp doupdate();
1115 84451b3e 2018-07-10 stsp
1116 84451b3e 2018-07-10 stsp return NULL;
1117 84451b3e 2018-07-10 stsp }
1118 84451b3e 2018-07-10 stsp
1119 84451b3e 2018-07-10 stsp struct tog_blame_cb_args {
1120 84451b3e 2018-07-10 stsp pthread_mutex_t *mutex;
1121 84451b3e 2018-07-10 stsp struct tog_blame_line *lines; /* one per line */
1122 84451b3e 2018-07-10 stsp int nlines;
1123 84451b3e 2018-07-10 stsp
1124 84451b3e 2018-07-10 stsp FILE *f;
1125 84451b3e 2018-07-10 stsp WINDOW *window;
1126 84451b3e 2018-07-10 stsp int *first_displayed_line;
1127 84451b3e 2018-07-10 stsp int *last_displayed_line;
1128 84451b3e 2018-07-10 stsp };
1129 84451b3e 2018-07-10 stsp
1130 84451b3e 2018-07-10 stsp static const struct got_error *
1131 84451b3e 2018-07-10 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
1132 84451b3e 2018-07-10 stsp {
1133 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
1134 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
1135 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
1136 84451b3e 2018-07-10 stsp int eof;
1137 84451b3e 2018-07-10 stsp
1138 84451b3e 2018-07-10 stsp if (nlines != a->nlines || lineno < 1 || lineno > a->nlines)
1139 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
1140 84451b3e 2018-07-10 stsp
1141 84451b3e 2018-07-10 stsp if (pthread_mutex_lock(a->mutex) != 0)
1142 84451b3e 2018-07-10 stsp return got_error_from_errno();
1143 84451b3e 2018-07-10 stsp
1144 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
1145 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
1146 84451b3e 2018-07-10 stsp if (line->id == NULL) {
1147 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1148 84451b3e 2018-07-10 stsp goto done;
1149 84451b3e 2018-07-10 stsp }
1150 84451b3e 2018-07-10 stsp line->annotated = 1;
1151 84451b3e 2018-07-10 stsp
1152 84451b3e 2018-07-10 stsp err = draw_blame(a->window, a->f, a->lines, a->nlines,
1153 84451b3e 2018-07-10 stsp a->first_displayed_line, a->last_displayed_line, &eof, LINES);
1154 84451b3e 2018-07-10 stsp done:
1155 84451b3e 2018-07-10 stsp if (pthread_mutex_unlock(a->mutex) != 0)
1156 84451b3e 2018-07-10 stsp return got_error_from_errno();
1157 84451b3e 2018-07-10 stsp return err;
1158 84451b3e 2018-07-10 stsp }
1159 84451b3e 2018-07-10 stsp
1160 84451b3e 2018-07-10 stsp struct tog_blame_thread_args {
1161 84451b3e 2018-07-10 stsp const char *path;
1162 84451b3e 2018-07-10 stsp struct got_object_id *commit_id;
1163 84451b3e 2018-07-10 stsp struct got_repository *repo;
1164 84451b3e 2018-07-10 stsp void *blame_cb_args;
1165 84451b3e 2018-07-10 stsp };
1166 84451b3e 2018-07-10 stsp
1167 84451b3e 2018-07-10 stsp static void *
1168 84451b3e 2018-07-10 stsp blame_thread(void *arg)
1169 84451b3e 2018-07-10 stsp {
1170 84451b3e 2018-07-10 stsp struct tog_blame_thread_args *a = arg;
1171 84451b3e 2018-07-10 stsp return (void *)got_blame_incremental(a->path, a->commit_id, a->repo,
1172 84451b3e 2018-07-10 stsp blame_cb, a->blame_cb_args);
1173 84451b3e 2018-07-10 stsp }
1174 84451b3e 2018-07-10 stsp
1175 84451b3e 2018-07-10 stsp static const struct got_error *
1176 a70480e0 2018-06-23 stsp show_blame_view(const char *path, struct got_object_id *commit_id,
1177 a70480e0 2018-06-23 stsp struct got_repository *repo)
1178 a70480e0 2018-06-23 stsp {
1179 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
1180 a70480e0 2018-06-23 stsp int ch, done = 0, first_displayed_line = 1, last_displayed_line = LINES;
1181 a70480e0 2018-06-23 stsp int eof, i;
1182 84451b3e 2018-07-10 stsp struct got_object *obj = NULL;
1183 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
1184 84451b3e 2018-07-10 stsp FILE *f = NULL;
1185 84451b3e 2018-07-10 stsp size_t filesize, nlines;
1186 84451b3e 2018-07-10 stsp struct tog_blame_line *lines = NULL;
1187 84451b3e 2018-07-10 stsp pthread_t thread = NULL;
1188 84451b3e 2018-07-10 stsp pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
1189 84451b3e 2018-07-10 stsp struct tog_blame_cb_args blame_cb_args;
1190 84451b3e 2018-07-10 stsp struct tog_blame_thread_args blame_thread_args;
1191 a70480e0 2018-06-23 stsp
1192 84451b3e 2018-07-10 stsp err = got_object_open_by_path(&obj, repo, commit_id, path);
1193 84451b3e 2018-07-10 stsp if (err)
1194 84451b3e 2018-07-10 stsp goto done;
1195 84451b3e 2018-07-10 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
1196 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1197 84451b3e 2018-07-10 stsp got_object_close(obj);
1198 84451b3e 2018-07-10 stsp goto done;
1199 84451b3e 2018-07-10 stsp }
1200 a70480e0 2018-06-23 stsp
1201 84451b3e 2018-07-10 stsp err = got_object_blob_open(&blob, repo, obj, 8192);
1202 84451b3e 2018-07-10 stsp got_object_close(obj);
1203 a70480e0 2018-06-23 stsp if (err)
1204 a70480e0 2018-06-23 stsp goto done;
1205 84451b3e 2018-07-10 stsp f = got_opentemp();
1206 84451b3e 2018-07-10 stsp if (f == NULL) {
1207 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1208 84451b3e 2018-07-10 stsp goto done;
1209 84451b3e 2018-07-10 stsp }
1210 84451b3e 2018-07-10 stsp err = got_object_blob_dump_to_file(&filesize, &nlines, f, blob);
1211 84451b3e 2018-07-10 stsp if (err)
1212 84451b3e 2018-07-10 stsp goto done;
1213 a70480e0 2018-06-23 stsp
1214 84451b3e 2018-07-10 stsp lines = calloc(nlines, sizeof(*lines));
1215 84451b3e 2018-07-10 stsp if (lines == NULL) {
1216 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1217 84451b3e 2018-07-10 stsp goto done;
1218 84451b3e 2018-07-10 stsp }
1219 a70480e0 2018-06-23 stsp
1220 a70480e0 2018-06-23 stsp if (tog_blame_view.window == NULL) {
1221 a70480e0 2018-06-23 stsp tog_blame_view.window = newwin(0, 0, 0, 0);
1222 a70480e0 2018-06-23 stsp if (tog_blame_view.window == NULL)
1223 a70480e0 2018-06-23 stsp return got_error_from_errno();
1224 a70480e0 2018-06-23 stsp keypad(tog_blame_view.window, TRUE);
1225 a70480e0 2018-06-23 stsp }
1226 a70480e0 2018-06-23 stsp if (tog_blame_view.panel == NULL) {
1227 a70480e0 2018-06-23 stsp tog_blame_view.panel = new_panel(tog_blame_view.window);
1228 a70480e0 2018-06-23 stsp if (tog_blame_view.panel == NULL)
1229 a70480e0 2018-06-23 stsp return got_error_from_errno();
1230 a70480e0 2018-06-23 stsp } else
1231 a70480e0 2018-06-23 stsp show_panel(tog_blame_view.panel);
1232 a70480e0 2018-06-23 stsp
1233 84451b3e 2018-07-10 stsp if (pthread_mutex_init(&mutex, NULL) != 0) {
1234 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1235 84451b3e 2018-07-10 stsp goto done;
1236 84451b3e 2018-07-10 stsp }
1237 84451b3e 2018-07-10 stsp blame_cb_args.lines = lines;
1238 84451b3e 2018-07-10 stsp blame_cb_args.nlines = nlines;
1239 84451b3e 2018-07-10 stsp blame_cb_args.mutex = &mutex;
1240 84451b3e 2018-07-10 stsp blame_cb_args.f = f;
1241 84451b3e 2018-07-10 stsp blame_cb_args.window = tog_blame_view.window;
1242 84451b3e 2018-07-10 stsp blame_cb_args.first_displayed_line = &first_displayed_line;
1243 84451b3e 2018-07-10 stsp blame_cb_args.last_displayed_line = &last_displayed_line;
1244 84451b3e 2018-07-10 stsp
1245 84451b3e 2018-07-10 stsp blame_thread_args.path = path;
1246 84451b3e 2018-07-10 stsp blame_thread_args.commit_id = commit_id;
1247 84451b3e 2018-07-10 stsp blame_thread_args.repo = repo;
1248 84451b3e 2018-07-10 stsp blame_thread_args.blame_cb_args = &blame_cb_args;
1249 84451b3e 2018-07-10 stsp
1250 84451b3e 2018-07-10 stsp if (pthread_create(&thread, NULL, blame_thread,
1251 84451b3e 2018-07-10 stsp &blame_thread_args) != 0) {
1252 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1253 84451b3e 2018-07-10 stsp goto done;
1254 84451b3e 2018-07-10 stsp }
1255 84451b3e 2018-07-10 stsp
1256 a70480e0 2018-06-23 stsp while (!done) {
1257 84451b3e 2018-07-10 stsp if (pthread_mutex_lock(&mutex) != 0) {
1258 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1259 84451b3e 2018-07-10 stsp goto done;
1260 84451b3e 2018-07-10 stsp }
1261 84451b3e 2018-07-10 stsp err = draw_blame(tog_blame_view.window, f, lines, nlines,
1262 84451b3e 2018-07-10 stsp &first_displayed_line, &last_displayed_line, &eof, LINES);
1263 84451b3e 2018-07-10 stsp if (pthread_mutex_unlock(&mutex) != 0) {
1264 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1265 84451b3e 2018-07-10 stsp goto done;
1266 84451b3e 2018-07-10 stsp }
1267 a70480e0 2018-06-23 stsp if (err)
1268 a70480e0 2018-06-23 stsp break;
1269 a70480e0 2018-06-23 stsp nodelay(stdscr, FALSE);
1270 a70480e0 2018-06-23 stsp ch = wgetch(tog_blame_view.window);
1271 a70480e0 2018-06-23 stsp nodelay(stdscr, TRUE);
1272 84451b3e 2018-07-10 stsp if (pthread_mutex_lock(&mutex) != 0) {
1273 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1274 84451b3e 2018-07-10 stsp goto done;
1275 84451b3e 2018-07-10 stsp }
1276 a70480e0 2018-06-23 stsp switch (ch) {
1277 a70480e0 2018-06-23 stsp case 'q':
1278 a70480e0 2018-06-23 stsp done = 1;
1279 a70480e0 2018-06-23 stsp break;
1280 a70480e0 2018-06-23 stsp case 'k':
1281 a70480e0 2018-06-23 stsp case KEY_UP:
1282 a70480e0 2018-06-23 stsp case KEY_BACKSPACE:
1283 a70480e0 2018-06-23 stsp if (first_displayed_line > 1)
1284 a70480e0 2018-06-23 stsp first_displayed_line--;
1285 a70480e0 2018-06-23 stsp break;
1286 a70480e0 2018-06-23 stsp case KEY_PPAGE:
1287 a70480e0 2018-06-23 stsp i = 0;
1288 a70480e0 2018-06-23 stsp while (i++ < LINES - 1 &&
1289 a70480e0 2018-06-23 stsp first_displayed_line > 1)
1290 a70480e0 2018-06-23 stsp first_displayed_line--;
1291 a70480e0 2018-06-23 stsp break;
1292 a70480e0 2018-06-23 stsp case 'j':
1293 a70480e0 2018-06-23 stsp case KEY_DOWN:
1294 a70480e0 2018-06-23 stsp case KEY_ENTER:
1295 a70480e0 2018-06-23 stsp case '\r':
1296 a70480e0 2018-06-23 stsp if (!eof)
1297 a70480e0 2018-06-23 stsp first_displayed_line++;
1298 a70480e0 2018-06-23 stsp break;
1299 a70480e0 2018-06-23 stsp case KEY_NPAGE:
1300 a70480e0 2018-06-23 stsp case ' ':
1301 a70480e0 2018-06-23 stsp i = 0;
1302 a70480e0 2018-06-23 stsp while (!eof && i++ < LINES - 1) {
1303 a70480e0 2018-06-23 stsp char *line = parse_next_line(f, NULL);
1304 a70480e0 2018-06-23 stsp first_displayed_line++;
1305 a70480e0 2018-06-23 stsp if (line == NULL)
1306 a70480e0 2018-06-23 stsp break;
1307 a70480e0 2018-06-23 stsp }
1308 a70480e0 2018-06-23 stsp break;
1309 a70480e0 2018-06-23 stsp default:
1310 a70480e0 2018-06-23 stsp break;
1311 84451b3e 2018-07-10 stsp }
1312 84451b3e 2018-07-10 stsp if (pthread_mutex_unlock(&mutex) != 0) {
1313 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1314 84451b3e 2018-07-10 stsp goto done;
1315 a70480e0 2018-06-23 stsp }
1316 a70480e0 2018-06-23 stsp }
1317 a70480e0 2018-06-23 stsp done:
1318 84451b3e 2018-07-10 stsp if (blob)
1319 84451b3e 2018-07-10 stsp got_object_blob_close(blob);
1320 84451b3e 2018-07-10 stsp if (f)
1321 84451b3e 2018-07-10 stsp fclose(f);
1322 84451b3e 2018-07-10 stsp free(lines);
1323 84451b3e 2018-07-10 stsp if (thread) {
1324 84451b3e 2018-07-10 stsp if (pthread_join(thread, (void **)&err) != 0)
1325 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1326 84451b3e 2018-07-10 stsp }
1327 a70480e0 2018-06-23 stsp return err;
1328 a70480e0 2018-06-23 stsp }
1329 a70480e0 2018-06-23 stsp
1330 a70480e0 2018-06-23 stsp static const struct got_error *
1331 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
1332 9f7d7167 2018-04-29 stsp {
1333 a70480e0 2018-06-23 stsp const struct got_error *error;
1334 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
1335 a70480e0 2018-06-23 stsp char *repo_path = NULL;
1336 a70480e0 2018-06-23 stsp char *path = NULL;
1337 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
1338 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
1339 a70480e0 2018-06-23 stsp int ch;
1340 a70480e0 2018-06-23 stsp
1341 a70480e0 2018-06-23 stsp #ifndef PROFILE
1342 a70480e0 2018-06-23 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1343 a70480e0 2018-06-23 stsp err(1, "pledge");
1344 a70480e0 2018-06-23 stsp #endif
1345 a70480e0 2018-06-23 stsp
1346 a70480e0 2018-06-23 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
1347 a70480e0 2018-06-23 stsp switch (ch) {
1348 a70480e0 2018-06-23 stsp case 'c':
1349 a70480e0 2018-06-23 stsp commit_id_str = optarg;
1350 a70480e0 2018-06-23 stsp break;
1351 a70480e0 2018-06-23 stsp default:
1352 a70480e0 2018-06-23 stsp usage();
1353 a70480e0 2018-06-23 stsp /* NOTREACHED */
1354 a70480e0 2018-06-23 stsp }
1355 a70480e0 2018-06-23 stsp }
1356 a70480e0 2018-06-23 stsp
1357 a70480e0 2018-06-23 stsp argc -= optind;
1358 a70480e0 2018-06-23 stsp argv += optind;
1359 a70480e0 2018-06-23 stsp
1360 a70480e0 2018-06-23 stsp if (argc == 0) {
1361 a70480e0 2018-06-23 stsp usage_blame();
1362 a70480e0 2018-06-23 stsp } else if (argc == 1) {
1363 a70480e0 2018-06-23 stsp repo_path = getcwd(NULL, 0);
1364 a70480e0 2018-06-23 stsp if (repo_path == NULL)
1365 a70480e0 2018-06-23 stsp return got_error_from_errno();
1366 a70480e0 2018-06-23 stsp path = argv[0];
1367 a70480e0 2018-06-23 stsp } else if (argc == 2) {
1368 a70480e0 2018-06-23 stsp repo_path = realpath(argv[0], NULL);
1369 a70480e0 2018-06-23 stsp if (repo_path == NULL)
1370 a70480e0 2018-06-23 stsp return got_error_from_errno();
1371 a70480e0 2018-06-23 stsp path = argv[1];
1372 a70480e0 2018-06-23 stsp } else
1373 a70480e0 2018-06-23 stsp usage_blame();
1374 a70480e0 2018-06-23 stsp
1375 a70480e0 2018-06-23 stsp error = got_repo_open(&repo, repo_path);
1376 a70480e0 2018-06-23 stsp free(repo_path);
1377 a70480e0 2018-06-23 stsp if (error != NULL)
1378 66b4983c 2018-06-23 stsp return error;
1379 a70480e0 2018-06-23 stsp
1380 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
1381 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
1382 a70480e0 2018-06-23 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1383 a70480e0 2018-06-23 stsp if (error != NULL)
1384 66b4983c 2018-06-23 stsp goto done;
1385 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
1386 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
1387 a70480e0 2018-06-23 stsp } else {
1388 a70480e0 2018-06-23 stsp struct got_object *obj;
1389 a70480e0 2018-06-23 stsp error = got_object_open_by_id_str(&obj, repo, commit_id_str);
1390 a70480e0 2018-06-23 stsp if (error != NULL)
1391 66b4983c 2018-06-23 stsp goto done;
1392 a70480e0 2018-06-23 stsp commit_id = got_object_get_id(obj);
1393 a19e88aa 2018-06-23 stsp if (commit_id == NULL)
1394 66b4983c 2018-06-23 stsp error = got_error_from_errno();
1395 a19e88aa 2018-06-23 stsp got_object_close(obj);
1396 a70480e0 2018-06-23 stsp }
1397 a19e88aa 2018-06-23 stsp if (error != NULL)
1398 a19e88aa 2018-06-23 stsp goto done;
1399 a70480e0 2018-06-23 stsp
1400 a70480e0 2018-06-23 stsp error = show_blame_view(path, commit_id, repo);
1401 a70480e0 2018-06-23 stsp done:
1402 a70480e0 2018-06-23 stsp free(commit_id);
1403 a70480e0 2018-06-23 stsp if (repo)
1404 a70480e0 2018-06-23 stsp got_repo_close(repo);
1405 a70480e0 2018-06-23 stsp return error;
1406 ffd1d5e5 2018-06-23 stsp }
1407 ffd1d5e5 2018-06-23 stsp
1408 ffd1d5e5 2018-06-23 stsp static const struct got_error *
1409 ffd1d5e5 2018-06-23 stsp draw_tree_entries(struct got_tree_entry **first_displayed_entry,
1410 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **last_displayed_entry,
1411 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **selected_entry, int *ndisplayed,
1412 ce52c690 2018-06-23 stsp WINDOW *window, const char *label, const char *parent_path,
1413 ce52c690 2018-06-23 stsp const struct got_tree_entries *entries, int selected, int limit, int isroot)
1414 ffd1d5e5 2018-06-23 stsp {
1415 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
1416 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
1417 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
1418 ffd1d5e5 2018-06-23 stsp int width, n;
1419 ffd1d5e5 2018-06-23 stsp
1420 ffd1d5e5 2018-06-23 stsp *ndisplayed = 0;
1421 ffd1d5e5 2018-06-23 stsp
1422 ffd1d5e5 2018-06-23 stsp werase(window);
1423 ffd1d5e5 2018-06-23 stsp
1424 ffd1d5e5 2018-06-23 stsp if (limit == 0)
1425 ffd1d5e5 2018-06-23 stsp return NULL;
1426 ffd1d5e5 2018-06-23 stsp
1427 ffd1d5e5 2018-06-23 stsp err = format_line(&wline, &width, label, COLS);
1428 ffd1d5e5 2018-06-23 stsp if (err)
1429 ffd1d5e5 2018-06-23 stsp return err;
1430 ffd1d5e5 2018-06-23 stsp waddwstr(window, wline);
1431 ffd1d5e5 2018-06-23 stsp if (width < COLS)
1432 ffd1d5e5 2018-06-23 stsp waddch(window, '\n');
1433 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
1434 ffd1d5e5 2018-06-23 stsp return NULL;
1435 ce52c690 2018-06-23 stsp err = format_line(&wline, &width, parent_path, COLS);
1436 ce52c690 2018-06-23 stsp if (err)
1437 ce52c690 2018-06-23 stsp return err;
1438 ce52c690 2018-06-23 stsp waddwstr(window, wline);
1439 ce52c690 2018-06-23 stsp if (width < COLS)
1440 ce52c690 2018-06-23 stsp waddch(window, '\n');
1441 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
1442 ffd1d5e5 2018-06-23 stsp return NULL;
1443 a1eca9bb 2018-06-23 stsp waddch(window, '\n');
1444 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
1445 a1eca9bb 2018-06-23 stsp return NULL;
1446 ffd1d5e5 2018-06-23 stsp
1447 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
1448 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL) {
1449 ffd1d5e5 2018-06-23 stsp if (selected == 0) {
1450 ffd1d5e5 2018-06-23 stsp wstandout(window);
1451 ffd1d5e5 2018-06-23 stsp *selected_entry = NULL;
1452 ffd1d5e5 2018-06-23 stsp }
1453 ffd1d5e5 2018-06-23 stsp waddstr(window, " ..\n"); /* parent directory */
1454 ffd1d5e5 2018-06-23 stsp if (selected == 0)
1455 ffd1d5e5 2018-06-23 stsp wstandend(window);
1456 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
1457 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
1458 ffd1d5e5 2018-06-23 stsp return NULL;
1459 ffd1d5e5 2018-06-23 stsp n = 1;
1460 ffd1d5e5 2018-06-23 stsp } else {
1461 ffd1d5e5 2018-06-23 stsp n = 0;
1462 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry)
1463 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
1464 ffd1d5e5 2018-06-23 stsp }
1465 ffd1d5e5 2018-06-23 stsp
1466 ffd1d5e5 2018-06-23 stsp while (te) {
1467 ce52c690 2018-06-23 stsp char *line = NULL;
1468 ffd1d5e5 2018-06-23 stsp if (asprintf(&line, " %s%s",
1469 ffd1d5e5 2018-06-23 stsp te->name, S_ISDIR(te->mode) ? "/" : "") == -1)
1470 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
1471 ffd1d5e5 2018-06-23 stsp err = format_line(&wline, &width, line, COLS);
1472 ffd1d5e5 2018-06-23 stsp if (err) {
1473 ffd1d5e5 2018-06-23 stsp free(line);
1474 ffd1d5e5 2018-06-23 stsp break;
1475 ffd1d5e5 2018-06-23 stsp }
1476 ffd1d5e5 2018-06-23 stsp if (n == selected) {
1477 ffd1d5e5 2018-06-23 stsp wstandout(window);
1478 ffd1d5e5 2018-06-23 stsp *selected_entry = te;
1479 ffd1d5e5 2018-06-23 stsp }
1480 ffd1d5e5 2018-06-23 stsp waddwstr(window, wline);
1481 ffd1d5e5 2018-06-23 stsp if (width < COLS)
1482 ffd1d5e5 2018-06-23 stsp waddch(window, '\n');
1483 ffd1d5e5 2018-06-23 stsp if (n == selected)
1484 ffd1d5e5 2018-06-23 stsp wstandend(window);
1485 ffd1d5e5 2018-06-23 stsp free(line);
1486 ffd1d5e5 2018-06-23 stsp n++;
1487 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
1488 ffd1d5e5 2018-06-23 stsp *last_displayed_entry = te;
1489 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
1490 ffd1d5e5 2018-06-23 stsp break;
1491 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
1492 ffd1d5e5 2018-06-23 stsp }
1493 ffd1d5e5 2018-06-23 stsp
1494 ffd1d5e5 2018-06-23 stsp return err;
1495 ffd1d5e5 2018-06-23 stsp }
1496 ffd1d5e5 2018-06-23 stsp
1497 ffd1d5e5 2018-06-23 stsp static void
1498 ffd1d5e5 2018-06-23 stsp tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
1499 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries, int isroot)
1500 ffd1d5e5 2018-06-23 stsp {
1501 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te, *prev;
1502 ffd1d5e5 2018-06-23 stsp int i;
1503 ffd1d5e5 2018-06-23 stsp
1504 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL)
1505 ffd1d5e5 2018-06-23 stsp return;
1506 ffd1d5e5 2018-06-23 stsp
1507 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
1508 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == te) {
1509 ffd1d5e5 2018-06-23 stsp if (!isroot)
1510 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
1511 ffd1d5e5 2018-06-23 stsp return;
1512 ffd1d5e5 2018-06-23 stsp }
1513 ffd1d5e5 2018-06-23 stsp
1514 ffd1d5e5 2018-06-23 stsp /* XXX this is stupid... switch to TAILQ? */
1515 ffd1d5e5 2018-06-23 stsp for (i = 0; i < maxscroll; i++) {
1516 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry) {
1517 ffd1d5e5 2018-06-23 stsp prev = te;
1518 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
1519 ffd1d5e5 2018-06-23 stsp }
1520 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = prev;
1521 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
1522 ffd1d5e5 2018-06-23 stsp }
1523 ffd1d5e5 2018-06-23 stsp if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
1524 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
1525 ffd1d5e5 2018-06-23 stsp }
1526 ffd1d5e5 2018-06-23 stsp
1527 ffd1d5e5 2018-06-23 stsp static void
1528 ffd1d5e5 2018-06-23 stsp tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
1529 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *last_displayed_entry,
1530 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries)
1531 ffd1d5e5 2018-06-23 stsp {
1532 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *next;
1533 ffd1d5e5 2018-06-23 stsp int n = 0;
1534 ffd1d5e5 2018-06-23 stsp
1535 ffd1d5e5 2018-06-23 stsp if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
1536 ffd1d5e5 2018-06-23 stsp return;
1537 ffd1d5e5 2018-06-23 stsp
1538 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry)
1539 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
1540 ffd1d5e5 2018-06-23 stsp else
1541 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_FIRST(&entries->head);
1542 ffd1d5e5 2018-06-23 stsp while (next) {
1543 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = next;
1544 ffd1d5e5 2018-06-23 stsp if (++n >= maxscroll)
1545 ffd1d5e5 2018-06-23 stsp break;
1546 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(next, entry);
1547 ffd1d5e5 2018-06-23 stsp }
1548 ffd1d5e5 2018-06-23 stsp }
1549 ffd1d5e5 2018-06-23 stsp
1550 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree {
1551 d9765a41 2018-06-23 stsp TAILQ_ENTRY(tog_parent_tree) entry;
1552 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree;
1553 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *first_displayed_entry;
1554 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *selected_entry;
1555 ffd1d5e5 2018-06-23 stsp int selected;
1556 ffd1d5e5 2018-06-23 stsp };
1557 ffd1d5e5 2018-06-23 stsp
1558 d9765a41 2018-06-23 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
1559 ffd1d5e5 2018-06-23 stsp
1560 ffd1d5e5 2018-06-23 stsp static const struct got_error *
1561 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
1562 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
1563 ffd1d5e5 2018-06-23 stsp {
1564 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
1565 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
1566 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
1567 ffd1d5e5 2018-06-23 stsp
1568 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
1569 ffd1d5e5 2018-06-23 stsp len += strlen(pt->selected_entry->name) + 1 /* slash */;
1570 ce52c690 2018-06-23 stsp if (te)
1571 ce52c690 2018-06-23 stsp len += strlen(te->name);
1572 ce52c690 2018-06-23 stsp
1573 ce52c690 2018-06-23 stsp *path = calloc(1, len);
1574 ffd1d5e5 2018-06-23 stsp if (path == NULL)
1575 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
1576 ffd1d5e5 2018-06-23 stsp
1577 ce52c690 2018-06-23 stsp (*path)[0] = '/';
1578 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
1579 d9765a41 2018-06-23 stsp while (pt) {
1580 ce52c690 2018-06-23 stsp if (strlcat(*path, pt->selected_entry->name, len) >= len) {
1581 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
1582 cb2ebc8a 2018-06-23 stsp goto done;
1583 cb2ebc8a 2018-06-23 stsp }
1584 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
1585 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
1586 cb2ebc8a 2018-06-23 stsp goto done;
1587 cb2ebc8a 2018-06-23 stsp }
1588 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
1589 ffd1d5e5 2018-06-23 stsp }
1590 ce52c690 2018-06-23 stsp if (te) {
1591 ce52c690 2018-06-23 stsp if (strlcat(*path, te->name, len) >= len) {
1592 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
1593 ce52c690 2018-06-23 stsp goto done;
1594 ce52c690 2018-06-23 stsp }
1595 cb2ebc8a 2018-06-23 stsp }
1596 ce52c690 2018-06-23 stsp done:
1597 ce52c690 2018-06-23 stsp if (err) {
1598 ce52c690 2018-06-23 stsp free(*path);
1599 ce52c690 2018-06-23 stsp *path = NULL;
1600 ce52c690 2018-06-23 stsp }
1601 ce52c690 2018-06-23 stsp return err;
1602 ce52c690 2018-06-23 stsp }
1603 ce52c690 2018-06-23 stsp
1604 ce52c690 2018-06-23 stsp static const struct got_error *
1605 ce52c690 2018-06-23 stsp blame_tree_entry(struct got_tree_entry *te, struct tog_parent_trees *parents,
1606 ce52c690 2018-06-23 stsp struct got_object_id *commit_id, struct got_repository *repo)
1607 ce52c690 2018-06-23 stsp {
1608 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
1609 ce52c690 2018-06-23 stsp char *path;
1610 ce52c690 2018-06-23 stsp
1611 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
1612 ce52c690 2018-06-23 stsp if (err)
1613 ce52c690 2018-06-23 stsp return err;
1614 ffd1d5e5 2018-06-23 stsp
1615 cb2ebc8a 2018-06-23 stsp err = show_blame_view(path, commit_id, repo);
1616 cb2ebc8a 2018-06-23 stsp free(path);
1617 cb2ebc8a 2018-06-23 stsp return err;
1618 ffd1d5e5 2018-06-23 stsp }
1619 ffd1d5e5 2018-06-23 stsp
1620 ffd1d5e5 2018-06-23 stsp static const struct got_error *
1621 ffd1d5e5 2018-06-23 stsp show_tree_view(struct got_tree_object *root, struct got_object_id *commit_id,
1622 ffd1d5e5 2018-06-23 stsp struct got_repository *repo)
1623 ffd1d5e5 2018-06-23 stsp {
1624 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
1625 ffd1d5e5 2018-06-23 stsp int ch, done = 0, selected = 0;
1626 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree = root;
1627 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries;
1628 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *first_displayed_entry = NULL;
1629 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *last_displayed_entry = NULL;
1630 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *selected_entry = NULL;
1631 ffd1d5e5 2018-06-23 stsp char *commit_id_str = NULL, *tree_label = NULL;
1632 ffd1d5e5 2018-06-23 stsp int nentries, ndisplayed;
1633 ffd1d5e5 2018-06-23 stsp struct tog_parent_trees parents;
1634 ffd1d5e5 2018-06-23 stsp
1635 d9765a41 2018-06-23 stsp TAILQ_INIT(&parents);
1636 ffd1d5e5 2018-06-23 stsp
1637 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
1638 ffd1d5e5 2018-06-23 stsp if (err != NULL)
1639 ffd1d5e5 2018-06-23 stsp goto done;
1640 ffd1d5e5 2018-06-23 stsp
1641 ffd1d5e5 2018-06-23 stsp if (asprintf(&tree_label, "tree of commit %s", commit_id_str) == -1) {
1642 ffd1d5e5 2018-06-23 stsp err = got_error_from_errno();
1643 ffd1d5e5 2018-06-23 stsp goto done;
1644 ffd1d5e5 2018-06-23 stsp }
1645 ffd1d5e5 2018-06-23 stsp
1646 ffd1d5e5 2018-06-23 stsp if (tog_tree_view.window == NULL) {
1647 ffd1d5e5 2018-06-23 stsp tog_tree_view.window = newwin(0, 0, 0, 0);
1648 ffd1d5e5 2018-06-23 stsp if (tog_tree_view.window == NULL)
1649 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
1650 ffd1d5e5 2018-06-23 stsp keypad(tog_tree_view.window, TRUE);
1651 ffd1d5e5 2018-06-23 stsp }
1652 ffd1d5e5 2018-06-23 stsp if (tog_tree_view.panel == NULL) {
1653 ffd1d5e5 2018-06-23 stsp tog_tree_view.panel = new_panel(tog_tree_view.window);
1654 ffd1d5e5 2018-06-23 stsp if (tog_tree_view.panel == NULL)
1655 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
1656 ffd1d5e5 2018-06-23 stsp } else
1657 ffd1d5e5 2018-06-23 stsp show_panel(tog_tree_view.panel);
1658 ffd1d5e5 2018-06-23 stsp
1659 ffd1d5e5 2018-06-23 stsp entries = got_object_tree_get_entries(root);
1660 ffd1d5e5 2018-06-23 stsp first_displayed_entry = SIMPLEQ_FIRST(&entries->head);
1661 ffd1d5e5 2018-06-23 stsp while (!done) {
1662 ce52c690 2018-06-23 stsp char *parent_path;
1663 ffd1d5e5 2018-06-23 stsp entries = got_object_tree_get_entries(tree);
1664 ffd1d5e5 2018-06-23 stsp nentries = entries->nentries;
1665 ffd1d5e5 2018-06-23 stsp if (tree != root)
1666 ffd1d5e5 2018-06-23 stsp nentries++; /* '..' directory */
1667 ce52c690 2018-06-23 stsp
1668 ce52c690 2018-06-23 stsp err = tree_entry_path(&parent_path, &parents, NULL);
1669 ce52c690 2018-06-23 stsp if (err)
1670 ce52c690 2018-06-23 stsp goto done;
1671 ffd1d5e5 2018-06-23 stsp
1672 ffd1d5e5 2018-06-23 stsp err = draw_tree_entries(&first_displayed_entry,
1673 ffd1d5e5 2018-06-23 stsp &last_displayed_entry, &selected_entry, &ndisplayed,
1674 ce52c690 2018-06-23 stsp tog_tree_view.window, tree_label, parent_path, entries,
1675 ce52c690 2018-06-23 stsp selected, LINES, tree == root);
1676 ce52c690 2018-06-23 stsp free(parent_path);
1677 ffd1d5e5 2018-06-23 stsp if (err)
1678 ffd1d5e5 2018-06-23 stsp break;
1679 ffd1d5e5 2018-06-23 stsp
1680 ffd1d5e5 2018-06-23 stsp nodelay(stdscr, FALSE);
1681 ffd1d5e5 2018-06-23 stsp ch = wgetch(tog_tree_view.window);
1682 ffd1d5e5 2018-06-23 stsp nodelay(stdscr, TRUE);
1683 ffd1d5e5 2018-06-23 stsp switch (ch) {
1684 ffd1d5e5 2018-06-23 stsp case 'q':
1685 ffd1d5e5 2018-06-23 stsp done = 1;
1686 ffd1d5e5 2018-06-23 stsp break;
1687 ffd1d5e5 2018-06-23 stsp case 'k':
1688 ffd1d5e5 2018-06-23 stsp case KEY_UP:
1689 ffd1d5e5 2018-06-23 stsp if (selected > 0)
1690 ffd1d5e5 2018-06-23 stsp selected--;
1691 ffd1d5e5 2018-06-23 stsp if (selected > 0)
1692 ffd1d5e5 2018-06-23 stsp break;
1693 ffd1d5e5 2018-06-23 stsp tree_scroll_up(&first_displayed_entry, 1,
1694 ffd1d5e5 2018-06-23 stsp entries, tree == root);
1695 ffd1d5e5 2018-06-23 stsp break;
1696 ffd1d5e5 2018-06-23 stsp case KEY_PPAGE:
1697 ffd1d5e5 2018-06-23 stsp if (SIMPLEQ_FIRST(&entries->head) ==
1698 ffd1d5e5 2018-06-23 stsp first_displayed_entry) {
1699 cf8f1261 2018-06-23 stsp if (tree != root)
1700 cf8f1261 2018-06-23 stsp first_displayed_entry = NULL;
1701 ffd1d5e5 2018-06-23 stsp selected = 0;
1702 ffd1d5e5 2018-06-23 stsp break;
1703 ffd1d5e5 2018-06-23 stsp }
1704 ffd1d5e5 2018-06-23 stsp tree_scroll_up(&first_displayed_entry, LINES,
1705 ffd1d5e5 2018-06-23 stsp entries, tree == root);
1706 ffd1d5e5 2018-06-23 stsp break;
1707 ffd1d5e5 2018-06-23 stsp case 'j':
1708 ffd1d5e5 2018-06-23 stsp case KEY_DOWN:
1709 ffd1d5e5 2018-06-23 stsp if (selected < ndisplayed - 1) {
1710 ffd1d5e5 2018-06-23 stsp selected++;
1711 ffd1d5e5 2018-06-23 stsp break;
1712 ffd1d5e5 2018-06-23 stsp }
1713 ffd1d5e5 2018-06-23 stsp tree_scroll_down(&first_displayed_entry, 1,
1714 ffd1d5e5 2018-06-23 stsp last_displayed_entry, entries);
1715 ffd1d5e5 2018-06-23 stsp break;
1716 ffd1d5e5 2018-06-23 stsp case KEY_NPAGE:
1717 ffd1d5e5 2018-06-23 stsp tree_scroll_down(&first_displayed_entry, LINES,
1718 ffd1d5e5 2018-06-23 stsp last_displayed_entry, entries);
1719 ffd1d5e5 2018-06-23 stsp if (SIMPLEQ_NEXT(last_displayed_entry, entry))
1720 ffd1d5e5 2018-06-23 stsp break;
1721 ffd1d5e5 2018-06-23 stsp /* can't scroll any further; move cursor down */
1722 ffd1d5e5 2018-06-23 stsp if (selected < ndisplayed - 1)
1723 ffd1d5e5 2018-06-23 stsp selected = ndisplayed - 1;
1724 ffd1d5e5 2018-06-23 stsp break;
1725 ffd1d5e5 2018-06-23 stsp case KEY_ENTER:
1726 ffd1d5e5 2018-06-23 stsp case '\r':
1727 ffd1d5e5 2018-06-23 stsp if (selected_entry == NULL) {
1728 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *parent;
1729 ffd1d5e5 2018-06-23 stsp case KEY_BACKSPACE:
1730 ffd1d5e5 2018-06-23 stsp /* user selected '..' */
1731 ffd1d5e5 2018-06-23 stsp if (tree == root)
1732 ffd1d5e5 2018-06-23 stsp break;
1733 d9765a41 2018-06-23 stsp parent = TAILQ_FIRST(&parents);
1734 d9765a41 2018-06-23 stsp TAILQ_REMOVE(&parents, parent, entry);
1735 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
1736 ffd1d5e5 2018-06-23 stsp tree = parent->tree;
1737 ffd1d5e5 2018-06-23 stsp first_displayed_entry =
1738 ffd1d5e5 2018-06-23 stsp parent->first_displayed_entry;
1739 ffd1d5e5 2018-06-23 stsp selected_entry = parent->selected_entry;
1740 ffd1d5e5 2018-06-23 stsp selected = parent->selected;
1741 ffd1d5e5 2018-06-23 stsp free(parent);
1742 ffd1d5e5 2018-06-23 stsp } else if (S_ISDIR(selected_entry->mode)) {
1743 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *parent;
1744 ffd1d5e5 2018-06-23 stsp struct got_tree_object *child;
1745 ffd1d5e5 2018-06-23 stsp err = got_object_open_as_tree(
1746 ffd1d5e5 2018-06-23 stsp &child, repo, selected_entry->id);
1747 ffd1d5e5 2018-06-23 stsp if (err)
1748 ffd1d5e5 2018-06-23 stsp goto done;
1749 ffd1d5e5 2018-06-23 stsp parent = calloc(1, sizeof(*parent));
1750 ffd1d5e5 2018-06-23 stsp if (parent == NULL) {
1751 ffd1d5e5 2018-06-23 stsp err = got_error_from_errno();
1752 ffd1d5e5 2018-06-23 stsp goto done;
1753 ffd1d5e5 2018-06-23 stsp }
1754 ffd1d5e5 2018-06-23 stsp parent->tree = tree;
1755 ffd1d5e5 2018-06-23 stsp parent->first_displayed_entry =
1756 ffd1d5e5 2018-06-23 stsp first_displayed_entry;
1757 ffd1d5e5 2018-06-23 stsp parent->selected_entry = selected_entry;
1758 ffd1d5e5 2018-06-23 stsp parent->selected = selected;
1759 d9765a41 2018-06-23 stsp TAILQ_INSERT_HEAD(&parents, parent,
1760 ffd1d5e5 2018-06-23 stsp entry);
1761 ffd1d5e5 2018-06-23 stsp tree = child;
1762 ffd1d5e5 2018-06-23 stsp selected = 0;
1763 ffd1d5e5 2018-06-23 stsp first_displayed_entry = NULL;
1764 ffd1d5e5 2018-06-23 stsp } else if (S_ISREG(selected_entry->mode)) {
1765 ffd1d5e5 2018-06-23 stsp err = blame_tree_entry(selected_entry,
1766 ffd1d5e5 2018-06-23 stsp &parents, commit_id, repo);
1767 ffd1d5e5 2018-06-23 stsp if (err)
1768 ffd1d5e5 2018-06-23 stsp goto done;
1769 ffd1d5e5 2018-06-23 stsp }
1770 ffd1d5e5 2018-06-23 stsp break;
1771 ffd1d5e5 2018-06-23 stsp case KEY_RESIZE:
1772 ffd1d5e5 2018-06-23 stsp if (selected > LINES)
1773 ffd1d5e5 2018-06-23 stsp selected = ndisplayed - 1;
1774 ffd1d5e5 2018-06-23 stsp break;
1775 ffd1d5e5 2018-06-23 stsp default:
1776 ffd1d5e5 2018-06-23 stsp break;
1777 ffd1d5e5 2018-06-23 stsp }
1778 ffd1d5e5 2018-06-23 stsp }
1779 ffd1d5e5 2018-06-23 stsp done:
1780 ffd1d5e5 2018-06-23 stsp free(tree_label);
1781 ffd1d5e5 2018-06-23 stsp free(commit_id_str);
1782 d9765a41 2018-06-23 stsp while (!TAILQ_EMPTY(&parents)) {
1783 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *parent;
1784 d9765a41 2018-06-23 stsp parent = TAILQ_FIRST(&parents);
1785 d9765a41 2018-06-23 stsp TAILQ_REMOVE(&parents, parent, entry);
1786 ffd1d5e5 2018-06-23 stsp free(parent);
1787 ffd1d5e5 2018-06-23 stsp
1788 ffd1d5e5 2018-06-23 stsp }
1789 ffd1d5e5 2018-06-23 stsp if (tree != root)
1790 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
1791 ffd1d5e5 2018-06-23 stsp return err;
1792 9f7d7167 2018-04-29 stsp }
1793 9f7d7167 2018-04-29 stsp
1794 ffd1d5e5 2018-06-23 stsp __dead static void
1795 ffd1d5e5 2018-06-23 stsp usage_tree(void)
1796 ffd1d5e5 2018-06-23 stsp {
1797 ffd1d5e5 2018-06-23 stsp endwin();
1798 ffd1d5e5 2018-06-23 stsp fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
1799 ffd1d5e5 2018-06-23 stsp getprogname());
1800 ffd1d5e5 2018-06-23 stsp exit(1);
1801 ffd1d5e5 2018-06-23 stsp }
1802 ffd1d5e5 2018-06-23 stsp
1803 ffd1d5e5 2018-06-23 stsp static const struct got_error *
1804 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
1805 ffd1d5e5 2018-06-23 stsp {
1806 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
1807 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
1808 ffd1d5e5 2018-06-23 stsp char *repo_path = NULL;
1809 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
1810 ffd1d5e5 2018-06-23 stsp char *commit_id_arg = NULL;
1811 ffd1d5e5 2018-06-23 stsp struct got_commit_object *commit = NULL;
1812 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree = NULL;
1813 ffd1d5e5 2018-06-23 stsp int ch;
1814 ffd1d5e5 2018-06-23 stsp
1815 ffd1d5e5 2018-06-23 stsp #ifndef PROFILE
1816 ffd1d5e5 2018-06-23 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1817 ffd1d5e5 2018-06-23 stsp err(1, "pledge");
1818 ffd1d5e5 2018-06-23 stsp #endif
1819 ffd1d5e5 2018-06-23 stsp
1820 ffd1d5e5 2018-06-23 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
1821 ffd1d5e5 2018-06-23 stsp switch (ch) {
1822 ffd1d5e5 2018-06-23 stsp case 'c':
1823 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
1824 ffd1d5e5 2018-06-23 stsp break;
1825 ffd1d5e5 2018-06-23 stsp default:
1826 ffd1d5e5 2018-06-23 stsp usage();
1827 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
1828 ffd1d5e5 2018-06-23 stsp }
1829 ffd1d5e5 2018-06-23 stsp }
1830 ffd1d5e5 2018-06-23 stsp
1831 ffd1d5e5 2018-06-23 stsp argc -= optind;
1832 ffd1d5e5 2018-06-23 stsp argv += optind;
1833 ffd1d5e5 2018-06-23 stsp
1834 ffd1d5e5 2018-06-23 stsp if (argc == 0) {
1835 ffd1d5e5 2018-06-23 stsp repo_path = getcwd(NULL, 0);
1836 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
1837 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
1838 ffd1d5e5 2018-06-23 stsp } else if (argc == 1) {
1839 ffd1d5e5 2018-06-23 stsp repo_path = realpath(argv[0], NULL);
1840 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
1841 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
1842 ffd1d5e5 2018-06-23 stsp } else
1843 ffd1d5e5 2018-06-23 stsp usage_log();
1844 ffd1d5e5 2018-06-23 stsp
1845 ffd1d5e5 2018-06-23 stsp error = got_repo_open(&repo, repo_path);
1846 ffd1d5e5 2018-06-23 stsp free(repo_path);
1847 ffd1d5e5 2018-06-23 stsp if (error != NULL)
1848 ffd1d5e5 2018-06-23 stsp return error;
1849 ffd1d5e5 2018-06-23 stsp
1850 ffd1d5e5 2018-06-23 stsp if (commit_id_arg == NULL) {
1851 ffd1d5e5 2018-06-23 stsp error = get_head_commit_id(&commit_id, repo);
1852 ffd1d5e5 2018-06-23 stsp if (error != NULL)
1853 ffd1d5e5 2018-06-23 stsp goto done;
1854 ffd1d5e5 2018-06-23 stsp } else {
1855 ffd1d5e5 2018-06-23 stsp struct got_object *obj;
1856 ffd1d5e5 2018-06-23 stsp error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
1857 ffd1d5e5 2018-06-23 stsp if (error == NULL) {
1858 ffd1d5e5 2018-06-23 stsp commit_id = got_object_get_id(obj);
1859 ffd1d5e5 2018-06-23 stsp if (commit_id == NULL)
1860 ffd1d5e5 2018-06-23 stsp error = got_error_from_errno();
1861 ffd1d5e5 2018-06-23 stsp }
1862 ffd1d5e5 2018-06-23 stsp }
1863 ffd1d5e5 2018-06-23 stsp if (error != NULL)
1864 ffd1d5e5 2018-06-23 stsp goto done;
1865 ffd1d5e5 2018-06-23 stsp
1866 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
1867 ffd1d5e5 2018-06-23 stsp if (error != NULL)
1868 ffd1d5e5 2018-06-23 stsp goto done;
1869 ffd1d5e5 2018-06-23 stsp
1870 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_tree(&tree, repo, commit->tree_id);
1871 ffd1d5e5 2018-06-23 stsp if (error != NULL)
1872 ffd1d5e5 2018-06-23 stsp goto done;
1873 ffd1d5e5 2018-06-23 stsp
1874 ffd1d5e5 2018-06-23 stsp error = show_tree_view(tree, commit_id, repo);
1875 ffd1d5e5 2018-06-23 stsp done:
1876 ffd1d5e5 2018-06-23 stsp free(commit_id);
1877 ffd1d5e5 2018-06-23 stsp if (commit)
1878 ffd1d5e5 2018-06-23 stsp got_object_commit_close(commit);
1879 ffd1d5e5 2018-06-23 stsp if (tree)
1880 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
1881 ffd1d5e5 2018-06-23 stsp if (repo)
1882 ffd1d5e5 2018-06-23 stsp got_repo_close(repo);
1883 ffd1d5e5 2018-06-23 stsp return error;
1884 ffd1d5e5 2018-06-23 stsp }
1885 5c5136c5 2018-05-20 stsp static void
1886 9f7d7167 2018-04-29 stsp init_curses(void)
1887 9f7d7167 2018-04-29 stsp {
1888 9f7d7167 2018-04-29 stsp initscr();
1889 9f7d7167 2018-04-29 stsp cbreak();
1890 9f7d7167 2018-04-29 stsp noecho();
1891 9f7d7167 2018-04-29 stsp nonl();
1892 9f7d7167 2018-04-29 stsp intrflush(stdscr, FALSE);
1893 9f7d7167 2018-04-29 stsp keypad(stdscr, TRUE);
1894 1f475ad8 2018-05-10 stsp curs_set(0);
1895 9f7d7167 2018-04-29 stsp }
1896 9f7d7167 2018-04-29 stsp
1897 4ed7e80c 2018-05-20 stsp __dead static void
1898 9f7d7167 2018-04-29 stsp usage(void)
1899 9f7d7167 2018-04-29 stsp {
1900 9f7d7167 2018-04-29 stsp int i;
1901 9f7d7167 2018-04-29 stsp
1902 c2301be8 2018-04-30 stsp fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
1903 9f7d7167 2018-04-29 stsp "Available commands:\n", getprogname());
1904 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
1905 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = &tog_commands[i];
1906 c2301be8 2018-04-30 stsp fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
1907 9f7d7167 2018-04-29 stsp }
1908 9f7d7167 2018-04-29 stsp exit(1);
1909 9f7d7167 2018-04-29 stsp }
1910 9f7d7167 2018-04-29 stsp
1911 c2301be8 2018-04-30 stsp static char **
1912 c2301be8 2018-04-30 stsp make_argv(const char *arg0, const char *arg1)
1913 c2301be8 2018-04-30 stsp {
1914 c2301be8 2018-04-30 stsp char **argv;
1915 c2301be8 2018-04-30 stsp int argc = (arg1 == NULL ? 1 : 2);
1916 c2301be8 2018-04-30 stsp
1917 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
1918 c2301be8 2018-04-30 stsp if (argv == NULL)
1919 c2301be8 2018-04-30 stsp err(1, "calloc");
1920 c2301be8 2018-04-30 stsp argv[0] = strdup(arg0);
1921 c2301be8 2018-04-30 stsp if (argv[0] == NULL)
1922 c2301be8 2018-04-30 stsp err(1, "calloc");
1923 c2301be8 2018-04-30 stsp if (arg1) {
1924 c2301be8 2018-04-30 stsp argv[1] = strdup(arg1);
1925 c2301be8 2018-04-30 stsp if (argv[1] == NULL)
1926 c2301be8 2018-04-30 stsp err(1, "calloc");
1927 c2301be8 2018-04-30 stsp }
1928 c2301be8 2018-04-30 stsp
1929 c2301be8 2018-04-30 stsp return argv;
1930 c2301be8 2018-04-30 stsp }
1931 c2301be8 2018-04-30 stsp
1932 9f7d7167 2018-04-29 stsp int
1933 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
1934 9f7d7167 2018-04-29 stsp {
1935 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
1936 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
1937 9f7d7167 2018-04-29 stsp int ch, hflag = 0;
1938 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
1939 9f7d7167 2018-04-29 stsp
1940 9f7d7167 2018-04-29 stsp setlocale(LC_ALL, "");
1941 9f7d7167 2018-04-29 stsp
1942 9f7d7167 2018-04-29 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
1943 9f7d7167 2018-04-29 stsp switch (ch) {
1944 9f7d7167 2018-04-29 stsp case 'h':
1945 9f7d7167 2018-04-29 stsp hflag = 1;
1946 9f7d7167 2018-04-29 stsp break;
1947 9f7d7167 2018-04-29 stsp default:
1948 9f7d7167 2018-04-29 stsp usage();
1949 9f7d7167 2018-04-29 stsp /* NOTREACHED */
1950 9f7d7167 2018-04-29 stsp }
1951 9f7d7167 2018-04-29 stsp }
1952 9f7d7167 2018-04-29 stsp
1953 9f7d7167 2018-04-29 stsp argc -= optind;
1954 9f7d7167 2018-04-29 stsp argv += optind;
1955 9f7d7167 2018-04-29 stsp optind = 0;
1956 c2301be8 2018-04-30 stsp optreset = 1;
1957 9f7d7167 2018-04-29 stsp
1958 c2301be8 2018-04-30 stsp if (argc == 0) {
1959 f29d3e89 2018-06-23 stsp if (hflag)
1960 f29d3e89 2018-06-23 stsp usage();
1961 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
1962 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
1963 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, NULL);
1964 c2301be8 2018-04-30 stsp argc = 1;
1965 c2301be8 2018-04-30 stsp } else {
1966 9f7d7167 2018-04-29 stsp int i;
1967 9f7d7167 2018-04-29 stsp
1968 c2301be8 2018-04-30 stsp /* Did the user specific a command? */
1969 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
1970 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
1971 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
1972 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
1973 9f7d7167 2018-04-29 stsp if (hflag)
1974 9f7d7167 2018-04-29 stsp tog_commands[i].cmd_usage();
1975 9f7d7167 2018-04-29 stsp break;
1976 9f7d7167 2018-04-29 stsp }
1977 9f7d7167 2018-04-29 stsp }
1978 9f7d7167 2018-04-29 stsp if (cmd == NULL) {
1979 c2301be8 2018-04-30 stsp /* Did the user specify a repository? */
1980 c2301be8 2018-04-30 stsp char *repo_path = realpath(argv[0], NULL);
1981 c2301be8 2018-04-30 stsp if (repo_path) {
1982 c2301be8 2018-04-30 stsp struct got_repository *repo;
1983 c2301be8 2018-04-30 stsp error = got_repo_open(&repo, repo_path);
1984 c2301be8 2018-04-30 stsp if (error == NULL)
1985 c2301be8 2018-04-30 stsp got_repo_close(repo);
1986 c2301be8 2018-04-30 stsp } else
1987 ad7de8d9 2018-04-30 stsp error = got_error_from_errno();
1988 c2301be8 2018-04-30 stsp if (error) {
1989 f29d3e89 2018-06-23 stsp if (hflag) {
1990 f29d3e89 2018-06-23 stsp fprintf(stderr, "%s: '%s' is not a "
1991 f29d3e89 2018-06-23 stsp "known command\n", getprogname(),
1992 f29d3e89 2018-06-23 stsp argv[0]);
1993 f29d3e89 2018-06-23 stsp usage();
1994 f29d3e89 2018-06-23 stsp }
1995 ad7de8d9 2018-04-30 stsp fprintf(stderr, "%s: '%s' is neither a known "
1996 ad7de8d9 2018-04-30 stsp "command nor a path to a repository\n",
1997 c2301be8 2018-04-30 stsp getprogname(), argv[0]);
1998 ad7de8d9 2018-04-30 stsp free(repo_path);
1999 c2301be8 2018-04-30 stsp return 1;
2000 c2301be8 2018-04-30 stsp }
2001 c2301be8 2018-04-30 stsp cmd = &tog_commands[0];
2002 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, repo_path);
2003 c2301be8 2018-04-30 stsp argc = 2;
2004 c2301be8 2018-04-30 stsp free(repo_path);
2005 9f7d7167 2018-04-29 stsp }
2006 9f7d7167 2018-04-29 stsp }
2007 9f7d7167 2018-04-29 stsp
2008 5c5136c5 2018-05-20 stsp init_curses();
2009 9f7d7167 2018-04-29 stsp
2010 c2301be8 2018-04-30 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
2011 9f7d7167 2018-04-29 stsp if (error)
2012 9f7d7167 2018-04-29 stsp goto done;
2013 9f7d7167 2018-04-29 stsp done:
2014 9f7d7167 2018-04-29 stsp endwin();
2015 c2301be8 2018-04-30 stsp free(cmd_argv);
2016 9f7d7167 2018-04-29 stsp if (error)
2017 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
2018 9f7d7167 2018-04-29 stsp return 0;
2019 9f7d7167 2018-04-29 stsp }