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, 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, 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, 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, repo);
1434 struct got_reference *ref;
1435 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1437 error = got_ref_open(&ref, repo, id_str1, 0);
1440 label1 = strdup(got_ref_get_name(ref));
1441 if (label1 == NULL) {
1442 error = got_error_from_errno("strdup");
1445 error = got_ref_resolve(&id1, repo, ref);
1450 label1 = strdup(id_str1);
1451 if (label1 == NULL) {
1452 error = got_error_from_errno("strdup");
1457 error = got_repo_match_object_id_prefix(&id2, id_str2, repo);
1459 struct got_reference *ref;
1460 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1462 error = got_ref_open(&ref, repo, id_str2, 0);
1465 label2 = strdup(got_ref_get_name(ref));
1466 if (label2 == NULL) {
1467 error = got_error_from_errno("strdup");
1470 error = got_ref_resolve(&id2, repo, ref);
1475 label2 = strdup(id_str2);
1476 if (label2 == NULL) {
1477 error = got_error_from_errno("strdup");
1482 error = got_object_get_type(&type1, repo, id1);
1486 error = got_object_get_type(&type2, repo, id2);
1490 if (type1 != type2) {
1491 error = got_error(GOT_ERR_OBJ_TYPE);
1496 case GOT_OBJ_TYPE_BLOB:
1497 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1498 diff_context, repo, stdout);
1500 case GOT_OBJ_TYPE_TREE:
1501 error = got_diff_objects_as_trees(id1, id2, "", "",
1502 diff_context, repo, stdout);
1504 case GOT_OBJ_TYPE_COMMIT:
1505 printf("diff %s %s\n", label1, label2);
1506 error = got_diff_objects_as_commits(id1, id2, diff_context,
1510 error = got_error(GOT_ERR_OBJ_TYPE);
1520 got_worktree_close(worktree);
1522 const struct got_error *repo_error;
1523 repo_error = got_repo_close(repo);
1534 "usage: %s blame [-c commit] [-r repository-path] path\n",
1539 static const struct got_error *
1540 cmd_blame(int argc, char *argv[])
1542 const struct got_error *error;
1543 struct got_repository *repo = NULL;
1544 struct got_worktree *worktree = NULL;
1545 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1546 struct got_object_id *commit_id = NULL;
1547 char *commit_id_str = NULL;
1551 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1556 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1559 commit_id_str = optarg;
1562 repo_path = realpath(optarg, NULL);
1563 if (repo_path == NULL)
1564 err(1, "-r option");
1565 got_path_strip_trailing_slashes(repo_path);
1581 cwd = getcwd(NULL, 0);
1583 error = got_error_from_errno("getcwd");
1586 if (repo_path == NULL) {
1587 error = got_worktree_open(&worktree, cwd);
1588 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1594 strdup(got_worktree_get_repo_path(worktree));
1595 if (repo_path == NULL)
1596 error = got_error_from_errno("strdup");
1600 repo_path = strdup(cwd);
1601 if (repo_path == NULL) {
1602 error = got_error_from_errno("strdup");
1608 error = got_repo_open(&repo, repo_path);
1612 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1617 const char *prefix = got_worktree_get_path_prefix(worktree);
1618 char *p, *worktree_subdir = cwd +
1619 strlen(got_worktree_get_root_path(worktree));
1620 if (asprintf(&p, "%s%s%s%s%s",
1621 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1622 worktree_subdir, worktree_subdir[0] ? "/" : "",
1624 error = got_error_from_errno("asprintf");
1627 error = got_repo_map_path(&in_repo_path, repo, p, 0);
1630 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1635 if (commit_id_str == NULL) {
1636 struct got_reference *head_ref;
1637 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1640 error = got_ref_resolve(&commit_id, repo, head_ref);
1641 got_ref_close(head_ref);
1645 error = got_repo_match_object_id_prefix(&commit_id,
1646 commit_id_str, repo);
1651 error = got_blame(in_repo_path, commit_id, repo, stdout);
1658 got_worktree_close(worktree);
1660 const struct got_error *repo_error;
1661 repo_error = got_repo_close(repo);
1672 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1678 print_entry(struct got_tree_entry *te, const char *id, const char *path,
1679 const char *root_path)
1681 int is_root_path = (strcmp(path, root_path) == 0);
1683 path += strlen(root_path);
1684 while (path[0] == '/')
1687 printf("%s%s%s%s%s\n", id ? id : "", path,
1688 is_root_path ? "" : "/", te->name,
1689 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
1692 static const struct got_error *
1693 print_tree(const char *path, struct got_object_id *commit_id,
1694 int show_ids, int recurse, const char *root_path,
1695 struct got_repository *repo)
1697 const struct got_error *err = NULL;
1698 struct got_object_id *tree_id = NULL;
1699 struct got_tree_object *tree = NULL;
1700 const struct got_tree_entries *entries;
1701 struct got_tree_entry *te;
1703 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1707 err = got_object_open_as_tree(&tree, repo, tree_id);
1710 entries = got_object_tree_get_entries(tree);
1711 te = SIMPLEQ_FIRST(&entries->head);
1715 if (sigint_received || sigpipe_received)
1720 err = got_object_id_str(&id_str, te->id);
1723 if (asprintf(&id, "%s ", id_str) == -1) {
1724 err = got_error_from_errno("asprintf");
1730 print_entry(te, id, path, root_path);
1733 if (recurse && S_ISDIR(te->mode)) {
1735 if (asprintf(&child_path, "%s%s%s", path,
1736 path[0] == '/' && path[1] == '\0' ? "" : "/",
1738 err = got_error_from_errno("asprintf");
1741 err = print_tree(child_path, commit_id, show_ids, 1,
1748 te = SIMPLEQ_NEXT(te, entry);
1752 got_object_tree_close(tree);
1757 static const struct got_error *
1758 cmd_tree(int argc, char *argv[])
1760 const struct got_error *error;
1761 struct got_repository *repo = NULL;
1762 struct got_worktree *worktree = NULL;
1764 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1765 struct got_object_id *commit_id = NULL;
1766 char *commit_id_str = NULL;
1767 int show_ids = 0, recurse = 0;
1771 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1776 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1779 commit_id_str = optarg;
1782 repo_path = realpath(optarg, NULL);
1783 if (repo_path == NULL)
1784 err(1, "-r option");
1785 got_path_strip_trailing_slashes(repo_path);
1809 cwd = getcwd(NULL, 0);
1811 error = got_error_from_errno("getcwd");
1814 if (repo_path == NULL) {
1815 error = got_worktree_open(&worktree, cwd);
1816 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1822 strdup(got_worktree_get_repo_path(worktree));
1823 if (repo_path == NULL)
1824 error = got_error_from_errno("strdup");
1828 repo_path = strdup(cwd);
1829 if (repo_path == NULL) {
1830 error = got_error_from_errno("strdup");
1836 error = got_repo_open(&repo, repo_path);
1840 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1846 char *p, *worktree_subdir = cwd +
1847 strlen(got_worktree_get_root_path(worktree));
1848 if (asprintf(&p, "%s/%s",
1849 got_worktree_get_path_prefix(worktree),
1850 worktree_subdir) == -1) {
1851 error = got_error_from_errno("asprintf");
1854 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1861 if (in_repo_path == NULL) {
1862 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1867 if (commit_id_str == NULL) {
1868 struct got_reference *head_ref;
1869 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1872 error = got_ref_resolve(&commit_id, repo, head_ref);
1873 got_ref_close(head_ref);
1877 error = got_repo_match_object_id_prefix(&commit_id,
1878 commit_id_str, repo);
1883 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1884 in_repo_path, repo);
1891 got_worktree_close(worktree);
1893 const struct got_error *repo_error;
1894 repo_error = got_repo_close(repo);
1904 fprintf(stderr, "usage: %s status [path]\n", getprogname());
1908 static const struct got_error *
1909 print_status(void *arg, unsigned char status, const char *path,
1910 struct got_object_id *blob_id, struct got_object_id *commit_id)
1912 printf("%c %s\n", status, path);
1916 static const struct got_error *
1917 cmd_status(int argc, char *argv[])
1919 const struct got_error *error = NULL;
1920 struct got_repository *repo = NULL;
1921 struct got_worktree *worktree = NULL;
1922 char *cwd = NULL, *path = NULL;
1925 while ((ch = getopt(argc, argv, "")) != -1) {
1937 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1941 cwd = getcwd(NULL, 0);
1943 error = got_error_from_errno("getcwd");
1947 error = got_worktree_open(&worktree, cwd);
1954 error = got_error_from_errno("strdup");
1957 } else if (argc == 1) {
1958 error = got_worktree_resolve_path(&path, worktree, argv[0]);
1964 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1968 error = apply_unveil(got_repo_get_path(repo), 1,
1969 got_worktree_get_root_path(worktree), 0);
1973 error = got_worktree_status(worktree, path, repo, print_status, NULL,
1974 check_cancelled, NULL);
1985 "usage: %s ref [-r repository] -l | -d name | name target\n",
1990 static const struct got_error *
1991 list_refs(struct got_repository *repo)
1993 static const struct got_error *err = NULL;
1994 struct got_reflist_head refs;
1995 struct got_reflist_entry *re;
1997 SIMPLEQ_INIT(&refs);
1998 err = got_ref_list(&refs, repo);
2002 SIMPLEQ_FOREACH(re, &refs, entry) {
2004 refstr = got_ref_to_str(re->ref);
2006 return got_error_from_errno("got_ref_to_str");
2007 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2011 got_ref_list_free(&refs);
2015 static const struct got_error *
2016 delete_ref(struct got_repository *repo, const char *refname)
2018 const struct got_error *err = NULL;
2019 struct got_reference *ref;
2021 err = got_ref_open(&ref, repo, refname, 0);
2025 err = got_ref_delete(ref, repo);
2030 static const struct got_error *
2031 add_ref(struct got_repository *repo, const char *refname, const char *target)
2033 const struct got_error *err = NULL;
2034 struct got_object_id *id;
2035 struct got_reference *ref = NULL;
2037 err = got_repo_match_object_id_prefix(&id, target, repo);
2039 struct got_reference *target_ref;
2041 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2043 err = got_ref_open(&target_ref, repo, target, 0);
2046 err = got_ref_resolve(&id, repo, target_ref);
2047 got_ref_close(target_ref);
2052 err = got_ref_alloc(&ref, refname, id);
2056 err = got_ref_write(ref, repo);
2064 static const struct got_error *
2065 cmd_ref(int argc, char *argv[])
2067 const struct got_error *error = NULL;
2068 struct got_repository *repo = NULL;
2069 struct got_worktree *worktree = NULL;
2070 char *cwd = NULL, *repo_path = NULL;
2071 int ch, do_list = 0;
2072 const char *delref = NULL;
2074 /* TODO: Add -s option for adding symbolic references. */
2075 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2081 repo_path = realpath(optarg, NULL);
2082 if (repo_path == NULL)
2083 err(1, "-r option");
2084 got_path_strip_trailing_slashes(repo_path);
2095 if (do_list && delref)
2096 errx(1, "-l and -d options are mutually exclusive\n");
2101 if (do_list || delref) {
2104 } else if (argc != 2)
2109 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2113 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2114 "sendfd unveil", NULL) == -1)
2118 cwd = getcwd(NULL, 0);
2120 error = got_error_from_errno("getcwd");
2124 if (repo_path == NULL) {
2125 error = got_worktree_open(&worktree, cwd);
2126 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2132 strdup(got_worktree_get_repo_path(worktree));
2133 if (repo_path == NULL)
2134 error = got_error_from_errno("strdup");
2138 repo_path = strdup(cwd);
2139 if (repo_path == NULL) {
2140 error = got_error_from_errno("strdup");
2146 error = got_repo_open(&repo, repo_path);
2150 error = apply_unveil(got_repo_get_path(repo), do_list,
2151 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
2156 error = list_refs(repo);
2158 error = delete_ref(repo, delref);
2160 error = add_ref(repo, argv[0], argv[1]);
2163 got_repo_close(repo);
2165 got_worktree_close(worktree);
2175 "usage: %s branch [-r repository] -l | -d name | "
2176 "name [base-branch]\n", getprogname());
2180 static const struct got_error *
2181 list_branches(struct got_repository *repo)
2183 static const struct got_error *err = NULL;
2184 struct got_reflist_head refs;
2185 struct got_reflist_entry *re;
2187 SIMPLEQ_INIT(&refs);
2188 err = got_ref_list(&refs, repo);
2192 SIMPLEQ_FOREACH(re, &refs, entry) {
2193 const char *refname;
2195 refname = got_ref_get_name(re->ref);
2196 if (strncmp(refname, "refs/heads/", 11) != 0)
2199 refstr = got_ref_to_str(re->ref);
2201 return got_error_from_errno("got_ref_to_str");
2202 printf("%s: %s\n", refname, refstr);
2206 got_ref_list_free(&refs);
2210 static const struct got_error *
2211 delete_branch(struct got_repository *repo, const char *branch_name)
2213 const struct got_error *err = NULL;
2214 struct got_reference *ref;
2217 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
2218 return got_error_from_errno("asprintf");
2220 err = got_ref_open(&ref, repo, refname, 0);
2224 err = got_ref_delete(ref, repo);
2231 static const struct got_error *
2232 add_branch(struct got_repository *repo, const char *branch_name,
2233 const char *base_branch)
2235 const struct got_error *err = NULL;
2236 struct got_object_id *id = NULL;
2237 struct got_reference *ref = NULL;
2238 char *base_refname = NULL, *refname = NULL;
2239 struct got_reference *base_ref;
2241 if (strcmp(GOT_REF_HEAD, base_branch) == 0) {
2242 base_refname = strdup(GOT_REF_HEAD);
2243 if (base_refname == NULL)
2244 return got_error_from_errno("strdup");
2245 } else if (asprintf(&base_refname, "refs/heads/%s", base_branch) == -1)
2246 return got_error_from_errno("asprintf");
2248 err = got_ref_open(&base_ref, repo, base_refname, 0);
2251 err = got_ref_resolve(&id, repo, base_ref);
2252 got_ref_close(base_ref);
2256 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
2257 err = got_error_from_errno("asprintf");
2261 err = got_ref_open(&ref, repo, refname, 0);
2263 err = got_error(GOT_ERR_BRANCH_EXISTS);
2265 } else if (err->code != GOT_ERR_NOT_REF)
2268 err = got_ref_alloc(&ref, refname, id);
2272 err = got_ref_write(ref, repo);
2282 static const struct got_error *
2283 cmd_branch(int argc, char *argv[])
2285 const struct got_error *error = NULL;
2286 struct got_repository *repo = NULL;
2287 struct got_worktree *worktree = NULL;
2288 char *cwd = NULL, *repo_path = NULL;
2289 int ch, do_list = 0;
2290 const char *delref = NULL;
2292 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2298 repo_path = realpath(optarg, NULL);
2299 if (repo_path == NULL)
2300 err(1, "-r option");
2301 got_path_strip_trailing_slashes(repo_path);
2312 if (do_list && delref)
2313 errx(1, "-l and -d options are mutually exclusive\n");
2318 if (do_list || delref) {
2321 } else if (argc < 1 || argc > 2)
2326 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2330 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2331 "sendfd unveil", NULL) == -1)
2335 cwd = getcwd(NULL, 0);
2337 error = got_error_from_errno("getcwd");
2341 if (repo_path == NULL) {
2342 error = got_worktree_open(&worktree, cwd);
2343 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2349 strdup(got_worktree_get_repo_path(worktree));
2350 if (repo_path == NULL)
2351 error = got_error_from_errno("strdup");
2355 repo_path = strdup(cwd);
2356 if (repo_path == NULL) {
2357 error = got_error_from_errno("strdup");
2363 error = got_repo_open(&repo, repo_path);
2367 error = apply_unveil(got_repo_get_path(repo), do_list,
2368 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
2373 error = list_branches(repo);
2375 error = delete_branch(repo, delref);
2377 const char *base_branch;
2379 base_branch = worktree ?
2380 got_worktree_get_head_ref_name(worktree) :
2383 base_branch = argv[1];
2384 error = add_branch(repo, argv[0], base_branch);
2388 got_repo_close(repo);
2390 got_worktree_close(worktree);
2399 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
2403 static const struct got_error *
2404 cmd_add(int argc, char *argv[])
2406 const struct got_error *error = NULL;
2407 struct got_repository *repo = NULL;
2408 struct got_worktree *worktree = NULL;
2410 struct got_pathlist_head paths;
2411 struct got_pathlist_entry *pe;
2416 while ((ch = getopt(argc, argv, "")) != -1) {
2430 /* make sure each file exists before doing anything halfway */
2431 for (x = 0; x < argc; x++) {
2432 char *path = realpath(argv[x], NULL);
2434 error = got_error_from_errno2("realpath", argv[x]);
2440 cwd = getcwd(NULL, 0);
2442 error = got_error_from_errno("getcwd");
2446 error = got_worktree_open(&worktree, cwd);
2450 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2454 error = apply_unveil(got_repo_get_path(repo), 1,
2455 got_worktree_get_root_path(worktree), 0);
2459 for (x = 0; x < argc; x++) {
2460 char *path = realpath(argv[x], NULL);
2462 error = got_error_from_errno2("realpath", argv[x]);
2466 got_path_strip_trailing_slashes(path);
2467 error = got_pathlist_insert(&pe, &paths, path, NULL);
2473 error = got_worktree_schedule_add(worktree, &paths, print_status,
2477 got_repo_close(repo);
2479 got_worktree_close(worktree);
2480 TAILQ_FOREACH(pe, &paths, entry)
2481 free((char *)pe->path);
2482 got_pathlist_free(&paths);
2490 fprintf(stderr, "usage: %s rm [-f] file-path ...\n", getprogname());
2494 static const struct got_error *
2495 cmd_rm(int argc, char *argv[])
2497 const struct got_error *error = NULL;
2498 struct got_worktree *worktree = NULL;
2499 struct got_repository *repo = NULL;
2501 struct got_pathlist_head paths;
2502 struct got_pathlist_entry *pe;
2503 int ch, i, delete_local_mods = 0;
2507 while ((ch = getopt(argc, argv, "f")) != -1) {
2510 delete_local_mods = 1;
2524 /* make sure each file exists before doing anything halfway */
2525 for (i = 0; i < argc; i++) {
2526 char *path = realpath(argv[i], NULL);
2528 error = got_error_from_errno2("realpath", argv[i]);
2534 cwd = getcwd(NULL, 0);
2536 error = got_error_from_errno("getcwd");
2539 error = got_worktree_open(&worktree, cwd);
2543 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2547 error = apply_unveil(got_repo_get_path(repo), 1,
2548 got_worktree_get_root_path(worktree), 0);
2552 for (i = 0; i < argc; i++) {
2553 char *path = realpath(argv[i], NULL);
2555 error = got_error_from_errno2("realpath", argv[i]);
2559 got_path_strip_trailing_slashes(path);
2560 error = got_pathlist_insert(&pe, &paths, path, NULL);
2566 error = got_worktree_schedule_delete(worktree, &paths,
2567 delete_local_mods, print_status, NULL, repo);
2572 got_repo_close(repo);
2574 got_worktree_close(worktree);
2575 TAILQ_FOREACH(pe, &paths, entry)
2576 free((char *)pe->path);
2577 got_pathlist_free(&paths);
2585 fprintf(stderr, "usage: %s revert file-path ...\n", getprogname());
2590 revert_progress(void *arg, unsigned char status, const char *path)
2592 while (path[0] == '/')
2594 printf("%c %s\n", status, path);
2597 static const struct got_error *
2598 cmd_revert(int argc, char *argv[])
2600 const struct got_error *error = NULL;
2601 struct got_worktree *worktree = NULL;
2602 struct got_repository *repo = NULL;
2603 char *cwd = NULL, *path = NULL;
2604 struct got_pathlist_head paths;
2605 struct got_pathlist_entry *pe;
2610 while ((ch = getopt(argc, argv, "")) != -1) {
2624 for (i = 0; i < argc; i++) {
2625 char *path = realpath(argv[i], NULL);
2627 error = got_error_from_errno2("realpath", argv[i]);
2631 got_path_strip_trailing_slashes(path);
2632 error = got_pathlist_insert(&pe, &paths, path, NULL);
2639 cwd = getcwd(NULL, 0);
2641 error = got_error_from_errno("getcwd");
2644 error = got_worktree_open(&worktree, cwd);
2648 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2652 error = apply_unveil(got_repo_get_path(repo), 1,
2653 got_worktree_get_root_path(worktree), 0);
2657 error = got_worktree_revert(worktree, &paths,
2658 revert_progress, NULL, repo);
2663 got_repo_close(repo);
2665 got_worktree_close(worktree);
2674 fprintf(stderr, "usage: %s commit [-m msg] file-path\n", getprogname());
2679 spawn_editor(const char *editor, const char *file)
2682 sig_t sighup, sigint, sigquit;
2685 sighup = signal(SIGHUP, SIG_IGN);
2686 sigint = signal(SIGINT, SIG_IGN);
2687 sigquit = signal(SIGQUIT, SIG_IGN);
2689 switch (pid = fork()) {
2693 execl(editor, editor, file, (char *)NULL);
2697 while (waitpid(pid, &st, 0) == -1)
2702 (void)signal(SIGHUP, sighup);
2703 (void)signal(SIGINT, sigint);
2704 (void)signal(SIGQUIT, sigquit);
2706 if (!WIFEXITED(st)) {
2711 return WEXITSTATUS(st);
2714 struct collect_commit_logmsg_arg {
2715 const char *cmdline_log;
2717 const char *worktree_path;
2718 const char *branch_name;
2719 const char *repo_path;
2724 static const struct got_error *
2725 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
2728 char *initial_content = NULL;
2729 struct got_pathlist_entry *pe;
2730 const struct got_error *err = NULL;
2731 char *template = NULL;
2732 struct collect_commit_logmsg_arg *a = arg;
2734 struct stat st, st2;
2737 int fd, content_changed = 0;
2739 /* if a message was specified on the command line, just use it */
2740 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
2741 len = strlen(a->cmdline_log) + 1;
2742 *logmsg = malloc(len + 1);
2743 if (*logmsg == NULL)
2744 return got_error_from_errno("malloc");
2745 strlcpy(*logmsg, a->cmdline_log, len);
2749 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
2750 return got_error_from_errno("asprintf");
2752 if (asprintf(&initial_content,
2753 "\n# changes to be committed on branch %s:\n",
2754 a->branch_name) == -1)
2755 return got_error_from_errno("asprintf");
2757 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
2761 dprintf(fd, initial_content);
2763 TAILQ_FOREACH(pe, commitable_paths, entry) {
2764 struct got_commitable *ct = pe->data;
2765 dprintf(fd, "# %c %s\n",
2766 got_commitable_get_status(ct),
2767 got_commitable_get_path(ct));
2771 if (stat(a->logmsg_path, &st) == -1) {
2772 err = got_error_from_errno2("stat", a->logmsg_path);
2776 if (spawn_editor(a->editor, a->logmsg_path) == -1) {
2777 err = got_error_from_errno("failed spawning editor");
2781 if (stat(a->logmsg_path, &st2) == -1) {
2782 err = got_error_from_errno("stat");
2786 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size) {
2787 unlink(a->logmsg_path);
2788 free(a->logmsg_path);
2789 a->logmsg_path = NULL;
2790 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2791 "no changes made to commit message, aborting");
2795 *logmsg = malloc(st2.st_size + 1);
2796 if (*logmsg == NULL) {
2797 err = got_error_from_errno("malloc");
2800 (*logmsg)[0] = '\0';
2803 fp = fopen(a->logmsg_path, "r");
2805 err = got_error_from_errno("fopen");
2808 while (fgets(buf, sizeof(buf), fp) != NULL) {
2809 if (!content_changed && strcmp(buf, initial_content) != 0)
2810 content_changed = 1;
2811 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
2812 continue; /* remove comments and leading empty lines */
2813 len = strlcat(*logmsg, buf, st2.st_size);
2817 while (len > 0 && (*logmsg)[len - 1] == '\n') {
2818 (*logmsg)[len - 1] = '\0';
2822 if (len == 0 || !content_changed) {
2823 unlink(a->logmsg_path);
2824 free(a->logmsg_path);
2825 a->logmsg_path = NULL;
2826 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2827 "commit message cannot be empty, aborting");
2831 free(initial_content);
2834 /* Editor is done; we can now apply unveil(2) */
2836 err = apply_unveil(a->repo_path, 0, a->worktree_path, 0);
2840 static const struct got_error *
2841 cmd_commit(int argc, char *argv[])
2843 const struct got_error *error = NULL;
2844 struct got_worktree *worktree = NULL;
2845 struct got_repository *repo = NULL;
2846 char *cwd = NULL, *path = NULL, *id_str = NULL;
2847 struct got_object_id *id = NULL;
2848 const char *logmsg = NULL;
2849 const char *got_author = getenv("GOT_AUTHOR");
2850 struct collect_commit_logmsg_arg cl_arg;
2851 char *editor = NULL;
2854 while ((ch = getopt(argc, argv, "m:")) != -1) {
2869 path = realpath(argv[0], NULL);
2871 error = got_error_from_errno2("realpath", argv[0]);
2874 got_path_strip_trailing_slashes(path);
2875 } else if (argc != 0)
2878 if (got_author == NULL) {
2879 /* TODO: Look current user up in password database */
2880 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
2884 cwd = getcwd(NULL, 0);
2886 error = got_error_from_errno("getcwd");
2889 error = got_worktree_open(&worktree, cwd);
2893 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2898 * unveil(2) traverses exec(2); if an editor is used we have
2899 * to apply unveil after the log message has been written.
2901 if (logmsg == NULL || strlen(logmsg) == 0)
2902 error = get_editor(&editor);
2904 error = apply_unveil(got_repo_get_path(repo), 0,
2905 got_worktree_get_root_path(worktree), 0);
2909 cl_arg.editor = editor;
2910 cl_arg.cmdline_log = logmsg;
2911 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
2912 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
2913 if (strncmp(cl_arg.branch_name, "refs/", 5) == 0)
2914 cl_arg.branch_name += 5;
2915 if (strncmp(cl_arg.branch_name, "heads/", 6) == 0)
2916 cl_arg.branch_name += 6;
2917 cl_arg.repo_path = got_repo_get_path(repo);
2918 cl_arg.logmsg_path = NULL;
2919 error = got_worktree_commit(&id, worktree, path, got_author, NULL,
2920 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
2922 if (cl_arg.logmsg_path)
2923 fprintf(stderr, "%s: log message preserved in %s\n",
2924 getprogname(), cl_arg.logmsg_path);
2928 if (cl_arg.logmsg_path)
2929 unlink(cl_arg.logmsg_path);
2931 error = got_object_id_str(&id_str, id);
2934 printf("Created commit %s\n", id_str);
2937 got_repo_close(repo);
2939 got_worktree_close(worktree);
2948 usage_cherrypick(void)
2950 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
2954 static const struct got_error *
2955 cmd_cherrypick(int argc, char *argv[])
2957 const struct got_error *error = NULL;
2958 struct got_worktree *worktree = NULL;
2959 struct got_repository *repo = NULL;
2960 char *cwd = NULL, *commit_id_str = NULL;
2961 struct got_object_id *commit_id = NULL;
2962 struct got_commit_object *commit = NULL;
2963 struct got_object_qid *pid;
2964 struct got_reference *head_ref = NULL;
2965 int ch, did_something = 0;
2967 while ((ch = getopt(argc, argv, "")) != -1) {
2981 cwd = getcwd(NULL, 0);
2983 error = got_error_from_errno("getcwd");
2986 error = got_worktree_open(&worktree, cwd);
2990 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2994 error = apply_unveil(got_repo_get_path(repo), 0,
2995 got_worktree_get_root_path(worktree), 0);
2999 error = got_repo_match_object_id_prefix(&commit_id, argv[0], repo);
3000 if (error != NULL) {
3001 struct got_reference *ref;
3002 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3004 error = got_ref_open(&ref, repo, argv[0], 0);
3007 error = got_ref_resolve(&commit_id, repo, ref);
3012 error = got_object_id_str(&commit_id_str, commit_id);
3016 error = got_ref_open(&head_ref, repo,
3017 got_worktree_get_head_ref_name(worktree), 0);
3021 error = check_same_branch(commit_id, head_ref, repo);
3023 if (error->code != GOT_ERR_ANCESTRY)
3027 error = got_error(GOT_ERR_SAME_BRANCH);
3031 error = got_object_open_as_commit(&commit, repo, commit_id);
3034 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3035 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
3036 commit_id, repo, update_progress, &did_something, check_cancelled,
3042 printf("Merged commit %s\n", commit_id_str);
3045 got_object_commit_close(commit);
3046 free(commit_id_str);
3048 got_ref_close(head_ref);
3050 got_worktree_close(worktree);
3052 got_repo_close(repo);
3059 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
3063 static const struct got_error *
3064 cmd_backout(int argc, char *argv[])
3066 const struct got_error *error = NULL;
3067 struct got_worktree *worktree = NULL;
3068 struct got_repository *repo = NULL;
3069 char *cwd = NULL, *commit_id_str = NULL;
3070 struct got_object_id *commit_id = NULL;
3071 struct got_commit_object *commit = NULL;
3072 struct got_object_qid *pid;
3073 struct got_reference *head_ref = NULL;
3074 int ch, did_something = 0;
3076 while ((ch = getopt(argc, argv, "")) != -1) {
3090 cwd = getcwd(NULL, 0);
3092 error = got_error_from_errno("getcwd");
3095 error = got_worktree_open(&worktree, cwd);
3099 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3103 error = apply_unveil(got_repo_get_path(repo), 0,
3104 got_worktree_get_root_path(worktree), 0);
3108 error = got_repo_match_object_id_prefix(&commit_id, argv[0], repo);
3109 if (error != NULL) {
3110 struct got_reference *ref;
3111 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3113 error = got_ref_open(&ref, repo, argv[0], 0);
3116 error = got_ref_resolve(&commit_id, repo, ref);
3121 error = got_object_id_str(&commit_id_str, commit_id);
3125 error = got_ref_open(&head_ref, repo,
3126 got_worktree_get_head_ref_name(worktree), 0);
3130 error = check_same_branch(commit_id, head_ref, repo);
3134 error = got_object_open_as_commit(&commit, repo, commit_id);
3137 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3139 error = got_error(GOT_ERR_ROOT_COMMIT);
3143 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
3144 update_progress, &did_something, check_cancelled, NULL);
3149 printf("Backed out commit %s\n", commit_id_str);
3152 got_object_commit_close(commit);
3153 free(commit_id_str);
3155 got_ref_close(head_ref);
3157 got_worktree_close(worktree);
3159 got_repo_close(repo);