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>
22 #include <sys/param.h>
35 #include "got_error.h"
36 #include "got_object.h"
37 #include "got_reference.h"
38 #include "got_repository.h"
40 #include "got_worktree.h"
42 #include "got_commit_graph.h"
43 #include "got_blame.h"
44 #include "got_privsep.h"
47 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
50 static volatile sig_atomic_t sigint_received;
51 static volatile sig_atomic_t sigpipe_received;
54 catch_sigint(int signo)
60 catch_sigpipe(int signo)
68 const struct got_error *(*cmd_main)(int, char *[]);
69 void (*cmd_usage)(void);
70 const char *cmd_descr;
73 __dead static void usage(void);
74 __dead static void usage_checkout(void);
75 __dead static void usage_update(void);
76 __dead static void usage_log(void);
77 __dead static void usage_diff(void);
78 __dead static void usage_blame(void);
79 __dead static void usage_tree(void);
80 __dead static void usage_status(void);
81 __dead static void usage_ref(void);
82 __dead static void usage_add(void);
83 __dead static void usage_rm(void);
84 __dead static void usage_revert(void);
85 __dead static void usage_commit(void);
87 static const struct got_error* cmd_checkout(int, char *[]);
88 static const struct got_error* cmd_update(int, char *[]);
89 static const struct got_error* cmd_log(int, char *[]);
90 static const struct got_error* cmd_diff(int, char *[]);
91 static const struct got_error* cmd_blame(int, char *[]);
92 static const struct got_error* cmd_tree(int, char *[]);
93 static const struct got_error* cmd_status(int, char *[]);
94 static const struct got_error* cmd_ref(int, char *[]);
95 static const struct got_error* cmd_add(int, char *[]);
96 static const struct got_error* cmd_rm(int, char *[]);
97 static const struct got_error* cmd_revert(int, char *[]);
98 static const struct got_error* cmd_commit(int, char *[]);
100 static struct cmd got_commands[] = {
101 { "checkout", cmd_checkout, usage_checkout,
102 "check out a new work tree from a repository" },
103 { "update", cmd_update, usage_update,
104 "update a work tree to a different commit" },
105 { "log", cmd_log, usage_log,
106 "show repository history" },
107 { "diff", cmd_diff, usage_diff,
108 "compare files and directories" },
109 { "blame", cmd_blame, usage_blame,
110 "show when lines in a file were changed" },
111 { "tree", cmd_tree, usage_tree,
112 "list files and directories in repository" },
113 { "status", cmd_status, usage_status,
114 "show modification status of files" },
115 { "ref", cmd_ref, usage_ref,
116 "manage references in repository" },
117 { "add", cmd_add, usage_add,
118 "add a new file to version control" },
119 { "rm", cmd_rm, usage_rm,
120 "remove a versioned file" },
121 { "revert", cmd_revert, usage_revert,
122 "revert uncommitted changes" },
123 { "commit", cmd_commit, usage_commit,
124 "write changes from work tree to repository" },
128 main(int argc, char *argv[])
135 setlocale(LC_CTYPE, "");
137 while ((ch = getopt(argc, argv, "h")) != -1) {
155 signal(SIGINT, catch_sigint);
156 signal(SIGPIPE, catch_sigpipe);
158 for (i = 0; i < nitems(got_commands); i++) {
159 const struct got_error *error;
161 cmd = &got_commands[i];
163 if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
167 got_commands[i].cmd_usage();
169 error = got_commands[i].cmd_main(argc, argv);
170 if (error && !(sigint_received || sigpipe_received)) {
171 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
178 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
187 fprintf(stderr, "usage: %s [-h] command [arg ...]\n\n"
188 "Available commands:\n", getprogname());
189 for (i = 0; i < nitems(got_commands); i++) {
190 struct cmd *cmd = &got_commands[i];
191 fprintf(stderr, " %s: %s\n", cmd->cmd_name, cmd->cmd_descr);
196 static const struct got_error *
197 apply_unveil(const char *repo_path, int repo_read_only,
198 const char *worktree_path, int create_worktree)
200 const struct got_error *error;
201 static char err_msg[MAXPATHLEN + 36];
203 if (create_worktree) {
204 /* Pre-create work tree path to avoid unveiling its parents. */
205 error = got_path_mkdir(worktree_path);
207 if (errno == EEXIST) {
208 if (got_path_dir_is_empty(worktree_path)) {
212 snprintf(err_msg, sizeof(err_msg),
213 "%s: directory exists but is not empty",
215 error = got_error_msg(GOT_ERR_BAD_PATH,
220 if (error && (error->code != GOT_ERR_ERRNO || errno != EISDIR))
224 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
225 return got_error_prefix_errno2("unveil", repo_path);
227 if (worktree_path && unveil(worktree_path, "rwc") != 0)
228 return got_error_prefix_errno2("unveil", worktree_path);
230 if (unveil("/tmp", "rwc") != 0)
231 return got_error_prefix_errno2("unveil", "/tmp");
233 error = got_privsep_unveil_exec_helpers();
237 if (unveil(NULL, NULL) != 0)
238 return got_error_prefix_errno("unveil");
246 fprintf(stderr, "usage: %s checkout [-p prefix] repository-path "
247 "[worktree-path]\n", getprogname());
252 checkout_progress(void *arg, unsigned char status, const char *path)
254 char *worktree_path = arg;
256 while (path[0] == '/')
259 printf("%c %s/%s\n", status, worktree_path, path);
262 static const struct got_error *
263 check_cancelled(void *arg)
265 if (sigint_received || sigpipe_received)
266 return got_error(GOT_ERR_CANCELLED);
270 static const struct got_error *
271 check_ancestry(struct got_worktree *worktree, struct got_object_id *commit_id,
272 struct got_repository *repo)
274 const struct got_error *err;
275 struct got_reference *head_ref = NULL;
276 struct got_object_id *head_commit_id = NULL;
277 struct got_commit_graph *graph = NULL;
279 err = got_ref_open(&head_ref, repo,
280 got_worktree_get_head_ref_name(worktree), 0);
284 /* TODO: Check the reflog. The head ref may have been rebased. */
285 err = got_ref_resolve(&head_commit_id, repo, head_ref);
289 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
293 err = got_commit_graph_iter_start(graph, head_commit_id, repo);
297 struct got_object_id *id;
299 if (sigint_received || sigpipe_received)
302 err = got_commit_graph_iter_next(&id, graph);
304 if (err->code == GOT_ERR_ITER_COMPLETED) {
305 err = got_error(GOT_ERR_ANCESTRY);
308 if (err->code != GOT_ERR_ITER_NEED_MORE)
310 err = got_commit_graph_fetch_commits(graph, 1, repo);
318 if (got_object_id_cmp(id, commit_id) == 0)
323 got_ref_close(head_ref);
325 got_commit_graph_close(graph);
330 static const struct got_error *
331 cmd_checkout(int argc, char *argv[])
333 const struct got_error *error = NULL;
334 struct got_repository *repo = NULL;
335 struct got_reference *head_ref = NULL;
336 struct got_worktree *worktree = NULL;
337 char *repo_path = NULL;
338 char *worktree_path = NULL;
339 const char *path_prefix = "";
340 char *commit_id_str = NULL;
341 int ch, same_path_prefix;
343 while ((ch = getopt(argc, argv, "c:p:")) != -1) {
346 commit_id_str = strdup(optarg);
347 if (commit_id_str == NULL)
348 return got_error_prefix_errno("strdup");
351 path_prefix = optarg;
363 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
364 "unveil", NULL) == -1)
368 char *cwd, *base, *dotgit;
369 repo_path = realpath(argv[0], NULL);
370 if (repo_path == NULL)
371 return got_error_prefix_errno2("realpath", argv[0]);
372 cwd = getcwd(NULL, 0);
374 error = got_error_prefix_errno("getcwd");
377 if (path_prefix[0]) {
378 base = basename(path_prefix);
380 error = got_error_prefix_errno2("basename",
385 base = basename(repo_path);
387 error = got_error_prefix_errno2("basename",
392 dotgit = strstr(base, ".git");
395 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
396 error = got_error_prefix_errno("asprintf");
401 } else if (argc == 2) {
402 repo_path = realpath(argv[0], NULL);
403 if (repo_path == NULL) {
404 error = got_error_prefix_errno2("realpath", argv[0]);
407 worktree_path = realpath(argv[1], NULL);
408 if (worktree_path == NULL) {
409 error = got_error_prefix_errno2("realpath", argv[1]);
415 got_path_strip_trailing_slashes(repo_path);
416 got_path_strip_trailing_slashes(worktree_path);
418 error = got_repo_open(&repo, repo_path);
422 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path, 1);
426 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
430 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
431 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
434 error = got_worktree_open(&worktree, worktree_path);
438 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
442 if (!same_path_prefix) {
443 error = got_error(GOT_ERR_PATH_PREFIX);
448 struct got_object_id *commit_id;
449 error = got_object_resolve_id_str(&commit_id, repo,
453 error = check_ancestry(worktree, commit_id, repo);
458 error = got_worktree_set_base_commit_id(worktree, repo,
465 error = got_worktree_checkout_files(worktree, "", repo,
466 checkout_progress, worktree_path, check_cancelled, NULL);
470 printf("Now shut up and hack\n");
482 fprintf(stderr, "usage: %s update [-c commit] [path]\n",
488 update_progress(void *arg, unsigned char status, const char *path)
490 int *did_something = arg;
492 if (status == GOT_STATUS_EXISTS)
496 while (path[0] == '/')
498 printf("%c %s\n", status, path);
501 static const struct got_error *
502 cmd_update(int argc, char *argv[])
504 const struct got_error *error = NULL;
505 struct got_repository *repo = NULL;
506 struct got_worktree *worktree = NULL;
507 char *worktree_path = NULL, *path = NULL;
508 struct got_object_id *commit_id = NULL;
509 char *commit_id_str = NULL;
510 int ch, did_something = 0;
512 while ((ch = getopt(argc, argv, "c:")) != -1) {
515 commit_id_str = strdup(optarg);
516 if (commit_id_str == NULL)
517 return got_error_prefix_errno("strdup");
529 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
530 "unveil", NULL) == -1)
533 worktree_path = getcwd(NULL, 0);
534 if (worktree_path == NULL) {
535 error = got_error_prefix_errno("getcwd");
538 error = got_worktree_open(&worktree, worktree_path);
545 error = got_error_prefix_errno("strdup");
548 } else if (argc == 1) {
549 error = got_worktree_resolve_path(&path, worktree, argv[0]);
555 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
559 error = apply_unveil(got_repo_get_path(repo), 0,
560 got_worktree_get_root_path(worktree), 0);
564 if (commit_id_str == NULL) {
565 struct got_reference *head_ref;
566 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
569 error = got_ref_resolve(&commit_id, repo, head_ref);
572 error = got_object_id_str(&commit_id_str, commit_id);
576 error = got_object_resolve_id_str(&commit_id, repo,
582 error = check_ancestry(worktree, commit_id, repo);
586 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
588 error = got_worktree_set_base_commit_id(worktree, repo,
594 error = got_worktree_checkout_files(worktree, path, repo,
595 update_progress, &did_something, check_cancelled, NULL);
600 printf("Updated to commit %s\n", commit_id_str);
602 printf("Already up-to-date\n");
611 static const struct got_error *
612 print_patch(struct got_commit_object *commit, struct got_object_id *id,
613 int diff_context, struct got_repository *repo)
615 const struct got_error *err = NULL;
616 struct got_tree_object *tree1 = NULL, *tree2;
617 struct got_object_qid *qid;
618 char *id_str1 = NULL, *id_str2;
620 err = got_object_open_as_tree(&tree2, repo,
621 got_object_commit_get_tree_id(commit));
625 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
627 struct got_commit_object *pcommit;
629 err = got_object_open_as_commit(&pcommit, repo, qid->id);
633 err = got_object_open_as_tree(&tree1, repo,
634 got_object_commit_get_tree_id(pcommit));
635 got_object_commit_close(pcommit);
639 err = got_object_id_str(&id_str1, qid->id);
644 err = got_object_id_str(&id_str2, id);
648 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
649 err = got_diff_tree(tree1, tree2, "", "", diff_context, repo, stdout);
652 got_object_tree_close(tree1);
653 got_object_tree_close(tree2);
660 get_datestr(time_t *time, char *datebuf)
662 char *p, *s = ctime_r(time, datebuf);
669 static const struct got_error *
670 print_commit(struct got_commit_object *commit, struct got_object_id *id,
671 struct got_repository *repo, int show_patch, int diff_context,
672 struct got_reflist_head *refs)
674 const struct got_error *err = NULL;
675 char *id_str, *datestr, *logmsg0, *logmsg, *line;
677 time_t committer_time;
678 const char *author, *committer;
679 char *refs_str = NULL;
680 struct got_reflist_entry *re;
682 SIMPLEQ_FOREACH(re, refs, entry) {
685 if (got_object_id_cmp(re->id, id) != 0)
687 name = got_ref_get_name(re->ref);
688 if (strcmp(name, GOT_REF_HEAD) == 0)
690 if (strncmp(name, "refs/", 5) == 0)
692 if (strncmp(name, "got/", 4) == 0)
694 if (strncmp(name, "heads/", 6) == 0)
696 if (strncmp(name, "remotes/", 8) == 0)
699 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
701 err = got_error_prefix_errno("asprintf");
707 err = got_object_id_str(&id_str, id);
711 printf("-----------------------------------------------\n");
712 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
713 refs_str ? refs_str : "", refs_str ? ")" : "");
718 printf("from: %s\n", got_object_commit_get_author(commit));
719 committer_time = got_object_commit_get_committer_time(commit);
720 datestr = get_datestr(&committer_time, datebuf);
721 printf("date: %s UTC\n", datestr);
722 author = got_object_commit_get_author(commit);
723 committer = got_object_commit_get_committer(commit);
724 if (strcmp(author, committer) != 0)
725 printf("via: %s\n", committer);
726 if (got_object_commit_get_nparents(commit) > 1) {
727 const struct got_object_id_queue *parent_ids;
728 struct got_object_qid *qid;
730 parent_ids = got_object_commit_get_parent_ids(commit);
731 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
732 err = got_object_id_str(&id_str, qid->id);
735 printf("parent %d: %s\n", n++, id_str);
740 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
742 return got_error_prefix_errno("strdup");
746 line = strsep(&logmsg, "\n");
748 printf(" %s\n", line);
753 err = print_patch(commit, id, diff_context, repo);
758 if (fflush(stdout) != 0 && err == NULL)
759 err = got_error_prefix_errno("fflush");
763 static const struct got_error *
764 print_commits(struct got_object_id *root_id, struct got_repository *repo,
765 char *path, int show_patch, int diff_context, int limit,
766 int first_parent_traversal, struct got_reflist_head *refs)
768 const struct got_error *err;
769 struct got_commit_graph *graph;
771 err = got_commit_graph_open(&graph, root_id, path,
772 first_parent_traversal, repo);
775 err = got_commit_graph_iter_start(graph, root_id, repo);
779 struct got_commit_object *commit;
780 struct got_object_id *id;
782 if (sigint_received || sigpipe_received)
785 err = got_commit_graph_iter_next(&id, graph);
787 if (err->code == GOT_ERR_ITER_COMPLETED) {
791 if (err->code != GOT_ERR_ITER_NEED_MORE)
793 err = got_commit_graph_fetch_commits(graph, 1, repo);
802 err = got_object_open_as_commit(&commit, repo, id);
805 err = print_commit(commit, id, repo, show_patch, diff_context,
807 got_object_commit_close(commit);
808 if (err || (limit && --limit == 0))
812 got_commit_graph_close(graph);
819 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
820 "[-r repository-path] [path]\n", getprogname());
824 static const struct got_error *
825 cmd_log(int argc, char *argv[])
827 const struct got_error *error;
828 struct got_repository *repo = NULL;
829 struct got_worktree *worktree = NULL;
830 struct got_commit_object *commit = NULL;
831 struct got_object_id *id = NULL;
832 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
833 char *start_commit = NULL;
834 int diff_context = 3, ch;
835 int show_patch = 0, limit = 0, first_parent_traversal = 0;
837 struct got_reflist_head refs;
842 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
848 while ((ch = getopt(argc, argv, "pc:C:l:fr:")) != -1) {
854 start_commit = optarg;
857 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
860 err(1, "-C option %s", errstr);
863 limit = strtonum(optarg, 1, INT_MAX, &errstr);
865 err(1, "-l option %s", errstr);
868 first_parent_traversal = 1;
871 repo_path = realpath(optarg, NULL);
872 if (repo_path == NULL)
874 got_path_strip_trailing_slashes(repo_path);
885 cwd = getcwd(NULL, 0);
887 error = got_error_prefix_errno("getcwd");
891 error = got_worktree_open(&worktree, cwd);
892 if (error && error->code != GOT_ERR_NOT_WORKTREE)
899 error = got_error_prefix_errno("strdup");
902 } else if (argc == 1) {
904 error = got_worktree_resolve_path(&path, worktree,
909 path = strdup(argv[0]);
911 error = got_error_prefix_errno("strdup");
918 if (repo_path == NULL) {
919 repo_path = worktree ?
920 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
922 if (repo_path == NULL) {
923 error = got_error_prefix_errno("strdup");
927 error = got_repo_open(&repo, repo_path);
931 error = apply_unveil(got_repo_get_path(repo), 1,
932 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
936 if (start_commit == NULL) {
937 struct got_reference *head_ref;
938 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
941 error = got_ref_resolve(&id, repo, head_ref);
942 got_ref_close(head_ref);
945 error = got_object_open_as_commit(&commit, repo, id);
947 struct got_reference *ref;
948 error = got_ref_open(&ref, repo, start_commit, 0);
951 error = got_ref_resolve(&id, repo, ref);
955 error = got_object_get_type(&obj_type, repo, id);
958 if (obj_type == GOT_OBJ_TYPE_TAG) {
959 struct got_tag_object *tag;
960 error = got_object_open_as_tag(&tag, repo, id);
963 if (got_object_tag_get_object_type(tag) !=
964 GOT_OBJ_TYPE_COMMIT) {
965 got_object_tag_close(tag);
966 error = got_error(GOT_ERR_OBJ_TYPE);
970 id = got_object_id_dup(
971 got_object_tag_get_object_id(tag));
973 error = got_error_prefix_errno(
974 "got_object_id_dup");
975 got_object_tag_close(tag);
978 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
979 error = got_error(GOT_ERR_OBJ_TYPE);
982 error = got_object_open_as_commit(&commit, repo, id);
986 if (commit == NULL) {
987 error = got_object_resolve_id_str(&id, repo,
996 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1001 path = in_repo_path;
1004 error = got_ref_list(&refs, repo);
1008 error = print_commits(id, repo, path, show_patch,
1009 diff_context, limit, first_parent_traversal, &refs);
1016 got_worktree_close(worktree);
1018 const struct got_error *repo_error;
1019 repo_error = got_repo_close(repo);
1023 got_ref_list_free(&refs);
1030 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1031 "[object1 object2 | path]\n", getprogname());
1035 struct print_diff_arg {
1036 struct got_repository *repo;
1037 struct got_worktree *worktree;
1043 static const struct got_error *
1044 print_diff(void *arg, unsigned char status, const char *path,
1045 struct got_object_id *id)
1047 struct print_diff_arg *a = arg;
1048 const struct got_error *err = NULL;
1049 struct got_blob_object *blob1 = NULL;
1051 char *abspath = NULL;
1054 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1055 status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1058 if (!a->header_shown) {
1059 printf("diff %s %s\n", a->id_str,
1060 got_worktree_get_root_path(a->worktree));
1061 a->header_shown = 1;
1064 if (status != GOT_STATUS_ADD) {
1065 err = got_object_open_as_blob(&blob1, a->repo, id, 8192);
1071 if (status != GOT_STATUS_DELETE) {
1072 if (asprintf(&abspath, "%s/%s",
1073 got_worktree_get_root_path(a->worktree), path) == -1) {
1074 err = got_error_prefix_errno("asprintf");
1078 f2 = fopen(abspath, "r");
1080 err = got_error_prefix_errno2("fopen", abspath);
1083 if (lstat(abspath, &sb) == -1) {
1084 err = got_error_prefix_errno2("lstat", abspath);
1090 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1094 got_object_blob_close(blob1);
1095 if (f2 && fclose(f2) != 0 && err == NULL)
1096 err = got_error_prefix_errno("fclose");
1101 static const struct got_error *
1102 cmd_diff(int argc, char *argv[])
1104 const struct got_error *error;
1105 struct got_repository *repo = NULL;
1106 struct got_worktree *worktree = NULL;
1107 char *cwd = NULL, *repo_path = NULL;
1108 struct got_object_id *id1 = NULL, *id2 = NULL;
1109 char *id_str1 = NULL, *id_str2 = NULL;
1111 int diff_context = 3, ch;
1116 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1121 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1124 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1126 err(1, "-C option %s", errstr);
1129 repo_path = realpath(optarg, NULL);
1130 if (repo_path == NULL)
1131 err(1, "-r option");
1132 got_path_strip_trailing_slashes(repo_path);
1143 cwd = getcwd(NULL, 0);
1145 error = got_error_prefix_errno("getcwd");
1148 error = got_worktree_open(&worktree, cwd);
1149 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1152 if (worktree == NULL) {
1153 error = got_error(GOT_ERR_NOT_WORKTREE);
1158 "-r option can't be used when diffing a work tree");
1159 repo_path = strdup(got_worktree_get_repo_path(worktree));
1160 if (repo_path == NULL) {
1161 error = got_error_prefix_errno("strdup");
1165 error = got_worktree_resolve_path(&path, worktree,
1172 error = got_error_prefix_errno("strdup");
1176 } else if (argc == 2) {
1182 if (repo_path == NULL) {
1183 repo_path = getcwd(NULL, 0);
1184 if (repo_path == NULL)
1185 return got_error_prefix_errno("getcwd");
1188 error = got_repo_open(&repo, repo_path);
1193 error = apply_unveil(got_repo_get_path(repo), 1,
1194 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1199 struct print_diff_arg arg;
1201 error = got_object_id_str(&id_str,
1202 got_worktree_get_base_commit_id(worktree));
1206 arg.worktree = worktree;
1207 arg.diff_context = diff_context;
1208 arg.id_str = id_str;
1209 arg.header_shown = 0;
1211 error = got_worktree_status(worktree, path, repo, print_diff,
1212 &arg, check_cancelled, NULL);
1217 error = got_object_resolve_id_str(&id1, repo, id_str1);
1221 error = got_object_resolve_id_str(&id2, repo, id_str2);
1225 error = got_object_get_type(&type1, repo, id1);
1229 error = got_object_get_type(&type2, repo, id2);
1233 if (type1 != type2) {
1234 error = got_error(GOT_ERR_OBJ_TYPE);
1239 case GOT_OBJ_TYPE_BLOB:
1240 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1241 diff_context, repo, stdout);
1243 case GOT_OBJ_TYPE_TREE:
1244 error = got_diff_objects_as_trees(id1, id2, "", "",
1245 diff_context, repo, stdout);
1247 case GOT_OBJ_TYPE_COMMIT:
1248 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
1250 error = got_diff_objects_as_commits(id1, id2, diff_context,
1254 error = got_error(GOT_ERR_OBJ_TYPE);
1262 got_worktree_close(worktree);
1264 const struct got_error *repo_error;
1265 repo_error = got_repo_close(repo);
1276 "usage: %s blame [-c commit] [-r repository-path] path\n",
1281 static const struct got_error *
1282 cmd_blame(int argc, char *argv[])
1284 const struct got_error *error;
1285 struct got_repository *repo = NULL;
1286 struct got_worktree *worktree = NULL;
1287 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1288 struct got_object_id *commit_id = NULL;
1289 char *commit_id_str = NULL;
1293 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1298 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1301 commit_id_str = optarg;
1304 repo_path = realpath(optarg, NULL);
1305 if (repo_path == NULL)
1306 err(1, "-r option");
1307 got_path_strip_trailing_slashes(repo_path);
1323 cwd = getcwd(NULL, 0);
1325 error = got_error_prefix_errno("getcwd");
1328 if (repo_path == NULL) {
1329 error = got_worktree_open(&worktree, cwd);
1330 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1336 strdup(got_worktree_get_repo_path(worktree));
1337 if (repo_path == NULL)
1338 error = got_error_prefix_errno("strdup");
1342 repo_path = strdup(cwd);
1343 if (repo_path == NULL) {
1344 error = got_error_prefix_errno("strdup");
1350 error = got_repo_open(&repo, repo_path);
1354 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1359 const char *prefix = got_worktree_get_path_prefix(worktree);
1360 char *p, *worktree_subdir = cwd +
1361 strlen(got_worktree_get_root_path(worktree));
1362 if (asprintf(&p, "%s%s%s%s%s",
1363 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1364 worktree_subdir, worktree_subdir[0] ? "/" : "",
1366 error = got_error_prefix_errno("asprintf");
1369 error = got_repo_map_path(&in_repo_path, repo, p, 0);
1372 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1377 if (commit_id_str == NULL) {
1378 struct got_reference *head_ref;
1379 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1382 error = got_ref_resolve(&commit_id, repo, head_ref);
1383 got_ref_close(head_ref);
1387 error = got_object_resolve_id_str(&commit_id, repo,
1393 error = got_blame(in_repo_path, commit_id, repo, stdout);
1400 got_worktree_close(worktree);
1402 const struct got_error *repo_error;
1403 repo_error = got_repo_close(repo);
1414 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1420 print_entry(struct got_tree_entry *te, const char *id, const char *path,
1421 const char *root_path)
1423 int is_root_path = (strcmp(path, root_path) == 0);
1425 path += strlen(root_path);
1426 while (path[0] == '/')
1429 printf("%s%s%s%s%s\n", id ? id : "", path,
1430 is_root_path ? "" : "/", te->name,
1431 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
1434 static const struct got_error *
1435 print_tree(const char *path, struct got_object_id *commit_id,
1436 int show_ids, int recurse, const char *root_path,
1437 struct got_repository *repo)
1439 const struct got_error *err = NULL;
1440 struct got_object_id *tree_id = NULL;
1441 struct got_tree_object *tree = NULL;
1442 const struct got_tree_entries *entries;
1443 struct got_tree_entry *te;
1445 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1449 err = got_object_open_as_tree(&tree, repo, tree_id);
1452 entries = got_object_tree_get_entries(tree);
1453 te = SIMPLEQ_FIRST(&entries->head);
1457 if (sigint_received || sigpipe_received)
1462 err = got_object_id_str(&id_str, te->id);
1465 if (asprintf(&id, "%s ", id_str) == -1) {
1466 err = got_error_prefix_errno("asprintf");
1472 print_entry(te, id, path, root_path);
1475 if (recurse && S_ISDIR(te->mode)) {
1477 if (asprintf(&child_path, "%s%s%s", path,
1478 path[0] == '/' && path[1] == '\0' ? "" : "/",
1480 err = got_error_prefix_errno("asprintf");
1483 err = print_tree(child_path, commit_id, show_ids, 1,
1490 te = SIMPLEQ_NEXT(te, entry);
1494 got_object_tree_close(tree);
1499 static const struct got_error *
1500 cmd_tree(int argc, char *argv[])
1502 const struct got_error *error;
1503 struct got_repository *repo = NULL;
1504 struct got_worktree *worktree = NULL;
1506 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1507 struct got_object_id *commit_id = NULL;
1508 char *commit_id_str = NULL;
1509 int show_ids = 0, recurse = 0;
1513 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1518 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1521 commit_id_str = optarg;
1524 repo_path = realpath(optarg, NULL);
1525 if (repo_path == NULL)
1526 err(1, "-r option");
1527 got_path_strip_trailing_slashes(repo_path);
1551 cwd = getcwd(NULL, 0);
1553 error = got_error_prefix_errno("getcwd");
1556 if (repo_path == NULL) {
1557 error = got_worktree_open(&worktree, cwd);
1558 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1564 strdup(got_worktree_get_repo_path(worktree));
1565 if (repo_path == NULL)
1566 error = got_error_prefix_errno("strdup");
1570 repo_path = strdup(cwd);
1571 if (repo_path == NULL) {
1572 error = got_error_prefix_errno("strdup");
1578 error = got_repo_open(&repo, repo_path);
1582 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1588 char *p, *worktree_subdir = cwd +
1589 strlen(got_worktree_get_root_path(worktree));
1590 if (asprintf(&p, "%s/%s",
1591 got_worktree_get_path_prefix(worktree),
1592 worktree_subdir) == -1) {
1593 error = got_error_prefix_errno("asprintf");
1596 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1603 if (in_repo_path == NULL) {
1604 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1609 if (commit_id_str == NULL) {
1610 struct got_reference *head_ref;
1611 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1614 error = got_ref_resolve(&commit_id, repo, head_ref);
1615 got_ref_close(head_ref);
1619 error = got_object_resolve_id_str(&commit_id, repo,
1625 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1626 in_repo_path, repo);
1633 got_worktree_close(worktree);
1635 const struct got_error *repo_error;
1636 repo_error = got_repo_close(repo);
1646 fprintf(stderr, "usage: %s status [path]\n", getprogname());
1650 static const struct got_error *
1651 print_status(void *arg, unsigned char status, const char *path,
1652 struct got_object_id *id)
1654 printf("%c %s\n", status, path);
1658 static const struct got_error *
1659 cmd_status(int argc, char *argv[])
1661 const struct got_error *error = NULL;
1662 struct got_repository *repo = NULL;
1663 struct got_worktree *worktree = NULL;
1664 char *cwd = NULL, *path = NULL;
1667 while ((ch = getopt(argc, argv, "")) != -1) {
1679 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1683 cwd = getcwd(NULL, 0);
1685 error = got_error_prefix_errno("getcwd");
1689 error = got_worktree_open(&worktree, cwd);
1696 error = got_error_prefix_errno("strdup");
1699 } else if (argc == 1) {
1700 error = got_worktree_resolve_path(&path, worktree, argv[0]);
1706 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1710 error = apply_unveil(got_repo_get_path(repo), 1,
1711 got_worktree_get_root_path(worktree), 0);
1715 error = got_worktree_status(worktree, path, repo, print_status, NULL,
1716 check_cancelled, NULL);
1727 "usage: %s ref [-r repository] -l | -d name | name object\n",
1732 static const struct got_error *
1733 list_refs(struct got_repository *repo)
1735 static const struct got_error *err = NULL;
1736 struct got_reflist_head refs;
1737 struct got_reflist_entry *re;
1739 SIMPLEQ_INIT(&refs);
1740 err = got_ref_list(&refs, repo);
1744 SIMPLEQ_FOREACH(re, &refs, entry) {
1746 refstr = got_ref_to_str(re->ref);
1748 return got_error_prefix_errno("got_ref_to_str");
1749 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
1753 got_ref_list_free(&refs);
1757 static const struct got_error *
1758 delete_ref(struct got_repository *repo, const char *refname)
1760 const struct got_error *err = NULL;
1761 struct got_reference *ref;
1763 err = got_ref_open(&ref, repo, refname, 0);
1767 err = got_ref_delete(ref, repo);
1772 static const struct got_error *
1773 add_ref(struct got_repository *repo, const char *refname, const char *id_str)
1775 const struct got_error *err = NULL;
1776 struct got_object_id *id;
1777 struct got_reference *ref = NULL;
1779 err = got_object_resolve_id_str(&id, repo, id_str);
1783 err = got_ref_alloc(&ref, refname, id);
1787 err = got_ref_write(ref, repo);
1795 static const struct got_error *
1796 cmd_ref(int argc, char *argv[])
1798 const struct got_error *error = NULL;
1799 struct got_repository *repo = NULL;
1800 struct got_worktree *worktree = NULL;
1801 char *cwd = NULL, *repo_path = NULL;
1802 int ch, do_list = 0;
1803 const char *delref = NULL;
1805 /* TODO: Add -s option for adding symbolic references. */
1806 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
1812 repo_path = realpath(optarg, NULL);
1813 if (repo_path == NULL)
1814 err(1, "-r option");
1815 got_path_strip_trailing_slashes(repo_path);
1826 if (do_list && delref)
1827 errx(1, "-l and -d options are mutually exclusive\n");
1832 if (do_list || delref) {
1835 } else if (argc != 2)
1840 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
1844 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1845 "sendfd unveil", NULL) == -1)
1849 cwd = getcwd(NULL, 0);
1851 error = got_error_prefix_errno("getcwd");
1855 if (repo_path == NULL) {
1856 error = got_worktree_open(&worktree, cwd);
1857 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1863 strdup(got_worktree_get_repo_path(worktree));
1864 if (repo_path == NULL)
1865 error = got_error_prefix_errno("strdup");
1869 repo_path = strdup(cwd);
1870 if (repo_path == NULL) {
1871 error = got_error_prefix_errno("strdup");
1877 error = got_repo_open(&repo, repo_path);
1881 error = apply_unveil(got_repo_get_path(repo), do_list,
1882 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1887 error = list_refs(repo);
1889 error = delete_ref(repo, delref);
1891 error = add_ref(repo, argv[0], argv[1]);
1894 got_repo_close(repo);
1896 got_worktree_close(worktree);
1905 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
1909 static const struct got_error *
1910 cmd_add(int argc, char *argv[])
1912 const struct got_error *error = NULL;
1913 struct got_repository *repo = NULL;
1914 struct got_worktree *worktree = NULL;
1916 struct got_pathlist_head paths;
1917 struct got_pathlist_entry *pe;
1922 while ((ch = getopt(argc, argv, "")) != -1) {
1936 /* make sure each file exists before doing anything halfway */
1937 for (x = 0; x < argc; x++) {
1938 char *path = realpath(argv[x], NULL);
1940 error = got_error_prefix_errno2("realpath", argv[x]);
1946 cwd = getcwd(NULL, 0);
1948 error = got_error_prefix_errno("getcwd");
1952 error = got_worktree_open(&worktree, cwd);
1956 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1960 error = apply_unveil(got_repo_get_path(repo), 1,
1961 got_worktree_get_root_path(worktree), 0);
1965 for (x = 0; x < argc; x++) {
1966 char *path = realpath(argv[x], NULL);
1968 error = got_error_prefix_errno2("realpath", argv[x]);
1972 got_path_strip_trailing_slashes(path);
1973 error = got_pathlist_insert(&pe, &paths, path, NULL);
1979 error = got_worktree_schedule_add(worktree, &paths, print_status,
1983 got_repo_close(repo);
1985 got_worktree_close(worktree);
1986 TAILQ_FOREACH(pe, &paths, entry)
1987 free((char *)pe->path);
1988 got_pathlist_free(&paths);
1996 fprintf(stderr, "usage: %s rm [-f] file-path\n", getprogname());
2000 static const struct got_error *
2001 cmd_rm(int argc, char *argv[])
2003 const struct got_error *error = NULL;
2004 struct got_worktree *worktree = NULL;
2005 struct got_repository *repo = NULL;
2006 char *cwd = NULL, *path = NULL;
2007 int ch, delete_local_mods = 0;
2009 while ((ch = getopt(argc, argv, "f")) != -1) {
2012 delete_local_mods = 1;
2026 path = realpath(argv[0], NULL);
2028 error = got_error_prefix_errno2("realpath", argv[0]);
2031 got_path_strip_trailing_slashes(path);
2033 cwd = getcwd(NULL, 0);
2035 error = got_error_prefix_errno("getcwd");
2038 error = got_worktree_open(&worktree, cwd);
2042 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2046 error = apply_unveil(got_repo_get_path(repo), 1,
2047 got_worktree_get_root_path(worktree), 0);
2051 error = got_worktree_schedule_delete(worktree, path, delete_local_mods,
2052 print_status, NULL, repo);
2057 got_repo_close(repo);
2059 got_worktree_close(worktree);
2068 fprintf(stderr, "usage: %s revert file-path\n", getprogname());
2073 revert_progress(void *arg, unsigned char status, const char *path)
2075 while (path[0] == '/')
2077 printf("%c %s\n", status, path);
2080 static const struct got_error *
2081 cmd_revert(int argc, char *argv[])
2083 const struct got_error *error = NULL;
2084 struct got_worktree *worktree = NULL;
2085 struct got_repository *repo = NULL;
2086 char *cwd = NULL, *path = NULL;
2089 while ((ch = getopt(argc, argv, "")) != -1) {
2103 path = realpath(argv[0], NULL);
2105 error = got_error_prefix_errno2("realpath", argv[0]);
2108 got_path_strip_trailing_slashes(path);
2110 cwd = getcwd(NULL, 0);
2112 error = got_error_prefix_errno("getcwd");
2115 error = got_worktree_open(&worktree, cwd);
2119 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2123 error = apply_unveil(got_repo_get_path(repo), 1,
2124 got_worktree_get_root_path(worktree), 0);
2128 error = got_worktree_revert(worktree, path,
2129 revert_progress, NULL, repo);
2134 got_repo_close(repo);
2136 got_worktree_close(worktree);
2145 fprintf(stderr, "usage: %s commit [-m msg] file-path\n", getprogname());
2149 static const struct got_error *
2150 cmd_commit(int argc, char *argv[])
2152 const struct got_error *error = NULL;
2153 struct got_worktree *worktree = NULL;
2154 struct got_repository *repo = NULL;
2155 char *cwd = NULL, *path = NULL, *id_str = NULL;
2156 struct got_object_id *id = NULL;
2157 const char *logmsg = "<no log message was specified>";
2158 const char *got_author = getenv("GOT_AUTHOR");
2161 while ((ch = getopt(argc, argv, "m:")) != -1) {
2176 path = realpath(argv[0], NULL);
2178 error = got_error_prefix_errno2("realpath", argv[0]);
2181 got_path_strip_trailing_slashes(path);
2182 } else if (argc != 0)
2185 if (got_author == NULL) {
2186 /* TODO: Look current user up in password database */
2187 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
2191 cwd = getcwd(NULL, 0);
2193 error = got_error_prefix_errno("getcwd");
2196 error = got_worktree_open(&worktree, cwd);
2200 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2204 error = apply_unveil(got_repo_get_path(repo), 0,
2205 got_worktree_get_root_path(worktree), 0);
2209 error = got_worktree_commit(&id, worktree, path, got_author, NULL,
2210 logmsg, print_status, NULL, repo);
2214 error = got_object_id_str(&id_str, id);
2217 printf("created commit %s\n", id_str);
2220 got_repo_close(repo);
2222 got_worktree_close(worktree);