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;
535 } else
536 break;
538 } while (++nscrolled < maxscroll);
540 return NULL;
543 static int
544 num_parents(struct commit_queue_entry *entry)
546 int nparents = 0;
548 while (entry) {
549 entry = TAILQ_NEXT(entry, entry);
550 nparents++;
553 return nparents;
556 static const struct got_error *
557 show_commit(struct commit_queue_entry *entry, struct got_repository *repo)
559 const struct got_error *err;
560 struct got_object *obj1 = NULL, *obj2 = NULL;
562 err = got_object_open(&obj2, repo, entry->id);
563 if (err)
564 return err;
566 entry = TAILQ_NEXT(entry, entry);
567 if (entry) {
568 err = got_object_open(&obj1, repo, entry->id);
569 if (err)
570 goto done;
573 err = show_diff_view(obj1, obj2, repo);
574 done:
575 if (obj1)
576 got_object_close(obj1);
577 if (obj2)
578 got_object_close(obj2);
579 return err;
582 static const struct got_error *
583 show_log_view(struct got_object_id *start_id, struct got_repository *repo)
585 const struct got_error *err = NULL;
586 struct got_object_id *id;
587 int ch, done = 0, selected = 0, nparents;
588 struct commit_queue commits;
589 struct commit_queue_entry *first_displayed_entry = NULL;
590 struct commit_queue_entry *last_displayed_entry = NULL;
591 struct commit_queue_entry *selected_entry = NULL;
593 id = got_object_id_dup(start_id);
594 if (id == NULL)
595 return got_error_from_errno();
597 if (tog_log_view.window == NULL) {
598 tog_log_view.window = newwin(0, 0, 0, 0);
599 if (tog_log_view.window == NULL)
600 return got_error_from_errno();
601 keypad(tog_log_view.window, TRUE);
603 if (tog_log_view.panel == NULL) {
604 tog_log_view.panel = new_panel(tog_log_view.window);
605 if (tog_log_view.panel == NULL)
606 return got_error_from_errno();
607 } else
608 show_panel(tog_log_view.panel);
610 TAILQ_INIT(&commits);
611 err = fetch_commits(&first_displayed_entry, id, &commits, LINES, repo);
612 if (err)
613 goto done;
614 while (!done) {
615 err = draw_commits(&last_displayed_entry, &selected_entry,
616 first_displayed_entry, selected, LINES);
617 if (err)
618 goto done;
620 nodelay(stdscr, FALSE);
621 ch = wgetch(tog_log_view.window);
622 nodelay(stdscr, TRUE);
623 switch (ch) {
624 case ERR:
625 if (errno) {
626 err = got_error_from_errno();
627 goto done;
629 break;
630 case 'q':
631 done = 1;
632 break;
633 case 'k':
634 case KEY_UP:
635 if (selected > 0)
636 selected--;
637 if (selected > 0)
638 break;
639 scroll_up(&first_displayed_entry, 1, &commits);
640 break;
641 case KEY_PPAGE:
642 if (TAILQ_FIRST(&commits) ==
643 first_displayed_entry) {
644 selected = 0;
645 break;
647 scroll_up(&first_displayed_entry, LINES,
648 &commits);
649 break;
650 case 'j':
651 case KEY_DOWN:
652 nparents = num_parents(first_displayed_entry);
653 if (selected < LINES - 1 &&
654 selected < nparents - 1)
655 selected++;
656 if (selected < LINES - 1 &&
657 selected < nparents - 1)
658 break;
659 err = scroll_down(&first_displayed_entry, 1,
660 last_displayed_entry, &commits, repo);
661 if (err)
662 goto done;
663 break;
664 case KEY_NPAGE:
665 nparents = num_parents(first_displayed_entry);
666 if (nparents < LINES - 1 &&
667 selected < nparents - 1) {
668 selected = nparents - 1;
669 break;
671 err = scroll_down(&first_displayed_entry, LINES,
672 last_displayed_entry, &commits, repo);
673 if (err)
674 goto done;
675 nparents = num_parents(first_displayed_entry);
676 if (selected > nparents)
677 selected = nparents - 1;
678 break;
679 case KEY_RESIZE:
680 if (selected > LINES)
681 selected = LINES - 1;
682 break;
683 case KEY_ENTER:
684 case '\r':
685 err = show_commit(selected_entry, repo);
686 if (err)
687 break;
688 show_panel(tog_log_view.panel);
689 break;
690 default:
691 break;
694 done:
695 free_commits(&commits);
696 return err;
699 static const struct got_error *
700 cmd_log(int argc, char *argv[])
702 const struct got_error *error;
703 struct got_repository *repo;
704 struct got_object_id *start_id = NULL;
705 char *repo_path = NULL;
706 char *start_commit = NULL;
707 int ch;
709 #ifndef PROFILE
710 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
711 err(1, "pledge");
712 #endif
714 while ((ch = getopt(argc, argv, "c:")) != -1) {
715 switch (ch) {
716 case 'c':
717 start_commit = optarg;
718 break;
719 default:
720 usage();
721 /* NOTREACHED */
725 argc -= optind;
726 argv += optind;
728 if (argc == 0) {
729 repo_path = getcwd(NULL, 0);
730 if (repo_path == NULL)
731 return got_error_from_errno();
732 } else if (argc == 1) {
733 repo_path = realpath(argv[0], NULL);
734 if (repo_path == NULL)
735 return got_error_from_errno();
736 } else
737 usage_log();
739 error = got_repo_open(&repo, repo_path);
740 free(repo_path);
741 if (error != NULL)
742 return error;
744 if (start_commit == NULL) {
745 error = get_head_commit_id(&start_id, repo);
746 if (error != NULL)
747 return error;
748 } else {
749 struct got_object *obj;
750 error = got_object_open_by_id_str(&obj, repo, start_commit);
751 if (error == NULL) {
752 start_id = got_object_get_id(obj);
753 if (start_id == NULL)
754 error = got_error_from_errno();
757 if (error != NULL)
758 return error;
759 error = show_log_view(start_id, repo);
760 free(start_id);
761 got_repo_close(repo);
762 return error;
765 __dead static void
766 usage_diff(void)
768 endwin();
769 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
770 getprogname());
771 exit(1);
774 static char *
775 parse_next_line(FILE *f, size_t *len)
777 char *line;
778 size_t linelen;
779 size_t lineno;
780 const char delim[3] = { '\0', '\0', '\0'};
782 line = fparseln(f, &linelen, &lineno, delim, 0);
783 if (len)
784 *len = linelen;
785 return line;
788 static const struct got_error *
789 draw_diff(FILE *f, int *first_displayed_line, int *last_displayed_line,
790 int *eof, int max_lines)
792 int nlines = 0, nprinted = 0;
793 char *line;
794 size_t len;
796 rewind(f);
797 wclear(tog_diff_view.window);
799 *eof = 0;
800 while (nprinted < max_lines) {
801 line = parse_next_line(f, &len);
802 if (line == NULL) {
803 *eof = 1;
804 break;
806 if (++nlines < *first_displayed_line) {
807 free(line);
808 continue;
811 if (len > COLS - 1)
812 line[COLS - 1] = '\0';
813 waddstr(tog_diff_view.window, line);
814 waddch(tog_diff_view.window, '\n');
815 if (++nprinted == 1)
816 *first_displayed_line = nlines;
817 free(line);
819 *last_displayed_line = nlines;
821 update_panels();
822 doupdate();
824 return NULL;
827 static const struct got_error *
828 show_diff_view(struct got_object *obj1, struct got_object *obj2,
829 struct got_repository *repo)
831 const struct got_error *err;
832 FILE *f;
833 int ch, done = 0, first_displayed_line = 1, last_displayed_line = LINES;
834 int eof, i;
836 if (obj1 != NULL && obj2 != NULL &&
837 got_object_get_type(obj1) != got_object_get_type(obj2))
838 return got_error(GOT_ERR_OBJ_TYPE);
840 f = got_opentemp();
841 if (f == NULL)
842 return got_error_from_errno();
844 switch (got_object_get_type(obj1 ? obj1 : obj2)) {
845 case GOT_OBJ_TYPE_BLOB:
846 err = got_diff_objects_as_blobs(obj1, obj2, repo, f);
847 break;
848 case GOT_OBJ_TYPE_TREE:
849 err = got_diff_objects_as_trees(obj1, obj2, repo, f);
850 break;
851 case GOT_OBJ_TYPE_COMMIT:
852 err = got_diff_objects_as_commits(obj1, obj2, repo, f);
853 break;
854 default:
855 return got_error(GOT_ERR_OBJ_TYPE);
858 fflush(f);
860 if (tog_diff_view.window == NULL) {
861 tog_diff_view.window = newwin(0, 0, 0, 0);
862 if (tog_diff_view.window == NULL)
863 return got_error_from_errno();
864 keypad(tog_diff_view.window, TRUE);
866 if (tog_diff_view.panel == NULL) {
867 tog_diff_view.panel = new_panel(tog_diff_view.window);
868 if (tog_diff_view.panel == NULL)
869 return got_error_from_errno();
870 } else
871 show_panel(tog_diff_view.panel);
873 while (!done) {
874 err = draw_diff(f, &first_displayed_line, &last_displayed_line,
875 &eof, LINES);
876 if (err)
877 break;
878 nodelay(stdscr, FALSE);
879 ch = wgetch(tog_diff_view.window);
880 nodelay(stdscr, TRUE);
881 switch (ch) {
882 case 'q':
883 done = 1;
884 break;
885 case 'k':
886 case KEY_UP:
887 case KEY_BACKSPACE:
888 if (first_displayed_line > 1)
889 first_displayed_line--;
890 break;
891 case KEY_PPAGE:
892 i = 0;
893 while (i++ < LINES - 1 && first_displayed_line > 1)
894 first_displayed_line--;
895 break;
896 case 'j':
897 case KEY_DOWN:
898 case KEY_ENTER:
899 case '\r':
900 if (!eof)
901 first_displayed_line++;
902 break;
903 case KEY_NPAGE:
904 case ' ':
905 i = 0;
906 while (!eof && i++ < LINES - 1) {
907 char *line = parse_next_line(f, NULL);
908 first_displayed_line++;
909 if (line == NULL)
910 break;
912 break;
913 default:
914 break;
917 fclose(f);
918 return err;
921 static const struct got_error *
922 cmd_diff(int argc, char *argv[])
924 const struct got_error *error = NULL;
925 struct got_repository *repo = NULL;
926 struct got_object *obj1 = NULL, *obj2 = NULL;
927 char *repo_path = NULL;
928 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
929 int ch;
931 #ifndef PROFILE
932 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
933 err(1, "pledge");
934 #endif
936 while ((ch = getopt(argc, argv, "")) != -1) {
937 switch (ch) {
938 default:
939 usage();
940 /* NOTREACHED */
944 argc -= optind;
945 argv += optind;
947 if (argc == 0) {
948 usage_diff(); /* TODO show local worktree changes */
949 } else if (argc == 2) {
950 repo_path = getcwd(NULL, 0);
951 if (repo_path == NULL)
952 return got_error_from_errno();
953 obj_id_str1 = argv[0];
954 obj_id_str2 = argv[1];
955 } else if (argc == 3) {
956 repo_path = realpath(argv[0], NULL);
957 if (repo_path == NULL)
958 return got_error_from_errno();
959 obj_id_str1 = argv[1];
960 obj_id_str2 = argv[2];
961 } else
962 usage_diff();
964 error = got_repo_open(&repo, repo_path);
965 free(repo_path);
966 if (error)
967 goto done;
969 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
970 if (error)
971 goto done;
973 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
974 if (error)
975 goto done;
977 error = show_diff_view(obj1, obj2, repo);
978 done:
979 got_repo_close(repo);
980 if (obj1)
981 got_object_close(obj1);
982 if (obj2)
983 got_object_close(obj2);
984 return error;
987 __dead static void
988 usage_blame(void)
990 endwin();
991 fprintf(stderr, "usage: %s blame [repository-path] blob-object\n",
992 getprogname());
993 exit(1);
996 static const struct got_error *
997 cmd_blame(int argc, char *argv[])
999 return got_error(GOT_ERR_NOT_IMPL);
1002 static void
1003 init_curses(void)
1005 initscr();
1006 cbreak();
1007 noecho();
1008 nonl();
1009 intrflush(stdscr, FALSE);
1010 keypad(stdscr, TRUE);
1011 curs_set(0);
1014 __dead static void
1015 usage(void)
1017 int i;
1019 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
1020 "Available commands:\n", getprogname());
1021 for (i = 0; i < nitems(tog_commands); i++) {
1022 struct tog_cmd *cmd = &tog_commands[i];
1023 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
1025 exit(1);
1028 static char **
1029 make_argv(const char *arg0, const char *arg1)
1031 char **argv;
1032 int argc = (arg1 == NULL ? 1 : 2);
1034 argv = calloc(argc, sizeof(char *));
1035 if (argv == NULL)
1036 err(1, "calloc");
1037 argv[0] = strdup(arg0);
1038 if (argv[0] == NULL)
1039 err(1, "calloc");
1040 if (arg1) {
1041 argv[1] = strdup(arg1);
1042 if (argv[1] == NULL)
1043 err(1, "calloc");
1046 return argv;
1049 int
1050 main(int argc, char *argv[])
1052 const struct got_error *error = NULL;
1053 struct tog_cmd *cmd = NULL;
1054 int ch, hflag = 0;
1055 char **cmd_argv = NULL;
1057 setlocale(LC_ALL, "");
1059 while ((ch = getopt(argc, argv, "h")) != -1) {
1060 switch (ch) {
1061 case 'h':
1062 hflag = 1;
1063 break;
1064 default:
1065 usage();
1066 /* NOTREACHED */
1070 argc -= optind;
1071 argv += optind;
1072 optind = 0;
1073 optreset = 1;
1075 if (argc == 0) {
1076 /* Build an argument vector which runs a default command. */
1077 cmd = &tog_commands[0];
1078 cmd_argv = make_argv(cmd->name, NULL);
1079 argc = 1;
1080 } else {
1081 int i;
1083 /* Did the user specific a command? */
1084 for (i = 0; i < nitems(tog_commands); i++) {
1085 if (strncmp(tog_commands[i].name, argv[0],
1086 strlen(argv[0])) == 0) {
1087 cmd = &tog_commands[i];
1088 if (hflag)
1089 tog_commands[i].cmd_usage();
1090 break;
1093 if (cmd == NULL) {
1094 /* Did the user specify a repository? */
1095 char *repo_path = realpath(argv[0], NULL);
1096 if (repo_path) {
1097 struct got_repository *repo;
1098 error = got_repo_open(&repo, repo_path);
1099 if (error == NULL)
1100 got_repo_close(repo);
1101 } else
1102 error = got_error_from_errno();
1103 if (error) {
1104 fprintf(stderr, "%s: '%s' is neither a known "
1105 "command nor a path to a repository\n",
1106 getprogname(), argv[0]);
1107 free(repo_path);
1108 return 1;
1110 cmd = &tog_commands[0];
1111 cmd_argv = make_argv(cmd->name, repo_path);
1112 argc = 2;
1113 free(repo_path);
1117 init_curses();
1119 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
1120 if (error)
1121 goto done;
1122 done:
1123 endwin();
1124 free(cmd_argv);
1125 if (error)
1126 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
1127 return 0;