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"
44 #include "got_blame.h"
46 #ifndef MIN
47 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
48 #endif
50 #ifndef nitems
51 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
52 #endif
54 struct tog_cmd {
55 const char *name;
56 const struct got_error *(*cmd_main)(int, char *[]);
57 void (*cmd_usage)(void);
58 const char *descr;
59 };
61 __dead static void usage(void);
62 __dead static void usage_log(void);
63 __dead static void usage_diff(void);
64 __dead static void usage_blame(void);
66 static const struct got_error* cmd_log(int, char *[]);
67 static const struct got_error* cmd_diff(int, char *[]);
68 static const struct got_error* cmd_blame(int, char *[]);
70 static struct tog_cmd tog_commands[] = {
71 { "log", cmd_log, usage_log,
72 "show repository history" },
73 { "diff", cmd_diff, usage_diff,
74 "compare files and directories" },
75 { "blame", cmd_blame, usage_blame,
76 "show line-by-line file history" },
77 };
79 static struct tog_view {
80 WINDOW *window;
81 PANEL *panel;
82 } tog_log_view, tog_diff_view, tog_blame_view;
84 static const struct got_error *
85 show_diff_view(struct got_object *, struct got_object *,
86 struct got_repository *);
87 static const struct got_error *
88 show_log_view(struct got_object_id *, struct got_repository *);
89 static const struct got_error *
90 show_blame_view(const char *, struct got_object_id *, struct got_repository *);
92 __dead static void
93 usage_log(void)
94 {
95 endwin();
96 fprintf(stderr, "usage: %s log [-c commit] [repository-path]\n",
97 getprogname());
98 exit(1);
99 }
101 /* Create newly allocated wide-character string equivalent to a byte string. */
102 static const struct got_error *
103 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
105 char *vis = NULL;
106 const struct got_error *err = NULL;
108 *ws = NULL;
109 *wlen = mbstowcs(NULL, s, 0);
110 if (*wlen == (size_t)-1) {
111 int vislen;
112 if (errno != EILSEQ)
113 return got_error_from_errno();
115 /* byte string invalid in current encoding; try to "fix" it */
116 err = got_mbsavis(&vis, &vislen, s);
117 if (err)
118 return err;
119 *wlen = mbstowcs(NULL, vis, 0);
120 if (*wlen == (size_t)-1) {
121 err = got_error_from_errno(); /* give up */
122 goto done;
126 *ws = calloc(*wlen + 1, sizeof(*ws));
127 if (*ws == NULL) {
128 err = got_error_from_errno();
129 goto done;
132 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
133 err = got_error_from_errno();
134 done:
135 free(vis);
136 if (err) {
137 free(*ws);
138 *ws = NULL;
139 *wlen = 0;
141 return err;
144 /* Format a line for display, ensuring that it won't overflow a width limit. */
145 static const struct got_error *
146 format_line(wchar_t **wlinep, int *widthp, char *line, int wlimit)
148 const struct got_error *err = NULL;
149 int cols = 0;
150 wchar_t *wline = NULL;
151 size_t wlen;
152 int i;
154 *wlinep = NULL;
156 err = mbs2ws(&wline, &wlen, line);
157 if (err)
158 return err;
160 i = 0;
161 while (i < wlen && cols <= wlimit) {
162 int width = wcwidth(wline[i]);
163 switch (width) {
164 case 0:
165 break;
166 case 1:
167 case 2:
168 cols += width;
169 break;
170 case -1:
171 if (wline[i] == L'\t')
172 cols += TABSIZE;
173 break;
174 default:
175 err = got_error_from_errno();
176 goto done;
178 if (cols <= COLS) {
179 i++;
180 if (widthp)
181 *widthp = cols;
184 wline[i] = L'\0';
185 done:
186 if (err)
187 free(wline);
188 else
189 *wlinep = wline;
190 return err;
193 static const struct got_error *
194 draw_commit(struct got_commit_object *commit, struct got_object_id *id)
196 const struct got_error *err = NULL;
197 char *logmsg0 = NULL, *logmsg = NULL;
198 char *author0 = NULL, *author = NULL;
199 wchar_t *wlogmsg = NULL, *wauthor = NULL;
200 int author_width, logmsg_width;
201 char *newline, *smallerthan;
202 char *line = NULL;
203 char *id_str = NULL;
204 size_t id_len;
205 int col, limit;
206 static const size_t id_display_cols = 8;
207 static const size_t author_display_cols = 16;
208 const int avail = COLS;
210 err = got_object_id_str(&id_str, id);
211 if (err)
212 return err;
213 id_len = strlen(id_str);
214 if (avail < id_display_cols) {
215 limit = MIN(id_len, avail);
216 waddnstr(tog_log_view.window, id_str, limit);
217 } else {
218 limit = MIN(id_display_cols, id_len);
219 waddnstr(tog_log_view.window, id_str, limit);
221 col = limit + 1;
222 while (col <= avail && col < id_display_cols + 2) {
223 waddch(tog_log_view.window, ' ');
224 col++;
226 if (col > avail)
227 goto done;
229 author0 = strdup(commit->author);
230 if (author0 == NULL) {
231 err = got_error_from_errno();
232 goto done;
234 author = author0;
235 smallerthan = strchr(author, '<');
236 if (smallerthan)
237 *smallerthan = '\0';
238 else {
239 char *at = strchr(author, '@');
240 if (at)
241 *at = '\0';
243 limit = MIN(avail, author_display_cols);
244 err = format_line(&wauthor, &author_width, author, limit);
245 if (err)
246 goto done;
247 waddwstr(tog_log_view.window, wauthor);
248 col += author_width;
249 while (col <= avail && author_width < author_display_cols + 1) {
250 waddch(tog_log_view.window, ' ');
251 col++;
252 author_width++;
254 if (col > avail)
255 goto done;
257 logmsg0 = strdup(commit->logmsg);
258 if (logmsg0 == NULL) {
259 err = got_error_from_errno();
260 goto done;
262 logmsg = logmsg0;
263 while (*logmsg == '\n')
264 logmsg++;
265 newline = strchr(logmsg, '\n');
266 if (newline)
267 *newline = '\0';
268 limit = avail - col;
269 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
270 if (err)
271 goto done;
272 waddwstr(tog_log_view.window, wlogmsg);
273 col += logmsg_width;
274 while (col <= avail) {
275 waddch(tog_log_view.window, ' ');
276 col++;
278 done:
279 free(logmsg0);
280 free(wlogmsg);
281 free(author0);
282 free(wauthor);
283 free(line);
284 free(id_str);
285 return err;
288 struct commit_queue_entry {
289 TAILQ_ENTRY(commit_queue_entry) entry;
290 struct got_object_id *id;
291 struct got_commit_object *commit;
292 };
293 TAILQ_HEAD(commit_queue, commit_queue_entry);
295 static struct commit_queue_entry *
296 alloc_commit_queue_entry(struct got_commit_object *commit,
297 struct got_object_id *id)
299 struct commit_queue_entry *entry;
301 entry = calloc(1, sizeof(*entry));
302 if (entry == NULL)
303 return NULL;
305 entry->id = id;
306 entry->commit = commit;
307 return entry;
310 static void
311 pop_commit(struct commit_queue *commits)
313 struct commit_queue_entry *entry;
315 entry = TAILQ_FIRST(commits);
316 TAILQ_REMOVE(commits, entry, entry);
317 got_object_commit_close(entry->commit);
318 /* Don't free entry->id! It is owned by the commit graph. */
319 free(entry);
322 static void
323 free_commits(struct commit_queue *commits)
325 while (!TAILQ_EMPTY(commits))
326 pop_commit(commits);
329 static const struct got_error *
330 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
331 struct got_object_id *start_id, struct got_repository *repo)
333 const struct got_error *err = NULL;
334 struct got_object_id *id;
335 struct commit_queue_entry *entry;
337 err = got_commit_graph_iter_start(graph, start_id);
338 if (err)
339 return err;
341 entry = TAILQ_LAST(commits, commit_queue);
342 if (entry && got_object_id_cmp(entry->id, start_id) == 0) {
343 int nfetched;
345 /* Start ID's commit is already on the queue; skip over it. */
346 err = got_commit_graph_iter_next(&id, graph);
347 if (err && err->code != GOT_ERR_ITER_NEED_MORE)
348 return err;
350 err = got_commit_graph_fetch_commits(&nfetched, graph, 1, repo);
351 if (err)
352 return err;
355 while (1) {
356 struct got_commit_object *commit;
358 err = got_commit_graph_iter_next(&id, graph);
359 if (err) {
360 if (err->code == GOT_ERR_ITER_NEED_MORE)
361 err = NULL;
362 break;
365 err = got_object_open_as_commit(&commit, repo, id);
366 if (err)
367 break;
369 entry = alloc_commit_queue_entry(commit, id);
370 if (entry == NULL) {
371 err = got_error_from_errno();
372 break;
375 TAILQ_INSERT_TAIL(commits, entry, entry);
378 return err;
381 static const struct got_error *
382 fetch_next_commit(struct commit_queue_entry **pentry,
383 struct commit_queue_entry *entry, struct commit_queue *commits,
384 struct got_commit_graph *graph, struct got_repository *repo)
386 const struct got_error *err = NULL;
387 struct got_object_qid *qid;
389 *pentry = NULL;
391 /* Populate commit graph with entry's parent commits. */
392 SIMPLEQ_FOREACH(qid, &entry->commit->parent_ids, entry) {
393 int nfetched;
394 err = got_commit_graph_fetch_commits_up_to(&nfetched,
395 graph, qid->id, repo);
396 if (err)
397 return err;
400 /* Append outstanding commits to queue in graph sort order. */
401 err = queue_commits(graph, commits, entry->id, repo);
402 if (err) {
403 if (err->code == GOT_ERR_ITER_COMPLETED)
404 err = NULL;
405 return err;
408 /* Next entry to display should now be available. */
409 *pentry = TAILQ_NEXT(entry, entry);
410 if (*pentry == NULL)
411 return got_error(GOT_ERR_NO_OBJ);
413 return NULL;
416 static const struct got_error *
417 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
419 const struct got_error *err = NULL;
420 struct got_reference *head_ref;
422 *head_id = NULL;
424 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
425 if (err)
426 return err;
428 err = got_ref_resolve(head_id, repo, head_ref);
429 got_ref_close(head_ref);
430 if (err) {
431 *head_id = NULL;
432 return err;
435 return NULL;
438 static const struct got_error *
439 draw_commits(struct commit_queue_entry **last, struct commit_queue_entry **selected,
440 struct commit_queue_entry *first, int selected_idx, int limit)
442 const struct got_error *err = NULL;
443 struct commit_queue_entry *entry;
444 int ncommits = 0;
446 werase(tog_log_view.window);
448 entry = first;
449 *last = first;
450 while (entry) {
451 if (ncommits == limit)
452 break;
453 if (ncommits == selected_idx) {
454 wstandout(tog_log_view.window);
455 *selected = entry;
457 err = draw_commit(entry->commit, entry->id);
458 if (ncommits == selected_idx)
459 wstandend(tog_log_view.window);
460 if (err)
461 break;
462 ncommits++;
463 *last = entry;
464 entry = TAILQ_NEXT(entry, entry);
467 update_panels();
468 doupdate();
470 return err;
473 static void
474 scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
475 struct commit_queue *commits)
477 struct commit_queue_entry *entry;
478 int nscrolled = 0;
480 entry = TAILQ_FIRST(commits);
481 if (*first_displayed_entry == entry)
482 return;
484 entry = *first_displayed_entry;
485 while (entry && nscrolled < maxscroll) {
486 entry = TAILQ_PREV(entry, commit_queue, entry);
487 if (entry) {
488 *first_displayed_entry = entry;
489 nscrolled++;
494 static const struct got_error *
495 scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
496 struct commit_queue_entry *last_displayed_entry,
497 struct commit_queue *commits, struct got_commit_graph *graph,
498 struct got_repository *repo)
500 const struct got_error *err = NULL;
501 struct commit_queue_entry *pentry;
502 int nscrolled = 0;
504 do {
505 pentry = TAILQ_NEXT(last_displayed_entry, entry);
506 if (pentry == NULL) {
507 err = fetch_next_commit(&pentry, last_displayed_entry,
508 commits, graph, repo);
509 if (err || pentry == NULL)
510 break;
512 last_displayed_entry = pentry;
514 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
515 if (pentry == NULL)
516 break;
517 *first_displayed_entry = pentry;
518 } while (++nscrolled < maxscroll);
520 return err;
523 static int
524 num_parents(struct commit_queue_entry *entry)
526 int nparents = 0;
528 while (entry) {
529 entry = TAILQ_NEXT(entry, entry);
530 nparents++;
533 return nparents;
536 static const struct got_error *
537 show_commit(struct commit_queue_entry *entry, struct got_repository *repo)
539 const struct got_error *err;
540 struct got_object *obj1 = NULL, *obj2 = NULL;
541 struct got_object_qid *parent_id;
543 err = got_object_open(&obj2, repo, entry->id);
544 if (err)
545 return err;
547 parent_id = SIMPLEQ_FIRST(&entry->commit->parent_ids);
548 if (parent_id) {
549 err = got_object_open(&obj1, repo, parent_id->id);
550 if (err)
551 goto done;
554 err = show_diff_view(obj1, obj2, repo);
555 done:
556 if (obj1)
557 got_object_close(obj1);
558 if (obj2)
559 got_object_close(obj2);
560 return err;
563 static const struct got_error *
564 show_log_view(struct got_object_id *start_id, struct got_repository *repo)
566 const struct got_error *err = NULL;
567 struct got_object_id *head_id = NULL;
568 int ch, done = 0, selected = 0, nparents, nfetched;
569 struct got_commit_graph *graph;
570 struct commit_queue commits;
571 struct commit_queue_entry *entry = NULL;
572 struct commit_queue_entry *first_displayed_entry = NULL;
573 struct commit_queue_entry *last_displayed_entry = NULL;
574 struct commit_queue_entry *selected_entry = NULL;
576 if (tog_log_view.window == NULL) {
577 tog_log_view.window = newwin(0, 0, 0, 0);
578 if (tog_log_view.window == NULL)
579 return got_error_from_errno();
580 keypad(tog_log_view.window, TRUE);
582 if (tog_log_view.panel == NULL) {
583 tog_log_view.panel = new_panel(tog_log_view.window);
584 if (tog_log_view.panel == NULL)
585 return got_error_from_errno();
586 } else
587 show_panel(tog_log_view.panel);
589 err = get_head_commit_id(&head_id, repo);
590 if (err)
591 return err;
593 TAILQ_INIT(&commits);
595 err = got_commit_graph_open(&graph, head_id, 0, repo);
596 if (err)
597 goto done;
599 /* Populate commit graph with a sufficient number of commits. */
600 err = got_commit_graph_fetch_commits_up_to(&nfetched, graph, start_id,
601 repo);
602 if (err)
603 goto done;
604 err = got_commit_graph_fetch_commits(&nfetched, graph, LINES, repo);
605 if (err)
606 goto done;
608 /*
609 * Open the initial batch of commits, sorted in commit graph order.
610 * We keep all commits open throughout the lifetime of the log view
611 * in order to avoid having to re-fetch commits from disk while
612 * updating the display.
613 */
614 err = queue_commits(graph, &commits, head_id, repo);
615 if (err && err->code != GOT_ERR_ITER_COMPLETED)
616 goto done;
618 /* Find entry corresponding to the first commit to display. */
619 TAILQ_FOREACH(entry, &commits, entry) {
620 if (got_object_id_cmp(entry->id, start_id) == 0) {
621 first_displayed_entry = entry;
622 break;
625 if (first_displayed_entry == NULL) {
626 err = got_error(GOT_ERR_NO_OBJ);
627 goto done;
630 while (!done) {
631 err = draw_commits(&last_displayed_entry, &selected_entry,
632 first_displayed_entry, selected, LINES);
633 if (err)
634 goto done;
636 nodelay(stdscr, FALSE);
637 ch = wgetch(tog_log_view.window);
638 nodelay(stdscr, TRUE);
639 switch (ch) {
640 case ERR:
641 if (errno) {
642 err = got_error_from_errno();
643 goto done;
645 break;
646 case 'q':
647 done = 1;
648 break;
649 case 'k':
650 case KEY_UP:
651 if (selected > 0)
652 selected--;
653 if (selected > 0)
654 break;
655 scroll_up(&first_displayed_entry, 1, &commits);
656 break;
657 case KEY_PPAGE:
658 if (TAILQ_FIRST(&commits) ==
659 first_displayed_entry) {
660 selected = 0;
661 break;
663 scroll_up(&first_displayed_entry, LINES,
664 &commits);
665 break;
666 case 'j':
667 case KEY_DOWN:
668 nparents = num_parents(first_displayed_entry);
669 if (selected < LINES - 1 &&
670 selected < nparents - 1) {
671 selected++;
672 break;
674 err = scroll_down(&first_displayed_entry, 1,
675 last_displayed_entry, &commits, graph,
676 repo);
677 if (err)
678 goto done;
679 break;
680 case KEY_NPAGE:
681 err = scroll_down(&first_displayed_entry, LINES,
682 last_displayed_entry, &commits, graph,
683 repo);
684 if (err)
685 goto done;
686 if (last_displayed_entry->commit->nparents > 0)
687 break;
688 /* can't scroll any further; move cursor down */
689 nparents = num_parents(first_displayed_entry);
690 if (selected < LINES - 1 ||
691 selected < nparents - 1)
692 selected = MIN(LINES - 1, nparents - 1);
693 break;
694 case KEY_RESIZE:
695 if (selected > LINES)
696 selected = LINES - 1;
697 break;
698 case KEY_ENTER:
699 case '\r':
700 err = show_commit(selected_entry, repo);
701 if (err)
702 break;
703 show_panel(tog_log_view.panel);
704 break;
705 default:
706 break;
709 done:
710 free(head_id);
711 if (graph)
712 got_commit_graph_close(graph);
713 free_commits(&commits);
714 return err;
717 static const struct got_error *
718 cmd_log(int argc, char *argv[])
720 const struct got_error *error;
721 struct got_repository *repo;
722 struct got_object_id *start_id = NULL;
723 char *repo_path = NULL;
724 char *start_commit = NULL;
725 int ch;
727 #ifndef PROFILE
728 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
729 err(1, "pledge");
730 #endif
732 while ((ch = getopt(argc, argv, "c:")) != -1) {
733 switch (ch) {
734 case 'c':
735 start_commit = optarg;
736 break;
737 default:
738 usage();
739 /* NOTREACHED */
743 argc -= optind;
744 argv += optind;
746 if (argc == 0) {
747 repo_path = getcwd(NULL, 0);
748 if (repo_path == NULL)
749 return got_error_from_errno();
750 } else if (argc == 1) {
751 repo_path = realpath(argv[0], NULL);
752 if (repo_path == NULL)
753 return got_error_from_errno();
754 } else
755 usage_log();
757 error = got_repo_open(&repo, repo_path);
758 free(repo_path);
759 if (error != NULL)
760 return error;
762 if (start_commit == NULL) {
763 error = get_head_commit_id(&start_id, repo);
764 if (error != NULL)
765 return error;
766 } else {
767 struct got_object *obj;
768 error = got_object_open_by_id_str(&obj, repo, start_commit);
769 if (error == NULL) {
770 start_id = got_object_get_id(obj);
771 if (start_id == NULL)
772 error = got_error_from_errno();
775 if (error != NULL)
776 return error;
777 error = show_log_view(start_id, repo);
778 free(start_id);
779 got_repo_close(repo);
780 return error;
783 __dead static void
784 usage_diff(void)
786 endwin();
787 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
788 getprogname());
789 exit(1);
792 static char *
793 parse_next_line(FILE *f, size_t *len)
795 char *line;
796 size_t linelen;
797 size_t lineno;
798 const char delim[3] = { '\0', '\0', '\0'};
800 line = fparseln(f, &linelen, &lineno, delim, 0);
801 if (len)
802 *len = linelen;
803 return line;
806 static const struct got_error *
807 draw_file(WINDOW *window, FILE *f, int *first_displayed_line,
808 int *last_displayed_line, int *eof, int max_lines)
810 const struct got_error *err;
811 int nlines = 0, nprinted = 0;
812 char *line;
813 size_t len;
814 wchar_t *wline;
815 int width;
817 rewind(f);
818 werase(window);
820 *eof = 0;
821 while (nprinted < max_lines) {
822 line = parse_next_line(f, &len);
823 if (line == NULL) {
824 *eof = 1;
825 break;
827 if (++nlines < *first_displayed_line) {
828 free(line);
829 continue;
832 err = format_line(&wline, &width, line, COLS);
833 if (err) {
834 free(line);
835 return err;
837 waddwstr(window, wline);
838 if (width < COLS)
839 waddch(window, '\n');
840 if (++nprinted == 1)
841 *first_displayed_line = nlines;
842 free(line);
844 *last_displayed_line = nlines;
846 update_panels();
847 doupdate();
849 return NULL;
852 static const struct got_error *
853 show_diff_view(struct got_object *obj1, struct got_object *obj2,
854 struct got_repository *repo)
856 const struct got_error *err;
857 FILE *f;
858 int ch, done = 0, first_displayed_line = 1, last_displayed_line = LINES;
859 int eof, i;
861 if (obj1 != NULL && obj2 != NULL &&
862 got_object_get_type(obj1) != got_object_get_type(obj2))
863 return got_error(GOT_ERR_OBJ_TYPE);
865 f = got_opentemp();
866 if (f == NULL)
867 return got_error_from_errno();
869 switch (got_object_get_type(obj1 ? obj1 : obj2)) {
870 case GOT_OBJ_TYPE_BLOB:
871 err = got_diff_objects_as_blobs(obj1, obj2, repo, f);
872 break;
873 case GOT_OBJ_TYPE_TREE:
874 err = got_diff_objects_as_trees(obj1, obj2, repo, f);
875 break;
876 case GOT_OBJ_TYPE_COMMIT:
877 err = got_diff_objects_as_commits(obj1, obj2, repo, f);
878 break;
879 default:
880 return got_error(GOT_ERR_OBJ_TYPE);
883 fflush(f);
885 if (tog_diff_view.window == NULL) {
886 tog_diff_view.window = newwin(0, 0, 0, 0);
887 if (tog_diff_view.window == NULL)
888 return got_error_from_errno();
889 keypad(tog_diff_view.window, TRUE);
891 if (tog_diff_view.panel == NULL) {
892 tog_diff_view.panel = new_panel(tog_diff_view.window);
893 if (tog_diff_view.panel == NULL)
894 return got_error_from_errno();
895 } else
896 show_panel(tog_diff_view.panel);
898 while (!done) {
899 err = draw_file(tog_diff_view.window, f, &first_displayed_line,
900 &last_displayed_line, &eof, LINES);
901 if (err)
902 break;
903 nodelay(stdscr, FALSE);
904 ch = wgetch(tog_diff_view.window);
905 nodelay(stdscr, TRUE);
906 switch (ch) {
907 case 'q':
908 done = 1;
909 break;
910 case 'k':
911 case KEY_UP:
912 case KEY_BACKSPACE:
913 if (first_displayed_line > 1)
914 first_displayed_line--;
915 break;
916 case KEY_PPAGE:
917 i = 0;
918 while (i++ < LINES - 1 &&
919 first_displayed_line > 1)
920 first_displayed_line--;
921 break;
922 case 'j':
923 case KEY_DOWN:
924 case KEY_ENTER:
925 case '\r':
926 if (!eof)
927 first_displayed_line++;
928 break;
929 case KEY_NPAGE:
930 case ' ':
931 i = 0;
932 while (!eof && i++ < LINES - 1) {
933 char *line = parse_next_line(f, NULL);
934 first_displayed_line++;
935 if (line == NULL)
936 break;
938 break;
939 default:
940 break;
943 fclose(f);
944 return err;
947 static const struct got_error *
948 cmd_diff(int argc, char *argv[])
950 const struct got_error *error = NULL;
951 struct got_repository *repo = NULL;
952 struct got_object *obj1 = NULL, *obj2 = NULL;
953 char *repo_path = NULL;
954 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
955 int ch;
957 #ifndef PROFILE
958 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
959 err(1, "pledge");
960 #endif
962 while ((ch = getopt(argc, argv, "")) != -1) {
963 switch (ch) {
964 default:
965 usage();
966 /* NOTREACHED */
970 argc -= optind;
971 argv += optind;
973 if (argc == 0) {
974 usage_diff(); /* TODO show local worktree changes */
975 } else if (argc == 2) {
976 repo_path = getcwd(NULL, 0);
977 if (repo_path == NULL)
978 return got_error_from_errno();
979 obj_id_str1 = argv[0];
980 obj_id_str2 = argv[1];
981 } else if (argc == 3) {
982 repo_path = realpath(argv[0], NULL);
983 if (repo_path == NULL)
984 return got_error_from_errno();
985 obj_id_str1 = argv[1];
986 obj_id_str2 = argv[2];
987 } else
988 usage_diff();
990 error = got_repo_open(&repo, repo_path);
991 free(repo_path);
992 if (error)
993 goto done;
995 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
996 if (error)
997 goto done;
999 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1000 if (error)
1001 goto done;
1003 error = show_diff_view(obj1, obj2, repo);
1004 done:
1005 got_repo_close(repo);
1006 if (obj1)
1007 got_object_close(obj1);
1008 if (obj2)
1009 got_object_close(obj2);
1010 return error;
1013 __dead static void
1014 usage_blame(void)
1016 endwin();
1017 fprintf(stderr, "usage: %s blame [-c commit] [repository-path] path\n",
1018 getprogname());
1019 exit(1);
1022 static const struct got_error *
1023 show_blame_view(const char *path, struct got_object_id *commit_id,
1024 struct got_repository *repo)
1026 const struct got_error *err = NULL;
1027 FILE *f;
1028 int ch, done = 0, first_displayed_line = 1, last_displayed_line = LINES;
1029 int eof, i;
1031 f = got_opentemp();
1032 if (f == NULL)
1033 return got_error_from_errno();
1035 err = got_blame(path, commit_id, repo, f);
1036 if (err)
1037 goto done;
1039 fflush(f);
1041 if (tog_blame_view.window == NULL) {
1042 tog_blame_view.window = newwin(0, 0, 0, 0);
1043 if (tog_blame_view.window == NULL)
1044 return got_error_from_errno();
1045 keypad(tog_blame_view.window, TRUE);
1047 if (tog_blame_view.panel == NULL) {
1048 tog_blame_view.panel = new_panel(tog_blame_view.window);
1049 if (tog_blame_view.panel == NULL)
1050 return got_error_from_errno();
1051 } else
1052 show_panel(tog_blame_view.panel);
1054 while (!done) {
1055 err = draw_file(tog_blame_view.window, f, &first_displayed_line,
1056 &last_displayed_line, &eof, LINES);
1057 if (err)
1058 break;
1059 nodelay(stdscr, FALSE);
1060 ch = wgetch(tog_blame_view.window);
1061 nodelay(stdscr, TRUE);
1062 switch (ch) {
1063 case 'q':
1064 done = 1;
1065 break;
1066 case 'k':
1067 case KEY_UP:
1068 case KEY_BACKSPACE:
1069 if (first_displayed_line > 1)
1070 first_displayed_line--;
1071 break;
1072 case KEY_PPAGE:
1073 i = 0;
1074 while (i++ < LINES - 1 &&
1075 first_displayed_line > 1)
1076 first_displayed_line--;
1077 break;
1078 case 'j':
1079 case KEY_DOWN:
1080 case KEY_ENTER:
1081 case '\r':
1082 if (!eof)
1083 first_displayed_line++;
1084 break;
1085 case KEY_NPAGE:
1086 case ' ':
1087 i = 0;
1088 while (!eof && i++ < LINES - 1) {
1089 char *line = parse_next_line(f, NULL);
1090 first_displayed_line++;
1091 if (line == NULL)
1092 break;
1094 break;
1095 default:
1096 break;
1099 done:
1100 fclose(f);
1101 return err;
1104 static const struct got_error *
1105 cmd_blame(int argc, char *argv[])
1107 const struct got_error *error;
1108 struct got_repository *repo = NULL;
1109 char *repo_path = NULL;
1110 char *path = NULL;
1111 struct got_object_id *commit_id = NULL;
1112 char *commit_id_str = NULL;
1113 int ch;
1115 #ifndef PROFILE
1116 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1117 err(1, "pledge");
1118 #endif
1120 while ((ch = getopt(argc, argv, "c:")) != -1) {
1121 switch (ch) {
1122 case 'c':
1123 commit_id_str = optarg;
1124 break;
1125 default:
1126 usage();
1127 /* NOTREACHED */
1131 argc -= optind;
1132 argv += optind;
1134 if (argc == 0) {
1135 usage_blame();
1136 } else if (argc == 1) {
1137 repo_path = getcwd(NULL, 0);
1138 if (repo_path == NULL)
1139 return got_error_from_errno();
1140 path = argv[0];
1141 } else if (argc == 2) {
1142 repo_path = realpath(argv[0], NULL);
1143 if (repo_path == NULL)
1144 return got_error_from_errno();
1145 path = argv[1];
1146 } else
1147 usage_blame();
1149 error = got_repo_open(&repo, repo_path);
1150 free(repo_path);
1151 if (error != NULL)
1152 return error;
1154 if (commit_id_str == NULL) {
1155 struct got_reference *head_ref;
1156 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1157 if (error != NULL)
1158 goto done;
1159 error = got_ref_resolve(&commit_id, repo, head_ref);
1160 got_ref_close(head_ref);
1161 if (error != NULL)
1162 goto done;
1163 } else {
1164 struct got_object *obj;
1165 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
1166 if (error != NULL)
1167 goto done;
1168 commit_id = got_object_get_id(obj);
1169 got_object_close(obj);
1170 if (commit_id == NULL) {
1171 error = got_error_from_errno();
1172 goto done;
1176 error = show_blame_view(path, commit_id, repo);
1177 done:
1178 free(commit_id);
1179 if (repo)
1180 got_repo_close(repo);
1181 return error;
1184 static void
1185 init_curses(void)
1187 initscr();
1188 cbreak();
1189 noecho();
1190 nonl();
1191 intrflush(stdscr, FALSE);
1192 keypad(stdscr, TRUE);
1193 curs_set(0);
1196 __dead static void
1197 usage(void)
1199 int i;
1201 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
1202 "Available commands:\n", getprogname());
1203 for (i = 0; i < nitems(tog_commands); i++) {
1204 struct tog_cmd *cmd = &tog_commands[i];
1205 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
1207 exit(1);
1210 static char **
1211 make_argv(const char *arg0, const char *arg1)
1213 char **argv;
1214 int argc = (arg1 == NULL ? 1 : 2);
1216 argv = calloc(argc, sizeof(char *));
1217 if (argv == NULL)
1218 err(1, "calloc");
1219 argv[0] = strdup(arg0);
1220 if (argv[0] == NULL)
1221 err(1, "calloc");
1222 if (arg1) {
1223 argv[1] = strdup(arg1);
1224 if (argv[1] == NULL)
1225 err(1, "calloc");
1228 return argv;
1231 int
1232 main(int argc, char *argv[])
1234 const struct got_error *error = NULL;
1235 struct tog_cmd *cmd = NULL;
1236 int ch, hflag = 0;
1237 char **cmd_argv = NULL;
1239 setlocale(LC_ALL, "");
1241 while ((ch = getopt(argc, argv, "h")) != -1) {
1242 switch (ch) {
1243 case 'h':
1244 hflag = 1;
1245 break;
1246 default:
1247 usage();
1248 /* NOTREACHED */
1252 argc -= optind;
1253 argv += optind;
1254 optind = 0;
1255 optreset = 1;
1257 if (argc == 0) {
1258 if (hflag)
1259 usage();
1260 /* Build an argument vector which runs a default command. */
1261 cmd = &tog_commands[0];
1262 cmd_argv = make_argv(cmd->name, NULL);
1263 argc = 1;
1264 } else {
1265 int i;
1267 /* Did the user specific a command? */
1268 for (i = 0; i < nitems(tog_commands); i++) {
1269 if (strncmp(tog_commands[i].name, argv[0],
1270 strlen(argv[0])) == 0) {
1271 cmd = &tog_commands[i];
1272 if (hflag)
1273 tog_commands[i].cmd_usage();
1274 break;
1277 if (cmd == NULL) {
1278 /* Did the user specify a repository? */
1279 char *repo_path = realpath(argv[0], NULL);
1280 if (repo_path) {
1281 struct got_repository *repo;
1282 error = got_repo_open(&repo, repo_path);
1283 if (error == NULL)
1284 got_repo_close(repo);
1285 } else
1286 error = got_error_from_errno();
1287 if (error) {
1288 if (hflag) {
1289 fprintf(stderr, "%s: '%s' is not a "
1290 "known command\n", getprogname(),
1291 argv[0]);
1292 usage();
1294 fprintf(stderr, "%s: '%s' is neither a known "
1295 "command nor a path to a repository\n",
1296 getprogname(), argv[0]);
1297 free(repo_path);
1298 return 1;
1300 cmd = &tog_commands[0];
1301 cmd_argv = make_argv(cmd->name, repo_path);
1302 argc = 2;
1303 free(repo_path);
1307 init_curses();
1309 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
1310 if (error)
1311 goto done;
1312 done:
1313 endwin();
1314 free(cmd_argv);
1315 if (error)
1316 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
1317 return 0;