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