2 9f7d7167 2018-04-29 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
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.
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.
17 80ddbec8 2018-04-29 stsp #include <sys/queue.h>
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>
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"
40 881b2d3e 2018-04-30 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
43 9f7d7167 2018-04-29 stsp #ifndef nitems
44 9f7d7167 2018-04-29 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
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;
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);
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 *[]);
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" },
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;
77 4ed7e80c 2018-05-20 stsp __dead static void
78 9f7d7167 2018-04-29 stsp usage_log(void)
81 80ddbec8 2018-04-29 stsp fprintf(stderr, "usage: %s log [-c commit] [repository-path]\n",
82 9f7d7167 2018-04-29 stsp getprogname());
86 80ddbec8 2018-04-29 stsp static const struct got_error *
87 0553a4e3 2018-04-30 stsp draw_commit(struct got_commit_object *commit, struct got_object_id *id)
89 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
90 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
91 6d9fbc00 2018-04-29 stsp char *author0 = NULL, *author = NULL;
92 6d9fbc00 2018-04-29 stsp char *newline, *smallerthan;
93 80ddbec8 2018-04-29 stsp char *line = NULL;
94 6d9fbc00 2018-04-29 stsp char *id_str = NULL;
95 881b2d3e 2018-04-30 stsp const size_t id_display_len = 8;
96 881b2d3e 2018-04-30 stsp const size_t author_display_len = 16;
97 881b2d3e 2018-04-30 stsp size_t id_len, author_len, logmsg_len, avail;
100 80ddbec8 2018-04-29 stsp err = got_object_id_str(&id_str, id);
102 80ddbec8 2018-04-29 stsp return err;
103 881b2d3e 2018-04-30 stsp id_len = strlen(id_str);
105 80ddbec8 2018-04-29 stsp logmsg0 = strdup(commit->logmsg);
106 6d9fbc00 2018-04-29 stsp if (logmsg0 == NULL) {
107 6d9fbc00 2018-04-29 stsp err = got_error_from_errno();
110 80ddbec8 2018-04-29 stsp logmsg = logmsg0;
111 80ddbec8 2018-04-29 stsp while (*logmsg == '\n')
113 80ddbec8 2018-04-29 stsp newline = strchr(logmsg, '\n');
114 6d9fbc00 2018-04-29 stsp if (newline)
115 80ddbec8 2018-04-29 stsp *newline = '\0';
116 881b2d3e 2018-04-30 stsp logmsg_len = strlen(logmsg);
118 6d9fbc00 2018-04-29 stsp author0 = strdup(commit->author);
119 6d9fbc00 2018-04-29 stsp if (author0 == NULL) {
120 80ddbec8 2018-04-29 stsp err = got_error_from_errno();
123 6d9fbc00 2018-04-29 stsp author = author0;
124 6d9fbc00 2018-04-29 stsp smallerthan = strchr(author, '<');
125 6d9fbc00 2018-04-29 stsp if (smallerthan)
126 6d9fbc00 2018-04-29 stsp *smallerthan = '\0';
128 6d9fbc00 2018-04-29 stsp char *at = strchr(author, '@');
130 6d9fbc00 2018-04-29 stsp *at = '\0';
132 881b2d3e 2018-04-30 stsp author_len = strlen(author);
134 881b2d3e 2018-04-30 stsp avail = COLS - 1;
135 881b2d3e 2018-04-30 stsp line = calloc(avail + 1, sizeof(*line));
136 881b2d3e 2018-04-30 stsp if (line == NULL) {
137 6d9fbc00 2018-04-29 stsp err = got_error_from_errno();
142 881b2d3e 2018-04-30 stsp for (i = 0; i < MIN(id_display_len, id_len); i++) {
143 881b2d3e 2018-04-30 stsp if (col >= avail)
145 881b2d3e 2018-04-30 stsp line[col++] = id_str[i];
147 881b2d3e 2018-04-30 stsp while (i < id_display_len) {
148 881b2d3e 2018-04-30 stsp if (col >= avail)
150 881b2d3e 2018-04-30 stsp line[col++] = ' ';
153 881b2d3e 2018-04-30 stsp if (col >= avail)
155 881b2d3e 2018-04-30 stsp line[col++] = ' ';
156 881b2d3e 2018-04-30 stsp for (i = 0; i < MIN(author_display_len, author_len); i++) {
157 881b2d3e 2018-04-30 stsp if (col >= avail)
159 881b2d3e 2018-04-30 stsp line[col++] = author[i];
161 881b2d3e 2018-04-30 stsp while (i < author_display_len) {
162 881b2d3e 2018-04-30 stsp if (col >= avail)
164 881b2d3e 2018-04-30 stsp line[col++] = ' ';
167 881b2d3e 2018-04-30 stsp if (col >= avail)
169 881b2d3e 2018-04-30 stsp line[col++] = ' ';
171 881b2d3e 2018-04-30 stsp while (col < avail && *logmsg)
172 881b2d3e 2018-04-30 stsp line[col++] = *logmsg++;
173 881b2d3e 2018-04-30 stsp while (col < avail)
174 881b2d3e 2018-04-30 stsp line[col++] = ' ';
176 881b2d3e 2018-04-30 stsp waddstr(tog_log_view.window, line);
177 80ddbec8 2018-04-29 stsp waddch(tog_log_view.window, '\n');
179 80ddbec8 2018-04-29 stsp free(logmsg0);
180 6d9fbc00 2018-04-29 stsp free(author0);
181 80ddbec8 2018-04-29 stsp free(line);
182 6d9fbc00 2018-04-29 stsp free(id_str);
183 80ddbec8 2018-04-29 stsp return err;
186 80ddbec8 2018-04-29 stsp struct commit_queue_entry {
187 80ddbec8 2018-04-29 stsp TAILQ_ENTRY(commit_queue_entry) entry;
188 80ddbec8 2018-04-29 stsp struct got_object_id *id;
189 80ddbec8 2018-04-29 stsp struct got_commit_object *commit;
191 0553a4e3 2018-04-30 stsp TAILQ_HEAD(commit_queue, commit_queue_entry);
193 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
194 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
195 899d86c2 2018-05-10 stsp struct got_object_id *id)
197 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
199 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
200 80ddbec8 2018-04-29 stsp if (entry == NULL)
201 899d86c2 2018-05-10 stsp return NULL;
203 899d86c2 2018-05-10 stsp entry->id = id;
204 99db9666 2018-05-07 stsp entry->commit = commit;
205 899d86c2 2018-05-10 stsp return entry;
208 99db9666 2018-05-07 stsp static void
209 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
211 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
213 99db9666 2018-05-07 stsp entry = TAILQ_FIRST(commits);
214 99db9666 2018-05-07 stsp TAILQ_REMOVE(commits, entry, entry);
215 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
216 99db9666 2018-05-07 stsp free(entry->id);
217 99db9666 2018-05-07 stsp free(entry);
220 99db9666 2018-05-07 stsp static void
221 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
223 99db9666 2018-05-07 stsp while (!TAILQ_EMPTY(commits))
224 99db9666 2018-05-07 stsp pop_commit(commits);
228 c4972b91 2018-05-07 stsp static const struct got_error *
229 899d86c2 2018-05-10 stsp fetch_parent_commit(struct commit_queue_entry **pentry,
230 899d86c2 2018-05-10 stsp struct commit_queue_entry *entry, struct got_repository *repo)
232 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
233 c4972b91 2018-05-07 stsp struct got_object *obj = NULL;
234 899d86c2 2018-05-10 stsp struct got_commit_object *commit;
235 899d86c2 2018-05-10 stsp struct got_object_id *id;
236 c4972b91 2018-05-07 stsp struct got_parent_id *pid;
238 899d86c2 2018-05-10 stsp *pentry = NULL;
240 c4972b91 2018-05-07 stsp /* Follow the first parent (TODO: handle merge commits). */
241 c4972b91 2018-05-07 stsp pid = SIMPLEQ_FIRST(&entry->commit->parent_ids);
242 c4972b91 2018-05-07 stsp if (pid == NULL)
243 c4972b91 2018-05-07 stsp return NULL;
244 c4972b91 2018-05-07 stsp err = got_object_open(&obj, repo, pid->id);
246 c4972b91 2018-05-07 stsp return err;
247 c4972b91 2018-05-07 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
248 c4972b91 2018-05-07 stsp err = got_error(GOT_ERR_OBJ_TYPE);
249 c4972b91 2018-05-07 stsp got_object_close(obj);
250 c4972b91 2018-05-07 stsp return err;
253 899d86c2 2018-05-10 stsp err = got_object_commit_open(&commit, repo, obj);
254 c4972b91 2018-05-07 stsp got_object_close(obj);
256 c4972b91 2018-05-07 stsp return err;
258 899d86c2 2018-05-10 stsp id = got_object_id_dup(pid->id);
259 899d86c2 2018-05-10 stsp if (id == NULL) {
260 c4972b91 2018-05-07 stsp err = got_error_from_errno();
261 899d86c2 2018-05-10 stsp got_object_commit_close(commit);
262 899d86c2 2018-05-10 stsp return err;;
265 899d86c2 2018-05-10 stsp *pentry = alloc_commit_queue_entry(commit, id);
266 899d86c2 2018-05-10 stsp if (*pentry == NULL) {
267 c4972b91 2018-05-07 stsp err = got_error_from_errno();
268 899d86c2 2018-05-10 stsp got_object_commit_close(commit);
271 899d86c2 2018-05-10 stsp return err;;
274 899d86c2 2018-05-10 stsp static const struct got_error *
275 899d86c2 2018-05-10 stsp get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
277 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
278 899d86c2 2018-05-10 stsp struct got_reference *head_ref;
280 899d86c2 2018-05-10 stsp *head_id = NULL;
282 899d86c2 2018-05-10 stsp err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
284 899d86c2 2018-05-10 stsp return err;
286 899d86c2 2018-05-10 stsp err = got_ref_resolve(head_id, repo, head_ref);
287 899d86c2 2018-05-10 stsp got_ref_close(head_ref);
289 899d86c2 2018-05-10 stsp *head_id = NULL;
290 899d86c2 2018-05-10 stsp return err;
293 c4972b91 2018-05-07 stsp return NULL;
296 99db9666 2018-05-07 stsp static const struct got_error *
297 899d86c2 2018-05-10 stsp prepend_commits(int *ncommits, struct commit_queue *commits,
298 899d86c2 2018-05-10 stsp struct got_object_id *first_id, struct got_object_id *last_id,
299 899d86c2 2018-05-10 stsp int limit, struct got_repository *repo)
301 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
302 899d86c2 2018-05-10 stsp struct got_object *first_obj = NULL, *last_obj = NULL;
303 899d86c2 2018-05-10 stsp struct got_commit_object *commit = NULL;
304 899d86c2 2018-05-10 stsp struct got_object_id *id = NULL;
305 899d86c2 2018-05-10 stsp struct commit_queue_entry *entry, *old_head_entry;
307 899d86c2 2018-05-10 stsp *ncommits = 0;
309 899d86c2 2018-05-10 stsp err = got_object_open(&first_obj, repo, first_id);
312 899d86c2 2018-05-10 stsp if (got_object_get_type(first_obj) != GOT_OBJ_TYPE_COMMIT) {
313 899d86c2 2018-05-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
316 899d86c2 2018-05-10 stsp err = got_object_open(&last_obj, repo, last_id);
319 899d86c2 2018-05-10 stsp if (got_object_get_type(last_obj) != GOT_OBJ_TYPE_COMMIT) {
320 899d86c2 2018-05-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
324 899d86c2 2018-05-10 stsp err = got_object_commit_open(&commit, repo, first_obj);
328 899d86c2 2018-05-10 stsp id = got_object_id_dup(first_id);
329 899d86c2 2018-05-10 stsp if (id == NULL) {
330 899d86c2 2018-05-10 stsp err = got_error_from_errno();
334 899d86c2 2018-05-10 stsp entry = alloc_commit_queue_entry(commit, id);
335 899d86c2 2018-05-10 stsp if (entry == NULL)
336 899d86c2 2018-05-10 stsp return got_error_from_errno();
338 899d86c2 2018-05-10 stsp old_head_entry = TAILQ_FIRST(commits);
339 899d86c2 2018-05-10 stsp if (old_head_entry)
340 899d86c2 2018-05-10 stsp TAILQ_INSERT_BEFORE(old_head_entry, entry, entry);
342 899d86c2 2018-05-10 stsp TAILQ_INSERT_HEAD(commits, entry, entry);
344 899d86c2 2018-05-10 stsp *ncommits = 1;
347 899d86c2 2018-05-10 stsp * Fetch parent commits.
348 899d86c2 2018-05-10 stsp * XXX If first and last commit aren't ancestrally related this loop
349 899d86c2 2018-05-10 stsp * we will keep iterating until a root commit is encountered.
351 899d86c2 2018-05-10 stsp while (1) {
352 899d86c2 2018-05-10 stsp struct commit_queue_entry *pentry;
354 899d86c2 2018-05-10 stsp err = fetch_parent_commit(&pentry, entry, repo);
357 899d86c2 2018-05-10 stsp if (pentry == NULL)
361 899d86c2 2018-05-10 stsp * Fill up to old HEAD commit if commit queue was not empty.
362 899d86c2 2018-05-10 stsp * We must not leave a gap in history.
364 899d86c2 2018-05-10 stsp if (old_head_entry &&
365 899d86c2 2018-05-10 stsp got_object_id_cmp(pentry->id, old_head_entry->id) == 0)
368 899d86c2 2018-05-10 stsp TAILQ_INSERT_AFTER(commits, entry, pentry, entry);
369 899d86c2 2018-05-10 stsp (*ncommits)++;
370 899d86c2 2018-05-10 stsp if (*ncommits >= limit)
373 899d86c2 2018-05-10 stsp /* Fill up to last requested commit if queue was empty. */
374 899d86c2 2018-05-10 stsp if (old_head_entry == NULL &&
375 899d86c2 2018-05-10 stsp got_object_id_cmp(pentry->id, last_id) == 0)
378 899d86c2 2018-05-10 stsp entry = pentry;
382 899d86c2 2018-05-10 stsp if (first_obj)
383 899d86c2 2018-05-10 stsp got_object_close(first_obj);
384 899d86c2 2018-05-10 stsp if (last_obj)
385 899d86c2 2018-05-10 stsp got_object_close(last_obj);
386 899d86c2 2018-05-10 stsp return err;
389 899d86c2 2018-05-10 stsp static const struct got_error *
390 899d86c2 2018-05-10 stsp fetch_commits(struct commit_queue_entry **start_entry,
391 899d86c2 2018-05-10 stsp struct got_object_id *start_id, struct commit_queue *commits,
392 899d86c2 2018-05-10 stsp int limit, struct got_repository *repo)
394 99db9666 2018-05-07 stsp const struct got_error *err;
395 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
396 99db9666 2018-05-07 stsp int ncommits = 0;
397 899d86c2 2018-05-10 stsp struct got_object_id *head_id = NULL;
399 899d86c2 2018-05-10 stsp *start_entry = NULL;
401 899d86c2 2018-05-10 stsp err = get_head_commit_id(&head_id, repo);
403 99db9666 2018-05-07 stsp return err;
405 899d86c2 2018-05-10 stsp /* Prepend HEAD commit and all ancestors up to start commit. */
406 899d86c2 2018-05-10 stsp err = prepend_commits(&ncommits, commits, head_id, start_id, limit,
409 99db9666 2018-05-07 stsp return err;
411 899d86c2 2018-05-10 stsp if (got_object_id_cmp(head_id, start_id) == 0)
412 899d86c2 2018-05-10 stsp *start_entry = TAILQ_FIRST(commits);
414 899d86c2 2018-05-10 stsp *start_entry = TAILQ_LAST(commits, commit_queue);
416 899d86c2 2018-05-10 stsp if (ncommits >= limit)
417 899d86c2 2018-05-10 stsp return NULL;
419 899d86c2 2018-05-10 stsp /* Append more commits from start commit up to the requested limit. */
420 899d86c2 2018-05-10 stsp entry = TAILQ_LAST(commits, commit_queue);
421 899d86c2 2018-05-10 stsp while (entry && ncommits < limit) {
422 899d86c2 2018-05-10 stsp struct commit_queue_entry *pentry;
424 899d86c2 2018-05-10 stsp err = fetch_parent_commit(&pentry, entry, repo);
427 899d86c2 2018-05-10 stsp if (pentry)
428 899d86c2 2018-05-10 stsp TAILQ_INSERT_TAIL(commits, pentry, entry);
429 899d86c2 2018-05-10 stsp entry = pentry;
430 0553a4e3 2018-04-30 stsp ncommits++;
434 899d86c2 2018-05-10 stsp *start_entry = NULL;
435 0553a4e3 2018-04-30 stsp return err;
438 0553a4e3 2018-04-30 stsp static const struct got_error *
439 4a7f7875 2018-05-10 stsp draw_commits(struct commit_queue_entry **last, struct commit_queue_entry *first,
440 4a7f7875 2018-05-10 stsp int selected, int limit)
442 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
443 0553a4e3 2018-04-30 stsp struct commit_queue_entry *entry;
444 0553a4e3 2018-04-30 stsp int ncommits = 0;
446 0553a4e3 2018-04-30 stsp wclear(tog_log_view.window);
448 899d86c2 2018-05-10 stsp entry = first;
449 899d86c2 2018-05-10 stsp *last = first;
450 899d86c2 2018-05-10 stsp while (entry) {
451 899d86c2 2018-05-10 stsp if (ncommits == limit)
453 0553a4e3 2018-04-30 stsp if (ncommits == selected)
454 0553a4e3 2018-04-30 stsp wstandout(tog_log_view.window);
455 0553a4e3 2018-04-30 stsp err = draw_commit(entry->commit, entry->id);
456 0553a4e3 2018-04-30 stsp if (ncommits == selected)
457 0553a4e3 2018-04-30 stsp wstandend(tog_log_view.window);
460 0553a4e3 2018-04-30 stsp ncommits++;
461 899d86c2 2018-05-10 stsp *last = entry;
462 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
465 80ddbec8 2018-04-29 stsp update_panels();
466 80ddbec8 2018-04-29 stsp doupdate();
468 80ddbec8 2018-04-29 stsp return err;
471 07b55e75 2018-05-10 stsp static void
472 07b55e75 2018-05-10 stsp scroll_up(struct commit_queue_entry **first_displayed_entry, int n,
473 07b55e75 2018-05-10 stsp struct commit_queue *commits)
475 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
476 07b55e75 2018-05-10 stsp int nscrolled = 0;
478 07b55e75 2018-05-10 stsp entry = TAILQ_FIRST(commits);
479 07b55e75 2018-05-10 stsp if (*first_displayed_entry == entry)
482 07b55e75 2018-05-10 stsp entry = *first_displayed_entry;
483 07b55e75 2018-05-10 stsp while (entry && nscrolled < n) {
484 07b55e75 2018-05-10 stsp entry = TAILQ_PREV(entry, commit_queue, entry);
485 07b55e75 2018-05-10 stsp if (entry) {
486 07b55e75 2018-05-10 stsp *first_displayed_entry = entry;
487 07b55e75 2018-05-10 stsp nscrolled++;
492 aa075928 2018-05-10 stsp static const struct got_error *
493 aa075928 2018-05-10 stsp scroll_down(struct commit_queue_entry **first_displayed_entry, int n,
494 aa075928 2018-05-10 stsp struct commit_queue_entry *last_displayed_entry,
495 aa075928 2018-05-10 stsp struct commit_queue *commits, struct got_repository *repo)
497 aa075928 2018-05-10 stsp const struct got_error *err = NULL;
498 aa075928 2018-05-10 stsp struct commit_queue_entry *entry;
499 aa075928 2018-05-10 stsp int nscrolled = 0;
501 aa075928 2018-05-10 stsp if (last_displayed_entry->commit->nparents == 0)
502 aa075928 2018-05-10 stsp return NULL;
504 aa075928 2018-05-10 stsp entry = *first_displayed_entry;
506 aa075928 2018-05-10 stsp struct commit_queue_entry *pentry;
508 aa075928 2018-05-10 stsp pentry = TAILQ_NEXT(entry, entry);
509 aa075928 2018-05-10 stsp if (pentry == NULL) {
510 aa075928 2018-05-10 stsp err = fetch_parent_commit(&pentry, entry, repo);
513 aa075928 2018-05-10 stsp if (pentry == NULL) {
514 aa075928 2018-05-10 stsp *first_displayed_entry = entry;
515 aa075928 2018-05-10 stsp return NULL;
517 aa075928 2018-05-10 stsp TAILQ_INSERT_TAIL(commits, pentry, entry);
518 aa075928 2018-05-10 stsp last_displayed_entry = pentry;
521 aa075928 2018-05-10 stsp *first_displayed_entry = pentry;
522 aa075928 2018-05-10 stsp entry = pentry;
524 aa075928 2018-05-10 stsp if (TAILQ_LAST(commits, commit_queue) == last_displayed_entry) {
525 aa075928 2018-05-10 stsp err = fetch_parent_commit(&pentry, last_displayed_entry,
529 aa075928 2018-05-10 stsp if (pentry) {
530 aa075928 2018-05-10 stsp TAILQ_INSERT_TAIL(commits, pentry, entry);
531 aa075928 2018-05-10 stsp last_displayed_entry = pentry;
534 aa075928 2018-05-10 stsp } while (++nscrolled < n);
536 aa075928 2018-05-10 stsp return NULL;
540 4a7f7875 2018-05-10 stsp num_parents(struct commit_queue_entry *entry)
542 4a7f7875 2018-05-10 stsp int nparents = 0;
544 4a7f7875 2018-05-10 stsp while (entry) {
545 4a7f7875 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
546 4a7f7875 2018-05-10 stsp nparents++;
549 4a7f7875 2018-05-10 stsp return nparents;
552 80ddbec8 2018-04-29 stsp static const struct got_error *
553 80ddbec8 2018-04-29 stsp show_log_view(struct got_object_id *start_id, struct got_repository *repo)
555 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
556 c4972b91 2018-05-07 stsp struct got_object_id *id;
557 4a7f7875 2018-05-10 stsp int ch, done = 0, selected = 0, nparents;
558 0553a4e3 2018-04-30 stsp struct commit_queue commits;
559 899d86c2 2018-05-10 stsp struct commit_queue_entry *first_displayed_entry = NULL;
560 899d86c2 2018-05-10 stsp struct commit_queue_entry *last_displayed_entry = NULL;
562 c4972b91 2018-05-07 stsp id = got_object_id_dup(start_id);
563 c4972b91 2018-05-07 stsp if (id == NULL)
564 c4972b91 2018-05-07 stsp return got_error_from_errno();
566 80ddbec8 2018-04-29 stsp if (tog_log_view.window == NULL) {
567 80ddbec8 2018-04-29 stsp tog_log_view.window = newwin(0, 0, 0, 0);
568 80ddbec8 2018-04-29 stsp if (tog_log_view.window == NULL)
569 80ddbec8 2018-04-29 stsp return got_error_from_errno();
570 00a838fa 2018-04-29 stsp keypad(tog_log_view.window, TRUE);
572 80ddbec8 2018-04-29 stsp if (tog_log_view.panel == NULL) {
573 80ddbec8 2018-04-29 stsp tog_log_view.panel = new_panel(tog_log_view.window);
574 80ddbec8 2018-04-29 stsp if (tog_log_view.panel == NULL)
575 80ddbec8 2018-04-29 stsp return got_error_from_errno();
578 899d86c2 2018-05-10 stsp TAILQ_INIT(&commits);
579 899d86c2 2018-05-10 stsp err = fetch_commits(&first_displayed_entry, id, &commits, LINES, repo);
582 899d86c2 2018-05-10 stsp while (!done) {
583 899d86c2 2018-05-10 stsp err = draw_commits(&last_displayed_entry, first_displayed_entry,
584 899d86c2 2018-05-10 stsp selected, LINES);
588 80ddbec8 2018-04-29 stsp nodelay(stdscr, FALSE);
589 80ddbec8 2018-04-29 stsp ch = wgetch(tog_log_view.window);
590 80ddbec8 2018-04-29 stsp switch (ch) {
592 31120ada 2018-04-30 stsp if (errno) {
593 31120ada 2018-04-30 stsp err = got_error_from_errno();
601 80ddbec8 2018-04-29 stsp case KEY_UP:
602 80ddbec8 2018-04-29 stsp if (selected > 0)
603 80ddbec8 2018-04-29 stsp selected--;
604 8ec9698d 2018-05-10 stsp if (selected > 0)
606 07b55e75 2018-05-10 stsp scroll_up(&first_displayed_entry, 1, &commits);
608 48531068 2018-05-10 stsp case KEY_PPAGE:
609 dfc1d240 2018-05-10 stsp if (TAILQ_FIRST(&commits) ==
610 dfc1d240 2018-05-10 stsp first_displayed_entry) {
611 dfc1d240 2018-05-10 stsp selected = 0;
614 48531068 2018-05-10 stsp scroll_up(&first_displayed_entry, LINES,
618 80ddbec8 2018-04-29 stsp case KEY_DOWN:
619 80ee4603 2018-05-10 stsp nparents = num_parents(first_displayed_entry);
620 06abe2cd 2018-05-10 stsp if (selected < LINES - 1 &&
621 06abe2cd 2018-05-10 stsp selected < nparents - 1)
622 80ddbec8 2018-04-29 stsp selected++;
623 06abe2cd 2018-05-10 stsp if (selected < LINES - 1 &&
624 06abe2cd 2018-05-10 stsp selected < nparents - 1)
626 aa075928 2018-05-10 stsp err = scroll_down(&first_displayed_entry, 1,
627 aa075928 2018-05-10 stsp last_displayed_entry, &commits, repo);
631 4a7f7875 2018-05-10 stsp case KEY_NPAGE:
632 4a7f7875 2018-05-10 stsp nparents = num_parents(first_displayed_entry);
633 dfc1d240 2018-05-10 stsp if (nparents < LINES - 1 &&
634 dfc1d240 2018-05-10 stsp selected < nparents - 1) {
635 dfc1d240 2018-05-10 stsp selected = nparents - 1;
638 e50ee4f1 2018-05-10 stsp err = scroll_down(&first_displayed_entry, LINES,
639 e50ee4f1 2018-05-10 stsp last_displayed_entry, &commits, repo);
642 4a7f7875 2018-05-10 stsp nparents = num_parents(first_displayed_entry);
643 dfc1d240 2018-05-10 stsp if (selected > nparents)
644 dfc1d240 2018-05-10 stsp selected = nparents - 1;
646 d6df9be4 2018-04-30 stsp case KEY_RESIZE:
647 31120ada 2018-04-30 stsp if (selected > LINES)
648 31120ada 2018-04-30 stsp selected = LINES - 1;
653 80ddbec8 2018-04-29 stsp nodelay(stdscr, TRUE);
656 0553a4e3 2018-04-30 stsp free_commits(&commits);
657 80ddbec8 2018-04-29 stsp return err;
660 4ed7e80c 2018-05-20 stsp static const struct got_error *
661 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
663 80ddbec8 2018-04-29 stsp const struct got_error *error;
664 80ddbec8 2018-04-29 stsp struct got_repository *repo;
665 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
666 80ddbec8 2018-04-29 stsp char *repo_path = NULL;
667 80ddbec8 2018-04-29 stsp char *start_commit = NULL;
670 80ddbec8 2018-04-29 stsp #ifndef PROFILE
671 80ddbec8 2018-04-29 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
672 80ddbec8 2018-04-29 stsp err(1, "pledge");
675 80ddbec8 2018-04-29 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
676 80ddbec8 2018-04-29 stsp switch (ch) {
678 80ddbec8 2018-04-29 stsp start_commit = optarg;
682 80ddbec8 2018-04-29 stsp /* NOTREACHED */
686 80ddbec8 2018-04-29 stsp argc -= optind;
687 80ddbec8 2018-04-29 stsp argv += optind;
689 80ddbec8 2018-04-29 stsp if (argc == 0) {
690 80ddbec8 2018-04-29 stsp repo_path = getcwd(NULL, 0);
691 80ddbec8 2018-04-29 stsp if (repo_path == NULL)
692 80ddbec8 2018-04-29 stsp return got_error_from_errno();
693 80ddbec8 2018-04-29 stsp } else if (argc == 1) {
694 80ddbec8 2018-04-29 stsp repo_path = realpath(argv[0], NULL);
695 80ddbec8 2018-04-29 stsp if (repo_path == NULL)
696 80ddbec8 2018-04-29 stsp return got_error_from_errno();
698 80ddbec8 2018-04-29 stsp usage_log();
700 80ddbec8 2018-04-29 stsp error = got_repo_open(&repo, repo_path);
701 80ddbec8 2018-04-29 stsp free(repo_path);
702 80ddbec8 2018-04-29 stsp if (error != NULL)
703 80ddbec8 2018-04-29 stsp return error;
705 80ddbec8 2018-04-29 stsp if (start_commit == NULL) {
706 899d86c2 2018-05-10 stsp error = get_head_commit_id(&start_id, repo);
707 80ddbec8 2018-04-29 stsp if (error != NULL)
708 80ddbec8 2018-04-29 stsp return error;
710 80ddbec8 2018-04-29 stsp struct got_object *obj;
711 80ddbec8 2018-04-29 stsp error = got_object_open_by_id_str(&obj, repo, start_commit);
712 80ddbec8 2018-04-29 stsp if (error == NULL) {
713 899d86c2 2018-05-10 stsp start_id = got_object_get_id(obj);
714 899d86c2 2018-05-10 stsp if (start_id == NULL)
715 80ddbec8 2018-04-29 stsp error = got_error_from_errno();
718 80ddbec8 2018-04-29 stsp if (error != NULL)
719 80ddbec8 2018-04-29 stsp return error;
720 899d86c2 2018-05-10 stsp error = show_log_view(start_id, repo);
721 899d86c2 2018-05-10 stsp free(start_id);
722 80ddbec8 2018-04-29 stsp got_repo_close(repo);
723 80ddbec8 2018-04-29 stsp return error;
726 4ed7e80c 2018-05-20 stsp __dead static void
727 9f7d7167 2018-04-29 stsp usage_diff(void)
730 9f7d7167 2018-04-29 stsp fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
731 9f7d7167 2018-04-29 stsp getprogname());
735 4ed7e80c 2018-05-20 stsp static const struct got_error *
736 26ed57b2 2018-05-19 stsp draw_diff(FILE *f, int *first_displayed_line, int *last_displayed_line,
737 26ed57b2 2018-05-19 stsp int *eof, int max_lines)
739 26ed57b2 2018-05-19 stsp int nlines = 0, nprinted = 0;
742 26ed57b2 2018-05-19 stsp wclear(tog_diff_view.window);
745 26ed57b2 2018-05-19 stsp while (nprinted < max_lines) {
746 26ed57b2 2018-05-19 stsp char *line;
747 26ed57b2 2018-05-19 stsp size_t lineno;
748 26ed57b2 2018-05-19 stsp size_t linelen;
749 26ed57b2 2018-05-19 stsp const char delim[3] = { '\0', '\0', '\0'};
751 26ed57b2 2018-05-19 stsp line = fparseln(f, &linelen, &lineno, delim, 0);
752 26ed57b2 2018-05-19 stsp if (line == NULL) {
756 26ed57b2 2018-05-19 stsp if (++nlines < *first_displayed_line) {
757 26ed57b2 2018-05-19 stsp free(line);
761 26ed57b2 2018-05-19 stsp if (linelen > COLS - 1)
762 26ed57b2 2018-05-19 stsp line[COLS - 1] = '\0';
763 26ed57b2 2018-05-19 stsp waddstr(tog_diff_view.window, line);
764 26ed57b2 2018-05-19 stsp waddch(tog_diff_view.window, '\n');
765 26ed57b2 2018-05-19 stsp if (++nprinted == 1)
766 26ed57b2 2018-05-19 stsp *first_displayed_line = nlines;
767 26ed57b2 2018-05-19 stsp free(line);
769 26ed57b2 2018-05-19 stsp *last_displayed_line = nlines;
771 26ed57b2 2018-05-19 stsp update_panels();
772 26ed57b2 2018-05-19 stsp doupdate();
774 26ed57b2 2018-05-19 stsp return NULL;
777 4ed7e80c 2018-05-20 stsp static const struct got_error *
778 26ed57b2 2018-05-19 stsp show_diff_view(struct got_object *obj1, struct got_object *obj2,
779 26ed57b2 2018-05-19 stsp struct got_repository *repo)
781 26ed57b2 2018-05-19 stsp const struct got_error *err;
783 26ed57b2 2018-05-19 stsp int ch, done = 0, first_displayed_line = 1, last_displayed_line = LINES;
786 26ed57b2 2018-05-19 stsp if (got_object_get_type(obj1) != got_object_get_type(obj2))
787 26ed57b2 2018-05-19 stsp return got_error(GOT_ERR_OBJ_TYPE);
789 511a516b 2018-05-19 stsp f = got_opentemp();
790 26ed57b2 2018-05-19 stsp if (f == NULL)
791 26ed57b2 2018-05-19 stsp return got_error_from_errno();
793 26ed57b2 2018-05-19 stsp switch (got_object_get_type(obj1)) {
794 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
795 11528a82 2018-05-19 stsp err = got_diff_objects_as_blobs(obj1, obj2, repo, f);
797 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
798 11528a82 2018-05-19 stsp err = got_diff_objects_as_trees(obj1, obj2, repo, f);
800 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_COMMIT:
801 11528a82 2018-05-19 stsp err = got_diff_objects_as_commits(obj1, obj2, repo, f);
804 26ed57b2 2018-05-19 stsp return got_error(GOT_ERR_OBJ_TYPE);
809 26ed57b2 2018-05-19 stsp if (tog_diff_view.window == NULL) {
810 26ed57b2 2018-05-19 stsp tog_diff_view.window = newwin(0, 0, 0, 0);
811 26ed57b2 2018-05-19 stsp if (tog_diff_view.window == NULL)
812 26ed57b2 2018-05-19 stsp return got_error_from_errno();
813 26ed57b2 2018-05-19 stsp keypad(tog_diff_view.window, TRUE);
815 26ed57b2 2018-05-19 stsp if (tog_diff_view.panel == NULL) {
816 26ed57b2 2018-05-19 stsp tog_diff_view.panel = new_panel(tog_diff_view.window);
817 26ed57b2 2018-05-19 stsp if (tog_diff_view.panel == NULL)
818 26ed57b2 2018-05-19 stsp return got_error_from_errno();
820 26ed57b2 2018-05-19 stsp show_panel(tog_diff_view.panel);
822 26ed57b2 2018-05-19 stsp while (!done) {
823 26ed57b2 2018-05-19 stsp err = draw_diff(f, &first_displayed_line, &last_displayed_line,
824 26ed57b2 2018-05-19 stsp &eof, LINES);
827 26ed57b2 2018-05-19 stsp nodelay(stdscr, FALSE);
828 26ed57b2 2018-05-19 stsp ch = wgetch(tog_diff_view.window);
829 26ed57b2 2018-05-19 stsp switch (ch) {
834 26ed57b2 2018-05-19 stsp case KEY_UP:
835 26ed57b2 2018-05-19 stsp if (first_displayed_line > 1)
836 26ed57b2 2018-05-19 stsp first_displayed_line--;
839 26ed57b2 2018-05-19 stsp case KEY_DOWN:
841 26ed57b2 2018-05-19 stsp first_displayed_line++;
846 26ed57b2 2018-05-19 stsp nodelay(stdscr, TRUE);
849 26ed57b2 2018-05-19 stsp return err;
852 4ed7e80c 2018-05-20 stsp static const struct got_error *
853 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
855 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
856 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
857 26ed57b2 2018-05-19 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
858 26ed57b2 2018-05-19 stsp char *repo_path = NULL;
859 26ed57b2 2018-05-19 stsp char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
862 26ed57b2 2018-05-19 stsp #ifndef PROFILE
863 26ed57b2 2018-05-19 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
864 26ed57b2 2018-05-19 stsp err(1, "pledge");
867 26ed57b2 2018-05-19 stsp while ((ch = getopt(argc, argv, "")) != -1) {
868 26ed57b2 2018-05-19 stsp switch (ch) {
871 26ed57b2 2018-05-19 stsp /* NOTREACHED */
875 26ed57b2 2018-05-19 stsp argc -= optind;
876 26ed57b2 2018-05-19 stsp argv += optind;
878 26ed57b2 2018-05-19 stsp if (argc == 0) {
879 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
880 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
881 26ed57b2 2018-05-19 stsp repo_path = getcwd(NULL, 0);
882 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
883 26ed57b2 2018-05-19 stsp return got_error_from_errno();
884 26ed57b2 2018-05-19 stsp obj_id_str1 = argv[0];
885 26ed57b2 2018-05-19 stsp obj_id_str2 = argv[1];
886 26ed57b2 2018-05-19 stsp } else if (argc == 3) {
887 26ed57b2 2018-05-19 stsp repo_path = realpath(argv[0], NULL);
888 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
889 26ed57b2 2018-05-19 stsp return got_error_from_errno();
890 26ed57b2 2018-05-19 stsp obj_id_str1 = argv[1];
891 26ed57b2 2018-05-19 stsp obj_id_str2 = argv[2];
893 26ed57b2 2018-05-19 stsp usage_diff();
895 26ed57b2 2018-05-19 stsp error = got_repo_open(&repo, repo_path);
896 26ed57b2 2018-05-19 stsp free(repo_path);
900 26ed57b2 2018-05-19 stsp error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
904 26ed57b2 2018-05-19 stsp error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
908 26ed57b2 2018-05-19 stsp error = show_diff_view(obj1, obj2, repo);
910 26ed57b2 2018-05-19 stsp got_repo_close(repo);
912 26ed57b2 2018-05-19 stsp got_object_close(obj1);
914 26ed57b2 2018-05-19 stsp got_object_close(obj2);
915 26ed57b2 2018-05-19 stsp return error;
918 4ed7e80c 2018-05-20 stsp __dead static void
919 9f7d7167 2018-04-29 stsp usage_blame(void)
922 9f7d7167 2018-04-29 stsp fprintf(stderr, "usage: %s blame [repository-path] blob-object\n",
923 9f7d7167 2018-04-29 stsp getprogname());
927 4ed7e80c 2018-05-20 stsp static const struct got_error *
928 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
930 9f7d7167 2018-04-29 stsp return got_error(GOT_ERR_NOT_IMPL);
933 5c5136c5 2018-05-20 stsp static void
934 9f7d7167 2018-04-29 stsp init_curses(void)
940 9f7d7167 2018-04-29 stsp intrflush(stdscr, FALSE);
941 9f7d7167 2018-04-29 stsp keypad(stdscr, TRUE);
942 1f475ad8 2018-05-10 stsp curs_set(0);
945 4ed7e80c 2018-05-20 stsp __dead static void
946 9f7d7167 2018-04-29 stsp usage(void)
950 c2301be8 2018-04-30 stsp fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
951 9f7d7167 2018-04-29 stsp "Available commands:\n", getprogname());
952 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
953 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = &tog_commands[i];
954 c2301be8 2018-04-30 stsp fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
959 c2301be8 2018-04-30 stsp static char **
960 c2301be8 2018-04-30 stsp make_argv(const char *arg0, const char *arg1)
962 c2301be8 2018-04-30 stsp char **argv;
963 c2301be8 2018-04-30 stsp int argc = (arg1 == NULL ? 1 : 2);
965 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
966 c2301be8 2018-04-30 stsp if (argv == NULL)
967 c2301be8 2018-04-30 stsp err(1, "calloc");
968 c2301be8 2018-04-30 stsp argv[0] = strdup(arg0);
969 c2301be8 2018-04-30 stsp if (argv[0] == NULL)
970 c2301be8 2018-04-30 stsp err(1, "calloc");
971 c2301be8 2018-04-30 stsp if (arg1) {
972 c2301be8 2018-04-30 stsp argv[1] = strdup(arg1);
973 c2301be8 2018-04-30 stsp if (argv[1] == NULL)
974 c2301be8 2018-04-30 stsp err(1, "calloc");
977 c2301be8 2018-04-30 stsp return argv;
981 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
983 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
984 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
985 9f7d7167 2018-04-29 stsp int ch, hflag = 0;
986 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
988 9f7d7167 2018-04-29 stsp setlocale(LC_ALL, "");
990 9f7d7167 2018-04-29 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
991 9f7d7167 2018-04-29 stsp switch (ch) {
997 9f7d7167 2018-04-29 stsp /* NOTREACHED */
1001 9f7d7167 2018-04-29 stsp argc -= optind;
1002 9f7d7167 2018-04-29 stsp argv += optind;
1003 9f7d7167 2018-04-29 stsp optind = 0;
1004 c2301be8 2018-04-30 stsp optreset = 1;
1006 c2301be8 2018-04-30 stsp if (argc == 0) {
1007 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
1008 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
1009 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, NULL);
1014 c2301be8 2018-04-30 stsp /* Did the user specific a command? */
1015 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
1016 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
1017 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
1018 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
1019 9f7d7167 2018-04-29 stsp if (hflag)
1020 9f7d7167 2018-04-29 stsp tog_commands[i].cmd_usage();
1024 9f7d7167 2018-04-29 stsp if (cmd == NULL) {
1025 c2301be8 2018-04-30 stsp /* Did the user specify a repository? */
1026 c2301be8 2018-04-30 stsp char *repo_path = realpath(argv[0], NULL);
1027 c2301be8 2018-04-30 stsp if (repo_path) {
1028 c2301be8 2018-04-30 stsp struct got_repository *repo;
1029 c2301be8 2018-04-30 stsp error = got_repo_open(&repo, repo_path);
1030 c2301be8 2018-04-30 stsp if (error == NULL)
1031 c2301be8 2018-04-30 stsp got_repo_close(repo);
1033 ad7de8d9 2018-04-30 stsp error = got_error_from_errno();
1034 c2301be8 2018-04-30 stsp if (error) {
1035 ad7de8d9 2018-04-30 stsp fprintf(stderr, "%s: '%s' is neither a known "
1036 ad7de8d9 2018-04-30 stsp "command nor a path to a repository\n",
1037 c2301be8 2018-04-30 stsp getprogname(), argv[0]);
1038 ad7de8d9 2018-04-30 stsp free(repo_path);
1041 c2301be8 2018-04-30 stsp cmd = &tog_commands[0];
1042 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, repo_path);
1044 c2301be8 2018-04-30 stsp free(repo_path);
1048 5c5136c5 2018-05-20 stsp init_curses();
1050 c2301be8 2018-04-30 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
1051 9f7d7167 2018-04-29 stsp if (error)
1052 9f7d7167 2018-04-29 stsp goto done;
1055 c2301be8 2018-04-30 stsp free(cmd_argv);
1056 9f7d7167 2018-04-29 stsp if (error)
1057 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);