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