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>
32 #include "got_error.h"
33 #include "got_object.h"
34 #include "got_reference.h"
35 #include "got_repository.h"
39 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
43 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
54 const struct got_error *(*cmd_main)(int, char *[]);
55 void (*cmd_usage)(void);
56 enum tog_view_id view;
60 __dead void usage(void);
61 __dead void usage_log(void);
62 __dead void usage_diff(void);
63 __dead void usage_blame(void);
65 const struct got_error* cmd_log(int, char *[]);
66 const struct got_error* cmd_diff(int, char *[]);
67 const struct got_error* cmd_blame(int, char *[]);
69 struct tog_cmd tog_commands[] = {
70 { "log", cmd_log, usage_log, TOG_VIEW_LOG,
71 "show repository history" },
72 { "diff", cmd_diff, usage_diff, TOG_VIEW_DIFF,
73 "compare files and directories" },
74 { "blame", cmd_blame, usage_blame, TOG_VIEW_BLAME,
75 "show line-by-line file history" },
80 PANEL *tog_main_panel;
81 static struct tog_log_view {
85 static struct tog_diff_view {
96 if (strlcpy(name, "/tmp/tog.XXXXXXXX", sizeof(name)) >= sizeof(name))
110 fd = tog_opentempfd();
114 f = fdopen(fd, "w+");
127 fprintf(stderr, "usage: %s log [-c commit] [repository-path]\n",
132 static const struct got_error *
133 draw_commit(struct got_commit_object *commit, struct got_object_id *id)
135 const struct got_error *err = NULL;
136 char *logmsg0 = NULL, *logmsg = NULL;
137 char *author0 = NULL, *author = NULL;
138 char *newline, *smallerthan;
141 const size_t id_display_len = 8;
142 const size_t author_display_len = 16;
143 size_t id_len, author_len, logmsg_len, avail;
146 err = got_object_id_str(&id_str, id);
149 id_len = strlen(id_str);
151 logmsg0 = strdup(commit->logmsg);
152 if (logmsg0 == NULL) {
153 err = got_error_from_errno();
157 while (*logmsg == '\n')
159 newline = strchr(logmsg, '\n');
162 logmsg_len = strlen(logmsg);
164 author0 = strdup(commit->author);
165 if (author0 == NULL) {
166 err = got_error_from_errno();
170 smallerthan = strchr(author, '<');
174 char *at = strchr(author, '@');
178 author_len = strlen(author);
181 line = calloc(avail + 1, sizeof(*line));
183 err = got_error_from_errno();
188 for (i = 0; i < MIN(id_display_len, id_len); i++) {
191 line[col++] = id_str[i];
193 while (i < id_display_len) {
202 for (i = 0; i < MIN(author_display_len, author_len); i++) {
205 line[col++] = author[i];
207 while (i < author_display_len) {
217 while (col < avail && *logmsg)
218 line[col++] = *logmsg++;
222 waddstr(tog_log_view.window, line);
223 waddch(tog_log_view.window, '\n');
232 struct commit_queue_entry {
233 TAILQ_ENTRY(commit_queue_entry) entry;
234 struct got_object_id *id;
235 struct got_commit_object *commit;
237 TAILQ_HEAD(commit_queue, commit_queue_entry);
239 static struct commit_queue_entry *
240 alloc_commit_queue_entry(struct got_commit_object *commit,
241 struct got_object_id *id)
243 struct commit_queue_entry *entry;
245 entry = calloc(1, sizeof(*entry));
250 entry->commit = commit;
255 pop_commit(struct commit_queue *commits)
257 struct commit_queue_entry *entry;
259 entry = TAILQ_FIRST(commits);
260 TAILQ_REMOVE(commits, entry, entry);
261 got_object_commit_close(entry->commit);
267 free_commits(struct commit_queue *commits)
269 while (!TAILQ_EMPTY(commits))
274 static const struct got_error *
275 fetch_parent_commit(struct commit_queue_entry **pentry,
276 struct commit_queue_entry *entry, struct got_repository *repo)
278 const struct got_error *err = NULL;
279 struct got_object *obj = NULL;
280 struct got_commit_object *commit;
281 struct got_object_id *id;
282 struct got_parent_id *pid;
286 /* Follow the first parent (TODO: handle merge commits). */
287 pid = SIMPLEQ_FIRST(&entry->commit->parent_ids);
290 err = got_object_open(&obj, repo, pid->id);
293 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
294 err = got_error(GOT_ERR_OBJ_TYPE);
295 got_object_close(obj);
299 err = got_object_commit_open(&commit, repo, obj);
300 got_object_close(obj);
304 id = got_object_id_dup(pid->id);
306 err = got_error_from_errno();
307 got_object_commit_close(commit);
311 *pentry = alloc_commit_queue_entry(commit, id);
312 if (*pentry == NULL) {
313 err = got_error_from_errno();
314 got_object_commit_close(commit);
320 static const struct got_error *
321 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
323 const struct got_error *err = NULL;
324 struct got_reference *head_ref;
328 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
332 err = got_ref_resolve(head_id, repo, head_ref);
333 got_ref_close(head_ref);
342 static const struct got_error *
343 prepend_commits(int *ncommits, struct commit_queue *commits,
344 struct got_object_id *first_id, struct got_object_id *last_id,
345 int limit, struct got_repository *repo)
347 const struct got_error *err = NULL;
348 struct got_object *first_obj = NULL, *last_obj = NULL;
349 struct got_commit_object *commit = NULL;
350 struct got_object_id *id = NULL;
351 struct commit_queue_entry *entry, *old_head_entry;
355 err = got_object_open(&first_obj, repo, first_id);
358 if (got_object_get_type(first_obj) != GOT_OBJ_TYPE_COMMIT) {
359 err = got_error(GOT_ERR_OBJ_TYPE);
362 err = got_object_open(&last_obj, repo, last_id);
365 if (got_object_get_type(last_obj) != GOT_OBJ_TYPE_COMMIT) {
366 err = got_error(GOT_ERR_OBJ_TYPE);
370 err = got_object_commit_open(&commit, repo, first_obj);
374 id = got_object_id_dup(first_id);
376 err = got_error_from_errno();
380 entry = alloc_commit_queue_entry(commit, id);
382 return got_error_from_errno();
384 old_head_entry = TAILQ_FIRST(commits);
386 TAILQ_INSERT_BEFORE(old_head_entry, entry, entry);
388 TAILQ_INSERT_HEAD(commits, entry, entry);
393 * Fetch parent commits.
394 * XXX If first and last commit aren't ancestrally related this loop
395 * we will keep iterating until a root commit is encountered.
398 struct commit_queue_entry *pentry;
400 err = fetch_parent_commit(&pentry, entry, repo);
407 * Fill up to old HEAD commit if commit queue was not empty.
408 * We must not leave a gap in history.
410 if (old_head_entry &&
411 got_object_id_cmp(pentry->id, old_head_entry->id) == 0)
414 TAILQ_INSERT_AFTER(commits, entry, pentry, entry);
416 if (*ncommits >= limit)
419 /* Fill up to last requested commit if queue was empty. */
420 if (old_head_entry == NULL &&
421 got_object_id_cmp(pentry->id, last_id) == 0)
429 got_object_close(first_obj);
431 got_object_close(last_obj);
435 static const struct got_error *
436 fetch_commits(struct commit_queue_entry **start_entry,
437 struct got_object_id *start_id, struct commit_queue *commits,
438 int limit, struct got_repository *repo)
440 const struct got_error *err;
441 struct commit_queue_entry *entry;
443 struct got_object_id *head_id = NULL;
447 err = get_head_commit_id(&head_id, repo);
451 /* Prepend HEAD commit and all ancestors up to start commit. */
452 err = prepend_commits(&ncommits, commits, head_id, start_id, limit,
457 if (got_object_id_cmp(head_id, start_id) == 0)
458 *start_entry = TAILQ_FIRST(commits);
460 *start_entry = TAILQ_LAST(commits, commit_queue);
462 if (ncommits >= limit)
465 /* Append more commits from start commit up to the requested limit. */
466 entry = TAILQ_LAST(commits, commit_queue);
467 while (entry && ncommits < limit) {
468 struct commit_queue_entry *pentry;
470 err = fetch_parent_commit(&pentry, entry, repo);
474 TAILQ_INSERT_TAIL(commits, pentry, entry);
484 static const struct got_error *
485 draw_commits(struct commit_queue_entry **last, struct commit_queue_entry *first,
486 int selected, int limit)
488 const struct got_error *err = NULL;
489 struct commit_queue_entry *entry;
492 wclear(tog_log_view.window);
497 if (ncommits == limit)
499 if (ncommits == selected)
500 wstandout(tog_log_view.window);
501 err = draw_commit(entry->commit, entry->id);
502 if (ncommits == selected)
503 wstandend(tog_log_view.window);
508 entry = TAILQ_NEXT(entry, entry);
518 scroll_up(struct commit_queue_entry **first_displayed_entry, int n,
519 struct commit_queue *commits)
521 struct commit_queue_entry *entry;
524 entry = TAILQ_FIRST(commits);
525 if (*first_displayed_entry == entry)
528 entry = *first_displayed_entry;
529 while (entry && nscrolled < n) {
530 entry = TAILQ_PREV(entry, commit_queue, entry);
532 *first_displayed_entry = entry;
538 static const struct got_error *
539 scroll_down(struct commit_queue_entry **first_displayed_entry, int n,
540 struct commit_queue_entry *last_displayed_entry,
541 struct commit_queue *commits, struct got_repository *repo)
543 const struct got_error *err = NULL;
544 struct commit_queue_entry *entry;
547 if (last_displayed_entry->commit->nparents == 0)
550 entry = *first_displayed_entry;
552 struct commit_queue_entry *pentry;
554 pentry = TAILQ_NEXT(entry, entry);
555 if (pentry == NULL) {
556 err = fetch_parent_commit(&pentry, entry, repo);
559 if (pentry == NULL) {
560 *first_displayed_entry = entry;
563 TAILQ_INSERT_TAIL(commits, pentry, entry);
564 last_displayed_entry = pentry;
567 *first_displayed_entry = pentry;
570 if (TAILQ_LAST(commits, commit_queue) == last_displayed_entry) {
571 err = fetch_parent_commit(&pentry, last_displayed_entry,
576 TAILQ_INSERT_TAIL(commits, pentry, entry);
577 last_displayed_entry = pentry;
580 } while (++nscrolled < n);
586 num_parents(struct commit_queue_entry *entry)
591 entry = TAILQ_NEXT(entry, entry);
598 static const struct got_error *
599 show_log_view(struct got_object_id *start_id, struct got_repository *repo)
601 const struct got_error *err = NULL;
602 struct got_object_id *id;
603 int ch, done = 0, selected = 0, nparents;
604 struct commit_queue commits;
605 struct commit_queue_entry *first_displayed_entry = NULL;
606 struct commit_queue_entry *last_displayed_entry = NULL;
608 id = got_object_id_dup(start_id);
610 return got_error_from_errno();
612 if (tog_log_view.window == NULL) {
613 tog_log_view.window = newwin(0, 0, 0, 0);
614 if (tog_log_view.window == NULL)
615 return got_error_from_errno();
616 keypad(tog_log_view.window, TRUE);
618 if (tog_log_view.panel == NULL) {
619 tog_log_view.panel = new_panel(tog_log_view.window);
620 if (tog_log_view.panel == NULL)
621 return got_error_from_errno();
624 TAILQ_INIT(&commits);
625 err = fetch_commits(&first_displayed_entry, id, &commits, LINES, repo);
629 err = draw_commits(&last_displayed_entry, first_displayed_entry,
634 nodelay(stdscr, FALSE);
635 ch = wgetch(tog_log_view.window);
639 err = got_error_from_errno();
652 scroll_up(&first_displayed_entry, 1, &commits);
655 if (TAILQ_FIRST(&commits) ==
656 first_displayed_entry) {
660 scroll_up(&first_displayed_entry, LINES,
665 nparents = num_parents(first_displayed_entry);
666 if (selected < LINES - 1 &&
667 selected < nparents - 1)
669 if (selected < LINES - 1 &&
670 selected < nparents - 1)
672 err = scroll_down(&first_displayed_entry, 1,
673 last_displayed_entry, &commits, repo);
678 nparents = num_parents(first_displayed_entry);
679 if (nparents < LINES - 1 &&
680 selected < nparents - 1) {
681 selected = nparents - 1;
684 err = scroll_down(&first_displayed_entry, LINES,
685 last_displayed_entry, &commits, repo);
688 nparents = num_parents(first_displayed_entry);
689 if (selected > nparents)
690 selected = nparents - 1;
693 if (selected > LINES)
694 selected = LINES - 1;
699 nodelay(stdscr, TRUE);
702 free_commits(&commits);
706 const struct got_error *
707 cmd_log(int argc, char *argv[])
709 const struct got_error *error;
710 struct got_repository *repo;
711 struct got_object_id *start_id = NULL;
712 char *repo_path = NULL;
713 char *start_commit = NULL;
717 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
721 while ((ch = getopt(argc, argv, "c:")) != -1) {
724 start_commit = optarg;
736 repo_path = getcwd(NULL, 0);
737 if (repo_path == NULL)
738 return got_error_from_errno();
739 } else if (argc == 1) {
740 repo_path = realpath(argv[0], NULL);
741 if (repo_path == NULL)
742 return got_error_from_errno();
746 error = got_repo_open(&repo, repo_path);
751 if (start_commit == NULL) {
752 error = get_head_commit_id(&start_id, repo);
756 struct got_object *obj;
757 error = got_object_open_by_id_str(&obj, repo, start_commit);
759 start_id = got_object_get_id(obj);
760 if (start_id == NULL)
761 error = got_error_from_errno();
766 error = show_log_view(start_id, repo);
768 got_repo_close(repo);
776 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
781 static const struct got_error *
782 diff_blobs(FILE *outfile, struct got_object *obj1, struct got_object *obj2,
783 struct got_repository *repo)
785 const struct got_error *err;
786 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
788 err = got_object_blob_open(&blob1, repo, obj1, 8192);
791 err = got_object_blob_open(&blob2, repo, obj2, 8192);
795 err = got_diff_blob(blob1, blob2, NULL, NULL, outfile);
798 got_object_blob_close(blob1);
800 got_object_blob_close(blob2);
804 static const struct got_error *
805 diff_trees(FILE *outfile, struct got_object *obj1, struct got_object *obj2,
806 struct got_repository *repo)
808 const struct got_error *err;
809 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
811 err = got_object_tree_open(&tree1, repo, obj1);
814 err = got_object_tree_open(&tree2, repo, obj2);
818 err = got_diff_tree(tree1, tree2, repo, outfile);
821 got_object_tree_close(tree1);
823 got_object_tree_close(tree2);
827 static const struct got_error *
828 diff_commits(FILE *outfile, struct got_object *obj1, struct got_object *obj2,
829 struct got_repository *repo)
831 const struct got_error *err;
832 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
833 struct got_object *tree_obj1 = NULL, *tree_obj2 = NULL;
835 err = got_object_commit_open(&commit1, repo, obj1);
838 err = got_object_commit_open(&commit2, repo, obj2);
842 err = got_object_open(&tree_obj1, repo, commit1->tree_id);
845 err = got_object_open(&tree_obj2, repo, commit2->tree_id);
849 err = diff_trees(outfile, tree_obj1, tree_obj2, repo);
852 got_object_close(tree_obj1);
854 got_object_close(tree_obj2);
856 got_object_commit_close(commit1);
858 got_object_commit_close(commit2);
863 const struct got_error *
864 draw_diff(FILE *f, int *first_displayed_line, int *last_displayed_line,
865 int *eof, int max_lines)
867 int nlines = 0, nprinted = 0;
870 wclear(tog_diff_view.window);
873 while (nprinted < max_lines) {
877 const char delim[3] = { '\0', '\0', '\0'};
879 line = fparseln(f, &linelen, &lineno, delim, 0);
884 if (++nlines < *first_displayed_line) {
889 if (linelen > COLS - 1)
890 line[COLS - 1] = '\0';
891 waddstr(tog_diff_view.window, line);
892 waddch(tog_diff_view.window, '\n');
894 *first_displayed_line = nlines;
897 *last_displayed_line = nlines;
905 const struct got_error *
906 show_diff_view(struct got_object *obj1, struct got_object *obj2,
907 struct got_repository *repo)
909 const struct got_error *err;
911 int ch, done = 0, first_displayed_line = 1, last_displayed_line = LINES;
914 if (got_object_get_type(obj1) != got_object_get_type(obj2))
915 return got_error(GOT_ERR_OBJ_TYPE);
919 return got_error_from_errno();
921 switch (got_object_get_type(obj1)) {
922 case GOT_OBJ_TYPE_BLOB:
923 err = diff_blobs(f, obj1, obj2, repo);
925 case GOT_OBJ_TYPE_TREE:
926 err = diff_trees(f, obj1, obj2, repo);
928 case GOT_OBJ_TYPE_COMMIT:
929 err = diff_commits(f, obj1, obj2, repo);
932 return got_error(GOT_ERR_OBJ_TYPE);
937 if (tog_diff_view.window == NULL) {
938 tog_diff_view.window = newwin(0, 0, 0, 0);
939 if (tog_diff_view.window == NULL)
940 return got_error_from_errno();
941 keypad(tog_diff_view.window, TRUE);
943 if (tog_diff_view.panel == NULL) {
944 tog_diff_view.panel = new_panel(tog_diff_view.window);
945 if (tog_diff_view.panel == NULL)
946 return got_error_from_errno();
948 show_panel(tog_diff_view.panel);
951 err = draw_diff(f, &first_displayed_line, &last_displayed_line,
955 nodelay(stdscr, FALSE);
956 ch = wgetch(tog_diff_view.window);
963 if (first_displayed_line > 1)
964 first_displayed_line--;
969 first_displayed_line++;
974 nodelay(stdscr, TRUE);
980 const struct got_error *
981 cmd_diff(int argc, char *argv[])
983 const struct got_error *error = NULL;
984 struct got_repository *repo = NULL;
985 struct got_object *obj1 = NULL, *obj2 = NULL;
986 char *repo_path = NULL;
987 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
991 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
995 while ((ch = getopt(argc, argv, "")) != -1) {
1007 usage_diff(); /* TODO show local worktree changes */
1008 } else if (argc == 2) {
1009 repo_path = getcwd(NULL, 0);
1010 if (repo_path == NULL)
1011 return got_error_from_errno();
1012 obj_id_str1 = argv[0];
1013 obj_id_str2 = argv[1];
1014 } else if (argc == 3) {
1015 repo_path = realpath(argv[0], NULL);
1016 if (repo_path == NULL)
1017 return got_error_from_errno();
1018 obj_id_str1 = argv[1];
1019 obj_id_str2 = argv[2];
1023 error = got_repo_open(&repo, repo_path);
1028 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
1032 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1036 error = show_diff_view(obj1, obj2, repo);
1038 got_repo_close(repo);
1040 got_object_close(obj1);
1042 got_object_close(obj2);
1050 fprintf(stderr, "usage: %s blame [repository-path] blob-object\n",
1055 const struct got_error *
1056 cmd_blame(int argc, char *argv[])
1058 return got_error(GOT_ERR_NOT_IMPL);
1061 static const struct got_error *
1068 intrflush(stdscr, FALSE);
1069 keypad(stdscr, TRUE);
1072 tog_main_win = newwin(0, 0, 0, 0);
1073 if (tog_main_win == NULL)
1074 return got_error_from_errno();
1075 tog_main_panel = new_panel(tog_main_win);
1076 if (tog_main_panel == NULL)
1077 return got_error_from_errno();
1087 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
1088 "Available commands:\n", getprogname());
1089 for (i = 0; i < nitems(tog_commands); i++) {
1090 struct tog_cmd *cmd = &tog_commands[i];
1091 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
1097 make_argv(const char *arg0, const char *arg1)
1100 int argc = (arg1 == NULL ? 1 : 2);
1102 argv = calloc(argc, sizeof(char *));
1105 argv[0] = strdup(arg0);
1106 if (argv[0] == NULL)
1109 argv[1] = strdup(arg1);
1110 if (argv[1] == NULL)
1118 main(int argc, char *argv[])
1120 const struct got_error *error = NULL;
1121 struct tog_cmd *cmd = NULL;
1123 char **cmd_argv = NULL;
1125 setlocale(LC_ALL, "");
1127 while ((ch = getopt(argc, argv, "h")) != -1) {
1144 /* Build an argument vector which runs a default command. */
1145 cmd = &tog_commands[0];
1146 cmd_argv = make_argv(cmd->name, NULL);
1151 /* Did the user specific a command? */
1152 for (i = 0; i < nitems(tog_commands); i++) {
1153 if (strncmp(tog_commands[i].name, argv[0],
1154 strlen(argv[0])) == 0) {
1155 cmd = &tog_commands[i];
1157 tog_commands[i].cmd_usage();
1162 /* Did the user specify a repository? */
1163 char *repo_path = realpath(argv[0], NULL);
1165 struct got_repository *repo;
1166 error = got_repo_open(&repo, repo_path);
1168 got_repo_close(repo);
1170 error = got_error_from_errno();
1172 fprintf(stderr, "%s: '%s' is neither a known "
1173 "command nor a path to a repository\n",
1174 getprogname(), argv[0]);
1178 cmd = &tog_commands[0];
1179 cmd_argv = make_argv(cmd->name, repo_path);
1185 error = init_curses();
1187 fprintf(stderr, "cannot initialize ncurses: %s\n", error->msg);
1191 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
1198 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);