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 flock proc exec sendfd unveil",
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 flock proc exec sendfd unveil",
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), worktree_path);
504 if (commit_id_str == NULL) {
505 struct got_reference *head_ref;
506 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
509 error = got_ref_resolve(&commit_id, repo, head_ref);
512 error = got_object_id_str(&commit_id_str, commit_id);
516 error = got_object_resolve_id_str(&commit_id, repo,
522 error = check_ancestry(worktree, commit_id, repo);
526 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
528 error = got_worktree_set_base_commit_id(worktree, repo,
534 error = got_worktree_checkout_files(worktree, repo,
535 update_progress, &did_something, check_cancelled, NULL);
540 printf("Updated to commit %s\n", commit_id_str);
542 printf("Already up-to-date\n");
550 static const struct got_error *
551 print_patch(struct got_commit_object *commit, struct got_object_id *id,
552 int diff_context, struct got_repository *repo)
554 const struct got_error *err = NULL;
555 struct got_tree_object *tree1 = NULL, *tree2;
556 struct got_object_qid *qid;
557 char *id_str1 = NULL, *id_str2;
559 err = got_object_open_as_tree(&tree2, repo,
560 got_object_commit_get_tree_id(commit));
564 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
566 struct got_commit_object *pcommit;
568 err = got_object_open_as_commit(&pcommit, repo, qid->id);
572 err = got_object_open_as_tree(&tree1, repo,
573 got_object_commit_get_tree_id(pcommit));
574 got_object_commit_close(pcommit);
578 err = got_object_id_str(&id_str1, qid->id);
583 err = got_object_id_str(&id_str2, id);
587 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
588 err = got_diff_tree(tree1, tree2, "", "", diff_context, repo, stdout);
591 got_object_tree_close(tree1);
592 got_object_tree_close(tree2);
599 get_datestr(time_t *time, char *datebuf)
601 char *p, *s = ctime_r(time, datebuf);
608 static const struct got_error *
609 print_commit(struct got_commit_object *commit, struct got_object_id *id,
610 struct got_repository *repo, int show_patch, int diff_context,
611 struct got_reflist_head *refs)
613 const struct got_error *err = NULL;
614 char *id_str, *datestr, *logmsg0, *logmsg, *line;
616 time_t committer_time;
617 const char *author, *committer;
618 char *refs_str = NULL;
619 struct got_reflist_entry *re;
621 SIMPLEQ_FOREACH(re, refs, entry) {
624 if (got_object_id_cmp(re->id, id) != 0)
626 name = got_ref_get_name(re->ref);
627 if (strncmp(name, "refs/", 5) == 0)
629 if (strncmp(name, "heads/", 6) == 0)
631 if (strncmp(name, "remotes/", 8) == 0)
634 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
636 err = got_error_from_errno();
642 err = got_object_id_str(&id_str, id);
646 printf("-----------------------------------------------\n");
647 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
648 refs_str ? refs_str : "", refs_str ? ")" : "");
650 printf("from: %s\n", got_object_commit_get_author(commit));
651 committer_time = got_object_commit_get_committer_time(commit);
652 datestr = get_datestr(&committer_time, datebuf);
653 printf("date: %s UTC\n", datestr);
654 author = got_object_commit_get_author(commit);
655 committer = got_object_commit_get_committer(commit);
656 if (strcmp(author, committer) != 0)
657 printf("via: %s\n", committer);
658 if (got_object_commit_get_nparents(commit) > 1) {
659 const struct got_object_id_queue *parent_ids;
660 struct got_object_qid *qid;
662 parent_ids = got_object_commit_get_parent_ids(commit);
663 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
664 err = got_object_id_str(&id_str, qid->id);
667 printf("parent %d: %s\n", n++, id_str);
672 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
674 return got_error_from_errno();
678 line = strsep(&logmsg, "\n");
680 printf(" %s\n", line);
685 err = print_patch(commit, id, diff_context, repo);
694 static const struct got_error *
695 print_commits(struct got_object_id *root_id, struct got_repository *repo,
696 char *path, int show_patch, int diff_context, int limit,
697 int first_parent_traversal, struct got_reflist_head *refs)
699 const struct got_error *err;
700 struct got_commit_graph *graph;
702 err = got_commit_graph_open(&graph, root_id, path,
703 first_parent_traversal, repo);
706 err = got_commit_graph_iter_start(graph, root_id, repo);
710 struct got_commit_object *commit;
711 struct got_object_id *id;
713 if (sigint_received || sigpipe_received)
716 err = got_commit_graph_iter_next(&id, graph);
718 if (err->code == GOT_ERR_ITER_COMPLETED) {
722 if (err->code != GOT_ERR_ITER_NEED_MORE)
724 err = got_commit_graph_fetch_commits(graph, 1, repo);
733 err = got_object_open_as_commit(&commit, repo, id);
736 err = print_commit(commit, id, repo, show_patch, diff_context,
738 got_object_commit_close(commit);
739 if (err || (limit && --limit == 0))
743 got_commit_graph_close(graph);
750 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
751 "[-r repository-path] [path]\n", getprogname());
755 static const struct got_error *
756 cmd_log(int argc, char *argv[])
758 const struct got_error *error;
759 struct got_repository *repo = NULL;
760 struct got_commit_object *commit = NULL;
761 struct got_object_id *id = NULL;
762 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
763 char *start_commit = NULL;
764 int diff_context = 3, ch;
765 int show_patch = 0, limit = 0, first_parent_traversal = 0;
767 struct got_reflist_head refs;
770 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
776 while ((ch = getopt(argc, argv, "pc:C:l:fr:")) != -1) {
782 start_commit = optarg;
785 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
788 err(1, "-C option %s", errstr);
791 limit = strtonum(optarg, 1, INT_MAX, &errstr);
793 err(1, "-l option %s", errstr);
796 first_parent_traversal = 1;
799 repo_path = realpath(optarg, NULL);
800 if (repo_path == NULL)
815 path = strdup(argv[0]);
819 return got_error_from_errno();
821 cwd = getcwd(NULL, 0);
823 error = got_error_from_errno();
826 if (repo_path == NULL) {
827 repo_path = strdup(cwd);
828 if (repo_path == NULL) {
829 error = got_error_from_errno();
834 error = apply_unveil(repo_path, NULL);
838 error = got_repo_open(&repo, repo_path);
842 if (start_commit == NULL) {
843 struct got_reference *head_ref;
844 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
847 error = got_ref_resolve(&id, repo, head_ref);
848 got_ref_close(head_ref);
851 error = got_object_open_as_commit(&commit, repo, id);
853 struct got_reference *ref;
854 error = got_ref_open(&ref, repo, start_commit);
857 error = got_ref_resolve(&id, repo, ref);
861 error = got_object_get_type(&obj_type, repo, id);
864 if (obj_type == GOT_OBJ_TYPE_TAG) {
865 struct got_tag_object *tag;
866 error = got_object_open_as_tag(&tag, repo, id);
869 if (got_object_tag_get_object_type(tag) !=
870 GOT_OBJ_TYPE_COMMIT) {
871 got_object_tag_close(tag);
872 error = got_error(GOT_ERR_OBJ_TYPE);
876 id = got_object_id_dup(
877 got_object_tag_get_object_id(tag));
879 error = got_error_from_errno();
880 got_object_tag_close(tag);
883 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
884 error = got_error(GOT_ERR_OBJ_TYPE);
887 error = got_object_open_as_commit(&commit, repo, id);
891 if (commit == NULL) {
892 error = got_object_resolve_id_str(&id, repo,
901 error = got_repo_map_path(&in_repo_path, repo, path, 1);
910 error = got_ref_list(&refs, repo);
914 error = print_commits(id, repo, path, show_patch,
915 diff_context, limit, first_parent_traversal, &refs);
922 const struct got_error *repo_error;
923 repo_error = got_repo_close(repo);
933 fprintf(stderr, "usage: %s diff [-C number] [repository-path] "
934 "object1 object2\n", getprogname());
938 static const struct got_error *
939 cmd_diff(int argc, char *argv[])
941 const struct got_error *error;
942 struct got_repository *repo = NULL;
943 char *repo_path = NULL;
944 struct got_object_id *id1 = NULL, *id2 = NULL;
945 char *id_str1 = NULL, *id_str2 = NULL;
947 int diff_context = 3, ch;
951 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
956 while ((ch = getopt(argc, argv, "C:")) != -1) {
959 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
961 err(1, "-C option %s", errstr);
973 usage_diff(); /* TODO show local worktree changes */
974 } else if (argc == 2) {
975 repo_path = getcwd(NULL, 0);
976 if (repo_path == NULL)
977 return got_error_from_errno();
980 } else if (argc == 3) {
981 repo_path = realpath(argv[0], NULL);
982 if (repo_path == NULL)
983 return got_error_from_errno();
989 error = apply_unveil(repo_path, NULL);
993 error = got_repo_open(&repo, repo_path);
998 error = got_object_resolve_id_str(&id1, repo, id_str1);
1002 error = got_object_resolve_id_str(&id2, repo, id_str2);
1006 error = got_object_get_type(&type1, repo, id1);
1010 error = got_object_get_type(&type2, repo, id2);
1014 if (type1 != type2) {
1015 error = got_error(GOT_ERR_OBJ_TYPE);
1020 case GOT_OBJ_TYPE_BLOB:
1021 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1022 diff_context, repo, stdout);
1024 case GOT_OBJ_TYPE_TREE:
1025 error = got_diff_objects_as_trees(id1, id2, "", "",
1026 diff_context, repo, stdout);
1028 case GOT_OBJ_TYPE_COMMIT:
1029 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
1031 error = got_diff_objects_as_commits(id1, id2, diff_context,
1035 error = got_error(GOT_ERR_OBJ_TYPE);
1042 const struct got_error *repo_error;
1043 repo_error = got_repo_close(repo);
1053 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
1058 static const struct got_error *
1059 cmd_blame(int argc, char *argv[])
1061 const struct got_error *error;
1062 struct got_repository *repo = NULL;
1063 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1064 struct got_object_id *commit_id = NULL;
1065 char *commit_id_str = NULL;
1069 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1074 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1077 commit_id_str = optarg;
1080 repo_path = realpath(optarg, NULL);
1081 if (repo_path == NULL)
1082 err(1, "-r option");
1098 cwd = getcwd(NULL, 0);
1100 error = got_error_from_errno();
1103 if (repo_path == NULL) {
1104 repo_path = strdup(cwd);
1105 if (repo_path == NULL) {
1106 error = got_error_from_errno();
1111 error = apply_unveil(repo_path, NULL);
1115 error = got_repo_open(&repo, repo_path);
1119 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1123 if (commit_id_str == NULL) {
1124 struct got_reference *head_ref;
1125 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1128 error = got_ref_resolve(&commit_id, repo, head_ref);
1129 got_ref_close(head_ref);
1133 error = got_object_resolve_id_str(&commit_id, repo,
1139 error = got_blame(in_repo_path, commit_id, repo, stdout);
1146 const struct got_error *repo_error;
1147 repo_error = got_repo_close(repo);
1158 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1164 print_entry(struct got_tree_entry *te, const char *id, const char *path,
1165 const char *root_path)
1167 int is_root_path = (strcmp(path, root_path) == 0);
1169 path += strlen(root_path);
1170 while (path[0] == '/')
1173 printf("%s%s%s%s%s\n", id ? id : "", path,
1174 is_root_path ? "" : "/",
1175 te->name, S_ISDIR(te->mode) ? "/" : "");
1178 static const struct got_error *
1179 print_tree(const char *path, struct got_object_id *commit_id,
1180 int show_ids, int recurse, const char *root_path,
1181 struct got_repository *repo)
1183 const struct got_error *err = NULL;
1184 struct got_object_id *tree_id = NULL;
1185 struct got_tree_object *tree = NULL;
1186 const struct got_tree_entries *entries;
1187 struct got_tree_entry *te;
1189 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1193 err = got_object_open_as_tree(&tree, repo, tree_id);
1196 entries = got_object_tree_get_entries(tree);
1197 te = SIMPLEQ_FIRST(&entries->head);
1201 if (sigint_received || sigpipe_received)
1206 err = got_object_id_str(&id_str, te->id);
1209 if (asprintf(&id, "%s ", id_str) == -1) {
1210 err = got_error_from_errno();
1216 print_entry(te, id, path, root_path);
1219 if (recurse && S_ISDIR(te->mode)) {
1221 if (asprintf(&child_path, "%s%s%s", path,
1222 path[0] == '/' && path[1] == '\0' ? "" : "/",
1224 err = got_error_from_errno();
1227 err = print_tree(child_path, commit_id, show_ids, 1,
1234 te = SIMPLEQ_NEXT(te, entry);
1238 got_object_tree_close(tree);
1243 static const struct got_error *
1244 cmd_tree(int argc, char *argv[])
1246 const struct got_error *error;
1247 struct got_repository *repo = NULL;
1248 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1249 struct got_object_id *commit_id = NULL;
1250 char *commit_id_str = NULL;
1251 int show_ids = 0, recurse = 0;
1255 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1260 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1263 commit_id_str = optarg;
1266 repo_path = realpath(optarg, NULL);
1267 if (repo_path == NULL)
1268 err(1, "-r option");
1292 cwd = getcwd(NULL, 0);
1294 error = got_error_from_errno();
1297 if (repo_path == NULL) {
1298 repo_path = strdup(cwd);
1299 if (repo_path == NULL) {
1300 error = got_error_from_errno();
1305 error = apply_unveil(repo_path, NULL);
1309 error = got_repo_open(&repo, repo_path);
1313 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1317 if (commit_id_str == NULL) {
1318 struct got_reference *head_ref;
1319 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1322 error = got_ref_resolve(&commit_id, repo, head_ref);
1323 got_ref_close(head_ref);
1327 error = got_object_resolve_id_str(&commit_id, repo,
1333 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1334 in_repo_path, repo);
1341 const struct got_error *repo_error;
1342 repo_error = got_repo_close(repo);
1352 fprintf(stderr, "usage: %s status [worktree-path]\n", getprogname());
1357 print_status(void *arg, unsigned char status, const char *path)
1359 printf("%c %s\n", status, path);
1362 static const struct got_error *
1363 cmd_status(int argc, char *argv[])
1365 const struct got_error *error = NULL;
1366 struct got_repository *repo = NULL;
1367 struct got_worktree *worktree = NULL;
1368 char *worktree_path = NULL;
1371 while ((ch = getopt(argc, argv, "")) != -1) {
1383 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1388 worktree_path = getcwd(NULL, 0);
1389 if (worktree_path == NULL) {
1390 error = got_error_from_errno();
1393 } else if (argc == 1) {
1394 worktree_path = realpath(argv[0], NULL);
1395 if (worktree_path == NULL) {
1396 error = got_error_from_errno();
1402 error = got_worktree_open(&worktree, worktree_path);
1406 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1410 error = apply_unveil(got_repo_get_path(repo), worktree_path);
1414 error = got_worktree_status(worktree, repo, print_status, NULL,
1415 check_cancelled, NULL);
1417 free(worktree_path);