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"
37 #include "got_opentemp.h"
40 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
44 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
49 const struct got_error *(*cmd_main)(int, char *[]);
50 void (*cmd_usage)(void);
54 __dead static void usage(void);
55 __dead static void usage_log(void);
56 __dead static void usage_diff(void);
57 __dead static void usage_blame(void);
59 static const struct got_error* cmd_log(int, char *[]);
60 static const struct got_error* cmd_diff(int, char *[]);
61 static const struct got_error* cmd_blame(int, char *[]);
63 static struct tog_cmd tog_commands[] = {
64 { "log", cmd_log, usage_log,
65 "show repository history" },
66 { "diff", cmd_diff, usage_diff,
67 "compare files and directories" },
68 { "blame", cmd_blame, usage_blame,
69 "show line-by-line file history" },
72 static struct tog_view {
75 } tog_log_view, tog_diff_view;
77 static const struct got_error *
78 show_diff_view(struct got_object *, struct got_object *,
79 struct got_repository *);
80 static const struct got_error *
81 show_log_view(struct got_object_id *, struct got_repository *);
87 fprintf(stderr, "usage: %s log [-c commit] [repository-path]\n",
92 static const struct got_error *
93 draw_commit(struct got_commit_object *commit, struct got_object_id *id)
95 const struct got_error *err = NULL;
96 char *logmsg0 = NULL, *logmsg = NULL;
97 char *author0 = NULL, *author = NULL;
98 char *newline, *smallerthan;
101 const size_t id_display_len = 8;
102 const size_t author_display_len = 16;
103 size_t id_len, author_len, logmsg_len, avail;
106 err = got_object_id_str(&id_str, id);
109 id_len = strlen(id_str);
111 logmsg0 = strdup(commit->logmsg);
112 if (logmsg0 == NULL) {
113 err = got_error_from_errno();
117 while (*logmsg == '\n')
119 newline = strchr(logmsg, '\n');
122 logmsg_len = strlen(logmsg);
124 author0 = strdup(commit->author);
125 if (author0 == NULL) {
126 err = got_error_from_errno();
130 smallerthan = strchr(author, '<');
134 char *at = strchr(author, '@');
138 author_len = strlen(author);
141 line = calloc(avail + 1, sizeof(*line));
143 err = got_error_from_errno();
148 for (i = 0; i < MIN(id_display_len, id_len); i++) {
151 line[col++] = id_str[i];
153 while (i < id_display_len) {
162 for (i = 0; i < MIN(author_display_len, author_len); i++) {
165 line[col++] = author[i];
167 while (i < author_display_len) {
177 while (col < avail && *logmsg)
178 line[col++] = *logmsg++;
182 waddstr(tog_log_view.window, line);
183 waddch(tog_log_view.window, '\n');
192 struct commit_queue_entry {
193 TAILQ_ENTRY(commit_queue_entry) entry;
194 struct got_object_id *id;
195 struct got_commit_object *commit;
197 TAILQ_HEAD(commit_queue, commit_queue_entry);
199 static struct commit_queue_entry *
200 alloc_commit_queue_entry(struct got_commit_object *commit,
201 struct got_object_id *id)
203 struct commit_queue_entry *entry;
205 entry = calloc(1, sizeof(*entry));
210 entry->commit = commit;
215 pop_commit(struct commit_queue *commits)
217 struct commit_queue_entry *entry;
219 entry = TAILQ_FIRST(commits);
220 TAILQ_REMOVE(commits, entry, entry);
221 got_object_commit_close(entry->commit);
227 free_commits(struct commit_queue *commits)
229 while (!TAILQ_EMPTY(commits))
234 static const struct got_error *
235 fetch_parent_commit(struct commit_queue_entry **pentry,
236 struct commit_queue_entry *entry, struct got_repository *repo)
238 const struct got_error *err = NULL;
239 struct got_object *obj = NULL;
240 struct got_commit_object *commit;
241 struct got_object_id *id;
242 struct got_parent_id *pid;
246 /* Follow the first parent (TODO: handle merge commits). */
247 pid = SIMPLEQ_FIRST(&entry->commit->parent_ids);
250 err = got_object_open(&obj, repo, pid->id);
253 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
254 err = got_error(GOT_ERR_OBJ_TYPE);
255 got_object_close(obj);
259 err = got_object_commit_open(&commit, repo, obj);
260 got_object_close(obj);
264 id = got_object_id_dup(pid->id);
266 err = got_error_from_errno();
267 got_object_commit_close(commit);
271 *pentry = alloc_commit_queue_entry(commit, id);
272 if (*pentry == NULL) {
273 err = got_error_from_errno();
274 got_object_commit_close(commit);
280 static const struct got_error *
281 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
283 const struct got_error *err = NULL;
284 struct got_reference *head_ref;
288 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
292 err = got_ref_resolve(head_id, repo, head_ref);
293 got_ref_close(head_ref);
302 static const struct got_error *
303 prepend_commits(int *ncommits, struct commit_queue *commits,
304 struct got_object_id *first_id, struct got_object_id *last_id,
305 int limit, struct got_repository *repo)
307 const struct got_error *err = NULL;
308 struct got_object *first_obj = NULL, *last_obj = NULL;
309 struct got_commit_object *commit = NULL;
310 struct got_object_id *id = NULL;
311 struct commit_queue_entry *entry, *old_head_entry;
315 err = got_object_open(&first_obj, repo, first_id);
318 if (got_object_get_type(first_obj) != GOT_OBJ_TYPE_COMMIT) {
319 err = got_error(GOT_ERR_OBJ_TYPE);
322 err = got_object_open(&last_obj, repo, last_id);
325 if (got_object_get_type(last_obj) != GOT_OBJ_TYPE_COMMIT) {
326 err = got_error(GOT_ERR_OBJ_TYPE);
330 err = got_object_commit_open(&commit, repo, first_obj);
334 id = got_object_id_dup(first_id);
336 err = got_error_from_errno();
340 entry = alloc_commit_queue_entry(commit, id);
342 return got_error_from_errno();
344 old_head_entry = TAILQ_FIRST(commits);
346 TAILQ_INSERT_BEFORE(old_head_entry, entry, entry);
348 TAILQ_INSERT_HEAD(commits, entry, entry);
353 * Fetch parent commits.
354 * XXX If first and last commit aren't ancestrally related this loop
355 * we will keep iterating until a root commit is encountered.
358 struct commit_queue_entry *pentry;
360 err = fetch_parent_commit(&pentry, entry, repo);
367 * Fill up to old HEAD commit if commit queue was not empty.
368 * We must not leave a gap in history.
370 if (old_head_entry &&
371 got_object_id_cmp(pentry->id, old_head_entry->id) == 0)
374 TAILQ_INSERT_AFTER(commits, entry, pentry, entry);
376 if (*ncommits >= limit)
379 /* Fill up to last requested commit if queue was empty. */
380 if (old_head_entry == NULL &&
381 got_object_id_cmp(pentry->id, last_id) == 0)
389 got_object_close(first_obj);
391 got_object_close(last_obj);
395 static const struct got_error *
396 fetch_commits(struct commit_queue_entry **start_entry,
397 struct got_object_id *start_id, struct commit_queue *commits,
398 int limit, struct got_repository *repo)
400 const struct got_error *err;
401 struct commit_queue_entry *entry;
403 struct got_object_id *head_id = NULL;
407 err = get_head_commit_id(&head_id, repo);
411 /* Prepend HEAD commit and all ancestors up to start commit. */
412 err = prepend_commits(&ncommits, commits, head_id, start_id, limit,
417 if (got_object_id_cmp(head_id, start_id) == 0)
418 *start_entry = TAILQ_FIRST(commits);
420 *start_entry = TAILQ_LAST(commits, commit_queue);
422 if (ncommits >= limit)
425 /* Append more commits from start commit up to the requested limit. */
426 entry = TAILQ_LAST(commits, commit_queue);
427 while (entry && ncommits < limit) {
428 struct commit_queue_entry *pentry;
430 err = fetch_parent_commit(&pentry, entry, repo);
434 TAILQ_INSERT_TAIL(commits, pentry, entry);
444 static const struct got_error *
445 draw_commits(struct commit_queue_entry **last, struct commit_queue_entry **selected,
446 struct commit_queue_entry *first, int selected_idx, int limit)
448 const struct got_error *err = NULL;
449 struct commit_queue_entry *entry;
452 wclear(tog_log_view.window);
457 if (ncommits == limit)
459 if (ncommits == selected_idx) {
460 wstandout(tog_log_view.window);
463 err = draw_commit(entry->commit, entry->id);
464 if (ncommits == selected_idx)
465 wstandend(tog_log_view.window);
470 entry = TAILQ_NEXT(entry, entry);
480 scroll_up(struct commit_queue_entry **first_displayed_entry, int n,
481 struct commit_queue *commits)
483 struct commit_queue_entry *entry;
486 entry = TAILQ_FIRST(commits);
487 if (*first_displayed_entry == entry)
490 entry = *first_displayed_entry;
491 while (entry && nscrolled < n) {
492 entry = TAILQ_PREV(entry, commit_queue, entry);
494 *first_displayed_entry = entry;
500 static const struct got_error *
501 scroll_down(struct commit_queue_entry **first_displayed_entry, int n,
502 struct commit_queue_entry *last_displayed_entry,
503 struct commit_queue *commits, struct got_repository *repo)
505 const struct got_error *err = NULL;
506 struct commit_queue_entry *entry;
509 if (last_displayed_entry->commit->nparents == 0)
512 entry = *first_displayed_entry;
514 struct commit_queue_entry *pentry;
516 pentry = TAILQ_NEXT(entry, entry);
517 if (pentry == NULL) {
518 err = fetch_parent_commit(&pentry, entry, repo);
521 if (pentry == NULL) {
522 *first_displayed_entry = entry;
525 TAILQ_INSERT_TAIL(commits, pentry, entry);
526 last_displayed_entry = pentry;
529 *first_displayed_entry = pentry;
532 if (TAILQ_LAST(commits, commit_queue) == last_displayed_entry) {
533 err = fetch_parent_commit(&pentry, last_displayed_entry,
538 TAILQ_INSERT_TAIL(commits, pentry, entry);
539 last_displayed_entry = pentry;
542 } while (++nscrolled < n);
548 num_parents(struct commit_queue_entry *entry)
553 entry = TAILQ_NEXT(entry, entry);
560 static const struct got_error *
561 show_commit(struct commit_queue_entry *entry, struct got_repository *repo)
563 const struct got_error *err;
564 struct got_object *obj1 = NULL, *obj2 = NULL;
566 err = got_object_open(&obj2, repo, entry->id);
570 entry = TAILQ_NEXT(entry, entry);
572 err = got_object_open(&obj1, repo, entry->id);
577 err = show_diff_view(obj1, obj2, repo);
580 got_object_close(obj1);
582 got_object_close(obj2);
586 static const struct got_error *
587 show_log_view(struct got_object_id *start_id, struct got_repository *repo)
589 const struct got_error *err = NULL;
590 struct got_object_id *id;
591 int ch, done = 0, selected = 0, nparents;
592 struct commit_queue commits;
593 struct commit_queue_entry *first_displayed_entry = NULL;
594 struct commit_queue_entry *last_displayed_entry = NULL;
595 struct commit_queue_entry *selected_entry = NULL;
597 id = got_object_id_dup(start_id);
599 return got_error_from_errno();
601 if (tog_log_view.window == NULL) {
602 tog_log_view.window = newwin(0, 0, 0, 0);
603 if (tog_log_view.window == NULL)
604 return got_error_from_errno();
605 keypad(tog_log_view.window, TRUE);
607 if (tog_log_view.panel == NULL) {
608 tog_log_view.panel = new_panel(tog_log_view.window);
609 if (tog_log_view.panel == NULL)
610 return got_error_from_errno();
612 show_panel(tog_log_view.panel);
614 TAILQ_INIT(&commits);
615 err = fetch_commits(&first_displayed_entry, id, &commits, LINES, repo);
619 err = draw_commits(&last_displayed_entry, &selected_entry,
620 first_displayed_entry, selected, LINES);
624 nodelay(stdscr, FALSE);
625 ch = wgetch(tog_log_view.window);
629 err = got_error_from_errno();
642 scroll_up(&first_displayed_entry, 1, &commits);
645 if (TAILQ_FIRST(&commits) ==
646 first_displayed_entry) {
650 scroll_up(&first_displayed_entry, LINES,
655 nparents = num_parents(first_displayed_entry);
656 if (selected < LINES - 1 &&
657 selected < nparents - 1)
659 if (selected < LINES - 1 &&
660 selected < nparents - 1)
662 err = scroll_down(&first_displayed_entry, 1,
663 last_displayed_entry, &commits, repo);
668 nparents = num_parents(first_displayed_entry);
669 if (nparents < LINES - 1 &&
670 selected < nparents - 1) {
671 selected = nparents - 1;
674 err = scroll_down(&first_displayed_entry, LINES,
675 last_displayed_entry, &commits, repo);
678 nparents = num_parents(first_displayed_entry);
679 if (selected > nparents)
680 selected = nparents - 1;
683 if (selected > LINES)
684 selected = LINES - 1;
688 err = show_commit(selected_entry, repo);
691 show_panel(tog_log_view.panel);
696 nodelay(stdscr, TRUE);
699 free_commits(&commits);
703 static const struct got_error *
704 cmd_log(int argc, char *argv[])
706 const struct got_error *error;
707 struct got_repository *repo;
708 struct got_object_id *start_id = NULL;
709 char *repo_path = NULL;
710 char *start_commit = NULL;
714 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
718 while ((ch = getopt(argc, argv, "c:")) != -1) {
721 start_commit = optarg;
733 repo_path = getcwd(NULL, 0);
734 if (repo_path == NULL)
735 return got_error_from_errno();
736 } else if (argc == 1) {
737 repo_path = realpath(argv[0], NULL);
738 if (repo_path == NULL)
739 return got_error_from_errno();
743 error = got_repo_open(&repo, repo_path);
748 if (start_commit == NULL) {
749 error = get_head_commit_id(&start_id, repo);
753 struct got_object *obj;
754 error = got_object_open_by_id_str(&obj, repo, start_commit);
756 start_id = got_object_get_id(obj);
757 if (start_id == NULL)
758 error = got_error_from_errno();
763 error = show_log_view(start_id, repo);
765 got_repo_close(repo);
773 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
778 static const struct got_error *
779 draw_diff(FILE *f, int *first_displayed_line, int *last_displayed_line,
780 int *eof, int max_lines)
782 int nlines = 0, nprinted = 0;
785 wclear(tog_diff_view.window);
788 while (nprinted < max_lines) {
792 const char delim[3] = { '\0', '\0', '\0'};
794 line = fparseln(f, &linelen, &lineno, delim, 0);
799 if (++nlines < *first_displayed_line) {
804 if (linelen > COLS - 1)
805 line[COLS - 1] = '\0';
806 waddstr(tog_diff_view.window, line);
807 waddch(tog_diff_view.window, '\n');
809 *first_displayed_line = nlines;
812 *last_displayed_line = nlines;
820 static const struct got_error *
821 show_diff_view(struct got_object *obj1, struct got_object *obj2,
822 struct got_repository *repo)
824 const struct got_error *err;
826 int ch, done = 0, first_displayed_line = 1, last_displayed_line = LINES;
829 if (obj1 != NULL && obj2 != NULL &&
830 got_object_get_type(obj1) != got_object_get_type(obj2))
831 return got_error(GOT_ERR_OBJ_TYPE);
835 return got_error_from_errno();
837 switch (got_object_get_type(obj1 ? obj1 : obj2)) {
838 case GOT_OBJ_TYPE_BLOB:
839 err = got_diff_objects_as_blobs(obj1, obj2, repo, f);
841 case GOT_OBJ_TYPE_TREE:
842 err = got_diff_objects_as_trees(obj1, obj2, repo, f);
844 case GOT_OBJ_TYPE_COMMIT:
845 err = got_diff_objects_as_commits(obj1, obj2, repo, f);
848 return got_error(GOT_ERR_OBJ_TYPE);
853 if (tog_diff_view.window == NULL) {
854 tog_diff_view.window = newwin(0, 0, 0, 0);
855 if (tog_diff_view.window == NULL)
856 return got_error_from_errno();
857 keypad(tog_diff_view.window, TRUE);
859 if (tog_diff_view.panel == NULL) {
860 tog_diff_view.panel = new_panel(tog_diff_view.window);
861 if (tog_diff_view.panel == NULL)
862 return got_error_from_errno();
864 show_panel(tog_diff_view.panel);
867 err = draw_diff(f, &first_displayed_line, &last_displayed_line,
871 nodelay(stdscr, FALSE);
872 ch = wgetch(tog_diff_view.window);
879 if (first_displayed_line > 1)
880 first_displayed_line--;
885 first_displayed_line++;
890 nodelay(stdscr, TRUE);
896 static const struct got_error *
897 cmd_diff(int argc, char *argv[])
899 const struct got_error *error = NULL;
900 struct got_repository *repo = NULL;
901 struct got_object *obj1 = NULL, *obj2 = NULL;
902 char *repo_path = NULL;
903 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
907 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
911 while ((ch = getopt(argc, argv, "")) != -1) {
923 usage_diff(); /* TODO show local worktree changes */
924 } else if (argc == 2) {
925 repo_path = getcwd(NULL, 0);
926 if (repo_path == NULL)
927 return got_error_from_errno();
928 obj_id_str1 = argv[0];
929 obj_id_str2 = argv[1];
930 } else if (argc == 3) {
931 repo_path = realpath(argv[0], NULL);
932 if (repo_path == NULL)
933 return got_error_from_errno();
934 obj_id_str1 = argv[1];
935 obj_id_str2 = argv[2];
939 error = got_repo_open(&repo, repo_path);
944 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
948 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
952 error = show_diff_view(obj1, obj2, repo);
954 got_repo_close(repo);
956 got_object_close(obj1);
958 got_object_close(obj2);
966 fprintf(stderr, "usage: %s blame [repository-path] blob-object\n",
971 static const struct got_error *
972 cmd_blame(int argc, char *argv[])
974 return got_error(GOT_ERR_NOT_IMPL);
984 intrflush(stdscr, FALSE);
985 keypad(stdscr, TRUE);
994 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
995 "Available commands:\n", getprogname());
996 for (i = 0; i < nitems(tog_commands); i++) {
997 struct tog_cmd *cmd = &tog_commands[i];
998 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
1004 make_argv(const char *arg0, const char *arg1)
1007 int argc = (arg1 == NULL ? 1 : 2);
1009 argv = calloc(argc, sizeof(char *));
1012 argv[0] = strdup(arg0);
1013 if (argv[0] == NULL)
1016 argv[1] = strdup(arg1);
1017 if (argv[1] == NULL)
1025 main(int argc, char *argv[])
1027 const struct got_error *error = NULL;
1028 struct tog_cmd *cmd = NULL;
1030 char **cmd_argv = NULL;
1032 setlocale(LC_ALL, "");
1034 while ((ch = getopt(argc, argv, "h")) != -1) {
1051 /* Build an argument vector which runs a default command. */
1052 cmd = &tog_commands[0];
1053 cmd_argv = make_argv(cmd->name, NULL);
1058 /* Did the user specific a command? */
1059 for (i = 0; i < nitems(tog_commands); i++) {
1060 if (strncmp(tog_commands[i].name, argv[0],
1061 strlen(argv[0])) == 0) {
1062 cmd = &tog_commands[i];
1064 tog_commands[i].cmd_usage();
1069 /* Did the user specify a repository? */
1070 char *repo_path = realpath(argv[0], NULL);
1072 struct got_repository *repo;
1073 error = got_repo_open(&repo, repo_path);
1075 got_repo_close(repo);
1077 error = got_error_from_errno();
1079 fprintf(stderr, "%s: '%s' is neither a known "
1080 "command nor a path to a repository\n",
1081 getprogname(), argv[0]);
1085 cmd = &tog_commands[0];
1086 cmd_argv = make_argv(cmd->name, repo_path);
1094 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
1101 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);