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);
80 __dead static void usage_add(void);
81 __dead static void usage_rm(void);
83 static const struct got_error* cmd_checkout(int, char *[]);
84 static const struct got_error* cmd_update(int, char *[]);
85 static const struct got_error* cmd_log(int, char *[]);
86 static const struct got_error* cmd_diff(int, char *[]);
87 static const struct got_error* cmd_blame(int, char *[]);
88 static const struct got_error* cmd_tree(int, char *[]);
89 static const struct got_error* cmd_status(int, char *[]);
90 static const struct got_error* cmd_ref(int, char *[]);
91 static const struct got_error* cmd_add(int, char *[]);
92 static const struct got_error* cmd_rm(int, char *[]);
94 static struct cmd got_commands[] = {
95 { "checkout", cmd_checkout, usage_checkout,
96 "check out a new work tree from a repository" },
97 { "update", cmd_update, usage_update,
98 "update a work tree to a different commit" },
99 { "log", cmd_log, usage_log,
100 "show repository history" },
101 { "diff", cmd_diff, usage_diff,
102 "compare files and directories" },
103 { "blame", cmd_blame, usage_blame,
104 "show when lines in a file were changed" },
105 { "tree", cmd_tree, usage_tree,
106 "list files and directories in repository" },
107 { "status", cmd_status, usage_status,
108 "show modification status of files" },
109 { "ref", cmd_ref, usage_ref,
110 "manage references in repository" },
111 { "add", cmd_add, usage_add,
112 "add a new file to version control" },
113 { "rm", cmd_rm, usage_rm,
114 "remove a versioned file" },
118 main(int argc, char *argv[])
125 setlocale(LC_CTYPE, "");
127 while ((ch = getopt(argc, argv, "h")) != -1) {
145 signal(SIGINT, catch_sigint);
146 signal(SIGPIPE, catch_sigpipe);
148 for (i = 0; i < nitems(got_commands); i++) {
149 const struct got_error *error;
151 cmd = &got_commands[i];
153 if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
157 got_commands[i].cmd_usage();
159 error = got_commands[i].cmd_main(argc, argv);
160 if (error && !(sigint_received || sigpipe_received)) {
161 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
168 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
177 fprintf(stderr, "usage: %s [-h] command [arg ...]\n\n"
178 "Available commands:\n", getprogname());
179 for (i = 0; i < nitems(got_commands); i++) {
180 struct cmd *cmd = &got_commands[i];
181 fprintf(stderr, " %s: %s\n", cmd->cmd_name, cmd->cmd_descr);
186 static const struct got_error *
187 apply_unveil(const char *repo_path, int repo_read_only,
188 const char *worktree_path)
190 const struct got_error *error;
192 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
193 return got_error_from_errno();
195 if (worktree_path && unveil(worktree_path, "rwc") != 0)
196 return got_error_from_errno();
198 if (unveil("/tmp", "rwc") != 0)
199 return got_error_from_errno();
201 error = got_privsep_unveil_exec_helpers();
205 if (unveil(NULL, NULL) != 0)
206 return got_error_from_errno();
214 fprintf(stderr, "usage: %s checkout [-p prefix] repository-path "
215 "[worktree-path]\n", getprogname());
220 checkout_progress(void *arg, unsigned char status, const char *path)
222 char *worktree_path = arg;
224 while (path[0] == '/')
227 printf("%c %s/%s\n", status, worktree_path, path);
230 static const struct got_error *
231 check_cancelled(void *arg)
233 if (sigint_received || sigpipe_received)
234 return got_error(GOT_ERR_CANCELLED);
238 static const struct got_error *
239 check_ancestry(struct got_worktree *worktree, struct got_object_id *commit_id,
240 struct got_repository *repo)
242 const struct got_error *err;
243 struct got_reference *head_ref = NULL;
244 struct got_object_id *head_commit_id = NULL;
245 struct got_commit_graph *graph = NULL;
247 head_ref = got_worktree_get_head_ref(worktree);
248 if (head_ref == NULL)
249 return got_error_from_errno();
251 /* TODO: Check the reflog. The head ref may have been rebased. */
252 err = got_ref_resolve(&head_commit_id, repo, head_ref);
256 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
260 err = got_commit_graph_iter_start(graph, head_commit_id, repo);
264 struct got_object_id *id;
266 if (sigint_received || sigpipe_received)
269 err = got_commit_graph_iter_next(&id, graph);
271 if (err->code == GOT_ERR_ITER_COMPLETED) {
272 err = got_error(GOT_ERR_ANCESTRY);
275 if (err->code != GOT_ERR_ITER_NEED_MORE)
277 err = got_commit_graph_fetch_commits(graph, 1, repo);
285 if (got_object_id_cmp(id, commit_id) == 0)
290 got_ref_close(head_ref);
292 got_commit_graph_close(graph);
297 static const struct got_error *
298 cmd_checkout(int argc, char *argv[])
300 const struct got_error *error = NULL;
301 struct got_repository *repo = NULL;
302 struct got_reference *head_ref = NULL;
303 struct got_worktree *worktree = NULL;
304 char *repo_path = NULL;
305 char *worktree_path = NULL;
306 const char *path_prefix = "";
307 char *commit_id_str = NULL;
308 int ch, same_path_prefix;
310 while ((ch = getopt(argc, argv, "c:p:")) != -1) {
313 commit_id_str = strdup(optarg);
314 if (commit_id_str == NULL)
315 return got_error_from_errno();
318 path_prefix = optarg;
330 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
331 "unveil", NULL) == -1)
335 char *cwd, *base, *dotgit;
336 repo_path = realpath(argv[0], NULL);
337 if (repo_path == NULL)
338 return got_error_from_errno();
339 cwd = getcwd(NULL, 0);
341 error = got_error_from_errno();
345 base = basename(path_prefix);
347 base = basename(repo_path);
349 error = got_error_from_errno();
352 dotgit = strstr(base, ".git");
355 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
356 error = got_error_from_errno();
361 } else if (argc == 2) {
362 repo_path = realpath(argv[0], NULL);
363 if (repo_path == NULL) {
364 error = got_error_from_errno();
367 worktree_path = realpath(argv[1], NULL);
368 if (worktree_path == NULL) {
369 error = got_error_from_errno();
375 error = apply_unveil(repo_path, 0, worktree_path);
379 error = got_repo_open(&repo, repo_path);
383 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
387 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
388 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
391 error = got_worktree_open(&worktree, worktree_path);
395 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
399 if (!same_path_prefix) {
400 error = got_error(GOT_ERR_PATH_PREFIX);
405 struct got_object_id *commit_id;
406 error = got_object_resolve_id_str(&commit_id, repo,
410 error = check_ancestry(worktree, commit_id, repo);
415 error = got_worktree_set_base_commit_id(worktree, repo,
422 error = got_worktree_checkout_files(worktree, repo,
423 checkout_progress, worktree_path, check_cancelled, NULL);
427 printf("Now shut up and hack\n");
439 fprintf(stderr, "usage: %s update [-c commit] [worktree-path]\n",
445 update_progress(void *arg, unsigned char status, const char *path)
447 int *did_something = arg;
449 if (status == GOT_STATUS_EXISTS)
453 while (path[0] == '/')
455 printf("%c %s\n", status, path);
458 static const struct got_error *
459 cmd_update(int argc, char *argv[])
461 const struct got_error *error = NULL;
462 struct got_repository *repo = NULL;
463 struct got_worktree *worktree = NULL;
464 char *worktree_path = NULL;
465 struct got_object_id *commit_id = NULL;
466 char *commit_id_str = NULL;
467 int ch, did_something = 0;
469 while ((ch = getopt(argc, argv, "c:")) != -1) {
472 commit_id_str = strdup(optarg);
473 if (commit_id_str == NULL)
474 return got_error_from_errno();
486 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
487 "unveil", NULL) == -1)
491 worktree_path = getcwd(NULL, 0);
492 if (worktree_path == NULL) {
493 error = got_error_from_errno();
496 } else if (argc == 1) {
497 worktree_path = realpath(argv[0], NULL);
498 if (worktree_path == NULL) {
499 error = got_error_from_errno();
505 error = got_worktree_open(&worktree, worktree_path);
509 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
513 error = apply_unveil(got_repo_get_path(repo), 0,
514 got_worktree_get_root_path(worktree));
518 if (commit_id_str == NULL) {
519 struct got_reference *head_ref;
520 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
523 error = got_ref_resolve(&commit_id, repo, head_ref);
526 error = got_object_id_str(&commit_id_str, commit_id);
530 error = got_object_resolve_id_str(&commit_id, repo,
536 error = check_ancestry(worktree, commit_id, repo);
540 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
542 error = got_worktree_set_base_commit_id(worktree, repo,
548 error = got_worktree_checkout_files(worktree, repo,
549 update_progress, &did_something, check_cancelled, NULL);
554 printf("Updated to commit %s\n", commit_id_str);
556 printf("Already up-to-date\n");
564 static const struct got_error *
565 print_patch(struct got_commit_object *commit, struct got_object_id *id,
566 int diff_context, struct got_repository *repo)
568 const struct got_error *err = NULL;
569 struct got_tree_object *tree1 = NULL, *tree2;
570 struct got_object_qid *qid;
571 char *id_str1 = NULL, *id_str2;
573 err = got_object_open_as_tree(&tree2, repo,
574 got_object_commit_get_tree_id(commit));
578 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
580 struct got_commit_object *pcommit;
582 err = got_object_open_as_commit(&pcommit, repo, qid->id);
586 err = got_object_open_as_tree(&tree1, repo,
587 got_object_commit_get_tree_id(pcommit));
588 got_object_commit_close(pcommit);
592 err = got_object_id_str(&id_str1, qid->id);
597 err = got_object_id_str(&id_str2, id);
601 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
602 err = got_diff_tree(tree1, tree2, "", "", diff_context, repo, stdout);
605 got_object_tree_close(tree1);
606 got_object_tree_close(tree2);
613 get_datestr(time_t *time, char *datebuf)
615 char *p, *s = ctime_r(time, datebuf);
622 static const struct got_error *
623 print_commit(struct got_commit_object *commit, struct got_object_id *id,
624 struct got_repository *repo, int show_patch, int diff_context,
625 struct got_reflist_head *refs)
627 const struct got_error *err = NULL;
628 char *id_str, *datestr, *logmsg0, *logmsg, *line;
630 time_t committer_time;
631 const char *author, *committer;
632 char *refs_str = NULL;
633 struct got_reflist_entry *re;
635 SIMPLEQ_FOREACH(re, refs, entry) {
638 if (got_object_id_cmp(re->id, id) != 0)
640 name = got_ref_get_name(re->ref);
641 if (strcmp(name, GOT_REF_HEAD) == 0)
643 if (strncmp(name, "refs/", 5) == 0)
645 if (strncmp(name, "got/", 4) == 0)
647 if (strncmp(name, "heads/", 6) == 0)
649 if (strncmp(name, "remotes/", 8) == 0)
652 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
654 err = got_error_from_errno();
660 err = got_object_id_str(&id_str, id);
664 printf("-----------------------------------------------\n");
665 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
666 refs_str ? refs_str : "", refs_str ? ")" : "");
671 printf("from: %s\n", got_object_commit_get_author(commit));
672 committer_time = got_object_commit_get_committer_time(commit);
673 datestr = get_datestr(&committer_time, datebuf);
674 printf("date: %s UTC\n", datestr);
675 author = got_object_commit_get_author(commit);
676 committer = got_object_commit_get_committer(commit);
677 if (strcmp(author, committer) != 0)
678 printf("via: %s\n", committer);
679 if (got_object_commit_get_nparents(commit) > 1) {
680 const struct got_object_id_queue *parent_ids;
681 struct got_object_qid *qid;
683 parent_ids = got_object_commit_get_parent_ids(commit);
684 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
685 err = got_object_id_str(&id_str, qid->id);
688 printf("parent %d: %s\n", n++, id_str);
693 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
695 return got_error_from_errno();
699 line = strsep(&logmsg, "\n");
701 printf(" %s\n", line);
706 err = print_patch(commit, id, diff_context, repo);
711 if (fflush(stdout) != 0 && err == NULL)
712 err = got_error_from_errno();
716 static const struct got_error *
717 print_commits(struct got_object_id *root_id, struct got_repository *repo,
718 char *path, int show_patch, int diff_context, int limit,
719 int first_parent_traversal, struct got_reflist_head *refs)
721 const struct got_error *err;
722 struct got_commit_graph *graph;
724 err = got_commit_graph_open(&graph, root_id, path,
725 first_parent_traversal, repo);
728 err = got_commit_graph_iter_start(graph, root_id, repo);
732 struct got_commit_object *commit;
733 struct got_object_id *id;
735 if (sigint_received || sigpipe_received)
738 err = got_commit_graph_iter_next(&id, graph);
740 if (err->code == GOT_ERR_ITER_COMPLETED) {
744 if (err->code != GOT_ERR_ITER_NEED_MORE)
746 err = got_commit_graph_fetch_commits(graph, 1, repo);
755 err = got_object_open_as_commit(&commit, repo, id);
758 err = print_commit(commit, id, repo, show_patch, diff_context,
760 got_object_commit_close(commit);
761 if (err || (limit && --limit == 0))
765 got_commit_graph_close(graph);
772 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
773 "[-r repository-path] [path]\n", getprogname());
777 static const struct got_error *
778 cmd_log(int argc, char *argv[])
780 const struct got_error *error;
781 struct got_repository *repo = NULL;
782 struct got_worktree *worktree = NULL;
783 struct got_commit_object *commit = NULL;
784 struct got_object_id *id = NULL;
785 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
786 char *start_commit = NULL;
787 int diff_context = 3, ch;
788 int show_patch = 0, limit = 0, first_parent_traversal = 0;
790 struct got_reflist_head refs;
795 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
801 while ((ch = getopt(argc, argv, "pc:C:l:fr:")) != -1) {
807 start_commit = optarg;
810 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
813 err(1, "-C option %s", errstr);
816 limit = strtonum(optarg, 1, INT_MAX, &errstr);
818 err(1, "-l option %s", errstr);
821 first_parent_traversal = 1;
824 repo_path = realpath(optarg, NULL);
825 if (repo_path == NULL)
837 cwd = getcwd(NULL, 0);
839 error = got_error_from_errno();
843 error = got_worktree_open(&worktree, cwd);
844 if (error && error->code != GOT_ERR_NOT_WORKTREE)
851 error = got_error_from_errno();
854 } else if (argc == 1) {
856 error = got_worktree_resolve_path(&path, worktree,
861 path = strdup(argv[0]);
863 error = got_error_from_errno();
870 repo_path = worktree ?
871 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
872 if (repo_path == NULL) {
873 error = got_error_from_errno();
877 error = apply_unveil(repo_path, 1,
878 worktree ? got_worktree_get_root_path(worktree) : NULL);
882 error = got_repo_open(&repo, repo_path);
886 if (start_commit == NULL) {
887 struct got_reference *head_ref;
888 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
891 error = got_ref_resolve(&id, repo, head_ref);
892 got_ref_close(head_ref);
895 error = got_object_open_as_commit(&commit, repo, id);
897 struct got_reference *ref;
898 error = got_ref_open(&ref, repo, start_commit);
901 error = got_ref_resolve(&id, repo, ref);
905 error = got_object_get_type(&obj_type, repo, id);
908 if (obj_type == GOT_OBJ_TYPE_TAG) {
909 struct got_tag_object *tag;
910 error = got_object_open_as_tag(&tag, repo, id);
913 if (got_object_tag_get_object_type(tag) !=
914 GOT_OBJ_TYPE_COMMIT) {
915 got_object_tag_close(tag);
916 error = got_error(GOT_ERR_OBJ_TYPE);
920 id = got_object_id_dup(
921 got_object_tag_get_object_id(tag));
923 error = got_error_from_errno();
924 got_object_tag_close(tag);
927 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
928 error = got_error(GOT_ERR_OBJ_TYPE);
931 error = got_object_open_as_commit(&commit, repo, id);
935 if (commit == NULL) {
936 error = got_object_resolve_id_str(&id, repo,
945 error = got_repo_map_path(&in_repo_path, repo, path, 1);
953 error = got_ref_list(&refs, repo);
957 error = print_commits(id, repo, path, show_patch,
958 diff_context, limit, first_parent_traversal, &refs);
965 got_worktree_close(worktree);
967 const struct got_error *repo_error;
968 repo_error = got_repo_close(repo);
972 got_ref_list_free(&refs);
979 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
980 "[object1 object2 | path]\n", getprogname());
984 struct print_diff_arg {
985 struct got_repository *repo;
986 struct got_worktree *worktree;
992 static const struct got_error *
993 print_diff(void *arg, unsigned char status, const char *path,
994 struct got_object_id *id)
996 struct print_diff_arg *a = arg;
997 const struct got_error *err = NULL;
998 struct got_blob_object *blob1 = NULL;
1000 char *abspath = NULL;
1003 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1004 status != GOT_STATUS_DELETE)
1007 if (!a->header_shown) {
1008 printf("diff %s %s\n", a->id_str,
1009 got_worktree_get_root_path(a->worktree));
1010 a->header_shown = 1;
1013 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_DELETE) {
1014 err = got_object_open_as_blob(&blob1, a->repo, id, 8192);
1020 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1021 if (asprintf(&abspath, "%s/%s",
1022 got_worktree_get_root_path(a->worktree), path) == -1) {
1023 err = got_error_from_errno();
1027 f2 = fopen(abspath, "r");
1029 err = got_error_from_errno();
1032 if (lstat(abspath, &sb) == -1) {
1033 err = got_error_from_errno();
1039 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1043 got_object_blob_close(blob1);
1044 if (f2 && fclose(f2) != 0 && err == NULL)
1045 err = got_error_from_errno();
1050 static const struct got_error *
1051 cmd_diff(int argc, char *argv[])
1053 const struct got_error *error;
1054 struct got_repository *repo = NULL;
1055 struct got_worktree *worktree = NULL;
1056 char *cwd = NULL, *repo_path = NULL;
1057 struct got_object_id *id1 = NULL, *id2 = NULL;
1058 char *id_str1 = NULL, *id_str2 = NULL;
1060 int diff_context = 3, ch;
1065 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1070 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1073 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1075 err(1, "-C option %s", errstr);
1078 repo_path = realpath(optarg, NULL);
1079 if (repo_path == NULL)
1080 err(1, "-r option");
1091 cwd = getcwd(NULL, 0);
1093 error = got_error_from_errno();
1096 error = got_worktree_open(&worktree, cwd);
1097 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1100 if (worktree == NULL) {
1101 error = got_error(GOT_ERR_NOT_WORKTREE);
1106 "-r option can't be used when diffing a work tree");
1107 repo_path = strdup(got_worktree_get_repo_path(worktree));
1108 if (repo_path == NULL) {
1109 error = got_error_from_errno();
1113 error = got_worktree_resolve_path(&path, worktree,
1120 error = got_error_from_errno();
1124 } else if (argc == 2) {
1130 if (repo_path == NULL) {
1131 repo_path = getcwd(NULL, 0);
1132 if (repo_path == NULL)
1133 return got_error_from_errno();
1136 error = apply_unveil(repo_path, 1,
1137 worktree ? got_worktree_get_root_path(worktree) : NULL);
1141 error = got_repo_open(&repo, repo_path);
1147 struct print_diff_arg arg;
1149 error = got_object_id_str(&id_str,
1150 got_worktree_get_base_commit_id(worktree));
1154 arg.worktree = worktree;
1155 arg.diff_context = diff_context;
1156 arg.id_str = id_str;
1157 arg.header_shown = 0;
1159 error = got_worktree_status(worktree, path, repo, print_diff,
1160 &arg, check_cancelled, NULL);
1165 error = got_object_resolve_id_str(&id1, repo, id_str1);
1169 error = got_object_resolve_id_str(&id2, repo, id_str2);
1173 error = got_object_get_type(&type1, repo, id1);
1177 error = got_object_get_type(&type2, repo, id2);
1181 if (type1 != type2) {
1182 error = got_error(GOT_ERR_OBJ_TYPE);
1187 case GOT_OBJ_TYPE_BLOB:
1188 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1189 diff_context, repo, stdout);
1191 case GOT_OBJ_TYPE_TREE:
1192 error = got_diff_objects_as_trees(id1, id2, "", "",
1193 diff_context, repo, stdout);
1195 case GOT_OBJ_TYPE_COMMIT:
1196 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
1198 error = got_diff_objects_as_commits(id1, id2, diff_context,
1202 error = got_error(GOT_ERR_OBJ_TYPE);
1210 got_worktree_close(worktree);
1212 const struct got_error *repo_error;
1213 repo_error = got_repo_close(repo);
1224 "usage: %s blame [-c commit] [-r repository-path] path\n",
1229 static const struct got_error *
1230 cmd_blame(int argc, char *argv[])
1232 const struct got_error *error;
1233 struct got_repository *repo = NULL;
1234 struct got_worktree *worktree = NULL;
1235 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1236 struct got_object_id *commit_id = NULL;
1237 char *commit_id_str = NULL;
1241 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1246 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1249 commit_id_str = optarg;
1252 repo_path = realpath(optarg, NULL);
1253 if (repo_path == NULL)
1254 err(1, "-r option");
1270 cwd = getcwd(NULL, 0);
1272 error = got_error_from_errno();
1275 if (repo_path == NULL) {
1276 error = got_worktree_open(&worktree, cwd);
1277 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1283 strdup(got_worktree_get_repo_path(worktree));
1284 if (repo_path == NULL)
1285 error = got_error_from_errno();
1289 repo_path = strdup(cwd);
1290 if (repo_path == NULL) {
1291 error = got_error_from_errno();
1297 error = apply_unveil(repo_path, 1, NULL);
1301 error = got_repo_open(&repo, repo_path);
1306 const char *prefix = got_worktree_get_path_prefix(worktree);
1307 char *p, *worktree_subdir = cwd +
1308 strlen(got_worktree_get_root_path(worktree));
1309 if (asprintf(&p, "%s%s%s%s%s",
1310 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1311 worktree_subdir, worktree_subdir[0] ? "/" : "",
1313 error = got_error_from_errno();
1316 error = got_repo_map_path(&in_repo_path, repo, p, 0);
1319 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1324 if (commit_id_str == NULL) {
1325 struct got_reference *head_ref;
1326 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1329 error = got_ref_resolve(&commit_id, repo, head_ref);
1330 got_ref_close(head_ref);
1334 error = got_object_resolve_id_str(&commit_id, repo,
1340 error = got_blame(in_repo_path, commit_id, repo, stdout);
1347 got_worktree_close(worktree);
1349 const struct got_error *repo_error;
1350 repo_error = got_repo_close(repo);
1361 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1367 print_entry(struct got_tree_entry *te, const char *id, const char *path,
1368 const char *root_path)
1370 int is_root_path = (strcmp(path, root_path) == 0);
1372 path += strlen(root_path);
1373 while (path[0] == '/')
1376 printf("%s%s%s%s%s\n", id ? id : "", path,
1377 is_root_path ? "" : "/", te->name,
1378 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
1381 static const struct got_error *
1382 print_tree(const char *path, struct got_object_id *commit_id,
1383 int show_ids, int recurse, const char *root_path,
1384 struct got_repository *repo)
1386 const struct got_error *err = NULL;
1387 struct got_object_id *tree_id = NULL;
1388 struct got_tree_object *tree = NULL;
1389 const struct got_tree_entries *entries;
1390 struct got_tree_entry *te;
1392 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1396 err = got_object_open_as_tree(&tree, repo, tree_id);
1399 entries = got_object_tree_get_entries(tree);
1400 te = SIMPLEQ_FIRST(&entries->head);
1404 if (sigint_received || sigpipe_received)
1409 err = got_object_id_str(&id_str, te->id);
1412 if (asprintf(&id, "%s ", id_str) == -1) {
1413 err = got_error_from_errno();
1419 print_entry(te, id, path, root_path);
1422 if (recurse && S_ISDIR(te->mode)) {
1424 if (asprintf(&child_path, "%s%s%s", path,
1425 path[0] == '/' && path[1] == '\0' ? "" : "/",
1427 err = got_error_from_errno();
1430 err = print_tree(child_path, commit_id, show_ids, 1,
1437 te = SIMPLEQ_NEXT(te, entry);
1441 got_object_tree_close(tree);
1446 static const struct got_error *
1447 cmd_tree(int argc, char *argv[])
1449 const struct got_error *error;
1450 struct got_repository *repo = NULL;
1451 struct got_worktree *worktree = NULL;
1453 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1454 struct got_object_id *commit_id = NULL;
1455 char *commit_id_str = NULL;
1456 int show_ids = 0, recurse = 0;
1460 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1465 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1468 commit_id_str = optarg;
1471 repo_path = realpath(optarg, NULL);
1472 if (repo_path == NULL)
1473 err(1, "-r option");
1497 cwd = getcwd(NULL, 0);
1499 error = got_error_from_errno();
1502 if (repo_path == NULL) {
1503 error = got_worktree_open(&worktree, cwd);
1504 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1510 strdup(got_worktree_get_repo_path(worktree));
1511 if (repo_path == NULL)
1512 error = got_error_from_errno();
1516 repo_path = strdup(cwd);
1517 if (repo_path == NULL) {
1518 error = got_error_from_errno();
1524 error = apply_unveil(repo_path, 1, NULL);
1528 error = got_repo_open(&repo, repo_path);
1534 char *p, *worktree_subdir = cwd +
1535 strlen(got_worktree_get_root_path(worktree));
1536 if (asprintf(&p, "%s/%s",
1537 got_worktree_get_path_prefix(worktree),
1538 worktree_subdir) == -1) {
1539 error = got_error_from_errno();
1542 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1549 if (in_repo_path == NULL) {
1550 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1555 if (commit_id_str == NULL) {
1556 struct got_reference *head_ref;
1557 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1560 error = got_ref_resolve(&commit_id, repo, head_ref);
1561 got_ref_close(head_ref);
1565 error = got_object_resolve_id_str(&commit_id, repo,
1571 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1572 in_repo_path, repo);
1579 got_worktree_close(worktree);
1581 const struct got_error *repo_error;
1582 repo_error = got_repo_close(repo);
1592 fprintf(stderr, "usage: %s status [path]\n", getprogname());
1596 static const struct got_error *
1597 print_status(void *arg, unsigned char status, const char *path,
1598 struct got_object_id *id)
1600 printf("%c %s\n", status, path);
1604 static const struct got_error *
1605 cmd_status(int argc, char *argv[])
1607 const struct got_error *error = NULL;
1608 struct got_repository *repo = NULL;
1609 struct got_worktree *worktree = NULL;
1610 char *cwd = NULL, *path = NULL;
1613 while ((ch = getopt(argc, argv, "")) != -1) {
1625 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1629 cwd = getcwd(NULL, 0);
1631 error = got_error_from_errno();
1635 error = got_worktree_open(&worktree, cwd);
1642 error = got_error_from_errno();
1645 } else if (argc == 1) {
1646 error = got_worktree_resolve_path(&path, worktree, argv[0]);
1652 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1656 error = apply_unveil(got_repo_get_path(repo), 1,
1657 got_worktree_get_root_path(worktree));
1661 error = got_worktree_status(worktree, path, repo, print_status, NULL,
1662 check_cancelled, NULL);
1673 "usage: %s ref [-r repository] -l | -d name | name object\n",
1678 static const struct got_error *
1679 list_refs(struct got_repository *repo)
1681 static const struct got_error *err = NULL;
1682 struct got_reflist_head refs;
1683 struct got_reflist_entry *re;
1685 SIMPLEQ_INIT(&refs);
1686 err = got_ref_list(&refs, repo);
1690 SIMPLEQ_FOREACH(re, &refs, entry) {
1692 refstr = got_ref_to_str(re->ref);
1694 return got_error_from_errno();
1695 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
1699 got_ref_list_free(&refs);
1703 static const struct got_error *
1704 delete_ref(struct got_repository *repo, const char *refname)
1706 const struct got_error *err = NULL;
1707 struct got_reference *ref;
1709 err = got_ref_open(&ref, repo, refname);
1713 err = got_ref_delete(ref, repo);
1718 static const struct got_error *
1719 add_ref(struct got_repository *repo, const char *refname, const char *id_str)
1721 const struct got_error *err = NULL;
1722 struct got_object_id *id;
1723 struct got_reference *ref = NULL;
1725 err = got_object_resolve_id_str(&id, repo, id_str);
1729 err = got_ref_alloc(&ref, refname, id);
1733 err = got_ref_write(ref, repo);
1741 static const struct got_error *
1742 cmd_ref(int argc, char *argv[])
1744 const struct got_error *error = NULL;
1745 struct got_repository *repo = NULL;
1746 struct got_worktree *worktree = NULL;
1747 char *cwd = NULL, *repo_path = NULL;
1748 int ch, do_list = 0;
1749 const char *delref = NULL;
1751 /* TODO: Add -s option for adding symbolic references. */
1752 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
1758 repo_path = realpath(optarg, NULL);
1759 if (repo_path == NULL)
1760 err(1, "-r option");
1771 if (do_list && delref)
1772 errx(1, "-l and -d options are mutually exclusive\n");
1777 if (do_list || delref) {
1780 } else if (argc != 2)
1785 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
1789 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1790 "sendfd unveil", NULL) == -1)
1794 cwd = getcwd(NULL, 0);
1796 error = got_error_from_errno();
1800 if (repo_path == NULL) {
1801 error = got_worktree_open(&worktree, cwd);
1802 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1808 strdup(got_worktree_get_repo_path(worktree));
1809 if (repo_path == NULL)
1810 error = got_error_from_errno();
1814 repo_path = strdup(cwd);
1815 if (repo_path == NULL) {
1816 error = got_error_from_errno();
1822 error = apply_unveil(repo_path, do_list,
1823 worktree ? got_worktree_get_root_path(worktree) : NULL);
1827 error = got_repo_open(&repo, repo_path);
1832 error = list_refs(repo);
1834 error = delete_ref(repo, delref);
1836 error = add_ref(repo, argv[0], argv[1]);
1839 got_repo_close(repo);
1841 got_worktree_close(worktree);
1850 fprintf(stderr, "usage: %s add file-path\n", getprogname());
1854 static const struct got_error *
1855 cmd_add(int argc, char *argv[])
1857 const struct got_error *error = NULL;
1858 struct got_repository *repo = NULL;
1859 struct got_worktree *worktree = NULL;
1860 char *cwd = NULL, *path = NULL, *relpath = NULL;
1863 while ((ch = getopt(argc, argv, "")) != -1) {
1877 path = realpath(argv[0], NULL);
1879 error = got_error_from_errno();
1883 cwd = getcwd(NULL, 0);
1885 error = got_error_from_errno();
1888 error = got_worktree_open(&worktree, cwd);
1892 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1896 error = apply_unveil(got_repo_get_path(repo), 1,
1897 got_worktree_get_root_path(worktree));
1901 error = got_worktree_schedule_add(worktree, path, print_status, NULL,
1907 got_repo_close(repo);
1909 got_worktree_close(worktree);
1919 fprintf(stderr, "usage: %s rm [-f] file-path\n", getprogname());
1923 static const struct got_error *
1924 cmd_rm(int argc, char *argv[])
1926 const struct got_error *error = NULL;
1927 struct got_worktree *worktree = NULL;
1928 struct got_repository *repo = NULL;
1929 char *cwd = NULL, *path = NULL;
1930 int ch, delete_local_mods = 0;
1932 while ((ch = getopt(argc, argv, "f")) != -1) {
1935 delete_local_mods = 1;
1949 path = realpath(argv[0], NULL);
1951 error = got_error_from_errno();
1955 cwd = getcwd(NULL, 0);
1957 error = got_error_from_errno();
1960 error = got_worktree_open(&worktree, cwd);
1964 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1968 error = apply_unveil(got_repo_get_path(repo), 1,
1969 got_worktree_get_root_path(worktree));
1973 error = got_worktree_schedule_delete(worktree, path, delete_local_mods,
1974 print_status, NULL, repo);
1979 got_repo_close(repo);
1981 got_worktree_close(worktree);