2 * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/queue.h>
19 #include <sys/limits.h>
20 #include <sys/types.h>
22 #include <sys/param.h>
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);
100 static const struct got_error* cmd_init(int, char *[]);
101 static const struct got_error* cmd_import(int, char *[]);
102 static const struct got_error* cmd_checkout(int, char *[]);
103 static const struct got_error* cmd_update(int, char *[]);
104 static const struct got_error* cmd_log(int, char *[]);
105 static const struct got_error* cmd_diff(int, char *[]);
106 static const struct got_error* cmd_blame(int, char *[]);
107 static const struct got_error* cmd_tree(int, char *[]);
108 static const struct got_error* cmd_status(int, char *[]);
109 static const struct got_error* cmd_ref(int, char *[]);
110 static const struct got_error* cmd_branch(int, char *[]);
111 static const struct got_error* cmd_add(int, char *[]);
112 static const struct got_error* cmd_remove(int, char *[]);
113 static const struct got_error* cmd_revert(int, char *[]);
114 static const struct got_error* cmd_commit(int, char *[]);
115 static const struct got_error* cmd_cherrypick(int, char *[]);
116 static const struct got_error* cmd_backout(int, char *[]);
117 static const struct got_error* cmd_rebase(int, char *[]);
118 static const struct got_error* cmd_histedit(int, char *[]);
119 static const struct got_error* cmd_stage(int, char *[]);
121 static struct got_cmd got_commands[] = {
122 { "init", cmd_init, usage_init, "" },
123 { "import", cmd_import, usage_import, "" },
124 { "checkout", cmd_checkout, usage_checkout, "co" },
125 { "update", cmd_update, usage_update, "up" },
126 { "log", cmd_log, usage_log, "" },
127 { "diff", cmd_diff, usage_diff, "" },
128 { "blame", cmd_blame, usage_blame, "" },
129 { "tree", cmd_tree, usage_tree, "" },
130 { "status", cmd_status, usage_status, "st" },
131 { "ref", cmd_ref, usage_ref, "" },
132 { "branch", cmd_branch, usage_branch, "br" },
133 { "add", cmd_add, usage_add, "" },
134 { "remove", cmd_remove, usage_remove, "rm" },
135 { "revert", cmd_revert, usage_revert, "rv" },
136 { "commit", cmd_commit, usage_commit, "ci" },
137 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
138 { "backout", cmd_backout, usage_backout, "bo" },
139 { "rebase", cmd_rebase, usage_rebase, "rb" },
140 { "histedit", cmd_histedit, usage_histedit, "he" },
141 { "stage", cmd_stage, usage_stage, "sg" },
149 fprintf(stderr, "commands:");
150 for (i = 0; i < nitems(got_commands); i++) {
151 struct got_cmd *cmd = &got_commands[i];
152 fprintf(stderr, " %s", cmd->cmd_name);
158 main(int argc, char *argv[])
163 int hflag = 0, Vflag = 0;
165 setlocale(LC_CTYPE, "");
167 while ((ch = getopt(argc, argv, "hV")) != -1) {
186 got_version_print_str();
193 signal(SIGINT, catch_sigint);
194 signal(SIGPIPE, catch_sigpipe);
196 for (i = 0; i < nitems(got_commands); i++) {
197 const struct got_error *error;
199 cmd = &got_commands[i];
201 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
202 strcmp(cmd->cmd_alias, argv[0]) != 0)
206 got_commands[i].cmd_usage();
208 error = got_commands[i].cmd_main(argc, argv);
209 if (error && !(sigint_received || sigpipe_received)) {
210 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
217 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
225 fprintf(stderr, "usage: %s [-h] [-V] command [arg ...]\n",
232 static const struct got_error *
233 get_editor(char **abspath)
235 const struct got_error *err = NULL;
238 editor = getenv("VISUAL");
240 editor = getenv("EDITOR");
243 err = got_path_find_prog(abspath, editor);
248 if (*abspath == NULL) {
249 *abspath = strdup("/bin/ed");
250 if (*abspath == NULL)
251 return got_error_from_errno("strdup");
257 static const struct got_error *
258 apply_unveil(const char *repo_path, int repo_read_only,
259 const char *worktree_path)
261 const struct got_error *err;
264 if (unveil("gmon.out", "rwc") != 0)
265 return got_error_from_errno2("unveil", "gmon.out");
267 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
268 return got_error_from_errno2("unveil", repo_path);
270 if (worktree_path && unveil(worktree_path, "rwc") != 0)
271 return got_error_from_errno2("unveil", worktree_path);
273 if (unveil("/tmp", "rwc") != 0)
274 return got_error_from_errno2("unveil", "/tmp");
276 err = got_privsep_unveil_exec_helpers();
280 if (unveil(NULL, NULL) != 0)
281 return got_error_from_errno("unveil");
289 fprintf(stderr, "usage: %s init repository-path\n", getprogname());
293 static const struct got_error *
294 cmd_init(int argc, char *argv[])
296 const struct got_error *error = NULL;
297 char *repo_path = NULL;
300 while ((ch = getopt(argc, argv, "")) != -1) {
312 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
318 repo_path = strdup(argv[0]);
319 if (repo_path == NULL)
320 return got_error_from_errno("strdup");
322 got_path_strip_trailing_slashes(repo_path);
324 error = got_path_mkdir(repo_path);
326 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
329 error = apply_unveil(repo_path, 0, NULL);
333 error = got_repo_init(repo_path);
345 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
346 "[-r repository-path] [-I pattern] path\n", getprogname());
351 spawn_editor(const char *editor, const char *file)
354 sig_t sighup, sigint, sigquit;
357 sighup = signal(SIGHUP, SIG_IGN);
358 sigint = signal(SIGINT, SIG_IGN);
359 sigquit = signal(SIGQUIT, SIG_IGN);
361 switch (pid = fork()) {
365 execl(editor, editor, file, (char *)NULL);
369 while (waitpid(pid, &st, 0) == -1)
374 (void)signal(SIGHUP, sighup);
375 (void)signal(SIGINT, sigint);
376 (void)signal(SIGQUIT, sigquit);
378 if (!WIFEXITED(st)) {
383 return WEXITSTATUS(st);
386 static const struct got_error *
387 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
388 const char *initial_content)
390 const struct got_error *err = NULL;
394 int content_changed = 0;
399 if (stat(logmsg_path, &st) == -1)
400 return got_error_from_errno2("stat", logmsg_path);
402 if (spawn_editor(editor, logmsg_path) == -1)
403 return got_error_from_errno("failed spawning editor");
405 if (stat(logmsg_path, &st2) == -1)
406 return got_error_from_errno("stat");
408 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
409 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
410 "no changes made to commit message, aborting");
412 *logmsg = malloc(st2.st_size + 1);
414 return got_error_from_errno("malloc");
418 fp = fopen(logmsg_path, "r");
420 err = got_error_from_errno("fopen");
423 while (fgets(buf, sizeof(buf), fp) != NULL) {
424 if (!content_changed && strcmp(buf, initial_content) != 0)
426 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
427 continue; /* remove comments and leading empty lines */
428 len = strlcat(*logmsg, buf, st2.st_size);
432 while (len > 0 && (*logmsg)[len - 1] == '\n') {
433 (*logmsg)[len - 1] = '\0';
437 if (len == 0 || !content_changed)
438 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
439 "commit message cannot be empty, aborting");
448 static const struct got_error *
449 collect_import_msg(char **logmsg, const char *editor, const char *path_dir,
450 const char *branch_name)
452 char *initial_content = NULL, *logmsg_path = NULL;
453 const struct got_error *err = NULL;
456 if (asprintf(&initial_content,
457 "\n# %s to be imported to branch %s\n", path_dir,
459 return got_error_from_errno("asprintf");
461 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-importmsg");
465 dprintf(fd, initial_content);
468 err = edit_logmsg(logmsg, editor, logmsg_path, initial_content);
470 free(initial_content);
475 static const struct got_error *
476 import_progress(void *arg, const char *path)
478 printf("A %s\n", path);
482 static const struct got_error *
483 cmd_import(int argc, char *argv[])
485 const struct got_error *error = NULL;
486 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
488 const char *got_author = getenv("GOT_AUTHOR");
489 const char *branch_name = "master";
490 char *refname = NULL, *id_str = NULL;
491 struct got_repository *repo = NULL;
492 struct got_reference *branch_ref = NULL, *head_ref = NULL;
493 struct got_object_id *new_commit_id = NULL;
495 struct got_pathlist_head ignores;
496 struct got_pathlist_entry *pe;
498 TAILQ_INIT(&ignores);
500 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
503 branch_name = optarg;
506 logmsg = strdup(optarg);
507 if (logmsg == NULL) {
508 error = got_error_from_errno("strdup");
513 repo_path = realpath(optarg, NULL);
514 if (repo_path == NULL) {
515 error = got_error_from_errno("realpath");
520 if (optarg[0] == '\0')
522 error = got_pathlist_insert(&pe, &ignores, optarg,
537 if (pledge("stdio rpath wpath cpath fattr flock proc exec unveil",
544 if (got_author == NULL) {
545 /* TODO: Look current user up in password database */
546 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
550 if (repo_path == NULL) {
551 repo_path = getcwd(NULL, 0);
552 if (repo_path == NULL)
553 return got_error_from_errno("getcwd");
555 got_path_strip_trailing_slashes(repo_path);
556 error = got_repo_open(&repo, repo_path);
560 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
561 error = got_error_from_errno("asprintf");
565 error = got_ref_open(&branch_ref, repo, refname, 0);
567 if (error->code != GOT_ERR_NOT_REF)
570 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
571 "import target branch already exists");
575 path_dir = realpath(argv[0], NULL);
576 if (path_dir == NULL) {
577 error = got_error_from_errno("realpath");
580 got_path_strip_trailing_slashes(path_dir);
583 * unveil(2) traverses exec(2); if an editor is used we have
584 * to apply unveil after the log message has been written.
586 if (logmsg == NULL || strlen(logmsg) == 0) {
587 error = get_editor(&editor);
590 error = collect_import_msg(&logmsg, editor, path_dir, refname);
595 if (unveil(path_dir, "r") != 0)
596 return got_error_from_errno2("unveil", path_dir);
598 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
602 error = got_repo_import(&new_commit_id, path_dir, logmsg,
603 got_author, &ignores, repo, import_progress, NULL);
607 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
611 error = got_ref_write(branch_ref, repo);
615 error = got_object_id_str(&id_str, new_commit_id);
619 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
621 if (error->code != GOT_ERR_NOT_REF)
624 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
629 error = got_ref_write(head_ref, repo);
634 printf("Created branch %s with commit %s\n",
635 got_ref_get_name(branch_ref), id_str);
643 got_ref_close(branch_ref);
645 got_ref_close(head_ref);
652 fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
653 "[-p prefix] repository-path [worktree-path]\n", getprogname());
657 static const struct got_error *
658 checkout_progress(void *arg, unsigned char status, const char *path)
660 char *worktree_path = arg;
662 /* Base commit bump happens silently. */
663 if (status == GOT_STATUS_BUMP_BASE)
666 while (path[0] == '/')
669 printf("%c %s/%s\n", status, worktree_path, path);
673 static const struct got_error *
674 check_cancelled(void *arg)
676 if (sigint_received || sigpipe_received)
677 return got_error(GOT_ERR_CANCELLED);
681 static const struct got_error *
682 check_linear_ancestry(struct got_object_id *commit_id,
683 struct got_object_id *base_commit_id, struct got_repository *repo)
685 const struct got_error *err = NULL;
686 struct got_object_id *yca_id;
688 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
689 commit_id, base_commit_id, repo);
694 return got_error(GOT_ERR_ANCESTRY);
697 * Require a straight line of history between the target commit
698 * and the work tree's base commit.
700 * Non-linear situations such as this require a rebase:
702 * (commit) D F (base_commit)
710 * 'got update' only handles linear cases:
711 * Update forwards in time: A (base/yca) - B - C - D (commit)
712 * Update backwards in time: D (base) - C - B - A (commit/yca)
714 if (got_object_id_cmp(commit_id, yca_id) != 0 &&
715 got_object_id_cmp(base_commit_id, yca_id) != 0)
716 return got_error(GOT_ERR_ANCESTRY);
722 static const struct got_error *
723 check_same_branch(struct got_object_id *commit_id,
724 struct got_reference *head_ref, struct got_object_id *yca_id,
725 struct got_repository *repo)
727 const struct got_error *err = NULL;
728 struct got_commit_graph *graph = NULL;
729 struct got_object_id *head_commit_id = NULL;
730 int is_same_branch = 0;
732 err = got_ref_resolve(&head_commit_id, repo, head_ref);
736 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
740 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
745 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
749 err = got_commit_graph_iter_start(graph, head_commit_id, repo);
754 struct got_object_id *id;
755 err = got_commit_graph_iter_next(&id, graph);
757 if (err->code == GOT_ERR_ITER_COMPLETED) {
760 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
762 err = got_commit_graph_fetch_commits(graph, 1,
769 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
771 if (got_object_id_cmp(id, commit_id) == 0) {
779 got_commit_graph_close(graph);
780 free(head_commit_id);
781 if (!err && !is_same_branch)
782 err = got_error(GOT_ERR_ANCESTRY);
786 static const struct got_error *
787 resolve_commit_arg(struct got_object_id **commit_id,
788 const char *commit_id_arg, struct got_repository *repo)
790 const struct got_error *err;
791 struct got_reference *ref;
793 err = got_ref_open(&ref, repo, commit_id_arg, 0);
795 err = got_ref_resolve(commit_id, repo, ref);
798 if (err->code != GOT_ERR_NOT_REF)
800 err = got_repo_match_object_id_prefix(commit_id,
801 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
806 static const struct got_error *
807 cmd_checkout(int argc, char *argv[])
809 const struct got_error *error = NULL;
810 struct got_repository *repo = NULL;
811 struct got_reference *head_ref = NULL;
812 struct got_worktree *worktree = NULL;
813 char *repo_path = NULL;
814 char *worktree_path = NULL;
815 const char *path_prefix = "";
816 const char *branch_name = GOT_REF_HEAD;
817 char *commit_id_str = NULL;
818 int ch, same_path_prefix;
819 struct got_pathlist_head paths;
823 while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
826 branch_name = optarg;
829 commit_id_str = strdup(optarg);
830 if (commit_id_str == NULL)
831 return got_error_from_errno("strdup");
834 path_prefix = optarg;
846 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
847 "unveil", NULL) == -1)
851 char *cwd, *base, *dotgit;
852 repo_path = realpath(argv[0], NULL);
853 if (repo_path == NULL)
854 return got_error_from_errno2("realpath", argv[0]);
855 cwd = getcwd(NULL, 0);
857 error = got_error_from_errno("getcwd");
860 if (path_prefix[0]) {
861 base = basename(path_prefix);
863 error = got_error_from_errno2("basename",
868 base = basename(repo_path);
870 error = got_error_from_errno2("basename",
875 dotgit = strstr(base, ".git");
878 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
879 error = got_error_from_errno("asprintf");
884 } else if (argc == 2) {
885 repo_path = realpath(argv[0], NULL);
886 if (repo_path == NULL) {
887 error = got_error_from_errno2("realpath", argv[0]);
890 worktree_path = realpath(argv[1], NULL);
891 if (worktree_path == NULL) {
892 if (errno != ENOENT) {
893 error = got_error_from_errno2("realpath",
897 worktree_path = strdup(argv[1]);
898 if (worktree_path == NULL) {
899 error = got_error_from_errno("strdup");
906 got_path_strip_trailing_slashes(repo_path);
907 got_path_strip_trailing_slashes(worktree_path);
909 error = got_repo_open(&repo, repo_path);
913 /* Pre-create work tree path for unveil(2) */
914 error = got_path_mkdir(worktree_path);
916 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR))
918 if (!got_path_dir_is_empty(worktree_path)) {
919 error = got_error_path(worktree_path,
920 GOT_ERR_DIR_NOT_EMPTY);
925 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
929 error = got_ref_open(&head_ref, repo, branch_name, 0);
933 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
934 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
937 error = got_worktree_open(&worktree, worktree_path);
941 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
945 if (!same_path_prefix) {
946 error = got_error(GOT_ERR_PATH_PREFIX);
951 struct got_object_id *commit_id;
952 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
955 error = check_linear_ancestry(commit_id,
956 got_worktree_get_base_commit_id(worktree), repo);
961 error = check_same_branch(commit_id, head_ref, NULL, repo);
964 error = got_worktree_set_base_commit_id(worktree, repo,
971 error = got_pathlist_append(&paths, "", NULL);
974 error = got_worktree_checkout_files(worktree, &paths, repo,
975 checkout_progress, worktree_path, check_cancelled, NULL);
979 printf("Now shut up and hack\n");
982 got_pathlist_free(&paths);
992 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
997 static const struct got_error *
998 update_progress(void *arg, unsigned char status, const char *path)
1000 int *did_something = arg;
1002 if (status == GOT_STATUS_EXISTS)
1007 /* Base commit bump happens silently. */
1008 if (status == GOT_STATUS_BUMP_BASE)
1011 while (path[0] == '/')
1013 printf("%c %s\n", status, path);
1017 static const struct got_error *
1018 switch_head_ref(struct got_reference *head_ref,
1019 struct got_object_id *commit_id, struct got_worktree *worktree,
1020 struct got_repository *repo)
1022 const struct got_error *err = NULL;
1024 int ref_has_moved = 0;
1026 /* Trivial case: switching between two different references. */
1027 if (strcmp(got_ref_get_name(head_ref),
1028 got_worktree_get_head_ref_name(worktree)) != 0) {
1029 printf("Switching work tree from %s to %s\n",
1030 got_worktree_get_head_ref_name(worktree),
1031 got_ref_get_name(head_ref));
1032 return got_worktree_set_head_ref(worktree, head_ref);
1035 err = check_linear_ancestry(commit_id,
1036 got_worktree_get_base_commit_id(worktree), repo);
1038 if (err->code != GOT_ERR_ANCESTRY)
1045 /* Switching to a rebased branch with the same reference name. */
1046 err = got_object_id_str(&base_id_str,
1047 got_worktree_get_base_commit_id(worktree));
1050 printf("Reference %s now points at a different branch\n",
1051 got_worktree_get_head_ref_name(worktree));
1052 printf("Switching work tree from %s to %s\n", base_id_str,
1053 got_worktree_get_head_ref_name(worktree));
1057 static const struct got_error *
1058 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1060 const struct got_error *err;
1063 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1067 return got_error(GOT_ERR_REBASING);
1069 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1073 return got_error(GOT_ERR_HISTEDIT_BUSY);
1078 static const struct got_error *
1079 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1080 char *argv[], struct got_worktree *worktree)
1082 const struct got_error *err;
1089 return got_error_from_errno("strdup");
1090 return got_pathlist_append(paths, path, NULL);
1093 for (i = 0; i < argc; i++) {
1094 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1097 err = got_pathlist_append(paths, path, NULL);
1107 static const struct got_error *
1108 cmd_update(int argc, char *argv[])
1110 const struct got_error *error = NULL;
1111 struct got_repository *repo = NULL;
1112 struct got_worktree *worktree = NULL;
1113 char *worktree_path = NULL;
1114 struct got_object_id *commit_id = NULL;
1115 char *commit_id_str = NULL;
1116 const char *branch_name = NULL;
1117 struct got_reference *head_ref = NULL;
1118 struct got_pathlist_head paths;
1119 struct got_pathlist_entry *pe;
1120 int ch, did_something = 0;
1124 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1127 branch_name = optarg;
1130 commit_id_str = strdup(optarg);
1131 if (commit_id_str == NULL)
1132 return got_error_from_errno("strdup");
1144 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1145 "unveil", NULL) == -1)
1148 worktree_path = getcwd(NULL, 0);
1149 if (worktree_path == NULL) {
1150 error = got_error_from_errno("getcwd");
1153 error = got_worktree_open(&worktree, worktree_path);
1157 error = check_rebase_or_histedit_in_progress(worktree);
1161 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1165 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1169 error = apply_unveil(got_repo_get_path(repo), 0,
1170 got_worktree_get_root_path(worktree));
1174 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1175 got_worktree_get_head_ref_name(worktree), 0);
1178 if (commit_id_str == NULL) {
1179 error = got_ref_resolve(&commit_id, repo, head_ref);
1182 error = got_object_id_str(&commit_id_str, commit_id);
1186 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1187 free(commit_id_str);
1188 commit_id_str = NULL;
1191 error = got_object_id_str(&commit_id_str, commit_id);
1197 struct got_object_id *head_commit_id;
1198 TAILQ_FOREACH(pe, &paths, entry) {
1199 if (pe->path_len == 0)
1201 error = got_error_msg(GOT_ERR_BAD_PATH,
1202 "switching between branches requires that "
1203 "the entire work tree gets updated");
1206 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1209 error = check_linear_ancestry(commit_id, head_commit_id, repo);
1210 free(head_commit_id);
1213 error = check_same_branch(commit_id, head_ref, NULL, repo);
1216 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1220 error = check_linear_ancestry(commit_id,
1221 got_worktree_get_base_commit_id(worktree), repo);
1222 if (error != NULL) {
1223 if (error->code == GOT_ERR_ANCESTRY)
1224 error = got_error(GOT_ERR_BRANCH_MOVED);
1227 error = check_same_branch(commit_id, head_ref, NULL, repo);
1232 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1234 error = got_worktree_set_base_commit_id(worktree, repo,
1240 error = got_worktree_checkout_files(worktree, &paths, repo,
1241 update_progress, &did_something, check_cancelled, NULL);
1246 printf("Updated to commit %s\n", commit_id_str);
1248 printf("Already up-to-date\n");
1250 free(worktree_path);
1251 TAILQ_FOREACH(pe, &paths, entry)
1252 free((char *)pe->path);
1253 got_pathlist_free(&paths);
1255 free(commit_id_str);
1259 static const struct got_error *
1260 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1261 int diff_context, struct got_repository *repo)
1263 const struct got_error *err = NULL;
1264 struct got_tree_object *tree1 = NULL, *tree2;
1265 struct got_object_qid *qid;
1266 char *id_str1 = NULL, *id_str2;
1267 struct got_diff_blob_output_unidiff_arg arg;
1269 err = got_object_open_as_tree(&tree2, repo,
1270 got_object_commit_get_tree_id(commit));
1274 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1276 struct got_commit_object *pcommit;
1278 err = got_object_open_as_commit(&pcommit, repo, qid->id);
1282 err = got_object_open_as_tree(&tree1, repo,
1283 got_object_commit_get_tree_id(pcommit));
1284 got_object_commit_close(pcommit);
1288 err = got_object_id_str(&id_str1, qid->id);
1293 err = got_object_id_str(&id_str2, id);
1297 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1298 arg.diff_context = diff_context;
1299 arg.outfile = stdout;
1300 err = got_diff_tree(tree1, tree2, "", "", repo,
1301 got_diff_blob_output_unidiff, &arg, 1);
1304 got_object_tree_close(tree1);
1305 got_object_tree_close(tree2);
1312 get_datestr(time_t *time, char *datebuf)
1314 char *p, *s = ctime_r(time, datebuf);
1315 p = strchr(s, '\n');
1321 static const struct got_error *
1322 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1323 struct got_repository *repo, int show_patch, int diff_context,
1324 struct got_reflist_head *refs)
1326 const struct got_error *err = NULL;
1327 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1329 time_t committer_time;
1330 const char *author, *committer;
1331 char *refs_str = NULL;
1332 struct got_reflist_entry *re;
1334 SIMPLEQ_FOREACH(re, refs, entry) {
1337 if (got_object_id_cmp(re->id, id) != 0)
1339 name = got_ref_get_name(re->ref);
1340 if (strcmp(name, GOT_REF_HEAD) == 0)
1342 if (strncmp(name, "refs/", 5) == 0)
1344 if (strncmp(name, "got/", 4) == 0)
1346 if (strncmp(name, "heads/", 6) == 0)
1348 if (strncmp(name, "remotes/", 8) == 0)
1351 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1353 err = got_error_from_errno("asprintf");
1359 err = got_object_id_str(&id_str, id);
1363 printf("-----------------------------------------------\n");
1364 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1365 refs_str ? refs_str : "", refs_str ? ")" : "");
1370 printf("from: %s\n", got_object_commit_get_author(commit));
1371 committer_time = got_object_commit_get_committer_time(commit);
1372 datestr = get_datestr(&committer_time, datebuf);
1373 printf("date: %s UTC\n", datestr);
1374 author = got_object_commit_get_author(commit);
1375 committer = got_object_commit_get_committer(commit);
1376 if (strcmp(author, committer) != 0)
1377 printf("via: %s\n", committer);
1378 if (got_object_commit_get_nparents(commit) > 1) {
1379 const struct got_object_id_queue *parent_ids;
1380 struct got_object_qid *qid;
1382 parent_ids = got_object_commit_get_parent_ids(commit);
1383 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1384 err = got_object_id_str(&id_str, qid->id);
1387 printf("parent %d: %s\n", n++, id_str);
1392 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
1393 if (logmsg0 == NULL)
1394 return got_error_from_errno("strdup");
1398 line = strsep(&logmsg, "\n");
1400 printf(" %s\n", line);
1405 err = print_patch(commit, id, diff_context, repo);
1410 if (fflush(stdout) != 0 && err == NULL)
1411 err = got_error_from_errno("fflush");
1415 static const struct got_error *
1416 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1417 char *path, int show_patch, int diff_context, int limit,
1418 int first_parent_traversal, struct got_reflist_head *refs)
1420 const struct got_error *err;
1421 struct got_commit_graph *graph;
1423 err = got_commit_graph_open(&graph, root_id, path,
1424 first_parent_traversal, repo);
1427 err = got_commit_graph_iter_start(graph, root_id, repo);
1431 struct got_commit_object *commit;
1432 struct got_object_id *id;
1434 if (sigint_received || sigpipe_received)
1437 err = got_commit_graph_iter_next(&id, graph);
1439 if (err->code == GOT_ERR_ITER_COMPLETED) {
1443 if (err->code != GOT_ERR_ITER_NEED_MORE)
1445 err = got_commit_graph_fetch_commits(graph, 1, repo);
1454 err = got_object_open_as_commit(&commit, repo, id);
1457 err = print_commit(commit, id, repo, show_patch, diff_context,
1459 got_object_commit_close(commit);
1460 if (err || (limit && --limit == 0))
1464 got_commit_graph_close(graph);
1471 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1472 "[-r repository-path] [path]\n", getprogname());
1476 static const struct got_error *
1477 cmd_log(int argc, char *argv[])
1479 const struct got_error *error;
1480 struct got_repository *repo = NULL;
1481 struct got_worktree *worktree = NULL;
1482 struct got_commit_object *commit = NULL;
1483 struct got_object_id *id = NULL;
1484 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1485 char *start_commit = NULL;
1486 int diff_context = 3, ch;
1487 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1489 struct got_reflist_head refs;
1491 SIMPLEQ_INIT(&refs);
1494 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1500 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
1506 start_commit = optarg;
1509 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1512 err(1, "-C option %s", errstr);
1515 limit = strtonum(optarg, 1, INT_MAX, &errstr);
1517 err(1, "-l option %s", errstr);
1520 first_parent_traversal = 1;
1523 repo_path = realpath(optarg, NULL);
1524 if (repo_path == NULL)
1525 err(1, "-r option");
1526 got_path_strip_trailing_slashes(repo_path);
1537 cwd = getcwd(NULL, 0);
1539 error = got_error_from_errno("getcwd");
1543 error = got_worktree_open(&worktree, cwd);
1544 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1551 error = got_error_from_errno("strdup");
1554 } else if (argc == 1) {
1556 error = got_worktree_resolve_path(&path, worktree,
1561 path = strdup(argv[0]);
1563 error = got_error_from_errno("strdup");
1570 if (repo_path == NULL) {
1571 repo_path = worktree ?
1572 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1574 if (repo_path == NULL) {
1575 error = got_error_from_errno("strdup");
1579 error = got_repo_open(&repo, repo_path);
1583 error = apply_unveil(got_repo_get_path(repo), 1,
1584 worktree ? got_worktree_get_root_path(worktree) : NULL);
1588 if (start_commit == NULL) {
1589 struct got_reference *head_ref;
1590 error = got_ref_open(&head_ref, repo,
1591 worktree ? got_worktree_get_head_ref_name(worktree)
1595 error = got_ref_resolve(&id, repo, head_ref);
1596 got_ref_close(head_ref);
1599 error = got_object_open_as_commit(&commit, repo, id);
1601 struct got_reference *ref;
1602 error = got_ref_open(&ref, repo, start_commit, 0);
1603 if (error == NULL) {
1605 error = got_ref_resolve(&id, repo, ref);
1609 error = got_object_get_type(&obj_type, repo, id);
1612 if (obj_type == GOT_OBJ_TYPE_TAG) {
1613 struct got_tag_object *tag;
1614 error = got_object_open_as_tag(&tag, repo, id);
1617 if (got_object_tag_get_object_type(tag) !=
1618 GOT_OBJ_TYPE_COMMIT) {
1619 got_object_tag_close(tag);
1620 error = got_error(GOT_ERR_OBJ_TYPE);
1624 id = got_object_id_dup(
1625 got_object_tag_get_object_id(tag));
1627 error = got_error_from_errno(
1628 "got_object_id_dup");
1629 got_object_tag_close(tag);
1632 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1633 error = got_error(GOT_ERR_OBJ_TYPE);
1636 error = got_object_open_as_commit(&commit, repo, id);
1640 if (commit == NULL) {
1641 error = got_repo_match_object_id_prefix(&id,
1642 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1650 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1655 path = in_repo_path;
1658 error = got_ref_list(&refs, repo);
1662 error = print_commits(id, repo, path, show_patch,
1663 diff_context, limit, first_parent_traversal, &refs);
1670 got_worktree_close(worktree);
1672 const struct got_error *repo_error;
1673 repo_error = got_repo_close(repo);
1677 got_ref_list_free(&refs);
1684 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1685 "[object1 object2 | path]\n", getprogname());
1689 struct print_diff_arg {
1690 struct got_repository *repo;
1691 struct got_worktree *worktree;
1697 static const struct got_error *
1698 print_diff(void *arg, unsigned char status, unsigned char staged_status,
1699 const char *path, struct got_object_id *blob_id,
1700 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
1702 struct print_diff_arg *a = arg;
1703 const struct got_error *err = NULL;
1704 struct got_blob_object *blob1 = NULL;
1706 char *abspath = NULL;
1709 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1710 status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1713 if (!a->header_shown) {
1714 printf("diff %s %s\n", a->id_str,
1715 got_worktree_get_root_path(a->worktree));
1716 a->header_shown = 1;
1719 if (status != GOT_STATUS_ADD) {
1720 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1726 if (status != GOT_STATUS_DELETE) {
1727 if (asprintf(&abspath, "%s/%s",
1728 got_worktree_get_root_path(a->worktree), path) == -1) {
1729 err = got_error_from_errno("asprintf");
1733 f2 = fopen(abspath, "r");
1735 err = got_error_from_errno2("fopen", abspath);
1738 if (lstat(abspath, &sb) == -1) {
1739 err = got_error_from_errno2("lstat", abspath);
1745 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1749 got_object_blob_close(blob1);
1750 if (f2 && fclose(f2) != 0 && err == NULL)
1751 err = got_error_from_errno("fclose");
1756 static const struct got_error *
1757 cmd_diff(int argc, char *argv[])
1759 const struct got_error *error;
1760 struct got_repository *repo = NULL;
1761 struct got_worktree *worktree = NULL;
1762 char *cwd = NULL, *repo_path = NULL;
1763 struct got_object_id *id1 = NULL, *id2 = NULL;
1764 const char *id_str1 = NULL, *id_str2 = NULL;
1765 char *label1 = NULL, *label2 = NULL;
1767 int diff_context = 3, ch;
1772 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1777 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1780 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1782 err(1, "-C option %s", errstr);
1785 repo_path = realpath(optarg, NULL);
1786 if (repo_path == NULL)
1787 err(1, "-r option");
1788 got_path_strip_trailing_slashes(repo_path);
1799 cwd = getcwd(NULL, 0);
1801 error = got_error_from_errno("getcwd");
1804 error = got_worktree_open(&worktree, cwd);
1805 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1808 if (worktree == NULL) {
1809 error = got_error(GOT_ERR_NOT_WORKTREE);
1814 "-r option can't be used when diffing a work tree");
1815 repo_path = strdup(got_worktree_get_repo_path(worktree));
1816 if (repo_path == NULL) {
1817 error = got_error_from_errno("strdup");
1821 error = got_worktree_resolve_path(&path, worktree,
1828 error = got_error_from_errno("strdup");
1832 } else if (argc == 2) {
1835 if (worktree && repo_path == NULL) {
1837 strdup(got_worktree_get_repo_path(worktree));
1838 if (repo_path == NULL) {
1839 error = got_error_from_errno("strdup");
1846 if (repo_path == NULL) {
1847 repo_path = getcwd(NULL, 0);
1848 if (repo_path == NULL)
1849 return got_error_from_errno("getcwd");
1852 error = got_repo_open(&repo, repo_path);
1857 error = apply_unveil(got_repo_get_path(repo), 1,
1858 worktree ? got_worktree_get_root_path(worktree) : NULL);
1863 struct print_diff_arg arg;
1864 struct got_pathlist_head paths;
1869 error = got_object_id_str(&id_str,
1870 got_worktree_get_base_commit_id(worktree));
1874 arg.worktree = worktree;
1875 arg.diff_context = diff_context;
1876 arg.id_str = id_str;
1877 arg.header_shown = 0;
1879 error = got_pathlist_append(&paths, path, NULL);
1883 error = got_worktree_status(worktree, &paths, repo, print_diff,
1884 &arg, check_cancelled, NULL);
1886 got_pathlist_free(&paths);
1890 error = got_repo_match_object_id_prefix(&id1, id_str1,
1891 GOT_OBJ_TYPE_ANY, repo);
1893 struct got_reference *ref;
1894 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1896 error = got_ref_open(&ref, repo, id_str1, 0);
1899 label1 = strdup(got_ref_get_name(ref));
1900 if (label1 == NULL) {
1901 error = got_error_from_errno("strdup");
1904 error = got_ref_resolve(&id1, repo, ref);
1909 error = got_object_id_str(&label1, id1);
1910 if (label1 == NULL) {
1911 error = got_error_from_errno("strdup");
1916 error = got_repo_match_object_id_prefix(&id2, id_str2,
1917 GOT_OBJ_TYPE_ANY, repo);
1919 struct got_reference *ref;
1920 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1922 error = got_ref_open(&ref, repo, id_str2, 0);
1925 label2 = strdup(got_ref_get_name(ref));
1926 if (label2 == NULL) {
1927 error = got_error_from_errno("strdup");
1930 error = got_ref_resolve(&id2, repo, ref);
1935 error = got_object_id_str(&label2, id2);
1936 if (label2 == NULL) {
1937 error = got_error_from_errno("strdup");
1942 error = got_object_get_type(&type1, repo, id1);
1946 error = got_object_get_type(&type2, repo, id2);
1950 if (type1 != type2) {
1951 error = got_error(GOT_ERR_OBJ_TYPE);
1956 case GOT_OBJ_TYPE_BLOB:
1957 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1958 diff_context, repo, stdout);
1960 case GOT_OBJ_TYPE_TREE:
1961 error = got_diff_objects_as_trees(id1, id2, "", "",
1962 diff_context, repo, stdout);
1964 case GOT_OBJ_TYPE_COMMIT:
1965 printf("diff %s %s\n", label1, label2);
1966 error = got_diff_objects_as_commits(id1, id2, diff_context,
1970 error = got_error(GOT_ERR_OBJ_TYPE);
1980 got_worktree_close(worktree);
1982 const struct got_error *repo_error;
1983 repo_error = got_repo_close(repo);
1994 "usage: %s blame [-c commit] [-r repository-path] path\n",
1999 static const struct got_error *
2000 cmd_blame(int argc, char *argv[])
2002 const struct got_error *error;
2003 struct got_repository *repo = NULL;
2004 struct got_worktree *worktree = NULL;
2005 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2006 struct got_object_id *commit_id = NULL;
2007 char *commit_id_str = NULL;
2011 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2016 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2019 commit_id_str = optarg;
2022 repo_path = realpath(optarg, NULL);
2023 if (repo_path == NULL)
2024 err(1, "-r option");
2025 got_path_strip_trailing_slashes(repo_path);
2041 cwd = getcwd(NULL, 0);
2043 error = got_error_from_errno("getcwd");
2046 if (repo_path == NULL) {
2047 error = got_worktree_open(&worktree, cwd);
2048 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2054 strdup(got_worktree_get_repo_path(worktree));
2055 if (repo_path == NULL)
2056 error = got_error_from_errno("strdup");
2060 repo_path = strdup(cwd);
2061 if (repo_path == NULL) {
2062 error = got_error_from_errno("strdup");
2068 error = got_repo_open(&repo, repo_path);
2072 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2077 const char *prefix = got_worktree_get_path_prefix(worktree);
2078 char *p, *worktree_subdir = cwd +
2079 strlen(got_worktree_get_root_path(worktree));
2080 if (asprintf(&p, "%s%s%s%s%s",
2081 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2082 worktree_subdir, worktree_subdir[0] ? "/" : "",
2084 error = got_error_from_errno("asprintf");
2087 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2090 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2095 if (commit_id_str == NULL) {
2096 struct got_reference *head_ref;
2097 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2100 error = got_ref_resolve(&commit_id, repo, head_ref);
2101 got_ref_close(head_ref);
2105 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2110 error = got_blame(in_repo_path, commit_id, repo, stdout);
2117 got_worktree_close(worktree);
2119 const struct got_error *repo_error;
2120 repo_error = got_repo_close(repo);
2131 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2137 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2138 const char *root_path)
2140 int is_root_path = (strcmp(path, root_path) == 0);
2142 path += strlen(root_path);
2143 while (path[0] == '/')
2146 printf("%s%s%s%s%s\n", id ? id : "", path,
2147 is_root_path ? "" : "/", te->name,
2148 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
2151 static const struct got_error *
2152 print_tree(const char *path, struct got_object_id *commit_id,
2153 int show_ids, int recurse, const char *root_path,
2154 struct got_repository *repo)
2156 const struct got_error *err = NULL;
2157 struct got_object_id *tree_id = NULL;
2158 struct got_tree_object *tree = NULL;
2159 const struct got_tree_entries *entries;
2160 struct got_tree_entry *te;
2162 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2166 err = got_object_open_as_tree(&tree, repo, tree_id);
2169 entries = got_object_tree_get_entries(tree);
2170 te = SIMPLEQ_FIRST(&entries->head);
2174 if (sigint_received || sigpipe_received)
2179 err = got_object_id_str(&id_str, te->id);
2182 if (asprintf(&id, "%s ", id_str) == -1) {
2183 err = got_error_from_errno("asprintf");
2189 print_entry(te, id, path, root_path);
2192 if (recurse && S_ISDIR(te->mode)) {
2194 if (asprintf(&child_path, "%s%s%s", path,
2195 path[0] == '/' && path[1] == '\0' ? "" : "/",
2197 err = got_error_from_errno("asprintf");
2200 err = print_tree(child_path, commit_id, show_ids, 1,
2207 te = SIMPLEQ_NEXT(te, entry);
2211 got_object_tree_close(tree);
2216 static const struct got_error *
2217 cmd_tree(int argc, char *argv[])
2219 const struct got_error *error;
2220 struct got_repository *repo = NULL;
2221 struct got_worktree *worktree = NULL;
2223 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2224 struct got_object_id *commit_id = NULL;
2225 char *commit_id_str = NULL;
2226 int show_ids = 0, recurse = 0;
2230 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2235 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2238 commit_id_str = optarg;
2241 repo_path = realpath(optarg, NULL);
2242 if (repo_path == NULL)
2243 err(1, "-r option");
2244 got_path_strip_trailing_slashes(repo_path);
2268 cwd = getcwd(NULL, 0);
2270 error = got_error_from_errno("getcwd");
2273 if (repo_path == NULL) {
2274 error = got_worktree_open(&worktree, cwd);
2275 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2281 strdup(got_worktree_get_repo_path(worktree));
2282 if (repo_path == NULL)
2283 error = got_error_from_errno("strdup");
2287 repo_path = strdup(cwd);
2288 if (repo_path == NULL) {
2289 error = got_error_from_errno("strdup");
2295 error = got_repo_open(&repo, repo_path);
2299 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2305 char *p, *worktree_subdir = cwd +
2306 strlen(got_worktree_get_root_path(worktree));
2307 if (asprintf(&p, "%s/%s",
2308 got_worktree_get_path_prefix(worktree),
2309 worktree_subdir) == -1) {
2310 error = got_error_from_errno("asprintf");
2313 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2320 if (in_repo_path == NULL) {
2321 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2326 if (commit_id_str == NULL) {
2327 struct got_reference *head_ref;
2328 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2331 error = got_ref_resolve(&commit_id, repo, head_ref);
2332 got_ref_close(head_ref);
2336 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2341 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2342 in_repo_path, repo);
2349 got_worktree_close(worktree);
2351 const struct got_error *repo_error;
2352 repo_error = got_repo_close(repo);
2362 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
2366 static const struct got_error *
2367 print_status(void *arg, unsigned char status, unsigned char staged_status,
2368 const char *path, struct got_object_id *blob_id,
2369 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
2371 if (status == staged_status && (status == GOT_STATUS_DELETE))
2372 status = GOT_STATUS_NO_CHANGE;
2373 printf("%c%c %s\n", status, staged_status, path);
2377 static const struct got_error *
2378 cmd_status(int argc, char *argv[])
2380 const struct got_error *error = NULL;
2381 struct got_repository *repo = NULL;
2382 struct got_worktree *worktree = NULL;
2384 struct got_pathlist_head paths;
2385 struct got_pathlist_entry *pe;
2390 while ((ch = getopt(argc, argv, "")) != -1) {
2402 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2406 cwd = getcwd(NULL, 0);
2408 error = got_error_from_errno("getcwd");
2412 error = got_worktree_open(&worktree, cwd);
2416 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2420 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2424 error = apply_unveil(got_repo_get_path(repo), 1,
2425 got_worktree_get_root_path(worktree));
2429 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
2430 check_cancelled, NULL);
2432 TAILQ_FOREACH(pe, &paths, entry)
2433 free((char *)pe->path);
2434 got_pathlist_free(&paths);
2443 "usage: %s ref [-r repository] -l | -d name | name target\n",
2448 static const struct got_error *
2449 list_refs(struct got_repository *repo)
2451 static const struct got_error *err = NULL;
2452 struct got_reflist_head refs;
2453 struct got_reflist_entry *re;
2455 SIMPLEQ_INIT(&refs);
2456 err = got_ref_list(&refs, repo);
2460 SIMPLEQ_FOREACH(re, &refs, entry) {
2462 refstr = got_ref_to_str(re->ref);
2464 return got_error_from_errno("got_ref_to_str");
2465 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2469 got_ref_list_free(&refs);
2473 static const struct got_error *
2474 delete_ref(struct got_repository *repo, const char *refname)
2476 const struct got_error *err = NULL;
2477 struct got_reference *ref;
2479 err = got_ref_open(&ref, repo, refname, 0);
2483 err = got_ref_delete(ref, repo);
2488 static const struct got_error *
2489 add_ref(struct got_repository *repo, const char *refname, const char *target)
2491 const struct got_error *err = NULL;
2492 struct got_object_id *id;
2493 struct got_reference *ref = NULL;
2496 * Don't let the user create a reference named '-'.
2497 * While technically a valid reference name, this case is usually
2498 * an unintended typo.
2500 if (refname[0] == '-' && refname[1] == '\0')
2501 return got_error(GOT_ERR_BAD_REF_NAME);
2503 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2506 struct got_reference *target_ref;
2508 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2510 err = got_ref_open(&target_ref, repo, target, 0);
2513 err = got_ref_resolve(&id, repo, target_ref);
2514 got_ref_close(target_ref);
2519 err = got_ref_alloc(&ref, refname, id);
2523 err = got_ref_write(ref, repo);
2531 static const struct got_error *
2532 cmd_ref(int argc, char *argv[])
2534 const struct got_error *error = NULL;
2535 struct got_repository *repo = NULL;
2536 struct got_worktree *worktree = NULL;
2537 char *cwd = NULL, *repo_path = NULL;
2538 int ch, do_list = 0;
2539 const char *delref = NULL;
2541 /* TODO: Add -s option for adding symbolic references. */
2542 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2548 repo_path = realpath(optarg, NULL);
2549 if (repo_path == NULL)
2550 err(1, "-r option");
2551 got_path_strip_trailing_slashes(repo_path);
2562 if (do_list && delref)
2563 errx(1, "-l and -d options are mutually exclusive\n");
2568 if (do_list || delref) {
2571 } else if (argc != 2)
2576 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2580 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2581 "sendfd unveil", NULL) == -1)
2585 cwd = getcwd(NULL, 0);
2587 error = got_error_from_errno("getcwd");
2591 if (repo_path == NULL) {
2592 error = got_worktree_open(&worktree, cwd);
2593 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2599 strdup(got_worktree_get_repo_path(worktree));
2600 if (repo_path == NULL)
2601 error = got_error_from_errno("strdup");
2605 repo_path = strdup(cwd);
2606 if (repo_path == NULL) {
2607 error = got_error_from_errno("strdup");
2613 error = got_repo_open(&repo, repo_path);
2617 error = apply_unveil(got_repo_get_path(repo), do_list,
2618 worktree ? got_worktree_get_root_path(worktree) : NULL);
2623 error = list_refs(repo);
2625 error = delete_ref(repo, delref);
2627 error = add_ref(repo, argv[0], argv[1]);
2630 got_repo_close(repo);
2632 got_worktree_close(worktree);
2642 "usage: %s branch [-r repository] -l | -d name | "
2643 "name [base-branch]\n", getprogname());
2647 static const struct got_error *
2648 list_branches(struct got_repository *repo, struct got_worktree *worktree)
2650 static const struct got_error *err = NULL;
2651 struct got_reflist_head refs;
2652 struct got_reflist_entry *re;
2654 SIMPLEQ_INIT(&refs);
2656 err = got_ref_list(&refs, repo);
2660 SIMPLEQ_FOREACH(re, &refs, entry) {
2661 const char *refname, *marker = " ";
2663 refname = got_ref_get_name(re->ref);
2664 if (strncmp(refname, "refs/heads/", 11) != 0)
2666 if (worktree && strcmp(refname,
2667 got_worktree_get_head_ref_name(worktree)) == 0) {
2668 struct got_object_id *id = NULL;
2669 err = got_ref_resolve(&id, repo, re->ref);
2672 if (got_object_id_cmp(id,
2673 got_worktree_get_base_commit_id(worktree)) == 0)
2680 refstr = got_ref_to_str(re->ref);
2682 return got_error_from_errno("got_ref_to_str");
2683 printf("%s%s: %s\n", marker, refname, refstr);
2687 got_ref_list_free(&refs);
2691 static const struct got_error *
2692 delete_branch(struct got_repository *repo, const char *branch_name)
2694 const struct got_error *err = NULL;
2695 struct got_reference *ref;
2698 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
2699 return got_error_from_errno("asprintf");
2701 err = got_ref_open(&ref, repo, refname, 0);
2705 err = got_ref_delete(ref, repo);
2712 static const struct got_error *
2713 add_branch(struct got_repository *repo, const char *branch_name,
2714 const char *base_branch)
2716 const struct got_error *err = NULL;
2717 struct got_object_id *id = NULL;
2718 struct got_reference *ref = NULL;
2719 char *base_refname = NULL, *refname = NULL;
2720 struct got_reference *base_ref;
2723 * Don't let the user create a branch named '-'.
2724 * While technically a valid reference name, this case is usually
2725 * an unintended typo.
2727 if (branch_name[0] == '-' && branch_name[1] == '\0')
2728 return got_error(GOT_ERR_BAD_REF_NAME);
2730 if (strcmp(GOT_REF_HEAD, base_branch) == 0) {
2731 base_refname = strdup(GOT_REF_HEAD);
2732 if (base_refname == NULL)
2733 return got_error_from_errno("strdup");
2734 } else if (asprintf(&base_refname, "refs/heads/%s", base_branch) == -1)
2735 return got_error_from_errno("asprintf");
2737 err = got_ref_open(&base_ref, repo, base_refname, 0);
2740 err = got_ref_resolve(&id, repo, base_ref);
2741 got_ref_close(base_ref);
2745 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
2746 err = got_error_from_errno("asprintf");
2750 err = got_ref_open(&ref, repo, refname, 0);
2752 err = got_error(GOT_ERR_BRANCH_EXISTS);
2754 } else if (err->code != GOT_ERR_NOT_REF)
2757 err = got_ref_alloc(&ref, refname, id);
2761 err = got_ref_write(ref, repo);
2771 static const struct got_error *
2772 cmd_branch(int argc, char *argv[])
2774 const struct got_error *error = NULL;
2775 struct got_repository *repo = NULL;
2776 struct got_worktree *worktree = NULL;
2777 char *cwd = NULL, *repo_path = NULL;
2778 int ch, do_list = 0;
2779 const char *delref = NULL;
2781 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2787 repo_path = realpath(optarg, NULL);
2788 if (repo_path == NULL)
2789 err(1, "-r option");
2790 got_path_strip_trailing_slashes(repo_path);
2801 if (do_list && delref)
2802 errx(1, "-l and -d options are mutually exclusive\n");
2807 if (do_list || delref) {
2810 } else if (argc < 1 || argc > 2)
2815 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2819 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2820 "sendfd unveil", NULL) == -1)
2824 cwd = getcwd(NULL, 0);
2826 error = got_error_from_errno("getcwd");
2830 if (repo_path == NULL) {
2831 error = got_worktree_open(&worktree, cwd);
2832 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2838 strdup(got_worktree_get_repo_path(worktree));
2839 if (repo_path == NULL)
2840 error = got_error_from_errno("strdup");
2844 repo_path = strdup(cwd);
2845 if (repo_path == NULL) {
2846 error = got_error_from_errno("strdup");
2852 error = got_repo_open(&repo, repo_path);
2856 error = apply_unveil(got_repo_get_path(repo), do_list,
2857 worktree ? got_worktree_get_root_path(worktree) : NULL);
2862 error = list_branches(repo, worktree);
2864 error = delete_branch(repo, delref);
2866 const char *base_branch;
2868 base_branch = worktree ?
2869 got_worktree_get_head_ref_name(worktree) :
2871 if (strncmp(base_branch, "refs/heads/", 11) == 0)
2874 base_branch = argv[1];
2875 error = add_branch(repo, argv[0], base_branch);
2879 got_repo_close(repo);
2881 got_worktree_close(worktree);
2890 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
2894 static const struct got_error *
2895 cmd_add(int argc, char *argv[])
2897 const struct got_error *error = NULL;
2898 struct got_repository *repo = NULL;
2899 struct got_worktree *worktree = NULL;
2901 struct got_pathlist_head paths;
2902 struct got_pathlist_entry *pe;
2907 while ((ch = getopt(argc, argv, "")) != -1) {
2919 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2926 cwd = getcwd(NULL, 0);
2928 error = got_error_from_errno("getcwd");
2932 error = got_worktree_open(&worktree, cwd);
2936 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2940 error = apply_unveil(got_repo_get_path(repo), 1,
2941 got_worktree_get_root_path(worktree));
2945 for (x = 0; x < argc; x++) {
2946 char *path = realpath(argv[x], NULL);
2948 error = got_error_from_errno2("realpath", argv[x]);
2952 got_path_strip_trailing_slashes(path);
2953 error = got_pathlist_insert(&pe, &paths, path, NULL);
2959 error = got_worktree_schedule_add(worktree, &paths, print_status,
2963 got_repo_close(repo);
2965 got_worktree_close(worktree);
2966 TAILQ_FOREACH(pe, &paths, entry)
2967 free((char *)pe->path);
2968 got_pathlist_free(&paths);
2976 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
2980 static const struct got_error *
2981 cmd_remove(int argc, char *argv[])
2983 const struct got_error *error = NULL;
2984 struct got_worktree *worktree = NULL;
2985 struct got_repository *repo = NULL;
2987 struct got_pathlist_head paths;
2988 struct got_pathlist_entry *pe;
2989 int ch, i, delete_local_mods = 0;
2993 while ((ch = getopt(argc, argv, "f")) != -1) {
2996 delete_local_mods = 1;
3008 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3015 cwd = getcwd(NULL, 0);
3017 error = got_error_from_errno("getcwd");
3020 error = got_worktree_open(&worktree, cwd);
3024 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3028 error = apply_unveil(got_repo_get_path(repo), 1,
3029 got_worktree_get_root_path(worktree));
3033 for (i = 0; i < argc; i++) {
3034 char *path = realpath(argv[i], NULL);
3036 error = got_error_from_errno2("realpath", argv[i]);
3040 got_path_strip_trailing_slashes(path);
3041 error = got_pathlist_insert(&pe, &paths, path, NULL);
3047 error = got_worktree_schedule_delete(worktree, &paths,
3048 delete_local_mods, print_status, NULL, repo);
3053 got_repo_close(repo);
3055 got_worktree_close(worktree);
3056 TAILQ_FOREACH(pe, &paths, entry)
3057 free((char *)pe->path);
3058 got_pathlist_free(&paths);
3066 fprintf(stderr, "usage: %s revert file-path ...\n", getprogname());
3070 static const struct got_error *
3071 revert_progress(void *arg, unsigned char status, const char *path)
3073 while (path[0] == '/')
3075 printf("%c %s\n", status, path);
3079 static const struct got_error *
3080 cmd_revert(int argc, char *argv[])
3082 const struct got_error *error = NULL;
3083 struct got_worktree *worktree = NULL;
3084 struct got_repository *repo = NULL;
3085 char *cwd = NULL, *path = NULL;
3086 struct got_pathlist_head paths;
3087 struct got_pathlist_entry *pe;
3088 const char *worktree_path;
3093 while ((ch = getopt(argc, argv, "")) != -1) {
3105 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3106 "unveil", NULL) == -1)
3112 cwd = getcwd(NULL, 0);
3114 error = got_error_from_errno("getcwd");
3117 error = got_worktree_open(&worktree, cwd);
3121 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3125 error = apply_unveil(got_repo_get_path(repo), 1,
3126 got_worktree_get_root_path(worktree));
3130 worktree_path = got_worktree_get_root_path(worktree);
3131 for (i = 0; i < argc; i++) {
3132 char *path = realpath(argv[i], NULL);
3134 if (errno != ENOENT) {
3135 error = got_error_from_errno2("realpath",
3139 if (got_path_is_child(argv[i], worktree_path,
3140 strlen(worktree_path))) {
3141 path = strdup(argv[i]);
3143 error = got_error_from_errno("strdup");
3147 } else if (asprintf(&path, "%s/%s",
3148 got_worktree_get_root_path(worktree),
3150 error = got_error_from_errno("asprintf");
3155 got_path_strip_trailing_slashes(path);
3156 error = got_pathlist_insert(&pe, &paths, path, NULL);
3163 error = got_worktree_revert(worktree, &paths,
3164 revert_progress, NULL, repo);
3169 got_repo_close(repo);
3171 got_worktree_close(worktree);
3180 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
3185 struct collect_commit_logmsg_arg {
3186 const char *cmdline_log;
3188 const char *worktree_path;
3189 const char *branch_name;
3190 const char *repo_path;
3195 static const struct got_error *
3196 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
3199 char *initial_content = NULL;
3200 struct got_pathlist_entry *pe;
3201 const struct got_error *err = NULL;
3202 char *template = NULL;
3203 struct collect_commit_logmsg_arg *a = arg;
3207 /* if a message was specified on the command line, just use it */
3208 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
3209 len = strlen(a->cmdline_log) + 1;
3210 *logmsg = malloc(len + 1);
3211 if (*logmsg == NULL)
3212 return got_error_from_errno("malloc");
3213 strlcpy(*logmsg, a->cmdline_log, len);
3217 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
3218 return got_error_from_errno("asprintf");
3220 if (asprintf(&initial_content,
3221 "\n# changes to be committed on branch %s:\n",
3222 a->branch_name) == -1)
3223 return got_error_from_errno("asprintf");
3225 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
3229 dprintf(fd, initial_content);
3231 TAILQ_FOREACH(pe, commitable_paths, entry) {
3232 struct got_commitable *ct = pe->data;
3233 dprintf(fd, "# %c %s\n",
3234 got_commitable_get_status(ct),
3235 got_commitable_get_path(ct));
3239 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
3241 unlink(a->logmsg_path);
3242 free(a->logmsg_path);
3243 free(initial_content);
3246 /* Editor is done; we can now apply unveil(2) */
3248 err = apply_unveil(a->repo_path, 0, a->worktree_path);
3257 static const struct got_error *
3258 cmd_commit(int argc, char *argv[])
3260 const struct got_error *error = NULL;
3261 struct got_worktree *worktree = NULL;
3262 struct got_repository *repo = NULL;
3263 char *cwd = NULL, *id_str = NULL;
3264 struct got_object_id *id = NULL;
3265 const char *logmsg = NULL;
3266 const char *got_author = getenv("GOT_AUTHOR");
3267 struct collect_commit_logmsg_arg cl_arg;
3268 char *editor = NULL;
3269 int ch, rebase_in_progress, histedit_in_progress;
3270 struct got_pathlist_head paths;
3274 while ((ch = getopt(argc, argv, "m:")) != -1) {
3289 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3290 "unveil", NULL) == -1)
3293 if (got_author == NULL) {
3294 /* TODO: Look current user up in password database */
3295 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
3299 cwd = getcwd(NULL, 0);
3301 error = got_error_from_errno("getcwd");
3304 error = got_worktree_open(&worktree, cwd);
3308 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3311 if (rebase_in_progress) {
3312 error = got_error(GOT_ERR_REBASING);
3316 error = got_worktree_histedit_in_progress(&histedit_in_progress,
3321 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3325 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3330 * unveil(2) traverses exec(2); if an editor is used we have
3331 * to apply unveil after the log message has been written.
3333 if (logmsg == NULL || strlen(logmsg) == 0)
3334 error = get_editor(&editor);
3336 error = apply_unveil(got_repo_get_path(repo), 0,
3337 got_worktree_get_root_path(worktree));
3341 cl_arg.editor = editor;
3342 cl_arg.cmdline_log = logmsg;
3343 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
3344 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
3345 if (!histedit_in_progress) {
3346 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
3347 error = got_error(GOT_ERR_COMMIT_BRANCH);
3350 cl_arg.branch_name += 11;
3352 cl_arg.repo_path = got_repo_get_path(repo);
3353 cl_arg.logmsg_path = NULL;
3354 error = got_worktree_commit(&id, worktree, &paths, got_author, NULL,
3355 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
3357 if (cl_arg.logmsg_path)
3358 fprintf(stderr, "%s: log message preserved in %s\n",
3359 getprogname(), cl_arg.logmsg_path);
3363 if (cl_arg.logmsg_path)
3364 unlink(cl_arg.logmsg_path);
3366 error = got_object_id_str(&id_str, id);
3369 printf("Created commit %s\n", id_str);
3372 got_repo_close(repo);
3374 got_worktree_close(worktree);
3382 usage_cherrypick(void)
3384 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
3388 static const struct got_error *
3389 cmd_cherrypick(int argc, char *argv[])
3391 const struct got_error *error = NULL;
3392 struct got_worktree *worktree = NULL;
3393 struct got_repository *repo = NULL;
3394 char *cwd = NULL, *commit_id_str = NULL;
3395 struct got_object_id *commit_id = NULL;
3396 struct got_commit_object *commit = NULL;
3397 struct got_object_qid *pid;
3398 struct got_reference *head_ref = NULL;
3399 int ch, did_something = 0;
3401 while ((ch = getopt(argc, argv, "")) != -1) {
3413 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3414 "unveil", NULL) == -1)
3420 cwd = getcwd(NULL, 0);
3422 error = got_error_from_errno("getcwd");
3425 error = got_worktree_open(&worktree, cwd);
3429 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3433 error = apply_unveil(got_repo_get_path(repo), 0,
3434 got_worktree_get_root_path(worktree));
3438 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3439 GOT_OBJ_TYPE_COMMIT, repo);
3440 if (error != NULL) {
3441 struct got_reference *ref;
3442 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3444 error = got_ref_open(&ref, repo, argv[0], 0);
3447 error = got_ref_resolve(&commit_id, repo, ref);
3452 error = got_object_id_str(&commit_id_str, commit_id);
3456 error = got_ref_open(&head_ref, repo,
3457 got_worktree_get_head_ref_name(worktree), 0);
3461 error = check_same_branch(commit_id, head_ref, NULL, repo);
3463 if (error->code != GOT_ERR_ANCESTRY)
3467 error = got_error(GOT_ERR_SAME_BRANCH);
3471 error = got_object_open_as_commit(&commit, repo, commit_id);
3474 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3475 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
3476 commit_id, repo, update_progress, &did_something, check_cancelled,
3482 printf("Merged commit %s\n", commit_id_str);
3485 got_object_commit_close(commit);
3486 free(commit_id_str);
3488 got_ref_close(head_ref);
3490 got_worktree_close(worktree);
3492 got_repo_close(repo);
3499 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
3503 static const struct got_error *
3504 cmd_backout(int argc, char *argv[])
3506 const struct got_error *error = NULL;
3507 struct got_worktree *worktree = NULL;
3508 struct got_repository *repo = NULL;
3509 char *cwd = NULL, *commit_id_str = NULL;
3510 struct got_object_id *commit_id = NULL;
3511 struct got_commit_object *commit = NULL;
3512 struct got_object_qid *pid;
3513 struct got_reference *head_ref = NULL;
3514 int ch, did_something = 0;
3516 while ((ch = getopt(argc, argv, "")) != -1) {
3528 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3529 "unveil", NULL) == -1)
3535 cwd = getcwd(NULL, 0);
3537 error = got_error_from_errno("getcwd");
3540 error = got_worktree_open(&worktree, cwd);
3544 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3548 error = apply_unveil(got_repo_get_path(repo), 0,
3549 got_worktree_get_root_path(worktree));
3553 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3554 GOT_OBJ_TYPE_COMMIT, repo);
3555 if (error != NULL) {
3556 struct got_reference *ref;
3557 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3559 error = got_ref_open(&ref, repo, argv[0], 0);
3562 error = got_ref_resolve(&commit_id, repo, ref);
3567 error = got_object_id_str(&commit_id_str, commit_id);
3571 error = got_ref_open(&head_ref, repo,
3572 got_worktree_get_head_ref_name(worktree), 0);
3576 error = check_same_branch(commit_id, head_ref, NULL, repo);
3580 error = got_object_open_as_commit(&commit, repo, commit_id);
3583 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3585 error = got_error(GOT_ERR_ROOT_COMMIT);
3589 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
3590 update_progress, &did_something, check_cancelled, NULL);
3595 printf("Backed out commit %s\n", commit_id_str);
3598 got_object_commit_close(commit);
3599 free(commit_id_str);
3601 got_ref_close(head_ref);
3603 got_worktree_close(worktree);
3605 got_repo_close(repo);
3612 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
3618 trim_logmsg(char *logmsg, int limit)
3623 len = strlen(logmsg);
3627 nl = strchr(logmsg, '\n');
3632 static const struct got_error *
3633 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
3635 const char *logmsg0 = NULL;
3637 logmsg0 = got_object_commit_get_logmsg(commit);
3639 while (isspace((unsigned char)logmsg0[0]))
3642 *logmsg = strdup(logmsg0);
3643 if (*logmsg == NULL)
3644 return got_error_from_errno("strdup");
3646 trim_logmsg(*logmsg, limit);
3650 static const struct got_error *
3651 show_rebase_progress(struct got_commit_object *commit,
3652 struct got_object_id *old_id, struct got_object_id *new_id)
3654 const struct got_error *err;
3655 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
3657 err = got_object_id_str(&old_id_str, old_id);
3662 err = got_object_id_str(&new_id_str, new_id);
3667 old_id_str[12] = '\0';
3669 new_id_str[12] = '\0';
3671 err = get_short_logmsg(&logmsg, 42, commit);
3675 printf("%s -> %s: %s\n", old_id_str,
3676 new_id_str ? new_id_str : "no-op change", logmsg);
3683 static const struct got_error *
3684 rebase_progress(void *arg, unsigned char status, const char *path)
3686 unsigned char *rebase_status = arg;
3688 while (path[0] == '/')
3690 printf("%c %s\n", status, path);
3692 if (*rebase_status == GOT_STATUS_CONFLICT)
3694 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
3695 *rebase_status = status;
3699 static const struct got_error *
3700 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
3701 struct got_reference *branch, struct got_reference *new_base_branch,
3702 struct got_reference *tmp_branch, struct got_repository *repo)
3704 printf("Switching work tree to %s\n", got_ref_get_name(branch));
3705 return got_worktree_rebase_complete(worktree, fileindex,
3706 new_base_branch, tmp_branch, branch, repo);
3709 static const struct got_error *
3710 rebase_commit(struct got_pathlist_head *merged_paths,
3711 struct got_worktree *worktree, struct got_fileindex *fileindex,
3712 struct got_reference *tmp_branch,
3713 struct got_object_id *commit_id, struct got_repository *repo)
3715 const struct got_error *error;
3716 struct got_commit_object *commit;
3717 struct got_object_id *new_commit_id;
3719 error = got_object_open_as_commit(&commit, repo, commit_id);
3723 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
3724 worktree, fileindex, tmp_branch, commit, commit_id, repo);
3726 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
3728 error = show_rebase_progress(commit, commit_id, NULL);
3730 error = show_rebase_progress(commit, commit_id, new_commit_id);
3731 free(new_commit_id);
3734 got_object_commit_close(commit);
3738 struct check_path_prefix_arg {
3739 const char *path_prefix;
3744 static const struct got_error *
3745 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
3746 struct got_blob_object *blob2, struct got_object_id *id1,
3747 struct got_object_id *id2, const char *path1, const char *path2,
3748 struct got_repository *repo)
3750 struct check_path_prefix_arg *a = arg;
3752 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
3753 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
3754 return got_error(a->errcode);
3759 static const struct got_error *
3760 check_path_prefix(struct got_object_id *parent_id,
3761 struct got_object_id *commit_id, const char *path_prefix,
3762 int errcode, struct got_repository *repo)
3764 const struct got_error *err;
3765 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3766 struct got_commit_object *commit = NULL, *parent_commit = NULL;
3767 struct check_path_prefix_arg cpp_arg;
3769 if (got_path_is_root_dir(path_prefix))
3772 err = got_object_open_as_commit(&commit, repo, commit_id);
3776 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
3780 err = got_object_open_as_tree(&tree1, repo,
3781 got_object_commit_get_tree_id(parent_commit));
3785 err = got_object_open_as_tree(&tree2, repo,
3786 got_object_commit_get_tree_id(commit));
3790 cpp_arg.path_prefix = path_prefix;
3791 while (cpp_arg.path_prefix[0] == '/')
3792 cpp_arg.path_prefix++;
3793 cpp_arg.len = strlen(cpp_arg.path_prefix);
3794 cpp_arg.errcode = errcode;
3795 err = got_diff_tree(tree1, tree2, "", "", repo,
3796 check_path_prefix_in_diff, &cpp_arg, 0);
3799 got_object_tree_close(tree1);
3801 got_object_tree_close(tree2);
3803 got_object_commit_close(commit);
3805 got_object_commit_close(parent_commit);
3809 static const struct got_error *
3810 collect_commits(struct got_object_id_queue *commits,
3811 struct got_object_id *initial_commit_id,
3812 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
3813 const char *path_prefix, int path_prefix_errcode,
3814 struct got_repository *repo)
3816 const struct got_error *err = NULL;
3817 struct got_commit_graph *graph = NULL;
3818 struct got_object_id *parent_id = NULL;
3819 struct got_object_qid *qid;
3820 struct got_object_id *commit_id = initial_commit_id;
3822 err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
3826 err = got_commit_graph_iter_start(graph, iter_start_id, repo);
3829 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
3830 err = got_commit_graph_iter_next(&parent_id, graph);
3832 if (err->code == GOT_ERR_ITER_COMPLETED) {
3833 err = got_error_msg(GOT_ERR_ANCESTRY,
3834 "ran out of commits to rebase before "
3835 "youngest common ancestor commit has "
3838 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
3840 err = got_commit_graph_fetch_commits(graph, 1, repo);
3844 err = check_path_prefix(parent_id, commit_id,
3845 path_prefix, path_prefix_errcode, repo);
3849 err = got_object_qid_alloc(&qid, commit_id);
3852 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
3853 commit_id = parent_id;
3857 got_commit_graph_close(graph);
3861 static const struct got_error *
3862 cmd_rebase(int argc, char *argv[])
3864 const struct got_error *error = NULL;
3865 struct got_worktree *worktree = NULL;
3866 struct got_repository *repo = NULL;
3867 struct got_fileindex *fileindex = NULL;
3869 struct got_reference *branch = NULL;
3870 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
3871 struct got_object_id *commit_id = NULL, *parent_id = NULL;
3872 struct got_object_id *resume_commit_id = NULL;
3873 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
3874 struct got_commit_object *commit = NULL;
3875 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
3876 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
3877 struct got_object_id_queue commits;
3878 struct got_pathlist_head merged_paths;
3879 const struct got_object_id_queue *parent_ids;
3880 struct got_object_qid *qid, *pid;
3882 SIMPLEQ_INIT(&commits);
3883 TAILQ_INIT(&merged_paths);
3885 while ((ch = getopt(argc, argv, "ac")) != -1) {
3891 continue_rebase = 1;
3903 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3904 "unveil", NULL) == -1)
3907 if (abort_rebase && continue_rebase)
3909 else if (abort_rebase || continue_rebase) {
3912 } else if (argc != 1)
3915 cwd = getcwd(NULL, 0);
3917 error = got_error_from_errno("getcwd");
3920 error = got_worktree_open(&worktree, cwd);
3924 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3928 error = apply_unveil(got_repo_get_path(repo), 0,
3929 got_worktree_get_root_path(worktree));
3933 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3939 if (!rebase_in_progress) {
3940 error = got_error(GOT_ERR_NOT_REBASING);
3943 error = got_worktree_rebase_continue(&resume_commit_id,
3944 &new_base_branch, &tmp_branch, &branch, &fileindex,
3948 printf("Switching work tree to %s\n",
3949 got_ref_get_symref_target(new_base_branch));
3950 error = got_worktree_rebase_abort(worktree, fileindex, repo,
3951 new_base_branch, update_progress, &did_something);
3954 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
3955 goto done; /* nothing else to do */
3958 if (continue_rebase) {
3959 if (!rebase_in_progress) {
3960 error = got_error(GOT_ERR_NOT_REBASING);
3963 error = got_worktree_rebase_continue(&resume_commit_id,
3964 &new_base_branch, &tmp_branch, &branch, &fileindex,
3969 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
3970 resume_commit_id, repo);
3974 yca_id = got_object_id_dup(resume_commit_id);
3975 if (yca_id == NULL) {
3976 error = got_error_from_errno("got_object_id_dup");
3980 error = got_ref_open(&branch, repo, argv[0], 0);
3985 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
3989 if (!continue_rebase) {
3990 struct got_object_id *base_commit_id;
3992 base_commit_id = got_worktree_get_base_commit_id(worktree);
3993 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
3994 base_commit_id, branch_head_commit_id, repo);
3997 if (yca_id == NULL) {
3998 error = got_error_msg(GOT_ERR_ANCESTRY,
3999 "specified branch shares no common ancestry "
4000 "with work tree's branch");
4004 error = check_same_branch(base_commit_id, branch, yca_id, repo);
4006 if (error->code != GOT_ERR_ANCESTRY)
4010 error = got_error_msg(GOT_ERR_SAME_BRANCH,
4011 "specified branch resolves to a commit which "
4012 "is already contained in work tree's branch");
4015 error = got_worktree_rebase_prepare(&new_base_branch,
4016 &tmp_branch, &fileindex, worktree, branch, repo);
4021 commit_id = branch_head_commit_id;
4022 error = got_object_open_as_commit(&commit, repo, commit_id);
4026 parent_ids = got_object_commit_get_parent_ids(commit);
4027 pid = SIMPLEQ_FIRST(parent_ids);
4028 error = collect_commits(&commits, commit_id, pid->id,
4029 yca_id, got_worktree_get_path_prefix(worktree),
4030 GOT_ERR_REBASE_PATH, repo);
4031 got_object_commit_close(commit);
4036 if (SIMPLEQ_EMPTY(&commits)) {
4037 if (continue_rebase)
4038 error = rebase_complete(worktree, fileindex,
4039 branch, new_base_branch, tmp_branch, repo);
4041 error = got_error(GOT_ERR_EMPTY_REBASE);
4046 SIMPLEQ_FOREACH(qid, &commits, entry) {
4047 commit_id = qid->id;
4048 parent_id = pid ? pid->id : yca_id;
4051 error = got_worktree_rebase_merge_files(&merged_paths,
4052 worktree, fileindex, parent_id, commit_id, repo,
4053 rebase_progress, &rebase_status, check_cancelled, NULL);
4057 if (rebase_status == GOT_STATUS_CONFLICT) {
4058 got_worktree_rebase_pathlist_free(&merged_paths);
4062 error = rebase_commit(&merged_paths, worktree, fileindex,
4063 tmp_branch, commit_id, repo);
4064 got_worktree_rebase_pathlist_free(&merged_paths);
4069 if (rebase_status == GOT_STATUS_CONFLICT) {
4070 error = got_worktree_rebase_postpone(worktree, fileindex);
4073 error = got_error_msg(GOT_ERR_CONFLICTS,
4074 "conflicts must be resolved before rebasing can continue");
4076 error = rebase_complete(worktree, fileindex, branch,
4077 new_base_branch, tmp_branch, repo);
4079 got_object_id_queue_free(&commits);
4080 free(branch_head_commit_id);
4081 free(resume_commit_id);
4084 got_object_commit_close(commit);
4086 got_ref_close(branch);
4087 if (new_base_branch)
4088 got_ref_close(new_base_branch);
4090 got_ref_close(tmp_branch);
4092 got_worktree_close(worktree);
4094 got_repo_close(repo);
4099 usage_histedit(void)
4101 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F path]\n",
4106 #define GOT_HISTEDIT_PICK 'p'
4107 #define GOT_HISTEDIT_EDIT 'e'
4108 #define GOT_HISTEDIT_FOLD 'f'
4109 #define GOT_HISTEDIT_DROP 'd'
4110 #define GOT_HISTEDIT_MESG 'm'
4112 static struct got_histedit_cmd {
4116 } got_histedit_cmds[] = {
4117 { GOT_HISTEDIT_PICK, "pick", "use commit" },
4118 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
4119 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
4120 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
4121 { GOT_HISTEDIT_MESG, "mesg",
4122 "single-line log message for commit above (open editor if empty)" },
4125 struct got_histedit_list_entry {
4126 TAILQ_ENTRY(got_histedit_list_entry) entry;
4127 struct got_object_id *commit_id;
4128 const struct got_histedit_cmd *cmd;
4131 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
4133 static const struct got_error *
4134 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
4135 FILE *f, struct got_repository *repo)
4137 const struct got_error *err = NULL;
4138 char *logmsg = NULL, *id_str = NULL;
4139 struct got_commit_object *commit = NULL;
4142 err = got_object_open_as_commit(&commit, repo, commit_id);
4146 err = get_short_logmsg(&logmsg, 34, commit);
4150 err = got_object_id_str(&id_str, commit_id);
4154 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
4156 err = got_ferror(f, GOT_ERR_IO);
4159 got_object_commit_close(commit);
4165 static const struct got_error *
4166 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
4167 struct got_repository *repo)
4169 const struct got_error *err = NULL;
4170 struct got_object_qid *qid;
4172 if (SIMPLEQ_EMPTY(commits))
4173 return got_error(GOT_ERR_EMPTY_HISTEDIT);
4175 SIMPLEQ_FOREACH(qid, commits, entry) {
4176 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
4185 static const struct got_error *
4186 write_cmd_list(FILE *f)
4188 const struct got_error *err = NULL;
4191 n = fprintf(f, "# Available histedit commands:\n");
4193 return got_ferror(f, GOT_ERR_IO);
4195 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4196 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
4197 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
4200 err = got_ferror(f, GOT_ERR_IO);
4204 n = fprintf(f, "# Commits will be processed in order from top to "
4205 "bottom of this file.\n");
4207 return got_ferror(f, GOT_ERR_IO);
4211 static const struct got_error *
4212 histedit_syntax_error(int lineno)
4214 static char msg[42];
4217 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
4219 if (ret == -1 || ret >= sizeof(msg))
4220 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
4222 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
4225 static const struct got_error *
4226 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
4227 char *logmsg, struct got_repository *repo)
4229 const struct got_error *err;
4230 struct got_commit_object *folded_commit = NULL;
4233 err = got_object_id_str(&id_str, hle->commit_id);
4237 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
4241 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
4242 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
4243 got_object_commit_get_logmsg(folded_commit)) == -1) {
4244 err = got_error_from_errno("asprintf");
4249 got_object_commit_close(folded_commit);
4254 static struct got_histedit_list_entry *
4255 get_folded_commits(struct got_histedit_list_entry *hle)
4257 struct got_histedit_list_entry *prev, *folded = NULL;
4259 prev = TAILQ_PREV(hle, got_histedit_list, entry);
4260 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
4261 prev->cmd->code == GOT_HISTEDIT_DROP)) {
4262 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
4264 prev = TAILQ_PREV(prev, got_histedit_list, entry);
4270 static const struct got_error *
4271 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
4272 struct got_repository *repo)
4274 char *logmsg_path = NULL, *id_str = NULL;
4275 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
4276 const struct got_error *err = NULL;
4277 struct got_commit_object *commit = NULL;
4279 struct got_histedit_list_entry *folded = NULL;
4281 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4285 folded = get_folded_commits(hle);
4287 while (folded != hle) {
4288 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
4289 folded = TAILQ_NEXT(folded, entry);
4292 err = append_folded_commit_msg(&new_msg, folded,
4298 folded = TAILQ_NEXT(folded, entry);
4302 err = got_object_id_str(&id_str, hle->commit_id);
4305 if (asprintf(&new_msg,
4306 "%s\n# original log message of commit %s: %s",
4307 logmsg ? logmsg : "", id_str,
4308 got_object_commit_get_logmsg(commit)) == -1) {
4309 err = got_error_from_errno("asprintf");
4315 err = got_object_id_str(&id_str, hle->commit_id);
4319 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
4323 dprintf(fd, logmsg);
4326 err = get_editor(&editor);
4330 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
4332 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
4335 hle->logmsg = strdup(got_object_commit_get_logmsg(commit));
4336 if (hle->logmsg == NULL)
4337 err = got_error_from_errno("strdup");
4340 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
4341 err = got_error_from_errno2("unlink", logmsg_path);
4346 got_object_commit_close(commit);
4350 static const struct got_error *
4351 histedit_parse_list(struct got_histedit_list *histedit_cmds,
4352 FILE *f, struct got_repository *repo)
4354 const struct got_error *err = NULL;
4355 char *line = NULL, *p, *end;
4359 const struct got_histedit_cmd *cmd;
4360 struct got_object_id *commit_id = NULL;
4361 struct got_histedit_list_entry *hle = NULL;
4364 len = getline(&line, &size, f);
4366 const struct got_error *getline_err;
4369 getline_err = got_error_from_errno("getline");
4370 err = got_ferror(f, getline_err->code);
4375 while (isspace((unsigned char)p[0]))
4377 if (p[0] == '#' || p[0] == '\0') {
4383 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4384 cmd = &got_histedit_cmds[i];
4385 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
4386 isspace((unsigned char)p[strlen(cmd->name)])) {
4387 p += strlen(cmd->name);
4390 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
4395 if (i == nitems(got_histedit_cmds)) {
4396 err = histedit_syntax_error(lineno);
4399 while (isspace((unsigned char)p[0]))
4401 if (cmd->code == GOT_HISTEDIT_MESG) {
4402 if (hle == NULL || hle->logmsg != NULL) {
4403 err = got_error(GOT_ERR_HISTEDIT_CMD);
4407 err = histedit_edit_logmsg(hle, repo);
4411 hle->logmsg = strdup(p);
4412 if (hle->logmsg == NULL) {
4413 err = got_error_from_errno("strdup");
4422 while (end[0] && !isspace((unsigned char)end[0]))
4426 err = got_object_resolve_id_str(&commit_id, repo, p);
4428 /* override error code */
4429 err = histedit_syntax_error(lineno);
4433 hle = malloc(sizeof(*hle));
4435 err = got_error_from_errno("malloc");
4439 hle->commit_id = commit_id;
4444 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
4452 static const struct got_error *
4453 histedit_check_script(struct got_histedit_list *histedit_cmds,
4454 struct got_object_id_queue *commits, struct got_repository *repo)
4456 const struct got_error *err = NULL;
4457 struct got_object_qid *qid;
4458 struct got_histedit_list_entry *hle;
4459 static char msg[80];
4462 if (TAILQ_EMPTY(histedit_cmds))
4463 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
4464 "histedit script contains no commands");
4466 SIMPLEQ_FOREACH(qid, commits, entry) {
4467 TAILQ_FOREACH(hle, histedit_cmds, entry) {
4468 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
4472 err = got_object_id_str(&id_str, qid->id);
4475 snprintf(msg, sizeof(msg),
4476 "commit %s missing from histedit script", id_str);
4478 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
4482 if (hle->cmd->code == GOT_HISTEDIT_FOLD)
4483 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
4484 "last commit in histedit script cannot be folded");
4489 static const struct got_error *
4490 histedit_run_editor(struct got_histedit_list *histedit_cmds,
4491 const char *path, struct got_object_id_queue *commits,
4492 struct got_repository *repo)
4494 const struct got_error *err = NULL;
4498 err = get_editor(&editor);
4502 if (spawn_editor(editor, path) == -1) {
4503 err = got_error_from_errno("failed spawning editor");
4507 f = fopen(path, "r");
4509 err = got_error_from_errno("fopen");
4512 err = histedit_parse_list(histedit_cmds, f, repo);
4516 err = histedit_check_script(histedit_cmds, commits, repo);
4518 if (f && fclose(f) != 0 && err == NULL)
4519 err = got_error_from_errno("fclose");
4524 static const struct got_error *
4525 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
4526 struct got_object_id_queue *, const char *, struct got_repository *);
4528 static const struct got_error *
4529 histedit_edit_script(struct got_histedit_list *histedit_cmds,
4530 struct got_object_id_queue *commits, struct got_repository *repo)
4532 const struct got_error *err;
4536 err = got_opentemp_named(&path, &f, "got-histedit");
4540 err = write_cmd_list(f);
4544 err = histedit_write_commit_list(commits, f, repo);
4548 if (fclose(f) != 0) {
4549 err = got_error_from_errno("fclose");
4554 err = histedit_run_editor(histedit_cmds, path, commits, repo);
4556 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
4557 err->code != GOT_ERR_HISTEDIT_CMD)
4559 err = histedit_edit_list_retry(histedit_cmds, err,
4560 commits, path, repo);
4563 if (f && fclose(f) != 0 && err == NULL)
4564 err = got_error_from_errno("fclose");
4565 if (path && unlink(path) != 0 && err == NULL)
4566 err = got_error_from_errno2("unlink", path);
4571 static const struct got_error *
4572 histedit_save_list(struct got_histedit_list *histedit_cmds,
4573 struct got_worktree *worktree, struct got_repository *repo)
4575 const struct got_error *err = NULL;
4578 struct got_histedit_list_entry *hle;
4579 struct got_commit_object *commit = NULL;
4581 err = got_worktree_get_histedit_script_path(&path, worktree);
4585 f = fopen(path, "w");
4587 err = got_error_from_errno2("fopen", path);
4590 TAILQ_FOREACH(hle, histedit_cmds, entry) {
4591 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
4597 int n = fprintf(f, "%c %s\n",
4598 GOT_HISTEDIT_MESG, hle->logmsg);
4600 err = got_ferror(f, GOT_ERR_IO);
4606 if (f && fclose(f) != 0 && err == NULL)
4607 err = got_error_from_errno("fclose");
4610 got_object_commit_close(commit);
4615 histedit_free_list(struct got_histedit_list *histedit_cmds)
4617 struct got_histedit_list_entry *hle;
4619 while ((hle = TAILQ_FIRST(histedit_cmds))) {
4620 TAILQ_REMOVE(histedit_cmds, hle, entry);
4625 static const struct got_error *
4626 histedit_load_list(struct got_histedit_list *histedit_cmds,
4627 const char *path, struct got_repository *repo)
4629 const struct got_error *err = NULL;
4632 f = fopen(path, "r");
4634 err = got_error_from_errno2("fopen", path);
4638 err = histedit_parse_list(histedit_cmds, f, repo);
4640 if (f && fclose(f) != 0 && err == NULL)
4641 err = got_error_from_errno("fclose");
4645 static const struct got_error *
4646 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
4647 const struct got_error *edit_err, struct got_object_id_queue *commits,
4648 const char *path, struct got_repository *repo)
4650 const struct got_error *err = NULL, *prev_err = edit_err;
4653 while (resp != 'c' && resp != 'r' && resp != 'a') {
4654 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
4655 "or (a)bort: ", getprogname(), prev_err->msg);
4658 histedit_free_list(histedit_cmds);
4659 err = histedit_run_editor(histedit_cmds, path, commits,
4662 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
4663 err->code != GOT_ERR_HISTEDIT_CMD)
4670 } else if (resp == 'r') {
4671 histedit_free_list(histedit_cmds);
4672 err = histedit_edit_script(histedit_cmds,
4675 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
4676 err->code != GOT_ERR_HISTEDIT_CMD)
4683 } else if (resp == 'a') {
4684 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
4687 printf("invalid response '%c'\n", resp);
4693 static const struct got_error *
4694 histedit_complete(struct got_worktree *worktree,
4695 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4696 struct got_reference *branch, struct got_repository *repo)
4698 printf("Switching work tree to %s\n",
4699 got_ref_get_symref_target(branch));
4700 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
4704 static const struct got_error *
4705 show_histedit_progress(struct got_commit_object *commit,
4706 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
4708 const struct got_error *err;
4709 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
4711 err = got_object_id_str(&old_id_str, hle->commit_id);
4716 err = got_object_id_str(&new_id_str, new_id);
4721 old_id_str[12] = '\0';
4723 new_id_str[12] = '\0';
4726 logmsg = strdup(hle->logmsg);
4727 if (logmsg == NULL) {
4728 err = got_error_from_errno("strdup");
4731 trim_logmsg(logmsg, 42);
4733 err = get_short_logmsg(&logmsg, 42, commit);
4738 switch (hle->cmd->code) {
4739 case GOT_HISTEDIT_PICK:
4740 case GOT_HISTEDIT_EDIT:
4741 printf("%s -> %s: %s\n", old_id_str,
4742 new_id_str ? new_id_str : "no-op change", logmsg);
4744 case GOT_HISTEDIT_DROP:
4745 case GOT_HISTEDIT_FOLD:
4746 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
4759 static const struct got_error *
4760 histedit_commit(struct got_pathlist_head *merged_paths,
4761 struct got_worktree *worktree, struct got_fileindex *fileindex,
4762 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
4763 struct got_repository *repo)
4765 const struct got_error *err;
4766 struct got_commit_object *commit;
4767 struct got_object_id *new_commit_id;
4769 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
4770 && hle->logmsg == NULL) {
4771 err = histedit_edit_logmsg(hle, repo);
4776 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4780 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
4781 worktree, fileindex, tmp_branch, commit, hle->commit_id,
4784 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
4786 err = show_histedit_progress(commit, hle, NULL);
4788 err = show_histedit_progress(commit, hle, new_commit_id);
4789 free(new_commit_id);
4792 got_object_commit_close(commit);
4796 static const struct got_error *
4797 histedit_skip_commit(struct got_histedit_list_entry *hle,
4798 struct got_worktree *worktree, struct got_repository *repo)
4800 const struct got_error *error;
4801 struct got_commit_object *commit;
4803 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
4808 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
4812 error = show_histedit_progress(commit, hle, NULL);
4813 got_object_commit_close(commit);
4817 static const struct got_error *
4818 cmd_histedit(int argc, char *argv[])
4820 const struct got_error *error = NULL;
4821 struct got_worktree *worktree = NULL;
4822 struct got_fileindex *fileindex = NULL;
4823 struct got_repository *repo = NULL;
4825 struct got_reference *branch = NULL;
4826 struct got_reference *tmp_branch = NULL;
4827 struct got_object_id *resume_commit_id = NULL;
4828 struct got_object_id *base_commit_id = NULL;
4829 struct got_object_id *head_commit_id = NULL;
4830 struct got_commit_object *commit = NULL;
4831 int ch, rebase_in_progress = 0, did_something;
4832 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
4833 const char *edit_script_path = NULL;
4834 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
4835 struct got_object_id_queue commits;
4836 struct got_pathlist_head merged_paths;
4837 const struct got_object_id_queue *parent_ids;
4838 struct got_object_qid *pid;
4839 struct got_histedit_list histedit_cmds;
4840 struct got_histedit_list_entry *hle;
4842 SIMPLEQ_INIT(&commits);
4843 TAILQ_INIT(&histedit_cmds);
4844 TAILQ_INIT(&merged_paths);
4846 while ((ch = getopt(argc, argv, "acF:")) != -1) {
4855 edit_script_path = optarg;
4867 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4868 "unveil", NULL) == -1)
4871 if (abort_edit && continue_edit)
4877 * This command cannot apply unveil(2) in all cases because the
4878 * user may choose to run an editor to edit the histedit script
4879 * and to edit individual commit log messages.
4880 * unveil(2) traverses exec(2); if an editor is used we have to
4881 * apply unveil after edit script and log messages have been written.
4882 * XXX TODO: Make use of unveil(2) where possible.
4885 cwd = getcwd(NULL, 0);
4887 error = got_error_from_errno("getcwd");
4890 error = got_worktree_open(&worktree, cwd);
4894 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4898 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4901 if (rebase_in_progress) {
4902 error = got_error(GOT_ERR_REBASING);
4906 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
4910 if (edit_in_progress && abort_edit) {
4911 error = got_worktree_histedit_continue(&resume_commit_id,
4912 &tmp_branch, &branch, &base_commit_id, &fileindex,
4916 printf("Switching work tree to %s\n",
4917 got_ref_get_symref_target(branch));
4918 error = got_worktree_histedit_abort(worktree, fileindex, repo,
4919 branch, base_commit_id, update_progress, &did_something);
4922 printf("Histedit of %s aborted\n",
4923 got_ref_get_symref_target(branch));
4924 goto done; /* nothing else to do */
4925 } else if (abort_edit) {
4926 error = got_error(GOT_ERR_NOT_HISTEDIT);
4930 if (continue_edit) {
4933 if (!edit_in_progress) {
4934 error = got_error(GOT_ERR_NOT_HISTEDIT);
4938 error = got_worktree_get_histedit_script_path(&path, worktree);
4942 error = histedit_load_list(&histedit_cmds, path, repo);
4947 error = got_worktree_histedit_continue(&resume_commit_id,
4948 &tmp_branch, &branch, &base_commit_id, &fileindex,
4953 error = got_ref_resolve(&head_commit_id, repo, branch);
4957 error = got_object_open_as_commit(&commit, repo,
4961 parent_ids = got_object_commit_get_parent_ids(commit);
4962 pid = SIMPLEQ_FIRST(parent_ids);
4964 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
4967 error = collect_commits(&commits, head_commit_id, pid->id,
4968 base_commit_id, got_worktree_get_path_prefix(worktree),
4969 GOT_ERR_HISTEDIT_PATH, repo);
4970 got_object_commit_close(commit);
4975 if (edit_in_progress) {
4976 error = got_error(GOT_ERR_HISTEDIT_BUSY);
4980 error = got_ref_open(&branch, repo,
4981 got_worktree_get_head_ref_name(worktree), 0);
4985 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
4986 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
4987 "will not edit commit history of a branch outside "
4988 "the \"refs/heads/\" reference namespace");
4992 error = got_ref_resolve(&head_commit_id, repo, branch);
4993 got_ref_close(branch);
4998 error = got_object_open_as_commit(&commit, repo,
5002 parent_ids = got_object_commit_get_parent_ids(commit);
5003 pid = SIMPLEQ_FIRST(parent_ids);
5005 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
5008 error = collect_commits(&commits, head_commit_id, pid->id,
5009 got_worktree_get_base_commit_id(worktree),
5010 got_worktree_get_path_prefix(worktree),
5011 GOT_ERR_HISTEDIT_PATH, repo);
5012 got_object_commit_close(commit);
5017 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
5018 &base_commit_id, &fileindex, worktree, repo);
5022 if (edit_script_path) {
5023 error = histedit_load_list(&histedit_cmds,
5024 edit_script_path, repo);
5026 got_worktree_histedit_abort(worktree, fileindex,
5027 repo, branch, base_commit_id,
5028 update_progress, &did_something);
5032 error = histedit_edit_script(&histedit_cmds, &commits,
5035 got_worktree_histedit_abort(worktree, fileindex,
5036 repo, branch, base_commit_id,
5037 update_progress, &did_something);
5043 error = histedit_save_list(&histedit_cmds, worktree,
5046 got_worktree_histedit_abort(worktree, fileindex,
5047 repo, branch, base_commit_id,
5048 update_progress, &did_something);
5054 error = histedit_check_script(&histedit_cmds, &commits, repo);
5058 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
5059 if (resume_commit_id) {
5060 if (got_object_id_cmp(hle->commit_id,
5061 resume_commit_id) != 0)
5064 resume_commit_id = NULL;
5065 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
5066 hle->cmd->code == GOT_HISTEDIT_FOLD) {
5067 error = histedit_skip_commit(hle, worktree,
5070 error = histedit_commit(NULL, worktree,
5071 fileindex, tmp_branch, hle, repo);
5078 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
5079 error = histedit_skip_commit(hle, worktree, repo);
5085 error = got_object_open_as_commit(&commit, repo,
5089 parent_ids = got_object_commit_get_parent_ids(commit);
5090 pid = SIMPLEQ_FIRST(parent_ids);
5092 error = got_worktree_histedit_merge_files(&merged_paths,
5093 worktree, fileindex, pid->id, hle->commit_id, repo,
5094 rebase_progress, &rebase_status, check_cancelled, NULL);
5097 got_object_commit_close(commit);
5100 if (rebase_status == GOT_STATUS_CONFLICT) {
5101 got_worktree_rebase_pathlist_free(&merged_paths);
5105 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
5107 error = got_object_id_str(&id_str, hle->commit_id);
5110 printf("Stopping histedit for amending commit %s\n",
5113 got_worktree_rebase_pathlist_free(&merged_paths);
5114 error = got_worktree_histedit_postpone(worktree,
5119 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
5120 error = histedit_skip_commit(hle, worktree, repo);
5126 error = histedit_commit(&merged_paths, worktree, fileindex,
5127 tmp_branch, hle, repo);
5128 got_worktree_rebase_pathlist_free(&merged_paths);
5133 if (rebase_status == GOT_STATUS_CONFLICT) {
5134 error = got_worktree_histedit_postpone(worktree, fileindex);
5137 error = got_error_msg(GOT_ERR_CONFLICTS,
5138 "conflicts must be resolved before rebasing can continue");
5140 error = histedit_complete(worktree, fileindex, tmp_branch,
5143 got_object_id_queue_free(&commits);
5144 histedit_free_list(&histedit_cmds);
5145 free(head_commit_id);
5146 free(base_commit_id);
5147 free(resume_commit_id);
5149 got_object_commit_close(commit);
5151 got_ref_close(branch);
5153 got_ref_close(tmp_branch);
5155 got_worktree_close(worktree);
5157 got_repo_close(repo);
5164 fprintf(stderr, "usage: %s stage file-path ...\n",
5169 static const struct got_error *
5170 cmd_stage(int argc, char *argv[])
5172 const struct got_error *error = NULL;
5173 struct got_repository *repo = NULL;
5174 struct got_worktree *worktree = NULL;
5176 struct got_pathlist_head paths;
5177 struct got_pathlist_entry *pe;
5178 const char *worktree_path;
5183 while ((ch = getopt(argc, argv, "")) != -1) {
5195 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5196 "unveil", NULL) == -1)
5202 cwd = getcwd(NULL, 0);
5204 error = got_error_from_errno("getcwd");
5208 error = got_worktree_open(&worktree, cwd);
5212 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5216 error = apply_unveil(got_repo_get_path(repo), 1,
5217 got_worktree_get_root_path(worktree));
5221 worktree_path = got_worktree_get_root_path(worktree);
5222 for (x = 0; x < argc; x++) {
5223 char *path = realpath(argv[x], NULL);
5225 if (errno != ENOENT) {
5226 error = got_error_from_errno2("realpath",
5230 if (got_path_is_child(argv[x], worktree_path,
5231 strlen(worktree_path))) {
5232 path = strdup(argv[x]);
5234 error = got_error_from_errno("strdup");
5238 } else if (asprintf(&path, "%s/%s",
5239 got_worktree_get_root_path(worktree),
5241 error = got_error_from_errno("asprintf");
5246 got_path_strip_trailing_slashes(path);
5247 error = got_pathlist_insert(&pe, &paths, path, NULL);
5253 error = got_worktree_stage(worktree, &paths, print_status, NULL, repo);
5256 got_repo_close(repo);
5258 got_worktree_close(worktree);
5259 TAILQ_FOREACH(pe, &paths, entry)
5260 free((char *)pe->path);
5261 got_pathlist_free(&paths);