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 enum tog_view_id {
48 TOG_VIEW_LOG,
49 TOG_VIEW_DIFF,
50 TOG_VIEW_BLAME,
51 };
53 struct tog_cmd {
54 const char *name;
55 const struct got_error *(*cmd_main)(int, char *[]);
56 void (*cmd_usage)(void);
57 enum tog_view_id view;
58 const char *descr;
59 };
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" },
77 };
79 /* globals */
80 static struct tog_log_view {
81 WINDOW *window;
82 PANEL *panel;
83 } tog_log_view;
84 static struct tog_diff_view {
85 WINDOW *window;
86 PANEL *panel;
87 } tog_diff_view;
89 __dead void
90 usage_log(void)
91 {
92 endwin();
93 fprintf(stderr, "usage: %s log [-c commit] [repository-path]\n",
94 getprogname());
95 exit(1);
96 }
98 static const struct got_error *
99 draw_commit(struct got_commit_object *commit, struct got_object_id *id)
101 const struct got_error *err = NULL;
102 char *logmsg0 = NULL, *logmsg = NULL;
103 char *author0 = NULL, *author = NULL;
104 char *newline, *smallerthan;
105 char *line = NULL;
106 char *id_str = NULL;
107 const size_t id_display_len = 8;
108 const size_t author_display_len = 16;
109 size_t id_len, author_len, logmsg_len, avail;
110 int i, col;
112 err = got_object_id_str(&id_str, id);
113 if (err)
114 return err;
115 id_len = strlen(id_str);
117 logmsg0 = strdup(commit->logmsg);
118 if (logmsg0 == NULL) {
119 err = got_error_from_errno();
120 goto done;
122 logmsg = logmsg0;
123 while (*logmsg == '\n')
124 logmsg++;
125 newline = strchr(logmsg, '\n');
126 if (newline)
127 *newline = '\0';
128 logmsg_len = strlen(logmsg);
130 author0 = strdup(commit->author);
131 if (author0 == NULL) {
132 err = got_error_from_errno();
133 goto done;
135 author = author0;
136 smallerthan = strchr(author, '<');
137 if (smallerthan)
138 *smallerthan = '\0';
139 else {
140 char *at = strchr(author, '@');
141 if (at)
142 *at = '\0';
144 author_len = strlen(author);
146 avail = COLS - 1;
147 line = calloc(avail + 1, sizeof(*line));
148 if (line == NULL) {
149 err = got_error_from_errno();
150 goto done;
153 col = 0;
154 for (i = 0; i < MIN(id_display_len, id_len); i++) {
155 if (col >= avail)
156 goto draw;
157 line[col++] = id_str[i];
159 while (i < id_display_len) {
160 if (col >= avail)
161 goto draw;
162 line[col++] = ' ';
163 i++;
165 if (col >= avail)
166 goto draw;
167 line[col++] = ' ';
168 for (i = 0; i < MIN(author_display_len, author_len); i++) {
169 if (col >= avail)
170 goto draw;
171 line[col++] = author[i];
173 while (i < author_display_len) {
174 if (col >= avail)
175 goto draw;
176 line[col++] = ' ';
177 i++;
179 if (col >= avail)
180 goto draw;
181 line[col++] = ' ';
183 while (col < avail && *logmsg)
184 line[col++] = *logmsg++;
185 while (col < avail)
186 line[col++] = ' ';
187 draw:
188 waddstr(tog_log_view.window, line);
189 waddch(tog_log_view.window, '\n');
190 done:
191 free(logmsg0);
192 free(author0);
193 free(line);
194 free(id_str);
195 return err;
198 struct commit_queue_entry {
199 TAILQ_ENTRY(commit_queue_entry) entry;
200 struct got_object_id *id;
201 struct got_commit_object *commit;
202 };
203 TAILQ_HEAD(commit_queue, commit_queue_entry);
205 static struct commit_queue_entry *
206 alloc_commit_queue_entry(struct got_commit_object *commit,
207 struct got_object_id *id)
209 struct commit_queue_entry *entry;
211 entry = calloc(1, sizeof(*entry));
212 if (entry == NULL)
213 return NULL;
215 entry->id = id;
216 entry->commit = commit;
217 return entry;
220 static void
221 pop_commit(struct commit_queue *commits)
223 struct commit_queue_entry *entry;
225 entry = TAILQ_FIRST(commits);
226 TAILQ_REMOVE(commits, entry, entry);
227 got_object_commit_close(entry->commit);
228 free(entry->id);
229 free(entry);
232 static void
233 free_commits(struct commit_queue *commits)
235 while (!TAILQ_EMPTY(commits))
236 pop_commit(commits);
240 static const struct got_error *
241 fetch_parent_commit(struct commit_queue_entry **pentry,
242 struct commit_queue_entry *entry, struct got_repository *repo)
244 const struct got_error *err = NULL;
245 struct got_object *obj = NULL;
246 struct got_commit_object *commit;
247 struct got_object_id *id;
248 struct got_parent_id *pid;
250 *pentry = NULL;
252 /* Follow the first parent (TODO: handle merge commits). */
253 pid = SIMPLEQ_FIRST(&entry->commit->parent_ids);
254 if (pid == NULL)
255 return NULL;
256 err = got_object_open(&obj, repo, pid->id);
257 if (err)
258 return err;
259 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
260 err = got_error(GOT_ERR_OBJ_TYPE);
261 got_object_close(obj);
262 return err;
265 err = got_object_commit_open(&commit, repo, obj);
266 got_object_close(obj);
267 if (err)
268 return err;
270 id = got_object_id_dup(pid->id);
271 if (id == NULL) {
272 err = got_error_from_errno();
273 got_object_commit_close(commit);
274 return err;;
277 *pentry = alloc_commit_queue_entry(commit, id);
278 if (*pentry == NULL) {
279 err = got_error_from_errno();
280 got_object_commit_close(commit);
283 return err;;
286 static const struct got_error *
287 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
289 const struct got_error *err = NULL;
290 struct got_reference *head_ref;
292 *head_id = NULL;
294 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
295 if (err)
296 return err;
298 err = got_ref_resolve(head_id, repo, head_ref);
299 got_ref_close(head_ref);
300 if (err) {
301 *head_id = NULL;
302 return err;
305 return NULL;
308 static const struct got_error *
309 prepend_commits(int *ncommits, struct commit_queue *commits,
310 struct got_object_id *first_id, struct got_object_id *last_id,
311 int limit, struct got_repository *repo)
313 const struct got_error *err = NULL;
314 struct got_object *first_obj = NULL, *last_obj = NULL;
315 struct got_commit_object *commit = NULL;
316 struct got_object_id *id = NULL;
317 struct commit_queue_entry *entry, *old_head_entry;
319 *ncommits = 0;
321 err = got_object_open(&first_obj, repo, first_id);
322 if (err)
323 goto done;
324 if (got_object_get_type(first_obj) != GOT_OBJ_TYPE_COMMIT) {
325 err = got_error(GOT_ERR_OBJ_TYPE);
326 goto done;
328 err = got_object_open(&last_obj, repo, last_id);
329 if (err)
330 goto done;
331 if (got_object_get_type(last_obj) != GOT_OBJ_TYPE_COMMIT) {
332 err = got_error(GOT_ERR_OBJ_TYPE);
333 goto done;
336 err = got_object_commit_open(&commit, repo, first_obj);
337 if (err)
338 goto done;
340 id = got_object_id_dup(first_id);
341 if (id == NULL) {
342 err = got_error_from_errno();
343 goto done;
346 entry = alloc_commit_queue_entry(commit, id);
347 if (entry == NULL)
348 return got_error_from_errno();
350 old_head_entry = TAILQ_FIRST(commits);
351 if (old_head_entry)
352 TAILQ_INSERT_BEFORE(old_head_entry, entry, entry);
353 else
354 TAILQ_INSERT_HEAD(commits, entry, entry);
356 *ncommits = 1;
358 /*
359 * Fetch parent commits.
360 * XXX If first and last commit aren't ancestrally related this loop
361 * we will keep iterating until a root commit is encountered.
362 */
363 while (1) {
364 struct commit_queue_entry *pentry;
366 err = fetch_parent_commit(&pentry, entry, repo);
367 if (err)
368 goto done;
369 if (pentry == NULL)
370 break;
372 /*
373 * Fill up to old HEAD commit if commit queue was not empty.
374 * We must not leave a gap in history.
375 */
376 if (old_head_entry &&
377 got_object_id_cmp(pentry->id, old_head_entry->id) == 0)
378 break;
380 TAILQ_INSERT_AFTER(commits, entry, pentry, entry);
381 (*ncommits)++;
382 if (*ncommits >= limit)
383 break;
385 /* Fill up to last requested commit if queue was empty. */
386 if (old_head_entry == NULL &&
387 got_object_id_cmp(pentry->id, last_id) == 0)
388 break;
390 entry = pentry;
393 done:
394 if (first_obj)
395 got_object_close(first_obj);
396 if (last_obj)
397 got_object_close(last_obj);
398 return err;
401 static const struct got_error *
402 fetch_commits(struct commit_queue_entry **start_entry,
403 struct got_object_id *start_id, struct commit_queue *commits,
404 int limit, struct got_repository *repo)
406 const struct got_error *err;
407 struct commit_queue_entry *entry;
408 int ncommits = 0;
409 struct got_object_id *head_id = NULL;
411 *start_entry = NULL;
413 err = get_head_commit_id(&head_id, repo);
414 if (err)
415 return err;
417 /* Prepend HEAD commit and all ancestors up to start commit. */
418 err = prepend_commits(&ncommits, commits, head_id, start_id, limit,
419 repo);
420 if (err)
421 return err;
423 if (got_object_id_cmp(head_id, start_id) == 0)
424 *start_entry = TAILQ_FIRST(commits);
425 else
426 *start_entry = TAILQ_LAST(commits, commit_queue);
428 if (ncommits >= limit)
429 return NULL;
431 /* Append more commits from start commit up to the requested limit. */
432 entry = TAILQ_LAST(commits, commit_queue);
433 while (entry && ncommits < limit) {
434 struct commit_queue_entry *pentry;
436 err = fetch_parent_commit(&pentry, entry, repo);
437 if (err)
438 break;
439 if (pentry)
440 TAILQ_INSERT_TAIL(commits, pentry, entry);
441 entry = pentry;
442 ncommits++;
445 if (err)
446 *start_entry = NULL;
447 return err;
450 static const struct got_error *
451 draw_commits(struct commit_queue_entry **last, struct commit_queue_entry *first,
452 int selected, int limit)
454 const struct got_error *err = NULL;
455 struct commit_queue_entry *entry;
456 int ncommits = 0;
458 wclear(tog_log_view.window);
460 entry = first;
461 *last = first;
462 while (entry) {
463 if (ncommits == limit)
464 break;
465 if (ncommits == selected)
466 wstandout(tog_log_view.window);
467 err = draw_commit(entry->commit, entry->id);
468 if (ncommits == selected)
469 wstandend(tog_log_view.window);
470 if (err)
471 break;
472 ncommits++;
473 *last = entry;
474 entry = TAILQ_NEXT(entry, entry);
477 update_panels();
478 doupdate();
480 return err;
483 static void
484 scroll_up(struct commit_queue_entry **first_displayed_entry, int n,
485 struct commit_queue *commits)
487 struct commit_queue_entry *entry;
488 int nscrolled = 0;
490 entry = TAILQ_FIRST(commits);
491 if (*first_displayed_entry == entry)
492 return;
494 entry = *first_displayed_entry;
495 while (entry && nscrolled < n) {
496 entry = TAILQ_PREV(entry, commit_queue, entry);
497 if (entry) {
498 *first_displayed_entry = entry;
499 nscrolled++;
504 static const struct got_error *
505 scroll_down(struct commit_queue_entry **first_displayed_entry, int n,
506 struct commit_queue_entry *last_displayed_entry,
507 struct commit_queue *commits, struct got_repository *repo)
509 const struct got_error *err = NULL;
510 struct commit_queue_entry *entry;
511 int nscrolled = 0;
513 if (last_displayed_entry->commit->nparents == 0)
514 return NULL;
516 entry = *first_displayed_entry;
517 do {
518 struct commit_queue_entry *pentry;
520 pentry = TAILQ_NEXT(entry, entry);
521 if (pentry == NULL) {
522 err = fetch_parent_commit(&pentry, entry, repo);
523 if (err)
524 break;
525 if (pentry == NULL) {
526 *first_displayed_entry = entry;
527 return NULL;
529 TAILQ_INSERT_TAIL(commits, pentry, entry);
530 last_displayed_entry = pentry;
533 *first_displayed_entry = pentry;
534 entry = pentry;
536 if (TAILQ_LAST(commits, commit_queue) == last_displayed_entry) {
537 err = fetch_parent_commit(&pentry, last_displayed_entry,
538 repo);
539 if (err)
540 break;
541 if (pentry) {
542 TAILQ_INSERT_TAIL(commits, pentry, entry);
543 last_displayed_entry = pentry;
546 } while (++nscrolled < n);
548 return NULL;
551 static int
552 num_parents(struct commit_queue_entry *entry)
554 int nparents = 0;
556 while (entry) {
557 entry = TAILQ_NEXT(entry, entry);
558 nparents++;
561 return nparents;
564 static const struct got_error *
565 show_log_view(struct got_object_id *start_id, struct got_repository *repo)
567 const struct got_error *err = NULL;
568 struct got_object_id *id;
569 int ch, done = 0, selected = 0, nparents;
570 struct commit_queue commits;
571 struct commit_queue_entry *first_displayed_entry = NULL;
572 struct commit_queue_entry *last_displayed_entry = NULL;
574 id = got_object_id_dup(start_id);
575 if (id == NULL)
576 return got_error_from_errno();
578 if (tog_log_view.window == NULL) {
579 tog_log_view.window = newwin(0, 0, 0, 0);
580 if (tog_log_view.window == NULL)
581 return got_error_from_errno();
582 keypad(tog_log_view.window, TRUE);
584 if (tog_log_view.panel == NULL) {
585 tog_log_view.panel = new_panel(tog_log_view.window);
586 if (tog_log_view.panel == NULL)
587 return got_error_from_errno();
590 TAILQ_INIT(&commits);
591 err = fetch_commits(&first_displayed_entry, id, &commits, LINES, repo);
592 if (err)
593 goto done;
594 while (!done) {
595 err = draw_commits(&last_displayed_entry, first_displayed_entry,
596 selected, LINES);
597 if (err)
598 goto done;
600 nodelay(stdscr, FALSE);
601 ch = wgetch(tog_log_view.window);
602 switch (ch) {
603 case ERR:
604 if (errno) {
605 err = got_error_from_errno();
606 goto done;
608 break;
609 case 'q':
610 done = 1;
611 break;
612 case 'k':
613 case KEY_UP:
614 if (selected > 0)
615 selected--;
616 if (selected > 0)
617 break;
618 scroll_up(&first_displayed_entry, 1, &commits);
619 break;
620 case KEY_PPAGE:
621 if (TAILQ_FIRST(&commits) ==
622 first_displayed_entry) {
623 selected = 0;
624 break;
626 scroll_up(&first_displayed_entry, LINES,
627 &commits);
628 break;
629 case 'j':
630 case KEY_DOWN:
631 nparents = num_parents(first_displayed_entry);
632 if (selected < LINES - 1 &&
633 selected < nparents - 1)
634 selected++;
635 if (selected < LINES - 1 &&
636 selected < nparents - 1)
637 break;
638 err = scroll_down(&first_displayed_entry, 1,
639 last_displayed_entry, &commits, repo);
640 if (err)
641 goto done;
642 break;
643 case KEY_NPAGE:
644 nparents = num_parents(first_displayed_entry);
645 if (nparents < LINES - 1 &&
646 selected < nparents - 1) {
647 selected = nparents - 1;
648 break;
650 err = scroll_down(&first_displayed_entry, LINES,
651 last_displayed_entry, &commits, repo);
652 if (err)
653 goto done;
654 nparents = num_parents(first_displayed_entry);
655 if (selected > nparents)
656 selected = nparents - 1;
657 break;
658 case KEY_RESIZE:
659 if (selected > LINES)
660 selected = LINES - 1;
661 break;
662 default:
663 break;
665 nodelay(stdscr, TRUE);
667 done:
668 free_commits(&commits);
669 return err;
672 const struct got_error *
673 cmd_log(int argc, char *argv[])
675 const struct got_error *error;
676 struct got_repository *repo;
677 struct got_object_id *start_id = NULL;
678 char *repo_path = NULL;
679 char *start_commit = NULL;
680 int ch;
682 #ifndef PROFILE
683 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
684 err(1, "pledge");
685 #endif
687 while ((ch = getopt(argc, argv, "c:")) != -1) {
688 switch (ch) {
689 case 'c':
690 start_commit = optarg;
691 break;
692 default:
693 usage();
694 /* NOTREACHED */
698 argc -= optind;
699 argv += optind;
701 if (argc == 0) {
702 repo_path = getcwd(NULL, 0);
703 if (repo_path == NULL)
704 return got_error_from_errno();
705 } else if (argc == 1) {
706 repo_path = realpath(argv[0], NULL);
707 if (repo_path == NULL)
708 return got_error_from_errno();
709 } else
710 usage_log();
712 error = got_repo_open(&repo, repo_path);
713 free(repo_path);
714 if (error != NULL)
715 return error;
717 if (start_commit == NULL) {
718 error = get_head_commit_id(&start_id, repo);
719 if (error != NULL)
720 return error;
721 } else {
722 struct got_object *obj;
723 error = got_object_open_by_id_str(&obj, repo, start_commit);
724 if (error == NULL) {
725 start_id = got_object_get_id(obj);
726 if (start_id == NULL)
727 error = got_error_from_errno();
730 if (error != NULL)
731 return error;
732 error = show_log_view(start_id, repo);
733 free(start_id);
734 got_repo_close(repo);
735 return error;
738 __dead void
739 usage_diff(void)
741 endwin();
742 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
743 getprogname());
744 exit(1);
747 const struct got_error *
748 draw_diff(FILE *f, int *first_displayed_line, int *last_displayed_line,
749 int *eof, int max_lines)
751 int nlines = 0, nprinted = 0;
753 rewind(f);
754 wclear(tog_diff_view.window);
756 *eof = 0;
757 while (nprinted < max_lines) {
758 char *line;
759 size_t lineno;
760 size_t linelen;
761 const char delim[3] = { '\0', '\0', '\0'};
763 line = fparseln(f, &linelen, &lineno, delim, 0);
764 if (line == NULL) {
765 *eof = 1;
766 break;
768 if (++nlines < *first_displayed_line) {
769 free(line);
770 continue;
773 if (linelen > COLS - 1)
774 line[COLS - 1] = '\0';
775 waddstr(tog_diff_view.window, line);
776 waddch(tog_diff_view.window, '\n');
777 if (++nprinted == 1)
778 *first_displayed_line = nlines;
779 free(line);
781 *last_displayed_line = nlines;
783 update_panels();
784 doupdate();
786 return NULL;
789 const struct got_error *
790 show_diff_view(struct got_object *obj1, struct got_object *obj2,
791 struct got_repository *repo)
793 const struct got_error *err;
794 FILE *f;
795 int ch, done = 0, first_displayed_line = 1, last_displayed_line = LINES;
796 int eof;
798 if (got_object_get_type(obj1) != got_object_get_type(obj2))
799 return got_error(GOT_ERR_OBJ_TYPE);
801 f = got_opentemp();
802 if (f == NULL)
803 return got_error_from_errno();
805 switch (got_object_get_type(obj1)) {
806 case GOT_OBJ_TYPE_BLOB:
807 err = got_diff_objects_as_blobs(obj1, obj2, repo, f);
808 break;
809 case GOT_OBJ_TYPE_TREE:
810 err = got_diff_objects_as_trees(obj1, obj2, repo, f);
811 break;
812 case GOT_OBJ_TYPE_COMMIT:
813 err = got_diff_objects_as_commits(obj1, obj2, repo, f);
814 break;
815 default:
816 return got_error(GOT_ERR_OBJ_TYPE);
819 fflush(f);
821 if (tog_diff_view.window == NULL) {
822 tog_diff_view.window = newwin(0, 0, 0, 0);
823 if (tog_diff_view.window == NULL)
824 return got_error_from_errno();
825 keypad(tog_diff_view.window, TRUE);
827 if (tog_diff_view.panel == NULL) {
828 tog_diff_view.panel = new_panel(tog_diff_view.window);
829 if (tog_diff_view.panel == NULL)
830 return got_error_from_errno();
831 } else
832 show_panel(tog_diff_view.panel);
834 while (!done) {
835 err = draw_diff(f, &first_displayed_line, &last_displayed_line,
836 &eof, LINES);
837 if (err)
838 break;
839 nodelay(stdscr, FALSE);
840 ch = wgetch(tog_diff_view.window);
841 switch (ch) {
842 case 'q':
843 done = 1;
844 break;
845 case 'k':
846 case KEY_UP:
847 if (first_displayed_line > 1)
848 first_displayed_line--;
849 break;
850 case 'j':
851 case KEY_DOWN:
852 if (!eof)
853 first_displayed_line++;
854 break;
855 default:
856 break;
858 nodelay(stdscr, TRUE);
860 fclose(f);
861 return err;
864 const struct got_error *
865 cmd_diff(int argc, char *argv[])
867 const struct got_error *error = NULL;
868 struct got_repository *repo = NULL;
869 struct got_object *obj1 = NULL, *obj2 = NULL;
870 char *repo_path = NULL;
871 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
872 int ch;
874 #ifndef PROFILE
875 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
876 err(1, "pledge");
877 #endif
879 while ((ch = getopt(argc, argv, "")) != -1) {
880 switch (ch) {
881 default:
882 usage();
883 /* NOTREACHED */
887 argc -= optind;
888 argv += optind;
890 if (argc == 0) {
891 usage_diff(); /* TODO show local worktree changes */
892 } else if (argc == 2) {
893 repo_path = getcwd(NULL, 0);
894 if (repo_path == NULL)
895 return got_error_from_errno();
896 obj_id_str1 = argv[0];
897 obj_id_str2 = argv[1];
898 } else if (argc == 3) {
899 repo_path = realpath(argv[0], NULL);
900 if (repo_path == NULL)
901 return got_error_from_errno();
902 obj_id_str1 = argv[1];
903 obj_id_str2 = argv[2];
904 } else
905 usage_diff();
907 error = got_repo_open(&repo, repo_path);
908 free(repo_path);
909 if (error)
910 goto done;
912 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
913 if (error)
914 goto done;
916 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
917 if (error)
918 goto done;
920 error = show_diff_view(obj1, obj2, repo);
921 done:
922 got_repo_close(repo);
923 if (obj1)
924 got_object_close(obj1);
925 if (obj2)
926 got_object_close(obj2);
927 return error;
930 __dead void
931 usage_blame(void)
933 endwin();
934 fprintf(stderr, "usage: %s blame [repository-path] blob-object\n",
935 getprogname());
936 exit(1);
939 const struct got_error *
940 cmd_blame(int argc, char *argv[])
942 return got_error(GOT_ERR_NOT_IMPL);
945 static void
946 init_curses(void)
948 initscr();
949 cbreak();
950 noecho();
951 nonl();
952 intrflush(stdscr, FALSE);
953 keypad(stdscr, TRUE);
954 curs_set(0);
957 __dead void
958 usage(void)
960 int i;
962 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
963 "Available commands:\n", getprogname());
964 for (i = 0; i < nitems(tog_commands); i++) {
965 struct tog_cmd *cmd = &tog_commands[i];
966 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
968 exit(1);
971 static char **
972 make_argv(const char *arg0, const char *arg1)
974 char **argv;
975 int argc = (arg1 == NULL ? 1 : 2);
977 argv = calloc(argc, sizeof(char *));
978 if (argv == NULL)
979 err(1, "calloc");
980 argv[0] = strdup(arg0);
981 if (argv[0] == NULL)
982 err(1, "calloc");
983 if (arg1) {
984 argv[1] = strdup(arg1);
985 if (argv[1] == NULL)
986 err(1, "calloc");
989 return argv;
992 int
993 main(int argc, char *argv[])
995 const struct got_error *error = NULL;
996 struct tog_cmd *cmd = NULL;
997 int ch, hflag = 0;
998 char **cmd_argv = NULL;
1000 setlocale(LC_ALL, "");
1002 while ((ch = getopt(argc, argv, "h")) != -1) {
1003 switch (ch) {
1004 case 'h':
1005 hflag = 1;
1006 break;
1007 default:
1008 usage();
1009 /* NOTREACHED */
1013 argc -= optind;
1014 argv += optind;
1015 optind = 0;
1016 optreset = 1;
1018 if (argc == 0) {
1019 /* Build an argument vector which runs a default command. */
1020 cmd = &tog_commands[0];
1021 cmd_argv = make_argv(cmd->name, NULL);
1022 argc = 1;
1023 } else {
1024 int i;
1026 /* Did the user specific a command? */
1027 for (i = 0; i < nitems(tog_commands); i++) {
1028 if (strncmp(tog_commands[i].name, argv[0],
1029 strlen(argv[0])) == 0) {
1030 cmd = &tog_commands[i];
1031 if (hflag)
1032 tog_commands[i].cmd_usage();
1033 break;
1036 if (cmd == NULL) {
1037 /* Did the user specify a repository? */
1038 char *repo_path = realpath(argv[0], NULL);
1039 if (repo_path) {
1040 struct got_repository *repo;
1041 error = got_repo_open(&repo, repo_path);
1042 if (error == NULL)
1043 got_repo_close(repo);
1044 } else
1045 error = got_error_from_errno();
1046 if (error) {
1047 fprintf(stderr, "%s: '%s' is neither a known "
1048 "command nor a path to a repository\n",
1049 getprogname(), argv[0]);
1050 free(repo_path);
1051 return 1;
1053 cmd = &tog_commands[0];
1054 cmd_argv = make_argv(cmd->name, repo_path);
1055 argc = 2;
1056 free(repo_path);
1060 init_curses();
1062 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
1063 if (error)
1064 goto done;
1065 done:
1066 endwin();
1067 free(cmd_argv);
1068 if (error)
1069 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
1070 return 0;