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/types.h>
21 #include <sys/param.h>
38 #include "got_version.h"
39 #include "got_error.h"
40 #include "got_object.h"
41 #include "got_reference.h"
42 #include "got_repository.h"
44 #include "got_worktree.h"
46 #include "got_commit_graph.h"
47 #include "got_blame.h"
48 #include "got_privsep.h"
49 #include "got_opentemp.h"
52 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
55 static volatile sig_atomic_t sigint_received;
56 static volatile sig_atomic_t sigpipe_received;
59 catch_sigint(int signo)
65 catch_sigpipe(int signo)
73 const struct got_error *(*cmd_main)(int, char *[]);
74 void (*cmd_usage)(void);
75 const char *cmd_alias;
78 __dead static void usage(int);
79 __dead static void usage_init(void);
80 __dead static void usage_import(void);
81 __dead static void usage_checkout(void);
82 __dead static void usage_update(void);
83 __dead static void usage_log(void);
84 __dead static void usage_diff(void);
85 __dead static void usage_blame(void);
86 __dead static void usage_tree(void);
87 __dead static void usage_status(void);
88 __dead static void usage_ref(void);
89 __dead static void usage_branch(void);
90 __dead static void usage_add(void);
91 __dead static void usage_remove(void);
92 __dead static void usage_revert(void);
93 __dead static void usage_commit(void);
94 __dead static void usage_cherrypick(void);
95 __dead static void usage_backout(void);
96 __dead static void usage_rebase(void);
97 __dead static void usage_histedit(void);
98 __dead static void usage_stage(void);
99 __dead static void usage_unstage(void);
101 static const struct got_error* cmd_init(int, char *[]);
102 static const struct got_error* cmd_import(int, char *[]);
103 static const struct got_error* cmd_checkout(int, char *[]);
104 static const struct got_error* cmd_update(int, char *[]);
105 static const struct got_error* cmd_log(int, char *[]);
106 static const struct got_error* cmd_diff(int, char *[]);
107 static const struct got_error* cmd_blame(int, char *[]);
108 static const struct got_error* cmd_tree(int, char *[]);
109 static const struct got_error* cmd_status(int, char *[]);
110 static const struct got_error* cmd_ref(int, char *[]);
111 static const struct got_error* cmd_branch(int, char *[]);
112 static const struct got_error* cmd_add(int, char *[]);
113 static const struct got_error* cmd_remove(int, char *[]);
114 static const struct got_error* cmd_revert(int, char *[]);
115 static const struct got_error* cmd_commit(int, char *[]);
116 static const struct got_error* cmd_cherrypick(int, char *[]);
117 static const struct got_error* cmd_backout(int, char *[]);
118 static const struct got_error* cmd_rebase(int, char *[]);
119 static const struct got_error* cmd_histedit(int, char *[]);
120 static const struct got_error* cmd_stage(int, char *[]);
121 static const struct got_error* cmd_unstage(int, char *[]);
123 static struct got_cmd got_commands[] = {
124 { "init", cmd_init, usage_init, "in" },
125 { "import", cmd_import, usage_import, "im" },
126 { "checkout", cmd_checkout, usage_checkout, "co" },
127 { "update", cmd_update, usage_update, "up" },
128 { "log", cmd_log, usage_log, "" },
129 { "diff", cmd_diff, usage_diff, "di" },
130 { "blame", cmd_blame, usage_blame, "bl" },
131 { "tree", cmd_tree, usage_tree, "tr" },
132 { "status", cmd_status, usage_status, "st" },
133 { "ref", cmd_ref, usage_ref, "" },
134 { "branch", cmd_branch, usage_branch, "br" },
135 { "add", cmd_add, usage_add, "" },
136 { "remove", cmd_remove, usage_remove, "rm" },
137 { "revert", cmd_revert, usage_revert, "rv" },
138 { "commit", cmd_commit, usage_commit, "ci" },
139 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
140 { "backout", cmd_backout, usage_backout, "bo" },
141 { "rebase", cmd_rebase, usage_rebase, "rb" },
142 { "histedit", cmd_histedit, usage_histedit, "he" },
143 { "stage", cmd_stage, usage_stage, "sg" },
144 { "unstage", cmd_unstage, usage_unstage, "ug" },
152 fprintf(stderr, "commands:");
153 for (i = 0; i < nitems(got_commands); i++) {
154 struct got_cmd *cmd = &got_commands[i];
155 fprintf(stderr, " %s", cmd->cmd_name);
161 main(int argc, char *argv[])
166 int hflag = 0, Vflag = 0;
168 setlocale(LC_CTYPE, "");
170 while ((ch = getopt(argc, argv, "hV")) != -1) {
189 got_version_print_str();
196 signal(SIGINT, catch_sigint);
197 signal(SIGPIPE, catch_sigpipe);
199 for (i = 0; i < nitems(got_commands); i++) {
200 const struct got_error *error;
202 cmd = &got_commands[i];
204 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
205 strcmp(cmd->cmd_alias, argv[0]) != 0)
209 got_commands[i].cmd_usage();
211 error = got_commands[i].cmd_main(argc, argv);
212 if (error && !(sigint_received || sigpipe_received)) {
213 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
220 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
228 fprintf(stderr, "usage: %s [-h] [-V] command [arg ...]\n",
235 static const struct got_error *
236 get_editor(char **abspath)
238 const struct got_error *err = NULL;
241 editor = getenv("VISUAL");
243 editor = getenv("EDITOR");
246 err = got_path_find_prog(abspath, editor);
251 if (*abspath == NULL) {
252 *abspath = strdup("/bin/ed");
253 if (*abspath == NULL)
254 return got_error_from_errno("strdup");
260 static const struct got_error *
261 apply_unveil(const char *repo_path, int repo_read_only,
262 const char *worktree_path)
264 const struct got_error *err;
267 if (unveil("gmon.out", "rwc") != 0)
268 return got_error_from_errno2("unveil", "gmon.out");
270 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
271 return got_error_from_errno2("unveil", repo_path);
273 if (worktree_path && unveil(worktree_path, "rwc") != 0)
274 return got_error_from_errno2("unveil", worktree_path);
276 if (unveil("/tmp", "rwc") != 0)
277 return got_error_from_errno2("unveil", "/tmp");
279 err = got_privsep_unveil_exec_helpers();
283 if (unveil(NULL, NULL) != 0)
284 return got_error_from_errno("unveil");
292 fprintf(stderr, "usage: %s init repository-path\n", getprogname());
296 static const struct got_error *
297 cmd_init(int argc, char *argv[])
299 const struct got_error *error = NULL;
300 char *repo_path = NULL;
303 while ((ch = getopt(argc, argv, "")) != -1) {
315 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
321 repo_path = strdup(argv[0]);
322 if (repo_path == NULL)
323 return got_error_from_errno("strdup");
325 got_path_strip_trailing_slashes(repo_path);
327 error = got_path_mkdir(repo_path);
329 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
332 error = apply_unveil(repo_path, 0, NULL);
336 error = got_repo_init(repo_path);
348 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
349 "[-r repository-path] [-I pattern] path\n", getprogname());
354 spawn_editor(const char *editor, const char *file)
357 sig_t sighup, sigint, sigquit;
360 sighup = signal(SIGHUP, SIG_IGN);
361 sigint = signal(SIGINT, SIG_IGN);
362 sigquit = signal(SIGQUIT, SIG_IGN);
364 switch (pid = fork()) {
368 execl(editor, editor, file, (char *)NULL);
372 while (waitpid(pid, &st, 0) == -1)
377 (void)signal(SIGHUP, sighup);
378 (void)signal(SIGINT, sigint);
379 (void)signal(SIGQUIT, sigquit);
381 if (!WIFEXITED(st)) {
386 return WEXITSTATUS(st);
389 static const struct got_error *
390 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
391 const char *initial_content)
393 const struct got_error *err = NULL;
397 int content_changed = 0;
402 if (stat(logmsg_path, &st) == -1)
403 return got_error_from_errno2("stat", logmsg_path);
405 if (spawn_editor(editor, logmsg_path) == -1)
406 return got_error_from_errno("failed spawning editor");
408 if (stat(logmsg_path, &st2) == -1)
409 return got_error_from_errno("stat");
411 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
412 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
413 "no changes made to commit message, aborting");
415 *logmsg = malloc(st2.st_size + 1);
417 return got_error_from_errno("malloc");
421 fp = fopen(logmsg_path, "r");
423 err = got_error_from_errno("fopen");
426 while (fgets(buf, sizeof(buf), fp) != NULL) {
427 if (!content_changed && strcmp(buf, initial_content) != 0)
429 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
430 continue; /* remove comments and leading empty lines */
431 len = strlcat(*logmsg, buf, st2.st_size);
435 while (len > 0 && (*logmsg)[len - 1] == '\n') {
436 (*logmsg)[len - 1] = '\0';
440 if (len == 0 || !content_changed)
441 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
442 "commit message cannot be empty, aborting");
451 static const struct got_error *
452 collect_import_msg(char **logmsg, const char *editor, const char *path_dir,
453 const char *branch_name)
455 char *initial_content = NULL, *logmsg_path = NULL;
456 const struct got_error *err = NULL;
459 if (asprintf(&initial_content,
460 "\n# %s to be imported to branch %s\n", path_dir,
462 return got_error_from_errno("asprintf");
464 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-importmsg");
468 dprintf(fd, initial_content);
471 err = edit_logmsg(logmsg, editor, logmsg_path, initial_content);
473 free(initial_content);
478 static const struct got_error *
479 import_progress(void *arg, const char *path)
481 printf("A %s\n", path);
485 static const struct got_error *
486 get_author(const char **author)
488 const char *got_author;
492 got_author = getenv("GOT_AUTHOR");
493 if (got_author == NULL) {
494 /* TODO: Look up user in password database? */
495 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
498 *author = got_author;
501 * Really dumb email address check; we're only doing this to
502 * avoid git's object parser breaking on commits we create.
504 while (*got_author && *got_author != '<')
506 if (*got_author != '<')
507 return got_error(GOT_ERR_COMMIT_NO_EMAIL);
508 while (*got_author && *got_author != '@')
510 if (*got_author != '@')
511 return got_error(GOT_ERR_COMMIT_NO_EMAIL);
512 while (*got_author && *got_author != '>')
514 if (*got_author != '>')
515 return got_error(GOT_ERR_COMMIT_NO_EMAIL);
520 static const struct got_error *
521 cmd_import(int argc, char *argv[])
523 const struct got_error *error = NULL;
524 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
527 const char *branch_name = "master";
528 char *refname = NULL, *id_str = NULL;
529 struct got_repository *repo = NULL;
530 struct got_reference *branch_ref = NULL, *head_ref = NULL;
531 struct got_object_id *new_commit_id = NULL;
533 struct got_pathlist_head ignores;
534 struct got_pathlist_entry *pe;
536 TAILQ_INIT(&ignores);
538 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
541 branch_name = optarg;
544 logmsg = strdup(optarg);
545 if (logmsg == NULL) {
546 error = got_error_from_errno("strdup");
551 repo_path = realpath(optarg, NULL);
552 if (repo_path == NULL) {
553 error = got_error_from_errno("realpath");
558 if (optarg[0] == '\0')
560 error = got_pathlist_insert(&pe, &ignores, optarg,
575 if (pledge("stdio rpath wpath cpath fattr flock proc exec unveil",
582 error = get_author(&author);
586 if (repo_path == NULL) {
587 repo_path = getcwd(NULL, 0);
588 if (repo_path == NULL)
589 return got_error_from_errno("getcwd");
591 got_path_strip_trailing_slashes(repo_path);
592 error = got_repo_open(&repo, repo_path);
596 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
597 error = got_error_from_errno("asprintf");
601 error = got_ref_open(&branch_ref, repo, refname, 0);
603 if (error->code != GOT_ERR_NOT_REF)
606 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
607 "import target branch already exists");
611 path_dir = realpath(argv[0], NULL);
612 if (path_dir == NULL) {
613 error = got_error_from_errno("realpath");
616 got_path_strip_trailing_slashes(path_dir);
619 * unveil(2) traverses exec(2); if an editor is used we have
620 * to apply unveil after the log message has been written.
622 if (logmsg == NULL || strlen(logmsg) == 0) {
623 error = get_editor(&editor);
626 error = collect_import_msg(&logmsg, editor, path_dir, refname);
631 if (unveil(path_dir, "r") != 0)
632 return got_error_from_errno2("unveil", path_dir);
634 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
638 error = got_repo_import(&new_commit_id, path_dir, logmsg,
639 author, &ignores, repo, import_progress, NULL);
643 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
647 error = got_ref_write(branch_ref, repo);
651 error = got_object_id_str(&id_str, new_commit_id);
655 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
657 if (error->code != GOT_ERR_NOT_REF)
660 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
665 error = got_ref_write(head_ref, repo);
670 printf("Created branch %s with commit %s\n",
671 got_ref_get_name(branch_ref), id_str);
679 got_ref_close(branch_ref);
681 got_ref_close(head_ref);
688 fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
689 "[-p prefix] repository-path [worktree-path]\n", getprogname());
693 static const struct got_error *
694 checkout_progress(void *arg, unsigned char status, const char *path)
696 char *worktree_path = arg;
698 /* Base commit bump happens silently. */
699 if (status == GOT_STATUS_BUMP_BASE)
702 while (path[0] == '/')
705 printf("%c %s/%s\n", status, worktree_path, path);
709 static const struct got_error *
710 check_cancelled(void *arg)
712 if (sigint_received || sigpipe_received)
713 return got_error(GOT_ERR_CANCELLED);
717 static const struct got_error *
718 check_linear_ancestry(struct got_object_id *commit_id,
719 struct got_object_id *base_commit_id, struct got_repository *repo)
721 const struct got_error *err = NULL;
722 struct got_object_id *yca_id;
724 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
725 commit_id, base_commit_id, repo);
730 return got_error(GOT_ERR_ANCESTRY);
733 * Require a straight line of history between the target commit
734 * and the work tree's base commit.
736 * Non-linear situations such as this require a rebase:
738 * (commit) D F (base_commit)
746 * 'got update' only handles linear cases:
747 * Update forwards in time: A (base/yca) - B - C - D (commit)
748 * Update backwards in time: D (base) - C - B - A (commit/yca)
750 if (got_object_id_cmp(commit_id, yca_id) != 0 &&
751 got_object_id_cmp(base_commit_id, yca_id) != 0)
752 return got_error(GOT_ERR_ANCESTRY);
758 static const struct got_error *
759 check_same_branch(struct got_object_id *commit_id,
760 struct got_reference *head_ref, struct got_object_id *yca_id,
761 struct got_repository *repo)
763 const struct got_error *err = NULL;
764 struct got_commit_graph *graph = NULL;
765 struct got_object_id *head_commit_id = NULL;
766 int is_same_branch = 0;
768 err = got_ref_resolve(&head_commit_id, repo, head_ref);
772 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
776 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
781 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
785 err = got_commit_graph_iter_start(graph, head_commit_id, repo);
790 struct got_object_id *id;
791 err = got_commit_graph_iter_next(&id, graph);
793 if (err->code == GOT_ERR_ITER_COMPLETED) {
796 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
798 err = got_commit_graph_fetch_commits(graph, 1,
805 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
807 if (got_object_id_cmp(id, commit_id) == 0) {
815 got_commit_graph_close(graph);
816 free(head_commit_id);
817 if (!err && !is_same_branch)
818 err = got_error(GOT_ERR_ANCESTRY);
822 static const struct got_error *
823 resolve_commit_arg(struct got_object_id **commit_id,
824 const char *commit_id_arg, struct got_repository *repo)
826 const struct got_error *err;
827 struct got_reference *ref;
828 struct got_tag_object *tag;
830 err = got_repo_object_match_tag(&tag, commit_id_arg,
831 GOT_OBJ_TYPE_COMMIT, repo);
833 *commit_id = got_object_id_dup(
834 got_object_tag_get_object_id(tag));
835 if (*commit_id == NULL)
836 err = got_error_from_errno("got_object_id_dup");
837 got_object_tag_close(tag);
839 } else if (err->code != GOT_ERR_NO_OBJ)
842 err = got_ref_open(&ref, repo, commit_id_arg, 0);
844 err = got_ref_resolve(commit_id, repo, ref);
847 if (err->code != GOT_ERR_NOT_REF)
849 err = got_repo_match_object_id_prefix(commit_id,
850 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
855 static const struct got_error *
856 cmd_checkout(int argc, char *argv[])
858 const struct got_error *error = NULL;
859 struct got_repository *repo = NULL;
860 struct got_reference *head_ref = NULL;
861 struct got_worktree *worktree = NULL;
862 char *repo_path = NULL;
863 char *worktree_path = NULL;
864 const char *path_prefix = "";
865 const char *branch_name = GOT_REF_HEAD;
866 char *commit_id_str = NULL;
867 int ch, same_path_prefix;
868 struct got_pathlist_head paths;
872 while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
875 branch_name = optarg;
878 commit_id_str = strdup(optarg);
879 if (commit_id_str == NULL)
880 return got_error_from_errno("strdup");
883 path_prefix = optarg;
895 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
896 "unveil", NULL) == -1)
900 char *cwd, *base, *dotgit;
901 repo_path = realpath(argv[0], NULL);
902 if (repo_path == NULL)
903 return got_error_from_errno2("realpath", argv[0]);
904 cwd = getcwd(NULL, 0);
906 error = got_error_from_errno("getcwd");
909 if (path_prefix[0]) {
910 base = basename(path_prefix);
912 error = got_error_from_errno2("basename",
917 base = basename(repo_path);
919 error = got_error_from_errno2("basename",
924 dotgit = strstr(base, ".git");
927 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
928 error = got_error_from_errno("asprintf");
933 } else if (argc == 2) {
934 repo_path = realpath(argv[0], NULL);
935 if (repo_path == NULL) {
936 error = got_error_from_errno2("realpath", argv[0]);
939 worktree_path = realpath(argv[1], NULL);
940 if (worktree_path == NULL) {
941 if (errno != ENOENT) {
942 error = got_error_from_errno2("realpath",
946 worktree_path = strdup(argv[1]);
947 if (worktree_path == NULL) {
948 error = got_error_from_errno("strdup");
955 got_path_strip_trailing_slashes(repo_path);
956 got_path_strip_trailing_slashes(worktree_path);
958 error = got_repo_open(&repo, repo_path);
962 /* Pre-create work tree path for unveil(2) */
963 error = got_path_mkdir(worktree_path);
965 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
966 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
968 if (!got_path_dir_is_empty(worktree_path)) {
969 error = got_error_path(worktree_path,
970 GOT_ERR_DIR_NOT_EMPTY);
975 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
979 error = got_ref_open(&head_ref, repo, branch_name, 0);
983 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
984 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
987 error = got_worktree_open(&worktree, worktree_path);
991 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
995 if (!same_path_prefix) {
996 error = got_error(GOT_ERR_PATH_PREFIX);
1000 if (commit_id_str) {
1001 struct got_object_id *commit_id;
1002 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1005 error = check_linear_ancestry(commit_id,
1006 got_worktree_get_base_commit_id(worktree), repo);
1007 if (error != NULL) {
1011 error = check_same_branch(commit_id, head_ref, NULL, repo);
1014 error = got_worktree_set_base_commit_id(worktree, repo,
1021 error = got_pathlist_append(&paths, "", NULL);
1024 error = got_worktree_checkout_files(worktree, &paths, repo,
1025 checkout_progress, worktree_path, check_cancelled, NULL);
1029 printf("Now shut up and hack\n");
1032 got_pathlist_free(&paths);
1033 free(commit_id_str);
1035 free(worktree_path);
1042 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1047 static const struct got_error *
1048 update_progress(void *arg, unsigned char status, const char *path)
1050 int *did_something = arg;
1052 if (status == GOT_STATUS_EXISTS)
1057 /* Base commit bump happens silently. */
1058 if (status == GOT_STATUS_BUMP_BASE)
1061 while (path[0] == '/')
1063 printf("%c %s\n", status, path);
1067 static const struct got_error *
1068 switch_head_ref(struct got_reference *head_ref,
1069 struct got_object_id *commit_id, struct got_worktree *worktree,
1070 struct got_repository *repo)
1072 const struct got_error *err = NULL;
1074 int ref_has_moved = 0;
1076 /* Trivial case: switching between two different references. */
1077 if (strcmp(got_ref_get_name(head_ref),
1078 got_worktree_get_head_ref_name(worktree)) != 0) {
1079 printf("Switching work tree from %s to %s\n",
1080 got_worktree_get_head_ref_name(worktree),
1081 got_ref_get_name(head_ref));
1082 return got_worktree_set_head_ref(worktree, head_ref);
1085 err = check_linear_ancestry(commit_id,
1086 got_worktree_get_base_commit_id(worktree), repo);
1088 if (err->code != GOT_ERR_ANCESTRY)
1095 /* Switching to a rebased branch with the same reference name. */
1096 err = got_object_id_str(&base_id_str,
1097 got_worktree_get_base_commit_id(worktree));
1100 printf("Reference %s now points at a different branch\n",
1101 got_worktree_get_head_ref_name(worktree));
1102 printf("Switching work tree from %s to %s\n", base_id_str,
1103 got_worktree_get_head_ref_name(worktree));
1107 static const struct got_error *
1108 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1110 const struct got_error *err;
1113 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1117 return got_error(GOT_ERR_REBASING);
1119 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1123 return got_error(GOT_ERR_HISTEDIT_BUSY);
1128 static const struct got_error *
1129 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1130 char *argv[], struct got_worktree *worktree)
1132 const struct got_error *err = NULL;
1139 return got_error_from_errno("strdup");
1140 return got_pathlist_append(paths, path, NULL);
1143 for (i = 0; i < argc; i++) {
1144 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1147 err = got_pathlist_append(paths, path, NULL);
1157 static const struct got_error *
1158 cmd_update(int argc, char *argv[])
1160 const struct got_error *error = NULL;
1161 struct got_repository *repo = NULL;
1162 struct got_worktree *worktree = NULL;
1163 char *worktree_path = NULL;
1164 struct got_object_id *commit_id = NULL;
1165 char *commit_id_str = NULL;
1166 const char *branch_name = NULL;
1167 struct got_reference *head_ref = NULL;
1168 struct got_pathlist_head paths;
1169 struct got_pathlist_entry *pe;
1170 int ch, did_something = 0;
1174 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1177 branch_name = optarg;
1180 commit_id_str = strdup(optarg);
1181 if (commit_id_str == NULL)
1182 return got_error_from_errno("strdup");
1194 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1195 "unveil", NULL) == -1)
1198 worktree_path = getcwd(NULL, 0);
1199 if (worktree_path == NULL) {
1200 error = got_error_from_errno("getcwd");
1203 error = got_worktree_open(&worktree, worktree_path);
1207 error = check_rebase_or_histedit_in_progress(worktree);
1211 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1215 error = apply_unveil(got_repo_get_path(repo), 0,
1216 got_worktree_get_root_path(worktree));
1220 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1224 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1225 got_worktree_get_head_ref_name(worktree), 0);
1228 if (commit_id_str == NULL) {
1229 error = got_ref_resolve(&commit_id, repo, head_ref);
1232 error = got_object_id_str(&commit_id_str, commit_id);
1236 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1237 free(commit_id_str);
1238 commit_id_str = NULL;
1241 error = got_object_id_str(&commit_id_str, commit_id);
1247 struct got_object_id *head_commit_id;
1248 TAILQ_FOREACH(pe, &paths, entry) {
1249 if (pe->path_len == 0)
1251 error = got_error_msg(GOT_ERR_BAD_PATH,
1252 "switching between branches requires that "
1253 "the entire work tree gets updated");
1256 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1259 error = check_linear_ancestry(commit_id, head_commit_id, repo);
1260 free(head_commit_id);
1263 error = check_same_branch(commit_id, head_ref, NULL, repo);
1266 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1270 error = check_linear_ancestry(commit_id,
1271 got_worktree_get_base_commit_id(worktree), repo);
1272 if (error != NULL) {
1273 if (error->code == GOT_ERR_ANCESTRY)
1274 error = got_error(GOT_ERR_BRANCH_MOVED);
1277 error = check_same_branch(commit_id, head_ref, NULL, repo);
1282 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1284 error = got_worktree_set_base_commit_id(worktree, repo,
1290 error = got_worktree_checkout_files(worktree, &paths, repo,
1291 update_progress, &did_something, check_cancelled, NULL);
1296 printf("Updated to commit %s\n", commit_id_str);
1298 printf("Already up-to-date\n");
1300 free(worktree_path);
1301 TAILQ_FOREACH(pe, &paths, entry)
1302 free((char *)pe->path);
1303 got_pathlist_free(&paths);
1305 free(commit_id_str);
1309 static const struct got_error *
1310 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1311 int diff_context, struct got_repository *repo)
1313 const struct got_error *err = NULL;
1314 struct got_tree_object *tree1 = NULL, *tree2;
1315 struct got_object_qid *qid;
1316 char *id_str1 = NULL, *id_str2;
1317 struct got_diff_blob_output_unidiff_arg arg;
1319 err = got_object_open_as_tree(&tree2, repo,
1320 got_object_commit_get_tree_id(commit));
1324 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1326 struct got_commit_object *pcommit;
1328 err = got_object_open_as_commit(&pcommit, repo, qid->id);
1332 err = got_object_open_as_tree(&tree1, repo,
1333 got_object_commit_get_tree_id(pcommit));
1334 got_object_commit_close(pcommit);
1338 err = got_object_id_str(&id_str1, qid->id);
1343 err = got_object_id_str(&id_str2, id);
1347 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1348 arg.diff_context = diff_context;
1349 arg.outfile = stdout;
1350 err = got_diff_tree(tree1, tree2, "", "", repo,
1351 got_diff_blob_output_unidiff, &arg, 1);
1354 got_object_tree_close(tree1);
1355 got_object_tree_close(tree2);
1362 get_datestr(time_t *time, char *datebuf)
1364 struct tm mytm, *tm;
1367 tm = gmtime_r(time, &mytm);
1370 s = asctime_r(tm, datebuf);
1373 p = strchr(s, '\n');
1379 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1381 static const struct got_error *
1382 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1383 struct got_repository *repo, int show_patch, int diff_context,
1384 struct got_reflist_head *refs)
1386 const struct got_error *err = NULL;
1387 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1389 time_t committer_time;
1390 const char *author, *committer;
1391 char *refs_str = NULL;
1392 struct got_reflist_entry *re;
1394 SIMPLEQ_FOREACH(re, refs, entry) {
1397 struct got_tag_object *tag = NULL;
1400 name = got_ref_get_name(re->ref);
1401 if (strcmp(name, GOT_REF_HEAD) == 0)
1403 if (strncmp(name, "refs/", 5) == 0)
1405 if (strncmp(name, "got/", 4) == 0)
1407 if (strncmp(name, "heads/", 6) == 0)
1409 if (strncmp(name, "remotes/", 8) == 0)
1411 if (strncmp(name, "tags/", 5) == 0) {
1412 err = got_object_open_as_tag(&tag, repo, re->id);
1414 if (err->code != GOT_ERR_OBJ_TYPE)
1416 /* Ref points at something other than a tag. */
1421 cmp = got_object_id_cmp(tag ?
1422 got_object_tag_get_object_id(tag) : re->id, id);
1424 got_object_tag_close(tag);
1428 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1430 err = got_error_from_errno("asprintf");
1436 err = got_object_id_str(&id_str, id);
1440 printf(GOT_COMMIT_SEP_STR);
1441 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1442 refs_str ? refs_str : "", refs_str ? ")" : "");
1447 printf("from: %s\n", got_object_commit_get_author(commit));
1448 committer_time = got_object_commit_get_committer_time(commit);
1449 datestr = get_datestr(&committer_time, datebuf);
1451 printf("date: %s UTC\n", datestr);
1452 author = got_object_commit_get_author(commit);
1453 committer = got_object_commit_get_committer(commit);
1454 if (strcmp(author, committer) != 0)
1455 printf("via: %s\n", committer);
1456 if (got_object_commit_get_nparents(commit) > 1) {
1457 const struct got_object_id_queue *parent_ids;
1458 struct got_object_qid *qid;
1460 parent_ids = got_object_commit_get_parent_ids(commit);
1461 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1462 err = got_object_id_str(&id_str, qid->id);
1465 printf("parent %d: %s\n", n++, id_str);
1470 err = got_object_commit_get_logmsg(&logmsg0, commit);
1476 line = strsep(&logmsg, "\n");
1478 printf(" %s\n", line);
1483 err = print_patch(commit, id, diff_context, repo);
1488 if (fflush(stdout) != 0 && err == NULL)
1489 err = got_error_from_errno("fflush");
1493 static const struct got_error *
1494 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1495 char *path, int show_patch, int diff_context, int limit,
1496 int first_parent_traversal, struct got_reflist_head *refs)
1498 const struct got_error *err;
1499 struct got_commit_graph *graph;
1501 err = got_commit_graph_open(&graph, root_id, path,
1502 first_parent_traversal, repo);
1505 err = got_commit_graph_iter_start(graph, root_id, repo);
1509 struct got_commit_object *commit;
1510 struct got_object_id *id;
1512 if (sigint_received || sigpipe_received)
1515 err = got_commit_graph_iter_next(&id, graph);
1517 if (err->code == GOT_ERR_ITER_COMPLETED) {
1521 if (err->code != GOT_ERR_ITER_NEED_MORE)
1523 err = got_commit_graph_fetch_commits(graph, 1, repo);
1532 err = got_object_open_as_commit(&commit, repo, id);
1535 err = print_commit(commit, id, repo, show_patch, diff_context,
1537 got_object_commit_close(commit);
1538 if (err || (limit && --limit == 0))
1542 got_commit_graph_close(graph);
1549 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1550 "[-r repository-path] [path]\n", getprogname());
1554 static const struct got_error *
1555 cmd_log(int argc, char *argv[])
1557 const struct got_error *error;
1558 struct got_repository *repo = NULL;
1559 struct got_worktree *worktree = NULL;
1560 struct got_commit_object *commit = NULL;
1561 struct got_object_id *id = NULL;
1562 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1563 char *start_commit = NULL;
1564 int diff_context = 3, ch;
1565 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1567 struct got_reflist_head refs;
1569 SIMPLEQ_INIT(&refs);
1572 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1578 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
1584 start_commit = optarg;
1587 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1590 err(1, "-C option %s", errstr);
1593 limit = strtonum(optarg, 1, INT_MAX, &errstr);
1595 err(1, "-l option %s", errstr);
1598 first_parent_traversal = 1;
1601 repo_path = realpath(optarg, NULL);
1602 if (repo_path == NULL)
1603 err(1, "-r option");
1604 got_path_strip_trailing_slashes(repo_path);
1615 cwd = getcwd(NULL, 0);
1617 error = got_error_from_errno("getcwd");
1621 error = got_worktree_open(&worktree, cwd);
1622 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1629 error = got_error_from_errno("strdup");
1632 } else if (argc == 1) {
1634 error = got_worktree_resolve_path(&path, worktree,
1639 path = strdup(argv[0]);
1641 error = got_error_from_errno("strdup");
1648 if (repo_path == NULL) {
1649 repo_path = worktree ?
1650 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1652 if (repo_path == NULL) {
1653 error = got_error_from_errno("strdup");
1657 error = got_repo_open(&repo, repo_path);
1661 error = apply_unveil(got_repo_get_path(repo), 1,
1662 worktree ? got_worktree_get_root_path(worktree) : NULL);
1666 if (start_commit == NULL) {
1667 struct got_reference *head_ref;
1668 error = got_ref_open(&head_ref, repo,
1669 worktree ? got_worktree_get_head_ref_name(worktree)
1673 error = got_ref_resolve(&id, repo, head_ref);
1674 got_ref_close(head_ref);
1677 error = got_object_open_as_commit(&commit, repo, id);
1679 struct got_reference *ref;
1680 error = got_ref_open(&ref, repo, start_commit, 0);
1681 if (error == NULL) {
1683 error = got_ref_resolve(&id, repo, ref);
1687 error = got_object_get_type(&obj_type, repo, id);
1690 if (obj_type == GOT_OBJ_TYPE_TAG) {
1691 struct got_tag_object *tag;
1692 error = got_object_open_as_tag(&tag, repo, id);
1695 if (got_object_tag_get_object_type(tag) !=
1696 GOT_OBJ_TYPE_COMMIT) {
1697 got_object_tag_close(tag);
1698 error = got_error(GOT_ERR_OBJ_TYPE);
1702 id = got_object_id_dup(
1703 got_object_tag_get_object_id(tag));
1705 error = got_error_from_errno(
1706 "got_object_id_dup");
1707 got_object_tag_close(tag);
1710 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1711 error = got_error(GOT_ERR_OBJ_TYPE);
1714 error = got_object_open_as_commit(&commit, repo, id);
1718 if (commit == NULL) {
1719 error = got_repo_match_object_id_prefix(&id,
1720 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1728 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1733 path = in_repo_path;
1736 error = got_ref_list(&refs, repo);
1740 error = print_commits(id, repo, path, show_patch,
1741 diff_context, limit, first_parent_traversal, &refs);
1748 got_worktree_close(worktree);
1750 const struct got_error *repo_error;
1751 repo_error = got_repo_close(repo);
1755 got_ref_list_free(&refs);
1762 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
1763 "[object1 object2 | path]\n", getprogname());
1767 struct print_diff_arg {
1768 struct got_repository *repo;
1769 struct got_worktree *worktree;
1776 static const struct got_error *
1777 print_diff(void *arg, unsigned char status, unsigned char staged_status,
1778 const char *path, struct got_object_id *blob_id,
1779 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
1781 struct print_diff_arg *a = arg;
1782 const struct got_error *err = NULL;
1783 struct got_blob_object *blob1 = NULL;
1785 char *abspath = NULL, *label1 = NULL;
1788 if (a->diff_staged) {
1789 if (staged_status != GOT_STATUS_MODIFY &&
1790 staged_status != GOT_STATUS_ADD &&
1791 staged_status != GOT_STATUS_DELETE)
1794 if (staged_status == GOT_STATUS_DELETE)
1796 if (status != GOT_STATUS_MODIFY &&
1797 status != GOT_STATUS_ADD &&
1798 status != GOT_STATUS_DELETE &&
1799 status != GOT_STATUS_CONFLICT)
1803 if (!a->header_shown) {
1804 printf("diff %s %s%s\n", a->id_str,
1805 got_worktree_get_root_path(a->worktree),
1806 a->diff_staged ? " (staged changes)" : "");
1807 a->header_shown = 1;
1810 if (a->diff_staged) {
1811 const char *label1 = NULL, *label2 = NULL;
1812 switch (staged_status) {
1813 case GOT_STATUS_MODIFY:
1817 case GOT_STATUS_ADD:
1820 case GOT_STATUS_DELETE:
1824 return got_error(GOT_ERR_FILE_STATUS);
1826 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
1827 label1, label2, a->diff_context, a->repo, stdout);
1830 if (staged_status == GOT_STATUS_ADD ||
1831 staged_status == GOT_STATUS_MODIFY) {
1833 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
1837 err = got_object_id_str(&id_str, staged_blob_id);
1840 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
1841 err = got_error_from_errno("asprintf");
1846 } else if (status != GOT_STATUS_ADD) {
1847 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1852 if (status != GOT_STATUS_DELETE) {
1853 if (asprintf(&abspath, "%s/%s",
1854 got_worktree_get_root_path(a->worktree), path) == -1) {
1855 err = got_error_from_errno("asprintf");
1859 f2 = fopen(abspath, "r");
1861 err = got_error_from_errno2("fopen", abspath);
1864 if (lstat(abspath, &sb) == -1) {
1865 err = got_error_from_errno2("lstat", abspath);
1871 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
1872 a->diff_context, stdout);
1875 got_object_blob_close(blob1);
1876 if (f2 && fclose(f2) != 0 && err == NULL)
1877 err = got_error_from_errno("fclose");
1882 static const struct got_error *
1883 match_object_id(struct got_object_id **id, char **label,
1884 const char *id_str, int obj_type, struct got_repository *repo)
1886 const struct got_error *err;
1887 struct got_tag_object *tag;
1888 struct got_reference *ref = NULL;
1893 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY, repo);
1895 *id = got_object_id_dup(got_object_tag_get_object_id(tag));
1897 err = got_error_from_errno("got_object_id_dup");
1898 if (asprintf(label, "refs/tags/%s",
1899 got_object_tag_get_name(tag)) == -1)
1900 err = got_error_from_errno("asprintf");
1901 got_object_tag_close(tag);
1903 } else if (err->code != GOT_ERR_NO_OBJ)
1906 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
1908 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1910 err = got_ref_open(&ref, repo, id_str, 0);
1913 *label = strdup(got_ref_get_name(ref));
1914 if (*label == NULL) {
1915 err = got_error_from_errno("strdup");
1918 err = got_ref_resolve(id, repo, ref);
1920 err = got_object_id_str(label, *id);
1921 if (*label == NULL) {
1922 err = got_error_from_errno("strdup");
1933 static const struct got_error *
1934 cmd_diff(int argc, char *argv[])
1936 const struct got_error *error;
1937 struct got_repository *repo = NULL;
1938 struct got_worktree *worktree = NULL;
1939 char *cwd = NULL, *repo_path = NULL;
1940 struct got_object_id *id1 = NULL, *id2 = NULL;
1941 const char *id_str1 = NULL, *id_str2 = NULL;
1942 char *label1 = NULL, *label2 = NULL;
1944 int diff_context = 3, diff_staged = 0, ch;
1949 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1954 while ((ch = getopt(argc, argv, "C:r:s")) != -1) {
1957 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1959 err(1, "-C option %s", errstr);
1962 repo_path = realpath(optarg, NULL);
1963 if (repo_path == NULL)
1964 err(1, "-r option");
1965 got_path_strip_trailing_slashes(repo_path);
1979 cwd = getcwd(NULL, 0);
1981 error = got_error_from_errno("getcwd");
1984 error = got_worktree_open(&worktree, cwd);
1985 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1988 if (worktree == NULL) {
1989 error = got_error(GOT_ERR_NOT_WORKTREE);
1994 "-r option can't be used when diffing a work tree");
1995 repo_path = strdup(got_worktree_get_repo_path(worktree));
1996 if (repo_path == NULL) {
1997 error = got_error_from_errno("strdup");
2001 error = got_worktree_resolve_path(&path, worktree,
2008 error = got_error_from_errno("strdup");
2012 } else if (argc == 2) {
2014 errx(1, "-s option can't be used when diffing "
2015 "objects in repository");
2018 if (worktree && repo_path == NULL) {
2020 strdup(got_worktree_get_repo_path(worktree));
2021 if (repo_path == NULL) {
2022 error = got_error_from_errno("strdup");
2029 if (repo_path == NULL) {
2030 repo_path = getcwd(NULL, 0);
2031 if (repo_path == NULL)
2032 return got_error_from_errno("getcwd");
2035 error = got_repo_open(&repo, repo_path);
2040 error = apply_unveil(got_repo_get_path(repo), 1,
2041 worktree ? got_worktree_get_root_path(worktree) : NULL);
2046 struct print_diff_arg arg;
2047 struct got_pathlist_head paths;
2052 error = got_object_id_str(&id_str,
2053 got_worktree_get_base_commit_id(worktree));
2057 arg.worktree = worktree;
2058 arg.diff_context = diff_context;
2059 arg.id_str = id_str;
2060 arg.header_shown = 0;
2061 arg.diff_staged = diff_staged;
2063 error = got_pathlist_append(&paths, path, NULL);
2067 error = got_worktree_status(worktree, &paths, repo, print_diff,
2068 &arg, check_cancelled, NULL);
2070 got_pathlist_free(&paths);
2074 error = match_object_id(&id1, &label1, id_str1, GOT_OBJ_TYPE_ANY, repo);
2078 error = match_object_id(&id2, &label2, id_str2, GOT_OBJ_TYPE_ANY, repo);
2082 error = got_object_get_type(&type1, repo, id1);
2086 error = got_object_get_type(&type2, repo, id2);
2090 if (type1 != type2) {
2091 error = got_error(GOT_ERR_OBJ_TYPE);
2096 case GOT_OBJ_TYPE_BLOB:
2097 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2098 diff_context, repo, stdout);
2100 case GOT_OBJ_TYPE_TREE:
2101 error = got_diff_objects_as_trees(id1, id2, "", "",
2102 diff_context, repo, stdout);
2104 case GOT_OBJ_TYPE_COMMIT:
2105 printf("diff %s %s\n", label1, label2);
2106 error = got_diff_objects_as_commits(id1, id2, diff_context,
2110 error = got_error(GOT_ERR_OBJ_TYPE);
2120 got_worktree_close(worktree);
2122 const struct got_error *repo_error;
2123 repo_error = got_repo_close(repo);
2134 "usage: %s blame [-c commit] [-r repository-path] path\n",
2139 static const struct got_error *
2140 cmd_blame(int argc, char *argv[])
2142 const struct got_error *error;
2143 struct got_repository *repo = NULL;
2144 struct got_worktree *worktree = NULL;
2145 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2146 struct got_object_id *commit_id = NULL;
2147 char *commit_id_str = NULL;
2151 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2156 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2159 commit_id_str = optarg;
2162 repo_path = realpath(optarg, NULL);
2163 if (repo_path == NULL)
2164 err(1, "-r option");
2165 got_path_strip_trailing_slashes(repo_path);
2181 cwd = getcwd(NULL, 0);
2183 error = got_error_from_errno("getcwd");
2186 if (repo_path == NULL) {
2187 error = got_worktree_open(&worktree, cwd);
2188 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2194 strdup(got_worktree_get_repo_path(worktree));
2195 if (repo_path == NULL)
2196 error = got_error_from_errno("strdup");
2200 repo_path = strdup(cwd);
2201 if (repo_path == NULL) {
2202 error = got_error_from_errno("strdup");
2208 error = got_repo_open(&repo, repo_path);
2212 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2217 const char *prefix = got_worktree_get_path_prefix(worktree);
2218 char *p, *worktree_subdir = cwd +
2219 strlen(got_worktree_get_root_path(worktree));
2220 if (asprintf(&p, "%s%s%s%s%s",
2221 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2222 worktree_subdir, worktree_subdir[0] ? "/" : "",
2224 error = got_error_from_errno("asprintf");
2227 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2230 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2235 if (commit_id_str == NULL) {
2236 struct got_reference *head_ref;
2237 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2240 error = got_ref_resolve(&commit_id, repo, head_ref);
2241 got_ref_close(head_ref);
2245 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2250 error = got_blame(in_repo_path, commit_id, repo, stdout);
2257 got_worktree_close(worktree);
2259 const struct got_error *repo_error;
2260 repo_error = got_repo_close(repo);
2271 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2277 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2278 const char *root_path)
2280 int is_root_path = (strcmp(path, root_path) == 0);
2281 const char *modestr = "";
2283 path += strlen(root_path);
2284 while (path[0] == '/')
2287 if (S_ISLNK(te->mode))
2289 else if (S_ISDIR(te->mode))
2291 else if (te->mode & S_IXUSR)
2294 printf("%s%s%s%s%s\n", id ? id : "", path,
2295 is_root_path ? "" : "/", te->name, modestr);
2298 static const struct got_error *
2299 print_tree(const char *path, struct got_object_id *commit_id,
2300 int show_ids, int recurse, const char *root_path,
2301 struct got_repository *repo)
2303 const struct got_error *err = NULL;
2304 struct got_object_id *tree_id = NULL;
2305 struct got_tree_object *tree = NULL;
2306 const struct got_tree_entries *entries;
2307 struct got_tree_entry *te;
2309 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2313 err = got_object_open_as_tree(&tree, repo, tree_id);
2316 entries = got_object_tree_get_entries(tree);
2317 te = SIMPLEQ_FIRST(&entries->head);
2321 if (sigint_received || sigpipe_received)
2326 err = got_object_id_str(&id_str, te->id);
2329 if (asprintf(&id, "%s ", id_str) == -1) {
2330 err = got_error_from_errno("asprintf");
2336 print_entry(te, id, path, root_path);
2339 if (recurse && S_ISDIR(te->mode)) {
2341 if (asprintf(&child_path, "%s%s%s", path,
2342 path[0] == '/' && path[1] == '\0' ? "" : "/",
2344 err = got_error_from_errno("asprintf");
2347 err = print_tree(child_path, commit_id, show_ids, 1,
2354 te = SIMPLEQ_NEXT(te, entry);
2358 got_object_tree_close(tree);
2363 static const struct got_error *
2364 cmd_tree(int argc, char *argv[])
2366 const struct got_error *error;
2367 struct got_repository *repo = NULL;
2368 struct got_worktree *worktree = NULL;
2370 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2371 struct got_object_id *commit_id = NULL;
2372 char *commit_id_str = NULL;
2373 int show_ids = 0, recurse = 0;
2377 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2382 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2385 commit_id_str = optarg;
2388 repo_path = realpath(optarg, NULL);
2389 if (repo_path == NULL)
2390 err(1, "-r option");
2391 got_path_strip_trailing_slashes(repo_path);
2415 cwd = getcwd(NULL, 0);
2417 error = got_error_from_errno("getcwd");
2420 if (repo_path == NULL) {
2421 error = got_worktree_open(&worktree, cwd);
2422 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2428 strdup(got_worktree_get_repo_path(worktree));
2429 if (repo_path == NULL)
2430 error = got_error_from_errno("strdup");
2434 repo_path = strdup(cwd);
2435 if (repo_path == NULL) {
2436 error = got_error_from_errno("strdup");
2442 error = got_repo_open(&repo, repo_path);
2446 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2452 char *p, *worktree_subdir = cwd +
2453 strlen(got_worktree_get_root_path(worktree));
2454 if (asprintf(&p, "%s/%s",
2455 got_worktree_get_path_prefix(worktree),
2456 worktree_subdir) == -1) {
2457 error = got_error_from_errno("asprintf");
2460 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2467 if (in_repo_path == NULL) {
2468 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2473 if (commit_id_str == NULL) {
2474 struct got_reference *head_ref;
2475 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2478 error = got_ref_resolve(&commit_id, repo, head_ref);
2479 got_ref_close(head_ref);
2483 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2488 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2489 in_repo_path, repo);
2496 got_worktree_close(worktree);
2498 const struct got_error *repo_error;
2499 repo_error = got_repo_close(repo);
2509 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
2513 static const struct got_error *
2514 print_status(void *arg, unsigned char status, unsigned char staged_status,
2515 const char *path, struct got_object_id *blob_id,
2516 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
2518 if (status == staged_status && (status == GOT_STATUS_DELETE))
2519 status = GOT_STATUS_NO_CHANGE;
2520 printf("%c%c %s\n", status, staged_status, path);
2524 static const struct got_error *
2525 cmd_status(int argc, char *argv[])
2527 const struct got_error *error = NULL;
2528 struct got_repository *repo = NULL;
2529 struct got_worktree *worktree = NULL;
2531 struct got_pathlist_head paths;
2532 struct got_pathlist_entry *pe;
2537 while ((ch = getopt(argc, argv, "")) != -1) {
2549 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2553 cwd = getcwd(NULL, 0);
2555 error = got_error_from_errno("getcwd");
2559 error = got_worktree_open(&worktree, cwd);
2563 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2567 error = apply_unveil(got_repo_get_path(repo), 1,
2568 got_worktree_get_root_path(worktree));
2572 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2576 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
2577 check_cancelled, NULL);
2579 TAILQ_FOREACH(pe, &paths, entry)
2580 free((char *)pe->path);
2581 got_pathlist_free(&paths);
2590 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
2595 static const struct got_error *
2596 list_refs(struct got_repository *repo)
2598 static const struct got_error *err = NULL;
2599 struct got_reflist_head refs;
2600 struct got_reflist_entry *re;
2602 SIMPLEQ_INIT(&refs);
2603 err = got_ref_list(&refs, repo);
2607 SIMPLEQ_FOREACH(re, &refs, entry) {
2609 refstr = got_ref_to_str(re->ref);
2611 return got_error_from_errno("got_ref_to_str");
2612 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2616 got_ref_list_free(&refs);
2620 static const struct got_error *
2621 delete_ref(struct got_repository *repo, const char *refname)
2623 const struct got_error *err = NULL;
2624 struct got_reference *ref;
2626 err = got_ref_open(&ref, repo, refname, 0);
2630 err = got_ref_delete(ref, repo);
2635 static const struct got_error *
2636 add_ref(struct got_repository *repo, const char *refname, const char *target)
2638 const struct got_error *err = NULL;
2639 struct got_object_id *id;
2640 struct got_reference *ref = NULL;
2643 * Don't let the user create a reference named '-'.
2644 * While technically a valid reference name, this case is usually
2645 * an unintended typo.
2647 if (refname[0] == '-' && refname[1] == '\0')
2648 return got_error(GOT_ERR_BAD_REF_NAME);
2650 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2653 struct got_reference *target_ref;
2655 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2657 err = got_ref_open(&target_ref, repo, target, 0);
2660 err = got_ref_resolve(&id, repo, target_ref);
2661 got_ref_close(target_ref);
2666 err = got_ref_alloc(&ref, refname, id);
2670 err = got_ref_write(ref, repo);
2678 static const struct got_error *
2679 add_symref(struct got_repository *repo, const char *refname, const char *target)
2681 const struct got_error *err = NULL;
2682 struct got_reference *ref = NULL;
2683 struct got_reference *target_ref = NULL;
2686 * Don't let the user create a reference named '-'.
2687 * While technically a valid reference name, this case is usually
2688 * an unintended typo.
2690 if (refname[0] == '-' && refname[1] == '\0')
2691 return got_error(GOT_ERR_BAD_REF_NAME);
2693 err = got_ref_open(&target_ref, repo, target, 0);
2697 err = got_ref_alloc_symref(&ref, refname, target_ref);
2701 err = got_ref_write(ref, repo);
2704 got_ref_close(target_ref);
2710 static const struct got_error *
2711 cmd_ref(int argc, char *argv[])
2713 const struct got_error *error = NULL;
2714 struct got_repository *repo = NULL;
2715 struct got_worktree *worktree = NULL;
2716 char *cwd = NULL, *repo_path = NULL;
2717 int ch, do_list = 0, create_symref = 0;
2718 const char *delref = NULL;
2720 /* TODO: Add -s option for adding symbolic references. */
2721 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
2727 repo_path = realpath(optarg, NULL);
2728 if (repo_path == NULL)
2729 err(1, "-r option");
2730 got_path_strip_trailing_slashes(repo_path);
2744 if (do_list && delref)
2745 errx(1, "-l and -d options are mutually exclusive\n");
2750 if (do_list || delref) {
2752 errx(1, "-s option cannot be used together with the "
2753 "-l or -d options");
2756 } else if (argc != 2)
2761 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2765 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2766 "sendfd unveil", NULL) == -1)
2770 cwd = getcwd(NULL, 0);
2772 error = got_error_from_errno("getcwd");
2776 if (repo_path == NULL) {
2777 error = got_worktree_open(&worktree, cwd);
2778 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2784 strdup(got_worktree_get_repo_path(worktree));
2785 if (repo_path == NULL)
2786 error = got_error_from_errno("strdup");
2790 repo_path = strdup(cwd);
2791 if (repo_path == NULL) {
2792 error = got_error_from_errno("strdup");
2798 error = got_repo_open(&repo, repo_path);
2802 error = apply_unveil(got_repo_get_path(repo), do_list,
2803 worktree ? got_worktree_get_root_path(worktree) : NULL);
2808 error = list_refs(repo);
2810 error = delete_ref(repo, delref);
2811 else if (create_symref)
2812 error = add_symref(repo, argv[0], argv[1]);
2814 error = add_ref(repo, argv[0], argv[1]);
2817 got_repo_close(repo);
2819 got_worktree_close(worktree);
2829 "usage: %s branch [-r repository] -l | -d name | "
2830 "name [base-branch]\n", getprogname());
2834 static const struct got_error *
2835 list_branches(struct got_repository *repo, struct got_worktree *worktree)
2837 static const struct got_error *err = NULL;
2838 struct got_reflist_head refs;
2839 struct got_reflist_entry *re;
2841 SIMPLEQ_INIT(&refs);
2843 err = got_ref_list(&refs, repo);
2847 SIMPLEQ_FOREACH(re, &refs, entry) {
2848 const char *refname, *marker = " ";
2850 refname = got_ref_get_name(re->ref);
2851 if (strncmp(refname, "refs/heads/", 11) != 0)
2853 if (worktree && strcmp(refname,
2854 got_worktree_get_head_ref_name(worktree)) == 0) {
2855 struct got_object_id *id = NULL;
2856 err = got_ref_resolve(&id, repo, re->ref);
2859 if (got_object_id_cmp(id,
2860 got_worktree_get_base_commit_id(worktree)) == 0)
2867 refstr = got_ref_to_str(re->ref);
2869 return got_error_from_errno("got_ref_to_str");
2870 printf("%s%s: %s\n", marker, refname, refstr);
2874 got_ref_list_free(&refs);
2878 static const struct got_error *
2879 delete_branch(struct got_repository *repo, const char *branch_name)
2881 const struct got_error *err = NULL;
2882 struct got_reference *ref;
2885 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
2886 return got_error_from_errno("asprintf");
2888 err = got_ref_open(&ref, repo, refname, 0);
2892 err = got_ref_delete(ref, repo);
2899 static const struct got_error *
2900 add_branch(struct got_repository *repo, const char *branch_name,
2901 const char *base_branch)
2903 const struct got_error *err = NULL;
2904 struct got_object_id *id = NULL;
2905 struct got_reference *ref = NULL;
2906 char *base_refname = NULL, *refname = NULL;
2907 struct got_reference *base_ref;
2910 * Don't let the user create a branch named '-'.
2911 * While technically a valid reference name, this case is usually
2912 * an unintended typo.
2914 if (branch_name[0] == '-' && branch_name[1] == '\0')
2915 return got_error(GOT_ERR_BAD_REF_NAME);
2917 if (strcmp(GOT_REF_HEAD, base_branch) == 0) {
2918 base_refname = strdup(GOT_REF_HEAD);
2919 if (base_refname == NULL)
2920 return got_error_from_errno("strdup");
2921 } else if (asprintf(&base_refname, "refs/heads/%s", base_branch) == -1)
2922 return got_error_from_errno("asprintf");
2924 err = got_ref_open(&base_ref, repo, base_refname, 0);
2927 err = got_ref_resolve(&id, repo, base_ref);
2928 got_ref_close(base_ref);
2932 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
2933 err = got_error_from_errno("asprintf");
2937 err = got_ref_open(&ref, repo, refname, 0);
2939 err = got_error(GOT_ERR_BRANCH_EXISTS);
2941 } else if (err->code != GOT_ERR_NOT_REF)
2944 err = got_ref_alloc(&ref, refname, id);
2948 err = got_ref_write(ref, repo);
2958 static const struct got_error *
2959 cmd_branch(int argc, char *argv[])
2961 const struct got_error *error = NULL;
2962 struct got_repository *repo = NULL;
2963 struct got_worktree *worktree = NULL;
2964 char *cwd = NULL, *repo_path = NULL;
2965 int ch, do_list = 0;
2966 const char *delref = NULL;
2968 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2974 repo_path = realpath(optarg, NULL);
2975 if (repo_path == NULL)
2976 err(1, "-r option");
2977 got_path_strip_trailing_slashes(repo_path);
2988 if (do_list && delref)
2989 errx(1, "-l and -d options are mutually exclusive\n");
2994 if (do_list || delref) {
2997 } else if (argc < 1 || argc > 2)
3002 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3006 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3007 "sendfd unveil", NULL) == -1)
3011 cwd = getcwd(NULL, 0);
3013 error = got_error_from_errno("getcwd");
3017 if (repo_path == NULL) {
3018 error = got_worktree_open(&worktree, cwd);
3019 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3025 strdup(got_worktree_get_repo_path(worktree));
3026 if (repo_path == NULL)
3027 error = got_error_from_errno("strdup");
3031 repo_path = strdup(cwd);
3032 if (repo_path == NULL) {
3033 error = got_error_from_errno("strdup");
3039 error = got_repo_open(&repo, repo_path);
3043 error = apply_unveil(got_repo_get_path(repo), do_list,
3044 worktree ? got_worktree_get_root_path(worktree) : NULL);
3049 error = list_branches(repo, worktree);
3051 error = delete_branch(repo, delref);
3053 const char *base_branch;
3055 base_branch = worktree ?
3056 got_worktree_get_head_ref_name(worktree) :
3058 if (strncmp(base_branch, "refs/heads/", 11) == 0)
3061 base_branch = argv[1];
3062 error = add_branch(repo, argv[0], base_branch);
3066 got_repo_close(repo);
3068 got_worktree_close(worktree);
3077 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
3081 static const struct got_error *
3082 cmd_add(int argc, char *argv[])
3084 const struct got_error *error = NULL;
3085 struct got_repository *repo = NULL;
3086 struct got_worktree *worktree = NULL;
3088 struct got_pathlist_head paths;
3089 struct got_pathlist_entry *pe;
3094 while ((ch = getopt(argc, argv, "")) != -1) {
3106 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3113 cwd = getcwd(NULL, 0);
3115 error = got_error_from_errno("getcwd");
3119 error = got_worktree_open(&worktree, cwd);
3123 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3127 error = apply_unveil(got_repo_get_path(repo), 1,
3128 got_worktree_get_root_path(worktree));
3132 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3136 error = got_worktree_schedule_add(worktree, &paths, print_status,
3140 got_repo_close(repo);
3142 got_worktree_close(worktree);
3143 TAILQ_FOREACH(pe, &paths, entry)
3144 free((char *)pe->path);
3145 got_pathlist_free(&paths);
3153 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
3157 static const struct got_error *
3158 cmd_remove(int argc, char *argv[])
3160 const struct got_error *error = NULL;
3161 struct got_worktree *worktree = NULL;
3162 struct got_repository *repo = NULL;
3164 struct got_pathlist_head paths;
3165 struct got_pathlist_entry *pe;
3166 int ch, delete_local_mods = 0;
3170 while ((ch = getopt(argc, argv, "f")) != -1) {
3173 delete_local_mods = 1;
3185 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3192 cwd = getcwd(NULL, 0);
3194 error = got_error_from_errno("getcwd");
3197 error = got_worktree_open(&worktree, cwd);
3201 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3205 error = apply_unveil(got_repo_get_path(repo), 1,
3206 got_worktree_get_root_path(worktree));
3210 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3214 error = got_worktree_schedule_delete(worktree, &paths,
3215 delete_local_mods, print_status, NULL, repo);
3220 got_repo_close(repo);
3222 got_worktree_close(worktree);
3223 TAILQ_FOREACH(pe, &paths, entry)
3224 free((char *)pe->path);
3225 got_pathlist_free(&paths);
3233 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
3234 "path ...\n", getprogname());
3238 static const struct got_error *
3239 revert_progress(void *arg, unsigned char status, const char *path)
3241 while (path[0] == '/')
3243 printf("%c %s\n", status, path);
3247 struct choose_patch_arg {
3248 FILE *patch_script_file;
3252 static const struct got_error *
3253 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
3254 int nchanges, const char *action)
3257 size_t linesize = 0;
3261 case GOT_STATUS_ADD:
3262 printf("A %s\n%s this addition? [y/n] ", path, action);
3264 case GOT_STATUS_DELETE:
3265 printf("D %s\n%s this deletion? [y/n] ", path, action);
3267 case GOT_STATUS_MODIFY:
3268 if (fseek(patch_file, 0L, SEEK_SET) == -1)
3269 return got_error_from_errno("fseek");
3270 printf(GOT_COMMIT_SEP_STR);
3271 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
3273 if (ferror(patch_file))
3274 return got_error_from_errno("getline");
3275 printf(GOT_COMMIT_SEP_STR);
3276 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
3277 path, n, nchanges, action);
3280 return got_error_path(path, GOT_ERR_FILE_STATUS);
3286 static const struct got_error *
3287 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
3288 FILE *patch_file, int n, int nchanges)
3290 const struct got_error *err = NULL;
3292 size_t linesize = 0;
3295 struct choose_patch_arg *a = arg;
3297 *choice = GOT_PATCH_CHOICE_NONE;
3299 if (a->patch_script_file) {
3301 err = show_change(status, path, patch_file, n, nchanges,
3305 linelen = getline(&line, &linesize, a->patch_script_file);
3306 if (linelen == -1) {
3307 if (ferror(a->patch_script_file))
3308 return got_error_from_errno("getline");
3311 nl = strchr(line, '\n');
3314 if (strcmp(line, "y") == 0) {
3315 *choice = GOT_PATCH_CHOICE_YES;
3317 } else if (strcmp(line, "n") == 0) {
3318 *choice = GOT_PATCH_CHOICE_NO;
3320 } else if (strcmp(line, "q") == 0 &&
3321 status == GOT_STATUS_MODIFY) {
3322 *choice = GOT_PATCH_CHOICE_QUIT;
3325 printf("invalid response '%s'\n", line);
3330 while (resp != 'y' && resp != 'n' && resp != 'q') {
3331 err = show_change(status, path, patch_file, n, nchanges,
3338 if (status == GOT_STATUS_MODIFY) {
3339 if (resp != 'y' && resp != 'n' && resp != 'q') {
3340 printf("invalid response '%c'\n", resp);
3343 } else if (resp != 'y' && resp != 'n') {
3344 printf("invalid response '%c'\n", resp);
3350 *choice = GOT_PATCH_CHOICE_YES;
3351 else if (resp == 'n')
3352 *choice = GOT_PATCH_CHOICE_NO;
3353 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
3354 *choice = GOT_PATCH_CHOICE_QUIT;
3360 static const struct got_error *
3361 cmd_revert(int argc, char *argv[])
3363 const struct got_error *error = NULL;
3364 struct got_worktree *worktree = NULL;
3365 struct got_repository *repo = NULL;
3366 char *cwd = NULL, *path = NULL;
3367 struct got_pathlist_head paths;
3368 struct got_pathlist_entry *pe;
3369 int ch, can_recurse = 0, pflag = 0;
3370 FILE *patch_script_file = NULL;
3371 const char *patch_script_path = NULL;
3372 struct choose_patch_arg cpa;
3376 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
3382 patch_script_path = optarg;
3397 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3398 "unveil", NULL) == -1)
3403 if (patch_script_path && !pflag)
3404 errx(1, "-F option can only be used together with -p option");
3406 cwd = getcwd(NULL, 0);
3408 error = got_error_from_errno("getcwd");
3411 error = got_worktree_open(&worktree, cwd);
3415 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3419 if (patch_script_path) {
3420 patch_script_file = fopen(patch_script_path, "r");
3421 if (patch_script_file == NULL) {
3422 error = got_error_from_errno2("fopen",
3427 error = apply_unveil(got_repo_get_path(repo), 1,
3428 got_worktree_get_root_path(worktree));
3432 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3439 TAILQ_FOREACH(pe, &paths, entry) {
3440 if (asprintf(&ondisk_path, "%s/%s",
3441 got_worktree_get_root_path(worktree),
3443 error = got_error_from_errno("asprintf");
3446 if (lstat(ondisk_path, &sb) == -1) {
3447 if (errno == ENOENT) {
3451 error = got_error_from_errno2("lstat",
3457 if (S_ISDIR(sb.st_mode)) {
3458 error = got_error_msg(GOT_ERR_BAD_PATH,
3459 "reverting directories requires -R option");
3465 cpa.patch_script_file = patch_script_file;
3466 cpa.action = "revert";
3467 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
3468 pflag ? choose_patch : NULL, &cpa, repo);
3472 if (patch_script_file && fclose(patch_script_file) == EOF &&
3474 error = got_error_from_errno2("fclose", patch_script_path);
3476 got_repo_close(repo);
3478 got_worktree_close(worktree);
3487 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
3492 struct collect_commit_logmsg_arg {
3493 const char *cmdline_log;
3495 const char *worktree_path;
3496 const char *branch_name;
3497 const char *repo_path;
3502 static const struct got_error *
3503 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
3506 char *initial_content = NULL;
3507 struct got_pathlist_entry *pe;
3508 const struct got_error *err = NULL;
3509 char *template = NULL;
3510 struct collect_commit_logmsg_arg *a = arg;
3514 /* if a message was specified on the command line, just use it */
3515 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
3516 len = strlen(a->cmdline_log) + 1;
3517 *logmsg = malloc(len + 1);
3518 if (*logmsg == NULL)
3519 return got_error_from_errno("malloc");
3520 strlcpy(*logmsg, a->cmdline_log, len);
3524 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
3525 return got_error_from_errno("asprintf");
3527 if (asprintf(&initial_content,
3528 "\n# changes to be committed on branch %s:\n",
3529 a->branch_name) == -1)
3530 return got_error_from_errno("asprintf");
3532 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
3536 dprintf(fd, initial_content);
3538 TAILQ_FOREACH(pe, commitable_paths, entry) {
3539 struct got_commitable *ct = pe->data;
3540 dprintf(fd, "# %c %s\n",
3541 got_commitable_get_status(ct),
3542 got_commitable_get_path(ct));
3546 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
3548 if (err == NULL || err->code == GOT_ERR_COMMIT_MSG_EMPTY) {
3549 unlink(a->logmsg_path);
3550 free(a->logmsg_path);
3551 a->logmsg_path = NULL;
3553 free(initial_content);
3556 /* Editor is done; we can now apply unveil(2) */
3558 err = apply_unveil(a->repo_path, 0, a->worktree_path);
3567 static const struct got_error *
3568 cmd_commit(int argc, char *argv[])
3570 const struct got_error *error = NULL;
3571 struct got_worktree *worktree = NULL;
3572 struct got_repository *repo = NULL;
3573 char *cwd = NULL, *id_str = NULL;
3574 struct got_object_id *id = NULL;
3575 const char *logmsg = NULL;
3577 struct collect_commit_logmsg_arg cl_arg;
3578 char *editor = NULL;
3579 int ch, rebase_in_progress, histedit_in_progress;
3580 struct got_pathlist_head paths;
3583 cl_arg.logmsg_path = NULL;
3585 while ((ch = getopt(argc, argv, "m:")) != -1) {
3600 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3601 "unveil", NULL) == -1)
3604 error = get_author(&author);
3608 cwd = getcwd(NULL, 0);
3610 error = got_error_from_errno("getcwd");
3613 error = got_worktree_open(&worktree, cwd);
3617 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3620 if (rebase_in_progress) {
3621 error = got_error(GOT_ERR_REBASING);
3625 error = got_worktree_histedit_in_progress(&histedit_in_progress,
3630 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3635 * unveil(2) traverses exec(2); if an editor is used we have
3636 * to apply unveil after the log message has been written.
3638 if (logmsg == NULL || strlen(logmsg) == 0)
3639 error = get_editor(&editor);
3641 error = apply_unveil(got_repo_get_path(repo), 0,
3642 got_worktree_get_root_path(worktree));
3646 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3650 cl_arg.editor = editor;
3651 cl_arg.cmdline_log = logmsg;
3652 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
3653 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
3654 if (!histedit_in_progress) {
3655 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
3656 error = got_error(GOT_ERR_COMMIT_BRANCH);
3659 cl_arg.branch_name += 11;
3661 cl_arg.repo_path = got_repo_get_path(repo);
3662 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
3663 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
3665 if (cl_arg.logmsg_path)
3666 fprintf(stderr, "%s: log message preserved in %s\n",
3667 getprogname(), cl_arg.logmsg_path);
3671 if (cl_arg.logmsg_path)
3672 unlink(cl_arg.logmsg_path);
3674 error = got_object_id_str(&id_str, id);
3677 printf("Created commit %s\n", id_str);
3679 free(cl_arg.logmsg_path);
3681 got_repo_close(repo);
3683 got_worktree_close(worktree);
3691 usage_cherrypick(void)
3693 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
3697 static const struct got_error *
3698 cmd_cherrypick(int argc, char *argv[])
3700 const struct got_error *error = NULL;
3701 struct got_worktree *worktree = NULL;
3702 struct got_repository *repo = NULL;
3703 char *cwd = NULL, *commit_id_str = NULL;
3704 struct got_object_id *commit_id = NULL;
3705 struct got_commit_object *commit = NULL;
3706 struct got_object_qid *pid;
3707 struct got_reference *head_ref = NULL;
3708 int ch, did_something = 0;
3710 while ((ch = getopt(argc, argv, "")) != -1) {
3722 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3723 "unveil", NULL) == -1)
3729 cwd = getcwd(NULL, 0);
3731 error = got_error_from_errno("getcwd");
3734 error = got_worktree_open(&worktree, cwd);
3738 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3742 error = apply_unveil(got_repo_get_path(repo), 0,
3743 got_worktree_get_root_path(worktree));
3747 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3748 GOT_OBJ_TYPE_COMMIT, repo);
3749 if (error != NULL) {
3750 struct got_reference *ref;
3751 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3753 error = got_ref_open(&ref, repo, argv[0], 0);
3756 error = got_ref_resolve(&commit_id, repo, ref);
3761 error = got_object_id_str(&commit_id_str, commit_id);
3765 error = got_ref_open(&head_ref, repo,
3766 got_worktree_get_head_ref_name(worktree), 0);
3770 error = check_same_branch(commit_id, head_ref, NULL, repo);
3772 if (error->code != GOT_ERR_ANCESTRY)
3776 error = got_error(GOT_ERR_SAME_BRANCH);
3780 error = got_object_open_as_commit(&commit, repo, commit_id);
3783 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3784 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
3785 commit_id, repo, update_progress, &did_something, check_cancelled,
3791 printf("Merged commit %s\n", commit_id_str);
3794 got_object_commit_close(commit);
3795 free(commit_id_str);
3797 got_ref_close(head_ref);
3799 got_worktree_close(worktree);
3801 got_repo_close(repo);
3808 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
3812 static const struct got_error *
3813 cmd_backout(int argc, char *argv[])
3815 const struct got_error *error = NULL;
3816 struct got_worktree *worktree = NULL;
3817 struct got_repository *repo = NULL;
3818 char *cwd = NULL, *commit_id_str = NULL;
3819 struct got_object_id *commit_id = NULL;
3820 struct got_commit_object *commit = NULL;
3821 struct got_object_qid *pid;
3822 struct got_reference *head_ref = NULL;
3823 int ch, did_something = 0;
3825 while ((ch = getopt(argc, argv, "")) != -1) {
3837 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3838 "unveil", NULL) == -1)
3844 cwd = getcwd(NULL, 0);
3846 error = got_error_from_errno("getcwd");
3849 error = got_worktree_open(&worktree, cwd);
3853 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3857 error = apply_unveil(got_repo_get_path(repo), 0,
3858 got_worktree_get_root_path(worktree));
3862 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3863 GOT_OBJ_TYPE_COMMIT, repo);
3864 if (error != NULL) {
3865 struct got_reference *ref;
3866 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3868 error = got_ref_open(&ref, repo, argv[0], 0);
3871 error = got_ref_resolve(&commit_id, repo, ref);
3876 error = got_object_id_str(&commit_id_str, commit_id);
3880 error = got_ref_open(&head_ref, repo,
3881 got_worktree_get_head_ref_name(worktree), 0);
3885 error = check_same_branch(commit_id, head_ref, NULL, repo);
3889 error = got_object_open_as_commit(&commit, repo, commit_id);
3892 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3894 error = got_error(GOT_ERR_ROOT_COMMIT);
3898 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
3899 update_progress, &did_something, check_cancelled, NULL);
3904 printf("Backed out commit %s\n", commit_id_str);
3907 got_object_commit_close(commit);
3908 free(commit_id_str);
3910 got_ref_close(head_ref);
3912 got_worktree_close(worktree);
3914 got_repo_close(repo);
3921 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
3927 trim_logmsg(char *logmsg, int limit)
3932 len = strlen(logmsg);
3936 nl = strchr(logmsg, '\n');
3941 static const struct got_error *
3942 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
3944 const struct got_error *err;
3945 char *logmsg0 = NULL;
3948 err = got_object_commit_get_logmsg(&logmsg0, commit);
3953 while (isspace((unsigned char)s[0]))
3956 *logmsg = strdup(s);
3957 if (*logmsg == NULL) {
3958 err = got_error_from_errno("strdup");
3962 trim_logmsg(*logmsg, limit);
3968 static const struct got_error *
3969 show_rebase_progress(struct got_commit_object *commit,
3970 struct got_object_id *old_id, struct got_object_id *new_id)
3972 const struct got_error *err;
3973 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
3975 err = got_object_id_str(&old_id_str, old_id);
3980 err = got_object_id_str(&new_id_str, new_id);
3985 old_id_str[12] = '\0';
3987 new_id_str[12] = '\0';
3989 err = get_short_logmsg(&logmsg, 42, commit);
3993 printf("%s -> %s: %s\n", old_id_str,
3994 new_id_str ? new_id_str : "no-op change", logmsg);
4001 static const struct got_error *
4002 rebase_progress(void *arg, unsigned char status, const char *path)
4004 unsigned char *rebase_status = arg;
4006 while (path[0] == '/')
4008 printf("%c %s\n", status, path);
4010 if (*rebase_status == GOT_STATUS_CONFLICT)
4012 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
4013 *rebase_status = status;
4017 static const struct got_error *
4018 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
4019 struct got_reference *branch, struct got_reference *new_base_branch,
4020 struct got_reference *tmp_branch, struct got_repository *repo)
4022 printf("Switching work tree to %s\n", got_ref_get_name(branch));
4023 return got_worktree_rebase_complete(worktree, fileindex,
4024 new_base_branch, tmp_branch, branch, repo);
4027 static const struct got_error *
4028 rebase_commit(struct got_pathlist_head *merged_paths,
4029 struct got_worktree *worktree, struct got_fileindex *fileindex,
4030 struct got_reference *tmp_branch,
4031 struct got_object_id *commit_id, struct got_repository *repo)
4033 const struct got_error *error;
4034 struct got_commit_object *commit;
4035 struct got_object_id *new_commit_id;
4037 error = got_object_open_as_commit(&commit, repo, commit_id);
4041 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
4042 worktree, fileindex, tmp_branch, commit, commit_id, repo);
4044 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
4046 error = show_rebase_progress(commit, commit_id, NULL);
4048 error = show_rebase_progress(commit, commit_id, new_commit_id);
4049 free(new_commit_id);
4052 got_object_commit_close(commit);
4056 struct check_path_prefix_arg {
4057 const char *path_prefix;
4062 static const struct got_error *
4063 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
4064 struct got_blob_object *blob2, struct got_object_id *id1,
4065 struct got_object_id *id2, const char *path1, const char *path2,
4066 struct got_repository *repo)
4068 struct check_path_prefix_arg *a = arg;
4070 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
4071 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
4072 return got_error(a->errcode);
4077 static const struct got_error *
4078 check_path_prefix(struct got_object_id *parent_id,
4079 struct got_object_id *commit_id, const char *path_prefix,
4080 int errcode, struct got_repository *repo)
4082 const struct got_error *err;
4083 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4084 struct got_commit_object *commit = NULL, *parent_commit = NULL;
4085 struct check_path_prefix_arg cpp_arg;
4087 if (got_path_is_root_dir(path_prefix))
4090 err = got_object_open_as_commit(&commit, repo, commit_id);
4094 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
4098 err = got_object_open_as_tree(&tree1, repo,
4099 got_object_commit_get_tree_id(parent_commit));
4103 err = got_object_open_as_tree(&tree2, repo,
4104 got_object_commit_get_tree_id(commit));
4108 cpp_arg.path_prefix = path_prefix;
4109 while (cpp_arg.path_prefix[0] == '/')
4110 cpp_arg.path_prefix++;
4111 cpp_arg.len = strlen(cpp_arg.path_prefix);
4112 cpp_arg.errcode = errcode;
4113 err = got_diff_tree(tree1, tree2, "", "", repo,
4114 check_path_prefix_in_diff, &cpp_arg, 0);
4117 got_object_tree_close(tree1);
4119 got_object_tree_close(tree2);
4121 got_object_commit_close(commit);
4123 got_object_commit_close(parent_commit);
4127 static const struct got_error *
4128 collect_commits(struct got_object_id_queue *commits,
4129 struct got_object_id *initial_commit_id,
4130 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
4131 const char *path_prefix, int path_prefix_errcode,
4132 struct got_repository *repo)
4134 const struct got_error *err = NULL;
4135 struct got_commit_graph *graph = NULL;
4136 struct got_object_id *parent_id = NULL;
4137 struct got_object_qid *qid;
4138 struct got_object_id *commit_id = initial_commit_id;
4140 err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
4144 err = got_commit_graph_iter_start(graph, iter_start_id, repo);
4147 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
4148 err = got_commit_graph_iter_next(&parent_id, graph);
4150 if (err->code == GOT_ERR_ITER_COMPLETED) {
4151 err = got_error_msg(GOT_ERR_ANCESTRY,
4152 "ran out of commits to rebase before "
4153 "youngest common ancestor commit has "
4156 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
4158 err = got_commit_graph_fetch_commits(graph, 1, repo);
4162 err = check_path_prefix(parent_id, commit_id,
4163 path_prefix, path_prefix_errcode, repo);
4167 err = got_object_qid_alloc(&qid, commit_id);
4170 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
4171 commit_id = parent_id;
4175 got_commit_graph_close(graph);
4179 static const struct got_error *
4180 cmd_rebase(int argc, char *argv[])
4182 const struct got_error *error = NULL;
4183 struct got_worktree *worktree = NULL;
4184 struct got_repository *repo = NULL;
4185 struct got_fileindex *fileindex = NULL;
4187 struct got_reference *branch = NULL;
4188 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
4189 struct got_object_id *commit_id = NULL, *parent_id = NULL;
4190 struct got_object_id *resume_commit_id = NULL;
4191 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
4192 struct got_commit_object *commit = NULL;
4193 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
4194 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
4195 struct got_object_id_queue commits;
4196 struct got_pathlist_head merged_paths;
4197 const struct got_object_id_queue *parent_ids;
4198 struct got_object_qid *qid, *pid;
4200 SIMPLEQ_INIT(&commits);
4201 TAILQ_INIT(&merged_paths);
4203 while ((ch = getopt(argc, argv, "ac")) != -1) {
4209 continue_rebase = 1;
4221 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4222 "unveil", NULL) == -1)
4225 if (abort_rebase && continue_rebase)
4227 else if (abort_rebase || continue_rebase) {
4230 } else if (argc != 1)
4233 cwd = getcwd(NULL, 0);
4235 error = got_error_from_errno("getcwd");
4238 error = got_worktree_open(&worktree, cwd);
4242 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4246 error = apply_unveil(got_repo_get_path(repo), 0,
4247 got_worktree_get_root_path(worktree));
4251 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4257 if (!rebase_in_progress) {
4258 error = got_error(GOT_ERR_NOT_REBASING);
4261 error = got_worktree_rebase_continue(&resume_commit_id,
4262 &new_base_branch, &tmp_branch, &branch, &fileindex,
4266 printf("Switching work tree to %s\n",
4267 got_ref_get_symref_target(new_base_branch));
4268 error = got_worktree_rebase_abort(worktree, fileindex, repo,
4269 new_base_branch, update_progress, &did_something);
4272 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
4273 goto done; /* nothing else to do */
4276 if (continue_rebase) {
4277 if (!rebase_in_progress) {
4278 error = got_error(GOT_ERR_NOT_REBASING);
4281 error = got_worktree_rebase_continue(&resume_commit_id,
4282 &new_base_branch, &tmp_branch, &branch, &fileindex,
4287 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
4288 resume_commit_id, repo);
4292 yca_id = got_object_id_dup(resume_commit_id);
4293 if (yca_id == NULL) {
4294 error = got_error_from_errno("got_object_id_dup");
4298 error = got_ref_open(&branch, repo, argv[0], 0);
4303 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
4307 if (!continue_rebase) {
4308 struct got_object_id *base_commit_id;
4310 base_commit_id = got_worktree_get_base_commit_id(worktree);
4311 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
4312 base_commit_id, branch_head_commit_id, repo);
4315 if (yca_id == NULL) {
4316 error = got_error_msg(GOT_ERR_ANCESTRY,
4317 "specified branch shares no common ancestry "
4318 "with work tree's branch");
4322 error = check_same_branch(base_commit_id, branch, yca_id, repo);
4324 if (error->code != GOT_ERR_ANCESTRY)
4328 error = got_error_msg(GOT_ERR_SAME_BRANCH,
4329 "specified branch resolves to a commit which "
4330 "is already contained in work tree's branch");
4333 error = got_worktree_rebase_prepare(&new_base_branch,
4334 &tmp_branch, &fileindex, worktree, branch, repo);
4339 commit_id = branch_head_commit_id;
4340 error = got_object_open_as_commit(&commit, repo, commit_id);
4344 parent_ids = got_object_commit_get_parent_ids(commit);
4345 pid = SIMPLEQ_FIRST(parent_ids);
4347 if (!continue_rebase) {
4349 error = got_worktree_rebase_abort(worktree, fileindex,
4350 repo, new_base_branch, update_progress,
4354 printf("Rebase of %s aborted\n",
4355 got_ref_get_name(branch));
4357 error = got_error(GOT_ERR_EMPTY_REBASE);
4360 error = collect_commits(&commits, commit_id, pid->id,
4361 yca_id, got_worktree_get_path_prefix(worktree),
4362 GOT_ERR_REBASE_PATH, repo);
4363 got_object_commit_close(commit);
4368 if (SIMPLEQ_EMPTY(&commits)) {
4369 if (continue_rebase)
4370 error = rebase_complete(worktree, fileindex,
4371 branch, new_base_branch, tmp_branch, repo);
4373 error = got_error(GOT_ERR_EMPTY_REBASE);
4378 SIMPLEQ_FOREACH(qid, &commits, entry) {
4379 commit_id = qid->id;
4380 parent_id = pid ? pid->id : yca_id;
4383 error = got_worktree_rebase_merge_files(&merged_paths,
4384 worktree, fileindex, parent_id, commit_id, repo,
4385 rebase_progress, &rebase_status, check_cancelled, NULL);
4389 if (rebase_status == GOT_STATUS_CONFLICT) {
4390 got_worktree_rebase_pathlist_free(&merged_paths);
4394 error = rebase_commit(&merged_paths, worktree, fileindex,
4395 tmp_branch, commit_id, repo);
4396 got_worktree_rebase_pathlist_free(&merged_paths);
4401 if (rebase_status == GOT_STATUS_CONFLICT) {
4402 error = got_worktree_rebase_postpone(worktree, fileindex);
4405 error = got_error_msg(GOT_ERR_CONFLICTS,
4406 "conflicts must be resolved before rebasing can continue");
4408 error = rebase_complete(worktree, fileindex, branch,
4409 new_base_branch, tmp_branch, repo);
4411 got_object_id_queue_free(&commits);
4412 free(branch_head_commit_id);
4413 free(resume_commit_id);
4416 got_object_commit_close(commit);
4418 got_ref_close(branch);
4419 if (new_base_branch)
4420 got_ref_close(new_base_branch);
4422 got_ref_close(tmp_branch);
4424 got_worktree_close(worktree);
4426 got_repo_close(repo);
4431 usage_histedit(void)
4433 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script]\n",
4438 #define GOT_HISTEDIT_PICK 'p'
4439 #define GOT_HISTEDIT_EDIT 'e'
4440 #define GOT_HISTEDIT_FOLD 'f'
4441 #define GOT_HISTEDIT_DROP 'd'
4442 #define GOT_HISTEDIT_MESG 'm'
4444 static struct got_histedit_cmd {
4448 } got_histedit_cmds[] = {
4449 { GOT_HISTEDIT_PICK, "pick", "use commit" },
4450 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
4451 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
4452 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
4453 { GOT_HISTEDIT_MESG, "mesg",
4454 "single-line log message for commit above (open editor if empty)" },
4457 struct got_histedit_list_entry {
4458 TAILQ_ENTRY(got_histedit_list_entry) entry;
4459 struct got_object_id *commit_id;
4460 const struct got_histedit_cmd *cmd;
4463 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
4465 static const struct got_error *
4466 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
4467 FILE *f, struct got_repository *repo)
4469 const struct got_error *err = NULL;
4470 char *logmsg = NULL, *id_str = NULL;
4471 struct got_commit_object *commit = NULL;
4474 err = got_object_open_as_commit(&commit, repo, commit_id);
4478 err = get_short_logmsg(&logmsg, 34, commit);
4482 err = got_object_id_str(&id_str, commit_id);
4486 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
4488 err = got_ferror(f, GOT_ERR_IO);
4491 got_object_commit_close(commit);
4497 static const struct got_error *
4498 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
4499 struct got_repository *repo)
4501 const struct got_error *err = NULL;
4502 struct got_object_qid *qid;
4504 if (SIMPLEQ_EMPTY(commits))
4505 return got_error(GOT_ERR_EMPTY_HISTEDIT);
4507 SIMPLEQ_FOREACH(qid, commits, entry) {
4508 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
4517 static const struct got_error *
4518 write_cmd_list(FILE *f)
4520 const struct got_error *err = NULL;
4523 n = fprintf(f, "# Available histedit commands:\n");
4525 return got_ferror(f, GOT_ERR_IO);
4527 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4528 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
4529 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
4532 err = got_ferror(f, GOT_ERR_IO);
4536 n = fprintf(f, "# Commits will be processed in order from top to "
4537 "bottom of this file.\n");
4539 return got_ferror(f, GOT_ERR_IO);
4543 static const struct got_error *
4544 histedit_syntax_error(int lineno)
4546 static char msg[42];
4549 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
4551 if (ret == -1 || ret >= sizeof(msg))
4552 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
4554 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
4557 static const struct got_error *
4558 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
4559 char *logmsg, struct got_repository *repo)
4561 const struct got_error *err;
4562 struct got_commit_object *folded_commit = NULL;
4563 char *id_str, *folded_logmsg = NULL;
4565 err = got_object_id_str(&id_str, hle->commit_id);
4569 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
4573 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
4576 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
4577 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
4578 folded_logmsg) == -1) {
4579 err = got_error_from_errno("asprintf");
4584 got_object_commit_close(folded_commit);
4586 free(folded_logmsg);
4590 static struct got_histedit_list_entry *
4591 get_folded_commits(struct got_histedit_list_entry *hle)
4593 struct got_histedit_list_entry *prev, *folded = NULL;
4595 prev = TAILQ_PREV(hle, got_histedit_list, entry);
4596 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
4597 prev->cmd->code == GOT_HISTEDIT_DROP)) {
4598 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
4600 prev = TAILQ_PREV(prev, got_histedit_list, entry);
4606 static const struct got_error *
4607 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
4608 struct got_repository *repo)
4610 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
4611 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
4612 const struct got_error *err = NULL;
4613 struct got_commit_object *commit = NULL;
4615 struct got_histedit_list_entry *folded = NULL;
4617 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4621 folded = get_folded_commits(hle);
4623 while (folded != hle) {
4624 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
4625 folded = TAILQ_NEXT(folded, entry);
4628 err = append_folded_commit_msg(&new_msg, folded,
4634 folded = TAILQ_NEXT(folded, entry);
4638 err = got_object_id_str(&id_str, hle->commit_id);
4641 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
4644 if (asprintf(&new_msg,
4645 "%s\n# original log message of commit %s: %s",
4646 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
4647 err = got_error_from_errno("asprintf");
4653 err = got_object_id_str(&id_str, hle->commit_id);
4657 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
4661 dprintf(fd, logmsg);
4664 err = get_editor(&editor);
4668 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
4670 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
4672 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
4675 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
4676 err = got_error_from_errno2("unlink", logmsg_path);
4682 got_object_commit_close(commit);
4686 static const struct got_error *
4687 histedit_parse_list(struct got_histedit_list *histedit_cmds,
4688 FILE *f, struct got_repository *repo)
4690 const struct got_error *err = NULL;
4691 char *line = NULL, *p, *end;
4695 const struct got_histedit_cmd *cmd;
4696 struct got_object_id *commit_id = NULL;
4697 struct got_histedit_list_entry *hle = NULL;
4700 len = getline(&line, &size, f);
4702 const struct got_error *getline_err;
4705 getline_err = got_error_from_errno("getline");
4706 err = got_ferror(f, getline_err->code);
4711 while (isspace((unsigned char)p[0]))
4713 if (p[0] == '#' || p[0] == '\0') {
4719 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4720 cmd = &got_histedit_cmds[i];
4721 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
4722 isspace((unsigned char)p[strlen(cmd->name)])) {
4723 p += strlen(cmd->name);
4726 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
4731 if (i == nitems(got_histedit_cmds)) {
4732 err = histedit_syntax_error(lineno);
4735 while (isspace((unsigned char)p[0]))
4737 if (cmd->code == GOT_HISTEDIT_MESG) {
4738 if (hle == NULL || hle->logmsg != NULL) {
4739 err = got_error(GOT_ERR_HISTEDIT_CMD);
4743 err = histedit_edit_logmsg(hle, repo);
4747 hle->logmsg = strdup(p);
4748 if (hle->logmsg == NULL) {
4749 err = got_error_from_errno("strdup");
4758 while (end[0] && !isspace((unsigned char)end[0]))
4762 err = got_object_resolve_id_str(&commit_id, repo, p);
4764 /* override error code */
4765 err = histedit_syntax_error(lineno);
4769 hle = malloc(sizeof(*hle));
4771 err = got_error_from_errno("malloc");
4775 hle->commit_id = commit_id;
4780 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
4788 static const struct got_error *
4789 histedit_check_script(struct got_histedit_list *histedit_cmds,
4790 struct got_object_id_queue *commits, struct got_repository *repo)
4792 const struct got_error *err = NULL;
4793 struct got_object_qid *qid;
4794 struct got_histedit_list_entry *hle;
4795 static char msg[80];
4798 if (TAILQ_EMPTY(histedit_cmds))
4799 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
4800 "histedit script contains no commands");
4801 if (SIMPLEQ_EMPTY(commits))
4802 return got_error(GOT_ERR_EMPTY_HISTEDIT);
4804 SIMPLEQ_FOREACH(qid, commits, entry) {
4805 TAILQ_FOREACH(hle, histedit_cmds, entry) {
4806 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
4810 err = got_object_id_str(&id_str, qid->id);
4813 snprintf(msg, sizeof(msg),
4814 "commit %s missing from histedit script", id_str);
4816 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
4820 if (hle->cmd->code == GOT_HISTEDIT_FOLD)
4821 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
4822 "last commit in histedit script cannot be folded");
4827 static const struct got_error *
4828 histedit_run_editor(struct got_histedit_list *histedit_cmds,
4829 const char *path, struct got_object_id_queue *commits,
4830 struct got_repository *repo)
4832 const struct got_error *err = NULL;
4836 err = get_editor(&editor);
4840 if (spawn_editor(editor, path) == -1) {
4841 err = got_error_from_errno("failed spawning editor");
4845 f = fopen(path, "r");
4847 err = got_error_from_errno("fopen");
4850 err = histedit_parse_list(histedit_cmds, f, repo);
4854 err = histedit_check_script(histedit_cmds, commits, repo);
4856 if (f && fclose(f) != 0 && err == NULL)
4857 err = got_error_from_errno("fclose");
4862 static const struct got_error *
4863 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
4864 struct got_object_id_queue *, const char *, struct got_repository *);
4866 static const struct got_error *
4867 histedit_edit_script(struct got_histedit_list *histedit_cmds,
4868 struct got_object_id_queue *commits, struct got_repository *repo)
4870 const struct got_error *err;
4874 err = got_opentemp_named(&path, &f, "got-histedit");
4878 err = write_cmd_list(f);
4882 err = histedit_write_commit_list(commits, f, repo);
4886 if (fclose(f) != 0) {
4887 err = got_error_from_errno("fclose");
4892 err = histedit_run_editor(histedit_cmds, path, commits, repo);
4894 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
4895 err->code != GOT_ERR_HISTEDIT_CMD)
4897 err = histedit_edit_list_retry(histedit_cmds, err,
4898 commits, path, repo);
4901 if (f && fclose(f) != 0 && err == NULL)
4902 err = got_error_from_errno("fclose");
4903 if (path && unlink(path) != 0 && err == NULL)
4904 err = got_error_from_errno2("unlink", path);
4909 static const struct got_error *
4910 histedit_save_list(struct got_histedit_list *histedit_cmds,
4911 struct got_worktree *worktree, struct got_repository *repo)
4913 const struct got_error *err = NULL;
4916 struct got_histedit_list_entry *hle;
4917 struct got_commit_object *commit = NULL;
4919 err = got_worktree_get_histedit_script_path(&path, worktree);
4923 f = fopen(path, "w");
4925 err = got_error_from_errno2("fopen", path);
4928 TAILQ_FOREACH(hle, histedit_cmds, entry) {
4929 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
4935 int n = fprintf(f, "%c %s\n",
4936 GOT_HISTEDIT_MESG, hle->logmsg);
4938 err = got_ferror(f, GOT_ERR_IO);
4944 if (f && fclose(f) != 0 && err == NULL)
4945 err = got_error_from_errno("fclose");
4948 got_object_commit_close(commit);
4953 histedit_free_list(struct got_histedit_list *histedit_cmds)
4955 struct got_histedit_list_entry *hle;
4957 while ((hle = TAILQ_FIRST(histedit_cmds))) {
4958 TAILQ_REMOVE(histedit_cmds, hle, entry);
4963 static const struct got_error *
4964 histedit_load_list(struct got_histedit_list *histedit_cmds,
4965 const char *path, struct got_repository *repo)
4967 const struct got_error *err = NULL;
4970 f = fopen(path, "r");
4972 err = got_error_from_errno2("fopen", path);
4976 err = histedit_parse_list(histedit_cmds, f, repo);
4978 if (f && fclose(f) != 0 && err == NULL)
4979 err = got_error_from_errno("fclose");
4983 static const struct got_error *
4984 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
4985 const struct got_error *edit_err, struct got_object_id_queue *commits,
4986 const char *path, struct got_repository *repo)
4988 const struct got_error *err = NULL, *prev_err = edit_err;
4991 while (resp != 'c' && resp != 'r' && resp != 'a') {
4992 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
4993 "or (a)bort: ", getprogname(), prev_err->msg);
4998 histedit_free_list(histedit_cmds);
4999 err = histedit_run_editor(histedit_cmds, path, commits,
5002 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5003 err->code != GOT_ERR_HISTEDIT_CMD)
5010 } else if (resp == 'r') {
5011 histedit_free_list(histedit_cmds);
5012 err = histedit_edit_script(histedit_cmds,
5015 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5016 err->code != GOT_ERR_HISTEDIT_CMD)
5023 } else if (resp == 'a') {
5024 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
5027 printf("invalid response '%c'\n", resp);
5033 static const struct got_error *
5034 histedit_complete(struct got_worktree *worktree,
5035 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5036 struct got_reference *branch, struct got_repository *repo)
5038 printf("Switching work tree to %s\n",
5039 got_ref_get_symref_target(branch));
5040 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
5044 static const struct got_error *
5045 show_histedit_progress(struct got_commit_object *commit,
5046 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
5048 const struct got_error *err;
5049 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5051 err = got_object_id_str(&old_id_str, hle->commit_id);
5056 err = got_object_id_str(&new_id_str, new_id);
5061 old_id_str[12] = '\0';
5063 new_id_str[12] = '\0';
5066 logmsg = strdup(hle->logmsg);
5067 if (logmsg == NULL) {
5068 err = got_error_from_errno("strdup");
5071 trim_logmsg(logmsg, 42);
5073 err = get_short_logmsg(&logmsg, 42, commit);
5078 switch (hle->cmd->code) {
5079 case GOT_HISTEDIT_PICK:
5080 case GOT_HISTEDIT_EDIT:
5081 printf("%s -> %s: %s\n", old_id_str,
5082 new_id_str ? new_id_str : "no-op change", logmsg);
5084 case GOT_HISTEDIT_DROP:
5085 case GOT_HISTEDIT_FOLD:
5086 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
5099 static const struct got_error *
5100 histedit_commit(struct got_pathlist_head *merged_paths,
5101 struct got_worktree *worktree, struct got_fileindex *fileindex,
5102 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
5103 struct got_repository *repo)
5105 const struct got_error *err;
5106 struct got_commit_object *commit;
5107 struct got_object_id *new_commit_id;
5109 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
5110 && hle->logmsg == NULL) {
5111 err = histedit_edit_logmsg(hle, repo);
5116 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5120 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
5121 worktree, fileindex, tmp_branch, commit, hle->commit_id,
5124 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
5126 err = show_histedit_progress(commit, hle, NULL);
5128 err = show_histedit_progress(commit, hle, new_commit_id);
5129 free(new_commit_id);
5132 got_object_commit_close(commit);
5136 static const struct got_error *
5137 histedit_skip_commit(struct got_histedit_list_entry *hle,
5138 struct got_worktree *worktree, struct got_repository *repo)
5140 const struct got_error *error;
5141 struct got_commit_object *commit;
5143 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
5148 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
5152 error = show_histedit_progress(commit, hle, NULL);
5153 got_object_commit_close(commit);
5157 static const struct got_error *
5158 cmd_histedit(int argc, char *argv[])
5160 const struct got_error *error = NULL;
5161 struct got_worktree *worktree = NULL;
5162 struct got_fileindex *fileindex = NULL;
5163 struct got_repository *repo = NULL;
5165 struct got_reference *branch = NULL;
5166 struct got_reference *tmp_branch = NULL;
5167 struct got_object_id *resume_commit_id = NULL;
5168 struct got_object_id *base_commit_id = NULL;
5169 struct got_object_id *head_commit_id = NULL;
5170 struct got_commit_object *commit = NULL;
5171 int ch, rebase_in_progress = 0, did_something;
5172 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
5173 const char *edit_script_path = NULL;
5174 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5175 struct got_object_id_queue commits;
5176 struct got_pathlist_head merged_paths;
5177 const struct got_object_id_queue *parent_ids;
5178 struct got_object_qid *pid;
5179 struct got_histedit_list histedit_cmds;
5180 struct got_histedit_list_entry *hle;
5182 SIMPLEQ_INIT(&commits);
5183 TAILQ_INIT(&histedit_cmds);
5184 TAILQ_INIT(&merged_paths);
5186 while ((ch = getopt(argc, argv, "acF:")) != -1) {
5195 edit_script_path = optarg;
5207 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5208 "unveil", NULL) == -1)
5211 if (abort_edit && continue_edit)
5217 * This command cannot apply unveil(2) in all cases because the
5218 * user may choose to run an editor to edit the histedit script
5219 * and to edit individual commit log messages.
5220 * unveil(2) traverses exec(2); if an editor is used we have to
5221 * apply unveil after edit script and log messages have been written.
5222 * XXX TODO: Make use of unveil(2) where possible.
5225 cwd = getcwd(NULL, 0);
5227 error = got_error_from_errno("getcwd");
5230 error = got_worktree_open(&worktree, cwd);
5234 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5238 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5241 if (rebase_in_progress) {
5242 error = got_error(GOT_ERR_REBASING);
5246 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
5250 if (edit_in_progress && abort_edit) {
5251 error = got_worktree_histedit_continue(&resume_commit_id,
5252 &tmp_branch, &branch, &base_commit_id, &fileindex,
5256 printf("Switching work tree to %s\n",
5257 got_ref_get_symref_target(branch));
5258 error = got_worktree_histedit_abort(worktree, fileindex, repo,
5259 branch, base_commit_id, update_progress, &did_something);
5262 printf("Histedit of %s aborted\n",
5263 got_ref_get_symref_target(branch));
5264 goto done; /* nothing else to do */
5265 } else if (abort_edit) {
5266 error = got_error(GOT_ERR_NOT_HISTEDIT);
5270 if (continue_edit) {
5273 if (!edit_in_progress) {
5274 error = got_error(GOT_ERR_NOT_HISTEDIT);
5278 error = got_worktree_get_histedit_script_path(&path, worktree);
5282 error = histedit_load_list(&histedit_cmds, path, repo);
5287 error = got_worktree_histedit_continue(&resume_commit_id,
5288 &tmp_branch, &branch, &base_commit_id, &fileindex,
5293 error = got_ref_resolve(&head_commit_id, repo, branch);
5297 error = got_object_open_as_commit(&commit, repo,
5301 parent_ids = got_object_commit_get_parent_ids(commit);
5302 pid = SIMPLEQ_FIRST(parent_ids);
5304 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
5307 error = collect_commits(&commits, head_commit_id, pid->id,
5308 base_commit_id, got_worktree_get_path_prefix(worktree),
5309 GOT_ERR_HISTEDIT_PATH, repo);
5310 got_object_commit_close(commit);
5315 if (edit_in_progress) {
5316 error = got_error(GOT_ERR_HISTEDIT_BUSY);
5320 error = got_ref_open(&branch, repo,
5321 got_worktree_get_head_ref_name(worktree), 0);
5325 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
5326 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
5327 "will not edit commit history of a branch outside "
5328 "the \"refs/heads/\" reference namespace");
5332 error = got_ref_resolve(&head_commit_id, repo, branch);
5333 got_ref_close(branch);
5338 error = got_object_open_as_commit(&commit, repo,
5342 parent_ids = got_object_commit_get_parent_ids(commit);
5343 pid = SIMPLEQ_FIRST(parent_ids);
5345 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
5348 error = collect_commits(&commits, head_commit_id, pid->id,
5349 got_worktree_get_base_commit_id(worktree),
5350 got_worktree_get_path_prefix(worktree),
5351 GOT_ERR_HISTEDIT_PATH, repo);
5352 got_object_commit_close(commit);
5357 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
5358 &base_commit_id, &fileindex, worktree, repo);
5362 if (edit_script_path) {
5363 error = histedit_load_list(&histedit_cmds,
5364 edit_script_path, repo);
5366 got_worktree_histedit_abort(worktree, fileindex,
5367 repo, branch, base_commit_id,
5368 update_progress, &did_something);
5372 error = histedit_edit_script(&histedit_cmds, &commits,
5375 got_worktree_histedit_abort(worktree, fileindex,
5376 repo, branch, base_commit_id,
5377 update_progress, &did_something);
5383 error = histedit_save_list(&histedit_cmds, worktree,
5386 got_worktree_histedit_abort(worktree, fileindex,
5387 repo, branch, base_commit_id,
5388 update_progress, &did_something);
5394 error = histedit_check_script(&histedit_cmds, &commits, repo);
5398 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
5399 if (resume_commit_id) {
5400 if (got_object_id_cmp(hle->commit_id,
5401 resume_commit_id) != 0)
5404 resume_commit_id = NULL;
5405 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
5406 hle->cmd->code == GOT_HISTEDIT_FOLD) {
5407 error = histedit_skip_commit(hle, worktree,
5410 error = histedit_commit(NULL, worktree,
5411 fileindex, tmp_branch, hle, repo);
5418 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
5419 error = histedit_skip_commit(hle, worktree, repo);
5425 error = got_object_open_as_commit(&commit, repo,
5429 parent_ids = got_object_commit_get_parent_ids(commit);
5430 pid = SIMPLEQ_FIRST(parent_ids);
5432 error = got_worktree_histedit_merge_files(&merged_paths,
5433 worktree, fileindex, pid->id, hle->commit_id, repo,
5434 rebase_progress, &rebase_status, check_cancelled, NULL);
5437 got_object_commit_close(commit);
5440 if (rebase_status == GOT_STATUS_CONFLICT) {
5441 got_worktree_rebase_pathlist_free(&merged_paths);
5445 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
5447 error = got_object_id_str(&id_str, hle->commit_id);
5450 printf("Stopping histedit for amending commit %s\n",
5453 got_worktree_rebase_pathlist_free(&merged_paths);
5454 error = got_worktree_histedit_postpone(worktree,
5459 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
5460 error = histedit_skip_commit(hle, worktree, repo);
5466 error = histedit_commit(&merged_paths, worktree, fileindex,
5467 tmp_branch, hle, repo);
5468 got_worktree_rebase_pathlist_free(&merged_paths);
5473 if (rebase_status == GOT_STATUS_CONFLICT) {
5474 error = got_worktree_histedit_postpone(worktree, fileindex);
5477 error = got_error_msg(GOT_ERR_CONFLICTS,
5478 "conflicts must be resolved before rebasing can continue");
5480 error = histedit_complete(worktree, fileindex, tmp_branch,
5483 got_object_id_queue_free(&commits);
5484 histedit_free_list(&histedit_cmds);
5485 free(head_commit_id);
5486 free(base_commit_id);
5487 free(resume_commit_id);
5489 got_object_commit_close(commit);
5491 got_ref_close(branch);
5493 got_ref_close(tmp_branch);
5495 got_worktree_close(worktree);
5497 got_repo_close(repo);
5504 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
5505 "[file-path ...]\n",
5510 static const struct got_error *
5511 print_stage(void *arg, unsigned char status, unsigned char staged_status,
5512 const char *path, struct got_object_id *blob_id,
5513 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
5515 const struct got_error *err = NULL;
5516 char *id_str = NULL;
5518 if (staged_status != GOT_STATUS_ADD &&
5519 staged_status != GOT_STATUS_MODIFY &&
5520 staged_status != GOT_STATUS_DELETE)
5523 if (staged_status == GOT_STATUS_ADD ||
5524 staged_status == GOT_STATUS_MODIFY)
5525 err = got_object_id_str(&id_str, staged_blob_id);
5527 err = got_object_id_str(&id_str, blob_id);
5531 printf("%s %c %s\n", id_str, staged_status, path);
5536 static const struct got_error *
5537 cmd_stage(int argc, char *argv[])
5539 const struct got_error *error = NULL;
5540 struct got_repository *repo = NULL;
5541 struct got_worktree *worktree = NULL;
5543 struct got_pathlist_head paths;
5544 struct got_pathlist_entry *pe;
5545 int ch, list_stage = 0, pflag = 0;
5546 FILE *patch_script_file = NULL;
5547 const char *patch_script_path = NULL;
5548 struct choose_patch_arg cpa;
5552 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
5561 patch_script_path = optarg;
5573 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5574 "unveil", NULL) == -1)
5577 if (list_stage && (pflag || patch_script_path))
5578 errx(1, "-l option cannot be used with other options");
5579 if (patch_script_path && !pflag)
5580 errx(1, "-F option can only be used together with -p option");
5582 cwd = getcwd(NULL, 0);
5584 error = got_error_from_errno("getcwd");
5588 error = got_worktree_open(&worktree, cwd);
5592 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5596 if (patch_script_path) {
5597 patch_script_file = fopen(patch_script_path, "r");
5598 if (patch_script_file == NULL) {
5599 error = got_error_from_errno2("fopen",
5604 error = apply_unveil(got_repo_get_path(repo), 1,
5605 got_worktree_get_root_path(worktree));
5609 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5614 error = got_worktree_status(worktree, &paths, repo,
5615 print_stage, NULL, check_cancelled, NULL);
5617 cpa.patch_script_file = patch_script_file;
5618 cpa.action = "stage";
5619 error = got_worktree_stage(worktree, &paths,
5620 pflag ? NULL : print_status, NULL,
5621 pflag ? choose_patch : NULL, &cpa, repo);
5624 if (patch_script_file && fclose(patch_script_file) == EOF &&
5626 error = got_error_from_errno2("fclose", patch_script_path);
5628 got_repo_close(repo);
5630 got_worktree_close(worktree);
5631 TAILQ_FOREACH(pe, &paths, entry)
5632 free((char *)pe->path);
5633 got_pathlist_free(&paths);
5641 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
5642 "[file-path ...]\n",
5648 static const struct got_error *
5649 cmd_unstage(int argc, char *argv[])
5651 const struct got_error *error = NULL;
5652 struct got_repository *repo = NULL;
5653 struct got_worktree *worktree = NULL;
5655 struct got_pathlist_head paths;
5656 struct got_pathlist_entry *pe;
5657 int ch, did_something = 0, pflag = 0;
5658 FILE *patch_script_file = NULL;
5659 const char *patch_script_path = NULL;
5660 struct choose_patch_arg cpa;
5664 while ((ch = getopt(argc, argv, "pF:")) != -1) {
5670 patch_script_path = optarg;
5682 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5683 "unveil", NULL) == -1)
5686 if (patch_script_path && !pflag)
5687 errx(1, "-F option can only be used together with -p option");
5689 cwd = getcwd(NULL, 0);
5691 error = got_error_from_errno("getcwd");
5695 error = got_worktree_open(&worktree, cwd);
5699 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5703 if (patch_script_path) {
5704 patch_script_file = fopen(patch_script_path, "r");
5705 if (patch_script_file == NULL) {
5706 error = got_error_from_errno2("fopen",
5712 error = apply_unveil(got_repo_get_path(repo), 1,
5713 got_worktree_get_root_path(worktree));
5717 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5721 cpa.patch_script_file = patch_script_file;
5722 cpa.action = "unstage";
5723 error = got_worktree_unstage(worktree, &paths, update_progress,
5724 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
5726 if (patch_script_file && fclose(patch_script_file) == EOF &&
5728 error = got_error_from_errno2("fclose", patch_script_path);
5730 got_repo_close(repo);
5732 got_worktree_close(worktree);
5733 TAILQ_FOREACH(pe, &paths, entry)
5734 free((char *)pe->path);
5735 got_pathlist_free(&paths);