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]))
55 const struct got_error *(*cmd_main)(int, char *[]);
56 void (*cmd_usage)(void);
57 enum tog_view_id view;
61 __dead void usage(void);
62 __dead void usage_log(void);
63 __dead void usage_diff(void);
64 __dead void usage_blame(void);
66 const struct got_error* cmd_log(int, char *[]);
67 const struct got_error* cmd_diff(int, char *[]);
68 const struct got_error* cmd_blame(int, char *[]);
70 struct tog_cmd tog_commands[] = {
71 { "log", cmd_log, usage_log, TOG_VIEW_LOG,
72 "show repository history" },
73 { "diff", cmd_diff, usage_diff, TOG_VIEW_DIFF,
74 "compare files and directories" },
75 { "blame", cmd_blame, usage_blame, TOG_VIEW_BLAME,
76 "show line-by-line file history" },
80 static struct tog_log_view {
84 static struct tog_diff_view {
93 fprintf(stderr, "usage: %s log [-c commit] [repository-path]\n",
98 static const struct got_error *
99 draw_commit(struct got_commit_object *commit, struct got_object_id *id)
101 const struct got_error *err = NULL;
102 char *logmsg0 = NULL, *logmsg = NULL;
103 char *author0 = NULL, *author = NULL;
104 char *newline, *smallerthan;
107 const size_t id_display_len = 8;
108 const size_t author_display_len = 16;
109 size_t id_len, author_len, logmsg_len, avail;
112 err = got_object_id_str(&id_str, id);
115 id_len = strlen(id_str);
117 logmsg0 = strdup(commit->logmsg);
118 if (logmsg0 == NULL) {
119 err = got_error_from_errno();
123 while (*logmsg == '\n')
125 newline = strchr(logmsg, '\n');
128 logmsg_len = strlen(logmsg);
130 author0 = strdup(commit->author);
131 if (author0 == NULL) {
132 err = got_error_from_errno();
136 smallerthan = strchr(author, '<');
140 char *at = strchr(author, '@');
144 author_len = strlen(author);
147 line = calloc(avail + 1, sizeof(*line));
149 err = got_error_from_errno();
154 for (i = 0; i < MIN(id_display_len, id_len); i++) {
157 line[col++] = id_str[i];
159 while (i < id_display_len) {
168 for (i = 0; i < MIN(author_display_len, author_len); i++) {
171 line[col++] = author[i];
173 while (i < author_display_len) {
183 while (col < avail && *logmsg)
184 line[col++] = *logmsg++;
188 waddstr(tog_log_view.window, line);
189 waddch(tog_log_view.window, '\n');
198 struct commit_queue_entry {
199 TAILQ_ENTRY(commit_queue_entry) entry;
200 struct got_object_id *id;
201 struct got_commit_object *commit;
203 TAILQ_HEAD(commit_queue, commit_queue_entry);
205 static struct commit_queue_entry *
206 alloc_commit_queue_entry(struct got_commit_object *commit,
207 struct got_object_id *id)
209 struct commit_queue_entry *entry;
211 entry = calloc(1, sizeof(*entry));
216 entry->commit = commit;
221 pop_commit(struct commit_queue *commits)
223 struct commit_queue_entry *entry;
225 entry = TAILQ_FIRST(commits);
226 TAILQ_REMOVE(commits, entry, entry);
227 got_object_commit_close(entry->commit);
233 free_commits(struct commit_queue *commits)
235 while (!TAILQ_EMPTY(commits))
240 static const struct got_error *
241 fetch_parent_commit(struct commit_queue_entry **pentry,
242 struct commit_queue_entry *entry, struct got_repository *repo)
244 const struct got_error *err = NULL;
245 struct got_object *obj = NULL;
246 struct got_commit_object *commit;
247 struct got_object_id *id;
248 struct got_parent_id *pid;
252 /* Follow the first parent (TODO: handle merge commits). */
253 pid = SIMPLEQ_FIRST(&entry->commit->parent_ids);
256 err = got_object_open(&obj, repo, pid->id);
259 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
260 err = got_error(GOT_ERR_OBJ_TYPE);
261 got_object_close(obj);
265 err = got_object_commit_open(&commit, repo, obj);
266 got_object_close(obj);
270 id = got_object_id_dup(pid->id);
272 err = got_error_from_errno();
273 got_object_commit_close(commit);
277 *pentry = alloc_commit_queue_entry(commit, id);
278 if (*pentry == NULL) {
279 err = got_error_from_errno();
280 got_object_commit_close(commit);
286 static const struct got_error *
287 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
289 const struct got_error *err = NULL;
290 struct got_reference *head_ref;
294 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
298 err = got_ref_resolve(head_id, repo, head_ref);
299 got_ref_close(head_ref);
308 static const struct got_error *
309 prepend_commits(int *ncommits, struct commit_queue *commits,
310 struct got_object_id *first_id, struct got_object_id *last_id,
311 int limit, struct got_repository *repo)
313 const struct got_error *err = NULL;
314 struct got_object *first_obj = NULL, *last_obj = NULL;
315 struct got_commit_object *commit = NULL;
316 struct got_object_id *id = NULL;
317 struct commit_queue_entry *entry, *old_head_entry;
321 err = got_object_open(&first_obj, repo, first_id);
324 if (got_object_get_type(first_obj) != GOT_OBJ_TYPE_COMMIT) {
325 err = got_error(GOT_ERR_OBJ_TYPE);
328 err = got_object_open(&last_obj, repo, last_id);
331 if (got_object_get_type(last_obj) != GOT_OBJ_TYPE_COMMIT) {
332 err = got_error(GOT_ERR_OBJ_TYPE);
336 err = got_object_commit_open(&commit, repo, first_obj);
340 id = got_object_id_dup(first_id);
342 err = got_error_from_errno();
346 entry = alloc_commit_queue_entry(commit, id);
348 return got_error_from_errno();
350 old_head_entry = TAILQ_FIRST(commits);
352 TAILQ_INSERT_BEFORE(old_head_entry, entry, entry);
354 TAILQ_INSERT_HEAD(commits, entry, entry);
359 * Fetch parent commits.
360 * XXX If first and last commit aren't ancestrally related this loop
361 * we will keep iterating until a root commit is encountered.
364 struct commit_queue_entry *pentry;
366 err = fetch_parent_commit(&pentry, entry, repo);
373 * Fill up to old HEAD commit if commit queue was not empty.
374 * We must not leave a gap in history.
376 if (old_head_entry &&
377 got_object_id_cmp(pentry->id, old_head_entry->id) == 0)
380 TAILQ_INSERT_AFTER(commits, entry, pentry, entry);
382 if (*ncommits >= limit)
385 /* Fill up to last requested commit if queue was empty. */
386 if (old_head_entry == NULL &&
387 got_object_id_cmp(pentry->id, last_id) == 0)
395 got_object_close(first_obj);
397 got_object_close(last_obj);
401 static const struct got_error *
402 fetch_commits(struct commit_queue_entry **start_entry,
403 struct got_object_id *start_id, struct commit_queue *commits,
404 int limit, struct got_repository *repo)
406 const struct got_error *err;
407 struct commit_queue_entry *entry;
409 struct got_object_id *head_id = NULL;
413 err = get_head_commit_id(&head_id, repo);
417 /* Prepend HEAD commit and all ancestors up to start commit. */
418 err = prepend_commits(&ncommits, commits, head_id, start_id, limit,
423 if (got_object_id_cmp(head_id, start_id) == 0)
424 *start_entry = TAILQ_FIRST(commits);
426 *start_entry = TAILQ_LAST(commits, commit_queue);
428 if (ncommits >= limit)
431 /* Append more commits from start commit up to the requested limit. */
432 entry = TAILQ_LAST(commits, commit_queue);
433 while (entry && ncommits < limit) {
434 struct commit_queue_entry *pentry;
436 err = fetch_parent_commit(&pentry, entry, repo);
440 TAILQ_INSERT_TAIL(commits, pentry, entry);
450 static const struct got_error *
451 draw_commits(struct commit_queue_entry **last, struct commit_queue_entry *first,
452 int selected, int limit)
454 const struct got_error *err = NULL;
455 struct commit_queue_entry *entry;
458 wclear(tog_log_view.window);
463 if (ncommits == limit)
465 if (ncommits == selected)
466 wstandout(tog_log_view.window);
467 err = draw_commit(entry->commit, entry->id);
468 if (ncommits == selected)
469 wstandend(tog_log_view.window);
474 entry = TAILQ_NEXT(entry, entry);
484 scroll_up(struct commit_queue_entry **first_displayed_entry, int n,
485 struct commit_queue *commits)
487 struct commit_queue_entry *entry;
490 entry = TAILQ_FIRST(commits);
491 if (*first_displayed_entry == entry)
494 entry = *first_displayed_entry;
495 while (entry && nscrolled < n) {
496 entry = TAILQ_PREV(entry, commit_queue, entry);
498 *first_displayed_entry = entry;
504 static const struct got_error *
505 scroll_down(struct commit_queue_entry **first_displayed_entry, int n,
506 struct commit_queue_entry *last_displayed_entry,
507 struct commit_queue *commits, struct got_repository *repo)
509 const struct got_error *err = NULL;
510 struct commit_queue_entry *entry;
513 if (last_displayed_entry->commit->nparents == 0)
516 entry = *first_displayed_entry;
518 struct commit_queue_entry *pentry;
520 pentry = TAILQ_NEXT(entry, entry);
521 if (pentry == NULL) {
522 err = fetch_parent_commit(&pentry, entry, repo);
525 if (pentry == NULL) {
526 *first_displayed_entry = entry;
529 TAILQ_INSERT_TAIL(commits, pentry, entry);
530 last_displayed_entry = pentry;
533 *first_displayed_entry = pentry;
536 if (TAILQ_LAST(commits, commit_queue) == last_displayed_entry) {
537 err = fetch_parent_commit(&pentry, last_displayed_entry,
542 TAILQ_INSERT_TAIL(commits, pentry, entry);
543 last_displayed_entry = pentry;
546 } while (++nscrolled < n);
552 num_parents(struct commit_queue_entry *entry)
557 entry = TAILQ_NEXT(entry, entry);
564 static const struct got_error *
565 show_log_view(struct got_object_id *start_id, struct got_repository *repo)
567 const struct got_error *err = NULL;
568 struct got_object_id *id;
569 int ch, done = 0, selected = 0, nparents;
570 struct commit_queue commits;
571 struct commit_queue_entry *first_displayed_entry = NULL;
572 struct commit_queue_entry *last_displayed_entry = NULL;
574 id = got_object_id_dup(start_id);
576 return got_error_from_errno();
578 if (tog_log_view.window == NULL) {
579 tog_log_view.window = newwin(0, 0, 0, 0);
580 if (tog_log_view.window == NULL)
581 return got_error_from_errno();
582 keypad(tog_log_view.window, TRUE);
584 if (tog_log_view.panel == NULL) {
585 tog_log_view.panel = new_panel(tog_log_view.window);
586 if (tog_log_view.panel == NULL)
587 return got_error_from_errno();
590 TAILQ_INIT(&commits);
591 err = fetch_commits(&first_displayed_entry, id, &commits, LINES, repo);
595 err = draw_commits(&last_displayed_entry, first_displayed_entry,
600 nodelay(stdscr, FALSE);
601 ch = wgetch(tog_log_view.window);
605 err = got_error_from_errno();
618 scroll_up(&first_displayed_entry, 1, &commits);
621 if (TAILQ_FIRST(&commits) ==
622 first_displayed_entry) {
626 scroll_up(&first_displayed_entry, LINES,
631 nparents = num_parents(first_displayed_entry);
632 if (selected < LINES - 1 &&
633 selected < nparents - 1)
635 if (selected < LINES - 1 &&
636 selected < nparents - 1)
638 err = scroll_down(&first_displayed_entry, 1,
639 last_displayed_entry, &commits, repo);
644 nparents = num_parents(first_displayed_entry);
645 if (nparents < LINES - 1 &&
646 selected < nparents - 1) {
647 selected = nparents - 1;
650 err = scroll_down(&first_displayed_entry, LINES,
651 last_displayed_entry, &commits, repo);
654 nparents = num_parents(first_displayed_entry);
655 if (selected > nparents)
656 selected = nparents - 1;
659 if (selected > LINES)
660 selected = LINES - 1;
665 nodelay(stdscr, TRUE);
668 free_commits(&commits);
672 const struct got_error *
673 cmd_log(int argc, char *argv[])
675 const struct got_error *error;
676 struct got_repository *repo;
677 struct got_object_id *start_id = NULL;
678 char *repo_path = NULL;
679 char *start_commit = NULL;
683 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
687 while ((ch = getopt(argc, argv, "c:")) != -1) {
690 start_commit = optarg;
702 repo_path = getcwd(NULL, 0);
703 if (repo_path == NULL)
704 return got_error_from_errno();
705 } else if (argc == 1) {
706 repo_path = realpath(argv[0], NULL);
707 if (repo_path == NULL)
708 return got_error_from_errno();
712 error = got_repo_open(&repo, repo_path);
717 if (start_commit == NULL) {
718 error = get_head_commit_id(&start_id, repo);
722 struct got_object *obj;
723 error = got_object_open_by_id_str(&obj, repo, start_commit);
725 start_id = got_object_get_id(obj);
726 if (start_id == NULL)
727 error = got_error_from_errno();
732 error = show_log_view(start_id, repo);
734 got_repo_close(repo);
742 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
747 const struct got_error *
748 draw_diff(FILE *f, int *first_displayed_line, int *last_displayed_line,
749 int *eof, int max_lines)
751 int nlines = 0, nprinted = 0;
754 wclear(tog_diff_view.window);
757 while (nprinted < max_lines) {
761 const char delim[3] = { '\0', '\0', '\0'};
763 line = fparseln(f, &linelen, &lineno, delim, 0);
768 if (++nlines < *first_displayed_line) {
773 if (linelen > COLS - 1)
774 line[COLS - 1] = '\0';
775 waddstr(tog_diff_view.window, line);
776 waddch(tog_diff_view.window, '\n');
778 *first_displayed_line = nlines;
781 *last_displayed_line = nlines;
789 const struct got_error *
790 show_diff_view(struct got_object *obj1, struct got_object *obj2,
791 struct got_repository *repo)
793 const struct got_error *err;
795 int ch, done = 0, first_displayed_line = 1, last_displayed_line = LINES;
798 if (got_object_get_type(obj1) != got_object_get_type(obj2))
799 return got_error(GOT_ERR_OBJ_TYPE);
803 return got_error_from_errno();
805 switch (got_object_get_type(obj1)) {
806 case GOT_OBJ_TYPE_BLOB:
807 err = got_diff_objects_as_blobs(obj1, obj2, repo, f);
809 case GOT_OBJ_TYPE_TREE:
810 err = got_diff_objects_as_trees(obj1, obj2, repo, f);
812 case GOT_OBJ_TYPE_COMMIT:
813 err = got_diff_objects_as_commits(obj1, obj2, repo, f);
816 return got_error(GOT_ERR_OBJ_TYPE);
821 if (tog_diff_view.window == NULL) {
822 tog_diff_view.window = newwin(0, 0, 0, 0);
823 if (tog_diff_view.window == NULL)
824 return got_error_from_errno();
825 keypad(tog_diff_view.window, TRUE);
827 if (tog_diff_view.panel == NULL) {
828 tog_diff_view.panel = new_panel(tog_diff_view.window);
829 if (tog_diff_view.panel == NULL)
830 return got_error_from_errno();
832 show_panel(tog_diff_view.panel);
835 err = draw_diff(f, &first_displayed_line, &last_displayed_line,
839 nodelay(stdscr, FALSE);
840 ch = wgetch(tog_diff_view.window);
847 if (first_displayed_line > 1)
848 first_displayed_line--;
853 first_displayed_line++;
858 nodelay(stdscr, TRUE);
864 const struct got_error *
865 cmd_diff(int argc, char *argv[])
867 const struct got_error *error = NULL;
868 struct got_repository *repo = NULL;
869 struct got_object *obj1 = NULL, *obj2 = NULL;
870 char *repo_path = NULL;
871 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
875 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
879 while ((ch = getopt(argc, argv, "")) != -1) {
891 usage_diff(); /* TODO show local worktree changes */
892 } else if (argc == 2) {
893 repo_path = getcwd(NULL, 0);
894 if (repo_path == NULL)
895 return got_error_from_errno();
896 obj_id_str1 = argv[0];
897 obj_id_str2 = argv[1];
898 } else if (argc == 3) {
899 repo_path = realpath(argv[0], NULL);
900 if (repo_path == NULL)
901 return got_error_from_errno();
902 obj_id_str1 = argv[1];
903 obj_id_str2 = argv[2];
907 error = got_repo_open(&repo, repo_path);
912 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
916 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
920 error = show_diff_view(obj1, obj2, repo);
922 got_repo_close(repo);
924 got_object_close(obj1);
926 got_object_close(obj2);
934 fprintf(stderr, "usage: %s blame [repository-path] blob-object\n",
939 const struct got_error *
940 cmd_blame(int argc, char *argv[])
942 return got_error(GOT_ERR_NOT_IMPL);
952 intrflush(stdscr, FALSE);
953 keypad(stdscr, TRUE);
962 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
963 "Available commands:\n", getprogname());
964 for (i = 0; i < nitems(tog_commands); i++) {
965 struct tog_cmd *cmd = &tog_commands[i];
966 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
972 make_argv(const char *arg0, const char *arg1)
975 int argc = (arg1 == NULL ? 1 : 2);
977 argv = calloc(argc, sizeof(char *));
980 argv[0] = strdup(arg0);
984 argv[1] = strdup(arg1);
993 main(int argc, char *argv[])
995 const struct got_error *error = NULL;
996 struct tog_cmd *cmd = NULL;
998 char **cmd_argv = NULL;
1000 setlocale(LC_ALL, "");
1002 while ((ch = getopt(argc, argv, "h")) != -1) {
1019 /* Build an argument vector which runs a default command. */
1020 cmd = &tog_commands[0];
1021 cmd_argv = make_argv(cmd->name, NULL);
1026 /* Did the user specific a command? */
1027 for (i = 0; i < nitems(tog_commands); i++) {
1028 if (strncmp(tog_commands[i].name, argv[0],
1029 strlen(argv[0])) == 0) {
1030 cmd = &tog_commands[i];
1032 tog_commands[i].cmd_usage();
1037 /* Did the user specify a repository? */
1038 char *repo_path = realpath(argv[0], NULL);
1040 struct got_repository *repo;
1041 error = got_repo_open(&repo, repo_path);
1043 got_repo_close(repo);
1045 error = got_error_from_errno();
1047 fprintf(stderr, "%s: '%s' is neither a known "
1048 "command nor a path to a repository\n",
1049 getprogname(), argv[0]);
1053 cmd = &tog_commands[0];
1054 cmd_argv = make_argv(cmd->name, repo_path);
1062 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
1069 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);