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