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