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;
81 fprintf(stderr, "usage: %s log [-c commit] [repository-path]\n",
86 static const struct got_error *
87 draw_commit(struct got_commit_object *commit, struct got_object_id *id)
89 const struct got_error *err = NULL;
90 char *logmsg0 = NULL, *logmsg = NULL;
91 char *author0 = NULL, *author = NULL;
92 char *newline, *smallerthan;
95 const size_t id_display_len = 8;
96 const size_t author_display_len = 16;
97 size_t id_len, author_len, logmsg_len, avail;
100 err = got_object_id_str(&id_str, id);
103 id_len = strlen(id_str);
105 logmsg0 = strdup(commit->logmsg);
106 if (logmsg0 == NULL) {
107 err = got_error_from_errno();
111 while (*logmsg == '\n')
113 newline = strchr(logmsg, '\n');
116 logmsg_len = strlen(logmsg);
118 author0 = strdup(commit->author);
119 if (author0 == NULL) {
120 err = got_error_from_errno();
124 smallerthan = strchr(author, '<');
128 char *at = strchr(author, '@');
132 author_len = strlen(author);
135 line = calloc(avail + 1, sizeof(*line));
137 err = got_error_from_errno();
142 for (i = 0; i < MIN(id_display_len, id_len); i++) {
145 line[col++] = id_str[i];
147 while (i < id_display_len) {
156 for (i = 0; i < MIN(author_display_len, author_len); i++) {
159 line[col++] = author[i];
161 while (i < author_display_len) {
171 while (col < avail && *logmsg)
172 line[col++] = *logmsg++;
176 waddstr(tog_log_view.window, line);
177 waddch(tog_log_view.window, '\n');
186 struct commit_queue_entry {
187 TAILQ_ENTRY(commit_queue_entry) entry;
188 struct got_object_id *id;
189 struct got_commit_object *commit;
191 TAILQ_HEAD(commit_queue, commit_queue_entry);
193 static struct commit_queue_entry *
194 alloc_commit_queue_entry(struct got_commit_object *commit,
195 struct got_object_id *id)
197 struct commit_queue_entry *entry;
199 entry = calloc(1, sizeof(*entry));
204 entry->commit = commit;
209 pop_commit(struct commit_queue *commits)
211 struct commit_queue_entry *entry;
213 entry = TAILQ_FIRST(commits);
214 TAILQ_REMOVE(commits, entry, entry);
215 got_object_commit_close(entry->commit);
221 free_commits(struct commit_queue *commits)
223 while (!TAILQ_EMPTY(commits))
228 static const struct got_error *
229 fetch_parent_commit(struct commit_queue_entry **pentry,
230 struct commit_queue_entry *entry, struct got_repository *repo)
232 const struct got_error *err = NULL;
233 struct got_object *obj = NULL;
234 struct got_commit_object *commit;
235 struct got_object_id *id;
236 struct got_parent_id *pid;
240 /* Follow the first parent (TODO: handle merge commits). */
241 pid = SIMPLEQ_FIRST(&entry->commit->parent_ids);
244 err = got_object_open(&obj, repo, pid->id);
247 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
248 err = got_error(GOT_ERR_OBJ_TYPE);
249 got_object_close(obj);
253 err = got_object_commit_open(&commit, repo, obj);
254 got_object_close(obj);
258 id = got_object_id_dup(pid->id);
260 err = got_error_from_errno();
261 got_object_commit_close(commit);
265 *pentry = alloc_commit_queue_entry(commit, id);
266 if (*pentry == NULL) {
267 err = got_error_from_errno();
268 got_object_commit_close(commit);
274 static const struct got_error *
275 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
277 const struct got_error *err = NULL;
278 struct got_reference *head_ref;
282 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
286 err = got_ref_resolve(head_id, repo, head_ref);
287 got_ref_close(head_ref);
296 static const struct got_error *
297 prepend_commits(int *ncommits, struct commit_queue *commits,
298 struct got_object_id *first_id, struct got_object_id *last_id,
299 int limit, struct got_repository *repo)
301 const struct got_error *err = NULL;
302 struct got_object *first_obj = NULL, *last_obj = NULL;
303 struct got_commit_object *commit = NULL;
304 struct got_object_id *id = NULL;
305 struct commit_queue_entry *entry, *old_head_entry;
309 err = got_object_open(&first_obj, repo, first_id);
312 if (got_object_get_type(first_obj) != GOT_OBJ_TYPE_COMMIT) {
313 err = got_error(GOT_ERR_OBJ_TYPE);
316 err = got_object_open(&last_obj, repo, last_id);
319 if (got_object_get_type(last_obj) != GOT_OBJ_TYPE_COMMIT) {
320 err = got_error(GOT_ERR_OBJ_TYPE);
324 err = got_object_commit_open(&commit, repo, first_obj);
328 id = got_object_id_dup(first_id);
330 err = got_error_from_errno();
334 entry = alloc_commit_queue_entry(commit, id);
336 return got_error_from_errno();
338 old_head_entry = TAILQ_FIRST(commits);
340 TAILQ_INSERT_BEFORE(old_head_entry, entry, entry);
342 TAILQ_INSERT_HEAD(commits, entry, entry);
347 * Fetch parent commits.
348 * XXX If first and last commit aren't ancestrally related this loop
349 * we will keep iterating until a root commit is encountered.
352 struct commit_queue_entry *pentry;
354 err = fetch_parent_commit(&pentry, entry, repo);
361 * Fill up to old HEAD commit if commit queue was not empty.
362 * We must not leave a gap in history.
364 if (old_head_entry &&
365 got_object_id_cmp(pentry->id, old_head_entry->id) == 0)
368 TAILQ_INSERT_AFTER(commits, entry, pentry, entry);
370 if (*ncommits >= limit)
373 /* Fill up to last requested commit if queue was empty. */
374 if (old_head_entry == NULL &&
375 got_object_id_cmp(pentry->id, last_id) == 0)
383 got_object_close(first_obj);
385 got_object_close(last_obj);
389 static const struct got_error *
390 fetch_commits(struct commit_queue_entry **start_entry,
391 struct got_object_id *start_id, struct commit_queue *commits,
392 int limit, struct got_repository *repo)
394 const struct got_error *err;
395 struct commit_queue_entry *entry;
397 struct got_object_id *head_id = NULL;
401 err = get_head_commit_id(&head_id, repo);
405 /* Prepend HEAD commit and all ancestors up to start commit. */
406 err = prepend_commits(&ncommits, commits, head_id, start_id, limit,
411 if (got_object_id_cmp(head_id, start_id) == 0)
412 *start_entry = TAILQ_FIRST(commits);
414 *start_entry = TAILQ_LAST(commits, commit_queue);
416 if (ncommits >= limit)
419 /* Append more commits from start commit up to the requested limit. */
420 entry = TAILQ_LAST(commits, commit_queue);
421 while (entry && ncommits < limit) {
422 struct commit_queue_entry *pentry;
424 err = fetch_parent_commit(&pentry, entry, repo);
428 TAILQ_INSERT_TAIL(commits, pentry, entry);
438 static const struct got_error *
439 draw_commits(struct commit_queue_entry **last, struct commit_queue_entry *first,
440 int selected, int limit)
442 const struct got_error *err = NULL;
443 struct commit_queue_entry *entry;
446 wclear(tog_log_view.window);
451 if (ncommits == limit)
453 if (ncommits == selected)
454 wstandout(tog_log_view.window);
455 err = draw_commit(entry->commit, entry->id);
456 if (ncommits == selected)
457 wstandend(tog_log_view.window);
462 entry = TAILQ_NEXT(entry, entry);
472 scroll_up(struct commit_queue_entry **first_displayed_entry, int n,
473 struct commit_queue *commits)
475 struct commit_queue_entry *entry;
478 entry = TAILQ_FIRST(commits);
479 if (*first_displayed_entry == entry)
482 entry = *first_displayed_entry;
483 while (entry && nscrolled < n) {
484 entry = TAILQ_PREV(entry, commit_queue, entry);
486 *first_displayed_entry = entry;
492 static const struct got_error *
493 scroll_down(struct commit_queue_entry **first_displayed_entry, int n,
494 struct commit_queue_entry *last_displayed_entry,
495 struct commit_queue *commits, struct got_repository *repo)
497 const struct got_error *err = NULL;
498 struct commit_queue_entry *entry;
501 if (last_displayed_entry->commit->nparents == 0)
504 entry = *first_displayed_entry;
506 struct commit_queue_entry *pentry;
508 pentry = TAILQ_NEXT(entry, entry);
509 if (pentry == NULL) {
510 err = fetch_parent_commit(&pentry, entry, repo);
513 if (pentry == NULL) {
514 *first_displayed_entry = entry;
517 TAILQ_INSERT_TAIL(commits, pentry, entry);
518 last_displayed_entry = pentry;
521 *first_displayed_entry = pentry;
524 if (TAILQ_LAST(commits, commit_queue) == last_displayed_entry) {
525 err = fetch_parent_commit(&pentry, last_displayed_entry,
530 TAILQ_INSERT_TAIL(commits, pentry, entry);
531 last_displayed_entry = pentry;
534 } while (++nscrolled < n);
540 num_parents(struct commit_queue_entry *entry)
545 entry = TAILQ_NEXT(entry, entry);
552 static const struct got_error *
553 show_log_view(struct got_object_id *start_id, struct got_repository *repo)
555 const struct got_error *err = NULL;
556 struct got_object_id *id;
557 int ch, done = 0, selected = 0, nparents;
558 struct commit_queue commits;
559 struct commit_queue_entry *first_displayed_entry = NULL;
560 struct commit_queue_entry *last_displayed_entry = NULL;
562 id = got_object_id_dup(start_id);
564 return got_error_from_errno();
566 if (tog_log_view.window == NULL) {
567 tog_log_view.window = newwin(0, 0, 0, 0);
568 if (tog_log_view.window == NULL)
569 return got_error_from_errno();
570 keypad(tog_log_view.window, TRUE);
572 if (tog_log_view.panel == NULL) {
573 tog_log_view.panel = new_panel(tog_log_view.window);
574 if (tog_log_view.panel == NULL)
575 return got_error_from_errno();
578 TAILQ_INIT(&commits);
579 err = fetch_commits(&first_displayed_entry, id, &commits, LINES, repo);
583 err = draw_commits(&last_displayed_entry, first_displayed_entry,
588 nodelay(stdscr, FALSE);
589 ch = wgetch(tog_log_view.window);
593 err = got_error_from_errno();
606 scroll_up(&first_displayed_entry, 1, &commits);
609 if (TAILQ_FIRST(&commits) ==
610 first_displayed_entry) {
614 scroll_up(&first_displayed_entry, LINES,
619 nparents = num_parents(first_displayed_entry);
620 if (selected < LINES - 1 &&
621 selected < nparents - 1)
623 if (selected < LINES - 1 &&
624 selected < nparents - 1)
626 err = scroll_down(&first_displayed_entry, 1,
627 last_displayed_entry, &commits, repo);
632 nparents = num_parents(first_displayed_entry);
633 if (nparents < LINES - 1 &&
634 selected < nparents - 1) {
635 selected = nparents - 1;
638 err = scroll_down(&first_displayed_entry, LINES,
639 last_displayed_entry, &commits, repo);
642 nparents = num_parents(first_displayed_entry);
643 if (selected > nparents)
644 selected = nparents - 1;
647 if (selected > LINES)
648 selected = LINES - 1;
653 nodelay(stdscr, TRUE);
656 free_commits(&commits);
660 static const struct got_error *
661 cmd_log(int argc, char *argv[])
663 const struct got_error *error;
664 struct got_repository *repo;
665 struct got_object_id *start_id = NULL;
666 char *repo_path = NULL;
667 char *start_commit = NULL;
671 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
675 while ((ch = getopt(argc, argv, "c:")) != -1) {
678 start_commit = optarg;
690 repo_path = getcwd(NULL, 0);
691 if (repo_path == NULL)
692 return got_error_from_errno();
693 } else if (argc == 1) {
694 repo_path = realpath(argv[0], NULL);
695 if (repo_path == NULL)
696 return got_error_from_errno();
700 error = got_repo_open(&repo, repo_path);
705 if (start_commit == NULL) {
706 error = get_head_commit_id(&start_id, repo);
710 struct got_object *obj;
711 error = got_object_open_by_id_str(&obj, repo, start_commit);
713 start_id = got_object_get_id(obj);
714 if (start_id == NULL)
715 error = got_error_from_errno();
720 error = show_log_view(start_id, repo);
722 got_repo_close(repo);
730 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
735 static const struct got_error *
736 draw_diff(FILE *f, int *first_displayed_line, int *last_displayed_line,
737 int *eof, int max_lines)
739 int nlines = 0, nprinted = 0;
742 wclear(tog_diff_view.window);
745 while (nprinted < max_lines) {
749 const char delim[3] = { '\0', '\0', '\0'};
751 line = fparseln(f, &linelen, &lineno, delim, 0);
756 if (++nlines < *first_displayed_line) {
761 if (linelen > COLS - 1)
762 line[COLS - 1] = '\0';
763 waddstr(tog_diff_view.window, line);
764 waddch(tog_diff_view.window, '\n');
766 *first_displayed_line = nlines;
769 *last_displayed_line = nlines;
777 static const struct got_error *
778 show_diff_view(struct got_object *obj1, struct got_object *obj2,
779 struct got_repository *repo)
781 const struct got_error *err;
783 int ch, done = 0, first_displayed_line = 1, last_displayed_line = LINES;
786 if (got_object_get_type(obj1) != got_object_get_type(obj2))
787 return got_error(GOT_ERR_OBJ_TYPE);
791 return got_error_from_errno();
793 switch (got_object_get_type(obj1)) {
794 case GOT_OBJ_TYPE_BLOB:
795 err = got_diff_objects_as_blobs(obj1, obj2, repo, f);
797 case GOT_OBJ_TYPE_TREE:
798 err = got_diff_objects_as_trees(obj1, obj2, repo, f);
800 case GOT_OBJ_TYPE_COMMIT:
801 err = got_diff_objects_as_commits(obj1, obj2, repo, f);
804 return got_error(GOT_ERR_OBJ_TYPE);
809 if (tog_diff_view.window == NULL) {
810 tog_diff_view.window = newwin(0, 0, 0, 0);
811 if (tog_diff_view.window == NULL)
812 return got_error_from_errno();
813 keypad(tog_diff_view.window, TRUE);
815 if (tog_diff_view.panel == NULL) {
816 tog_diff_view.panel = new_panel(tog_diff_view.window);
817 if (tog_diff_view.panel == NULL)
818 return got_error_from_errno();
820 show_panel(tog_diff_view.panel);
823 err = draw_diff(f, &first_displayed_line, &last_displayed_line,
827 nodelay(stdscr, FALSE);
828 ch = wgetch(tog_diff_view.window);
835 if (first_displayed_line > 1)
836 first_displayed_line--;
841 first_displayed_line++;
846 nodelay(stdscr, TRUE);
852 static const struct got_error *
853 cmd_diff(int argc, char *argv[])
855 const struct got_error *error = NULL;
856 struct got_repository *repo = NULL;
857 struct got_object *obj1 = NULL, *obj2 = NULL;
858 char *repo_path = NULL;
859 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
863 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
867 while ((ch = getopt(argc, argv, "")) != -1) {
879 usage_diff(); /* TODO show local worktree changes */
880 } else if (argc == 2) {
881 repo_path = getcwd(NULL, 0);
882 if (repo_path == NULL)
883 return got_error_from_errno();
884 obj_id_str1 = argv[0];
885 obj_id_str2 = argv[1];
886 } else if (argc == 3) {
887 repo_path = realpath(argv[0], NULL);
888 if (repo_path == NULL)
889 return got_error_from_errno();
890 obj_id_str1 = argv[1];
891 obj_id_str2 = argv[2];
895 error = got_repo_open(&repo, repo_path);
900 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
904 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
908 error = show_diff_view(obj1, obj2, repo);
910 got_repo_close(repo);
912 got_object_close(obj1);
914 got_object_close(obj2);
922 fprintf(stderr, "usage: %s blame [repository-path] blob-object\n",
927 static const struct got_error *
928 cmd_blame(int argc, char *argv[])
930 return got_error(GOT_ERR_NOT_IMPL);
940 intrflush(stdscr, FALSE);
941 keypad(stdscr, TRUE);
950 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
951 "Available commands:\n", getprogname());
952 for (i = 0; i < nitems(tog_commands); i++) {
953 struct tog_cmd *cmd = &tog_commands[i];
954 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
960 make_argv(const char *arg0, const char *arg1)
963 int argc = (arg1 == NULL ? 1 : 2);
965 argv = calloc(argc, sizeof(char *));
968 argv[0] = strdup(arg0);
972 argv[1] = strdup(arg1);
981 main(int argc, char *argv[])
983 const struct got_error *error = NULL;
984 struct tog_cmd *cmd = NULL;
986 char **cmd_argv = NULL;
988 setlocale(LC_ALL, "");
990 while ((ch = getopt(argc, argv, "h")) != -1) {
1007 /* Build an argument vector which runs a default command. */
1008 cmd = &tog_commands[0];
1009 cmd_argv = make_argv(cmd->name, NULL);
1014 /* Did the user specific a command? */
1015 for (i = 0; i < nitems(tog_commands); i++) {
1016 if (strncmp(tog_commands[i].name, argv[0],
1017 strlen(argv[0])) == 0) {
1018 cmd = &tog_commands[i];
1020 tog_commands[i].cmd_usage();
1025 /* Did the user specify a repository? */
1026 char *repo_path = realpath(argv[0], NULL);
1028 struct got_repository *repo;
1029 error = got_repo_open(&repo, repo_path);
1031 got_repo_close(repo);
1033 error = got_error_from_errno();
1035 fprintf(stderr, "%s: '%s' is neither a known "
1036 "command nor a path to a repository\n",
1037 getprogname(), argv[0]);
1041 cmd = &tog_commands[0];
1042 cmd_argv = make_argv(cmd->name, repo_path);
1050 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
1057 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);