Blob


1 /*
2 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/queue.h>
19 #include <errno.h>
20 #define _XOPEN_SOURCE_EXTENDED
21 #include <curses.h>
22 #undef _XOPEN_SOURCE_EXTENDED
23 #include <panel.h>
24 #include <locale.h>
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <getopt.h>
28 #include <string.h>
29 #include <err.h>
30 #include <unistd.h>
31 #include <util.h>
32 #include <limits.h>
33 #include <wchar.h>
35 #include "got_error.h"
36 #include "got_object.h"
37 #include "got_reference.h"
38 #include "got_repository.h"
39 #include "got_diff.h"
40 #include "got_opentemp.h"
42 #ifndef MIN
43 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
44 #endif
46 #ifndef nitems
47 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
48 #endif
50 struct tog_cmd {
51 const char *name;
52 const struct got_error *(*cmd_main)(int, char *[]);
53 void (*cmd_usage)(void);
54 const char *descr;
55 };
57 __dead static void usage(void);
58 __dead static void usage_log(void);
59 __dead static void usage_diff(void);
60 __dead static void usage_blame(void);
62 static const struct got_error* cmd_log(int, char *[]);
63 static const struct got_error* cmd_diff(int, char *[]);
64 static const struct got_error* cmd_blame(int, char *[]);
66 static struct tog_cmd tog_commands[] = {
67 { "log", cmd_log, usage_log,
68 "show repository history" },
69 { "diff", cmd_diff, usage_diff,
70 "compare files and directories" },
71 { "blame", cmd_blame, usage_blame,
72 "show line-by-line file history" },
73 };
75 static struct tog_view {
76 WINDOW *window;
77 PANEL *panel;
78 } tog_log_view, tog_diff_view;
80 static const struct got_error *
81 show_diff_view(struct got_object *, struct got_object *,
82 struct got_repository *);
83 static const struct got_error *
84 show_log_view(struct got_object_id *, struct got_repository *);
86 static const struct got_error *format_line(wchar_t **, int *, char *, int);
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 static const struct got_error *
98 draw_commit(struct got_commit_object *commit, struct got_object_id *id)
99 {
100 const struct got_error *err = NULL;
101 char *logmsg0 = NULL, *logmsg = NULL;
102 char *author0 = NULL, *author = NULL;
103 wchar_t *wlogmsg = NULL, *wauthor = NULL;
104 int author_width, logmsg_width;
105 char *newline, *smallerthan;
106 char *line = NULL;
107 char *id_str = NULL;
108 size_t id_len;
109 int col, limit;
110 static const size_t id_display_cols = 8;
111 static const size_t author_display_cols = 16;
112 const int avail = COLS - 1;
114 err = got_object_id_str(&id_str, id);
115 if (err)
116 return err;
117 id_len = strlen(id_str);
118 if (avail < id_display_cols) {
119 limit = MIN(id_len, avail);
120 waddnstr(tog_log_view.window, id_str, limit);
121 } else {
122 limit = MIN(id_display_cols, id_len);
123 waddnstr(tog_log_view.window, id_str, limit);
125 col = limit + 1;
126 while (col < avail && col < id_display_cols + 2) {
127 waddch(tog_log_view.window, ' ');
128 col++;
130 if (col >= avail)
131 goto endline;
133 author0 = strdup(commit->author);
134 if (author0 == NULL) {
135 err = got_error_from_errno();
136 goto done;
138 author = author0;
139 smallerthan = strchr(author, '<');
140 if (smallerthan)
141 *smallerthan = '\0';
142 else {
143 char *at = strchr(author, '@');
144 if (at)
145 *at = '\0';
147 limit = MIN(avail, author_display_cols);
148 err = format_line(&wauthor, &author_width, author, limit);
149 if (err)
150 goto done;
151 waddwstr(tog_log_view.window, wauthor);
152 col += author_width;
153 while (col < avail && author_width < author_display_cols + 1) {
154 waddch(tog_log_view.window, ' ');
155 col++;
156 author_width++;
158 if (col >= avail)
159 goto endline;
161 logmsg0 = strdup(commit->logmsg);
162 if (logmsg0 == NULL) {
163 err = got_error_from_errno();
164 goto done;
166 logmsg = logmsg0;
167 while (*logmsg == '\n')
168 logmsg++;
169 newline = strchr(logmsg, '\n');
170 if (newline)
171 *newline = '\0';
172 limit = avail - col;
173 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
174 if (err)
175 goto done;
176 waddwstr(tog_log_view.window, wlogmsg);
177 col += logmsg_width;
178 while (col <= avail) {
179 waddch(tog_log_view.window, ' ');
180 col++;
182 endline:
183 waddch(tog_log_view.window, '\n');
184 done:
185 free(logmsg0);
186 free(wlogmsg);
187 free(author0);
188 free(wauthor);
189 free(line);
190 free(id_str);
191 return err;
194 struct commit_queue_entry {
195 TAILQ_ENTRY(commit_queue_entry) entry;
196 struct got_object_id *id;
197 struct got_commit_object *commit;
198 };
199 TAILQ_HEAD(commit_queue, commit_queue_entry);
201 static struct commit_queue_entry *
202 alloc_commit_queue_entry(struct got_commit_object *commit,
203 struct got_object_id *id)
205 struct commit_queue_entry *entry;
207 entry = calloc(1, sizeof(*entry));
208 if (entry == NULL)
209 return NULL;
211 entry->id = id;
212 entry->commit = commit;
213 return entry;
216 static void
217 pop_commit(struct commit_queue *commits)
219 struct commit_queue_entry *entry;
221 entry = TAILQ_FIRST(commits);
222 TAILQ_REMOVE(commits, entry, entry);
223 got_object_commit_close(entry->commit);
224 free(entry->id);
225 free(entry);
228 static void
229 free_commits(struct commit_queue *commits)
231 while (!TAILQ_EMPTY(commits))
232 pop_commit(commits);
235 static const struct got_error *
236 fetch_parent_commit(struct commit_queue_entry **pentry,
237 struct commit_queue_entry *entry, struct got_repository *repo)
239 const struct got_error *err = NULL;
240 struct got_object *obj = NULL;
241 struct got_commit_object *commit;
242 struct got_object_id *id;
243 struct got_parent_id *pid;
245 *pentry = NULL;
247 /* Follow the first parent (TODO: handle merge commits). */
248 pid = SIMPLEQ_FIRST(&entry->commit->parent_ids);
249 if (pid == NULL)
250 return NULL;
251 err = got_object_open(&obj, repo, pid->id);
252 if (err)
253 return err;
254 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
255 err = got_error(GOT_ERR_OBJ_TYPE);
256 got_object_close(obj);
257 return err;
260 err = got_object_commit_open(&commit, repo, obj);
261 got_object_close(obj);
262 if (err)
263 return err;
265 id = got_object_id_dup(pid->id);
266 if (id == NULL) {
267 err = got_error_from_errno();
268 got_object_commit_close(commit);
269 return err;;
272 *pentry = alloc_commit_queue_entry(commit, id);
273 if (*pentry == NULL) {
274 err = got_error_from_errno();
275 got_object_commit_close(commit);
278 return err;;
281 static const struct got_error *
282 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
284 const struct got_error *err = NULL;
285 struct got_reference *head_ref;
287 *head_id = NULL;
289 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
290 if (err)
291 return err;
293 err = got_ref_resolve(head_id, repo, head_ref);
294 got_ref_close(head_ref);
295 if (err) {
296 *head_id = NULL;
297 return err;
300 return NULL;
303 static const struct got_error *
304 prepend_commits(int *ncommits, struct commit_queue *commits,
305 struct got_object_id *first_id, struct got_object_id *last_id,
306 int limit, struct got_repository *repo)
308 const struct got_error *err = NULL;
309 struct got_object *first_obj = NULL, *last_obj = NULL;
310 struct got_commit_object *commit = NULL;
311 struct got_object_id *id = NULL;
312 struct commit_queue_entry *entry, *old_head_entry;
314 *ncommits = 0;
316 err = got_object_open(&first_obj, repo, first_id);
317 if (err)
318 goto done;
319 if (got_object_get_type(first_obj) != GOT_OBJ_TYPE_COMMIT) {
320 err = got_error(GOT_ERR_OBJ_TYPE);
321 goto done;
323 err = got_object_open(&last_obj, repo, last_id);
324 if (err)
325 goto done;
326 if (got_object_get_type(last_obj) != GOT_OBJ_TYPE_COMMIT) {
327 err = got_error(GOT_ERR_OBJ_TYPE);
328 goto done;
331 err = got_object_commit_open(&commit, repo, first_obj);
332 if (err)
333 goto done;
335 id = got_object_id_dup(first_id);
336 if (id == NULL) {
337 err = got_error_from_errno();
338 goto done;
341 entry = alloc_commit_queue_entry(commit, id);
342 if (entry == NULL)
343 return got_error_from_errno();
345 old_head_entry = TAILQ_FIRST(commits);
346 if (old_head_entry)
347 TAILQ_INSERT_BEFORE(old_head_entry, entry, entry);
348 else
349 TAILQ_INSERT_HEAD(commits, entry, entry);
351 *ncommits = 1;
353 /*
354 * Fetch parent commits.
355 * XXX If first and last commit aren't ancestrally related this loop
356 * we will keep iterating until a root commit is encountered.
357 */
358 while (1) {
359 struct commit_queue_entry *pentry;
361 err = fetch_parent_commit(&pentry, entry, repo);
362 if (err)
363 goto done;
364 if (pentry == NULL)
365 break;
367 /*
368 * Fill up to old HEAD commit if commit queue was not empty.
369 * We must not leave a gap in history.
370 */
371 if (old_head_entry &&
372 got_object_id_cmp(pentry->id, old_head_entry->id) == 0)
373 break;
375 TAILQ_INSERT_AFTER(commits, entry, pentry, entry);
376 (*ncommits)++;
377 if (*ncommits >= limit)
378 break;
380 /* Fill up to last requested commit if queue was empty. */
381 if (old_head_entry == NULL &&
382 got_object_id_cmp(pentry->id, last_id) == 0)
383 break;
385 entry = pentry;
388 done:
389 if (first_obj)
390 got_object_close(first_obj);
391 if (last_obj)
392 got_object_close(last_obj);
393 return err;
396 static const struct got_error *
397 fetch_commits(struct commit_queue_entry **start_entry,
398 struct got_object_id *start_id, struct commit_queue *commits,
399 int limit, struct got_repository *repo)
401 const struct got_error *err;
402 struct commit_queue_entry *entry;
403 int ncommits = 0;
404 struct got_object_id *head_id = NULL;
406 *start_entry = NULL;
408 err = get_head_commit_id(&head_id, repo);
409 if (err)
410 return err;
412 /* Prepend HEAD commit and all ancestors up to start commit. */
413 err = prepend_commits(&ncommits, commits, head_id, start_id, limit,
414 repo);
415 if (err)
416 return err;
418 if (got_object_id_cmp(head_id, start_id) == 0)
419 *start_entry = TAILQ_FIRST(commits);
420 else
421 *start_entry = TAILQ_LAST(commits, commit_queue);
423 if (ncommits >= limit)
424 return NULL;
426 /* Append more commits from start commit up to the requested limit. */
427 entry = TAILQ_LAST(commits, commit_queue);
428 while (entry && ncommits < limit) {
429 struct commit_queue_entry *pentry;
431 err = fetch_parent_commit(&pentry, entry, repo);
432 if (err)
433 break;
434 if (pentry)
435 TAILQ_INSERT_TAIL(commits, pentry, entry);
436 entry = pentry;
437 ncommits++;
440 if (err)
441 *start_entry = NULL;
442 return err;
445 static const struct got_error *
446 draw_commits(struct commit_queue_entry **last, struct commit_queue_entry **selected,
447 struct commit_queue_entry *first, int selected_idx, int limit)
449 const struct got_error *err = NULL;
450 struct commit_queue_entry *entry;
451 int ncommits = 0;
453 werase(tog_log_view.window);
455 entry = first;
456 *last = first;
457 while (entry) {
458 if (ncommits == limit)
459 break;
460 if (ncommits == selected_idx) {
461 wstandout(tog_log_view.window);
462 *selected = entry;
464 err = draw_commit(entry->commit, entry->id);
465 if (ncommits == selected_idx)
466 wstandend(tog_log_view.window);
467 if (err)
468 break;
469 ncommits++;
470 *last = entry;
471 entry = TAILQ_NEXT(entry, entry);
474 update_panels();
475 doupdate();
477 return err;
480 static void
481 scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
482 struct commit_queue *commits)
484 struct commit_queue_entry *entry;
485 int nscrolled = 0;
487 entry = TAILQ_FIRST(commits);
488 if (*first_displayed_entry == entry)
489 return;
491 entry = *first_displayed_entry;
492 while (entry && nscrolled < maxscroll) {
493 entry = TAILQ_PREV(entry, commit_queue, entry);
494 if (entry) {
495 *first_displayed_entry = entry;
496 nscrolled++;
501 static const struct got_error *
502 scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
503 struct commit_queue_entry *last_displayed_entry,
504 struct commit_queue *commits, struct got_repository *repo)
506 const struct got_error *err = NULL;
507 struct commit_queue_entry *pentry;
508 int nscrolled = 0;
510 do {
511 pentry = TAILQ_NEXT(last_displayed_entry, entry);
512 if (pentry == NULL) {
513 err = fetch_parent_commit(&pentry,
514 last_displayed_entry, repo);
515 if (err || pentry == NULL)
516 break;
517 TAILQ_INSERT_TAIL(commits, pentry, entry);
519 last_displayed_entry = pentry;
521 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
522 if (pentry == NULL)
523 break;
524 *first_displayed_entry = pentry;
525 } while (++nscrolled < maxscroll);
527 return err;
530 static int
531 num_parents(struct commit_queue_entry *entry)
533 int nparents = 0;
535 while (entry) {
536 entry = TAILQ_NEXT(entry, entry);
537 nparents++;
540 return nparents;
543 static const struct got_error *
544 show_commit(struct commit_queue_entry *entry, struct got_repository *repo)
546 const struct got_error *err;
547 struct commit_queue_entry *pentry;
548 struct got_object *obj1 = NULL, *obj2 = NULL;
550 err = got_object_open(&obj2, repo, entry->id);
551 if (err)
552 return err;
554 pentry = TAILQ_NEXT(entry, entry);
555 if (pentry == NULL) {
556 err = fetch_parent_commit(&pentry, entry, repo);
557 if (err)
558 return err;
560 if (pentry) {
561 err = got_object_open(&obj1, repo, pentry->id);
562 if (err)
563 goto done;
566 err = show_diff_view(obj1, obj2, repo);
567 done:
568 if (obj1)
569 got_object_close(obj1);
570 if (obj2)
571 got_object_close(obj2);
572 return err;
575 static const struct got_error *
576 show_log_view(struct got_object_id *start_id, struct got_repository *repo)
578 const struct got_error *err = NULL;
579 struct got_object_id *id;
580 int ch, done = 0, selected = 0, nparents;
581 struct commit_queue commits;
582 struct commit_queue_entry *first_displayed_entry = NULL;
583 struct commit_queue_entry *last_displayed_entry = NULL;
584 struct commit_queue_entry *selected_entry = NULL;
586 id = got_object_id_dup(start_id);
587 if (id == NULL)
588 return got_error_from_errno();
590 if (tog_log_view.window == NULL) {
591 tog_log_view.window = newwin(0, 0, 0, 0);
592 if (tog_log_view.window == NULL)
593 return got_error_from_errno();
594 keypad(tog_log_view.window, TRUE);
596 if (tog_log_view.panel == NULL) {
597 tog_log_view.panel = new_panel(tog_log_view.window);
598 if (tog_log_view.panel == NULL)
599 return got_error_from_errno();
600 } else
601 show_panel(tog_log_view.panel);
603 TAILQ_INIT(&commits);
604 err = fetch_commits(&first_displayed_entry, id, &commits, LINES, repo);
605 if (err)
606 goto done;
607 while (!done) {
608 err = draw_commits(&last_displayed_entry, &selected_entry,
609 first_displayed_entry, selected, LINES);
610 if (err)
611 goto done;
613 nodelay(stdscr, FALSE);
614 ch = wgetch(tog_log_view.window);
615 nodelay(stdscr, TRUE);
616 switch (ch) {
617 case ERR:
618 if (errno) {
619 err = got_error_from_errno();
620 goto done;
622 break;
623 case 'q':
624 done = 1;
625 break;
626 case 'k':
627 case KEY_UP:
628 if (selected > 0)
629 selected--;
630 if (selected > 0)
631 break;
632 scroll_up(&first_displayed_entry, 1, &commits);
633 break;
634 case KEY_PPAGE:
635 if (TAILQ_FIRST(&commits) ==
636 first_displayed_entry) {
637 selected = 0;
638 break;
640 scroll_up(&first_displayed_entry, LINES,
641 &commits);
642 break;
643 case 'j':
644 case KEY_DOWN:
645 nparents = num_parents(first_displayed_entry);
646 if (selected < LINES - 1 &&
647 selected < nparents - 1) {
648 selected++;
649 break;
651 err = scroll_down(&first_displayed_entry, 1,
652 last_displayed_entry, &commits, repo);
653 if (err)
654 goto done;
655 break;
656 case KEY_NPAGE:
657 err = scroll_down(&first_displayed_entry, LINES,
658 last_displayed_entry, &commits, repo);
659 if (err)
660 goto done;
661 if (last_displayed_entry->commit->nparents > 0)
662 break;
663 /* can't scroll any further; move cursor down */
664 nparents = num_parents(first_displayed_entry);
665 if (selected < LINES - 1 ||
666 selected < nparents - 1)
667 selected = MIN(LINES - 1, nparents - 1);
668 break;
669 case KEY_RESIZE:
670 if (selected > LINES)
671 selected = LINES - 1;
672 break;
673 case KEY_ENTER:
674 case '\r':
675 err = show_commit(selected_entry, repo);
676 if (err)
677 break;
678 show_panel(tog_log_view.panel);
679 break;
680 default:
681 break;
684 done:
685 free_commits(&commits);
686 return err;
689 static const struct got_error *
690 cmd_log(int argc, char *argv[])
692 const struct got_error *error;
693 struct got_repository *repo;
694 struct got_object_id *start_id = NULL;
695 char *repo_path = NULL;
696 char *start_commit = NULL;
697 int ch;
699 #ifndef PROFILE
700 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
701 err(1, "pledge");
702 #endif
704 while ((ch = getopt(argc, argv, "c:")) != -1) {
705 switch (ch) {
706 case 'c':
707 start_commit = optarg;
708 break;
709 default:
710 usage();
711 /* NOTREACHED */
715 argc -= optind;
716 argv += optind;
718 if (argc == 0) {
719 repo_path = getcwd(NULL, 0);
720 if (repo_path == NULL)
721 return got_error_from_errno();
722 } else if (argc == 1) {
723 repo_path = realpath(argv[0], NULL);
724 if (repo_path == NULL)
725 return got_error_from_errno();
726 } else
727 usage_log();
729 error = got_repo_open(&repo, repo_path);
730 free(repo_path);
731 if (error != NULL)
732 return error;
734 if (start_commit == NULL) {
735 error = get_head_commit_id(&start_id, repo);
736 if (error != NULL)
737 return error;
738 } else {
739 struct got_object *obj;
740 error = got_object_open_by_id_str(&obj, repo, start_commit);
741 if (error == NULL) {
742 start_id = got_object_get_id(obj);
743 if (start_id == NULL)
744 error = got_error_from_errno();
747 if (error != NULL)
748 return error;
749 error = show_log_view(start_id, repo);
750 free(start_id);
751 got_repo_close(repo);
752 return error;
755 __dead static void
756 usage_diff(void)
758 endwin();
759 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
760 getprogname());
761 exit(1);
764 static char *
765 parse_next_line(FILE *f, size_t *len)
767 char *line;
768 size_t linelen;
769 size_t lineno;
770 const char delim[3] = { '\0', '\0', '\0'};
772 line = fparseln(f, &linelen, &lineno, delim, 0);
773 if (len)
774 *len = linelen;
775 return line;
778 static const struct got_error *
779 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
781 const struct got_error *err = NULL;
783 *ws = NULL;
784 *wlen = mbstowcs(NULL, s, 0);
785 if (*wlen == (size_t)-1)
786 return got_error_from_errno();
788 *ws = calloc(*wlen + 1, sizeof(*ws));
789 if (*ws == NULL)
790 return got_error_from_errno();
792 if (mbstowcs(*ws, s, *wlen) != *wlen)
793 err = got_error_from_errno();
795 if (err) {
796 free(*ws);
797 *ws = NULL;
798 *wlen = 0;
800 return err;
803 /* Format a line for display, ensuring that it won't overflow a width limit. */
804 static const struct got_error *
805 format_line(wchar_t **wlinep, int *widthp, char *line, int wlimit)
807 const struct got_error *err = NULL;
808 int cols = 0;
809 wchar_t *wline = NULL;
810 size_t wlen;
811 int i;
813 *wlinep = NULL;
815 err = mbs2ws(&wline, &wlen, line);
816 if (err)
817 return err;
819 i = 0;
820 while (i < wlen && cols <= wlimit) {
821 int width = wcwidth(wline[i]);
822 switch (width) {
823 case 0:
824 break;
825 case 1:
826 case 2:
827 cols += width;
828 break;
829 case -1:
830 if (wline[i] == L'\t')
831 cols += TABSIZE;
832 break;
833 default:
834 err = got_error_from_errno();
835 goto done;
837 if (cols <= COLS) {
838 i++;
839 if (widthp)
840 *widthp = cols;
843 wline[i] = L'\0';
844 done:
845 if (err)
846 free(wline);
847 else
848 *wlinep = wline;
849 return err;
852 static const struct got_error *
853 draw_diff(FILE *f, int *first_displayed_line, int *last_displayed_line,
854 int *eof, int max_lines)
856 const struct got_error *err;
857 int nlines = 0, nprinted = 0;
858 char *line;
859 size_t len;
860 wchar_t *wline;
862 rewind(f);
863 werase(tog_diff_view.window);
865 *eof = 0;
866 while (nprinted < max_lines) {
867 line = parse_next_line(f, &len);
868 if (line == NULL) {
869 *eof = 1;
870 break;
872 if (++nlines < *first_displayed_line) {
873 free(line);
874 continue;
877 err = format_line(&wline, NULL, line, COLS);
878 if (err) {
879 free(line);
880 return err;
882 waddwstr(tog_diff_view.window, wline);
883 waddch(tog_diff_view.window, '\n');
884 if (++nprinted == 1)
885 *first_displayed_line = nlines;
886 free(line);
888 *last_displayed_line = nlines;
890 update_panels();
891 doupdate();
893 return NULL;
896 static const struct got_error *
897 show_diff_view(struct got_object *obj1, struct got_object *obj2,
898 struct got_repository *repo)
900 const struct got_error *err;
901 FILE *f;
902 int ch, done = 0, first_displayed_line = 1, last_displayed_line = LINES;
903 int eof, i;
905 if (obj1 != NULL && obj2 != NULL &&
906 got_object_get_type(obj1) != got_object_get_type(obj2))
907 return got_error(GOT_ERR_OBJ_TYPE);
909 f = got_opentemp();
910 if (f == NULL)
911 return got_error_from_errno();
913 switch (got_object_get_type(obj1 ? obj1 : obj2)) {
914 case GOT_OBJ_TYPE_BLOB:
915 err = got_diff_objects_as_blobs(obj1, obj2, repo, f);
916 break;
917 case GOT_OBJ_TYPE_TREE:
918 err = got_diff_objects_as_trees(obj1, obj2, repo, f);
919 break;
920 case GOT_OBJ_TYPE_COMMIT:
921 err = got_diff_objects_as_commits(obj1, obj2, repo, f);
922 break;
923 default:
924 return got_error(GOT_ERR_OBJ_TYPE);
927 fflush(f);
929 if (tog_diff_view.window == NULL) {
930 tog_diff_view.window = newwin(0, 0, 0, 0);
931 if (tog_diff_view.window == NULL)
932 return got_error_from_errno();
933 keypad(tog_diff_view.window, TRUE);
935 if (tog_diff_view.panel == NULL) {
936 tog_diff_view.panel = new_panel(tog_diff_view.window);
937 if (tog_diff_view.panel == NULL)
938 return got_error_from_errno();
939 } else
940 show_panel(tog_diff_view.panel);
942 while (!done) {
943 err = draw_diff(f, &first_displayed_line, &last_displayed_line,
944 &eof, LINES);
945 if (err)
946 break;
947 nodelay(stdscr, FALSE);
948 ch = wgetch(tog_diff_view.window);
949 nodelay(stdscr, TRUE);
950 switch (ch) {
951 case 'q':
952 done = 1;
953 break;
954 case 'k':
955 case KEY_UP:
956 case KEY_BACKSPACE:
957 if (first_displayed_line > 1)
958 first_displayed_line--;
959 break;
960 case KEY_PPAGE:
961 i = 0;
962 while (i++ < LINES - 1 &&
963 first_displayed_line > 1)
964 first_displayed_line--;
965 break;
966 case 'j':
967 case KEY_DOWN:
968 case KEY_ENTER:
969 case '\r':
970 if (!eof)
971 first_displayed_line++;
972 break;
973 case KEY_NPAGE:
974 case ' ':
975 i = 0;
976 while (!eof && i++ < LINES - 1) {
977 char *line = parse_next_line(f, NULL);
978 first_displayed_line++;
979 if (line == NULL)
980 break;
982 break;
983 default:
984 break;
987 fclose(f);
988 return err;
991 static const struct got_error *
992 cmd_diff(int argc, char *argv[])
994 const struct got_error *error = NULL;
995 struct got_repository *repo = NULL;
996 struct got_object *obj1 = NULL, *obj2 = NULL;
997 char *repo_path = NULL;
998 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
999 int ch;
1001 #ifndef PROFILE
1002 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1003 err(1, "pledge");
1004 #endif
1006 while ((ch = getopt(argc, argv, "")) != -1) {
1007 switch (ch) {
1008 default:
1009 usage();
1010 /* NOTREACHED */
1014 argc -= optind;
1015 argv += optind;
1017 if (argc == 0) {
1018 usage_diff(); /* TODO show local worktree changes */
1019 } else if (argc == 2) {
1020 repo_path = getcwd(NULL, 0);
1021 if (repo_path == NULL)
1022 return got_error_from_errno();
1023 obj_id_str1 = argv[0];
1024 obj_id_str2 = argv[1];
1025 } else if (argc == 3) {
1026 repo_path = realpath(argv[0], NULL);
1027 if (repo_path == NULL)
1028 return got_error_from_errno();
1029 obj_id_str1 = argv[1];
1030 obj_id_str2 = argv[2];
1031 } else
1032 usage_diff();
1034 error = got_repo_open(&repo, repo_path);
1035 free(repo_path);
1036 if (error)
1037 goto done;
1039 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
1040 if (error)
1041 goto done;
1043 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1044 if (error)
1045 goto done;
1047 error = show_diff_view(obj1, obj2, repo);
1048 done:
1049 got_repo_close(repo);
1050 if (obj1)
1051 got_object_close(obj1);
1052 if (obj2)
1053 got_object_close(obj2);
1054 return error;
1057 __dead static void
1058 usage_blame(void)
1060 endwin();
1061 fprintf(stderr, "usage: %s blame [repository-path] blob-object\n",
1062 getprogname());
1063 exit(1);
1066 static const struct got_error *
1067 cmd_blame(int argc, char *argv[])
1069 return got_error(GOT_ERR_NOT_IMPL);
1072 static void
1073 init_curses(void)
1075 initscr();
1076 cbreak();
1077 noecho();
1078 nonl();
1079 intrflush(stdscr, FALSE);
1080 keypad(stdscr, TRUE);
1081 curs_set(0);
1084 __dead static void
1085 usage(void)
1087 int i;
1089 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
1090 "Available commands:\n", getprogname());
1091 for (i = 0; i < nitems(tog_commands); i++) {
1092 struct tog_cmd *cmd = &tog_commands[i];
1093 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
1095 exit(1);
1098 static char **
1099 make_argv(const char *arg0, const char *arg1)
1101 char **argv;
1102 int argc = (arg1 == NULL ? 1 : 2);
1104 argv = calloc(argc, sizeof(char *));
1105 if (argv == NULL)
1106 err(1, "calloc");
1107 argv[0] = strdup(arg0);
1108 if (argv[0] == NULL)
1109 err(1, "calloc");
1110 if (arg1) {
1111 argv[1] = strdup(arg1);
1112 if (argv[1] == NULL)
1113 err(1, "calloc");
1116 return argv;
1119 int
1120 main(int argc, char *argv[])
1122 const struct got_error *error = NULL;
1123 struct tog_cmd *cmd = NULL;
1124 int ch, hflag = 0;
1125 char **cmd_argv = NULL;
1127 setlocale(LC_ALL, "");
1129 while ((ch = getopt(argc, argv, "h")) != -1) {
1130 switch (ch) {
1131 case 'h':
1132 hflag = 1;
1133 break;
1134 default:
1135 usage();
1136 /* NOTREACHED */
1140 argc -= optind;
1141 argv += optind;
1142 optind = 0;
1143 optreset = 1;
1145 if (argc == 0) {
1146 /* Build an argument vector which runs a default command. */
1147 cmd = &tog_commands[0];
1148 cmd_argv = make_argv(cmd->name, NULL);
1149 argc = 1;
1150 } else {
1151 int i;
1153 /* Did the user specific a command? */
1154 for (i = 0; i < nitems(tog_commands); i++) {
1155 if (strncmp(tog_commands[i].name, argv[0],
1156 strlen(argv[0])) == 0) {
1157 cmd = &tog_commands[i];
1158 if (hflag)
1159 tog_commands[i].cmd_usage();
1160 break;
1163 if (cmd == NULL) {
1164 /* Did the user specify a repository? */
1165 char *repo_path = realpath(argv[0], NULL);
1166 if (repo_path) {
1167 struct got_repository *repo;
1168 error = got_repo_open(&repo, repo_path);
1169 if (error == NULL)
1170 got_repo_close(repo);
1171 } else
1172 error = got_error_from_errno();
1173 if (error) {
1174 fprintf(stderr, "%s: '%s' is neither a known "
1175 "command nor a path to a repository\n",
1176 getprogname(), argv[0]);
1177 free(repo_path);
1178 return 1;
1180 cmd = &tog_commands[0];
1181 cmd_argv = make_argv(cmd->name, repo_path);
1182 argc = 2;
1183 free(repo_path);
1187 init_curses();
1189 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
1190 if (error)
1191 goto done;
1192 done:
1193 endwin();
1194 free(cmd_argv);
1195 if (error)
1196 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
1197 return 0;