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))
233 static const struct got_error *
234 fetch_parent_commit(struct commit_queue_entry **pentry,
235 struct commit_queue_entry *entry, struct got_repository *repo)
237 const struct got_error *err = NULL;
238 struct got_object *obj = NULL;
239 struct got_commit_object *commit;
240 struct got_object_id *id;
241 struct got_parent_id *pid;
245 /* Follow the first parent (TODO: handle merge commits). */
246 pid = SIMPLEQ_FIRST(&entry->commit->parent_ids);
249 err = got_object_open(&obj, repo, pid->id);
252 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
253 err = got_error(GOT_ERR_OBJ_TYPE);
254 got_object_close(obj);
258 err = got_object_commit_open(&commit, repo, obj);
259 got_object_close(obj);
263 id = got_object_id_dup(pid->id);
265 err = got_error_from_errno();
266 got_object_commit_close(commit);
270 *pentry = alloc_commit_queue_entry(commit, id);
271 if (*pentry == NULL) {
272 err = got_error_from_errno();
273 got_object_commit_close(commit);
279 static const struct got_error *
280 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
282 const struct got_error *err = NULL;
283 struct got_reference *head_ref;
287 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
291 err = got_ref_resolve(head_id, repo, head_ref);
292 got_ref_close(head_ref);
301 static const struct got_error *
302 prepend_commits(int *ncommits, struct commit_queue *commits,
303 struct got_object_id *first_id, struct got_object_id *last_id,
304 int limit, struct got_repository *repo)
306 const struct got_error *err = NULL;
307 struct got_object *first_obj = NULL, *last_obj = NULL;
308 struct got_commit_object *commit = NULL;
309 struct got_object_id *id = NULL;
310 struct commit_queue_entry *entry, *old_head_entry;
314 err = got_object_open(&first_obj, repo, first_id);
317 if (got_object_get_type(first_obj) != GOT_OBJ_TYPE_COMMIT) {
318 err = got_error(GOT_ERR_OBJ_TYPE);
321 err = got_object_open(&last_obj, repo, last_id);
324 if (got_object_get_type(last_obj) != GOT_OBJ_TYPE_COMMIT) {
325 err = got_error(GOT_ERR_OBJ_TYPE);
329 err = got_object_commit_open(&commit, repo, first_obj);
333 id = got_object_id_dup(first_id);
335 err = got_error_from_errno();
339 entry = alloc_commit_queue_entry(commit, id);
341 return got_error_from_errno();
343 old_head_entry = TAILQ_FIRST(commits);
345 TAILQ_INSERT_BEFORE(old_head_entry, entry, entry);
347 TAILQ_INSERT_HEAD(commits, entry, entry);
352 * Fetch parent commits.
353 * XXX If first and last commit aren't ancestrally related this loop
354 * we will keep iterating until a root commit is encountered.
357 struct commit_queue_entry *pentry;
359 err = fetch_parent_commit(&pentry, entry, repo);
366 * Fill up to old HEAD commit if commit queue was not empty.
367 * We must not leave a gap in history.
369 if (old_head_entry &&
370 got_object_id_cmp(pentry->id, old_head_entry->id) == 0)
373 TAILQ_INSERT_AFTER(commits, entry, pentry, entry);
375 if (*ncommits >= limit)
378 /* Fill up to last requested commit if queue was empty. */
379 if (old_head_entry == NULL &&
380 got_object_id_cmp(pentry->id, last_id) == 0)
388 got_object_close(first_obj);
390 got_object_close(last_obj);
394 static const struct got_error *
395 fetch_commits(struct commit_queue_entry **start_entry,
396 struct got_object_id *start_id, struct commit_queue *commits,
397 int limit, struct got_repository *repo)
399 const struct got_error *err;
400 struct commit_queue_entry *entry;
402 struct got_object_id *head_id = NULL;
406 err = get_head_commit_id(&head_id, repo);
410 /* Prepend HEAD commit and all ancestors up to start commit. */
411 err = prepend_commits(&ncommits, commits, head_id, start_id, limit,
416 if (got_object_id_cmp(head_id, start_id) == 0)
417 *start_entry = TAILQ_FIRST(commits);
419 *start_entry = TAILQ_LAST(commits, commit_queue);
421 if (ncommits >= limit)
424 /* Append more commits from start commit up to the requested limit. */
425 entry = TAILQ_LAST(commits, commit_queue);
426 while (entry && ncommits < limit) {
427 struct commit_queue_entry *pentry;
429 err = fetch_parent_commit(&pentry, entry, repo);
433 TAILQ_INSERT_TAIL(commits, pentry, entry);
443 static const struct got_error *
444 draw_commits(struct commit_queue_entry **last, struct commit_queue_entry **selected,
445 struct commit_queue_entry *first, int selected_idx, int limit)
447 const struct got_error *err = NULL;
448 struct commit_queue_entry *entry;
451 wclear(tog_log_view.window);
456 if (ncommits == limit)
458 if (ncommits == selected_idx) {
459 wstandout(tog_log_view.window);
462 err = draw_commit(entry->commit, entry->id);
463 if (ncommits == selected_idx)
464 wstandend(tog_log_view.window);
469 entry = TAILQ_NEXT(entry, entry);
479 scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
480 struct commit_queue *commits)
482 struct commit_queue_entry *entry;
485 entry = TAILQ_FIRST(commits);
486 if (*first_displayed_entry == entry)
489 entry = *first_displayed_entry;
490 while (entry && nscrolled < maxscroll) {
491 entry = TAILQ_PREV(entry, commit_queue, entry);
493 *first_displayed_entry = entry;
499 static const struct got_error *
500 scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
501 struct commit_queue_entry *last_displayed_entry,
502 struct commit_queue *commits, struct got_repository *repo)
504 const struct got_error *err = NULL;
505 struct commit_queue_entry *entry;
508 if (last_displayed_entry->commit->nparents == 0)
511 entry = *first_displayed_entry;
513 struct commit_queue_entry *pentry;
515 pentry = TAILQ_NEXT(entry, entry);
516 if (pentry == NULL) {
517 err = fetch_parent_commit(&pentry, entry, repo);
518 if (err || pentry == NULL)
520 TAILQ_INSERT_TAIL(commits, pentry, entry);
521 last_displayed_entry = pentry;
524 *first_displayed_entry = pentry;
527 if (TAILQ_LAST(commits, commit_queue) == last_displayed_entry) {
528 err = fetch_parent_commit(&pentry, last_displayed_entry,
533 TAILQ_INSERT_TAIL(commits, pentry, entry);
534 last_displayed_entry = pentry;
537 } while (++nscrolled < maxscroll);
543 num_parents(struct commit_queue_entry *entry)
548 entry = TAILQ_NEXT(entry, entry);
555 static const struct got_error *
556 show_commit(struct commit_queue_entry *entry, struct got_repository *repo)
558 const struct got_error *err;
559 struct got_object *obj1 = NULL, *obj2 = NULL;
561 err = got_object_open(&obj2, repo, entry->id);
565 entry = TAILQ_NEXT(entry, entry);
567 err = got_object_open(&obj1, repo, entry->id);
572 err = show_diff_view(obj1, obj2, repo);
575 got_object_close(obj1);
577 got_object_close(obj2);
581 static const struct got_error *
582 show_log_view(struct got_object_id *start_id, struct got_repository *repo)
584 const struct got_error *err = NULL;
585 struct got_object_id *id;
586 int ch, done = 0, selected = 0, nparents;
587 struct commit_queue commits;
588 struct commit_queue_entry *first_displayed_entry = NULL;
589 struct commit_queue_entry *last_displayed_entry = NULL;
590 struct commit_queue_entry *selected_entry = NULL;
592 id = got_object_id_dup(start_id);
594 return got_error_from_errno();
596 if (tog_log_view.window == NULL) {
597 tog_log_view.window = newwin(0, 0, 0, 0);
598 if (tog_log_view.window == NULL)
599 return got_error_from_errno();
600 keypad(tog_log_view.window, TRUE);
602 if (tog_log_view.panel == NULL) {
603 tog_log_view.panel = new_panel(tog_log_view.window);
604 if (tog_log_view.panel == NULL)
605 return got_error_from_errno();
607 show_panel(tog_log_view.panel);
609 TAILQ_INIT(&commits);
610 err = fetch_commits(&first_displayed_entry, id, &commits, LINES, repo);
614 err = draw_commits(&last_displayed_entry, &selected_entry,
615 first_displayed_entry, selected, LINES);
619 nodelay(stdscr, FALSE);
620 ch = wgetch(tog_log_view.window);
621 nodelay(stdscr, TRUE);
625 err = got_error_from_errno();
638 scroll_up(&first_displayed_entry, 1, &commits);
641 if (TAILQ_FIRST(&commits) ==
642 first_displayed_entry) {
646 scroll_up(&first_displayed_entry, LINES,
651 nparents = num_parents(first_displayed_entry);
652 if (selected < LINES - 1 &&
653 selected < nparents - 1)
655 if (selected < LINES - 1 &&
656 selected < nparents - 1)
658 err = scroll_down(&first_displayed_entry, 1,
659 last_displayed_entry, &commits, repo);
664 nparents = num_parents(first_displayed_entry);
665 if (nparents < LINES - 1 &&
666 selected < nparents - 1) {
667 selected = nparents - 1;
670 err = scroll_down(&first_displayed_entry, LINES,
671 last_displayed_entry, &commits, repo);
674 nparents = num_parents(first_displayed_entry);
675 if (selected > nparents)
676 selected = nparents - 1;
679 if (selected > LINES)
680 selected = LINES - 1;
684 err = show_commit(selected_entry, repo);
687 show_panel(tog_log_view.panel);
694 free_commits(&commits);
698 static const struct got_error *
699 cmd_log(int argc, char *argv[])
701 const struct got_error *error;
702 struct got_repository *repo;
703 struct got_object_id *start_id = NULL;
704 char *repo_path = NULL;
705 char *start_commit = NULL;
709 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
713 while ((ch = getopt(argc, argv, "c:")) != -1) {
716 start_commit = optarg;
728 repo_path = getcwd(NULL, 0);
729 if (repo_path == NULL)
730 return got_error_from_errno();
731 } else if (argc == 1) {
732 repo_path = realpath(argv[0], NULL);
733 if (repo_path == NULL)
734 return got_error_from_errno();
738 error = got_repo_open(&repo, repo_path);
743 if (start_commit == NULL) {
744 error = get_head_commit_id(&start_id, repo);
748 struct got_object *obj;
749 error = got_object_open_by_id_str(&obj, repo, start_commit);
751 start_id = got_object_get_id(obj);
752 if (start_id == NULL)
753 error = got_error_from_errno();
758 error = show_log_view(start_id, repo);
760 got_repo_close(repo);
768 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
774 parse_next_line(FILE *f, size_t *len)
779 const char delim[3] = { '\0', '\0', '\0'};
781 line = fparseln(f, &linelen, &lineno, delim, 0);
787 static const struct got_error *
788 draw_diff(FILE *f, int *first_displayed_line, int *last_displayed_line,
789 int *eof, int max_lines)
791 int nlines = 0, nprinted = 0;
796 wclear(tog_diff_view.window);
799 while (nprinted < max_lines) {
800 line = parse_next_line(f, &len);
805 if (++nlines < *first_displayed_line) {
811 line[COLS - 1] = '\0';
812 waddstr(tog_diff_view.window, line);
813 waddch(tog_diff_view.window, '\n');
815 *first_displayed_line = nlines;
818 *last_displayed_line = nlines;
826 static const struct got_error *
827 show_diff_view(struct got_object *obj1, struct got_object *obj2,
828 struct got_repository *repo)
830 const struct got_error *err;
832 int ch, done = 0, first_displayed_line = 1, last_displayed_line = LINES;
835 if (obj1 != NULL && obj2 != NULL &&
836 got_object_get_type(obj1) != got_object_get_type(obj2))
837 return got_error(GOT_ERR_OBJ_TYPE);
841 return got_error_from_errno();
843 switch (got_object_get_type(obj1 ? obj1 : obj2)) {
844 case GOT_OBJ_TYPE_BLOB:
845 err = got_diff_objects_as_blobs(obj1, obj2, repo, f);
847 case GOT_OBJ_TYPE_TREE:
848 err = got_diff_objects_as_trees(obj1, obj2, repo, f);
850 case GOT_OBJ_TYPE_COMMIT:
851 err = got_diff_objects_as_commits(obj1, obj2, repo, f);
854 return got_error(GOT_ERR_OBJ_TYPE);
859 if (tog_diff_view.window == NULL) {
860 tog_diff_view.window = newwin(0, 0, 0, 0);
861 if (tog_diff_view.window == NULL)
862 return got_error_from_errno();
863 keypad(tog_diff_view.window, TRUE);
865 if (tog_diff_view.panel == NULL) {
866 tog_diff_view.panel = new_panel(tog_diff_view.window);
867 if (tog_diff_view.panel == NULL)
868 return got_error_from_errno();
870 show_panel(tog_diff_view.panel);
873 err = draw_diff(f, &first_displayed_line, &last_displayed_line,
877 nodelay(stdscr, FALSE);
878 ch = wgetch(tog_diff_view.window);
879 nodelay(stdscr, TRUE);
887 if (first_displayed_line > 1)
888 first_displayed_line--;
892 while (i++ < LINES - 1 && first_displayed_line > 1)
893 first_displayed_line--;
900 first_displayed_line++;
905 while (!eof && i++ < LINES - 1) {
906 char *line = parse_next_line(f, NULL);
907 first_displayed_line++;
920 static const struct got_error *
921 cmd_diff(int argc, char *argv[])
923 const struct got_error *error = NULL;
924 struct got_repository *repo = NULL;
925 struct got_object *obj1 = NULL, *obj2 = NULL;
926 char *repo_path = NULL;
927 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
931 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
935 while ((ch = getopt(argc, argv, "")) != -1) {
947 usage_diff(); /* TODO show local worktree changes */
948 } else if (argc == 2) {
949 repo_path = getcwd(NULL, 0);
950 if (repo_path == NULL)
951 return got_error_from_errno();
952 obj_id_str1 = argv[0];
953 obj_id_str2 = argv[1];
954 } else if (argc == 3) {
955 repo_path = realpath(argv[0], NULL);
956 if (repo_path == NULL)
957 return got_error_from_errno();
958 obj_id_str1 = argv[1];
959 obj_id_str2 = argv[2];
963 error = got_repo_open(&repo, repo_path);
968 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
972 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
976 error = show_diff_view(obj1, obj2, repo);
978 got_repo_close(repo);
980 got_object_close(obj1);
982 got_object_close(obj2);
990 fprintf(stderr, "usage: %s blame [repository-path] blob-object\n",
995 static const struct got_error *
996 cmd_blame(int argc, char *argv[])
998 return got_error(GOT_ERR_NOT_IMPL);
1008 intrflush(stdscr, FALSE);
1009 keypad(stdscr, TRUE);
1018 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
1019 "Available commands:\n", getprogname());
1020 for (i = 0; i < nitems(tog_commands); i++) {
1021 struct tog_cmd *cmd = &tog_commands[i];
1022 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
1028 make_argv(const char *arg0, const char *arg1)
1031 int argc = (arg1 == NULL ? 1 : 2);
1033 argv = calloc(argc, sizeof(char *));
1036 argv[0] = strdup(arg0);
1037 if (argv[0] == NULL)
1040 argv[1] = strdup(arg1);
1041 if (argv[1] == NULL)
1049 main(int argc, char *argv[])
1051 const struct got_error *error = NULL;
1052 struct tog_cmd *cmd = NULL;
1054 char **cmd_argv = NULL;
1056 setlocale(LC_ALL, "");
1058 while ((ch = getopt(argc, argv, "h")) != -1) {
1075 /* Build an argument vector which runs a default command. */
1076 cmd = &tog_commands[0];
1077 cmd_argv = make_argv(cmd->name, NULL);
1082 /* Did the user specific a command? */
1083 for (i = 0; i < nitems(tog_commands); i++) {
1084 if (strncmp(tog_commands[i].name, argv[0],
1085 strlen(argv[0])) == 0) {
1086 cmd = &tog_commands[i];
1088 tog_commands[i].cmd_usage();
1093 /* Did the user specify a repository? */
1094 char *repo_path = realpath(argv[0], NULL);
1096 struct got_repository *repo;
1097 error = got_repo_open(&repo, repo_path);
1099 got_repo_close(repo);
1101 error = got_error_from_errno();
1103 fprintf(stderr, "%s: '%s' is neither a known "
1104 "command nor a path to a repository\n",
1105 getprogname(), argv[0]);
1109 cmd = &tog_commands[0];
1110 cmd_argv = make_argv(cmd->name, repo_path);
1118 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
1125 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);