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>
29 #include "got_error.h"
30 #include "got_object.h"
31 #include "got_reference.h"
32 #include "got_repository.h"
36 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
40 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
51 const struct got_error *(*cmd_main)(int, char *[]);
52 void (*cmd_usage)(void);
53 enum tog_view_id view;
57 __dead void usage(void);
58 __dead void usage_log(void);
59 __dead void usage_diff(void);
60 __dead void usage_blame(void);
62 const struct got_error* cmd_log(int, char *[]);
63 const struct got_error* cmd_diff(int, char *[]);
64 const struct got_error* cmd_blame(int, char *[]);
66 struct tog_cmd tog_commands[] = {
67 { "log", cmd_log, usage_log, TOG_VIEW_LOG,
68 "show repository history" },
69 { "diff", cmd_diff, usage_diff, TOG_VIEW_DIFF,
70 "compare files and directories" },
71 { "blame", cmd_blame, usage_blame, TOG_VIEW_BLAME,
72 "show line-by-line file history" },
77 PANEL *tog_main_panel;
78 static struct tog_log_view {
87 fprintf(stderr, "usage: %s log [-c commit] [repository-path]\n",
92 static const struct got_error *
93 draw_commit(struct got_commit_object *commit, struct got_object_id *id)
95 const struct got_error *err = NULL;
96 char *logmsg0 = NULL, *logmsg = NULL;
97 char *author0 = NULL, *author = NULL;
98 char *newline, *smallerthan;
101 const size_t id_display_len = 8;
102 const size_t author_display_len = 16;
103 size_t id_len, author_len, logmsg_len, avail;
106 err = got_object_id_str(&id_str, id);
109 id_len = strlen(id_str);
111 logmsg0 = strdup(commit->logmsg);
112 if (logmsg0 == NULL) {
113 err = got_error_from_errno();
117 while (*logmsg == '\n')
119 newline = strchr(logmsg, '\n');
122 logmsg_len = strlen(logmsg);
124 author0 = strdup(commit->author);
125 if (author0 == NULL) {
126 err = got_error_from_errno();
130 smallerthan = strchr(author, '<');
134 char *at = strchr(author, '@');
138 author_len = strlen(author);
141 line = calloc(avail + 1, sizeof(*line));
143 err = got_error_from_errno();
148 for (i = 0; i < MIN(id_display_len, id_len); i++) {
151 line[col++] = id_str[i];
153 while (i < id_display_len) {
162 for (i = 0; i < MIN(author_display_len, author_len); i++) {
165 line[col++] = author[i];
167 while (i < author_display_len) {
177 while (col < avail && *logmsg)
178 line[col++] = *logmsg++;
182 waddstr(tog_log_view.window, line);
183 waddch(tog_log_view.window, '\n');
191 struct commit_queue_entry {
192 TAILQ_ENTRY(commit_queue_entry) entry;
193 struct got_object_id *id;
194 struct got_commit_object *commit;
196 TAILQ_HEAD(commit_queue, commit_queue_entry);
198 static struct commit_queue_entry *
199 alloc_commit_queue_entry(struct got_commit_object *commit,
200 struct got_object_id *id)
202 struct commit_queue_entry *entry;
204 entry = calloc(1, sizeof(*entry));
209 entry->commit = commit;
214 pop_commit(struct commit_queue *commits)
216 struct commit_queue_entry *entry;
218 entry = TAILQ_FIRST(commits);
219 TAILQ_REMOVE(commits, entry, entry);
220 got_object_commit_close(entry->commit);
226 free_commits(struct commit_queue *commits)
228 while (!TAILQ_EMPTY(commits))
233 static const struct got_error *
234 fetch_parent_commit(struct commit_queue_entry **pentry,
235 struct commit_queue_entry *entry, struct got_repository *repo)
237 const struct got_error *err = NULL;
238 struct got_object *obj = NULL;
239 struct got_commit_object *commit;
240 struct got_object_id *id;
241 struct got_parent_id *pid;
245 /* Follow the first parent (TODO: handle merge commits). */
246 pid = SIMPLEQ_FIRST(&entry->commit->parent_ids);
249 err = got_object_open(&obj, repo, pid->id);
252 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
253 err = got_error(GOT_ERR_OBJ_TYPE);
254 got_object_close(obj);
258 err = got_object_commit_open(&commit, repo, obj);
259 got_object_close(obj);
263 id = got_object_id_dup(pid->id);
265 err = got_error_from_errno();
266 got_object_commit_close(commit);
270 *pentry = alloc_commit_queue_entry(commit, id);
271 if (*pentry == NULL) {
272 err = got_error_from_errno();
273 got_object_commit_close(commit);
279 static const struct got_error *
280 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
282 const struct got_error *err = NULL;
283 struct got_reference *head_ref;
287 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
291 err = got_ref_resolve(head_id, repo, head_ref);
292 got_ref_close(head_ref);
301 static const struct got_error *
302 prepend_commits(int *ncommits, struct commit_queue *commits,
303 struct got_object_id *first_id, struct got_object_id *last_id,
304 int limit, struct got_repository *repo)
306 const struct got_error *err = NULL;
307 struct got_object *first_obj = NULL, *last_obj = NULL;
308 struct got_commit_object *commit = NULL;
309 struct got_object_id *id = NULL;
310 struct commit_queue_entry *entry, *old_head_entry;
314 err = got_object_open(&first_obj, repo, first_id);
317 if (got_object_get_type(first_obj) != GOT_OBJ_TYPE_COMMIT) {
318 err = got_error(GOT_ERR_OBJ_TYPE);
321 err = got_object_open(&last_obj, repo, last_id);
324 if (got_object_get_type(last_obj) != GOT_OBJ_TYPE_COMMIT) {
325 err = got_error(GOT_ERR_OBJ_TYPE);
329 err = got_object_commit_open(&commit, repo, first_obj);
333 id = got_object_id_dup(first_id);
335 err = got_error_from_errno();
339 entry = alloc_commit_queue_entry(commit, id);
341 return got_error_from_errno();
343 old_head_entry = TAILQ_FIRST(commits);
345 TAILQ_INSERT_BEFORE(old_head_entry, entry, entry);
347 TAILQ_INSERT_HEAD(commits, entry, entry);
352 * Fetch parent commits.
353 * XXX If first and last commit aren't ancestrally related this loop
354 * we will keep iterating until a root commit is encountered.
357 struct commit_queue_entry *pentry;
359 err = fetch_parent_commit(&pentry, entry, repo);
366 * Fill up to old HEAD commit if commit queue was not empty.
367 * We must not leave a gap in history.
369 if (old_head_entry &&
370 got_object_id_cmp(pentry->id, old_head_entry->id) == 0)
373 TAILQ_INSERT_AFTER(commits, entry, pentry, entry);
375 if (*ncommits >= limit)
378 /* Fill up to last requested commit if queue was empty. */
379 if (old_head_entry == NULL &&
380 got_object_id_cmp(pentry->id, last_id) == 0)
388 got_object_close(first_obj);
390 got_object_close(last_obj);
394 static const struct got_error *
395 fetch_commits(struct commit_queue_entry **start_entry,
396 struct got_object_id *start_id, struct commit_queue *commits,
397 int limit, struct got_repository *repo)
399 const struct got_error *err;
400 struct commit_queue_entry *entry;
402 struct got_object_id *head_id = NULL;
406 err = get_head_commit_id(&head_id, repo);
410 /* Prepend HEAD commit and all ancestors up to start commit. */
411 err = prepend_commits(&ncommits, commits, head_id, start_id, limit,
416 if (got_object_id_cmp(head_id, start_id) == 0)
417 *start_entry = TAILQ_FIRST(commits);
419 *start_entry = TAILQ_LAST(commits, commit_queue);
421 if (ncommits >= limit)
424 /* Append more commits from start commit up to the requested limit. */
425 entry = TAILQ_LAST(commits, commit_queue);
426 while (entry && ncommits < limit) {
427 struct commit_queue_entry *pentry;
429 err = fetch_parent_commit(&pentry, entry, repo);
433 TAILQ_INSERT_TAIL(commits, pentry, entry);
443 static const struct got_error *
444 draw_commits(struct commit_queue_entry **last, struct commit_queue_entry *first,
445 int selected, int limit)
447 const struct got_error *err = NULL;
448 struct commit_queue_entry *entry;
451 wclear(tog_log_view.window);
456 if (ncommits == limit)
458 if (ncommits == selected)
459 wstandout(tog_log_view.window);
460 err = draw_commit(entry->commit, entry->id);
461 if (ncommits == selected)
462 wstandend(tog_log_view.window);
467 entry = TAILQ_NEXT(entry, entry);
477 scroll_up(struct commit_queue_entry **first_displayed_entry, int n,
478 struct commit_queue *commits)
480 struct commit_queue_entry *entry;
483 entry = TAILQ_FIRST(commits);
484 if (*first_displayed_entry == entry)
487 entry = *first_displayed_entry;
488 while (entry && nscrolled < n) {
489 entry = TAILQ_PREV(entry, commit_queue, entry);
491 *first_displayed_entry = entry;
497 static const struct got_error *
498 scroll_down(struct commit_queue_entry **first_displayed_entry, int n,
499 struct commit_queue_entry *last_displayed_entry,
500 struct commit_queue *commits, struct got_repository *repo)
502 const struct got_error *err = NULL;
503 struct commit_queue_entry *entry;
506 if (last_displayed_entry->commit->nparents == 0)
509 entry = *first_displayed_entry;
511 struct commit_queue_entry *pentry;
513 pentry = TAILQ_NEXT(entry, entry);
514 if (pentry == NULL) {
515 err = fetch_parent_commit(&pentry, entry, repo);
518 if (pentry == NULL) {
519 *first_displayed_entry = entry;
522 TAILQ_INSERT_TAIL(commits, pentry, entry);
523 last_displayed_entry = pentry;
526 *first_displayed_entry = pentry;
529 if (TAILQ_LAST(commits, commit_queue) == last_displayed_entry) {
530 err = fetch_parent_commit(&pentry, last_displayed_entry,
535 TAILQ_INSERT_TAIL(commits, pentry, entry);
536 last_displayed_entry = pentry;
539 } while (++nscrolled < n);
545 num_parents(struct commit_queue_entry *entry)
550 entry = TAILQ_NEXT(entry, entry);
557 static const struct got_error *
558 show_log_view(struct got_object_id *start_id, struct got_repository *repo)
560 const struct got_error *err = NULL;
561 struct got_object_id *id;
562 int ch, done = 0, selected = 0, nparents;
563 struct commit_queue commits;
564 struct commit_queue_entry *first_displayed_entry = NULL;
565 struct commit_queue_entry *last_displayed_entry = NULL;
567 id = got_object_id_dup(start_id);
569 return got_error_from_errno();
571 if (tog_log_view.window == NULL) {
572 tog_log_view.window = newwin(0, 0, 0, 0);
573 if (tog_log_view.window == NULL)
574 return got_error_from_errno();
575 keypad(tog_log_view.window, TRUE);
577 if (tog_log_view.panel == NULL) {
578 tog_log_view.panel = new_panel(tog_log_view.window);
579 if (tog_log_view.panel == NULL)
580 return got_error_from_errno();
583 TAILQ_INIT(&commits);
584 err = fetch_commits(&first_displayed_entry, id, &commits, LINES, repo);
588 err = draw_commits(&last_displayed_entry, first_displayed_entry,
593 nodelay(stdscr, FALSE);
594 ch = wgetch(tog_log_view.window);
598 err = got_error_from_errno();
611 scroll_up(&first_displayed_entry, 1, &commits);
614 if (TAILQ_FIRST(&commits) ==
615 first_displayed_entry) {
619 scroll_up(&first_displayed_entry, LINES,
624 nparents = num_parents(first_displayed_entry);
625 if (selected < LINES - 1 &&
626 selected < nparents - 1)
628 if (selected < LINES - 1 &&
629 selected < nparents - 1)
631 err = scroll_down(&first_displayed_entry, 1,
632 last_displayed_entry, &commits, repo);
637 nparents = num_parents(first_displayed_entry);
638 if (nparents < LINES - 1 &&
639 selected < nparents - 1) {
640 selected = nparents - 1;
643 err = scroll_down(&first_displayed_entry,
644 MIN(nparents, LINES), last_displayed_entry,
648 nparents = num_parents(first_displayed_entry);
649 if (selected > nparents)
650 selected = nparents - 1;
653 if (selected > LINES)
654 selected = LINES - 1;
659 nodelay(stdscr, TRUE);
662 free_commits(&commits);
666 const struct got_error *
667 cmd_log(int argc, char *argv[])
669 const struct got_error *error;
670 struct got_repository *repo;
671 struct got_object_id *start_id = NULL;
672 char *repo_path = NULL;
673 char *start_commit = NULL;
677 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
681 while ((ch = getopt(argc, argv, "c:")) != -1) {
684 start_commit = optarg;
696 repo_path = getcwd(NULL, 0);
697 if (repo_path == NULL)
698 return got_error_from_errno();
699 } else if (argc == 1) {
700 repo_path = realpath(argv[0], NULL);
701 if (repo_path == NULL)
702 return got_error_from_errno();
706 error = got_repo_open(&repo, repo_path);
711 if (start_commit == NULL) {
712 error = get_head_commit_id(&start_id, repo);
716 struct got_object *obj;
717 error = got_object_open_by_id_str(&obj, repo, start_commit);
719 start_id = got_object_get_id(obj);
720 if (start_id == NULL)
721 error = got_error_from_errno();
726 error = show_log_view(start_id, repo);
728 got_repo_close(repo);
736 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
741 const struct got_error *
742 cmd_diff(int argc, char *argv[])
744 return got_error(GOT_ERR_NOT_IMPL);
751 fprintf(stderr, "usage: %s blame [repository-path] blob-object\n",
756 const struct got_error *
757 cmd_blame(int argc, char *argv[])
759 return got_error(GOT_ERR_NOT_IMPL);
762 static const struct got_error *
769 intrflush(stdscr, FALSE);
770 keypad(stdscr, TRUE);
773 tog_main_win = newwin(0, 0, 0, 0);
774 if (tog_main_win == NULL)
775 return got_error_from_errno();
776 tog_main_panel = new_panel(tog_main_win);
777 if (tog_main_panel == NULL)
778 return got_error_from_errno();
788 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
789 "Available commands:\n", getprogname());
790 for (i = 0; i < nitems(tog_commands); i++) {
791 struct tog_cmd *cmd = &tog_commands[i];
792 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
798 make_argv(const char *arg0, const char *arg1)
801 int argc = (arg1 == NULL ? 1 : 2);
803 argv = calloc(argc, sizeof(char *));
806 argv[0] = strdup(arg0);
810 argv[1] = strdup(arg1);
819 main(int argc, char *argv[])
821 const struct got_error *error = NULL;
822 struct tog_cmd *cmd = NULL;
824 char **cmd_argv = NULL;
826 setlocale(LC_ALL, "");
828 while ((ch = getopt(argc, argv, "h")) != -1) {
845 /* Build an argument vector which runs a default command. */
846 cmd = &tog_commands[0];
847 cmd_argv = make_argv(cmd->name, NULL);
852 /* Did the user specific a command? */
853 for (i = 0; i < nitems(tog_commands); i++) {
854 if (strncmp(tog_commands[i].name, argv[0],
855 strlen(argv[0])) == 0) {
856 cmd = &tog_commands[i];
858 tog_commands[i].cmd_usage();
863 /* Did the user specify a repository? */
864 char *repo_path = realpath(argv[0], NULL);
866 struct got_repository *repo;
867 error = got_repo_open(&repo, repo_path);
869 got_repo_close(repo);
871 error = got_error_from_errno();
873 fprintf(stderr, "%s: '%s' is neither a known "
874 "command nor a path to a repository\n",
875 getprogname(), argv[0]);
879 cmd = &tog_commands[0];
880 cmd_argv = make_argv(cmd->name, repo_path);
886 error = init_curses();
888 fprintf(stderr, "cannot initialize ncurses: %s\n", error->msg);
892 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
899 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);