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_view {
81 WINDOW *window;
82 PANEL *panel;
83 } tog_log_view, tog_diff_view;
85 __dead void
86 usage_log(void)
87 {
88 endwin();
89 fprintf(stderr, "usage: %s log [-c commit] [repository-path]\n",
90 getprogname());
91 exit(1);
92 }
94 static const struct got_error *
95 draw_commit(struct got_commit_object *commit, struct got_object_id *id)
96 {
97 const struct got_error *err = NULL;
98 char *logmsg0 = NULL, *logmsg = NULL;
99 char *author0 = NULL, *author = NULL;
100 char *newline, *smallerthan;
101 char *line = NULL;
102 char *id_str = NULL;
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;
106 int i, col;
108 err = got_object_id_str(&id_str, id);
109 if (err)
110 return err;
111 id_len = strlen(id_str);
113 logmsg0 = strdup(commit->logmsg);
114 if (logmsg0 == NULL) {
115 err = got_error_from_errno();
116 goto done;
118 logmsg = logmsg0;
119 while (*logmsg == '\n')
120 logmsg++;
121 newline = strchr(logmsg, '\n');
122 if (newline)
123 *newline = '\0';
124 logmsg_len = strlen(logmsg);
126 author0 = strdup(commit->author);
127 if (author0 == NULL) {
128 err = got_error_from_errno();
129 goto done;
131 author = author0;
132 smallerthan = strchr(author, '<');
133 if (smallerthan)
134 *smallerthan = '\0';
135 else {
136 char *at = strchr(author, '@');
137 if (at)
138 *at = '\0';
140 author_len = strlen(author);
142 avail = COLS - 1;
143 line = calloc(avail + 1, sizeof(*line));
144 if (line == NULL) {
145 err = got_error_from_errno();
146 goto done;
149 col = 0;
150 for (i = 0; i < MIN(id_display_len, id_len); i++) {
151 if (col >= avail)
152 goto draw;
153 line[col++] = id_str[i];
155 while (i < id_display_len) {
156 if (col >= avail)
157 goto draw;
158 line[col++] = ' ';
159 i++;
161 if (col >= avail)
162 goto draw;
163 line[col++] = ' ';
164 for (i = 0; i < MIN(author_display_len, author_len); i++) {
165 if (col >= avail)
166 goto draw;
167 line[col++] = author[i];
169 while (i < author_display_len) {
170 if (col >= avail)
171 goto draw;
172 line[col++] = ' ';
173 i++;
175 if (col >= avail)
176 goto draw;
177 line[col++] = ' ';
179 while (col < avail && *logmsg)
180 line[col++] = *logmsg++;
181 while (col < avail)
182 line[col++] = ' ';
183 draw:
184 waddstr(tog_log_view.window, line);
185 waddch(tog_log_view.window, '\n');
186 done:
187 free(logmsg0);
188 free(author0);
189 free(line);
190 free(id_str);
191 return err;
194 struct commit_queue_entry {
195 TAILQ_ENTRY(commit_queue_entry) entry;
196 struct got_object_id *id;
197 struct got_commit_object *commit;
198 };
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));
208 if (entry == NULL)
209 return NULL;
211 entry->id = id;
212 entry->commit = commit;
213 return entry;
216 static void
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);
224 free(entry->id);
225 free(entry);
228 static void
229 free_commits(struct commit_queue *commits)
231 while (!TAILQ_EMPTY(commits))
232 pop_commit(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;
246 *pentry = NULL;
248 /* Follow the first parent (TODO: handle merge commits). */
249 pid = SIMPLEQ_FIRST(&entry->commit->parent_ids);
250 if (pid == NULL)
251 return NULL;
252 err = got_object_open(&obj, repo, pid->id);
253 if (err)
254 return err;
255 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
256 err = got_error(GOT_ERR_OBJ_TYPE);
257 got_object_close(obj);
258 return err;
261 err = got_object_commit_open(&commit, repo, obj);
262 got_object_close(obj);
263 if (err)
264 return err;
266 id = got_object_id_dup(pid->id);
267 if (id == NULL) {
268 err = got_error_from_errno();
269 got_object_commit_close(commit);
270 return err;;
273 *pentry = alloc_commit_queue_entry(commit, id);
274 if (*pentry == NULL) {
275 err = got_error_from_errno();
276 got_object_commit_close(commit);
279 return err;;
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;
288 *head_id = NULL;
290 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
291 if (err)
292 return err;
294 err = got_ref_resolve(head_id, repo, head_ref);
295 got_ref_close(head_ref);
296 if (err) {
297 *head_id = NULL;
298 return err;
301 return NULL;
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;
315 *ncommits = 0;
317 err = got_object_open(&first_obj, repo, first_id);
318 if (err)
319 goto done;
320 if (got_object_get_type(first_obj) != GOT_OBJ_TYPE_COMMIT) {
321 err = got_error(GOT_ERR_OBJ_TYPE);
322 goto done;
324 err = got_object_open(&last_obj, repo, last_id);
325 if (err)
326 goto done;
327 if (got_object_get_type(last_obj) != GOT_OBJ_TYPE_COMMIT) {
328 err = got_error(GOT_ERR_OBJ_TYPE);
329 goto done;
332 err = got_object_commit_open(&commit, repo, first_obj);
333 if (err)
334 goto done;
336 id = got_object_id_dup(first_id);
337 if (id == NULL) {
338 err = got_error_from_errno();
339 goto done;
342 entry = alloc_commit_queue_entry(commit, id);
343 if (entry == NULL)
344 return got_error_from_errno();
346 old_head_entry = TAILQ_FIRST(commits);
347 if (old_head_entry)
348 TAILQ_INSERT_BEFORE(old_head_entry, entry, entry);
349 else
350 TAILQ_INSERT_HEAD(commits, entry, entry);
352 *ncommits = 1;
354 /*
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.
358 */
359 while (1) {
360 struct commit_queue_entry *pentry;
362 err = fetch_parent_commit(&pentry, entry, repo);
363 if (err)
364 goto done;
365 if (pentry == NULL)
366 break;
368 /*
369 * Fill up to old HEAD commit if commit queue was not empty.
370 * We must not leave a gap in history.
371 */
372 if (old_head_entry &&
373 got_object_id_cmp(pentry->id, old_head_entry->id) == 0)
374 break;
376 TAILQ_INSERT_AFTER(commits, entry, pentry, entry);
377 (*ncommits)++;
378 if (*ncommits >= limit)
379 break;
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)
384 break;
386 entry = pentry;
389 done:
390 if (first_obj)
391 got_object_close(first_obj);
392 if (last_obj)
393 got_object_close(last_obj);
394 return err;
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;
404 int ncommits = 0;
405 struct got_object_id *head_id = NULL;
407 *start_entry = NULL;
409 err = get_head_commit_id(&head_id, repo);
410 if (err)
411 return err;
413 /* Prepend HEAD commit and all ancestors up to start commit. */
414 err = prepend_commits(&ncommits, commits, head_id, start_id, limit,
415 repo);
416 if (err)
417 return err;
419 if (got_object_id_cmp(head_id, start_id) == 0)
420 *start_entry = TAILQ_FIRST(commits);
421 else
422 *start_entry = TAILQ_LAST(commits, commit_queue);
424 if (ncommits >= limit)
425 return NULL;
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);
433 if (err)
434 break;
435 if (pentry)
436 TAILQ_INSERT_TAIL(commits, pentry, entry);
437 entry = pentry;
438 ncommits++;
441 if (err)
442 *start_entry = NULL;
443 return err;
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;
452 int ncommits = 0;
454 wclear(tog_log_view.window);
456 entry = first;
457 *last = first;
458 while (entry) {
459 if (ncommits == limit)
460 break;
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);
466 if (err)
467 break;
468 ncommits++;
469 *last = entry;
470 entry = TAILQ_NEXT(entry, entry);
473 update_panels();
474 doupdate();
476 return err;
479 static void
480 scroll_up(struct commit_queue_entry **first_displayed_entry, int n,
481 struct commit_queue *commits)
483 struct commit_queue_entry *entry;
484 int nscrolled = 0;
486 entry = TAILQ_FIRST(commits);
487 if (*first_displayed_entry == entry)
488 return;
490 entry = *first_displayed_entry;
491 while (entry && nscrolled < n) {
492 entry = TAILQ_PREV(entry, commit_queue, entry);
493 if (entry) {
494 *first_displayed_entry = entry;
495 nscrolled++;
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;
507 int nscrolled = 0;
509 if (last_displayed_entry->commit->nparents == 0)
510 return NULL;
512 entry = *first_displayed_entry;
513 do {
514 struct commit_queue_entry *pentry;
516 pentry = TAILQ_NEXT(entry, entry);
517 if (pentry == NULL) {
518 err = fetch_parent_commit(&pentry, entry, repo);
519 if (err)
520 break;
521 if (pentry == NULL) {
522 *first_displayed_entry = entry;
523 return NULL;
525 TAILQ_INSERT_TAIL(commits, pentry, entry);
526 last_displayed_entry = pentry;
529 *first_displayed_entry = pentry;
530 entry = pentry;
532 if (TAILQ_LAST(commits, commit_queue) == last_displayed_entry) {
533 err = fetch_parent_commit(&pentry, last_displayed_entry,
534 repo);
535 if (err)
536 break;
537 if (pentry) {
538 TAILQ_INSERT_TAIL(commits, pentry, entry);
539 last_displayed_entry = pentry;
542 } while (++nscrolled < n);
544 return NULL;
547 static int
548 num_parents(struct commit_queue_entry *entry)
550 int nparents = 0;
552 while (entry) {
553 entry = TAILQ_NEXT(entry, entry);
554 nparents++;
557 return nparents;
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);
571 if (id == NULL)
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);
588 if (err)
589 goto done;
590 while (!done) {
591 err = draw_commits(&last_displayed_entry, first_displayed_entry,
592 selected, LINES);
593 if (err)
594 goto done;
596 nodelay(stdscr, FALSE);
597 ch = wgetch(tog_log_view.window);
598 switch (ch) {
599 case ERR:
600 if (errno) {
601 err = got_error_from_errno();
602 goto done;
604 break;
605 case 'q':
606 done = 1;
607 break;
608 case 'k':
609 case KEY_UP:
610 if (selected > 0)
611 selected--;
612 if (selected > 0)
613 break;
614 scroll_up(&first_displayed_entry, 1, &commits);
615 break;
616 case KEY_PPAGE:
617 if (TAILQ_FIRST(&commits) ==
618 first_displayed_entry) {
619 selected = 0;
620 break;
622 scroll_up(&first_displayed_entry, LINES,
623 &commits);
624 break;
625 case 'j':
626 case KEY_DOWN:
627 nparents = num_parents(first_displayed_entry);
628 if (selected < LINES - 1 &&
629 selected < nparents - 1)
630 selected++;
631 if (selected < LINES - 1 &&
632 selected < nparents - 1)
633 break;
634 err = scroll_down(&first_displayed_entry, 1,
635 last_displayed_entry, &commits, repo);
636 if (err)
637 goto done;
638 break;
639 case KEY_NPAGE:
640 nparents = num_parents(first_displayed_entry);
641 if (nparents < LINES - 1 &&
642 selected < nparents - 1) {
643 selected = nparents - 1;
644 break;
646 err = scroll_down(&first_displayed_entry, LINES,
647 last_displayed_entry, &commits, repo);
648 if (err)
649 goto done;
650 nparents = num_parents(first_displayed_entry);
651 if (selected > nparents)
652 selected = nparents - 1;
653 break;
654 case KEY_RESIZE:
655 if (selected > LINES)
656 selected = LINES - 1;
657 break;
658 default:
659 break;
661 nodelay(stdscr, TRUE);
663 done:
664 free_commits(&commits);
665 return err;
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;
676 int ch;
678 #ifndef PROFILE
679 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
680 err(1, "pledge");
681 #endif
683 while ((ch = getopt(argc, argv, "c:")) != -1) {
684 switch (ch) {
685 case 'c':
686 start_commit = optarg;
687 break;
688 default:
689 usage();
690 /* NOTREACHED */
694 argc -= optind;
695 argv += optind;
697 if (argc == 0) {
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();
705 } else
706 usage_log();
708 error = got_repo_open(&repo, repo_path);
709 free(repo_path);
710 if (error != NULL)
711 return error;
713 if (start_commit == NULL) {
714 error = get_head_commit_id(&start_id, repo);
715 if (error != NULL)
716 return error;
717 } else {
718 struct got_object *obj;
719 error = got_object_open_by_id_str(&obj, repo, start_commit);
720 if (error == NULL) {
721 start_id = got_object_get_id(obj);
722 if (start_id == NULL)
723 error = got_error_from_errno();
726 if (error != NULL)
727 return error;
728 error = show_log_view(start_id, repo);
729 free(start_id);
730 got_repo_close(repo);
731 return error;
734 __dead void
735 usage_diff(void)
737 endwin();
738 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
739 getprogname());
740 exit(1);
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;
749 rewind(f);
750 wclear(tog_diff_view.window);
752 *eof = 0;
753 while (nprinted < max_lines) {
754 char *line;
755 size_t lineno;
756 size_t linelen;
757 const char delim[3] = { '\0', '\0', '\0'};
759 line = fparseln(f, &linelen, &lineno, delim, 0);
760 if (line == NULL) {
761 *eof = 1;
762 break;
764 if (++nlines < *first_displayed_line) {
765 free(line);
766 continue;
769 if (linelen > COLS - 1)
770 line[COLS - 1] = '\0';
771 waddstr(tog_diff_view.window, line);
772 waddch(tog_diff_view.window, '\n');
773 if (++nprinted == 1)
774 *first_displayed_line = nlines;
775 free(line);
777 *last_displayed_line = nlines;
779 update_panels();
780 doupdate();
782 return NULL;
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;
790 FILE *f;
791 int ch, done = 0, first_displayed_line = 1, last_displayed_line = LINES;
792 int eof;
794 if (got_object_get_type(obj1) != got_object_get_type(obj2))
795 return got_error(GOT_ERR_OBJ_TYPE);
797 f = got_opentemp();
798 if (f == NULL)
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);
804 break;
805 case GOT_OBJ_TYPE_TREE:
806 err = got_diff_objects_as_trees(obj1, obj2, repo, f);
807 break;
808 case GOT_OBJ_TYPE_COMMIT:
809 err = got_diff_objects_as_commits(obj1, obj2, repo, f);
810 break;
811 default:
812 return got_error(GOT_ERR_OBJ_TYPE);
815 fflush(f);
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();
827 } else
828 show_panel(tog_diff_view.panel);
830 while (!done) {
831 err = draw_diff(f, &first_displayed_line, &last_displayed_line,
832 &eof, LINES);
833 if (err)
834 break;
835 nodelay(stdscr, FALSE);
836 ch = wgetch(tog_diff_view.window);
837 switch (ch) {
838 case 'q':
839 done = 1;
840 break;
841 case 'k':
842 case KEY_UP:
843 if (first_displayed_line > 1)
844 first_displayed_line--;
845 break;
846 case 'j':
847 case KEY_DOWN:
848 if (!eof)
849 first_displayed_line++;
850 break;
851 default:
852 break;
854 nodelay(stdscr, TRUE);
856 fclose(f);
857 return err;
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;
868 int ch;
870 #ifndef PROFILE
871 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
872 err(1, "pledge");
873 #endif
875 while ((ch = getopt(argc, argv, "")) != -1) {
876 switch (ch) {
877 default:
878 usage();
879 /* NOTREACHED */
883 argc -= optind;
884 argv += optind;
886 if (argc == 0) {
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];
900 } else
901 usage_diff();
903 error = got_repo_open(&repo, repo_path);
904 free(repo_path);
905 if (error)
906 goto done;
908 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
909 if (error)
910 goto done;
912 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
913 if (error)
914 goto done;
916 error = show_diff_view(obj1, obj2, repo);
917 done:
918 got_repo_close(repo);
919 if (obj1)
920 got_object_close(obj1);
921 if (obj2)
922 got_object_close(obj2);
923 return error;
926 __dead void
927 usage_blame(void)
929 endwin();
930 fprintf(stderr, "usage: %s blame [repository-path] blob-object\n",
931 getprogname());
932 exit(1);
935 const struct got_error *
936 cmd_blame(int argc, char *argv[])
938 return got_error(GOT_ERR_NOT_IMPL);
941 static void
942 init_curses(void)
944 initscr();
945 cbreak();
946 noecho();
947 nonl();
948 intrflush(stdscr, FALSE);
949 keypad(stdscr, TRUE);
950 curs_set(0);
953 __dead void
954 usage(void)
956 int i;
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);
964 exit(1);
967 static char **
968 make_argv(const char *arg0, const char *arg1)
970 char **argv;
971 int argc = (arg1 == NULL ? 1 : 2);
973 argv = calloc(argc, sizeof(char *));
974 if (argv == NULL)
975 err(1, "calloc");
976 argv[0] = strdup(arg0);
977 if (argv[0] == NULL)
978 err(1, "calloc");
979 if (arg1) {
980 argv[1] = strdup(arg1);
981 if (argv[1] == NULL)
982 err(1, "calloc");
985 return argv;
988 int
989 main(int argc, char *argv[])
991 const struct got_error *error = NULL;
992 struct tog_cmd *cmd = NULL;
993 int ch, hflag = 0;
994 char **cmd_argv = NULL;
996 setlocale(LC_ALL, "");
998 while ((ch = getopt(argc, argv, "h")) != -1) {
999 switch (ch) {
1000 case 'h':
1001 hflag = 1;
1002 break;
1003 default:
1004 usage();
1005 /* NOTREACHED */
1009 argc -= optind;
1010 argv += optind;
1011 optind = 0;
1012 optreset = 1;
1014 if (argc == 0) {
1015 /* Build an argument vector which runs a default command. */
1016 cmd = &tog_commands[0];
1017 cmd_argv = make_argv(cmd->name, NULL);
1018 argc = 1;
1019 } else {
1020 int i;
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];
1027 if (hflag)
1028 tog_commands[i].cmd_usage();
1029 break;
1032 if (cmd == NULL) {
1033 /* Did the user specify a repository? */
1034 char *repo_path = realpath(argv[0], NULL);
1035 if (repo_path) {
1036 struct got_repository *repo;
1037 error = got_repo_open(&repo, repo_path);
1038 if (error == NULL)
1039 got_repo_close(repo);
1040 } else
1041 error = got_error_from_errno();
1042 if (error) {
1043 fprintf(stderr, "%s: '%s' is neither a known "
1044 "command nor a path to a repository\n",
1045 getprogname(), argv[0]);
1046 free(repo_path);
1047 return 1;
1049 cmd = &tog_commands[0];
1050 cmd_argv = make_argv(cmd->name, repo_path);
1051 argc = 2;
1052 free(repo_path);
1056 init_curses();
1058 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
1059 if (error)
1060 goto done;
1061 done:
1062 endwin();
1063 free(cmd_argv);
1064 if (error)
1065 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
1066 return 0;