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);
73 const char *cmd_alias;
76 __dead static void usage(int);
77 __dead static void usage_init(void);
78 __dead static void usage_checkout(void);
79 __dead static void usage_update(void);
80 __dead static void usage_log(void);
81 __dead static void usage_diff(void);
82 __dead static void usage_blame(void);
83 __dead static void usage_tree(void);
84 __dead static void usage_status(void);
85 __dead static void usage_ref(void);
86 __dead static void usage_branch(void);
87 __dead static void usage_add(void);
88 __dead static void usage_rm(void);
89 __dead static void usage_revert(void);
90 __dead static void usage_commit(void);
91 __dead static void usage_cherrypick(void);
92 __dead static void usage_backout(void);
94 static const struct got_error* cmd_init(int, char *[]);
95 static const struct got_error* cmd_checkout(int, char *[]);
96 static const struct got_error* cmd_update(int, char *[]);
97 static const struct got_error* cmd_log(int, char *[]);
98 static const struct got_error* cmd_diff(int, char *[]);
99 static const struct got_error* cmd_blame(int, char *[]);
100 static const struct got_error* cmd_tree(int, char *[]);
101 static const struct got_error* cmd_status(int, char *[]);
102 static const struct got_error* cmd_ref(int, char *[]);
103 static const struct got_error* cmd_branch(int, char *[]);
104 static const struct got_error* cmd_add(int, char *[]);
105 static const struct got_error* cmd_rm(int, char *[]);
106 static const struct got_error* cmd_revert(int, char *[]);
107 static const struct got_error* cmd_commit(int, char *[]);
108 static const struct got_error* cmd_cherrypick(int, char *[]);
109 static const struct got_error* cmd_backout(int, char *[]);
111 static struct got_cmd got_commands[] = {
112 { "init", cmd_init, usage_init, "" },
113 { "checkout", cmd_checkout, usage_checkout, "co" },
114 { "update", cmd_update, usage_update, "up" },
115 { "log", cmd_log, usage_log, "" },
116 { "diff", cmd_diff, usage_diff, "" },
117 { "blame", cmd_blame, usage_blame, "" },
118 { "tree", cmd_tree, usage_tree, "" },
119 { "status", cmd_status, usage_status, "st" },
120 { "ref", cmd_ref, usage_ref, "" },
121 { "branch", cmd_branch, usage_branch, "br" },
122 { "add", cmd_add, usage_add, "" },
123 { "rm", cmd_rm, usage_rm, "" },
124 { "revert", cmd_revert, usage_revert, "rv" },
125 { "commit", cmd_commit, usage_commit, "ci" },
126 { "cherrypick", cmd_cherrypick, usage_cherrypick, "ch" },
127 { "backout", cmd_backout, usage_backout, "bo" },
135 fprintf(stderr, "commands:");
136 for (i = 0; i < nitems(got_commands); i++) {
137 struct got_cmd *cmd = &got_commands[i];
138 fprintf(stderr, " %s", cmd->cmd_name);
144 main(int argc, char *argv[])
151 setlocale(LC_CTYPE, "");
153 while ((ch = getopt(argc, argv, "h")) != -1) {
171 signal(SIGINT, catch_sigint);
172 signal(SIGPIPE, catch_sigpipe);
174 for (i = 0; i < nitems(got_commands); i++) {
175 const struct got_error *error;
177 cmd = &got_commands[i];
179 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
180 strcmp(cmd->cmd_alias, argv[0]) != 0)
184 got_commands[i].cmd_usage();
186 error = got_commands[i].cmd_main(argc, argv);
187 if (error && !(sigint_received || sigpipe_received)) {
188 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
195 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
203 fprintf(stderr, "usage: %s [-h] command [arg ...]\n", getprogname());
209 static const struct got_error *
210 get_editor(char **abspath)
212 const struct got_error *err = NULL;
215 editor = getenv("VISUAL");
217 editor = getenv("EDITOR");
220 err = got_path_find_prog(abspath, editor);
225 if (*abspath == NULL) {
226 *abspath = strdup("/bin/ed");
227 if (*abspath == NULL)
228 return got_error_from_errno("strdup");
234 static const struct got_error *
235 apply_unveil(const char *repo_path, int repo_read_only,
236 const char *worktree_path, int create_worktree)
238 const struct got_error *err;
240 if (create_worktree) {
241 /* Pre-create work tree path to avoid unveiling its parents. */
242 err = got_path_mkdir(worktree_path);
244 if (errno == EEXIST) {
245 if (got_path_dir_is_empty(worktree_path)) {
249 err = got_error_path(worktree_path,
250 GOT_ERR_DIR_NOT_EMPTY);
254 if (err && (err->code != GOT_ERR_ERRNO || errno != EISDIR))
258 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
259 return got_error_from_errno2("unveil", repo_path);
261 if (worktree_path && unveil(worktree_path, "rwc") != 0)
262 return got_error_from_errno2("unveil", worktree_path);
264 if (unveil("/tmp", "rwc") != 0)
265 return got_error_from_errno2("unveil", "/tmp");
267 err = got_privsep_unveil_exec_helpers();
271 if (unveil(NULL, NULL) != 0)
272 return got_error_from_errno("unveil");
280 fprintf(stderr, "usage: %s init path\n", getprogname());
284 static const struct got_error *
285 cmd_init(int argc, char *argv[])
287 const struct got_error *error = NULL;
288 char *repo_path = NULL;
291 while ((ch = getopt(argc, argv, "")) != -1) {
303 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
309 repo_path = strdup(argv[0]);
310 if (repo_path == NULL)
311 return got_error_from_errno("strdup");
313 got_path_strip_trailing_slashes(repo_path);
315 error = got_path_mkdir(repo_path);
317 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
320 error = apply_unveil(repo_path, 0, NULL, 0);
324 error = got_repo_init(repo_path);
336 fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
337 "[-p prefix] repository-path [worktree-path]\n", getprogname());
342 checkout_progress(void *arg, unsigned char status, const char *path)
344 char *worktree_path = arg;
346 /* Base commit bump happens silently. */
347 if (status == GOT_STATUS_BUMP_BASE)
350 while (path[0] == '/')
353 printf("%c %s/%s\n", status, worktree_path, path);
356 static const struct got_error *
357 check_cancelled(void *arg)
359 if (sigint_received || sigpipe_received)
360 return got_error(GOT_ERR_CANCELLED);
364 static const struct got_error *
365 check_linear_ancestry(struct got_object_id *commit_id,
366 struct got_object_id *base_commit_id, struct got_repository *repo)
368 const struct got_error *err = NULL;
369 struct got_object_id *yca_id;
371 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
372 commit_id, base_commit_id, repo);
377 return got_error(GOT_ERR_ANCESTRY);
380 * Require a straight line of history between the target commit
381 * and the work tree's base commit.
383 * Non-linear situations such as this require a rebase:
385 * (commit) D F (base_commit)
393 * 'got update' only handles linear cases:
394 * Update forwards in time: A (base/yca) - B - C - D (commit)
395 * Update backwards in time: D (base) - C - B - A (commit/yca)
397 if (got_object_id_cmp(commit_id, yca_id) != 0 &&
398 got_object_id_cmp(base_commit_id, yca_id) != 0)
399 return got_error(GOT_ERR_ANCESTRY);
405 static const struct got_error *
406 check_same_branch(struct got_object_id *commit_id,
407 struct got_reference *head_ref, struct got_repository *repo)
409 const struct got_error *err = NULL;
410 struct got_commit_graph *graph = NULL;
411 struct got_object_id *head_commit_id = NULL;
412 int is_same_branch = 0;
414 err = got_ref_resolve(&head_commit_id, repo, head_ref);
418 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
422 err = got_commit_graph_iter_start(graph, head_commit_id, repo);
427 struct got_object_id *id;
428 err = got_commit_graph_iter_next(&id, graph);
430 if (err->code == GOT_ERR_ITER_COMPLETED) {
434 else if (err->code != GOT_ERR_ITER_NEED_MORE)
436 err = got_commit_graph_fetch_commits(graph, 1,
443 if (got_object_id_cmp(id, commit_id) == 0) {
451 got_commit_graph_close(graph);
452 free(head_commit_id);
453 if (!err && !is_same_branch)
454 err = got_error(GOT_ERR_ANCESTRY);
458 static const struct got_error *
459 cmd_checkout(int argc, char *argv[])
461 const struct got_error *error = NULL;
462 struct got_repository *repo = NULL;
463 struct got_reference *head_ref = NULL;
464 struct got_worktree *worktree = NULL;
465 char *repo_path = NULL;
466 char *worktree_path = NULL;
467 const char *path_prefix = "";
468 const char *branch_name = GOT_REF_HEAD;
469 char *commit_id_str = NULL;
470 int ch, same_path_prefix;
472 while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
475 branch_name = optarg;
478 commit_id_str = strdup(optarg);
479 if (commit_id_str == NULL)
480 return got_error_from_errno("strdup");
483 path_prefix = optarg;
495 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
496 "unveil", NULL) == -1)
500 char *cwd, *base, *dotgit;
501 repo_path = realpath(argv[0], NULL);
502 if (repo_path == NULL)
503 return got_error_from_errno2("realpath", argv[0]);
504 cwd = getcwd(NULL, 0);
506 error = got_error_from_errno("getcwd");
509 if (path_prefix[0]) {
510 base = basename(path_prefix);
512 error = got_error_from_errno2("basename",
517 base = basename(repo_path);
519 error = got_error_from_errno2("basename",
524 dotgit = strstr(base, ".git");
527 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
528 error = got_error_from_errno("asprintf");
533 } else if (argc == 2) {
534 repo_path = realpath(argv[0], NULL);
535 if (repo_path == NULL) {
536 error = got_error_from_errno2("realpath", argv[0]);
539 worktree_path = realpath(argv[1], NULL);
540 if (worktree_path == NULL) {
541 error = got_error_from_errno2("realpath", argv[1]);
547 got_path_strip_trailing_slashes(repo_path);
548 got_path_strip_trailing_slashes(worktree_path);
550 error = got_repo_open(&repo, repo_path);
554 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path, 1);
558 error = got_ref_open(&head_ref, repo, branch_name, 0);
562 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
563 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
566 error = got_worktree_open(&worktree, worktree_path);
570 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
574 if (!same_path_prefix) {
575 error = got_error(GOT_ERR_PATH_PREFIX);
580 struct got_object_id *commit_id;
581 error = got_repo_match_object_id_prefix(&commit_id,
582 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
585 error = check_linear_ancestry(commit_id,
586 got_worktree_get_base_commit_id(worktree), repo);
591 error = check_same_branch(commit_id, head_ref, repo);
594 error = got_worktree_set_base_commit_id(worktree, repo,
601 error = got_worktree_checkout_files(worktree, "", repo,
602 checkout_progress, worktree_path, check_cancelled, NULL);
606 printf("Now shut up and hack\n");
618 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path]\n",
624 update_progress(void *arg, unsigned char status, const char *path)
626 int *did_something = arg;
628 if (status == GOT_STATUS_EXISTS)
633 /* Base commit bump happens silently. */
634 if (status == GOT_STATUS_BUMP_BASE)
637 while (path[0] == '/')
639 printf("%c %s\n", status, path);
642 static const struct got_error *
643 switch_head_ref(struct got_reference *head_ref,
644 struct got_object_id *commit_id, struct got_worktree *worktree,
645 struct got_repository *repo)
647 const struct got_error *err = NULL;
649 int ref_has_moved = 0;
651 /* Trivial case: switching between two different references. */
652 if (strcmp(got_ref_get_name(head_ref),
653 got_worktree_get_head_ref_name(worktree)) != 0) {
654 printf("Switching work tree from %s to %s\n",
655 got_worktree_get_head_ref_name(worktree),
656 got_ref_get_name(head_ref));
657 return got_worktree_set_head_ref(worktree, head_ref);
660 err = check_linear_ancestry(commit_id,
661 got_worktree_get_base_commit_id(worktree), repo);
663 if (err->code != GOT_ERR_ANCESTRY)
670 /* Switching to a rebased branch with the same reference name. */
671 err = got_object_id_str(&base_id_str,
672 got_worktree_get_base_commit_id(worktree));
675 printf("Reference %s now points at a different branch\n",
676 got_worktree_get_head_ref_name(worktree));
677 printf("Switching work tree from %s to %s\n", base_id_str,
678 got_worktree_get_head_ref_name(worktree));
682 static const struct got_error *
683 cmd_update(int argc, char *argv[])
685 const struct got_error *error = NULL;
686 struct got_repository *repo = NULL;
687 struct got_worktree *worktree = NULL;
688 char *worktree_path = NULL, *path = NULL;
689 struct got_object_id *commit_id = NULL;
690 char *commit_id_str = NULL;
691 const char *branch_name = NULL;
692 struct got_reference *head_ref = NULL;
693 int ch, did_something = 0;
695 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
698 branch_name = optarg;
701 commit_id_str = strdup(optarg);
702 if (commit_id_str == NULL)
703 return got_error_from_errno("strdup");
715 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
716 "unveil", NULL) == -1)
719 worktree_path = getcwd(NULL, 0);
720 if (worktree_path == NULL) {
721 error = got_error_from_errno("getcwd");
724 error = got_worktree_open(&worktree, worktree_path);
731 error = got_error_from_errno("strdup");
734 } else if (argc == 1) {
735 error = got_worktree_resolve_path(&path, worktree, argv[0]);
741 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
745 error = apply_unveil(got_repo_get_path(repo), 0,
746 got_worktree_get_root_path(worktree), 0);
750 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
751 got_worktree_get_head_ref_name(worktree), 0);
754 if (commit_id_str == NULL) {
755 error = got_ref_resolve(&commit_id, repo, head_ref);
758 error = got_object_id_str(&commit_id_str, commit_id);
762 error = got_repo_match_object_id_prefix(&commit_id,
763 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
769 struct got_object_id *head_commit_id;
770 if (strlen(path) != 0) {
771 fprintf(stderr, "%s: switching between branches "
772 "requires that the entire work tree "
773 "gets updated, not just '%s'\n",
774 getprogname(), path);
775 error = got_error(GOT_ERR_BAD_PATH);
778 error = got_ref_resolve(&head_commit_id, repo, head_ref);
781 error = check_linear_ancestry(commit_id, head_commit_id, repo);
782 free(head_commit_id);
785 error = check_same_branch(commit_id, head_ref, repo);
788 error = switch_head_ref(head_ref, commit_id, worktree, repo);
792 error = check_linear_ancestry(commit_id,
793 got_worktree_get_base_commit_id(worktree), repo);
795 if (error->code == GOT_ERR_ANCESTRY)
796 error = got_error(GOT_ERR_BRANCH_MOVED);
799 error = check_same_branch(commit_id, head_ref, repo);
804 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
806 error = got_worktree_set_base_commit_id(worktree, repo,
812 error = got_worktree_checkout_files(worktree, path, repo,
813 update_progress, &did_something, check_cancelled, NULL);
818 printf("Updated to commit %s\n", commit_id_str);
820 printf("Already up-to-date\n");
829 static const struct got_error *
830 print_patch(struct got_commit_object *commit, struct got_object_id *id,
831 int diff_context, struct got_repository *repo)
833 const struct got_error *err = NULL;
834 struct got_tree_object *tree1 = NULL, *tree2;
835 struct got_object_qid *qid;
836 char *id_str1 = NULL, *id_str2;
837 struct got_diff_blob_output_unidiff_arg arg;
839 err = got_object_open_as_tree(&tree2, repo,
840 got_object_commit_get_tree_id(commit));
844 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
846 struct got_commit_object *pcommit;
848 err = got_object_open_as_commit(&pcommit, repo, qid->id);
852 err = got_object_open_as_tree(&tree1, repo,
853 got_object_commit_get_tree_id(pcommit));
854 got_object_commit_close(pcommit);
858 err = got_object_id_str(&id_str1, qid->id);
863 err = got_object_id_str(&id_str2, id);
867 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
868 arg.diff_context = diff_context;
869 arg.outfile = stdout;
870 err = got_diff_tree(tree1, tree2, "", "", repo,
871 got_diff_blob_output_unidiff, &arg);
874 got_object_tree_close(tree1);
875 got_object_tree_close(tree2);
882 get_datestr(time_t *time, char *datebuf)
884 char *p, *s = ctime_r(time, datebuf);
891 static const struct got_error *
892 print_commit(struct got_commit_object *commit, struct got_object_id *id,
893 struct got_repository *repo, int show_patch, int diff_context,
894 struct got_reflist_head *refs)
896 const struct got_error *err = NULL;
897 char *id_str, *datestr, *logmsg0, *logmsg, *line;
899 time_t committer_time;
900 const char *author, *committer;
901 char *refs_str = NULL;
902 struct got_reflist_entry *re;
904 SIMPLEQ_FOREACH(re, refs, entry) {
907 if (got_object_id_cmp(re->id, id) != 0)
909 name = got_ref_get_name(re->ref);
910 if (strcmp(name, GOT_REF_HEAD) == 0)
912 if (strncmp(name, "refs/", 5) == 0)
914 if (strncmp(name, "got/", 4) == 0)
916 if (strncmp(name, "heads/", 6) == 0)
918 if (strncmp(name, "remotes/", 8) == 0)
921 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
923 err = got_error_from_errno("asprintf");
929 err = got_object_id_str(&id_str, id);
933 printf("-----------------------------------------------\n");
934 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
935 refs_str ? refs_str : "", refs_str ? ")" : "");
940 printf("from: %s\n", got_object_commit_get_author(commit));
941 committer_time = got_object_commit_get_committer_time(commit);
942 datestr = get_datestr(&committer_time, datebuf);
943 printf("date: %s UTC\n", datestr);
944 author = got_object_commit_get_author(commit);
945 committer = got_object_commit_get_committer(commit);
946 if (strcmp(author, committer) != 0)
947 printf("via: %s\n", committer);
948 if (got_object_commit_get_nparents(commit) > 1) {
949 const struct got_object_id_queue *parent_ids;
950 struct got_object_qid *qid;
952 parent_ids = got_object_commit_get_parent_ids(commit);
953 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
954 err = got_object_id_str(&id_str, qid->id);
957 printf("parent %d: %s\n", n++, id_str);
962 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
964 return got_error_from_errno("strdup");
968 line = strsep(&logmsg, "\n");
970 printf(" %s\n", line);
975 err = print_patch(commit, id, diff_context, repo);
980 if (fflush(stdout) != 0 && err == NULL)
981 err = got_error_from_errno("fflush");
985 static const struct got_error *
986 print_commits(struct got_object_id *root_id, struct got_repository *repo,
987 char *path, int show_patch, int diff_context, int limit,
988 int first_parent_traversal, struct got_reflist_head *refs)
990 const struct got_error *err;
991 struct got_commit_graph *graph;
993 err = got_commit_graph_open(&graph, root_id, path,
994 first_parent_traversal, repo);
997 err = got_commit_graph_iter_start(graph, root_id, repo);
1001 struct got_commit_object *commit;
1002 struct got_object_id *id;
1004 if (sigint_received || sigpipe_received)
1007 err = got_commit_graph_iter_next(&id, graph);
1009 if (err->code == GOT_ERR_ITER_COMPLETED) {
1013 if (err->code != GOT_ERR_ITER_NEED_MORE)
1015 err = got_commit_graph_fetch_commits(graph, 1, repo);
1024 err = got_object_open_as_commit(&commit, repo, id);
1027 err = print_commit(commit, id, repo, show_patch, diff_context,
1029 got_object_commit_close(commit);
1030 if (err || (limit && --limit == 0))
1034 got_commit_graph_close(graph);
1041 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
1042 "[-r repository-path] [path]\n", getprogname());
1046 static const struct got_error *
1047 cmd_log(int argc, char *argv[])
1049 const struct got_error *error;
1050 struct got_repository *repo = NULL;
1051 struct got_worktree *worktree = NULL;
1052 struct got_commit_object *commit = NULL;
1053 struct got_object_id *id = NULL;
1054 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1055 char *start_commit = NULL;
1056 int diff_context = 3, ch;
1057 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1059 struct got_reflist_head refs;
1061 SIMPLEQ_INIT(&refs);
1064 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1070 while ((ch = getopt(argc, argv, "bpc:C:l:r:")) != -1) {
1073 first_parent_traversal = 1;
1079 start_commit = optarg;
1082 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1085 err(1, "-C option %s", errstr);
1088 limit = strtonum(optarg, 1, INT_MAX, &errstr);
1090 err(1, "-l option %s", errstr);
1093 repo_path = realpath(optarg, NULL);
1094 if (repo_path == NULL)
1095 err(1, "-r option");
1096 got_path_strip_trailing_slashes(repo_path);
1107 cwd = getcwd(NULL, 0);
1109 error = got_error_from_errno("getcwd");
1113 error = got_worktree_open(&worktree, cwd);
1114 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1121 error = got_error_from_errno("strdup");
1124 } else if (argc == 1) {
1126 error = got_worktree_resolve_path(&path, worktree,
1131 path = strdup(argv[0]);
1133 error = got_error_from_errno("strdup");
1140 if (repo_path == NULL) {
1141 repo_path = worktree ?
1142 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1144 if (repo_path == NULL) {
1145 error = got_error_from_errno("strdup");
1149 error = got_repo_open(&repo, repo_path);
1153 error = apply_unveil(got_repo_get_path(repo), 1,
1154 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1158 if (start_commit == NULL) {
1159 struct got_reference *head_ref;
1160 error = got_ref_open(&head_ref, repo,
1161 worktree ? got_worktree_get_head_ref_name(worktree)
1165 error = got_ref_resolve(&id, repo, head_ref);
1166 got_ref_close(head_ref);
1169 error = got_object_open_as_commit(&commit, repo, id);
1171 struct got_reference *ref;
1172 error = got_ref_open(&ref, repo, start_commit, 0);
1173 if (error == NULL) {
1175 error = got_ref_resolve(&id, repo, ref);
1179 error = got_object_get_type(&obj_type, repo, id);
1182 if (obj_type == GOT_OBJ_TYPE_TAG) {
1183 struct got_tag_object *tag;
1184 error = got_object_open_as_tag(&tag, repo, id);
1187 if (got_object_tag_get_object_type(tag) !=
1188 GOT_OBJ_TYPE_COMMIT) {
1189 got_object_tag_close(tag);
1190 error = got_error(GOT_ERR_OBJ_TYPE);
1194 id = got_object_id_dup(
1195 got_object_tag_get_object_id(tag));
1197 error = got_error_from_errno(
1198 "got_object_id_dup");
1199 got_object_tag_close(tag);
1202 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1203 error = got_error(GOT_ERR_OBJ_TYPE);
1206 error = got_object_open_as_commit(&commit, repo, id);
1210 if (commit == NULL) {
1211 error = got_repo_match_object_id_prefix(&id,
1212 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1220 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1225 path = in_repo_path;
1228 error = got_ref_list(&refs, repo);
1232 error = print_commits(id, repo, path, show_patch,
1233 diff_context, limit, first_parent_traversal, &refs);
1240 got_worktree_close(worktree);
1242 const struct got_error *repo_error;
1243 repo_error = got_repo_close(repo);
1247 got_ref_list_free(&refs);
1254 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1255 "[object1 object2 | path]\n", getprogname());
1259 struct print_diff_arg {
1260 struct got_repository *repo;
1261 struct got_worktree *worktree;
1267 static const struct got_error *
1268 print_diff(void *arg, unsigned char status, const char *path,
1269 struct got_object_id *blob_id, struct got_object_id *commit_id)
1271 struct print_diff_arg *a = arg;
1272 const struct got_error *err = NULL;
1273 struct got_blob_object *blob1 = NULL;
1275 char *abspath = NULL;
1278 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1279 status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1282 if (!a->header_shown) {
1283 printf("diff %s %s\n", a->id_str,
1284 got_worktree_get_root_path(a->worktree));
1285 a->header_shown = 1;
1288 if (status != GOT_STATUS_ADD) {
1289 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1295 if (status != GOT_STATUS_DELETE) {
1296 if (asprintf(&abspath, "%s/%s",
1297 got_worktree_get_root_path(a->worktree), path) == -1) {
1298 err = got_error_from_errno("asprintf");
1302 f2 = fopen(abspath, "r");
1304 err = got_error_from_errno2("fopen", abspath);
1307 if (lstat(abspath, &sb) == -1) {
1308 err = got_error_from_errno2("lstat", abspath);
1314 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1318 got_object_blob_close(blob1);
1319 if (f2 && fclose(f2) != 0 && err == NULL)
1320 err = got_error_from_errno("fclose");
1325 static const struct got_error *
1326 cmd_diff(int argc, char *argv[])
1328 const struct got_error *error;
1329 struct got_repository *repo = NULL;
1330 struct got_worktree *worktree = NULL;
1331 char *cwd = NULL, *repo_path = NULL;
1332 struct got_object_id *id1 = NULL, *id2 = NULL;
1333 const char *id_str1 = NULL, *id_str2 = NULL;
1334 char *label1 = NULL, *label2 = NULL;
1336 int diff_context = 3, ch;
1341 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1346 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1349 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1351 err(1, "-C option %s", errstr);
1354 repo_path = realpath(optarg, NULL);
1355 if (repo_path == NULL)
1356 err(1, "-r option");
1357 got_path_strip_trailing_slashes(repo_path);
1368 cwd = getcwd(NULL, 0);
1370 error = got_error_from_errno("getcwd");
1373 error = got_worktree_open(&worktree, cwd);
1374 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1377 if (worktree == NULL) {
1378 error = got_error(GOT_ERR_NOT_WORKTREE);
1383 "-r option can't be used when diffing a work tree");
1384 repo_path = strdup(got_worktree_get_repo_path(worktree));
1385 if (repo_path == NULL) {
1386 error = got_error_from_errno("strdup");
1390 error = got_worktree_resolve_path(&path, worktree,
1397 error = got_error_from_errno("strdup");
1401 } else if (argc == 2) {
1404 if (worktree && repo_path == NULL) {
1406 strdup(got_worktree_get_repo_path(worktree));
1407 if (repo_path == NULL) {
1408 error = got_error_from_errno("strdup");
1415 if (repo_path == NULL) {
1416 repo_path = getcwd(NULL, 0);
1417 if (repo_path == NULL)
1418 return got_error_from_errno("getcwd");
1421 error = got_repo_open(&repo, repo_path);
1426 error = apply_unveil(got_repo_get_path(repo), 1,
1427 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1432 struct print_diff_arg arg;
1434 error = got_object_id_str(&id_str,
1435 got_worktree_get_base_commit_id(worktree));
1439 arg.worktree = worktree;
1440 arg.diff_context = diff_context;
1441 arg.id_str = id_str;
1442 arg.header_shown = 0;
1444 error = got_worktree_status(worktree, path, repo, print_diff,
1445 &arg, check_cancelled, NULL);
1450 error = got_repo_match_object_id_prefix(&id1, id_str1,
1451 GOT_OBJ_TYPE_ANY, repo);
1453 struct got_reference *ref;
1454 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1456 error = got_ref_open(&ref, repo, id_str1, 0);
1459 label1 = strdup(got_ref_get_name(ref));
1460 if (label1 == NULL) {
1461 error = got_error_from_errno("strdup");
1464 error = got_ref_resolve(&id1, repo, ref);
1469 label1 = strdup(id_str1);
1470 if (label1 == NULL) {
1471 error = got_error_from_errno("strdup");
1476 error = got_repo_match_object_id_prefix(&id2, id_str2,
1477 GOT_OBJ_TYPE_ANY, repo);
1479 struct got_reference *ref;
1480 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1482 error = got_ref_open(&ref, repo, id_str2, 0);
1485 label2 = strdup(got_ref_get_name(ref));
1486 if (label2 == NULL) {
1487 error = got_error_from_errno("strdup");
1490 error = got_ref_resolve(&id2, repo, ref);
1495 label2 = strdup(id_str2);
1496 if (label2 == NULL) {
1497 error = got_error_from_errno("strdup");
1502 error = got_object_get_type(&type1, repo, id1);
1506 error = got_object_get_type(&type2, repo, id2);
1510 if (type1 != type2) {
1511 error = got_error(GOT_ERR_OBJ_TYPE);
1516 case GOT_OBJ_TYPE_BLOB:
1517 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1518 diff_context, repo, stdout);
1520 case GOT_OBJ_TYPE_TREE:
1521 error = got_diff_objects_as_trees(id1, id2, "", "",
1522 diff_context, repo, stdout);
1524 case GOT_OBJ_TYPE_COMMIT:
1525 printf("diff %s %s\n", label1, label2);
1526 error = got_diff_objects_as_commits(id1, id2, diff_context,
1530 error = got_error(GOT_ERR_OBJ_TYPE);
1540 got_worktree_close(worktree);
1542 const struct got_error *repo_error;
1543 repo_error = got_repo_close(repo);
1554 "usage: %s blame [-c commit] [-r repository-path] path\n",
1559 static const struct got_error *
1560 cmd_blame(int argc, char *argv[])
1562 const struct got_error *error;
1563 struct got_repository *repo = NULL;
1564 struct got_worktree *worktree = NULL;
1565 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1566 struct got_object_id *commit_id = NULL;
1567 char *commit_id_str = NULL;
1571 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1576 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1579 commit_id_str = optarg;
1582 repo_path = realpath(optarg, NULL);
1583 if (repo_path == NULL)
1584 err(1, "-r option");
1585 got_path_strip_trailing_slashes(repo_path);
1601 cwd = getcwd(NULL, 0);
1603 error = got_error_from_errno("getcwd");
1606 if (repo_path == NULL) {
1607 error = got_worktree_open(&worktree, cwd);
1608 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1614 strdup(got_worktree_get_repo_path(worktree));
1615 if (repo_path == NULL)
1616 error = got_error_from_errno("strdup");
1620 repo_path = strdup(cwd);
1621 if (repo_path == NULL) {
1622 error = got_error_from_errno("strdup");
1628 error = got_repo_open(&repo, repo_path);
1632 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1637 const char *prefix = got_worktree_get_path_prefix(worktree);
1638 char *p, *worktree_subdir = cwd +
1639 strlen(got_worktree_get_root_path(worktree));
1640 if (asprintf(&p, "%s%s%s%s%s",
1641 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1642 worktree_subdir, worktree_subdir[0] ? "/" : "",
1644 error = got_error_from_errno("asprintf");
1647 error = got_repo_map_path(&in_repo_path, repo, p, 0);
1650 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1655 if (commit_id_str == NULL) {
1656 struct got_reference *head_ref;
1657 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1660 error = got_ref_resolve(&commit_id, repo, head_ref);
1661 got_ref_close(head_ref);
1665 error = got_repo_match_object_id_prefix(&commit_id,
1666 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
1671 error = got_blame(in_repo_path, commit_id, repo, stdout);
1678 got_worktree_close(worktree);
1680 const struct got_error *repo_error;
1681 repo_error = got_repo_close(repo);
1692 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1698 print_entry(struct got_tree_entry *te, const char *id, const char *path,
1699 const char *root_path)
1701 int is_root_path = (strcmp(path, root_path) == 0);
1703 path += strlen(root_path);
1704 while (path[0] == '/')
1707 printf("%s%s%s%s%s\n", id ? id : "", path,
1708 is_root_path ? "" : "/", te->name,
1709 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
1712 static const struct got_error *
1713 print_tree(const char *path, struct got_object_id *commit_id,
1714 int show_ids, int recurse, const char *root_path,
1715 struct got_repository *repo)
1717 const struct got_error *err = NULL;
1718 struct got_object_id *tree_id = NULL;
1719 struct got_tree_object *tree = NULL;
1720 const struct got_tree_entries *entries;
1721 struct got_tree_entry *te;
1723 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1727 err = got_object_open_as_tree(&tree, repo, tree_id);
1730 entries = got_object_tree_get_entries(tree);
1731 te = SIMPLEQ_FIRST(&entries->head);
1735 if (sigint_received || sigpipe_received)
1740 err = got_object_id_str(&id_str, te->id);
1743 if (asprintf(&id, "%s ", id_str) == -1) {
1744 err = got_error_from_errno("asprintf");
1750 print_entry(te, id, path, root_path);
1753 if (recurse && S_ISDIR(te->mode)) {
1755 if (asprintf(&child_path, "%s%s%s", path,
1756 path[0] == '/' && path[1] == '\0' ? "" : "/",
1758 err = got_error_from_errno("asprintf");
1761 err = print_tree(child_path, commit_id, show_ids, 1,
1768 te = SIMPLEQ_NEXT(te, entry);
1772 got_object_tree_close(tree);
1777 static const struct got_error *
1778 cmd_tree(int argc, char *argv[])
1780 const struct got_error *error;
1781 struct got_repository *repo = NULL;
1782 struct got_worktree *worktree = NULL;
1784 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1785 struct got_object_id *commit_id = NULL;
1786 char *commit_id_str = NULL;
1787 int show_ids = 0, recurse = 0;
1791 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1796 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1799 commit_id_str = optarg;
1802 repo_path = realpath(optarg, NULL);
1803 if (repo_path == NULL)
1804 err(1, "-r option");
1805 got_path_strip_trailing_slashes(repo_path);
1829 cwd = getcwd(NULL, 0);
1831 error = got_error_from_errno("getcwd");
1834 if (repo_path == NULL) {
1835 error = got_worktree_open(&worktree, cwd);
1836 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1842 strdup(got_worktree_get_repo_path(worktree));
1843 if (repo_path == NULL)
1844 error = got_error_from_errno("strdup");
1848 repo_path = strdup(cwd);
1849 if (repo_path == NULL) {
1850 error = got_error_from_errno("strdup");
1856 error = got_repo_open(&repo, repo_path);
1860 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1866 char *p, *worktree_subdir = cwd +
1867 strlen(got_worktree_get_root_path(worktree));
1868 if (asprintf(&p, "%s/%s",
1869 got_worktree_get_path_prefix(worktree),
1870 worktree_subdir) == -1) {
1871 error = got_error_from_errno("asprintf");
1874 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1881 if (in_repo_path == NULL) {
1882 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1887 if (commit_id_str == NULL) {
1888 struct got_reference *head_ref;
1889 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1892 error = got_ref_resolve(&commit_id, repo, head_ref);
1893 got_ref_close(head_ref);
1897 error = got_repo_match_object_id_prefix(&commit_id,
1898 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
1903 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1904 in_repo_path, repo);
1911 got_worktree_close(worktree);
1913 const struct got_error *repo_error;
1914 repo_error = got_repo_close(repo);
1924 fprintf(stderr, "usage: %s status [path]\n", getprogname());
1928 static const struct got_error *
1929 print_status(void *arg, unsigned char status, const char *path,
1930 struct got_object_id *blob_id, struct got_object_id *commit_id)
1932 printf("%c %s\n", status, path);
1936 static const struct got_error *
1937 cmd_status(int argc, char *argv[])
1939 const struct got_error *error = NULL;
1940 struct got_repository *repo = NULL;
1941 struct got_worktree *worktree = NULL;
1942 char *cwd = NULL, *path = NULL;
1945 while ((ch = getopt(argc, argv, "")) != -1) {
1957 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1961 cwd = getcwd(NULL, 0);
1963 error = got_error_from_errno("getcwd");
1967 error = got_worktree_open(&worktree, cwd);
1974 error = got_error_from_errno("strdup");
1977 } else if (argc == 1) {
1978 error = got_worktree_resolve_path(&path, worktree, argv[0]);
1984 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1988 error = apply_unveil(got_repo_get_path(repo), 1,
1989 got_worktree_get_root_path(worktree), 0);
1993 error = got_worktree_status(worktree, path, repo, print_status, NULL,
1994 check_cancelled, NULL);
2005 "usage: %s ref [-r repository] -l | -d name | name target\n",
2010 static const struct got_error *
2011 list_refs(struct got_repository *repo)
2013 static const struct got_error *err = NULL;
2014 struct got_reflist_head refs;
2015 struct got_reflist_entry *re;
2017 SIMPLEQ_INIT(&refs);
2018 err = got_ref_list(&refs, repo);
2022 SIMPLEQ_FOREACH(re, &refs, entry) {
2024 refstr = got_ref_to_str(re->ref);
2026 return got_error_from_errno("got_ref_to_str");
2027 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2031 got_ref_list_free(&refs);
2035 static const struct got_error *
2036 delete_ref(struct got_repository *repo, const char *refname)
2038 const struct got_error *err = NULL;
2039 struct got_reference *ref;
2041 err = got_ref_open(&ref, repo, refname, 0);
2045 err = got_ref_delete(ref, repo);
2050 static const struct got_error *
2051 add_ref(struct got_repository *repo, const char *refname, const char *target)
2053 const struct got_error *err = NULL;
2054 struct got_object_id *id;
2055 struct got_reference *ref = NULL;
2057 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2060 struct got_reference *target_ref;
2062 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2064 err = got_ref_open(&target_ref, repo, target, 0);
2067 err = got_ref_resolve(&id, repo, target_ref);
2068 got_ref_close(target_ref);
2073 err = got_ref_alloc(&ref, refname, id);
2077 err = got_ref_write(ref, repo);
2085 static const struct got_error *
2086 cmd_ref(int argc, char *argv[])
2088 const struct got_error *error = NULL;
2089 struct got_repository *repo = NULL;
2090 struct got_worktree *worktree = NULL;
2091 char *cwd = NULL, *repo_path = NULL;
2092 int ch, do_list = 0;
2093 const char *delref = NULL;
2095 /* TODO: Add -s option for adding symbolic references. */
2096 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2102 repo_path = realpath(optarg, NULL);
2103 if (repo_path == NULL)
2104 err(1, "-r option");
2105 got_path_strip_trailing_slashes(repo_path);
2116 if (do_list && delref)
2117 errx(1, "-l and -d options are mutually exclusive\n");
2122 if (do_list || delref) {
2125 } else if (argc != 2)
2130 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2134 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2135 "sendfd unveil", NULL) == -1)
2139 cwd = getcwd(NULL, 0);
2141 error = got_error_from_errno("getcwd");
2145 if (repo_path == NULL) {
2146 error = got_worktree_open(&worktree, cwd);
2147 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2153 strdup(got_worktree_get_repo_path(worktree));
2154 if (repo_path == NULL)
2155 error = got_error_from_errno("strdup");
2159 repo_path = strdup(cwd);
2160 if (repo_path == NULL) {
2161 error = got_error_from_errno("strdup");
2167 error = got_repo_open(&repo, repo_path);
2171 error = apply_unveil(got_repo_get_path(repo), do_list,
2172 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
2177 error = list_refs(repo);
2179 error = delete_ref(repo, delref);
2181 error = add_ref(repo, argv[0], argv[1]);
2184 got_repo_close(repo);
2186 got_worktree_close(worktree);
2196 "usage: %s branch [-r repository] -l | -d name | "
2197 "name [base-branch]\n", getprogname());
2201 static const struct got_error *
2202 list_branches(struct got_repository *repo)
2204 static const struct got_error *err = NULL;
2205 struct got_reflist_head refs;
2206 struct got_reflist_entry *re;
2208 SIMPLEQ_INIT(&refs);
2209 err = got_ref_list(&refs, repo);
2213 SIMPLEQ_FOREACH(re, &refs, entry) {
2214 const char *refname;
2216 refname = got_ref_get_name(re->ref);
2217 if (strncmp(refname, "refs/heads/", 11) != 0)
2220 refstr = got_ref_to_str(re->ref);
2222 return got_error_from_errno("got_ref_to_str");
2223 printf("%s: %s\n", refname, refstr);
2227 got_ref_list_free(&refs);
2231 static const struct got_error *
2232 delete_branch(struct got_repository *repo, const char *branch_name)
2234 const struct got_error *err = NULL;
2235 struct got_reference *ref;
2238 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
2239 return got_error_from_errno("asprintf");
2241 err = got_ref_open(&ref, repo, refname, 0);
2245 err = got_ref_delete(ref, repo);
2252 static const struct got_error *
2253 add_branch(struct got_repository *repo, const char *branch_name,
2254 const char *base_branch)
2256 const struct got_error *err = NULL;
2257 struct got_object_id *id = NULL;
2258 struct got_reference *ref = NULL;
2259 char *base_refname = NULL, *refname = NULL;
2260 struct got_reference *base_ref;
2262 if (strcmp(GOT_REF_HEAD, base_branch) == 0) {
2263 base_refname = strdup(GOT_REF_HEAD);
2264 if (base_refname == NULL)
2265 return got_error_from_errno("strdup");
2266 } else if (asprintf(&base_refname, "refs/heads/%s", base_branch) == -1)
2267 return got_error_from_errno("asprintf");
2269 err = got_ref_open(&base_ref, repo, base_refname, 0);
2272 err = got_ref_resolve(&id, repo, base_ref);
2273 got_ref_close(base_ref);
2277 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
2278 err = got_error_from_errno("asprintf");
2282 err = got_ref_open(&ref, repo, refname, 0);
2284 err = got_error(GOT_ERR_BRANCH_EXISTS);
2286 } else if (err->code != GOT_ERR_NOT_REF)
2289 err = got_ref_alloc(&ref, refname, id);
2293 err = got_ref_write(ref, repo);
2303 static const struct got_error *
2304 cmd_branch(int argc, char *argv[])
2306 const struct got_error *error = NULL;
2307 struct got_repository *repo = NULL;
2308 struct got_worktree *worktree = NULL;
2309 char *cwd = NULL, *repo_path = NULL;
2310 int ch, do_list = 0;
2311 const char *delref = NULL;
2313 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2319 repo_path = realpath(optarg, NULL);
2320 if (repo_path == NULL)
2321 err(1, "-r option");
2322 got_path_strip_trailing_slashes(repo_path);
2333 if (do_list && delref)
2334 errx(1, "-l and -d options are mutually exclusive\n");
2339 if (do_list || delref) {
2342 } else if (argc < 1 || argc > 2)
2347 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2351 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2352 "sendfd unveil", NULL) == -1)
2356 cwd = getcwd(NULL, 0);
2358 error = got_error_from_errno("getcwd");
2362 if (repo_path == NULL) {
2363 error = got_worktree_open(&worktree, cwd);
2364 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2370 strdup(got_worktree_get_repo_path(worktree));
2371 if (repo_path == NULL)
2372 error = got_error_from_errno("strdup");
2376 repo_path = strdup(cwd);
2377 if (repo_path == NULL) {
2378 error = got_error_from_errno("strdup");
2384 error = got_repo_open(&repo, repo_path);
2388 error = apply_unveil(got_repo_get_path(repo), do_list,
2389 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
2394 error = list_branches(repo);
2396 error = delete_branch(repo, delref);
2398 const char *base_branch;
2400 base_branch = worktree ?
2401 got_worktree_get_head_ref_name(worktree) :
2404 base_branch = argv[1];
2405 error = add_branch(repo, argv[0], base_branch);
2409 got_repo_close(repo);
2411 got_worktree_close(worktree);
2420 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
2424 static const struct got_error *
2425 cmd_add(int argc, char *argv[])
2427 const struct got_error *error = NULL;
2428 struct got_repository *repo = NULL;
2429 struct got_worktree *worktree = NULL;
2431 struct got_pathlist_head paths;
2432 struct got_pathlist_entry *pe;
2437 while ((ch = getopt(argc, argv, "")) != -1) {
2451 /* make sure each file exists before doing anything halfway */
2452 for (x = 0; x < argc; x++) {
2453 char *path = realpath(argv[x], NULL);
2455 error = got_error_from_errno2("realpath", argv[x]);
2461 cwd = getcwd(NULL, 0);
2463 error = got_error_from_errno("getcwd");
2467 error = got_worktree_open(&worktree, cwd);
2471 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2475 error = apply_unveil(got_repo_get_path(repo), 1,
2476 got_worktree_get_root_path(worktree), 0);
2480 for (x = 0; x < argc; x++) {
2481 char *path = realpath(argv[x], NULL);
2483 error = got_error_from_errno2("realpath", argv[x]);
2487 got_path_strip_trailing_slashes(path);
2488 error = got_pathlist_insert(&pe, &paths, path, NULL);
2494 error = got_worktree_schedule_add(worktree, &paths, print_status,
2498 got_repo_close(repo);
2500 got_worktree_close(worktree);
2501 TAILQ_FOREACH(pe, &paths, entry)
2502 free((char *)pe->path);
2503 got_pathlist_free(&paths);
2511 fprintf(stderr, "usage: %s rm [-f] file-path ...\n", getprogname());
2515 static const struct got_error *
2516 cmd_rm(int argc, char *argv[])
2518 const struct got_error *error = NULL;
2519 struct got_worktree *worktree = NULL;
2520 struct got_repository *repo = NULL;
2522 struct got_pathlist_head paths;
2523 struct got_pathlist_entry *pe;
2524 int ch, i, delete_local_mods = 0;
2528 while ((ch = getopt(argc, argv, "f")) != -1) {
2531 delete_local_mods = 1;
2545 /* make sure each file exists before doing anything halfway */
2546 for (i = 0; i < argc; i++) {
2547 char *path = realpath(argv[i], NULL);
2549 error = got_error_from_errno2("realpath", argv[i]);
2555 cwd = getcwd(NULL, 0);
2557 error = got_error_from_errno("getcwd");
2560 error = got_worktree_open(&worktree, cwd);
2564 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2568 error = apply_unveil(got_repo_get_path(repo), 1,
2569 got_worktree_get_root_path(worktree), 0);
2573 for (i = 0; i < argc; i++) {
2574 char *path = realpath(argv[i], NULL);
2576 error = got_error_from_errno2("realpath", argv[i]);
2580 got_path_strip_trailing_slashes(path);
2581 error = got_pathlist_insert(&pe, &paths, path, NULL);
2587 error = got_worktree_schedule_delete(worktree, &paths,
2588 delete_local_mods, print_status, NULL, repo);
2593 got_repo_close(repo);
2595 got_worktree_close(worktree);
2596 TAILQ_FOREACH(pe, &paths, entry)
2597 free((char *)pe->path);
2598 got_pathlist_free(&paths);
2606 fprintf(stderr, "usage: %s revert file-path ...\n", getprogname());
2611 revert_progress(void *arg, unsigned char status, const char *path)
2613 while (path[0] == '/')
2615 printf("%c %s\n", status, path);
2618 static const struct got_error *
2619 cmd_revert(int argc, char *argv[])
2621 const struct got_error *error = NULL;
2622 struct got_worktree *worktree = NULL;
2623 struct got_repository *repo = NULL;
2624 char *cwd = NULL, *path = NULL;
2625 struct got_pathlist_head paths;
2626 struct got_pathlist_entry *pe;
2631 while ((ch = getopt(argc, argv, "")) != -1) {
2645 for (i = 0; i < argc; i++) {
2646 char *path = realpath(argv[i], NULL);
2648 error = got_error_from_errno2("realpath", argv[i]);
2652 got_path_strip_trailing_slashes(path);
2653 error = got_pathlist_insert(&pe, &paths, path, NULL);
2660 cwd = getcwd(NULL, 0);
2662 error = got_error_from_errno("getcwd");
2665 error = got_worktree_open(&worktree, cwd);
2669 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2673 error = apply_unveil(got_repo_get_path(repo), 1,
2674 got_worktree_get_root_path(worktree), 0);
2678 error = got_worktree_revert(worktree, &paths,
2679 revert_progress, NULL, repo);
2684 got_repo_close(repo);
2686 got_worktree_close(worktree);
2695 fprintf(stderr, "usage: %s commit [-m msg] file-path\n", getprogname());
2700 spawn_editor(const char *editor, const char *file)
2703 sig_t sighup, sigint, sigquit;
2706 sighup = signal(SIGHUP, SIG_IGN);
2707 sigint = signal(SIGINT, SIG_IGN);
2708 sigquit = signal(SIGQUIT, SIG_IGN);
2710 switch (pid = fork()) {
2714 execl(editor, editor, file, (char *)NULL);
2718 while (waitpid(pid, &st, 0) == -1)
2723 (void)signal(SIGHUP, sighup);
2724 (void)signal(SIGINT, sigint);
2725 (void)signal(SIGQUIT, sigquit);
2727 if (!WIFEXITED(st)) {
2732 return WEXITSTATUS(st);
2735 struct collect_commit_logmsg_arg {
2736 const char *cmdline_log;
2738 const char *worktree_path;
2739 const char *branch_name;
2740 const char *repo_path;
2745 static const struct got_error *
2746 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
2749 char *initial_content = NULL;
2750 struct got_pathlist_entry *pe;
2751 const struct got_error *err = NULL;
2752 char *template = NULL;
2753 struct collect_commit_logmsg_arg *a = arg;
2755 struct stat st, st2;
2758 int fd, content_changed = 0;
2760 /* if a message was specified on the command line, just use it */
2761 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
2762 len = strlen(a->cmdline_log) + 1;
2763 *logmsg = malloc(len + 1);
2764 if (*logmsg == NULL)
2765 return got_error_from_errno("malloc");
2766 strlcpy(*logmsg, a->cmdline_log, len);
2770 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
2771 return got_error_from_errno("asprintf");
2773 if (asprintf(&initial_content,
2774 "\n# changes to be committed on branch %s:\n",
2775 a->branch_name) == -1)
2776 return got_error_from_errno("asprintf");
2778 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
2782 dprintf(fd, initial_content);
2784 TAILQ_FOREACH(pe, commitable_paths, entry) {
2785 struct got_commitable *ct = pe->data;
2786 dprintf(fd, "# %c %s\n",
2787 got_commitable_get_status(ct),
2788 got_commitable_get_path(ct));
2792 if (stat(a->logmsg_path, &st) == -1) {
2793 err = got_error_from_errno2("stat", a->logmsg_path);
2797 if (spawn_editor(a->editor, a->logmsg_path) == -1) {
2798 err = got_error_from_errno("failed spawning editor");
2802 if (stat(a->logmsg_path, &st2) == -1) {
2803 err = got_error_from_errno("stat");
2807 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size) {
2808 unlink(a->logmsg_path);
2809 free(a->logmsg_path);
2810 a->logmsg_path = NULL;
2811 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2812 "no changes made to commit message, aborting");
2816 *logmsg = malloc(st2.st_size + 1);
2817 if (*logmsg == NULL) {
2818 err = got_error_from_errno("malloc");
2821 (*logmsg)[0] = '\0';
2824 fp = fopen(a->logmsg_path, "r");
2826 err = got_error_from_errno("fopen");
2829 while (fgets(buf, sizeof(buf), fp) != NULL) {
2830 if (!content_changed && strcmp(buf, initial_content) != 0)
2831 content_changed = 1;
2832 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
2833 continue; /* remove comments and leading empty lines */
2834 len = strlcat(*logmsg, buf, st2.st_size);
2838 while (len > 0 && (*logmsg)[len - 1] == '\n') {
2839 (*logmsg)[len - 1] = '\0';
2843 if (len == 0 || !content_changed) {
2844 unlink(a->logmsg_path);
2845 free(a->logmsg_path);
2846 a->logmsg_path = NULL;
2847 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2848 "commit message cannot be empty, aborting");
2852 free(initial_content);
2855 /* Editor is done; we can now apply unveil(2) */
2857 err = apply_unveil(a->repo_path, 0, a->worktree_path, 0);
2861 static const struct got_error *
2862 cmd_commit(int argc, char *argv[])
2864 const struct got_error *error = NULL;
2865 struct got_worktree *worktree = NULL;
2866 struct got_repository *repo = NULL;
2867 char *cwd = NULL, *path = NULL, *id_str = NULL;
2868 struct got_object_id *id = NULL;
2869 const char *logmsg = NULL;
2870 const char *got_author = getenv("GOT_AUTHOR");
2871 struct collect_commit_logmsg_arg cl_arg;
2872 char *editor = NULL;
2875 while ((ch = getopt(argc, argv, "m:")) != -1) {
2890 path = realpath(argv[0], NULL);
2892 error = got_error_from_errno2("realpath", argv[0]);
2895 got_path_strip_trailing_slashes(path);
2896 } else if (argc != 0)
2899 if (got_author == NULL) {
2900 /* TODO: Look current user up in password database */
2901 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
2905 cwd = getcwd(NULL, 0);
2907 error = got_error_from_errno("getcwd");
2910 error = got_worktree_open(&worktree, cwd);
2914 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2919 * unveil(2) traverses exec(2); if an editor is used we have
2920 * to apply unveil after the log message has been written.
2922 if (logmsg == NULL || strlen(logmsg) == 0)
2923 error = get_editor(&editor);
2925 error = apply_unveil(got_repo_get_path(repo), 0,
2926 got_worktree_get_root_path(worktree), 0);
2930 cl_arg.editor = editor;
2931 cl_arg.cmdline_log = logmsg;
2932 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
2933 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
2934 if (strncmp(cl_arg.branch_name, "refs/", 5) == 0)
2935 cl_arg.branch_name += 5;
2936 if (strncmp(cl_arg.branch_name, "heads/", 6) == 0)
2937 cl_arg.branch_name += 6;
2938 cl_arg.repo_path = got_repo_get_path(repo);
2939 cl_arg.logmsg_path = NULL;
2940 error = got_worktree_commit(&id, worktree, path, got_author, NULL,
2941 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
2943 if (cl_arg.logmsg_path)
2944 fprintf(stderr, "%s: log message preserved in %s\n",
2945 getprogname(), cl_arg.logmsg_path);
2949 if (cl_arg.logmsg_path)
2950 unlink(cl_arg.logmsg_path);
2952 error = got_object_id_str(&id_str, id);
2955 printf("Created commit %s\n", id_str);
2958 got_repo_close(repo);
2960 got_worktree_close(worktree);
2969 usage_cherrypick(void)
2971 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
2975 static const struct got_error *
2976 cmd_cherrypick(int argc, char *argv[])
2978 const struct got_error *error = NULL;
2979 struct got_worktree *worktree = NULL;
2980 struct got_repository *repo = NULL;
2981 char *cwd = NULL, *commit_id_str = NULL;
2982 struct got_object_id *commit_id = NULL;
2983 struct got_commit_object *commit = NULL;
2984 struct got_object_qid *pid;
2985 struct got_reference *head_ref = NULL;
2986 int ch, did_something = 0;
2988 while ((ch = getopt(argc, argv, "")) != -1) {
3002 cwd = getcwd(NULL, 0);
3004 error = got_error_from_errno("getcwd");
3007 error = got_worktree_open(&worktree, cwd);
3011 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3015 error = apply_unveil(got_repo_get_path(repo), 0,
3016 got_worktree_get_root_path(worktree), 0);
3020 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3021 GOT_OBJ_TYPE_COMMIT, repo);
3022 if (error != NULL) {
3023 struct got_reference *ref;
3024 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3026 error = got_ref_open(&ref, repo, argv[0], 0);
3029 error = got_ref_resolve(&commit_id, repo, ref);
3034 error = got_object_id_str(&commit_id_str, commit_id);
3038 error = got_ref_open(&head_ref, repo,
3039 got_worktree_get_head_ref_name(worktree), 0);
3043 error = check_same_branch(commit_id, head_ref, repo);
3045 if (error->code != GOT_ERR_ANCESTRY)
3049 error = got_error(GOT_ERR_SAME_BRANCH);
3053 error = got_object_open_as_commit(&commit, repo, commit_id);
3056 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3057 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
3058 commit_id, repo, update_progress, &did_something, check_cancelled,
3064 printf("Merged commit %s\n", commit_id_str);
3067 got_object_commit_close(commit);
3068 free(commit_id_str);
3070 got_ref_close(head_ref);
3072 got_worktree_close(worktree);
3074 got_repo_close(repo);
3081 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
3085 static const struct got_error *
3086 cmd_backout(int argc, char *argv[])
3088 const struct got_error *error = NULL;
3089 struct got_worktree *worktree = NULL;
3090 struct got_repository *repo = NULL;
3091 char *cwd = NULL, *commit_id_str = NULL;
3092 struct got_object_id *commit_id = NULL;
3093 struct got_commit_object *commit = NULL;
3094 struct got_object_qid *pid;
3095 struct got_reference *head_ref = NULL;
3096 int ch, did_something = 0;
3098 while ((ch = getopt(argc, argv, "")) != -1) {
3112 cwd = getcwd(NULL, 0);
3114 error = got_error_from_errno("getcwd");
3117 error = got_worktree_open(&worktree, cwd);
3121 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3125 error = apply_unveil(got_repo_get_path(repo), 0,
3126 got_worktree_get_root_path(worktree), 0);
3130 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3131 GOT_OBJ_TYPE_COMMIT, repo);
3132 if (error != NULL) {
3133 struct got_reference *ref;
3134 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3136 error = got_ref_open(&ref, repo, argv[0], 0);
3139 error = got_ref_resolve(&commit_id, repo, ref);
3144 error = got_object_id_str(&commit_id_str, commit_id);
3148 error = got_ref_open(&head_ref, repo,
3149 got_worktree_get_head_ref_name(worktree), 0);
3153 error = check_same_branch(commit_id, head_ref, repo);
3157 error = got_object_open_as_commit(&commit, repo, commit_id);
3160 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3162 error = got_error(GOT_ERR_ROOT_COMMIT);
3166 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
3167 update_progress, &did_something, check_cancelled, NULL);
3172 printf("Backed out commit %s\n", commit_id_str);
3175 got_object_commit_close(commit);
3176 free(commit_id_str);
3178 got_ref_close(head_ref);
3180 got_worktree_close(worktree);
3182 got_repo_close(repo);