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);
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" },
106 main(int argc, char *argv[])
113 setlocale(LC_CTYPE, "");
115 while ((ch = getopt(argc, argv, "h")) != -1) {
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])))
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);
156 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
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);
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();
192 if (unveil(NULL, NULL) != 0)
193 return got_error_from_errno();
201 fprintf(stderr, "usage: %s checkout [-p prefix] repository-path "
202 "[worktree-path]\n", getprogname());
207 checkout_progress(void *arg, unsigned char status, const char *path)
209 char *worktree_path = arg;
211 while (path[0] == '/')
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);
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);
243 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
247 err = got_commit_graph_iter_start(graph, head_commit_id, repo);
251 struct got_object_id *id;
253 if (sigint_received || sigpipe_received)
256 err = got_commit_graph_iter_next(&id, graph);
258 if (err->code == GOT_ERR_ITER_COMPLETED) {
259 err = got_error(GOT_ERR_ANCESTRY);
262 if (err->code != GOT_ERR_ITER_NEED_MORE)
264 err = got_commit_graph_fetch_commits(graph, 1, repo);
272 if (got_object_id_cmp(id, commit_id) == 0)
277 got_ref_close(head_ref);
279 got_commit_graph_close(graph);
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) {
300 commit_id_str = strdup(optarg);
301 if (commit_id_str == NULL)
302 return got_error_from_errno();
305 path_prefix = optarg;
317 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
318 "unveil", NULL) == -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);
328 error = got_error_from_errno();
332 base = basename(path_prefix);
334 base = basename(repo_path);
336 error = got_error_from_errno();
339 dotgit = strstr(base, ".git");
342 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
343 error = got_error_from_errno();
348 } else if (argc == 2) {
349 repo_path = realpath(argv[0], NULL);
350 if (repo_path == NULL) {
351 error = got_error_from_errno();
354 worktree_path = realpath(argv[1], NULL);
355 if (worktree_path == NULL) {
356 error = got_error_from_errno();
362 error = apply_unveil(repo_path, worktree_path);
366 error = got_repo_open(&repo, repo_path);
370 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
374 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
375 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
378 error = got_worktree_open(&worktree, worktree_path);
382 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
386 if (!same_path_prefix) {
387 error = got_error(GOT_ERR_PATH_PREFIX);
392 struct got_object_id *commit_id;
393 error = got_object_resolve_id_str(&commit_id, repo,
397 error = check_ancestry(worktree, commit_id, repo);
402 error = got_worktree_set_base_commit_id(worktree, repo,
409 error = got_worktree_checkout_files(worktree, repo,
410 checkout_progress, worktree_path, check_cancelled, NULL);
414 printf("Now shut up and hack\n");
426 fprintf(stderr, "usage: %s update [-c commit] [worktree-path]\n",
432 update_progress(void *arg, unsigned char status, const char *path)
434 int *did_something = arg;
436 if (status == GOT_STATUS_EXISTS)
440 while (path[0] == '/')
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) {
459 commit_id_str = strdup(optarg);
460 if (commit_id_str == NULL)
461 return got_error_from_errno();
473 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
474 "unveil", NULL) == -1)
478 worktree_path = getcwd(NULL, 0);
479 if (worktree_path == NULL) {
480 error = got_error_from_errno();
483 } else if (argc == 1) {
484 worktree_path = realpath(argv[0], NULL);
485 if (worktree_path == NULL) {
486 error = got_error_from_errno();
492 error = got_worktree_open(&worktree, worktree_path);
496 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
500 error = apply_unveil(got_repo_get_path(repo),
501 got_worktree_get_root_path(worktree));
505 if (commit_id_str == NULL) {
506 struct got_reference *head_ref;
507 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
510 error = got_ref_resolve(&commit_id, repo, head_ref);
513 error = got_object_id_str(&commit_id_str, commit_id);
517 error = got_object_resolve_id_str(&commit_id, repo,
523 error = check_ancestry(worktree, commit_id, repo);
527 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
529 error = got_worktree_set_base_commit_id(worktree, repo,
535 error = got_worktree_checkout_files(worktree, repo,
536 update_progress, &did_something, check_cancelled, NULL);
541 printf("Updated to commit %s\n", commit_id_str);
543 printf("Already up-to-date\n");
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));
565 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
567 struct got_commit_object *pcommit;
569 err = got_object_open_as_commit(&pcommit, repo, qid->id);
573 err = got_object_open_as_tree(&tree1, repo,
574 got_object_commit_get_tree_id(pcommit));
575 got_object_commit_close(pcommit);
579 err = got_object_id_str(&id_str1, qid->id);
584 err = got_object_id_str(&id_str2, id);
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);
592 got_object_tree_close(tree1);
593 got_object_tree_close(tree2);
600 get_datestr(time_t *time, char *datebuf)
602 char *p, *s = ctime_r(time, datebuf);
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;
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) {
625 if (got_object_id_cmp(re->id, id) != 0)
627 name = got_ref_get_name(re->ref);
628 if (strcmp(name, GOT_REF_HEAD) == 0)
630 if (strncmp(name, "refs/", 5) == 0)
632 if (strncmp(name, "heads/", 6) == 0)
634 if (strncmp(name, "remotes/", 8) == 0)
637 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
639 err = got_error_from_errno();
645 err = got_object_id_str(&id_str, id);
649 printf("-----------------------------------------------\n");
650 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
651 refs_str ? refs_str : "", refs_str ? ")" : "");
656 printf("from: %s\n", got_object_commit_get_author(commit));
657 committer_time = got_object_commit_get_committer_time(commit);
658 datestr = get_datestr(&committer_time, datebuf);
659 printf("date: %s UTC\n", datestr);
660 author = got_object_commit_get_author(commit);
661 committer = got_object_commit_get_committer(commit);
662 if (strcmp(author, committer) != 0)
663 printf("via: %s\n", committer);
664 if (got_object_commit_get_nparents(commit) > 1) {
665 const struct got_object_id_queue *parent_ids;
666 struct got_object_qid *qid;
668 parent_ids = got_object_commit_get_parent_ids(commit);
669 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
670 err = got_object_id_str(&id_str, qid->id);
673 printf("parent %d: %s\n", n++, id_str);
678 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
680 return got_error_from_errno();
684 line = strsep(&logmsg, "\n");
686 printf(" %s\n", line);
691 err = print_patch(commit, id, diff_context, repo);
696 if (fflush(stdout) != 0 && err == NULL)
697 err = got_error_from_errno();
701 static const struct got_error *
702 print_commits(struct got_object_id *root_id, struct got_repository *repo,
703 char *path, int show_patch, int diff_context, int limit,
704 int first_parent_traversal, struct got_reflist_head *refs)
706 const struct got_error *err;
707 struct got_commit_graph *graph;
709 err = got_commit_graph_open(&graph, root_id, path,
710 first_parent_traversal, repo);
713 err = got_commit_graph_iter_start(graph, root_id, repo);
717 struct got_commit_object *commit;
718 struct got_object_id *id;
720 if (sigint_received || sigpipe_received)
723 err = got_commit_graph_iter_next(&id, graph);
725 if (err->code == GOT_ERR_ITER_COMPLETED) {
729 if (err->code != GOT_ERR_ITER_NEED_MORE)
731 err = got_commit_graph_fetch_commits(graph, 1, repo);
740 err = got_object_open_as_commit(&commit, repo, id);
743 err = print_commit(commit, id, repo, show_patch, diff_context,
745 got_object_commit_close(commit);
746 if (err || (limit && --limit == 0))
750 got_commit_graph_close(graph);
757 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
758 "[-r repository-path] [path]\n", getprogname());
762 static const struct got_error *
763 cmd_log(int argc, char *argv[])
765 const struct got_error *error;
766 struct got_repository *repo = NULL;
767 struct got_worktree *worktree = NULL;
768 struct got_commit_object *commit = NULL;
769 struct got_object_id *id = NULL;
770 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
771 char *start_commit = NULL;
772 int diff_context = 3, ch;
773 int show_patch = 0, limit = 0, first_parent_traversal = 0;
775 struct got_reflist_head refs;
778 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
784 while ((ch = getopt(argc, argv, "pc:C:l:fr:")) != -1) {
790 start_commit = optarg;
793 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
796 err(1, "-C option %s", errstr);
799 limit = strtonum(optarg, 1, INT_MAX, &errstr);
801 err(1, "-l option %s", errstr);
804 first_parent_traversal = 1;
807 repo_path = realpath(optarg, NULL);
808 if (repo_path == NULL)
823 path = strdup(argv[0]);
827 return got_error_from_errno();
829 cwd = getcwd(NULL, 0);
831 error = got_error_from_errno();
835 error = got_worktree_open(&worktree, cwd);
836 if (error && error->code != GOT_ERR_NOT_WORKTREE)
840 repo_path = worktree ?
841 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
842 if (repo_path == NULL) {
843 error = got_error_from_errno();
847 error = apply_unveil(repo_path, NULL);
851 error = got_repo_open(&repo, repo_path);
855 if (start_commit == NULL) {
856 struct got_reference *head_ref;
857 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
860 error = got_ref_resolve(&id, repo, head_ref);
861 got_ref_close(head_ref);
864 error = got_object_open_as_commit(&commit, repo, id);
866 struct got_reference *ref;
867 error = got_ref_open(&ref, repo, start_commit);
870 error = got_ref_resolve(&id, repo, ref);
874 error = got_object_get_type(&obj_type, repo, id);
877 if (obj_type == GOT_OBJ_TYPE_TAG) {
878 struct got_tag_object *tag;
879 error = got_object_open_as_tag(&tag, repo, id);
882 if (got_object_tag_get_object_type(tag) !=
883 GOT_OBJ_TYPE_COMMIT) {
884 got_object_tag_close(tag);
885 error = got_error(GOT_ERR_OBJ_TYPE);
889 id = got_object_id_dup(
890 got_object_tag_get_object_id(tag));
892 error = got_error_from_errno();
893 got_object_tag_close(tag);
896 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
897 error = got_error(GOT_ERR_OBJ_TYPE);
900 error = got_object_open_as_commit(&commit, repo, id);
904 if (commit == NULL) {
905 error = got_object_resolve_id_str(&id, repo,
914 error = got_repo_map_path(&in_repo_path, repo, path, 1);
923 error = got_ref_list(&refs, repo);
927 error = print_commits(id, repo, path, show_patch,
928 diff_context, limit, first_parent_traversal, &refs);
935 got_worktree_close(worktree);
937 const struct got_error *repo_error;
938 repo_error = got_repo_close(repo);
948 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
949 "[object1 object2 | path]\n", getprogname());
953 struct print_diff_arg {
954 struct got_repository *repo;
955 struct got_worktree *worktree;
961 static const struct got_error *
962 print_diff(void *arg, unsigned char status, const char *path,
963 struct got_object_id *id)
965 struct print_diff_arg *a = arg;
966 const struct got_error *err = NULL;
967 struct got_blob_object *blob1 = NULL;
969 char *abspath = NULL;
972 if (status != GOT_STATUS_MODIFY)
975 if (!a->header_shown) {
976 printf("diff %s %s\n", a->id_str,
977 got_worktree_get_root_path(a->worktree));
981 err = got_object_open_as_blob(&blob1, a->repo, id, 8192);
985 if (asprintf(&abspath, "%s/%s",
986 got_worktree_get_root_path(a->worktree), path) == -1) {
987 err = got_error_from_errno();
991 f2 = fopen(abspath, "r");
993 err = got_error_from_errno();
996 if (lstat(abspath, &sb) == -1) {
997 err = got_error_from_errno();
1001 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1005 got_object_blob_close(blob1);
1006 if (f2 && fclose(f2) != 0 && err == NULL)
1007 err = got_error_from_errno();
1012 static const struct got_error *
1013 get_status_path(char **status_path, struct got_worktree *worktree,
1016 const struct got_error *err = NULL;
1017 char *resolved, *path = NULL;
1020 *status_path = NULL;
1022 resolved = realpath(arg, NULL);
1023 if (resolved == NULL)
1024 return got_error_from_errno();
1026 if (strncmp(got_worktree_get_root_path(worktree), resolved,
1027 strlen(got_worktree_get_root_path(worktree)))) {
1028 err = got_error(GOT_ERR_BAD_PATH);
1032 path = strdup(resolved + strlen(got_worktree_get_root_path(worktree)));
1034 err = got_error_from_errno();
1038 /* XXX status walk can't deal with trailing slash! */
1040 while (path[len - 1] == '/') {
1041 path[len - 1] = '\0';
1047 *status_path = path;
1053 static const struct got_error *
1054 cmd_diff(int argc, char *argv[])
1056 const struct got_error *error;
1057 struct got_repository *repo = NULL;
1058 struct got_worktree *worktree = NULL;
1059 char *cwd = NULL, *repo_path = NULL;
1060 struct got_object_id *id1 = NULL, *id2 = NULL;
1061 char *id_str1 = NULL, *id_str2 = NULL;
1063 int diff_context = 3, ch;
1068 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1073 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1076 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1078 err(1, "-C option %s", errstr);
1081 repo_path = realpath(optarg, NULL);
1082 if (repo_path == NULL)
1083 err(1, "-r option");
1094 cwd = getcwd(NULL, 0);
1096 error = got_error_from_errno();
1099 error = got_worktree_open(&worktree, cwd);
1100 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1103 if (worktree == NULL) {
1104 error = got_error(GOT_ERR_NOT_WORKTREE);
1109 "-r option can't be used when diffing a work tree");
1110 repo_path = strdup(got_worktree_get_repo_path(worktree));
1111 if (repo_path == NULL) {
1112 error = got_error_from_errno();
1116 error = get_status_path(&path, worktree, argv[0]);
1122 error = got_error_from_errno();
1126 } else if (argc == 2) {
1132 if (repo_path == NULL) {
1133 repo_path = getcwd(NULL, 0);
1134 if (repo_path == NULL)
1135 return got_error_from_errno();
1138 error = apply_unveil(repo_path,
1139 worktree ? got_worktree_get_root_path(worktree) : NULL);
1143 error = got_repo_open(&repo, repo_path);
1149 struct print_diff_arg arg;
1151 error = got_object_id_str(&id_str,
1152 got_worktree_get_base_commit_id(worktree));
1156 arg.worktree = worktree;
1157 arg.diff_context = diff_context;
1158 arg.id_str = id_str;
1159 arg.header_shown = 0;
1161 error = got_worktree_status(worktree, path, repo, print_diff,
1162 &arg, check_cancelled, NULL);
1167 error = got_object_resolve_id_str(&id1, repo, id_str1);
1171 error = got_object_resolve_id_str(&id2, repo, id_str2);
1175 error = got_object_get_type(&type1, repo, id1);
1179 error = got_object_get_type(&type2, repo, id2);
1183 if (type1 != type2) {
1184 error = got_error(GOT_ERR_OBJ_TYPE);
1189 case GOT_OBJ_TYPE_BLOB:
1190 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1191 diff_context, repo, stdout);
1193 case GOT_OBJ_TYPE_TREE:
1194 error = got_diff_objects_as_trees(id1, id2, "", "",
1195 diff_context, repo, stdout);
1197 case GOT_OBJ_TYPE_COMMIT:
1198 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
1200 error = got_diff_objects_as_commits(id1, id2, diff_context,
1204 error = got_error(GOT_ERR_OBJ_TYPE);
1212 got_worktree_close(worktree);
1214 const struct got_error *repo_error;
1215 repo_error = got_repo_close(repo);
1226 "usage: %s blame [-c commit] [-r repository-path] path\n",
1231 static const struct got_error *
1232 cmd_blame(int argc, char *argv[])
1234 const struct got_error *error;
1235 struct got_repository *repo = NULL;
1236 struct got_worktree *worktree = NULL;
1237 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1238 struct got_object_id *commit_id = NULL;
1239 char *commit_id_str = NULL;
1243 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1248 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1251 commit_id_str = optarg;
1254 repo_path = realpath(optarg, NULL);
1255 if (repo_path == NULL)
1256 err(1, "-r option");
1272 cwd = getcwd(NULL, 0);
1274 error = got_error_from_errno();
1277 if (repo_path == NULL) {
1278 error = got_worktree_open(&worktree, cwd);
1279 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1285 strdup(got_worktree_get_repo_path(worktree));
1286 if (repo_path == NULL)
1287 error = got_error_from_errno();
1291 repo_path = strdup(cwd);
1292 if (repo_path == NULL) {
1293 error = got_error_from_errno();
1299 error = apply_unveil(repo_path, NULL);
1303 error = got_repo_open(&repo, repo_path);
1308 const char *prefix = got_worktree_get_path_prefix(worktree);
1309 char *p, *worktree_subdir = cwd +
1310 strlen(got_worktree_get_root_path(worktree));
1311 if (asprintf(&p, "%s%s%s%s%s",
1312 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1313 worktree_subdir, worktree_subdir[0] ? "/" : "",
1315 error = got_error_from_errno();
1318 error = got_repo_map_path(&in_repo_path, repo, p, 0);
1321 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1326 if (commit_id_str == NULL) {
1327 struct got_reference *head_ref;
1328 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1331 error = got_ref_resolve(&commit_id, repo, head_ref);
1332 got_ref_close(head_ref);
1336 error = got_object_resolve_id_str(&commit_id, repo,
1342 error = got_blame(in_repo_path, commit_id, repo, stdout);
1349 got_worktree_close(worktree);
1351 const struct got_error *repo_error;
1352 repo_error = got_repo_close(repo);
1363 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1369 print_entry(struct got_tree_entry *te, const char *id, const char *path,
1370 const char *root_path)
1372 int is_root_path = (strcmp(path, root_path) == 0);
1374 path += strlen(root_path);
1375 while (path[0] == '/')
1378 printf("%s%s%s%s%s\n", id ? id : "", path,
1379 is_root_path ? "" : "/", te->name,
1380 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
1383 static const struct got_error *
1384 print_tree(const char *path, struct got_object_id *commit_id,
1385 int show_ids, int recurse, const char *root_path,
1386 struct got_repository *repo)
1388 const struct got_error *err = NULL;
1389 struct got_object_id *tree_id = NULL;
1390 struct got_tree_object *tree = NULL;
1391 const struct got_tree_entries *entries;
1392 struct got_tree_entry *te;
1394 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1398 err = got_object_open_as_tree(&tree, repo, tree_id);
1401 entries = got_object_tree_get_entries(tree);
1402 te = SIMPLEQ_FIRST(&entries->head);
1406 if (sigint_received || sigpipe_received)
1411 err = got_object_id_str(&id_str, te->id);
1414 if (asprintf(&id, "%s ", id_str) == -1) {
1415 err = got_error_from_errno();
1421 print_entry(te, id, path, root_path);
1424 if (recurse && S_ISDIR(te->mode)) {
1426 if (asprintf(&child_path, "%s%s%s", path,
1427 path[0] == '/' && path[1] == '\0' ? "" : "/",
1429 err = got_error_from_errno();
1432 err = print_tree(child_path, commit_id, show_ids, 1,
1439 te = SIMPLEQ_NEXT(te, entry);
1443 got_object_tree_close(tree);
1448 static const struct got_error *
1449 cmd_tree(int argc, char *argv[])
1451 const struct got_error *error;
1452 struct got_repository *repo = NULL;
1453 struct got_worktree *worktree = NULL;
1455 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1456 struct got_object_id *commit_id = NULL;
1457 char *commit_id_str = NULL;
1458 int show_ids = 0, recurse = 0;
1462 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1467 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1470 commit_id_str = optarg;
1473 repo_path = realpath(optarg, NULL);
1474 if (repo_path == NULL)
1475 err(1, "-r option");
1499 cwd = getcwd(NULL, 0);
1501 error = got_error_from_errno();
1504 if (repo_path == NULL) {
1505 error = got_worktree_open(&worktree, cwd);
1506 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1512 strdup(got_worktree_get_repo_path(worktree));
1513 if (repo_path == NULL)
1514 error = got_error_from_errno();
1518 repo_path = strdup(cwd);
1519 if (repo_path == NULL) {
1520 error = got_error_from_errno();
1526 error = apply_unveil(repo_path, NULL);
1530 error = got_repo_open(&repo, repo_path);
1536 char *p, *worktree_subdir = cwd +
1537 strlen(got_worktree_get_root_path(worktree));
1538 if (asprintf(&p, "%s/%s",
1539 got_worktree_get_path_prefix(worktree),
1540 worktree_subdir) == -1) {
1541 error = got_error_from_errno();
1544 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1551 if (in_repo_path == NULL) {
1552 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1557 if (commit_id_str == NULL) {
1558 struct got_reference *head_ref;
1559 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1562 error = got_ref_resolve(&commit_id, repo, head_ref);
1563 got_ref_close(head_ref);
1567 error = got_object_resolve_id_str(&commit_id, repo,
1573 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1574 in_repo_path, repo);
1581 got_worktree_close(worktree);
1583 const struct got_error *repo_error;
1584 repo_error = got_repo_close(repo);
1594 fprintf(stderr, "usage: %s status [path]\n", getprogname());
1598 static const struct got_error *
1599 print_status(void *arg, unsigned char status, const char *path,
1600 struct got_object_id *id)
1602 printf("%c %s\n", status, path);
1606 static const struct got_error *
1607 cmd_status(int argc, char *argv[])
1609 const struct got_error *error = NULL;
1610 struct got_repository *repo = NULL;
1611 struct got_worktree *worktree = NULL;
1612 char *cwd = NULL, *path = NULL;
1615 while ((ch = getopt(argc, argv, "")) != -1) {
1627 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1631 cwd = getcwd(NULL, 0);
1633 error = got_error_from_errno();
1637 error = got_worktree_open(&worktree, cwd);
1644 error = got_error_from_errno();
1647 } else if (argc == 1) {
1648 error = get_status_path(&path, worktree, argv[0]);
1654 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1658 error = apply_unveil(got_repo_get_path(repo),
1659 got_worktree_get_root_path(worktree));
1663 error = got_worktree_status(worktree, path, repo, print_status, NULL,
1664 check_cancelled, NULL);