Blob


1 /*
2 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 *
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.
7 *
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.
15 */
17 #include <sys/queue.h>
19 #include <errno.h>
20 #include <curses.h>
21 #include <panel.h>
22 #include <locale.h>
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <getopt.h>
26 #include <string.h>
27 #include <err.h>
28 #include <unistd.h>
29 #include <util.h>
30 #include <limits.h>
32 #include "got_error.h"
33 #include "got_object.h"
34 #include "got_reference.h"
35 #include "got_repository.h"
36 #include "got_diff.h"
37 #include "got_opentemp.h"
39 #ifndef MIN
40 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
41 #endif
43 #ifndef nitems
44 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
45 #endif
47 struct tog_cmd {
48 const char *name;
49 const struct got_error *(*cmd_main)(int, char *[]);
50 void (*cmd_usage)(void);
51 const char *descr;
52 };
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" },
70 };
72 static struct tog_view {
73 WINDOW *window;
74 PANEL *panel;
75 } tog_log_view, tog_diff_view;
77 __dead static void
78 usage_log(void)
79 {
80 endwin();
81 fprintf(stderr, "usage: %s log [-c commit] [repository-path]\n",
82 getprogname());
83 exit(1);
84 }
86 static const struct got_error *
87 draw_commit(struct got_commit_object *commit, struct got_object_id *id)
88 {
89 const struct got_error *err = NULL;
90 char *logmsg0 = NULL, *logmsg = NULL;
91 char *author0 = NULL, *author = NULL;
92 char *newline, *smallerthan;
93 char *line = NULL;
94 char *id_str = NULL;
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;
98 int i, col;
100 err = got_object_id_str(&id_str, id);
101 if (err)
102 return err;
103 id_len = strlen(id_str);
105 logmsg0 = strdup(commit->logmsg);
106 if (logmsg0 == NULL) {
107 err = got_error_from_errno();
108 goto done;
110 logmsg = logmsg0;
111 while (*logmsg == '\n')
112 logmsg++;
113 newline = strchr(logmsg, '\n');
114 if (newline)
115 *newline = '\0';
116 logmsg_len = strlen(logmsg);
118 author0 = strdup(commit->author);
119 if (author0 == NULL) {
120 err = got_error_from_errno();
121 goto done;
123 author = author0;
124 smallerthan = strchr(author, '<');
125 if (smallerthan)
126 *smallerthan = '\0';
127 else {
128 char *at = strchr(author, '@');
129 if (at)
130 *at = '\0';
132 author_len = strlen(author);
134 avail = COLS - 1;
135 line = calloc(avail + 1, sizeof(*line));
136 if (line == NULL) {
137 err = got_error_from_errno();
138 goto done;
141 col = 0;
142 for (i = 0; i < MIN(id_display_len, id_len); i++) {
143 if (col >= avail)
144 goto draw;
145 line[col++] = id_str[i];
147 while (i < id_display_len) {
148 if (col >= avail)
149 goto draw;
150 line[col++] = ' ';
151 i++;
153 if (col >= avail)
154 goto draw;
155 line[col++] = ' ';
156 for (i = 0; i < MIN(author_display_len, author_len); i++) {
157 if (col >= avail)
158 goto draw;
159 line[col++] = author[i];
161 while (i < author_display_len) {
162 if (col >= avail)
163 goto draw;
164 line[col++] = ' ';
165 i++;
167 if (col >= avail)
168 goto draw;
169 line[col++] = ' ';
171 while (col < avail && *logmsg)
172 line[col++] = *logmsg++;
173 while (col < avail)
174 line[col++] = ' ';
175 draw:
176 waddstr(tog_log_view.window, line);
177 waddch(tog_log_view.window, '\n');
178 done:
179 free(logmsg0);
180 free(author0);
181 free(line);
182 free(id_str);
183 return err;
186 struct commit_queue_entry {
187 TAILQ_ENTRY(commit_queue_entry) entry;
188 struct got_object_id *id;
189 struct got_commit_object *commit;
190 };
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));
200 if (entry == NULL)
201 return NULL;
203 entry->id = id;
204 entry->commit = commit;
205 return entry;
208 static void
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);
216 free(entry->id);
217 free(entry);
220 static void
221 free_commits(struct commit_queue *commits)
223 while (!TAILQ_EMPTY(commits))
224 pop_commit(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;
238 *pentry = NULL;
240 /* Follow the first parent (TODO: handle merge commits). */
241 pid = SIMPLEQ_FIRST(&entry->commit->parent_ids);
242 if (pid == NULL)
243 return NULL;
244 err = got_object_open(&obj, repo, pid->id);
245 if (err)
246 return err;
247 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
248 err = got_error(GOT_ERR_OBJ_TYPE);
249 got_object_close(obj);
250 return err;
253 err = got_object_commit_open(&commit, repo, obj);
254 got_object_close(obj);
255 if (err)
256 return err;
258 id = got_object_id_dup(pid->id);
259 if (id == NULL) {
260 err = got_error_from_errno();
261 got_object_commit_close(commit);
262 return err;;
265 *pentry = alloc_commit_queue_entry(commit, id);
266 if (*pentry == NULL) {
267 err = got_error_from_errno();
268 got_object_commit_close(commit);
271 return err;;
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;
280 *head_id = NULL;
282 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
283 if (err)
284 return err;
286 err = got_ref_resolve(head_id, repo, head_ref);
287 got_ref_close(head_ref);
288 if (err) {
289 *head_id = NULL;
290 return err;
293 return NULL;
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;
307 *ncommits = 0;
309 err = got_object_open(&first_obj, repo, first_id);
310 if (err)
311 goto done;
312 if (got_object_get_type(first_obj) != GOT_OBJ_TYPE_COMMIT) {
313 err = got_error(GOT_ERR_OBJ_TYPE);
314 goto done;
316 err = got_object_open(&last_obj, repo, last_id);
317 if (err)
318 goto done;
319 if (got_object_get_type(last_obj) != GOT_OBJ_TYPE_COMMIT) {
320 err = got_error(GOT_ERR_OBJ_TYPE);
321 goto done;
324 err = got_object_commit_open(&commit, repo, first_obj);
325 if (err)
326 goto done;
328 id = got_object_id_dup(first_id);
329 if (id == NULL) {
330 err = got_error_from_errno();
331 goto done;
334 entry = alloc_commit_queue_entry(commit, id);
335 if (entry == NULL)
336 return got_error_from_errno();
338 old_head_entry = TAILQ_FIRST(commits);
339 if (old_head_entry)
340 TAILQ_INSERT_BEFORE(old_head_entry, entry, entry);
341 else
342 TAILQ_INSERT_HEAD(commits, entry, entry);
344 *ncommits = 1;
346 /*
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.
350 */
351 while (1) {
352 struct commit_queue_entry *pentry;
354 err = fetch_parent_commit(&pentry, entry, repo);
355 if (err)
356 goto done;
357 if (pentry == NULL)
358 break;
360 /*
361 * Fill up to old HEAD commit if commit queue was not empty.
362 * We must not leave a gap in history.
363 */
364 if (old_head_entry &&
365 got_object_id_cmp(pentry->id, old_head_entry->id) == 0)
366 break;
368 TAILQ_INSERT_AFTER(commits, entry, pentry, entry);
369 (*ncommits)++;
370 if (*ncommits >= limit)
371 break;
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)
376 break;
378 entry = pentry;
381 done:
382 if (first_obj)
383 got_object_close(first_obj);
384 if (last_obj)
385 got_object_close(last_obj);
386 return err;
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;
396 int ncommits = 0;
397 struct got_object_id *head_id = NULL;
399 *start_entry = NULL;
401 err = get_head_commit_id(&head_id, repo);
402 if (err)
403 return err;
405 /* Prepend HEAD commit and all ancestors up to start commit. */
406 err = prepend_commits(&ncommits, commits, head_id, start_id, limit,
407 repo);
408 if (err)
409 return err;
411 if (got_object_id_cmp(head_id, start_id) == 0)
412 *start_entry = TAILQ_FIRST(commits);
413 else
414 *start_entry = TAILQ_LAST(commits, commit_queue);
416 if (ncommits >= limit)
417 return NULL;
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);
425 if (err)
426 break;
427 if (pentry)
428 TAILQ_INSERT_TAIL(commits, pentry, entry);
429 entry = pentry;
430 ncommits++;
433 if (err)
434 *start_entry = NULL;
435 return err;
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;
444 int ncommits = 0;
446 wclear(tog_log_view.window);
448 entry = first;
449 *last = first;
450 while (entry) {
451 if (ncommits == limit)
452 break;
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);
458 if (err)
459 break;
460 ncommits++;
461 *last = entry;
462 entry = TAILQ_NEXT(entry, entry);
465 update_panels();
466 doupdate();
468 return err;
471 static void
472 scroll_up(struct commit_queue_entry **first_displayed_entry, int n,
473 struct commit_queue *commits)
475 struct commit_queue_entry *entry;
476 int nscrolled = 0;
478 entry = TAILQ_FIRST(commits);
479 if (*first_displayed_entry == entry)
480 return;
482 entry = *first_displayed_entry;
483 while (entry && nscrolled < n) {
484 entry = TAILQ_PREV(entry, commit_queue, entry);
485 if (entry) {
486 *first_displayed_entry = entry;
487 nscrolled++;
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;
499 int nscrolled = 0;
501 if (last_displayed_entry->commit->nparents == 0)
502 return NULL;
504 entry = *first_displayed_entry;
505 do {
506 struct commit_queue_entry *pentry;
508 pentry = TAILQ_NEXT(entry, entry);
509 if (pentry == NULL) {
510 err = fetch_parent_commit(&pentry, entry, repo);
511 if (err)
512 break;
513 if (pentry == NULL) {
514 *first_displayed_entry = entry;
515 return NULL;
517 TAILQ_INSERT_TAIL(commits, pentry, entry);
518 last_displayed_entry = pentry;
521 *first_displayed_entry = pentry;
522 entry = pentry;
524 if (TAILQ_LAST(commits, commit_queue) == last_displayed_entry) {
525 err = fetch_parent_commit(&pentry, last_displayed_entry,
526 repo);
527 if (err)
528 break;
529 if (pentry) {
530 TAILQ_INSERT_TAIL(commits, pentry, entry);
531 last_displayed_entry = pentry;
534 } while (++nscrolled < n);
536 return NULL;
539 static int
540 num_parents(struct commit_queue_entry *entry)
542 int nparents = 0;
544 while (entry) {
545 entry = TAILQ_NEXT(entry, entry);
546 nparents++;
549 return nparents;
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);
563 if (id == NULL)
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);
580 if (err)
581 goto done;
582 while (!done) {
583 err = draw_commits(&last_displayed_entry, first_displayed_entry,
584 selected, LINES);
585 if (err)
586 goto done;
588 nodelay(stdscr, FALSE);
589 ch = wgetch(tog_log_view.window);
590 switch (ch) {
591 case ERR:
592 if (errno) {
593 err = got_error_from_errno();
594 goto done;
596 break;
597 case 'q':
598 done = 1;
599 break;
600 case 'k':
601 case KEY_UP:
602 if (selected > 0)
603 selected--;
604 if (selected > 0)
605 break;
606 scroll_up(&first_displayed_entry, 1, &commits);
607 break;
608 case KEY_PPAGE:
609 if (TAILQ_FIRST(&commits) ==
610 first_displayed_entry) {
611 selected = 0;
612 break;
614 scroll_up(&first_displayed_entry, LINES,
615 &commits);
616 break;
617 case 'j':
618 case KEY_DOWN:
619 nparents = num_parents(first_displayed_entry);
620 if (selected < LINES - 1 &&
621 selected < nparents - 1)
622 selected++;
623 if (selected < LINES - 1 &&
624 selected < nparents - 1)
625 break;
626 err = scroll_down(&first_displayed_entry, 1,
627 last_displayed_entry, &commits, repo);
628 if (err)
629 goto done;
630 break;
631 case KEY_NPAGE:
632 nparents = num_parents(first_displayed_entry);
633 if (nparents < LINES - 1 &&
634 selected < nparents - 1) {
635 selected = nparents - 1;
636 break;
638 err = scroll_down(&first_displayed_entry, LINES,
639 last_displayed_entry, &commits, repo);
640 if (err)
641 goto done;
642 nparents = num_parents(first_displayed_entry);
643 if (selected > nparents)
644 selected = nparents - 1;
645 break;
646 case KEY_RESIZE:
647 if (selected > LINES)
648 selected = LINES - 1;
649 break;
650 default:
651 break;
653 nodelay(stdscr, TRUE);
655 done:
656 free_commits(&commits);
657 return err;
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;
668 int ch;
670 #ifndef PROFILE
671 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
672 err(1, "pledge");
673 #endif
675 while ((ch = getopt(argc, argv, "c:")) != -1) {
676 switch (ch) {
677 case 'c':
678 start_commit = optarg;
679 break;
680 default:
681 usage();
682 /* NOTREACHED */
686 argc -= optind;
687 argv += optind;
689 if (argc == 0) {
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();
697 } else
698 usage_log();
700 error = got_repo_open(&repo, repo_path);
701 free(repo_path);
702 if (error != NULL)
703 return error;
705 if (start_commit == NULL) {
706 error = get_head_commit_id(&start_id, repo);
707 if (error != NULL)
708 return error;
709 } else {
710 struct got_object *obj;
711 error = got_object_open_by_id_str(&obj, repo, start_commit);
712 if (error == NULL) {
713 start_id = got_object_get_id(obj);
714 if (start_id == NULL)
715 error = got_error_from_errno();
718 if (error != NULL)
719 return error;
720 error = show_log_view(start_id, repo);
721 free(start_id);
722 got_repo_close(repo);
723 return error;
726 __dead static void
727 usage_diff(void)
729 endwin();
730 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
731 getprogname());
732 exit(1);
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;
741 rewind(f);
742 wclear(tog_diff_view.window);
744 *eof = 0;
745 while (nprinted < max_lines) {
746 char *line;
747 size_t lineno;
748 size_t linelen;
749 const char delim[3] = { '\0', '\0', '\0'};
751 line = fparseln(f, &linelen, &lineno, delim, 0);
752 if (line == NULL) {
753 *eof = 1;
754 break;
756 if (++nlines < *first_displayed_line) {
757 free(line);
758 continue;
761 if (linelen > COLS - 1)
762 line[COLS - 1] = '\0';
763 waddstr(tog_diff_view.window, line);
764 waddch(tog_diff_view.window, '\n');
765 if (++nprinted == 1)
766 *first_displayed_line = nlines;
767 free(line);
769 *last_displayed_line = nlines;
771 update_panels();
772 doupdate();
774 return NULL;
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;
782 FILE *f;
783 int ch, done = 0, first_displayed_line = 1, last_displayed_line = LINES;
784 int eof;
786 if (got_object_get_type(obj1) != got_object_get_type(obj2))
787 return got_error(GOT_ERR_OBJ_TYPE);
789 f = got_opentemp();
790 if (f == NULL)
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);
796 break;
797 case GOT_OBJ_TYPE_TREE:
798 err = got_diff_objects_as_trees(obj1, obj2, repo, f);
799 break;
800 case GOT_OBJ_TYPE_COMMIT:
801 err = got_diff_objects_as_commits(obj1, obj2, repo, f);
802 break;
803 default:
804 return got_error(GOT_ERR_OBJ_TYPE);
807 fflush(f);
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();
819 } else
820 show_panel(tog_diff_view.panel);
822 while (!done) {
823 err = draw_diff(f, &first_displayed_line, &last_displayed_line,
824 &eof, LINES);
825 if (err)
826 break;
827 nodelay(stdscr, FALSE);
828 ch = wgetch(tog_diff_view.window);
829 switch (ch) {
830 case 'q':
831 done = 1;
832 break;
833 case 'k':
834 case KEY_UP:
835 if (first_displayed_line > 1)
836 first_displayed_line--;
837 break;
838 case 'j':
839 case KEY_DOWN:
840 if (!eof)
841 first_displayed_line++;
842 break;
843 default:
844 break;
846 nodelay(stdscr, TRUE);
848 fclose(f);
849 return err;
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;
860 int ch;
862 #ifndef PROFILE
863 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
864 err(1, "pledge");
865 #endif
867 while ((ch = getopt(argc, argv, "")) != -1) {
868 switch (ch) {
869 default:
870 usage();
871 /* NOTREACHED */
875 argc -= optind;
876 argv += optind;
878 if (argc == 0) {
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];
892 } else
893 usage_diff();
895 error = got_repo_open(&repo, repo_path);
896 free(repo_path);
897 if (error)
898 goto done;
900 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
901 if (error)
902 goto done;
904 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
905 if (error)
906 goto done;
908 error = show_diff_view(obj1, obj2, repo);
909 done:
910 got_repo_close(repo);
911 if (obj1)
912 got_object_close(obj1);
913 if (obj2)
914 got_object_close(obj2);
915 return error;
918 __dead static void
919 usage_blame(void)
921 endwin();
922 fprintf(stderr, "usage: %s blame [repository-path] blob-object\n",
923 getprogname());
924 exit(1);
927 static const struct got_error *
928 cmd_blame(int argc, char *argv[])
930 return got_error(GOT_ERR_NOT_IMPL);
933 static void
934 init_curses(void)
936 initscr();
937 cbreak();
938 noecho();
939 nonl();
940 intrflush(stdscr, FALSE);
941 keypad(stdscr, TRUE);
942 curs_set(0);
945 __dead static void
946 usage(void)
948 int i;
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);
956 exit(1);
959 static char **
960 make_argv(const char *arg0, const char *arg1)
962 char **argv;
963 int argc = (arg1 == NULL ? 1 : 2);
965 argv = calloc(argc, sizeof(char *));
966 if (argv == NULL)
967 err(1, "calloc");
968 argv[0] = strdup(arg0);
969 if (argv[0] == NULL)
970 err(1, "calloc");
971 if (arg1) {
972 argv[1] = strdup(arg1);
973 if (argv[1] == NULL)
974 err(1, "calloc");
977 return argv;
980 int
981 main(int argc, char *argv[])
983 const struct got_error *error = NULL;
984 struct tog_cmd *cmd = NULL;
985 int ch, hflag = 0;
986 char **cmd_argv = NULL;
988 setlocale(LC_ALL, "");
990 while ((ch = getopt(argc, argv, "h")) != -1) {
991 switch (ch) {
992 case 'h':
993 hflag = 1;
994 break;
995 default:
996 usage();
997 /* NOTREACHED */
1001 argc -= optind;
1002 argv += optind;
1003 optind = 0;
1004 optreset = 1;
1006 if (argc == 0) {
1007 /* Build an argument vector which runs a default command. */
1008 cmd = &tog_commands[0];
1009 cmd_argv = make_argv(cmd->name, NULL);
1010 argc = 1;
1011 } else {
1012 int i;
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];
1019 if (hflag)
1020 tog_commands[i].cmd_usage();
1021 break;
1024 if (cmd == NULL) {
1025 /* Did the user specify a repository? */
1026 char *repo_path = realpath(argv[0], NULL);
1027 if (repo_path) {
1028 struct got_repository *repo;
1029 error = got_repo_open(&repo, repo_path);
1030 if (error == NULL)
1031 got_repo_close(repo);
1032 } else
1033 error = got_error_from_errno();
1034 if (error) {
1035 fprintf(stderr, "%s: '%s' is neither a known "
1036 "command nor a path to a repository\n",
1037 getprogname(), argv[0]);
1038 free(repo_path);
1039 return 1;
1041 cmd = &tog_commands[0];
1042 cmd_argv = make_argv(cmd->name, repo_path);
1043 argc = 2;
1044 free(repo_path);
1048 init_curses();
1050 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
1051 if (error)
1052 goto done;
1053 done:
1054 endwin();
1055 free(cmd_argv);
1056 if (error)
1057 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
1058 return 0;