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