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 #include <curses.h>
21 #include <panel.h>
22 #include <locale.h>
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <getopt.h>
26 #include <string.h>
27 #include <err.h>
28 #include <unistd.h>
29 #include <util.h>
30 #include <limits.h>
32 #include "got_error.h"
33 #include "got_object.h"
34 #include "got_reference.h"
35 #include "got_repository.h"
36 #include "got_diff.h"
38 #ifndef MIN
39 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
40 #endif
42 #ifndef nitems
43 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
44 #endif
46 enum tog_view_id {
47 TOG_VIEW_LOG,
48 TOG_VIEW_DIFF,
49 TOG_VIEW_BLAME,
50 };
52 struct tog_cmd {
53 const char *name;
54 const struct got_error *(*cmd_main)(int, char *[]);
55 void (*cmd_usage)(void);
56 enum tog_view_id view;
57 const char *descr;
58 };
60 __dead void usage(void);
61 __dead void usage_log(void);
62 __dead void usage_diff(void);
63 __dead void usage_blame(void);
65 const struct got_error* cmd_log(int, char *[]);
66 const struct got_error* cmd_diff(int, char *[]);
67 const struct got_error* cmd_blame(int, char *[]);
69 struct tog_cmd tog_commands[] = {
70 { "log", cmd_log, usage_log, TOG_VIEW_LOG,
71 "show repository history" },
72 { "diff", cmd_diff, usage_diff, TOG_VIEW_DIFF,
73 "compare files and directories" },
74 { "blame", cmd_blame, usage_blame, TOG_VIEW_BLAME,
75 "show line-by-line file history" },
76 };
78 /* globals */
79 WINDOW *tog_main_win;
80 PANEL *tog_main_panel;
81 static struct tog_log_view {
82 WINDOW *window;
83 PANEL *panel;
84 } tog_log_view;
85 static struct tog_diff_view {
86 WINDOW *window;
87 PANEL *panel;
88 } tog_diff_view;
90 int
91 tog_opentempfd(void)
92 {
93 char name[PATH_MAX];
94 int fd;
96 if (strlcpy(name, "/tmp/tog.XXXXXXXX", sizeof(name)) >= sizeof(name))
97 return -1;
99 fd = mkstemp(name);
100 unlink(name);
101 return fd;
104 FILE *
105 tog_opentemp(void)
107 int fd;
108 FILE *f;
110 fd = tog_opentempfd();
111 if (fd < 0)
112 return NULL;
114 f = fdopen(fd, "w+");
115 if (f == NULL) {
116 close(fd);
117 return NULL;
120 return f;
123 __dead void
124 usage_log(void)
126 endwin();
127 fprintf(stderr, "usage: %s log [-c commit] [repository-path]\n",
128 getprogname());
129 exit(1);
132 static const struct got_error *
133 draw_commit(struct got_commit_object *commit, struct got_object_id *id)
135 const struct got_error *err = NULL;
136 char *logmsg0 = NULL, *logmsg = NULL;
137 char *author0 = NULL, *author = NULL;
138 char *newline, *smallerthan;
139 char *line = NULL;
140 char *id_str = NULL;
141 const size_t id_display_len = 8;
142 const size_t author_display_len = 16;
143 size_t id_len, author_len, logmsg_len, avail;
144 int i, col;
146 err = got_object_id_str(&id_str, id);
147 if (err)
148 return err;
149 id_len = strlen(id_str);
151 logmsg0 = strdup(commit->logmsg);
152 if (logmsg0 == NULL) {
153 err = got_error_from_errno();
154 goto done;
156 logmsg = logmsg0;
157 while (*logmsg == '\n')
158 logmsg++;
159 newline = strchr(logmsg, '\n');
160 if (newline)
161 *newline = '\0';
162 logmsg_len = strlen(logmsg);
164 author0 = strdup(commit->author);
165 if (author0 == NULL) {
166 err = got_error_from_errno();
167 goto done;
169 author = author0;
170 smallerthan = strchr(author, '<');
171 if (smallerthan)
172 *smallerthan = '\0';
173 else {
174 char *at = strchr(author, '@');
175 if (at)
176 *at = '\0';
178 author_len = strlen(author);
180 avail = COLS - 1;
181 line = calloc(avail + 1, sizeof(*line));
182 if (line == NULL) {
183 err = got_error_from_errno();
184 goto done;
187 col = 0;
188 for (i = 0; i < MIN(id_display_len, id_len); i++) {
189 if (col >= avail)
190 goto draw;
191 line[col++] = id_str[i];
193 while (i < id_display_len) {
194 if (col >= avail)
195 goto draw;
196 line[col++] = ' ';
197 i++;
199 if (col >= avail)
200 goto draw;
201 line[col++] = ' ';
202 for (i = 0; i < MIN(author_display_len, author_len); i++) {
203 if (col >= avail)
204 goto draw;
205 line[col++] = author[i];
207 while (i < author_display_len) {
208 if (col >= avail)
209 goto draw;
210 line[col++] = ' ';
211 i++;
213 if (col >= avail)
214 goto draw;
215 line[col++] = ' ';
217 while (col < avail && *logmsg)
218 line[col++] = *logmsg++;
219 while (col < avail)
220 line[col++] = ' ';
221 draw:
222 waddstr(tog_log_view.window, line);
223 waddch(tog_log_view.window, '\n');
224 done:
225 free(logmsg0);
226 free(author0);
227 free(line);
228 free(id_str);
229 return err;
232 struct commit_queue_entry {
233 TAILQ_ENTRY(commit_queue_entry) entry;
234 struct got_object_id *id;
235 struct got_commit_object *commit;
236 };
237 TAILQ_HEAD(commit_queue, commit_queue_entry);
239 static struct commit_queue_entry *
240 alloc_commit_queue_entry(struct got_commit_object *commit,
241 struct got_object_id *id)
243 struct commit_queue_entry *entry;
245 entry = calloc(1, sizeof(*entry));
246 if (entry == NULL)
247 return NULL;
249 entry->id = id;
250 entry->commit = commit;
251 return entry;
254 static void
255 pop_commit(struct commit_queue *commits)
257 struct commit_queue_entry *entry;
259 entry = TAILQ_FIRST(commits);
260 TAILQ_REMOVE(commits, entry, entry);
261 got_object_commit_close(entry->commit);
262 free(entry->id);
263 free(entry);
266 static void
267 free_commits(struct commit_queue *commits)
269 while (!TAILQ_EMPTY(commits))
270 pop_commit(commits);
274 static const struct got_error *
275 fetch_parent_commit(struct commit_queue_entry **pentry,
276 struct commit_queue_entry *entry, struct got_repository *repo)
278 const struct got_error *err = NULL;
279 struct got_object *obj = NULL;
280 struct got_commit_object *commit;
281 struct got_object_id *id;
282 struct got_parent_id *pid;
284 *pentry = NULL;
286 /* Follow the first parent (TODO: handle merge commits). */
287 pid = SIMPLEQ_FIRST(&entry->commit->parent_ids);
288 if (pid == NULL)
289 return NULL;
290 err = got_object_open(&obj, repo, pid->id);
291 if (err)
292 return err;
293 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
294 err = got_error(GOT_ERR_OBJ_TYPE);
295 got_object_close(obj);
296 return err;
299 err = got_object_commit_open(&commit, repo, obj);
300 got_object_close(obj);
301 if (err)
302 return err;
304 id = got_object_id_dup(pid->id);
305 if (id == NULL) {
306 err = got_error_from_errno();
307 got_object_commit_close(commit);
308 return err;;
311 *pentry = alloc_commit_queue_entry(commit, id);
312 if (*pentry == NULL) {
313 err = got_error_from_errno();
314 got_object_commit_close(commit);
317 return err;;
320 static const struct got_error *
321 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
323 const struct got_error *err = NULL;
324 struct got_reference *head_ref;
326 *head_id = NULL;
328 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
329 if (err)
330 return err;
332 err = got_ref_resolve(head_id, repo, head_ref);
333 got_ref_close(head_ref);
334 if (err) {
335 *head_id = NULL;
336 return err;
339 return NULL;
342 static const struct got_error *
343 prepend_commits(int *ncommits, struct commit_queue *commits,
344 struct got_object_id *first_id, struct got_object_id *last_id,
345 int limit, struct got_repository *repo)
347 const struct got_error *err = NULL;
348 struct got_object *first_obj = NULL, *last_obj = NULL;
349 struct got_commit_object *commit = NULL;
350 struct got_object_id *id = NULL;
351 struct commit_queue_entry *entry, *old_head_entry;
353 *ncommits = 0;
355 err = got_object_open(&first_obj, repo, first_id);
356 if (err)
357 goto done;
358 if (got_object_get_type(first_obj) != GOT_OBJ_TYPE_COMMIT) {
359 err = got_error(GOT_ERR_OBJ_TYPE);
360 goto done;
362 err = got_object_open(&last_obj, repo, last_id);
363 if (err)
364 goto done;
365 if (got_object_get_type(last_obj) != GOT_OBJ_TYPE_COMMIT) {
366 err = got_error(GOT_ERR_OBJ_TYPE);
367 goto done;
370 err = got_object_commit_open(&commit, repo, first_obj);
371 if (err)
372 goto done;
374 id = got_object_id_dup(first_id);
375 if (id == NULL) {
376 err = got_error_from_errno();
377 goto done;
380 entry = alloc_commit_queue_entry(commit, id);
381 if (entry == NULL)
382 return got_error_from_errno();
384 old_head_entry = TAILQ_FIRST(commits);
385 if (old_head_entry)
386 TAILQ_INSERT_BEFORE(old_head_entry, entry, entry);
387 else
388 TAILQ_INSERT_HEAD(commits, entry, entry);
390 *ncommits = 1;
392 /*
393 * Fetch parent commits.
394 * XXX If first and last commit aren't ancestrally related this loop
395 * we will keep iterating until a root commit is encountered.
396 */
397 while (1) {
398 struct commit_queue_entry *pentry;
400 err = fetch_parent_commit(&pentry, entry, repo);
401 if (err)
402 goto done;
403 if (pentry == NULL)
404 break;
406 /*
407 * Fill up to old HEAD commit if commit queue was not empty.
408 * We must not leave a gap in history.
409 */
410 if (old_head_entry &&
411 got_object_id_cmp(pentry->id, old_head_entry->id) == 0)
412 break;
414 TAILQ_INSERT_AFTER(commits, entry, pentry, entry);
415 (*ncommits)++;
416 if (*ncommits >= limit)
417 break;
419 /* Fill up to last requested commit if queue was empty. */
420 if (old_head_entry == NULL &&
421 got_object_id_cmp(pentry->id, last_id) == 0)
422 break;
424 entry = pentry;
427 done:
428 if (first_obj)
429 got_object_close(first_obj);
430 if (last_obj)
431 got_object_close(last_obj);
432 return err;
435 static const struct got_error *
436 fetch_commits(struct commit_queue_entry **start_entry,
437 struct got_object_id *start_id, struct commit_queue *commits,
438 int limit, struct got_repository *repo)
440 const struct got_error *err;
441 struct commit_queue_entry *entry;
442 int ncommits = 0;
443 struct got_object_id *head_id = NULL;
445 *start_entry = NULL;
447 err = get_head_commit_id(&head_id, repo);
448 if (err)
449 return err;
451 /* Prepend HEAD commit and all ancestors up to start commit. */
452 err = prepend_commits(&ncommits, commits, head_id, start_id, limit,
453 repo);
454 if (err)
455 return err;
457 if (got_object_id_cmp(head_id, start_id) == 0)
458 *start_entry = TAILQ_FIRST(commits);
459 else
460 *start_entry = TAILQ_LAST(commits, commit_queue);
462 if (ncommits >= limit)
463 return NULL;
465 /* Append more commits from start commit up to the requested limit. */
466 entry = TAILQ_LAST(commits, commit_queue);
467 while (entry && ncommits < limit) {
468 struct commit_queue_entry *pentry;
470 err = fetch_parent_commit(&pentry, entry, repo);
471 if (err)
472 break;
473 if (pentry)
474 TAILQ_INSERT_TAIL(commits, pentry, entry);
475 entry = pentry;
476 ncommits++;
479 if (err)
480 *start_entry = NULL;
481 return err;
484 static const struct got_error *
485 draw_commits(struct commit_queue_entry **last, struct commit_queue_entry *first,
486 int selected, int limit)
488 const struct got_error *err = NULL;
489 struct commit_queue_entry *entry;
490 int ncommits = 0;
492 wclear(tog_log_view.window);
494 entry = first;
495 *last = first;
496 while (entry) {
497 if (ncommits == limit)
498 break;
499 if (ncommits == selected)
500 wstandout(tog_log_view.window);
501 err = draw_commit(entry->commit, entry->id);
502 if (ncommits == selected)
503 wstandend(tog_log_view.window);
504 if (err)
505 break;
506 ncommits++;
507 *last = entry;
508 entry = TAILQ_NEXT(entry, entry);
511 update_panels();
512 doupdate();
514 return err;
517 static void
518 scroll_up(struct commit_queue_entry **first_displayed_entry, int n,
519 struct commit_queue *commits)
521 struct commit_queue_entry *entry;
522 int nscrolled = 0;
524 entry = TAILQ_FIRST(commits);
525 if (*first_displayed_entry == entry)
526 return;
528 entry = *first_displayed_entry;
529 while (entry && nscrolled < n) {
530 entry = TAILQ_PREV(entry, commit_queue, entry);
531 if (entry) {
532 *first_displayed_entry = entry;
533 nscrolled++;
538 static const struct got_error *
539 scroll_down(struct commit_queue_entry **first_displayed_entry, int n,
540 struct commit_queue_entry *last_displayed_entry,
541 struct commit_queue *commits, struct got_repository *repo)
543 const struct got_error *err = NULL;
544 struct commit_queue_entry *entry;
545 int nscrolled = 0;
547 if (last_displayed_entry->commit->nparents == 0)
548 return NULL;
550 entry = *first_displayed_entry;
551 do {
552 struct commit_queue_entry *pentry;
554 pentry = TAILQ_NEXT(entry, entry);
555 if (pentry == NULL) {
556 err = fetch_parent_commit(&pentry, entry, repo);
557 if (err)
558 break;
559 if (pentry == NULL) {
560 *first_displayed_entry = entry;
561 return NULL;
563 TAILQ_INSERT_TAIL(commits, pentry, entry);
564 last_displayed_entry = pentry;
567 *first_displayed_entry = pentry;
568 entry = pentry;
570 if (TAILQ_LAST(commits, commit_queue) == last_displayed_entry) {
571 err = fetch_parent_commit(&pentry, last_displayed_entry,
572 repo);
573 if (err)
574 break;
575 if (pentry) {
576 TAILQ_INSERT_TAIL(commits, pentry, entry);
577 last_displayed_entry = pentry;
580 } while (++nscrolled < n);
582 return NULL;
585 static int
586 num_parents(struct commit_queue_entry *entry)
588 int nparents = 0;
590 while (entry) {
591 entry = TAILQ_NEXT(entry, entry);
592 nparents++;
595 return nparents;
598 static const struct got_error *
599 show_log_view(struct got_object_id *start_id, struct got_repository *repo)
601 const struct got_error *err = NULL;
602 struct got_object_id *id;
603 int ch, done = 0, selected = 0, nparents;
604 struct commit_queue commits;
605 struct commit_queue_entry *first_displayed_entry = NULL;
606 struct commit_queue_entry *last_displayed_entry = NULL;
608 id = got_object_id_dup(start_id);
609 if (id == NULL)
610 return got_error_from_errno();
612 if (tog_log_view.window == NULL) {
613 tog_log_view.window = newwin(0, 0, 0, 0);
614 if (tog_log_view.window == NULL)
615 return got_error_from_errno();
616 keypad(tog_log_view.window, TRUE);
618 if (tog_log_view.panel == NULL) {
619 tog_log_view.panel = new_panel(tog_log_view.window);
620 if (tog_log_view.panel == NULL)
621 return got_error_from_errno();
624 TAILQ_INIT(&commits);
625 err = fetch_commits(&first_displayed_entry, id, &commits, LINES, repo);
626 if (err)
627 goto done;
628 while (!done) {
629 err = draw_commits(&last_displayed_entry, first_displayed_entry,
630 selected, LINES);
631 if (err)
632 goto done;
634 nodelay(stdscr, FALSE);
635 ch = wgetch(tog_log_view.window);
636 switch (ch) {
637 case ERR:
638 if (errno) {
639 err = got_error_from_errno();
640 goto done;
642 break;
643 case 'q':
644 done = 1;
645 break;
646 case 'k':
647 case KEY_UP:
648 if (selected > 0)
649 selected--;
650 if (selected > 0)
651 break;
652 scroll_up(&first_displayed_entry, 1, &commits);
653 break;
654 case KEY_PPAGE:
655 if (TAILQ_FIRST(&commits) ==
656 first_displayed_entry) {
657 selected = 0;
658 break;
660 scroll_up(&first_displayed_entry, LINES,
661 &commits);
662 break;
663 case 'j':
664 case KEY_DOWN:
665 nparents = num_parents(first_displayed_entry);
666 if (selected < LINES - 1 &&
667 selected < nparents - 1)
668 selected++;
669 if (selected < LINES - 1 &&
670 selected < nparents - 1)
671 break;
672 err = scroll_down(&first_displayed_entry, 1,
673 last_displayed_entry, &commits, repo);
674 if (err)
675 goto done;
676 break;
677 case KEY_NPAGE:
678 nparents = num_parents(first_displayed_entry);
679 if (nparents < LINES - 1 &&
680 selected < nparents - 1) {
681 selected = nparents - 1;
682 break;
684 err = scroll_down(&first_displayed_entry, LINES,
685 last_displayed_entry, &commits, repo);
686 if (err)
687 goto done;
688 nparents = num_parents(first_displayed_entry);
689 if (selected > nparents)
690 selected = nparents - 1;
691 break;
692 case KEY_RESIZE:
693 if (selected > LINES)
694 selected = LINES - 1;
695 break;
696 default:
697 break;
699 nodelay(stdscr, TRUE);
701 done:
702 free_commits(&commits);
703 return err;
706 const struct got_error *
707 cmd_log(int argc, char *argv[])
709 const struct got_error *error;
710 struct got_repository *repo;
711 struct got_object_id *start_id = NULL;
712 char *repo_path = NULL;
713 char *start_commit = NULL;
714 int ch;
716 #ifndef PROFILE
717 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
718 err(1, "pledge");
719 #endif
721 while ((ch = getopt(argc, argv, "c:")) != -1) {
722 switch (ch) {
723 case 'c':
724 start_commit = optarg;
725 break;
726 default:
727 usage();
728 /* NOTREACHED */
732 argc -= optind;
733 argv += optind;
735 if (argc == 0) {
736 repo_path = getcwd(NULL, 0);
737 if (repo_path == NULL)
738 return got_error_from_errno();
739 } else if (argc == 1) {
740 repo_path = realpath(argv[0], NULL);
741 if (repo_path == NULL)
742 return got_error_from_errno();
743 } else
744 usage_log();
746 error = got_repo_open(&repo, repo_path);
747 free(repo_path);
748 if (error != NULL)
749 return error;
751 if (start_commit == NULL) {
752 error = get_head_commit_id(&start_id, repo);
753 if (error != NULL)
754 return error;
755 } else {
756 struct got_object *obj;
757 error = got_object_open_by_id_str(&obj, repo, start_commit);
758 if (error == NULL) {
759 start_id = got_object_get_id(obj);
760 if (start_id == NULL)
761 error = got_error_from_errno();
764 if (error != NULL)
765 return error;
766 error = show_log_view(start_id, repo);
767 free(start_id);
768 got_repo_close(repo);
769 return error;
772 __dead void
773 usage_diff(void)
775 endwin();
776 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
777 getprogname());
778 exit(1);
781 static const struct got_error *
782 diff_blobs(FILE *outfile, struct got_object *obj1, struct got_object *obj2,
783 struct got_repository *repo)
785 const struct got_error *err;
786 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
788 err = got_object_blob_open(&blob1, repo, obj1, 8192);
789 if (err)
790 goto done;
791 err = got_object_blob_open(&blob2, repo, obj2, 8192);
792 if (err)
793 goto done;
795 err = got_diff_blob(blob1, blob2, NULL, NULL, outfile);
796 done:
797 if (blob1)
798 got_object_blob_close(blob1);
799 if (blob2)
800 got_object_blob_close(blob2);
801 return err;
804 static const struct got_error *
805 diff_trees(FILE *outfile, struct got_object *obj1, struct got_object *obj2,
806 struct got_repository *repo)
808 const struct got_error *err;
809 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
811 err = got_object_tree_open(&tree1, repo, obj1);
812 if (err)
813 goto done;
814 err = got_object_tree_open(&tree2, repo, obj2);
815 if (err)
816 goto done;
818 err = got_diff_tree(tree1, tree2, repo, outfile);
819 done:
820 if (tree1)
821 got_object_tree_close(tree1);
822 if (tree2)
823 got_object_tree_close(tree2);
824 return err;
827 static const struct got_error *
828 diff_commits(FILE *outfile, struct got_object *obj1, struct got_object *obj2,
829 struct got_repository *repo)
831 const struct got_error *err;
832 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
833 struct got_object *tree_obj1 = NULL, *tree_obj2 = NULL;
835 err = got_object_commit_open(&commit1, repo, obj1);
836 if (err)
837 goto done;
838 err = got_object_commit_open(&commit2, repo, obj2);
839 if (err)
840 goto done;
842 err = got_object_open(&tree_obj1, repo, commit1->tree_id);
843 if (err)
844 goto done;
845 err = got_object_open(&tree_obj2, repo, commit2->tree_id);
846 if (err)
847 goto done;
849 err = diff_trees(outfile, tree_obj1, tree_obj2, repo);
850 done:
851 if (tree_obj1)
852 got_object_close(tree_obj1);
853 if (tree_obj2)
854 got_object_close(tree_obj2);
855 if (commit1)
856 got_object_commit_close(commit1);
857 if (commit2)
858 got_object_commit_close(commit2);
859 return err;
863 const struct got_error *
864 draw_diff(FILE *f, int *first_displayed_line, int *last_displayed_line,
865 int *eof, int max_lines)
867 int nlines = 0, nprinted = 0;
869 rewind(f);
870 wclear(tog_diff_view.window);
872 *eof = 0;
873 while (nprinted < max_lines) {
874 char *line;
875 size_t lineno;
876 size_t linelen;
877 const char delim[3] = { '\0', '\0', '\0'};
879 line = fparseln(f, &linelen, &lineno, delim, 0);
880 if (line == NULL) {
881 *eof = 1;
882 break;
884 if (++nlines < *first_displayed_line) {
885 free(line);
886 continue;
889 if (linelen > COLS - 1)
890 line[COLS - 1] = '\0';
891 waddstr(tog_diff_view.window, line);
892 waddch(tog_diff_view.window, '\n');
893 if (++nprinted == 1)
894 *first_displayed_line = nlines;
895 free(line);
897 *last_displayed_line = nlines;
899 update_panels();
900 doupdate();
902 return NULL;
905 const struct got_error *
906 show_diff_view(struct got_object *obj1, struct got_object *obj2,
907 struct got_repository *repo)
909 const struct got_error *err;
910 FILE *f;
911 int ch, done = 0, first_displayed_line = 1, last_displayed_line = LINES;
912 int eof;
914 if (got_object_get_type(obj1) != got_object_get_type(obj2))
915 return got_error(GOT_ERR_OBJ_TYPE);
917 f = tog_opentemp();
918 if (f == NULL)
919 return got_error_from_errno();
921 switch (got_object_get_type(obj1)) {
922 case GOT_OBJ_TYPE_BLOB:
923 err = diff_blobs(f, obj1, obj2, repo);
924 break;
925 case GOT_OBJ_TYPE_TREE:
926 err = diff_trees(f, obj1, obj2, repo);
927 break;
928 case GOT_OBJ_TYPE_COMMIT:
929 err = diff_commits(f, obj1, obj2, repo);
930 break;
931 default:
932 return got_error(GOT_ERR_OBJ_TYPE);
935 fflush(f);
937 if (tog_diff_view.window == NULL) {
938 tog_diff_view.window = newwin(0, 0, 0, 0);
939 if (tog_diff_view.window == NULL)
940 return got_error_from_errno();
941 keypad(tog_diff_view.window, TRUE);
943 if (tog_diff_view.panel == NULL) {
944 tog_diff_view.panel = new_panel(tog_diff_view.window);
945 if (tog_diff_view.panel == NULL)
946 return got_error_from_errno();
947 } else
948 show_panel(tog_diff_view.panel);
950 while (!done) {
951 err = draw_diff(f, &first_displayed_line, &last_displayed_line,
952 &eof, LINES);
953 if (err)
954 break;
955 nodelay(stdscr, FALSE);
956 ch = wgetch(tog_diff_view.window);
957 switch (ch) {
958 case 'q':
959 done = 1;
960 break;
961 case 'k':
962 case KEY_UP:
963 if (first_displayed_line > 1)
964 first_displayed_line--;
965 break;
966 case 'j':
967 case KEY_DOWN:
968 if (!eof)
969 first_displayed_line++;
970 break;
971 default:
972 break;
974 nodelay(stdscr, TRUE);
976 fclose(f);
977 return err;
980 const struct got_error *
981 cmd_diff(int argc, char *argv[])
983 const struct got_error *error = NULL;
984 struct got_repository *repo = NULL;
985 struct got_object *obj1 = NULL, *obj2 = NULL;
986 char *repo_path = NULL;
987 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
988 int ch;
990 #ifndef PROFILE
991 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
992 err(1, "pledge");
993 #endif
995 while ((ch = getopt(argc, argv, "")) != -1) {
996 switch (ch) {
997 default:
998 usage();
999 /* NOTREACHED */
1003 argc -= optind;
1004 argv += optind;
1006 if (argc == 0) {
1007 usage_diff(); /* TODO show local worktree changes */
1008 } else if (argc == 2) {
1009 repo_path = getcwd(NULL, 0);
1010 if (repo_path == NULL)
1011 return got_error_from_errno();
1012 obj_id_str1 = argv[0];
1013 obj_id_str2 = argv[1];
1014 } else if (argc == 3) {
1015 repo_path = realpath(argv[0], NULL);
1016 if (repo_path == NULL)
1017 return got_error_from_errno();
1018 obj_id_str1 = argv[1];
1019 obj_id_str2 = argv[2];
1020 } else
1021 usage_diff();
1023 error = got_repo_open(&repo, repo_path);
1024 free(repo_path);
1025 if (error)
1026 goto done;
1028 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
1029 if (error)
1030 goto done;
1032 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1033 if (error)
1034 goto done;
1036 error = show_diff_view(obj1, obj2, repo);
1037 done:
1038 got_repo_close(repo);
1039 if (obj1)
1040 got_object_close(obj1);
1041 if (obj2)
1042 got_object_close(obj2);
1043 return error;
1046 __dead void
1047 usage_blame(void)
1049 endwin();
1050 fprintf(stderr, "usage: %s blame [repository-path] blob-object\n",
1051 getprogname());
1052 exit(1);
1055 const struct got_error *
1056 cmd_blame(int argc, char *argv[])
1058 return got_error(GOT_ERR_NOT_IMPL);
1061 static const struct got_error *
1062 init_curses(void)
1064 initscr();
1065 cbreak();
1066 noecho();
1067 nonl();
1068 intrflush(stdscr, FALSE);
1069 keypad(stdscr, TRUE);
1070 curs_set(0);
1072 tog_main_win = newwin(0, 0, 0, 0);
1073 if (tog_main_win == NULL)
1074 return got_error_from_errno();
1075 tog_main_panel = new_panel(tog_main_win);
1076 if (tog_main_panel == NULL)
1077 return got_error_from_errno();
1079 return NULL;
1082 __dead void
1083 usage(void)
1085 int i;
1087 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
1088 "Available commands:\n", getprogname());
1089 for (i = 0; i < nitems(tog_commands); i++) {
1090 struct tog_cmd *cmd = &tog_commands[i];
1091 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
1093 exit(1);
1096 static char **
1097 make_argv(const char *arg0, const char *arg1)
1099 char **argv;
1100 int argc = (arg1 == NULL ? 1 : 2);
1102 argv = calloc(argc, sizeof(char *));
1103 if (argv == NULL)
1104 err(1, "calloc");
1105 argv[0] = strdup(arg0);
1106 if (argv[0] == NULL)
1107 err(1, "calloc");
1108 if (arg1) {
1109 argv[1] = strdup(arg1);
1110 if (argv[1] == NULL)
1111 err(1, "calloc");
1114 return argv;
1117 int
1118 main(int argc, char *argv[])
1120 const struct got_error *error = NULL;
1121 struct tog_cmd *cmd = NULL;
1122 int ch, hflag = 0;
1123 char **cmd_argv = NULL;
1125 setlocale(LC_ALL, "");
1127 while ((ch = getopt(argc, argv, "h")) != -1) {
1128 switch (ch) {
1129 case 'h':
1130 hflag = 1;
1131 break;
1132 default:
1133 usage();
1134 /* NOTREACHED */
1138 argc -= optind;
1139 argv += optind;
1140 optind = 0;
1141 optreset = 1;
1143 if (argc == 0) {
1144 /* Build an argument vector which runs a default command. */
1145 cmd = &tog_commands[0];
1146 cmd_argv = make_argv(cmd->name, NULL);
1147 argc = 1;
1148 } else {
1149 int i;
1151 /* Did the user specific a command? */
1152 for (i = 0; i < nitems(tog_commands); i++) {
1153 if (strncmp(tog_commands[i].name, argv[0],
1154 strlen(argv[0])) == 0) {
1155 cmd = &tog_commands[i];
1156 if (hflag)
1157 tog_commands[i].cmd_usage();
1158 break;
1161 if (cmd == NULL) {
1162 /* Did the user specify a repository? */
1163 char *repo_path = realpath(argv[0], NULL);
1164 if (repo_path) {
1165 struct got_repository *repo;
1166 error = got_repo_open(&repo, repo_path);
1167 if (error == NULL)
1168 got_repo_close(repo);
1169 } else
1170 error = got_error_from_errno();
1171 if (error) {
1172 fprintf(stderr, "%s: '%s' is neither a known "
1173 "command nor a path to a repository\n",
1174 getprogname(), argv[0]);
1175 free(repo_path);
1176 return 1;
1178 cmd = &tog_commands[0];
1179 cmd_argv = make_argv(cmd->name, repo_path);
1180 argc = 2;
1181 free(repo_path);
1185 error = init_curses();
1186 if (error) {
1187 fprintf(stderr, "cannot initialize ncurses: %s\n", error->msg);
1188 return 1;
1191 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
1192 if (error)
1193 goto done;
1194 done:
1195 endwin();
1196 free(cmd_argv);
1197 if (error)
1198 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
1199 return 0;