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