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());
1555 get_default_log_limit(void)
1557 const char *got_default_log_limit;
1561 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
1562 if (got_default_log_limit == NULL)
1564 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
1570 static const struct got_error *
1571 cmd_log(int argc, char *argv[])
1573 const struct got_error *error;
1574 struct got_repository *repo = NULL;
1575 struct got_worktree *worktree = NULL;
1576 struct got_commit_object *commit = NULL;
1577 struct got_object_id *id = NULL;
1578 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1579 char *start_commit = NULL;
1580 int diff_context = 3, ch;
1581 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1583 struct got_reflist_head refs;
1585 SIMPLEQ_INIT(&refs);
1588 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1594 limit = get_default_log_limit();
1596 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
1602 start_commit = optarg;
1605 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1608 err(1, "-C option %s", errstr);
1611 limit = strtonum(optarg, 0, INT_MAX, &errstr);
1613 err(1, "-l option %s", errstr);
1616 first_parent_traversal = 1;
1619 repo_path = realpath(optarg, NULL);
1620 if (repo_path == NULL)
1621 err(1, "-r option");
1622 got_path_strip_trailing_slashes(repo_path);
1633 cwd = getcwd(NULL, 0);
1635 error = got_error_from_errno("getcwd");
1639 error = got_worktree_open(&worktree, cwd);
1640 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1647 error = got_error_from_errno("strdup");
1650 } else if (argc == 1) {
1652 error = got_worktree_resolve_path(&path, worktree,
1657 path = strdup(argv[0]);
1659 error = got_error_from_errno("strdup");
1666 if (repo_path == NULL) {
1667 repo_path = worktree ?
1668 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1670 if (repo_path == NULL) {
1671 error = got_error_from_errno("strdup");
1675 error = got_repo_open(&repo, repo_path);
1679 error = apply_unveil(got_repo_get_path(repo), 1,
1680 worktree ? got_worktree_get_root_path(worktree) : NULL);
1684 if (start_commit == NULL) {
1685 struct got_reference *head_ref;
1686 error = got_ref_open(&head_ref, repo,
1687 worktree ? got_worktree_get_head_ref_name(worktree)
1691 error = got_ref_resolve(&id, repo, head_ref);
1692 got_ref_close(head_ref);
1695 error = got_object_open_as_commit(&commit, repo, id);
1697 struct got_reference *ref;
1698 error = got_ref_open(&ref, repo, start_commit, 0);
1699 if (error == NULL) {
1701 error = got_ref_resolve(&id, repo, ref);
1705 error = got_object_get_type(&obj_type, repo, id);
1708 if (obj_type == GOT_OBJ_TYPE_TAG) {
1709 struct got_tag_object *tag;
1710 error = got_object_open_as_tag(&tag, repo, id);
1713 if (got_object_tag_get_object_type(tag) !=
1714 GOT_OBJ_TYPE_COMMIT) {
1715 got_object_tag_close(tag);
1716 error = got_error(GOT_ERR_OBJ_TYPE);
1720 id = got_object_id_dup(
1721 got_object_tag_get_object_id(tag));
1723 error = got_error_from_errno(
1724 "got_object_id_dup");
1725 got_object_tag_close(tag);
1728 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1729 error = got_error(GOT_ERR_OBJ_TYPE);
1732 error = got_object_open_as_commit(&commit, repo, id);
1736 if (commit == NULL) {
1737 error = got_repo_match_object_id_prefix(&id,
1738 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1746 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1751 path = in_repo_path;
1754 error = got_ref_list(&refs, repo);
1758 error = print_commits(id, repo, path, show_patch,
1759 diff_context, limit, first_parent_traversal, &refs);
1766 got_worktree_close(worktree);
1768 const struct got_error *repo_error;
1769 repo_error = got_repo_close(repo);
1773 got_ref_list_free(&refs);
1780 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
1781 "[object1 object2 | path]\n", getprogname());
1785 struct print_diff_arg {
1786 struct got_repository *repo;
1787 struct got_worktree *worktree;
1794 static const struct got_error *
1795 print_diff(void *arg, unsigned char status, unsigned char staged_status,
1796 const char *path, struct got_object_id *blob_id,
1797 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
1799 struct print_diff_arg *a = arg;
1800 const struct got_error *err = NULL;
1801 struct got_blob_object *blob1 = NULL;
1803 char *abspath = NULL, *label1 = NULL;
1806 if (a->diff_staged) {
1807 if (staged_status != GOT_STATUS_MODIFY &&
1808 staged_status != GOT_STATUS_ADD &&
1809 staged_status != GOT_STATUS_DELETE)
1812 if (staged_status == GOT_STATUS_DELETE)
1814 if (status != GOT_STATUS_MODIFY &&
1815 status != GOT_STATUS_ADD &&
1816 status != GOT_STATUS_DELETE &&
1817 status != GOT_STATUS_CONFLICT)
1821 if (!a->header_shown) {
1822 printf("diff %s %s%s\n", a->id_str,
1823 got_worktree_get_root_path(a->worktree),
1824 a->diff_staged ? " (staged changes)" : "");
1825 a->header_shown = 1;
1828 if (a->diff_staged) {
1829 const char *label1 = NULL, *label2 = NULL;
1830 switch (staged_status) {
1831 case GOT_STATUS_MODIFY:
1835 case GOT_STATUS_ADD:
1838 case GOT_STATUS_DELETE:
1842 return got_error(GOT_ERR_FILE_STATUS);
1844 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
1845 label1, label2, a->diff_context, a->repo, stdout);
1848 if (staged_status == GOT_STATUS_ADD ||
1849 staged_status == GOT_STATUS_MODIFY) {
1851 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
1855 err = got_object_id_str(&id_str, staged_blob_id);
1858 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
1859 err = got_error_from_errno("asprintf");
1864 } else if (status != GOT_STATUS_ADD) {
1865 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1870 if (status != GOT_STATUS_DELETE) {
1871 if (asprintf(&abspath, "%s/%s",
1872 got_worktree_get_root_path(a->worktree), path) == -1) {
1873 err = got_error_from_errno("asprintf");
1877 f2 = fopen(abspath, "r");
1879 err = got_error_from_errno2("fopen", abspath);
1882 if (lstat(abspath, &sb) == -1) {
1883 err = got_error_from_errno2("lstat", abspath);
1889 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
1890 a->diff_context, stdout);
1893 got_object_blob_close(blob1);
1894 if (f2 && fclose(f2) != 0 && err == NULL)
1895 err = got_error_from_errno("fclose");
1900 static const struct got_error *
1901 match_object_id(struct got_object_id **id, char **label,
1902 const char *id_str, int obj_type, struct got_repository *repo)
1904 const struct got_error *err;
1905 struct got_tag_object *tag;
1906 struct got_reference *ref = NULL;
1911 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY, repo);
1913 *id = got_object_id_dup(got_object_tag_get_object_id(tag));
1915 err = got_error_from_errno("got_object_id_dup");
1916 if (asprintf(label, "refs/tags/%s",
1917 got_object_tag_get_name(tag)) == -1)
1918 err = got_error_from_errno("asprintf");
1919 got_object_tag_close(tag);
1921 } else if (err->code != GOT_ERR_NO_OBJ)
1924 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
1926 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1928 err = got_ref_open(&ref, repo, id_str, 0);
1931 *label = strdup(got_ref_get_name(ref));
1932 if (*label == NULL) {
1933 err = got_error_from_errno("strdup");
1936 err = got_ref_resolve(id, repo, ref);
1938 err = got_object_id_str(label, *id);
1939 if (*label == NULL) {
1940 err = got_error_from_errno("strdup");
1951 static const struct got_error *
1952 cmd_diff(int argc, char *argv[])
1954 const struct got_error *error;
1955 struct got_repository *repo = NULL;
1956 struct got_worktree *worktree = NULL;
1957 char *cwd = NULL, *repo_path = NULL;
1958 struct got_object_id *id1 = NULL, *id2 = NULL;
1959 const char *id_str1 = NULL, *id_str2 = NULL;
1960 char *label1 = NULL, *label2 = NULL;
1962 int diff_context = 3, diff_staged = 0, ch;
1967 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1972 while ((ch = getopt(argc, argv, "C:r:s")) != -1) {
1975 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1977 err(1, "-C option %s", errstr);
1980 repo_path = realpath(optarg, NULL);
1981 if (repo_path == NULL)
1982 err(1, "-r option");
1983 got_path_strip_trailing_slashes(repo_path);
1997 cwd = getcwd(NULL, 0);
1999 error = got_error_from_errno("getcwd");
2002 error = got_worktree_open(&worktree, cwd);
2003 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2006 if (worktree == NULL) {
2007 error = got_error(GOT_ERR_NOT_WORKTREE);
2012 "-r option can't be used when diffing a work tree");
2013 repo_path = strdup(got_worktree_get_repo_path(worktree));
2014 if (repo_path == NULL) {
2015 error = got_error_from_errno("strdup");
2019 error = got_worktree_resolve_path(&path, worktree,
2026 error = got_error_from_errno("strdup");
2030 } else if (argc == 2) {
2032 errx(1, "-s option can't be used when diffing "
2033 "objects in repository");
2036 if (worktree && repo_path == NULL) {
2038 strdup(got_worktree_get_repo_path(worktree));
2039 if (repo_path == NULL) {
2040 error = got_error_from_errno("strdup");
2047 if (repo_path == NULL) {
2048 repo_path = getcwd(NULL, 0);
2049 if (repo_path == NULL)
2050 return got_error_from_errno("getcwd");
2053 error = got_repo_open(&repo, repo_path);
2058 error = apply_unveil(got_repo_get_path(repo), 1,
2059 worktree ? got_worktree_get_root_path(worktree) : NULL);
2064 struct print_diff_arg arg;
2065 struct got_pathlist_head paths;
2070 error = got_object_id_str(&id_str,
2071 got_worktree_get_base_commit_id(worktree));
2075 arg.worktree = worktree;
2076 arg.diff_context = diff_context;
2077 arg.id_str = id_str;
2078 arg.header_shown = 0;
2079 arg.diff_staged = diff_staged;
2081 error = got_pathlist_append(&paths, path, NULL);
2085 error = got_worktree_status(worktree, &paths, repo, print_diff,
2086 &arg, check_cancelled, NULL);
2088 got_pathlist_free(&paths);
2092 error = match_object_id(&id1, &label1, id_str1, GOT_OBJ_TYPE_ANY, repo);
2096 error = match_object_id(&id2, &label2, id_str2, GOT_OBJ_TYPE_ANY, repo);
2100 error = got_object_get_type(&type1, repo, id1);
2104 error = got_object_get_type(&type2, repo, id2);
2108 if (type1 != type2) {
2109 error = got_error(GOT_ERR_OBJ_TYPE);
2114 case GOT_OBJ_TYPE_BLOB:
2115 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2116 diff_context, repo, stdout);
2118 case GOT_OBJ_TYPE_TREE:
2119 error = got_diff_objects_as_trees(id1, id2, "", "",
2120 diff_context, repo, stdout);
2122 case GOT_OBJ_TYPE_COMMIT:
2123 printf("diff %s %s\n", label1, label2);
2124 error = got_diff_objects_as_commits(id1, id2, diff_context,
2128 error = got_error(GOT_ERR_OBJ_TYPE);
2138 got_worktree_close(worktree);
2140 const struct got_error *repo_error;
2141 repo_error = got_repo_close(repo);
2152 "usage: %s blame [-c commit] [-r repository-path] path\n",
2161 char datebuf[9]; /* YY-MM-DD + NUL */
2164 struct blame_cb_args {
2165 struct blame_line *lines;
2169 off_t *line_offsets;
2171 struct got_repository *repo;
2174 static const struct got_error *
2175 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2177 const struct got_error *err = NULL;
2178 struct blame_cb_args *a = arg;
2179 struct blame_line *bline;
2181 size_t linesize = 0;
2182 struct got_commit_object *commit = NULL;
2185 time_t committer_time;
2187 if (nlines != a->nlines ||
2188 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2189 return got_error(GOT_ERR_RANGE);
2191 if (sigint_received)
2192 return got_error(GOT_ERR_ITER_COMPLETED);
2194 /* Annotate this line. */
2195 bline = &a->lines[lineno - 1];
2196 if (bline->annotated)
2198 err = got_object_id_str(&bline->id_str, id);
2202 err = got_object_open_as_commit(&commit, a->repo, id);
2206 bline->committer = strdup(got_object_commit_get_committer(commit));
2207 if (bline->committer == NULL) {
2208 err = got_error_from_errno("strdup");
2212 committer_time = got_object_commit_get_committer_time(commit);
2213 if (localtime_r(&committer_time, &tm) == NULL)
2214 return got_error_from_errno("localtime_r");
2215 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%g/%m/%d",
2216 &tm) >= sizeof(bline->datebuf)) {
2217 err = got_error(GOT_ERR_NO_SPACE);
2220 bline->annotated = 1;
2222 /* Print lines annotated so far. */
2223 bline = &a->lines[a->lineno_cur - 1];
2224 if (!bline->annotated)
2227 offset = a->line_offsets[a->lineno_cur - 1];
2228 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2229 err = got_error_from_errno("fseeko");
2233 while (bline->annotated) {
2234 char *smallerthan, *at, *nl, *committer;
2237 if (getline(&line, &linesize, a->f) == (ssize_t)-1) {
2239 err = got_error_from_errno("getline");
2243 committer = bline->committer;
2244 smallerthan = strchr(committer, '<');
2245 if (smallerthan && smallerthan[1] != '\0')
2246 committer = smallerthan + 1;
2247 at = strchr(committer, '@');
2250 len = strlen(committer);
2252 committer[8] = '\0';
2254 nl = strchr(line, '\n');
2257 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2258 bline->id_str, bline->datebuf, committer, line);
2261 bline = &a->lines[a->lineno_cur - 1];
2265 got_object_commit_close(commit);
2270 static const struct got_error *
2271 cmd_blame(int argc, char *argv[])
2273 const struct got_error *error;
2274 struct got_repository *repo = NULL;
2275 struct got_worktree *worktree = NULL;
2276 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2277 struct got_object_id *obj_id = NULL;
2278 struct got_object_id *commit_id = NULL;
2279 struct got_blob_object *blob = NULL;
2280 char *commit_id_str = NULL;
2281 struct blame_cb_args bca;
2282 int ch, obj_type, i;
2285 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2290 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2293 commit_id_str = optarg;
2296 repo_path = realpath(optarg, NULL);
2297 if (repo_path == NULL)
2298 err(1, "-r option");
2299 got_path_strip_trailing_slashes(repo_path);
2315 cwd = getcwd(NULL, 0);
2317 error = got_error_from_errno("getcwd");
2320 if (repo_path == NULL) {
2321 error = got_worktree_open(&worktree, cwd);
2322 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2328 strdup(got_worktree_get_repo_path(worktree));
2329 if (repo_path == NULL)
2330 error = got_error_from_errno("strdup");
2334 repo_path = strdup(cwd);
2335 if (repo_path == NULL) {
2336 error = got_error_from_errno("strdup");
2342 error = got_repo_open(&repo, repo_path);
2346 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2351 const char *prefix = got_worktree_get_path_prefix(worktree);
2352 char *p, *worktree_subdir = cwd +
2353 strlen(got_worktree_get_root_path(worktree));
2354 if (asprintf(&p, "%s%s%s%s%s",
2355 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2356 worktree_subdir, worktree_subdir[0] ? "/" : "",
2358 error = got_error_from_errno("asprintf");
2361 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2364 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2369 if (commit_id_str == NULL) {
2370 struct got_reference *head_ref;
2371 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2374 error = got_ref_resolve(&commit_id, repo, head_ref);
2375 got_ref_close(head_ref);
2379 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2384 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2387 if (obj_id == NULL) {
2388 error = got_error(GOT_ERR_NO_OBJ);
2392 error = got_object_get_type(&obj_type, repo, obj_id);
2396 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2397 error = got_error(GOT_ERR_OBJ_TYPE);
2401 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2404 bca.f = got_opentemp();
2405 if (bca.f == NULL) {
2406 error = got_error_from_errno("got_opentemp");
2409 error = got_object_blob_dump_to_file(NULL, &bca.nlines,
2410 &bca.line_offsets, bca.f, blob);
2411 if (error || bca.nlines == 0)
2414 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2415 if (bca.lines == NULL) {
2416 error = got_error_from_errno("calloc");
2420 bca.nlines_prec = 0;
2428 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca);
2438 got_object_blob_close(blob);
2440 got_worktree_close(worktree);
2442 const struct got_error *repo_error;
2443 repo_error = got_repo_close(repo);
2447 for (i = 0; i < bca.nlines; i++) {
2448 struct blame_line *bline = &bca.lines[i];
2449 free(bline->id_str);
2450 free(bline->committer);
2453 free(bca.line_offsets);
2454 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2455 error = got_error_from_errno("fclose");
2463 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2469 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2470 const char *root_path)
2472 int is_root_path = (strcmp(path, root_path) == 0);
2473 const char *modestr = "";
2475 path += strlen(root_path);
2476 while (path[0] == '/')
2479 if (S_ISLNK(te->mode))
2481 else if (S_ISDIR(te->mode))
2483 else if (te->mode & S_IXUSR)
2486 printf("%s%s%s%s%s\n", id ? id : "", path,
2487 is_root_path ? "" : "/", te->name, modestr);
2490 static const struct got_error *
2491 print_tree(const char *path, struct got_object_id *commit_id,
2492 int show_ids, int recurse, const char *root_path,
2493 struct got_repository *repo)
2495 const struct got_error *err = NULL;
2496 struct got_object_id *tree_id = NULL;
2497 struct got_tree_object *tree = NULL;
2498 const struct got_tree_entries *entries;
2499 struct got_tree_entry *te;
2501 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2505 err = got_object_open_as_tree(&tree, repo, tree_id);
2508 entries = got_object_tree_get_entries(tree);
2509 te = SIMPLEQ_FIRST(&entries->head);
2513 if (sigint_received || sigpipe_received)
2518 err = got_object_id_str(&id_str, te->id);
2521 if (asprintf(&id, "%s ", id_str) == -1) {
2522 err = got_error_from_errno("asprintf");
2528 print_entry(te, id, path, root_path);
2531 if (recurse && S_ISDIR(te->mode)) {
2533 if (asprintf(&child_path, "%s%s%s", path,
2534 path[0] == '/' && path[1] == '\0' ? "" : "/",
2536 err = got_error_from_errno("asprintf");
2539 err = print_tree(child_path, commit_id, show_ids, 1,
2546 te = SIMPLEQ_NEXT(te, entry);
2550 got_object_tree_close(tree);
2555 static const struct got_error *
2556 cmd_tree(int argc, char *argv[])
2558 const struct got_error *error;
2559 struct got_repository *repo = NULL;
2560 struct got_worktree *worktree = NULL;
2562 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2563 struct got_object_id *commit_id = NULL;
2564 char *commit_id_str = NULL;
2565 int show_ids = 0, recurse = 0;
2569 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2574 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2577 commit_id_str = optarg;
2580 repo_path = realpath(optarg, NULL);
2581 if (repo_path == NULL)
2582 err(1, "-r option");
2583 got_path_strip_trailing_slashes(repo_path);
2607 cwd = getcwd(NULL, 0);
2609 error = got_error_from_errno("getcwd");
2612 if (repo_path == NULL) {
2613 error = got_worktree_open(&worktree, cwd);
2614 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2620 strdup(got_worktree_get_repo_path(worktree));
2621 if (repo_path == NULL)
2622 error = got_error_from_errno("strdup");
2626 repo_path = strdup(cwd);
2627 if (repo_path == NULL) {
2628 error = got_error_from_errno("strdup");
2634 error = got_repo_open(&repo, repo_path);
2638 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2644 char *p, *worktree_subdir = cwd +
2645 strlen(got_worktree_get_root_path(worktree));
2646 if (asprintf(&p, "%s/%s",
2647 got_worktree_get_path_prefix(worktree),
2648 worktree_subdir) == -1) {
2649 error = got_error_from_errno("asprintf");
2652 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2659 if (in_repo_path == NULL) {
2660 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2665 if (commit_id_str == NULL) {
2666 struct got_reference *head_ref;
2667 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2670 error = got_ref_resolve(&commit_id, repo, head_ref);
2671 got_ref_close(head_ref);
2675 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2680 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2681 in_repo_path, repo);
2688 got_worktree_close(worktree);
2690 const struct got_error *repo_error;
2691 repo_error = got_repo_close(repo);
2701 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
2705 static const struct got_error *
2706 print_status(void *arg, unsigned char status, unsigned char staged_status,
2707 const char *path, struct got_object_id *blob_id,
2708 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
2710 if (status == staged_status && (status == GOT_STATUS_DELETE))
2711 status = GOT_STATUS_NO_CHANGE;
2712 printf("%c%c %s\n", status, staged_status, path);
2716 static const struct got_error *
2717 cmd_status(int argc, char *argv[])
2719 const struct got_error *error = NULL;
2720 struct got_repository *repo = NULL;
2721 struct got_worktree *worktree = NULL;
2723 struct got_pathlist_head paths;
2724 struct got_pathlist_entry *pe;
2729 while ((ch = getopt(argc, argv, "")) != -1) {
2741 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2745 cwd = getcwd(NULL, 0);
2747 error = got_error_from_errno("getcwd");
2751 error = got_worktree_open(&worktree, cwd);
2755 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2759 error = apply_unveil(got_repo_get_path(repo), 1,
2760 got_worktree_get_root_path(worktree));
2764 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2768 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
2769 check_cancelled, NULL);
2771 TAILQ_FOREACH(pe, &paths, entry)
2772 free((char *)pe->path);
2773 got_pathlist_free(&paths);
2782 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
2787 static const struct got_error *
2788 list_refs(struct got_repository *repo)
2790 static const struct got_error *err = NULL;
2791 struct got_reflist_head refs;
2792 struct got_reflist_entry *re;
2794 SIMPLEQ_INIT(&refs);
2795 err = got_ref_list(&refs, repo);
2799 SIMPLEQ_FOREACH(re, &refs, entry) {
2801 refstr = got_ref_to_str(re->ref);
2803 return got_error_from_errno("got_ref_to_str");
2804 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2808 got_ref_list_free(&refs);
2812 static const struct got_error *
2813 delete_ref(struct got_repository *repo, const char *refname)
2815 const struct got_error *err = NULL;
2816 struct got_reference *ref;
2818 err = got_ref_open(&ref, repo, refname, 0);
2822 err = got_ref_delete(ref, repo);
2827 static const struct got_error *
2828 add_ref(struct got_repository *repo, const char *refname, const char *target)
2830 const struct got_error *err = NULL;
2831 struct got_object_id *id;
2832 struct got_reference *ref = NULL;
2835 * Don't let the user create a reference named '-'.
2836 * While technically a valid reference name, this case is usually
2837 * an unintended typo.
2839 if (refname[0] == '-' && refname[1] == '\0')
2840 return got_error(GOT_ERR_BAD_REF_NAME);
2842 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2845 struct got_reference *target_ref;
2847 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2849 err = got_ref_open(&target_ref, repo, target, 0);
2852 err = got_ref_resolve(&id, repo, target_ref);
2853 got_ref_close(target_ref);
2858 err = got_ref_alloc(&ref, refname, id);
2862 err = got_ref_write(ref, repo);
2870 static const struct got_error *
2871 add_symref(struct got_repository *repo, const char *refname, const char *target)
2873 const struct got_error *err = NULL;
2874 struct got_reference *ref = NULL;
2875 struct got_reference *target_ref = NULL;
2878 * Don't let the user create a reference named '-'.
2879 * While technically a valid reference name, this case is usually
2880 * an unintended typo.
2882 if (refname[0] == '-' && refname[1] == '\0')
2883 return got_error(GOT_ERR_BAD_REF_NAME);
2885 err = got_ref_open(&target_ref, repo, target, 0);
2889 err = got_ref_alloc_symref(&ref, refname, target_ref);
2893 err = got_ref_write(ref, repo);
2896 got_ref_close(target_ref);
2902 static const struct got_error *
2903 cmd_ref(int argc, char *argv[])
2905 const struct got_error *error = NULL;
2906 struct got_repository *repo = NULL;
2907 struct got_worktree *worktree = NULL;
2908 char *cwd = NULL, *repo_path = NULL;
2909 int ch, do_list = 0, create_symref = 0;
2910 const char *delref = NULL;
2912 /* TODO: Add -s option for adding symbolic references. */
2913 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
2919 repo_path = realpath(optarg, NULL);
2920 if (repo_path == NULL)
2921 err(1, "-r option");
2922 got_path_strip_trailing_slashes(repo_path);
2936 if (do_list && delref)
2937 errx(1, "-l and -d options are mutually exclusive\n");
2942 if (do_list || delref) {
2944 errx(1, "-s option cannot be used together with the "
2945 "-l or -d options");
2948 } else if (argc != 2)
2953 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2957 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2958 "sendfd unveil", NULL) == -1)
2962 cwd = getcwd(NULL, 0);
2964 error = got_error_from_errno("getcwd");
2968 if (repo_path == NULL) {
2969 error = got_worktree_open(&worktree, cwd);
2970 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2976 strdup(got_worktree_get_repo_path(worktree));
2977 if (repo_path == NULL)
2978 error = got_error_from_errno("strdup");
2982 repo_path = strdup(cwd);
2983 if (repo_path == NULL) {
2984 error = got_error_from_errno("strdup");
2990 error = got_repo_open(&repo, repo_path);
2994 error = apply_unveil(got_repo_get_path(repo), do_list,
2995 worktree ? got_worktree_get_root_path(worktree) : NULL);
3000 error = list_refs(repo);
3002 error = delete_ref(repo, delref);
3003 else if (create_symref)
3004 error = add_symref(repo, argv[0], argv[1]);
3006 error = add_ref(repo, argv[0], argv[1]);
3009 got_repo_close(repo);
3011 got_worktree_close(worktree);
3021 "usage: %s branch [-r repository] -l | -d name | "
3022 "name [base-branch]\n", getprogname());
3026 static const struct got_error *
3027 list_branches(struct got_repository *repo, struct got_worktree *worktree)
3029 static const struct got_error *err = NULL;
3030 struct got_reflist_head refs;
3031 struct got_reflist_entry *re;
3033 SIMPLEQ_INIT(&refs);
3035 err = got_ref_list(&refs, repo);
3039 SIMPLEQ_FOREACH(re, &refs, entry) {
3040 const char *refname, *marker = " ";
3042 refname = got_ref_get_name(re->ref);
3043 if (strncmp(refname, "refs/heads/", 11) != 0)
3045 if (worktree && strcmp(refname,
3046 got_worktree_get_head_ref_name(worktree)) == 0) {
3047 struct got_object_id *id = NULL;
3048 err = got_ref_resolve(&id, repo, re->ref);
3051 if (got_object_id_cmp(id,
3052 got_worktree_get_base_commit_id(worktree)) == 0)
3059 refstr = got_ref_to_str(re->ref);
3061 return got_error_from_errno("got_ref_to_str");
3062 printf("%s%s: %s\n", marker, refname, refstr);
3066 got_ref_list_free(&refs);
3070 static const struct got_error *
3071 delete_branch(struct got_repository *repo, const char *branch_name)
3073 const struct got_error *err = NULL;
3074 struct got_reference *ref;
3077 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3078 return got_error_from_errno("asprintf");
3080 err = got_ref_open(&ref, repo, refname, 0);
3084 err = got_ref_delete(ref, repo);
3091 static const struct got_error *
3092 add_branch(struct got_repository *repo, const char *branch_name,
3093 const char *base_branch)
3095 const struct got_error *err = NULL;
3096 struct got_object_id *id = NULL;
3097 struct got_reference *ref = NULL;
3098 char *base_refname = NULL, *refname = NULL;
3099 struct got_reference *base_ref;
3102 * Don't let the user create a branch named '-'.
3103 * While technically a valid reference name, this case is usually
3104 * an unintended typo.
3106 if (branch_name[0] == '-' && branch_name[1] == '\0')
3107 return got_error(GOT_ERR_BAD_REF_NAME);
3109 if (strcmp(GOT_REF_HEAD, base_branch) == 0) {
3110 base_refname = strdup(GOT_REF_HEAD);
3111 if (base_refname == NULL)
3112 return got_error_from_errno("strdup");
3113 } else if (asprintf(&base_refname, "refs/heads/%s", base_branch) == -1)
3114 return got_error_from_errno("asprintf");
3116 err = got_ref_open(&base_ref, repo, base_refname, 0);
3119 err = got_ref_resolve(&id, repo, base_ref);
3120 got_ref_close(base_ref);
3124 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3125 err = got_error_from_errno("asprintf");
3129 err = got_ref_open(&ref, repo, refname, 0);
3131 err = got_error(GOT_ERR_BRANCH_EXISTS);
3133 } else if (err->code != GOT_ERR_NOT_REF)
3136 err = got_ref_alloc(&ref, refname, id);
3140 err = got_ref_write(ref, repo);
3150 static const struct got_error *
3151 cmd_branch(int argc, char *argv[])
3153 const struct got_error *error = NULL;
3154 struct got_repository *repo = NULL;
3155 struct got_worktree *worktree = NULL;
3156 char *cwd = NULL, *repo_path = NULL;
3157 int ch, do_list = 0;
3158 const char *delref = NULL;
3160 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
3166 repo_path = realpath(optarg, NULL);
3167 if (repo_path == NULL)
3168 err(1, "-r option");
3169 got_path_strip_trailing_slashes(repo_path);
3180 if (do_list && delref)
3181 errx(1, "-l and -d options are mutually exclusive\n");
3186 if (do_list || delref) {
3189 } else if (argc < 1 || argc > 2)
3194 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3198 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3199 "sendfd unveil", NULL) == -1)
3203 cwd = getcwd(NULL, 0);
3205 error = got_error_from_errno("getcwd");
3209 if (repo_path == NULL) {
3210 error = got_worktree_open(&worktree, cwd);
3211 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3217 strdup(got_worktree_get_repo_path(worktree));
3218 if (repo_path == NULL)
3219 error = got_error_from_errno("strdup");
3223 repo_path = strdup(cwd);
3224 if (repo_path == NULL) {
3225 error = got_error_from_errno("strdup");
3231 error = got_repo_open(&repo, repo_path);
3235 error = apply_unveil(got_repo_get_path(repo), do_list,
3236 worktree ? got_worktree_get_root_path(worktree) : NULL);
3241 error = list_branches(repo, worktree);
3243 error = delete_branch(repo, delref);
3245 const char *base_branch;
3247 base_branch = worktree ?
3248 got_worktree_get_head_ref_name(worktree) :
3250 if (strncmp(base_branch, "refs/heads/", 11) == 0)
3253 base_branch = argv[1];
3254 error = add_branch(repo, argv[0], base_branch);
3258 got_repo_close(repo);
3260 got_worktree_close(worktree);
3269 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
3273 static const struct got_error *
3274 cmd_add(int argc, char *argv[])
3276 const struct got_error *error = NULL;
3277 struct got_repository *repo = NULL;
3278 struct got_worktree *worktree = NULL;
3280 struct got_pathlist_head paths;
3281 struct got_pathlist_entry *pe;
3286 while ((ch = getopt(argc, argv, "")) != -1) {
3298 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3305 cwd = getcwd(NULL, 0);
3307 error = got_error_from_errno("getcwd");
3311 error = got_worktree_open(&worktree, cwd);
3315 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3319 error = apply_unveil(got_repo_get_path(repo), 1,
3320 got_worktree_get_root_path(worktree));
3324 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3328 error = got_worktree_schedule_add(worktree, &paths, print_status,
3332 got_repo_close(repo);
3334 got_worktree_close(worktree);
3335 TAILQ_FOREACH(pe, &paths, entry)
3336 free((char *)pe->path);
3337 got_pathlist_free(&paths);
3345 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
3349 static const struct got_error *
3350 cmd_remove(int argc, char *argv[])
3352 const struct got_error *error = NULL;
3353 struct got_worktree *worktree = NULL;
3354 struct got_repository *repo = NULL;
3356 struct got_pathlist_head paths;
3357 struct got_pathlist_entry *pe;
3358 int ch, delete_local_mods = 0;
3362 while ((ch = getopt(argc, argv, "f")) != -1) {
3365 delete_local_mods = 1;
3377 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3384 cwd = getcwd(NULL, 0);
3386 error = got_error_from_errno("getcwd");
3389 error = got_worktree_open(&worktree, cwd);
3393 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3397 error = apply_unveil(got_repo_get_path(repo), 1,
3398 got_worktree_get_root_path(worktree));
3402 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3406 error = got_worktree_schedule_delete(worktree, &paths,
3407 delete_local_mods, print_status, NULL, repo);
3412 got_repo_close(repo);
3414 got_worktree_close(worktree);
3415 TAILQ_FOREACH(pe, &paths, entry)
3416 free((char *)pe->path);
3417 got_pathlist_free(&paths);
3425 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
3426 "path ...\n", getprogname());
3430 static const struct got_error *
3431 revert_progress(void *arg, unsigned char status, const char *path)
3433 while (path[0] == '/')
3435 printf("%c %s\n", status, path);
3439 struct choose_patch_arg {
3440 FILE *patch_script_file;
3444 static const struct got_error *
3445 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
3446 int nchanges, const char *action)
3449 size_t linesize = 0;
3453 case GOT_STATUS_ADD:
3454 printf("A %s\n%s this addition? [y/n] ", path, action);
3456 case GOT_STATUS_DELETE:
3457 printf("D %s\n%s this deletion? [y/n] ", path, action);
3459 case GOT_STATUS_MODIFY:
3460 if (fseek(patch_file, 0L, SEEK_SET) == -1)
3461 return got_error_from_errno("fseek");
3462 printf(GOT_COMMIT_SEP_STR);
3463 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
3465 if (ferror(patch_file))
3466 return got_error_from_errno("getline");
3467 printf(GOT_COMMIT_SEP_STR);
3468 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
3469 path, n, nchanges, action);
3472 return got_error_path(path, GOT_ERR_FILE_STATUS);
3478 static const struct got_error *
3479 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
3480 FILE *patch_file, int n, int nchanges)
3482 const struct got_error *err = NULL;
3484 size_t linesize = 0;
3487 struct choose_patch_arg *a = arg;
3489 *choice = GOT_PATCH_CHOICE_NONE;
3491 if (a->patch_script_file) {
3493 err = show_change(status, path, patch_file, n, nchanges,
3497 linelen = getline(&line, &linesize, a->patch_script_file);
3498 if (linelen == -1) {
3499 if (ferror(a->patch_script_file))
3500 return got_error_from_errno("getline");
3503 nl = strchr(line, '\n');
3506 if (strcmp(line, "y") == 0) {
3507 *choice = GOT_PATCH_CHOICE_YES;
3509 } else if (strcmp(line, "n") == 0) {
3510 *choice = GOT_PATCH_CHOICE_NO;
3512 } else if (strcmp(line, "q") == 0 &&
3513 status == GOT_STATUS_MODIFY) {
3514 *choice = GOT_PATCH_CHOICE_QUIT;
3517 printf("invalid response '%s'\n", line);
3522 while (resp != 'y' && resp != 'n' && resp != 'q') {
3523 err = show_change(status, path, patch_file, n, nchanges,
3530 if (status == GOT_STATUS_MODIFY) {
3531 if (resp != 'y' && resp != 'n' && resp != 'q') {
3532 printf("invalid response '%c'\n", resp);
3535 } else if (resp != 'y' && resp != 'n') {
3536 printf("invalid response '%c'\n", resp);
3542 *choice = GOT_PATCH_CHOICE_YES;
3543 else if (resp == 'n')
3544 *choice = GOT_PATCH_CHOICE_NO;
3545 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
3546 *choice = GOT_PATCH_CHOICE_QUIT;
3552 static const struct got_error *
3553 cmd_revert(int argc, char *argv[])
3555 const struct got_error *error = NULL;
3556 struct got_worktree *worktree = NULL;
3557 struct got_repository *repo = NULL;
3558 char *cwd = NULL, *path = NULL;
3559 struct got_pathlist_head paths;
3560 struct got_pathlist_entry *pe;
3561 int ch, can_recurse = 0, pflag = 0;
3562 FILE *patch_script_file = NULL;
3563 const char *patch_script_path = NULL;
3564 struct choose_patch_arg cpa;
3568 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
3574 patch_script_path = optarg;
3589 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3590 "unveil", NULL) == -1)
3595 if (patch_script_path && !pflag)
3596 errx(1, "-F option can only be used together with -p option");
3598 cwd = getcwd(NULL, 0);
3600 error = got_error_from_errno("getcwd");
3603 error = got_worktree_open(&worktree, cwd);
3607 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3611 if (patch_script_path) {
3612 patch_script_file = fopen(patch_script_path, "r");
3613 if (patch_script_file == NULL) {
3614 error = got_error_from_errno2("fopen",
3619 error = apply_unveil(got_repo_get_path(repo), 1,
3620 got_worktree_get_root_path(worktree));
3624 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3631 TAILQ_FOREACH(pe, &paths, entry) {
3632 if (asprintf(&ondisk_path, "%s/%s",
3633 got_worktree_get_root_path(worktree),
3635 error = got_error_from_errno("asprintf");
3638 if (lstat(ondisk_path, &sb) == -1) {
3639 if (errno == ENOENT) {
3643 error = got_error_from_errno2("lstat",
3649 if (S_ISDIR(sb.st_mode)) {
3650 error = got_error_msg(GOT_ERR_BAD_PATH,
3651 "reverting directories requires -R option");
3657 cpa.patch_script_file = patch_script_file;
3658 cpa.action = "revert";
3659 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
3660 pflag ? choose_patch : NULL, &cpa, repo);
3664 if (patch_script_file && fclose(patch_script_file) == EOF &&
3666 error = got_error_from_errno2("fclose", patch_script_path);
3668 got_repo_close(repo);
3670 got_worktree_close(worktree);
3679 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
3684 struct collect_commit_logmsg_arg {
3685 const char *cmdline_log;
3687 const char *worktree_path;
3688 const char *branch_name;
3689 const char *repo_path;
3694 static const struct got_error *
3695 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
3698 char *initial_content = NULL;
3699 struct got_pathlist_entry *pe;
3700 const struct got_error *err = NULL;
3701 char *template = NULL;
3702 struct collect_commit_logmsg_arg *a = arg;
3706 /* if a message was specified on the command line, just use it */
3707 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
3708 len = strlen(a->cmdline_log) + 1;
3709 *logmsg = malloc(len + 1);
3710 if (*logmsg == NULL)
3711 return got_error_from_errno("malloc");
3712 strlcpy(*logmsg, a->cmdline_log, len);
3716 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
3717 return got_error_from_errno("asprintf");
3719 if (asprintf(&initial_content,
3720 "\n# changes to be committed on branch %s:\n",
3721 a->branch_name) == -1)
3722 return got_error_from_errno("asprintf");
3724 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
3728 dprintf(fd, initial_content);
3730 TAILQ_FOREACH(pe, commitable_paths, entry) {
3731 struct got_commitable *ct = pe->data;
3732 dprintf(fd, "# %c %s\n",
3733 got_commitable_get_status(ct),
3734 got_commitable_get_path(ct));
3738 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
3740 if (err == NULL || err->code == GOT_ERR_COMMIT_MSG_EMPTY) {
3741 unlink(a->logmsg_path);
3742 free(a->logmsg_path);
3743 a->logmsg_path = NULL;
3745 free(initial_content);
3748 /* Editor is done; we can now apply unveil(2) */
3750 err = apply_unveil(a->repo_path, 0, a->worktree_path);
3759 static const struct got_error *
3760 cmd_commit(int argc, char *argv[])
3762 const struct got_error *error = NULL;
3763 struct got_worktree *worktree = NULL;
3764 struct got_repository *repo = NULL;
3765 char *cwd = NULL, *id_str = NULL;
3766 struct got_object_id *id = NULL;
3767 const char *logmsg = NULL;
3769 struct collect_commit_logmsg_arg cl_arg;
3770 char *editor = NULL;
3771 int ch, rebase_in_progress, histedit_in_progress;
3772 struct got_pathlist_head paths;
3775 cl_arg.logmsg_path = NULL;
3777 while ((ch = getopt(argc, argv, "m:")) != -1) {
3792 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3793 "unveil", NULL) == -1)
3796 error = get_author(&author);
3800 cwd = getcwd(NULL, 0);
3802 error = got_error_from_errno("getcwd");
3805 error = got_worktree_open(&worktree, cwd);
3809 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3812 if (rebase_in_progress) {
3813 error = got_error(GOT_ERR_REBASING);
3817 error = got_worktree_histedit_in_progress(&histedit_in_progress,
3822 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3827 * unveil(2) traverses exec(2); if an editor is used we have
3828 * to apply unveil after the log message has been written.
3830 if (logmsg == NULL || strlen(logmsg) == 0)
3831 error = get_editor(&editor);
3833 error = apply_unveil(got_repo_get_path(repo), 0,
3834 got_worktree_get_root_path(worktree));
3838 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3842 cl_arg.editor = editor;
3843 cl_arg.cmdline_log = logmsg;
3844 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
3845 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
3846 if (!histedit_in_progress) {
3847 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
3848 error = got_error(GOT_ERR_COMMIT_BRANCH);
3851 cl_arg.branch_name += 11;
3853 cl_arg.repo_path = got_repo_get_path(repo);
3854 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
3855 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
3857 if (cl_arg.logmsg_path)
3858 fprintf(stderr, "%s: log message preserved in %s\n",
3859 getprogname(), cl_arg.logmsg_path);
3863 if (cl_arg.logmsg_path)
3864 unlink(cl_arg.logmsg_path);
3866 error = got_object_id_str(&id_str, id);
3869 printf("Created commit %s\n", id_str);
3871 free(cl_arg.logmsg_path);
3873 got_repo_close(repo);
3875 got_worktree_close(worktree);
3883 usage_cherrypick(void)
3885 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
3889 static const struct got_error *
3890 cmd_cherrypick(int argc, char *argv[])
3892 const struct got_error *error = NULL;
3893 struct got_worktree *worktree = NULL;
3894 struct got_repository *repo = NULL;
3895 char *cwd = NULL, *commit_id_str = NULL;
3896 struct got_object_id *commit_id = NULL;
3897 struct got_commit_object *commit = NULL;
3898 struct got_object_qid *pid;
3899 struct got_reference *head_ref = NULL;
3900 int ch, did_something = 0;
3902 while ((ch = getopt(argc, argv, "")) != -1) {
3914 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3915 "unveil", NULL) == -1)
3921 cwd = getcwd(NULL, 0);
3923 error = got_error_from_errno("getcwd");
3926 error = got_worktree_open(&worktree, cwd);
3930 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3934 error = apply_unveil(got_repo_get_path(repo), 0,
3935 got_worktree_get_root_path(worktree));
3939 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3940 GOT_OBJ_TYPE_COMMIT, repo);
3941 if (error != NULL) {
3942 struct got_reference *ref;
3943 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3945 error = got_ref_open(&ref, repo, argv[0], 0);
3948 error = got_ref_resolve(&commit_id, repo, ref);
3953 error = got_object_id_str(&commit_id_str, commit_id);
3957 error = got_ref_open(&head_ref, repo,
3958 got_worktree_get_head_ref_name(worktree), 0);
3962 error = check_same_branch(commit_id, head_ref, NULL, repo);
3964 if (error->code != GOT_ERR_ANCESTRY)
3968 error = got_error(GOT_ERR_SAME_BRANCH);
3972 error = got_object_open_as_commit(&commit, repo, commit_id);
3975 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3976 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
3977 commit_id, repo, update_progress, &did_something, check_cancelled,
3983 printf("Merged commit %s\n", commit_id_str);
3986 got_object_commit_close(commit);
3987 free(commit_id_str);
3989 got_ref_close(head_ref);
3991 got_worktree_close(worktree);
3993 got_repo_close(repo);
4000 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
4004 static const struct got_error *
4005 cmd_backout(int argc, char *argv[])
4007 const struct got_error *error = NULL;
4008 struct got_worktree *worktree = NULL;
4009 struct got_repository *repo = NULL;
4010 char *cwd = NULL, *commit_id_str = NULL;
4011 struct got_object_id *commit_id = NULL;
4012 struct got_commit_object *commit = NULL;
4013 struct got_object_qid *pid;
4014 struct got_reference *head_ref = NULL;
4015 int ch, did_something = 0;
4017 while ((ch = getopt(argc, argv, "")) != -1) {
4029 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4030 "unveil", NULL) == -1)
4036 cwd = getcwd(NULL, 0);
4038 error = got_error_from_errno("getcwd");
4041 error = got_worktree_open(&worktree, cwd);
4045 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4049 error = apply_unveil(got_repo_get_path(repo), 0,
4050 got_worktree_get_root_path(worktree));
4054 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4055 GOT_OBJ_TYPE_COMMIT, repo);
4056 if (error != NULL) {
4057 struct got_reference *ref;
4058 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4060 error = got_ref_open(&ref, repo, argv[0], 0);
4063 error = got_ref_resolve(&commit_id, repo, ref);
4068 error = got_object_id_str(&commit_id_str, commit_id);
4072 error = got_ref_open(&head_ref, repo,
4073 got_worktree_get_head_ref_name(worktree), 0);
4077 error = check_same_branch(commit_id, head_ref, NULL, repo);
4081 error = got_object_open_as_commit(&commit, repo, commit_id);
4084 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4086 error = got_error(GOT_ERR_ROOT_COMMIT);
4090 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
4091 update_progress, &did_something, check_cancelled, NULL);
4096 printf("Backed out commit %s\n", commit_id_str);
4099 got_object_commit_close(commit);
4100 free(commit_id_str);
4102 got_ref_close(head_ref);
4104 got_worktree_close(worktree);
4106 got_repo_close(repo);
4113 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
4119 trim_logmsg(char *logmsg, int limit)
4124 len = strlen(logmsg);
4128 nl = strchr(logmsg, '\n');
4133 static const struct got_error *
4134 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
4136 const struct got_error *err;
4137 char *logmsg0 = NULL;
4140 err = got_object_commit_get_logmsg(&logmsg0, commit);
4145 while (isspace((unsigned char)s[0]))
4148 *logmsg = strdup(s);
4149 if (*logmsg == NULL) {
4150 err = got_error_from_errno("strdup");
4154 trim_logmsg(*logmsg, limit);
4160 static const struct got_error *
4161 show_rebase_progress(struct got_commit_object *commit,
4162 struct got_object_id *old_id, struct got_object_id *new_id)
4164 const struct got_error *err;
4165 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
4167 err = got_object_id_str(&old_id_str, old_id);
4172 err = got_object_id_str(&new_id_str, new_id);
4177 old_id_str[12] = '\0';
4179 new_id_str[12] = '\0';
4181 err = get_short_logmsg(&logmsg, 42, commit);
4185 printf("%s -> %s: %s\n", old_id_str,
4186 new_id_str ? new_id_str : "no-op change", logmsg);
4193 static const struct got_error *
4194 rebase_progress(void *arg, unsigned char status, const char *path)
4196 unsigned char *rebase_status = arg;
4198 while (path[0] == '/')
4200 printf("%c %s\n", status, path);
4202 if (*rebase_status == GOT_STATUS_CONFLICT)
4204 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
4205 *rebase_status = status;
4209 static const struct got_error *
4210 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
4211 struct got_reference *branch, struct got_reference *new_base_branch,
4212 struct got_reference *tmp_branch, struct got_repository *repo)
4214 printf("Switching work tree to %s\n", got_ref_get_name(branch));
4215 return got_worktree_rebase_complete(worktree, fileindex,
4216 new_base_branch, tmp_branch, branch, repo);
4219 static const struct got_error *
4220 rebase_commit(struct got_pathlist_head *merged_paths,
4221 struct got_worktree *worktree, struct got_fileindex *fileindex,
4222 struct got_reference *tmp_branch,
4223 struct got_object_id *commit_id, struct got_repository *repo)
4225 const struct got_error *error;
4226 struct got_commit_object *commit;
4227 struct got_object_id *new_commit_id;
4229 error = got_object_open_as_commit(&commit, repo, commit_id);
4233 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
4234 worktree, fileindex, tmp_branch, commit, commit_id, repo);
4236 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
4238 error = show_rebase_progress(commit, commit_id, NULL);
4240 error = show_rebase_progress(commit, commit_id, new_commit_id);
4241 free(new_commit_id);
4244 got_object_commit_close(commit);
4248 struct check_path_prefix_arg {
4249 const char *path_prefix;
4254 static const struct got_error *
4255 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
4256 struct got_blob_object *blob2, struct got_object_id *id1,
4257 struct got_object_id *id2, const char *path1, const char *path2,
4258 struct got_repository *repo)
4260 struct check_path_prefix_arg *a = arg;
4262 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
4263 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
4264 return got_error(a->errcode);
4269 static const struct got_error *
4270 check_path_prefix(struct got_object_id *parent_id,
4271 struct got_object_id *commit_id, const char *path_prefix,
4272 int errcode, struct got_repository *repo)
4274 const struct got_error *err;
4275 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4276 struct got_commit_object *commit = NULL, *parent_commit = NULL;
4277 struct check_path_prefix_arg cpp_arg;
4279 if (got_path_is_root_dir(path_prefix))
4282 err = got_object_open_as_commit(&commit, repo, commit_id);
4286 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
4290 err = got_object_open_as_tree(&tree1, repo,
4291 got_object_commit_get_tree_id(parent_commit));
4295 err = got_object_open_as_tree(&tree2, repo,
4296 got_object_commit_get_tree_id(commit));
4300 cpp_arg.path_prefix = path_prefix;
4301 while (cpp_arg.path_prefix[0] == '/')
4302 cpp_arg.path_prefix++;
4303 cpp_arg.len = strlen(cpp_arg.path_prefix);
4304 cpp_arg.errcode = errcode;
4305 err = got_diff_tree(tree1, tree2, "", "", repo,
4306 check_path_prefix_in_diff, &cpp_arg, 0);
4309 got_object_tree_close(tree1);
4311 got_object_tree_close(tree2);
4313 got_object_commit_close(commit);
4315 got_object_commit_close(parent_commit);
4319 static const struct got_error *
4320 collect_commits(struct got_object_id_queue *commits,
4321 struct got_object_id *initial_commit_id,
4322 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
4323 const char *path_prefix, int path_prefix_errcode,
4324 struct got_repository *repo)
4326 const struct got_error *err = NULL;
4327 struct got_commit_graph *graph = NULL;
4328 struct got_object_id *parent_id = NULL;
4329 struct got_object_qid *qid;
4330 struct got_object_id *commit_id = initial_commit_id;
4332 err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
4336 err = got_commit_graph_iter_start(graph, iter_start_id, repo);
4339 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
4340 err = got_commit_graph_iter_next(&parent_id, graph);
4342 if (err->code == GOT_ERR_ITER_COMPLETED) {
4343 err = got_error_msg(GOT_ERR_ANCESTRY,
4344 "ran out of commits to rebase before "
4345 "youngest common ancestor commit has "
4348 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
4350 err = got_commit_graph_fetch_commits(graph, 1, repo);
4354 err = check_path_prefix(parent_id, commit_id,
4355 path_prefix, path_prefix_errcode, repo);
4359 err = got_object_qid_alloc(&qid, commit_id);
4362 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
4363 commit_id = parent_id;
4367 got_commit_graph_close(graph);
4371 static const struct got_error *
4372 cmd_rebase(int argc, char *argv[])
4374 const struct got_error *error = NULL;
4375 struct got_worktree *worktree = NULL;
4376 struct got_repository *repo = NULL;
4377 struct got_fileindex *fileindex = NULL;
4379 struct got_reference *branch = NULL;
4380 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
4381 struct got_object_id *commit_id = NULL, *parent_id = NULL;
4382 struct got_object_id *resume_commit_id = NULL;
4383 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
4384 struct got_commit_object *commit = NULL;
4385 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
4386 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
4387 struct got_object_id_queue commits;
4388 struct got_pathlist_head merged_paths;
4389 const struct got_object_id_queue *parent_ids;
4390 struct got_object_qid *qid, *pid;
4392 SIMPLEQ_INIT(&commits);
4393 TAILQ_INIT(&merged_paths);
4395 while ((ch = getopt(argc, argv, "ac")) != -1) {
4401 continue_rebase = 1;
4413 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4414 "unveil", NULL) == -1)
4417 if (abort_rebase && continue_rebase)
4419 else if (abort_rebase || continue_rebase) {
4422 } else if (argc != 1)
4425 cwd = getcwd(NULL, 0);
4427 error = got_error_from_errno("getcwd");
4430 error = got_worktree_open(&worktree, cwd);
4434 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4438 error = apply_unveil(got_repo_get_path(repo), 0,
4439 got_worktree_get_root_path(worktree));
4443 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4449 if (!rebase_in_progress) {
4450 error = got_error(GOT_ERR_NOT_REBASING);
4453 error = got_worktree_rebase_continue(&resume_commit_id,
4454 &new_base_branch, &tmp_branch, &branch, &fileindex,
4458 printf("Switching work tree to %s\n",
4459 got_ref_get_symref_target(new_base_branch));
4460 error = got_worktree_rebase_abort(worktree, fileindex, repo,
4461 new_base_branch, update_progress, &did_something);
4464 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
4465 goto done; /* nothing else to do */
4468 if (continue_rebase) {
4469 if (!rebase_in_progress) {
4470 error = got_error(GOT_ERR_NOT_REBASING);
4473 error = got_worktree_rebase_continue(&resume_commit_id,
4474 &new_base_branch, &tmp_branch, &branch, &fileindex,
4479 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
4480 resume_commit_id, repo);
4484 yca_id = got_object_id_dup(resume_commit_id);
4485 if (yca_id == NULL) {
4486 error = got_error_from_errno("got_object_id_dup");
4490 error = got_ref_open(&branch, repo, argv[0], 0);
4495 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
4499 if (!continue_rebase) {
4500 struct got_object_id *base_commit_id;
4502 base_commit_id = got_worktree_get_base_commit_id(worktree);
4503 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
4504 base_commit_id, branch_head_commit_id, repo);
4507 if (yca_id == NULL) {
4508 error = got_error_msg(GOT_ERR_ANCESTRY,
4509 "specified branch shares no common ancestry "
4510 "with work tree's branch");
4514 error = check_same_branch(base_commit_id, branch, yca_id, repo);
4516 if (error->code != GOT_ERR_ANCESTRY)
4520 error = got_error_msg(GOT_ERR_SAME_BRANCH,
4521 "specified branch resolves to a commit which "
4522 "is already contained in work tree's branch");
4525 error = got_worktree_rebase_prepare(&new_base_branch,
4526 &tmp_branch, &fileindex, worktree, branch, repo);
4531 commit_id = branch_head_commit_id;
4532 error = got_object_open_as_commit(&commit, repo, commit_id);
4536 parent_ids = got_object_commit_get_parent_ids(commit);
4537 pid = SIMPLEQ_FIRST(parent_ids);
4539 if (!continue_rebase) {
4541 error = got_worktree_rebase_abort(worktree, fileindex,
4542 repo, new_base_branch, update_progress,
4546 printf("Rebase of %s aborted\n",
4547 got_ref_get_name(branch));
4549 error = got_error(GOT_ERR_EMPTY_REBASE);
4552 error = collect_commits(&commits, commit_id, pid->id,
4553 yca_id, got_worktree_get_path_prefix(worktree),
4554 GOT_ERR_REBASE_PATH, repo);
4555 got_object_commit_close(commit);
4560 if (SIMPLEQ_EMPTY(&commits)) {
4561 if (continue_rebase)
4562 error = rebase_complete(worktree, fileindex,
4563 branch, new_base_branch, tmp_branch, repo);
4565 error = got_error(GOT_ERR_EMPTY_REBASE);
4570 SIMPLEQ_FOREACH(qid, &commits, entry) {
4571 commit_id = qid->id;
4572 parent_id = pid ? pid->id : yca_id;
4575 error = got_worktree_rebase_merge_files(&merged_paths,
4576 worktree, fileindex, parent_id, commit_id, repo,
4577 rebase_progress, &rebase_status, check_cancelled, NULL);
4581 if (rebase_status == GOT_STATUS_CONFLICT) {
4582 got_worktree_rebase_pathlist_free(&merged_paths);
4586 error = rebase_commit(&merged_paths, worktree, fileindex,
4587 tmp_branch, commit_id, repo);
4588 got_worktree_rebase_pathlist_free(&merged_paths);
4593 if (rebase_status == GOT_STATUS_CONFLICT) {
4594 error = got_worktree_rebase_postpone(worktree, fileindex);
4597 error = got_error_msg(GOT_ERR_CONFLICTS,
4598 "conflicts must be resolved before rebasing can continue");
4600 error = rebase_complete(worktree, fileindex, branch,
4601 new_base_branch, tmp_branch, repo);
4603 got_object_id_queue_free(&commits);
4604 free(branch_head_commit_id);
4605 free(resume_commit_id);
4608 got_object_commit_close(commit);
4610 got_ref_close(branch);
4611 if (new_base_branch)
4612 got_ref_close(new_base_branch);
4614 got_ref_close(tmp_branch);
4616 got_worktree_close(worktree);
4618 got_repo_close(repo);
4623 usage_histedit(void)
4625 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script]\n",
4630 #define GOT_HISTEDIT_PICK 'p'
4631 #define GOT_HISTEDIT_EDIT 'e'
4632 #define GOT_HISTEDIT_FOLD 'f'
4633 #define GOT_HISTEDIT_DROP 'd'
4634 #define GOT_HISTEDIT_MESG 'm'
4636 static struct got_histedit_cmd {
4640 } got_histedit_cmds[] = {
4641 { GOT_HISTEDIT_PICK, "pick", "use commit" },
4642 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
4643 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
4644 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
4645 { GOT_HISTEDIT_MESG, "mesg",
4646 "single-line log message for commit above (open editor if empty)" },
4649 struct got_histedit_list_entry {
4650 TAILQ_ENTRY(got_histedit_list_entry) entry;
4651 struct got_object_id *commit_id;
4652 const struct got_histedit_cmd *cmd;
4655 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
4657 static const struct got_error *
4658 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
4659 FILE *f, struct got_repository *repo)
4661 const struct got_error *err = NULL;
4662 char *logmsg = NULL, *id_str = NULL;
4663 struct got_commit_object *commit = NULL;
4666 err = got_object_open_as_commit(&commit, repo, commit_id);
4670 err = get_short_logmsg(&logmsg, 34, commit);
4674 err = got_object_id_str(&id_str, commit_id);
4678 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
4680 err = got_ferror(f, GOT_ERR_IO);
4683 got_object_commit_close(commit);
4689 static const struct got_error *
4690 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
4691 struct got_repository *repo)
4693 const struct got_error *err = NULL;
4694 struct got_object_qid *qid;
4696 if (SIMPLEQ_EMPTY(commits))
4697 return got_error(GOT_ERR_EMPTY_HISTEDIT);
4699 SIMPLEQ_FOREACH(qid, commits, entry) {
4700 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
4709 static const struct got_error *
4710 write_cmd_list(FILE *f)
4712 const struct got_error *err = NULL;
4715 n = fprintf(f, "# Available histedit commands:\n");
4717 return got_ferror(f, GOT_ERR_IO);
4719 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4720 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
4721 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
4724 err = got_ferror(f, GOT_ERR_IO);
4728 n = fprintf(f, "# Commits will be processed in order from top to "
4729 "bottom of this file.\n");
4731 return got_ferror(f, GOT_ERR_IO);
4735 static const struct got_error *
4736 histedit_syntax_error(int lineno)
4738 static char msg[42];
4741 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
4743 if (ret == -1 || ret >= sizeof(msg))
4744 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
4746 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
4749 static const struct got_error *
4750 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
4751 char *logmsg, struct got_repository *repo)
4753 const struct got_error *err;
4754 struct got_commit_object *folded_commit = NULL;
4755 char *id_str, *folded_logmsg = NULL;
4757 err = got_object_id_str(&id_str, hle->commit_id);
4761 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
4765 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
4768 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
4769 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
4770 folded_logmsg) == -1) {
4771 err = got_error_from_errno("asprintf");
4776 got_object_commit_close(folded_commit);
4778 free(folded_logmsg);
4782 static struct got_histedit_list_entry *
4783 get_folded_commits(struct got_histedit_list_entry *hle)
4785 struct got_histedit_list_entry *prev, *folded = NULL;
4787 prev = TAILQ_PREV(hle, got_histedit_list, entry);
4788 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
4789 prev->cmd->code == GOT_HISTEDIT_DROP)) {
4790 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
4792 prev = TAILQ_PREV(prev, got_histedit_list, entry);
4798 static const struct got_error *
4799 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
4800 struct got_repository *repo)
4802 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
4803 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
4804 const struct got_error *err = NULL;
4805 struct got_commit_object *commit = NULL;
4807 struct got_histedit_list_entry *folded = NULL;
4809 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4813 folded = get_folded_commits(hle);
4815 while (folded != hle) {
4816 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
4817 folded = TAILQ_NEXT(folded, entry);
4820 err = append_folded_commit_msg(&new_msg, folded,
4826 folded = TAILQ_NEXT(folded, entry);
4830 err = got_object_id_str(&id_str, hle->commit_id);
4833 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
4836 if (asprintf(&new_msg,
4837 "%s\n# original log message of commit %s: %s",
4838 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
4839 err = got_error_from_errno("asprintf");
4845 err = got_object_id_str(&id_str, hle->commit_id);
4849 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
4853 dprintf(fd, logmsg);
4856 err = get_editor(&editor);
4860 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
4862 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
4864 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
4867 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
4868 err = got_error_from_errno2("unlink", logmsg_path);
4874 got_object_commit_close(commit);
4878 static const struct got_error *
4879 histedit_parse_list(struct got_histedit_list *histedit_cmds,
4880 FILE *f, struct got_repository *repo)
4882 const struct got_error *err = NULL;
4883 char *line = NULL, *p, *end;
4887 const struct got_histedit_cmd *cmd;
4888 struct got_object_id *commit_id = NULL;
4889 struct got_histedit_list_entry *hle = NULL;
4892 len = getline(&line, &size, f);
4894 const struct got_error *getline_err;
4897 getline_err = got_error_from_errno("getline");
4898 err = got_ferror(f, getline_err->code);
4903 while (isspace((unsigned char)p[0]))
4905 if (p[0] == '#' || p[0] == '\0') {
4911 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4912 cmd = &got_histedit_cmds[i];
4913 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
4914 isspace((unsigned char)p[strlen(cmd->name)])) {
4915 p += strlen(cmd->name);
4918 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
4923 if (i == nitems(got_histedit_cmds)) {
4924 err = histedit_syntax_error(lineno);
4927 while (isspace((unsigned char)p[0]))
4929 if (cmd->code == GOT_HISTEDIT_MESG) {
4930 if (hle == NULL || hle->logmsg != NULL) {
4931 err = got_error(GOT_ERR_HISTEDIT_CMD);
4935 err = histedit_edit_logmsg(hle, repo);
4939 hle->logmsg = strdup(p);
4940 if (hle->logmsg == NULL) {
4941 err = got_error_from_errno("strdup");
4950 while (end[0] && !isspace((unsigned char)end[0]))
4954 err = got_object_resolve_id_str(&commit_id, repo, p);
4956 /* override error code */
4957 err = histedit_syntax_error(lineno);
4961 hle = malloc(sizeof(*hle));
4963 err = got_error_from_errno("malloc");
4967 hle->commit_id = commit_id;
4972 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
4980 static const struct got_error *
4981 histedit_check_script(struct got_histedit_list *histedit_cmds,
4982 struct got_object_id_queue *commits, struct got_repository *repo)
4984 const struct got_error *err = NULL;
4985 struct got_object_qid *qid;
4986 struct got_histedit_list_entry *hle;
4987 static char msg[80];
4990 if (TAILQ_EMPTY(histedit_cmds))
4991 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
4992 "histedit script contains no commands");
4993 if (SIMPLEQ_EMPTY(commits))
4994 return got_error(GOT_ERR_EMPTY_HISTEDIT);
4996 SIMPLEQ_FOREACH(qid, commits, entry) {
4997 TAILQ_FOREACH(hle, histedit_cmds, entry) {
4998 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
5002 err = got_object_id_str(&id_str, qid->id);
5005 snprintf(msg, sizeof(msg),
5006 "commit %s missing from histedit script", id_str);
5008 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
5012 if (hle->cmd->code == GOT_HISTEDIT_FOLD)
5013 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
5014 "last commit in histedit script cannot be folded");
5019 static const struct got_error *
5020 histedit_run_editor(struct got_histedit_list *histedit_cmds,
5021 const char *path, struct got_object_id_queue *commits,
5022 struct got_repository *repo)
5024 const struct got_error *err = NULL;
5028 err = get_editor(&editor);
5032 if (spawn_editor(editor, path) == -1) {
5033 err = got_error_from_errno("failed spawning editor");
5037 f = fopen(path, "r");
5039 err = got_error_from_errno("fopen");
5042 err = histedit_parse_list(histedit_cmds, f, repo);
5046 err = histedit_check_script(histedit_cmds, commits, repo);
5048 if (f && fclose(f) != 0 && err == NULL)
5049 err = got_error_from_errno("fclose");
5054 static const struct got_error *
5055 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
5056 struct got_object_id_queue *, const char *, struct got_repository *);
5058 static const struct got_error *
5059 histedit_edit_script(struct got_histedit_list *histedit_cmds,
5060 struct got_object_id_queue *commits, struct got_repository *repo)
5062 const struct got_error *err;
5066 err = got_opentemp_named(&path, &f, "got-histedit");
5070 err = write_cmd_list(f);
5074 err = histedit_write_commit_list(commits, f, repo);
5078 if (fclose(f) != 0) {
5079 err = got_error_from_errno("fclose");
5084 err = histedit_run_editor(histedit_cmds, path, commits, repo);
5086 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5087 err->code != GOT_ERR_HISTEDIT_CMD)
5089 err = histedit_edit_list_retry(histedit_cmds, err,
5090 commits, path, repo);
5093 if (f && fclose(f) != 0 && err == NULL)
5094 err = got_error_from_errno("fclose");
5095 if (path && unlink(path) != 0 && err == NULL)
5096 err = got_error_from_errno2("unlink", path);
5101 static const struct got_error *
5102 histedit_save_list(struct got_histedit_list *histedit_cmds,
5103 struct got_worktree *worktree, struct got_repository *repo)
5105 const struct got_error *err = NULL;
5108 struct got_histedit_list_entry *hle;
5109 struct got_commit_object *commit = NULL;
5111 err = got_worktree_get_histedit_script_path(&path, worktree);
5115 f = fopen(path, "w");
5117 err = got_error_from_errno2("fopen", path);
5120 TAILQ_FOREACH(hle, histedit_cmds, entry) {
5121 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
5127 int n = fprintf(f, "%c %s\n",
5128 GOT_HISTEDIT_MESG, hle->logmsg);
5130 err = got_ferror(f, GOT_ERR_IO);
5136 if (f && fclose(f) != 0 && err == NULL)
5137 err = got_error_from_errno("fclose");
5140 got_object_commit_close(commit);
5145 histedit_free_list(struct got_histedit_list *histedit_cmds)
5147 struct got_histedit_list_entry *hle;
5149 while ((hle = TAILQ_FIRST(histedit_cmds))) {
5150 TAILQ_REMOVE(histedit_cmds, hle, entry);
5155 static const struct got_error *
5156 histedit_load_list(struct got_histedit_list *histedit_cmds,
5157 const char *path, struct got_repository *repo)
5159 const struct got_error *err = NULL;
5162 f = fopen(path, "r");
5164 err = got_error_from_errno2("fopen", path);
5168 err = histedit_parse_list(histedit_cmds, f, repo);
5170 if (f && fclose(f) != 0 && err == NULL)
5171 err = got_error_from_errno("fclose");
5175 static const struct got_error *
5176 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
5177 const struct got_error *edit_err, struct got_object_id_queue *commits,
5178 const char *path, struct got_repository *repo)
5180 const struct got_error *err = NULL, *prev_err = edit_err;
5183 while (resp != 'c' && resp != 'r' && resp != 'a') {
5184 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
5185 "or (a)bort: ", getprogname(), prev_err->msg);
5190 histedit_free_list(histedit_cmds);
5191 err = histedit_run_editor(histedit_cmds, path, commits,
5194 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5195 err->code != GOT_ERR_HISTEDIT_CMD)
5202 } else if (resp == 'r') {
5203 histedit_free_list(histedit_cmds);
5204 err = histedit_edit_script(histedit_cmds,
5207 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5208 err->code != GOT_ERR_HISTEDIT_CMD)
5215 } else if (resp == 'a') {
5216 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
5219 printf("invalid response '%c'\n", resp);
5225 static const struct got_error *
5226 histedit_complete(struct got_worktree *worktree,
5227 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5228 struct got_reference *branch, struct got_repository *repo)
5230 printf("Switching work tree to %s\n",
5231 got_ref_get_symref_target(branch));
5232 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
5236 static const struct got_error *
5237 show_histedit_progress(struct got_commit_object *commit,
5238 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
5240 const struct got_error *err;
5241 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5243 err = got_object_id_str(&old_id_str, hle->commit_id);
5248 err = got_object_id_str(&new_id_str, new_id);
5253 old_id_str[12] = '\0';
5255 new_id_str[12] = '\0';
5258 logmsg = strdup(hle->logmsg);
5259 if (logmsg == NULL) {
5260 err = got_error_from_errno("strdup");
5263 trim_logmsg(logmsg, 42);
5265 err = get_short_logmsg(&logmsg, 42, commit);
5270 switch (hle->cmd->code) {
5271 case GOT_HISTEDIT_PICK:
5272 case GOT_HISTEDIT_EDIT:
5273 printf("%s -> %s: %s\n", old_id_str,
5274 new_id_str ? new_id_str : "no-op change", logmsg);
5276 case GOT_HISTEDIT_DROP:
5277 case GOT_HISTEDIT_FOLD:
5278 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
5291 static const struct got_error *
5292 histedit_commit(struct got_pathlist_head *merged_paths,
5293 struct got_worktree *worktree, struct got_fileindex *fileindex,
5294 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
5295 struct got_repository *repo)
5297 const struct got_error *err;
5298 struct got_commit_object *commit;
5299 struct got_object_id *new_commit_id;
5301 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
5302 && hle->logmsg == NULL) {
5303 err = histedit_edit_logmsg(hle, repo);
5308 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5312 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
5313 worktree, fileindex, tmp_branch, commit, hle->commit_id,
5316 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
5318 err = show_histedit_progress(commit, hle, NULL);
5320 err = show_histedit_progress(commit, hle, new_commit_id);
5321 free(new_commit_id);
5324 got_object_commit_close(commit);
5328 static const struct got_error *
5329 histedit_skip_commit(struct got_histedit_list_entry *hle,
5330 struct got_worktree *worktree, struct got_repository *repo)
5332 const struct got_error *error;
5333 struct got_commit_object *commit;
5335 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
5340 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
5344 error = show_histedit_progress(commit, hle, NULL);
5345 got_object_commit_close(commit);
5349 static const struct got_error *
5350 cmd_histedit(int argc, char *argv[])
5352 const struct got_error *error = NULL;
5353 struct got_worktree *worktree = NULL;
5354 struct got_fileindex *fileindex = NULL;
5355 struct got_repository *repo = NULL;
5357 struct got_reference *branch = NULL;
5358 struct got_reference *tmp_branch = NULL;
5359 struct got_object_id *resume_commit_id = NULL;
5360 struct got_object_id *base_commit_id = NULL;
5361 struct got_object_id *head_commit_id = NULL;
5362 struct got_commit_object *commit = NULL;
5363 int ch, rebase_in_progress = 0, did_something;
5364 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
5365 const char *edit_script_path = NULL;
5366 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5367 struct got_object_id_queue commits;
5368 struct got_pathlist_head merged_paths;
5369 const struct got_object_id_queue *parent_ids;
5370 struct got_object_qid *pid;
5371 struct got_histedit_list histedit_cmds;
5372 struct got_histedit_list_entry *hle;
5374 SIMPLEQ_INIT(&commits);
5375 TAILQ_INIT(&histedit_cmds);
5376 TAILQ_INIT(&merged_paths);
5378 while ((ch = getopt(argc, argv, "acF:")) != -1) {
5387 edit_script_path = optarg;
5399 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5400 "unveil", NULL) == -1)
5403 if (abort_edit && continue_edit)
5409 * This command cannot apply unveil(2) in all cases because the
5410 * user may choose to run an editor to edit the histedit script
5411 * and to edit individual commit log messages.
5412 * unveil(2) traverses exec(2); if an editor is used we have to
5413 * apply unveil after edit script and log messages have been written.
5414 * XXX TODO: Make use of unveil(2) where possible.
5417 cwd = getcwd(NULL, 0);
5419 error = got_error_from_errno("getcwd");
5422 error = got_worktree_open(&worktree, cwd);
5426 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5430 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5433 if (rebase_in_progress) {
5434 error = got_error(GOT_ERR_REBASING);
5438 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
5442 if (edit_in_progress && abort_edit) {
5443 error = got_worktree_histedit_continue(&resume_commit_id,
5444 &tmp_branch, &branch, &base_commit_id, &fileindex,
5448 printf("Switching work tree to %s\n",
5449 got_ref_get_symref_target(branch));
5450 error = got_worktree_histedit_abort(worktree, fileindex, repo,
5451 branch, base_commit_id, update_progress, &did_something);
5454 printf("Histedit of %s aborted\n",
5455 got_ref_get_symref_target(branch));
5456 goto done; /* nothing else to do */
5457 } else if (abort_edit) {
5458 error = got_error(GOT_ERR_NOT_HISTEDIT);
5462 if (continue_edit) {
5465 if (!edit_in_progress) {
5466 error = got_error(GOT_ERR_NOT_HISTEDIT);
5470 error = got_worktree_get_histedit_script_path(&path, worktree);
5474 error = histedit_load_list(&histedit_cmds, path, repo);
5479 error = got_worktree_histedit_continue(&resume_commit_id,
5480 &tmp_branch, &branch, &base_commit_id, &fileindex,
5485 error = got_ref_resolve(&head_commit_id, repo, branch);
5489 error = got_object_open_as_commit(&commit, repo,
5493 parent_ids = got_object_commit_get_parent_ids(commit);
5494 pid = SIMPLEQ_FIRST(parent_ids);
5496 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
5499 error = collect_commits(&commits, head_commit_id, pid->id,
5500 base_commit_id, got_worktree_get_path_prefix(worktree),
5501 GOT_ERR_HISTEDIT_PATH, repo);
5502 got_object_commit_close(commit);
5507 if (edit_in_progress) {
5508 error = got_error(GOT_ERR_HISTEDIT_BUSY);
5512 error = got_ref_open(&branch, repo,
5513 got_worktree_get_head_ref_name(worktree), 0);
5517 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
5518 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
5519 "will not edit commit history of a branch outside "
5520 "the \"refs/heads/\" reference namespace");
5524 error = got_ref_resolve(&head_commit_id, repo, branch);
5525 got_ref_close(branch);
5530 error = got_object_open_as_commit(&commit, repo,
5534 parent_ids = got_object_commit_get_parent_ids(commit);
5535 pid = SIMPLEQ_FIRST(parent_ids);
5537 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
5540 error = collect_commits(&commits, head_commit_id, pid->id,
5541 got_worktree_get_base_commit_id(worktree),
5542 got_worktree_get_path_prefix(worktree),
5543 GOT_ERR_HISTEDIT_PATH, repo);
5544 got_object_commit_close(commit);
5549 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
5550 &base_commit_id, &fileindex, worktree, repo);
5554 if (edit_script_path) {
5555 error = histedit_load_list(&histedit_cmds,
5556 edit_script_path, repo);
5558 got_worktree_histedit_abort(worktree, fileindex,
5559 repo, branch, base_commit_id,
5560 update_progress, &did_something);
5564 error = histedit_edit_script(&histedit_cmds, &commits,
5567 got_worktree_histedit_abort(worktree, fileindex,
5568 repo, branch, base_commit_id,
5569 update_progress, &did_something);
5575 error = histedit_save_list(&histedit_cmds, worktree,
5578 got_worktree_histedit_abort(worktree, fileindex,
5579 repo, branch, base_commit_id,
5580 update_progress, &did_something);
5586 error = histedit_check_script(&histedit_cmds, &commits, repo);
5590 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
5591 if (resume_commit_id) {
5592 if (got_object_id_cmp(hle->commit_id,
5593 resume_commit_id) != 0)
5596 resume_commit_id = NULL;
5597 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
5598 hle->cmd->code == GOT_HISTEDIT_FOLD) {
5599 error = histedit_skip_commit(hle, worktree,
5602 error = histedit_commit(NULL, worktree,
5603 fileindex, tmp_branch, hle, repo);
5610 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
5611 error = histedit_skip_commit(hle, worktree, repo);
5617 error = got_object_open_as_commit(&commit, repo,
5621 parent_ids = got_object_commit_get_parent_ids(commit);
5622 pid = SIMPLEQ_FIRST(parent_ids);
5624 error = got_worktree_histedit_merge_files(&merged_paths,
5625 worktree, fileindex, pid->id, hle->commit_id, repo,
5626 rebase_progress, &rebase_status, check_cancelled, NULL);
5629 got_object_commit_close(commit);
5632 if (rebase_status == GOT_STATUS_CONFLICT) {
5633 got_worktree_rebase_pathlist_free(&merged_paths);
5637 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
5639 error = got_object_id_str(&id_str, hle->commit_id);
5642 printf("Stopping histedit for amending commit %s\n",
5645 got_worktree_rebase_pathlist_free(&merged_paths);
5646 error = got_worktree_histedit_postpone(worktree,
5651 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
5652 error = histedit_skip_commit(hle, worktree, repo);
5658 error = histedit_commit(&merged_paths, worktree, fileindex,
5659 tmp_branch, hle, repo);
5660 got_worktree_rebase_pathlist_free(&merged_paths);
5665 if (rebase_status == GOT_STATUS_CONFLICT) {
5666 error = got_worktree_histedit_postpone(worktree, fileindex);
5669 error = got_error_msg(GOT_ERR_CONFLICTS,
5670 "conflicts must be resolved before rebasing can continue");
5672 error = histedit_complete(worktree, fileindex, tmp_branch,
5675 got_object_id_queue_free(&commits);
5676 histedit_free_list(&histedit_cmds);
5677 free(head_commit_id);
5678 free(base_commit_id);
5679 free(resume_commit_id);
5681 got_object_commit_close(commit);
5683 got_ref_close(branch);
5685 got_ref_close(tmp_branch);
5687 got_worktree_close(worktree);
5689 got_repo_close(repo);
5696 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
5697 "[file-path ...]\n",
5702 static const struct got_error *
5703 print_stage(void *arg, unsigned char status, unsigned char staged_status,
5704 const char *path, struct got_object_id *blob_id,
5705 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
5707 const struct got_error *err = NULL;
5708 char *id_str = NULL;
5710 if (staged_status != GOT_STATUS_ADD &&
5711 staged_status != GOT_STATUS_MODIFY &&
5712 staged_status != GOT_STATUS_DELETE)
5715 if (staged_status == GOT_STATUS_ADD ||
5716 staged_status == GOT_STATUS_MODIFY)
5717 err = got_object_id_str(&id_str, staged_blob_id);
5719 err = got_object_id_str(&id_str, blob_id);
5723 printf("%s %c %s\n", id_str, staged_status, path);
5728 static const struct got_error *
5729 cmd_stage(int argc, char *argv[])
5731 const struct got_error *error = NULL;
5732 struct got_repository *repo = NULL;
5733 struct got_worktree *worktree = NULL;
5735 struct got_pathlist_head paths;
5736 struct got_pathlist_entry *pe;
5737 int ch, list_stage = 0, pflag = 0;
5738 FILE *patch_script_file = NULL;
5739 const char *patch_script_path = NULL;
5740 struct choose_patch_arg cpa;
5744 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
5753 patch_script_path = optarg;
5765 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5766 "unveil", NULL) == -1)
5769 if (list_stage && (pflag || patch_script_path))
5770 errx(1, "-l option cannot be used with other options");
5771 if (patch_script_path && !pflag)
5772 errx(1, "-F option can only be used together with -p option");
5774 cwd = getcwd(NULL, 0);
5776 error = got_error_from_errno("getcwd");
5780 error = got_worktree_open(&worktree, cwd);
5784 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5788 if (patch_script_path) {
5789 patch_script_file = fopen(patch_script_path, "r");
5790 if (patch_script_file == NULL) {
5791 error = got_error_from_errno2("fopen",
5796 error = apply_unveil(got_repo_get_path(repo), 1,
5797 got_worktree_get_root_path(worktree));
5801 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5806 error = got_worktree_status(worktree, &paths, repo,
5807 print_stage, NULL, check_cancelled, NULL);
5809 cpa.patch_script_file = patch_script_file;
5810 cpa.action = "stage";
5811 error = got_worktree_stage(worktree, &paths,
5812 pflag ? NULL : print_status, NULL,
5813 pflag ? choose_patch : NULL, &cpa, repo);
5816 if (patch_script_file && fclose(patch_script_file) == EOF &&
5818 error = got_error_from_errno2("fclose", patch_script_path);
5820 got_repo_close(repo);
5822 got_worktree_close(worktree);
5823 TAILQ_FOREACH(pe, &paths, entry)
5824 free((char *)pe->path);
5825 got_pathlist_free(&paths);
5833 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
5834 "[file-path ...]\n",
5840 static const struct got_error *
5841 cmd_unstage(int argc, char *argv[])
5843 const struct got_error *error = NULL;
5844 struct got_repository *repo = NULL;
5845 struct got_worktree *worktree = NULL;
5847 struct got_pathlist_head paths;
5848 struct got_pathlist_entry *pe;
5849 int ch, did_something = 0, pflag = 0;
5850 FILE *patch_script_file = NULL;
5851 const char *patch_script_path = NULL;
5852 struct choose_patch_arg cpa;
5856 while ((ch = getopt(argc, argv, "pF:")) != -1) {
5862 patch_script_path = optarg;
5874 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5875 "unveil", NULL) == -1)
5878 if (patch_script_path && !pflag)
5879 errx(1, "-F option can only be used together with -p option");
5881 cwd = getcwd(NULL, 0);
5883 error = got_error_from_errno("getcwd");
5887 error = got_worktree_open(&worktree, cwd);
5891 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5895 if (patch_script_path) {
5896 patch_script_file = fopen(patch_script_path, "r");
5897 if (patch_script_file == NULL) {
5898 error = got_error_from_errno2("fopen",
5904 error = apply_unveil(got_repo_get_path(repo), 1,
5905 got_worktree_get_root_path(worktree));
5909 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5913 cpa.patch_script_file = patch_script_file;
5914 cpa.action = "unstage";
5915 error = got_worktree_unstage(worktree, &paths, update_progress,
5916 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
5918 if (patch_script_file && fclose(patch_script_file) == EOF &&
5920 error = got_error_from_errno2("fclose", patch_script_path);
5922 got_repo_close(repo);
5924 got_worktree_close(worktree);
5925 TAILQ_FOREACH(pe, &paths, entry)
5926 free((char *)pe->path);
5927 got_pathlist_free(&paths);