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