Blob


1 /*
2 * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
18 #include <sys/queue.h>
19 #include <sys/limits.h>
20 #include <sys/types.h>
21 #include <sys/stat.h>
23 #include <err.h>
24 #include <errno.h>
25 #include <locale.h>
26 #include <signal.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
31 #include <libgen.h>
32 #include <time.h>
34 #include "got_error.h"
35 #include "got_object.h"
36 #include "got_reference.h"
37 #include "got_repository.h"
38 #include "got_worktree.h"
39 #include "got_diff.h"
40 #include "got_commit_graph.h"
41 #include "got_blame.h"
42 #include "got_privsep.h"
44 #ifndef nitems
45 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
46 #endif
48 static volatile sig_atomic_t sigint_received;
49 static volatile sig_atomic_t sigpipe_received;
51 static void
52 catch_sigint(int signo)
53 {
54 sigint_received = 1;
55 }
57 static void
58 catch_sigpipe(int signo)
59 {
60 sigpipe_received = 1;
61 }
64 struct cmd {
65 const char *cmd_name;
66 const struct got_error *(*cmd_main)(int, char *[]);
67 void (*cmd_usage)(void);
68 const char *cmd_descr;
69 };
71 __dead static void usage(void);
72 __dead static void usage_checkout(void);
73 __dead static void usage_update(void);
74 __dead static void usage_log(void);
75 __dead static void usage_diff(void);
76 __dead static void usage_blame(void);
77 __dead static void usage_tree(void);
78 __dead static void usage_status(void);
80 static const struct got_error* cmd_checkout(int, char *[]);
81 static const struct got_error* cmd_update(int, char *[]);
82 static const struct got_error* cmd_log(int, char *[]);
83 static const struct got_error* cmd_diff(int, char *[]);
84 static const struct got_error* cmd_blame(int, char *[]);
85 static const struct got_error* cmd_tree(int, char *[]);
86 static const struct got_error* cmd_status(int, char *[]);
88 static struct cmd got_commands[] = {
89 { "checkout", cmd_checkout, usage_checkout,
90 "check out a new work tree from a repository" },
91 { "update", cmd_update, usage_update,
92 "update a work tree to a different commit" },
93 { "log", cmd_log, usage_log,
94 "show repository history" },
95 { "diff", cmd_diff, usage_diff,
96 "compare files and directories" },
97 { "blame", cmd_blame, usage_blame,
98 " show when lines in a file were changed" },
99 { "tree", cmd_tree, usage_tree,
100 " list files and directories in repository" },
101 { "status", cmd_status, usage_status,
102 "show modification status of files" },
103 };
105 int
106 main(int argc, char *argv[])
108 struct cmd *cmd;
109 unsigned int i;
110 int ch;
111 int hflag = 0;
113 setlocale(LC_CTYPE, "");
115 while ((ch = getopt(argc, argv, "h")) != -1) {
116 switch (ch) {
117 case 'h':
118 hflag = 1;
119 break;
120 default:
121 usage();
122 /* NOTREACHED */
126 argc -= optind;
127 argv += optind;
128 optind = 0;
130 if (argc <= 0)
131 usage();
133 signal(SIGINT, catch_sigint);
134 signal(SIGPIPE, catch_sigpipe);
136 for (i = 0; i < nitems(got_commands); i++) {
137 const struct got_error *error;
139 cmd = &got_commands[i];
141 if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
142 continue;
144 if (hflag)
145 got_commands[i].cmd_usage();
147 error = got_commands[i].cmd_main(argc, argv);
148 if (error && !(sigint_received || sigpipe_received)) {
149 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
150 return 1;
153 return 0;
156 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
157 return 1;
160 __dead static void
161 usage(void)
163 int i;
165 fprintf(stderr, "usage: %s [-h] command [arg ...]\n\n"
166 "Available commands:\n", getprogname());
167 for (i = 0; i < nitems(got_commands); i++) {
168 struct cmd *cmd = &got_commands[i];
169 fprintf(stderr, " %s: %s\n", cmd->cmd_name, cmd->cmd_descr);
171 exit(1);
174 static const struct got_error *
175 apply_unveil(const char *repo_path, const char *worktree_path)
177 const struct got_error *error;
179 if (repo_path && unveil(repo_path, "r") != 0)
180 return got_error_from_errno();
182 if (worktree_path && unveil(worktree_path, "rwc") != 0)
183 return got_error_from_errno();
185 if (unveil("/tmp", "rwc") != 0)
186 return got_error_from_errno();
188 error = got_privsep_unveil_exec_helpers();
189 if (error != NULL)
190 return error;
192 if (unveil(NULL, NULL) != 0)
193 return got_error_from_errno();
195 return NULL;
198 __dead static void
199 usage_checkout(void)
201 fprintf(stderr, "usage: %s checkout [-p prefix] repository-path "
202 "[worktree-path]\n", getprogname());
203 exit(1);
206 static void
207 checkout_progress(void *arg, unsigned char status, const char *path)
209 char *worktree_path = arg;
211 while (path[0] == '/')
212 path++;
214 printf("%c %s/%s\n", status, worktree_path, path);
217 static const struct got_error *
218 check_cancelled(void *arg)
220 if (sigint_received || sigpipe_received)
221 return got_error(GOT_ERR_CANCELLED);
222 return NULL;
225 static const struct got_error *
226 check_ancestry(struct got_worktree *worktree, struct got_object_id *commit_id,
227 struct got_repository *repo)
229 const struct got_error *err;
230 struct got_reference *head_ref = NULL;
231 struct got_object_id *head_commit_id = NULL;
232 struct got_commit_graph *graph = NULL;
234 head_ref = got_worktree_get_head_ref(worktree);
235 if (head_ref == NULL)
236 return got_error_from_errno();
238 /* TODO: Check the reflog. The head ref may have been rebased. */
239 err = got_ref_resolve(&head_commit_id, repo, head_ref);
240 if (err)
241 goto done;
243 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
244 if (err)
245 goto done;
247 err = got_commit_graph_iter_start(graph, head_commit_id, repo);
248 if (err)
249 goto done;
250 while (1) {
251 struct got_object_id *id;
253 if (sigint_received || sigpipe_received)
254 break;
256 err = got_commit_graph_iter_next(&id, graph);
257 if (err) {
258 if (err->code == GOT_ERR_ITER_COMPLETED) {
259 err = got_error(GOT_ERR_ANCESTRY);
260 break;
262 if (err->code != GOT_ERR_ITER_NEED_MORE)
263 break;
264 err = got_commit_graph_fetch_commits(graph, 1, repo);
265 if (err)
266 break;
267 else
268 continue;
270 if (id == NULL)
271 break;
272 if (got_object_id_cmp(id, commit_id) == 0)
273 break;
275 done:
276 if (head_ref)
277 got_ref_close(head_ref);
278 if (graph)
279 got_commit_graph_close(graph);
280 return err;
284 static const struct got_error *
285 cmd_checkout(int argc, char *argv[])
287 const struct got_error *error = NULL;
288 struct got_repository *repo = NULL;
289 struct got_reference *head_ref = NULL;
290 struct got_worktree *worktree = NULL;
291 char *repo_path = NULL;
292 char *worktree_path = NULL;
293 const char *path_prefix = "";
294 char *commit_id_str = NULL;
295 int ch, same_path_prefix;
297 while ((ch = getopt(argc, argv, "c:p:")) != -1) {
298 switch (ch) {
299 case 'c':
300 commit_id_str = strdup(optarg);
301 if (commit_id_str == NULL)
302 return got_error_from_errno();
303 break;
304 case 'p':
305 path_prefix = optarg;
306 break;
307 default:
308 usage();
309 /* NOTREACHED */
313 argc -= optind;
314 argv += optind;
316 #ifndef PROFILE
317 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
318 NULL) == -1)
319 err(1, "pledge");
320 #endif
321 if (argc == 1) {
322 char *cwd, *base, *dotgit;
323 repo_path = realpath(argv[0], NULL);
324 if (repo_path == NULL)
325 return got_error_from_errno();
326 cwd = getcwd(NULL, 0);
327 if (cwd == NULL) {
328 error = got_error_from_errno();
329 goto done;
331 if (path_prefix[0])
332 base = basename(path_prefix);
333 else
334 base = basename(repo_path);
335 if (base == NULL) {
336 error = got_error_from_errno();
337 goto done;
339 dotgit = strstr(base, ".git");
340 if (dotgit)
341 *dotgit = '\0';
342 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
343 error = got_error_from_errno();
344 free(cwd);
345 goto done;
347 free(cwd);
348 } else if (argc == 2) {
349 repo_path = realpath(argv[0], NULL);
350 if (repo_path == NULL) {
351 error = got_error_from_errno();
352 goto done;
354 worktree_path = realpath(argv[1], NULL);
355 if (worktree_path == NULL) {
356 error = got_error_from_errno();
357 goto done;
359 } else
360 usage_checkout();
362 error = apply_unveil(repo_path, worktree_path);
363 if (error)
364 goto done;
366 error = got_repo_open(&repo, repo_path);
367 if (error != NULL)
368 goto done;
370 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
371 if (error != NULL)
372 goto done;
374 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
375 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
376 goto done;
378 error = got_worktree_open(&worktree, worktree_path);
379 if (error != NULL)
380 goto done;
382 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
383 path_prefix);
384 if (error != NULL)
385 goto done;
386 if (!same_path_prefix) {
387 error = got_error(GOT_ERR_PATH_PREFIX);
388 goto done;
391 if (commit_id_str) {
392 struct got_object_id *commit_id;
393 error = got_object_resolve_id_str(&commit_id, repo,
394 commit_id_str);
395 if (error != NULL)
396 goto done;
397 error = check_ancestry(worktree, commit_id, repo);
398 if (error != NULL) {
399 free(commit_id);
400 goto done;
402 error = got_worktree_set_base_commit_id(worktree, repo,
403 commit_id);
404 free(commit_id);
405 if (error)
406 goto done;
409 error = got_worktree_checkout_files(worktree, repo,
410 checkout_progress, worktree_path, check_cancelled, NULL);
411 if (error != NULL)
412 goto done;
414 printf("Now shut up and hack\n");
416 done:
417 free(commit_id_str);
418 free(repo_path);
419 free(worktree_path);
420 return error;
423 __dead static void
424 usage_update(void)
426 fprintf(stderr, "usage: %s update [-c commit] [worktree-path]\n",
427 getprogname());
428 exit(1);
431 static void
432 update_progress(void *arg, unsigned char status, const char *path)
434 int *did_something = arg;
436 if (status == GOT_STATUS_EXISTS)
437 return;
439 *did_something = 1;
440 while (path[0] == '/')
441 path++;
442 printf("%c %s\n", status, path);
445 static const struct got_error *
446 cmd_update(int argc, char *argv[])
448 const struct got_error *error = NULL;
449 struct got_repository *repo = NULL;
450 struct got_worktree *worktree = NULL;
451 char *worktree_path = NULL;
452 struct got_object_id *commit_id = NULL;
453 char *commit_id_str = NULL;
454 int ch, did_something = 0;
456 while ((ch = getopt(argc, argv, "c:")) != -1) {
457 switch (ch) {
458 case 'c':
459 commit_id_str = strdup(optarg);
460 if (commit_id_str == NULL)
461 return got_error_from_errno();
462 break;
463 default:
464 usage();
465 /* NOTREACHED */
469 argc -= optind;
470 argv += optind;
472 #ifndef PROFILE
473 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
474 NULL) == -1)
475 err(1, "pledge");
476 #endif
477 if (argc == 0) {
478 worktree_path = getcwd(NULL, 0);
479 if (worktree_path == NULL) {
480 error = got_error_from_errno();
481 goto done;
483 } else if (argc == 1) {
484 worktree_path = realpath(argv[0], NULL);
485 if (worktree_path == NULL) {
486 error = got_error_from_errno();
487 goto done;
489 } else
490 usage_update();
492 error = got_worktree_open(&worktree, worktree_path);
493 if (error != NULL)
494 goto done;
496 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
497 if (error != NULL)
498 goto done;
500 error = apply_unveil(got_repo_get_path(repo),
501 got_worktree_get_root_path(worktree));
502 if (error)
503 goto done;
505 if (commit_id_str == NULL) {
506 struct got_reference *head_ref;
507 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
508 if (error != NULL)
509 goto done;
510 error = got_ref_resolve(&commit_id, repo, head_ref);
511 if (error != NULL)
512 goto done;
513 error = got_object_id_str(&commit_id_str, commit_id);
514 if (error != NULL)
515 goto done;
516 } else {
517 error = got_object_resolve_id_str(&commit_id, repo,
518 commit_id_str);
519 if (error != NULL)
520 goto done;
523 error = check_ancestry(worktree, commit_id, repo);
524 if (error != NULL)
525 goto done;
527 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
528 commit_id) != 0) {
529 error = got_worktree_set_base_commit_id(worktree, repo,
530 commit_id);
531 if (error)
532 goto done;
535 error = got_worktree_checkout_files(worktree, repo,
536 update_progress, &did_something, check_cancelled, NULL);
537 if (error != NULL)
538 goto done;
540 if (did_something)
541 printf("Updated to commit %s\n", commit_id_str);
542 else
543 printf("Already up-to-date\n");
544 done:
545 free(worktree_path);
546 free(commit_id);
547 free(commit_id_str);
548 return error;
551 static const struct got_error *
552 print_patch(struct got_commit_object *commit, struct got_object_id *id,
553 int diff_context, struct got_repository *repo)
555 const struct got_error *err = NULL;
556 struct got_tree_object *tree1 = NULL, *tree2;
557 struct got_object_qid *qid;
558 char *id_str1 = NULL, *id_str2;
560 err = got_object_open_as_tree(&tree2, repo,
561 got_object_commit_get_tree_id(commit));
562 if (err)
563 return err;
565 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
566 if (qid != NULL) {
567 struct got_commit_object *pcommit;
569 err = got_object_open_as_commit(&pcommit, repo, qid->id);
570 if (err)
571 return err;
573 err = got_object_open_as_tree(&tree1, repo,
574 got_object_commit_get_tree_id(pcommit));
575 got_object_commit_close(pcommit);
576 if (err)
577 return err;
579 err = got_object_id_str(&id_str1, qid->id);
580 if (err)
581 return err;
584 err = got_object_id_str(&id_str2, id);
585 if (err)
586 goto done;
588 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
589 err = got_diff_tree(tree1, tree2, "", "", diff_context, repo, stdout);
590 done:
591 if (tree1)
592 got_object_tree_close(tree1);
593 got_object_tree_close(tree2);
594 free(id_str1);
595 free(id_str2);
596 return err;
599 static char *
600 get_datestr(time_t *time, char *datebuf)
602 char *p, *s = ctime_r(time, datebuf);
603 p = strchr(s, '\n');
604 if (p)
605 *p = '\0';
606 return s;
609 static const struct got_error *
610 print_commit(struct got_commit_object *commit, struct got_object_id *id,
611 struct got_repository *repo, int show_patch, int diff_context,
612 struct got_reflist_head *refs)
614 const struct got_error *err = NULL;
615 char *id_str, *datestr, *logmsg0, *logmsg, *line;
616 char datebuf[26];
617 time_t committer_time;
618 const char *author, *committer;
619 char *refs_str = NULL;
620 struct got_reflist_entry *re;
622 SIMPLEQ_FOREACH(re, refs, entry) {
623 char *s;
624 const char *name;
625 if (got_object_id_cmp(re->id, id) != 0)
626 continue;
627 name = got_ref_get_name(re->ref);
628 if (strncmp(name, "refs/", 5) == 0)
629 name += 5;
630 if (strncmp(name, "heads/", 6) == 0)
631 name += 6;
632 if (strncmp(name, "remotes/", 8) == 0)
633 name += 8;
634 s = refs_str;
635 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
636 name) == -1) {
637 err = got_error_from_errno();
638 free(s);
639 break;
641 free(s);
643 err = got_object_id_str(&id_str, id);
644 if (err)
645 return err;
647 printf("-----------------------------------------------\n");
648 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
649 refs_str ? refs_str : "", refs_str ? ")" : "");
650 free(id_str);
651 printf("from: %s\n", got_object_commit_get_author(commit));
652 committer_time = got_object_commit_get_committer_time(commit);
653 datestr = get_datestr(&committer_time, datebuf);
654 printf("date: %s UTC\n", datestr);
655 author = got_object_commit_get_author(commit);
656 committer = got_object_commit_get_committer(commit);
657 if (strcmp(author, committer) != 0)
658 printf("via: %s\n", committer);
659 if (got_object_commit_get_nparents(commit) > 1) {
660 const struct got_object_id_queue *parent_ids;
661 struct got_object_qid *qid;
662 int n = 1;
663 parent_ids = got_object_commit_get_parent_ids(commit);
664 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
665 err = got_object_id_str(&id_str, qid->id);
666 if (err)
667 return err;
668 printf("parent %d: %s\n", n++, id_str);
669 free(id_str);
673 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
674 if (logmsg0 == NULL)
675 return got_error_from_errno();
677 logmsg = logmsg0;
678 do {
679 line = strsep(&logmsg, "\n");
680 if (line)
681 printf(" %s\n", line);
682 } while (line);
683 free(logmsg0);
685 if (show_patch) {
686 err = print_patch(commit, id, diff_context, repo);
687 if (err == 0)
688 printf("\n");
691 fflush(stdout);
692 return err;
695 static const struct got_error *
696 print_commits(struct got_object_id *root_id, struct got_repository *repo,
697 char *path, int show_patch, int diff_context, int limit,
698 int first_parent_traversal, struct got_reflist_head *refs)
700 const struct got_error *err;
701 struct got_commit_graph *graph;
703 err = got_commit_graph_open(&graph, root_id, path,
704 first_parent_traversal, repo);
705 if (err)
706 return err;
707 err = got_commit_graph_iter_start(graph, root_id, repo);
708 if (err)
709 goto done;
710 while (1) {
711 struct got_commit_object *commit;
712 struct got_object_id *id;
714 if (sigint_received || sigpipe_received)
715 break;
717 err = got_commit_graph_iter_next(&id, graph);
718 if (err) {
719 if (err->code == GOT_ERR_ITER_COMPLETED) {
720 err = NULL;
721 break;
723 if (err->code != GOT_ERR_ITER_NEED_MORE)
724 break;
725 err = got_commit_graph_fetch_commits(graph, 1, repo);
726 if (err)
727 break;
728 else
729 continue;
731 if (id == NULL)
732 break;
734 err = got_object_open_as_commit(&commit, repo, id);
735 if (err)
736 break;
737 err = print_commit(commit, id, repo, show_patch, diff_context,
738 refs);
739 got_object_commit_close(commit);
740 if (err || (limit && --limit == 0))
741 break;
743 done:
744 got_commit_graph_close(graph);
745 return err;
748 __dead static void
749 usage_log(void)
751 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
752 "[-r repository-path] [path]\n", getprogname());
753 exit(1);
756 static const struct got_error *
757 cmd_log(int argc, char *argv[])
759 const struct got_error *error;
760 struct got_repository *repo = NULL;
761 struct got_worktree *worktree = NULL;
762 struct got_commit_object *commit = NULL;
763 struct got_object_id *id = NULL;
764 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
765 char *start_commit = NULL;
766 int diff_context = 3, ch;
767 int show_patch = 0, limit = 0, first_parent_traversal = 0;
768 const char *errstr;
769 struct got_reflist_head refs;
771 #ifndef PROFILE
772 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
773 NULL)
774 == -1)
775 err(1, "pledge");
776 #endif
778 while ((ch = getopt(argc, argv, "pc:C:l:fr:")) != -1) {
779 switch (ch) {
780 case 'p':
781 show_patch = 1;
782 break;
783 case 'c':
784 start_commit = optarg;
785 break;
786 case 'C':
787 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
788 &errstr);
789 if (errstr != NULL)
790 err(1, "-C option %s", errstr);
791 break;
792 case 'l':
793 limit = strtonum(optarg, 1, INT_MAX, &errstr);
794 if (errstr != NULL)
795 err(1, "-l option %s", errstr);
796 break;
797 case 'f':
798 first_parent_traversal = 1;
799 break;
800 case 'r':
801 repo_path = realpath(optarg, NULL);
802 if (repo_path == NULL)
803 err(1, "-r option");
804 break;
805 default:
806 usage();
807 /* NOTREACHED */
811 argc -= optind;
812 argv += optind;
814 if (argc == 0)
815 path = strdup("");
816 else if (argc == 1)
817 path = strdup(argv[0]);
818 else
819 usage_log();
820 if (path == NULL)
821 return got_error_from_errno();
823 cwd = getcwd(NULL, 0);
824 if (cwd == NULL) {
825 error = got_error_from_errno();
826 goto done;
829 error = got_worktree_open(&worktree, cwd);
830 if (error && error->code != GOT_ERR_NOT_WORKTREE)
831 goto done;
832 error = NULL;
834 repo_path = worktree ?
835 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
836 if (repo_path == NULL) {
837 error = got_error_from_errno();
838 goto done;
841 error = apply_unveil(repo_path, NULL);
842 if (error)
843 goto done;
845 error = got_repo_open(&repo, repo_path);
846 if (error != NULL)
847 goto done;
849 if (start_commit == NULL) {
850 struct got_reference *head_ref;
851 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
852 if (error != NULL)
853 return error;
854 error = got_ref_resolve(&id, repo, head_ref);
855 got_ref_close(head_ref);
856 if (error != NULL)
857 return error;
858 error = got_object_open_as_commit(&commit, repo, id);
859 } else {
860 struct got_reference *ref;
861 error = got_ref_open(&ref, repo, start_commit);
862 if (error == NULL) {
863 int obj_type;
864 error = got_ref_resolve(&id, repo, ref);
865 got_ref_close(ref);
866 if (error != NULL)
867 goto done;
868 error = got_object_get_type(&obj_type, repo, id);
869 if (error != NULL)
870 goto done;
871 if (obj_type == GOT_OBJ_TYPE_TAG) {
872 struct got_tag_object *tag;
873 error = got_object_open_as_tag(&tag, repo, id);
874 if (error != NULL)
875 goto done;
876 if (got_object_tag_get_object_type(tag) !=
877 GOT_OBJ_TYPE_COMMIT) {
878 got_object_tag_close(tag);
879 error = got_error(GOT_ERR_OBJ_TYPE);
880 goto done;
882 free(id);
883 id = got_object_id_dup(
884 got_object_tag_get_object_id(tag));
885 if (id == NULL)
886 error = got_error_from_errno();
887 got_object_tag_close(tag);
888 if (error)
889 goto done;
890 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
891 error = got_error(GOT_ERR_OBJ_TYPE);
892 goto done;
894 error = got_object_open_as_commit(&commit, repo, id);
895 if (error != NULL)
896 goto done;
898 if (commit == NULL) {
899 error = got_object_resolve_id_str(&id, repo,
900 start_commit);
901 if (error != NULL)
902 return error;
905 if (error != NULL)
906 goto done;
908 error = got_repo_map_path(&in_repo_path, repo, path, 1);
909 if (error != NULL)
910 goto done;
911 if (in_repo_path) {
912 free(path);
913 path = in_repo_path;
916 SIMPLEQ_INIT(&refs);
917 error = got_ref_list(&refs, repo);
918 if (error)
919 goto done;
921 error = print_commits(id, repo, path, show_patch,
922 diff_context, limit, first_parent_traversal, &refs);
923 done:
924 free(path);
925 free(repo_path);
926 free(cwd);
927 free(id);
928 if (worktree)
929 got_worktree_close(worktree);
930 if (repo) {
931 const struct got_error *repo_error;
932 repo_error = got_repo_close(repo);
933 if (error == NULL)
934 error = repo_error;
936 return error;
939 __dead static void
940 usage_diff(void)
942 fprintf(stderr, "usage: %s diff [-C number] [repository-path] "
943 "object1 object2\n", getprogname());
944 exit(1);
947 static const struct got_error *
948 cmd_diff(int argc, char *argv[])
950 const struct got_error *error;
951 struct got_repository *repo = NULL;
952 char *repo_path = NULL;
953 struct got_object_id *id1 = NULL, *id2 = NULL;
954 char *id_str1 = NULL, *id_str2 = NULL;
955 int type1, type2;
956 int diff_context = 3, ch;
957 const char *errstr;
959 #ifndef PROFILE
960 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
961 NULL) == -1)
962 err(1, "pledge");
963 #endif
965 while ((ch = getopt(argc, argv, "C:")) != -1) {
966 switch (ch) {
967 case 'C':
968 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
969 if (errstr != NULL)
970 err(1, "-C option %s", errstr);
971 break;
972 default:
973 usage();
974 /* NOTREACHED */
978 argc -= optind;
979 argv += optind;
981 if (argc == 0) {
982 usage_diff(); /* TODO show local worktree changes */
983 } else if (argc == 2) {
984 repo_path = getcwd(NULL, 0);
985 if (repo_path == NULL)
986 return got_error_from_errno();
987 id_str1 = argv[0];
988 id_str2 = argv[1];
989 } else if (argc == 3) {
990 repo_path = realpath(argv[0], NULL);
991 if (repo_path == NULL)
992 return got_error_from_errno();
993 id_str1 = argv[1];
994 id_str2 = argv[2];
995 } else
996 usage_diff();
998 error = apply_unveil(repo_path, NULL);
999 if (error)
1000 goto done;
1002 error = got_repo_open(&repo, repo_path);
1003 free(repo_path);
1004 if (error != NULL)
1005 goto done;
1007 error = got_object_resolve_id_str(&id1, repo, id_str1);
1008 if (error)
1009 goto done;
1011 error = got_object_resolve_id_str(&id2, repo, id_str2);
1012 if (error)
1013 goto done;
1015 error = got_object_get_type(&type1, repo, id1);
1016 if (error)
1017 goto done;
1019 error = got_object_get_type(&type2, repo, id2);
1020 if (error)
1021 goto done;
1023 if (type1 != type2) {
1024 error = got_error(GOT_ERR_OBJ_TYPE);
1025 goto done;
1028 switch (type1) {
1029 case GOT_OBJ_TYPE_BLOB:
1030 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1031 diff_context, repo, stdout);
1032 break;
1033 case GOT_OBJ_TYPE_TREE:
1034 error = got_diff_objects_as_trees(id1, id2, "", "",
1035 diff_context, repo, stdout);
1036 break;
1037 case GOT_OBJ_TYPE_COMMIT:
1038 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
1039 id_str2);
1040 error = got_diff_objects_as_commits(id1, id2, diff_context,
1041 repo, stdout);
1042 break;
1043 default:
1044 error = got_error(GOT_ERR_OBJ_TYPE);
1047 done:
1048 free(id1);
1049 free(id2);
1050 if (repo) {
1051 const struct got_error *repo_error;
1052 repo_error = got_repo_close(repo);
1053 if (error == NULL)
1054 error = repo_error;
1056 return error;
1059 __dead static void
1060 usage_blame(void)
1062 fprintf(stderr,
1063 "usage: %s blame [-c commit] [-r repository-path] path\n",
1064 getprogname());
1065 exit(1);
1068 static const struct got_error *
1069 cmd_blame(int argc, char *argv[])
1071 const struct got_error *error;
1072 struct got_repository *repo = NULL;
1073 struct got_worktree *worktree = NULL;
1074 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1075 struct got_object_id *commit_id = NULL;
1076 char *commit_id_str = NULL;
1077 int ch;
1079 #ifndef PROFILE
1080 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1081 NULL) == -1)
1082 err(1, "pledge");
1083 #endif
1085 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1086 switch (ch) {
1087 case 'c':
1088 commit_id_str = optarg;
1089 break;
1090 case 'r':
1091 repo_path = realpath(optarg, NULL);
1092 if (repo_path == NULL)
1093 err(1, "-r option");
1094 break;
1095 default:
1096 usage();
1097 /* NOTREACHED */
1101 argc -= optind;
1102 argv += optind;
1104 if (argc == 1)
1105 path = argv[0];
1106 else
1107 usage_blame();
1109 cwd = getcwd(NULL, 0);
1110 if (cwd == NULL) {
1111 error = got_error_from_errno();
1112 goto done;
1114 if (repo_path == NULL) {
1115 error = got_worktree_open(&worktree, cwd);
1116 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1117 goto done;
1118 else
1119 error = NULL;
1120 if (worktree) {
1121 repo_path =
1122 strdup(got_worktree_get_repo_path(worktree));
1123 if (repo_path == NULL)
1124 error = got_error_from_errno();
1125 if (error)
1126 goto done;
1127 } else {
1128 repo_path = strdup(cwd);
1129 if (repo_path == NULL) {
1130 error = got_error_from_errno();
1131 goto done;
1136 error = apply_unveil(repo_path, NULL);
1137 if (error)
1138 goto done;
1140 error = got_repo_open(&repo, repo_path);
1141 if (error != NULL)
1142 goto done;
1144 if (worktree) {
1145 char *p, *worktree_subdir = cwd +
1146 strlen(got_worktree_get_root_path(worktree));
1147 if (asprintf(&p, "%s/%s/%s",
1148 got_worktree_get_path_prefix(worktree),
1149 worktree_subdir, path) == -1) {
1150 error = got_error_from_errno();
1151 goto done;
1153 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1154 free(p);
1155 } else {
1156 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1158 if (error)
1159 goto done;
1161 if (commit_id_str == NULL) {
1162 struct got_reference *head_ref;
1163 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1164 if (error != NULL)
1165 goto done;
1166 error = got_ref_resolve(&commit_id, repo, head_ref);
1167 got_ref_close(head_ref);
1168 if (error != NULL)
1169 goto done;
1170 } else {
1171 error = got_object_resolve_id_str(&commit_id, repo,
1172 commit_id_str);
1173 if (error != NULL)
1174 goto done;
1177 error = got_blame(in_repo_path, commit_id, repo, stdout);
1178 done:
1179 free(in_repo_path);
1180 free(repo_path);
1181 free(cwd);
1182 free(commit_id);
1183 if (worktree)
1184 got_worktree_close(worktree);
1185 if (repo) {
1186 const struct got_error *repo_error;
1187 repo_error = got_repo_close(repo);
1188 if (error == NULL)
1189 error = repo_error;
1191 return error;
1194 __dead static void
1195 usage_tree(void)
1197 fprintf(stderr,
1198 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1199 getprogname());
1200 exit(1);
1203 static void
1204 print_entry(struct got_tree_entry *te, const char *id, const char *path,
1205 const char *root_path)
1207 int is_root_path = (strcmp(path, root_path) == 0);
1209 path += strlen(root_path);
1210 while (path[0] == '/')
1211 path++;
1213 printf("%s%s%s%s%s\n", id ? id : "", path,
1214 is_root_path ? "" : "/",
1215 te->name, S_ISDIR(te->mode) ? "/" : "");
1218 static const struct got_error *
1219 print_tree(const char *path, struct got_object_id *commit_id,
1220 int show_ids, int recurse, const char *root_path,
1221 struct got_repository *repo)
1223 const struct got_error *err = NULL;
1224 struct got_object_id *tree_id = NULL;
1225 struct got_tree_object *tree = NULL;
1226 const struct got_tree_entries *entries;
1227 struct got_tree_entry *te;
1229 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1230 if (err)
1231 goto done;
1233 err = got_object_open_as_tree(&tree, repo, tree_id);
1234 if (err)
1235 goto done;
1236 entries = got_object_tree_get_entries(tree);
1237 te = SIMPLEQ_FIRST(&entries->head);
1238 while (te) {
1239 char *id = NULL;
1241 if (sigint_received || sigpipe_received)
1242 break;
1244 if (show_ids) {
1245 char *id_str;
1246 err = got_object_id_str(&id_str, te->id);
1247 if (err)
1248 goto done;
1249 if (asprintf(&id, "%s ", id_str) == -1) {
1250 err = got_error_from_errno();
1251 free(id_str);
1252 goto done;
1254 free(id_str);
1256 print_entry(te, id, path, root_path);
1257 free(id);
1259 if (recurse && S_ISDIR(te->mode)) {
1260 char *child_path;
1261 if (asprintf(&child_path, "%s%s%s", path,
1262 path[0] == '/' && path[1] == '\0' ? "" : "/",
1263 te->name) == -1) {
1264 err = got_error_from_errno();
1265 goto done;
1267 err = print_tree(child_path, commit_id, show_ids, 1,
1268 root_path, repo);
1269 free(child_path);
1270 if (err)
1271 goto done;
1274 te = SIMPLEQ_NEXT(te, entry);
1276 done:
1277 if (tree)
1278 got_object_tree_close(tree);
1279 free(tree_id);
1280 return err;
1283 static const struct got_error *
1284 cmd_tree(int argc, char *argv[])
1286 const struct got_error *error;
1287 struct got_repository *repo = NULL;
1288 struct got_worktree *worktree = NULL;
1289 const char *path;
1290 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1291 struct got_object_id *commit_id = NULL;
1292 char *commit_id_str = NULL;
1293 int show_ids = 0, recurse = 0;
1294 int ch;
1296 #ifndef PROFILE
1297 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1298 NULL) == -1)
1299 err(1, "pledge");
1300 #endif
1302 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1303 switch (ch) {
1304 case 'c':
1305 commit_id_str = optarg;
1306 break;
1307 case 'r':
1308 repo_path = realpath(optarg, NULL);
1309 if (repo_path == NULL)
1310 err(1, "-r option");
1311 break;
1312 case 'i':
1313 show_ids = 1;
1314 break;
1315 case 'R':
1316 recurse = 1;
1317 break;
1318 default:
1319 usage();
1320 /* NOTREACHED */
1324 argc -= optind;
1325 argv += optind;
1327 if (argc == 1)
1328 path = argv[0];
1329 else if (argc > 1)
1330 usage_tree();
1331 else
1332 path = NULL;
1334 cwd = getcwd(NULL, 0);
1335 if (cwd == NULL) {
1336 error = got_error_from_errno();
1337 goto done;
1339 if (repo_path == NULL) {
1340 error = got_worktree_open(&worktree, cwd);
1341 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1342 goto done;
1343 else
1344 error = NULL;
1345 if (worktree) {
1346 repo_path =
1347 strdup(got_worktree_get_repo_path(worktree));
1348 if (repo_path == NULL)
1349 error = got_error_from_errno();
1350 if (error)
1351 goto done;
1352 } else {
1353 repo_path = strdup(cwd);
1354 if (repo_path == NULL) {
1355 error = got_error_from_errno();
1356 goto done;
1361 error = apply_unveil(repo_path, NULL);
1362 if (error)
1363 goto done;
1365 error = got_repo_open(&repo, repo_path);
1366 if (error != NULL)
1367 goto done;
1369 if (path == NULL) {
1370 if (worktree) {
1371 char *p, *worktree_subdir = cwd +
1372 strlen(got_worktree_get_root_path(worktree));
1373 if (asprintf(&p, "%s/%s",
1374 got_worktree_get_path_prefix(worktree),
1375 worktree_subdir) == -1) {
1376 error = got_error_from_errno();
1377 goto done;
1379 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1380 free(p);
1381 if (error)
1382 goto done;
1383 } else
1384 path = "/";
1386 if (in_repo_path == NULL) {
1387 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1388 if (error != NULL)
1389 goto done;
1392 if (commit_id_str == NULL) {
1393 struct got_reference *head_ref;
1394 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1395 if (error != NULL)
1396 goto done;
1397 error = got_ref_resolve(&commit_id, repo, head_ref);
1398 got_ref_close(head_ref);
1399 if (error != NULL)
1400 goto done;
1401 } else {
1402 error = got_object_resolve_id_str(&commit_id, repo,
1403 commit_id_str);
1404 if (error != NULL)
1405 goto done;
1408 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1409 in_repo_path, repo);
1410 done:
1411 free(in_repo_path);
1412 free(repo_path);
1413 free(cwd);
1414 free(commit_id);
1415 if (worktree)
1416 got_worktree_close(worktree);
1417 if (repo) {
1418 const struct got_error *repo_error;
1419 repo_error = got_repo_close(repo);
1420 if (error == NULL)
1421 error = repo_error;
1423 return error;
1426 __dead static void
1427 usage_status(void)
1429 fprintf(stderr, "usage: %s status [worktree-path]\n", getprogname());
1430 exit(1);
1433 static void
1434 print_status(void *arg, unsigned char status, const char *path)
1436 printf("%c %s\n", status, path);
1439 static const struct got_error *
1440 cmd_status(int argc, char *argv[])
1442 const struct got_error *error = NULL;
1443 struct got_repository *repo = NULL;
1444 struct got_worktree *worktree = NULL;
1445 char *worktree_path = NULL;
1446 int ch;
1448 while ((ch = getopt(argc, argv, "")) != -1) {
1449 switch (ch) {
1450 default:
1451 usage();
1452 /* NOTREACHED */
1456 argc -= optind;
1457 argv += optind;
1459 #ifndef PROFILE
1460 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1461 NULL) == -1)
1462 err(1, "pledge");
1463 #endif
1464 if (argc == 0) {
1465 worktree_path = getcwd(NULL, 0);
1466 if (worktree_path == NULL) {
1467 error = got_error_from_errno();
1468 goto done;
1470 } else if (argc == 1) {
1471 worktree_path = realpath(argv[0], NULL);
1472 if (worktree_path == NULL) {
1473 error = got_error_from_errno();
1474 goto done;
1476 } else
1477 usage_status();
1479 error = got_worktree_open(&worktree, worktree_path);
1480 if (error != NULL)
1481 goto done;
1483 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1484 if (error != NULL)
1485 goto done;
1487 error = apply_unveil(got_repo_get_path(repo),
1488 got_worktree_get_root_path(worktree));
1489 if (error)
1490 goto done;
1492 error = got_worktree_status(worktree, repo, print_status, NULL,
1493 check_cancelled, NULL);
1494 done:
1495 free(worktree_path);
1496 return error;