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