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"
46 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
50 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
55 const struct got_error *(*cmd_main)(int, char *[]);
56 void (*cmd_usage)(void);
60 __dead static void usage(void);
61 __dead static void usage_log(void);
62 __dead static void usage_diff(void);
63 __dead static void usage_blame(void);
65 static const struct got_error* cmd_log(int, char *[]);
66 static const struct got_error* cmd_diff(int, char *[]);
67 static const struct got_error* cmd_blame(int, char *[]);
69 static struct tog_cmd tog_commands[] = {
70 { "log", cmd_log, usage_log,
71 "show repository history" },
72 { "diff", cmd_diff, usage_diff,
73 "compare files and directories" },
74 { "blame", cmd_blame, usage_blame,
75 "show line-by-line file history" },
78 static struct tog_view {
81 } tog_log_view, tog_diff_view;
83 static const struct got_error *
84 show_diff_view(struct got_object *, struct got_object *,
85 struct got_repository *);
86 static const struct got_error *
87 show_log_view(struct got_object_id *, struct got_repository *);
93 fprintf(stderr, "usage: %s log [-c commit] [repository-path]\n",
98 /* Create newly allocated wide-character string equivalent to a byte string. */
99 static const struct got_error *
100 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
103 const struct got_error *err = NULL;
106 *wlen = mbstowcs(NULL, s, 0);
107 if (*wlen == (size_t)-1) {
110 return got_error_from_errno();
112 /* byte string invalid in current encoding; try to "fix" it */
113 err = got_mbsavis(&vis, &vislen, s);
116 *wlen = mbstowcs(NULL, vis, 0);
117 if (*wlen == (size_t)-1) {
118 err = got_error_from_errno(); /* give up */
123 *ws = calloc(*wlen + 1, sizeof(*ws));
125 err = got_error_from_errno();
129 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
130 err = got_error_from_errno();
141 /* Format a line for display, ensuring that it won't overflow a width limit. */
142 static const struct got_error *
143 format_line(wchar_t **wlinep, int *widthp, char *line, int wlimit)
145 const struct got_error *err = NULL;
147 wchar_t *wline = NULL;
153 err = mbs2ws(&wline, &wlen, line);
158 while (i < wlen && cols <= wlimit) {
159 int width = wcwidth(wline[i]);
168 if (wline[i] == L'\t')
172 err = got_error_from_errno();
190 static const struct got_error *
191 draw_commit(struct got_commit_object *commit, struct got_object_id *id)
193 const struct got_error *err = NULL;
194 char *logmsg0 = NULL, *logmsg = NULL;
195 char *author0 = NULL, *author = NULL;
196 wchar_t *wlogmsg = NULL, *wauthor = NULL;
197 int author_width, logmsg_width;
198 char *newline, *smallerthan;
203 static const size_t id_display_cols = 8;
204 static const size_t author_display_cols = 16;
205 const int avail = COLS;
207 err = got_object_id_str(&id_str, id);
210 id_len = strlen(id_str);
211 if (avail < id_display_cols) {
212 limit = MIN(id_len, avail);
213 waddnstr(tog_log_view.window, id_str, limit);
215 limit = MIN(id_display_cols, id_len);
216 waddnstr(tog_log_view.window, id_str, limit);
219 while (col <= avail && col < id_display_cols + 2) {
220 waddch(tog_log_view.window, ' ');
226 author0 = strdup(commit->author);
227 if (author0 == NULL) {
228 err = got_error_from_errno();
232 smallerthan = strchr(author, '<');
236 char *at = strchr(author, '@');
240 limit = MIN(avail, author_display_cols);
241 err = format_line(&wauthor, &author_width, author, limit);
244 waddwstr(tog_log_view.window, wauthor);
246 while (col <= avail && author_width < author_display_cols + 1) {
247 waddch(tog_log_view.window, ' ');
254 logmsg0 = strdup(commit->logmsg);
255 if (logmsg0 == NULL) {
256 err = got_error_from_errno();
260 while (*logmsg == '\n')
262 newline = strchr(logmsg, '\n');
266 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
269 waddwstr(tog_log_view.window, wlogmsg);
271 while (col <= avail) {
272 waddch(tog_log_view.window, ' ');
285 struct commit_queue_entry {
286 TAILQ_ENTRY(commit_queue_entry) entry;
287 struct got_object_id *id;
288 struct got_commit_object *commit;
290 TAILQ_HEAD(commit_queue, commit_queue_entry);
292 static struct commit_queue_entry *
293 alloc_commit_queue_entry(struct got_commit_object *commit,
294 struct got_object_id *id)
296 struct commit_queue_entry *entry;
298 entry = calloc(1, sizeof(*entry));
303 entry->commit = commit;
308 pop_commit(struct commit_queue *commits)
310 struct commit_queue_entry *entry;
312 entry = TAILQ_FIRST(commits);
313 TAILQ_REMOVE(commits, entry, entry);
314 got_object_commit_close(entry->commit);
315 /* Don't free entry->id! It is owned by the commit graph. */
320 free_commits(struct commit_queue *commits)
322 while (!TAILQ_EMPTY(commits))
326 static const struct got_error *
327 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
328 struct got_object_id *start_id, struct got_repository *repo)
330 const struct got_error *err = NULL;
331 struct got_object_id *id;
332 struct commit_queue_entry *entry;
334 err = got_commit_graph_iter_start(graph, start_id);
338 entry = TAILQ_LAST(commits, commit_queue);
339 if (entry && got_object_id_cmp(entry->id, start_id) == 0) {
342 /* Start ID's commit is already on the queue; skip over it. */
343 err = got_commit_graph_iter_next(&id, graph);
344 if (err && err->code != GOT_ERR_ITER_NEED_MORE)
347 err = got_commit_graph_fetch_commits(&nfetched, graph, 1, repo);
353 struct got_commit_object *commit;
355 err = got_commit_graph_iter_next(&id, graph);
357 if (err->code == GOT_ERR_ITER_NEED_MORE)
362 err = got_object_open_as_commit(&commit, repo, id);
366 entry = alloc_commit_queue_entry(commit, id);
368 err = got_error_from_errno();
372 TAILQ_INSERT_TAIL(commits, entry, entry);
378 static const struct got_error *
379 fetch_next_commit(struct commit_queue_entry **pentry,
380 struct commit_queue_entry *entry, struct commit_queue *commits,
381 struct got_commit_graph *graph, struct got_repository *repo)
383 const struct got_error *err = NULL;
384 struct got_object_qid *qid;
388 /* Populate commit graph with entry's parent commits. */
389 SIMPLEQ_FOREACH(qid, &entry->commit->parent_ids, entry) {
391 err = got_commit_graph_fetch_commits_up_to(&nfetched,
392 graph, qid->id, repo);
397 /* Append outstanding commits to queue in graph sort order. */
398 err = queue_commits(graph, commits, entry->id, repo);
400 if (err->code == GOT_ERR_ITER_COMPLETED)
405 /* Next entry to display should now be available. */
406 *pentry = TAILQ_NEXT(entry, entry);
408 return got_error(GOT_ERR_NO_OBJ);
413 static const struct got_error *
414 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
416 const struct got_error *err = NULL;
417 struct got_reference *head_ref;
421 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
425 err = got_ref_resolve(head_id, repo, head_ref);
426 got_ref_close(head_ref);
435 static const struct got_error *
436 draw_commits(struct commit_queue_entry **last, struct commit_queue_entry **selected,
437 struct commit_queue_entry *first, int selected_idx, int limit)
439 const struct got_error *err = NULL;
440 struct commit_queue_entry *entry;
443 werase(tog_log_view.window);
448 if (ncommits == limit)
450 if (ncommits == selected_idx) {
451 wstandout(tog_log_view.window);
454 err = draw_commit(entry->commit, entry->id);
455 if (ncommits == selected_idx)
456 wstandend(tog_log_view.window);
461 entry = TAILQ_NEXT(entry, entry);
471 scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
472 struct commit_queue *commits)
474 struct commit_queue_entry *entry;
477 entry = TAILQ_FIRST(commits);
478 if (*first_displayed_entry == entry)
481 entry = *first_displayed_entry;
482 while (entry && nscrolled < maxscroll) {
483 entry = TAILQ_PREV(entry, commit_queue, entry);
485 *first_displayed_entry = entry;
491 static const struct got_error *
492 scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
493 struct commit_queue_entry *last_displayed_entry,
494 struct commit_queue *commits, struct got_commit_graph *graph,
495 struct got_repository *repo)
497 const struct got_error *err = NULL;
498 struct commit_queue_entry *pentry;
502 pentry = TAILQ_NEXT(last_displayed_entry, entry);
503 if (pentry == NULL) {
504 err = fetch_next_commit(&pentry, last_displayed_entry,
505 commits, graph, repo);
506 if (err || pentry == NULL)
509 last_displayed_entry = pentry;
511 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
514 *first_displayed_entry = pentry;
515 } while (++nscrolled < maxscroll);
521 num_parents(struct commit_queue_entry *entry)
526 entry = TAILQ_NEXT(entry, entry);
533 static const struct got_error *
534 show_commit(struct commit_queue_entry *entry, struct got_repository *repo)
536 const struct got_error *err;
537 struct got_object *obj1 = NULL, *obj2 = NULL;
538 struct got_object_qid *parent_id;
540 err = got_object_open(&obj2, repo, entry->id);
544 parent_id = SIMPLEQ_FIRST(&entry->commit->parent_ids);
546 err = got_object_open(&obj1, repo, parent_id->id);
551 err = show_diff_view(obj1, obj2, repo);
554 got_object_close(obj1);
556 got_object_close(obj2);
560 static const struct got_error *
561 show_log_view(struct got_object_id *start_id, struct got_repository *repo)
563 const struct got_error *err = NULL;
564 struct got_object_id *head_id = NULL;
565 int ch, done = 0, selected = 0, nparents, nfetched;
566 struct got_commit_graph *graph;
567 struct commit_queue commits;
568 struct commit_queue_entry *entry = NULL;
569 struct commit_queue_entry *first_displayed_entry = NULL;
570 struct commit_queue_entry *last_displayed_entry = NULL;
571 struct commit_queue_entry *selected_entry = NULL;
573 if (tog_log_view.window == NULL) {
574 tog_log_view.window = newwin(0, 0, 0, 0);
575 if (tog_log_view.window == NULL)
576 return got_error_from_errno();
577 keypad(tog_log_view.window, TRUE);
579 if (tog_log_view.panel == NULL) {
580 tog_log_view.panel = new_panel(tog_log_view.window);
581 if (tog_log_view.panel == NULL)
582 return got_error_from_errno();
584 show_panel(tog_log_view.panel);
586 err = get_head_commit_id(&head_id, repo);
590 TAILQ_INIT(&commits);
592 err = got_commit_graph_open(&graph, head_id, 0, repo);
596 /* Populate commit graph with a sufficient number of commits. */
597 err = got_commit_graph_fetch_commits_up_to(&nfetched, graph, start_id,
601 err = got_commit_graph_fetch_commits(&nfetched, graph, LINES, repo);
606 * Open the initial batch of commits, sorted in commit graph order.
607 * We keep all commits open throughout the lifetime of the log view
608 * in order to avoid having to re-fetch commits from disk while
609 * updating the display.
611 err = queue_commits(graph, &commits, head_id, repo);
612 if (err && err->code != GOT_ERR_ITER_COMPLETED)
615 /* Find entry corresponding to the first commit to display. */
616 TAILQ_FOREACH(entry, &commits, entry) {
617 if (got_object_id_cmp(entry->id, start_id) == 0) {
618 first_displayed_entry = entry;
622 if (first_displayed_entry == NULL) {
623 err = got_error(GOT_ERR_NO_OBJ);
628 err = draw_commits(&last_displayed_entry, &selected_entry,
629 first_displayed_entry, selected, LINES);
633 nodelay(stdscr, FALSE);
634 ch = wgetch(tog_log_view.window);
635 nodelay(stdscr, TRUE);
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) {
671 err = scroll_down(&first_displayed_entry, 1,
672 last_displayed_entry, &commits, graph,
678 err = scroll_down(&first_displayed_entry, LINES,
679 last_displayed_entry, &commits, graph,
683 if (last_displayed_entry->commit->nparents > 0)
685 /* can't scroll any further; move cursor down */
686 nparents = num_parents(first_displayed_entry);
687 if (selected < LINES - 1 ||
688 selected < nparents - 1)
689 selected = MIN(LINES - 1, nparents - 1);
692 if (selected > LINES)
693 selected = LINES - 1;
697 err = show_commit(selected_entry, repo);
700 show_panel(tog_log_view.panel);
709 got_commit_graph_close(graph);
710 free_commits(&commits);
714 static const struct got_error *
715 cmd_log(int argc, char *argv[])
717 const struct got_error *error;
718 struct got_repository *repo;
719 struct got_object_id *start_id = NULL;
720 char *repo_path = NULL;
721 char *start_commit = NULL;
725 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
729 while ((ch = getopt(argc, argv, "c:")) != -1) {
732 start_commit = optarg;
744 repo_path = getcwd(NULL, 0);
745 if (repo_path == NULL)
746 return got_error_from_errno();
747 } else if (argc == 1) {
748 repo_path = realpath(argv[0], NULL);
749 if (repo_path == NULL)
750 return got_error_from_errno();
754 error = got_repo_open(&repo, repo_path);
759 if (start_commit == NULL) {
760 error = get_head_commit_id(&start_id, repo);
764 struct got_object *obj;
765 error = got_object_open_by_id_str(&obj, repo, start_commit);
767 start_id = got_object_get_id(obj);
768 if (start_id == NULL)
769 error = got_error_from_errno();
774 error = show_log_view(start_id, repo);
776 got_repo_close(repo);
784 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
790 parse_next_line(FILE *f, size_t *len)
795 const char delim[3] = { '\0', '\0', '\0'};
797 line = fparseln(f, &linelen, &lineno, delim, 0);
803 static const struct got_error *
804 draw_diff(FILE *f, int *first_displayed_line, int *last_displayed_line,
805 int *eof, int max_lines)
807 const struct got_error *err;
808 int nlines = 0, nprinted = 0;
815 werase(tog_diff_view.window);
818 while (nprinted < max_lines) {
819 line = parse_next_line(f, &len);
824 if (++nlines < *first_displayed_line) {
829 err = format_line(&wline, &width, line, COLS);
834 waddwstr(tog_diff_view.window, wline);
836 waddch(tog_diff_view.window, '\n');
838 *first_displayed_line = nlines;
841 *last_displayed_line = nlines;
849 static const struct got_error *
850 show_diff_view(struct got_object *obj1, struct got_object *obj2,
851 struct got_repository *repo)
853 const struct got_error *err;
855 int ch, done = 0, first_displayed_line = 1, last_displayed_line = LINES;
858 if (obj1 != NULL && obj2 != NULL &&
859 got_object_get_type(obj1) != got_object_get_type(obj2))
860 return got_error(GOT_ERR_OBJ_TYPE);
864 return got_error_from_errno();
866 switch (got_object_get_type(obj1 ? obj1 : obj2)) {
867 case GOT_OBJ_TYPE_BLOB:
868 err = got_diff_objects_as_blobs(obj1, obj2, repo, f);
870 case GOT_OBJ_TYPE_TREE:
871 err = got_diff_objects_as_trees(obj1, obj2, repo, f);
873 case GOT_OBJ_TYPE_COMMIT:
874 err = got_diff_objects_as_commits(obj1, obj2, repo, f);
877 return got_error(GOT_ERR_OBJ_TYPE);
882 if (tog_diff_view.window == NULL) {
883 tog_diff_view.window = newwin(0, 0, 0, 0);
884 if (tog_diff_view.window == NULL)
885 return got_error_from_errno();
886 keypad(tog_diff_view.window, TRUE);
888 if (tog_diff_view.panel == NULL) {
889 tog_diff_view.panel = new_panel(tog_diff_view.window);
890 if (tog_diff_view.panel == NULL)
891 return got_error_from_errno();
893 show_panel(tog_diff_view.panel);
896 err = draw_diff(f, &first_displayed_line, &last_displayed_line,
900 nodelay(stdscr, FALSE);
901 ch = wgetch(tog_diff_view.window);
902 nodelay(stdscr, TRUE);
910 if (first_displayed_line > 1)
911 first_displayed_line--;
915 while (i++ < LINES - 1 &&
916 first_displayed_line > 1)
917 first_displayed_line--;
924 first_displayed_line++;
929 while (!eof && i++ < LINES - 1) {
930 char *line = parse_next_line(f, NULL);
931 first_displayed_line++;
944 static const struct got_error *
945 cmd_diff(int argc, char *argv[])
947 const struct got_error *error = NULL;
948 struct got_repository *repo = NULL;
949 struct got_object *obj1 = NULL, *obj2 = NULL;
950 char *repo_path = NULL;
951 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
955 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
959 while ((ch = getopt(argc, argv, "")) != -1) {
971 usage_diff(); /* TODO show local worktree changes */
972 } else if (argc == 2) {
973 repo_path = getcwd(NULL, 0);
974 if (repo_path == NULL)
975 return got_error_from_errno();
976 obj_id_str1 = argv[0];
977 obj_id_str2 = argv[1];
978 } else if (argc == 3) {
979 repo_path = realpath(argv[0], NULL);
980 if (repo_path == NULL)
981 return got_error_from_errno();
982 obj_id_str1 = argv[1];
983 obj_id_str2 = argv[2];
987 error = got_repo_open(&repo, repo_path);
992 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
996 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1000 error = show_diff_view(obj1, obj2, repo);
1002 got_repo_close(repo);
1004 got_object_close(obj1);
1006 got_object_close(obj2);
1014 fprintf(stderr, "usage: %s blame [repository-path] blob-object\n",
1019 static const struct got_error *
1020 cmd_blame(int argc, char *argv[])
1022 return got_error(GOT_ERR_NOT_IMPL);
1032 intrflush(stdscr, FALSE);
1033 keypad(stdscr, TRUE);
1042 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
1043 "Available commands:\n", getprogname());
1044 for (i = 0; i < nitems(tog_commands); i++) {
1045 struct tog_cmd *cmd = &tog_commands[i];
1046 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
1052 make_argv(const char *arg0, const char *arg1)
1055 int argc = (arg1 == NULL ? 1 : 2);
1057 argv = calloc(argc, sizeof(char *));
1060 argv[0] = strdup(arg0);
1061 if (argv[0] == NULL)
1064 argv[1] = strdup(arg1);
1065 if (argv[1] == NULL)
1073 main(int argc, char *argv[])
1075 const struct got_error *error = NULL;
1076 struct tog_cmd *cmd = NULL;
1078 char **cmd_argv = NULL;
1080 setlocale(LC_ALL, "");
1082 while ((ch = getopt(argc, argv, "h")) != -1) {
1099 /* Build an argument vector which runs a default command. */
1100 cmd = &tog_commands[0];
1101 cmd_argv = make_argv(cmd->name, NULL);
1106 /* Did the user specific a command? */
1107 for (i = 0; i < nitems(tog_commands); i++) {
1108 if (strncmp(tog_commands[i].name, argv[0],
1109 strlen(argv[0])) == 0) {
1110 cmd = &tog_commands[i];
1112 tog_commands[i].cmd_usage();
1117 /* Did the user specify a repository? */
1118 char *repo_path = realpath(argv[0], NULL);
1120 struct got_repository *repo;
1121 error = got_repo_open(&repo, repo_path);
1123 got_repo_close(repo);
1125 error = got_error_from_errno();
1127 fprintf(stderr, "%s: '%s' is neither a known "
1128 "command nor a path to a repository\n",
1129 getprogname(), argv[0]);
1133 cmd = &tog_commands[0];
1134 cmd_argv = make_argv(cmd->name, repo_path);
1142 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
1149 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);