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_view {
83 } tog_log_view, tog_diff_view;
89 fprintf(stderr, "usage: %s log [-c commit] [repository-path]\n",
94 static const struct got_error *
95 draw_commit(struct got_commit_object *commit, struct got_object_id *id)
97 const struct got_error *err = NULL;
98 char *logmsg0 = NULL, *logmsg = NULL;
99 char *author0 = NULL, *author = NULL;
100 char *newline, *smallerthan;
103 const size_t id_display_len = 8;
104 const size_t author_display_len = 16;
105 size_t id_len, author_len, logmsg_len, avail;
108 err = got_object_id_str(&id_str, id);
111 id_len = strlen(id_str);
113 logmsg0 = strdup(commit->logmsg);
114 if (logmsg0 == NULL) {
115 err = got_error_from_errno();
119 while (*logmsg == '\n')
121 newline = strchr(logmsg, '\n');
124 logmsg_len = strlen(logmsg);
126 author0 = strdup(commit->author);
127 if (author0 == NULL) {
128 err = got_error_from_errno();
132 smallerthan = strchr(author, '<');
136 char *at = strchr(author, '@');
140 author_len = strlen(author);
143 line = calloc(avail + 1, sizeof(*line));
145 err = got_error_from_errno();
150 for (i = 0; i < MIN(id_display_len, id_len); i++) {
153 line[col++] = id_str[i];
155 while (i < id_display_len) {
164 for (i = 0; i < MIN(author_display_len, author_len); i++) {
167 line[col++] = author[i];
169 while (i < author_display_len) {
179 while (col < avail && *logmsg)
180 line[col++] = *logmsg++;
184 waddstr(tog_log_view.window, line);
185 waddch(tog_log_view.window, '\n');
194 struct commit_queue_entry {
195 TAILQ_ENTRY(commit_queue_entry) entry;
196 struct got_object_id *id;
197 struct got_commit_object *commit;
199 TAILQ_HEAD(commit_queue, commit_queue_entry);
201 static struct commit_queue_entry *
202 alloc_commit_queue_entry(struct got_commit_object *commit,
203 struct got_object_id *id)
205 struct commit_queue_entry *entry;
207 entry = calloc(1, sizeof(*entry));
212 entry->commit = commit;
217 pop_commit(struct commit_queue *commits)
219 struct commit_queue_entry *entry;
221 entry = TAILQ_FIRST(commits);
222 TAILQ_REMOVE(commits, entry, entry);
223 got_object_commit_close(entry->commit);
229 free_commits(struct commit_queue *commits)
231 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 *first,
448 int selected, int limit)
450 const struct got_error *err = NULL;
451 struct commit_queue_entry *entry;
454 wclear(tog_log_view.window);
459 if (ncommits == limit)
461 if (ncommits == selected)
462 wstandout(tog_log_view.window);
463 err = draw_commit(entry->commit, entry->id);
464 if (ncommits == selected)
465 wstandend(tog_log_view.window);
470 entry = TAILQ_NEXT(entry, entry);
480 scroll_up(struct commit_queue_entry **first_displayed_entry, int n,
481 struct commit_queue *commits)
483 struct commit_queue_entry *entry;
486 entry = TAILQ_FIRST(commits);
487 if (*first_displayed_entry == entry)
490 entry = *first_displayed_entry;
491 while (entry && nscrolled < n) {
492 entry = TAILQ_PREV(entry, commit_queue, entry);
494 *first_displayed_entry = entry;
500 static const struct got_error *
501 scroll_down(struct commit_queue_entry **first_displayed_entry, int n,
502 struct commit_queue_entry *last_displayed_entry,
503 struct commit_queue *commits, struct got_repository *repo)
505 const struct got_error *err = NULL;
506 struct commit_queue_entry *entry;
509 if (last_displayed_entry->commit->nparents == 0)
512 entry = *first_displayed_entry;
514 struct commit_queue_entry *pentry;
516 pentry = TAILQ_NEXT(entry, entry);
517 if (pentry == NULL) {
518 err = fetch_parent_commit(&pentry, entry, repo);
521 if (pentry == NULL) {
522 *first_displayed_entry = entry;
525 TAILQ_INSERT_TAIL(commits, pentry, entry);
526 last_displayed_entry = pentry;
529 *first_displayed_entry = pentry;
532 if (TAILQ_LAST(commits, commit_queue) == last_displayed_entry) {
533 err = fetch_parent_commit(&pentry, last_displayed_entry,
538 TAILQ_INSERT_TAIL(commits, pentry, entry);
539 last_displayed_entry = pentry;
542 } while (++nscrolled < n);
548 num_parents(struct commit_queue_entry *entry)
553 entry = TAILQ_NEXT(entry, entry);
560 static const struct got_error *
561 show_log_view(struct got_object_id *start_id, struct got_repository *repo)
563 const struct got_error *err = NULL;
564 struct got_object_id *id;
565 int ch, done = 0, selected = 0, nparents;
566 struct commit_queue commits;
567 struct commit_queue_entry *first_displayed_entry = NULL;
568 struct commit_queue_entry *last_displayed_entry = NULL;
570 id = got_object_id_dup(start_id);
572 return got_error_from_errno();
574 if (tog_log_view.window == NULL) {
575 tog_log_view.window = newwin(0, 0, 0, 0);
576 if (tog_log_view.window == NULL)
577 return got_error_from_errno();
578 keypad(tog_log_view.window, TRUE);
580 if (tog_log_view.panel == NULL) {
581 tog_log_view.panel = new_panel(tog_log_view.window);
582 if (tog_log_view.panel == NULL)
583 return got_error_from_errno();
586 TAILQ_INIT(&commits);
587 err = fetch_commits(&first_displayed_entry, id, &commits, LINES, repo);
591 err = draw_commits(&last_displayed_entry, first_displayed_entry,
596 nodelay(stdscr, FALSE);
597 ch = wgetch(tog_log_view.window);
601 err = got_error_from_errno();
614 scroll_up(&first_displayed_entry, 1, &commits);
617 if (TAILQ_FIRST(&commits) ==
618 first_displayed_entry) {
622 scroll_up(&first_displayed_entry, LINES,
627 nparents = num_parents(first_displayed_entry);
628 if (selected < LINES - 1 &&
629 selected < nparents - 1)
631 if (selected < LINES - 1 &&
632 selected < nparents - 1)
634 err = scroll_down(&first_displayed_entry, 1,
635 last_displayed_entry, &commits, repo);
640 nparents = num_parents(first_displayed_entry);
641 if (nparents < LINES - 1 &&
642 selected < nparents - 1) {
643 selected = nparents - 1;
646 err = scroll_down(&first_displayed_entry, LINES,
647 last_displayed_entry, &commits, repo);
650 nparents = num_parents(first_displayed_entry);
651 if (selected > nparents)
652 selected = nparents - 1;
655 if (selected > LINES)
656 selected = LINES - 1;
661 nodelay(stdscr, TRUE);
664 free_commits(&commits);
668 const struct got_error *
669 cmd_log(int argc, char *argv[])
671 const struct got_error *error;
672 struct got_repository *repo;
673 struct got_object_id *start_id = NULL;
674 char *repo_path = NULL;
675 char *start_commit = NULL;
679 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
683 while ((ch = getopt(argc, argv, "c:")) != -1) {
686 start_commit = optarg;
698 repo_path = getcwd(NULL, 0);
699 if (repo_path == NULL)
700 return got_error_from_errno();
701 } else if (argc == 1) {
702 repo_path = realpath(argv[0], NULL);
703 if (repo_path == NULL)
704 return got_error_from_errno();
708 error = got_repo_open(&repo, repo_path);
713 if (start_commit == NULL) {
714 error = get_head_commit_id(&start_id, repo);
718 struct got_object *obj;
719 error = got_object_open_by_id_str(&obj, repo, start_commit);
721 start_id = got_object_get_id(obj);
722 if (start_id == NULL)
723 error = got_error_from_errno();
728 error = show_log_view(start_id, repo);
730 got_repo_close(repo);
738 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
743 const struct got_error *
744 draw_diff(FILE *f, int *first_displayed_line, int *last_displayed_line,
745 int *eof, int max_lines)
747 int nlines = 0, nprinted = 0;
750 wclear(tog_diff_view.window);
753 while (nprinted < max_lines) {
757 const char delim[3] = { '\0', '\0', '\0'};
759 line = fparseln(f, &linelen, &lineno, delim, 0);
764 if (++nlines < *first_displayed_line) {
769 if (linelen > COLS - 1)
770 line[COLS - 1] = '\0';
771 waddstr(tog_diff_view.window, line);
772 waddch(tog_diff_view.window, '\n');
774 *first_displayed_line = nlines;
777 *last_displayed_line = nlines;
785 const struct got_error *
786 show_diff_view(struct got_object *obj1, struct got_object *obj2,
787 struct got_repository *repo)
789 const struct got_error *err;
791 int ch, done = 0, first_displayed_line = 1, last_displayed_line = LINES;
794 if (got_object_get_type(obj1) != got_object_get_type(obj2))
795 return got_error(GOT_ERR_OBJ_TYPE);
799 return got_error_from_errno();
801 switch (got_object_get_type(obj1)) {
802 case GOT_OBJ_TYPE_BLOB:
803 err = got_diff_objects_as_blobs(obj1, obj2, repo, f);
805 case GOT_OBJ_TYPE_TREE:
806 err = got_diff_objects_as_trees(obj1, obj2, repo, f);
808 case GOT_OBJ_TYPE_COMMIT:
809 err = got_diff_objects_as_commits(obj1, obj2, repo, f);
812 return got_error(GOT_ERR_OBJ_TYPE);
817 if (tog_diff_view.window == NULL) {
818 tog_diff_view.window = newwin(0, 0, 0, 0);
819 if (tog_diff_view.window == NULL)
820 return got_error_from_errno();
821 keypad(tog_diff_view.window, TRUE);
823 if (tog_diff_view.panel == NULL) {
824 tog_diff_view.panel = new_panel(tog_diff_view.window);
825 if (tog_diff_view.panel == NULL)
826 return got_error_from_errno();
828 show_panel(tog_diff_view.panel);
831 err = draw_diff(f, &first_displayed_line, &last_displayed_line,
835 nodelay(stdscr, FALSE);
836 ch = wgetch(tog_diff_view.window);
843 if (first_displayed_line > 1)
844 first_displayed_line--;
849 first_displayed_line++;
854 nodelay(stdscr, TRUE);
860 const struct got_error *
861 cmd_diff(int argc, char *argv[])
863 const struct got_error *error = NULL;
864 struct got_repository *repo = NULL;
865 struct got_object *obj1 = NULL, *obj2 = NULL;
866 char *repo_path = NULL;
867 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
871 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
875 while ((ch = getopt(argc, argv, "")) != -1) {
887 usage_diff(); /* TODO show local worktree changes */
888 } else if (argc == 2) {
889 repo_path = getcwd(NULL, 0);
890 if (repo_path == NULL)
891 return got_error_from_errno();
892 obj_id_str1 = argv[0];
893 obj_id_str2 = argv[1];
894 } else if (argc == 3) {
895 repo_path = realpath(argv[0], NULL);
896 if (repo_path == NULL)
897 return got_error_from_errno();
898 obj_id_str1 = argv[1];
899 obj_id_str2 = argv[2];
903 error = got_repo_open(&repo, repo_path);
908 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
912 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
916 error = show_diff_view(obj1, obj2, repo);
918 got_repo_close(repo);
920 got_object_close(obj1);
922 got_object_close(obj2);
930 fprintf(stderr, "usage: %s blame [repository-path] blob-object\n",
935 const struct got_error *
936 cmd_blame(int argc, char *argv[])
938 return got_error(GOT_ERR_NOT_IMPL);
948 intrflush(stdscr, FALSE);
949 keypad(stdscr, TRUE);
958 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
959 "Available commands:\n", getprogname());
960 for (i = 0; i < nitems(tog_commands); i++) {
961 struct tog_cmd *cmd = &tog_commands[i];
962 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
968 make_argv(const char *arg0, const char *arg1)
971 int argc = (arg1 == NULL ? 1 : 2);
973 argv = calloc(argc, sizeof(char *));
976 argv[0] = strdup(arg0);
980 argv[1] = strdup(arg1);
989 main(int argc, char *argv[])
991 const struct got_error *error = NULL;
992 struct tog_cmd *cmd = NULL;
994 char **cmd_argv = NULL;
996 setlocale(LC_ALL, "");
998 while ((ch = getopt(argc, argv, "h")) != -1) {
1015 /* Build an argument vector which runs a default command. */
1016 cmd = &tog_commands[0];
1017 cmd_argv = make_argv(cmd->name, NULL);
1022 /* Did the user specific a command? */
1023 for (i = 0; i < nitems(tog_commands); i++) {
1024 if (strncmp(tog_commands[i].name, argv[0],
1025 strlen(argv[0])) == 0) {
1026 cmd = &tog_commands[i];
1028 tog_commands[i].cmd_usage();
1033 /* Did the user specify a repository? */
1034 char *repo_path = realpath(argv[0], NULL);
1036 struct got_repository *repo;
1037 error = got_repo_open(&repo, repo_path);
1039 got_repo_close(repo);
1041 error = got_error_from_errno();
1043 fprintf(stderr, "%s: '%s' is neither a known "
1044 "command nor a path to a repository\n",
1045 getprogname(), argv[0]);
1049 cmd = &tog_commands[0];
1050 cmd_argv = make_argv(cmd->name, repo_path);
1058 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
1065 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);