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" },
81 PANEL *tog_main_panel;
82 static struct tog_log_view {
86 static struct tog_diff_view {
95 fprintf(stderr, "usage: %s log [-c commit] [repository-path]\n",
100 static const struct got_error *
101 draw_commit(struct got_commit_object *commit, struct got_object_id *id)
103 const struct got_error *err = NULL;
104 char *logmsg0 = NULL, *logmsg = NULL;
105 char *author0 = NULL, *author = NULL;
106 char *newline, *smallerthan;
109 const size_t id_display_len = 8;
110 const size_t author_display_len = 16;
111 size_t id_len, author_len, logmsg_len, avail;
114 err = got_object_id_str(&id_str, id);
117 id_len = strlen(id_str);
119 logmsg0 = strdup(commit->logmsg);
120 if (logmsg0 == NULL) {
121 err = got_error_from_errno();
125 while (*logmsg == '\n')
127 newline = strchr(logmsg, '\n');
130 logmsg_len = strlen(logmsg);
132 author0 = strdup(commit->author);
133 if (author0 == NULL) {
134 err = got_error_from_errno();
138 smallerthan = strchr(author, '<');
142 char *at = strchr(author, '@');
146 author_len = strlen(author);
149 line = calloc(avail + 1, sizeof(*line));
151 err = got_error_from_errno();
156 for (i = 0; i < MIN(id_display_len, id_len); i++) {
159 line[col++] = id_str[i];
161 while (i < id_display_len) {
170 for (i = 0; i < MIN(author_display_len, author_len); i++) {
173 line[col++] = author[i];
175 while (i < author_display_len) {
185 while (col < avail && *logmsg)
186 line[col++] = *logmsg++;
190 waddstr(tog_log_view.window, line);
191 waddch(tog_log_view.window, '\n');
200 struct commit_queue_entry {
201 TAILQ_ENTRY(commit_queue_entry) entry;
202 struct got_object_id *id;
203 struct got_commit_object *commit;
205 TAILQ_HEAD(commit_queue, commit_queue_entry);
207 static struct commit_queue_entry *
208 alloc_commit_queue_entry(struct got_commit_object *commit,
209 struct got_object_id *id)
211 struct commit_queue_entry *entry;
213 entry = calloc(1, sizeof(*entry));
218 entry->commit = commit;
223 pop_commit(struct commit_queue *commits)
225 struct commit_queue_entry *entry;
227 entry = TAILQ_FIRST(commits);
228 TAILQ_REMOVE(commits, entry, entry);
229 got_object_commit_close(entry->commit);
235 free_commits(struct commit_queue *commits)
237 while (!TAILQ_EMPTY(commits))
242 static const struct got_error *
243 fetch_parent_commit(struct commit_queue_entry **pentry,
244 struct commit_queue_entry *entry, struct got_repository *repo)
246 const struct got_error *err = NULL;
247 struct got_object *obj = NULL;
248 struct got_commit_object *commit;
249 struct got_object_id *id;
250 struct got_parent_id *pid;
254 /* Follow the first parent (TODO: handle merge commits). */
255 pid = SIMPLEQ_FIRST(&entry->commit->parent_ids);
258 err = got_object_open(&obj, repo, pid->id);
261 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
262 err = got_error(GOT_ERR_OBJ_TYPE);
263 got_object_close(obj);
267 err = got_object_commit_open(&commit, repo, obj);
268 got_object_close(obj);
272 id = got_object_id_dup(pid->id);
274 err = got_error_from_errno();
275 got_object_commit_close(commit);
279 *pentry = alloc_commit_queue_entry(commit, id);
280 if (*pentry == NULL) {
281 err = got_error_from_errno();
282 got_object_commit_close(commit);
288 static const struct got_error *
289 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
291 const struct got_error *err = NULL;
292 struct got_reference *head_ref;
296 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
300 err = got_ref_resolve(head_id, repo, head_ref);
301 got_ref_close(head_ref);
310 static const struct got_error *
311 prepend_commits(int *ncommits, struct commit_queue *commits,
312 struct got_object_id *first_id, struct got_object_id *last_id,
313 int limit, struct got_repository *repo)
315 const struct got_error *err = NULL;
316 struct got_object *first_obj = NULL, *last_obj = NULL;
317 struct got_commit_object *commit = NULL;
318 struct got_object_id *id = NULL;
319 struct commit_queue_entry *entry, *old_head_entry;
323 err = got_object_open(&first_obj, repo, first_id);
326 if (got_object_get_type(first_obj) != GOT_OBJ_TYPE_COMMIT) {
327 err = got_error(GOT_ERR_OBJ_TYPE);
330 err = got_object_open(&last_obj, repo, last_id);
333 if (got_object_get_type(last_obj) != GOT_OBJ_TYPE_COMMIT) {
334 err = got_error(GOT_ERR_OBJ_TYPE);
338 err = got_object_commit_open(&commit, repo, first_obj);
342 id = got_object_id_dup(first_id);
344 err = got_error_from_errno();
348 entry = alloc_commit_queue_entry(commit, id);
350 return got_error_from_errno();
352 old_head_entry = TAILQ_FIRST(commits);
354 TAILQ_INSERT_BEFORE(old_head_entry, entry, entry);
356 TAILQ_INSERT_HEAD(commits, entry, entry);
361 * Fetch parent commits.
362 * XXX If first and last commit aren't ancestrally related this loop
363 * we will keep iterating until a root commit is encountered.
366 struct commit_queue_entry *pentry;
368 err = fetch_parent_commit(&pentry, entry, repo);
375 * Fill up to old HEAD commit if commit queue was not empty.
376 * We must not leave a gap in history.
378 if (old_head_entry &&
379 got_object_id_cmp(pentry->id, old_head_entry->id) == 0)
382 TAILQ_INSERT_AFTER(commits, entry, pentry, entry);
384 if (*ncommits >= limit)
387 /* Fill up to last requested commit if queue was empty. */
388 if (old_head_entry == NULL &&
389 got_object_id_cmp(pentry->id, last_id) == 0)
397 got_object_close(first_obj);
399 got_object_close(last_obj);
403 static const struct got_error *
404 fetch_commits(struct commit_queue_entry **start_entry,
405 struct got_object_id *start_id, struct commit_queue *commits,
406 int limit, struct got_repository *repo)
408 const struct got_error *err;
409 struct commit_queue_entry *entry;
411 struct got_object_id *head_id = NULL;
415 err = get_head_commit_id(&head_id, repo);
419 /* Prepend HEAD commit and all ancestors up to start commit. */
420 err = prepend_commits(&ncommits, commits, head_id, start_id, limit,
425 if (got_object_id_cmp(head_id, start_id) == 0)
426 *start_entry = TAILQ_FIRST(commits);
428 *start_entry = TAILQ_LAST(commits, commit_queue);
430 if (ncommits >= limit)
433 /* Append more commits from start commit up to the requested limit. */
434 entry = TAILQ_LAST(commits, commit_queue);
435 while (entry && ncommits < limit) {
436 struct commit_queue_entry *pentry;
438 err = fetch_parent_commit(&pentry, entry, repo);
442 TAILQ_INSERT_TAIL(commits, pentry, entry);
452 static const struct got_error *
453 draw_commits(struct commit_queue_entry **last, struct commit_queue_entry *first,
454 int selected, int limit)
456 const struct got_error *err = NULL;
457 struct commit_queue_entry *entry;
460 wclear(tog_log_view.window);
465 if (ncommits == limit)
467 if (ncommits == selected)
468 wstandout(tog_log_view.window);
469 err = draw_commit(entry->commit, entry->id);
470 if (ncommits == selected)
471 wstandend(tog_log_view.window);
476 entry = TAILQ_NEXT(entry, entry);
486 scroll_up(struct commit_queue_entry **first_displayed_entry, int n,
487 struct commit_queue *commits)
489 struct commit_queue_entry *entry;
492 entry = TAILQ_FIRST(commits);
493 if (*first_displayed_entry == entry)
496 entry = *first_displayed_entry;
497 while (entry && nscrolled < n) {
498 entry = TAILQ_PREV(entry, commit_queue, entry);
500 *first_displayed_entry = entry;
506 static const struct got_error *
507 scroll_down(struct commit_queue_entry **first_displayed_entry, int n,
508 struct commit_queue_entry *last_displayed_entry,
509 struct commit_queue *commits, struct got_repository *repo)
511 const struct got_error *err = NULL;
512 struct commit_queue_entry *entry;
515 if (last_displayed_entry->commit->nparents == 0)
518 entry = *first_displayed_entry;
520 struct commit_queue_entry *pentry;
522 pentry = TAILQ_NEXT(entry, entry);
523 if (pentry == NULL) {
524 err = fetch_parent_commit(&pentry, entry, repo);
527 if (pentry == NULL) {
528 *first_displayed_entry = entry;
531 TAILQ_INSERT_TAIL(commits, pentry, entry);
532 last_displayed_entry = pentry;
535 *first_displayed_entry = pentry;
538 if (TAILQ_LAST(commits, commit_queue) == last_displayed_entry) {
539 err = fetch_parent_commit(&pentry, last_displayed_entry,
544 TAILQ_INSERT_TAIL(commits, pentry, entry);
545 last_displayed_entry = pentry;
548 } while (++nscrolled < n);
554 num_parents(struct commit_queue_entry *entry)
559 entry = TAILQ_NEXT(entry, entry);
566 static const struct got_error *
567 show_log_view(struct got_object_id *start_id, struct got_repository *repo)
569 const struct got_error *err = NULL;
570 struct got_object_id *id;
571 int ch, done = 0, selected = 0, nparents;
572 struct commit_queue commits;
573 struct commit_queue_entry *first_displayed_entry = NULL;
574 struct commit_queue_entry *last_displayed_entry = NULL;
576 id = got_object_id_dup(start_id);
578 return got_error_from_errno();
580 if (tog_log_view.window == NULL) {
581 tog_log_view.window = newwin(0, 0, 0, 0);
582 if (tog_log_view.window == NULL)
583 return got_error_from_errno();
584 keypad(tog_log_view.window, TRUE);
586 if (tog_log_view.panel == NULL) {
587 tog_log_view.panel = new_panel(tog_log_view.window);
588 if (tog_log_view.panel == NULL)
589 return got_error_from_errno();
592 TAILQ_INIT(&commits);
593 err = fetch_commits(&first_displayed_entry, id, &commits, LINES, repo);
597 err = draw_commits(&last_displayed_entry, first_displayed_entry,
602 nodelay(stdscr, FALSE);
603 ch = wgetch(tog_log_view.window);
607 err = got_error_from_errno();
620 scroll_up(&first_displayed_entry, 1, &commits);
623 if (TAILQ_FIRST(&commits) ==
624 first_displayed_entry) {
628 scroll_up(&first_displayed_entry, LINES,
633 nparents = num_parents(first_displayed_entry);
634 if (selected < LINES - 1 &&
635 selected < nparents - 1)
637 if (selected < LINES - 1 &&
638 selected < nparents - 1)
640 err = scroll_down(&first_displayed_entry, 1,
641 last_displayed_entry, &commits, repo);
646 nparents = num_parents(first_displayed_entry);
647 if (nparents < LINES - 1 &&
648 selected < nparents - 1) {
649 selected = nparents - 1;
652 err = scroll_down(&first_displayed_entry, LINES,
653 last_displayed_entry, &commits, repo);
656 nparents = num_parents(first_displayed_entry);
657 if (selected > nparents)
658 selected = nparents - 1;
661 if (selected > LINES)
662 selected = LINES - 1;
667 nodelay(stdscr, TRUE);
670 free_commits(&commits);
674 const struct got_error *
675 cmd_log(int argc, char *argv[])
677 const struct got_error *error;
678 struct got_repository *repo;
679 struct got_object_id *start_id = NULL;
680 char *repo_path = NULL;
681 char *start_commit = NULL;
685 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
689 while ((ch = getopt(argc, argv, "c:")) != -1) {
692 start_commit = optarg;
704 repo_path = getcwd(NULL, 0);
705 if (repo_path == NULL)
706 return got_error_from_errno();
707 } else if (argc == 1) {
708 repo_path = realpath(argv[0], NULL);
709 if (repo_path == NULL)
710 return got_error_from_errno();
714 error = got_repo_open(&repo, repo_path);
719 if (start_commit == NULL) {
720 error = get_head_commit_id(&start_id, repo);
724 struct got_object *obj;
725 error = got_object_open_by_id_str(&obj, repo, start_commit);
727 start_id = got_object_get_id(obj);
728 if (start_id == NULL)
729 error = got_error_from_errno();
734 error = show_log_view(start_id, repo);
736 got_repo_close(repo);
744 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
749 const struct got_error *
750 draw_diff(FILE *f, int *first_displayed_line, int *last_displayed_line,
751 int *eof, int max_lines)
753 int nlines = 0, nprinted = 0;
756 wclear(tog_diff_view.window);
759 while (nprinted < max_lines) {
763 const char delim[3] = { '\0', '\0', '\0'};
765 line = fparseln(f, &linelen, &lineno, delim, 0);
770 if (++nlines < *first_displayed_line) {
775 if (linelen > COLS - 1)
776 line[COLS - 1] = '\0';
777 waddstr(tog_diff_view.window, line);
778 waddch(tog_diff_view.window, '\n');
780 *first_displayed_line = nlines;
783 *last_displayed_line = nlines;
791 const struct got_error *
792 show_diff_view(struct got_object *obj1, struct got_object *obj2,
793 struct got_repository *repo)
795 const struct got_error *err;
797 int ch, done = 0, first_displayed_line = 1, last_displayed_line = LINES;
800 if (got_object_get_type(obj1) != got_object_get_type(obj2))
801 return got_error(GOT_ERR_OBJ_TYPE);
805 return got_error_from_errno();
807 switch (got_object_get_type(obj1)) {
808 case GOT_OBJ_TYPE_BLOB:
809 err = got_diff_objects_as_blobs(obj1, obj2, repo, f);
811 case GOT_OBJ_TYPE_TREE:
812 err = got_diff_objects_as_trees(obj1, obj2, repo, f);
814 case GOT_OBJ_TYPE_COMMIT:
815 err = got_diff_objects_as_commits(obj1, obj2, repo, f);
818 return got_error(GOT_ERR_OBJ_TYPE);
823 if (tog_diff_view.window == NULL) {
824 tog_diff_view.window = newwin(0, 0, 0, 0);
825 if (tog_diff_view.window == NULL)
826 return got_error_from_errno();
827 keypad(tog_diff_view.window, TRUE);
829 if (tog_diff_view.panel == NULL) {
830 tog_diff_view.panel = new_panel(tog_diff_view.window);
831 if (tog_diff_view.panel == NULL)
832 return got_error_from_errno();
834 show_panel(tog_diff_view.panel);
837 err = draw_diff(f, &first_displayed_line, &last_displayed_line,
841 nodelay(stdscr, FALSE);
842 ch = wgetch(tog_diff_view.window);
849 if (first_displayed_line > 1)
850 first_displayed_line--;
855 first_displayed_line++;
860 nodelay(stdscr, TRUE);
866 const struct got_error *
867 cmd_diff(int argc, char *argv[])
869 const struct got_error *error = NULL;
870 struct got_repository *repo = NULL;
871 struct got_object *obj1 = NULL, *obj2 = NULL;
872 char *repo_path = NULL;
873 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
877 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
881 while ((ch = getopt(argc, argv, "")) != -1) {
893 usage_diff(); /* TODO show local worktree changes */
894 } else if (argc == 2) {
895 repo_path = getcwd(NULL, 0);
896 if (repo_path == NULL)
897 return got_error_from_errno();
898 obj_id_str1 = argv[0];
899 obj_id_str2 = argv[1];
900 } else if (argc == 3) {
901 repo_path = realpath(argv[0], NULL);
902 if (repo_path == NULL)
903 return got_error_from_errno();
904 obj_id_str1 = argv[1];
905 obj_id_str2 = argv[2];
909 error = got_repo_open(&repo, repo_path);
914 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
918 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
922 error = show_diff_view(obj1, obj2, repo);
924 got_repo_close(repo);
926 got_object_close(obj1);
928 got_object_close(obj2);
936 fprintf(stderr, "usage: %s blame [repository-path] blob-object\n",
941 const struct got_error *
942 cmd_blame(int argc, char *argv[])
944 return got_error(GOT_ERR_NOT_IMPL);
947 static const struct got_error *
954 intrflush(stdscr, FALSE);
955 keypad(stdscr, TRUE);
958 tog_main_win = newwin(0, 0, 0, 0);
959 if (tog_main_win == NULL)
960 return got_error_from_errno();
961 tog_main_panel = new_panel(tog_main_win);
962 if (tog_main_panel == NULL)
963 return got_error_from_errno();
973 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
974 "Available commands:\n", getprogname());
975 for (i = 0; i < nitems(tog_commands); i++) {
976 struct tog_cmd *cmd = &tog_commands[i];
977 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
983 make_argv(const char *arg0, const char *arg1)
986 int argc = (arg1 == NULL ? 1 : 2);
988 argv = calloc(argc, sizeof(char *));
991 argv[0] = strdup(arg0);
995 argv[1] = strdup(arg1);
1004 main(int argc, char *argv[])
1006 const struct got_error *error = NULL;
1007 struct tog_cmd *cmd = NULL;
1009 char **cmd_argv = NULL;
1011 setlocale(LC_ALL, "");
1013 while ((ch = getopt(argc, argv, "h")) != -1) {
1030 /* Build an argument vector which runs a default command. */
1031 cmd = &tog_commands[0];
1032 cmd_argv = make_argv(cmd->name, NULL);
1037 /* Did the user specific a command? */
1038 for (i = 0; i < nitems(tog_commands); i++) {
1039 if (strncmp(tog_commands[i].name, argv[0],
1040 strlen(argv[0])) == 0) {
1041 cmd = &tog_commands[i];
1043 tog_commands[i].cmd_usage();
1048 /* Did the user specify a repository? */
1049 char *repo_path = realpath(argv[0], NULL);
1051 struct got_repository *repo;
1052 error = got_repo_open(&repo, repo_path);
1054 got_repo_close(repo);
1056 error = got_error_from_errno();
1058 fprintf(stderr, "%s: '%s' is neither a known "
1059 "command nor a path to a repository\n",
1060 getprogname(), argv[0]);
1064 cmd = &tog_commands[0];
1065 cmd_argv = make_argv(cmd->name, repo_path);
1071 error = init_curses();
1073 fprintf(stderr, "cannot initialize ncurses: %s\n", error->msg);
1077 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
1084 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);