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