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 *);
90 fprintf(stderr, "usage: %s log [-c commit] [repository-path]\n",
95 static const struct got_error *
96 draw_commit(struct got_commit_object *commit, struct got_object_id *id)
98 const struct got_error *err = NULL;
99 char *logmsg0 = NULL, *logmsg = NULL;
100 char *author0 = NULL, *author = NULL;
101 char *newline, *smallerthan;
104 const size_t id_display_len = 8;
105 const size_t author_display_len = 16;
106 size_t id_len, author_len, logmsg_len, avail;
109 err = got_object_id_str(&id_str, id);
112 id_len = strlen(id_str);
114 logmsg0 = strdup(commit->logmsg);
115 if (logmsg0 == NULL) {
116 err = got_error_from_errno();
120 while (*logmsg == '\n')
122 newline = strchr(logmsg, '\n');
125 logmsg_len = strlen(logmsg);
127 author0 = strdup(commit->author);
128 if (author0 == NULL) {
129 err = got_error_from_errno();
133 smallerthan = strchr(author, '<');
137 char *at = strchr(author, '@');
141 author_len = strlen(author);
144 line = calloc(avail + 1, sizeof(*line));
146 err = got_error_from_errno();
151 for (i = 0; i < MIN(id_display_len, id_len); i++) {
154 line[col++] = id_str[i];
156 while (i < id_display_len) {
165 for (i = 0; i < MIN(author_display_len, author_len); i++) {
168 line[col++] = author[i];
170 while (i < author_display_len) {
180 while (col < avail && *logmsg)
181 line[col++] = *logmsg++;
185 waddstr(tog_log_view.window, line);
186 waddch(tog_log_view.window, '\n');
195 struct commit_queue_entry {
196 TAILQ_ENTRY(commit_queue_entry) entry;
197 struct got_object_id *id;
198 struct got_commit_object *commit;
200 TAILQ_HEAD(commit_queue, commit_queue_entry);
202 static struct commit_queue_entry *
203 alloc_commit_queue_entry(struct got_commit_object *commit,
204 struct got_object_id *id)
206 struct commit_queue_entry *entry;
208 entry = calloc(1, sizeof(*entry));
213 entry->commit = commit;
218 pop_commit(struct commit_queue *commits)
220 struct commit_queue_entry *entry;
222 entry = TAILQ_FIRST(commits);
223 TAILQ_REMOVE(commits, entry, entry);
224 got_object_commit_close(entry->commit);
230 free_commits(struct commit_queue *commits)
232 while (!TAILQ_EMPTY(commits))
236 static const struct got_error *
237 fetch_parent_commit(struct commit_queue_entry **pentry,
238 struct commit_queue_entry *entry, struct got_repository *repo)
240 const struct got_error *err = NULL;
241 struct got_object *obj = NULL;
242 struct got_commit_object *commit;
243 struct got_object_id *id;
244 struct got_parent_id *pid;
248 /* Follow the first parent (TODO: handle merge commits). */
249 pid = SIMPLEQ_FIRST(&entry->commit->parent_ids);
252 err = got_object_open(&obj, repo, pid->id);
255 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
256 err = got_error(GOT_ERR_OBJ_TYPE);
257 got_object_close(obj);
261 err = got_object_commit_open(&commit, repo, obj);
262 got_object_close(obj);
266 id = got_object_id_dup(pid->id);
268 err = got_error_from_errno();
269 got_object_commit_close(commit);
273 *pentry = alloc_commit_queue_entry(commit, id);
274 if (*pentry == NULL) {
275 err = got_error_from_errno();
276 got_object_commit_close(commit);
282 static const struct got_error *
283 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
285 const struct got_error *err = NULL;
286 struct got_reference *head_ref;
290 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
294 err = got_ref_resolve(head_id, repo, head_ref);
295 got_ref_close(head_ref);
304 static const struct got_error *
305 prepend_commits(int *ncommits, struct commit_queue *commits,
306 struct got_object_id *first_id, struct got_object_id *last_id,
307 int limit, struct got_repository *repo)
309 const struct got_error *err = NULL;
310 struct got_object *first_obj = NULL, *last_obj = NULL;
311 struct got_commit_object *commit = NULL;
312 struct got_object_id *id = NULL;
313 struct commit_queue_entry *entry, *old_head_entry;
317 err = got_object_open(&first_obj, repo, first_id);
320 if (got_object_get_type(first_obj) != GOT_OBJ_TYPE_COMMIT) {
321 err = got_error(GOT_ERR_OBJ_TYPE);
324 err = got_object_open(&last_obj, repo, last_id);
327 if (got_object_get_type(last_obj) != GOT_OBJ_TYPE_COMMIT) {
328 err = got_error(GOT_ERR_OBJ_TYPE);
332 err = got_object_commit_open(&commit, repo, first_obj);
336 id = got_object_id_dup(first_id);
338 err = got_error_from_errno();
342 entry = alloc_commit_queue_entry(commit, id);
344 return got_error_from_errno();
346 old_head_entry = TAILQ_FIRST(commits);
348 TAILQ_INSERT_BEFORE(old_head_entry, entry, entry);
350 TAILQ_INSERT_HEAD(commits, entry, entry);
355 * Fetch parent commits.
356 * XXX If first and last commit aren't ancestrally related this loop
357 * we will keep iterating until a root commit is encountered.
360 struct commit_queue_entry *pentry;
362 err = fetch_parent_commit(&pentry, entry, repo);
369 * Fill up to old HEAD commit if commit queue was not empty.
370 * We must not leave a gap in history.
372 if (old_head_entry &&
373 got_object_id_cmp(pentry->id, old_head_entry->id) == 0)
376 TAILQ_INSERT_AFTER(commits, entry, pentry, entry);
378 if (*ncommits >= limit)
381 /* Fill up to last requested commit if queue was empty. */
382 if (old_head_entry == NULL &&
383 got_object_id_cmp(pentry->id, last_id) == 0)
391 got_object_close(first_obj);
393 got_object_close(last_obj);
397 static const struct got_error *
398 fetch_commits(struct commit_queue_entry **start_entry,
399 struct got_object_id *start_id, struct commit_queue *commits,
400 int limit, struct got_repository *repo)
402 const struct got_error *err;
403 struct commit_queue_entry *entry;
405 struct got_object_id *head_id = NULL;
409 err = get_head_commit_id(&head_id, repo);
413 /* Prepend HEAD commit and all ancestors up to start commit. */
414 err = prepend_commits(&ncommits, commits, head_id, start_id, limit,
419 if (got_object_id_cmp(head_id, start_id) == 0)
420 *start_entry = TAILQ_FIRST(commits);
422 *start_entry = TAILQ_LAST(commits, commit_queue);
424 if (ncommits >= limit)
427 /* Append more commits from start commit up to the requested limit. */
428 entry = TAILQ_LAST(commits, commit_queue);
429 while (entry && ncommits < limit) {
430 struct commit_queue_entry *pentry;
432 err = fetch_parent_commit(&pentry, entry, repo);
436 TAILQ_INSERT_TAIL(commits, pentry, entry);
446 static const struct got_error *
447 draw_commits(struct commit_queue_entry **last, struct commit_queue_entry **selected,
448 struct commit_queue_entry *first, int selected_idx, int limit)
450 const struct got_error *err = NULL;
451 struct commit_queue_entry *entry;
454 werase(tog_log_view.window);
459 if (ncommits == limit)
461 if (ncommits == selected_idx) {
462 wstandout(tog_log_view.window);
465 err = draw_commit(entry->commit, entry->id);
466 if (ncommits == selected_idx)
467 wstandend(tog_log_view.window);
472 entry = TAILQ_NEXT(entry, entry);
482 scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
483 struct commit_queue *commits)
485 struct commit_queue_entry *entry;
488 entry = TAILQ_FIRST(commits);
489 if (*first_displayed_entry == entry)
492 entry = *first_displayed_entry;
493 while (entry && nscrolled < maxscroll) {
494 entry = TAILQ_PREV(entry, commit_queue, entry);
496 *first_displayed_entry = entry;
502 static const struct got_error *
503 scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
504 struct commit_queue_entry *last_displayed_entry,
505 struct commit_queue *commits, struct got_repository *repo)
507 const struct got_error *err = NULL;
508 struct commit_queue_entry *pentry;
512 pentry = TAILQ_NEXT(last_displayed_entry, entry);
513 if (pentry == NULL) {
514 err = fetch_parent_commit(&pentry,
515 last_displayed_entry, repo);
516 if (err || pentry == NULL)
518 TAILQ_INSERT_TAIL(commits, pentry, entry);
520 last_displayed_entry = pentry;
522 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
525 *first_displayed_entry = pentry;
526 } while (++nscrolled < maxscroll);
532 num_parents(struct commit_queue_entry *entry)
537 entry = TAILQ_NEXT(entry, entry);
544 static const struct got_error *
545 show_commit(struct commit_queue_entry *entry, struct got_repository *repo)
547 const struct got_error *err;
548 struct commit_queue_entry *pentry;
549 struct got_object *obj1 = NULL, *obj2 = NULL;
551 err = got_object_open(&obj2, repo, entry->id);
555 pentry = TAILQ_NEXT(entry, entry);
556 if (pentry == NULL) {
557 err = fetch_parent_commit(&pentry, entry, repo);
562 err = got_object_open(&obj1, repo, pentry->id);
567 err = show_diff_view(obj1, obj2, repo);
570 got_object_close(obj1);
572 got_object_close(obj2);
576 static const struct got_error *
577 show_log_view(struct got_object_id *start_id, struct got_repository *repo)
579 const struct got_error *err = NULL;
580 struct got_object_id *id;
581 int ch, done = 0, selected = 0, nparents;
582 struct commit_queue commits;
583 struct commit_queue_entry *first_displayed_entry = NULL;
584 struct commit_queue_entry *last_displayed_entry = NULL;
585 struct commit_queue_entry *selected_entry = NULL;
587 id = got_object_id_dup(start_id);
589 return got_error_from_errno();
591 if (tog_log_view.window == NULL) {
592 tog_log_view.window = newwin(0, 0, 0, 0);
593 if (tog_log_view.window == NULL)
594 return got_error_from_errno();
595 keypad(tog_log_view.window, TRUE);
597 if (tog_log_view.panel == NULL) {
598 tog_log_view.panel = new_panel(tog_log_view.window);
599 if (tog_log_view.panel == NULL)
600 return got_error_from_errno();
602 show_panel(tog_log_view.panel);
604 TAILQ_INIT(&commits);
605 err = fetch_commits(&first_displayed_entry, id, &commits, LINES, repo);
609 err = draw_commits(&last_displayed_entry, &selected_entry,
610 first_displayed_entry, selected, LINES);
614 nodelay(stdscr, FALSE);
615 ch = wgetch(tog_log_view.window);
616 nodelay(stdscr, TRUE);
620 err = got_error_from_errno();
633 scroll_up(&first_displayed_entry, 1, &commits);
636 if (TAILQ_FIRST(&commits) ==
637 first_displayed_entry) {
641 scroll_up(&first_displayed_entry, LINES,
646 nparents = num_parents(first_displayed_entry);
647 if (selected < LINES - 1 &&
648 selected < nparents - 1) {
652 err = scroll_down(&first_displayed_entry, 1,
653 last_displayed_entry, &commits, repo);
658 err = scroll_down(&first_displayed_entry, LINES,
659 last_displayed_entry, &commits, repo);
662 if (last_displayed_entry->commit->nparents > 0)
664 /* can't scroll any further; move cursor down */
665 nparents = num_parents(first_displayed_entry);
666 if (selected < LINES - 1 ||
667 selected < nparents - 1)
668 selected = MIN(LINES - 1, nparents - 1);
671 if (selected > LINES)
672 selected = LINES - 1;
676 err = show_commit(selected_entry, repo);
679 show_panel(tog_log_view.panel);
686 free_commits(&commits);
690 static const struct got_error *
691 cmd_log(int argc, char *argv[])
693 const struct got_error *error;
694 struct got_repository *repo;
695 struct got_object_id *start_id = NULL;
696 char *repo_path = NULL;
697 char *start_commit = NULL;
701 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
705 while ((ch = getopt(argc, argv, "c:")) != -1) {
708 start_commit = optarg;
720 repo_path = getcwd(NULL, 0);
721 if (repo_path == NULL)
722 return got_error_from_errno();
723 } else if (argc == 1) {
724 repo_path = realpath(argv[0], NULL);
725 if (repo_path == NULL)
726 return got_error_from_errno();
730 error = got_repo_open(&repo, repo_path);
735 if (start_commit == NULL) {
736 error = get_head_commit_id(&start_id, repo);
740 struct got_object *obj;
741 error = got_object_open_by_id_str(&obj, repo, start_commit);
743 start_id = got_object_get_id(obj);
744 if (start_id == NULL)
745 error = got_error_from_errno();
750 error = show_log_view(start_id, repo);
752 got_repo_close(repo);
760 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
766 parse_next_line(FILE *f, size_t *len)
771 const char delim[3] = { '\0', '\0', '\0'};
773 line = fparseln(f, &linelen, &lineno, delim, 0);
779 /* Format a line for display, ensuring that it won't overflow COLS columns. */
780 static const struct got_error *
781 format_line(wchar_t **wlinep, char *line)
783 const struct got_error *err = NULL;
785 wchar_t *wline = NULL;
791 wlen = mbstowcs(NULL, line, 0);
792 if (wlen == (size_t)-1)
793 return got_error_from_errno();
795 wline = calloc(wlen + 1, sizeof(wchar_t));
797 return got_error_from_errno();
799 if (mbstowcs(wline, line, wlen) != wlen) {
800 err = got_error_from_errno();
805 while (i < wlen && cols <= COLS) {
806 int width = wcwidth(wline[i]);
815 if (wline[i] == L'\t')
819 err = got_error_from_errno();
834 static const struct got_error *
835 draw_diff(FILE *f, int *first_displayed_line, int *last_displayed_line,
836 int *eof, int max_lines)
838 const struct got_error *err;
839 int nlines = 0, nprinted = 0;
845 werase(tog_diff_view.window);
848 while (nprinted < max_lines) {
849 line = parse_next_line(f, &len);
854 if (++nlines < *first_displayed_line) {
859 err = format_line(&wline, line);
864 waddwstr(tog_diff_view.window, wline);
865 waddch(tog_diff_view.window, '\n');
867 *first_displayed_line = nlines;
870 *last_displayed_line = nlines;
878 static const struct got_error *
879 show_diff_view(struct got_object *obj1, struct got_object *obj2,
880 struct got_repository *repo)
882 const struct got_error *err;
884 int ch, done = 0, first_displayed_line = 1, last_displayed_line = LINES;
887 if (obj1 != NULL && obj2 != NULL &&
888 got_object_get_type(obj1) != got_object_get_type(obj2))
889 return got_error(GOT_ERR_OBJ_TYPE);
893 return got_error_from_errno();
895 switch (got_object_get_type(obj1 ? obj1 : obj2)) {
896 case GOT_OBJ_TYPE_BLOB:
897 err = got_diff_objects_as_blobs(obj1, obj2, repo, f);
899 case GOT_OBJ_TYPE_TREE:
900 err = got_diff_objects_as_trees(obj1, obj2, repo, f);
902 case GOT_OBJ_TYPE_COMMIT:
903 err = got_diff_objects_as_commits(obj1, obj2, repo, f);
906 return got_error(GOT_ERR_OBJ_TYPE);
911 if (tog_diff_view.window == NULL) {
912 tog_diff_view.window = newwin(0, 0, 0, 0);
913 if (tog_diff_view.window == NULL)
914 return got_error_from_errno();
915 keypad(tog_diff_view.window, TRUE);
917 if (tog_diff_view.panel == NULL) {
918 tog_diff_view.panel = new_panel(tog_diff_view.window);
919 if (tog_diff_view.panel == NULL)
920 return got_error_from_errno();
922 show_panel(tog_diff_view.panel);
925 err = draw_diff(f, &first_displayed_line, &last_displayed_line,
929 nodelay(stdscr, FALSE);
930 ch = wgetch(tog_diff_view.window);
931 nodelay(stdscr, TRUE);
939 if (first_displayed_line > 1)
940 first_displayed_line--;
944 while (i++ < LINES - 1 &&
945 first_displayed_line > 1)
946 first_displayed_line--;
953 first_displayed_line++;
958 while (!eof && i++ < LINES - 1) {
959 char *line = parse_next_line(f, NULL);
960 first_displayed_line++;
973 static const struct got_error *
974 cmd_diff(int argc, char *argv[])
976 const struct got_error *error = NULL;
977 struct got_repository *repo = NULL;
978 struct got_object *obj1 = NULL, *obj2 = NULL;
979 char *repo_path = NULL;
980 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
984 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
988 while ((ch = getopt(argc, argv, "")) != -1) {
1000 usage_diff(); /* TODO show local worktree changes */
1001 } else if (argc == 2) {
1002 repo_path = getcwd(NULL, 0);
1003 if (repo_path == NULL)
1004 return got_error_from_errno();
1005 obj_id_str1 = argv[0];
1006 obj_id_str2 = argv[1];
1007 } else if (argc == 3) {
1008 repo_path = realpath(argv[0], NULL);
1009 if (repo_path == NULL)
1010 return got_error_from_errno();
1011 obj_id_str1 = argv[1];
1012 obj_id_str2 = argv[2];
1016 error = got_repo_open(&repo, repo_path);
1021 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
1025 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1029 error = show_diff_view(obj1, obj2, repo);
1031 got_repo_close(repo);
1033 got_object_close(obj1);
1035 got_object_close(obj2);
1043 fprintf(stderr, "usage: %s blame [repository-path] blob-object\n",
1048 static const struct got_error *
1049 cmd_blame(int argc, char *argv[])
1051 return got_error(GOT_ERR_NOT_IMPL);
1061 intrflush(stdscr, FALSE);
1062 keypad(stdscr, TRUE);
1071 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
1072 "Available commands:\n", getprogname());
1073 for (i = 0; i < nitems(tog_commands); i++) {
1074 struct tog_cmd *cmd = &tog_commands[i];
1075 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
1081 make_argv(const char *arg0, const char *arg1)
1084 int argc = (arg1 == NULL ? 1 : 2);
1086 argv = calloc(argc, sizeof(char *));
1089 argv[0] = strdup(arg0);
1090 if (argv[0] == NULL)
1093 argv[1] = strdup(arg1);
1094 if (argv[1] == NULL)
1102 main(int argc, char *argv[])
1104 const struct got_error *error = NULL;
1105 struct tog_cmd *cmd = NULL;
1107 char **cmd_argv = NULL;
1109 setlocale(LC_ALL, "");
1111 while ((ch = getopt(argc, argv, "h")) != -1) {
1128 /* Build an argument vector which runs a default command. */
1129 cmd = &tog_commands[0];
1130 cmd_argv = make_argv(cmd->name, NULL);
1135 /* Did the user specific a command? */
1136 for (i = 0; i < nitems(tog_commands); i++) {
1137 if (strncmp(tog_commands[i].name, argv[0],
1138 strlen(argv[0])) == 0) {
1139 cmd = &tog_commands[i];
1141 tog_commands[i].cmd_usage();
1146 /* Did the user specify a repository? */
1147 char *repo_path = realpath(argv[0], NULL);
1149 struct got_repository *repo;
1150 error = got_repo_open(&repo, repo_path);
1152 got_repo_close(repo);
1154 error = got_error_from_errno();
1156 fprintf(stderr, "%s: '%s' is neither a known "
1157 "command nor a path to a repository\n",
1158 getprogname(), argv[0]);
1162 cmd = &tog_commands[0];
1163 cmd_argv = make_argv(cmd->name, repo_path);
1171 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
1178 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);