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