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 80ddbec8 2018-04-29 stsp
19 31120ada 2018-04-30 stsp #include <errno.h>
20 61e69b96 2018-05-20 stsp #define _XOPEN_SOURCE_EXTENDED
21 9f7d7167 2018-04-29 stsp #include <curses.h>
22 61e69b96 2018-05-20 stsp #undef _XOPEN_SOURCE_EXTENDED
23 9f7d7167 2018-04-29 stsp #include <panel.h>
24 9f7d7167 2018-04-29 stsp #include <locale.h>
25 9f7d7167 2018-04-29 stsp #include <stdlib.h>
26 26ed57b2 2018-05-19 stsp #include <stdio.h>
27 9f7d7167 2018-04-29 stsp #include <getopt.h>
28 9f7d7167 2018-04-29 stsp #include <string.h>
29 9f7d7167 2018-04-29 stsp #include <err.h>
30 80ddbec8 2018-04-29 stsp #include <unistd.h>
31 26ed57b2 2018-05-19 stsp #include <util.h>
32 26ed57b2 2018-05-19 stsp #include <limits.h>
33 61e69b96 2018-05-20 stsp #include <wchar.h>
34 9f7d7167 2018-04-29 stsp
35 9f7d7167 2018-04-29 stsp #include "got_error.h"
36 80ddbec8 2018-04-29 stsp #include "got_object.h"
37 80ddbec8 2018-04-29 stsp #include "got_reference.h"
38 80ddbec8 2018-04-29 stsp #include "got_repository.h"
39 80ddbec8 2018-04-29 stsp #include "got_diff.h"
40 511a516b 2018-05-19 stsp #include "got_opentemp.h"
41 9ba79e04 2018-06-11 stsp #include "got_commit_graph.h"
42 9f7d7167 2018-04-29 stsp
43 881b2d3e 2018-04-30 stsp #ifndef MIN
44 881b2d3e 2018-04-30 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
45 881b2d3e 2018-04-30 stsp #endif
46 881b2d3e 2018-04-30 stsp
47 9f7d7167 2018-04-29 stsp #ifndef nitems
48 9f7d7167 2018-04-29 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
49 9f7d7167 2018-04-29 stsp #endif
50 9f7d7167 2018-04-29 stsp
51 9f7d7167 2018-04-29 stsp struct tog_cmd {
52 c2301be8 2018-04-30 stsp const char *name;
53 9f7d7167 2018-04-29 stsp const struct got_error *(*cmd_main)(int, char *[]);
54 9f7d7167 2018-04-29 stsp void (*cmd_usage)(void);
55 c2301be8 2018-04-30 stsp const char *descr;
56 9f7d7167 2018-04-29 stsp };
57 9f7d7167 2018-04-29 stsp
58 4ed7e80c 2018-05-20 stsp __dead static void usage(void);
59 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
60 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
61 4ed7e80c 2018-05-20 stsp __dead static void usage_blame(void);
62 9f7d7167 2018-04-29 stsp
63 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
64 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
65 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_blame(int, char *[]);
66 9f7d7167 2018-04-29 stsp
67 4ed7e80c 2018-05-20 stsp static struct tog_cmd tog_commands[] = {
68 cbb6b58a 2018-05-20 stsp { "log", cmd_log, usage_log,
69 9f7d7167 2018-04-29 stsp "show repository history" },
70 cbb6b58a 2018-05-20 stsp { "diff", cmd_diff, usage_diff,
71 9f7d7167 2018-04-29 stsp "compare files and directories" },
72 cbb6b58a 2018-05-20 stsp { "blame", cmd_blame, usage_blame,
73 9f7d7167 2018-04-29 stsp "show line-by-line file history" },
74 9f7d7167 2018-04-29 stsp };
75 9f7d7167 2018-04-29 stsp
76 fed328cc 2018-05-20 stsp static struct tog_view {
77 26ed57b2 2018-05-19 stsp WINDOW *window;
78 26ed57b2 2018-05-19 stsp PANEL *panel;
79 fed328cc 2018-05-20 stsp } tog_log_view, tog_diff_view;
80 cd0acaa7 2018-05-20 stsp
81 cd0acaa7 2018-05-20 stsp static const struct got_error *
82 cd0acaa7 2018-05-20 stsp show_diff_view(struct got_object *, struct got_object *,
83 cd0acaa7 2018-05-20 stsp struct got_repository *);
84 cd0acaa7 2018-05-20 stsp static const struct got_error *
85 cd0acaa7 2018-05-20 stsp show_log_view(struct got_object_id *, struct got_repository *);
86 26ed57b2 2018-05-19 stsp
87 4ed7e80c 2018-05-20 stsp __dead static void
88 9f7d7167 2018-04-29 stsp usage_log(void)
89 9f7d7167 2018-04-29 stsp {
90 80ddbec8 2018-04-29 stsp endwin();
91 80ddbec8 2018-04-29 stsp fprintf(stderr, "usage: %s log [-c commit] [repository-path]\n",
92 9f7d7167 2018-04-29 stsp getprogname());
93 9f7d7167 2018-04-29 stsp exit(1);
94 80ddbec8 2018-04-29 stsp }
95 80ddbec8 2018-04-29 stsp
96 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
97 80ddbec8 2018-04-29 stsp static const struct got_error *
98 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
99 963b370f 2018-05-20 stsp {
100 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
101 963b370f 2018-05-20 stsp
102 963b370f 2018-05-20 stsp *ws = NULL;
103 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
104 963b370f 2018-05-20 stsp if (*wlen == (size_t)-1)
105 963b370f 2018-05-20 stsp return got_error_from_errno();
106 963b370f 2018-05-20 stsp
107 963b370f 2018-05-20 stsp *ws = calloc(*wlen + 1, sizeof(*ws));
108 963b370f 2018-05-20 stsp if (*ws == NULL)
109 963b370f 2018-05-20 stsp return got_error_from_errno();
110 963b370f 2018-05-20 stsp
111 963b370f 2018-05-20 stsp if (mbstowcs(*ws, s, *wlen) != *wlen)
112 963b370f 2018-05-20 stsp err = got_error_from_errno();
113 963b370f 2018-05-20 stsp
114 963b370f 2018-05-20 stsp if (err) {
115 963b370f 2018-05-20 stsp free(*ws);
116 963b370f 2018-05-20 stsp *ws = NULL;
117 963b370f 2018-05-20 stsp *wlen = 0;
118 963b370f 2018-05-20 stsp }
119 963b370f 2018-05-20 stsp return err;
120 963b370f 2018-05-20 stsp }
121 963b370f 2018-05-20 stsp
122 963b370f 2018-05-20 stsp /* Format a line for display, ensuring that it won't overflow a width limit. */
123 963b370f 2018-05-20 stsp static const struct got_error *
124 963b370f 2018-05-20 stsp format_line(wchar_t **wlinep, int *widthp, char *line, int wlimit)
125 963b370f 2018-05-20 stsp {
126 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
127 963b370f 2018-05-20 stsp int cols = 0;
128 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
129 963b370f 2018-05-20 stsp size_t wlen;
130 963b370f 2018-05-20 stsp int i;
131 963b370f 2018-05-20 stsp
132 963b370f 2018-05-20 stsp *wlinep = NULL;
133 963b370f 2018-05-20 stsp
134 963b370f 2018-05-20 stsp err = mbs2ws(&wline, &wlen, line);
135 963b370f 2018-05-20 stsp if (err)
136 963b370f 2018-05-20 stsp return err;
137 963b370f 2018-05-20 stsp
138 963b370f 2018-05-20 stsp i = 0;
139 963b370f 2018-05-20 stsp while (i < wlen && cols <= wlimit) {
140 963b370f 2018-05-20 stsp int width = wcwidth(wline[i]);
141 963b370f 2018-05-20 stsp switch (width) {
142 963b370f 2018-05-20 stsp case 0:
143 963b370f 2018-05-20 stsp break;
144 963b370f 2018-05-20 stsp case 1:
145 963b370f 2018-05-20 stsp case 2:
146 963b370f 2018-05-20 stsp cols += width;
147 963b370f 2018-05-20 stsp break;
148 963b370f 2018-05-20 stsp case -1:
149 963b370f 2018-05-20 stsp if (wline[i] == L'\t')
150 963b370f 2018-05-20 stsp cols += TABSIZE;
151 963b370f 2018-05-20 stsp break;
152 963b370f 2018-05-20 stsp default:
153 963b370f 2018-05-20 stsp err = got_error_from_errno();
154 963b370f 2018-05-20 stsp goto done;
155 963b370f 2018-05-20 stsp }
156 963b370f 2018-05-20 stsp if (cols <= COLS) {
157 963b370f 2018-05-20 stsp i++;
158 963b370f 2018-05-20 stsp if (widthp)
159 963b370f 2018-05-20 stsp *widthp = cols;
160 963b370f 2018-05-20 stsp }
161 963b370f 2018-05-20 stsp }
162 963b370f 2018-05-20 stsp wline[i] = L'\0';
163 963b370f 2018-05-20 stsp done:
164 963b370f 2018-05-20 stsp if (err)
165 963b370f 2018-05-20 stsp free(wline);
166 963b370f 2018-05-20 stsp else
167 963b370f 2018-05-20 stsp *wlinep = wline;
168 963b370f 2018-05-20 stsp return err;
169 963b370f 2018-05-20 stsp }
170 963b370f 2018-05-20 stsp
171 963b370f 2018-05-20 stsp static const struct got_error *
172 0553a4e3 2018-04-30 stsp draw_commit(struct got_commit_object *commit, struct got_object_id *id)
173 80ddbec8 2018-04-29 stsp {
174 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
175 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
176 6d9fbc00 2018-04-29 stsp char *author0 = NULL, *author = NULL;
177 bb737323 2018-05-20 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
178 bb737323 2018-05-20 stsp int author_width, logmsg_width;
179 6d9fbc00 2018-04-29 stsp char *newline, *smallerthan;
180 80ddbec8 2018-04-29 stsp char *line = NULL;
181 6d9fbc00 2018-04-29 stsp char *id_str = NULL;
182 bb737323 2018-05-20 stsp size_t id_len;
183 bb737323 2018-05-20 stsp int col, limit;
184 bb737323 2018-05-20 stsp static const size_t id_display_cols = 8;
185 bb737323 2018-05-20 stsp static const size_t author_display_cols = 16;
186 9c2eaf34 2018-05-20 stsp const int avail = COLS;
187 80ddbec8 2018-04-29 stsp
188 80ddbec8 2018-04-29 stsp err = got_object_id_str(&id_str, id);
189 80ddbec8 2018-04-29 stsp if (err)
190 80ddbec8 2018-04-29 stsp return err;
191 881b2d3e 2018-04-30 stsp id_len = strlen(id_str);
192 bb737323 2018-05-20 stsp if (avail < id_display_cols) {
193 bb737323 2018-05-20 stsp limit = MIN(id_len, avail);
194 bb737323 2018-05-20 stsp waddnstr(tog_log_view.window, id_str, limit);
195 bb737323 2018-05-20 stsp } else {
196 bb737323 2018-05-20 stsp limit = MIN(id_display_cols, id_len);
197 bb737323 2018-05-20 stsp waddnstr(tog_log_view.window, id_str, limit);
198 6d9fbc00 2018-04-29 stsp }
199 bb737323 2018-05-20 stsp col = limit + 1;
200 9c2eaf34 2018-05-20 stsp while (col <= avail && col < id_display_cols + 2) {
201 bb737323 2018-05-20 stsp waddch(tog_log_view.window, ' ');
202 bb737323 2018-05-20 stsp col++;
203 bb737323 2018-05-20 stsp }
204 9c2eaf34 2018-05-20 stsp if (col > avail)
205 9c2eaf34 2018-05-20 stsp goto done;
206 80ddbec8 2018-04-29 stsp
207 6d9fbc00 2018-04-29 stsp author0 = strdup(commit->author);
208 6d9fbc00 2018-04-29 stsp if (author0 == NULL) {
209 80ddbec8 2018-04-29 stsp err = got_error_from_errno();
210 80ddbec8 2018-04-29 stsp goto done;
211 80ddbec8 2018-04-29 stsp }
212 6d9fbc00 2018-04-29 stsp author = author0;
213 6d9fbc00 2018-04-29 stsp smallerthan = strchr(author, '<');
214 6d9fbc00 2018-04-29 stsp if (smallerthan)
215 6d9fbc00 2018-04-29 stsp *smallerthan = '\0';
216 6d9fbc00 2018-04-29 stsp else {
217 6d9fbc00 2018-04-29 stsp char *at = strchr(author, '@');
218 6d9fbc00 2018-04-29 stsp if (at)
219 6d9fbc00 2018-04-29 stsp *at = '\0';
220 6d9fbc00 2018-04-29 stsp }
221 bb737323 2018-05-20 stsp limit = MIN(avail, author_display_cols);
222 bb737323 2018-05-20 stsp err = format_line(&wauthor, &author_width, author, limit);
223 bb737323 2018-05-20 stsp if (err)
224 bb737323 2018-05-20 stsp goto done;
225 bb737323 2018-05-20 stsp waddwstr(tog_log_view.window, wauthor);
226 bb737323 2018-05-20 stsp col += author_width;
227 9c2eaf34 2018-05-20 stsp while (col <= avail && author_width < author_display_cols + 1) {
228 bb737323 2018-05-20 stsp waddch(tog_log_view.window, ' ');
229 bb737323 2018-05-20 stsp col++;
230 bb737323 2018-05-20 stsp author_width++;
231 bb737323 2018-05-20 stsp }
232 9c2eaf34 2018-05-20 stsp if (col > avail)
233 9c2eaf34 2018-05-20 stsp goto done;
234 80ddbec8 2018-04-29 stsp
235 bb737323 2018-05-20 stsp logmsg0 = strdup(commit->logmsg);
236 bb737323 2018-05-20 stsp if (logmsg0 == NULL) {
237 6d9fbc00 2018-04-29 stsp err = got_error_from_errno();
238 6d9fbc00 2018-04-29 stsp goto done;
239 6d9fbc00 2018-04-29 stsp }
240 bb737323 2018-05-20 stsp logmsg = logmsg0;
241 bb737323 2018-05-20 stsp while (*logmsg == '\n')
242 bb737323 2018-05-20 stsp logmsg++;
243 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
244 bb737323 2018-05-20 stsp if (newline)
245 bb737323 2018-05-20 stsp *newline = '\0';
246 bb737323 2018-05-20 stsp limit = avail - col;
247 bb737323 2018-05-20 stsp err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
248 bb737323 2018-05-20 stsp if (err)
249 bb737323 2018-05-20 stsp goto done;
250 bb737323 2018-05-20 stsp waddwstr(tog_log_view.window, wlogmsg);
251 bb737323 2018-05-20 stsp col += logmsg_width;
252 bb737323 2018-05-20 stsp while (col <= avail) {
253 bb737323 2018-05-20 stsp waddch(tog_log_view.window, ' ');
254 bb737323 2018-05-20 stsp col++;
255 881b2d3e 2018-04-30 stsp }
256 80ddbec8 2018-04-29 stsp done:
257 80ddbec8 2018-04-29 stsp free(logmsg0);
258 bb737323 2018-05-20 stsp free(wlogmsg);
259 6d9fbc00 2018-04-29 stsp free(author0);
260 bb737323 2018-05-20 stsp free(wauthor);
261 80ddbec8 2018-04-29 stsp free(line);
262 6d9fbc00 2018-04-29 stsp free(id_str);
263 80ddbec8 2018-04-29 stsp return err;
264 80ddbec8 2018-04-29 stsp }
265 26ed57b2 2018-05-19 stsp
266 80ddbec8 2018-04-29 stsp struct commit_queue_entry {
267 80ddbec8 2018-04-29 stsp TAILQ_ENTRY(commit_queue_entry) entry;
268 80ddbec8 2018-04-29 stsp struct got_object_id *id;
269 80ddbec8 2018-04-29 stsp struct got_commit_object *commit;
270 80ddbec8 2018-04-29 stsp };
271 0553a4e3 2018-04-30 stsp TAILQ_HEAD(commit_queue, commit_queue_entry);
272 80ddbec8 2018-04-29 stsp
273 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
274 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
275 899d86c2 2018-05-10 stsp struct got_object_id *id)
276 80ddbec8 2018-04-29 stsp {
277 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
278 80ddbec8 2018-04-29 stsp
279 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
280 80ddbec8 2018-04-29 stsp if (entry == NULL)
281 899d86c2 2018-05-10 stsp return NULL;
282 99db9666 2018-05-07 stsp
283 899d86c2 2018-05-10 stsp entry->id = id;
284 99db9666 2018-05-07 stsp entry->commit = commit;
285 899d86c2 2018-05-10 stsp return entry;
286 99db9666 2018-05-07 stsp }
287 80ddbec8 2018-04-29 stsp
288 99db9666 2018-05-07 stsp static void
289 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
290 99db9666 2018-05-07 stsp {
291 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
292 99db9666 2018-05-07 stsp
293 99db9666 2018-05-07 stsp entry = TAILQ_FIRST(commits);
294 99db9666 2018-05-07 stsp TAILQ_REMOVE(commits, entry, entry);
295 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
296 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
297 99db9666 2018-05-07 stsp free(entry);
298 99db9666 2018-05-07 stsp }
299 99db9666 2018-05-07 stsp
300 99db9666 2018-05-07 stsp static void
301 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
302 99db9666 2018-05-07 stsp {
303 99db9666 2018-05-07 stsp while (!TAILQ_EMPTY(commits))
304 99db9666 2018-05-07 stsp pop_commit(commits);
305 c4972b91 2018-05-07 stsp }
306 c4972b91 2018-05-07 stsp
307 c4972b91 2018-05-07 stsp static const struct got_error *
308 9ba79e04 2018-06-11 stsp queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
309 9ba79e04 2018-06-11 stsp struct got_object_id *start_id, struct got_repository *repo)
310 c4972b91 2018-05-07 stsp {
311 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
312 899d86c2 2018-05-10 stsp struct got_object_id *id;
313 9ba79e04 2018-06-11 stsp struct commit_queue_entry *entry;
314 c4972b91 2018-05-07 stsp
315 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_start(graph, start_id);
316 c4972b91 2018-05-07 stsp if (err)
317 c4972b91 2018-05-07 stsp return err;
318 c4972b91 2018-05-07 stsp
319 9ba79e04 2018-06-11 stsp entry = TAILQ_LAST(commits, commit_queue);
320 9ba79e04 2018-06-11 stsp if (entry && got_object_id_cmp(entry->id, start_id) == 0) {
321 9ba79e04 2018-06-11 stsp int nfetched;
322 899d86c2 2018-05-10 stsp
323 9ba79e04 2018-06-11 stsp /* Start ID's commit is already on the queue; skip over it. */
324 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
325 9ba79e04 2018-06-11 stsp if (err && err->code != GOT_ERR_ITER_NEED_MORE)
326 9ba79e04 2018-06-11 stsp return err;
327 9ba79e04 2018-06-11 stsp
328 9ba79e04 2018-06-11 stsp err = got_commit_graph_fetch_commits(&nfetched, graph, 1, repo);
329 9ba79e04 2018-06-11 stsp if (err)
330 9ba79e04 2018-06-11 stsp return err;
331 c4972b91 2018-05-07 stsp }
332 c4972b91 2018-05-07 stsp
333 9ba79e04 2018-06-11 stsp while (1) {
334 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
335 899d86c2 2018-05-10 stsp
336 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
337 9ba79e04 2018-06-11 stsp if (err) {
338 9ba79e04 2018-06-11 stsp if (err->code == GOT_ERR_ITER_NEED_MORE)
339 9ba79e04 2018-06-11 stsp err = NULL;
340 9ba79e04 2018-06-11 stsp break;
341 9ba79e04 2018-06-11 stsp }
342 899d86c2 2018-05-10 stsp
343 9ba79e04 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
344 9ba79e04 2018-06-11 stsp if (err)
345 9ba79e04 2018-06-11 stsp break;
346 899d86c2 2018-05-10 stsp
347 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
348 9ba79e04 2018-06-11 stsp if (entry == NULL) {
349 9ba79e04 2018-06-11 stsp err = got_error_from_errno();
350 9ba79e04 2018-06-11 stsp break;
351 9ba79e04 2018-06-11 stsp }
352 899d86c2 2018-05-10 stsp
353 9ba79e04 2018-06-11 stsp TAILQ_INSERT_TAIL(commits, entry, entry);
354 899d86c2 2018-05-10 stsp }
355 899d86c2 2018-05-10 stsp
356 9ba79e04 2018-06-11 stsp return err;
357 99db9666 2018-05-07 stsp }
358 99db9666 2018-05-07 stsp
359 99db9666 2018-05-07 stsp static const struct got_error *
360 9ba79e04 2018-06-11 stsp fetch_next_commit(struct commit_queue_entry **pentry,
361 9ba79e04 2018-06-11 stsp struct commit_queue_entry *entry, struct commit_queue *commits,
362 9ba79e04 2018-06-11 stsp struct got_commit_graph *graph, struct got_repository *repo)
363 899d86c2 2018-05-10 stsp {
364 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
365 9ba79e04 2018-06-11 stsp struct got_object_qid *qid;
366 899d86c2 2018-05-10 stsp
367 9ba79e04 2018-06-11 stsp *pentry = NULL;
368 899d86c2 2018-05-10 stsp
369 9ba79e04 2018-06-11 stsp /* Populate commit graph with entry's parent commits. */
370 9ba79e04 2018-06-11 stsp SIMPLEQ_FOREACH(qid, &entry->commit->parent_ids, entry) {
371 9ba79e04 2018-06-11 stsp int nfetched;
372 9ba79e04 2018-06-11 stsp err = got_commit_graph_fetch_commits_up_to(&nfetched,
373 9ba79e04 2018-06-11 stsp graph, qid->id, repo);
374 9ba79e04 2018-06-11 stsp if (err)
375 9ba79e04 2018-06-11 stsp return err;
376 899d86c2 2018-05-10 stsp }
377 899d86c2 2018-05-10 stsp
378 9ba79e04 2018-06-11 stsp /* Append outstanding commits to queue in graph sort order. */
379 9ba79e04 2018-06-11 stsp err = queue_commits(graph, commits, entry->id, repo);
380 9ba79e04 2018-06-11 stsp if (err) {
381 9ba79e04 2018-06-11 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
382 9ba79e04 2018-06-11 stsp err = NULL;
383 9ba79e04 2018-06-11 stsp return err;
384 899d86c2 2018-05-10 stsp }
385 899d86c2 2018-05-10 stsp
386 9ba79e04 2018-06-11 stsp /* Next entry to display should now be available. */
387 9ba79e04 2018-06-11 stsp *pentry = TAILQ_NEXT(entry, entry);
388 9ba79e04 2018-06-11 stsp if (*pentry == NULL)
389 9ba79e04 2018-06-11 stsp return got_error(GOT_ERR_NO_OBJ);
390 899d86c2 2018-05-10 stsp
391 9ba79e04 2018-06-11 stsp return NULL;
392 899d86c2 2018-05-10 stsp }
393 899d86c2 2018-05-10 stsp
394 899d86c2 2018-05-10 stsp static const struct got_error *
395 9ba79e04 2018-06-11 stsp get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
396 99db9666 2018-05-07 stsp {
397 9ba79e04 2018-06-11 stsp const struct got_error *err = NULL;
398 9ba79e04 2018-06-11 stsp struct got_reference *head_ref;
399 99db9666 2018-05-07 stsp
400 9ba79e04 2018-06-11 stsp *head_id = NULL;
401 899d86c2 2018-05-10 stsp
402 9ba79e04 2018-06-11 stsp err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
403 99db9666 2018-05-07 stsp if (err)
404 99db9666 2018-05-07 stsp return err;
405 99db9666 2018-05-07 stsp
406 9ba79e04 2018-06-11 stsp err = got_ref_resolve(head_id, repo, head_ref);
407 9ba79e04 2018-06-11 stsp got_ref_close(head_ref);
408 9ba79e04 2018-06-11 stsp if (err) {
409 9ba79e04 2018-06-11 stsp *head_id = NULL;
410 99db9666 2018-05-07 stsp return err;
411 0553a4e3 2018-04-30 stsp }
412 80ddbec8 2018-04-29 stsp
413 9ba79e04 2018-06-11 stsp return NULL;
414 0553a4e3 2018-04-30 stsp }
415 0553a4e3 2018-04-30 stsp
416 0553a4e3 2018-04-30 stsp static const struct got_error *
417 cd0acaa7 2018-05-20 stsp draw_commits(struct commit_queue_entry **last, struct commit_queue_entry **selected,
418 cd0acaa7 2018-05-20 stsp struct commit_queue_entry *first, int selected_idx, int limit)
419 0553a4e3 2018-04-30 stsp {
420 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
421 0553a4e3 2018-04-30 stsp struct commit_queue_entry *entry;
422 0553a4e3 2018-04-30 stsp int ncommits = 0;
423 0553a4e3 2018-04-30 stsp
424 9c2de6ef 2018-05-20 stsp werase(tog_log_view.window);
425 0553a4e3 2018-04-30 stsp
426 899d86c2 2018-05-10 stsp entry = first;
427 899d86c2 2018-05-10 stsp *last = first;
428 899d86c2 2018-05-10 stsp while (entry) {
429 899d86c2 2018-05-10 stsp if (ncommits == limit)
430 899d86c2 2018-05-10 stsp break;
431 cd0acaa7 2018-05-20 stsp if (ncommits == selected_idx) {
432 0553a4e3 2018-04-30 stsp wstandout(tog_log_view.window);
433 cd0acaa7 2018-05-20 stsp *selected = entry;
434 cd0acaa7 2018-05-20 stsp }
435 0553a4e3 2018-04-30 stsp err = draw_commit(entry->commit, entry->id);
436 cd0acaa7 2018-05-20 stsp if (ncommits == selected_idx)
437 0553a4e3 2018-04-30 stsp wstandend(tog_log_view.window);
438 0553a4e3 2018-04-30 stsp if (err)
439 0553a4e3 2018-04-30 stsp break;
440 0553a4e3 2018-04-30 stsp ncommits++;
441 899d86c2 2018-05-10 stsp *last = entry;
442 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
443 80ddbec8 2018-04-29 stsp }
444 80ddbec8 2018-04-29 stsp
445 80ddbec8 2018-04-29 stsp update_panels();
446 80ddbec8 2018-04-29 stsp doupdate();
447 0553a4e3 2018-04-30 stsp
448 80ddbec8 2018-04-29 stsp return err;
449 9f7d7167 2018-04-29 stsp }
450 07b55e75 2018-05-10 stsp
451 07b55e75 2018-05-10 stsp static void
452 16482c3b 2018-05-20 stsp scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
453 07b55e75 2018-05-10 stsp struct commit_queue *commits)
454 07b55e75 2018-05-10 stsp {
455 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
456 07b55e75 2018-05-10 stsp int nscrolled = 0;
457 07b55e75 2018-05-10 stsp
458 07b55e75 2018-05-10 stsp entry = TAILQ_FIRST(commits);
459 07b55e75 2018-05-10 stsp if (*first_displayed_entry == entry)
460 07b55e75 2018-05-10 stsp return;
461 9f7d7167 2018-04-29 stsp
462 07b55e75 2018-05-10 stsp entry = *first_displayed_entry;
463 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
464 07b55e75 2018-05-10 stsp entry = TAILQ_PREV(entry, commit_queue, entry);
465 07b55e75 2018-05-10 stsp if (entry) {
466 07b55e75 2018-05-10 stsp *first_displayed_entry = entry;
467 07b55e75 2018-05-10 stsp nscrolled++;
468 07b55e75 2018-05-10 stsp }
469 07b55e75 2018-05-10 stsp }
470 aa075928 2018-05-10 stsp }
471 aa075928 2018-05-10 stsp
472 aa075928 2018-05-10 stsp static const struct got_error *
473 16482c3b 2018-05-20 stsp scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
474 aa075928 2018-05-10 stsp struct commit_queue_entry *last_displayed_entry,
475 9ba79e04 2018-06-11 stsp struct commit_queue *commits, struct got_commit_graph *graph,
476 9ba79e04 2018-06-11 stsp struct got_repository *repo)
477 aa075928 2018-05-10 stsp {
478 aa075928 2018-05-10 stsp const struct got_error *err = NULL;
479 dd0a52c1 2018-05-20 stsp struct commit_queue_entry *pentry;
480 aa075928 2018-05-10 stsp int nscrolled = 0;
481 aa075928 2018-05-10 stsp
482 aa075928 2018-05-10 stsp do {
483 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(last_displayed_entry, entry);
484 aa075928 2018-05-10 stsp if (pentry == NULL) {
485 9ba79e04 2018-06-11 stsp err = fetch_next_commit(&pentry, last_displayed_entry,
486 9ba79e04 2018-06-11 stsp commits, graph, repo);
487 9a6bf2a5 2018-05-20 stsp if (err || pentry == NULL)
488 aa075928 2018-05-10 stsp break;
489 aa075928 2018-05-10 stsp }
490 dd0a52c1 2018-05-20 stsp last_displayed_entry = pentry;
491 aa075928 2018-05-10 stsp
492 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(*first_displayed_entry, entry);
493 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
494 dd0a52c1 2018-05-20 stsp break;
495 aa075928 2018-05-10 stsp *first_displayed_entry = pentry;
496 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
497 aa075928 2018-05-10 stsp
498 dd0a52c1 2018-05-20 stsp return err;
499 07b55e75 2018-05-10 stsp }
500 4a7f7875 2018-05-10 stsp
501 4a7f7875 2018-05-10 stsp static int
502 4a7f7875 2018-05-10 stsp num_parents(struct commit_queue_entry *entry)
503 4a7f7875 2018-05-10 stsp {
504 4a7f7875 2018-05-10 stsp int nparents = 0;
505 07b55e75 2018-05-10 stsp
506 4a7f7875 2018-05-10 stsp while (entry) {
507 4a7f7875 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
508 4a7f7875 2018-05-10 stsp nparents++;
509 4a7f7875 2018-05-10 stsp }
510 4a7f7875 2018-05-10 stsp
511 4a7f7875 2018-05-10 stsp return nparents;
512 cd0acaa7 2018-05-20 stsp }
513 cd0acaa7 2018-05-20 stsp
514 cd0acaa7 2018-05-20 stsp static const struct got_error *
515 cd0acaa7 2018-05-20 stsp show_commit(struct commit_queue_entry *entry, struct got_repository *repo)
516 cd0acaa7 2018-05-20 stsp {
517 cd0acaa7 2018-05-20 stsp const struct got_error *err;
518 cd0acaa7 2018-05-20 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
519 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
520 cd0acaa7 2018-05-20 stsp
521 cd0acaa7 2018-05-20 stsp err = got_object_open(&obj2, repo, entry->id);
522 cd0acaa7 2018-05-20 stsp if (err)
523 cd0acaa7 2018-05-20 stsp return err;
524 cd0acaa7 2018-05-20 stsp
525 9ba79e04 2018-06-11 stsp parent_id = SIMPLEQ_FIRST(&entry->commit->parent_ids);
526 9ba79e04 2018-06-11 stsp if (parent_id) {
527 9ba79e04 2018-06-11 stsp err = got_object_open(&obj1, repo, parent_id->id);
528 cd0acaa7 2018-05-20 stsp if (err)
529 cd0acaa7 2018-05-20 stsp goto done;
530 cd0acaa7 2018-05-20 stsp }
531 cd0acaa7 2018-05-20 stsp
532 cd0acaa7 2018-05-20 stsp err = show_diff_view(obj1, obj2, repo);
533 cd0acaa7 2018-05-20 stsp done:
534 cd0acaa7 2018-05-20 stsp if (obj1)
535 cd0acaa7 2018-05-20 stsp got_object_close(obj1);
536 cd0acaa7 2018-05-20 stsp if (obj2)
537 cd0acaa7 2018-05-20 stsp got_object_close(obj2);
538 cd0acaa7 2018-05-20 stsp return err;
539 4a7f7875 2018-05-10 stsp }
540 4a7f7875 2018-05-10 stsp
541 80ddbec8 2018-04-29 stsp static const struct got_error *
542 80ddbec8 2018-04-29 stsp show_log_view(struct got_object_id *start_id, struct got_repository *repo)
543 80ddbec8 2018-04-29 stsp {
544 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
545 9ba79e04 2018-06-11 stsp struct got_object_id *head_id = NULL;
546 9ba79e04 2018-06-11 stsp int ch, done = 0, selected = 0, nparents, nfetched;
547 9ba79e04 2018-06-11 stsp struct got_commit_graph *graph;
548 0553a4e3 2018-04-30 stsp struct commit_queue commits;
549 9ba79e04 2018-06-11 stsp struct commit_queue_entry *entry = NULL;
550 899d86c2 2018-05-10 stsp struct commit_queue_entry *first_displayed_entry = NULL;
551 899d86c2 2018-05-10 stsp struct commit_queue_entry *last_displayed_entry = NULL;
552 cd0acaa7 2018-05-20 stsp struct commit_queue_entry *selected_entry = NULL;
553 80ddbec8 2018-04-29 stsp
554 80ddbec8 2018-04-29 stsp if (tog_log_view.window == NULL) {
555 80ddbec8 2018-04-29 stsp tog_log_view.window = newwin(0, 0, 0, 0);
556 80ddbec8 2018-04-29 stsp if (tog_log_view.window == NULL)
557 80ddbec8 2018-04-29 stsp return got_error_from_errno();
558 00a838fa 2018-04-29 stsp keypad(tog_log_view.window, TRUE);
559 80ddbec8 2018-04-29 stsp }
560 80ddbec8 2018-04-29 stsp if (tog_log_view.panel == NULL) {
561 80ddbec8 2018-04-29 stsp tog_log_view.panel = new_panel(tog_log_view.window);
562 80ddbec8 2018-04-29 stsp if (tog_log_view.panel == NULL)
563 80ddbec8 2018-04-29 stsp return got_error_from_errno();
564 cd0acaa7 2018-05-20 stsp } else
565 cd0acaa7 2018-05-20 stsp show_panel(tog_log_view.panel);
566 80ddbec8 2018-04-29 stsp
567 9ba79e04 2018-06-11 stsp err = get_head_commit_id(&head_id, repo);
568 9ba79e04 2018-06-11 stsp if (err)
569 9ba79e04 2018-06-11 stsp return err;
570 9ba79e04 2018-06-11 stsp
571 899d86c2 2018-05-10 stsp TAILQ_INIT(&commits);
572 9ba79e04 2018-06-11 stsp
573 9ba79e04 2018-06-11 stsp err = got_commit_graph_open(&graph, head_id, repo);
574 9ba79e04 2018-06-11 stsp if (err)
575 9ba79e04 2018-06-11 stsp goto done;
576 9ba79e04 2018-06-11 stsp
577 9ba79e04 2018-06-11 stsp /* Populate commit graph with a sufficient number of commits. */
578 9ba79e04 2018-06-11 stsp err = got_commit_graph_fetch_commits_up_to(&nfetched, graph, start_id,
579 9ba79e04 2018-06-11 stsp repo);
580 9ba79e04 2018-06-11 stsp if (err)
581 9ba79e04 2018-06-11 stsp goto done;
582 9ba79e04 2018-06-11 stsp err = got_commit_graph_fetch_commits(&nfetched, graph, LINES, repo);
583 80ddbec8 2018-04-29 stsp if (err)
584 9ba79e04 2018-06-11 stsp goto done;
585 9ba79e04 2018-06-11 stsp
586 9ba79e04 2018-06-11 stsp /*
587 9ba79e04 2018-06-11 stsp * Open the initial batch of commits, sorted in commit graph order.
588 9ba79e04 2018-06-11 stsp * We keep all commits open throughout the lifetime of the log view
589 9ba79e04 2018-06-11 stsp * in order to avoid having to re-fetch commits from disk while
590 9ba79e04 2018-06-11 stsp * updating the display.
591 9ba79e04 2018-06-11 stsp */
592 9ba79e04 2018-06-11 stsp err = queue_commits(graph, &commits, head_id, repo);
593 9ba79e04 2018-06-11 stsp if (err && err->code != GOT_ERR_ITER_COMPLETED)
594 9ba79e04 2018-06-11 stsp goto done;
595 9ba79e04 2018-06-11 stsp
596 9ba79e04 2018-06-11 stsp /* Find entry corresponding to the first commit to display. */
597 9ba79e04 2018-06-11 stsp TAILQ_FOREACH(entry, &commits, entry) {
598 9ba79e04 2018-06-11 stsp if (got_object_id_cmp(entry->id, start_id) == 0) {
599 9ba79e04 2018-06-11 stsp first_displayed_entry = entry;
600 9ba79e04 2018-06-11 stsp break;
601 9ba79e04 2018-06-11 stsp }
602 9ba79e04 2018-06-11 stsp }
603 9ba79e04 2018-06-11 stsp if (first_displayed_entry == NULL) {
604 9ba79e04 2018-06-11 stsp err = got_error(GOT_ERR_NO_OBJ);
605 80ddbec8 2018-04-29 stsp goto done;
606 9ba79e04 2018-06-11 stsp }
607 9ba79e04 2018-06-11 stsp
608 899d86c2 2018-05-10 stsp while (!done) {
609 cd0acaa7 2018-05-20 stsp err = draw_commits(&last_displayed_entry, &selected_entry,
610 cd0acaa7 2018-05-20 stsp first_displayed_entry, selected, LINES);
611 80ddbec8 2018-04-29 stsp if (err)
612 d0f709cb 2018-04-30 stsp goto done;
613 80ddbec8 2018-04-29 stsp
614 80ddbec8 2018-04-29 stsp nodelay(stdscr, FALSE);
615 80ddbec8 2018-04-29 stsp ch = wgetch(tog_log_view.window);
616 f7182337 2018-05-20 stsp nodelay(stdscr, TRUE);
617 80ddbec8 2018-04-29 stsp switch (ch) {
618 31120ada 2018-04-30 stsp case ERR:
619 31120ada 2018-04-30 stsp if (errno) {
620 31120ada 2018-04-30 stsp err = got_error_from_errno();
621 31120ada 2018-04-30 stsp goto done;
622 31120ada 2018-04-30 stsp }
623 31120ada 2018-04-30 stsp break;
624 80ddbec8 2018-04-29 stsp case 'q':
625 80ddbec8 2018-04-29 stsp done = 1;
626 80ddbec8 2018-04-29 stsp break;
627 80ddbec8 2018-04-29 stsp case 'k':
628 80ddbec8 2018-04-29 stsp case KEY_UP:
629 80ddbec8 2018-04-29 stsp if (selected > 0)
630 80ddbec8 2018-04-29 stsp selected--;
631 8ec9698d 2018-05-10 stsp if (selected > 0)
632 d91e25cb 2018-05-10 stsp break;
633 07b55e75 2018-05-10 stsp scroll_up(&first_displayed_entry, 1, &commits);
634 80ddbec8 2018-04-29 stsp break;
635 48531068 2018-05-10 stsp case KEY_PPAGE:
636 dfc1d240 2018-05-10 stsp if (TAILQ_FIRST(&commits) ==
637 dfc1d240 2018-05-10 stsp first_displayed_entry) {
638 dfc1d240 2018-05-10 stsp selected = 0;
639 dfc1d240 2018-05-10 stsp break;
640 dfc1d240 2018-05-10 stsp }
641 48531068 2018-05-10 stsp scroll_up(&first_displayed_entry, LINES,
642 48531068 2018-05-10 stsp &commits);
643 48531068 2018-05-10 stsp break;
644 80ddbec8 2018-04-29 stsp case 'j':
645 80ddbec8 2018-04-29 stsp case KEY_DOWN:
646 80ee4603 2018-05-10 stsp nparents = num_parents(first_displayed_entry);
647 06abe2cd 2018-05-10 stsp if (selected < LINES - 1 &&
648 15c91275 2018-05-20 stsp selected < nparents - 1) {
649 15c91275 2018-05-20 stsp selected++;
650 15c91275 2018-05-20 stsp break;
651 8df4052c 2018-05-20 stsp }
652 15c91275 2018-05-20 stsp err = scroll_down(&first_displayed_entry, 1,
653 9ba79e04 2018-06-11 stsp last_displayed_entry, &commits, graph,
654 9ba79e04 2018-06-11 stsp repo);
655 15c91275 2018-05-20 stsp if (err)
656 15c91275 2018-05-20 stsp goto done;
657 4a7f7875 2018-05-10 stsp break;
658 4a7f7875 2018-05-10 stsp case KEY_NPAGE:
659 e50ee4f1 2018-05-10 stsp err = scroll_down(&first_displayed_entry, LINES,
660 9ba79e04 2018-06-11 stsp last_displayed_entry, &commits, graph,
661 9ba79e04 2018-06-11 stsp repo);
662 899d86c2 2018-05-10 stsp if (err)
663 aa075928 2018-05-10 stsp goto done;
664 dd0a52c1 2018-05-20 stsp if (last_displayed_entry->commit->nparents > 0)
665 dd0a52c1 2018-05-20 stsp break;
666 dd0a52c1 2018-05-20 stsp /* can't scroll any further; move cursor down */
667 4a7f7875 2018-05-10 stsp nparents = num_parents(first_displayed_entry);
668 dd0a52c1 2018-05-20 stsp if (selected < LINES - 1 ||
669 dd0a52c1 2018-05-20 stsp selected < nparents - 1)
670 dd0a52c1 2018-05-20 stsp selected = MIN(LINES - 1, nparents - 1);
671 80ddbec8 2018-04-29 stsp break;
672 d6df9be4 2018-04-30 stsp case KEY_RESIZE:
673 31120ada 2018-04-30 stsp if (selected > LINES)
674 31120ada 2018-04-30 stsp selected = LINES - 1;
675 cd0acaa7 2018-05-20 stsp break;
676 cd0acaa7 2018-05-20 stsp case KEY_ENTER:
677 cd0acaa7 2018-05-20 stsp case '\r':
678 cd0acaa7 2018-05-20 stsp err = show_commit(selected_entry, repo);
679 cd0acaa7 2018-05-20 stsp if (err)
680 cd0acaa7 2018-05-20 stsp break;
681 cd0acaa7 2018-05-20 stsp show_panel(tog_log_view.panel);
682 31120ada 2018-04-30 stsp break;
683 80ddbec8 2018-04-29 stsp default:
684 80ddbec8 2018-04-29 stsp break;
685 80ddbec8 2018-04-29 stsp }
686 899d86c2 2018-05-10 stsp }
687 80ddbec8 2018-04-29 stsp done:
688 9ba79e04 2018-06-11 stsp free(head_id);
689 9ba79e04 2018-06-11 stsp if (graph)
690 9ba79e04 2018-06-11 stsp got_commit_graph_close(graph);
691 0553a4e3 2018-04-30 stsp free_commits(&commits);
692 80ddbec8 2018-04-29 stsp return err;
693 80ddbec8 2018-04-29 stsp }
694 80ddbec8 2018-04-29 stsp
695 4ed7e80c 2018-05-20 stsp static const struct got_error *
696 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
697 9f7d7167 2018-04-29 stsp {
698 80ddbec8 2018-04-29 stsp const struct got_error *error;
699 80ddbec8 2018-04-29 stsp struct got_repository *repo;
700 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
701 80ddbec8 2018-04-29 stsp char *repo_path = NULL;
702 80ddbec8 2018-04-29 stsp char *start_commit = NULL;
703 80ddbec8 2018-04-29 stsp int ch;
704 80ddbec8 2018-04-29 stsp
705 80ddbec8 2018-04-29 stsp #ifndef PROFILE
706 80ddbec8 2018-04-29 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
707 80ddbec8 2018-04-29 stsp err(1, "pledge");
708 80ddbec8 2018-04-29 stsp #endif
709 80ddbec8 2018-04-29 stsp
710 80ddbec8 2018-04-29 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
711 80ddbec8 2018-04-29 stsp switch (ch) {
712 80ddbec8 2018-04-29 stsp case 'c':
713 80ddbec8 2018-04-29 stsp start_commit = optarg;
714 80ddbec8 2018-04-29 stsp break;
715 80ddbec8 2018-04-29 stsp default:
716 80ddbec8 2018-04-29 stsp usage();
717 80ddbec8 2018-04-29 stsp /* NOTREACHED */
718 80ddbec8 2018-04-29 stsp }
719 80ddbec8 2018-04-29 stsp }
720 80ddbec8 2018-04-29 stsp
721 80ddbec8 2018-04-29 stsp argc -= optind;
722 80ddbec8 2018-04-29 stsp argv += optind;
723 80ddbec8 2018-04-29 stsp
724 80ddbec8 2018-04-29 stsp if (argc == 0) {
725 80ddbec8 2018-04-29 stsp repo_path = getcwd(NULL, 0);
726 80ddbec8 2018-04-29 stsp if (repo_path == NULL)
727 80ddbec8 2018-04-29 stsp return got_error_from_errno();
728 80ddbec8 2018-04-29 stsp } else if (argc == 1) {
729 80ddbec8 2018-04-29 stsp repo_path = realpath(argv[0], NULL);
730 80ddbec8 2018-04-29 stsp if (repo_path == NULL)
731 80ddbec8 2018-04-29 stsp return got_error_from_errno();
732 80ddbec8 2018-04-29 stsp } else
733 80ddbec8 2018-04-29 stsp usage_log();
734 80ddbec8 2018-04-29 stsp
735 80ddbec8 2018-04-29 stsp error = got_repo_open(&repo, repo_path);
736 80ddbec8 2018-04-29 stsp free(repo_path);
737 80ddbec8 2018-04-29 stsp if (error != NULL)
738 80ddbec8 2018-04-29 stsp return error;
739 80ddbec8 2018-04-29 stsp
740 80ddbec8 2018-04-29 stsp if (start_commit == NULL) {
741 899d86c2 2018-05-10 stsp error = get_head_commit_id(&start_id, repo);
742 80ddbec8 2018-04-29 stsp if (error != NULL)
743 80ddbec8 2018-04-29 stsp return error;
744 80ddbec8 2018-04-29 stsp } else {
745 80ddbec8 2018-04-29 stsp struct got_object *obj;
746 80ddbec8 2018-04-29 stsp error = got_object_open_by_id_str(&obj, repo, start_commit);
747 80ddbec8 2018-04-29 stsp if (error == NULL) {
748 899d86c2 2018-05-10 stsp start_id = got_object_get_id(obj);
749 899d86c2 2018-05-10 stsp if (start_id == NULL)
750 80ddbec8 2018-04-29 stsp error = got_error_from_errno();
751 80ddbec8 2018-04-29 stsp }
752 80ddbec8 2018-04-29 stsp }
753 80ddbec8 2018-04-29 stsp if (error != NULL)
754 80ddbec8 2018-04-29 stsp return error;
755 899d86c2 2018-05-10 stsp error = show_log_view(start_id, repo);
756 899d86c2 2018-05-10 stsp free(start_id);
757 80ddbec8 2018-04-29 stsp got_repo_close(repo);
758 80ddbec8 2018-04-29 stsp return error;
759 9f7d7167 2018-04-29 stsp }
760 9f7d7167 2018-04-29 stsp
761 4ed7e80c 2018-05-20 stsp __dead static void
762 9f7d7167 2018-04-29 stsp usage_diff(void)
763 9f7d7167 2018-04-29 stsp {
764 80ddbec8 2018-04-29 stsp endwin();
765 9f7d7167 2018-04-29 stsp fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
766 9f7d7167 2018-04-29 stsp getprogname());
767 9f7d7167 2018-04-29 stsp exit(1);
768 b304db33 2018-05-20 stsp }
769 b304db33 2018-05-20 stsp
770 b304db33 2018-05-20 stsp static char *
771 b304db33 2018-05-20 stsp parse_next_line(FILE *f, size_t *len)
772 b304db33 2018-05-20 stsp {
773 b304db33 2018-05-20 stsp char *line;
774 b304db33 2018-05-20 stsp size_t linelen;
775 b304db33 2018-05-20 stsp size_t lineno;
776 b304db33 2018-05-20 stsp const char delim[3] = { '\0', '\0', '\0'};
777 b304db33 2018-05-20 stsp
778 b304db33 2018-05-20 stsp line = fparseln(f, &linelen, &lineno, delim, 0);
779 b304db33 2018-05-20 stsp if (len)
780 b304db33 2018-05-20 stsp *len = linelen;
781 b304db33 2018-05-20 stsp return line;
782 26ed57b2 2018-05-19 stsp }
783 26ed57b2 2018-05-19 stsp
784 4ed7e80c 2018-05-20 stsp static const struct got_error *
785 26ed57b2 2018-05-19 stsp draw_diff(FILE *f, int *first_displayed_line, int *last_displayed_line,
786 26ed57b2 2018-05-19 stsp int *eof, int max_lines)
787 26ed57b2 2018-05-19 stsp {
788 61e69b96 2018-05-20 stsp const struct got_error *err;
789 26ed57b2 2018-05-19 stsp int nlines = 0, nprinted = 0;
790 b304db33 2018-05-20 stsp char *line;
791 b304db33 2018-05-20 stsp size_t len;
792 61e69b96 2018-05-20 stsp wchar_t *wline;
793 e0b650dd 2018-05-20 stsp int width;
794 26ed57b2 2018-05-19 stsp
795 26ed57b2 2018-05-19 stsp rewind(f);
796 9c2de6ef 2018-05-20 stsp werase(tog_diff_view.window);
797 26ed57b2 2018-05-19 stsp
798 26ed57b2 2018-05-19 stsp *eof = 0;
799 26ed57b2 2018-05-19 stsp while (nprinted < max_lines) {
800 b304db33 2018-05-20 stsp line = parse_next_line(f, &len);
801 26ed57b2 2018-05-19 stsp if (line == NULL) {
802 26ed57b2 2018-05-19 stsp *eof = 1;
803 26ed57b2 2018-05-19 stsp break;
804 26ed57b2 2018-05-19 stsp }
805 26ed57b2 2018-05-19 stsp if (++nlines < *first_displayed_line) {
806 26ed57b2 2018-05-19 stsp free(line);
807 26ed57b2 2018-05-19 stsp continue;
808 26ed57b2 2018-05-19 stsp }
809 26ed57b2 2018-05-19 stsp
810 e0b650dd 2018-05-20 stsp err = format_line(&wline, &width, line, COLS);
811 61e69b96 2018-05-20 stsp if (err) {
812 61e69b96 2018-05-20 stsp free(line);
813 61e69b96 2018-05-20 stsp return err;
814 61e69b96 2018-05-20 stsp }
815 61e69b96 2018-05-20 stsp waddwstr(tog_diff_view.window, wline);
816 e0b650dd 2018-05-20 stsp if (width < COLS)
817 e0b650dd 2018-05-20 stsp waddch(tog_diff_view.window, '\n');
818 26ed57b2 2018-05-19 stsp if (++nprinted == 1)
819 26ed57b2 2018-05-19 stsp *first_displayed_line = nlines;
820 26ed57b2 2018-05-19 stsp free(line);
821 26ed57b2 2018-05-19 stsp }
822 26ed57b2 2018-05-19 stsp *last_displayed_line = nlines;
823 26ed57b2 2018-05-19 stsp
824 26ed57b2 2018-05-19 stsp update_panels();
825 26ed57b2 2018-05-19 stsp doupdate();
826 26ed57b2 2018-05-19 stsp
827 26ed57b2 2018-05-19 stsp return NULL;
828 9f7d7167 2018-04-29 stsp }
829 9f7d7167 2018-04-29 stsp
830 4ed7e80c 2018-05-20 stsp static const struct got_error *
831 26ed57b2 2018-05-19 stsp show_diff_view(struct got_object *obj1, struct got_object *obj2,
832 26ed57b2 2018-05-19 stsp struct got_repository *repo)
833 26ed57b2 2018-05-19 stsp {
834 26ed57b2 2018-05-19 stsp const struct got_error *err;
835 26ed57b2 2018-05-19 stsp FILE *f;
836 26ed57b2 2018-05-19 stsp int ch, done = 0, first_displayed_line = 1, last_displayed_line = LINES;
837 b304db33 2018-05-20 stsp int eof, i;
838 26ed57b2 2018-05-19 stsp
839 cd0acaa7 2018-05-20 stsp if (obj1 != NULL && obj2 != NULL &&
840 cd0acaa7 2018-05-20 stsp got_object_get_type(obj1) != got_object_get_type(obj2))
841 26ed57b2 2018-05-19 stsp return got_error(GOT_ERR_OBJ_TYPE);
842 26ed57b2 2018-05-19 stsp
843 511a516b 2018-05-19 stsp f = got_opentemp();
844 26ed57b2 2018-05-19 stsp if (f == NULL)
845 26ed57b2 2018-05-19 stsp return got_error_from_errno();
846 26ed57b2 2018-05-19 stsp
847 cd0acaa7 2018-05-20 stsp switch (got_object_get_type(obj1 ? obj1 : obj2)) {
848 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
849 11528a82 2018-05-19 stsp err = got_diff_objects_as_blobs(obj1, obj2, repo, f);
850 26ed57b2 2018-05-19 stsp break;
851 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
852 11528a82 2018-05-19 stsp err = got_diff_objects_as_trees(obj1, obj2, repo, f);
853 26ed57b2 2018-05-19 stsp break;
854 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_COMMIT:
855 11528a82 2018-05-19 stsp err = got_diff_objects_as_commits(obj1, obj2, repo, f);
856 26ed57b2 2018-05-19 stsp break;
857 26ed57b2 2018-05-19 stsp default:
858 26ed57b2 2018-05-19 stsp return got_error(GOT_ERR_OBJ_TYPE);
859 26ed57b2 2018-05-19 stsp }
860 26ed57b2 2018-05-19 stsp
861 26ed57b2 2018-05-19 stsp fflush(f);
862 26ed57b2 2018-05-19 stsp
863 26ed57b2 2018-05-19 stsp if (tog_diff_view.window == NULL) {
864 26ed57b2 2018-05-19 stsp tog_diff_view.window = newwin(0, 0, 0, 0);
865 26ed57b2 2018-05-19 stsp if (tog_diff_view.window == NULL)
866 26ed57b2 2018-05-19 stsp return got_error_from_errno();
867 26ed57b2 2018-05-19 stsp keypad(tog_diff_view.window, TRUE);
868 26ed57b2 2018-05-19 stsp }
869 26ed57b2 2018-05-19 stsp if (tog_diff_view.panel == NULL) {
870 26ed57b2 2018-05-19 stsp tog_diff_view.panel = new_panel(tog_diff_view.window);
871 26ed57b2 2018-05-19 stsp if (tog_diff_view.panel == NULL)
872 26ed57b2 2018-05-19 stsp return got_error_from_errno();
873 26ed57b2 2018-05-19 stsp } else
874 26ed57b2 2018-05-19 stsp show_panel(tog_diff_view.panel);
875 26ed57b2 2018-05-19 stsp
876 26ed57b2 2018-05-19 stsp while (!done) {
877 26ed57b2 2018-05-19 stsp err = draw_diff(f, &first_displayed_line, &last_displayed_line,
878 26ed57b2 2018-05-19 stsp &eof, LINES);
879 26ed57b2 2018-05-19 stsp if (err)
880 26ed57b2 2018-05-19 stsp break;
881 26ed57b2 2018-05-19 stsp nodelay(stdscr, FALSE);
882 26ed57b2 2018-05-19 stsp ch = wgetch(tog_diff_view.window);
883 f7182337 2018-05-20 stsp nodelay(stdscr, TRUE);
884 26ed57b2 2018-05-19 stsp switch (ch) {
885 26ed57b2 2018-05-19 stsp case 'q':
886 26ed57b2 2018-05-19 stsp done = 1;
887 26ed57b2 2018-05-19 stsp break;
888 26ed57b2 2018-05-19 stsp case 'k':
889 26ed57b2 2018-05-19 stsp case KEY_UP:
890 925e6f23 2018-05-20 stsp case KEY_BACKSPACE:
891 26ed57b2 2018-05-19 stsp if (first_displayed_line > 1)
892 b304db33 2018-05-20 stsp first_displayed_line--;
893 b304db33 2018-05-20 stsp break;
894 b304db33 2018-05-20 stsp case KEY_PPAGE:
895 b304db33 2018-05-20 stsp i = 0;
896 d56caa55 2018-05-20 stsp while (i++ < LINES - 1 &&
897 d56caa55 2018-05-20 stsp first_displayed_line > 1)
898 26ed57b2 2018-05-19 stsp first_displayed_line--;
899 26ed57b2 2018-05-19 stsp break;
900 26ed57b2 2018-05-19 stsp case 'j':
901 26ed57b2 2018-05-19 stsp case KEY_DOWN:
902 925e6f23 2018-05-20 stsp case KEY_ENTER:
903 925e6f23 2018-05-20 stsp case '\r':
904 26ed57b2 2018-05-19 stsp if (!eof)
905 26ed57b2 2018-05-19 stsp first_displayed_line++;
906 26ed57b2 2018-05-19 stsp break;
907 b304db33 2018-05-20 stsp case KEY_NPAGE:
908 75e48879 2018-05-20 stsp case ' ':
909 b304db33 2018-05-20 stsp i = 0;
910 b304db33 2018-05-20 stsp while (!eof && i++ < LINES - 1) {
911 b304db33 2018-05-20 stsp char *line = parse_next_line(f, NULL);
912 b304db33 2018-05-20 stsp first_displayed_line++;
913 b304db33 2018-05-20 stsp if (line == NULL)
914 b304db33 2018-05-20 stsp break;
915 b304db33 2018-05-20 stsp }
916 b304db33 2018-05-20 stsp break;
917 26ed57b2 2018-05-19 stsp default:
918 26ed57b2 2018-05-19 stsp break;
919 26ed57b2 2018-05-19 stsp }
920 26ed57b2 2018-05-19 stsp }
921 26ed57b2 2018-05-19 stsp fclose(f);
922 26ed57b2 2018-05-19 stsp return err;
923 26ed57b2 2018-05-19 stsp }
924 26ed57b2 2018-05-19 stsp
925 4ed7e80c 2018-05-20 stsp static const struct got_error *
926 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
927 9f7d7167 2018-04-29 stsp {
928 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
929 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
930 26ed57b2 2018-05-19 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
931 26ed57b2 2018-05-19 stsp char *repo_path = NULL;
932 26ed57b2 2018-05-19 stsp char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
933 26ed57b2 2018-05-19 stsp int ch;
934 26ed57b2 2018-05-19 stsp
935 26ed57b2 2018-05-19 stsp #ifndef PROFILE
936 26ed57b2 2018-05-19 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
937 26ed57b2 2018-05-19 stsp err(1, "pledge");
938 26ed57b2 2018-05-19 stsp #endif
939 26ed57b2 2018-05-19 stsp
940 26ed57b2 2018-05-19 stsp while ((ch = getopt(argc, argv, "")) != -1) {
941 26ed57b2 2018-05-19 stsp switch (ch) {
942 26ed57b2 2018-05-19 stsp default:
943 26ed57b2 2018-05-19 stsp usage();
944 26ed57b2 2018-05-19 stsp /* NOTREACHED */
945 26ed57b2 2018-05-19 stsp }
946 26ed57b2 2018-05-19 stsp }
947 26ed57b2 2018-05-19 stsp
948 26ed57b2 2018-05-19 stsp argc -= optind;
949 26ed57b2 2018-05-19 stsp argv += optind;
950 26ed57b2 2018-05-19 stsp
951 26ed57b2 2018-05-19 stsp if (argc == 0) {
952 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
953 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
954 26ed57b2 2018-05-19 stsp repo_path = getcwd(NULL, 0);
955 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
956 26ed57b2 2018-05-19 stsp return got_error_from_errno();
957 26ed57b2 2018-05-19 stsp obj_id_str1 = argv[0];
958 26ed57b2 2018-05-19 stsp obj_id_str2 = argv[1];
959 26ed57b2 2018-05-19 stsp } else if (argc == 3) {
960 26ed57b2 2018-05-19 stsp repo_path = realpath(argv[0], NULL);
961 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
962 26ed57b2 2018-05-19 stsp return got_error_from_errno();
963 26ed57b2 2018-05-19 stsp obj_id_str1 = argv[1];
964 26ed57b2 2018-05-19 stsp obj_id_str2 = argv[2];
965 26ed57b2 2018-05-19 stsp } else
966 26ed57b2 2018-05-19 stsp usage_diff();
967 26ed57b2 2018-05-19 stsp
968 26ed57b2 2018-05-19 stsp error = got_repo_open(&repo, repo_path);
969 26ed57b2 2018-05-19 stsp free(repo_path);
970 26ed57b2 2018-05-19 stsp if (error)
971 26ed57b2 2018-05-19 stsp goto done;
972 26ed57b2 2018-05-19 stsp
973 26ed57b2 2018-05-19 stsp error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
974 26ed57b2 2018-05-19 stsp if (error)
975 26ed57b2 2018-05-19 stsp goto done;
976 26ed57b2 2018-05-19 stsp
977 26ed57b2 2018-05-19 stsp error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
978 26ed57b2 2018-05-19 stsp if (error)
979 26ed57b2 2018-05-19 stsp goto done;
980 26ed57b2 2018-05-19 stsp
981 26ed57b2 2018-05-19 stsp error = show_diff_view(obj1, obj2, repo);
982 26ed57b2 2018-05-19 stsp done:
983 26ed57b2 2018-05-19 stsp got_repo_close(repo);
984 26ed57b2 2018-05-19 stsp if (obj1)
985 26ed57b2 2018-05-19 stsp got_object_close(obj1);
986 26ed57b2 2018-05-19 stsp if (obj2)
987 26ed57b2 2018-05-19 stsp got_object_close(obj2);
988 26ed57b2 2018-05-19 stsp return error;
989 9f7d7167 2018-04-29 stsp }
990 9f7d7167 2018-04-29 stsp
991 4ed7e80c 2018-05-20 stsp __dead static void
992 9f7d7167 2018-04-29 stsp usage_blame(void)
993 9f7d7167 2018-04-29 stsp {
994 80ddbec8 2018-04-29 stsp endwin();
995 9f7d7167 2018-04-29 stsp fprintf(stderr, "usage: %s blame [repository-path] blob-object\n",
996 9f7d7167 2018-04-29 stsp getprogname());
997 9f7d7167 2018-04-29 stsp exit(1);
998 9f7d7167 2018-04-29 stsp }
999 9f7d7167 2018-04-29 stsp
1000 4ed7e80c 2018-05-20 stsp static const struct got_error *
1001 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
1002 9f7d7167 2018-04-29 stsp {
1003 9f7d7167 2018-04-29 stsp return got_error(GOT_ERR_NOT_IMPL);
1004 9f7d7167 2018-04-29 stsp }
1005 9f7d7167 2018-04-29 stsp
1006 5c5136c5 2018-05-20 stsp static void
1007 9f7d7167 2018-04-29 stsp init_curses(void)
1008 9f7d7167 2018-04-29 stsp {
1009 9f7d7167 2018-04-29 stsp initscr();
1010 9f7d7167 2018-04-29 stsp cbreak();
1011 9f7d7167 2018-04-29 stsp noecho();
1012 9f7d7167 2018-04-29 stsp nonl();
1013 9f7d7167 2018-04-29 stsp intrflush(stdscr, FALSE);
1014 9f7d7167 2018-04-29 stsp keypad(stdscr, TRUE);
1015 1f475ad8 2018-05-10 stsp curs_set(0);
1016 9f7d7167 2018-04-29 stsp }
1017 9f7d7167 2018-04-29 stsp
1018 4ed7e80c 2018-05-20 stsp __dead static void
1019 9f7d7167 2018-04-29 stsp usage(void)
1020 9f7d7167 2018-04-29 stsp {
1021 9f7d7167 2018-04-29 stsp int i;
1022 9f7d7167 2018-04-29 stsp
1023 c2301be8 2018-04-30 stsp fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
1024 9f7d7167 2018-04-29 stsp "Available commands:\n", getprogname());
1025 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
1026 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = &tog_commands[i];
1027 c2301be8 2018-04-30 stsp fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
1028 9f7d7167 2018-04-29 stsp }
1029 9f7d7167 2018-04-29 stsp exit(1);
1030 9f7d7167 2018-04-29 stsp }
1031 9f7d7167 2018-04-29 stsp
1032 c2301be8 2018-04-30 stsp static char **
1033 c2301be8 2018-04-30 stsp make_argv(const char *arg0, const char *arg1)
1034 c2301be8 2018-04-30 stsp {
1035 c2301be8 2018-04-30 stsp char **argv;
1036 c2301be8 2018-04-30 stsp int argc = (arg1 == NULL ? 1 : 2);
1037 c2301be8 2018-04-30 stsp
1038 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
1039 c2301be8 2018-04-30 stsp if (argv == NULL)
1040 c2301be8 2018-04-30 stsp err(1, "calloc");
1041 c2301be8 2018-04-30 stsp argv[0] = strdup(arg0);
1042 c2301be8 2018-04-30 stsp if (argv[0] == NULL)
1043 c2301be8 2018-04-30 stsp err(1, "calloc");
1044 c2301be8 2018-04-30 stsp if (arg1) {
1045 c2301be8 2018-04-30 stsp argv[1] = strdup(arg1);
1046 c2301be8 2018-04-30 stsp if (argv[1] == NULL)
1047 c2301be8 2018-04-30 stsp err(1, "calloc");
1048 c2301be8 2018-04-30 stsp }
1049 c2301be8 2018-04-30 stsp
1050 c2301be8 2018-04-30 stsp return argv;
1051 c2301be8 2018-04-30 stsp }
1052 c2301be8 2018-04-30 stsp
1053 9f7d7167 2018-04-29 stsp int
1054 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
1055 9f7d7167 2018-04-29 stsp {
1056 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
1057 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
1058 9f7d7167 2018-04-29 stsp int ch, hflag = 0;
1059 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
1060 9f7d7167 2018-04-29 stsp
1061 9f7d7167 2018-04-29 stsp setlocale(LC_ALL, "");
1062 9f7d7167 2018-04-29 stsp
1063 9f7d7167 2018-04-29 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
1064 9f7d7167 2018-04-29 stsp switch (ch) {
1065 9f7d7167 2018-04-29 stsp case 'h':
1066 9f7d7167 2018-04-29 stsp hflag = 1;
1067 9f7d7167 2018-04-29 stsp break;
1068 9f7d7167 2018-04-29 stsp default:
1069 9f7d7167 2018-04-29 stsp usage();
1070 9f7d7167 2018-04-29 stsp /* NOTREACHED */
1071 9f7d7167 2018-04-29 stsp }
1072 9f7d7167 2018-04-29 stsp }
1073 9f7d7167 2018-04-29 stsp
1074 9f7d7167 2018-04-29 stsp argc -= optind;
1075 9f7d7167 2018-04-29 stsp argv += optind;
1076 9f7d7167 2018-04-29 stsp optind = 0;
1077 c2301be8 2018-04-30 stsp optreset = 1;
1078 9f7d7167 2018-04-29 stsp
1079 c2301be8 2018-04-30 stsp if (argc == 0) {
1080 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
1081 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
1082 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, NULL);
1083 c2301be8 2018-04-30 stsp argc = 1;
1084 c2301be8 2018-04-30 stsp } else {
1085 9f7d7167 2018-04-29 stsp int i;
1086 9f7d7167 2018-04-29 stsp
1087 c2301be8 2018-04-30 stsp /* Did the user specific a command? */
1088 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
1089 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
1090 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
1091 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
1092 9f7d7167 2018-04-29 stsp if (hflag)
1093 9f7d7167 2018-04-29 stsp tog_commands[i].cmd_usage();
1094 9f7d7167 2018-04-29 stsp break;
1095 9f7d7167 2018-04-29 stsp }
1096 9f7d7167 2018-04-29 stsp }
1097 9f7d7167 2018-04-29 stsp if (cmd == NULL) {
1098 c2301be8 2018-04-30 stsp /* Did the user specify a repository? */
1099 c2301be8 2018-04-30 stsp char *repo_path = realpath(argv[0], NULL);
1100 c2301be8 2018-04-30 stsp if (repo_path) {
1101 c2301be8 2018-04-30 stsp struct got_repository *repo;
1102 c2301be8 2018-04-30 stsp error = got_repo_open(&repo, repo_path);
1103 c2301be8 2018-04-30 stsp if (error == NULL)
1104 c2301be8 2018-04-30 stsp got_repo_close(repo);
1105 c2301be8 2018-04-30 stsp } else
1106 ad7de8d9 2018-04-30 stsp error = got_error_from_errno();
1107 c2301be8 2018-04-30 stsp if (error) {
1108 ad7de8d9 2018-04-30 stsp fprintf(stderr, "%s: '%s' is neither a known "
1109 ad7de8d9 2018-04-30 stsp "command nor a path to a repository\n",
1110 c2301be8 2018-04-30 stsp getprogname(), argv[0]);
1111 ad7de8d9 2018-04-30 stsp free(repo_path);
1112 c2301be8 2018-04-30 stsp return 1;
1113 c2301be8 2018-04-30 stsp }
1114 c2301be8 2018-04-30 stsp cmd = &tog_commands[0];
1115 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, repo_path);
1116 c2301be8 2018-04-30 stsp argc = 2;
1117 c2301be8 2018-04-30 stsp free(repo_path);
1118 9f7d7167 2018-04-29 stsp }
1119 9f7d7167 2018-04-29 stsp }
1120 9f7d7167 2018-04-29 stsp
1121 5c5136c5 2018-05-20 stsp init_curses();
1122 9f7d7167 2018-04-29 stsp
1123 c2301be8 2018-04-30 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
1124 9f7d7167 2018-04-29 stsp if (error)
1125 9f7d7167 2018-04-29 stsp goto done;
1126 9f7d7167 2018-04-29 stsp done:
1127 9f7d7167 2018-04-29 stsp endwin();
1128 c2301be8 2018-04-30 stsp free(cmd_argv);
1129 9f7d7167 2018-04-29 stsp if (error)
1130 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
1131 9f7d7167 2018-04-29 stsp return 0;
1132 9f7d7167 2018-04-29 stsp }