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"
37 #include "got_opentemp.h"
39 #ifndef MIN
40 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
41 #endif
43 #ifndef nitems
44 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
45 #endif
47 struct tog_cmd {
48 const char *name;
49 const struct got_error *(*cmd_main)(int, char *[]);
50 void (*cmd_usage)(void);
51 const char *descr;
52 };
54 __dead static void usage(void);
55 __dead static void usage_log(void);
56 __dead static void usage_diff(void);
57 __dead static void usage_blame(void);
59 static const struct got_error* cmd_log(int, char *[]);
60 static const struct got_error* cmd_diff(int, char *[]);
61 static const struct got_error* cmd_blame(int, char *[]);
63 static struct tog_cmd tog_commands[] = {
64 { "log", cmd_log, usage_log,
65 "show repository history" },
66 { "diff", cmd_diff, usage_diff,
67 "compare files and directories" },
68 { "blame", cmd_blame, usage_blame,
69 "show line-by-line file history" },
70 };
72 static struct tog_view {
73 WINDOW *window;
74 PANEL *panel;
75 } tog_log_view, tog_diff_view;
77 static const struct got_error *
78 show_diff_view(struct got_object *, struct got_object *,
79 struct got_repository *);
80 static const struct got_error *
81 show_log_view(struct got_object_id *, struct got_repository *);
83 __dead static void
84 usage_log(void)
85 {
86 endwin();
87 fprintf(stderr, "usage: %s log [-c commit] [repository-path]\n",
88 getprogname());
89 exit(1);
90 }
92 static const struct got_error *
93 draw_commit(struct got_commit_object *commit, struct got_object_id *id)
94 {
95 const struct got_error *err = NULL;
96 char *logmsg0 = NULL, *logmsg = NULL;
97 char *author0 = NULL, *author = NULL;
98 char *newline, *smallerthan;
99 char *line = NULL;
100 char *id_str = NULL;
101 const size_t id_display_len = 8;
102 const size_t author_display_len = 16;
103 size_t id_len, author_len, logmsg_len, avail;
104 int i, col;
106 err = got_object_id_str(&id_str, id);
107 if (err)
108 return err;
109 id_len = strlen(id_str);
111 logmsg0 = strdup(commit->logmsg);
112 if (logmsg0 == NULL) {
113 err = got_error_from_errno();
114 goto done;
116 logmsg = logmsg0;
117 while (*logmsg == '\n')
118 logmsg++;
119 newline = strchr(logmsg, '\n');
120 if (newline)
121 *newline = '\0';
122 logmsg_len = strlen(logmsg);
124 author0 = strdup(commit->author);
125 if (author0 == NULL) {
126 err = got_error_from_errno();
127 goto done;
129 author = author0;
130 smallerthan = strchr(author, '<');
131 if (smallerthan)
132 *smallerthan = '\0';
133 else {
134 char *at = strchr(author, '@');
135 if (at)
136 *at = '\0';
138 author_len = strlen(author);
140 avail = COLS - 1;
141 line = calloc(avail + 1, sizeof(*line));
142 if (line == NULL) {
143 err = got_error_from_errno();
144 goto done;
147 col = 0;
148 for (i = 0; i < MIN(id_display_len, id_len); i++) {
149 if (col >= avail)
150 goto draw;
151 line[col++] = id_str[i];
153 while (i < id_display_len) {
154 if (col >= avail)
155 goto draw;
156 line[col++] = ' ';
157 i++;
159 if (col >= avail)
160 goto draw;
161 line[col++] = ' ';
162 for (i = 0; i < MIN(author_display_len, author_len); i++) {
163 if (col >= avail)
164 goto draw;
165 line[col++] = author[i];
167 while (i < author_display_len) {
168 if (col >= avail)
169 goto draw;
170 line[col++] = ' ';
171 i++;
173 if (col >= avail)
174 goto draw;
175 line[col++] = ' ';
177 while (col < avail && *logmsg)
178 line[col++] = *logmsg++;
179 while (col < avail)
180 line[col++] = ' ';
181 draw:
182 waddstr(tog_log_view.window, line);
183 waddch(tog_log_view.window, '\n');
184 done:
185 free(logmsg0);
186 free(author0);
187 free(line);
188 free(id_str);
189 return err;
192 struct commit_queue_entry {
193 TAILQ_ENTRY(commit_queue_entry) entry;
194 struct got_object_id *id;
195 struct got_commit_object *commit;
196 };
197 TAILQ_HEAD(commit_queue, commit_queue_entry);
199 static struct commit_queue_entry *
200 alloc_commit_queue_entry(struct got_commit_object *commit,
201 struct got_object_id *id)
203 struct commit_queue_entry *entry;
205 entry = calloc(1, sizeof(*entry));
206 if (entry == NULL)
207 return NULL;
209 entry->id = id;
210 entry->commit = commit;
211 return entry;
214 static void
215 pop_commit(struct commit_queue *commits)
217 struct commit_queue_entry *entry;
219 entry = TAILQ_FIRST(commits);
220 TAILQ_REMOVE(commits, entry, entry);
221 got_object_commit_close(entry->commit);
222 free(entry->id);
223 free(entry);
226 static void
227 free_commits(struct commit_queue *commits)
229 while (!TAILQ_EMPTY(commits))
230 pop_commit(commits);
233 static const struct got_error *
234 fetch_parent_commit(struct commit_queue_entry **pentry,
235 struct commit_queue_entry *entry, struct got_repository *repo)
237 const struct got_error *err = NULL;
238 struct got_object *obj = NULL;
239 struct got_commit_object *commit;
240 struct got_object_id *id;
241 struct got_parent_id *pid;
243 *pentry = NULL;
245 /* Follow the first parent (TODO: handle merge commits). */
246 pid = SIMPLEQ_FIRST(&entry->commit->parent_ids);
247 if (pid == NULL)
248 return NULL;
249 err = got_object_open(&obj, repo, pid->id);
250 if (err)
251 return err;
252 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
253 err = got_error(GOT_ERR_OBJ_TYPE);
254 got_object_close(obj);
255 return err;
258 err = got_object_commit_open(&commit, repo, obj);
259 got_object_close(obj);
260 if (err)
261 return err;
263 id = got_object_id_dup(pid->id);
264 if (id == NULL) {
265 err = got_error_from_errno();
266 got_object_commit_close(commit);
267 return err;;
270 *pentry = alloc_commit_queue_entry(commit, id);
271 if (*pentry == NULL) {
272 err = got_error_from_errno();
273 got_object_commit_close(commit);
276 return err;;
279 static const struct got_error *
280 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
282 const struct got_error *err = NULL;
283 struct got_reference *head_ref;
285 *head_id = NULL;
287 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
288 if (err)
289 return err;
291 err = got_ref_resolve(head_id, repo, head_ref);
292 got_ref_close(head_ref);
293 if (err) {
294 *head_id = NULL;
295 return err;
298 return NULL;
301 static const struct got_error *
302 prepend_commits(int *ncommits, struct commit_queue *commits,
303 struct got_object_id *first_id, struct got_object_id *last_id,
304 int limit, struct got_repository *repo)
306 const struct got_error *err = NULL;
307 struct got_object *first_obj = NULL, *last_obj = NULL;
308 struct got_commit_object *commit = NULL;
309 struct got_object_id *id = NULL;
310 struct commit_queue_entry *entry, *old_head_entry;
312 *ncommits = 0;
314 err = got_object_open(&first_obj, repo, first_id);
315 if (err)
316 goto done;
317 if (got_object_get_type(first_obj) != GOT_OBJ_TYPE_COMMIT) {
318 err = got_error(GOT_ERR_OBJ_TYPE);
319 goto done;
321 err = got_object_open(&last_obj, repo, last_id);
322 if (err)
323 goto done;
324 if (got_object_get_type(last_obj) != GOT_OBJ_TYPE_COMMIT) {
325 err = got_error(GOT_ERR_OBJ_TYPE);
326 goto done;
329 err = got_object_commit_open(&commit, repo, first_obj);
330 if (err)
331 goto done;
333 id = got_object_id_dup(first_id);
334 if (id == NULL) {
335 err = got_error_from_errno();
336 goto done;
339 entry = alloc_commit_queue_entry(commit, id);
340 if (entry == NULL)
341 return got_error_from_errno();
343 old_head_entry = TAILQ_FIRST(commits);
344 if (old_head_entry)
345 TAILQ_INSERT_BEFORE(old_head_entry, entry, entry);
346 else
347 TAILQ_INSERT_HEAD(commits, entry, entry);
349 *ncommits = 1;
351 /*
352 * Fetch parent commits.
353 * XXX If first and last commit aren't ancestrally related this loop
354 * we will keep iterating until a root commit is encountered.
355 */
356 while (1) {
357 struct commit_queue_entry *pentry;
359 err = fetch_parent_commit(&pentry, entry, repo);
360 if (err)
361 goto done;
362 if (pentry == NULL)
363 break;
365 /*
366 * Fill up to old HEAD commit if commit queue was not empty.
367 * We must not leave a gap in history.
368 */
369 if (old_head_entry &&
370 got_object_id_cmp(pentry->id, old_head_entry->id) == 0)
371 break;
373 TAILQ_INSERT_AFTER(commits, entry, pentry, entry);
374 (*ncommits)++;
375 if (*ncommits >= limit)
376 break;
378 /* Fill up to last requested commit if queue was empty. */
379 if (old_head_entry == NULL &&
380 got_object_id_cmp(pentry->id, last_id) == 0)
381 break;
383 entry = pentry;
386 done:
387 if (first_obj)
388 got_object_close(first_obj);
389 if (last_obj)
390 got_object_close(last_obj);
391 return err;
394 static const struct got_error *
395 fetch_commits(struct commit_queue_entry **start_entry,
396 struct got_object_id *start_id, struct commit_queue *commits,
397 int limit, struct got_repository *repo)
399 const struct got_error *err;
400 struct commit_queue_entry *entry;
401 int ncommits = 0;
402 struct got_object_id *head_id = NULL;
404 *start_entry = NULL;
406 err = get_head_commit_id(&head_id, repo);
407 if (err)
408 return err;
410 /* Prepend HEAD commit and all ancestors up to start commit. */
411 err = prepend_commits(&ncommits, commits, head_id, start_id, limit,
412 repo);
413 if (err)
414 return err;
416 if (got_object_id_cmp(head_id, start_id) == 0)
417 *start_entry = TAILQ_FIRST(commits);
418 else
419 *start_entry = TAILQ_LAST(commits, commit_queue);
421 if (ncommits >= limit)
422 return NULL;
424 /* Append more commits from start commit up to the requested limit. */
425 entry = TAILQ_LAST(commits, commit_queue);
426 while (entry && ncommits < limit) {
427 struct commit_queue_entry *pentry;
429 err = fetch_parent_commit(&pentry, entry, repo);
430 if (err)
431 break;
432 if (pentry)
433 TAILQ_INSERT_TAIL(commits, pentry, entry);
434 entry = pentry;
435 ncommits++;
438 if (err)
439 *start_entry = NULL;
440 return err;
443 static const struct got_error *
444 draw_commits(struct commit_queue_entry **last, struct commit_queue_entry **selected,
445 struct commit_queue_entry *first, int selected_idx, int limit)
447 const struct got_error *err = NULL;
448 struct commit_queue_entry *entry;
449 int ncommits = 0;
451 wclear(tog_log_view.window);
453 entry = first;
454 *last = first;
455 while (entry) {
456 if (ncommits == limit)
457 break;
458 if (ncommits == selected_idx) {
459 wstandout(tog_log_view.window);
460 *selected = entry;
462 err = draw_commit(entry->commit, entry->id);
463 if (ncommits == selected_idx)
464 wstandend(tog_log_view.window);
465 if (err)
466 break;
467 ncommits++;
468 *last = entry;
469 entry = TAILQ_NEXT(entry, entry);
472 update_panels();
473 doupdate();
475 return err;
478 static void
479 scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
480 struct commit_queue *commits)
482 struct commit_queue_entry *entry;
483 int nscrolled = 0;
485 entry = TAILQ_FIRST(commits);
486 if (*first_displayed_entry == entry)
487 return;
489 entry = *first_displayed_entry;
490 while (entry && nscrolled < maxscroll) {
491 entry = TAILQ_PREV(entry, commit_queue, entry);
492 if (entry) {
493 *first_displayed_entry = entry;
494 nscrolled++;
499 static const struct got_error *
500 scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
501 struct commit_queue_entry *last_displayed_entry,
502 struct commit_queue *commits, struct got_repository *repo)
504 const struct got_error *err = NULL;
505 struct commit_queue_entry *entry;
506 int nscrolled = 0;
508 if (last_displayed_entry->commit->nparents == 0)
509 return NULL;
511 entry = *first_displayed_entry;
512 do {
513 struct commit_queue_entry *pentry;
515 pentry = TAILQ_NEXT(entry, entry);
516 if (pentry == NULL) {
517 err = fetch_parent_commit(&pentry, entry, repo);
518 if (err || pentry == NULL)
519 break;
520 TAILQ_INSERT_TAIL(commits, pentry, entry);
521 last_displayed_entry = pentry;
524 *first_displayed_entry = pentry;
525 entry = pentry;
527 if (TAILQ_LAST(commits, commit_queue) == last_displayed_entry) {
528 err = fetch_parent_commit(&pentry, last_displayed_entry,
529 repo);
530 if (err)
531 break;
532 if (pentry) {
533 TAILQ_INSERT_TAIL(commits, pentry, entry);
534 last_displayed_entry = pentry;
537 } while (++nscrolled < maxscroll);
539 return NULL;
542 static int
543 num_parents(struct commit_queue_entry *entry)
545 int nparents = 0;
547 while (entry) {
548 entry = TAILQ_NEXT(entry, entry);
549 nparents++;
552 return nparents;
555 static const struct got_error *
556 show_commit(struct commit_queue_entry *entry, struct got_repository *repo)
558 const struct got_error *err;
559 struct commit_queue_entry *pentry;
560 struct got_object *obj1 = NULL, *obj2 = NULL;
562 err = got_object_open(&obj2, repo, entry->id);
563 if (err)
564 return err;
566 pentry = TAILQ_NEXT(entry, entry);
567 if (pentry == NULL) {
568 err = fetch_parent_commit(&pentry, entry, repo);
569 if (err)
570 return err;
572 if (pentry) {
573 err = got_object_open(&obj1, repo, pentry->id);
574 if (err)
575 goto done;
578 err = show_diff_view(obj1, obj2, repo);
579 done:
580 if (obj1)
581 got_object_close(obj1);
582 if (obj2)
583 got_object_close(obj2);
584 return err;
587 static const struct got_error *
588 show_log_view(struct got_object_id *start_id, struct got_repository *repo)
590 const struct got_error *err = NULL;
591 struct got_object_id *id;
592 int ch, done = 0, selected = 0, nparents;
593 struct commit_queue commits;
594 struct commit_queue_entry *first_displayed_entry = NULL;
595 struct commit_queue_entry *last_displayed_entry = NULL;
596 struct commit_queue_entry *selected_entry = NULL;
598 id = got_object_id_dup(start_id);
599 if (id == NULL)
600 return got_error_from_errno();
602 if (tog_log_view.window == NULL) {
603 tog_log_view.window = newwin(0, 0, 0, 0);
604 if (tog_log_view.window == NULL)
605 return got_error_from_errno();
606 keypad(tog_log_view.window, TRUE);
608 if (tog_log_view.panel == NULL) {
609 tog_log_view.panel = new_panel(tog_log_view.window);
610 if (tog_log_view.panel == NULL)
611 return got_error_from_errno();
612 } else
613 show_panel(tog_log_view.panel);
615 TAILQ_INIT(&commits);
616 err = fetch_commits(&first_displayed_entry, id, &commits, LINES, repo);
617 if (err)
618 goto done;
619 while (!done) {
620 err = draw_commits(&last_displayed_entry, &selected_entry,
621 first_displayed_entry, selected, LINES);
622 if (err)
623 goto done;
625 nodelay(stdscr, FALSE);
626 ch = wgetch(tog_log_view.window);
627 nodelay(stdscr, TRUE);
628 switch (ch) {
629 case ERR:
630 if (errno) {
631 err = got_error_from_errno();
632 goto done;
634 break;
635 case 'q':
636 done = 1;
637 break;
638 case 'k':
639 case KEY_UP:
640 if (selected > 0)
641 selected--;
642 if (selected > 0)
643 break;
644 scroll_up(&first_displayed_entry, 1, &commits);
645 break;
646 case KEY_PPAGE:
647 if (TAILQ_FIRST(&commits) ==
648 first_displayed_entry) {
649 selected = 0;
650 break;
652 scroll_up(&first_displayed_entry, LINES,
653 &commits);
654 break;
655 case 'j':
656 case KEY_DOWN:
657 nparents = num_parents(first_displayed_entry);
658 if (selected < LINES - 1 &&
659 selected < nparents - 1)
660 selected++;
661 else {
662 err = scroll_down(&first_displayed_entry, 1,
663 last_displayed_entry, &commits, repo);
664 if (err)
665 goto done;
667 break;
668 case KEY_NPAGE:
669 nparents = num_parents(first_displayed_entry);
670 if (nparents < LINES - 1 &&
671 selected < nparents - 1) {
672 selected = nparents - 1;
673 break;
675 err = scroll_down(&first_displayed_entry, LINES,
676 last_displayed_entry, &commits, repo);
677 if (err)
678 goto done;
679 nparents = num_parents(first_displayed_entry);
680 if (selected > nparents)
681 selected = nparents - 1;
682 break;
683 case KEY_RESIZE:
684 if (selected > LINES)
685 selected = LINES - 1;
686 break;
687 case KEY_ENTER:
688 case '\r':
689 err = show_commit(selected_entry, repo);
690 if (err)
691 break;
692 show_panel(tog_log_view.panel);
693 break;
694 default:
695 break;
698 done:
699 free_commits(&commits);
700 return err;
703 static const struct got_error *
704 cmd_log(int argc, char *argv[])
706 const struct got_error *error;
707 struct got_repository *repo;
708 struct got_object_id *start_id = NULL;
709 char *repo_path = NULL;
710 char *start_commit = NULL;
711 int ch;
713 #ifndef PROFILE
714 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
715 err(1, "pledge");
716 #endif
718 while ((ch = getopt(argc, argv, "c:")) != -1) {
719 switch (ch) {
720 case 'c':
721 start_commit = optarg;
722 break;
723 default:
724 usage();
725 /* NOTREACHED */
729 argc -= optind;
730 argv += optind;
732 if (argc == 0) {
733 repo_path = getcwd(NULL, 0);
734 if (repo_path == NULL)
735 return got_error_from_errno();
736 } else if (argc == 1) {
737 repo_path = realpath(argv[0], NULL);
738 if (repo_path == NULL)
739 return got_error_from_errno();
740 } else
741 usage_log();
743 error = got_repo_open(&repo, repo_path);
744 free(repo_path);
745 if (error != NULL)
746 return error;
748 if (start_commit == NULL) {
749 error = get_head_commit_id(&start_id, repo);
750 if (error != NULL)
751 return error;
752 } else {
753 struct got_object *obj;
754 error = got_object_open_by_id_str(&obj, repo, start_commit);
755 if (error == NULL) {
756 start_id = got_object_get_id(obj);
757 if (start_id == NULL)
758 error = got_error_from_errno();
761 if (error != NULL)
762 return error;
763 error = show_log_view(start_id, repo);
764 free(start_id);
765 got_repo_close(repo);
766 return error;
769 __dead static void
770 usage_diff(void)
772 endwin();
773 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
774 getprogname());
775 exit(1);
778 static char *
779 parse_next_line(FILE *f, size_t *len)
781 char *line;
782 size_t linelen;
783 size_t lineno;
784 const char delim[3] = { '\0', '\0', '\0'};
786 line = fparseln(f, &linelen, &lineno, delim, 0);
787 if (len)
788 *len = linelen;
789 return line;
792 static const struct got_error *
793 draw_diff(FILE *f, int *first_displayed_line, int *last_displayed_line,
794 int *eof, int max_lines)
796 int nlines = 0, nprinted = 0;
797 char *line;
798 size_t len;
800 rewind(f);
801 wclear(tog_diff_view.window);
803 *eof = 0;
804 while (nprinted < max_lines) {
805 line = parse_next_line(f, &len);
806 if (line == NULL) {
807 *eof = 1;
808 break;
810 if (++nlines < *first_displayed_line) {
811 free(line);
812 continue;
815 if (len > COLS - 1)
816 line[COLS - 1] = '\0';
817 waddstr(tog_diff_view.window, line);
818 waddch(tog_diff_view.window, '\n');
819 if (++nprinted == 1)
820 *first_displayed_line = nlines;
821 free(line);
823 *last_displayed_line = nlines;
825 update_panels();
826 doupdate();
828 return NULL;
831 static const struct got_error *
832 show_diff_view(struct got_object *obj1, struct got_object *obj2,
833 struct got_repository *repo)
835 const struct got_error *err;
836 FILE *f;
837 int ch, done = 0, first_displayed_line = 1, last_displayed_line = LINES;
838 int eof, i;
840 if (obj1 != NULL && obj2 != NULL &&
841 got_object_get_type(obj1) != got_object_get_type(obj2))
842 return got_error(GOT_ERR_OBJ_TYPE);
844 f = got_opentemp();
845 if (f == NULL)
846 return got_error_from_errno();
848 switch (got_object_get_type(obj1 ? obj1 : obj2)) {
849 case GOT_OBJ_TYPE_BLOB:
850 err = got_diff_objects_as_blobs(obj1, obj2, repo, f);
851 break;
852 case GOT_OBJ_TYPE_TREE:
853 err = got_diff_objects_as_trees(obj1, obj2, repo, f);
854 break;
855 case GOT_OBJ_TYPE_COMMIT:
856 err = got_diff_objects_as_commits(obj1, obj2, repo, f);
857 break;
858 default:
859 return got_error(GOT_ERR_OBJ_TYPE);
862 fflush(f);
864 if (tog_diff_view.window == NULL) {
865 tog_diff_view.window = newwin(0, 0, 0, 0);
866 if (tog_diff_view.window == NULL)
867 return got_error_from_errno();
868 keypad(tog_diff_view.window, TRUE);
870 if (tog_diff_view.panel == NULL) {
871 tog_diff_view.panel = new_panel(tog_diff_view.window);
872 if (tog_diff_view.panel == NULL)
873 return got_error_from_errno();
874 } else
875 show_panel(tog_diff_view.panel);
877 while (!done) {
878 err = draw_diff(f, &first_displayed_line, &last_displayed_line,
879 &eof, LINES);
880 if (err)
881 break;
882 nodelay(stdscr, FALSE);
883 ch = wgetch(tog_diff_view.window);
884 nodelay(stdscr, TRUE);
885 switch (ch) {
886 case 'q':
887 done = 1;
888 break;
889 case 'k':
890 case KEY_UP:
891 case KEY_BACKSPACE:
892 if (first_displayed_line > 1)
893 first_displayed_line--;
894 break;
895 case KEY_PPAGE:
896 i = 0;
897 while (i++ < LINES - 1 && first_displayed_line > 1)
898 first_displayed_line--;
899 break;
900 case 'j':
901 case KEY_DOWN:
902 case KEY_ENTER:
903 case '\r':
904 if (!eof)
905 first_displayed_line++;
906 break;
907 case KEY_NPAGE:
908 case ' ':
909 i = 0;
910 while (!eof && i++ < LINES - 1) {
911 char *line = parse_next_line(f, NULL);
912 first_displayed_line++;
913 if (line == NULL)
914 break;
916 break;
917 default:
918 break;
921 fclose(f);
922 return err;
925 static const struct got_error *
926 cmd_diff(int argc, char *argv[])
928 const struct got_error *error = NULL;
929 struct got_repository *repo = NULL;
930 struct got_object *obj1 = NULL, *obj2 = NULL;
931 char *repo_path = NULL;
932 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
933 int ch;
935 #ifndef PROFILE
936 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
937 err(1, "pledge");
938 #endif
940 while ((ch = getopt(argc, argv, "")) != -1) {
941 switch (ch) {
942 default:
943 usage();
944 /* NOTREACHED */
948 argc -= optind;
949 argv += optind;
951 if (argc == 0) {
952 usage_diff(); /* TODO show local worktree changes */
953 } else if (argc == 2) {
954 repo_path = getcwd(NULL, 0);
955 if (repo_path == NULL)
956 return got_error_from_errno();
957 obj_id_str1 = argv[0];
958 obj_id_str2 = argv[1];
959 } else if (argc == 3) {
960 repo_path = realpath(argv[0], NULL);
961 if (repo_path == NULL)
962 return got_error_from_errno();
963 obj_id_str1 = argv[1];
964 obj_id_str2 = argv[2];
965 } else
966 usage_diff();
968 error = got_repo_open(&repo, repo_path);
969 free(repo_path);
970 if (error)
971 goto done;
973 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
974 if (error)
975 goto done;
977 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
978 if (error)
979 goto done;
981 error = show_diff_view(obj1, obj2, repo);
982 done:
983 got_repo_close(repo);
984 if (obj1)
985 got_object_close(obj1);
986 if (obj2)
987 got_object_close(obj2);
988 return error;
991 __dead static void
992 usage_blame(void)
994 endwin();
995 fprintf(stderr, "usage: %s blame [repository-path] blob-object\n",
996 getprogname());
997 exit(1);
1000 static const struct got_error *
1001 cmd_blame(int argc, char *argv[])
1003 return got_error(GOT_ERR_NOT_IMPL);
1006 static void
1007 init_curses(void)
1009 initscr();
1010 cbreak();
1011 noecho();
1012 nonl();
1013 intrflush(stdscr, FALSE);
1014 keypad(stdscr, TRUE);
1015 curs_set(0);
1018 __dead static void
1019 usage(void)
1021 int i;
1023 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
1024 "Available commands:\n", getprogname());
1025 for (i = 0; i < nitems(tog_commands); i++) {
1026 struct tog_cmd *cmd = &tog_commands[i];
1027 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
1029 exit(1);
1032 static char **
1033 make_argv(const char *arg0, const char *arg1)
1035 char **argv;
1036 int argc = (arg1 == NULL ? 1 : 2);
1038 argv = calloc(argc, sizeof(char *));
1039 if (argv == NULL)
1040 err(1, "calloc");
1041 argv[0] = strdup(arg0);
1042 if (argv[0] == NULL)
1043 err(1, "calloc");
1044 if (arg1) {
1045 argv[1] = strdup(arg1);
1046 if (argv[1] == NULL)
1047 err(1, "calloc");
1050 return argv;
1053 int
1054 main(int argc, char *argv[])
1056 const struct got_error *error = NULL;
1057 struct tog_cmd *cmd = NULL;
1058 int ch, hflag = 0;
1059 char **cmd_argv = NULL;
1061 setlocale(LC_ALL, "");
1063 while ((ch = getopt(argc, argv, "h")) != -1) {
1064 switch (ch) {
1065 case 'h':
1066 hflag = 1;
1067 break;
1068 default:
1069 usage();
1070 /* NOTREACHED */
1074 argc -= optind;
1075 argv += optind;
1076 optind = 0;
1077 optreset = 1;
1079 if (argc == 0) {
1080 /* Build an argument vector which runs a default command. */
1081 cmd = &tog_commands[0];
1082 cmd_argv = make_argv(cmd->name, NULL);
1083 argc = 1;
1084 } else {
1085 int i;
1087 /* Did the user specific a command? */
1088 for (i = 0; i < nitems(tog_commands); i++) {
1089 if (strncmp(tog_commands[i].name, argv[0],
1090 strlen(argv[0])) == 0) {
1091 cmd = &tog_commands[i];
1092 if (hflag)
1093 tog_commands[i].cmd_usage();
1094 break;
1097 if (cmd == NULL) {
1098 /* Did the user specify a repository? */
1099 char *repo_path = realpath(argv[0], NULL);
1100 if (repo_path) {
1101 struct got_repository *repo;
1102 error = got_repo_open(&repo, repo_path);
1103 if (error == NULL)
1104 got_repo_close(repo);
1105 } else
1106 error = got_error_from_errno();
1107 if (error) {
1108 fprintf(stderr, "%s: '%s' is neither a known "
1109 "command nor a path to a repository\n",
1110 getprogname(), argv[0]);
1111 free(repo_path);
1112 return 1;
1114 cmd = &tog_commands[0];
1115 cmd_argv = make_argv(cmd->name, repo_path);
1116 argc = 2;
1117 free(repo_path);
1121 init_curses();
1123 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
1124 if (error)
1125 goto done;
1126 done:
1127 endwin();
1128 free(cmd_argv);
1129 if (error)
1130 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
1131 return 0;