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>
37 #include "got_error.h"
38 #include "got_object.h"
39 #include "got_reference.h"
40 #include "got_repository.h"
42 #include "got_worktree.h"
44 #include "got_commit_graph.h"
45 #include "got_blame.h"
46 #include "got_privsep.h"
49 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
52 static volatile sig_atomic_t sigint_received;
53 static volatile sig_atomic_t sigpipe_received;
56 catch_sigint(int signo)
62 catch_sigpipe(int signo)
70 const struct got_error *(*cmd_main)(int, char *[]);
71 void (*cmd_usage)(void);
72 const char *cmd_descr;
75 __dead static void usage(void);
76 __dead static void usage_checkout(void);
77 __dead static void usage_update(void);
78 __dead static void usage_log(void);
79 __dead static void usage_diff(void);
80 __dead static void usage_blame(void);
81 __dead static void usage_tree(void);
82 __dead static void usage_status(void);
83 __dead static void usage_ref(void);
84 __dead static void usage_add(void);
85 __dead static void usage_rm(void);
86 __dead static void usage_revert(void);
87 __dead static void usage_commit(void);
89 static const struct got_error* cmd_checkout(int, char *[]);
90 static const struct got_error* cmd_update(int, char *[]);
91 static const struct got_error* cmd_log(int, char *[]);
92 static const struct got_error* cmd_diff(int, char *[]);
93 static const struct got_error* cmd_blame(int, char *[]);
94 static const struct got_error* cmd_tree(int, char *[]);
95 static const struct got_error* cmd_status(int, char *[]);
96 static const struct got_error* cmd_ref(int, char *[]);
97 static const struct got_error* cmd_add(int, char *[]);
98 static const struct got_error* cmd_rm(int, char *[]);
99 static const struct got_error* cmd_revert(int, char *[]);
100 static const struct got_error* cmd_commit(int, char *[]);
102 static struct cmd got_commands[] = {
103 { "checkout", cmd_checkout, usage_checkout,
104 "check out a new work tree from a repository" },
105 { "update", cmd_update, usage_update,
106 "update a work tree to a different commit" },
107 { "log", cmd_log, usage_log,
108 "show repository history" },
109 { "diff", cmd_diff, usage_diff,
110 "compare files and directories" },
111 { "blame", cmd_blame, usage_blame,
112 "show when lines in a file were changed" },
113 { "tree", cmd_tree, usage_tree,
114 "list files and directories in repository" },
115 { "status", cmd_status, usage_status,
116 "show modification status of files" },
117 { "ref", cmd_ref, usage_ref,
118 "manage references in repository" },
119 { "add", cmd_add, usage_add,
120 "add new files to version control" },
121 { "rm", cmd_rm, usage_rm,
122 "remove a versioned file" },
123 { "revert", cmd_revert, usage_revert,
124 "revert uncommitted changes" },
125 { "commit", cmd_commit, usage_commit,
126 "write changes from work tree to repository" },
130 main(int argc, char *argv[])
137 setlocale(LC_CTYPE, "");
139 while ((ch = getopt(argc, argv, "h")) != -1) {
157 signal(SIGINT, catch_sigint);
158 signal(SIGPIPE, catch_sigpipe);
160 for (i = 0; i < nitems(got_commands); i++) {
161 const struct got_error *error;
163 cmd = &got_commands[i];
165 if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
169 got_commands[i].cmd_usage();
171 error = got_commands[i].cmd_main(argc, argv);
172 if (error && !(sigint_received || sigpipe_received)) {
173 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
180 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
189 fprintf(stderr, "usage: %s [-h] command [arg ...]\n\n"
190 "Available commands:\n", getprogname());
191 for (i = 0; i < nitems(got_commands); i++) {
192 struct cmd *cmd = &got_commands[i];
193 fprintf(stderr, " %s: %s\n", cmd->cmd_name, cmd->cmd_descr);
198 static const struct got_error *
199 apply_unveil(const char *repo_path, int repo_read_only,
200 const char *worktree_path, int create_worktree)
202 const struct got_error *error;
203 static char err_msg[MAXPATHLEN + 36];
205 if (create_worktree) {
206 /* Pre-create work tree path to avoid unveiling its parents. */
207 error = got_path_mkdir(worktree_path);
209 if (errno == EEXIST) {
210 if (got_path_dir_is_empty(worktree_path)) {
214 snprintf(err_msg, sizeof(err_msg),
215 "%s: directory exists but is not empty",
217 error = got_error_msg(GOT_ERR_BAD_PATH,
222 if (error && (error->code != GOT_ERR_ERRNO || errno != EISDIR))
226 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
227 return got_error_prefix_errno2("unveil", repo_path);
229 if (worktree_path && unveil(worktree_path, "rwc") != 0)
230 return got_error_prefix_errno2("unveil", worktree_path);
232 if (unveil("/tmp", "rwc") != 0)
233 return got_error_prefix_errno2("unveil", "/tmp");
235 error = got_privsep_unveil_exec_helpers();
239 if (unveil(NULL, NULL) != 0)
240 return got_error_prefix_errno("unveil");
248 fprintf(stderr, "usage: %s checkout [-p prefix] repository-path "
249 "[worktree-path]\n", getprogname());
254 checkout_progress(void *arg, unsigned char status, const char *path)
256 char *worktree_path = arg;
258 while (path[0] == '/')
261 printf("%c %s/%s\n", status, worktree_path, path);
264 static const struct got_error *
265 check_cancelled(void *arg)
267 if (sigint_received || sigpipe_received)
268 return got_error(GOT_ERR_CANCELLED);
272 static const struct got_error *
273 check_ancestry(struct got_worktree *worktree, struct got_object_id *commit_id,
274 struct got_repository *repo)
276 const struct got_error *err;
277 struct got_reference *head_ref = NULL;
278 struct got_object_id *head_commit_id = NULL;
279 struct got_commit_graph *graph = NULL;
281 err = got_ref_open(&head_ref, repo,
282 got_worktree_get_head_ref_name(worktree), 0);
286 /* TODO: Check the reflog. The head ref may have been rebased. */
287 err = got_ref_resolve(&head_commit_id, repo, head_ref);
291 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
295 err = got_commit_graph_iter_start(graph, head_commit_id, repo);
299 struct got_object_id *id;
301 if (sigint_received || sigpipe_received)
304 err = got_commit_graph_iter_next(&id, graph);
306 if (err->code == GOT_ERR_ITER_COMPLETED) {
307 err = got_error(GOT_ERR_ANCESTRY);
310 if (err->code != GOT_ERR_ITER_NEED_MORE)
312 err = got_commit_graph_fetch_commits(graph, 1, repo);
320 if (got_object_id_cmp(id, commit_id) == 0)
325 got_ref_close(head_ref);
327 got_commit_graph_close(graph);
332 static const struct got_error *
333 cmd_checkout(int argc, char *argv[])
335 const struct got_error *error = NULL;
336 struct got_repository *repo = NULL;
337 struct got_reference *head_ref = NULL;
338 struct got_worktree *worktree = NULL;
339 char *repo_path = NULL;
340 char *worktree_path = NULL;
341 const char *path_prefix = "";
342 char *commit_id_str = NULL;
343 int ch, same_path_prefix;
345 while ((ch = getopt(argc, argv, "c:p:")) != -1) {
348 commit_id_str = strdup(optarg);
349 if (commit_id_str == NULL)
350 return got_error_prefix_errno("strdup");
353 path_prefix = optarg;
365 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
366 "unveil", NULL) == -1)
370 char *cwd, *base, *dotgit;
371 repo_path = realpath(argv[0], NULL);
372 if (repo_path == NULL)
373 return got_error_prefix_errno2("realpath", argv[0]);
374 cwd = getcwd(NULL, 0);
376 error = got_error_prefix_errno("getcwd");
379 if (path_prefix[0]) {
380 base = basename(path_prefix);
382 error = got_error_prefix_errno2("basename",
387 base = basename(repo_path);
389 error = got_error_prefix_errno2("basename",
394 dotgit = strstr(base, ".git");
397 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
398 error = got_error_prefix_errno("asprintf");
403 } else if (argc == 2) {
404 repo_path = realpath(argv[0], NULL);
405 if (repo_path == NULL) {
406 error = got_error_prefix_errno2("realpath", argv[0]);
409 worktree_path = realpath(argv[1], NULL);
410 if (worktree_path == NULL) {
411 error = got_error_prefix_errno2("realpath", argv[1]);
417 got_path_strip_trailing_slashes(repo_path);
418 got_path_strip_trailing_slashes(worktree_path);
420 error = got_repo_open(&repo, repo_path);
424 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path, 1);
428 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
432 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
433 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
436 error = got_worktree_open(&worktree, worktree_path);
440 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
444 if (!same_path_prefix) {
445 error = got_error(GOT_ERR_PATH_PREFIX);
450 struct got_object_id *commit_id;
451 error = got_object_resolve_id_str(&commit_id, repo,
455 error = check_ancestry(worktree, commit_id, repo);
460 error = got_worktree_set_base_commit_id(worktree, repo,
467 error = got_worktree_checkout_files(worktree, "", repo,
468 checkout_progress, worktree_path, check_cancelled, NULL);
472 printf("Now shut up and hack\n");
484 fprintf(stderr, "usage: %s update [-c commit] [path]\n",
490 update_progress(void *arg, unsigned char status, const char *path)
492 int *did_something = arg;
494 if (status == GOT_STATUS_EXISTS)
498 while (path[0] == '/')
500 printf("%c %s\n", status, path);
503 static const struct got_error *
504 cmd_update(int argc, char *argv[])
506 const struct got_error *error = NULL;
507 struct got_repository *repo = NULL;
508 struct got_worktree *worktree = NULL;
509 char *worktree_path = NULL, *path = NULL;
510 struct got_object_id *commit_id = NULL;
511 char *commit_id_str = NULL;
512 int ch, did_something = 0;
514 while ((ch = getopt(argc, argv, "c:")) != -1) {
517 commit_id_str = strdup(optarg);
518 if (commit_id_str == NULL)
519 return got_error_prefix_errno("strdup");
531 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
532 "unveil", NULL) == -1)
535 worktree_path = getcwd(NULL, 0);
536 if (worktree_path == NULL) {
537 error = got_error_prefix_errno("getcwd");
540 error = got_worktree_open(&worktree, worktree_path);
547 error = got_error_prefix_errno("strdup");
550 } else if (argc == 1) {
551 error = got_worktree_resolve_path(&path, worktree, argv[0]);
557 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
561 error = apply_unveil(got_repo_get_path(repo), 0,
562 got_worktree_get_root_path(worktree), 0);
566 if (commit_id_str == NULL) {
567 struct got_reference *head_ref;
568 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
571 error = got_ref_resolve(&commit_id, repo, head_ref);
574 error = got_object_id_str(&commit_id_str, commit_id);
578 error = got_object_resolve_id_str(&commit_id, repo,
584 error = check_ancestry(worktree, commit_id, repo);
588 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
590 error = got_worktree_set_base_commit_id(worktree, repo,
596 error = got_worktree_checkout_files(worktree, path, repo,
597 update_progress, &did_something, check_cancelled, NULL);
602 printf("Updated to commit %s\n", commit_id_str);
604 printf("Already up-to-date\n");
613 static const struct got_error *
614 print_patch(struct got_commit_object *commit, struct got_object_id *id,
615 int diff_context, struct got_repository *repo)
617 const struct got_error *err = NULL;
618 struct got_tree_object *tree1 = NULL, *tree2;
619 struct got_object_qid *qid;
620 char *id_str1 = NULL, *id_str2;
622 err = got_object_open_as_tree(&tree2, repo,
623 got_object_commit_get_tree_id(commit));
627 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
629 struct got_commit_object *pcommit;
631 err = got_object_open_as_commit(&pcommit, repo, qid->id);
635 err = got_object_open_as_tree(&tree1, repo,
636 got_object_commit_get_tree_id(pcommit));
637 got_object_commit_close(pcommit);
641 err = got_object_id_str(&id_str1, qid->id);
646 err = got_object_id_str(&id_str2, id);
650 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
651 err = got_diff_tree(tree1, tree2, "", "", diff_context, repo, stdout);
654 got_object_tree_close(tree1);
655 got_object_tree_close(tree2);
662 get_datestr(time_t *time, char *datebuf)
664 char *p, *s = ctime_r(time, datebuf);
671 static const struct got_error *
672 print_commit(struct got_commit_object *commit, struct got_object_id *id,
673 struct got_repository *repo, int show_patch, int diff_context,
674 struct got_reflist_head *refs)
676 const struct got_error *err = NULL;
677 char *id_str, *datestr, *logmsg0, *logmsg, *line;
679 time_t committer_time;
680 const char *author, *committer;
681 char *refs_str = NULL;
682 struct got_reflist_entry *re;
684 SIMPLEQ_FOREACH(re, refs, entry) {
687 if (got_object_id_cmp(re->id, id) != 0)
689 name = got_ref_get_name(re->ref);
690 if (strcmp(name, GOT_REF_HEAD) == 0)
692 if (strncmp(name, "refs/", 5) == 0)
694 if (strncmp(name, "got/", 4) == 0)
696 if (strncmp(name, "heads/", 6) == 0)
698 if (strncmp(name, "remotes/", 8) == 0)
701 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
703 err = got_error_prefix_errno("asprintf");
709 err = got_object_id_str(&id_str, id);
713 printf("-----------------------------------------------\n");
714 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
715 refs_str ? refs_str : "", refs_str ? ")" : "");
720 printf("from: %s\n", got_object_commit_get_author(commit));
721 committer_time = got_object_commit_get_committer_time(commit);
722 datestr = get_datestr(&committer_time, datebuf);
723 printf("date: %s UTC\n", datestr);
724 author = got_object_commit_get_author(commit);
725 committer = got_object_commit_get_committer(commit);
726 if (strcmp(author, committer) != 0)
727 printf("via: %s\n", committer);
728 if (got_object_commit_get_nparents(commit) > 1) {
729 const struct got_object_id_queue *parent_ids;
730 struct got_object_qid *qid;
732 parent_ids = got_object_commit_get_parent_ids(commit);
733 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
734 err = got_object_id_str(&id_str, qid->id);
737 printf("parent %d: %s\n", n++, id_str);
742 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
744 return got_error_prefix_errno("strdup");
748 line = strsep(&logmsg, "\n");
750 printf(" %s\n", line);
755 err = print_patch(commit, id, diff_context, repo);
760 if (fflush(stdout) != 0 && err == NULL)
761 err = got_error_prefix_errno("fflush");
765 static const struct got_error *
766 print_commits(struct got_object_id *root_id, struct got_repository *repo,
767 char *path, int show_patch, int diff_context, int limit,
768 int first_parent_traversal, struct got_reflist_head *refs)
770 const struct got_error *err;
771 struct got_commit_graph *graph;
773 err = got_commit_graph_open(&graph, root_id, path,
774 first_parent_traversal, repo);
777 err = got_commit_graph_iter_start(graph, root_id, repo);
781 struct got_commit_object *commit;
782 struct got_object_id *id;
784 if (sigint_received || sigpipe_received)
787 err = got_commit_graph_iter_next(&id, graph);
789 if (err->code == GOT_ERR_ITER_COMPLETED) {
793 if (err->code != GOT_ERR_ITER_NEED_MORE)
795 err = got_commit_graph_fetch_commits(graph, 1, repo);
804 err = got_object_open_as_commit(&commit, repo, id);
807 err = print_commit(commit, id, repo, show_patch, diff_context,
809 got_object_commit_close(commit);
810 if (err || (limit && --limit == 0))
814 got_commit_graph_close(graph);
821 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
822 "[-r repository-path] [path]\n", getprogname());
826 static const struct got_error *
827 cmd_log(int argc, char *argv[])
829 const struct got_error *error;
830 struct got_repository *repo = NULL;
831 struct got_worktree *worktree = NULL;
832 struct got_commit_object *commit = NULL;
833 struct got_object_id *id = NULL;
834 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
835 char *start_commit = NULL;
836 int diff_context = 3, ch;
837 int show_patch = 0, limit = 0, first_parent_traversal = 0;
839 struct got_reflist_head refs;
844 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
850 while ((ch = getopt(argc, argv, "pc:C:l:fr:")) != -1) {
856 start_commit = optarg;
859 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
862 err(1, "-C option %s", errstr);
865 limit = strtonum(optarg, 1, INT_MAX, &errstr);
867 err(1, "-l option %s", errstr);
870 first_parent_traversal = 1;
873 repo_path = realpath(optarg, NULL);
874 if (repo_path == NULL)
876 got_path_strip_trailing_slashes(repo_path);
887 cwd = getcwd(NULL, 0);
889 error = got_error_prefix_errno("getcwd");
893 error = got_worktree_open(&worktree, cwd);
894 if (error && error->code != GOT_ERR_NOT_WORKTREE)
901 error = got_error_prefix_errno("strdup");
904 } else if (argc == 1) {
906 error = got_worktree_resolve_path(&path, worktree,
911 path = strdup(argv[0]);
913 error = got_error_prefix_errno("strdup");
920 if (repo_path == NULL) {
921 repo_path = worktree ?
922 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
924 if (repo_path == NULL) {
925 error = got_error_prefix_errno("strdup");
929 error = got_repo_open(&repo, repo_path);
933 error = apply_unveil(got_repo_get_path(repo), 1,
934 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
938 if (start_commit == NULL) {
939 struct got_reference *head_ref;
940 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
943 error = got_ref_resolve(&id, repo, head_ref);
944 got_ref_close(head_ref);
947 error = got_object_open_as_commit(&commit, repo, id);
949 struct got_reference *ref;
950 error = got_ref_open(&ref, repo, start_commit, 0);
953 error = got_ref_resolve(&id, repo, ref);
957 error = got_object_get_type(&obj_type, repo, id);
960 if (obj_type == GOT_OBJ_TYPE_TAG) {
961 struct got_tag_object *tag;
962 error = got_object_open_as_tag(&tag, repo, id);
965 if (got_object_tag_get_object_type(tag) !=
966 GOT_OBJ_TYPE_COMMIT) {
967 got_object_tag_close(tag);
968 error = got_error(GOT_ERR_OBJ_TYPE);
972 id = got_object_id_dup(
973 got_object_tag_get_object_id(tag));
975 error = got_error_prefix_errno(
976 "got_object_id_dup");
977 got_object_tag_close(tag);
980 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
981 error = got_error(GOT_ERR_OBJ_TYPE);
984 error = got_object_open_as_commit(&commit, repo, id);
988 if (commit == NULL) {
989 error = got_object_resolve_id_str(&id, repo,
998 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1003 path = in_repo_path;
1006 error = got_ref_list(&refs, repo);
1010 error = print_commits(id, repo, path, show_patch,
1011 diff_context, limit, first_parent_traversal, &refs);
1018 got_worktree_close(worktree);
1020 const struct got_error *repo_error;
1021 repo_error = got_repo_close(repo);
1025 got_ref_list_free(&refs);
1032 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1033 "[object1 object2 | path]\n", getprogname());
1037 struct print_diff_arg {
1038 struct got_repository *repo;
1039 struct got_worktree *worktree;
1045 static const struct got_error *
1046 print_diff(void *arg, unsigned char status, const char *path,
1047 struct got_object_id *id)
1049 struct print_diff_arg *a = arg;
1050 const struct got_error *err = NULL;
1051 struct got_blob_object *blob1 = NULL;
1053 char *abspath = NULL;
1056 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1057 status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1060 if (!a->header_shown) {
1061 printf("diff %s %s\n", a->id_str,
1062 got_worktree_get_root_path(a->worktree));
1063 a->header_shown = 1;
1066 if (status != GOT_STATUS_ADD) {
1067 err = got_object_open_as_blob(&blob1, a->repo, id, 8192);
1073 if (status != GOT_STATUS_DELETE) {
1074 if (asprintf(&abspath, "%s/%s",
1075 got_worktree_get_root_path(a->worktree), path) == -1) {
1076 err = got_error_prefix_errno("asprintf");
1080 f2 = fopen(abspath, "r");
1082 err = got_error_prefix_errno2("fopen", abspath);
1085 if (lstat(abspath, &sb) == -1) {
1086 err = got_error_prefix_errno2("lstat", abspath);
1092 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1096 got_object_blob_close(blob1);
1097 if (f2 && fclose(f2) != 0 && err == NULL)
1098 err = got_error_prefix_errno("fclose");
1103 static const struct got_error *
1104 cmd_diff(int argc, char *argv[])
1106 const struct got_error *error;
1107 struct got_repository *repo = NULL;
1108 struct got_worktree *worktree = NULL;
1109 char *cwd = NULL, *repo_path = NULL;
1110 struct got_object_id *id1 = NULL, *id2 = NULL;
1111 char *id_str1 = NULL, *id_str2 = NULL;
1113 int diff_context = 3, ch;
1118 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1123 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1126 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1128 err(1, "-C option %s", errstr);
1131 repo_path = realpath(optarg, NULL);
1132 if (repo_path == NULL)
1133 err(1, "-r option");
1134 got_path_strip_trailing_slashes(repo_path);
1145 cwd = getcwd(NULL, 0);
1147 error = got_error_prefix_errno("getcwd");
1150 error = got_worktree_open(&worktree, cwd);
1151 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1154 if (worktree == NULL) {
1155 error = got_error(GOT_ERR_NOT_WORKTREE);
1160 "-r option can't be used when diffing a work tree");
1161 repo_path = strdup(got_worktree_get_repo_path(worktree));
1162 if (repo_path == NULL) {
1163 error = got_error_prefix_errno("strdup");
1167 error = got_worktree_resolve_path(&path, worktree,
1174 error = got_error_prefix_errno("strdup");
1178 } else if (argc == 2) {
1184 if (repo_path == NULL) {
1185 repo_path = getcwd(NULL, 0);
1186 if (repo_path == NULL)
1187 return got_error_prefix_errno("getcwd");
1190 error = got_repo_open(&repo, repo_path);
1195 error = apply_unveil(got_repo_get_path(repo), 1,
1196 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1201 struct print_diff_arg arg;
1203 error = got_object_id_str(&id_str,
1204 got_worktree_get_base_commit_id(worktree));
1208 arg.worktree = worktree;
1209 arg.diff_context = diff_context;
1210 arg.id_str = id_str;
1211 arg.header_shown = 0;
1213 error = got_worktree_status(worktree, path, repo, print_diff,
1214 &arg, check_cancelled, NULL);
1219 error = got_object_resolve_id_str(&id1, repo, id_str1);
1223 error = got_object_resolve_id_str(&id2, repo, id_str2);
1227 error = got_object_get_type(&type1, repo, id1);
1231 error = got_object_get_type(&type2, repo, id2);
1235 if (type1 != type2) {
1236 error = got_error(GOT_ERR_OBJ_TYPE);
1241 case GOT_OBJ_TYPE_BLOB:
1242 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1243 diff_context, repo, stdout);
1245 case GOT_OBJ_TYPE_TREE:
1246 error = got_diff_objects_as_trees(id1, id2, "", "",
1247 diff_context, repo, stdout);
1249 case GOT_OBJ_TYPE_COMMIT:
1250 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
1252 error = got_diff_objects_as_commits(id1, id2, diff_context,
1256 error = got_error(GOT_ERR_OBJ_TYPE);
1264 got_worktree_close(worktree);
1266 const struct got_error *repo_error;
1267 repo_error = got_repo_close(repo);
1278 "usage: %s blame [-c commit] [-r repository-path] path\n",
1283 static const struct got_error *
1284 cmd_blame(int argc, char *argv[])
1286 const struct got_error *error;
1287 struct got_repository *repo = NULL;
1288 struct got_worktree *worktree = NULL;
1289 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1290 struct got_object_id *commit_id = NULL;
1291 char *commit_id_str = NULL;
1295 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1300 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1303 commit_id_str = optarg;
1306 repo_path = realpath(optarg, NULL);
1307 if (repo_path == NULL)
1308 err(1, "-r option");
1309 got_path_strip_trailing_slashes(repo_path);
1325 cwd = getcwd(NULL, 0);
1327 error = got_error_prefix_errno("getcwd");
1330 if (repo_path == NULL) {
1331 error = got_worktree_open(&worktree, cwd);
1332 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1338 strdup(got_worktree_get_repo_path(worktree));
1339 if (repo_path == NULL)
1340 error = got_error_prefix_errno("strdup");
1344 repo_path = strdup(cwd);
1345 if (repo_path == NULL) {
1346 error = got_error_prefix_errno("strdup");
1352 error = got_repo_open(&repo, repo_path);
1356 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1361 const char *prefix = got_worktree_get_path_prefix(worktree);
1362 char *p, *worktree_subdir = cwd +
1363 strlen(got_worktree_get_root_path(worktree));
1364 if (asprintf(&p, "%s%s%s%s%s",
1365 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1366 worktree_subdir, worktree_subdir[0] ? "/" : "",
1368 error = got_error_prefix_errno("asprintf");
1371 error = got_repo_map_path(&in_repo_path, repo, p, 0);
1374 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1379 if (commit_id_str == NULL) {
1380 struct got_reference *head_ref;
1381 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1384 error = got_ref_resolve(&commit_id, repo, head_ref);
1385 got_ref_close(head_ref);
1389 error = got_object_resolve_id_str(&commit_id, repo,
1395 error = got_blame(in_repo_path, commit_id, repo, stdout);
1402 got_worktree_close(worktree);
1404 const struct got_error *repo_error;
1405 repo_error = got_repo_close(repo);
1416 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1422 print_entry(struct got_tree_entry *te, const char *id, const char *path,
1423 const char *root_path)
1425 int is_root_path = (strcmp(path, root_path) == 0);
1427 path += strlen(root_path);
1428 while (path[0] == '/')
1431 printf("%s%s%s%s%s\n", id ? id : "", path,
1432 is_root_path ? "" : "/", te->name,
1433 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
1436 static const struct got_error *
1437 print_tree(const char *path, struct got_object_id *commit_id,
1438 int show_ids, int recurse, const char *root_path,
1439 struct got_repository *repo)
1441 const struct got_error *err = NULL;
1442 struct got_object_id *tree_id = NULL;
1443 struct got_tree_object *tree = NULL;
1444 const struct got_tree_entries *entries;
1445 struct got_tree_entry *te;
1447 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1451 err = got_object_open_as_tree(&tree, repo, tree_id);
1454 entries = got_object_tree_get_entries(tree);
1455 te = SIMPLEQ_FIRST(&entries->head);
1459 if (sigint_received || sigpipe_received)
1464 err = got_object_id_str(&id_str, te->id);
1467 if (asprintf(&id, "%s ", id_str) == -1) {
1468 err = got_error_prefix_errno("asprintf");
1474 print_entry(te, id, path, root_path);
1477 if (recurse && S_ISDIR(te->mode)) {
1479 if (asprintf(&child_path, "%s%s%s", path,
1480 path[0] == '/' && path[1] == '\0' ? "" : "/",
1482 err = got_error_prefix_errno("asprintf");
1485 err = print_tree(child_path, commit_id, show_ids, 1,
1492 te = SIMPLEQ_NEXT(te, entry);
1496 got_object_tree_close(tree);
1501 static const struct got_error *
1502 cmd_tree(int argc, char *argv[])
1504 const struct got_error *error;
1505 struct got_repository *repo = NULL;
1506 struct got_worktree *worktree = NULL;
1508 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1509 struct got_object_id *commit_id = NULL;
1510 char *commit_id_str = NULL;
1511 int show_ids = 0, recurse = 0;
1515 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1520 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1523 commit_id_str = optarg;
1526 repo_path = realpath(optarg, NULL);
1527 if (repo_path == NULL)
1528 err(1, "-r option");
1529 got_path_strip_trailing_slashes(repo_path);
1553 cwd = getcwd(NULL, 0);
1555 error = got_error_prefix_errno("getcwd");
1558 if (repo_path == NULL) {
1559 error = got_worktree_open(&worktree, cwd);
1560 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1566 strdup(got_worktree_get_repo_path(worktree));
1567 if (repo_path == NULL)
1568 error = got_error_prefix_errno("strdup");
1572 repo_path = strdup(cwd);
1573 if (repo_path == NULL) {
1574 error = got_error_prefix_errno("strdup");
1580 error = got_repo_open(&repo, repo_path);
1584 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1590 char *p, *worktree_subdir = cwd +
1591 strlen(got_worktree_get_root_path(worktree));
1592 if (asprintf(&p, "%s/%s",
1593 got_worktree_get_path_prefix(worktree),
1594 worktree_subdir) == -1) {
1595 error = got_error_prefix_errno("asprintf");
1598 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1605 if (in_repo_path == NULL) {
1606 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1611 if (commit_id_str == NULL) {
1612 struct got_reference *head_ref;
1613 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1616 error = got_ref_resolve(&commit_id, repo, head_ref);
1617 got_ref_close(head_ref);
1621 error = got_object_resolve_id_str(&commit_id, repo,
1627 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1628 in_repo_path, repo);
1635 got_worktree_close(worktree);
1637 const struct got_error *repo_error;
1638 repo_error = got_repo_close(repo);
1648 fprintf(stderr, "usage: %s status [path]\n", getprogname());
1652 static const struct got_error *
1653 print_status(void *arg, unsigned char status, const char *path,
1654 struct got_object_id *id)
1656 printf("%c %s\n", status, path);
1660 static const struct got_error *
1661 cmd_status(int argc, char *argv[])
1663 const struct got_error *error = NULL;
1664 struct got_repository *repo = NULL;
1665 struct got_worktree *worktree = NULL;
1666 char *cwd = NULL, *path = NULL;
1669 while ((ch = getopt(argc, argv, "")) != -1) {
1681 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1685 cwd = getcwd(NULL, 0);
1687 error = got_error_prefix_errno("getcwd");
1691 error = got_worktree_open(&worktree, cwd);
1698 error = got_error_prefix_errno("strdup");
1701 } else if (argc == 1) {
1702 error = got_worktree_resolve_path(&path, worktree, argv[0]);
1708 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1712 error = apply_unveil(got_repo_get_path(repo), 1,
1713 got_worktree_get_root_path(worktree), 0);
1717 error = got_worktree_status(worktree, path, repo, print_status, NULL,
1718 check_cancelled, NULL);
1729 "usage: %s ref [-r repository] -l | -d name | name object\n",
1734 static const struct got_error *
1735 list_refs(struct got_repository *repo)
1737 static const struct got_error *err = NULL;
1738 struct got_reflist_head refs;
1739 struct got_reflist_entry *re;
1741 SIMPLEQ_INIT(&refs);
1742 err = got_ref_list(&refs, repo);
1746 SIMPLEQ_FOREACH(re, &refs, entry) {
1748 refstr = got_ref_to_str(re->ref);
1750 return got_error_prefix_errno("got_ref_to_str");
1751 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
1755 got_ref_list_free(&refs);
1759 static const struct got_error *
1760 delete_ref(struct got_repository *repo, const char *refname)
1762 const struct got_error *err = NULL;
1763 struct got_reference *ref;
1765 err = got_ref_open(&ref, repo, refname, 0);
1769 err = got_ref_delete(ref, repo);
1774 static const struct got_error *
1775 add_ref(struct got_repository *repo, const char *refname, const char *id_str)
1777 const struct got_error *err = NULL;
1778 struct got_object_id *id;
1779 struct got_reference *ref = NULL;
1781 err = got_object_resolve_id_str(&id, repo, id_str);
1785 err = got_ref_alloc(&ref, refname, id);
1789 err = got_ref_write(ref, repo);
1797 static const struct got_error *
1798 cmd_ref(int argc, char *argv[])
1800 const struct got_error *error = NULL;
1801 struct got_repository *repo = NULL;
1802 struct got_worktree *worktree = NULL;
1803 char *cwd = NULL, *repo_path = NULL;
1804 int ch, do_list = 0;
1805 const char *delref = NULL;
1807 /* TODO: Add -s option for adding symbolic references. */
1808 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
1814 repo_path = realpath(optarg, NULL);
1815 if (repo_path == NULL)
1816 err(1, "-r option");
1817 got_path_strip_trailing_slashes(repo_path);
1828 if (do_list && delref)
1829 errx(1, "-l and -d options are mutually exclusive\n");
1834 if (do_list || delref) {
1837 } else if (argc != 2)
1842 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
1846 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1847 "sendfd unveil", NULL) == -1)
1851 cwd = getcwd(NULL, 0);
1853 error = got_error_prefix_errno("getcwd");
1857 if (repo_path == NULL) {
1858 error = got_worktree_open(&worktree, cwd);
1859 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1865 strdup(got_worktree_get_repo_path(worktree));
1866 if (repo_path == NULL)
1867 error = got_error_prefix_errno("strdup");
1871 repo_path = strdup(cwd);
1872 if (repo_path == NULL) {
1873 error = got_error_prefix_errno("strdup");
1879 error = got_repo_open(&repo, repo_path);
1883 error = apply_unveil(got_repo_get_path(repo), do_list,
1884 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1889 error = list_refs(repo);
1891 error = delete_ref(repo, delref);
1893 error = add_ref(repo, argv[0], argv[1]);
1896 got_repo_close(repo);
1898 got_worktree_close(worktree);
1907 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
1911 static const struct got_error *
1912 cmd_add(int argc, char *argv[])
1914 const struct got_error *error = NULL;
1915 struct got_repository *repo = NULL;
1916 struct got_worktree *worktree = NULL;
1918 struct got_pathlist_head paths;
1919 struct got_pathlist_entry *pe;
1924 while ((ch = getopt(argc, argv, "")) != -1) {
1938 /* make sure each file exists before doing anything halfway */
1939 for (x = 0; x < argc; x++) {
1940 char *path = realpath(argv[x], NULL);
1942 error = got_error_prefix_errno2("realpath", argv[x]);
1948 cwd = getcwd(NULL, 0);
1950 error = got_error_prefix_errno("getcwd");
1954 error = got_worktree_open(&worktree, cwd);
1958 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1962 error = apply_unveil(got_repo_get_path(repo), 1,
1963 got_worktree_get_root_path(worktree), 0);
1967 for (x = 0; x < argc; x++) {
1968 char *path = realpath(argv[x], NULL);
1970 error = got_error_prefix_errno2("realpath", argv[x]);
1974 got_path_strip_trailing_slashes(path);
1975 error = got_pathlist_insert(&pe, &paths, path, NULL);
1981 error = got_worktree_schedule_add(worktree, &paths, print_status,
1985 got_repo_close(repo);
1987 got_worktree_close(worktree);
1988 TAILQ_FOREACH(pe, &paths, entry)
1989 free((char *)pe->path);
1990 got_pathlist_free(&paths);
1998 fprintf(stderr, "usage: %s rm [-f] file-path\n", getprogname());
2002 static const struct got_error *
2003 cmd_rm(int argc, char *argv[])
2005 const struct got_error *error = NULL;
2006 struct got_worktree *worktree = NULL;
2007 struct got_repository *repo = NULL;
2008 char *cwd = NULL, *path = NULL;
2009 int ch, delete_local_mods = 0;
2011 while ((ch = getopt(argc, argv, "f")) != -1) {
2014 delete_local_mods = 1;
2028 path = realpath(argv[0], NULL);
2030 error = got_error_prefix_errno2("realpath", argv[0]);
2033 got_path_strip_trailing_slashes(path);
2035 cwd = getcwd(NULL, 0);
2037 error = got_error_prefix_errno("getcwd");
2040 error = got_worktree_open(&worktree, cwd);
2044 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2048 error = apply_unveil(got_repo_get_path(repo), 1,
2049 got_worktree_get_root_path(worktree), 0);
2053 error = got_worktree_schedule_delete(worktree, path, delete_local_mods,
2054 print_status, NULL, repo);
2059 got_repo_close(repo);
2061 got_worktree_close(worktree);
2070 fprintf(stderr, "usage: %s revert file-path\n", getprogname());
2075 revert_progress(void *arg, unsigned char status, const char *path)
2077 while (path[0] == '/')
2079 printf("%c %s\n", status, path);
2082 static const struct got_error *
2083 cmd_revert(int argc, char *argv[])
2085 const struct got_error *error = NULL;
2086 struct got_worktree *worktree = NULL;
2087 struct got_repository *repo = NULL;
2088 char *cwd = NULL, *path = NULL;
2091 while ((ch = getopt(argc, argv, "")) != -1) {
2105 path = realpath(argv[0], NULL);
2107 error = got_error_prefix_errno2("realpath", argv[0]);
2110 got_path_strip_trailing_slashes(path);
2112 cwd = getcwd(NULL, 0);
2114 error = got_error_prefix_errno("getcwd");
2117 error = got_worktree_open(&worktree, cwd);
2121 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2125 error = apply_unveil(got_repo_get_path(repo), 1,
2126 got_worktree_get_root_path(worktree), 0);
2130 error = got_worktree_revert(worktree, path,
2131 revert_progress, NULL, repo);
2136 got_repo_close(repo);
2138 got_worktree_close(worktree);
2147 fprintf(stderr, "usage: %s commit [-m msg] file-path\n", getprogname());
2152 spawn_editor(const char *file)
2154 char *argp[] = { "sh", "-c", NULL, NULL };
2155 char *editor, *editp;
2157 sig_t sighup, sigint, sigquit;
2160 editor = getenv("VISUAL");
2162 editor = getenv("EDITOR");
2166 if (asprintf(&editp, "%s %s", editor, file) == -1)
2171 sighup = signal(SIGHUP, SIG_IGN);
2172 sigint = signal(SIGINT, SIG_IGN);
2173 sigquit = signal(SIGQUIT, SIG_IGN);
2175 switch (pid = fork()) {
2179 execv(_PATH_BSHELL, argp);
2185 while (waitpid(pid, &st, 0) == -1)
2190 (void)signal(SIGHUP, sighup);
2191 (void)signal(SIGINT, sigint);
2192 (void)signal(SIGQUIT, sigquit);
2194 if (!WIFEXITED(st)) {
2199 return WEXITSTATUS(st);
2202 static const struct got_error *
2203 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
2206 struct got_pathlist_entry *pe;
2207 const struct got_error *err = NULL;
2208 char *tmppath = NULL;
2209 char *tmpfile = NULL;
2210 char *cmdline_log = (char *)arg;
2212 struct stat st, st2;
2217 /* if a message was specified on the command line, just use it */
2218 if (cmdline_log != NULL && strlen(cmdline_log) != 0) {
2219 len = strlen(cmdline_log) + 1;
2220 *logmsg = malloc(len + 1);
2221 strlcpy(*logmsg, cmdline_log, len);
2225 tmppath = getenv("TMPDIR");
2226 if (tmppath == NULL || strlen(tmppath) == 0)
2229 if (asprintf(&tmpfile, "%s/got-XXXXXXXXXX", tmppath) == -1) {
2230 err = got_error_prefix_errno("asprintf");
2234 fd = mkstemp(tmpfile);
2236 err = got_error_prefix_errno("mktemp");
2242 "# changes to be committed:\n");
2244 TAILQ_FOREACH(pe, commitable_paths, entry) {
2245 struct got_commitable *ct = pe->data;
2246 dprintf(fd, "# %c %s\n", ct->status, pe->path);
2250 if (stat(tmpfile, &st) == -1) {
2251 err = got_error_prefix_errno2("stat", tmpfile);
2255 if (spawn_editor(tmpfile) == -1) {
2256 err = got_error_prefix_errno("failed spawning editor");
2260 if (stat(tmpfile, &st2) == -1) {
2261 err = got_error_prefix_errno("stat");
2265 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size) {
2266 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2267 "no changes made to commit message, aborting");
2271 /* remove comments */
2272 *logmsg = malloc(st2.st_size + 1);
2275 fp = fopen(tmpfile, "r");
2276 while (fgets(buf, sizeof(buf), fp) != NULL) {
2277 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
2279 len = strlcat(*logmsg, buf, st2.st_size);
2283 while (len > 0 && (*logmsg)[len - 1] == '\n') {
2284 (*logmsg)[len - 1] = '\0';
2289 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2290 "commit message cannot be empty, aborting");
2305 static const struct got_error *
2306 cmd_commit(int argc, char *argv[])
2308 const struct got_error *error = NULL;
2309 struct got_worktree *worktree = NULL;
2310 struct got_repository *repo = NULL;
2311 char *cwd = NULL, *path = NULL, *id_str = NULL;
2312 struct got_object_id *id = NULL;
2313 const char *logmsg = NULL;
2314 const char *got_author = getenv("GOT_AUTHOR");
2317 while ((ch = getopt(argc, argv, "m:")) != -1) {
2332 path = realpath(argv[0], NULL);
2334 error = got_error_prefix_errno2("realpath", argv[0]);
2337 got_path_strip_trailing_slashes(path);
2338 } else if (argc != 0)
2341 if (got_author == NULL) {
2342 /* TODO: Look current user up in password database */
2343 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
2347 cwd = getcwd(NULL, 0);
2349 error = got_error_prefix_errno("getcwd");
2352 error = got_worktree_open(&worktree, cwd);
2356 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2361 error = apply_unveil(got_repo_get_path(repo), 0,
2362 got_worktree_get_root_path(worktree), 0);
2367 error = got_worktree_commit(&id, worktree, path, got_author, NULL,
2368 collect_commit_logmsg, (void *)logmsg, print_status, NULL, repo);
2372 error = got_object_id_str(&id_str, id);
2375 printf("created commit %s\n", id_str);
2378 got_repo_close(repo);
2380 got_worktree_close(worktree);