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
35 #include "got_error.h"
36 #include "got_object.h"
37 #include "got_reference.h"
38 #include "got_repository.h"
40 #include "got_opentemp.h"
43 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
47 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
52 const struct got_error *(*cmd_main)(int, char *[]);
53 void (*cmd_usage)(void);
57 __dead static void usage(void);
58 __dead static void usage_log(void);
59 __dead static void usage_diff(void);
60 __dead static void usage_blame(void);
62 static const struct got_error* cmd_log(int, char *[]);
63 static const struct got_error* cmd_diff(int, char *[]);
64 static const struct got_error* cmd_blame(int, char *[]);
66 static struct tog_cmd tog_commands[] = {
67 { "log", cmd_log, usage_log,
68 "show repository history" },
69 { "diff", cmd_diff, usage_diff,
70 "compare files and directories" },
71 { "blame", cmd_blame, usage_blame,
72 "show line-by-line file history" },
75 static struct tog_view {
78 } tog_log_view, tog_diff_view;
80 static const struct got_error *
81 show_diff_view(struct got_object *, struct got_object *,
82 struct got_repository *);
83 static const struct got_error *
84 show_log_view(struct got_object_id *, struct got_repository *);
86 static const struct got_error *format_line(wchar_t **, int *, char *, int);
92 fprintf(stderr, "usage: %s log [-c commit] [repository-path]\n",
97 static const struct got_error *
98 draw_commit(struct got_commit_object *commit, struct got_object_id *id)
100 const struct got_error *err = NULL;
101 char *logmsg0 = NULL, *logmsg = NULL;
102 char *author0 = NULL, *author = NULL;
103 wchar_t *wlogmsg = NULL, *wauthor = NULL;
104 int author_width, logmsg_width;
105 char *newline, *smallerthan;
110 static const size_t id_display_cols = 8;
111 static const size_t author_display_cols = 16;
112 const int avail = COLS - 1;
114 err = got_object_id_str(&id_str, id);
117 id_len = strlen(id_str);
118 if (avail < id_display_cols) {
119 limit = MIN(id_len, avail);
120 waddnstr(tog_log_view.window, id_str, limit);
122 limit = MIN(id_display_cols, id_len);
123 waddnstr(tog_log_view.window, id_str, limit);
126 while (col < avail && col < id_display_cols + 2) {
127 waddch(tog_log_view.window, ' ');
133 author0 = strdup(commit->author);
134 if (author0 == NULL) {
135 err = got_error_from_errno();
139 smallerthan = strchr(author, '<');
143 char *at = strchr(author, '@');
147 limit = MIN(avail, author_display_cols);
148 err = format_line(&wauthor, &author_width, author, limit);
151 waddwstr(tog_log_view.window, wauthor);
153 while (col < avail && author_width < author_display_cols + 1) {
154 waddch(tog_log_view.window, ' ');
161 logmsg0 = strdup(commit->logmsg);
162 if (logmsg0 == NULL) {
163 err = got_error_from_errno();
167 while (*logmsg == '\n')
169 newline = strchr(logmsg, '\n');
173 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
176 waddwstr(tog_log_view.window, wlogmsg);
178 while (col <= avail) {
179 waddch(tog_log_view.window, ' ');
183 waddch(tog_log_view.window, '\n');
194 struct commit_queue_entry {
195 TAILQ_ENTRY(commit_queue_entry) entry;
196 struct got_object_id *id;
197 struct got_commit_object *commit;
199 TAILQ_HEAD(commit_queue, commit_queue_entry);
201 static struct commit_queue_entry *
202 alloc_commit_queue_entry(struct got_commit_object *commit,
203 struct got_object_id *id)
205 struct commit_queue_entry *entry;
207 entry = calloc(1, sizeof(*entry));
212 entry->commit = commit;
217 pop_commit(struct commit_queue *commits)
219 struct commit_queue_entry *entry;
221 entry = TAILQ_FIRST(commits);
222 TAILQ_REMOVE(commits, entry, entry);
223 got_object_commit_close(entry->commit);
229 free_commits(struct commit_queue *commits)
231 while (!TAILQ_EMPTY(commits))
235 static const struct got_error *
236 fetch_parent_commit(struct commit_queue_entry **pentry,
237 struct commit_queue_entry *entry, struct got_repository *repo)
239 const struct got_error *err = NULL;
240 struct got_object *obj = NULL;
241 struct got_commit_object *commit;
242 struct got_object_id *id;
243 struct got_parent_id *pid;
247 /* Follow the first parent (TODO: handle merge commits). */
248 pid = SIMPLEQ_FIRST(&entry->commit->parent_ids);
251 err = got_object_open(&obj, repo, pid->id);
254 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
255 err = got_error(GOT_ERR_OBJ_TYPE);
256 got_object_close(obj);
260 err = got_object_commit_open(&commit, repo, obj);
261 got_object_close(obj);
265 id = got_object_id_dup(pid->id);
267 err = got_error_from_errno();
268 got_object_commit_close(commit);
272 *pentry = alloc_commit_queue_entry(commit, id);
273 if (*pentry == NULL) {
274 err = got_error_from_errno();
275 got_object_commit_close(commit);
281 static const struct got_error *
282 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
284 const struct got_error *err = NULL;
285 struct got_reference *head_ref;
289 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
293 err = got_ref_resolve(head_id, repo, head_ref);
294 got_ref_close(head_ref);
303 static const struct got_error *
304 prepend_commits(int *ncommits, struct commit_queue *commits,
305 struct got_object_id *first_id, struct got_object_id *last_id,
306 int limit, struct got_repository *repo)
308 const struct got_error *err = NULL;
309 struct got_object *first_obj = NULL, *last_obj = NULL;
310 struct got_commit_object *commit = NULL;
311 struct got_object_id *id = NULL;
312 struct commit_queue_entry *entry, *old_head_entry;
316 err = got_object_open(&first_obj, repo, first_id);
319 if (got_object_get_type(first_obj) != GOT_OBJ_TYPE_COMMIT) {
320 err = got_error(GOT_ERR_OBJ_TYPE);
323 err = got_object_open(&last_obj, repo, last_id);
326 if (got_object_get_type(last_obj) != GOT_OBJ_TYPE_COMMIT) {
327 err = got_error(GOT_ERR_OBJ_TYPE);
331 err = got_object_commit_open(&commit, repo, first_obj);
335 id = got_object_id_dup(first_id);
337 err = got_error_from_errno();
341 entry = alloc_commit_queue_entry(commit, id);
343 return got_error_from_errno();
345 old_head_entry = TAILQ_FIRST(commits);
347 TAILQ_INSERT_BEFORE(old_head_entry, entry, entry);
349 TAILQ_INSERT_HEAD(commits, entry, entry);
354 * Fetch parent commits.
355 * XXX If first and last commit aren't ancestrally related this loop
356 * we will keep iterating until a root commit is encountered.
359 struct commit_queue_entry *pentry;
361 err = fetch_parent_commit(&pentry, entry, repo);
368 * Fill up to old HEAD commit if commit queue was not empty.
369 * We must not leave a gap in history.
371 if (old_head_entry &&
372 got_object_id_cmp(pentry->id, old_head_entry->id) == 0)
375 TAILQ_INSERT_AFTER(commits, entry, pentry, entry);
377 if (*ncommits >= limit)
380 /* Fill up to last requested commit if queue was empty. */
381 if (old_head_entry == NULL &&
382 got_object_id_cmp(pentry->id, last_id) == 0)
390 got_object_close(first_obj);
392 got_object_close(last_obj);
396 static const struct got_error *
397 fetch_commits(struct commit_queue_entry **start_entry,
398 struct got_object_id *start_id, struct commit_queue *commits,
399 int limit, struct got_repository *repo)
401 const struct got_error *err;
402 struct commit_queue_entry *entry;
404 struct got_object_id *head_id = NULL;
408 err = get_head_commit_id(&head_id, repo);
412 /* Prepend HEAD commit and all ancestors up to start commit. */
413 err = prepend_commits(&ncommits, commits, head_id, start_id, limit,
418 if (got_object_id_cmp(head_id, start_id) == 0)
419 *start_entry = TAILQ_FIRST(commits);
421 *start_entry = TAILQ_LAST(commits, commit_queue);
423 if (ncommits >= limit)
426 /* Append more commits from start commit up to the requested limit. */
427 entry = TAILQ_LAST(commits, commit_queue);
428 while (entry && ncommits < limit) {
429 struct commit_queue_entry *pentry;
431 err = fetch_parent_commit(&pentry, entry, repo);
435 TAILQ_INSERT_TAIL(commits, pentry, entry);
445 static const struct got_error *
446 draw_commits(struct commit_queue_entry **last, struct commit_queue_entry **selected,
447 struct commit_queue_entry *first, int selected_idx, int limit)
449 const struct got_error *err = NULL;
450 struct commit_queue_entry *entry;
453 werase(tog_log_view.window);
458 if (ncommits == limit)
460 if (ncommits == selected_idx) {
461 wstandout(tog_log_view.window);
464 err = draw_commit(entry->commit, entry->id);
465 if (ncommits == selected_idx)
466 wstandend(tog_log_view.window);
471 entry = TAILQ_NEXT(entry, entry);
481 scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
482 struct commit_queue *commits)
484 struct commit_queue_entry *entry;
487 entry = TAILQ_FIRST(commits);
488 if (*first_displayed_entry == entry)
491 entry = *first_displayed_entry;
492 while (entry && nscrolled < maxscroll) {
493 entry = TAILQ_PREV(entry, commit_queue, entry);
495 *first_displayed_entry = entry;
501 static const struct got_error *
502 scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
503 struct commit_queue_entry *last_displayed_entry,
504 struct commit_queue *commits, struct got_repository *repo)
506 const struct got_error *err = NULL;
507 struct commit_queue_entry *pentry;
511 pentry = TAILQ_NEXT(last_displayed_entry, entry);
512 if (pentry == NULL) {
513 err = fetch_parent_commit(&pentry,
514 last_displayed_entry, repo);
515 if (err || pentry == NULL)
517 TAILQ_INSERT_TAIL(commits, pentry, entry);
519 last_displayed_entry = pentry;
521 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
524 *first_displayed_entry = pentry;
525 } while (++nscrolled < maxscroll);
531 num_parents(struct commit_queue_entry *entry)
536 entry = TAILQ_NEXT(entry, entry);
543 static const struct got_error *
544 show_commit(struct commit_queue_entry *entry, struct got_repository *repo)
546 const struct got_error *err;
547 struct commit_queue_entry *pentry;
548 struct got_object *obj1 = NULL, *obj2 = NULL;
550 err = got_object_open(&obj2, repo, entry->id);
554 pentry = TAILQ_NEXT(entry, entry);
555 if (pentry == NULL) {
556 err = fetch_parent_commit(&pentry, entry, repo);
561 err = got_object_open(&obj1, repo, pentry->id);
566 err = show_diff_view(obj1, obj2, repo);
569 got_object_close(obj1);
571 got_object_close(obj2);
575 static const struct got_error *
576 show_log_view(struct got_object_id *start_id, struct got_repository *repo)
578 const struct got_error *err = NULL;
579 struct got_object_id *id;
580 int ch, done = 0, selected = 0, nparents;
581 struct commit_queue commits;
582 struct commit_queue_entry *first_displayed_entry = NULL;
583 struct commit_queue_entry *last_displayed_entry = NULL;
584 struct commit_queue_entry *selected_entry = NULL;
586 id = got_object_id_dup(start_id);
588 return got_error_from_errno();
590 if (tog_log_view.window == NULL) {
591 tog_log_view.window = newwin(0, 0, 0, 0);
592 if (tog_log_view.window == NULL)
593 return got_error_from_errno();
594 keypad(tog_log_view.window, TRUE);
596 if (tog_log_view.panel == NULL) {
597 tog_log_view.panel = new_panel(tog_log_view.window);
598 if (tog_log_view.panel == NULL)
599 return got_error_from_errno();
601 show_panel(tog_log_view.panel);
603 TAILQ_INIT(&commits);
604 err = fetch_commits(&first_displayed_entry, id, &commits, LINES, repo);
608 err = draw_commits(&last_displayed_entry, &selected_entry,
609 first_displayed_entry, selected, LINES);
613 nodelay(stdscr, FALSE);
614 ch = wgetch(tog_log_view.window);
615 nodelay(stdscr, TRUE);
619 err = got_error_from_errno();
632 scroll_up(&first_displayed_entry, 1, &commits);
635 if (TAILQ_FIRST(&commits) ==
636 first_displayed_entry) {
640 scroll_up(&first_displayed_entry, LINES,
645 nparents = num_parents(first_displayed_entry);
646 if (selected < LINES - 1 &&
647 selected < nparents - 1) {
651 err = scroll_down(&first_displayed_entry, 1,
652 last_displayed_entry, &commits, repo);
657 err = scroll_down(&first_displayed_entry, LINES,
658 last_displayed_entry, &commits, repo);
661 if (last_displayed_entry->commit->nparents > 0)
663 /* can't scroll any further; move cursor down */
664 nparents = num_parents(first_displayed_entry);
665 if (selected < LINES - 1 ||
666 selected < nparents - 1)
667 selected = MIN(LINES - 1, nparents - 1);
670 if (selected > LINES)
671 selected = LINES - 1;
675 err = show_commit(selected_entry, repo);
678 show_panel(tog_log_view.panel);
685 free_commits(&commits);
689 static const struct got_error *
690 cmd_log(int argc, char *argv[])
692 const struct got_error *error;
693 struct got_repository *repo;
694 struct got_object_id *start_id = NULL;
695 char *repo_path = NULL;
696 char *start_commit = NULL;
700 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
704 while ((ch = getopt(argc, argv, "c:")) != -1) {
707 start_commit = optarg;
719 repo_path = getcwd(NULL, 0);
720 if (repo_path == NULL)
721 return got_error_from_errno();
722 } else if (argc == 1) {
723 repo_path = realpath(argv[0], NULL);
724 if (repo_path == NULL)
725 return got_error_from_errno();
729 error = got_repo_open(&repo, repo_path);
734 if (start_commit == NULL) {
735 error = get_head_commit_id(&start_id, repo);
739 struct got_object *obj;
740 error = got_object_open_by_id_str(&obj, repo, start_commit);
742 start_id = got_object_get_id(obj);
743 if (start_id == NULL)
744 error = got_error_from_errno();
749 error = show_log_view(start_id, repo);
751 got_repo_close(repo);
759 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
765 parse_next_line(FILE *f, size_t *len)
770 const char delim[3] = { '\0', '\0', '\0'};
772 line = fparseln(f, &linelen, &lineno, delim, 0);
778 static const struct got_error *
779 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
781 const struct got_error *err = NULL;
784 *wlen = mbstowcs(NULL, s, 0);
785 if (*wlen == (size_t)-1)
786 return got_error_from_errno();
788 *ws = calloc(*wlen + 1, sizeof(*ws));
790 return got_error_from_errno();
792 if (mbstowcs(*ws, s, *wlen) != *wlen)
793 err = got_error_from_errno();
803 /* Format a line for display, ensuring that it won't overflow a width limit. */
804 static const struct got_error *
805 format_line(wchar_t **wlinep, int *widthp, char *line, int wlimit)
807 const struct got_error *err = NULL;
809 wchar_t *wline = NULL;
815 err = mbs2ws(&wline, &wlen, line);
820 while (i < wlen && cols <= wlimit) {
821 int width = wcwidth(wline[i]);
830 if (wline[i] == L'\t')
834 err = got_error_from_errno();
852 static const struct got_error *
853 draw_diff(FILE *f, int *first_displayed_line, int *last_displayed_line,
854 int *eof, int max_lines)
856 const struct got_error *err;
857 int nlines = 0, nprinted = 0;
863 werase(tog_diff_view.window);
866 while (nprinted < max_lines) {
867 line = parse_next_line(f, &len);
872 if (++nlines < *first_displayed_line) {
877 err = format_line(&wline, NULL, line, COLS);
882 waddwstr(tog_diff_view.window, wline);
883 waddch(tog_diff_view.window, '\n');
885 *first_displayed_line = nlines;
888 *last_displayed_line = nlines;
896 static const struct got_error *
897 show_diff_view(struct got_object *obj1, struct got_object *obj2,
898 struct got_repository *repo)
900 const struct got_error *err;
902 int ch, done = 0, first_displayed_line = 1, last_displayed_line = LINES;
905 if (obj1 != NULL && obj2 != NULL &&
906 got_object_get_type(obj1) != got_object_get_type(obj2))
907 return got_error(GOT_ERR_OBJ_TYPE);
911 return got_error_from_errno();
913 switch (got_object_get_type(obj1 ? obj1 : obj2)) {
914 case GOT_OBJ_TYPE_BLOB:
915 err = got_diff_objects_as_blobs(obj1, obj2, repo, f);
917 case GOT_OBJ_TYPE_TREE:
918 err = got_diff_objects_as_trees(obj1, obj2, repo, f);
920 case GOT_OBJ_TYPE_COMMIT:
921 err = got_diff_objects_as_commits(obj1, obj2, repo, f);
924 return got_error(GOT_ERR_OBJ_TYPE);
929 if (tog_diff_view.window == NULL) {
930 tog_diff_view.window = newwin(0, 0, 0, 0);
931 if (tog_diff_view.window == NULL)
932 return got_error_from_errno();
933 keypad(tog_diff_view.window, TRUE);
935 if (tog_diff_view.panel == NULL) {
936 tog_diff_view.panel = new_panel(tog_diff_view.window);
937 if (tog_diff_view.panel == NULL)
938 return got_error_from_errno();
940 show_panel(tog_diff_view.panel);
943 err = draw_diff(f, &first_displayed_line, &last_displayed_line,
947 nodelay(stdscr, FALSE);
948 ch = wgetch(tog_diff_view.window);
949 nodelay(stdscr, TRUE);
957 if (first_displayed_line > 1)
958 first_displayed_line--;
962 while (i++ < LINES - 1 &&
963 first_displayed_line > 1)
964 first_displayed_line--;
971 first_displayed_line++;
976 while (!eof && i++ < LINES - 1) {
977 char *line = parse_next_line(f, NULL);
978 first_displayed_line++;
991 static const struct got_error *
992 cmd_diff(int argc, char *argv[])
994 const struct got_error *error = NULL;
995 struct got_repository *repo = NULL;
996 struct got_object *obj1 = NULL, *obj2 = NULL;
997 char *repo_path = NULL;
998 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
1002 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1006 while ((ch = getopt(argc, argv, "")) != -1) {
1018 usage_diff(); /* TODO show local worktree changes */
1019 } else if (argc == 2) {
1020 repo_path = getcwd(NULL, 0);
1021 if (repo_path == NULL)
1022 return got_error_from_errno();
1023 obj_id_str1 = argv[0];
1024 obj_id_str2 = argv[1];
1025 } else if (argc == 3) {
1026 repo_path = realpath(argv[0], NULL);
1027 if (repo_path == NULL)
1028 return got_error_from_errno();
1029 obj_id_str1 = argv[1];
1030 obj_id_str2 = argv[2];
1034 error = got_repo_open(&repo, repo_path);
1039 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
1043 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1047 error = show_diff_view(obj1, obj2, repo);
1049 got_repo_close(repo);
1051 got_object_close(obj1);
1053 got_object_close(obj2);
1061 fprintf(stderr, "usage: %s blame [repository-path] blob-object\n",
1066 static const struct got_error *
1067 cmd_blame(int argc, char *argv[])
1069 return got_error(GOT_ERR_NOT_IMPL);
1079 intrflush(stdscr, FALSE);
1080 keypad(stdscr, TRUE);
1089 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
1090 "Available commands:\n", getprogname());
1091 for (i = 0; i < nitems(tog_commands); i++) {
1092 struct tog_cmd *cmd = &tog_commands[i];
1093 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
1099 make_argv(const char *arg0, const char *arg1)
1102 int argc = (arg1 == NULL ? 1 : 2);
1104 argv = calloc(argc, sizeof(char *));
1107 argv[0] = strdup(arg0);
1108 if (argv[0] == NULL)
1111 argv[1] = strdup(arg1);
1112 if (argv[1] == NULL)
1120 main(int argc, char *argv[])
1122 const struct got_error *error = NULL;
1123 struct tog_cmd *cmd = NULL;
1125 char **cmd_argv = NULL;
1127 setlocale(LC_ALL, "");
1129 while ((ch = getopt(argc, argv, "h")) != -1) {
1146 /* Build an argument vector which runs a default command. */
1147 cmd = &tog_commands[0];
1148 cmd_argv = make_argv(cmd->name, NULL);
1153 /* Did the user specific a command? */
1154 for (i = 0; i < nitems(tog_commands); i++) {
1155 if (strncmp(tog_commands[i].name, argv[0],
1156 strlen(argv[0])) == 0) {
1157 cmd = &tog_commands[i];
1159 tog_commands[i].cmd_usage();
1164 /* Did the user specify a repository? */
1165 char *repo_path = realpath(argv[0], NULL);
1167 struct got_repository *repo;
1168 error = got_repo_open(&repo, repo_path);
1170 got_repo_close(repo);
1172 error = got_error_from_errno();
1174 fprintf(stderr, "%s: '%s' is neither a known "
1175 "command nor a path to a repository\n",
1176 getprogname(), argv[0]);
1180 cmd = &tog_commands[0];
1181 cmd_argv = make_argv(cmd->name, repo_path);
1189 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
1196 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);