2 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/queue.h>
20 #define _XOPEN_SOURCE_EXTENDED
22 #undef _XOPEN_SOURCE_EXTENDED
36 #include "got_error.h"
37 #include "got_object.h"
38 #include "got_reference.h"
39 #include "got_repository.h"
41 #include "got_opentemp.h"
42 #include "got_commit_graph.h"
44 #include "got_blame.h"
47 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
51 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
56 const struct got_error *(*cmd_main)(int, char *[]);
57 void (*cmd_usage)(void);
61 __dead static void usage(void);
62 __dead static void usage_log(void);
63 __dead static void usage_diff(void);
64 __dead static void usage_blame(void);
66 static const struct got_error* cmd_log(int, char *[]);
67 static const struct got_error* cmd_diff(int, char *[]);
68 static const struct got_error* cmd_blame(int, char *[]);
70 static struct tog_cmd tog_commands[] = {
71 { "log", cmd_log, usage_log,
72 "show repository history" },
73 { "diff", cmd_diff, usage_diff,
74 "compare files and directories" },
75 { "blame", cmd_blame, usage_blame,
76 "show line-by-line file history" },
79 static struct tog_view {
82 } tog_log_view, tog_diff_view, tog_blame_view;
84 static const struct got_error *
85 show_diff_view(struct got_object *, struct got_object *,
86 struct got_repository *);
87 static const struct got_error *
88 show_log_view(struct got_object_id *, struct got_repository *);
89 static const struct got_error *
90 show_blame_view(const char *, struct got_object_id *, struct got_repository *);
96 fprintf(stderr, "usage: %s log [-c commit] [repository-path]\n",
101 /* Create newly allocated wide-character string equivalent to a byte string. */
102 static const struct got_error *
103 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
106 const struct got_error *err = NULL;
109 *wlen = mbstowcs(NULL, s, 0);
110 if (*wlen == (size_t)-1) {
113 return got_error_from_errno();
115 /* byte string invalid in current encoding; try to "fix" it */
116 err = got_mbsavis(&vis, &vislen, s);
119 *wlen = mbstowcs(NULL, vis, 0);
120 if (*wlen == (size_t)-1) {
121 err = got_error_from_errno(); /* give up */
126 *ws = calloc(*wlen + 1, sizeof(*ws));
128 err = got_error_from_errno();
132 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
133 err = got_error_from_errno();
144 /* Format a line for display, ensuring that it won't overflow a width limit. */
145 static const struct got_error *
146 format_line(wchar_t **wlinep, int *widthp, char *line, int wlimit)
148 const struct got_error *err = NULL;
150 wchar_t *wline = NULL;
156 err = mbs2ws(&wline, &wlen, line);
161 while (i < wlen && cols <= wlimit) {
162 int width = wcwidth(wline[i]);
171 if (wline[i] == L'\t')
175 err = got_error_from_errno();
193 static const struct got_error *
194 draw_commit(struct got_commit_object *commit, struct got_object_id *id)
196 const struct got_error *err = NULL;
197 char *logmsg0 = NULL, *logmsg = NULL;
198 char *author0 = NULL, *author = NULL;
199 wchar_t *wlogmsg = NULL, *wauthor = NULL;
200 int author_width, logmsg_width;
201 char *newline, *smallerthan;
206 static const size_t id_display_cols = 8;
207 static const size_t author_display_cols = 16;
208 const int avail = COLS;
210 err = got_object_id_str(&id_str, id);
213 id_len = strlen(id_str);
214 if (avail < id_display_cols) {
215 limit = MIN(id_len, avail);
216 waddnstr(tog_log_view.window, id_str, limit);
218 limit = MIN(id_display_cols, id_len);
219 waddnstr(tog_log_view.window, id_str, limit);
222 while (col <= avail && col < id_display_cols + 2) {
223 waddch(tog_log_view.window, ' ');
229 author0 = strdup(commit->author);
230 if (author0 == NULL) {
231 err = got_error_from_errno();
235 smallerthan = strchr(author, '<');
239 char *at = strchr(author, '@');
243 limit = MIN(avail, author_display_cols);
244 err = format_line(&wauthor, &author_width, author, limit);
247 waddwstr(tog_log_view.window, wauthor);
249 while (col <= avail && author_width < author_display_cols + 1) {
250 waddch(tog_log_view.window, ' ');
257 logmsg0 = strdup(commit->logmsg);
258 if (logmsg0 == NULL) {
259 err = got_error_from_errno();
263 while (*logmsg == '\n')
265 newline = strchr(logmsg, '\n');
269 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
272 waddwstr(tog_log_view.window, wlogmsg);
274 while (col <= avail) {
275 waddch(tog_log_view.window, ' ');
288 struct commit_queue_entry {
289 TAILQ_ENTRY(commit_queue_entry) entry;
290 struct got_object_id *id;
291 struct got_commit_object *commit;
293 TAILQ_HEAD(commit_queue, commit_queue_entry);
295 static struct commit_queue_entry *
296 alloc_commit_queue_entry(struct got_commit_object *commit,
297 struct got_object_id *id)
299 struct commit_queue_entry *entry;
301 entry = calloc(1, sizeof(*entry));
306 entry->commit = commit;
311 pop_commit(struct commit_queue *commits)
313 struct commit_queue_entry *entry;
315 entry = TAILQ_FIRST(commits);
316 TAILQ_REMOVE(commits, entry, entry);
317 got_object_commit_close(entry->commit);
318 /* Don't free entry->id! It is owned by the commit graph. */
323 free_commits(struct commit_queue *commits)
325 while (!TAILQ_EMPTY(commits))
329 static const struct got_error *
330 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
331 struct got_object_id *start_id, struct got_repository *repo)
333 const struct got_error *err = NULL;
334 struct got_object_id *id;
335 struct commit_queue_entry *entry;
337 err = got_commit_graph_iter_start(graph, start_id);
341 entry = TAILQ_LAST(commits, commit_queue);
342 if (entry && got_object_id_cmp(entry->id, start_id) == 0) {
345 /* Start ID's commit is already on the queue; skip over it. */
346 err = got_commit_graph_iter_next(&id, graph);
347 if (err && err->code != GOT_ERR_ITER_NEED_MORE)
350 err = got_commit_graph_fetch_commits(&nfetched, graph, 1, repo);
356 struct got_commit_object *commit;
358 err = got_commit_graph_iter_next(&id, graph);
360 if (err->code == GOT_ERR_ITER_NEED_MORE)
365 err = got_object_open_as_commit(&commit, repo, id);
369 entry = alloc_commit_queue_entry(commit, id);
371 err = got_error_from_errno();
375 TAILQ_INSERT_TAIL(commits, entry, entry);
381 static const struct got_error *
382 fetch_next_commit(struct commit_queue_entry **pentry,
383 struct commit_queue_entry *entry, struct commit_queue *commits,
384 struct got_commit_graph *graph, struct got_repository *repo)
386 const struct got_error *err = NULL;
387 struct got_object_qid *qid;
391 /* Populate commit graph with entry's parent commits. */
392 SIMPLEQ_FOREACH(qid, &entry->commit->parent_ids, entry) {
394 err = got_commit_graph_fetch_commits_up_to(&nfetched,
395 graph, qid->id, repo);
400 /* Append outstanding commits to queue in graph sort order. */
401 err = queue_commits(graph, commits, entry->id, repo);
403 if (err->code == GOT_ERR_ITER_COMPLETED)
408 /* Next entry to display should now be available. */
409 *pentry = TAILQ_NEXT(entry, entry);
411 return got_error(GOT_ERR_NO_OBJ);
416 static const struct got_error *
417 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
419 const struct got_error *err = NULL;
420 struct got_reference *head_ref;
424 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
428 err = got_ref_resolve(head_id, repo, head_ref);
429 got_ref_close(head_ref);
438 static const struct got_error *
439 draw_commits(struct commit_queue_entry **last, struct commit_queue_entry **selected,
440 struct commit_queue_entry *first, int selected_idx, int limit)
442 const struct got_error *err = NULL;
443 struct commit_queue_entry *entry;
446 werase(tog_log_view.window);
451 if (ncommits == limit)
453 if (ncommits == selected_idx) {
454 wstandout(tog_log_view.window);
457 err = draw_commit(entry->commit, entry->id);
458 if (ncommits == selected_idx)
459 wstandend(tog_log_view.window);
464 entry = TAILQ_NEXT(entry, entry);
474 scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
475 struct commit_queue *commits)
477 struct commit_queue_entry *entry;
480 entry = TAILQ_FIRST(commits);
481 if (*first_displayed_entry == entry)
484 entry = *first_displayed_entry;
485 while (entry && nscrolled < maxscroll) {
486 entry = TAILQ_PREV(entry, commit_queue, entry);
488 *first_displayed_entry = entry;
494 static const struct got_error *
495 scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
496 struct commit_queue_entry *last_displayed_entry,
497 struct commit_queue *commits, struct got_commit_graph *graph,
498 struct got_repository *repo)
500 const struct got_error *err = NULL;
501 struct commit_queue_entry *pentry;
505 pentry = TAILQ_NEXT(last_displayed_entry, entry);
506 if (pentry == NULL) {
507 err = fetch_next_commit(&pentry, last_displayed_entry,
508 commits, graph, repo);
509 if (err || pentry == NULL)
512 last_displayed_entry = pentry;
514 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
517 *first_displayed_entry = pentry;
518 } while (++nscrolled < maxscroll);
524 num_parents(struct commit_queue_entry *entry)
529 entry = TAILQ_NEXT(entry, entry);
536 static const struct got_error *
537 show_commit(struct commit_queue_entry *entry, struct got_repository *repo)
539 const struct got_error *err;
540 struct got_object *obj1 = NULL, *obj2 = NULL;
541 struct got_object_qid *parent_id;
543 err = got_object_open(&obj2, repo, entry->id);
547 parent_id = SIMPLEQ_FIRST(&entry->commit->parent_ids);
549 err = got_object_open(&obj1, repo, parent_id->id);
554 err = show_diff_view(obj1, obj2, repo);
557 got_object_close(obj1);
559 got_object_close(obj2);
563 static const struct got_error *
564 show_log_view(struct got_object_id *start_id, struct got_repository *repo)
566 const struct got_error *err = NULL;
567 struct got_object_id *head_id = NULL;
568 int ch, done = 0, selected = 0, nparents, nfetched;
569 struct got_commit_graph *graph;
570 struct commit_queue commits;
571 struct commit_queue_entry *entry = NULL;
572 struct commit_queue_entry *first_displayed_entry = NULL;
573 struct commit_queue_entry *last_displayed_entry = NULL;
574 struct commit_queue_entry *selected_entry = NULL;
576 if (tog_log_view.window == NULL) {
577 tog_log_view.window = newwin(0, 0, 0, 0);
578 if (tog_log_view.window == NULL)
579 return got_error_from_errno();
580 keypad(tog_log_view.window, TRUE);
582 if (tog_log_view.panel == NULL) {
583 tog_log_view.panel = new_panel(tog_log_view.window);
584 if (tog_log_view.panel == NULL)
585 return got_error_from_errno();
587 show_panel(tog_log_view.panel);
589 err = get_head_commit_id(&head_id, repo);
593 TAILQ_INIT(&commits);
595 err = got_commit_graph_open(&graph, head_id, 0, repo);
599 /* Populate commit graph with a sufficient number of commits. */
600 err = got_commit_graph_fetch_commits_up_to(&nfetched, graph, start_id,
604 err = got_commit_graph_fetch_commits(&nfetched, graph, LINES, repo);
609 * Open the initial batch of commits, sorted in commit graph order.
610 * We keep all commits open throughout the lifetime of the log view
611 * in order to avoid having to re-fetch commits from disk while
612 * updating the display.
614 err = queue_commits(graph, &commits, head_id, repo);
615 if (err && err->code != GOT_ERR_ITER_COMPLETED)
618 /* Find entry corresponding to the first commit to display. */
619 TAILQ_FOREACH(entry, &commits, entry) {
620 if (got_object_id_cmp(entry->id, start_id) == 0) {
621 first_displayed_entry = entry;
625 if (first_displayed_entry == NULL) {
626 err = got_error(GOT_ERR_NO_OBJ);
631 err = draw_commits(&last_displayed_entry, &selected_entry,
632 first_displayed_entry, selected, LINES);
636 nodelay(stdscr, FALSE);
637 ch = wgetch(tog_log_view.window);
638 nodelay(stdscr, TRUE);
642 err = got_error_from_errno();
655 scroll_up(&first_displayed_entry, 1, &commits);
658 if (TAILQ_FIRST(&commits) ==
659 first_displayed_entry) {
663 scroll_up(&first_displayed_entry, LINES,
668 nparents = num_parents(first_displayed_entry);
669 if (selected < LINES - 1 &&
670 selected < nparents - 1) {
674 err = scroll_down(&first_displayed_entry, 1,
675 last_displayed_entry, &commits, graph,
681 err = scroll_down(&first_displayed_entry, LINES,
682 last_displayed_entry, &commits, graph,
686 if (last_displayed_entry->commit->nparents > 0)
688 /* can't scroll any further; move cursor down */
689 nparents = num_parents(first_displayed_entry);
690 if (selected < LINES - 1 ||
691 selected < nparents - 1)
692 selected = MIN(LINES - 1, nparents - 1);
695 if (selected > LINES)
696 selected = LINES - 1;
700 err = show_commit(selected_entry, repo);
703 show_panel(tog_log_view.panel);
712 got_commit_graph_close(graph);
713 free_commits(&commits);
717 static const struct got_error *
718 cmd_log(int argc, char *argv[])
720 const struct got_error *error;
721 struct got_repository *repo;
722 struct got_object_id *start_id = NULL;
723 char *repo_path = NULL;
724 char *start_commit = NULL;
728 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
732 while ((ch = getopt(argc, argv, "c:")) != -1) {
735 start_commit = optarg;
747 repo_path = getcwd(NULL, 0);
748 if (repo_path == NULL)
749 return got_error_from_errno();
750 } else if (argc == 1) {
751 repo_path = realpath(argv[0], NULL);
752 if (repo_path == NULL)
753 return got_error_from_errno();
757 error = got_repo_open(&repo, repo_path);
762 if (start_commit == NULL) {
763 error = get_head_commit_id(&start_id, repo);
767 struct got_object *obj;
768 error = got_object_open_by_id_str(&obj, repo, start_commit);
770 start_id = got_object_get_id(obj);
771 if (start_id == NULL)
772 error = got_error_from_errno();
777 error = show_log_view(start_id, repo);
779 got_repo_close(repo);
787 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
793 parse_next_line(FILE *f, size_t *len)
798 const char delim[3] = { '\0', '\0', '\0'};
800 line = fparseln(f, &linelen, &lineno, delim, 0);
806 static const struct got_error *
807 draw_file(WINDOW *window, FILE *f, int *first_displayed_line,
808 int *last_displayed_line, int *eof, int max_lines)
810 const struct got_error *err;
811 int nlines = 0, nprinted = 0;
821 while (nprinted < max_lines) {
822 line = parse_next_line(f, &len);
827 if (++nlines < *first_displayed_line) {
832 err = format_line(&wline, &width, line, COLS);
837 waddwstr(window, wline);
839 waddch(window, '\n');
841 *first_displayed_line = nlines;
844 *last_displayed_line = nlines;
852 static const struct got_error *
853 show_diff_view(struct got_object *obj1, struct got_object *obj2,
854 struct got_repository *repo)
856 const struct got_error *err;
858 int ch, done = 0, first_displayed_line = 1, last_displayed_line = LINES;
861 if (obj1 != NULL && obj2 != NULL &&
862 got_object_get_type(obj1) != got_object_get_type(obj2))
863 return got_error(GOT_ERR_OBJ_TYPE);
867 return got_error_from_errno();
869 switch (got_object_get_type(obj1 ? obj1 : obj2)) {
870 case GOT_OBJ_TYPE_BLOB:
871 err = got_diff_objects_as_blobs(obj1, obj2, repo, f);
873 case GOT_OBJ_TYPE_TREE:
874 err = got_diff_objects_as_trees(obj1, obj2, repo, f);
876 case GOT_OBJ_TYPE_COMMIT:
877 err = got_diff_objects_as_commits(obj1, obj2, repo, f);
880 return got_error(GOT_ERR_OBJ_TYPE);
885 if (tog_diff_view.window == NULL) {
886 tog_diff_view.window = newwin(0, 0, 0, 0);
887 if (tog_diff_view.window == NULL)
888 return got_error_from_errno();
889 keypad(tog_diff_view.window, TRUE);
891 if (tog_diff_view.panel == NULL) {
892 tog_diff_view.panel = new_panel(tog_diff_view.window);
893 if (tog_diff_view.panel == NULL)
894 return got_error_from_errno();
896 show_panel(tog_diff_view.panel);
899 err = draw_file(tog_diff_view.window, f, &first_displayed_line,
900 &last_displayed_line, &eof, LINES);
903 nodelay(stdscr, FALSE);
904 ch = wgetch(tog_diff_view.window);
905 nodelay(stdscr, TRUE);
913 if (first_displayed_line > 1)
914 first_displayed_line--;
918 while (i++ < LINES - 1 &&
919 first_displayed_line > 1)
920 first_displayed_line--;
927 first_displayed_line++;
932 while (!eof && i++ < LINES - 1) {
933 char *line = parse_next_line(f, NULL);
934 first_displayed_line++;
947 static const struct got_error *
948 cmd_diff(int argc, char *argv[])
950 const struct got_error *error = NULL;
951 struct got_repository *repo = NULL;
952 struct got_object *obj1 = NULL, *obj2 = NULL;
953 char *repo_path = NULL;
954 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
958 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
962 while ((ch = getopt(argc, argv, "")) != -1) {
974 usage_diff(); /* TODO show local worktree changes */
975 } else if (argc == 2) {
976 repo_path = getcwd(NULL, 0);
977 if (repo_path == NULL)
978 return got_error_from_errno();
979 obj_id_str1 = argv[0];
980 obj_id_str2 = argv[1];
981 } else if (argc == 3) {
982 repo_path = realpath(argv[0], NULL);
983 if (repo_path == NULL)
984 return got_error_from_errno();
985 obj_id_str1 = argv[1];
986 obj_id_str2 = argv[2];
990 error = got_repo_open(&repo, repo_path);
995 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
999 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1003 error = show_diff_view(obj1, obj2, repo);
1005 got_repo_close(repo);
1007 got_object_close(obj1);
1009 got_object_close(obj2);
1017 fprintf(stderr, "usage: %s blame [-c commit] [repository-path] path\n",
1022 static const struct got_error *
1023 show_blame_view(const char *path, struct got_object_id *commit_id,
1024 struct got_repository *repo)
1026 const struct got_error *err = NULL;
1028 int ch, done = 0, first_displayed_line = 1, last_displayed_line = LINES;
1033 return got_error_from_errno();
1035 err = got_blame(path, commit_id, repo, f);
1041 if (tog_blame_view.window == NULL) {
1042 tog_blame_view.window = newwin(0, 0, 0, 0);
1043 if (tog_blame_view.window == NULL)
1044 return got_error_from_errno();
1045 keypad(tog_blame_view.window, TRUE);
1047 if (tog_blame_view.panel == NULL) {
1048 tog_blame_view.panel = new_panel(tog_blame_view.window);
1049 if (tog_blame_view.panel == NULL)
1050 return got_error_from_errno();
1052 show_panel(tog_blame_view.panel);
1055 err = draw_file(tog_blame_view.window, f, &first_displayed_line,
1056 &last_displayed_line, &eof, LINES);
1059 nodelay(stdscr, FALSE);
1060 ch = wgetch(tog_blame_view.window);
1061 nodelay(stdscr, TRUE);
1069 if (first_displayed_line > 1)
1070 first_displayed_line--;
1074 while (i++ < LINES - 1 &&
1075 first_displayed_line > 1)
1076 first_displayed_line--;
1083 first_displayed_line++;
1088 while (!eof && i++ < LINES - 1) {
1089 char *line = parse_next_line(f, NULL);
1090 first_displayed_line++;
1104 static const struct got_error *
1105 cmd_blame(int argc, char *argv[])
1107 const struct got_error *error;
1108 struct got_repository *repo = NULL;
1109 char *repo_path = NULL;
1111 struct got_object_id *commit_id = NULL;
1112 char *commit_id_str = NULL;
1116 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1120 while ((ch = getopt(argc, argv, "c:")) != -1) {
1123 commit_id_str = optarg;
1136 } else if (argc == 1) {
1137 repo_path = getcwd(NULL, 0);
1138 if (repo_path == NULL)
1139 return got_error_from_errno();
1141 } else if (argc == 2) {
1142 repo_path = realpath(argv[0], NULL);
1143 if (repo_path == NULL)
1144 return got_error_from_errno();
1149 error = got_repo_open(&repo, repo_path);
1154 if (commit_id_str == NULL) {
1155 struct got_reference *head_ref;
1156 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1159 error = got_ref_resolve(&commit_id, repo, head_ref);
1160 got_ref_close(head_ref);
1164 struct got_object *obj;
1165 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
1168 commit_id = got_object_get_id(obj);
1169 if (commit_id == NULL)
1170 error = got_error_from_errno();
1171 got_object_close(obj);
1174 error = show_blame_view(path, commit_id, repo);
1178 got_repo_close(repo);
1189 intrflush(stdscr, FALSE);
1190 keypad(stdscr, TRUE);
1199 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
1200 "Available commands:\n", getprogname());
1201 for (i = 0; i < nitems(tog_commands); i++) {
1202 struct tog_cmd *cmd = &tog_commands[i];
1203 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
1209 make_argv(const char *arg0, const char *arg1)
1212 int argc = (arg1 == NULL ? 1 : 2);
1214 argv = calloc(argc, sizeof(char *));
1217 argv[0] = strdup(arg0);
1218 if (argv[0] == NULL)
1221 argv[1] = strdup(arg1);
1222 if (argv[1] == NULL)
1230 main(int argc, char *argv[])
1232 const struct got_error *error = NULL;
1233 struct tog_cmd *cmd = NULL;
1235 char **cmd_argv = NULL;
1237 setlocale(LC_ALL, "");
1239 while ((ch = getopt(argc, argv, "h")) != -1) {
1256 /* Build an argument vector which runs a default command. */
1257 cmd = &tog_commands[0];
1258 cmd_argv = make_argv(cmd->name, NULL);
1263 /* Did the user specific a command? */
1264 for (i = 0; i < nitems(tog_commands); i++) {
1265 if (strncmp(tog_commands[i].name, argv[0],
1266 strlen(argv[0])) == 0) {
1267 cmd = &tog_commands[i];
1269 tog_commands[i].cmd_usage();
1274 /* Did the user specify a repository? */
1275 char *repo_path = realpath(argv[0], NULL);
1277 struct got_repository *repo;
1278 error = got_repo_open(&repo, repo_path);
1280 got_repo_close(repo);
1282 error = got_error_from_errno();
1284 fprintf(stderr, "%s: '%s' is neither a known "
1285 "command nor a path to a repository\n",
1286 getprogname(), argv[0]);
1290 cmd = &tog_commands[0];
1291 cmd_argv = make_argv(cmd->name, repo_path);
1299 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
1306 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);