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 /* Create newly allocated wide-character string equivalent to a byte string. */
96 static const struct got_error *
97 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
98 {
99 const struct got_error *err = NULL;
101 *ws = NULL;
102 *wlen = mbstowcs(NULL, s, 0);
103 if (*wlen == (size_t)-1)
104 return got_error_from_errno();
106 *ws = calloc(*wlen + 1, sizeof(*ws));
107 if (*ws == NULL)
108 return got_error_from_errno();
110 if (mbstowcs(*ws, s, *wlen) != *wlen)
111 err = got_error_from_errno();
113 if (err) {
114 free(*ws);
115 *ws = NULL;
116 *wlen = 0;
118 return err;
121 /* Format a line for display, ensuring that it won't overflow a width limit. */
122 static const struct got_error *
123 format_line(wchar_t **wlinep, int *widthp, char *line, int wlimit)
125 const struct got_error *err = NULL;
126 int cols = 0;
127 wchar_t *wline = NULL;
128 size_t wlen;
129 int i;
131 *wlinep = NULL;
133 err = mbs2ws(&wline, &wlen, line);
134 if (err)
135 return err;
137 i = 0;
138 while (i < wlen && cols <= wlimit) {
139 int width = wcwidth(wline[i]);
140 switch (width) {
141 case 0:
142 break;
143 case 1:
144 case 2:
145 cols += width;
146 break;
147 case -1:
148 if (wline[i] == L'\t')
149 cols += TABSIZE;
150 break;
151 default:
152 err = got_error_from_errno();
153 goto done;
155 if (cols <= COLS) {
156 i++;
157 if (widthp)
158 *widthp = cols;
161 wline[i] = L'\0';
162 done:
163 if (err)
164 free(wline);
165 else
166 *wlinep = wline;
167 return err;
170 static const struct got_error *
171 draw_commit(struct got_commit_object *commit, struct got_object_id *id)
173 const struct got_error *err = NULL;
174 char *logmsg0 = NULL, *logmsg = NULL;
175 char *author0 = NULL, *author = NULL;
176 wchar_t *wlogmsg = NULL, *wauthor = NULL;
177 int author_width, logmsg_width;
178 char *newline, *smallerthan;
179 char *line = NULL;
180 char *id_str = NULL;
181 size_t id_len;
182 int col, limit;
183 static const size_t id_display_cols = 8;
184 static const size_t author_display_cols = 16;
185 const int avail = COLS;
187 err = got_object_id_str(&id_str, id);
188 if (err)
189 return err;
190 id_len = strlen(id_str);
191 if (avail < id_display_cols) {
192 limit = MIN(id_len, avail);
193 waddnstr(tog_log_view.window, id_str, limit);
194 } else {
195 limit = MIN(id_display_cols, id_len);
196 waddnstr(tog_log_view.window, id_str, limit);
198 col = limit + 1;
199 while (col <= avail && col < id_display_cols + 2) {
200 waddch(tog_log_view.window, ' ');
201 col++;
203 if (col > avail)
204 goto done;
206 author0 = strdup(commit->author);
207 if (author0 == NULL) {
208 err = got_error_from_errno();
209 goto done;
211 author = author0;
212 smallerthan = strchr(author, '<');
213 if (smallerthan)
214 *smallerthan = '\0';
215 else {
216 char *at = strchr(author, '@');
217 if (at)
218 *at = '\0';
220 limit = MIN(avail, author_display_cols);
221 err = format_line(&wauthor, &author_width, author, limit);
222 if (err)
223 goto done;
224 waddwstr(tog_log_view.window, wauthor);
225 col += author_width;
226 while (col <= avail && author_width < author_display_cols + 1) {
227 waddch(tog_log_view.window, ' ');
228 col++;
229 author_width++;
231 if (col > avail)
232 goto done;
234 logmsg0 = strdup(commit->logmsg);
235 if (logmsg0 == NULL) {
236 err = got_error_from_errno();
237 goto done;
239 logmsg = logmsg0;
240 while (*logmsg == '\n')
241 logmsg++;
242 newline = strchr(logmsg, '\n');
243 if (newline)
244 *newline = '\0';
245 limit = avail - col;
246 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
247 if (err)
248 goto done;
249 waddwstr(tog_log_view.window, wlogmsg);
250 col += logmsg_width;
251 while (col <= avail) {
252 waddch(tog_log_view.window, ' ');
253 col++;
255 done:
256 free(logmsg0);
257 free(wlogmsg);
258 free(author0);
259 free(wauthor);
260 free(line);
261 free(id_str);
262 return err;
265 struct commit_queue_entry {
266 TAILQ_ENTRY(commit_queue_entry) entry;
267 struct got_object_id *id;
268 struct got_commit_object *commit;
269 };
270 TAILQ_HEAD(commit_queue, commit_queue_entry);
272 static struct commit_queue_entry *
273 alloc_commit_queue_entry(struct got_commit_object *commit,
274 struct got_object_id *id)
276 struct commit_queue_entry *entry;
278 entry = calloc(1, sizeof(*entry));
279 if (entry == NULL)
280 return NULL;
282 entry->id = id;
283 entry->commit = commit;
284 return entry;
287 static void
288 pop_commit(struct commit_queue *commits)
290 struct commit_queue_entry *entry;
292 entry = TAILQ_FIRST(commits);
293 TAILQ_REMOVE(commits, entry, entry);
294 got_object_commit_close(entry->commit);
295 free(entry->id);
296 free(entry);
299 static void
300 free_commits(struct commit_queue *commits)
302 while (!TAILQ_EMPTY(commits))
303 pop_commit(commits);
306 static const struct got_error *
307 fetch_parent_commit(struct commit_queue_entry **pentry,
308 struct commit_queue_entry *entry, struct got_repository *repo)
310 const struct got_error *err = NULL;
311 struct got_commit_object *commit;
312 struct got_object_id *id;
313 struct got_object_qid *qid;
315 *pentry = NULL;
317 /* Follow the first parent (TODO: handle merge commits). */
318 qid = SIMPLEQ_FIRST(&entry->commit->parent_ids);
319 if (qid == NULL)
320 return NULL;
322 err = got_object_open_as_commit(&commit, repo, qid->id);
323 if (err)
324 return err;
326 id = got_object_id_dup(qid->id);
327 if (id == NULL) {
328 err = got_error_from_errno();
329 got_object_commit_close(commit);
330 return err;;
333 *pentry = alloc_commit_queue_entry(commit, id);
334 if (*pentry == NULL) {
335 err = got_error_from_errno();
336 got_object_commit_close(commit);
339 return err;;
342 static const struct got_error *
343 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
345 const struct got_error *err = NULL;
346 struct got_reference *head_ref;
348 *head_id = NULL;
350 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
351 if (err)
352 return err;
354 err = got_ref_resolve(head_id, repo, head_ref);
355 got_ref_close(head_ref);
356 if (err) {
357 *head_id = NULL;
358 return err;
361 return NULL;
364 static const struct got_error *
365 prepend_commits(int *ncommits, struct commit_queue *commits,
366 struct got_object_id *first_id, struct got_object_id *last_id,
367 int limit, struct got_repository *repo)
369 const struct got_error *err = NULL;
370 struct got_object *last_obj = NULL;
371 struct got_commit_object *commit = NULL;
372 struct got_object_id *id = NULL;
373 struct commit_queue_entry *entry, *old_head_entry;
375 *ncommits = 0;
377 err = got_object_open_as_commit(&commit, repo, first_id);
378 if (err)
379 goto done;
381 err = got_object_open(&last_obj, repo, last_id);
382 if (err)
383 goto done;
384 if (got_object_get_type(last_obj) != GOT_OBJ_TYPE_COMMIT) {
385 err = got_error(GOT_ERR_OBJ_TYPE);
386 goto done;
389 id = got_object_id_dup(first_id);
390 if (id == NULL) {
391 err = got_error_from_errno();
392 goto done;
395 entry = alloc_commit_queue_entry(commit, id);
396 if (entry == NULL)
397 return got_error_from_errno();
399 old_head_entry = TAILQ_FIRST(commits);
400 if (old_head_entry)
401 TAILQ_INSERT_BEFORE(old_head_entry, entry, entry);
402 else
403 TAILQ_INSERT_HEAD(commits, entry, entry);
405 *ncommits = 1;
407 /*
408 * Fetch parent commits.
409 * XXX If first and last commit aren't ancestrally related this loop
410 * we will keep iterating until a root commit is encountered.
411 */
412 while (1) {
413 struct commit_queue_entry *pentry;
415 err = fetch_parent_commit(&pentry, entry, repo);
416 if (err)
417 goto done;
418 if (pentry == NULL)
419 break;
421 /*
422 * Fill up to old HEAD commit if commit queue was not empty.
423 * We must not leave a gap in history.
424 */
425 if (old_head_entry &&
426 got_object_id_cmp(pentry->id, old_head_entry->id) == 0)
427 break;
429 TAILQ_INSERT_AFTER(commits, entry, pentry, entry);
430 (*ncommits)++;
431 if (*ncommits >= limit)
432 break;
434 /* Fill up to last requested commit if queue was empty. */
435 if (old_head_entry == NULL &&
436 got_object_id_cmp(pentry->id, last_id) == 0)
437 break;
439 entry = pentry;
442 done:
443 if (last_obj)
444 got_object_close(last_obj);
445 return err;
448 static const struct got_error *
449 fetch_commits(struct commit_queue_entry **start_entry,
450 struct got_object_id *start_id, struct commit_queue *commits,
451 int limit, struct got_repository *repo)
453 const struct got_error *err;
454 struct commit_queue_entry *entry;
455 int ncommits = 0;
456 struct got_object_id *head_id = NULL;
458 *start_entry = NULL;
460 err = get_head_commit_id(&head_id, repo);
461 if (err)
462 return err;
464 /* Prepend HEAD commit and all ancestors up to start commit. */
465 err = prepend_commits(&ncommits, commits, head_id, start_id, limit,
466 repo);
467 if (err)
468 return err;
470 if (got_object_id_cmp(head_id, start_id) == 0)
471 *start_entry = TAILQ_FIRST(commits);
472 else
473 *start_entry = TAILQ_LAST(commits, commit_queue);
475 if (ncommits >= limit)
476 return NULL;
478 /* Append more commits from start commit up to the requested limit. */
479 entry = TAILQ_LAST(commits, commit_queue);
480 while (entry && ncommits < limit) {
481 struct commit_queue_entry *pentry;
483 err = fetch_parent_commit(&pentry, entry, repo);
484 if (err)
485 break;
486 if (pentry)
487 TAILQ_INSERT_TAIL(commits, pentry, entry);
488 entry = pentry;
489 ncommits++;
492 if (err)
493 *start_entry = NULL;
494 return err;
497 static const struct got_error *
498 draw_commits(struct commit_queue_entry **last, struct commit_queue_entry **selected,
499 struct commit_queue_entry *first, int selected_idx, int limit)
501 const struct got_error *err = NULL;
502 struct commit_queue_entry *entry;
503 int ncommits = 0;
505 werase(tog_log_view.window);
507 entry = first;
508 *last = first;
509 while (entry) {
510 if (ncommits == limit)
511 break;
512 if (ncommits == selected_idx) {
513 wstandout(tog_log_view.window);
514 *selected = entry;
516 err = draw_commit(entry->commit, entry->id);
517 if (ncommits == selected_idx)
518 wstandend(tog_log_view.window);
519 if (err)
520 break;
521 ncommits++;
522 *last = entry;
523 entry = TAILQ_NEXT(entry, entry);
526 update_panels();
527 doupdate();
529 return err;
532 static void
533 scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
534 struct commit_queue *commits)
536 struct commit_queue_entry *entry;
537 int nscrolled = 0;
539 entry = TAILQ_FIRST(commits);
540 if (*first_displayed_entry == entry)
541 return;
543 entry = *first_displayed_entry;
544 while (entry && nscrolled < maxscroll) {
545 entry = TAILQ_PREV(entry, commit_queue, entry);
546 if (entry) {
547 *first_displayed_entry = entry;
548 nscrolled++;
553 static const struct got_error *
554 scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
555 struct commit_queue_entry *last_displayed_entry,
556 struct commit_queue *commits, struct got_repository *repo)
558 const struct got_error *err = NULL;
559 struct commit_queue_entry *pentry;
560 int nscrolled = 0;
562 do {
563 pentry = TAILQ_NEXT(last_displayed_entry, entry);
564 if (pentry == NULL) {
565 err = fetch_parent_commit(&pentry,
566 last_displayed_entry, repo);
567 if (err || pentry == NULL)
568 break;
569 TAILQ_INSERT_TAIL(commits, pentry, entry);
571 last_displayed_entry = pentry;
573 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
574 if (pentry == NULL)
575 break;
576 *first_displayed_entry = pentry;
577 } while (++nscrolled < maxscroll);
579 return err;
582 static int
583 num_parents(struct commit_queue_entry *entry)
585 int nparents = 0;
587 while (entry) {
588 entry = TAILQ_NEXT(entry, entry);
589 nparents++;
592 return nparents;
595 static const struct got_error *
596 show_commit(struct commit_queue_entry *entry, struct got_repository *repo)
598 const struct got_error *err;
599 struct commit_queue_entry *pentry;
600 struct got_object *obj1 = NULL, *obj2 = NULL;
602 err = got_object_open(&obj2, repo, entry->id);
603 if (err)
604 return err;
606 pentry = TAILQ_NEXT(entry, entry);
607 if (pentry == NULL) {
608 err = fetch_parent_commit(&pentry, entry, repo);
609 if (err)
610 return err;
612 if (pentry) {
613 err = got_object_open(&obj1, repo, pentry->id);
614 if (err)
615 goto done;
618 err = show_diff_view(obj1, obj2, repo);
619 done:
620 if (obj1)
621 got_object_close(obj1);
622 if (obj2)
623 got_object_close(obj2);
624 return err;
627 static const struct got_error *
628 show_log_view(struct got_object_id *start_id, struct got_repository *repo)
630 const struct got_error *err = NULL;
631 struct got_object_id *id;
632 int ch, done = 0, selected = 0, nparents;
633 struct commit_queue commits;
634 struct commit_queue_entry *first_displayed_entry = NULL;
635 struct commit_queue_entry *last_displayed_entry = NULL;
636 struct commit_queue_entry *selected_entry = NULL;
638 id = got_object_id_dup(start_id);
639 if (id == NULL)
640 return got_error_from_errno();
642 if (tog_log_view.window == NULL) {
643 tog_log_view.window = newwin(0, 0, 0, 0);
644 if (tog_log_view.window == NULL)
645 return got_error_from_errno();
646 keypad(tog_log_view.window, TRUE);
648 if (tog_log_view.panel == NULL) {
649 tog_log_view.panel = new_panel(tog_log_view.window);
650 if (tog_log_view.panel == NULL)
651 return got_error_from_errno();
652 } else
653 show_panel(tog_log_view.panel);
655 TAILQ_INIT(&commits);
656 err = fetch_commits(&first_displayed_entry, id, &commits, LINES, repo);
657 if (err)
658 goto done;
659 while (!done) {
660 err = draw_commits(&last_displayed_entry, &selected_entry,
661 first_displayed_entry, selected, LINES);
662 if (err)
663 goto done;
665 nodelay(stdscr, FALSE);
666 ch = wgetch(tog_log_view.window);
667 nodelay(stdscr, TRUE);
668 switch (ch) {
669 case ERR:
670 if (errno) {
671 err = got_error_from_errno();
672 goto done;
674 break;
675 case 'q':
676 done = 1;
677 break;
678 case 'k':
679 case KEY_UP:
680 if (selected > 0)
681 selected--;
682 if (selected > 0)
683 break;
684 scroll_up(&first_displayed_entry, 1, &commits);
685 break;
686 case KEY_PPAGE:
687 if (TAILQ_FIRST(&commits) ==
688 first_displayed_entry) {
689 selected = 0;
690 break;
692 scroll_up(&first_displayed_entry, LINES,
693 &commits);
694 break;
695 case 'j':
696 case KEY_DOWN:
697 nparents = num_parents(first_displayed_entry);
698 if (selected < LINES - 1 &&
699 selected < nparents - 1) {
700 selected++;
701 break;
703 err = scroll_down(&first_displayed_entry, 1,
704 last_displayed_entry, &commits, repo);
705 if (err)
706 goto done;
707 break;
708 case KEY_NPAGE:
709 err = scroll_down(&first_displayed_entry, LINES,
710 last_displayed_entry, &commits, repo);
711 if (err)
712 goto done;
713 if (last_displayed_entry->commit->nparents > 0)
714 break;
715 /* can't scroll any further; move cursor down */
716 nparents = num_parents(first_displayed_entry);
717 if (selected < LINES - 1 ||
718 selected < nparents - 1)
719 selected = MIN(LINES - 1, nparents - 1);
720 break;
721 case KEY_RESIZE:
722 if (selected > LINES)
723 selected = LINES - 1;
724 break;
725 case KEY_ENTER:
726 case '\r':
727 err = show_commit(selected_entry, repo);
728 if (err)
729 break;
730 show_panel(tog_log_view.panel);
731 break;
732 default:
733 break;
736 done:
737 free_commits(&commits);
738 return err;
741 static const struct got_error *
742 cmd_log(int argc, char *argv[])
744 const struct got_error *error;
745 struct got_repository *repo;
746 struct got_object_id *start_id = NULL;
747 char *repo_path = NULL;
748 char *start_commit = NULL;
749 int ch;
751 #ifndef PROFILE
752 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
753 err(1, "pledge");
754 #endif
756 while ((ch = getopt(argc, argv, "c:")) != -1) {
757 switch (ch) {
758 case 'c':
759 start_commit = optarg;
760 break;
761 default:
762 usage();
763 /* NOTREACHED */
767 argc -= optind;
768 argv += optind;
770 if (argc == 0) {
771 repo_path = getcwd(NULL, 0);
772 if (repo_path == NULL)
773 return got_error_from_errno();
774 } else if (argc == 1) {
775 repo_path = realpath(argv[0], NULL);
776 if (repo_path == NULL)
777 return got_error_from_errno();
778 } else
779 usage_log();
781 error = got_repo_open(&repo, repo_path);
782 free(repo_path);
783 if (error != NULL)
784 return error;
786 if (start_commit == NULL) {
787 error = get_head_commit_id(&start_id, repo);
788 if (error != NULL)
789 return error;
790 } else {
791 struct got_object *obj;
792 error = got_object_open_by_id_str(&obj, repo, start_commit);
793 if (error == NULL) {
794 start_id = got_object_get_id(obj);
795 if (start_id == NULL)
796 error = got_error_from_errno();
799 if (error != NULL)
800 return error;
801 error = show_log_view(start_id, repo);
802 free(start_id);
803 got_repo_close(repo);
804 return error;
807 __dead static void
808 usage_diff(void)
810 endwin();
811 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
812 getprogname());
813 exit(1);
816 static char *
817 parse_next_line(FILE *f, size_t *len)
819 char *line;
820 size_t linelen;
821 size_t lineno;
822 const char delim[3] = { '\0', '\0', '\0'};
824 line = fparseln(f, &linelen, &lineno, delim, 0);
825 if (len)
826 *len = linelen;
827 return line;
830 static const struct got_error *
831 draw_diff(FILE *f, int *first_displayed_line, int *last_displayed_line,
832 int *eof, int max_lines)
834 const struct got_error *err;
835 int nlines = 0, nprinted = 0;
836 char *line;
837 size_t len;
838 wchar_t *wline;
839 int width;
841 rewind(f);
842 werase(tog_diff_view.window);
844 *eof = 0;
845 while (nprinted < max_lines) {
846 line = parse_next_line(f, &len);
847 if (line == NULL) {
848 *eof = 1;
849 break;
851 if (++nlines < *first_displayed_line) {
852 free(line);
853 continue;
856 err = format_line(&wline, &width, line, COLS);
857 if (err) {
858 free(line);
859 return err;
861 waddwstr(tog_diff_view.window, wline);
862 if (width < COLS)
863 waddch(tog_diff_view.window, '\n');
864 if (++nprinted == 1)
865 *first_displayed_line = nlines;
866 free(line);
868 *last_displayed_line = nlines;
870 update_panels();
871 doupdate();
873 return NULL;
876 static const struct got_error *
877 show_diff_view(struct got_object *obj1, struct got_object *obj2,
878 struct got_repository *repo)
880 const struct got_error *err;
881 FILE *f;
882 int ch, done = 0, first_displayed_line = 1, last_displayed_line = LINES;
883 int eof, i;
885 if (obj1 != NULL && obj2 != NULL &&
886 got_object_get_type(obj1) != got_object_get_type(obj2))
887 return got_error(GOT_ERR_OBJ_TYPE);
889 f = got_opentemp();
890 if (f == NULL)
891 return got_error_from_errno();
893 switch (got_object_get_type(obj1 ? obj1 : obj2)) {
894 case GOT_OBJ_TYPE_BLOB:
895 err = got_diff_objects_as_blobs(obj1, obj2, repo, f);
896 break;
897 case GOT_OBJ_TYPE_TREE:
898 err = got_diff_objects_as_trees(obj1, obj2, repo, f);
899 break;
900 case GOT_OBJ_TYPE_COMMIT:
901 err = got_diff_objects_as_commits(obj1, obj2, repo, f);
902 break;
903 default:
904 return got_error(GOT_ERR_OBJ_TYPE);
907 fflush(f);
909 if (tog_diff_view.window == NULL) {
910 tog_diff_view.window = newwin(0, 0, 0, 0);
911 if (tog_diff_view.window == NULL)
912 return got_error_from_errno();
913 keypad(tog_diff_view.window, TRUE);
915 if (tog_diff_view.panel == NULL) {
916 tog_diff_view.panel = new_panel(tog_diff_view.window);
917 if (tog_diff_view.panel == NULL)
918 return got_error_from_errno();
919 } else
920 show_panel(tog_diff_view.panel);
922 while (!done) {
923 err = draw_diff(f, &first_displayed_line, &last_displayed_line,
924 &eof, LINES);
925 if (err)
926 break;
927 nodelay(stdscr, FALSE);
928 ch = wgetch(tog_diff_view.window);
929 nodelay(stdscr, TRUE);
930 switch (ch) {
931 case 'q':
932 done = 1;
933 break;
934 case 'k':
935 case KEY_UP:
936 case KEY_BACKSPACE:
937 if (first_displayed_line > 1)
938 first_displayed_line--;
939 break;
940 case KEY_PPAGE:
941 i = 0;
942 while (i++ < LINES - 1 &&
943 first_displayed_line > 1)
944 first_displayed_line--;
945 break;
946 case 'j':
947 case KEY_DOWN:
948 case KEY_ENTER:
949 case '\r':
950 if (!eof)
951 first_displayed_line++;
952 break;
953 case KEY_NPAGE:
954 case ' ':
955 i = 0;
956 while (!eof && i++ < LINES - 1) {
957 char *line = parse_next_line(f, NULL);
958 first_displayed_line++;
959 if (line == NULL)
960 break;
962 break;
963 default:
964 break;
967 fclose(f);
968 return err;
971 static const struct got_error *
972 cmd_diff(int argc, char *argv[])
974 const struct got_error *error = NULL;
975 struct got_repository *repo = NULL;
976 struct got_object *obj1 = NULL, *obj2 = NULL;
977 char *repo_path = NULL;
978 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
979 int ch;
981 #ifndef PROFILE
982 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
983 err(1, "pledge");
984 #endif
986 while ((ch = getopt(argc, argv, "")) != -1) {
987 switch (ch) {
988 default:
989 usage();
990 /* NOTREACHED */
994 argc -= optind;
995 argv += optind;
997 if (argc == 0) {
998 usage_diff(); /* TODO show local worktree changes */
999 } else if (argc == 2) {
1000 repo_path = getcwd(NULL, 0);
1001 if (repo_path == NULL)
1002 return got_error_from_errno();
1003 obj_id_str1 = argv[0];
1004 obj_id_str2 = argv[1];
1005 } else if (argc == 3) {
1006 repo_path = realpath(argv[0], NULL);
1007 if (repo_path == NULL)
1008 return got_error_from_errno();
1009 obj_id_str1 = argv[1];
1010 obj_id_str2 = argv[2];
1011 } else
1012 usage_diff();
1014 error = got_repo_open(&repo, repo_path);
1015 free(repo_path);
1016 if (error)
1017 goto done;
1019 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
1020 if (error)
1021 goto done;
1023 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1024 if (error)
1025 goto done;
1027 error = show_diff_view(obj1, obj2, repo);
1028 done:
1029 got_repo_close(repo);
1030 if (obj1)
1031 got_object_close(obj1);
1032 if (obj2)
1033 got_object_close(obj2);
1034 return error;
1037 __dead static void
1038 usage_blame(void)
1040 endwin();
1041 fprintf(stderr, "usage: %s blame [repository-path] blob-object\n",
1042 getprogname());
1043 exit(1);
1046 static const struct got_error *
1047 cmd_blame(int argc, char *argv[])
1049 return got_error(GOT_ERR_NOT_IMPL);
1052 static void
1053 init_curses(void)
1055 initscr();
1056 cbreak();
1057 noecho();
1058 nonl();
1059 intrflush(stdscr, FALSE);
1060 keypad(stdscr, TRUE);
1061 curs_set(0);
1064 __dead static void
1065 usage(void)
1067 int i;
1069 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
1070 "Available commands:\n", getprogname());
1071 for (i = 0; i < nitems(tog_commands); i++) {
1072 struct tog_cmd *cmd = &tog_commands[i];
1073 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
1075 exit(1);
1078 static char **
1079 make_argv(const char *arg0, const char *arg1)
1081 char **argv;
1082 int argc = (arg1 == NULL ? 1 : 2);
1084 argv = calloc(argc, sizeof(char *));
1085 if (argv == NULL)
1086 err(1, "calloc");
1087 argv[0] = strdup(arg0);
1088 if (argv[0] == NULL)
1089 err(1, "calloc");
1090 if (arg1) {
1091 argv[1] = strdup(arg1);
1092 if (argv[1] == NULL)
1093 err(1, "calloc");
1096 return argv;
1099 int
1100 main(int argc, char *argv[])
1102 const struct got_error *error = NULL;
1103 struct tog_cmd *cmd = NULL;
1104 int ch, hflag = 0;
1105 char **cmd_argv = NULL;
1107 setlocale(LC_ALL, "");
1109 while ((ch = getopt(argc, argv, "h")) != -1) {
1110 switch (ch) {
1111 case 'h':
1112 hflag = 1;
1113 break;
1114 default:
1115 usage();
1116 /* NOTREACHED */
1120 argc -= optind;
1121 argv += optind;
1122 optind = 0;
1123 optreset = 1;
1125 if (argc == 0) {
1126 /* Build an argument vector which runs a default command. */
1127 cmd = &tog_commands[0];
1128 cmd_argv = make_argv(cmd->name, NULL);
1129 argc = 1;
1130 } else {
1131 int i;
1133 /* Did the user specific a command? */
1134 for (i = 0; i < nitems(tog_commands); i++) {
1135 if (strncmp(tog_commands[i].name, argv[0],
1136 strlen(argv[0])) == 0) {
1137 cmd = &tog_commands[i];
1138 if (hflag)
1139 tog_commands[i].cmd_usage();
1140 break;
1143 if (cmd == NULL) {
1144 /* Did the user specify a repository? */
1145 char *repo_path = realpath(argv[0], NULL);
1146 if (repo_path) {
1147 struct got_repository *repo;
1148 error = got_repo_open(&repo, repo_path);
1149 if (error == NULL)
1150 got_repo_close(repo);
1151 } else
1152 error = got_error_from_errno();
1153 if (error) {
1154 fprintf(stderr, "%s: '%s' is neither a known "
1155 "command nor a path to a repository\n",
1156 getprogname(), argv[0]);
1157 free(repo_path);
1158 return 1;
1160 cmd = &tog_commands[0];
1161 cmd_argv = make_argv(cmd->name, repo_path);
1162 argc = 2;
1163 free(repo_path);
1167 init_curses();
1169 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
1170 if (error)
1171 goto done;
1172 done:
1173 endwin();
1174 free(cmd_argv);
1175 if (error)
1176 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
1177 return 0;