2 * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
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.
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.
18 #include <sys/queue.h>
19 #include <sys/limits.h>
20 #include <sys/types.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"
40 #include "got_commit_graph.h"
41 #include "got_blame.h"
42 #include "got_privsep.h"
45 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
48 static volatile sig_atomic_t sigint_received;
49 static volatile sig_atomic_t sigpipe_received;
52 catch_sigint(int signo)
58 catch_sigpipe(int signo)
66 const struct got_error *(*cmd_main)(int, char *[]);
67 void (*cmd_usage)(void);
68 const char *cmd_descr;
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);
79 __dead static void usage_ref(void);
81 static const struct got_error* cmd_checkout(int, char *[]);
82 static const struct got_error* cmd_update(int, char *[]);
83 static const struct got_error* cmd_log(int, char *[]);
84 static const struct got_error* cmd_diff(int, char *[]);
85 static const struct got_error* cmd_blame(int, char *[]);
86 static const struct got_error* cmd_tree(int, char *[]);
87 static const struct got_error* cmd_status(int, char *[]);
88 static const struct got_error* cmd_ref(int, char *[]);
90 static struct cmd got_commands[] = {
91 { "checkout", cmd_checkout, usage_checkout,
92 "check out a new work tree from a repository" },
93 { "update", cmd_update, usage_update,
94 "update a work tree to a different commit" },
95 { "log", cmd_log, usage_log,
96 "show repository history" },
97 { "diff", cmd_diff, usage_diff,
98 "compare files and directories" },
99 { "blame", cmd_blame, usage_blame,
100 " show when lines in a file were changed" },
101 { "tree", cmd_tree, usage_tree,
102 " list files and directories in repository" },
103 { "status", cmd_status, usage_status,
104 "show modification status of files" },
105 { "ref", cmd_ref, usage_ref,
106 "manage references in repository" },
110 main(int argc, char *argv[])
117 setlocale(LC_CTYPE, "");
119 while ((ch = getopt(argc, argv, "h")) != -1) {
137 signal(SIGINT, catch_sigint);
138 signal(SIGPIPE, catch_sigpipe);
140 for (i = 0; i < nitems(got_commands); i++) {
141 const struct got_error *error;
143 cmd = &got_commands[i];
145 if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
149 got_commands[i].cmd_usage();
151 error = got_commands[i].cmd_main(argc, argv);
152 if (error && !(sigint_received || sigpipe_received)) {
153 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
160 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
169 fprintf(stderr, "usage: %s [-h] command [arg ...]\n\n"
170 "Available commands:\n", getprogname());
171 for (i = 0; i < nitems(got_commands); i++) {
172 struct cmd *cmd = &got_commands[i];
173 fprintf(stderr, " %s: %s\n", cmd->cmd_name, cmd->cmd_descr);
178 static const struct got_error *
179 apply_unveil(const char *repo_path, int repo_read_only,
180 const char *worktree_path)
182 const struct got_error *error;
184 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
185 return got_error_from_errno();
187 if (worktree_path && unveil(worktree_path, "rwc") != 0)
188 return got_error_from_errno();
190 if (unveil("/tmp", "rwc") != 0)
191 return got_error_from_errno();
193 error = got_privsep_unveil_exec_helpers();
197 if (unveil(NULL, NULL) != 0)
198 return got_error_from_errno();
206 fprintf(stderr, "usage: %s checkout [-p prefix] repository-path "
207 "[worktree-path]\n", getprogname());
212 checkout_progress(void *arg, unsigned char status, const char *path)
214 char *worktree_path = arg;
216 while (path[0] == '/')
219 printf("%c %s/%s\n", status, worktree_path, path);
222 static const struct got_error *
223 check_cancelled(void *arg)
225 if (sigint_received || sigpipe_received)
226 return got_error(GOT_ERR_CANCELLED);
230 static const struct got_error *
231 check_ancestry(struct got_worktree *worktree, struct got_object_id *commit_id,
232 struct got_repository *repo)
234 const struct got_error *err;
235 struct got_reference *head_ref = NULL;
236 struct got_object_id *head_commit_id = NULL;
237 struct got_commit_graph *graph = NULL;
239 head_ref = got_worktree_get_head_ref(worktree);
240 if (head_ref == NULL)
241 return got_error_from_errno();
243 /* TODO: Check the reflog. The head ref may have been rebased. */
244 err = got_ref_resolve(&head_commit_id, repo, head_ref);
248 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
252 err = got_commit_graph_iter_start(graph, head_commit_id, repo);
256 struct got_object_id *id;
258 if (sigint_received || sigpipe_received)
261 err = got_commit_graph_iter_next(&id, graph);
263 if (err->code == GOT_ERR_ITER_COMPLETED) {
264 err = got_error(GOT_ERR_ANCESTRY);
267 if (err->code != GOT_ERR_ITER_NEED_MORE)
269 err = got_commit_graph_fetch_commits(graph, 1, repo);
277 if (got_object_id_cmp(id, commit_id) == 0)
282 got_ref_close(head_ref);
284 got_commit_graph_close(graph);
289 static const struct got_error *
290 cmd_checkout(int argc, char *argv[])
292 const struct got_error *error = NULL;
293 struct got_repository *repo = NULL;
294 struct got_reference *head_ref = NULL;
295 struct got_worktree *worktree = NULL;
296 char *repo_path = NULL;
297 char *worktree_path = NULL;
298 const char *path_prefix = "";
299 char *commit_id_str = NULL;
300 int ch, same_path_prefix;
302 while ((ch = getopt(argc, argv, "c:p:")) != -1) {
305 commit_id_str = strdup(optarg);
306 if (commit_id_str == NULL)
307 return got_error_from_errno();
310 path_prefix = optarg;
322 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
323 "unveil", NULL) == -1)
327 char *cwd, *base, *dotgit;
328 repo_path = realpath(argv[0], NULL);
329 if (repo_path == NULL)
330 return got_error_from_errno();
331 cwd = getcwd(NULL, 0);
333 error = got_error_from_errno();
337 base = basename(path_prefix);
339 base = basename(repo_path);
341 error = got_error_from_errno();
344 dotgit = strstr(base, ".git");
347 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
348 error = got_error_from_errno();
353 } else if (argc == 2) {
354 repo_path = realpath(argv[0], NULL);
355 if (repo_path == NULL) {
356 error = got_error_from_errno();
359 worktree_path = realpath(argv[1], NULL);
360 if (worktree_path == NULL) {
361 error = got_error_from_errno();
367 error = apply_unveil(repo_path, 0, worktree_path);
371 error = got_repo_open(&repo, repo_path);
375 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
379 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
380 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
383 error = got_worktree_open(&worktree, worktree_path);
387 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
391 if (!same_path_prefix) {
392 error = got_error(GOT_ERR_PATH_PREFIX);
397 struct got_object_id *commit_id;
398 error = got_object_resolve_id_str(&commit_id, repo,
402 error = check_ancestry(worktree, commit_id, repo);
407 error = got_worktree_set_base_commit_id(worktree, repo,
414 error = got_worktree_checkout_files(worktree, repo,
415 checkout_progress, worktree_path, check_cancelled, NULL);
419 printf("Now shut up and hack\n");
431 fprintf(stderr, "usage: %s update [-c commit] [worktree-path]\n",
437 update_progress(void *arg, unsigned char status, const char *path)
439 int *did_something = arg;
441 if (status == GOT_STATUS_EXISTS)
445 while (path[0] == '/')
447 printf("%c %s\n", status, path);
450 static const struct got_error *
451 cmd_update(int argc, char *argv[])
453 const struct got_error *error = NULL;
454 struct got_repository *repo = NULL;
455 struct got_worktree *worktree = NULL;
456 char *worktree_path = NULL;
457 struct got_object_id *commit_id = NULL;
458 char *commit_id_str = NULL;
459 int ch, did_something = 0;
461 while ((ch = getopt(argc, argv, "c:")) != -1) {
464 commit_id_str = strdup(optarg);
465 if (commit_id_str == NULL)
466 return got_error_from_errno();
478 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
479 "unveil", NULL) == -1)
483 worktree_path = getcwd(NULL, 0);
484 if (worktree_path == NULL) {
485 error = got_error_from_errno();
488 } else if (argc == 1) {
489 worktree_path = realpath(argv[0], NULL);
490 if (worktree_path == NULL) {
491 error = got_error_from_errno();
497 error = got_worktree_open(&worktree, worktree_path);
501 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
505 error = apply_unveil(got_repo_get_path(repo), 0,
506 got_worktree_get_root_path(worktree));
510 if (commit_id_str == NULL) {
511 struct got_reference *head_ref;
512 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
515 error = got_ref_resolve(&commit_id, repo, head_ref);
518 error = got_object_id_str(&commit_id_str, commit_id);
522 error = got_object_resolve_id_str(&commit_id, repo,
528 error = check_ancestry(worktree, commit_id, repo);
532 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
534 error = got_worktree_set_base_commit_id(worktree, repo,
540 error = got_worktree_checkout_files(worktree, repo,
541 update_progress, &did_something, check_cancelled, NULL);
546 printf("Updated to commit %s\n", commit_id_str);
548 printf("Already up-to-date\n");
556 static const struct got_error *
557 print_patch(struct got_commit_object *commit, struct got_object_id *id,
558 int diff_context, struct got_repository *repo)
560 const struct got_error *err = NULL;
561 struct got_tree_object *tree1 = NULL, *tree2;
562 struct got_object_qid *qid;
563 char *id_str1 = NULL, *id_str2;
565 err = got_object_open_as_tree(&tree2, repo,
566 got_object_commit_get_tree_id(commit));
570 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
572 struct got_commit_object *pcommit;
574 err = got_object_open_as_commit(&pcommit, repo, qid->id);
578 err = got_object_open_as_tree(&tree1, repo,
579 got_object_commit_get_tree_id(pcommit));
580 got_object_commit_close(pcommit);
584 err = got_object_id_str(&id_str1, qid->id);
589 err = got_object_id_str(&id_str2, id);
593 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
594 err = got_diff_tree(tree1, tree2, "", "", diff_context, repo, stdout);
597 got_object_tree_close(tree1);
598 got_object_tree_close(tree2);
605 get_datestr(time_t *time, char *datebuf)
607 char *p, *s = ctime_r(time, datebuf);
614 static const struct got_error *
615 print_commit(struct got_commit_object *commit, struct got_object_id *id,
616 struct got_repository *repo, int show_patch, int diff_context,
617 struct got_reflist_head *refs)
619 const struct got_error *err = NULL;
620 char *id_str, *datestr, *logmsg0, *logmsg, *line;
622 time_t committer_time;
623 const char *author, *committer;
624 char *refs_str = NULL;
625 struct got_reflist_entry *re;
627 SIMPLEQ_FOREACH(re, refs, entry) {
630 if (got_object_id_cmp(re->id, id) != 0)
632 name = got_ref_get_name(re->ref);
633 if (strcmp(name, GOT_REF_HEAD) == 0)
635 if (strncmp(name, "refs/", 5) == 0)
637 if (strncmp(name, "got/", 4) == 0)
639 if (strncmp(name, "heads/", 6) == 0)
641 if (strncmp(name, "remotes/", 8) == 0)
644 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
646 err = got_error_from_errno();
652 err = got_object_id_str(&id_str, id);
656 printf("-----------------------------------------------\n");
657 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
658 refs_str ? refs_str : "", refs_str ? ")" : "");
663 printf("from: %s\n", got_object_commit_get_author(commit));
664 committer_time = got_object_commit_get_committer_time(commit);
665 datestr = get_datestr(&committer_time, datebuf);
666 printf("date: %s UTC\n", datestr);
667 author = got_object_commit_get_author(commit);
668 committer = got_object_commit_get_committer(commit);
669 if (strcmp(author, committer) != 0)
670 printf("via: %s\n", committer);
671 if (got_object_commit_get_nparents(commit) > 1) {
672 const struct got_object_id_queue *parent_ids;
673 struct got_object_qid *qid;
675 parent_ids = got_object_commit_get_parent_ids(commit);
676 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
677 err = got_object_id_str(&id_str, qid->id);
680 printf("parent %d: %s\n", n++, id_str);
685 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
687 return got_error_from_errno();
691 line = strsep(&logmsg, "\n");
693 printf(" %s\n", line);
698 err = print_patch(commit, id, diff_context, repo);
703 if (fflush(stdout) != 0 && err == NULL)
704 err = got_error_from_errno();
708 static const struct got_error *
709 print_commits(struct got_object_id *root_id, struct got_repository *repo,
710 char *path, int show_patch, int diff_context, int limit,
711 int first_parent_traversal, struct got_reflist_head *refs)
713 const struct got_error *err;
714 struct got_commit_graph *graph;
716 err = got_commit_graph_open(&graph, root_id, path,
717 first_parent_traversal, repo);
720 err = got_commit_graph_iter_start(graph, root_id, repo);
724 struct got_commit_object *commit;
725 struct got_object_id *id;
727 if (sigint_received || sigpipe_received)
730 err = got_commit_graph_iter_next(&id, graph);
732 if (err->code == GOT_ERR_ITER_COMPLETED) {
736 if (err->code != GOT_ERR_ITER_NEED_MORE)
738 err = got_commit_graph_fetch_commits(graph, 1, repo);
747 err = got_object_open_as_commit(&commit, repo, id);
750 err = print_commit(commit, id, repo, show_patch, diff_context,
752 got_object_commit_close(commit);
753 if (err || (limit && --limit == 0))
757 got_commit_graph_close(graph);
764 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
765 "[-r repository-path] [path]\n", getprogname());
769 static const struct got_error *
770 cmd_log(int argc, char *argv[])
772 const struct got_error *error;
773 struct got_repository *repo = NULL;
774 struct got_worktree *worktree = NULL;
775 struct got_commit_object *commit = NULL;
776 struct got_object_id *id = NULL;
777 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
778 char *start_commit = NULL;
779 int diff_context = 3, ch;
780 int show_patch = 0, limit = 0, first_parent_traversal = 0;
782 struct got_reflist_head refs;
785 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
791 while ((ch = getopt(argc, argv, "pc:C:l:fr:")) != -1) {
797 start_commit = optarg;
800 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
803 err(1, "-C option %s", errstr);
806 limit = strtonum(optarg, 1, INT_MAX, &errstr);
808 err(1, "-l option %s", errstr);
811 first_parent_traversal = 1;
814 repo_path = realpath(optarg, NULL);
815 if (repo_path == NULL)
830 path = strdup(argv[0]);
834 return got_error_from_errno();
836 cwd = getcwd(NULL, 0);
838 error = got_error_from_errno();
842 error = got_worktree_open(&worktree, cwd);
843 if (error && error->code != GOT_ERR_NOT_WORKTREE)
847 repo_path = worktree ?
848 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
849 if (repo_path == NULL) {
850 error = got_error_from_errno();
854 error = apply_unveil(repo_path, 1,
855 worktree ? got_worktree_get_root_path(worktree) : NULL);
859 error = got_repo_open(&repo, repo_path);
863 if (start_commit == NULL) {
864 struct got_reference *head_ref;
865 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
868 error = got_ref_resolve(&id, repo, head_ref);
869 got_ref_close(head_ref);
872 error = got_object_open_as_commit(&commit, repo, id);
874 struct got_reference *ref;
875 error = got_ref_open(&ref, repo, start_commit);
878 error = got_ref_resolve(&id, repo, ref);
882 error = got_object_get_type(&obj_type, repo, id);
885 if (obj_type == GOT_OBJ_TYPE_TAG) {
886 struct got_tag_object *tag;
887 error = got_object_open_as_tag(&tag, repo, id);
890 if (got_object_tag_get_object_type(tag) !=
891 GOT_OBJ_TYPE_COMMIT) {
892 got_object_tag_close(tag);
893 error = got_error(GOT_ERR_OBJ_TYPE);
897 id = got_object_id_dup(
898 got_object_tag_get_object_id(tag));
900 error = got_error_from_errno();
901 got_object_tag_close(tag);
904 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
905 error = got_error(GOT_ERR_OBJ_TYPE);
908 error = got_object_open_as_commit(&commit, repo, id);
912 if (commit == NULL) {
913 error = got_object_resolve_id_str(&id, repo,
922 error = got_repo_map_path(&in_repo_path, repo, path, 1);
931 error = got_ref_list(&refs, repo);
935 error = print_commits(id, repo, path, show_patch,
936 diff_context, limit, first_parent_traversal, &refs);
943 got_worktree_close(worktree);
945 const struct got_error *repo_error;
946 repo_error = got_repo_close(repo);
950 got_ref_list_free(&refs);
957 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
958 "[object1 object2 | path]\n", getprogname());
962 struct print_diff_arg {
963 struct got_repository *repo;
964 struct got_worktree *worktree;
970 static const struct got_error *
971 print_diff(void *arg, unsigned char status, const char *path,
972 struct got_object_id *id)
974 struct print_diff_arg *a = arg;
975 const struct got_error *err = NULL;
976 struct got_blob_object *blob1 = NULL;
978 char *abspath = NULL;
981 if (status != GOT_STATUS_MODIFY)
984 if (!a->header_shown) {
985 printf("diff %s %s\n", a->id_str,
986 got_worktree_get_root_path(a->worktree));
990 err = got_object_open_as_blob(&blob1, a->repo, id, 8192);
994 if (asprintf(&abspath, "%s/%s",
995 got_worktree_get_root_path(a->worktree), path) == -1) {
996 err = got_error_from_errno();
1000 f2 = fopen(abspath, "r");
1002 err = got_error_from_errno();
1005 if (lstat(abspath, &sb) == -1) {
1006 err = got_error_from_errno();
1010 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1014 got_object_blob_close(blob1);
1015 if (f2 && fclose(f2) != 0 && err == NULL)
1016 err = got_error_from_errno();
1021 static const struct got_error *
1022 get_status_path(char **status_path, struct got_worktree *worktree,
1025 const struct got_error *err = NULL;
1026 char *resolved, *path = NULL;
1029 *status_path = NULL;
1031 resolved = realpath(arg, NULL);
1032 if (resolved == NULL)
1033 return got_error_from_errno();
1035 if (strncmp(got_worktree_get_root_path(worktree), resolved,
1036 strlen(got_worktree_get_root_path(worktree)))) {
1037 err = got_error(GOT_ERR_BAD_PATH);
1041 path = strdup(resolved + strlen(got_worktree_get_root_path(worktree)));
1043 err = got_error_from_errno();
1047 /* XXX status walk can't deal with trailing slash! */
1049 while (path[len - 1] == '/') {
1050 path[len - 1] = '\0';
1056 *status_path = path;
1062 static const struct got_error *
1063 cmd_diff(int argc, char *argv[])
1065 const struct got_error *error;
1066 struct got_repository *repo = NULL;
1067 struct got_worktree *worktree = NULL;
1068 char *cwd = NULL, *repo_path = NULL;
1069 struct got_object_id *id1 = NULL, *id2 = NULL;
1070 char *id_str1 = NULL, *id_str2 = NULL;
1072 int diff_context = 3, ch;
1077 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1082 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1085 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1087 err(1, "-C option %s", errstr);
1090 repo_path = realpath(optarg, NULL);
1091 if (repo_path == NULL)
1092 err(1, "-r option");
1103 cwd = getcwd(NULL, 0);
1105 error = got_error_from_errno();
1108 error = got_worktree_open(&worktree, cwd);
1109 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1112 if (worktree == NULL) {
1113 error = got_error(GOT_ERR_NOT_WORKTREE);
1118 "-r option can't be used when diffing a work tree");
1119 repo_path = strdup(got_worktree_get_repo_path(worktree));
1120 if (repo_path == NULL) {
1121 error = got_error_from_errno();
1125 error = get_status_path(&path, worktree, argv[0]);
1131 error = got_error_from_errno();
1135 } else if (argc == 2) {
1141 if (repo_path == NULL) {
1142 repo_path = getcwd(NULL, 0);
1143 if (repo_path == NULL)
1144 return got_error_from_errno();
1147 error = apply_unveil(repo_path, 1,
1148 worktree ? got_worktree_get_root_path(worktree) : NULL);
1152 error = got_repo_open(&repo, repo_path);
1158 struct print_diff_arg arg;
1160 error = got_object_id_str(&id_str,
1161 got_worktree_get_base_commit_id(worktree));
1165 arg.worktree = worktree;
1166 arg.diff_context = diff_context;
1167 arg.id_str = id_str;
1168 arg.header_shown = 0;
1170 error = got_worktree_status(worktree, path, repo, print_diff,
1171 &arg, check_cancelled, NULL);
1176 error = got_object_resolve_id_str(&id1, repo, id_str1);
1180 error = got_object_resolve_id_str(&id2, repo, id_str2);
1184 error = got_object_get_type(&type1, repo, id1);
1188 error = got_object_get_type(&type2, repo, id2);
1192 if (type1 != type2) {
1193 error = got_error(GOT_ERR_OBJ_TYPE);
1198 case GOT_OBJ_TYPE_BLOB:
1199 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1200 diff_context, repo, stdout);
1202 case GOT_OBJ_TYPE_TREE:
1203 error = got_diff_objects_as_trees(id1, id2, "", "",
1204 diff_context, repo, stdout);
1206 case GOT_OBJ_TYPE_COMMIT:
1207 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
1209 error = got_diff_objects_as_commits(id1, id2, diff_context,
1213 error = got_error(GOT_ERR_OBJ_TYPE);
1221 got_worktree_close(worktree);
1223 const struct got_error *repo_error;
1224 repo_error = got_repo_close(repo);
1235 "usage: %s blame [-c commit] [-r repository-path] path\n",
1240 static const struct got_error *
1241 cmd_blame(int argc, char *argv[])
1243 const struct got_error *error;
1244 struct got_repository *repo = NULL;
1245 struct got_worktree *worktree = NULL;
1246 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1247 struct got_object_id *commit_id = NULL;
1248 char *commit_id_str = NULL;
1252 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1257 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1260 commit_id_str = optarg;
1263 repo_path = realpath(optarg, NULL);
1264 if (repo_path == NULL)
1265 err(1, "-r option");
1281 cwd = getcwd(NULL, 0);
1283 error = got_error_from_errno();
1286 if (repo_path == NULL) {
1287 error = got_worktree_open(&worktree, cwd);
1288 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1294 strdup(got_worktree_get_repo_path(worktree));
1295 if (repo_path == NULL)
1296 error = got_error_from_errno();
1300 repo_path = strdup(cwd);
1301 if (repo_path == NULL) {
1302 error = got_error_from_errno();
1308 error = apply_unveil(repo_path, 1, NULL);
1312 error = got_repo_open(&repo, repo_path);
1317 const char *prefix = got_worktree_get_path_prefix(worktree);
1318 char *p, *worktree_subdir = cwd +
1319 strlen(got_worktree_get_root_path(worktree));
1320 if (asprintf(&p, "%s%s%s%s%s",
1321 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1322 worktree_subdir, worktree_subdir[0] ? "/" : "",
1324 error = got_error_from_errno();
1327 error = got_repo_map_path(&in_repo_path, repo, p, 0);
1330 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1335 if (commit_id_str == NULL) {
1336 struct got_reference *head_ref;
1337 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1340 error = got_ref_resolve(&commit_id, repo, head_ref);
1341 got_ref_close(head_ref);
1345 error = got_object_resolve_id_str(&commit_id, repo,
1351 error = got_blame(in_repo_path, commit_id, repo, stdout);
1358 got_worktree_close(worktree);
1360 const struct got_error *repo_error;
1361 repo_error = got_repo_close(repo);
1372 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1378 print_entry(struct got_tree_entry *te, const char *id, const char *path,
1379 const char *root_path)
1381 int is_root_path = (strcmp(path, root_path) == 0);
1383 path += strlen(root_path);
1384 while (path[0] == '/')
1387 printf("%s%s%s%s%s\n", id ? id : "", path,
1388 is_root_path ? "" : "/", te->name,
1389 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
1392 static const struct got_error *
1393 print_tree(const char *path, struct got_object_id *commit_id,
1394 int show_ids, int recurse, const char *root_path,
1395 struct got_repository *repo)
1397 const struct got_error *err = NULL;
1398 struct got_object_id *tree_id = NULL;
1399 struct got_tree_object *tree = NULL;
1400 const struct got_tree_entries *entries;
1401 struct got_tree_entry *te;
1403 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1407 err = got_object_open_as_tree(&tree, repo, tree_id);
1410 entries = got_object_tree_get_entries(tree);
1411 te = SIMPLEQ_FIRST(&entries->head);
1415 if (sigint_received || sigpipe_received)
1420 err = got_object_id_str(&id_str, te->id);
1423 if (asprintf(&id, "%s ", id_str) == -1) {
1424 err = got_error_from_errno();
1430 print_entry(te, id, path, root_path);
1433 if (recurse && S_ISDIR(te->mode)) {
1435 if (asprintf(&child_path, "%s%s%s", path,
1436 path[0] == '/' && path[1] == '\0' ? "" : "/",
1438 err = got_error_from_errno();
1441 err = print_tree(child_path, commit_id, show_ids, 1,
1448 te = SIMPLEQ_NEXT(te, entry);
1452 got_object_tree_close(tree);
1457 static const struct got_error *
1458 cmd_tree(int argc, char *argv[])
1460 const struct got_error *error;
1461 struct got_repository *repo = NULL;
1462 struct got_worktree *worktree = NULL;
1464 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1465 struct got_object_id *commit_id = NULL;
1466 char *commit_id_str = NULL;
1467 int show_ids = 0, recurse = 0;
1471 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1476 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1479 commit_id_str = optarg;
1482 repo_path = realpath(optarg, NULL);
1483 if (repo_path == NULL)
1484 err(1, "-r option");
1508 cwd = getcwd(NULL, 0);
1510 error = got_error_from_errno();
1513 if (repo_path == NULL) {
1514 error = got_worktree_open(&worktree, cwd);
1515 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1521 strdup(got_worktree_get_repo_path(worktree));
1522 if (repo_path == NULL)
1523 error = got_error_from_errno();
1527 repo_path = strdup(cwd);
1528 if (repo_path == NULL) {
1529 error = got_error_from_errno();
1535 error = apply_unveil(repo_path, 1, NULL);
1539 error = got_repo_open(&repo, repo_path);
1545 char *p, *worktree_subdir = cwd +
1546 strlen(got_worktree_get_root_path(worktree));
1547 if (asprintf(&p, "%s/%s",
1548 got_worktree_get_path_prefix(worktree),
1549 worktree_subdir) == -1) {
1550 error = got_error_from_errno();
1553 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1560 if (in_repo_path == NULL) {
1561 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1566 if (commit_id_str == NULL) {
1567 struct got_reference *head_ref;
1568 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1571 error = got_ref_resolve(&commit_id, repo, head_ref);
1572 got_ref_close(head_ref);
1576 error = got_object_resolve_id_str(&commit_id, repo,
1582 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1583 in_repo_path, repo);
1590 got_worktree_close(worktree);
1592 const struct got_error *repo_error;
1593 repo_error = got_repo_close(repo);
1603 fprintf(stderr, "usage: %s status [path]\n", getprogname());
1607 static const struct got_error *
1608 print_status(void *arg, unsigned char status, const char *path,
1609 struct got_object_id *id)
1611 printf("%c %s\n", status, path);
1615 static const struct got_error *
1616 cmd_status(int argc, char *argv[])
1618 const struct got_error *error = NULL;
1619 struct got_repository *repo = NULL;
1620 struct got_worktree *worktree = NULL;
1621 char *cwd = NULL, *path = NULL;
1624 while ((ch = getopt(argc, argv, "")) != -1) {
1636 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1640 cwd = getcwd(NULL, 0);
1642 error = got_error_from_errno();
1646 error = got_worktree_open(&worktree, cwd);
1653 error = got_error_from_errno();
1656 } else if (argc == 1) {
1657 error = get_status_path(&path, worktree, argv[0]);
1663 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1667 error = apply_unveil(got_repo_get_path(repo), 1,
1668 got_worktree_get_root_path(worktree));
1672 error = got_worktree_status(worktree, path, repo, print_status, NULL,
1673 check_cancelled, NULL);
1684 "usage: %s ref [-r repository] -l | -d name | name object\n",
1689 static const struct got_error *
1690 list_refs(struct got_repository *repo)
1692 static const struct got_error *err = NULL;
1693 struct got_reflist_head refs;
1694 struct got_reflist_entry *re;
1696 SIMPLEQ_INIT(&refs);
1697 err = got_ref_list(&refs, repo);
1701 SIMPLEQ_FOREACH(re, &refs, entry) {
1703 refstr = got_ref_to_str(re->ref);
1705 return got_error_from_errno();
1706 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
1710 got_ref_list_free(&refs);
1714 static const struct got_error *
1715 delete_ref(struct got_repository *repo, const char *refname)
1717 const struct got_error *err = NULL;
1718 struct got_reference *ref;
1720 err = got_ref_open(&ref, repo, refname);
1724 err = got_ref_delete(ref, repo);
1729 static const struct got_error *
1730 add_ref(struct got_repository *repo, const char *refname, const char *id_str)
1732 const struct got_error *err = NULL;
1733 struct got_object_id *id;
1734 struct got_reference *ref = NULL;
1736 err = got_object_resolve_id_str(&id, repo, id_str);
1740 err = got_ref_alloc(&ref, refname, id);
1744 err = got_ref_write(ref, repo);
1752 static const struct got_error *
1753 cmd_ref(int argc, char *argv[])
1755 const struct got_error *error = NULL;
1756 struct got_repository *repo = NULL;
1757 struct got_worktree *worktree = NULL;
1758 char *cwd = NULL, *repo_path = NULL;
1759 int ch, do_list = 0;
1760 const char *delref = NULL;
1762 /* TODO: Add -s option for adding symbolic references. */
1763 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
1769 repo_path = realpath(optarg, NULL);
1770 if (repo_path == NULL)
1771 err(1, "-r option");
1782 if (do_list && delref)
1783 errx(1, "-l and -d options are mutually exclusive\n");
1788 if (do_list || delref) {
1791 } else if (argc != 2)
1796 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
1800 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1801 "sendfd unveil", NULL) == -1)
1805 cwd = getcwd(NULL, 0);
1807 error = got_error_from_errno();
1811 if (repo_path == NULL) {
1812 error = got_worktree_open(&worktree, cwd);
1813 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1819 strdup(got_worktree_get_repo_path(worktree));
1820 if (repo_path == NULL)
1821 error = got_error_from_errno();
1825 repo_path = strdup(cwd);
1826 if (repo_path == NULL) {
1827 error = got_error_from_errno();
1833 error = apply_unveil(repo_path, do_list,
1834 worktree ? got_worktree_get_root_path(worktree) : NULL);
1838 error = got_repo_open(&repo, repo_path);
1843 error = list_refs(repo);
1845 error = delete_ref(repo, delref);
1847 error = add_ref(repo, argv[0], argv[1]);
1850 got_repo_close(repo);
1852 got_worktree_close(worktree);