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 #define _XOPEN_SOURCE_EXTENDED
21 #include <curses.h>
22 #undef _XOPEN_SOURCE_EXTENDED
23 #include <panel.h>
24 #include <locale.h>
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <getopt.h>
28 #include <string.h>
29 #include <err.h>
30 #include <unistd.h>
31 #include <util.h>
32 #include <limits.h>
33 #include <wchar.h>
35 #include "got_error.h"
36 #include "got_object.h"
37 #include "got_reference.h"
38 #include "got_repository.h"
39 #include "got_diff.h"
40 #include "got_opentemp.h"
42 #ifndef MIN
43 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
44 #endif
46 #ifndef nitems
47 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
48 #endif
50 struct tog_cmd {
51 const char *name;
52 const struct got_error *(*cmd_main)(int, char *[]);
53 void (*cmd_usage)(void);
54 const char *descr;
55 };
57 __dead static void usage(void);
58 __dead static void usage_log(void);
59 __dead static void usage_diff(void);
60 __dead static void usage_blame(void);
62 static const struct got_error* cmd_log(int, char *[]);
63 static const struct got_error* cmd_diff(int, char *[]);
64 static const struct got_error* cmd_blame(int, char *[]);
66 static struct tog_cmd tog_commands[] = {
67 { "log", cmd_log, usage_log,
68 "show repository history" },
69 { "diff", cmd_diff, usage_diff,
70 "compare files and directories" },
71 { "blame", cmd_blame, usage_blame,
72 "show line-by-line file history" },
73 };
75 static struct tog_view {
76 WINDOW *window;
77 PANEL *panel;
78 } tog_log_view, tog_diff_view;
80 static const struct got_error *
81 show_diff_view(struct got_object *, struct got_object *,
82 struct got_repository *);
83 static const struct got_error *
84 show_log_view(struct got_object_id *, struct got_repository *);
86 __dead static void
87 usage_log(void)
88 {
89 endwin();
90 fprintf(stderr, "usage: %s log [-c commit] [repository-path]\n",
91 getprogname());
92 exit(1);
93 }
95 static const struct got_error *
96 draw_commit(struct got_commit_object *commit, struct got_object_id *id)
97 {
98 const struct got_error *err = NULL;
99 char *logmsg0 = NULL, *logmsg = NULL;
100 char *author0 = NULL, *author = NULL;
101 char *newline, *smallerthan;
102 char *line = NULL;
103 char *id_str = NULL;
104 const size_t id_display_len = 8;
105 const size_t author_display_len = 16;
106 size_t id_len, author_len, logmsg_len, avail;
107 int i, col;
109 err = got_object_id_str(&id_str, id);
110 if (err)
111 return err;
112 id_len = strlen(id_str);
114 logmsg0 = strdup(commit->logmsg);
115 if (logmsg0 == NULL) {
116 err = got_error_from_errno();
117 goto done;
119 logmsg = logmsg0;
120 while (*logmsg == '\n')
121 logmsg++;
122 newline = strchr(logmsg, '\n');
123 if (newline)
124 *newline = '\0';
125 logmsg_len = strlen(logmsg);
127 author0 = strdup(commit->author);
128 if (author0 == NULL) {
129 err = got_error_from_errno();
130 goto done;
132 author = author0;
133 smallerthan = strchr(author, '<');
134 if (smallerthan)
135 *smallerthan = '\0';
136 else {
137 char *at = strchr(author, '@');
138 if (at)
139 *at = '\0';
141 author_len = strlen(author);
143 avail = COLS - 1;
144 line = calloc(avail + 1, sizeof(*line));
145 if (line == NULL) {
146 err = got_error_from_errno();
147 goto done;
150 col = 0;
151 for (i = 0; i < MIN(id_display_len, id_len); i++) {
152 if (col >= avail)
153 goto draw;
154 line[col++] = id_str[i];
156 while (i < id_display_len) {
157 if (col >= avail)
158 goto draw;
159 line[col++] = ' ';
160 i++;
162 if (col >= avail)
163 goto draw;
164 line[col++] = ' ';
165 for (i = 0; i < MIN(author_display_len, author_len); i++) {
166 if (col >= avail)
167 goto draw;
168 line[col++] = author[i];
170 while (i < author_display_len) {
171 if (col >= avail)
172 goto draw;
173 line[col++] = ' ';
174 i++;
176 if (col >= avail)
177 goto draw;
178 line[col++] = ' ';
180 while (col < avail && *logmsg)
181 line[col++] = *logmsg++;
182 while (col < avail)
183 line[col++] = ' ';
184 draw:
185 waddstr(tog_log_view.window, line);
186 waddch(tog_log_view.window, '\n');
187 done:
188 free(logmsg0);
189 free(author0);
190 free(line);
191 free(id_str);
192 return err;
195 struct commit_queue_entry {
196 TAILQ_ENTRY(commit_queue_entry) entry;
197 struct got_object_id *id;
198 struct got_commit_object *commit;
199 };
200 TAILQ_HEAD(commit_queue, commit_queue_entry);
202 static struct commit_queue_entry *
203 alloc_commit_queue_entry(struct got_commit_object *commit,
204 struct got_object_id *id)
206 struct commit_queue_entry *entry;
208 entry = calloc(1, sizeof(*entry));
209 if (entry == NULL)
210 return NULL;
212 entry->id = id;
213 entry->commit = commit;
214 return entry;
217 static void
218 pop_commit(struct commit_queue *commits)
220 struct commit_queue_entry *entry;
222 entry = TAILQ_FIRST(commits);
223 TAILQ_REMOVE(commits, entry, entry);
224 got_object_commit_close(entry->commit);
225 free(entry->id);
226 free(entry);
229 static void
230 free_commits(struct commit_queue *commits)
232 while (!TAILQ_EMPTY(commits))
233 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 **selected,
448 struct commit_queue_entry *first, int selected_idx, int limit)
450 const struct got_error *err = NULL;
451 struct commit_queue_entry *entry;
452 int ncommits = 0;
454 werase(tog_log_view.window);
456 entry = first;
457 *last = first;
458 while (entry) {
459 if (ncommits == limit)
460 break;
461 if (ncommits == selected_idx) {
462 wstandout(tog_log_view.window);
463 *selected = entry;
465 err = draw_commit(entry->commit, entry->id);
466 if (ncommits == selected_idx)
467 wstandend(tog_log_view.window);
468 if (err)
469 break;
470 ncommits++;
471 *last = entry;
472 entry = TAILQ_NEXT(entry, entry);
475 update_panels();
476 doupdate();
478 return err;
481 static void
482 scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
483 struct commit_queue *commits)
485 struct commit_queue_entry *entry;
486 int nscrolled = 0;
488 entry = TAILQ_FIRST(commits);
489 if (*first_displayed_entry == entry)
490 return;
492 entry = *first_displayed_entry;
493 while (entry && nscrolled < maxscroll) {
494 entry = TAILQ_PREV(entry, commit_queue, entry);
495 if (entry) {
496 *first_displayed_entry = entry;
497 nscrolled++;
502 static const struct got_error *
503 scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
504 struct commit_queue_entry *last_displayed_entry,
505 struct commit_queue *commits, struct got_repository *repo)
507 const struct got_error *err = NULL;
508 struct commit_queue_entry *pentry;
509 int nscrolled = 0;
511 do {
512 pentry = TAILQ_NEXT(last_displayed_entry, entry);
513 if (pentry == NULL) {
514 err = fetch_parent_commit(&pentry,
515 last_displayed_entry, repo);
516 if (err || pentry == NULL)
517 break;
518 TAILQ_INSERT_TAIL(commits, pentry, entry);
520 last_displayed_entry = pentry;
522 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
523 if (pentry == NULL)
524 break;
525 *first_displayed_entry = pentry;
526 } while (++nscrolled < maxscroll);
528 return err;
531 static int
532 num_parents(struct commit_queue_entry *entry)
534 int nparents = 0;
536 while (entry) {
537 entry = TAILQ_NEXT(entry, entry);
538 nparents++;
541 return nparents;
544 static const struct got_error *
545 show_commit(struct commit_queue_entry *entry, struct got_repository *repo)
547 const struct got_error *err;
548 struct commit_queue_entry *pentry;
549 struct got_object *obj1 = NULL, *obj2 = NULL;
551 err = got_object_open(&obj2, repo, entry->id);
552 if (err)
553 return err;
555 pentry = TAILQ_NEXT(entry, entry);
556 if (pentry == NULL) {
557 err = fetch_parent_commit(&pentry, entry, repo);
558 if (err)
559 return err;
561 if (pentry) {
562 err = got_object_open(&obj1, repo, pentry->id);
563 if (err)
564 goto done;
567 err = show_diff_view(obj1, obj2, repo);
568 done:
569 if (obj1)
570 got_object_close(obj1);
571 if (obj2)
572 got_object_close(obj2);
573 return err;
576 static const struct got_error *
577 show_log_view(struct got_object_id *start_id, struct got_repository *repo)
579 const struct got_error *err = NULL;
580 struct got_object_id *id;
581 int ch, done = 0, selected = 0, nparents;
582 struct commit_queue commits;
583 struct commit_queue_entry *first_displayed_entry = NULL;
584 struct commit_queue_entry *last_displayed_entry = NULL;
585 struct commit_queue_entry *selected_entry = NULL;
587 id = got_object_id_dup(start_id);
588 if (id == NULL)
589 return got_error_from_errno();
591 if (tog_log_view.window == NULL) {
592 tog_log_view.window = newwin(0, 0, 0, 0);
593 if (tog_log_view.window == NULL)
594 return got_error_from_errno();
595 keypad(tog_log_view.window, TRUE);
597 if (tog_log_view.panel == NULL) {
598 tog_log_view.panel = new_panel(tog_log_view.window);
599 if (tog_log_view.panel == NULL)
600 return got_error_from_errno();
601 } else
602 show_panel(tog_log_view.panel);
604 TAILQ_INIT(&commits);
605 err = fetch_commits(&first_displayed_entry, id, &commits, LINES, repo);
606 if (err)
607 goto done;
608 while (!done) {
609 err = draw_commits(&last_displayed_entry, &selected_entry,
610 first_displayed_entry, selected, LINES);
611 if (err)
612 goto done;
614 nodelay(stdscr, FALSE);
615 ch = wgetch(tog_log_view.window);
616 nodelay(stdscr, TRUE);
617 switch (ch) {
618 case ERR:
619 if (errno) {
620 err = got_error_from_errno();
621 goto done;
623 break;
624 case 'q':
625 done = 1;
626 break;
627 case 'k':
628 case KEY_UP:
629 if (selected > 0)
630 selected--;
631 if (selected > 0)
632 break;
633 scroll_up(&first_displayed_entry, 1, &commits);
634 break;
635 case KEY_PPAGE:
636 if (TAILQ_FIRST(&commits) ==
637 first_displayed_entry) {
638 selected = 0;
639 break;
641 scroll_up(&first_displayed_entry, LINES,
642 &commits);
643 break;
644 case 'j':
645 case KEY_DOWN:
646 nparents = num_parents(first_displayed_entry);
647 if (selected < LINES - 1 &&
648 selected < nparents - 1) {
649 selected++;
650 break;
652 err = scroll_down(&first_displayed_entry, 1,
653 last_displayed_entry, &commits, repo);
654 if (err)
655 goto done;
656 break;
657 case KEY_NPAGE:
658 err = scroll_down(&first_displayed_entry, LINES,
659 last_displayed_entry, &commits, repo);
660 if (err)
661 goto done;
662 if (last_displayed_entry->commit->nparents > 0)
663 break;
664 /* can't scroll any further; move cursor down */
665 nparents = num_parents(first_displayed_entry);
666 if (selected < LINES - 1 ||
667 selected < nparents - 1)
668 selected = MIN(LINES - 1, nparents - 1);
669 break;
670 case KEY_RESIZE:
671 if (selected > LINES)
672 selected = LINES - 1;
673 break;
674 case KEY_ENTER:
675 case '\r':
676 err = show_commit(selected_entry, repo);
677 if (err)
678 break;
679 show_panel(tog_log_view.panel);
680 break;
681 default:
682 break;
685 done:
686 free_commits(&commits);
687 return err;
690 static const struct got_error *
691 cmd_log(int argc, char *argv[])
693 const struct got_error *error;
694 struct got_repository *repo;
695 struct got_object_id *start_id = NULL;
696 char *repo_path = NULL;
697 char *start_commit = NULL;
698 int ch;
700 #ifndef PROFILE
701 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
702 err(1, "pledge");
703 #endif
705 while ((ch = getopt(argc, argv, "c:")) != -1) {
706 switch (ch) {
707 case 'c':
708 start_commit = optarg;
709 break;
710 default:
711 usage();
712 /* NOTREACHED */
716 argc -= optind;
717 argv += optind;
719 if (argc == 0) {
720 repo_path = getcwd(NULL, 0);
721 if (repo_path == NULL)
722 return got_error_from_errno();
723 } else if (argc == 1) {
724 repo_path = realpath(argv[0], NULL);
725 if (repo_path == NULL)
726 return got_error_from_errno();
727 } else
728 usage_log();
730 error = got_repo_open(&repo, repo_path);
731 free(repo_path);
732 if (error != NULL)
733 return error;
735 if (start_commit == NULL) {
736 error = get_head_commit_id(&start_id, repo);
737 if (error != NULL)
738 return error;
739 } else {
740 struct got_object *obj;
741 error = got_object_open_by_id_str(&obj, repo, start_commit);
742 if (error == NULL) {
743 start_id = got_object_get_id(obj);
744 if (start_id == NULL)
745 error = got_error_from_errno();
748 if (error != NULL)
749 return error;
750 error = show_log_view(start_id, repo);
751 free(start_id);
752 got_repo_close(repo);
753 return error;
756 __dead static void
757 usage_diff(void)
759 endwin();
760 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
761 getprogname());
762 exit(1);
765 static char *
766 parse_next_line(FILE *f, size_t *len)
768 char *line;
769 size_t linelen;
770 size_t lineno;
771 const char delim[3] = { '\0', '\0', '\0'};
773 line = fparseln(f, &linelen, &lineno, delim, 0);
774 if (len)
775 *len = linelen;
776 return line;
779 /* Format a line for display, ensuring that it won't overflow COLS columns. */
780 static const struct got_error *
781 format_line(wchar_t **wlinep, char *line)
783 const struct got_error *err = NULL;
784 int cols = 0;
785 wchar_t *wline = NULL;
786 size_t wlen;
787 int i;
789 *wlinep = NULL;
791 wlen = mbstowcs(NULL, line, 0);
792 if (wlen == (size_t)-1)
793 return got_error_from_errno();
795 wline = calloc(wlen + 1, sizeof(wchar_t));
796 if (wline == NULL)
797 return got_error_from_errno();
799 if (mbstowcs(wline, line, wlen) != wlen) {
800 err = got_error_from_errno();
801 goto done;
804 i = 0;
805 while (i < wlen && cols <= COLS) {
806 int width = wcwidth(wline[i]);
807 switch (width) {
808 case 0:
809 break;
810 case 1:
811 case 2:
812 cols += width;
813 break;
814 case -1:
815 if (wline[i] == L'\t')
816 cols += TABSIZE;
817 break;
818 default:
819 err = got_error_from_errno();
820 goto done;
822 if (cols <= COLS)
823 i++;
825 wline[i] = L'\0';
826 done:
827 if (err)
828 free(wline);
829 else
830 *wlinep = wline;
831 return err;
834 static const struct got_error *
835 draw_diff(FILE *f, int *first_displayed_line, int *last_displayed_line,
836 int *eof, int max_lines)
838 const struct got_error *err;
839 int nlines = 0, nprinted = 0;
840 char *line;
841 size_t len;
842 wchar_t *wline;
844 rewind(f);
845 werase(tog_diff_view.window);
847 *eof = 0;
848 while (nprinted < max_lines) {
849 line = parse_next_line(f, &len);
850 if (line == NULL) {
851 *eof = 1;
852 break;
854 if (++nlines < *first_displayed_line) {
855 free(line);
856 continue;
859 err = format_line(&wline, line);
860 if (err) {
861 free(line);
862 return err;
864 waddwstr(tog_diff_view.window, wline);
865 waddch(tog_diff_view.window, '\n');
866 if (++nprinted == 1)
867 *first_displayed_line = nlines;
868 free(line);
870 *last_displayed_line = nlines;
872 update_panels();
873 doupdate();
875 return NULL;
878 static const struct got_error *
879 show_diff_view(struct got_object *obj1, struct got_object *obj2,
880 struct got_repository *repo)
882 const struct got_error *err;
883 FILE *f;
884 int ch, done = 0, first_displayed_line = 1, last_displayed_line = LINES;
885 int eof, i;
887 if (obj1 != NULL && obj2 != NULL &&
888 got_object_get_type(obj1) != got_object_get_type(obj2))
889 return got_error(GOT_ERR_OBJ_TYPE);
891 f = got_opentemp();
892 if (f == NULL)
893 return got_error_from_errno();
895 switch (got_object_get_type(obj1 ? obj1 : obj2)) {
896 case GOT_OBJ_TYPE_BLOB:
897 err = got_diff_objects_as_blobs(obj1, obj2, repo, f);
898 break;
899 case GOT_OBJ_TYPE_TREE:
900 err = got_diff_objects_as_trees(obj1, obj2, repo, f);
901 break;
902 case GOT_OBJ_TYPE_COMMIT:
903 err = got_diff_objects_as_commits(obj1, obj2, repo, f);
904 break;
905 default:
906 return got_error(GOT_ERR_OBJ_TYPE);
909 fflush(f);
911 if (tog_diff_view.window == NULL) {
912 tog_diff_view.window = newwin(0, 0, 0, 0);
913 if (tog_diff_view.window == NULL)
914 return got_error_from_errno();
915 keypad(tog_diff_view.window, TRUE);
917 if (tog_diff_view.panel == NULL) {
918 tog_diff_view.panel = new_panel(tog_diff_view.window);
919 if (tog_diff_view.panel == NULL)
920 return got_error_from_errno();
921 } else
922 show_panel(tog_diff_view.panel);
924 while (!done) {
925 err = draw_diff(f, &first_displayed_line, &last_displayed_line,
926 &eof, LINES);
927 if (err)
928 break;
929 nodelay(stdscr, FALSE);
930 ch = wgetch(tog_diff_view.window);
931 nodelay(stdscr, TRUE);
932 switch (ch) {
933 case 'q':
934 done = 1;
935 break;
936 case 'k':
937 case KEY_UP:
938 case KEY_BACKSPACE:
939 if (first_displayed_line > 1)
940 first_displayed_line--;
941 break;
942 case KEY_PPAGE:
943 i = 0;
944 while (i++ < LINES - 1 &&
945 first_displayed_line > 1)
946 first_displayed_line--;
947 break;
948 case 'j':
949 case KEY_DOWN:
950 case KEY_ENTER:
951 case '\r':
952 if (!eof)
953 first_displayed_line++;
954 break;
955 case KEY_NPAGE:
956 case ' ':
957 i = 0;
958 while (!eof && i++ < LINES - 1) {
959 char *line = parse_next_line(f, NULL);
960 first_displayed_line++;
961 if (line == NULL)
962 break;
964 break;
965 default:
966 break;
969 fclose(f);
970 return err;
973 static const struct got_error *
974 cmd_diff(int argc, char *argv[])
976 const struct got_error *error = NULL;
977 struct got_repository *repo = NULL;
978 struct got_object *obj1 = NULL, *obj2 = NULL;
979 char *repo_path = NULL;
980 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
981 int ch;
983 #ifndef PROFILE
984 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
985 err(1, "pledge");
986 #endif
988 while ((ch = getopt(argc, argv, "")) != -1) {
989 switch (ch) {
990 default:
991 usage();
992 /* NOTREACHED */
996 argc -= optind;
997 argv += optind;
999 if (argc == 0) {
1000 usage_diff(); /* TODO show local worktree changes */
1001 } else if (argc == 2) {
1002 repo_path = getcwd(NULL, 0);
1003 if (repo_path == NULL)
1004 return got_error_from_errno();
1005 obj_id_str1 = argv[0];
1006 obj_id_str2 = argv[1];
1007 } else if (argc == 3) {
1008 repo_path = realpath(argv[0], NULL);
1009 if (repo_path == NULL)
1010 return got_error_from_errno();
1011 obj_id_str1 = argv[1];
1012 obj_id_str2 = argv[2];
1013 } else
1014 usage_diff();
1016 error = got_repo_open(&repo, repo_path);
1017 free(repo_path);
1018 if (error)
1019 goto done;
1021 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
1022 if (error)
1023 goto done;
1025 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1026 if (error)
1027 goto done;
1029 error = show_diff_view(obj1, obj2, repo);
1030 done:
1031 got_repo_close(repo);
1032 if (obj1)
1033 got_object_close(obj1);
1034 if (obj2)
1035 got_object_close(obj2);
1036 return error;
1039 __dead static void
1040 usage_blame(void)
1042 endwin();
1043 fprintf(stderr, "usage: %s blame [repository-path] blob-object\n",
1044 getprogname());
1045 exit(1);
1048 static const struct got_error *
1049 cmd_blame(int argc, char *argv[])
1051 return got_error(GOT_ERR_NOT_IMPL);
1054 static void
1055 init_curses(void)
1057 initscr();
1058 cbreak();
1059 noecho();
1060 nonl();
1061 intrflush(stdscr, FALSE);
1062 keypad(stdscr, TRUE);
1063 curs_set(0);
1066 __dead static void
1067 usage(void)
1069 int i;
1071 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
1072 "Available commands:\n", getprogname());
1073 for (i = 0; i < nitems(tog_commands); i++) {
1074 struct tog_cmd *cmd = &tog_commands[i];
1075 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
1077 exit(1);
1080 static char **
1081 make_argv(const char *arg0, const char *arg1)
1083 char **argv;
1084 int argc = (arg1 == NULL ? 1 : 2);
1086 argv = calloc(argc, sizeof(char *));
1087 if (argv == NULL)
1088 err(1, "calloc");
1089 argv[0] = strdup(arg0);
1090 if (argv[0] == NULL)
1091 err(1, "calloc");
1092 if (arg1) {
1093 argv[1] = strdup(arg1);
1094 if (argv[1] == NULL)
1095 err(1, "calloc");
1098 return argv;
1101 int
1102 main(int argc, char *argv[])
1104 const struct got_error *error = NULL;
1105 struct tog_cmd *cmd = NULL;
1106 int ch, hflag = 0;
1107 char **cmd_argv = NULL;
1109 setlocale(LC_ALL, "");
1111 while ((ch = getopt(argc, argv, "h")) != -1) {
1112 switch (ch) {
1113 case 'h':
1114 hflag = 1;
1115 break;
1116 default:
1117 usage();
1118 /* NOTREACHED */
1122 argc -= optind;
1123 argv += optind;
1124 optind = 0;
1125 optreset = 1;
1127 if (argc == 0) {
1128 /* Build an argument vector which runs a default command. */
1129 cmd = &tog_commands[0];
1130 cmd_argv = make_argv(cmd->name, NULL);
1131 argc = 1;
1132 } else {
1133 int i;
1135 /* Did the user specific a command? */
1136 for (i = 0; i < nitems(tog_commands); i++) {
1137 if (strncmp(tog_commands[i].name, argv[0],
1138 strlen(argv[0])) == 0) {
1139 cmd = &tog_commands[i];
1140 if (hflag)
1141 tog_commands[i].cmd_usage();
1142 break;
1145 if (cmd == NULL) {
1146 /* Did the user specify a repository? */
1147 char *repo_path = realpath(argv[0], NULL);
1148 if (repo_path) {
1149 struct got_repository *repo;
1150 error = got_repo_open(&repo, repo_path);
1151 if (error == NULL)
1152 got_repo_close(repo);
1153 } else
1154 error = got_error_from_errno();
1155 if (error) {
1156 fprintf(stderr, "%s: '%s' is neither a known "
1157 "command nor a path to a repository\n",
1158 getprogname(), argv[0]);
1159 free(repo_path);
1160 return 1;
1162 cmd = &tog_commands[0];
1163 cmd_argv = make_argv(cmd->name, repo_path);
1164 argc = 2;
1165 free(repo_path);
1169 init_curses();
1171 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
1172 if (error)
1173 goto done;
1174 done:
1175 endwin();
1176 free(cmd_argv);
1177 if (error)
1178 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
1179 return 0;