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"
47 #include "got_opentemp.h"
50 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
53 static volatile sig_atomic_t sigint_received;
54 static volatile sig_atomic_t sigpipe_received;
57 catch_sigint(int signo)
63 catch_sigpipe(int signo)
71 const struct got_error *(*cmd_main)(int, char *[]);
72 void (*cmd_usage)(void);
75 __dead static void usage(void);
76 __dead static void usage_init(void);
77 __dead static void usage_checkout(void);
78 __dead static void usage_update(void);
79 __dead static void usage_log(void);
80 __dead static void usage_diff(void);
81 __dead static void usage_blame(void);
82 __dead static void usage_tree(void);
83 __dead static void usage_status(void);
84 __dead static void usage_ref(void);
85 __dead static void usage_branch(void);
86 __dead static void usage_add(void);
87 __dead static void usage_rm(void);
88 __dead static void usage_revert(void);
89 __dead static void usage_commit(void);
90 __dead static void usage_cherrypick(void);
91 __dead static void usage_backout(void);
93 static const struct got_error* cmd_init(int, char *[]);
94 static const struct got_error* cmd_checkout(int, char *[]);
95 static const struct got_error* cmd_update(int, char *[]);
96 static const struct got_error* cmd_log(int, char *[]);
97 static const struct got_error* cmd_diff(int, char *[]);
98 static const struct got_error* cmd_blame(int, char *[]);
99 static const struct got_error* cmd_tree(int, char *[]);
100 static const struct got_error* cmd_status(int, char *[]);
101 static const struct got_error* cmd_ref(int, char *[]);
102 static const struct got_error* cmd_branch(int, char *[]);
103 static const struct got_error* cmd_add(int, char *[]);
104 static const struct got_error* cmd_rm(int, char *[]);
105 static const struct got_error* cmd_revert(int, char *[]);
106 static const struct got_error* cmd_commit(int, char *[]);
107 static const struct got_error* cmd_cherrypick(int, char *[]);
108 static const struct got_error* cmd_backout(int, char *[]);
110 static struct cmd got_commands[] = {
111 { "init", cmd_init, usage_init },
112 { "checkout", cmd_checkout, usage_checkout },
113 { "update", cmd_update, usage_update },
114 { "log", cmd_log, usage_log },
115 { "diff", cmd_diff, usage_diff },
116 { "blame", cmd_blame, usage_blame },
117 { "tree", cmd_tree, usage_tree },
118 { "status", cmd_status, usage_status },
119 { "ref", cmd_ref, usage_ref },
120 { "branch", cmd_branch, usage_branch },
121 { "add", cmd_add, usage_add },
122 { "rm", cmd_rm, usage_rm },
123 { "revert", cmd_revert, usage_revert },
124 { "commit", cmd_commit, usage_commit },
125 { "cherrypick", cmd_cherrypick, usage_cherrypick },
126 { "backout", cmd_backout, usage_backout },
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]);
187 fprintf(stderr, "usage: %s [-h] command [arg ...]\n", getprogname());
191 static const struct got_error *
192 get_editor(char **abspath)
194 const struct got_error *err = NULL;
197 editor = getenv("VISUAL");
199 editor = getenv("EDITOR");
202 err = got_path_find_prog(abspath, editor);
207 if (*abspath == NULL) {
208 *abspath = strdup("/bin/ed");
209 if (*abspath == NULL)
210 return got_error_from_errno("strdup");
216 static const struct got_error *
217 apply_unveil(const char *repo_path, int repo_read_only,
218 const char *worktree_path, int create_worktree)
220 const struct got_error *err;
222 if (create_worktree) {
223 /* Pre-create work tree path to avoid unveiling its parents. */
224 err = got_path_mkdir(worktree_path);
226 if (errno == EEXIST) {
227 if (got_path_dir_is_empty(worktree_path)) {
231 err = got_error_path(worktree_path,
232 GOT_ERR_DIR_NOT_EMPTY);
236 if (err && (err->code != GOT_ERR_ERRNO || errno != EISDIR))
240 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
241 return got_error_from_errno2("unveil", repo_path);
243 if (worktree_path && unveil(worktree_path, "rwc") != 0)
244 return got_error_from_errno2("unveil", worktree_path);
246 if (unveil("/tmp", "rwc") != 0)
247 return got_error_from_errno2("unveil", "/tmp");
249 err = got_privsep_unveil_exec_helpers();
253 if (unveil(NULL, NULL) != 0)
254 return got_error_from_errno("unveil");
262 fprintf(stderr, "usage: %s init path\n", getprogname());
266 static const struct got_error *
267 cmd_init(int argc, char *argv[])
269 const struct got_error *error = NULL;
270 char *repo_path = NULL;
273 while ((ch = getopt(argc, argv, "")) != -1) {
285 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
291 repo_path = strdup(argv[0]);
292 if (repo_path == NULL)
293 return got_error_from_errno("strdup");
295 got_path_strip_trailing_slashes(repo_path);
297 error = got_path_mkdir(repo_path);
299 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
302 error = apply_unveil(repo_path, 0, NULL, 0);
306 error = got_repo_init(repo_path);
318 fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
319 "[-p prefix] repository-path [worktree-path]\n", getprogname());
324 checkout_progress(void *arg, unsigned char status, const char *path)
326 char *worktree_path = arg;
328 /* Base commit bump happens silently. */
329 if (status == GOT_STATUS_BUMP_BASE)
332 while (path[0] == '/')
335 printf("%c %s/%s\n", status, worktree_path, path);
338 static const struct got_error *
339 check_cancelled(void *arg)
341 if (sigint_received || sigpipe_received)
342 return got_error(GOT_ERR_CANCELLED);
346 static const struct got_error *
347 check_linear_ancestry(struct got_object_id *commit_id,
348 struct got_object_id *base_commit_id, struct got_repository *repo)
350 const struct got_error *err = NULL;
351 struct got_object_id *yca_id;
353 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
354 commit_id, base_commit_id, repo);
359 return got_error(GOT_ERR_ANCESTRY);
362 * Require a straight line of history between the target commit
363 * and the work tree's base commit.
365 * Non-linear situations such as this require a rebase:
367 * (commit) D F (base_commit)
375 * 'got update' only handles linear cases:
376 * Update forwards in time: A (base/yca) - B - C - D (commit)
377 * Update backwards in time: D (base) - C - B - A (commit/yca)
379 if (got_object_id_cmp(commit_id, yca_id) != 0 &&
380 got_object_id_cmp(base_commit_id, yca_id) != 0)
381 return got_error(GOT_ERR_ANCESTRY);
387 static const struct got_error *
388 check_same_branch(struct got_object_id *commit_id,
389 struct got_reference *head_ref, struct got_repository *repo)
391 const struct got_error *err = NULL;
392 struct got_commit_graph *graph = NULL;
393 struct got_object_id *head_commit_id = NULL;
394 int is_same_branch = 0;
396 err = got_ref_resolve(&head_commit_id, repo, head_ref);
400 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
404 err = got_commit_graph_iter_start(graph, head_commit_id, repo);
409 struct got_object_id *id;
410 err = got_commit_graph_iter_next(&id, graph);
412 if (err->code == GOT_ERR_ITER_COMPLETED) {
416 else if (err->code != GOT_ERR_ITER_NEED_MORE)
418 err = got_commit_graph_fetch_commits(graph, 1,
425 if (got_object_id_cmp(id, commit_id) == 0) {
433 got_commit_graph_close(graph);
434 free(head_commit_id);
435 if (!err && !is_same_branch)
436 err = got_error(GOT_ERR_ANCESTRY);
440 static const struct got_error *
441 cmd_checkout(int argc, char *argv[])
443 const struct got_error *error = NULL;
444 struct got_repository *repo = NULL;
445 struct got_reference *head_ref = NULL;
446 struct got_worktree *worktree = NULL;
447 char *repo_path = NULL;
448 char *worktree_path = NULL;
449 const char *path_prefix = "";
450 const char *branch_name = GOT_REF_HEAD;
451 char *commit_id_str = NULL;
452 int ch, same_path_prefix;
454 while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
457 branch_name = optarg;
460 commit_id_str = strdup(optarg);
461 if (commit_id_str == NULL)
462 return got_error_from_errno("strdup");
465 path_prefix = optarg;
477 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
478 "unveil", NULL) == -1)
482 char *cwd, *base, *dotgit;
483 repo_path = realpath(argv[0], NULL);
484 if (repo_path == NULL)
485 return got_error_from_errno2("realpath", argv[0]);
486 cwd = getcwd(NULL, 0);
488 error = got_error_from_errno("getcwd");
491 if (path_prefix[0]) {
492 base = basename(path_prefix);
494 error = got_error_from_errno2("basename",
499 base = basename(repo_path);
501 error = got_error_from_errno2("basename",
506 dotgit = strstr(base, ".git");
509 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
510 error = got_error_from_errno("asprintf");
515 } else if (argc == 2) {
516 repo_path = realpath(argv[0], NULL);
517 if (repo_path == NULL) {
518 error = got_error_from_errno2("realpath", argv[0]);
521 worktree_path = realpath(argv[1], NULL);
522 if (worktree_path == NULL) {
523 error = got_error_from_errno2("realpath", argv[1]);
529 got_path_strip_trailing_slashes(repo_path);
530 got_path_strip_trailing_slashes(worktree_path);
532 error = got_repo_open(&repo, repo_path);
536 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path, 1);
540 error = got_ref_open(&head_ref, repo, branch_name, 0);
544 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
545 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
548 error = got_worktree_open(&worktree, worktree_path);
552 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
556 if (!same_path_prefix) {
557 error = got_error(GOT_ERR_PATH_PREFIX);
562 struct got_object_id *commit_id;
563 error = got_repo_match_object_id_prefix(&commit_id,
564 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
567 error = check_linear_ancestry(commit_id,
568 got_worktree_get_base_commit_id(worktree), repo);
573 error = check_same_branch(commit_id, head_ref, repo);
576 error = got_worktree_set_base_commit_id(worktree, repo,
583 error = got_worktree_checkout_files(worktree, "", repo,
584 checkout_progress, worktree_path, check_cancelled, NULL);
588 printf("Now shut up and hack\n");
600 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path]\n",
606 update_progress(void *arg, unsigned char status, const char *path)
608 int *did_something = arg;
610 if (status == GOT_STATUS_EXISTS)
615 /* Base commit bump happens silently. */
616 if (status == GOT_STATUS_BUMP_BASE)
619 while (path[0] == '/')
621 printf("%c %s\n", status, path);
624 static const struct got_error *
625 switch_head_ref(struct got_reference *head_ref,
626 struct got_object_id *commit_id, struct got_worktree *worktree,
627 struct got_repository *repo)
629 const struct got_error *err = NULL;
631 int ref_has_moved = 0;
633 /* Trivial case: switching between two different references. */
634 if (strcmp(got_ref_get_name(head_ref),
635 got_worktree_get_head_ref_name(worktree)) != 0) {
636 printf("Switching work tree from %s to %s\n",
637 got_worktree_get_head_ref_name(worktree),
638 got_ref_get_name(head_ref));
639 return got_worktree_set_head_ref(worktree, head_ref);
642 err = check_linear_ancestry(commit_id,
643 got_worktree_get_base_commit_id(worktree), repo);
645 if (err->code != GOT_ERR_ANCESTRY)
652 /* Switching to a rebased branch with the same reference name. */
653 err = got_object_id_str(&base_id_str,
654 got_worktree_get_base_commit_id(worktree));
657 printf("Reference %s now points at a different branch\n",
658 got_worktree_get_head_ref_name(worktree));
659 printf("Switching work tree from %s to %s\n", base_id_str,
660 got_worktree_get_head_ref_name(worktree));
664 static const struct got_error *
665 cmd_update(int argc, char *argv[])
667 const struct got_error *error = NULL;
668 struct got_repository *repo = NULL;
669 struct got_worktree *worktree = NULL;
670 char *worktree_path = NULL, *path = NULL;
671 struct got_object_id *commit_id = NULL;
672 char *commit_id_str = NULL;
673 const char *branch_name = NULL;
674 struct got_reference *head_ref = NULL;
675 int ch, did_something = 0;
677 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
680 branch_name = optarg;
683 commit_id_str = strdup(optarg);
684 if (commit_id_str == NULL)
685 return got_error_from_errno("strdup");
697 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
698 "unveil", NULL) == -1)
701 worktree_path = getcwd(NULL, 0);
702 if (worktree_path == NULL) {
703 error = got_error_from_errno("getcwd");
706 error = got_worktree_open(&worktree, worktree_path);
713 error = got_error_from_errno("strdup");
716 } else if (argc == 1) {
717 error = got_worktree_resolve_path(&path, worktree, argv[0]);
723 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
727 error = apply_unveil(got_repo_get_path(repo), 0,
728 got_worktree_get_root_path(worktree), 0);
732 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
733 got_worktree_get_head_ref_name(worktree), 0);
736 if (commit_id_str == NULL) {
737 error = got_ref_resolve(&commit_id, repo, head_ref);
740 error = got_object_id_str(&commit_id_str, commit_id);
744 error = got_repo_match_object_id_prefix(&commit_id,
745 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
751 struct got_object_id *head_commit_id;
752 if (strlen(path) != 0) {
753 fprintf(stderr, "%s: switching between branches "
754 "requires that the entire work tree "
755 "gets updated, not just '%s'\n",
756 getprogname(), path);
757 error = got_error(GOT_ERR_BAD_PATH);
760 error = got_ref_resolve(&head_commit_id, repo, head_ref);
763 error = check_linear_ancestry(commit_id, head_commit_id, repo);
764 free(head_commit_id);
767 error = check_same_branch(commit_id, head_ref, repo);
770 error = switch_head_ref(head_ref, commit_id, worktree, repo);
774 error = check_linear_ancestry(commit_id,
775 got_worktree_get_base_commit_id(worktree), repo);
777 if (error->code == GOT_ERR_ANCESTRY)
778 error = got_error(GOT_ERR_BRANCH_MOVED);
781 error = check_same_branch(commit_id, head_ref, repo);
786 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
788 error = got_worktree_set_base_commit_id(worktree, repo,
794 error = got_worktree_checkout_files(worktree, path, repo,
795 update_progress, &did_something, check_cancelled, NULL);
800 printf("Updated to commit %s\n", commit_id_str);
802 printf("Already up-to-date\n");
811 static const struct got_error *
812 print_patch(struct got_commit_object *commit, struct got_object_id *id,
813 int diff_context, struct got_repository *repo)
815 const struct got_error *err = NULL;
816 struct got_tree_object *tree1 = NULL, *tree2;
817 struct got_object_qid *qid;
818 char *id_str1 = NULL, *id_str2;
819 struct got_diff_blob_output_unidiff_arg arg;
821 err = got_object_open_as_tree(&tree2, repo,
822 got_object_commit_get_tree_id(commit));
826 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
828 struct got_commit_object *pcommit;
830 err = got_object_open_as_commit(&pcommit, repo, qid->id);
834 err = got_object_open_as_tree(&tree1, repo,
835 got_object_commit_get_tree_id(pcommit));
836 got_object_commit_close(pcommit);
840 err = got_object_id_str(&id_str1, qid->id);
845 err = got_object_id_str(&id_str2, id);
849 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
850 arg.diff_context = diff_context;
851 arg.outfile = stdout;
852 err = got_diff_tree(tree1, tree2, "", "", repo,
853 got_diff_blob_output_unidiff, &arg);
856 got_object_tree_close(tree1);
857 got_object_tree_close(tree2);
864 get_datestr(time_t *time, char *datebuf)
866 char *p, *s = ctime_r(time, datebuf);
873 static const struct got_error *
874 print_commit(struct got_commit_object *commit, struct got_object_id *id,
875 struct got_repository *repo, int show_patch, int diff_context,
876 struct got_reflist_head *refs)
878 const struct got_error *err = NULL;
879 char *id_str, *datestr, *logmsg0, *logmsg, *line;
881 time_t committer_time;
882 const char *author, *committer;
883 char *refs_str = NULL;
884 struct got_reflist_entry *re;
886 SIMPLEQ_FOREACH(re, refs, entry) {
889 if (got_object_id_cmp(re->id, id) != 0)
891 name = got_ref_get_name(re->ref);
892 if (strcmp(name, GOT_REF_HEAD) == 0)
894 if (strncmp(name, "refs/", 5) == 0)
896 if (strncmp(name, "got/", 4) == 0)
898 if (strncmp(name, "heads/", 6) == 0)
900 if (strncmp(name, "remotes/", 8) == 0)
903 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
905 err = got_error_from_errno("asprintf");
911 err = got_object_id_str(&id_str, id);
915 printf("-----------------------------------------------\n");
916 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
917 refs_str ? refs_str : "", refs_str ? ")" : "");
922 printf("from: %s\n", got_object_commit_get_author(commit));
923 committer_time = got_object_commit_get_committer_time(commit);
924 datestr = get_datestr(&committer_time, datebuf);
925 printf("date: %s UTC\n", datestr);
926 author = got_object_commit_get_author(commit);
927 committer = got_object_commit_get_committer(commit);
928 if (strcmp(author, committer) != 0)
929 printf("via: %s\n", committer);
930 if (got_object_commit_get_nparents(commit) > 1) {
931 const struct got_object_id_queue *parent_ids;
932 struct got_object_qid *qid;
934 parent_ids = got_object_commit_get_parent_ids(commit);
935 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
936 err = got_object_id_str(&id_str, qid->id);
939 printf("parent %d: %s\n", n++, id_str);
944 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
946 return got_error_from_errno("strdup");
950 line = strsep(&logmsg, "\n");
952 printf(" %s\n", line);
957 err = print_patch(commit, id, diff_context, repo);
962 if (fflush(stdout) != 0 && err == NULL)
963 err = got_error_from_errno("fflush");
967 static const struct got_error *
968 print_commits(struct got_object_id *root_id, struct got_repository *repo,
969 char *path, int show_patch, int diff_context, int limit,
970 int first_parent_traversal, struct got_reflist_head *refs)
972 const struct got_error *err;
973 struct got_commit_graph *graph;
975 err = got_commit_graph_open(&graph, root_id, path,
976 first_parent_traversal, repo);
979 err = got_commit_graph_iter_start(graph, root_id, repo);
983 struct got_commit_object *commit;
984 struct got_object_id *id;
986 if (sigint_received || sigpipe_received)
989 err = got_commit_graph_iter_next(&id, graph);
991 if (err->code == GOT_ERR_ITER_COMPLETED) {
995 if (err->code != GOT_ERR_ITER_NEED_MORE)
997 err = got_commit_graph_fetch_commits(graph, 1, repo);
1006 err = got_object_open_as_commit(&commit, repo, id);
1009 err = print_commit(commit, id, repo, show_patch, diff_context,
1011 got_object_commit_close(commit);
1012 if (err || (limit && --limit == 0))
1016 got_commit_graph_close(graph);
1023 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
1024 "[-r repository-path] [path]\n", getprogname());
1028 static const struct got_error *
1029 cmd_log(int argc, char *argv[])
1031 const struct got_error *error;
1032 struct got_repository *repo = NULL;
1033 struct got_worktree *worktree = NULL;
1034 struct got_commit_object *commit = NULL;
1035 struct got_object_id *id = NULL;
1036 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1037 char *start_commit = NULL;
1038 int diff_context = 3, ch;
1039 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1041 struct got_reflist_head refs;
1043 SIMPLEQ_INIT(&refs);
1046 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1052 while ((ch = getopt(argc, argv, "bpc:C:l:r:")) != -1) {
1055 first_parent_traversal = 1;
1061 start_commit = optarg;
1064 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1067 err(1, "-C option %s", errstr);
1070 limit = strtonum(optarg, 1, INT_MAX, &errstr);
1072 err(1, "-l option %s", errstr);
1075 repo_path = realpath(optarg, NULL);
1076 if (repo_path == NULL)
1077 err(1, "-r option");
1078 got_path_strip_trailing_slashes(repo_path);
1089 cwd = getcwd(NULL, 0);
1091 error = got_error_from_errno("getcwd");
1095 error = got_worktree_open(&worktree, cwd);
1096 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1103 error = got_error_from_errno("strdup");
1106 } else if (argc == 1) {
1108 error = got_worktree_resolve_path(&path, worktree,
1113 path = strdup(argv[0]);
1115 error = got_error_from_errno("strdup");
1122 if (repo_path == NULL) {
1123 repo_path = worktree ?
1124 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1126 if (repo_path == NULL) {
1127 error = got_error_from_errno("strdup");
1131 error = got_repo_open(&repo, repo_path);
1135 error = apply_unveil(got_repo_get_path(repo), 1,
1136 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1140 if (start_commit == NULL) {
1141 struct got_reference *head_ref;
1142 error = got_ref_open(&head_ref, repo,
1143 worktree ? got_worktree_get_head_ref_name(worktree)
1147 error = got_ref_resolve(&id, repo, head_ref);
1148 got_ref_close(head_ref);
1151 error = got_object_open_as_commit(&commit, repo, id);
1153 struct got_reference *ref;
1154 error = got_ref_open(&ref, repo, start_commit, 0);
1155 if (error == NULL) {
1157 error = got_ref_resolve(&id, repo, ref);
1161 error = got_object_get_type(&obj_type, repo, id);
1164 if (obj_type == GOT_OBJ_TYPE_TAG) {
1165 struct got_tag_object *tag;
1166 error = got_object_open_as_tag(&tag, repo, id);
1169 if (got_object_tag_get_object_type(tag) !=
1170 GOT_OBJ_TYPE_COMMIT) {
1171 got_object_tag_close(tag);
1172 error = got_error(GOT_ERR_OBJ_TYPE);
1176 id = got_object_id_dup(
1177 got_object_tag_get_object_id(tag));
1179 error = got_error_from_errno(
1180 "got_object_id_dup");
1181 got_object_tag_close(tag);
1184 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1185 error = got_error(GOT_ERR_OBJ_TYPE);
1188 error = got_object_open_as_commit(&commit, repo, id);
1192 if (commit == NULL) {
1193 error = got_repo_match_object_id_prefix(&id,
1194 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1202 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1207 path = in_repo_path;
1210 error = got_ref_list(&refs, repo);
1214 error = print_commits(id, repo, path, show_patch,
1215 diff_context, limit, first_parent_traversal, &refs);
1222 got_worktree_close(worktree);
1224 const struct got_error *repo_error;
1225 repo_error = got_repo_close(repo);
1229 got_ref_list_free(&refs);
1236 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1237 "[object1 object2 | path]\n", getprogname());
1241 struct print_diff_arg {
1242 struct got_repository *repo;
1243 struct got_worktree *worktree;
1249 static const struct got_error *
1250 print_diff(void *arg, unsigned char status, const char *path,
1251 struct got_object_id *blob_id, struct got_object_id *commit_id)
1253 struct print_diff_arg *a = arg;
1254 const struct got_error *err = NULL;
1255 struct got_blob_object *blob1 = NULL;
1257 char *abspath = NULL;
1260 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1261 status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1264 if (!a->header_shown) {
1265 printf("diff %s %s\n", a->id_str,
1266 got_worktree_get_root_path(a->worktree));
1267 a->header_shown = 1;
1270 if (status != GOT_STATUS_ADD) {
1271 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1277 if (status != GOT_STATUS_DELETE) {
1278 if (asprintf(&abspath, "%s/%s",
1279 got_worktree_get_root_path(a->worktree), path) == -1) {
1280 err = got_error_from_errno("asprintf");
1284 f2 = fopen(abspath, "r");
1286 err = got_error_from_errno2("fopen", abspath);
1289 if (lstat(abspath, &sb) == -1) {
1290 err = got_error_from_errno2("lstat", abspath);
1296 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1300 got_object_blob_close(blob1);
1301 if (f2 && fclose(f2) != 0 && err == NULL)
1302 err = got_error_from_errno("fclose");
1307 static const struct got_error *
1308 cmd_diff(int argc, char *argv[])
1310 const struct got_error *error;
1311 struct got_repository *repo = NULL;
1312 struct got_worktree *worktree = NULL;
1313 char *cwd = NULL, *repo_path = NULL;
1314 struct got_object_id *id1 = NULL, *id2 = NULL;
1315 const char *id_str1 = NULL, *id_str2 = NULL;
1316 char *label1 = NULL, *label2 = NULL;
1318 int diff_context = 3, ch;
1323 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1328 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1331 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1333 err(1, "-C option %s", errstr);
1336 repo_path = realpath(optarg, NULL);
1337 if (repo_path == NULL)
1338 err(1, "-r option");
1339 got_path_strip_trailing_slashes(repo_path);
1350 cwd = getcwd(NULL, 0);
1352 error = got_error_from_errno("getcwd");
1355 error = got_worktree_open(&worktree, cwd);
1356 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1359 if (worktree == NULL) {
1360 error = got_error(GOT_ERR_NOT_WORKTREE);
1365 "-r option can't be used when diffing a work tree");
1366 repo_path = strdup(got_worktree_get_repo_path(worktree));
1367 if (repo_path == NULL) {
1368 error = got_error_from_errno("strdup");
1372 error = got_worktree_resolve_path(&path, worktree,
1379 error = got_error_from_errno("strdup");
1383 } else if (argc == 2) {
1386 if (worktree && repo_path == NULL) {
1388 strdup(got_worktree_get_repo_path(worktree));
1389 if (repo_path == NULL) {
1390 error = got_error_from_errno("strdup");
1397 if (repo_path == NULL) {
1398 repo_path = getcwd(NULL, 0);
1399 if (repo_path == NULL)
1400 return got_error_from_errno("getcwd");
1403 error = got_repo_open(&repo, repo_path);
1408 error = apply_unveil(got_repo_get_path(repo), 1,
1409 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1414 struct print_diff_arg arg;
1416 error = got_object_id_str(&id_str,
1417 got_worktree_get_base_commit_id(worktree));
1421 arg.worktree = worktree;
1422 arg.diff_context = diff_context;
1423 arg.id_str = id_str;
1424 arg.header_shown = 0;
1426 error = got_worktree_status(worktree, path, repo, print_diff,
1427 &arg, check_cancelled, NULL);
1432 error = got_repo_match_object_id_prefix(&id1, id_str1,
1433 GOT_OBJ_TYPE_ANY, repo);
1435 struct got_reference *ref;
1436 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1438 error = got_ref_open(&ref, repo, id_str1, 0);
1441 label1 = strdup(got_ref_get_name(ref));
1442 if (label1 == NULL) {
1443 error = got_error_from_errno("strdup");
1446 error = got_ref_resolve(&id1, repo, ref);
1451 label1 = strdup(id_str1);
1452 if (label1 == NULL) {
1453 error = got_error_from_errno("strdup");
1458 error = got_repo_match_object_id_prefix(&id2, id_str2,
1459 GOT_OBJ_TYPE_ANY, repo);
1461 struct got_reference *ref;
1462 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1464 error = got_ref_open(&ref, repo, id_str2, 0);
1467 label2 = strdup(got_ref_get_name(ref));
1468 if (label2 == NULL) {
1469 error = got_error_from_errno("strdup");
1472 error = got_ref_resolve(&id2, repo, ref);
1477 label2 = strdup(id_str2);
1478 if (label2 == NULL) {
1479 error = got_error_from_errno("strdup");
1484 error = got_object_get_type(&type1, repo, id1);
1488 error = got_object_get_type(&type2, repo, id2);
1492 if (type1 != type2) {
1493 error = got_error(GOT_ERR_OBJ_TYPE);
1498 case GOT_OBJ_TYPE_BLOB:
1499 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1500 diff_context, repo, stdout);
1502 case GOT_OBJ_TYPE_TREE:
1503 error = got_diff_objects_as_trees(id1, id2, "", "",
1504 diff_context, repo, stdout);
1506 case GOT_OBJ_TYPE_COMMIT:
1507 printf("diff %s %s\n", label1, label2);
1508 error = got_diff_objects_as_commits(id1, id2, diff_context,
1512 error = got_error(GOT_ERR_OBJ_TYPE);
1522 got_worktree_close(worktree);
1524 const struct got_error *repo_error;
1525 repo_error = got_repo_close(repo);
1536 "usage: %s blame [-c commit] [-r repository-path] path\n",
1541 static const struct got_error *
1542 cmd_blame(int argc, char *argv[])
1544 const struct got_error *error;
1545 struct got_repository *repo = NULL;
1546 struct got_worktree *worktree = NULL;
1547 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1548 struct got_object_id *commit_id = NULL;
1549 char *commit_id_str = NULL;
1553 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1558 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1561 commit_id_str = optarg;
1564 repo_path = realpath(optarg, NULL);
1565 if (repo_path == NULL)
1566 err(1, "-r option");
1567 got_path_strip_trailing_slashes(repo_path);
1583 cwd = getcwd(NULL, 0);
1585 error = got_error_from_errno("getcwd");
1588 if (repo_path == NULL) {
1589 error = got_worktree_open(&worktree, cwd);
1590 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1596 strdup(got_worktree_get_repo_path(worktree));
1597 if (repo_path == NULL)
1598 error = got_error_from_errno("strdup");
1602 repo_path = strdup(cwd);
1603 if (repo_path == NULL) {
1604 error = got_error_from_errno("strdup");
1610 error = got_repo_open(&repo, repo_path);
1614 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1619 const char *prefix = got_worktree_get_path_prefix(worktree);
1620 char *p, *worktree_subdir = cwd +
1621 strlen(got_worktree_get_root_path(worktree));
1622 if (asprintf(&p, "%s%s%s%s%s",
1623 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1624 worktree_subdir, worktree_subdir[0] ? "/" : "",
1626 error = got_error_from_errno("asprintf");
1629 error = got_repo_map_path(&in_repo_path, repo, p, 0);
1632 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1637 if (commit_id_str == NULL) {
1638 struct got_reference *head_ref;
1639 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1642 error = got_ref_resolve(&commit_id, repo, head_ref);
1643 got_ref_close(head_ref);
1647 error = got_repo_match_object_id_prefix(&commit_id,
1648 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
1653 error = got_blame(in_repo_path, commit_id, repo, stdout);
1660 got_worktree_close(worktree);
1662 const struct got_error *repo_error;
1663 repo_error = got_repo_close(repo);
1674 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1680 print_entry(struct got_tree_entry *te, const char *id, const char *path,
1681 const char *root_path)
1683 int is_root_path = (strcmp(path, root_path) == 0);
1685 path += strlen(root_path);
1686 while (path[0] == '/')
1689 printf("%s%s%s%s%s\n", id ? id : "", path,
1690 is_root_path ? "" : "/", te->name,
1691 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
1694 static const struct got_error *
1695 print_tree(const char *path, struct got_object_id *commit_id,
1696 int show_ids, int recurse, const char *root_path,
1697 struct got_repository *repo)
1699 const struct got_error *err = NULL;
1700 struct got_object_id *tree_id = NULL;
1701 struct got_tree_object *tree = NULL;
1702 const struct got_tree_entries *entries;
1703 struct got_tree_entry *te;
1705 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1709 err = got_object_open_as_tree(&tree, repo, tree_id);
1712 entries = got_object_tree_get_entries(tree);
1713 te = SIMPLEQ_FIRST(&entries->head);
1717 if (sigint_received || sigpipe_received)
1722 err = got_object_id_str(&id_str, te->id);
1725 if (asprintf(&id, "%s ", id_str) == -1) {
1726 err = got_error_from_errno("asprintf");
1732 print_entry(te, id, path, root_path);
1735 if (recurse && S_ISDIR(te->mode)) {
1737 if (asprintf(&child_path, "%s%s%s", path,
1738 path[0] == '/' && path[1] == '\0' ? "" : "/",
1740 err = got_error_from_errno("asprintf");
1743 err = print_tree(child_path, commit_id, show_ids, 1,
1750 te = SIMPLEQ_NEXT(te, entry);
1754 got_object_tree_close(tree);
1759 static const struct got_error *
1760 cmd_tree(int argc, char *argv[])
1762 const struct got_error *error;
1763 struct got_repository *repo = NULL;
1764 struct got_worktree *worktree = NULL;
1766 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1767 struct got_object_id *commit_id = NULL;
1768 char *commit_id_str = NULL;
1769 int show_ids = 0, recurse = 0;
1773 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1778 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1781 commit_id_str = optarg;
1784 repo_path = realpath(optarg, NULL);
1785 if (repo_path == NULL)
1786 err(1, "-r option");
1787 got_path_strip_trailing_slashes(repo_path);
1811 cwd = getcwd(NULL, 0);
1813 error = got_error_from_errno("getcwd");
1816 if (repo_path == NULL) {
1817 error = got_worktree_open(&worktree, cwd);
1818 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1824 strdup(got_worktree_get_repo_path(worktree));
1825 if (repo_path == NULL)
1826 error = got_error_from_errno("strdup");
1830 repo_path = strdup(cwd);
1831 if (repo_path == NULL) {
1832 error = got_error_from_errno("strdup");
1838 error = got_repo_open(&repo, repo_path);
1842 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1848 char *p, *worktree_subdir = cwd +
1849 strlen(got_worktree_get_root_path(worktree));
1850 if (asprintf(&p, "%s/%s",
1851 got_worktree_get_path_prefix(worktree),
1852 worktree_subdir) == -1) {
1853 error = got_error_from_errno("asprintf");
1856 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1863 if (in_repo_path == NULL) {
1864 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1869 if (commit_id_str == NULL) {
1870 struct got_reference *head_ref;
1871 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1874 error = got_ref_resolve(&commit_id, repo, head_ref);
1875 got_ref_close(head_ref);
1879 error = got_repo_match_object_id_prefix(&commit_id,
1880 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
1885 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1886 in_repo_path, repo);
1893 got_worktree_close(worktree);
1895 const struct got_error *repo_error;
1896 repo_error = got_repo_close(repo);
1906 fprintf(stderr, "usage: %s status [path]\n", getprogname());
1910 static const struct got_error *
1911 print_status(void *arg, unsigned char status, const char *path,
1912 struct got_object_id *blob_id, struct got_object_id *commit_id)
1914 printf("%c %s\n", status, path);
1918 static const struct got_error *
1919 cmd_status(int argc, char *argv[])
1921 const struct got_error *error = NULL;
1922 struct got_repository *repo = NULL;
1923 struct got_worktree *worktree = NULL;
1924 char *cwd = NULL, *path = NULL;
1927 while ((ch = getopt(argc, argv, "")) != -1) {
1939 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1943 cwd = getcwd(NULL, 0);
1945 error = got_error_from_errno("getcwd");
1949 error = got_worktree_open(&worktree, cwd);
1956 error = got_error_from_errno("strdup");
1959 } else if (argc == 1) {
1960 error = got_worktree_resolve_path(&path, worktree, argv[0]);
1966 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1970 error = apply_unveil(got_repo_get_path(repo), 1,
1971 got_worktree_get_root_path(worktree), 0);
1975 error = got_worktree_status(worktree, path, repo, print_status, NULL,
1976 check_cancelled, NULL);
1987 "usage: %s ref [-r repository] -l | -d name | name target\n",
1992 static const struct got_error *
1993 list_refs(struct got_repository *repo)
1995 static const struct got_error *err = NULL;
1996 struct got_reflist_head refs;
1997 struct got_reflist_entry *re;
1999 SIMPLEQ_INIT(&refs);
2000 err = got_ref_list(&refs, repo);
2004 SIMPLEQ_FOREACH(re, &refs, entry) {
2006 refstr = got_ref_to_str(re->ref);
2008 return got_error_from_errno("got_ref_to_str");
2009 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2013 got_ref_list_free(&refs);
2017 static const struct got_error *
2018 delete_ref(struct got_repository *repo, const char *refname)
2020 const struct got_error *err = NULL;
2021 struct got_reference *ref;
2023 err = got_ref_open(&ref, repo, refname, 0);
2027 err = got_ref_delete(ref, repo);
2032 static const struct got_error *
2033 add_ref(struct got_repository *repo, const char *refname, const char *target)
2035 const struct got_error *err = NULL;
2036 struct got_object_id *id;
2037 struct got_reference *ref = NULL;
2039 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2042 struct got_reference *target_ref;
2044 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2046 err = got_ref_open(&target_ref, repo, target, 0);
2049 err = got_ref_resolve(&id, repo, target_ref);
2050 got_ref_close(target_ref);
2055 err = got_ref_alloc(&ref, refname, id);
2059 err = got_ref_write(ref, repo);
2067 static const struct got_error *
2068 cmd_ref(int argc, char *argv[])
2070 const struct got_error *error = NULL;
2071 struct got_repository *repo = NULL;
2072 struct got_worktree *worktree = NULL;
2073 char *cwd = NULL, *repo_path = NULL;
2074 int ch, do_list = 0;
2075 const char *delref = NULL;
2077 /* TODO: Add -s option for adding symbolic references. */
2078 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2084 repo_path = realpath(optarg, NULL);
2085 if (repo_path == NULL)
2086 err(1, "-r option");
2087 got_path_strip_trailing_slashes(repo_path);
2098 if (do_list && delref)
2099 errx(1, "-l and -d options are mutually exclusive\n");
2104 if (do_list || delref) {
2107 } else if (argc != 2)
2112 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2116 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2117 "sendfd unveil", NULL) == -1)
2121 cwd = getcwd(NULL, 0);
2123 error = got_error_from_errno("getcwd");
2127 if (repo_path == NULL) {
2128 error = got_worktree_open(&worktree, cwd);
2129 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2135 strdup(got_worktree_get_repo_path(worktree));
2136 if (repo_path == NULL)
2137 error = got_error_from_errno("strdup");
2141 repo_path = strdup(cwd);
2142 if (repo_path == NULL) {
2143 error = got_error_from_errno("strdup");
2149 error = got_repo_open(&repo, repo_path);
2153 error = apply_unveil(got_repo_get_path(repo), do_list,
2154 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
2159 error = list_refs(repo);
2161 error = delete_ref(repo, delref);
2163 error = add_ref(repo, argv[0], argv[1]);
2166 got_repo_close(repo);
2168 got_worktree_close(worktree);
2178 "usage: %s branch [-r repository] -l | -d name | "
2179 "name [base-branch]\n", getprogname());
2183 static const struct got_error *
2184 list_branches(struct got_repository *repo)
2186 static const struct got_error *err = NULL;
2187 struct got_reflist_head refs;
2188 struct got_reflist_entry *re;
2190 SIMPLEQ_INIT(&refs);
2191 err = got_ref_list(&refs, repo);
2195 SIMPLEQ_FOREACH(re, &refs, entry) {
2196 const char *refname;
2198 refname = got_ref_get_name(re->ref);
2199 if (strncmp(refname, "refs/heads/", 11) != 0)
2202 refstr = got_ref_to_str(re->ref);
2204 return got_error_from_errno("got_ref_to_str");
2205 printf("%s: %s\n", refname, refstr);
2209 got_ref_list_free(&refs);
2213 static const struct got_error *
2214 delete_branch(struct got_repository *repo, const char *branch_name)
2216 const struct got_error *err = NULL;
2217 struct got_reference *ref;
2220 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
2221 return got_error_from_errno("asprintf");
2223 err = got_ref_open(&ref, repo, refname, 0);
2227 err = got_ref_delete(ref, repo);
2234 static const struct got_error *
2235 add_branch(struct got_repository *repo, const char *branch_name,
2236 const char *base_branch)
2238 const struct got_error *err = NULL;
2239 struct got_object_id *id = NULL;
2240 struct got_reference *ref = NULL;
2241 char *base_refname = NULL, *refname = NULL;
2242 struct got_reference *base_ref;
2244 if (strcmp(GOT_REF_HEAD, base_branch) == 0) {
2245 base_refname = strdup(GOT_REF_HEAD);
2246 if (base_refname == NULL)
2247 return got_error_from_errno("strdup");
2248 } else if (asprintf(&base_refname, "refs/heads/%s", base_branch) == -1)
2249 return got_error_from_errno("asprintf");
2251 err = got_ref_open(&base_ref, repo, base_refname, 0);
2254 err = got_ref_resolve(&id, repo, base_ref);
2255 got_ref_close(base_ref);
2259 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
2260 err = got_error_from_errno("asprintf");
2264 err = got_ref_open(&ref, repo, refname, 0);
2266 err = got_error(GOT_ERR_BRANCH_EXISTS);
2268 } else if (err->code != GOT_ERR_NOT_REF)
2271 err = got_ref_alloc(&ref, refname, id);
2275 err = got_ref_write(ref, repo);
2285 static const struct got_error *
2286 cmd_branch(int argc, char *argv[])
2288 const struct got_error *error = NULL;
2289 struct got_repository *repo = NULL;
2290 struct got_worktree *worktree = NULL;
2291 char *cwd = NULL, *repo_path = NULL;
2292 int ch, do_list = 0;
2293 const char *delref = NULL;
2295 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2301 repo_path = realpath(optarg, NULL);
2302 if (repo_path == NULL)
2303 err(1, "-r option");
2304 got_path_strip_trailing_slashes(repo_path);
2315 if (do_list && delref)
2316 errx(1, "-l and -d options are mutually exclusive\n");
2321 if (do_list || delref) {
2324 } else if (argc < 1 || argc > 2)
2329 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2333 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2334 "sendfd unveil", NULL) == -1)
2338 cwd = getcwd(NULL, 0);
2340 error = got_error_from_errno("getcwd");
2344 if (repo_path == NULL) {
2345 error = got_worktree_open(&worktree, cwd);
2346 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2352 strdup(got_worktree_get_repo_path(worktree));
2353 if (repo_path == NULL)
2354 error = got_error_from_errno("strdup");
2358 repo_path = strdup(cwd);
2359 if (repo_path == NULL) {
2360 error = got_error_from_errno("strdup");
2366 error = got_repo_open(&repo, repo_path);
2370 error = apply_unveil(got_repo_get_path(repo), do_list,
2371 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
2376 error = list_branches(repo);
2378 error = delete_branch(repo, delref);
2380 const char *base_branch;
2382 base_branch = worktree ?
2383 got_worktree_get_head_ref_name(worktree) :
2386 base_branch = argv[1];
2387 error = add_branch(repo, argv[0], base_branch);
2391 got_repo_close(repo);
2393 got_worktree_close(worktree);
2402 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
2406 static const struct got_error *
2407 cmd_add(int argc, char *argv[])
2409 const struct got_error *error = NULL;
2410 struct got_repository *repo = NULL;
2411 struct got_worktree *worktree = NULL;
2413 struct got_pathlist_head paths;
2414 struct got_pathlist_entry *pe;
2419 while ((ch = getopt(argc, argv, "")) != -1) {
2433 /* make sure each file exists before doing anything halfway */
2434 for (x = 0; x < argc; x++) {
2435 char *path = realpath(argv[x], NULL);
2437 error = got_error_from_errno2("realpath", argv[x]);
2443 cwd = getcwd(NULL, 0);
2445 error = got_error_from_errno("getcwd");
2449 error = got_worktree_open(&worktree, cwd);
2453 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2457 error = apply_unveil(got_repo_get_path(repo), 1,
2458 got_worktree_get_root_path(worktree), 0);
2462 for (x = 0; x < argc; x++) {
2463 char *path = realpath(argv[x], NULL);
2465 error = got_error_from_errno2("realpath", argv[x]);
2469 got_path_strip_trailing_slashes(path);
2470 error = got_pathlist_insert(&pe, &paths, path, NULL);
2476 error = got_worktree_schedule_add(worktree, &paths, print_status,
2480 got_repo_close(repo);
2482 got_worktree_close(worktree);
2483 TAILQ_FOREACH(pe, &paths, entry)
2484 free((char *)pe->path);
2485 got_pathlist_free(&paths);
2493 fprintf(stderr, "usage: %s rm [-f] file-path ...\n", getprogname());
2497 static const struct got_error *
2498 cmd_rm(int argc, char *argv[])
2500 const struct got_error *error = NULL;
2501 struct got_worktree *worktree = NULL;
2502 struct got_repository *repo = NULL;
2504 struct got_pathlist_head paths;
2505 struct got_pathlist_entry *pe;
2506 int ch, i, delete_local_mods = 0;
2510 while ((ch = getopt(argc, argv, "f")) != -1) {
2513 delete_local_mods = 1;
2527 /* make sure each file exists before doing anything halfway */
2528 for (i = 0; i < argc; i++) {
2529 char *path = realpath(argv[i], NULL);
2531 error = got_error_from_errno2("realpath", argv[i]);
2537 cwd = getcwd(NULL, 0);
2539 error = got_error_from_errno("getcwd");
2542 error = got_worktree_open(&worktree, cwd);
2546 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2550 error = apply_unveil(got_repo_get_path(repo), 1,
2551 got_worktree_get_root_path(worktree), 0);
2555 for (i = 0; i < argc; i++) {
2556 char *path = realpath(argv[i], NULL);
2558 error = got_error_from_errno2("realpath", argv[i]);
2562 got_path_strip_trailing_slashes(path);
2563 error = got_pathlist_insert(&pe, &paths, path, NULL);
2569 error = got_worktree_schedule_delete(worktree, &paths,
2570 delete_local_mods, print_status, NULL, repo);
2575 got_repo_close(repo);
2577 got_worktree_close(worktree);
2578 TAILQ_FOREACH(pe, &paths, entry)
2579 free((char *)pe->path);
2580 got_pathlist_free(&paths);
2588 fprintf(stderr, "usage: %s revert file-path ...\n", getprogname());
2593 revert_progress(void *arg, unsigned char status, const char *path)
2595 while (path[0] == '/')
2597 printf("%c %s\n", status, path);
2600 static const struct got_error *
2601 cmd_revert(int argc, char *argv[])
2603 const struct got_error *error = NULL;
2604 struct got_worktree *worktree = NULL;
2605 struct got_repository *repo = NULL;
2606 char *cwd = NULL, *path = NULL;
2607 struct got_pathlist_head paths;
2608 struct got_pathlist_entry *pe;
2613 while ((ch = getopt(argc, argv, "")) != -1) {
2627 for (i = 0; i < argc; i++) {
2628 char *path = realpath(argv[i], NULL);
2630 error = got_error_from_errno2("realpath", argv[i]);
2634 got_path_strip_trailing_slashes(path);
2635 error = got_pathlist_insert(&pe, &paths, path, NULL);
2642 cwd = getcwd(NULL, 0);
2644 error = got_error_from_errno("getcwd");
2647 error = got_worktree_open(&worktree, cwd);
2651 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2655 error = apply_unveil(got_repo_get_path(repo), 1,
2656 got_worktree_get_root_path(worktree), 0);
2660 error = got_worktree_revert(worktree, &paths,
2661 revert_progress, NULL, repo);
2666 got_repo_close(repo);
2668 got_worktree_close(worktree);
2677 fprintf(stderr, "usage: %s commit [-m msg] file-path\n", getprogname());
2682 spawn_editor(const char *editor, const char *file)
2685 sig_t sighup, sigint, sigquit;
2688 sighup = signal(SIGHUP, SIG_IGN);
2689 sigint = signal(SIGINT, SIG_IGN);
2690 sigquit = signal(SIGQUIT, SIG_IGN);
2692 switch (pid = fork()) {
2696 execl(editor, editor, file, (char *)NULL);
2700 while (waitpid(pid, &st, 0) == -1)
2705 (void)signal(SIGHUP, sighup);
2706 (void)signal(SIGINT, sigint);
2707 (void)signal(SIGQUIT, sigquit);
2709 if (!WIFEXITED(st)) {
2714 return WEXITSTATUS(st);
2717 struct collect_commit_logmsg_arg {
2718 const char *cmdline_log;
2720 const char *worktree_path;
2721 const char *branch_name;
2722 const char *repo_path;
2727 static const struct got_error *
2728 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
2731 char *initial_content = NULL;
2732 struct got_pathlist_entry *pe;
2733 const struct got_error *err = NULL;
2734 char *template = NULL;
2735 struct collect_commit_logmsg_arg *a = arg;
2737 struct stat st, st2;
2740 int fd, content_changed = 0;
2742 /* if a message was specified on the command line, just use it */
2743 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
2744 len = strlen(a->cmdline_log) + 1;
2745 *logmsg = malloc(len + 1);
2746 if (*logmsg == NULL)
2747 return got_error_from_errno("malloc");
2748 strlcpy(*logmsg, a->cmdline_log, len);
2752 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
2753 return got_error_from_errno("asprintf");
2755 if (asprintf(&initial_content,
2756 "\n# changes to be committed on branch %s:\n",
2757 a->branch_name) == -1)
2758 return got_error_from_errno("asprintf");
2760 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
2764 dprintf(fd, initial_content);
2766 TAILQ_FOREACH(pe, commitable_paths, entry) {
2767 struct got_commitable *ct = pe->data;
2768 dprintf(fd, "# %c %s\n",
2769 got_commitable_get_status(ct),
2770 got_commitable_get_path(ct));
2774 if (stat(a->logmsg_path, &st) == -1) {
2775 err = got_error_from_errno2("stat", a->logmsg_path);
2779 if (spawn_editor(a->editor, a->logmsg_path) == -1) {
2780 err = got_error_from_errno("failed spawning editor");
2784 if (stat(a->logmsg_path, &st2) == -1) {
2785 err = got_error_from_errno("stat");
2789 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size) {
2790 unlink(a->logmsg_path);
2791 free(a->logmsg_path);
2792 a->logmsg_path = NULL;
2793 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2794 "no changes made to commit message, aborting");
2798 *logmsg = malloc(st2.st_size + 1);
2799 if (*logmsg == NULL) {
2800 err = got_error_from_errno("malloc");
2803 (*logmsg)[0] = '\0';
2806 fp = fopen(a->logmsg_path, "r");
2808 err = got_error_from_errno("fopen");
2811 while (fgets(buf, sizeof(buf), fp) != NULL) {
2812 if (!content_changed && strcmp(buf, initial_content) != 0)
2813 content_changed = 1;
2814 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
2815 continue; /* remove comments and leading empty lines */
2816 len = strlcat(*logmsg, buf, st2.st_size);
2820 while (len > 0 && (*logmsg)[len - 1] == '\n') {
2821 (*logmsg)[len - 1] = '\0';
2825 if (len == 0 || !content_changed) {
2826 unlink(a->logmsg_path);
2827 free(a->logmsg_path);
2828 a->logmsg_path = NULL;
2829 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2830 "commit message cannot be empty, aborting");
2834 free(initial_content);
2837 /* Editor is done; we can now apply unveil(2) */
2839 err = apply_unveil(a->repo_path, 0, a->worktree_path, 0);
2843 static const struct got_error *
2844 cmd_commit(int argc, char *argv[])
2846 const struct got_error *error = NULL;
2847 struct got_worktree *worktree = NULL;
2848 struct got_repository *repo = NULL;
2849 char *cwd = NULL, *path = NULL, *id_str = NULL;
2850 struct got_object_id *id = NULL;
2851 const char *logmsg = NULL;
2852 const char *got_author = getenv("GOT_AUTHOR");
2853 struct collect_commit_logmsg_arg cl_arg;
2854 char *editor = NULL;
2857 while ((ch = getopt(argc, argv, "m:")) != -1) {
2872 path = realpath(argv[0], NULL);
2874 error = got_error_from_errno2("realpath", argv[0]);
2877 got_path_strip_trailing_slashes(path);
2878 } else if (argc != 0)
2881 if (got_author == NULL) {
2882 /* TODO: Look current user up in password database */
2883 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
2887 cwd = getcwd(NULL, 0);
2889 error = got_error_from_errno("getcwd");
2892 error = got_worktree_open(&worktree, cwd);
2896 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2901 * unveil(2) traverses exec(2); if an editor is used we have
2902 * to apply unveil after the log message has been written.
2904 if (logmsg == NULL || strlen(logmsg) == 0)
2905 error = get_editor(&editor);
2907 error = apply_unveil(got_repo_get_path(repo), 0,
2908 got_worktree_get_root_path(worktree), 0);
2912 cl_arg.editor = editor;
2913 cl_arg.cmdline_log = logmsg;
2914 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
2915 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
2916 if (strncmp(cl_arg.branch_name, "refs/", 5) == 0)
2917 cl_arg.branch_name += 5;
2918 if (strncmp(cl_arg.branch_name, "heads/", 6) == 0)
2919 cl_arg.branch_name += 6;
2920 cl_arg.repo_path = got_repo_get_path(repo);
2921 cl_arg.logmsg_path = NULL;
2922 error = got_worktree_commit(&id, worktree, path, got_author, NULL,
2923 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
2925 if (cl_arg.logmsg_path)
2926 fprintf(stderr, "%s: log message preserved in %s\n",
2927 getprogname(), cl_arg.logmsg_path);
2931 if (cl_arg.logmsg_path)
2932 unlink(cl_arg.logmsg_path);
2934 error = got_object_id_str(&id_str, id);
2937 printf("Created commit %s\n", id_str);
2940 got_repo_close(repo);
2942 got_worktree_close(worktree);
2951 usage_cherrypick(void)
2953 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
2957 static const struct got_error *
2958 cmd_cherrypick(int argc, char *argv[])
2960 const struct got_error *error = NULL;
2961 struct got_worktree *worktree = NULL;
2962 struct got_repository *repo = NULL;
2963 char *cwd = NULL, *commit_id_str = NULL;
2964 struct got_object_id *commit_id = NULL;
2965 struct got_commit_object *commit = NULL;
2966 struct got_object_qid *pid;
2967 struct got_reference *head_ref = NULL;
2968 int ch, did_something = 0;
2970 while ((ch = getopt(argc, argv, "")) != -1) {
2984 cwd = getcwd(NULL, 0);
2986 error = got_error_from_errno("getcwd");
2989 error = got_worktree_open(&worktree, cwd);
2993 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2997 error = apply_unveil(got_repo_get_path(repo), 0,
2998 got_worktree_get_root_path(worktree), 0);
3002 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3003 GOT_OBJ_TYPE_COMMIT, repo);
3004 if (error != NULL) {
3005 struct got_reference *ref;
3006 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3008 error = got_ref_open(&ref, repo, argv[0], 0);
3011 error = got_ref_resolve(&commit_id, repo, ref);
3016 error = got_object_id_str(&commit_id_str, commit_id);
3020 error = got_ref_open(&head_ref, repo,
3021 got_worktree_get_head_ref_name(worktree), 0);
3025 error = check_same_branch(commit_id, head_ref, repo);
3027 if (error->code != GOT_ERR_ANCESTRY)
3031 error = got_error(GOT_ERR_SAME_BRANCH);
3035 error = got_object_open_as_commit(&commit, repo, commit_id);
3038 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3039 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
3040 commit_id, repo, update_progress, &did_something, check_cancelled,
3046 printf("Merged commit %s\n", commit_id_str);
3049 got_object_commit_close(commit);
3050 free(commit_id_str);
3052 got_ref_close(head_ref);
3054 got_worktree_close(worktree);
3056 got_repo_close(repo);
3063 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
3067 static const struct got_error *
3068 cmd_backout(int argc, char *argv[])
3070 const struct got_error *error = NULL;
3071 struct got_worktree *worktree = NULL;
3072 struct got_repository *repo = NULL;
3073 char *cwd = NULL, *commit_id_str = NULL;
3074 struct got_object_id *commit_id = NULL;
3075 struct got_commit_object *commit = NULL;
3076 struct got_object_qid *pid;
3077 struct got_reference *head_ref = NULL;
3078 int ch, did_something = 0;
3080 while ((ch = getopt(argc, argv, "")) != -1) {
3094 cwd = getcwd(NULL, 0);
3096 error = got_error_from_errno("getcwd");
3099 error = got_worktree_open(&worktree, cwd);
3103 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3107 error = apply_unveil(got_repo_get_path(repo), 0,
3108 got_worktree_get_root_path(worktree), 0);
3112 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3113 GOT_OBJ_TYPE_COMMIT, repo);
3114 if (error != NULL) {
3115 struct got_reference *ref;
3116 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3118 error = got_ref_open(&ref, repo, argv[0], 0);
3121 error = got_ref_resolve(&commit_id, repo, ref);
3126 error = got_object_id_str(&commit_id_str, commit_id);
3130 error = got_ref_open(&head_ref, repo,
3131 got_worktree_get_head_ref_name(worktree), 0);
3135 error = check_same_branch(commit_id, head_ref, repo);
3139 error = got_object_open_as_commit(&commit, repo, commit_id);
3142 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3144 error = got_error(GOT_ERR_ROOT_COMMIT);
3148 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
3149 update_progress, &did_something, check_cancelled, NULL);
3154 printf("Backed out commit %s\n", commit_id_str);
3157 got_object_commit_close(commit);
3158 free(commit_id_str);
3160 got_ref_close(head_ref);
3162 got_worktree_close(worktree);
3164 got_repo_close(repo);