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"
39 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
43 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
54 const struct got_error *(*cmd_main)(int, char *[]);
55 void (*cmd_usage)(void);
56 enum tog_view_id view;
60 __dead void usage(void);
61 __dead void usage_log(void);
62 __dead void usage_diff(void);
63 __dead void usage_blame(void);
65 const struct got_error* cmd_log(int, char *[]);
66 const struct got_error* cmd_diff(int, char *[]);
67 const struct got_error* cmd_blame(int, char *[]);
69 struct tog_cmd tog_commands[] = {
70 { "log", cmd_log, usage_log, TOG_VIEW_LOG,
71 "show repository history" },
72 { "diff", cmd_diff, usage_diff, TOG_VIEW_DIFF,
73 "compare files and directories" },
74 { "blame", cmd_blame, usage_blame, TOG_VIEW_BLAME,
75 "show line-by-line file history" },
80 PANEL *tog_main_panel;
81 static struct tog_log_view {
85 static struct tog_diff_view {
96 if (strlcpy(name, "/tmp/tog.XXXXXXXX", sizeof(name)) >= sizeof(name))
110 fd = tog_opentempfd();
114 f = fdopen(fd, "w+");
127 fprintf(stderr, "usage: %s log [-c commit] [repository-path]\n",
132 static const struct got_error *
133 draw_commit(struct got_commit_object *commit, struct got_object_id *id)
135 const struct got_error *err = NULL;
136 char *logmsg0 = NULL, *logmsg = NULL;
137 char *author0 = NULL, *author = NULL;
138 char *newline, *smallerthan;
141 const size_t id_display_len = 8;
142 const size_t author_display_len = 16;
143 size_t id_len, author_len, logmsg_len, avail;
146 err = got_object_id_str(&id_str, id);
149 id_len = strlen(id_str);
151 logmsg0 = strdup(commit->logmsg);
152 if (logmsg0 == NULL) {
153 err = got_error_from_errno();
157 while (*logmsg == '\n')
159 newline = strchr(logmsg, '\n');
162 logmsg_len = strlen(logmsg);
164 author0 = strdup(commit->author);
165 if (author0 == NULL) {
166 err = got_error_from_errno();
170 smallerthan = strchr(author, '<');
174 char *at = strchr(author, '@');
178 author_len = strlen(author);
181 line = calloc(avail + 1, sizeof(*line));
183 err = got_error_from_errno();
188 for (i = 0; i < MIN(id_display_len, id_len); i++) {
191 line[col++] = id_str[i];
193 while (i < id_display_len) {
202 for (i = 0; i < MIN(author_display_len, author_len); i++) {
205 line[col++] = author[i];
207 while (i < author_display_len) {
217 while (col < avail && *logmsg)
218 line[col++] = *logmsg++;
222 waddstr(tog_log_view.window, line);
223 waddch(tog_log_view.window, '\n');
232 struct commit_queue_entry {
233 TAILQ_ENTRY(commit_queue_entry) entry;
234 struct got_object_id *id;
235 struct got_commit_object *commit;
237 TAILQ_HEAD(commit_queue, commit_queue_entry);
239 static struct commit_queue_entry *
240 alloc_commit_queue_entry(struct got_commit_object *commit,
241 struct got_object_id *id)
243 struct commit_queue_entry *entry;
245 entry = calloc(1, sizeof(*entry));
250 entry->commit = commit;
255 pop_commit(struct commit_queue *commits)
257 struct commit_queue_entry *entry;
259 entry = TAILQ_FIRST(commits);
260 TAILQ_REMOVE(commits, entry, entry);
261 got_object_commit_close(entry->commit);
267 free_commits(struct commit_queue *commits)
269 while (!TAILQ_EMPTY(commits))
274 static const struct got_error *
275 fetch_parent_commit(struct commit_queue_entry **pentry,
276 struct commit_queue_entry *entry, struct got_repository *repo)
278 const struct got_error *err = NULL;
279 struct got_object *obj = NULL;
280 struct got_commit_object *commit;
281 struct got_object_id *id;
282 struct got_parent_id *pid;
286 /* Follow the first parent (TODO: handle merge commits). */
287 pid = SIMPLEQ_FIRST(&entry->commit->parent_ids);
290 err = got_object_open(&obj, repo, pid->id);
293 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
294 err = got_error(GOT_ERR_OBJ_TYPE);
295 got_object_close(obj);
299 err = got_object_commit_open(&commit, repo, obj);
300 got_object_close(obj);
304 id = got_object_id_dup(pid->id);
306 err = got_error_from_errno();
307 got_object_commit_close(commit);
311 *pentry = alloc_commit_queue_entry(commit, id);
312 if (*pentry == NULL) {
313 err = got_error_from_errno();
314 got_object_commit_close(commit);
320 static const struct got_error *
321 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
323 const struct got_error *err = NULL;
324 struct got_reference *head_ref;
328 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
332 err = got_ref_resolve(head_id, repo, head_ref);
333 got_ref_close(head_ref);
342 static const struct got_error *
343 prepend_commits(int *ncommits, struct commit_queue *commits,
344 struct got_object_id *first_id, struct got_object_id *last_id,
345 int limit, struct got_repository *repo)
347 const struct got_error *err = NULL;
348 struct got_object *first_obj = NULL, *last_obj = NULL;
349 struct got_commit_object *commit = NULL;
350 struct got_object_id *id = NULL;
351 struct commit_queue_entry *entry, *old_head_entry;
355 err = got_object_open(&first_obj, repo, first_id);
358 if (got_object_get_type(first_obj) != GOT_OBJ_TYPE_COMMIT) {
359 err = got_error(GOT_ERR_OBJ_TYPE);
362 err = got_object_open(&last_obj, repo, last_id);
365 if (got_object_get_type(last_obj) != GOT_OBJ_TYPE_COMMIT) {
366 err = got_error(GOT_ERR_OBJ_TYPE);
370 err = got_object_commit_open(&commit, repo, first_obj);
374 id = got_object_id_dup(first_id);
376 err = got_error_from_errno();
380 entry = alloc_commit_queue_entry(commit, id);
382 return got_error_from_errno();
384 old_head_entry = TAILQ_FIRST(commits);
386 TAILQ_INSERT_BEFORE(old_head_entry, entry, entry);
388 TAILQ_INSERT_HEAD(commits, entry, entry);
393 * Fetch parent commits.
394 * XXX If first and last commit aren't ancestrally related this loop
395 * we will keep iterating until a root commit is encountered.
398 struct commit_queue_entry *pentry;
400 err = fetch_parent_commit(&pentry, entry, repo);
407 * Fill up to old HEAD commit if commit queue was not empty.
408 * We must not leave a gap in history.
410 if (old_head_entry &&
411 got_object_id_cmp(pentry->id, old_head_entry->id) == 0)
414 TAILQ_INSERT_AFTER(commits, entry, pentry, entry);
416 if (*ncommits >= limit)
419 /* Fill up to last requested commit if queue was empty. */
420 if (old_head_entry == NULL &&
421 got_object_id_cmp(pentry->id, last_id) == 0)
429 got_object_close(first_obj);
431 got_object_close(last_obj);
435 static const struct got_error *
436 fetch_commits(struct commit_queue_entry **start_entry,
437 struct got_object_id *start_id, struct commit_queue *commits,
438 int limit, struct got_repository *repo)
440 const struct got_error *err;
441 struct commit_queue_entry *entry;
443 struct got_object_id *head_id = NULL;
447 err = get_head_commit_id(&head_id, repo);
451 /* Prepend HEAD commit and all ancestors up to start commit. */
452 err = prepend_commits(&ncommits, commits, head_id, start_id, limit,
457 if (got_object_id_cmp(head_id, start_id) == 0)
458 *start_entry = TAILQ_FIRST(commits);
460 *start_entry = TAILQ_LAST(commits, commit_queue);
462 if (ncommits >= limit)
465 /* Append more commits from start commit up to the requested limit. */
466 entry = TAILQ_LAST(commits, commit_queue);
467 while (entry && ncommits < limit) {
468 struct commit_queue_entry *pentry;
470 err = fetch_parent_commit(&pentry, entry, repo);
474 TAILQ_INSERT_TAIL(commits, pentry, entry);
484 static const struct got_error *
485 draw_commits(struct commit_queue_entry **last, struct commit_queue_entry *first,
486 int selected, int limit)
488 const struct got_error *err = NULL;
489 struct commit_queue_entry *entry;
492 wclear(tog_log_view.window);
497 if (ncommits == limit)
499 if (ncommits == selected)
500 wstandout(tog_log_view.window);
501 err = draw_commit(entry->commit, entry->id);
502 if (ncommits == selected)
503 wstandend(tog_log_view.window);
508 entry = TAILQ_NEXT(entry, entry);
518 scroll_up(struct commit_queue_entry **first_displayed_entry, int n,
519 struct commit_queue *commits)
521 struct commit_queue_entry *entry;
524 entry = TAILQ_FIRST(commits);
525 if (*first_displayed_entry == entry)
528 entry = *first_displayed_entry;
529 while (entry && nscrolled < n) {
530 entry = TAILQ_PREV(entry, commit_queue, entry);
532 *first_displayed_entry = entry;
538 static const struct got_error *
539 scroll_down(struct commit_queue_entry **first_displayed_entry, int n,
540 struct commit_queue_entry *last_displayed_entry,
541 struct commit_queue *commits, struct got_repository *repo)
543 const struct got_error *err = NULL;
544 struct commit_queue_entry *entry;
547 if (last_displayed_entry->commit->nparents == 0)
550 entry = *first_displayed_entry;
552 struct commit_queue_entry *pentry;
554 pentry = TAILQ_NEXT(entry, entry);
555 if (pentry == NULL) {
556 err = fetch_parent_commit(&pentry, entry, repo);
559 if (pentry == NULL) {
560 *first_displayed_entry = entry;
563 TAILQ_INSERT_TAIL(commits, pentry, entry);
564 last_displayed_entry = pentry;
567 *first_displayed_entry = pentry;
570 if (TAILQ_LAST(commits, commit_queue) == last_displayed_entry) {
571 err = fetch_parent_commit(&pentry, last_displayed_entry,
576 TAILQ_INSERT_TAIL(commits, pentry, entry);
577 last_displayed_entry = pentry;
580 } while (++nscrolled < n);
586 num_parents(struct commit_queue_entry *entry)
591 entry = TAILQ_NEXT(entry, entry);
598 static const struct got_error *
599 show_log_view(struct got_object_id *start_id, struct got_repository *repo)
601 const struct got_error *err = NULL;
602 struct got_object_id *id;
603 int ch, done = 0, selected = 0, nparents;
604 struct commit_queue commits;
605 struct commit_queue_entry *first_displayed_entry = NULL;
606 struct commit_queue_entry *last_displayed_entry = NULL;
608 id = got_object_id_dup(start_id);
610 return got_error_from_errno();
612 if (tog_log_view.window == NULL) {
613 tog_log_view.window = newwin(0, 0, 0, 0);
614 if (tog_log_view.window == NULL)
615 return got_error_from_errno();
616 keypad(tog_log_view.window, TRUE);
618 if (tog_log_view.panel == NULL) {
619 tog_log_view.panel = new_panel(tog_log_view.window);
620 if (tog_log_view.panel == NULL)
621 return got_error_from_errno();
624 TAILQ_INIT(&commits);
625 err = fetch_commits(&first_displayed_entry, id, &commits, LINES, repo);
629 err = draw_commits(&last_displayed_entry, first_displayed_entry,
634 nodelay(stdscr, FALSE);
635 ch = wgetch(tog_log_view.window);
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)
669 if (selected < LINES - 1 &&
670 selected < nparents - 1)
672 err = scroll_down(&first_displayed_entry, 1,
673 last_displayed_entry, &commits, repo);
678 nparents = num_parents(first_displayed_entry);
679 if (nparents < LINES - 1 &&
680 selected < nparents - 1) {
681 selected = nparents - 1;
684 err = scroll_down(&first_displayed_entry, LINES,
685 last_displayed_entry, &commits, repo);
688 nparents = num_parents(first_displayed_entry);
689 if (selected > nparents)
690 selected = nparents - 1;
693 if (selected > LINES)
694 selected = LINES - 1;
699 nodelay(stdscr, TRUE);
702 free_commits(&commits);
706 const struct got_error *
707 cmd_log(int argc, char *argv[])
709 const struct got_error *error;
710 struct got_repository *repo;
711 struct got_object_id *start_id = NULL;
712 char *repo_path = NULL;
713 char *start_commit = NULL;
717 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
721 while ((ch = getopt(argc, argv, "c:")) != -1) {
724 start_commit = optarg;
736 repo_path = getcwd(NULL, 0);
737 if (repo_path == NULL)
738 return got_error_from_errno();
739 } else if (argc == 1) {
740 repo_path = realpath(argv[0], NULL);
741 if (repo_path == NULL)
742 return got_error_from_errno();
746 error = got_repo_open(&repo, repo_path);
751 if (start_commit == NULL) {
752 error = get_head_commit_id(&start_id, repo);
756 struct got_object *obj;
757 error = got_object_open_by_id_str(&obj, repo, start_commit);
759 start_id = got_object_get_id(obj);
760 if (start_id == NULL)
761 error = got_error_from_errno();
766 error = show_log_view(start_id, repo);
768 got_repo_close(repo);
776 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
781 const struct got_error *
782 draw_diff(FILE *f, int *first_displayed_line, int *last_displayed_line,
783 int *eof, int max_lines)
785 int nlines = 0, nprinted = 0;
788 wclear(tog_diff_view.window);
791 while (nprinted < max_lines) {
795 const char delim[3] = { '\0', '\0', '\0'};
797 line = fparseln(f, &linelen, &lineno, delim, 0);
802 if (++nlines < *first_displayed_line) {
807 if (linelen > COLS - 1)
808 line[COLS - 1] = '\0';
809 waddstr(tog_diff_view.window, line);
810 waddch(tog_diff_view.window, '\n');
812 *first_displayed_line = nlines;
815 *last_displayed_line = nlines;
823 const struct got_error *
824 show_diff_view(struct got_object *obj1, struct got_object *obj2,
825 struct got_repository *repo)
827 const struct got_error *err;
829 int ch, done = 0, first_displayed_line = 1, last_displayed_line = LINES;
832 if (got_object_get_type(obj1) != got_object_get_type(obj2))
833 return got_error(GOT_ERR_OBJ_TYPE);
837 return got_error_from_errno();
839 switch (got_object_get_type(obj1)) {
840 case GOT_OBJ_TYPE_BLOB:
841 err = got_diff_objects_as_blobs(obj1, obj2, repo, f);
843 case GOT_OBJ_TYPE_TREE:
844 err = got_diff_objects_as_trees(obj1, obj2, repo, f);
846 case GOT_OBJ_TYPE_COMMIT:
847 err = got_diff_objects_as_commits(obj1, obj2, repo, f);
850 return got_error(GOT_ERR_OBJ_TYPE);
855 if (tog_diff_view.window == NULL) {
856 tog_diff_view.window = newwin(0, 0, 0, 0);
857 if (tog_diff_view.window == NULL)
858 return got_error_from_errno();
859 keypad(tog_diff_view.window, TRUE);
861 if (tog_diff_view.panel == NULL) {
862 tog_diff_view.panel = new_panel(tog_diff_view.window);
863 if (tog_diff_view.panel == NULL)
864 return got_error_from_errno();
866 show_panel(tog_diff_view.panel);
869 err = draw_diff(f, &first_displayed_line, &last_displayed_line,
873 nodelay(stdscr, FALSE);
874 ch = wgetch(tog_diff_view.window);
881 if (first_displayed_line > 1)
882 first_displayed_line--;
887 first_displayed_line++;
892 nodelay(stdscr, TRUE);
898 const struct got_error *
899 cmd_diff(int argc, char *argv[])
901 const struct got_error *error = NULL;
902 struct got_repository *repo = NULL;
903 struct got_object *obj1 = NULL, *obj2 = NULL;
904 char *repo_path = NULL;
905 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
909 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
913 while ((ch = getopt(argc, argv, "")) != -1) {
925 usage_diff(); /* TODO show local worktree changes */
926 } else if (argc == 2) {
927 repo_path = getcwd(NULL, 0);
928 if (repo_path == NULL)
929 return got_error_from_errno();
930 obj_id_str1 = argv[0];
931 obj_id_str2 = argv[1];
932 } else if (argc == 3) {
933 repo_path = realpath(argv[0], NULL);
934 if (repo_path == NULL)
935 return got_error_from_errno();
936 obj_id_str1 = argv[1];
937 obj_id_str2 = argv[2];
941 error = got_repo_open(&repo, repo_path);
946 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
950 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
954 error = show_diff_view(obj1, obj2, repo);
956 got_repo_close(repo);
958 got_object_close(obj1);
960 got_object_close(obj2);
968 fprintf(stderr, "usage: %s blame [repository-path] blob-object\n",
973 const struct got_error *
974 cmd_blame(int argc, char *argv[])
976 return got_error(GOT_ERR_NOT_IMPL);
979 static const struct got_error *
986 intrflush(stdscr, FALSE);
987 keypad(stdscr, TRUE);
990 tog_main_win = newwin(0, 0, 0, 0);
991 if (tog_main_win == NULL)
992 return got_error_from_errno();
993 tog_main_panel = new_panel(tog_main_win);
994 if (tog_main_panel == NULL)
995 return got_error_from_errno();
1005 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
1006 "Available commands:\n", getprogname());
1007 for (i = 0; i < nitems(tog_commands); i++) {
1008 struct tog_cmd *cmd = &tog_commands[i];
1009 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
1015 make_argv(const char *arg0, const char *arg1)
1018 int argc = (arg1 == NULL ? 1 : 2);
1020 argv = calloc(argc, sizeof(char *));
1023 argv[0] = strdup(arg0);
1024 if (argv[0] == NULL)
1027 argv[1] = strdup(arg1);
1028 if (argv[1] == NULL)
1036 main(int argc, char *argv[])
1038 const struct got_error *error = NULL;
1039 struct tog_cmd *cmd = NULL;
1041 char **cmd_argv = NULL;
1043 setlocale(LC_ALL, "");
1045 while ((ch = getopt(argc, argv, "h")) != -1) {
1062 /* Build an argument vector which runs a default command. */
1063 cmd = &tog_commands[0];
1064 cmd_argv = make_argv(cmd->name, NULL);
1069 /* Did the user specific a command? */
1070 for (i = 0; i < nitems(tog_commands); i++) {
1071 if (strncmp(tog_commands[i].name, argv[0],
1072 strlen(argv[0])) == 0) {
1073 cmd = &tog_commands[i];
1075 tog_commands[i].cmd_usage();
1080 /* Did the user specify a repository? */
1081 char *repo_path = realpath(argv[0], NULL);
1083 struct got_repository *repo;
1084 error = got_repo_open(&repo, repo_path);
1086 got_repo_close(repo);
1088 error = got_error_from_errno();
1090 fprintf(stderr, "%s: '%s' is neither a known "
1091 "command nor a path to a repository\n",
1092 getprogname(), argv[0]);
1096 cmd = &tog_commands[0];
1097 cmd_argv = make_argv(cmd->name, repo_path);
1103 error = init_curses();
1105 fprintf(stderr, "cannot initialize ncurses: %s\n", error->msg);
1109 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
1116 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);