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 WINDOW *tog_main_win;
81 PANEL *tog_main_panel;
82 static struct tog_log_view {
83 WINDOW *window;
84 PANEL *panel;
85 } tog_log_view;
86 static struct tog_diff_view {
87 WINDOW *window;
88 PANEL *panel;
89 } tog_diff_view;
91 __dead void
92 usage_log(void)
93 {
94 endwin();
95 fprintf(stderr, "usage: %s log [-c commit] [repository-path]\n",
96 getprogname());
97 exit(1);
98 }
100 static const struct got_error *
101 draw_commit(struct got_commit_object *commit, struct got_object_id *id)
103 const struct got_error *err = NULL;
104 char *logmsg0 = NULL, *logmsg = NULL;
105 char *author0 = NULL, *author = NULL;
106 char *newline, *smallerthan;
107 char *line = NULL;
108 char *id_str = NULL;
109 const size_t id_display_len = 8;
110 const size_t author_display_len = 16;
111 size_t id_len, author_len, logmsg_len, avail;
112 int i, col;
114 err = got_object_id_str(&id_str, id);
115 if (err)
116 return err;
117 id_len = strlen(id_str);
119 logmsg0 = strdup(commit->logmsg);
120 if (logmsg0 == NULL) {
121 err = got_error_from_errno();
122 goto done;
124 logmsg = logmsg0;
125 while (*logmsg == '\n')
126 logmsg++;
127 newline = strchr(logmsg, '\n');
128 if (newline)
129 *newline = '\0';
130 logmsg_len = strlen(logmsg);
132 author0 = strdup(commit->author);
133 if (author0 == NULL) {
134 err = got_error_from_errno();
135 goto done;
137 author = author0;
138 smallerthan = strchr(author, '<');
139 if (smallerthan)
140 *smallerthan = '\0';
141 else {
142 char *at = strchr(author, '@');
143 if (at)
144 *at = '\0';
146 author_len = strlen(author);
148 avail = COLS - 1;
149 line = calloc(avail + 1, sizeof(*line));
150 if (line == NULL) {
151 err = got_error_from_errno();
152 goto done;
155 col = 0;
156 for (i = 0; i < MIN(id_display_len, id_len); i++) {
157 if (col >= avail)
158 goto draw;
159 line[col++] = id_str[i];
161 while (i < id_display_len) {
162 if (col >= avail)
163 goto draw;
164 line[col++] = ' ';
165 i++;
167 if (col >= avail)
168 goto draw;
169 line[col++] = ' ';
170 for (i = 0; i < MIN(author_display_len, author_len); i++) {
171 if (col >= avail)
172 goto draw;
173 line[col++] = author[i];
175 while (i < author_display_len) {
176 if (col >= avail)
177 goto draw;
178 line[col++] = ' ';
179 i++;
181 if (col >= avail)
182 goto draw;
183 line[col++] = ' ';
185 while (col < avail && *logmsg)
186 line[col++] = *logmsg++;
187 while (col < avail)
188 line[col++] = ' ';
189 draw:
190 waddstr(tog_log_view.window, line);
191 waddch(tog_log_view.window, '\n');
192 done:
193 free(logmsg0);
194 free(author0);
195 free(line);
196 free(id_str);
197 return err;
200 struct commit_queue_entry {
201 TAILQ_ENTRY(commit_queue_entry) entry;
202 struct got_object_id *id;
203 struct got_commit_object *commit;
204 };
205 TAILQ_HEAD(commit_queue, commit_queue_entry);
207 static struct commit_queue_entry *
208 alloc_commit_queue_entry(struct got_commit_object *commit,
209 struct got_object_id *id)
211 struct commit_queue_entry *entry;
213 entry = calloc(1, sizeof(*entry));
214 if (entry == NULL)
215 return NULL;
217 entry->id = id;
218 entry->commit = commit;
219 return entry;
222 static void
223 pop_commit(struct commit_queue *commits)
225 struct commit_queue_entry *entry;
227 entry = TAILQ_FIRST(commits);
228 TAILQ_REMOVE(commits, entry, entry);
229 got_object_commit_close(entry->commit);
230 free(entry->id);
231 free(entry);
234 static void
235 free_commits(struct commit_queue *commits)
237 while (!TAILQ_EMPTY(commits))
238 pop_commit(commits);
242 static const struct got_error *
243 fetch_parent_commit(struct commit_queue_entry **pentry,
244 struct commit_queue_entry *entry, struct got_repository *repo)
246 const struct got_error *err = NULL;
247 struct got_object *obj = NULL;
248 struct got_commit_object *commit;
249 struct got_object_id *id;
250 struct got_parent_id *pid;
252 *pentry = NULL;
254 /* Follow the first parent (TODO: handle merge commits). */
255 pid = SIMPLEQ_FIRST(&entry->commit->parent_ids);
256 if (pid == NULL)
257 return NULL;
258 err = got_object_open(&obj, repo, pid->id);
259 if (err)
260 return err;
261 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
262 err = got_error(GOT_ERR_OBJ_TYPE);
263 got_object_close(obj);
264 return err;
267 err = got_object_commit_open(&commit, repo, obj);
268 got_object_close(obj);
269 if (err)
270 return err;
272 id = got_object_id_dup(pid->id);
273 if (id == NULL) {
274 err = got_error_from_errno();
275 got_object_commit_close(commit);
276 return err;;
279 *pentry = alloc_commit_queue_entry(commit, id);
280 if (*pentry == NULL) {
281 err = got_error_from_errno();
282 got_object_commit_close(commit);
285 return err;;
288 static const struct got_error *
289 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
291 const struct got_error *err = NULL;
292 struct got_reference *head_ref;
294 *head_id = NULL;
296 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
297 if (err)
298 return err;
300 err = got_ref_resolve(head_id, repo, head_ref);
301 got_ref_close(head_ref);
302 if (err) {
303 *head_id = NULL;
304 return err;
307 return NULL;
310 static const struct got_error *
311 prepend_commits(int *ncommits, struct commit_queue *commits,
312 struct got_object_id *first_id, struct got_object_id *last_id,
313 int limit, struct got_repository *repo)
315 const struct got_error *err = NULL;
316 struct got_object *first_obj = NULL, *last_obj = NULL;
317 struct got_commit_object *commit = NULL;
318 struct got_object_id *id = NULL;
319 struct commit_queue_entry *entry, *old_head_entry;
321 *ncommits = 0;
323 err = got_object_open(&first_obj, repo, first_id);
324 if (err)
325 goto done;
326 if (got_object_get_type(first_obj) != GOT_OBJ_TYPE_COMMIT) {
327 err = got_error(GOT_ERR_OBJ_TYPE);
328 goto done;
330 err = got_object_open(&last_obj, repo, last_id);
331 if (err)
332 goto done;
333 if (got_object_get_type(last_obj) != GOT_OBJ_TYPE_COMMIT) {
334 err = got_error(GOT_ERR_OBJ_TYPE);
335 goto done;
338 err = got_object_commit_open(&commit, repo, first_obj);
339 if (err)
340 goto done;
342 id = got_object_id_dup(first_id);
343 if (id == NULL) {
344 err = got_error_from_errno();
345 goto done;
348 entry = alloc_commit_queue_entry(commit, id);
349 if (entry == NULL)
350 return got_error_from_errno();
352 old_head_entry = TAILQ_FIRST(commits);
353 if (old_head_entry)
354 TAILQ_INSERT_BEFORE(old_head_entry, entry, entry);
355 else
356 TAILQ_INSERT_HEAD(commits, entry, entry);
358 *ncommits = 1;
360 /*
361 * Fetch parent commits.
362 * XXX If first and last commit aren't ancestrally related this loop
363 * we will keep iterating until a root commit is encountered.
364 */
365 while (1) {
366 struct commit_queue_entry *pentry;
368 err = fetch_parent_commit(&pentry, entry, repo);
369 if (err)
370 goto done;
371 if (pentry == NULL)
372 break;
374 /*
375 * Fill up to old HEAD commit if commit queue was not empty.
376 * We must not leave a gap in history.
377 */
378 if (old_head_entry &&
379 got_object_id_cmp(pentry->id, old_head_entry->id) == 0)
380 break;
382 TAILQ_INSERT_AFTER(commits, entry, pentry, entry);
383 (*ncommits)++;
384 if (*ncommits >= limit)
385 break;
387 /* Fill up to last requested commit if queue was empty. */
388 if (old_head_entry == NULL &&
389 got_object_id_cmp(pentry->id, last_id) == 0)
390 break;
392 entry = pentry;
395 done:
396 if (first_obj)
397 got_object_close(first_obj);
398 if (last_obj)
399 got_object_close(last_obj);
400 return err;
403 static const struct got_error *
404 fetch_commits(struct commit_queue_entry **start_entry,
405 struct got_object_id *start_id, struct commit_queue *commits,
406 int limit, struct got_repository *repo)
408 const struct got_error *err;
409 struct commit_queue_entry *entry;
410 int ncommits = 0;
411 struct got_object_id *head_id = NULL;
413 *start_entry = NULL;
415 err = get_head_commit_id(&head_id, repo);
416 if (err)
417 return err;
419 /* Prepend HEAD commit and all ancestors up to start commit. */
420 err = prepend_commits(&ncommits, commits, head_id, start_id, limit,
421 repo);
422 if (err)
423 return err;
425 if (got_object_id_cmp(head_id, start_id) == 0)
426 *start_entry = TAILQ_FIRST(commits);
427 else
428 *start_entry = TAILQ_LAST(commits, commit_queue);
430 if (ncommits >= limit)
431 return NULL;
433 /* Append more commits from start commit up to the requested limit. */
434 entry = TAILQ_LAST(commits, commit_queue);
435 while (entry && ncommits < limit) {
436 struct commit_queue_entry *pentry;
438 err = fetch_parent_commit(&pentry, entry, repo);
439 if (err)
440 break;
441 if (pentry)
442 TAILQ_INSERT_TAIL(commits, pentry, entry);
443 entry = pentry;
444 ncommits++;
447 if (err)
448 *start_entry = NULL;
449 return err;
452 static const struct got_error *
453 draw_commits(struct commit_queue_entry **last, struct commit_queue_entry *first,
454 int selected, int limit)
456 const struct got_error *err = NULL;
457 struct commit_queue_entry *entry;
458 int ncommits = 0;
460 wclear(tog_log_view.window);
462 entry = first;
463 *last = first;
464 while (entry) {
465 if (ncommits == limit)
466 break;
467 if (ncommits == selected)
468 wstandout(tog_log_view.window);
469 err = draw_commit(entry->commit, entry->id);
470 if (ncommits == selected)
471 wstandend(tog_log_view.window);
472 if (err)
473 break;
474 ncommits++;
475 *last = entry;
476 entry = TAILQ_NEXT(entry, entry);
479 update_panels();
480 doupdate();
482 return err;
485 static void
486 scroll_up(struct commit_queue_entry **first_displayed_entry, int n,
487 struct commit_queue *commits)
489 struct commit_queue_entry *entry;
490 int nscrolled = 0;
492 entry = TAILQ_FIRST(commits);
493 if (*first_displayed_entry == entry)
494 return;
496 entry = *first_displayed_entry;
497 while (entry && nscrolled < n) {
498 entry = TAILQ_PREV(entry, commit_queue, entry);
499 if (entry) {
500 *first_displayed_entry = entry;
501 nscrolled++;
506 static const struct got_error *
507 scroll_down(struct commit_queue_entry **first_displayed_entry, int n,
508 struct commit_queue_entry *last_displayed_entry,
509 struct commit_queue *commits, struct got_repository *repo)
511 const struct got_error *err = NULL;
512 struct commit_queue_entry *entry;
513 int nscrolled = 0;
515 if (last_displayed_entry->commit->nparents == 0)
516 return NULL;
518 entry = *first_displayed_entry;
519 do {
520 struct commit_queue_entry *pentry;
522 pentry = TAILQ_NEXT(entry, entry);
523 if (pentry == NULL) {
524 err = fetch_parent_commit(&pentry, entry, repo);
525 if (err)
526 break;
527 if (pentry == NULL) {
528 *first_displayed_entry = entry;
529 return NULL;
531 TAILQ_INSERT_TAIL(commits, pentry, entry);
532 last_displayed_entry = pentry;
535 *first_displayed_entry = pentry;
536 entry = pentry;
538 if (TAILQ_LAST(commits, commit_queue) == last_displayed_entry) {
539 err = fetch_parent_commit(&pentry, last_displayed_entry,
540 repo);
541 if (err)
542 break;
543 if (pentry) {
544 TAILQ_INSERT_TAIL(commits, pentry, entry);
545 last_displayed_entry = pentry;
548 } while (++nscrolled < n);
550 return NULL;
553 static int
554 num_parents(struct commit_queue_entry *entry)
556 int nparents = 0;
558 while (entry) {
559 entry = TAILQ_NEXT(entry, entry);
560 nparents++;
563 return nparents;
566 static const struct got_error *
567 show_log_view(struct got_object_id *start_id, struct got_repository *repo)
569 const struct got_error *err = NULL;
570 struct got_object_id *id;
571 int ch, done = 0, selected = 0, nparents;
572 struct commit_queue commits;
573 struct commit_queue_entry *first_displayed_entry = NULL;
574 struct commit_queue_entry *last_displayed_entry = NULL;
576 id = got_object_id_dup(start_id);
577 if (id == NULL)
578 return got_error_from_errno();
580 if (tog_log_view.window == NULL) {
581 tog_log_view.window = newwin(0, 0, 0, 0);
582 if (tog_log_view.window == NULL)
583 return got_error_from_errno();
584 keypad(tog_log_view.window, TRUE);
586 if (tog_log_view.panel == NULL) {
587 tog_log_view.panel = new_panel(tog_log_view.window);
588 if (tog_log_view.panel == NULL)
589 return got_error_from_errno();
592 TAILQ_INIT(&commits);
593 err = fetch_commits(&first_displayed_entry, id, &commits, LINES, repo);
594 if (err)
595 goto done;
596 while (!done) {
597 err = draw_commits(&last_displayed_entry, first_displayed_entry,
598 selected, LINES);
599 if (err)
600 goto done;
602 nodelay(stdscr, FALSE);
603 ch = wgetch(tog_log_view.window);
604 switch (ch) {
605 case ERR:
606 if (errno) {
607 err = got_error_from_errno();
608 goto done;
610 break;
611 case 'q':
612 done = 1;
613 break;
614 case 'k':
615 case KEY_UP:
616 if (selected > 0)
617 selected--;
618 if (selected > 0)
619 break;
620 scroll_up(&first_displayed_entry, 1, &commits);
621 break;
622 case KEY_PPAGE:
623 if (TAILQ_FIRST(&commits) ==
624 first_displayed_entry) {
625 selected = 0;
626 break;
628 scroll_up(&first_displayed_entry, LINES,
629 &commits);
630 break;
631 case 'j':
632 case KEY_DOWN:
633 nparents = num_parents(first_displayed_entry);
634 if (selected < LINES - 1 &&
635 selected < nparents - 1)
636 selected++;
637 if (selected < LINES - 1 &&
638 selected < nparents - 1)
639 break;
640 err = scroll_down(&first_displayed_entry, 1,
641 last_displayed_entry, &commits, repo);
642 if (err)
643 goto done;
644 break;
645 case KEY_NPAGE:
646 nparents = num_parents(first_displayed_entry);
647 if (nparents < LINES - 1 &&
648 selected < nparents - 1) {
649 selected = nparents - 1;
650 break;
652 err = scroll_down(&first_displayed_entry, LINES,
653 last_displayed_entry, &commits, repo);
654 if (err)
655 goto done;
656 nparents = num_parents(first_displayed_entry);
657 if (selected > nparents)
658 selected = nparents - 1;
659 break;
660 case KEY_RESIZE:
661 if (selected > LINES)
662 selected = LINES - 1;
663 break;
664 default:
665 break;
667 nodelay(stdscr, TRUE);
669 done:
670 free_commits(&commits);
671 return err;
674 const struct got_error *
675 cmd_log(int argc, char *argv[])
677 const struct got_error *error;
678 struct got_repository *repo;
679 struct got_object_id *start_id = NULL;
680 char *repo_path = NULL;
681 char *start_commit = NULL;
682 int ch;
684 #ifndef PROFILE
685 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
686 err(1, "pledge");
687 #endif
689 while ((ch = getopt(argc, argv, "c:")) != -1) {
690 switch (ch) {
691 case 'c':
692 start_commit = optarg;
693 break;
694 default:
695 usage();
696 /* NOTREACHED */
700 argc -= optind;
701 argv += optind;
703 if (argc == 0) {
704 repo_path = getcwd(NULL, 0);
705 if (repo_path == NULL)
706 return got_error_from_errno();
707 } else if (argc == 1) {
708 repo_path = realpath(argv[0], NULL);
709 if (repo_path == NULL)
710 return got_error_from_errno();
711 } else
712 usage_log();
714 error = got_repo_open(&repo, repo_path);
715 free(repo_path);
716 if (error != NULL)
717 return error;
719 if (start_commit == NULL) {
720 error = get_head_commit_id(&start_id, repo);
721 if (error != NULL)
722 return error;
723 } else {
724 struct got_object *obj;
725 error = got_object_open_by_id_str(&obj, repo, start_commit);
726 if (error == NULL) {
727 start_id = got_object_get_id(obj);
728 if (start_id == NULL)
729 error = got_error_from_errno();
732 if (error != NULL)
733 return error;
734 error = show_log_view(start_id, repo);
735 free(start_id);
736 got_repo_close(repo);
737 return error;
740 __dead void
741 usage_diff(void)
743 endwin();
744 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
745 getprogname());
746 exit(1);
749 const struct got_error *
750 draw_diff(FILE *f, int *first_displayed_line, int *last_displayed_line,
751 int *eof, int max_lines)
753 int nlines = 0, nprinted = 0;
755 rewind(f);
756 wclear(tog_diff_view.window);
758 *eof = 0;
759 while (nprinted < max_lines) {
760 char *line;
761 size_t lineno;
762 size_t linelen;
763 const char delim[3] = { '\0', '\0', '\0'};
765 line = fparseln(f, &linelen, &lineno, delim, 0);
766 if (line == NULL) {
767 *eof = 1;
768 break;
770 if (++nlines < *first_displayed_line) {
771 free(line);
772 continue;
775 if (linelen > COLS - 1)
776 line[COLS - 1] = '\0';
777 waddstr(tog_diff_view.window, line);
778 waddch(tog_diff_view.window, '\n');
779 if (++nprinted == 1)
780 *first_displayed_line = nlines;
781 free(line);
783 *last_displayed_line = nlines;
785 update_panels();
786 doupdate();
788 return NULL;
791 const struct got_error *
792 show_diff_view(struct got_object *obj1, struct got_object *obj2,
793 struct got_repository *repo)
795 const struct got_error *err;
796 FILE *f;
797 int ch, done = 0, first_displayed_line = 1, last_displayed_line = LINES;
798 int eof;
800 if (got_object_get_type(obj1) != got_object_get_type(obj2))
801 return got_error(GOT_ERR_OBJ_TYPE);
803 f = got_opentemp();
804 if (f == NULL)
805 return got_error_from_errno();
807 switch (got_object_get_type(obj1)) {
808 case GOT_OBJ_TYPE_BLOB:
809 err = got_diff_objects_as_blobs(obj1, obj2, repo, f);
810 break;
811 case GOT_OBJ_TYPE_TREE:
812 err = got_diff_objects_as_trees(obj1, obj2, repo, f);
813 break;
814 case GOT_OBJ_TYPE_COMMIT:
815 err = got_diff_objects_as_commits(obj1, obj2, repo, f);
816 break;
817 default:
818 return got_error(GOT_ERR_OBJ_TYPE);
821 fflush(f);
823 if (tog_diff_view.window == NULL) {
824 tog_diff_view.window = newwin(0, 0, 0, 0);
825 if (tog_diff_view.window == NULL)
826 return got_error_from_errno();
827 keypad(tog_diff_view.window, TRUE);
829 if (tog_diff_view.panel == NULL) {
830 tog_diff_view.panel = new_panel(tog_diff_view.window);
831 if (tog_diff_view.panel == NULL)
832 return got_error_from_errno();
833 } else
834 show_panel(tog_diff_view.panel);
836 while (!done) {
837 err = draw_diff(f, &first_displayed_line, &last_displayed_line,
838 &eof, LINES);
839 if (err)
840 break;
841 nodelay(stdscr, FALSE);
842 ch = wgetch(tog_diff_view.window);
843 switch (ch) {
844 case 'q':
845 done = 1;
846 break;
847 case 'k':
848 case KEY_UP:
849 if (first_displayed_line > 1)
850 first_displayed_line--;
851 break;
852 case 'j':
853 case KEY_DOWN:
854 if (!eof)
855 first_displayed_line++;
856 break;
857 default:
858 break;
860 nodelay(stdscr, TRUE);
862 fclose(f);
863 return err;
866 const struct got_error *
867 cmd_diff(int argc, char *argv[])
869 const struct got_error *error = NULL;
870 struct got_repository *repo = NULL;
871 struct got_object *obj1 = NULL, *obj2 = NULL;
872 char *repo_path = NULL;
873 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
874 int ch;
876 #ifndef PROFILE
877 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
878 err(1, "pledge");
879 #endif
881 while ((ch = getopt(argc, argv, "")) != -1) {
882 switch (ch) {
883 default:
884 usage();
885 /* NOTREACHED */
889 argc -= optind;
890 argv += optind;
892 if (argc == 0) {
893 usage_diff(); /* TODO show local worktree changes */
894 } else if (argc == 2) {
895 repo_path = getcwd(NULL, 0);
896 if (repo_path == NULL)
897 return got_error_from_errno();
898 obj_id_str1 = argv[0];
899 obj_id_str2 = argv[1];
900 } else if (argc == 3) {
901 repo_path = realpath(argv[0], NULL);
902 if (repo_path == NULL)
903 return got_error_from_errno();
904 obj_id_str1 = argv[1];
905 obj_id_str2 = argv[2];
906 } else
907 usage_diff();
909 error = got_repo_open(&repo, repo_path);
910 free(repo_path);
911 if (error)
912 goto done;
914 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
915 if (error)
916 goto done;
918 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
919 if (error)
920 goto done;
922 error = show_diff_view(obj1, obj2, repo);
923 done:
924 got_repo_close(repo);
925 if (obj1)
926 got_object_close(obj1);
927 if (obj2)
928 got_object_close(obj2);
929 return error;
932 __dead void
933 usage_blame(void)
935 endwin();
936 fprintf(stderr, "usage: %s blame [repository-path] blob-object\n",
937 getprogname());
938 exit(1);
941 const struct got_error *
942 cmd_blame(int argc, char *argv[])
944 return got_error(GOT_ERR_NOT_IMPL);
947 static const struct got_error *
948 init_curses(void)
950 initscr();
951 cbreak();
952 noecho();
953 nonl();
954 intrflush(stdscr, FALSE);
955 keypad(stdscr, TRUE);
956 curs_set(0);
958 tog_main_win = newwin(0, 0, 0, 0);
959 if (tog_main_win == NULL)
960 return got_error_from_errno();
961 tog_main_panel = new_panel(tog_main_win);
962 if (tog_main_panel == NULL)
963 return got_error_from_errno();
965 return NULL;
968 __dead void
969 usage(void)
971 int i;
973 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
974 "Available commands:\n", getprogname());
975 for (i = 0; i < nitems(tog_commands); i++) {
976 struct tog_cmd *cmd = &tog_commands[i];
977 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
979 exit(1);
982 static char **
983 make_argv(const char *arg0, const char *arg1)
985 char **argv;
986 int argc = (arg1 == NULL ? 1 : 2);
988 argv = calloc(argc, sizeof(char *));
989 if (argv == NULL)
990 err(1, "calloc");
991 argv[0] = strdup(arg0);
992 if (argv[0] == NULL)
993 err(1, "calloc");
994 if (arg1) {
995 argv[1] = strdup(arg1);
996 if (argv[1] == NULL)
997 err(1, "calloc");
1000 return argv;
1003 int
1004 main(int argc, char *argv[])
1006 const struct got_error *error = NULL;
1007 struct tog_cmd *cmd = NULL;
1008 int ch, hflag = 0;
1009 char **cmd_argv = NULL;
1011 setlocale(LC_ALL, "");
1013 while ((ch = getopt(argc, argv, "h")) != -1) {
1014 switch (ch) {
1015 case 'h':
1016 hflag = 1;
1017 break;
1018 default:
1019 usage();
1020 /* NOTREACHED */
1024 argc -= optind;
1025 argv += optind;
1026 optind = 0;
1027 optreset = 1;
1029 if (argc == 0) {
1030 /* Build an argument vector which runs a default command. */
1031 cmd = &tog_commands[0];
1032 cmd_argv = make_argv(cmd->name, NULL);
1033 argc = 1;
1034 } else {
1035 int i;
1037 /* Did the user specific a command? */
1038 for (i = 0; i < nitems(tog_commands); i++) {
1039 if (strncmp(tog_commands[i].name, argv[0],
1040 strlen(argv[0])) == 0) {
1041 cmd = &tog_commands[i];
1042 if (hflag)
1043 tog_commands[i].cmd_usage();
1044 break;
1047 if (cmd == NULL) {
1048 /* Did the user specify a repository? */
1049 char *repo_path = realpath(argv[0], NULL);
1050 if (repo_path) {
1051 struct got_repository *repo;
1052 error = got_repo_open(&repo, repo_path);
1053 if (error == NULL)
1054 got_repo_close(repo);
1055 } else
1056 error = got_error_from_errno();
1057 if (error) {
1058 fprintf(stderr, "%s: '%s' is neither a known "
1059 "command nor a path to a repository\n",
1060 getprogname(), argv[0]);
1061 free(repo_path);
1062 return 1;
1064 cmd = &tog_commands[0];
1065 cmd_argv = make_argv(cmd->name, repo_path);
1066 argc = 2;
1067 free(repo_path);
1071 error = init_curses();
1072 if (error) {
1073 fprintf(stderr, "cannot initialize ncurses: %s\n", error->msg);
1074 return 1;
1077 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
1078 if (error)
1079 goto done;
1080 done:
1081 endwin();
1082 free(cmd_argv);
1083 if (error)
1084 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
1085 return 0;