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