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 got_object *obj1 = NULL, *obj2 = NULL;
561 err = got_object_open(&obj2, repo, entry->id);
562 if (err)
563 return err;
565 entry = TAILQ_NEXT(entry, entry);
566 if (entry) {
567 err = got_object_open(&obj1, repo, entry->id);
568 if (err)
569 goto done;
572 err = show_diff_view(obj1, obj2, repo);
573 done:
574 if (obj1)
575 got_object_close(obj1);
576 if (obj2)
577 got_object_close(obj2);
578 return err;
581 static const struct got_error *
582 show_log_view(struct got_object_id *start_id, struct got_repository *repo)
584 const struct got_error *err = NULL;
585 struct got_object_id *id;
586 int ch, done = 0, selected = 0, nparents;
587 struct commit_queue commits;
588 struct commit_queue_entry *first_displayed_entry = NULL;
589 struct commit_queue_entry *last_displayed_entry = NULL;
590 struct commit_queue_entry *selected_entry = NULL;
592 id = got_object_id_dup(start_id);
593 if (id == NULL)
594 return got_error_from_errno();
596 if (tog_log_view.window == NULL) {
597 tog_log_view.window = newwin(0, 0, 0, 0);
598 if (tog_log_view.window == NULL)
599 return got_error_from_errno();
600 keypad(tog_log_view.window, TRUE);
602 if (tog_log_view.panel == NULL) {
603 tog_log_view.panel = new_panel(tog_log_view.window);
604 if (tog_log_view.panel == NULL)
605 return got_error_from_errno();
606 } else
607 show_panel(tog_log_view.panel);
609 TAILQ_INIT(&commits);
610 err = fetch_commits(&first_displayed_entry, id, &commits, LINES, repo);
611 if (err)
612 goto done;
613 while (!done) {
614 err = draw_commits(&last_displayed_entry, &selected_entry,
615 first_displayed_entry, selected, LINES);
616 if (err)
617 goto done;
619 nodelay(stdscr, FALSE);
620 ch = wgetch(tog_log_view.window);
621 nodelay(stdscr, TRUE);
622 switch (ch) {
623 case ERR:
624 if (errno) {
625 err = got_error_from_errno();
626 goto done;
628 break;
629 case 'q':
630 done = 1;
631 break;
632 case 'k':
633 case KEY_UP:
634 if (selected > 0)
635 selected--;
636 if (selected > 0)
637 break;
638 scroll_up(&first_displayed_entry, 1, &commits);
639 break;
640 case KEY_PPAGE:
641 if (TAILQ_FIRST(&commits) ==
642 first_displayed_entry) {
643 selected = 0;
644 break;
646 scroll_up(&first_displayed_entry, LINES,
647 &commits);
648 break;
649 case 'j':
650 case KEY_DOWN:
651 nparents = num_parents(first_displayed_entry);
652 if (selected < LINES - 1 &&
653 selected < nparents - 1)
654 selected++;
655 if (selected < LINES - 1 &&
656 selected < nparents - 1)
657 break;
658 err = scroll_down(&first_displayed_entry, 1,
659 last_displayed_entry, &commits, repo);
660 if (err)
661 goto done;
662 break;
663 case KEY_NPAGE:
664 nparents = num_parents(first_displayed_entry);
665 if (nparents < LINES - 1 &&
666 selected < nparents - 1) {
667 selected = nparents - 1;
668 break;
670 err = scroll_down(&first_displayed_entry, LINES,
671 last_displayed_entry, &commits, repo);
672 if (err)
673 goto done;
674 nparents = num_parents(first_displayed_entry);
675 if (selected > nparents)
676 selected = nparents - 1;
677 break;
678 case KEY_RESIZE:
679 if (selected > LINES)
680 selected = LINES - 1;
681 break;
682 case KEY_ENTER:
683 case '\r':
684 err = show_commit(selected_entry, repo);
685 if (err)
686 break;
687 show_panel(tog_log_view.panel);
688 break;
689 default:
690 break;
693 done:
694 free_commits(&commits);
695 return err;
698 static const struct got_error *
699 cmd_log(int argc, char *argv[])
701 const struct got_error *error;
702 struct got_repository *repo;
703 struct got_object_id *start_id = NULL;
704 char *repo_path = NULL;
705 char *start_commit = NULL;
706 int ch;
708 #ifndef PROFILE
709 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
710 err(1, "pledge");
711 #endif
713 while ((ch = getopt(argc, argv, "c:")) != -1) {
714 switch (ch) {
715 case 'c':
716 start_commit = optarg;
717 break;
718 default:
719 usage();
720 /* NOTREACHED */
724 argc -= optind;
725 argv += optind;
727 if (argc == 0) {
728 repo_path = getcwd(NULL, 0);
729 if (repo_path == NULL)
730 return got_error_from_errno();
731 } else if (argc == 1) {
732 repo_path = realpath(argv[0], NULL);
733 if (repo_path == NULL)
734 return got_error_from_errno();
735 } else
736 usage_log();
738 error = got_repo_open(&repo, repo_path);
739 free(repo_path);
740 if (error != NULL)
741 return error;
743 if (start_commit == NULL) {
744 error = get_head_commit_id(&start_id, repo);
745 if (error != NULL)
746 return error;
747 } else {
748 struct got_object *obj;
749 error = got_object_open_by_id_str(&obj, repo, start_commit);
750 if (error == NULL) {
751 start_id = got_object_get_id(obj);
752 if (start_id == NULL)
753 error = got_error_from_errno();
756 if (error != NULL)
757 return error;
758 error = show_log_view(start_id, repo);
759 free(start_id);
760 got_repo_close(repo);
761 return error;
764 __dead static void
765 usage_diff(void)
767 endwin();
768 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
769 getprogname());
770 exit(1);
773 static char *
774 parse_next_line(FILE *f, size_t *len)
776 char *line;
777 size_t linelen;
778 size_t lineno;
779 const char delim[3] = { '\0', '\0', '\0'};
781 line = fparseln(f, &linelen, &lineno, delim, 0);
782 if (len)
783 *len = linelen;
784 return line;
787 static const struct got_error *
788 draw_diff(FILE *f, int *first_displayed_line, int *last_displayed_line,
789 int *eof, int max_lines)
791 int nlines = 0, nprinted = 0;
792 char *line;
793 size_t len;
795 rewind(f);
796 wclear(tog_diff_view.window);
798 *eof = 0;
799 while (nprinted < max_lines) {
800 line = parse_next_line(f, &len);
801 if (line == NULL) {
802 *eof = 1;
803 break;
805 if (++nlines < *first_displayed_line) {
806 free(line);
807 continue;
810 if (len > COLS - 1)
811 line[COLS - 1] = '\0';
812 waddstr(tog_diff_view.window, line);
813 waddch(tog_diff_view.window, '\n');
814 if (++nprinted == 1)
815 *first_displayed_line = nlines;
816 free(line);
818 *last_displayed_line = nlines;
820 update_panels();
821 doupdate();
823 return NULL;
826 static const struct got_error *
827 show_diff_view(struct got_object *obj1, struct got_object *obj2,
828 struct got_repository *repo)
830 const struct got_error *err;
831 FILE *f;
832 int ch, done = 0, first_displayed_line = 1, last_displayed_line = LINES;
833 int eof, i;
835 if (obj1 != NULL && obj2 != NULL &&
836 got_object_get_type(obj1) != got_object_get_type(obj2))
837 return got_error(GOT_ERR_OBJ_TYPE);
839 f = got_opentemp();
840 if (f == NULL)
841 return got_error_from_errno();
843 switch (got_object_get_type(obj1 ? obj1 : obj2)) {
844 case GOT_OBJ_TYPE_BLOB:
845 err = got_diff_objects_as_blobs(obj1, obj2, repo, f);
846 break;
847 case GOT_OBJ_TYPE_TREE:
848 err = got_diff_objects_as_trees(obj1, obj2, repo, f);
849 break;
850 case GOT_OBJ_TYPE_COMMIT:
851 err = got_diff_objects_as_commits(obj1, obj2, repo, f);
852 break;
853 default:
854 return got_error(GOT_ERR_OBJ_TYPE);
857 fflush(f);
859 if (tog_diff_view.window == NULL) {
860 tog_diff_view.window = newwin(0, 0, 0, 0);
861 if (tog_diff_view.window == NULL)
862 return got_error_from_errno();
863 keypad(tog_diff_view.window, TRUE);
865 if (tog_diff_view.panel == NULL) {
866 tog_diff_view.panel = new_panel(tog_diff_view.window);
867 if (tog_diff_view.panel == NULL)
868 return got_error_from_errno();
869 } else
870 show_panel(tog_diff_view.panel);
872 while (!done) {
873 err = draw_diff(f, &first_displayed_line, &last_displayed_line,
874 &eof, LINES);
875 if (err)
876 break;
877 nodelay(stdscr, FALSE);
878 ch = wgetch(tog_diff_view.window);
879 nodelay(stdscr, TRUE);
880 switch (ch) {
881 case 'q':
882 done = 1;
883 break;
884 case 'k':
885 case KEY_UP:
886 case KEY_BACKSPACE:
887 if (first_displayed_line > 1)
888 first_displayed_line--;
889 break;
890 case KEY_PPAGE:
891 i = 0;
892 while (i++ < LINES - 1 && first_displayed_line > 1)
893 first_displayed_line--;
894 break;
895 case 'j':
896 case KEY_DOWN:
897 case KEY_ENTER:
898 case '\r':
899 if (!eof)
900 first_displayed_line++;
901 break;
902 case KEY_NPAGE:
903 case ' ':
904 i = 0;
905 while (!eof && i++ < LINES - 1) {
906 char *line = parse_next_line(f, NULL);
907 first_displayed_line++;
908 if (line == NULL)
909 break;
911 break;
912 default:
913 break;
916 fclose(f);
917 return err;
920 static const struct got_error *
921 cmd_diff(int argc, char *argv[])
923 const struct got_error *error = NULL;
924 struct got_repository *repo = NULL;
925 struct got_object *obj1 = NULL, *obj2 = NULL;
926 char *repo_path = NULL;
927 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
928 int ch;
930 #ifndef PROFILE
931 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
932 err(1, "pledge");
933 #endif
935 while ((ch = getopt(argc, argv, "")) != -1) {
936 switch (ch) {
937 default:
938 usage();
939 /* NOTREACHED */
943 argc -= optind;
944 argv += optind;
946 if (argc == 0) {
947 usage_diff(); /* TODO show local worktree changes */
948 } else if (argc == 2) {
949 repo_path = getcwd(NULL, 0);
950 if (repo_path == NULL)
951 return got_error_from_errno();
952 obj_id_str1 = argv[0];
953 obj_id_str2 = argv[1];
954 } else if (argc == 3) {
955 repo_path = realpath(argv[0], NULL);
956 if (repo_path == NULL)
957 return got_error_from_errno();
958 obj_id_str1 = argv[1];
959 obj_id_str2 = argv[2];
960 } else
961 usage_diff();
963 error = got_repo_open(&repo, repo_path);
964 free(repo_path);
965 if (error)
966 goto done;
968 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
969 if (error)
970 goto done;
972 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
973 if (error)
974 goto done;
976 error = show_diff_view(obj1, obj2, repo);
977 done:
978 got_repo_close(repo);
979 if (obj1)
980 got_object_close(obj1);
981 if (obj2)
982 got_object_close(obj2);
983 return error;
986 __dead static void
987 usage_blame(void)
989 endwin();
990 fprintf(stderr, "usage: %s blame [repository-path] blob-object\n",
991 getprogname());
992 exit(1);
995 static const struct got_error *
996 cmd_blame(int argc, char *argv[])
998 return got_error(GOT_ERR_NOT_IMPL);
1001 static void
1002 init_curses(void)
1004 initscr();
1005 cbreak();
1006 noecho();
1007 nonl();
1008 intrflush(stdscr, FALSE);
1009 keypad(stdscr, TRUE);
1010 curs_set(0);
1013 __dead static void
1014 usage(void)
1016 int i;
1018 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
1019 "Available commands:\n", getprogname());
1020 for (i = 0; i < nitems(tog_commands); i++) {
1021 struct tog_cmd *cmd = &tog_commands[i];
1022 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
1024 exit(1);
1027 static char **
1028 make_argv(const char *arg0, const char *arg1)
1030 char **argv;
1031 int argc = (arg1 == NULL ? 1 : 2);
1033 argv = calloc(argc, sizeof(char *));
1034 if (argv == NULL)
1035 err(1, "calloc");
1036 argv[0] = strdup(arg0);
1037 if (argv[0] == NULL)
1038 err(1, "calloc");
1039 if (arg1) {
1040 argv[1] = strdup(arg1);
1041 if (argv[1] == NULL)
1042 err(1, "calloc");
1045 return argv;
1048 int
1049 main(int argc, char *argv[])
1051 const struct got_error *error = NULL;
1052 struct tog_cmd *cmd = NULL;
1053 int ch, hflag = 0;
1054 char **cmd_argv = NULL;
1056 setlocale(LC_ALL, "");
1058 while ((ch = getopt(argc, argv, "h")) != -1) {
1059 switch (ch) {
1060 case 'h':
1061 hflag = 1;
1062 break;
1063 default:
1064 usage();
1065 /* NOTREACHED */
1069 argc -= optind;
1070 argv += optind;
1071 optind = 0;
1072 optreset = 1;
1074 if (argc == 0) {
1075 /* Build an argument vector which runs a default command. */
1076 cmd = &tog_commands[0];
1077 cmd_argv = make_argv(cmd->name, NULL);
1078 argc = 1;
1079 } else {
1080 int i;
1082 /* Did the user specific a command? */
1083 for (i = 0; i < nitems(tog_commands); i++) {
1084 if (strncmp(tog_commands[i].name, argv[0],
1085 strlen(argv[0])) == 0) {
1086 cmd = &tog_commands[i];
1087 if (hflag)
1088 tog_commands[i].cmd_usage();
1089 break;
1092 if (cmd == NULL) {
1093 /* Did the user specify a repository? */
1094 char *repo_path = realpath(argv[0], NULL);
1095 if (repo_path) {
1096 struct got_repository *repo;
1097 error = got_repo_open(&repo, repo_path);
1098 if (error == NULL)
1099 got_repo_close(repo);
1100 } else
1101 error = got_error_from_errno();
1102 if (error) {
1103 fprintf(stderr, "%s: '%s' is neither a known "
1104 "command nor a path to a repository\n",
1105 getprogname(), argv[0]);
1106 free(repo_path);
1107 return 1;
1109 cmd = &tog_commands[0];
1110 cmd_argv = make_argv(cmd->name, repo_path);
1111 argc = 2;
1112 free(repo_path);
1116 init_curses();
1118 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
1119 if (error)
1120 goto done;
1121 done:
1122 endwin();
1123 free(cmd_argv);
1124 if (error)
1125 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
1126 return 0;