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 err = got_error_from_errno(); /* give up */
118 goto done;
122 *ws = calloc(*wlen + 1, sizeof(*ws));
123 if (*ws == NULL) {
124 err = got_error_from_errno();
125 goto done;
128 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
129 err = got_error_from_errno();
130 done:
131 free(vis);
132 if (err) {
133 free(*ws);
134 *ws = NULL;
135 *wlen = 0;
137 return err;
140 /* Format a line for display, ensuring that it won't overflow a width limit. */
141 static const struct got_error *
142 format_line(wchar_t **wlinep, int *widthp, char *line, int wlimit)
144 const struct got_error *err = NULL;
145 int cols = 0;
146 wchar_t *wline = NULL;
147 size_t wlen;
148 int i;
150 *wlinep = NULL;
152 err = mbs2ws(&wline, &wlen, line);
153 if (err)
154 return err;
156 i = 0;
157 while (i < wlen && cols <= wlimit) {
158 int width = wcwidth(wline[i]);
159 switch (width) {
160 case 0:
161 break;
162 case 1:
163 case 2:
164 cols += width;
165 break;
166 case -1:
167 if (wline[i] == L'\t')
168 cols += TABSIZE;
169 break;
170 default:
171 err = got_error_from_errno();
172 goto done;
174 if (cols <= COLS) {
175 i++;
176 if (widthp)
177 *widthp = cols;
180 wline[i] = L'\0';
181 done:
182 if (err)
183 free(wline);
184 else
185 *wlinep = wline;
186 return err;
189 static const struct got_error *
190 draw_commit(struct got_commit_object *commit, struct got_object_id *id)
192 const struct got_error *err = NULL;
193 char *logmsg0 = NULL, *logmsg = NULL;
194 char *author0 = NULL, *author = NULL;
195 wchar_t *wlogmsg = NULL, *wauthor = NULL;
196 int author_width, logmsg_width;
197 char *newline, *smallerthan;
198 char *line = NULL;
199 char *id_str = NULL;
200 size_t id_len;
201 int col, limit;
202 static const size_t id_display_cols = 8;
203 static const size_t author_display_cols = 16;
204 const int avail = COLS;
206 err = got_object_id_str(&id_str, id);
207 if (err)
208 return err;
209 id_len = strlen(id_str);
210 if (avail < id_display_cols) {
211 limit = MIN(id_len, avail);
212 waddnstr(tog_log_view.window, id_str, limit);
213 } else {
214 limit = MIN(id_display_cols, id_len);
215 waddnstr(tog_log_view.window, id_str, limit);
217 col = limit + 1;
218 while (col <= avail && col < id_display_cols + 2) {
219 waddch(tog_log_view.window, ' ');
220 col++;
222 if (col > avail)
223 goto done;
225 author0 = strdup(commit->author);
226 if (author0 == NULL) {
227 err = got_error_from_errno();
228 goto done;
230 author = author0;
231 smallerthan = strchr(author, '<');
232 if (smallerthan)
233 *smallerthan = '\0';
234 else {
235 char *at = strchr(author, '@');
236 if (at)
237 *at = '\0';
239 limit = MIN(avail, author_display_cols);
240 err = format_line(&wauthor, &author_width, author, limit);
241 if (err)
242 goto done;
243 waddwstr(tog_log_view.window, wauthor);
244 col += author_width;
245 while (col <= avail && author_width < author_display_cols + 1) {
246 waddch(tog_log_view.window, ' ');
247 col++;
248 author_width++;
250 if (col > avail)
251 goto done;
253 logmsg0 = strdup(commit->logmsg);
254 if (logmsg0 == NULL) {
255 err = got_error_from_errno();
256 goto done;
258 logmsg = logmsg0;
259 while (*logmsg == '\n')
260 logmsg++;
261 newline = strchr(logmsg, '\n');
262 if (newline)
263 *newline = '\0';
264 limit = avail - col;
265 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
266 if (err)
267 goto done;
268 waddwstr(tog_log_view.window, wlogmsg);
269 col += logmsg_width;
270 while (col <= avail) {
271 waddch(tog_log_view.window, ' ');
272 col++;
274 done:
275 free(logmsg0);
276 free(wlogmsg);
277 free(author0);
278 free(wauthor);
279 free(line);
280 free(id_str);
281 return err;
284 struct commit_queue_entry {
285 TAILQ_ENTRY(commit_queue_entry) entry;
286 struct got_object_id *id;
287 struct got_commit_object *commit;
288 };
289 TAILQ_HEAD(commit_queue, commit_queue_entry);
291 static struct commit_queue_entry *
292 alloc_commit_queue_entry(struct got_commit_object *commit,
293 struct got_object_id *id)
295 struct commit_queue_entry *entry;
297 entry = calloc(1, sizeof(*entry));
298 if (entry == NULL)
299 return NULL;
301 entry->id = id;
302 entry->commit = commit;
303 return entry;
306 static void
307 pop_commit(struct commit_queue *commits)
309 struct commit_queue_entry *entry;
311 entry = TAILQ_FIRST(commits);
312 TAILQ_REMOVE(commits, entry, entry);
313 got_object_commit_close(entry->commit);
314 /* Don't free entry->id! It is owned by the commit graph. */
315 free(entry);
318 static void
319 free_commits(struct commit_queue *commits)
321 while (!TAILQ_EMPTY(commits))
322 pop_commit(commits);
325 static const struct got_error *
326 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
327 struct got_object_id *start_id, struct got_repository *repo)
329 const struct got_error *err = NULL;
330 struct got_object_id *id;
331 struct commit_queue_entry *entry;
333 err = got_commit_graph_iter_start(graph, start_id);
334 if (err)
335 return err;
337 entry = TAILQ_LAST(commits, commit_queue);
338 if (entry && got_object_id_cmp(entry->id, start_id) == 0) {
339 int nfetched;
341 /* Start ID's commit is already on the queue; skip over it. */
342 err = got_commit_graph_iter_next(&id, graph);
343 if (err && err->code != GOT_ERR_ITER_NEED_MORE)
344 return err;
346 err = got_commit_graph_fetch_commits(&nfetched, graph, 1, repo);
347 if (err)
348 return err;
351 while (1) {
352 struct got_commit_object *commit;
354 err = got_commit_graph_iter_next(&id, graph);
355 if (err) {
356 if (err->code == GOT_ERR_ITER_NEED_MORE)
357 err = NULL;
358 break;
361 err = got_object_open_as_commit(&commit, repo, id);
362 if (err)
363 break;
365 entry = alloc_commit_queue_entry(commit, id);
366 if (entry == NULL) {
367 err = got_error_from_errno();
368 break;
371 TAILQ_INSERT_TAIL(commits, entry, entry);
374 return err;
377 static const struct got_error *
378 fetch_next_commit(struct commit_queue_entry **pentry,
379 struct commit_queue_entry *entry, struct commit_queue *commits,
380 struct got_commit_graph *graph, struct got_repository *repo)
382 const struct got_error *err = NULL;
383 struct got_object_qid *qid;
385 *pentry = NULL;
387 /* Populate commit graph with entry's parent commits. */
388 SIMPLEQ_FOREACH(qid, &entry->commit->parent_ids, entry) {
389 int nfetched;
390 err = got_commit_graph_fetch_commits_up_to(&nfetched,
391 graph, qid->id, repo);
392 if (err)
393 return err;
396 /* Append outstanding commits to queue in graph sort order. */
397 err = queue_commits(graph, commits, entry->id, repo);
398 if (err) {
399 if (err->code == GOT_ERR_ITER_COMPLETED)
400 err = NULL;
401 return err;
404 /* Next entry to display should now be available. */
405 *pentry = TAILQ_NEXT(entry, entry);
406 if (*pentry == NULL)
407 return got_error(GOT_ERR_NO_OBJ);
409 return NULL;
412 static const struct got_error *
413 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
415 const struct got_error *err = NULL;
416 struct got_reference *head_ref;
418 *head_id = NULL;
420 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
421 if (err)
422 return err;
424 err = got_ref_resolve(head_id, repo, head_ref);
425 got_ref_close(head_ref);
426 if (err) {
427 *head_id = NULL;
428 return err;
431 return NULL;
434 static const struct got_error *
435 draw_commits(struct commit_queue_entry **last, struct commit_queue_entry **selected,
436 struct commit_queue_entry *first, int selected_idx, int limit)
438 const struct got_error *err = NULL;
439 struct commit_queue_entry *entry;
440 int ncommits = 0;
442 werase(tog_log_view.window);
444 entry = first;
445 *last = first;
446 while (entry) {
447 if (ncommits == limit)
448 break;
449 if (ncommits == selected_idx) {
450 wstandout(tog_log_view.window);
451 *selected = entry;
453 err = draw_commit(entry->commit, entry->id);
454 if (ncommits == selected_idx)
455 wstandend(tog_log_view.window);
456 if (err)
457 break;
458 ncommits++;
459 *last = entry;
460 entry = TAILQ_NEXT(entry, entry);
463 update_panels();
464 doupdate();
466 return err;
469 static void
470 scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
471 struct commit_queue *commits)
473 struct commit_queue_entry *entry;
474 int nscrolled = 0;
476 entry = TAILQ_FIRST(commits);
477 if (*first_displayed_entry == entry)
478 return;
480 entry = *first_displayed_entry;
481 while (entry && nscrolled < maxscroll) {
482 entry = TAILQ_PREV(entry, commit_queue, entry);
483 if (entry) {
484 *first_displayed_entry = entry;
485 nscrolled++;
490 static const struct got_error *
491 scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
492 struct commit_queue_entry *last_displayed_entry,
493 struct commit_queue *commits, struct got_commit_graph *graph,
494 struct got_repository *repo)
496 const struct got_error *err = NULL;
497 struct commit_queue_entry *pentry;
498 int nscrolled = 0;
500 do {
501 pentry = TAILQ_NEXT(last_displayed_entry, entry);
502 if (pentry == NULL) {
503 err = fetch_next_commit(&pentry, last_displayed_entry,
504 commits, graph, repo);
505 if (err || pentry == NULL)
506 break;
508 last_displayed_entry = pentry;
510 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
511 if (pentry == NULL)
512 break;
513 *first_displayed_entry = pentry;
514 } while (++nscrolled < maxscroll);
516 return err;
519 static int
520 num_parents(struct commit_queue_entry *entry)
522 int nparents = 0;
524 while (entry) {
525 entry = TAILQ_NEXT(entry, entry);
526 nparents++;
529 return nparents;
532 static const struct got_error *
533 show_commit(struct commit_queue_entry *entry, struct got_repository *repo)
535 const struct got_error *err;
536 struct got_object *obj1 = NULL, *obj2 = NULL;
537 struct got_object_qid *parent_id;
539 err = got_object_open(&obj2, repo, entry->id);
540 if (err)
541 return err;
543 parent_id = SIMPLEQ_FIRST(&entry->commit->parent_ids);
544 if (parent_id) {
545 err = got_object_open(&obj1, repo, parent_id->id);
546 if (err)
547 goto done;
550 err = show_diff_view(obj1, obj2, repo);
551 done:
552 if (obj1)
553 got_object_close(obj1);
554 if (obj2)
555 got_object_close(obj2);
556 return err;
559 static const struct got_error *
560 show_log_view(struct got_object_id *start_id, struct got_repository *repo)
562 const struct got_error *err = NULL;
563 struct got_object_id *head_id = NULL;
564 int ch, done = 0, selected = 0, nparents, nfetched;
565 struct got_commit_graph *graph;
566 struct commit_queue commits;
567 struct commit_queue_entry *entry = NULL;
568 struct commit_queue_entry *first_displayed_entry = NULL;
569 struct commit_queue_entry *last_displayed_entry = NULL;
570 struct commit_queue_entry *selected_entry = NULL;
572 if (tog_log_view.window == NULL) {
573 tog_log_view.window = newwin(0, 0, 0, 0);
574 if (tog_log_view.window == NULL)
575 return got_error_from_errno();
576 keypad(tog_log_view.window, TRUE);
578 if (tog_log_view.panel == NULL) {
579 tog_log_view.panel = new_panel(tog_log_view.window);
580 if (tog_log_view.panel == NULL)
581 return got_error_from_errno();
582 } else
583 show_panel(tog_log_view.panel);
585 err = get_head_commit_id(&head_id, repo);
586 if (err)
587 return err;
589 TAILQ_INIT(&commits);
591 err = got_commit_graph_open(&graph, head_id, repo);
592 if (err)
593 goto done;
595 /* Populate commit graph with a sufficient number of commits. */
596 err = got_commit_graph_fetch_commits_up_to(&nfetched, graph, start_id,
597 repo);
598 if (err)
599 goto done;
600 err = got_commit_graph_fetch_commits(&nfetched, graph, LINES, repo);
601 if (err)
602 goto done;
604 /*
605 * Open the initial batch of commits, sorted in commit graph order.
606 * We keep all commits open throughout the lifetime of the log view
607 * in order to avoid having to re-fetch commits from disk while
608 * updating the display.
609 */
610 err = queue_commits(graph, &commits, head_id, repo);
611 if (err && err->code != GOT_ERR_ITER_COMPLETED)
612 goto done;
614 /* Find entry corresponding to the first commit to display. */
615 TAILQ_FOREACH(entry, &commits, entry) {
616 if (got_object_id_cmp(entry->id, start_id) == 0) {
617 first_displayed_entry = entry;
618 break;
621 if (first_displayed_entry == NULL) {
622 err = got_error(GOT_ERR_NO_OBJ);
623 goto done;
626 while (!done) {
627 err = draw_commits(&last_displayed_entry, &selected_entry,
628 first_displayed_entry, selected, LINES);
629 if (err)
630 goto done;
632 nodelay(stdscr, FALSE);
633 ch = wgetch(tog_log_view.window);
634 nodelay(stdscr, TRUE);
635 switch (ch) {
636 case ERR:
637 if (errno) {
638 err = got_error_from_errno();
639 goto done;
641 break;
642 case 'q':
643 done = 1;
644 break;
645 case 'k':
646 case KEY_UP:
647 if (selected > 0)
648 selected--;
649 if (selected > 0)
650 break;
651 scroll_up(&first_displayed_entry, 1, &commits);
652 break;
653 case KEY_PPAGE:
654 if (TAILQ_FIRST(&commits) ==
655 first_displayed_entry) {
656 selected = 0;
657 break;
659 scroll_up(&first_displayed_entry, LINES,
660 &commits);
661 break;
662 case 'j':
663 case KEY_DOWN:
664 nparents = num_parents(first_displayed_entry);
665 if (selected < LINES - 1 &&
666 selected < nparents - 1) {
667 selected++;
668 break;
670 err = scroll_down(&first_displayed_entry, 1,
671 last_displayed_entry, &commits, graph,
672 repo);
673 if (err)
674 goto done;
675 break;
676 case KEY_NPAGE:
677 err = scroll_down(&first_displayed_entry, LINES,
678 last_displayed_entry, &commits, graph,
679 repo);
680 if (err)
681 goto done;
682 if (last_displayed_entry->commit->nparents > 0)
683 break;
684 /* can't scroll any further; move cursor down */
685 nparents = num_parents(first_displayed_entry);
686 if (selected < LINES - 1 ||
687 selected < nparents - 1)
688 selected = MIN(LINES - 1, nparents - 1);
689 break;
690 case KEY_RESIZE:
691 if (selected > LINES)
692 selected = LINES - 1;
693 break;
694 case KEY_ENTER:
695 case '\r':
696 err = show_commit(selected_entry, repo);
697 if (err)
698 break;
699 show_panel(tog_log_view.panel);
700 break;
701 default:
702 break;
705 done:
706 free(head_id);
707 if (graph)
708 got_commit_graph_close(graph);
709 free_commits(&commits);
710 return err;
713 static const struct got_error *
714 cmd_log(int argc, char *argv[])
716 const struct got_error *error;
717 struct got_repository *repo;
718 struct got_object_id *start_id = NULL;
719 char *repo_path = NULL;
720 char *start_commit = NULL;
721 int ch;
723 #ifndef PROFILE
724 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
725 err(1, "pledge");
726 #endif
728 while ((ch = getopt(argc, argv, "c:")) != -1) {
729 switch (ch) {
730 case 'c':
731 start_commit = optarg;
732 break;
733 default:
734 usage();
735 /* NOTREACHED */
739 argc -= optind;
740 argv += optind;
742 if (argc == 0) {
743 repo_path = getcwd(NULL, 0);
744 if (repo_path == NULL)
745 return got_error_from_errno();
746 } else if (argc == 1) {
747 repo_path = realpath(argv[0], NULL);
748 if (repo_path == NULL)
749 return got_error_from_errno();
750 } else
751 usage_log();
753 error = got_repo_open(&repo, repo_path);
754 free(repo_path);
755 if (error != NULL)
756 return error;
758 if (start_commit == NULL) {
759 error = get_head_commit_id(&start_id, repo);
760 if (error != NULL)
761 return error;
762 } else {
763 struct got_object *obj;
764 error = got_object_open_by_id_str(&obj, repo, start_commit);
765 if (error == NULL) {
766 start_id = got_object_get_id(obj);
767 if (start_id == NULL)
768 error = got_error_from_errno();
771 if (error != NULL)
772 return error;
773 error = show_log_view(start_id, repo);
774 free(start_id);
775 got_repo_close(repo);
776 return error;
779 __dead static void
780 usage_diff(void)
782 endwin();
783 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
784 getprogname());
785 exit(1);
788 static char *
789 parse_next_line(FILE *f, size_t *len)
791 char *line;
792 size_t linelen;
793 size_t lineno;
794 const char delim[3] = { '\0', '\0', '\0'};
796 line = fparseln(f, &linelen, &lineno, delim, 0);
797 if (len)
798 *len = linelen;
799 return line;
802 static const struct got_error *
803 draw_diff(FILE *f, int *first_displayed_line, int *last_displayed_line,
804 int *eof, int max_lines)
806 const struct got_error *err;
807 int nlines = 0, nprinted = 0;
808 char *line;
809 size_t len;
810 wchar_t *wline;
811 int width;
813 rewind(f);
814 werase(tog_diff_view.window);
816 *eof = 0;
817 while (nprinted < max_lines) {
818 line = parse_next_line(f, &len);
819 if (line == NULL) {
820 *eof = 1;
821 break;
823 if (++nlines < *first_displayed_line) {
824 free(line);
825 continue;
828 err = format_line(&wline, &width, line, COLS);
829 if (err) {
830 free(line);
831 return err;
833 waddwstr(tog_diff_view.window, wline);
834 if (width < COLS)
835 waddch(tog_diff_view.window, '\n');
836 if (++nprinted == 1)
837 *first_displayed_line = nlines;
838 free(line);
840 *last_displayed_line = nlines;
842 update_panels();
843 doupdate();
845 return NULL;
848 static const struct got_error *
849 show_diff_view(struct got_object *obj1, struct got_object *obj2,
850 struct got_repository *repo)
852 const struct got_error *err;
853 FILE *f;
854 int ch, done = 0, first_displayed_line = 1, last_displayed_line = LINES;
855 int eof, i;
857 if (obj1 != NULL && obj2 != NULL &&
858 got_object_get_type(obj1) != got_object_get_type(obj2))
859 return got_error(GOT_ERR_OBJ_TYPE);
861 f = got_opentemp();
862 if (f == NULL)
863 return got_error_from_errno();
865 switch (got_object_get_type(obj1 ? obj1 : obj2)) {
866 case GOT_OBJ_TYPE_BLOB:
867 err = got_diff_objects_as_blobs(obj1, obj2, repo, f);
868 break;
869 case GOT_OBJ_TYPE_TREE:
870 err = got_diff_objects_as_trees(obj1, obj2, repo, f);
871 break;
872 case GOT_OBJ_TYPE_COMMIT:
873 err = got_diff_objects_as_commits(obj1, obj2, repo, f);
874 break;
875 default:
876 return got_error(GOT_ERR_OBJ_TYPE);
879 fflush(f);
881 if (tog_diff_view.window == NULL) {
882 tog_diff_view.window = newwin(0, 0, 0, 0);
883 if (tog_diff_view.window == NULL)
884 return got_error_from_errno();
885 keypad(tog_diff_view.window, TRUE);
887 if (tog_diff_view.panel == NULL) {
888 tog_diff_view.panel = new_panel(tog_diff_view.window);
889 if (tog_diff_view.panel == NULL)
890 return got_error_from_errno();
891 } else
892 show_panel(tog_diff_view.panel);
894 while (!done) {
895 err = draw_diff(f, &first_displayed_line, &last_displayed_line,
896 &eof, LINES);
897 if (err)
898 break;
899 nodelay(stdscr, FALSE);
900 ch = wgetch(tog_diff_view.window);
901 nodelay(stdscr, TRUE);
902 switch (ch) {
903 case 'q':
904 done = 1;
905 break;
906 case 'k':
907 case KEY_UP:
908 case KEY_BACKSPACE:
909 if (first_displayed_line > 1)
910 first_displayed_line--;
911 break;
912 case KEY_PPAGE:
913 i = 0;
914 while (i++ < LINES - 1 &&
915 first_displayed_line > 1)
916 first_displayed_line--;
917 break;
918 case 'j':
919 case KEY_DOWN:
920 case KEY_ENTER:
921 case '\r':
922 if (!eof)
923 first_displayed_line++;
924 break;
925 case KEY_NPAGE:
926 case ' ':
927 i = 0;
928 while (!eof && i++ < LINES - 1) {
929 char *line = parse_next_line(f, NULL);
930 first_displayed_line++;
931 if (line == NULL)
932 break;
934 break;
935 default:
936 break;
939 fclose(f);
940 return err;
943 static const struct got_error *
944 cmd_diff(int argc, char *argv[])
946 const struct got_error *error = NULL;
947 struct got_repository *repo = NULL;
948 struct got_object *obj1 = NULL, *obj2 = NULL;
949 char *repo_path = NULL;
950 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
951 int ch;
953 #ifndef PROFILE
954 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
955 err(1, "pledge");
956 #endif
958 while ((ch = getopt(argc, argv, "")) != -1) {
959 switch (ch) {
960 default:
961 usage();
962 /* NOTREACHED */
966 argc -= optind;
967 argv += optind;
969 if (argc == 0) {
970 usage_diff(); /* TODO show local worktree changes */
971 } else if (argc == 2) {
972 repo_path = getcwd(NULL, 0);
973 if (repo_path == NULL)
974 return got_error_from_errno();
975 obj_id_str1 = argv[0];
976 obj_id_str2 = argv[1];
977 } else if (argc == 3) {
978 repo_path = realpath(argv[0], NULL);
979 if (repo_path == NULL)
980 return got_error_from_errno();
981 obj_id_str1 = argv[1];
982 obj_id_str2 = argv[2];
983 } else
984 usage_diff();
986 error = got_repo_open(&repo, repo_path);
987 free(repo_path);
988 if (error)
989 goto done;
991 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
992 if (error)
993 goto done;
995 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
996 if (error)
997 goto done;
999 error = show_diff_view(obj1, obj2, repo);
1000 done:
1001 got_repo_close(repo);
1002 if (obj1)
1003 got_object_close(obj1);
1004 if (obj2)
1005 got_object_close(obj2);
1006 return error;
1009 __dead static void
1010 usage_blame(void)
1012 endwin();
1013 fprintf(stderr, "usage: %s blame [repository-path] blob-object\n",
1014 getprogname());
1015 exit(1);
1018 static const struct got_error *
1019 cmd_blame(int argc, char *argv[])
1021 return got_error(GOT_ERR_NOT_IMPL);
1024 static void
1025 init_curses(void)
1027 initscr();
1028 cbreak();
1029 noecho();
1030 nonl();
1031 intrflush(stdscr, FALSE);
1032 keypad(stdscr, TRUE);
1033 curs_set(0);
1036 __dead static void
1037 usage(void)
1039 int i;
1041 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
1042 "Available commands:\n", getprogname());
1043 for (i = 0; i < nitems(tog_commands); i++) {
1044 struct tog_cmd *cmd = &tog_commands[i];
1045 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
1047 exit(1);
1050 static char **
1051 make_argv(const char *arg0, const char *arg1)
1053 char **argv;
1054 int argc = (arg1 == NULL ? 1 : 2);
1056 argv = calloc(argc, sizeof(char *));
1057 if (argv == NULL)
1058 err(1, "calloc");
1059 argv[0] = strdup(arg0);
1060 if (argv[0] == NULL)
1061 err(1, "calloc");
1062 if (arg1) {
1063 argv[1] = strdup(arg1);
1064 if (argv[1] == NULL)
1065 err(1, "calloc");
1068 return argv;
1071 int
1072 main(int argc, char *argv[])
1074 const struct got_error *error = NULL;
1075 struct tog_cmd *cmd = NULL;
1076 int ch, hflag = 0;
1077 char **cmd_argv = NULL;
1079 setlocale(LC_ALL, "");
1081 while ((ch = getopt(argc, argv, "h")) != -1) {
1082 switch (ch) {
1083 case 'h':
1084 hflag = 1;
1085 break;
1086 default:
1087 usage();
1088 /* NOTREACHED */
1092 argc -= optind;
1093 argv += optind;
1094 optind = 0;
1095 optreset = 1;
1097 if (argc == 0) {
1098 /* Build an argument vector which runs a default command. */
1099 cmd = &tog_commands[0];
1100 cmd_argv = make_argv(cmd->name, NULL);
1101 argc = 1;
1102 } else {
1103 int i;
1105 /* Did the user specific a command? */
1106 for (i = 0; i < nitems(tog_commands); i++) {
1107 if (strncmp(tog_commands[i].name, argv[0],
1108 strlen(argv[0])) == 0) {
1109 cmd = &tog_commands[i];
1110 if (hflag)
1111 tog_commands[i].cmd_usage();
1112 break;
1115 if (cmd == NULL) {
1116 /* Did the user specify a repository? */
1117 char *repo_path = realpath(argv[0], NULL);
1118 if (repo_path) {
1119 struct got_repository *repo;
1120 error = got_repo_open(&repo, repo_path);
1121 if (error == NULL)
1122 got_repo_close(repo);
1123 } else
1124 error = got_error_from_errno();
1125 if (error) {
1126 fprintf(stderr, "%s: '%s' is neither a known "
1127 "command nor a path to a repository\n",
1128 getprogname(), argv[0]);
1129 free(repo_path);
1130 return 1;
1132 cmd = &tog_commands[0];
1133 cmd_argv = make_argv(cmd->name, repo_path);
1134 argc = 2;
1135 free(repo_path);
1139 init_curses();
1141 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
1142 if (error)
1143 goto done;
1144 done:
1145 endwin();
1146 free(cmd_argv);
1147 if (error)
1148 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
1149 return 0;