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"
41 #include "got_commit_graph.h"
42 #include "got_utf8.h"
44 #ifndef MIN
45 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
46 #endif
48 #ifndef nitems
49 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
50 #endif
52 struct tog_cmd {
53 const char *name;
54 const struct got_error *(*cmd_main)(int, char *[]);
55 void (*cmd_usage)(void);
56 const char *descr;
57 };
59 __dead static void usage(void);
60 __dead static void usage_log(void);
61 __dead static void usage_diff(void);
62 __dead static void usage_blame(void);
64 static const struct got_error* cmd_log(int, char *[]);
65 static const struct got_error* cmd_diff(int, char *[]);
66 static const struct got_error* cmd_blame(int, char *[]);
68 static struct tog_cmd tog_commands[] = {
69 { "log", cmd_log, usage_log,
70 "show repository history" },
71 { "diff", cmd_diff, usage_diff,
72 "compare files and directories" },
73 { "blame", cmd_blame, usage_blame,
74 "show line-by-line file history" },
75 };
77 static struct tog_view {
78 WINDOW *window;
79 PANEL *panel;
80 } tog_log_view, tog_diff_view;
82 static const struct got_error *
83 show_diff_view(struct got_object *, struct got_object *,
84 struct got_repository *);
85 static const struct got_error *
86 show_log_view(struct got_object_id *, struct got_repository *);
88 __dead static void
89 usage_log(void)
90 {
91 endwin();
92 fprintf(stderr, "usage: %s log [-c commit] [repository-path]\n",
93 getprogname());
94 exit(1);
95 }
97 /* Create newly allocated wide-character string equivalent to a byte string. */
98 static const struct got_error *
99 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
101 char *vis = NULL;
102 const struct got_error *err = NULL;
104 *ws = NULL;
105 *wlen = mbstowcs(NULL, s, 0);
106 if (*wlen == (size_t)-1) {
107 int vislen;
108 if (errno != EILSEQ)
109 return got_error_from_errno();
111 /* byte string invalid in current encoding; try to "fix" it */
112 err = got_mbsavis(&vis, &vislen, s);
113 if (err)
114 return err;
115 *wlen = mbstowcs(NULL, vis, 0);
116 if (*wlen == (size_t)-1)
117 return got_error_from_errno(); /* give up */
120 *ws = calloc(*wlen + 1, sizeof(*ws));
121 if (*ws == NULL)
122 return got_error_from_errno();
124 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
125 err = got_error_from_errno();
127 free(vis);
128 if (err) {
129 free(*ws);
130 *ws = NULL;
131 *wlen = 0;
133 return err;
136 /* Format a line for display, ensuring that it won't overflow a width limit. */
137 static const struct got_error *
138 format_line(wchar_t **wlinep, int *widthp, char *line, int wlimit)
140 const struct got_error *err = NULL;
141 int cols = 0;
142 wchar_t *wline = NULL;
143 size_t wlen;
144 int i;
146 *wlinep = NULL;
148 err = mbs2ws(&wline, &wlen, line);
149 if (err)
150 return err;
152 i = 0;
153 while (i < wlen && cols <= wlimit) {
154 int width = wcwidth(wline[i]);
155 switch (width) {
156 case 0:
157 break;
158 case 1:
159 case 2:
160 cols += width;
161 break;
162 case -1:
163 if (wline[i] == L'\t')
164 cols += TABSIZE;
165 break;
166 default:
167 err = got_error_from_errno();
168 goto done;
170 if (cols <= COLS) {
171 i++;
172 if (widthp)
173 *widthp = cols;
176 wline[i] = L'\0';
177 done:
178 if (err)
179 free(wline);
180 else
181 *wlinep = wline;
182 return err;
185 static const struct got_error *
186 draw_commit(struct got_commit_object *commit, struct got_object_id *id)
188 const struct got_error *err = NULL;
189 char *logmsg0 = NULL, *logmsg = NULL;
190 char *author0 = NULL, *author = NULL;
191 wchar_t *wlogmsg = NULL, *wauthor = NULL;
192 int author_width, logmsg_width;
193 char *newline, *smallerthan;
194 char *line = NULL;
195 char *id_str = NULL;
196 size_t id_len;
197 int col, limit;
198 static const size_t id_display_cols = 8;
199 static const size_t author_display_cols = 16;
200 const int avail = COLS;
202 err = got_object_id_str(&id_str, id);
203 if (err)
204 return err;
205 id_len = strlen(id_str);
206 if (avail < id_display_cols) {
207 limit = MIN(id_len, avail);
208 waddnstr(tog_log_view.window, id_str, limit);
209 } else {
210 limit = MIN(id_display_cols, id_len);
211 waddnstr(tog_log_view.window, id_str, limit);
213 col = limit + 1;
214 while (col <= avail && col < id_display_cols + 2) {
215 waddch(tog_log_view.window, ' ');
216 col++;
218 if (col > avail)
219 goto done;
221 author0 = strdup(commit->author);
222 if (author0 == NULL) {
223 err = got_error_from_errno();
224 goto done;
226 author = author0;
227 smallerthan = strchr(author, '<');
228 if (smallerthan)
229 *smallerthan = '\0';
230 else {
231 char *at = strchr(author, '@');
232 if (at)
233 *at = '\0';
235 limit = MIN(avail, author_display_cols);
236 err = format_line(&wauthor, &author_width, author, limit);
237 if (err)
238 goto done;
239 waddwstr(tog_log_view.window, wauthor);
240 col += author_width;
241 while (col <= avail && author_width < author_display_cols + 1) {
242 waddch(tog_log_view.window, ' ');
243 col++;
244 author_width++;
246 if (col > avail)
247 goto done;
249 logmsg0 = strdup(commit->logmsg);
250 if (logmsg0 == NULL) {
251 err = got_error_from_errno();
252 goto done;
254 logmsg = logmsg0;
255 while (*logmsg == '\n')
256 logmsg++;
257 newline = strchr(logmsg, '\n');
258 if (newline)
259 *newline = '\0';
260 limit = avail - col;
261 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
262 if (err)
263 goto done;
264 waddwstr(tog_log_view.window, wlogmsg);
265 col += logmsg_width;
266 while (col <= avail) {
267 waddch(tog_log_view.window, ' ');
268 col++;
270 done:
271 free(logmsg0);
272 free(wlogmsg);
273 free(author0);
274 free(wauthor);
275 free(line);
276 free(id_str);
277 return err;
280 struct commit_queue_entry {
281 TAILQ_ENTRY(commit_queue_entry) entry;
282 struct got_object_id *id;
283 struct got_commit_object *commit;
284 };
285 TAILQ_HEAD(commit_queue, commit_queue_entry);
287 static struct commit_queue_entry *
288 alloc_commit_queue_entry(struct got_commit_object *commit,
289 struct got_object_id *id)
291 struct commit_queue_entry *entry;
293 entry = calloc(1, sizeof(*entry));
294 if (entry == NULL)
295 return NULL;
297 entry->id = id;
298 entry->commit = commit;
299 return entry;
302 static void
303 pop_commit(struct commit_queue *commits)
305 struct commit_queue_entry *entry;
307 entry = TAILQ_FIRST(commits);
308 TAILQ_REMOVE(commits, entry, entry);
309 got_object_commit_close(entry->commit);
310 /* Don't free entry->id! It is owned by the commit graph. */
311 free(entry);
314 static void
315 free_commits(struct commit_queue *commits)
317 while (!TAILQ_EMPTY(commits))
318 pop_commit(commits);
321 static const struct got_error *
322 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
323 struct got_object_id *start_id, struct got_repository *repo)
325 const struct got_error *err = NULL;
326 struct got_object_id *id;
327 struct commit_queue_entry *entry;
329 err = got_commit_graph_iter_start(graph, start_id);
330 if (err)
331 return err;
333 entry = TAILQ_LAST(commits, commit_queue);
334 if (entry && got_object_id_cmp(entry->id, start_id) == 0) {
335 int nfetched;
337 /* Start ID's commit is already on the queue; skip over it. */
338 err = got_commit_graph_iter_next(&id, graph);
339 if (err && err->code != GOT_ERR_ITER_NEED_MORE)
340 return err;
342 err = got_commit_graph_fetch_commits(&nfetched, graph, 1, repo);
343 if (err)
344 return err;
347 while (1) {
348 struct got_commit_object *commit;
350 err = got_commit_graph_iter_next(&id, graph);
351 if (err) {
352 if (err->code == GOT_ERR_ITER_NEED_MORE)
353 err = NULL;
354 break;
357 err = got_object_open_as_commit(&commit, repo, id);
358 if (err)
359 break;
361 entry = alloc_commit_queue_entry(commit, id);
362 if (entry == NULL) {
363 err = got_error_from_errno();
364 break;
367 TAILQ_INSERT_TAIL(commits, entry, entry);
370 return err;
373 static const struct got_error *
374 fetch_next_commit(struct commit_queue_entry **pentry,
375 struct commit_queue_entry *entry, struct commit_queue *commits,
376 struct got_commit_graph *graph, struct got_repository *repo)
378 const struct got_error *err = NULL;
379 struct got_object_qid *qid;
381 *pentry = NULL;
383 /* Populate commit graph with entry's parent commits. */
384 SIMPLEQ_FOREACH(qid, &entry->commit->parent_ids, entry) {
385 int nfetched;
386 err = got_commit_graph_fetch_commits_up_to(&nfetched,
387 graph, qid->id, repo);
388 if (err)
389 return err;
392 /* Append outstanding commits to queue in graph sort order. */
393 err = queue_commits(graph, commits, entry->id, repo);
394 if (err) {
395 if (err->code == GOT_ERR_ITER_COMPLETED)
396 err = NULL;
397 return err;
400 /* Next entry to display should now be available. */
401 *pentry = TAILQ_NEXT(entry, entry);
402 if (*pentry == NULL)
403 return got_error(GOT_ERR_NO_OBJ);
405 return NULL;
408 static const struct got_error *
409 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
411 const struct got_error *err = NULL;
412 struct got_reference *head_ref;
414 *head_id = NULL;
416 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
417 if (err)
418 return err;
420 err = got_ref_resolve(head_id, repo, head_ref);
421 got_ref_close(head_ref);
422 if (err) {
423 *head_id = NULL;
424 return err;
427 return NULL;
430 static const struct got_error *
431 draw_commits(struct commit_queue_entry **last, struct commit_queue_entry **selected,
432 struct commit_queue_entry *first, int selected_idx, int limit)
434 const struct got_error *err = NULL;
435 struct commit_queue_entry *entry;
436 int ncommits = 0;
438 werase(tog_log_view.window);
440 entry = first;
441 *last = first;
442 while (entry) {
443 if (ncommits == limit)
444 break;
445 if (ncommits == selected_idx) {
446 wstandout(tog_log_view.window);
447 *selected = entry;
449 err = draw_commit(entry->commit, entry->id);
450 if (ncommits == selected_idx)
451 wstandend(tog_log_view.window);
452 if (err)
453 break;
454 ncommits++;
455 *last = entry;
456 entry = TAILQ_NEXT(entry, entry);
459 update_panels();
460 doupdate();
462 return err;
465 static void
466 scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
467 struct commit_queue *commits)
469 struct commit_queue_entry *entry;
470 int nscrolled = 0;
472 entry = TAILQ_FIRST(commits);
473 if (*first_displayed_entry == entry)
474 return;
476 entry = *first_displayed_entry;
477 while (entry && nscrolled < maxscroll) {
478 entry = TAILQ_PREV(entry, commit_queue, entry);
479 if (entry) {
480 *first_displayed_entry = entry;
481 nscrolled++;
486 static const struct got_error *
487 scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
488 struct commit_queue_entry *last_displayed_entry,
489 struct commit_queue *commits, struct got_commit_graph *graph,
490 struct got_repository *repo)
492 const struct got_error *err = NULL;
493 struct commit_queue_entry *pentry;
494 int nscrolled = 0;
496 do {
497 pentry = TAILQ_NEXT(last_displayed_entry, entry);
498 if (pentry == NULL) {
499 err = fetch_next_commit(&pentry, last_displayed_entry,
500 commits, graph, repo);
501 if (err || pentry == NULL)
502 break;
504 last_displayed_entry = pentry;
506 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
507 if (pentry == NULL)
508 break;
509 *first_displayed_entry = pentry;
510 } while (++nscrolled < maxscroll);
512 return err;
515 static int
516 num_parents(struct commit_queue_entry *entry)
518 int nparents = 0;
520 while (entry) {
521 entry = TAILQ_NEXT(entry, entry);
522 nparents++;
525 return nparents;
528 static const struct got_error *
529 show_commit(struct commit_queue_entry *entry, struct got_repository *repo)
531 const struct got_error *err;
532 struct got_object *obj1 = NULL, *obj2 = NULL;
533 struct got_object_qid *parent_id;
535 err = got_object_open(&obj2, repo, entry->id);
536 if (err)
537 return err;
539 parent_id = SIMPLEQ_FIRST(&entry->commit->parent_ids);
540 if (parent_id) {
541 err = got_object_open(&obj1, repo, parent_id->id);
542 if (err)
543 goto done;
546 err = show_diff_view(obj1, obj2, repo);
547 done:
548 if (obj1)
549 got_object_close(obj1);
550 if (obj2)
551 got_object_close(obj2);
552 return err;
555 static const struct got_error *
556 show_log_view(struct got_object_id *start_id, struct got_repository *repo)
558 const struct got_error *err = NULL;
559 struct got_object_id *head_id = NULL;
560 int ch, done = 0, selected = 0, nparents, nfetched;
561 struct got_commit_graph *graph;
562 struct commit_queue commits;
563 struct commit_queue_entry *entry = NULL;
564 struct commit_queue_entry *first_displayed_entry = NULL;
565 struct commit_queue_entry *last_displayed_entry = NULL;
566 struct commit_queue_entry *selected_entry = NULL;
568 if (tog_log_view.window == NULL) {
569 tog_log_view.window = newwin(0, 0, 0, 0);
570 if (tog_log_view.window == NULL)
571 return got_error_from_errno();
572 keypad(tog_log_view.window, TRUE);
574 if (tog_log_view.panel == NULL) {
575 tog_log_view.panel = new_panel(tog_log_view.window);
576 if (tog_log_view.panel == NULL)
577 return got_error_from_errno();
578 } else
579 show_panel(tog_log_view.panel);
581 err = get_head_commit_id(&head_id, repo);
582 if (err)
583 return err;
585 TAILQ_INIT(&commits);
587 err = got_commit_graph_open(&graph, head_id, repo);
588 if (err)
589 goto done;
591 /* Populate commit graph with a sufficient number of commits. */
592 err = got_commit_graph_fetch_commits_up_to(&nfetched, graph, start_id,
593 repo);
594 if (err)
595 goto done;
596 err = got_commit_graph_fetch_commits(&nfetched, graph, LINES, repo);
597 if (err)
598 goto done;
600 /*
601 * Open the initial batch of commits, sorted in commit graph order.
602 * We keep all commits open throughout the lifetime of the log view
603 * in order to avoid having to re-fetch commits from disk while
604 * updating the display.
605 */
606 err = queue_commits(graph, &commits, head_id, repo);
607 if (err && err->code != GOT_ERR_ITER_COMPLETED)
608 goto done;
610 /* Find entry corresponding to the first commit to display. */
611 TAILQ_FOREACH(entry, &commits, entry) {
612 if (got_object_id_cmp(entry->id, start_id) == 0) {
613 first_displayed_entry = entry;
614 break;
617 if (first_displayed_entry == NULL) {
618 err = got_error(GOT_ERR_NO_OBJ);
619 goto done;
622 while (!done) {
623 err = draw_commits(&last_displayed_entry, &selected_entry,
624 first_displayed_entry, selected, LINES);
625 if (err)
626 goto done;
628 nodelay(stdscr, FALSE);
629 ch = wgetch(tog_log_view.window);
630 nodelay(stdscr, TRUE);
631 switch (ch) {
632 case ERR:
633 if (errno) {
634 err = got_error_from_errno();
635 goto done;
637 break;
638 case 'q':
639 done = 1;
640 break;
641 case 'k':
642 case KEY_UP:
643 if (selected > 0)
644 selected--;
645 if (selected > 0)
646 break;
647 scroll_up(&first_displayed_entry, 1, &commits);
648 break;
649 case KEY_PPAGE:
650 if (TAILQ_FIRST(&commits) ==
651 first_displayed_entry) {
652 selected = 0;
653 break;
655 scroll_up(&first_displayed_entry, LINES,
656 &commits);
657 break;
658 case 'j':
659 case KEY_DOWN:
660 nparents = num_parents(first_displayed_entry);
661 if (selected < LINES - 1 &&
662 selected < nparents - 1) {
663 selected++;
664 break;
666 err = scroll_down(&first_displayed_entry, 1,
667 last_displayed_entry, &commits, graph,
668 repo);
669 if (err)
670 goto done;
671 break;
672 case KEY_NPAGE:
673 err = scroll_down(&first_displayed_entry, LINES,
674 last_displayed_entry, &commits, graph,
675 repo);
676 if (err)
677 goto done;
678 if (last_displayed_entry->commit->nparents > 0)
679 break;
680 /* can't scroll any further; move cursor down */
681 nparents = num_parents(first_displayed_entry);
682 if (selected < LINES - 1 ||
683 selected < nparents - 1)
684 selected = MIN(LINES - 1, nparents - 1);
685 break;
686 case KEY_RESIZE:
687 if (selected > LINES)
688 selected = LINES - 1;
689 break;
690 case KEY_ENTER:
691 case '\r':
692 err = show_commit(selected_entry, repo);
693 if (err)
694 break;
695 show_panel(tog_log_view.panel);
696 break;
697 default:
698 break;
701 done:
702 free(head_id);
703 if (graph)
704 got_commit_graph_close(graph);
705 free_commits(&commits);
706 return err;
709 static const struct got_error *
710 cmd_log(int argc, char *argv[])
712 const struct got_error *error;
713 struct got_repository *repo;
714 struct got_object_id *start_id = NULL;
715 char *repo_path = NULL;
716 char *start_commit = NULL;
717 int ch;
719 #ifndef PROFILE
720 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
721 err(1, "pledge");
722 #endif
724 while ((ch = getopt(argc, argv, "c:")) != -1) {
725 switch (ch) {
726 case 'c':
727 start_commit = optarg;
728 break;
729 default:
730 usage();
731 /* NOTREACHED */
735 argc -= optind;
736 argv += optind;
738 if (argc == 0) {
739 repo_path = getcwd(NULL, 0);
740 if (repo_path == NULL)
741 return got_error_from_errno();
742 } else if (argc == 1) {
743 repo_path = realpath(argv[0], NULL);
744 if (repo_path == NULL)
745 return got_error_from_errno();
746 } else
747 usage_log();
749 error = got_repo_open(&repo, repo_path);
750 free(repo_path);
751 if (error != NULL)
752 return error;
754 if (start_commit == NULL) {
755 error = get_head_commit_id(&start_id, repo);
756 if (error != NULL)
757 return error;
758 } else {
759 struct got_object *obj;
760 error = got_object_open_by_id_str(&obj, repo, start_commit);
761 if (error == NULL) {
762 start_id = got_object_get_id(obj);
763 if (start_id == NULL)
764 error = got_error_from_errno();
767 if (error != NULL)
768 return error;
769 error = show_log_view(start_id, repo);
770 free(start_id);
771 got_repo_close(repo);
772 return error;
775 __dead static void
776 usage_diff(void)
778 endwin();
779 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
780 getprogname());
781 exit(1);
784 static char *
785 parse_next_line(FILE *f, size_t *len)
787 char *line;
788 size_t linelen;
789 size_t lineno;
790 const char delim[3] = { '\0', '\0', '\0'};
792 line = fparseln(f, &linelen, &lineno, delim, 0);
793 if (len)
794 *len = linelen;
795 return line;
798 static const struct got_error *
799 draw_diff(FILE *f, int *first_displayed_line, int *last_displayed_line,
800 int *eof, int max_lines)
802 const struct got_error *err;
803 int nlines = 0, nprinted = 0;
804 char *line;
805 size_t len;
806 wchar_t *wline;
807 int width;
809 rewind(f);
810 werase(tog_diff_view.window);
812 *eof = 0;
813 while (nprinted < max_lines) {
814 line = parse_next_line(f, &len);
815 if (line == NULL) {
816 *eof = 1;
817 break;
819 if (++nlines < *first_displayed_line) {
820 free(line);
821 continue;
824 err = format_line(&wline, &width, line, COLS);
825 if (err) {
826 free(line);
827 return err;
829 waddwstr(tog_diff_view.window, wline);
830 if (width < COLS)
831 waddch(tog_diff_view.window, '\n');
832 if (++nprinted == 1)
833 *first_displayed_line = nlines;
834 free(line);
836 *last_displayed_line = nlines;
838 update_panels();
839 doupdate();
841 return NULL;
844 static const struct got_error *
845 show_diff_view(struct got_object *obj1, struct got_object *obj2,
846 struct got_repository *repo)
848 const struct got_error *err;
849 FILE *f;
850 int ch, done = 0, first_displayed_line = 1, last_displayed_line = LINES;
851 int eof, i;
853 if (obj1 != NULL && obj2 != NULL &&
854 got_object_get_type(obj1) != got_object_get_type(obj2))
855 return got_error(GOT_ERR_OBJ_TYPE);
857 f = got_opentemp();
858 if (f == NULL)
859 return got_error_from_errno();
861 switch (got_object_get_type(obj1 ? obj1 : obj2)) {
862 case GOT_OBJ_TYPE_BLOB:
863 err = got_diff_objects_as_blobs(obj1, obj2, repo, f);
864 break;
865 case GOT_OBJ_TYPE_TREE:
866 err = got_diff_objects_as_trees(obj1, obj2, repo, f);
867 break;
868 case GOT_OBJ_TYPE_COMMIT:
869 err = got_diff_objects_as_commits(obj1, obj2, repo, f);
870 break;
871 default:
872 return got_error(GOT_ERR_OBJ_TYPE);
875 fflush(f);
877 if (tog_diff_view.window == NULL) {
878 tog_diff_view.window = newwin(0, 0, 0, 0);
879 if (tog_diff_view.window == NULL)
880 return got_error_from_errno();
881 keypad(tog_diff_view.window, TRUE);
883 if (tog_diff_view.panel == NULL) {
884 tog_diff_view.panel = new_panel(tog_diff_view.window);
885 if (tog_diff_view.panel == NULL)
886 return got_error_from_errno();
887 } else
888 show_panel(tog_diff_view.panel);
890 while (!done) {
891 err = draw_diff(f, &first_displayed_line, &last_displayed_line,
892 &eof, LINES);
893 if (err)
894 break;
895 nodelay(stdscr, FALSE);
896 ch = wgetch(tog_diff_view.window);
897 nodelay(stdscr, TRUE);
898 switch (ch) {
899 case 'q':
900 done = 1;
901 break;
902 case 'k':
903 case KEY_UP:
904 case KEY_BACKSPACE:
905 if (first_displayed_line > 1)
906 first_displayed_line--;
907 break;
908 case KEY_PPAGE:
909 i = 0;
910 while (i++ < LINES - 1 &&
911 first_displayed_line > 1)
912 first_displayed_line--;
913 break;
914 case 'j':
915 case KEY_DOWN:
916 case KEY_ENTER:
917 case '\r':
918 if (!eof)
919 first_displayed_line++;
920 break;
921 case KEY_NPAGE:
922 case ' ':
923 i = 0;
924 while (!eof && i++ < LINES - 1) {
925 char *line = parse_next_line(f, NULL);
926 first_displayed_line++;
927 if (line == NULL)
928 break;
930 break;
931 default:
932 break;
935 fclose(f);
936 return err;
939 static const struct got_error *
940 cmd_diff(int argc, char *argv[])
942 const struct got_error *error = NULL;
943 struct got_repository *repo = NULL;
944 struct got_object *obj1 = NULL, *obj2 = NULL;
945 char *repo_path = NULL;
946 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
947 int ch;
949 #ifndef PROFILE
950 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
951 err(1, "pledge");
952 #endif
954 while ((ch = getopt(argc, argv, "")) != -1) {
955 switch (ch) {
956 default:
957 usage();
958 /* NOTREACHED */
962 argc -= optind;
963 argv += optind;
965 if (argc == 0) {
966 usage_diff(); /* TODO show local worktree changes */
967 } else if (argc == 2) {
968 repo_path = getcwd(NULL, 0);
969 if (repo_path == NULL)
970 return got_error_from_errno();
971 obj_id_str1 = argv[0];
972 obj_id_str2 = argv[1];
973 } else if (argc == 3) {
974 repo_path = realpath(argv[0], NULL);
975 if (repo_path == NULL)
976 return got_error_from_errno();
977 obj_id_str1 = argv[1];
978 obj_id_str2 = argv[2];
979 } else
980 usage_diff();
982 error = got_repo_open(&repo, repo_path);
983 free(repo_path);
984 if (error)
985 goto done;
987 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
988 if (error)
989 goto done;
991 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
992 if (error)
993 goto done;
995 error = show_diff_view(obj1, obj2, repo);
996 done:
997 got_repo_close(repo);
998 if (obj1)
999 got_object_close(obj1);
1000 if (obj2)
1001 got_object_close(obj2);
1002 return error;
1005 __dead static void
1006 usage_blame(void)
1008 endwin();
1009 fprintf(stderr, "usage: %s blame [repository-path] blob-object\n",
1010 getprogname());
1011 exit(1);
1014 static const struct got_error *
1015 cmd_blame(int argc, char *argv[])
1017 return got_error(GOT_ERR_NOT_IMPL);
1020 static void
1021 init_curses(void)
1023 initscr();
1024 cbreak();
1025 noecho();
1026 nonl();
1027 intrflush(stdscr, FALSE);
1028 keypad(stdscr, TRUE);
1029 curs_set(0);
1032 __dead static void
1033 usage(void)
1035 int i;
1037 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
1038 "Available commands:\n", getprogname());
1039 for (i = 0; i < nitems(tog_commands); i++) {
1040 struct tog_cmd *cmd = &tog_commands[i];
1041 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
1043 exit(1);
1046 static char **
1047 make_argv(const char *arg0, const char *arg1)
1049 char **argv;
1050 int argc = (arg1 == NULL ? 1 : 2);
1052 argv = calloc(argc, sizeof(char *));
1053 if (argv == NULL)
1054 err(1, "calloc");
1055 argv[0] = strdup(arg0);
1056 if (argv[0] == NULL)
1057 err(1, "calloc");
1058 if (arg1) {
1059 argv[1] = strdup(arg1);
1060 if (argv[1] == NULL)
1061 err(1, "calloc");
1064 return argv;
1067 int
1068 main(int argc, char *argv[])
1070 const struct got_error *error = NULL;
1071 struct tog_cmd *cmd = NULL;
1072 int ch, hflag = 0;
1073 char **cmd_argv = NULL;
1075 setlocale(LC_ALL, "");
1077 while ((ch = getopt(argc, argv, "h")) != -1) {
1078 switch (ch) {
1079 case 'h':
1080 hflag = 1;
1081 break;
1082 default:
1083 usage();
1084 /* NOTREACHED */
1088 argc -= optind;
1089 argv += optind;
1090 optind = 0;
1091 optreset = 1;
1093 if (argc == 0) {
1094 /* Build an argument vector which runs a default command. */
1095 cmd = &tog_commands[0];
1096 cmd_argv = make_argv(cmd->name, NULL);
1097 argc = 1;
1098 } else {
1099 int i;
1101 /* Did the user specific a command? */
1102 for (i = 0; i < nitems(tog_commands); i++) {
1103 if (strncmp(tog_commands[i].name, argv[0],
1104 strlen(argv[0])) == 0) {
1105 cmd = &tog_commands[i];
1106 if (hflag)
1107 tog_commands[i].cmd_usage();
1108 break;
1111 if (cmd == NULL) {
1112 /* Did the user specify a repository? */
1113 char *repo_path = realpath(argv[0], NULL);
1114 if (repo_path) {
1115 struct got_repository *repo;
1116 error = got_repo_open(&repo, repo_path);
1117 if (error == NULL)
1118 got_repo_close(repo);
1119 } else
1120 error = got_error_from_errno();
1121 if (error) {
1122 fprintf(stderr, "%s: '%s' is neither a known "
1123 "command nor a path to a repository\n",
1124 getprogname(), argv[0]);
1125 free(repo_path);
1126 return 1;
1128 cmd = &tog_commands[0];
1129 cmd_argv = make_argv(cmd->name, repo_path);
1130 argc = 2;
1131 free(repo_path);
1135 init_curses();
1137 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
1138 if (error)
1139 goto done;
1140 done:
1141 endwin();
1142 free(cmd_argv);
1143 if (error)
1144 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
1145 return 0;