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 static const struct got_error *
78 show_diff_view(struct got_object *, struct got_object *,
79 struct got_repository *);
80 static const struct got_error *
81 show_log_view(struct got_object_id *, struct got_repository *);
83 __dead static void
84 usage_log(void)
85 {
86 endwin();
87 fprintf(stderr, "usage: %s log [-c commit] [repository-path]\n",
88 getprogname());
89 exit(1);
90 }
92 static const struct got_error *
93 draw_commit(struct got_commit_object *commit, struct got_object_id *id)
94 {
95 const struct got_error *err = NULL;
96 char *logmsg0 = NULL, *logmsg = NULL;
97 char *author0 = NULL, *author = NULL;
98 char *newline, *smallerthan;
99 char *line = NULL;
100 char *id_str = NULL;
101 const size_t id_display_len = 8;
102 const size_t author_display_len = 16;
103 size_t id_len, author_len, logmsg_len, avail;
104 int i, col;
106 err = got_object_id_str(&id_str, id);
107 if (err)
108 return err;
109 id_len = strlen(id_str);
111 logmsg0 = strdup(commit->logmsg);
112 if (logmsg0 == NULL) {
113 err = got_error_from_errno();
114 goto done;
116 logmsg = logmsg0;
117 while (*logmsg == '\n')
118 logmsg++;
119 newline = strchr(logmsg, '\n');
120 if (newline)
121 *newline = '\0';
122 logmsg_len = strlen(logmsg);
124 author0 = strdup(commit->author);
125 if (author0 == NULL) {
126 err = got_error_from_errno();
127 goto done;
129 author = author0;
130 smallerthan = strchr(author, '<');
131 if (smallerthan)
132 *smallerthan = '\0';
133 else {
134 char *at = strchr(author, '@');
135 if (at)
136 *at = '\0';
138 author_len = strlen(author);
140 avail = COLS - 1;
141 line = calloc(avail + 1, sizeof(*line));
142 if (line == NULL) {
143 err = got_error_from_errno();
144 goto done;
147 col = 0;
148 for (i = 0; i < MIN(id_display_len, id_len); i++) {
149 if (col >= avail)
150 goto draw;
151 line[col++] = id_str[i];
153 while (i < id_display_len) {
154 if (col >= avail)
155 goto draw;
156 line[col++] = ' ';
157 i++;
159 if (col >= avail)
160 goto draw;
161 line[col++] = ' ';
162 for (i = 0; i < MIN(author_display_len, author_len); i++) {
163 if (col >= avail)
164 goto draw;
165 line[col++] = author[i];
167 while (i < author_display_len) {
168 if (col >= avail)
169 goto draw;
170 line[col++] = ' ';
171 i++;
173 if (col >= avail)
174 goto draw;
175 line[col++] = ' ';
177 while (col < avail && *logmsg)
178 line[col++] = *logmsg++;
179 while (col < avail)
180 line[col++] = ' ';
181 draw:
182 waddstr(tog_log_view.window, line);
183 waddch(tog_log_view.window, '\n');
184 done:
185 free(logmsg0);
186 free(author0);
187 free(line);
188 free(id_str);
189 return err;
192 struct commit_queue_entry {
193 TAILQ_ENTRY(commit_queue_entry) entry;
194 struct got_object_id *id;
195 struct got_commit_object *commit;
196 };
197 TAILQ_HEAD(commit_queue, commit_queue_entry);
199 static struct commit_queue_entry *
200 alloc_commit_queue_entry(struct got_commit_object *commit,
201 struct got_object_id *id)
203 struct commit_queue_entry *entry;
205 entry = calloc(1, sizeof(*entry));
206 if (entry == NULL)
207 return NULL;
209 entry->id = id;
210 entry->commit = commit;
211 return entry;
214 static void
215 pop_commit(struct commit_queue *commits)
217 struct commit_queue_entry *entry;
219 entry = TAILQ_FIRST(commits);
220 TAILQ_REMOVE(commits, entry, entry);
221 got_object_commit_close(entry->commit);
222 free(entry->id);
223 free(entry);
226 static void
227 free_commits(struct commit_queue *commits)
229 while (!TAILQ_EMPTY(commits))
230 pop_commit(commits);
234 static const struct got_error *
235 fetch_parent_commit(struct commit_queue_entry **pentry,
236 struct commit_queue_entry *entry, struct got_repository *repo)
238 const struct got_error *err = NULL;
239 struct got_object *obj = NULL;
240 struct got_commit_object *commit;
241 struct got_object_id *id;
242 struct got_parent_id *pid;
244 *pentry = NULL;
246 /* Follow the first parent (TODO: handle merge commits). */
247 pid = SIMPLEQ_FIRST(&entry->commit->parent_ids);
248 if (pid == NULL)
249 return NULL;
250 err = got_object_open(&obj, repo, pid->id);
251 if (err)
252 return err;
253 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
254 err = got_error(GOT_ERR_OBJ_TYPE);
255 got_object_close(obj);
256 return err;
259 err = got_object_commit_open(&commit, repo, obj);
260 got_object_close(obj);
261 if (err)
262 return err;
264 id = got_object_id_dup(pid->id);
265 if (id == NULL) {
266 err = got_error_from_errno();
267 got_object_commit_close(commit);
268 return err;;
271 *pentry = alloc_commit_queue_entry(commit, id);
272 if (*pentry == NULL) {
273 err = got_error_from_errno();
274 got_object_commit_close(commit);
277 return err;;
280 static const struct got_error *
281 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
283 const struct got_error *err = NULL;
284 struct got_reference *head_ref;
286 *head_id = NULL;
288 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
289 if (err)
290 return err;
292 err = got_ref_resolve(head_id, repo, head_ref);
293 got_ref_close(head_ref);
294 if (err) {
295 *head_id = NULL;
296 return err;
299 return NULL;
302 static const struct got_error *
303 prepend_commits(int *ncommits, struct commit_queue *commits,
304 struct got_object_id *first_id, struct got_object_id *last_id,
305 int limit, struct got_repository *repo)
307 const struct got_error *err = NULL;
308 struct got_object *first_obj = NULL, *last_obj = NULL;
309 struct got_commit_object *commit = NULL;
310 struct got_object_id *id = NULL;
311 struct commit_queue_entry *entry, *old_head_entry;
313 *ncommits = 0;
315 err = got_object_open(&first_obj, repo, first_id);
316 if (err)
317 goto done;
318 if (got_object_get_type(first_obj) != GOT_OBJ_TYPE_COMMIT) {
319 err = got_error(GOT_ERR_OBJ_TYPE);
320 goto done;
322 err = got_object_open(&last_obj, repo, last_id);
323 if (err)
324 goto done;
325 if (got_object_get_type(last_obj) != GOT_OBJ_TYPE_COMMIT) {
326 err = got_error(GOT_ERR_OBJ_TYPE);
327 goto done;
330 err = got_object_commit_open(&commit, repo, first_obj);
331 if (err)
332 goto done;
334 id = got_object_id_dup(first_id);
335 if (id == NULL) {
336 err = got_error_from_errno();
337 goto done;
340 entry = alloc_commit_queue_entry(commit, id);
341 if (entry == NULL)
342 return got_error_from_errno();
344 old_head_entry = TAILQ_FIRST(commits);
345 if (old_head_entry)
346 TAILQ_INSERT_BEFORE(old_head_entry, entry, entry);
347 else
348 TAILQ_INSERT_HEAD(commits, entry, entry);
350 *ncommits = 1;
352 /*
353 * Fetch parent commits.
354 * XXX If first and last commit aren't ancestrally related this loop
355 * we will keep iterating until a root commit is encountered.
356 */
357 while (1) {
358 struct commit_queue_entry *pentry;
360 err = fetch_parent_commit(&pentry, entry, repo);
361 if (err)
362 goto done;
363 if (pentry == NULL)
364 break;
366 /*
367 * Fill up to old HEAD commit if commit queue was not empty.
368 * We must not leave a gap in history.
369 */
370 if (old_head_entry &&
371 got_object_id_cmp(pentry->id, old_head_entry->id) == 0)
372 break;
374 TAILQ_INSERT_AFTER(commits, entry, pentry, entry);
375 (*ncommits)++;
376 if (*ncommits >= limit)
377 break;
379 /* Fill up to last requested commit if queue was empty. */
380 if (old_head_entry == NULL &&
381 got_object_id_cmp(pentry->id, last_id) == 0)
382 break;
384 entry = pentry;
387 done:
388 if (first_obj)
389 got_object_close(first_obj);
390 if (last_obj)
391 got_object_close(last_obj);
392 return err;
395 static const struct got_error *
396 fetch_commits(struct commit_queue_entry **start_entry,
397 struct got_object_id *start_id, struct commit_queue *commits,
398 int limit, struct got_repository *repo)
400 const struct got_error *err;
401 struct commit_queue_entry *entry;
402 int ncommits = 0;
403 struct got_object_id *head_id = NULL;
405 *start_entry = NULL;
407 err = get_head_commit_id(&head_id, repo);
408 if (err)
409 return err;
411 /* Prepend HEAD commit and all ancestors up to start commit. */
412 err = prepend_commits(&ncommits, commits, head_id, start_id, limit,
413 repo);
414 if (err)
415 return err;
417 if (got_object_id_cmp(head_id, start_id) == 0)
418 *start_entry = TAILQ_FIRST(commits);
419 else
420 *start_entry = TAILQ_LAST(commits, commit_queue);
422 if (ncommits >= limit)
423 return NULL;
425 /* Append more commits from start commit up to the requested limit. */
426 entry = TAILQ_LAST(commits, commit_queue);
427 while (entry && ncommits < limit) {
428 struct commit_queue_entry *pentry;
430 err = fetch_parent_commit(&pentry, entry, repo);
431 if (err)
432 break;
433 if (pentry)
434 TAILQ_INSERT_TAIL(commits, pentry, entry);
435 entry = pentry;
436 ncommits++;
439 if (err)
440 *start_entry = NULL;
441 return err;
444 static const struct got_error *
445 draw_commits(struct commit_queue_entry **last, struct commit_queue_entry **selected,
446 struct commit_queue_entry *first, int selected_idx, int limit)
448 const struct got_error *err = NULL;
449 struct commit_queue_entry *entry;
450 int ncommits = 0;
452 wclear(tog_log_view.window);
454 entry = first;
455 *last = first;
456 while (entry) {
457 if (ncommits == limit)
458 break;
459 if (ncommits == selected_idx) {
460 wstandout(tog_log_view.window);
461 *selected = entry;
463 err = draw_commit(entry->commit, entry->id);
464 if (ncommits == selected_idx)
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_commit(struct commit_queue_entry *entry, struct got_repository *repo)
563 const struct got_error *err;
564 struct got_object *obj1 = NULL, *obj2 = NULL;
566 err = got_object_open(&obj2, repo, entry->id);
567 if (err)
568 return err;
570 entry = TAILQ_NEXT(entry, entry);
571 if (entry) {
572 err = got_object_open(&obj1, repo, entry->id);
573 if (err)
574 goto done;
577 err = show_diff_view(obj1, obj2, repo);
578 done:
579 if (obj1)
580 got_object_close(obj1);
581 if (obj2)
582 got_object_close(obj2);
583 return err;
586 static const struct got_error *
587 show_log_view(struct got_object_id *start_id, struct got_repository *repo)
589 const struct got_error *err = NULL;
590 struct got_object_id *id;
591 int ch, done = 0, selected = 0, nparents;
592 struct commit_queue commits;
593 struct commit_queue_entry *first_displayed_entry = NULL;
594 struct commit_queue_entry *last_displayed_entry = NULL;
595 struct commit_queue_entry *selected_entry = NULL;
597 id = got_object_id_dup(start_id);
598 if (id == NULL)
599 return got_error_from_errno();
601 if (tog_log_view.window == NULL) {
602 tog_log_view.window = newwin(0, 0, 0, 0);
603 if (tog_log_view.window == NULL)
604 return got_error_from_errno();
605 keypad(tog_log_view.window, TRUE);
607 if (tog_log_view.panel == NULL) {
608 tog_log_view.panel = new_panel(tog_log_view.window);
609 if (tog_log_view.panel == NULL)
610 return got_error_from_errno();
611 } else
612 show_panel(tog_log_view.panel);
614 TAILQ_INIT(&commits);
615 err = fetch_commits(&first_displayed_entry, id, &commits, LINES, repo);
616 if (err)
617 goto done;
618 while (!done) {
619 err = draw_commits(&last_displayed_entry, &selected_entry,
620 first_displayed_entry, selected, LINES);
621 if (err)
622 goto done;
624 nodelay(stdscr, FALSE);
625 ch = wgetch(tog_log_view.window);
626 switch (ch) {
627 case ERR:
628 if (errno) {
629 err = got_error_from_errno();
630 goto done;
632 break;
633 case 'q':
634 done = 1;
635 break;
636 case 'k':
637 case KEY_UP:
638 if (selected > 0)
639 selected--;
640 if (selected > 0)
641 break;
642 scroll_up(&first_displayed_entry, 1, &commits);
643 break;
644 case KEY_PPAGE:
645 if (TAILQ_FIRST(&commits) ==
646 first_displayed_entry) {
647 selected = 0;
648 break;
650 scroll_up(&first_displayed_entry, LINES,
651 &commits);
652 break;
653 case 'j':
654 case KEY_DOWN:
655 nparents = num_parents(first_displayed_entry);
656 if (selected < LINES - 1 &&
657 selected < nparents - 1)
658 selected++;
659 if (selected < LINES - 1 &&
660 selected < nparents - 1)
661 break;
662 err = scroll_down(&first_displayed_entry, 1,
663 last_displayed_entry, &commits, repo);
664 if (err)
665 goto done;
666 break;
667 case KEY_NPAGE:
668 nparents = num_parents(first_displayed_entry);
669 if (nparents < LINES - 1 &&
670 selected < nparents - 1) {
671 selected = nparents - 1;
672 break;
674 err = scroll_down(&first_displayed_entry, LINES,
675 last_displayed_entry, &commits, repo);
676 if (err)
677 goto done;
678 nparents = num_parents(first_displayed_entry);
679 if (selected > nparents)
680 selected = nparents - 1;
681 break;
682 case KEY_RESIZE:
683 if (selected > LINES)
684 selected = LINES - 1;
685 break;
686 case KEY_ENTER:
687 case '\r':
688 err = show_commit(selected_entry, repo);
689 if (err)
690 break;
691 show_panel(tog_log_view.panel);
692 break;
693 default:
694 break;
696 nodelay(stdscr, TRUE);
698 done:
699 free_commits(&commits);
700 return err;
703 static const struct got_error *
704 cmd_log(int argc, char *argv[])
706 const struct got_error *error;
707 struct got_repository *repo;
708 struct got_object_id *start_id = NULL;
709 char *repo_path = NULL;
710 char *start_commit = NULL;
711 int ch;
713 #ifndef PROFILE
714 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
715 err(1, "pledge");
716 #endif
718 while ((ch = getopt(argc, argv, "c:")) != -1) {
719 switch (ch) {
720 case 'c':
721 start_commit = optarg;
722 break;
723 default:
724 usage();
725 /* NOTREACHED */
729 argc -= optind;
730 argv += optind;
732 if (argc == 0) {
733 repo_path = getcwd(NULL, 0);
734 if (repo_path == NULL)
735 return got_error_from_errno();
736 } else if (argc == 1) {
737 repo_path = realpath(argv[0], NULL);
738 if (repo_path == NULL)
739 return got_error_from_errno();
740 } else
741 usage_log();
743 error = got_repo_open(&repo, repo_path);
744 free(repo_path);
745 if (error != NULL)
746 return error;
748 if (start_commit == NULL) {
749 error = get_head_commit_id(&start_id, repo);
750 if (error != NULL)
751 return error;
752 } else {
753 struct got_object *obj;
754 error = got_object_open_by_id_str(&obj, repo, start_commit);
755 if (error == NULL) {
756 start_id = got_object_get_id(obj);
757 if (start_id == NULL)
758 error = got_error_from_errno();
761 if (error != NULL)
762 return error;
763 error = show_log_view(start_id, repo);
764 free(start_id);
765 got_repo_close(repo);
766 return error;
769 __dead static void
770 usage_diff(void)
772 endwin();
773 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
774 getprogname());
775 exit(1);
778 static const struct got_error *
779 draw_diff(FILE *f, int *first_displayed_line, int *last_displayed_line,
780 int *eof, int max_lines)
782 int nlines = 0, nprinted = 0;
784 rewind(f);
785 wclear(tog_diff_view.window);
787 *eof = 0;
788 while (nprinted < max_lines) {
789 char *line;
790 size_t lineno;
791 size_t linelen;
792 const char delim[3] = { '\0', '\0', '\0'};
794 line = fparseln(f, &linelen, &lineno, delim, 0);
795 if (line == NULL) {
796 *eof = 1;
797 break;
799 if (++nlines < *first_displayed_line) {
800 free(line);
801 continue;
804 if (linelen > COLS - 1)
805 line[COLS - 1] = '\0';
806 waddstr(tog_diff_view.window, line);
807 waddch(tog_diff_view.window, '\n');
808 if (++nprinted == 1)
809 *first_displayed_line = nlines;
810 free(line);
812 *last_displayed_line = nlines;
814 update_panels();
815 doupdate();
817 return NULL;
820 static const struct got_error *
821 show_diff_view(struct got_object *obj1, struct got_object *obj2,
822 struct got_repository *repo)
824 const struct got_error *err;
825 FILE *f;
826 int ch, done = 0, first_displayed_line = 1, last_displayed_line = LINES;
827 int eof;
829 if (obj1 != NULL && obj2 != NULL &&
830 got_object_get_type(obj1) != got_object_get_type(obj2))
831 return got_error(GOT_ERR_OBJ_TYPE);
833 f = got_opentemp();
834 if (f == NULL)
835 return got_error_from_errno();
837 switch (got_object_get_type(obj1 ? obj1 : obj2)) {
838 case GOT_OBJ_TYPE_BLOB:
839 err = got_diff_objects_as_blobs(obj1, obj2, repo, f);
840 break;
841 case GOT_OBJ_TYPE_TREE:
842 err = got_diff_objects_as_trees(obj1, obj2, repo, f);
843 break;
844 case GOT_OBJ_TYPE_COMMIT:
845 err = got_diff_objects_as_commits(obj1, obj2, repo, f);
846 break;
847 default:
848 return got_error(GOT_ERR_OBJ_TYPE);
851 fflush(f);
853 if (tog_diff_view.window == NULL) {
854 tog_diff_view.window = newwin(0, 0, 0, 0);
855 if (tog_diff_view.window == NULL)
856 return got_error_from_errno();
857 keypad(tog_diff_view.window, TRUE);
859 if (tog_diff_view.panel == NULL) {
860 tog_diff_view.panel = new_panel(tog_diff_view.window);
861 if (tog_diff_view.panel == NULL)
862 return got_error_from_errno();
863 } else
864 show_panel(tog_diff_view.panel);
866 while (!done) {
867 err = draw_diff(f, &first_displayed_line, &last_displayed_line,
868 &eof, LINES);
869 if (err)
870 break;
871 nodelay(stdscr, FALSE);
872 ch = wgetch(tog_diff_view.window);
873 switch (ch) {
874 case 'q':
875 done = 1;
876 break;
877 case 'k':
878 case KEY_UP:
879 if (first_displayed_line > 1)
880 first_displayed_line--;
881 break;
882 case 'j':
883 case KEY_DOWN:
884 if (!eof)
885 first_displayed_line++;
886 break;
887 default:
888 break;
890 nodelay(stdscr, TRUE);
892 fclose(f);
893 return err;
896 static const struct got_error *
897 cmd_diff(int argc, char *argv[])
899 const struct got_error *error = NULL;
900 struct got_repository *repo = NULL;
901 struct got_object *obj1 = NULL, *obj2 = NULL;
902 char *repo_path = NULL;
903 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
904 int ch;
906 #ifndef PROFILE
907 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
908 err(1, "pledge");
909 #endif
911 while ((ch = getopt(argc, argv, "")) != -1) {
912 switch (ch) {
913 default:
914 usage();
915 /* NOTREACHED */
919 argc -= optind;
920 argv += optind;
922 if (argc == 0) {
923 usage_diff(); /* TODO show local worktree changes */
924 } else if (argc == 2) {
925 repo_path = getcwd(NULL, 0);
926 if (repo_path == NULL)
927 return got_error_from_errno();
928 obj_id_str1 = argv[0];
929 obj_id_str2 = argv[1];
930 } else if (argc == 3) {
931 repo_path = realpath(argv[0], NULL);
932 if (repo_path == NULL)
933 return got_error_from_errno();
934 obj_id_str1 = argv[1];
935 obj_id_str2 = argv[2];
936 } else
937 usage_diff();
939 error = got_repo_open(&repo, repo_path);
940 free(repo_path);
941 if (error)
942 goto done;
944 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
945 if (error)
946 goto done;
948 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
949 if (error)
950 goto done;
952 error = show_diff_view(obj1, obj2, repo);
953 done:
954 got_repo_close(repo);
955 if (obj1)
956 got_object_close(obj1);
957 if (obj2)
958 got_object_close(obj2);
959 return error;
962 __dead static void
963 usage_blame(void)
965 endwin();
966 fprintf(stderr, "usage: %s blame [repository-path] blob-object\n",
967 getprogname());
968 exit(1);
971 static const struct got_error *
972 cmd_blame(int argc, char *argv[])
974 return got_error(GOT_ERR_NOT_IMPL);
977 static void
978 init_curses(void)
980 initscr();
981 cbreak();
982 noecho();
983 nonl();
984 intrflush(stdscr, FALSE);
985 keypad(stdscr, TRUE);
986 curs_set(0);
989 __dead static void
990 usage(void)
992 int i;
994 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
995 "Available commands:\n", getprogname());
996 for (i = 0; i < nitems(tog_commands); i++) {
997 struct tog_cmd *cmd = &tog_commands[i];
998 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
1000 exit(1);
1003 static char **
1004 make_argv(const char *arg0, const char *arg1)
1006 char **argv;
1007 int argc = (arg1 == NULL ? 1 : 2);
1009 argv = calloc(argc, sizeof(char *));
1010 if (argv == NULL)
1011 err(1, "calloc");
1012 argv[0] = strdup(arg0);
1013 if (argv[0] == NULL)
1014 err(1, "calloc");
1015 if (arg1) {
1016 argv[1] = strdup(arg1);
1017 if (argv[1] == NULL)
1018 err(1, "calloc");
1021 return argv;
1024 int
1025 main(int argc, char *argv[])
1027 const struct got_error *error = NULL;
1028 struct tog_cmd *cmd = NULL;
1029 int ch, hflag = 0;
1030 char **cmd_argv = NULL;
1032 setlocale(LC_ALL, "");
1034 while ((ch = getopt(argc, argv, "h")) != -1) {
1035 switch (ch) {
1036 case 'h':
1037 hflag = 1;
1038 break;
1039 default:
1040 usage();
1041 /* NOTREACHED */
1045 argc -= optind;
1046 argv += optind;
1047 optind = 0;
1048 optreset = 1;
1050 if (argc == 0) {
1051 /* Build an argument vector which runs a default command. */
1052 cmd = &tog_commands[0];
1053 cmd_argv = make_argv(cmd->name, NULL);
1054 argc = 1;
1055 } else {
1056 int i;
1058 /* Did the user specific a command? */
1059 for (i = 0; i < nitems(tog_commands); i++) {
1060 if (strncmp(tog_commands[i].name, argv[0],
1061 strlen(argv[0])) == 0) {
1062 cmd = &tog_commands[i];
1063 if (hflag)
1064 tog_commands[i].cmd_usage();
1065 break;
1068 if (cmd == NULL) {
1069 /* Did the user specify a repository? */
1070 char *repo_path = realpath(argv[0], NULL);
1071 if (repo_path) {
1072 struct got_repository *repo;
1073 error = got_repo_open(&repo, repo_path);
1074 if (error == NULL)
1075 got_repo_close(repo);
1076 } else
1077 error = got_error_from_errno();
1078 if (error) {
1079 fprintf(stderr, "%s: '%s' is neither a known "
1080 "command nor a path to a repository\n",
1081 getprogname(), argv[0]);
1082 free(repo_path);
1083 return 1;
1085 cmd = &tog_commands[0];
1086 cmd_argv = make_argv(cmd->name, repo_path);
1087 argc = 2;
1088 free(repo_path);
1092 init_curses();
1094 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
1095 if (error)
1096 goto done;
1097 done:
1098 endwin();
1099 free(cmd_argv);
1100 if (error)
1101 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
1102 return 0;