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>
34 #include <time.h>
36 #include "got_error.h"
37 #include "got_object.h"
38 #include "got_reference.h"
39 #include "got_repository.h"
40 #include "got_diff.h"
41 #include "got_opentemp.h"
42 #include "got_commit_graph.h"
43 #include "got_utf8.h"
45 #ifndef MIN
46 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
47 #endif
49 #ifndef nitems
50 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
51 #endif
53 struct tog_cmd {
54 const char *name;
55 const struct got_error *(*cmd_main)(int, char *[]);
56 void (*cmd_usage)(void);
57 const char *descr;
58 };
60 __dead static void usage(void);
61 __dead static void usage_log(void);
62 __dead static void usage_diff(void);
63 __dead static void usage_blame(void);
65 static const struct got_error* cmd_log(int, char *[]);
66 static const struct got_error* cmd_diff(int, char *[]);
67 static const struct got_error* cmd_blame(int, char *[]);
69 static struct tog_cmd tog_commands[] = {
70 { "log", cmd_log, usage_log,
71 "show repository history" },
72 { "diff", cmd_diff, usage_diff,
73 "compare files and directories" },
74 { "blame", cmd_blame, usage_blame,
75 "show line-by-line file history" },
76 };
78 static struct tog_view {
79 WINDOW *window;
80 PANEL *panel;
81 } tog_log_view, tog_diff_view;
83 static const struct got_error *
84 show_diff_view(struct got_object *, struct got_object *,
85 struct got_repository *);
86 static const struct got_error *
87 show_log_view(struct got_object_id *, struct got_repository *);
89 __dead static void
90 usage_log(void)
91 {
92 endwin();
93 fprintf(stderr, "usage: %s log [-c commit] [repository-path]\n",
94 getprogname());
95 exit(1);
96 }
98 /* Create newly allocated wide-character string equivalent to a byte string. */
99 static const struct got_error *
100 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
102 char *vis = NULL;
103 const struct got_error *err = NULL;
105 *ws = NULL;
106 *wlen = mbstowcs(NULL, s, 0);
107 if (*wlen == (size_t)-1) {
108 int vislen;
109 if (errno != EILSEQ)
110 return got_error_from_errno();
112 /* byte string invalid in current encoding; try to "fix" it */
113 err = got_mbsavis(&vis, &vislen, s);
114 if (err)
115 return err;
116 *wlen = mbstowcs(NULL, vis, 0);
117 if (*wlen == (size_t)-1) {
118 err = got_error_from_errno(); /* give up */
119 goto done;
123 *ws = calloc(*wlen + 1, sizeof(*ws));
124 if (*ws == NULL) {
125 err = got_error_from_errno();
126 goto done;
129 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
130 err = got_error_from_errno();
131 done:
132 free(vis);
133 if (err) {
134 free(*ws);
135 *ws = NULL;
136 *wlen = 0;
138 return err;
141 /* Format a line for display, ensuring that it won't overflow a width limit. */
142 static const struct got_error *
143 format_line(wchar_t **wlinep, int *widthp, char *line, int wlimit)
145 const struct got_error *err = NULL;
146 int cols = 0;
147 wchar_t *wline = NULL;
148 size_t wlen;
149 int i;
151 *wlinep = NULL;
153 err = mbs2ws(&wline, &wlen, line);
154 if (err)
155 return err;
157 i = 0;
158 while (i < wlen && cols <= wlimit) {
159 int width = wcwidth(wline[i]);
160 switch (width) {
161 case 0:
162 break;
163 case 1:
164 case 2:
165 cols += width;
166 break;
167 case -1:
168 if (wline[i] == L'\t')
169 cols += TABSIZE;
170 break;
171 default:
172 err = got_error_from_errno();
173 goto done;
175 if (cols <= COLS) {
176 i++;
177 if (widthp)
178 *widthp = cols;
181 wline[i] = L'\0';
182 done:
183 if (err)
184 free(wline);
185 else
186 *wlinep = wline;
187 return err;
190 static const struct got_error *
191 draw_commit(struct got_commit_object *commit, struct got_object_id *id)
193 const struct got_error *err = NULL;
194 char *logmsg0 = NULL, *logmsg = NULL;
195 char *author0 = NULL, *author = NULL;
196 wchar_t *wlogmsg = NULL, *wauthor = NULL;
197 int author_width, logmsg_width;
198 char *newline, *smallerthan;
199 char *line = NULL;
200 char *id_str = NULL;
201 size_t id_len;
202 int col, limit;
203 static const size_t id_display_cols = 8;
204 static const size_t author_display_cols = 16;
205 const int avail = COLS;
207 err = got_object_id_str(&id_str, id);
208 if (err)
209 return err;
210 id_len = strlen(id_str);
211 if (avail < id_display_cols) {
212 limit = MIN(id_len, avail);
213 waddnstr(tog_log_view.window, id_str, limit);
214 } else {
215 limit = MIN(id_display_cols, id_len);
216 waddnstr(tog_log_view.window, id_str, limit);
218 col = limit + 1;
219 while (col <= avail && col < id_display_cols + 2) {
220 waddch(tog_log_view.window, ' ');
221 col++;
223 if (col > avail)
224 goto done;
226 author0 = strdup(commit->author);
227 if (author0 == NULL) {
228 err = got_error_from_errno();
229 goto done;
231 author = author0;
232 smallerthan = strchr(author, '<');
233 if (smallerthan)
234 *smallerthan = '\0';
235 else {
236 char *at = strchr(author, '@');
237 if (at)
238 *at = '\0';
240 limit = MIN(avail, author_display_cols);
241 err = format_line(&wauthor, &author_width, author, limit);
242 if (err)
243 goto done;
244 waddwstr(tog_log_view.window, wauthor);
245 col += author_width;
246 while (col <= avail && author_width < author_display_cols + 1) {
247 waddch(tog_log_view.window, ' ');
248 col++;
249 author_width++;
251 if (col > avail)
252 goto done;
254 logmsg0 = strdup(commit->logmsg);
255 if (logmsg0 == NULL) {
256 err = got_error_from_errno();
257 goto done;
259 logmsg = logmsg0;
260 while (*logmsg == '\n')
261 logmsg++;
262 newline = strchr(logmsg, '\n');
263 if (newline)
264 *newline = '\0';
265 limit = avail - col;
266 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
267 if (err)
268 goto done;
269 waddwstr(tog_log_view.window, wlogmsg);
270 col += logmsg_width;
271 while (col <= avail) {
272 waddch(tog_log_view.window, ' ');
273 col++;
275 done:
276 free(logmsg0);
277 free(wlogmsg);
278 free(author0);
279 free(wauthor);
280 free(line);
281 free(id_str);
282 return err;
285 struct commit_queue_entry {
286 TAILQ_ENTRY(commit_queue_entry) entry;
287 struct got_object_id *id;
288 struct got_commit_object *commit;
289 };
290 TAILQ_HEAD(commit_queue, commit_queue_entry);
292 static struct commit_queue_entry *
293 alloc_commit_queue_entry(struct got_commit_object *commit,
294 struct got_object_id *id)
296 struct commit_queue_entry *entry;
298 entry = calloc(1, sizeof(*entry));
299 if (entry == NULL)
300 return NULL;
302 entry->id = id;
303 entry->commit = commit;
304 return entry;
307 static void
308 pop_commit(struct commit_queue *commits)
310 struct commit_queue_entry *entry;
312 entry = TAILQ_FIRST(commits);
313 TAILQ_REMOVE(commits, entry, entry);
314 got_object_commit_close(entry->commit);
315 /* Don't free entry->id! It is owned by the commit graph. */
316 free(entry);
319 static void
320 free_commits(struct commit_queue *commits)
322 while (!TAILQ_EMPTY(commits))
323 pop_commit(commits);
326 static const struct got_error *
327 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
328 struct got_object_id *start_id, struct got_repository *repo)
330 const struct got_error *err = NULL;
331 struct got_object_id *id;
332 struct commit_queue_entry *entry;
334 err = got_commit_graph_iter_start(graph, start_id);
335 if (err)
336 return err;
338 entry = TAILQ_LAST(commits, commit_queue);
339 if (entry && got_object_id_cmp(entry->id, start_id) == 0) {
340 int nfetched;
342 /* Start ID's commit is already on the queue; skip over it. */
343 err = got_commit_graph_iter_next(&id, graph);
344 if (err && err->code != GOT_ERR_ITER_NEED_MORE)
345 return err;
347 err = got_commit_graph_fetch_commits(&nfetched, graph, 1, repo);
348 if (err)
349 return err;
352 while (1) {
353 struct got_commit_object *commit;
355 err = got_commit_graph_iter_next(&id, graph);
356 if (err) {
357 if (err->code == GOT_ERR_ITER_NEED_MORE)
358 err = NULL;
359 break;
362 err = got_object_open_as_commit(&commit, repo, id);
363 if (err)
364 break;
366 entry = alloc_commit_queue_entry(commit, id);
367 if (entry == NULL) {
368 err = got_error_from_errno();
369 break;
372 TAILQ_INSERT_TAIL(commits, entry, entry);
375 return err;
378 static const struct got_error *
379 fetch_next_commit(struct commit_queue_entry **pentry,
380 struct commit_queue_entry *entry, struct commit_queue *commits,
381 struct got_commit_graph *graph, struct got_repository *repo)
383 const struct got_error *err = NULL;
384 struct got_object_qid *qid;
386 *pentry = NULL;
388 /* Populate commit graph with entry's parent commits. */
389 SIMPLEQ_FOREACH(qid, &entry->commit->parent_ids, entry) {
390 int nfetched;
391 err = got_commit_graph_fetch_commits_up_to(&nfetched,
392 graph, qid->id, repo);
393 if (err)
394 return err;
397 /* Append outstanding commits to queue in graph sort order. */
398 err = queue_commits(graph, commits, entry->id, repo);
399 if (err) {
400 if (err->code == GOT_ERR_ITER_COMPLETED)
401 err = NULL;
402 return err;
405 /* Next entry to display should now be available. */
406 *pentry = TAILQ_NEXT(entry, entry);
407 if (*pentry == NULL)
408 return got_error(GOT_ERR_NO_OBJ);
410 return NULL;
413 static const struct got_error *
414 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
416 const struct got_error *err = NULL;
417 struct got_reference *head_ref;
419 *head_id = NULL;
421 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
422 if (err)
423 return err;
425 err = got_ref_resolve(head_id, repo, head_ref);
426 got_ref_close(head_ref);
427 if (err) {
428 *head_id = NULL;
429 return err;
432 return NULL;
435 static const struct got_error *
436 draw_commits(struct commit_queue_entry **last, struct commit_queue_entry **selected,
437 struct commit_queue_entry *first, int selected_idx, int limit)
439 const struct got_error *err = NULL;
440 struct commit_queue_entry *entry;
441 int ncommits = 0;
443 werase(tog_log_view.window);
445 entry = first;
446 *last = first;
447 while (entry) {
448 if (ncommits == limit)
449 break;
450 if (ncommits == selected_idx) {
451 wstandout(tog_log_view.window);
452 *selected = entry;
454 err = draw_commit(entry->commit, entry->id);
455 if (ncommits == selected_idx)
456 wstandend(tog_log_view.window);
457 if (err)
458 break;
459 ncommits++;
460 *last = entry;
461 entry = TAILQ_NEXT(entry, entry);
464 update_panels();
465 doupdate();
467 return err;
470 static void
471 scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
472 struct commit_queue *commits)
474 struct commit_queue_entry *entry;
475 int nscrolled = 0;
477 entry = TAILQ_FIRST(commits);
478 if (*first_displayed_entry == entry)
479 return;
481 entry = *first_displayed_entry;
482 while (entry && nscrolled < maxscroll) {
483 entry = TAILQ_PREV(entry, commit_queue, entry);
484 if (entry) {
485 *first_displayed_entry = entry;
486 nscrolled++;
491 static const struct got_error *
492 scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
493 struct commit_queue_entry *last_displayed_entry,
494 struct commit_queue *commits, struct got_commit_graph *graph,
495 struct got_repository *repo)
497 const struct got_error *err = NULL;
498 struct commit_queue_entry *pentry;
499 int nscrolled = 0;
501 do {
502 pentry = TAILQ_NEXT(last_displayed_entry, entry);
503 if (pentry == NULL) {
504 err = fetch_next_commit(&pentry, last_displayed_entry,
505 commits, graph, repo);
506 if (err || pentry == NULL)
507 break;
509 last_displayed_entry = pentry;
511 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
512 if (pentry == NULL)
513 break;
514 *first_displayed_entry = pentry;
515 } while (++nscrolled < maxscroll);
517 return err;
520 static int
521 num_parents(struct commit_queue_entry *entry)
523 int nparents = 0;
525 while (entry) {
526 entry = TAILQ_NEXT(entry, entry);
527 nparents++;
530 return nparents;
533 static const struct got_error *
534 show_commit(struct commit_queue_entry *entry, struct got_repository *repo)
536 const struct got_error *err;
537 struct got_object *obj1 = NULL, *obj2 = NULL;
538 struct got_object_qid *parent_id;
540 err = got_object_open(&obj2, repo, entry->id);
541 if (err)
542 return err;
544 parent_id = SIMPLEQ_FIRST(&entry->commit->parent_ids);
545 if (parent_id) {
546 err = got_object_open(&obj1, repo, parent_id->id);
547 if (err)
548 goto done;
551 err = show_diff_view(obj1, obj2, repo);
552 done:
553 if (obj1)
554 got_object_close(obj1);
555 if (obj2)
556 got_object_close(obj2);
557 return err;
560 static const struct got_error *
561 show_log_view(struct got_object_id *start_id, struct got_repository *repo)
563 const struct got_error *err = NULL;
564 struct got_object_id *head_id = NULL;
565 int ch, done = 0, selected = 0, nparents, nfetched;
566 struct got_commit_graph *graph;
567 struct commit_queue commits;
568 struct commit_queue_entry *entry = NULL;
569 struct commit_queue_entry *first_displayed_entry = NULL;
570 struct commit_queue_entry *last_displayed_entry = NULL;
571 struct commit_queue_entry *selected_entry = NULL;
573 if (tog_log_view.window == NULL) {
574 tog_log_view.window = newwin(0, 0, 0, 0);
575 if (tog_log_view.window == NULL)
576 return got_error_from_errno();
577 keypad(tog_log_view.window, TRUE);
579 if (tog_log_view.panel == NULL) {
580 tog_log_view.panel = new_panel(tog_log_view.window);
581 if (tog_log_view.panel == NULL)
582 return got_error_from_errno();
583 } else
584 show_panel(tog_log_view.panel);
586 err = get_head_commit_id(&head_id, repo);
587 if (err)
588 return err;
590 TAILQ_INIT(&commits);
592 err = got_commit_graph_open(&graph, head_id, 0, repo);
593 if (err)
594 goto done;
596 /* Populate commit graph with a sufficient number of commits. */
597 err = got_commit_graph_fetch_commits_up_to(&nfetched, graph, start_id,
598 repo);
599 if (err)
600 goto done;
601 err = got_commit_graph_fetch_commits(&nfetched, graph, LINES, repo);
602 if (err)
603 goto done;
605 /*
606 * Open the initial batch of commits, sorted in commit graph order.
607 * We keep all commits open throughout the lifetime of the log view
608 * in order to avoid having to re-fetch commits from disk while
609 * updating the display.
610 */
611 err = queue_commits(graph, &commits, head_id, repo);
612 if (err && err->code != GOT_ERR_ITER_COMPLETED)
613 goto done;
615 /* Find entry corresponding to the first commit to display. */
616 TAILQ_FOREACH(entry, &commits, entry) {
617 if (got_object_id_cmp(entry->id, start_id) == 0) {
618 first_displayed_entry = entry;
619 break;
622 if (first_displayed_entry == NULL) {
623 err = got_error(GOT_ERR_NO_OBJ);
624 goto done;
627 while (!done) {
628 err = draw_commits(&last_displayed_entry, &selected_entry,
629 first_displayed_entry, selected, LINES);
630 if (err)
631 goto done;
633 nodelay(stdscr, FALSE);
634 ch = wgetch(tog_log_view.window);
635 nodelay(stdscr, TRUE);
636 switch (ch) {
637 case ERR:
638 if (errno) {
639 err = got_error_from_errno();
640 goto done;
642 break;
643 case 'q':
644 done = 1;
645 break;
646 case 'k':
647 case KEY_UP:
648 if (selected > 0)
649 selected--;
650 if (selected > 0)
651 break;
652 scroll_up(&first_displayed_entry, 1, &commits);
653 break;
654 case KEY_PPAGE:
655 if (TAILQ_FIRST(&commits) ==
656 first_displayed_entry) {
657 selected = 0;
658 break;
660 scroll_up(&first_displayed_entry, LINES,
661 &commits);
662 break;
663 case 'j':
664 case KEY_DOWN:
665 nparents = num_parents(first_displayed_entry);
666 if (selected < LINES - 1 &&
667 selected < nparents - 1) {
668 selected++;
669 break;
671 err = scroll_down(&first_displayed_entry, 1,
672 last_displayed_entry, &commits, graph,
673 repo);
674 if (err)
675 goto done;
676 break;
677 case KEY_NPAGE:
678 err = scroll_down(&first_displayed_entry, LINES,
679 last_displayed_entry, &commits, graph,
680 repo);
681 if (err)
682 goto done;
683 if (last_displayed_entry->commit->nparents > 0)
684 break;
685 /* can't scroll any further; move cursor down */
686 nparents = num_parents(first_displayed_entry);
687 if (selected < LINES - 1 ||
688 selected < nparents - 1)
689 selected = MIN(LINES - 1, nparents - 1);
690 break;
691 case KEY_RESIZE:
692 if (selected > LINES)
693 selected = LINES - 1;
694 break;
695 case KEY_ENTER:
696 case '\r':
697 err = show_commit(selected_entry, repo);
698 if (err)
699 break;
700 show_panel(tog_log_view.panel);
701 break;
702 default:
703 break;
706 done:
707 free(head_id);
708 if (graph)
709 got_commit_graph_close(graph);
710 free_commits(&commits);
711 return err;
714 static const struct got_error *
715 cmd_log(int argc, char *argv[])
717 const struct got_error *error;
718 struct got_repository *repo;
719 struct got_object_id *start_id = NULL;
720 char *repo_path = NULL;
721 char *start_commit = NULL;
722 int ch;
724 #ifndef PROFILE
725 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
726 err(1, "pledge");
727 #endif
729 while ((ch = getopt(argc, argv, "c:")) != -1) {
730 switch (ch) {
731 case 'c':
732 start_commit = optarg;
733 break;
734 default:
735 usage();
736 /* NOTREACHED */
740 argc -= optind;
741 argv += optind;
743 if (argc == 0) {
744 repo_path = getcwd(NULL, 0);
745 if (repo_path == NULL)
746 return got_error_from_errno();
747 } else if (argc == 1) {
748 repo_path = realpath(argv[0], NULL);
749 if (repo_path == NULL)
750 return got_error_from_errno();
751 } else
752 usage_log();
754 error = got_repo_open(&repo, repo_path);
755 free(repo_path);
756 if (error != NULL)
757 return error;
759 if (start_commit == NULL) {
760 error = get_head_commit_id(&start_id, repo);
761 if (error != NULL)
762 return error;
763 } else {
764 struct got_object *obj;
765 error = got_object_open_by_id_str(&obj, repo, start_commit);
766 if (error == NULL) {
767 start_id = got_object_get_id(obj);
768 if (start_id == NULL)
769 error = got_error_from_errno();
772 if (error != NULL)
773 return error;
774 error = show_log_view(start_id, repo);
775 free(start_id);
776 got_repo_close(repo);
777 return error;
780 __dead static void
781 usage_diff(void)
783 endwin();
784 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
785 getprogname());
786 exit(1);
789 static char *
790 parse_next_line(FILE *f, size_t *len)
792 char *line;
793 size_t linelen;
794 size_t lineno;
795 const char delim[3] = { '\0', '\0', '\0'};
797 line = fparseln(f, &linelen, &lineno, delim, 0);
798 if (len)
799 *len = linelen;
800 return line;
803 static const struct got_error *
804 draw_diff(FILE *f, int *first_displayed_line, int *last_displayed_line,
805 int *eof, int max_lines)
807 const struct got_error *err;
808 int nlines = 0, nprinted = 0;
809 char *line;
810 size_t len;
811 wchar_t *wline;
812 int width;
814 rewind(f);
815 werase(tog_diff_view.window);
817 *eof = 0;
818 while (nprinted < max_lines) {
819 line = parse_next_line(f, &len);
820 if (line == NULL) {
821 *eof = 1;
822 break;
824 if (++nlines < *first_displayed_line) {
825 free(line);
826 continue;
829 err = format_line(&wline, &width, line, COLS);
830 if (err) {
831 free(line);
832 return err;
834 waddwstr(tog_diff_view.window, wline);
835 if (width < COLS)
836 waddch(tog_diff_view.window, '\n');
837 if (++nprinted == 1)
838 *first_displayed_line = nlines;
839 free(line);
841 *last_displayed_line = nlines;
843 update_panels();
844 doupdate();
846 return NULL;
849 static const struct got_error *
850 show_diff_view(struct got_object *obj1, struct got_object *obj2,
851 struct got_repository *repo)
853 const struct got_error *err;
854 FILE *f;
855 int ch, done = 0, first_displayed_line = 1, last_displayed_line = LINES;
856 int eof, i;
858 if (obj1 != NULL && obj2 != NULL &&
859 got_object_get_type(obj1) != got_object_get_type(obj2))
860 return got_error(GOT_ERR_OBJ_TYPE);
862 f = got_opentemp();
863 if (f == NULL)
864 return got_error_from_errno();
866 switch (got_object_get_type(obj1 ? obj1 : obj2)) {
867 case GOT_OBJ_TYPE_BLOB:
868 err = got_diff_objects_as_blobs(obj1, obj2, repo, f);
869 break;
870 case GOT_OBJ_TYPE_TREE:
871 err = got_diff_objects_as_trees(obj1, obj2, repo, f);
872 break;
873 case GOT_OBJ_TYPE_COMMIT:
874 err = got_diff_objects_as_commits(obj1, obj2, repo, f);
875 break;
876 default:
877 return got_error(GOT_ERR_OBJ_TYPE);
880 fflush(f);
882 if (tog_diff_view.window == NULL) {
883 tog_diff_view.window = newwin(0, 0, 0, 0);
884 if (tog_diff_view.window == NULL)
885 return got_error_from_errno();
886 keypad(tog_diff_view.window, TRUE);
888 if (tog_diff_view.panel == NULL) {
889 tog_diff_view.panel = new_panel(tog_diff_view.window);
890 if (tog_diff_view.panel == NULL)
891 return got_error_from_errno();
892 } else
893 show_panel(tog_diff_view.panel);
895 while (!done) {
896 err = draw_diff(f, &first_displayed_line, &last_displayed_line,
897 &eof, LINES);
898 if (err)
899 break;
900 nodelay(stdscr, FALSE);
901 ch = wgetch(tog_diff_view.window);
902 nodelay(stdscr, TRUE);
903 switch (ch) {
904 case 'q':
905 done = 1;
906 break;
907 case 'k':
908 case KEY_UP:
909 case KEY_BACKSPACE:
910 if (first_displayed_line > 1)
911 first_displayed_line--;
912 break;
913 case KEY_PPAGE:
914 i = 0;
915 while (i++ < LINES - 1 &&
916 first_displayed_line > 1)
917 first_displayed_line--;
918 break;
919 case 'j':
920 case KEY_DOWN:
921 case KEY_ENTER:
922 case '\r':
923 if (!eof)
924 first_displayed_line++;
925 break;
926 case KEY_NPAGE:
927 case ' ':
928 i = 0;
929 while (!eof && i++ < LINES - 1) {
930 char *line = parse_next_line(f, NULL);
931 first_displayed_line++;
932 if (line == NULL)
933 break;
935 break;
936 default:
937 break;
940 fclose(f);
941 return err;
944 static const struct got_error *
945 cmd_diff(int argc, char *argv[])
947 const struct got_error *error = NULL;
948 struct got_repository *repo = NULL;
949 struct got_object *obj1 = NULL, *obj2 = NULL;
950 char *repo_path = NULL;
951 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
952 int ch;
954 #ifndef PROFILE
955 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
956 err(1, "pledge");
957 #endif
959 while ((ch = getopt(argc, argv, "")) != -1) {
960 switch (ch) {
961 default:
962 usage();
963 /* NOTREACHED */
967 argc -= optind;
968 argv += optind;
970 if (argc == 0) {
971 usage_diff(); /* TODO show local worktree changes */
972 } else if (argc == 2) {
973 repo_path = getcwd(NULL, 0);
974 if (repo_path == NULL)
975 return got_error_from_errno();
976 obj_id_str1 = argv[0];
977 obj_id_str2 = argv[1];
978 } else if (argc == 3) {
979 repo_path = realpath(argv[0], NULL);
980 if (repo_path == NULL)
981 return got_error_from_errno();
982 obj_id_str1 = argv[1];
983 obj_id_str2 = argv[2];
984 } else
985 usage_diff();
987 error = got_repo_open(&repo, repo_path);
988 free(repo_path);
989 if (error)
990 goto done;
992 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
993 if (error)
994 goto done;
996 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
997 if (error)
998 goto done;
1000 error = show_diff_view(obj1, obj2, repo);
1001 done:
1002 got_repo_close(repo);
1003 if (obj1)
1004 got_object_close(obj1);
1005 if (obj2)
1006 got_object_close(obj2);
1007 return error;
1010 __dead static void
1011 usage_blame(void)
1013 endwin();
1014 fprintf(stderr, "usage: %s blame [repository-path] blob-object\n",
1015 getprogname());
1016 exit(1);
1019 static const struct got_error *
1020 cmd_blame(int argc, char *argv[])
1022 return got_error(GOT_ERR_NOT_IMPL);
1025 static void
1026 init_curses(void)
1028 initscr();
1029 cbreak();
1030 noecho();
1031 nonl();
1032 intrflush(stdscr, FALSE);
1033 keypad(stdscr, TRUE);
1034 curs_set(0);
1037 __dead static void
1038 usage(void)
1040 int i;
1042 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
1043 "Available commands:\n", getprogname());
1044 for (i = 0; i < nitems(tog_commands); i++) {
1045 struct tog_cmd *cmd = &tog_commands[i];
1046 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
1048 exit(1);
1051 static char **
1052 make_argv(const char *arg0, const char *arg1)
1054 char **argv;
1055 int argc = (arg1 == NULL ? 1 : 2);
1057 argv = calloc(argc, sizeof(char *));
1058 if (argv == NULL)
1059 err(1, "calloc");
1060 argv[0] = strdup(arg0);
1061 if (argv[0] == NULL)
1062 err(1, "calloc");
1063 if (arg1) {
1064 argv[1] = strdup(arg1);
1065 if (argv[1] == NULL)
1066 err(1, "calloc");
1069 return argv;
1072 int
1073 main(int argc, char *argv[])
1075 const struct got_error *error = NULL;
1076 struct tog_cmd *cmd = NULL;
1077 int ch, hflag = 0;
1078 char **cmd_argv = NULL;
1080 setlocale(LC_ALL, "");
1082 while ((ch = getopt(argc, argv, "h")) != -1) {
1083 switch (ch) {
1084 case 'h':
1085 hflag = 1;
1086 break;
1087 default:
1088 usage();
1089 /* NOTREACHED */
1093 argc -= optind;
1094 argv += optind;
1095 optind = 0;
1096 optreset = 1;
1098 if (argc == 0) {
1099 /* Build an argument vector which runs a default command. */
1100 cmd = &tog_commands[0];
1101 cmd_argv = make_argv(cmd->name, NULL);
1102 argc = 1;
1103 } else {
1104 int i;
1106 /* Did the user specific a command? */
1107 for (i = 0; i < nitems(tog_commands); i++) {
1108 if (strncmp(tog_commands[i].name, argv[0],
1109 strlen(argv[0])) == 0) {
1110 cmd = &tog_commands[i];
1111 if (hflag)
1112 tog_commands[i].cmd_usage();
1113 break;
1116 if (cmd == NULL) {
1117 /* Did the user specify a repository? */
1118 char *repo_path = realpath(argv[0], NULL);
1119 if (repo_path) {
1120 struct got_repository *repo;
1121 error = got_repo_open(&repo, repo_path);
1122 if (error == NULL)
1123 got_repo_close(repo);
1124 } else
1125 error = got_error_from_errno();
1126 if (error) {
1127 fprintf(stderr, "%s: '%s' is neither a known "
1128 "command nor a path to a repository\n",
1129 getprogname(), argv[0]);
1130 free(repo_path);
1131 return 1;
1133 cmd = &tog_commands[0];
1134 cmd_argv = make_argv(cmd->name, repo_path);
1135 argc = 2;
1136 free(repo_path);
1140 init_curses();
1142 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
1143 if (error)
1144 goto done;
1145 done:
1146 endwin();
1147 free(cmd_argv);
1148 if (error)
1149 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
1150 return 0;