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