2 * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/queue.h>
19 #include <sys/types.h>
21 #include <sys/param.h>
38 #include "got_version.h"
39 #include "got_error.h"
40 #include "got_object.h"
41 #include "got_reference.h"
42 #include "got_repository.h"
44 #include "got_cancel.h"
45 #include "got_worktree.h"
47 #include "got_commit_graph.h"
48 #include "got_blame.h"
49 #include "got_privsep.h"
50 #include "got_opentemp.h"
53 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
56 static volatile sig_atomic_t sigint_received;
57 static volatile sig_atomic_t sigpipe_received;
60 catch_sigint(int signo)
66 catch_sigpipe(int signo)
74 const struct got_error *(*cmd_main)(int, char *[]);
75 void (*cmd_usage)(void);
76 const char *cmd_alias;
79 __dead static void usage(int);
80 __dead static void usage_init(void);
81 __dead static void usage_import(void);
82 __dead static void usage_checkout(void);
83 __dead static void usage_update(void);
84 __dead static void usage_log(void);
85 __dead static void usage_diff(void);
86 __dead static void usage_blame(void);
87 __dead static void usage_tree(void);
88 __dead static void usage_status(void);
89 __dead static void usage_ref(void);
90 __dead static void usage_branch(void);
91 __dead static void usage_tag(void);
92 __dead static void usage_add(void);
93 __dead static void usage_remove(void);
94 __dead static void usage_revert(void);
95 __dead static void usage_commit(void);
96 __dead static void usage_cherrypick(void);
97 __dead static void usage_backout(void);
98 __dead static void usage_rebase(void);
99 __dead static void usage_histedit(void);
100 __dead static void usage_integrate(void);
101 __dead static void usage_stage(void);
102 __dead static void usage_unstage(void);
103 __dead static void usage_cat(void);
105 static const struct got_error* cmd_init(int, char *[]);
106 static const struct got_error* cmd_import(int, char *[]);
107 static const struct got_error* cmd_checkout(int, char *[]);
108 static const struct got_error* cmd_update(int, char *[]);
109 static const struct got_error* cmd_log(int, char *[]);
110 static const struct got_error* cmd_diff(int, char *[]);
111 static const struct got_error* cmd_blame(int, char *[]);
112 static const struct got_error* cmd_tree(int, char *[]);
113 static const struct got_error* cmd_status(int, char *[]);
114 static const struct got_error* cmd_ref(int, char *[]);
115 static const struct got_error* cmd_branch(int, char *[]);
116 static const struct got_error* cmd_tag(int, char *[]);
117 static const struct got_error* cmd_add(int, char *[]);
118 static const struct got_error* cmd_remove(int, char *[]);
119 static const struct got_error* cmd_revert(int, char *[]);
120 static const struct got_error* cmd_commit(int, char *[]);
121 static const struct got_error* cmd_cherrypick(int, char *[]);
122 static const struct got_error* cmd_backout(int, char *[]);
123 static const struct got_error* cmd_rebase(int, char *[]);
124 static const struct got_error* cmd_histedit(int, char *[]);
125 static const struct got_error* cmd_integrate(int, char *[]);
126 static const struct got_error* cmd_stage(int, char *[]);
127 static const struct got_error* cmd_unstage(int, char *[]);
128 static const struct got_error* cmd_cat(int, char *[]);
130 static struct got_cmd got_commands[] = {
131 { "init", cmd_init, usage_init, "in" },
132 { "import", cmd_import, usage_import, "im" },
133 { "checkout", cmd_checkout, usage_checkout, "co" },
134 { "update", cmd_update, usage_update, "up" },
135 { "log", cmd_log, usage_log, "" },
136 { "diff", cmd_diff, usage_diff, "di" },
137 { "blame", cmd_blame, usage_blame, "bl" },
138 { "tree", cmd_tree, usage_tree, "tr" },
139 { "status", cmd_status, usage_status, "st" },
140 { "ref", cmd_ref, usage_ref, "" },
141 { "branch", cmd_branch, usage_branch, "br" },
142 { "tag", cmd_tag, usage_tag, "" },
143 { "add", cmd_add, usage_add, "" },
144 { "remove", cmd_remove, usage_remove, "rm" },
145 { "revert", cmd_revert, usage_revert, "rv" },
146 { "commit", cmd_commit, usage_commit, "ci" },
147 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
148 { "backout", cmd_backout, usage_backout, "bo" },
149 { "rebase", cmd_rebase, usage_rebase, "rb" },
150 { "histedit", cmd_histedit, usage_histedit, "he" },
151 { "integrate", cmd_integrate, usage_integrate,"ig" },
152 { "stage", cmd_stage, usage_stage, "sg" },
153 { "unstage", cmd_unstage, usage_unstage, "ug" },
154 { "cat", cmd_cat, usage_cat, "" },
162 fprintf(stderr, "commands:");
163 for (i = 0; i < nitems(got_commands); i++) {
164 struct got_cmd *cmd = &got_commands[i];
165 fprintf(stderr, " %s", cmd->cmd_name);
171 main(int argc, char *argv[])
176 int hflag = 0, Vflag = 0;
178 setlocale(LC_CTYPE, "");
180 while ((ch = getopt(argc, argv, "hV")) != -1) {
199 got_version_print_str();
206 signal(SIGINT, catch_sigint);
207 signal(SIGPIPE, catch_sigpipe);
209 for (i = 0; i < nitems(got_commands); i++) {
210 const struct got_error *error;
212 cmd = &got_commands[i];
214 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
215 strcmp(cmd->cmd_alias, argv[0]) != 0)
219 got_commands[i].cmd_usage();
221 error = got_commands[i].cmd_main(argc, argv);
222 if (error && !(sigint_received || sigpipe_received)) {
223 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
230 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
238 fprintf(stderr, "usage: %s [-h] [-V] command [arg ...]\n",
245 static const struct got_error *
246 get_editor(char **abspath)
248 const struct got_error *err = NULL;
253 editor = getenv("VISUAL");
255 editor = getenv("EDITOR");
258 err = got_path_find_prog(abspath, editor);
263 if (*abspath == NULL) {
264 *abspath = strdup("/bin/ed");
265 if (*abspath == NULL)
266 return got_error_from_errno("strdup");
272 static const struct got_error *
273 apply_unveil(const char *repo_path, int repo_read_only,
274 const char *worktree_path)
276 const struct got_error *err;
279 if (unveil("gmon.out", "rwc") != 0)
280 return got_error_from_errno2("unveil", "gmon.out");
282 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
283 return got_error_from_errno2("unveil", repo_path);
285 if (worktree_path && unveil(worktree_path, "rwc") != 0)
286 return got_error_from_errno2("unveil", worktree_path);
288 if (unveil("/tmp", "rwc") != 0)
289 return got_error_from_errno2("unveil", "/tmp");
291 err = got_privsep_unveil_exec_helpers();
295 if (unveil(NULL, NULL) != 0)
296 return got_error_from_errno("unveil");
304 fprintf(stderr, "usage: %s init repository-path\n", getprogname());
308 static const struct got_error *
309 cmd_init(int argc, char *argv[])
311 const struct got_error *error = NULL;
312 char *repo_path = NULL;
315 while ((ch = getopt(argc, argv, "")) != -1) {
327 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
333 repo_path = strdup(argv[0]);
334 if (repo_path == NULL)
335 return got_error_from_errno("strdup");
337 got_path_strip_trailing_slashes(repo_path);
339 error = got_path_mkdir(repo_path);
341 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
344 error = apply_unveil(repo_path, 0, NULL);
348 error = got_repo_init(repo_path);
360 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
361 "[-r repository-path] [-I pattern] path\n", getprogname());
366 spawn_editor(const char *editor, const char *file)
369 sig_t sighup, sigint, sigquit;
372 sighup = signal(SIGHUP, SIG_IGN);
373 sigint = signal(SIGINT, SIG_IGN);
374 sigquit = signal(SIGQUIT, SIG_IGN);
376 switch (pid = fork()) {
380 execl(editor, editor, file, (char *)NULL);
384 while (waitpid(pid, &st, 0) == -1)
389 (void)signal(SIGHUP, sighup);
390 (void)signal(SIGINT, sigint);
391 (void)signal(SIGQUIT, sigquit);
393 if (!WIFEXITED(st)) {
398 return WEXITSTATUS(st);
401 static const struct got_error *
402 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
403 const char *initial_content)
405 const struct got_error *err = NULL;
409 int content_changed = 0;
414 if (stat(logmsg_path, &st) == -1)
415 return got_error_from_errno2("stat", logmsg_path);
417 if (spawn_editor(editor, logmsg_path) == -1)
418 return got_error_from_errno("failed spawning editor");
420 if (stat(logmsg_path, &st2) == -1)
421 return got_error_from_errno("stat");
423 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
424 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
425 "no changes made to commit message, aborting");
427 *logmsg = malloc(st2.st_size + 1);
429 return got_error_from_errno("malloc");
433 fp = fopen(logmsg_path, "r");
435 err = got_error_from_errno("fopen");
438 while (fgets(buf, sizeof(buf), fp) != NULL) {
439 if (!content_changed && strcmp(buf, initial_content) != 0)
441 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
442 continue; /* remove comments and leading empty lines */
443 len = strlcat(*logmsg, buf, st2.st_size);
447 while (len > 0 && (*logmsg)[len - 1] == '\n') {
448 (*logmsg)[len - 1] = '\0';
452 if (len == 0 || !content_changed)
453 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
454 "commit message cannot be empty, aborting");
463 static const struct got_error *
464 collect_import_msg(char **logmsg, const char *editor, const char *path_dir,
465 const char *branch_name)
467 char *initial_content = NULL, *logmsg_path = NULL;
468 const struct got_error *err = NULL;
471 if (asprintf(&initial_content,
472 "\n# %s to be imported to branch %s\n", path_dir,
474 return got_error_from_errno("asprintf");
476 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-importmsg");
480 dprintf(fd, initial_content);
483 err = edit_logmsg(logmsg, editor, logmsg_path, initial_content);
485 free(initial_content);
490 static const struct got_error *
491 import_progress(void *arg, const char *path)
493 printf("A %s\n", path);
497 static const struct got_error *
498 get_author(char **author, struct got_repository *repo)
500 const struct got_error *err = NULL;
501 const char *got_author, *name, *email;
505 name = got_repo_get_gitconfig_author_name(repo);
506 email = got_repo_get_gitconfig_author_email(repo);
508 if (asprintf(author, "%s <%s>", name, email) == -1)
509 return got_error_from_errno("asprintf");
513 got_author = getenv("GOT_AUTHOR");
514 if (got_author == NULL) {
515 name = got_repo_get_global_gitconfig_author_name(repo);
516 email = got_repo_get_global_gitconfig_author_email(repo);
518 if (asprintf(author, "%s <%s>", name, email) == -1)
519 return got_error_from_errno("asprintf");
522 /* TODO: Look up user in password database? */
523 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
526 *author = strdup(got_author);
528 return got_error_from_errno("strdup");
531 * Really dumb email address check; we're only doing this to
532 * avoid git's object parser breaking on commits we create.
534 while (*got_author && *got_author != '<')
536 if (*got_author != '<') {
537 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
540 while (*got_author && *got_author != '@')
542 if (*got_author != '@') {
543 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
546 while (*got_author && *got_author != '>')
548 if (*got_author != '>')
549 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
558 static const struct got_error *
559 get_gitconfig_path(char **gitconfig_path)
561 const char *homedir = getenv("HOME");
563 *gitconfig_path = NULL;
565 if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
566 return got_error_from_errno("asprintf");
572 static const struct got_error *
573 cmd_import(int argc, char *argv[])
575 const struct got_error *error = NULL;
576 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
577 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
578 const char *branch_name = "master";
579 char *refname = NULL, *id_str = NULL;
580 struct got_repository *repo = NULL;
581 struct got_reference *branch_ref = NULL, *head_ref = NULL;
582 struct got_object_id *new_commit_id = NULL;
584 struct got_pathlist_head ignores;
585 struct got_pathlist_entry *pe;
587 TAILQ_INIT(&ignores);
589 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
592 branch_name = optarg;
595 logmsg = strdup(optarg);
596 if (logmsg == NULL) {
597 error = got_error_from_errno("strdup");
602 repo_path = realpath(optarg, NULL);
603 if (repo_path == NULL) {
604 error = got_error_from_errno("realpath");
609 if (optarg[0] == '\0')
611 error = got_pathlist_insert(&pe, &ignores, optarg,
626 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
634 if (repo_path == NULL) {
635 repo_path = getcwd(NULL, 0);
636 if (repo_path == NULL)
637 return got_error_from_errno("getcwd");
639 got_path_strip_trailing_slashes(repo_path);
640 error = get_gitconfig_path(&gitconfig_path);
643 error = got_repo_open(&repo, repo_path, gitconfig_path);
647 error = get_author(&author, repo);
651 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
652 error = got_error_from_errno("asprintf");
656 error = got_ref_open(&branch_ref, repo, refname, 0);
658 if (error->code != GOT_ERR_NOT_REF)
661 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
662 "import target branch already exists");
666 path_dir = realpath(argv[0], NULL);
667 if (path_dir == NULL) {
668 error = got_error_from_errno("realpath");
671 got_path_strip_trailing_slashes(path_dir);
674 * unveil(2) traverses exec(2); if an editor is used we have
675 * to apply unveil after the log message has been written.
677 if (logmsg == NULL || strlen(logmsg) == 0) {
678 error = get_editor(&editor);
682 error = collect_import_msg(&logmsg, editor, path_dir, refname);
687 if (unveil(path_dir, "r") != 0)
688 return got_error_from_errno2("unveil", path_dir);
690 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
694 error = got_repo_import(&new_commit_id, path_dir, logmsg,
695 author, &ignores, repo, import_progress, NULL);
699 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
703 error = got_ref_write(branch_ref, repo);
707 error = got_object_id_str(&id_str, new_commit_id);
711 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
713 if (error->code != GOT_ERR_NOT_REF)
716 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
721 error = got_ref_write(head_ref, repo);
726 printf("Created branch %s with commit %s\n",
727 got_ref_get_name(branch_ref), id_str);
736 free(gitconfig_path);
738 got_ref_close(branch_ref);
740 got_ref_close(head_ref);
747 fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
748 "[-p prefix] repository-path [worktree-path]\n", getprogname());
752 static const struct got_error *
753 checkout_progress(void *arg, unsigned char status, const char *path)
755 char *worktree_path = arg;
757 /* Base commit bump happens silently. */
758 if (status == GOT_STATUS_BUMP_BASE)
761 while (path[0] == '/')
764 printf("%c %s/%s\n", status, worktree_path, path);
768 static const struct got_error *
769 check_cancelled(void *arg)
771 if (sigint_received || sigpipe_received)
772 return got_error(GOT_ERR_CANCELLED);
776 static const struct got_error *
777 check_linear_ancestry(struct got_object_id *commit_id,
778 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
779 struct got_repository *repo)
781 const struct got_error *err = NULL;
782 struct got_object_id *yca_id;
784 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
785 commit_id, base_commit_id, repo, check_cancelled, NULL);
790 return got_error(GOT_ERR_ANCESTRY);
793 * Require a straight line of history between the target commit
794 * and the work tree's base commit.
796 * Non-linear situations such as this require a rebase:
798 * (commit) D F (base_commit)
806 * 'got update' only handles linear cases:
807 * Update forwards in time: A (base/yca) - B - C - D (commit)
808 * Update backwards in time: D (base) - C - B - A (commit/yca)
810 if (allow_forwards_in_time_only) {
811 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
812 return got_error(GOT_ERR_ANCESTRY);
813 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
814 got_object_id_cmp(base_commit_id, yca_id) != 0)
815 return got_error(GOT_ERR_ANCESTRY);
821 static const struct got_error *
822 check_same_branch(struct got_object_id *commit_id,
823 struct got_reference *head_ref, struct got_object_id *yca_id,
824 struct got_repository *repo)
826 const struct got_error *err = NULL;
827 struct got_commit_graph *graph = NULL;
828 struct got_object_id *head_commit_id = NULL;
829 int is_same_branch = 0;
831 err = got_ref_resolve(&head_commit_id, repo, head_ref);
835 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
839 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
844 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
848 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
849 check_cancelled, NULL);
854 struct got_object_id *id;
855 err = got_commit_graph_iter_next(&id, graph);
857 if (err->code == GOT_ERR_ITER_COMPLETED) {
860 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
862 err = got_commit_graph_fetch_commits(graph, 1,
863 repo, check_cancelled, NULL);
869 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
871 if (got_object_id_cmp(id, commit_id) == 0) {
879 got_commit_graph_close(graph);
880 free(head_commit_id);
881 if (!err && !is_same_branch)
882 err = got_error(GOT_ERR_ANCESTRY);
886 static const struct got_error *
887 resolve_commit_arg(struct got_object_id **commit_id,
888 const char *commit_id_arg, struct got_repository *repo)
890 const struct got_error *err;
891 struct got_reference *ref;
892 struct got_tag_object *tag;
894 err = got_repo_object_match_tag(&tag, commit_id_arg,
895 GOT_OBJ_TYPE_COMMIT, repo);
897 *commit_id = got_object_id_dup(
898 got_object_tag_get_object_id(tag));
899 if (*commit_id == NULL)
900 err = got_error_from_errno("got_object_id_dup");
901 got_object_tag_close(tag);
903 } else if (err->code != GOT_ERR_NO_OBJ)
906 err = got_ref_open(&ref, repo, commit_id_arg, 0);
908 err = got_ref_resolve(commit_id, repo, ref);
911 if (err->code != GOT_ERR_NOT_REF)
913 err = got_repo_match_object_id_prefix(commit_id,
914 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
919 static const struct got_error *
920 cmd_checkout(int argc, char *argv[])
922 const struct got_error *error = NULL;
923 struct got_repository *repo = NULL;
924 struct got_reference *head_ref = NULL;
925 struct got_worktree *worktree = NULL;
926 char *repo_path = NULL;
927 char *worktree_path = NULL;
928 const char *path_prefix = "";
929 const char *branch_name = GOT_REF_HEAD;
930 char *commit_id_str = NULL;
931 int ch, same_path_prefix;
932 struct got_pathlist_head paths;
936 while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
939 branch_name = optarg;
942 commit_id_str = strdup(optarg);
943 if (commit_id_str == NULL)
944 return got_error_from_errno("strdup");
947 path_prefix = optarg;
959 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
960 "unveil", NULL) == -1)
964 char *cwd, *base, *dotgit;
965 repo_path = realpath(argv[0], NULL);
966 if (repo_path == NULL)
967 return got_error_from_errno2("realpath", argv[0]);
968 cwd = getcwd(NULL, 0);
970 error = got_error_from_errno("getcwd");
973 if (path_prefix[0]) {
974 base = basename(path_prefix);
976 error = got_error_from_errno2("basename",
981 base = basename(repo_path);
983 error = got_error_from_errno2("basename",
988 dotgit = strstr(base, ".git");
991 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
992 error = got_error_from_errno("asprintf");
997 } else if (argc == 2) {
998 repo_path = realpath(argv[0], NULL);
999 if (repo_path == NULL) {
1000 error = got_error_from_errno2("realpath", argv[0]);
1003 worktree_path = realpath(argv[1], NULL);
1004 if (worktree_path == NULL) {
1005 if (errno != ENOENT) {
1006 error = got_error_from_errno2("realpath",
1010 worktree_path = strdup(argv[1]);
1011 if (worktree_path == NULL) {
1012 error = got_error_from_errno("strdup");
1019 got_path_strip_trailing_slashes(repo_path);
1020 got_path_strip_trailing_slashes(worktree_path);
1022 error = got_repo_open(&repo, repo_path, NULL);
1026 /* Pre-create work tree path for unveil(2) */
1027 error = got_path_mkdir(worktree_path);
1029 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1030 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1032 if (!got_path_dir_is_empty(worktree_path)) {
1033 error = got_error_path(worktree_path,
1034 GOT_ERR_DIR_NOT_EMPTY);
1039 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1043 error = got_ref_open(&head_ref, repo, branch_name, 0);
1047 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1048 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1051 error = got_worktree_open(&worktree, worktree_path);
1055 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1059 if (!same_path_prefix) {
1060 error = got_error(GOT_ERR_PATH_PREFIX);
1064 if (commit_id_str) {
1065 struct got_object_id *commit_id;
1066 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1069 error = check_linear_ancestry(commit_id,
1070 got_worktree_get_base_commit_id(worktree), 0, repo);
1071 if (error != NULL) {
1075 error = check_same_branch(commit_id, head_ref, NULL, repo);
1078 error = got_worktree_set_base_commit_id(worktree, repo,
1085 error = got_pathlist_append(&paths, "", NULL);
1088 error = got_worktree_checkout_files(worktree, &paths, repo,
1089 checkout_progress, worktree_path, check_cancelled, NULL);
1093 printf("Now shut up and hack\n");
1096 got_pathlist_free(&paths);
1097 free(commit_id_str);
1099 free(worktree_path);
1106 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1111 static const struct got_error *
1112 update_progress(void *arg, unsigned char status, const char *path)
1114 int *did_something = arg;
1116 if (status == GOT_STATUS_EXISTS)
1121 /* Base commit bump happens silently. */
1122 if (status == GOT_STATUS_BUMP_BASE)
1125 while (path[0] == '/')
1127 printf("%c %s\n", status, path);
1131 static const struct got_error *
1132 switch_head_ref(struct got_reference *head_ref,
1133 struct got_object_id *commit_id, struct got_worktree *worktree,
1134 struct got_repository *repo)
1136 const struct got_error *err = NULL;
1138 int ref_has_moved = 0;
1140 /* Trivial case: switching between two different references. */
1141 if (strcmp(got_ref_get_name(head_ref),
1142 got_worktree_get_head_ref_name(worktree)) != 0) {
1143 printf("Switching work tree from %s to %s\n",
1144 got_worktree_get_head_ref_name(worktree),
1145 got_ref_get_name(head_ref));
1146 return got_worktree_set_head_ref(worktree, head_ref);
1149 err = check_linear_ancestry(commit_id,
1150 got_worktree_get_base_commit_id(worktree), 0, repo);
1152 if (err->code != GOT_ERR_ANCESTRY)
1159 /* Switching to a rebased branch with the same reference name. */
1160 err = got_object_id_str(&base_id_str,
1161 got_worktree_get_base_commit_id(worktree));
1164 printf("Reference %s now points at a different branch\n",
1165 got_worktree_get_head_ref_name(worktree));
1166 printf("Switching work tree from %s to %s\n", base_id_str,
1167 got_worktree_get_head_ref_name(worktree));
1171 static const struct got_error *
1172 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1174 const struct got_error *err;
1177 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1181 return got_error(GOT_ERR_REBASING);
1183 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1187 return got_error(GOT_ERR_HISTEDIT_BUSY);
1192 static const struct got_error *
1193 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1194 char *argv[], struct got_worktree *worktree)
1196 const struct got_error *err = NULL;
1203 return got_error_from_errno("strdup");
1204 return got_pathlist_append(paths, path, NULL);
1207 for (i = 0; i < argc; i++) {
1208 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1211 err = got_pathlist_append(paths, path, NULL);
1221 static const struct got_error *
1222 cmd_update(int argc, char *argv[])
1224 const struct got_error *error = NULL;
1225 struct got_repository *repo = NULL;
1226 struct got_worktree *worktree = NULL;
1227 char *worktree_path = NULL;
1228 struct got_object_id *commit_id = NULL;
1229 char *commit_id_str = NULL;
1230 const char *branch_name = NULL;
1231 struct got_reference *head_ref = NULL;
1232 struct got_pathlist_head paths;
1233 struct got_pathlist_entry *pe;
1234 int ch, did_something = 0;
1238 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1241 branch_name = optarg;
1244 commit_id_str = strdup(optarg);
1245 if (commit_id_str == NULL)
1246 return got_error_from_errno("strdup");
1258 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1259 "unveil", NULL) == -1)
1262 worktree_path = getcwd(NULL, 0);
1263 if (worktree_path == NULL) {
1264 error = got_error_from_errno("getcwd");
1267 error = got_worktree_open(&worktree, worktree_path);
1271 error = check_rebase_or_histedit_in_progress(worktree);
1275 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
1280 error = apply_unveil(got_repo_get_path(repo), 0,
1281 got_worktree_get_root_path(worktree));
1285 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1289 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1290 got_worktree_get_head_ref_name(worktree), 0);
1293 if (commit_id_str == NULL) {
1294 error = got_ref_resolve(&commit_id, repo, head_ref);
1297 error = got_object_id_str(&commit_id_str, commit_id);
1301 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1302 free(commit_id_str);
1303 commit_id_str = NULL;
1306 error = got_object_id_str(&commit_id_str, commit_id);
1312 struct got_object_id *head_commit_id;
1313 TAILQ_FOREACH(pe, &paths, entry) {
1314 if (pe->path_len == 0)
1316 error = got_error_msg(GOT_ERR_BAD_PATH,
1317 "switching between branches requires that "
1318 "the entire work tree gets updated");
1321 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1324 error = check_linear_ancestry(commit_id, head_commit_id, 0,
1326 free(head_commit_id);
1329 error = check_same_branch(commit_id, head_ref, NULL, repo);
1332 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1336 error = check_linear_ancestry(commit_id,
1337 got_worktree_get_base_commit_id(worktree), 0, repo);
1338 if (error != NULL) {
1339 if (error->code == GOT_ERR_ANCESTRY)
1340 error = got_error(GOT_ERR_BRANCH_MOVED);
1343 error = check_same_branch(commit_id, head_ref, NULL, repo);
1348 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1350 error = got_worktree_set_base_commit_id(worktree, repo,
1356 error = got_worktree_checkout_files(worktree, &paths, repo,
1357 update_progress, &did_something, check_cancelled, NULL);
1362 printf("Updated to commit %s\n", commit_id_str);
1364 printf("Already up-to-date\n");
1366 free(worktree_path);
1367 TAILQ_FOREACH(pe, &paths, entry)
1368 free((char *)pe->path);
1369 got_pathlist_free(&paths);
1371 free(commit_id_str);
1375 static const struct got_error *
1376 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
1377 const char *path, int diff_context, int ignore_whitespace,
1378 struct got_repository *repo)
1380 const struct got_error *err = NULL;
1381 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1384 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
1389 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
1393 while (path[0] == '/')
1395 err = got_diff_blob(blob1, blob2, path, path, diff_context,
1396 ignore_whitespace, stdout);
1399 got_object_blob_close(blob1);
1400 got_object_blob_close(blob2);
1404 static const struct got_error *
1405 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
1406 const char *path, int diff_context, int ignore_whitespace,
1407 struct got_repository *repo)
1409 const struct got_error *err = NULL;
1410 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1411 struct got_diff_blob_output_unidiff_arg arg;
1414 err = got_object_open_as_tree(&tree1, repo, tree_id1);
1419 err = got_object_open_as_tree(&tree2, repo, tree_id2);
1423 arg.diff_context = diff_context;
1424 arg.ignore_whitespace = ignore_whitespace;
1425 arg.outfile = stdout;
1426 while (path[0] == '/')
1428 err = got_diff_tree(tree1, tree2, path, path, repo,
1429 got_diff_blob_output_unidiff, &arg, 1);
1432 got_object_tree_close(tree1);
1434 got_object_tree_close(tree2);
1438 static const struct got_error *
1439 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1440 const char *path, int diff_context, struct got_repository *repo)
1442 const struct got_error *err = NULL;
1443 struct got_commit_object *pcommit = NULL;
1444 char *id_str1 = NULL, *id_str2 = NULL;
1445 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
1446 struct got_object_qid *qid;
1448 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1450 err = got_object_open_as_commit(&pcommit, repo,
1456 if (path && path[0] != '\0') {
1458 err = got_object_id_by_path(&obj_id2, repo, id, path);
1461 err = got_object_id_str(&id_str2, obj_id2);
1467 err = got_object_id_by_path(&obj_id1, repo,
1473 err = got_object_id_str(&id_str1, obj_id1);
1479 err = got_object_get_type(&obj_type, repo, obj_id2);
1484 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1486 case GOT_OBJ_TYPE_BLOB:
1487 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
1490 case GOT_OBJ_TYPE_TREE:
1491 err = diff_trees(obj_id1, obj_id2, path, diff_context,
1495 err = got_error(GOT_ERR_OBJ_TYPE);
1501 obj_id2 = got_object_commit_get_tree_id(commit);
1502 err = got_object_id_str(&id_str2, obj_id2);
1505 obj_id1 = got_object_commit_get_tree_id(pcommit);
1506 err = got_object_id_str(&id_str1, obj_id1);
1509 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1510 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
1517 got_object_commit_close(pcommit);
1522 get_datestr(time_t *time, char *datebuf)
1524 struct tm mytm, *tm;
1527 tm = gmtime_r(time, &mytm);
1530 s = asctime_r(tm, datebuf);
1533 p = strchr(s, '\n');
1539 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1541 static const struct got_error *
1542 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1543 struct got_repository *repo, const char *path, int show_patch,
1544 int diff_context, struct got_reflist_head *refs)
1546 const struct got_error *err = NULL;
1547 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1549 time_t committer_time;
1550 const char *author, *committer;
1551 char *refs_str = NULL;
1552 struct got_reflist_entry *re;
1554 SIMPLEQ_FOREACH(re, refs, entry) {
1557 struct got_tag_object *tag = NULL;
1560 name = got_ref_get_name(re->ref);
1561 if (strcmp(name, GOT_REF_HEAD) == 0)
1563 if (strncmp(name, "refs/", 5) == 0)
1565 if (strncmp(name, "got/", 4) == 0)
1567 if (strncmp(name, "heads/", 6) == 0)
1569 if (strncmp(name, "remotes/", 8) == 0)
1571 if (strncmp(name, "tags/", 5) == 0) {
1572 err = got_object_open_as_tag(&tag, repo, re->id);
1574 if (err->code != GOT_ERR_OBJ_TYPE)
1576 /* Ref points at something other than a tag. */
1581 cmp = got_object_id_cmp(tag ?
1582 got_object_tag_get_object_id(tag) : re->id, id);
1584 got_object_tag_close(tag);
1588 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1590 err = got_error_from_errno("asprintf");
1596 err = got_object_id_str(&id_str, id);
1600 printf(GOT_COMMIT_SEP_STR);
1601 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1602 refs_str ? refs_str : "", refs_str ? ")" : "");
1607 printf("from: %s\n", got_object_commit_get_author(commit));
1608 committer_time = got_object_commit_get_committer_time(commit);
1609 datestr = get_datestr(&committer_time, datebuf);
1611 printf("date: %s UTC\n", datestr);
1612 author = got_object_commit_get_author(commit);
1613 committer = got_object_commit_get_committer(commit);
1614 if (strcmp(author, committer) != 0)
1615 printf("via: %s\n", committer);
1616 if (got_object_commit_get_nparents(commit) > 1) {
1617 const struct got_object_id_queue *parent_ids;
1618 struct got_object_qid *qid;
1620 parent_ids = got_object_commit_get_parent_ids(commit);
1621 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1622 err = got_object_id_str(&id_str, qid->id);
1625 printf("parent %d: %s\n", n++, id_str);
1630 err = got_object_commit_get_logmsg(&logmsg0, commit);
1636 line = strsep(&logmsg, "\n");
1638 printf(" %s\n", line);
1643 err = print_patch(commit, id, path, diff_context, repo);
1648 if (fflush(stdout) != 0 && err == NULL)
1649 err = got_error_from_errno("fflush");
1653 static const struct got_error *
1654 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1655 char *path, int show_patch, int diff_context, int limit,
1656 int first_parent_traversal, struct got_reflist_head *refs)
1658 const struct got_error *err;
1659 struct got_commit_graph *graph;
1661 err = got_commit_graph_open(&graph, root_id, path,
1662 first_parent_traversal, repo);
1665 err = got_commit_graph_iter_start(graph, root_id, repo,
1666 check_cancelled, NULL);
1670 struct got_commit_object *commit;
1671 struct got_object_id *id;
1673 if (sigint_received || sigpipe_received)
1676 err = got_commit_graph_iter_next(&id, graph);
1678 if (err->code == GOT_ERR_ITER_COMPLETED) {
1682 if (err->code != GOT_ERR_ITER_NEED_MORE)
1684 err = got_commit_graph_fetch_commits(graph, 1, repo,
1685 check_cancelled, NULL);
1694 err = got_object_open_as_commit(&commit, repo, id);
1697 err = print_commit(commit, id, repo, path, show_patch,
1698 diff_context, refs);
1699 got_object_commit_close(commit);
1700 if (err || (limit && --limit == 0))
1704 got_commit_graph_close(graph);
1711 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1712 "[-r repository-path] [path]\n", getprogname());
1717 get_default_log_limit(void)
1719 const char *got_default_log_limit;
1723 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
1724 if (got_default_log_limit == NULL)
1726 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
1732 static const struct got_error *
1733 cmd_log(int argc, char *argv[])
1735 const struct got_error *error;
1736 struct got_repository *repo = NULL;
1737 struct got_worktree *worktree = NULL;
1738 struct got_commit_object *commit = NULL;
1739 struct got_object_id *id = NULL;
1740 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1741 char *start_commit = NULL;
1742 int diff_context = 3, ch;
1743 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1745 struct got_reflist_head refs;
1747 SIMPLEQ_INIT(&refs);
1750 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1756 limit = get_default_log_limit();
1758 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
1764 start_commit = optarg;
1767 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1770 err(1, "-C option %s", errstr);
1773 limit = strtonum(optarg, 0, INT_MAX, &errstr);
1775 err(1, "-l option %s", errstr);
1778 first_parent_traversal = 1;
1781 repo_path = realpath(optarg, NULL);
1782 if (repo_path == NULL)
1783 err(1, "-r option");
1784 got_path_strip_trailing_slashes(repo_path);
1795 cwd = getcwd(NULL, 0);
1797 error = got_error_from_errno("getcwd");
1801 error = got_worktree_open(&worktree, cwd);
1802 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1809 error = got_error_from_errno("strdup");
1812 } else if (argc == 1) {
1814 error = got_worktree_resolve_path(&path, worktree,
1819 path = strdup(argv[0]);
1821 error = got_error_from_errno("strdup");
1828 if (repo_path == NULL) {
1829 repo_path = worktree ?
1830 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1832 if (repo_path == NULL) {
1833 error = got_error_from_errno("strdup");
1837 error = got_repo_open(&repo, repo_path, NULL);
1841 error = apply_unveil(got_repo_get_path(repo), 1,
1842 worktree ? got_worktree_get_root_path(worktree) : NULL);
1846 if (start_commit == NULL) {
1847 struct got_reference *head_ref;
1848 error = got_ref_open(&head_ref, repo,
1849 worktree ? got_worktree_get_head_ref_name(worktree)
1853 error = got_ref_resolve(&id, repo, head_ref);
1854 got_ref_close(head_ref);
1857 error = got_object_open_as_commit(&commit, repo, id);
1859 struct got_reference *ref;
1860 error = got_ref_open(&ref, repo, start_commit, 0);
1861 if (error == NULL) {
1863 error = got_ref_resolve(&id, repo, ref);
1867 error = got_object_get_type(&obj_type, repo, id);
1870 if (obj_type == GOT_OBJ_TYPE_TAG) {
1871 struct got_tag_object *tag;
1872 error = got_object_open_as_tag(&tag, repo, id);
1875 if (got_object_tag_get_object_type(tag) !=
1876 GOT_OBJ_TYPE_COMMIT) {
1877 got_object_tag_close(tag);
1878 error = got_error(GOT_ERR_OBJ_TYPE);
1882 id = got_object_id_dup(
1883 got_object_tag_get_object_id(tag));
1885 error = got_error_from_errno(
1886 "got_object_id_dup");
1887 got_object_tag_close(tag);
1890 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1891 error = got_error(GOT_ERR_OBJ_TYPE);
1894 error = got_object_open_as_commit(&commit, repo, id);
1898 if (commit == NULL) {
1899 error = got_repo_match_object_id_prefix(&id,
1900 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1909 const char *prefix = got_worktree_get_path_prefix(worktree);
1911 if (asprintf(&p, "%s%s%s", prefix,
1912 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
1913 error = got_error_from_errno("asprintf");
1916 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1919 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1924 path = in_repo_path;
1927 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
1931 error = print_commits(id, repo, path, show_patch,
1932 diff_context, limit, first_parent_traversal, &refs);
1939 got_worktree_close(worktree);
1941 const struct got_error *repo_error;
1942 repo_error = got_repo_close(repo);
1946 got_ref_list_free(&refs);
1953 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
1954 "[-w] [object1 object2 | path]\n", getprogname());
1958 struct print_diff_arg {
1959 struct got_repository *repo;
1960 struct got_worktree *worktree;
1965 int ignore_whitespace;
1968 static const struct got_error *
1969 print_diff(void *arg, unsigned char status, unsigned char staged_status,
1970 const char *path, struct got_object_id *blob_id,
1971 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
1973 struct print_diff_arg *a = arg;
1974 const struct got_error *err = NULL;
1975 struct got_blob_object *blob1 = NULL;
1977 char *abspath = NULL, *label1 = NULL;
1980 if (a->diff_staged) {
1981 if (staged_status != GOT_STATUS_MODIFY &&
1982 staged_status != GOT_STATUS_ADD &&
1983 staged_status != GOT_STATUS_DELETE)
1986 if (staged_status == GOT_STATUS_DELETE)
1988 if (status == GOT_STATUS_NONEXISTENT)
1989 return got_error_set_errno(ENOENT, path);
1990 if (status != GOT_STATUS_MODIFY &&
1991 status != GOT_STATUS_ADD &&
1992 status != GOT_STATUS_DELETE &&
1993 status != GOT_STATUS_CONFLICT)
1997 if (!a->header_shown) {
1998 printf("diff %s %s%s\n", a->id_str,
1999 got_worktree_get_root_path(a->worktree),
2000 a->diff_staged ? " (staged changes)" : "");
2001 a->header_shown = 1;
2004 if (a->diff_staged) {
2005 const char *label1 = NULL, *label2 = NULL;
2006 switch (staged_status) {
2007 case GOT_STATUS_MODIFY:
2011 case GOT_STATUS_ADD:
2014 case GOT_STATUS_DELETE:
2018 return got_error(GOT_ERR_FILE_STATUS);
2020 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2021 label1, label2, a->diff_context, a->ignore_whitespace,
2025 if (staged_status == GOT_STATUS_ADD ||
2026 staged_status == GOT_STATUS_MODIFY) {
2028 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2032 err = got_object_id_str(&id_str, staged_blob_id);
2035 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2036 err = got_error_from_errno("asprintf");
2041 } else if (status != GOT_STATUS_ADD) {
2042 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2047 if (status != GOT_STATUS_DELETE) {
2048 if (asprintf(&abspath, "%s/%s",
2049 got_worktree_get_root_path(a->worktree), path) == -1) {
2050 err = got_error_from_errno("asprintf");
2054 f2 = fopen(abspath, "r");
2056 err = got_error_from_errno2("fopen", abspath);
2059 if (lstat(abspath, &sb) == -1) {
2060 err = got_error_from_errno2("lstat", abspath);
2066 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2067 a->diff_context, a->ignore_whitespace, stdout);
2070 got_object_blob_close(blob1);
2071 if (f2 && fclose(f2) != 0 && err == NULL)
2072 err = got_error_from_errno("fclose");
2077 static const struct got_error *
2078 match_object_id(struct got_object_id **id, char **label,
2079 const char *id_str, int obj_type, int resolve_tags,
2080 struct got_repository *repo)
2082 const struct got_error *err;
2083 struct got_tag_object *tag;
2084 struct got_reference *ref = NULL;
2090 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
2093 *id = got_object_id_dup(
2094 got_object_tag_get_object_id(tag));
2096 err = got_error_from_errno("got_object_id_dup");
2097 else if (asprintf(label, "refs/tags/%s",
2098 got_object_tag_get_name(tag)) == -1) {
2099 err = got_error_from_errno("asprintf");
2103 got_object_tag_close(tag);
2105 } else if (err->code != GOT_ERR_NO_OBJ)
2109 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
2111 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2113 err = got_ref_open(&ref, repo, id_str, 0);
2116 *label = strdup(got_ref_get_name(ref));
2117 if (*label == NULL) {
2118 err = got_error_from_errno("strdup");
2121 err = got_ref_resolve(id, repo, ref);
2123 err = got_object_id_str(label, *id);
2124 if (*label == NULL) {
2125 err = got_error_from_errno("strdup");
2136 static const struct got_error *
2137 cmd_diff(int argc, char *argv[])
2139 const struct got_error *error;
2140 struct got_repository *repo = NULL;
2141 struct got_worktree *worktree = NULL;
2142 char *cwd = NULL, *repo_path = NULL;
2143 struct got_object_id *id1 = NULL, *id2 = NULL;
2144 const char *id_str1 = NULL, *id_str2 = NULL;
2145 char *label1 = NULL, *label2 = NULL;
2147 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2152 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2157 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2160 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
2162 err(1, "-C option %s", errstr);
2165 repo_path = realpath(optarg, NULL);
2166 if (repo_path == NULL)
2167 err(1, "-r option");
2168 got_path_strip_trailing_slashes(repo_path);
2174 ignore_whitespace = 1;
2185 cwd = getcwd(NULL, 0);
2187 error = got_error_from_errno("getcwd");
2190 error = got_worktree_open(&worktree, cwd);
2191 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2194 if (worktree == NULL) {
2195 error = got_error(GOT_ERR_NOT_WORKTREE);
2200 "-r option can't be used when diffing a work tree");
2201 repo_path = strdup(got_worktree_get_repo_path(worktree));
2202 if (repo_path == NULL) {
2203 error = got_error_from_errno("strdup");
2207 error = got_worktree_resolve_path(&path, worktree,
2214 error = got_error_from_errno("strdup");
2218 } else if (argc == 2) {
2220 errx(1, "-s option can't be used when diffing "
2221 "objects in repository");
2224 if (worktree && repo_path == NULL) {
2226 strdup(got_worktree_get_repo_path(worktree));
2227 if (repo_path == NULL) {
2228 error = got_error_from_errno("strdup");
2235 if (repo_path == NULL) {
2236 repo_path = getcwd(NULL, 0);
2237 if (repo_path == NULL)
2238 return got_error_from_errno("getcwd");
2241 error = got_repo_open(&repo, repo_path, NULL);
2246 error = apply_unveil(got_repo_get_path(repo), 1,
2247 worktree ? got_worktree_get_root_path(worktree) : NULL);
2252 struct print_diff_arg arg;
2253 struct got_pathlist_head paths;
2258 error = got_object_id_str(&id_str,
2259 got_worktree_get_base_commit_id(worktree));
2263 arg.worktree = worktree;
2264 arg.diff_context = diff_context;
2265 arg.id_str = id_str;
2266 arg.header_shown = 0;
2267 arg.diff_staged = diff_staged;
2268 arg.ignore_whitespace = ignore_whitespace;
2270 error = got_pathlist_append(&paths, path, NULL);
2274 error = got_worktree_status(worktree, &paths, repo, print_diff,
2275 &arg, check_cancelled, NULL);
2277 got_pathlist_free(&paths);
2281 error = match_object_id(&id1, &label1, id_str1, GOT_OBJ_TYPE_ANY, 1,
2286 error = match_object_id(&id2, &label2, id_str2, GOT_OBJ_TYPE_ANY, 1,
2291 error = got_object_get_type(&type1, repo, id1);
2295 error = got_object_get_type(&type2, repo, id2);
2299 if (type1 != type2) {
2300 error = got_error(GOT_ERR_OBJ_TYPE);
2305 case GOT_OBJ_TYPE_BLOB:
2306 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2307 diff_context, ignore_whitespace, repo, stdout);
2309 case GOT_OBJ_TYPE_TREE:
2310 error = got_diff_objects_as_trees(id1, id2, "", "",
2311 diff_context, ignore_whitespace, repo, stdout);
2313 case GOT_OBJ_TYPE_COMMIT:
2314 printf("diff %s %s\n", label1, label2);
2315 error = got_diff_objects_as_commits(id1, id2, diff_context,
2316 ignore_whitespace, repo, stdout);
2319 error = got_error(GOT_ERR_OBJ_TYPE);
2329 got_worktree_close(worktree);
2331 const struct got_error *repo_error;
2332 repo_error = got_repo_close(repo);
2343 "usage: %s blame [-c commit] [-r repository-path] path\n",
2352 char datebuf[9]; /* YY-MM-DD + NUL */
2355 struct blame_cb_args {
2356 struct blame_line *lines;
2360 off_t *line_offsets;
2362 struct got_repository *repo;
2365 static const struct got_error *
2366 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2368 const struct got_error *err = NULL;
2369 struct blame_cb_args *a = arg;
2370 struct blame_line *bline;
2372 size_t linesize = 0;
2373 struct got_commit_object *commit = NULL;
2376 time_t committer_time;
2378 if (nlines != a->nlines ||
2379 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2380 return got_error(GOT_ERR_RANGE);
2382 if (sigint_received)
2383 return got_error(GOT_ERR_ITER_COMPLETED);
2386 return NULL; /* no change in this commit */
2388 /* Annotate this line. */
2389 bline = &a->lines[lineno - 1];
2390 if (bline->annotated)
2392 err = got_object_id_str(&bline->id_str, id);
2396 err = got_object_open_as_commit(&commit, a->repo, id);
2400 bline->committer = strdup(got_object_commit_get_committer(commit));
2401 if (bline->committer == NULL) {
2402 err = got_error_from_errno("strdup");
2406 committer_time = got_object_commit_get_committer_time(commit);
2407 if (localtime_r(&committer_time, &tm) == NULL)
2408 return got_error_from_errno("localtime_r");
2409 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%g/%m/%d",
2410 &tm) >= sizeof(bline->datebuf)) {
2411 err = got_error(GOT_ERR_NO_SPACE);
2414 bline->annotated = 1;
2416 /* Print lines annotated so far. */
2417 bline = &a->lines[a->lineno_cur - 1];
2418 if (!bline->annotated)
2421 offset = a->line_offsets[a->lineno_cur - 1];
2422 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2423 err = got_error_from_errno("fseeko");
2427 while (bline->annotated) {
2428 char *smallerthan, *at, *nl, *committer;
2431 if (getline(&line, &linesize, a->f) == -1) {
2433 err = got_error_from_errno("getline");
2437 committer = bline->committer;
2438 smallerthan = strchr(committer, '<');
2439 if (smallerthan && smallerthan[1] != '\0')
2440 committer = smallerthan + 1;
2441 at = strchr(committer, '@');
2444 len = strlen(committer);
2446 committer[8] = '\0';
2448 nl = strchr(line, '\n');
2451 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2452 bline->id_str, bline->datebuf, committer, line);
2455 bline = &a->lines[a->lineno_cur - 1];
2459 got_object_commit_close(commit);
2464 static const struct got_error *
2465 cmd_blame(int argc, char *argv[])
2467 const struct got_error *error;
2468 struct got_repository *repo = NULL;
2469 struct got_worktree *worktree = NULL;
2470 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2471 struct got_object_id *obj_id = NULL;
2472 struct got_object_id *commit_id = NULL;
2473 struct got_blob_object *blob = NULL;
2474 char *commit_id_str = NULL;
2475 struct blame_cb_args bca;
2476 int ch, obj_type, i;
2479 memset(&bca, 0, sizeof(bca));
2482 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2487 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2490 commit_id_str = optarg;
2493 repo_path = realpath(optarg, NULL);
2494 if (repo_path == NULL)
2495 err(1, "-r option");
2496 got_path_strip_trailing_slashes(repo_path);
2512 cwd = getcwd(NULL, 0);
2514 error = got_error_from_errno("getcwd");
2517 if (repo_path == NULL) {
2518 error = got_worktree_open(&worktree, cwd);
2519 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2525 strdup(got_worktree_get_repo_path(worktree));
2526 if (repo_path == NULL)
2527 error = got_error_from_errno("strdup");
2531 repo_path = strdup(cwd);
2532 if (repo_path == NULL) {
2533 error = got_error_from_errno("strdup");
2539 error = got_repo_open(&repo, repo_path, NULL);
2543 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2548 const char *prefix = got_worktree_get_path_prefix(worktree);
2549 char *p, *worktree_subdir = cwd +
2550 strlen(got_worktree_get_root_path(worktree));
2551 if (asprintf(&p, "%s%s%s%s%s",
2552 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2553 worktree_subdir, worktree_subdir[0] ? "/" : "",
2555 error = got_error_from_errno("asprintf");
2558 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2561 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2566 if (commit_id_str == NULL) {
2567 struct got_reference *head_ref;
2568 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2571 error = got_ref_resolve(&commit_id, repo, head_ref);
2572 got_ref_close(head_ref);
2576 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2581 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2584 if (obj_id == NULL) {
2585 error = got_error(GOT_ERR_NO_OBJ);
2589 error = got_object_get_type(&obj_type, repo, obj_id);
2593 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2594 error = got_error(GOT_ERR_OBJ_TYPE);
2598 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2601 bca.f = got_opentemp();
2602 if (bca.f == NULL) {
2603 error = got_error_from_errno("got_opentemp");
2606 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2607 &bca.line_offsets, bca.f, blob);
2608 if (error || bca.nlines == 0)
2611 /* Don't include \n at EOF in the blame line count. */
2612 if (bca.line_offsets[bca.nlines - 1] == filesize)
2615 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2616 if (bca.lines == NULL) {
2617 error = got_error_from_errno("calloc");
2621 bca.nlines_prec = 0;
2629 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2630 check_cancelled, NULL);
2640 got_object_blob_close(blob);
2642 got_worktree_close(worktree);
2644 const struct got_error *repo_error;
2645 repo_error = got_repo_close(repo);
2650 for (i = 0; i < bca.nlines; i++) {
2651 struct blame_line *bline = &bca.lines[i];
2652 free(bline->id_str);
2653 free(bline->committer);
2657 free(bca.line_offsets);
2658 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2659 error = got_error_from_errno("fclose");
2667 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2673 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2674 const char *root_path)
2676 int is_root_path = (strcmp(path, root_path) == 0);
2677 const char *modestr = "";
2679 path += strlen(root_path);
2680 while (path[0] == '/')
2683 if (got_object_tree_entry_is_submodule(te))
2685 else if (S_ISLNK(te->mode))
2687 else if (S_ISDIR(te->mode))
2689 else if (te->mode & S_IXUSR)
2692 printf("%s%s%s%s%s\n", id ? id : "", path,
2693 is_root_path ? "" : "/", te->name, modestr);
2696 static const struct got_error *
2697 print_tree(const char *path, struct got_object_id *commit_id,
2698 int show_ids, int recurse, const char *root_path,
2699 struct got_repository *repo)
2701 const struct got_error *err = NULL;
2702 struct got_object_id *tree_id = NULL;
2703 struct got_tree_object *tree = NULL;
2704 const struct got_tree_entries *entries;
2705 struct got_tree_entry *te;
2707 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2711 err = got_object_open_as_tree(&tree, repo, tree_id);
2714 entries = got_object_tree_get_entries(tree);
2715 te = SIMPLEQ_FIRST(&entries->head);
2719 if (sigint_received || sigpipe_received)
2724 err = got_object_id_str(&id_str, te->id);
2727 if (asprintf(&id, "%s ", id_str) == -1) {
2728 err = got_error_from_errno("asprintf");
2734 print_entry(te, id, path, root_path);
2737 if (recurse && S_ISDIR(te->mode)) {
2739 if (asprintf(&child_path, "%s%s%s", path,
2740 path[0] == '/' && path[1] == '\0' ? "" : "/",
2742 err = got_error_from_errno("asprintf");
2745 err = print_tree(child_path, commit_id, show_ids, 1,
2752 te = SIMPLEQ_NEXT(te, entry);
2756 got_object_tree_close(tree);
2761 static const struct got_error *
2762 cmd_tree(int argc, char *argv[])
2764 const struct got_error *error;
2765 struct got_repository *repo = NULL;
2766 struct got_worktree *worktree = NULL;
2768 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2769 struct got_object_id *commit_id = NULL;
2770 char *commit_id_str = NULL;
2771 int show_ids = 0, recurse = 0;
2775 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2780 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2783 commit_id_str = optarg;
2786 repo_path = realpath(optarg, NULL);
2787 if (repo_path == NULL)
2788 err(1, "-r option");
2789 got_path_strip_trailing_slashes(repo_path);
2813 cwd = getcwd(NULL, 0);
2815 error = got_error_from_errno("getcwd");
2818 if (repo_path == NULL) {
2819 error = got_worktree_open(&worktree, cwd);
2820 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2826 strdup(got_worktree_get_repo_path(worktree));
2827 if (repo_path == NULL)
2828 error = got_error_from_errno("strdup");
2832 repo_path = strdup(cwd);
2833 if (repo_path == NULL) {
2834 error = got_error_from_errno("strdup");
2840 error = got_repo_open(&repo, repo_path, NULL);
2844 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2850 char *p, *worktree_subdir = cwd +
2851 strlen(got_worktree_get_root_path(worktree));
2852 if (asprintf(&p, "%s/%s",
2853 got_worktree_get_path_prefix(worktree),
2854 worktree_subdir) == -1) {
2855 error = got_error_from_errno("asprintf");
2858 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2865 if (in_repo_path == NULL) {
2866 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2871 if (commit_id_str == NULL) {
2872 struct got_reference *head_ref;
2873 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2876 error = got_ref_resolve(&commit_id, repo, head_ref);
2877 got_ref_close(head_ref);
2881 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2886 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2887 in_repo_path, repo);
2894 got_worktree_close(worktree);
2896 const struct got_error *repo_error;
2897 repo_error = got_repo_close(repo);
2907 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
2911 static const struct got_error *
2912 print_status(void *arg, unsigned char status, unsigned char staged_status,
2913 const char *path, struct got_object_id *blob_id,
2914 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
2916 if (status == staged_status && (status == GOT_STATUS_DELETE))
2917 status = GOT_STATUS_NO_CHANGE;
2918 printf("%c%c %s\n", status, staged_status, path);
2922 static const struct got_error *
2923 cmd_status(int argc, char *argv[])
2925 const struct got_error *error = NULL;
2926 struct got_repository *repo = NULL;
2927 struct got_worktree *worktree = NULL;
2929 struct got_pathlist_head paths;
2930 struct got_pathlist_entry *pe;
2935 while ((ch = getopt(argc, argv, "")) != -1) {
2947 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2951 cwd = getcwd(NULL, 0);
2953 error = got_error_from_errno("getcwd");
2957 error = got_worktree_open(&worktree, cwd);
2961 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
2966 error = apply_unveil(got_repo_get_path(repo), 1,
2967 got_worktree_get_root_path(worktree));
2971 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2975 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
2976 check_cancelled, NULL);
2978 TAILQ_FOREACH(pe, &paths, entry)
2979 free((char *)pe->path);
2980 got_pathlist_free(&paths);
2989 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
2994 static const struct got_error *
2995 list_refs(struct got_repository *repo)
2997 static const struct got_error *err = NULL;
2998 struct got_reflist_head refs;
2999 struct got_reflist_entry *re;
3001 SIMPLEQ_INIT(&refs);
3002 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3006 SIMPLEQ_FOREACH(re, &refs, entry) {
3008 refstr = got_ref_to_str(re->ref);
3010 return got_error_from_errno("got_ref_to_str");
3011 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3015 got_ref_list_free(&refs);
3019 static const struct got_error *
3020 delete_ref(struct got_repository *repo, const char *refname)
3022 const struct got_error *err = NULL;
3023 struct got_reference *ref;
3025 err = got_ref_open(&ref, repo, refname, 0);
3029 err = got_ref_delete(ref, repo);
3034 static const struct got_error *
3035 add_ref(struct got_repository *repo, const char *refname, const char *target)
3037 const struct got_error *err = NULL;
3038 struct got_object_id *id;
3039 struct got_reference *ref = NULL;
3042 * Don't let the user create a reference named '-'.
3043 * While technically a valid reference name, this case is usually
3044 * an unintended typo.
3046 if (refname[0] == '-' && refname[1] == '\0')
3047 return got_error_path(refname, GOT_ERR_BAD_REF_NAME);
3049 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3052 struct got_reference *target_ref;
3054 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3056 err = got_ref_open(&target_ref, repo, target, 0);
3059 err = got_ref_resolve(&id, repo, target_ref);
3060 got_ref_close(target_ref);
3065 err = got_ref_alloc(&ref, refname, id);
3069 err = got_ref_write(ref, repo);
3077 static const struct got_error *
3078 add_symref(struct got_repository *repo, const char *refname, const char *target)
3080 const struct got_error *err = NULL;
3081 struct got_reference *ref = NULL;
3082 struct got_reference *target_ref = NULL;
3085 * Don't let the user create a reference named '-'.
3086 * While technically a valid reference name, this case is usually
3087 * an unintended typo.
3089 if (refname[0] == '-' && refname[1] == '\0')
3090 return got_error_path(refname, GOT_ERR_BAD_REF_NAME);
3092 err = got_ref_open(&target_ref, repo, target, 0);
3096 err = got_ref_alloc_symref(&ref, refname, target_ref);
3100 err = got_ref_write(ref, repo);
3103 got_ref_close(target_ref);
3109 static const struct got_error *
3110 cmd_ref(int argc, char *argv[])
3112 const struct got_error *error = NULL;
3113 struct got_repository *repo = NULL;
3114 struct got_worktree *worktree = NULL;
3115 char *cwd = NULL, *repo_path = NULL;
3116 int ch, do_list = 0, create_symref = 0;
3117 const char *delref = NULL;
3119 /* TODO: Add -s option for adding symbolic references. */
3120 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3126 repo_path = realpath(optarg, NULL);
3127 if (repo_path == NULL)
3128 err(1, "-r option");
3129 got_path_strip_trailing_slashes(repo_path);
3143 if (do_list && delref)
3144 errx(1, "-l and -d options are mutually exclusive\n");
3149 if (do_list || delref) {
3151 errx(1, "-s option cannot be used together with the "
3152 "-l or -d options");
3155 } else if (argc != 2)
3160 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3164 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3165 "sendfd unveil", NULL) == -1)
3169 cwd = getcwd(NULL, 0);
3171 error = got_error_from_errno("getcwd");
3175 if (repo_path == NULL) {
3176 error = got_worktree_open(&worktree, cwd);
3177 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3183 strdup(got_worktree_get_repo_path(worktree));
3184 if (repo_path == NULL)
3185 error = got_error_from_errno("strdup");
3189 repo_path = strdup(cwd);
3190 if (repo_path == NULL) {
3191 error = got_error_from_errno("strdup");
3197 error = got_repo_open(&repo, repo_path, NULL);
3201 error = apply_unveil(got_repo_get_path(repo), do_list,
3202 worktree ? got_worktree_get_root_path(worktree) : NULL);
3207 error = list_refs(repo);
3209 error = delete_ref(repo, delref);
3210 else if (create_symref)
3211 error = add_symref(repo, argv[0], argv[1]);
3213 error = add_ref(repo, argv[0], argv[1]);
3216 got_repo_close(repo);
3218 got_worktree_close(worktree);
3228 "usage: %s branch [-r repository] [-l] | -d name | "
3229 "[name [commit]]\n", getprogname());
3233 static const struct got_error *
3234 list_branch(struct got_repository *repo, struct got_worktree *worktree,
3235 struct got_reference *ref)
3237 const struct got_error *err = NULL;
3238 const char *refname, *marker = " ";
3241 refname = got_ref_get_name(ref);
3242 if (worktree && strcmp(refname,
3243 got_worktree_get_head_ref_name(worktree)) == 0) {
3244 struct got_object_id *id = NULL;
3246 err = got_ref_resolve(&id, repo, ref);
3249 if (got_object_id_cmp(id,
3250 got_worktree_get_base_commit_id(worktree)) == 0)
3257 if (strncmp(refname, "refs/heads/", 11) == 0)
3259 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3262 refstr = got_ref_to_str(ref);
3264 return got_error_from_errno("got_ref_to_str");
3266 printf("%s%s: %s\n", marker, refname, refstr);
3271 static const struct got_error *
3272 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
3274 const char *refname;
3276 if (worktree == NULL)
3277 return got_error(GOT_ERR_NOT_WORKTREE);
3279 refname = got_worktree_get_head_ref_name(worktree);
3281 if (strncmp(refname, "refs/heads/", 11) == 0)
3283 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3286 printf("%s\n", refname);
3291 static const struct got_error *
3292 list_branches(struct got_repository *repo, struct got_worktree *worktree)
3294 static const struct got_error *err = NULL;
3295 struct got_reflist_head refs;
3296 struct got_reflist_entry *re;
3297 struct got_reference *temp_ref = NULL;
3298 int rebase_in_progress, histedit_in_progress;
3300 SIMPLEQ_INIT(&refs);
3303 err = got_worktree_rebase_in_progress(&rebase_in_progress,
3308 err = got_worktree_histedit_in_progress(&histedit_in_progress,
3313 if (rebase_in_progress || histedit_in_progress) {
3314 err = got_ref_open(&temp_ref, repo,
3315 got_worktree_get_head_ref_name(worktree), 0);
3318 list_branch(repo, worktree, temp_ref);
3319 got_ref_close(temp_ref);
3323 err = got_ref_list(&refs, repo, "refs/heads",
3324 got_ref_cmp_by_name, NULL);
3328 SIMPLEQ_FOREACH(re, &refs, entry)
3329 list_branch(repo, worktree, re->ref);
3331 got_ref_list_free(&refs);
3335 static const struct got_error *
3336 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
3337 const char *branch_name)
3339 const struct got_error *err = NULL;
3340 struct got_reference *ref = NULL;
3343 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3344 return got_error_from_errno("asprintf");
3346 err = got_ref_open(&ref, repo, refname, 0);
3351 strcmp(got_worktree_get_head_ref_name(worktree),
3352 got_ref_get_name(ref)) == 0) {
3353 err = got_error_msg(GOT_ERR_SAME_BRANCH,
3354 "will not delete this work tree's current branch");
3358 err = got_ref_delete(ref, repo);
3366 static const struct got_error *
3367 add_branch(struct got_repository *repo, const char *branch_name,
3368 const char *base_branch)
3370 const struct got_error *err = NULL;
3371 struct got_object_id *id = NULL;
3373 struct got_reference *ref = NULL;
3374 char *base_refname = NULL, *refname = NULL;
3377 * Don't let the user create a branch named '-'.
3378 * While technically a valid reference name, this case is usually
3379 * an unintended typo.
3381 if (branch_name[0] == '-' && branch_name[1] == '\0')
3382 return got_error_path(branch_name, GOT_ERR_BAD_REF_NAME);
3384 err = match_object_id(&id, &label, base_branch,
3385 GOT_OBJ_TYPE_COMMIT, 1, repo);
3389 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3390 err = got_error_from_errno("asprintf");
3394 err = got_ref_open(&ref, repo, refname, 0);
3396 err = got_error(GOT_ERR_BRANCH_EXISTS);
3398 } else if (err->code != GOT_ERR_NOT_REF)
3401 err = got_ref_alloc(&ref, refname, id);
3405 err = got_ref_write(ref, repo);
3415 static const struct got_error *
3416 cmd_branch(int argc, char *argv[])
3418 const struct got_error *error = NULL;
3419 struct got_repository *repo = NULL;
3420 struct got_worktree *worktree = NULL;
3421 char *cwd = NULL, *repo_path = NULL;
3422 int ch, do_list = 0, do_show = 0;
3423 const char *delref = NULL;
3425 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
3431 repo_path = realpath(optarg, NULL);
3432 if (repo_path == NULL)
3433 err(1, "-r option");
3434 got_path_strip_trailing_slashes(repo_path);
3445 if (do_list && delref)
3446 errx(1, "-l and -d options are mutually exclusive\n");
3451 if (!do_list && !delref && argc == 0)
3454 if (do_list || delref) {
3457 } else if (!do_show && (argc < 1 || argc > 2))
3461 if (do_list || do_show) {
3462 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3466 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3467 "sendfd unveil", NULL) == -1)
3471 cwd = getcwd(NULL, 0);
3473 error = got_error_from_errno("getcwd");
3477 if (repo_path == NULL) {
3478 error = got_worktree_open(&worktree, cwd);
3479 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3485 strdup(got_worktree_get_repo_path(worktree));
3486 if (repo_path == NULL)
3487 error = got_error_from_errno("strdup");
3491 repo_path = strdup(cwd);
3492 if (repo_path == NULL) {
3493 error = got_error_from_errno("strdup");
3499 error = got_repo_open(&repo, repo_path, NULL);
3503 error = apply_unveil(got_repo_get_path(repo), do_list,
3504 worktree ? got_worktree_get_root_path(worktree) : NULL);
3509 error = show_current_branch(repo, worktree);
3511 error = list_branches(repo, worktree);
3513 error = delete_branch(repo, worktree, delref);
3515 const char *base_branch;
3517 base_branch = worktree ?
3518 got_worktree_get_head_ref_name(worktree) :
3520 if (strncmp(base_branch, "refs/heads/", 11) == 0)
3523 base_branch = argv[1];
3524 error = add_branch(repo, argv[0], base_branch);
3528 got_repo_close(repo);
3530 got_worktree_close(worktree);
3541 "usage: %s tag [-r repository] | -l | "
3542 "[-m message] name [commit]\n", getprogname());
3547 static const struct got_error *
3548 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
3550 const struct got_error *err = NULL;
3551 struct got_reflist_entry *re, *se, *new;
3552 struct got_object_id *re_id, *se_id;
3553 struct got_tag_object *re_tag, *se_tag;
3554 time_t re_time, se_time;
3556 SIMPLEQ_FOREACH(re, tags, entry) {
3557 se = SIMPLEQ_FIRST(sorted);
3559 err = got_reflist_entry_dup(&new, re);
3562 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
3565 err = got_ref_resolve(&re_id, repo, re->ref);
3568 err = got_object_open_as_tag(&re_tag, repo, re_id);
3572 re_time = got_object_tag_get_tagger_time(re_tag);
3573 got_object_tag_close(re_tag);
3577 err = got_ref_resolve(&se_id, repo, re->ref);
3580 err = got_object_open_as_tag(&se_tag, repo, se_id);
3584 se_time = got_object_tag_get_tagger_time(se_tag);
3585 got_object_tag_close(se_tag);
3587 if (se_time > re_time) {
3588 err = got_reflist_entry_dup(&new, re);
3591 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
3594 se = SIMPLEQ_NEXT(se, entry);
3603 static const struct got_error *
3604 cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
3605 struct got_reference *ref2)
3607 const struct got_error *err = NULL;
3608 struct got_repository *repo = arg;
3609 struct got_object_id *id1, *id2 = NULL;
3610 struct got_tag_object *tag1 = NULL, *tag2 = NULL;
3611 time_t time1, time2;
3615 err = got_ref_resolve(&id1, repo, ref1);
3618 err = got_object_open_as_tag(&tag1, repo, id1);
3622 err = got_ref_resolve(&id2, repo, ref2);
3625 err = got_object_open_as_tag(&tag2, repo, id2);
3629 time1 = got_object_tag_get_tagger_time(tag1);
3630 time2 = got_object_tag_get_tagger_time(tag2);
3632 /* Put latest tags first. */
3635 else if (time1 > time2)
3638 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
3643 got_object_tag_close(tag1);
3645 got_object_tag_close(tag2);
3649 static const struct got_error *
3650 list_tags(struct got_repository *repo, struct got_worktree *worktree)
3652 static const struct got_error *err = NULL;
3653 struct got_reflist_head refs;
3654 struct got_reflist_entry *re;
3656 SIMPLEQ_INIT(&refs);
3658 err = got_ref_list(&refs, repo, "refs/tags", cmp_tags, repo);
3662 SIMPLEQ_FOREACH(re, &refs, entry) {
3663 const char *refname;
3664 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
3667 struct got_object_id *id;
3668 struct got_tag_object *tag;
3670 refname = got_ref_get_name(re->ref);
3671 if (strncmp(refname, "refs/tags/", 10) != 0)
3674 refstr = got_ref_to_str(re->ref);
3675 if (refstr == NULL) {
3676 err = got_error_from_errno("got_ref_to_str");
3679 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
3682 err = got_ref_resolve(&id, repo, re->ref);
3685 err = got_object_open_as_tag(&tag, repo, id);
3689 printf("from: %s\n", got_object_tag_get_tagger(tag));
3690 tagger_time = got_object_tag_get_tagger_time(tag);
3691 datestr = get_datestr(&tagger_time, datebuf);
3693 printf("date: %s UTC\n", datestr);
3694 err = got_object_id_str(&id_str,
3695 got_object_tag_get_object_id(tag));
3698 switch (got_object_tag_get_object_type(tag)) {
3699 case GOT_OBJ_TYPE_BLOB:
3700 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB, id_str);
3702 case GOT_OBJ_TYPE_TREE:
3703 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE, id_str);
3705 case GOT_OBJ_TYPE_COMMIT:
3706 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
3708 case GOT_OBJ_TYPE_TAG:
3709 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG, id_str);
3715 tagmsg0 = strdup(got_object_tag_get_message(tag));
3716 got_object_tag_close(tag);
3717 if (tagmsg0 == NULL) {
3718 err = got_error_from_errno("strdup");
3724 line = strsep(&tagmsg, "\n");
3726 printf(" %s\n", line);
3731 got_ref_list_free(&refs);
3735 static const struct got_error *
3736 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
3737 const char *tag_name, const char *repo_path)
3739 const struct got_error *err = NULL;
3740 char *template = NULL, *initial_content = NULL;
3741 char *editor = NULL;
3744 if (asprintf(&template, "/tmp/got-tagmsg") == -1) {
3745 err = got_error_from_errno("asprintf");
3749 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
3750 commit_id_str, tag_name) == -1) {
3751 err = got_error_from_errno("asprintf");
3755 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
3759 dprintf(fd, initial_content);
3762 err = get_editor(&editor);
3765 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
3767 free(initial_content);
3771 /* Editor is done; we can now apply unveil(2) */
3773 err = apply_unveil(repo_path, 0, NULL);
3782 static const struct got_error *
3783 add_tag(struct got_repository *repo, const char *tag_name,
3784 const char *commit_arg, const char *tagmsg_arg)
3786 const struct got_error *err = NULL;
3787 struct got_object_id *commit_id = NULL, *tag_id = NULL;
3788 char *label = NULL, *commit_id_str = NULL;
3789 struct got_reference *ref = NULL;
3790 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
3791 char *tagmsg_path = NULL, *tag_id_str = NULL;
3792 int preserve_tagmsg = 0;
3795 * Don't let the user create a tag named '-'.
3796 * While technically a valid reference name, this case is usually
3797 * an unintended typo.
3799 if (tag_name[0] == '-' && tag_name[1] == '\0')
3800 return got_error_path(tag_name, GOT_ERR_BAD_REF_NAME);
3802 err = get_author(&tagger, repo);
3806 err = match_object_id(&commit_id, &label, commit_arg,
3807 GOT_OBJ_TYPE_COMMIT, 1, repo);
3811 err = got_object_id_str(&commit_id_str, commit_id);
3815 if (strncmp("refs/tags/", tag_name, 10) == 0) {
3816 refname = strdup(tag_name);
3817 if (refname == NULL) {
3818 err = got_error_from_errno("strdup");
3822 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
3823 err = got_error_from_errno("asprintf");
3827 err = got_ref_open(&ref, repo, refname, 0);
3829 err = got_error(GOT_ERR_TAG_EXISTS);
3831 } else if (err->code != GOT_ERR_NOT_REF)
3834 if (tagmsg_arg == NULL) {
3835 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
3836 tag_name, got_repo_get_path(repo));
3838 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
3839 tagmsg_path != NULL)
3840 preserve_tagmsg = 1;
3845 err = got_object_tag_create(&tag_id, tag_name, commit_id,
3846 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
3849 preserve_tagmsg = 1;
3853 err = got_ref_alloc(&ref, refname, tag_id);
3856 preserve_tagmsg = 1;
3860 err = got_ref_write(ref, repo);
3863 preserve_tagmsg = 1;
3867 err = got_object_id_str(&tag_id_str, tag_id);
3870 preserve_tagmsg = 1;
3873 printf("Created tag %s\n", tag_id_str);
3875 if (preserve_tagmsg) {
3876 fprintf(stderr, "%s: tag message preserved in %s\n",
3877 getprogname(), tagmsg_path);
3878 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
3879 err = got_error_from_errno2("unlink", tagmsg_path);
3884 free(commit_id_str);
3892 static const struct got_error *
3893 cmd_tag(int argc, char *argv[])
3895 const struct got_error *error = NULL;
3896 struct got_repository *repo = NULL;
3897 struct got_worktree *worktree = NULL;
3898 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
3899 char *gitconfig_path = NULL;
3900 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
3901 int ch, do_list = 0;
3903 while ((ch = getopt(argc, argv, "m:r:l")) != -1) {
3909 repo_path = realpath(optarg, NULL);
3910 if (repo_path == NULL)
3911 err(1, "-r option");
3912 got_path_strip_trailing_slashes(repo_path);
3928 errx(1, "-l and -m options are mutually exclusive\n");
3931 } else if (argc < 1 || argc > 2)
3934 commit_id_arg = argv[1];
3939 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3943 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3944 "sendfd unveil", NULL) == -1)
3948 cwd = getcwd(NULL, 0);
3950 error = got_error_from_errno("getcwd");
3954 if (repo_path == NULL) {
3955 error = got_worktree_open(&worktree, cwd);
3956 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3962 strdup(got_worktree_get_repo_path(worktree));
3963 if (repo_path == NULL)
3964 error = got_error_from_errno("strdup");
3968 repo_path = strdup(cwd);
3969 if (repo_path == NULL) {
3970 error = got_error_from_errno("strdup");
3977 error = got_repo_open(&repo, repo_path, NULL);
3980 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3983 error = list_tags(repo, worktree);
3985 error = get_gitconfig_path(&gitconfig_path);
3988 error = got_repo_open(&repo, repo_path, gitconfig_path);
3993 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3998 if (commit_id_arg == NULL) {
3999 struct got_reference *head_ref;
4000 struct got_object_id *commit_id;
4001 error = got_ref_open(&head_ref, repo,
4002 worktree ? got_worktree_get_head_ref_name(worktree)
4006 error = got_ref_resolve(&commit_id, repo, head_ref);
4007 got_ref_close(head_ref);
4010 error = got_object_id_str(&commit_id_str, commit_id);
4016 error = add_tag(repo, tag_name,
4017 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4021 got_repo_close(repo);
4023 got_worktree_close(worktree);
4026 free(gitconfig_path);
4027 free(commit_id_str);
4034 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
4038 static const struct got_error *
4039 cmd_add(int argc, char *argv[])
4041 const struct got_error *error = NULL;
4042 struct got_repository *repo = NULL;
4043 struct got_worktree *worktree = NULL;
4045 struct got_pathlist_head paths;
4046 struct got_pathlist_entry *pe;
4051 while ((ch = getopt(argc, argv, "")) != -1) {
4063 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4070 cwd = getcwd(NULL, 0);
4072 error = got_error_from_errno("getcwd");
4076 error = got_worktree_open(&worktree, cwd);
4080 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4085 error = apply_unveil(got_repo_get_path(repo), 1,
4086 got_worktree_get_root_path(worktree));
4090 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4094 error = got_worktree_schedule_add(worktree, &paths, print_status,
4098 got_repo_close(repo);
4100 got_worktree_close(worktree);
4101 TAILQ_FOREACH(pe, &paths, entry)
4102 free((char *)pe->path);
4103 got_pathlist_free(&paths);
4111 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
4115 static const struct got_error *
4116 print_remove_status(void *arg, unsigned char status,
4117 unsigned char staged_status, const char *path,
4118 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4119 struct got_object_id *commit_id)
4121 if (status == GOT_STATUS_NONEXISTENT)
4123 if (status == staged_status && (status == GOT_STATUS_DELETE))
4124 status = GOT_STATUS_NO_CHANGE;
4125 printf("%c%c %s\n", status, staged_status, path);
4129 static const struct got_error *
4130 cmd_remove(int argc, char *argv[])
4132 const struct got_error *error = NULL;
4133 struct got_worktree *worktree = NULL;
4134 struct got_repository *repo = NULL;
4136 struct got_pathlist_head paths;
4137 struct got_pathlist_entry *pe;
4138 int ch, delete_local_mods = 0;
4142 while ((ch = getopt(argc, argv, "f")) != -1) {
4145 delete_local_mods = 1;
4157 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4164 cwd = getcwd(NULL, 0);
4166 error = got_error_from_errno("getcwd");
4169 error = got_worktree_open(&worktree, cwd);
4173 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4178 error = apply_unveil(got_repo_get_path(repo), 1,
4179 got_worktree_get_root_path(worktree));
4183 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4187 error = got_worktree_schedule_delete(worktree, &paths,
4188 delete_local_mods, print_remove_status, NULL, repo);
4193 got_repo_close(repo);
4195 got_worktree_close(worktree);
4196 TAILQ_FOREACH(pe, &paths, entry)
4197 free((char *)pe->path);
4198 got_pathlist_free(&paths);
4206 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4207 "path ...\n", getprogname());
4211 static const struct got_error *
4212 revert_progress(void *arg, unsigned char status, const char *path)
4214 while (path[0] == '/')
4216 printf("%c %s\n", status, path);
4220 struct choose_patch_arg {
4221 FILE *patch_script_file;
4225 static const struct got_error *
4226 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4227 int nchanges, const char *action)
4230 size_t linesize = 0;
4234 case GOT_STATUS_ADD:
4235 printf("A %s\n%s this addition? [y/n] ", path, action);
4237 case GOT_STATUS_DELETE:
4238 printf("D %s\n%s this deletion? [y/n] ", path, action);
4240 case GOT_STATUS_MODIFY:
4241 if (fseek(patch_file, 0L, SEEK_SET) == -1)
4242 return got_error_from_errno("fseek");
4243 printf(GOT_COMMIT_SEP_STR);
4244 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4246 if (ferror(patch_file))
4247 return got_error_from_errno("getline");
4248 printf(GOT_COMMIT_SEP_STR);
4249 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4250 path, n, nchanges, action);
4253 return got_error_path(path, GOT_ERR_FILE_STATUS);
4259 static const struct got_error *
4260 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4261 FILE *patch_file, int n, int nchanges)
4263 const struct got_error *err = NULL;
4265 size_t linesize = 0;
4268 struct choose_patch_arg *a = arg;
4270 *choice = GOT_PATCH_CHOICE_NONE;
4272 if (a->patch_script_file) {
4274 err = show_change(status, path, patch_file, n, nchanges,
4278 linelen = getline(&line, &linesize, a->patch_script_file);
4279 if (linelen == -1) {
4280 if (ferror(a->patch_script_file))
4281 return got_error_from_errno("getline");
4284 nl = strchr(line, '\n');
4287 if (strcmp(line, "y") == 0) {
4288 *choice = GOT_PATCH_CHOICE_YES;
4290 } else if (strcmp(line, "n") == 0) {
4291 *choice = GOT_PATCH_CHOICE_NO;
4293 } else if (strcmp(line, "q") == 0 &&
4294 status == GOT_STATUS_MODIFY) {
4295 *choice = GOT_PATCH_CHOICE_QUIT;
4298 printf("invalid response '%s'\n", line);
4303 while (resp != 'y' && resp != 'n' && resp != 'q') {
4304 err = show_change(status, path, patch_file, n, nchanges,
4311 if (status == GOT_STATUS_MODIFY) {
4312 if (resp != 'y' && resp != 'n' && resp != 'q') {
4313 printf("invalid response '%c'\n", resp);
4316 } else if (resp != 'y' && resp != 'n') {
4317 printf("invalid response '%c'\n", resp);
4323 *choice = GOT_PATCH_CHOICE_YES;
4324 else if (resp == 'n')
4325 *choice = GOT_PATCH_CHOICE_NO;
4326 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4327 *choice = GOT_PATCH_CHOICE_QUIT;
4333 static const struct got_error *
4334 cmd_revert(int argc, char *argv[])
4336 const struct got_error *error = NULL;
4337 struct got_worktree *worktree = NULL;
4338 struct got_repository *repo = NULL;
4339 char *cwd = NULL, *path = NULL;
4340 struct got_pathlist_head paths;
4341 struct got_pathlist_entry *pe;
4342 int ch, can_recurse = 0, pflag = 0;
4343 FILE *patch_script_file = NULL;
4344 const char *patch_script_path = NULL;
4345 struct choose_patch_arg cpa;
4349 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4355 patch_script_path = optarg;
4370 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4371 "unveil", NULL) == -1)
4376 if (patch_script_path && !pflag)
4377 errx(1, "-F option can only be used together with -p option");
4379 cwd = getcwd(NULL, 0);
4381 error = got_error_from_errno("getcwd");
4384 error = got_worktree_open(&worktree, cwd);
4388 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4393 if (patch_script_path) {
4394 patch_script_file = fopen(patch_script_path, "r");
4395 if (patch_script_file == NULL) {
4396 error = got_error_from_errno2("fopen",
4401 error = apply_unveil(got_repo_get_path(repo), 1,
4402 got_worktree_get_root_path(worktree));
4406 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4413 TAILQ_FOREACH(pe, &paths, entry) {
4414 if (asprintf(&ondisk_path, "%s/%s",
4415 got_worktree_get_root_path(worktree),
4417 error = got_error_from_errno("asprintf");
4420 if (lstat(ondisk_path, &sb) == -1) {
4421 if (errno == ENOENT) {
4425 error = got_error_from_errno2("lstat",
4431 if (S_ISDIR(sb.st_mode)) {
4432 error = got_error_msg(GOT_ERR_BAD_PATH,
4433 "reverting directories requires -R option");
4439 cpa.patch_script_file = patch_script_file;
4440 cpa.action = "revert";
4441 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4442 pflag ? choose_patch : NULL, &cpa, repo);
4446 if (patch_script_file && fclose(patch_script_file) == EOF &&
4448 error = got_error_from_errno2("fclose", patch_script_path);
4450 got_repo_close(repo);
4452 got_worktree_close(worktree);
4461 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4466 struct collect_commit_logmsg_arg {
4467 const char *cmdline_log;
4469 const char *worktree_path;
4470 const char *branch_name;
4471 const char *repo_path;
4476 static const struct got_error *
4477 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4480 char *initial_content = NULL;
4481 struct got_pathlist_entry *pe;
4482 const struct got_error *err = NULL;
4483 char *template = NULL;
4484 struct collect_commit_logmsg_arg *a = arg;
4488 /* if a message was specified on the command line, just use it */
4489 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4490 len = strlen(a->cmdline_log) + 1;
4491 *logmsg = malloc(len + 1);
4492 if (*logmsg == NULL)
4493 return got_error_from_errno("malloc");
4494 strlcpy(*logmsg, a->cmdline_log, len);
4498 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
4499 return got_error_from_errno("asprintf");
4501 if (asprintf(&initial_content,
4502 "\n# changes to be committed on branch %s:\n",
4503 a->branch_name) == -1)
4504 return got_error_from_errno("asprintf");
4506 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
4510 dprintf(fd, initial_content);
4512 TAILQ_FOREACH(pe, commitable_paths, entry) {
4513 struct got_commitable *ct = pe->data;
4514 dprintf(fd, "# %c %s\n",
4515 got_commitable_get_status(ct),
4516 got_commitable_get_path(ct));
4520 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
4522 free(initial_content);
4525 /* Editor is done; we can now apply unveil(2) */
4527 err = apply_unveil(a->repo_path, 0, a->worktree_path);
4536 static const struct got_error *
4537 cmd_commit(int argc, char *argv[])
4539 const struct got_error *error = NULL;
4540 struct got_worktree *worktree = NULL;
4541 struct got_repository *repo = NULL;
4542 char *cwd = NULL, *id_str = NULL;
4543 struct got_object_id *id = NULL;
4544 const char *logmsg = NULL;
4545 struct collect_commit_logmsg_arg cl_arg;
4546 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
4547 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
4548 struct got_pathlist_head paths;
4551 cl_arg.logmsg_path = NULL;
4553 while ((ch = getopt(argc, argv, "m:")) != -1) {
4568 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4569 "unveil", NULL) == -1)
4572 cwd = getcwd(NULL, 0);
4574 error = got_error_from_errno("getcwd");
4577 error = got_worktree_open(&worktree, cwd);
4581 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4584 if (rebase_in_progress) {
4585 error = got_error(GOT_ERR_REBASING);
4589 error = got_worktree_histedit_in_progress(&histedit_in_progress,
4594 error = get_gitconfig_path(&gitconfig_path);
4597 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4602 error = get_author(&author, repo);
4607 * unveil(2) traverses exec(2); if an editor is used we have
4608 * to apply unveil after the log message has been written.
4610 if (logmsg == NULL || strlen(logmsg) == 0)
4611 error = get_editor(&editor);
4613 error = apply_unveil(got_repo_get_path(repo), 0,
4614 got_worktree_get_root_path(worktree));
4618 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4622 cl_arg.editor = editor;
4623 cl_arg.cmdline_log = logmsg;
4624 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
4625 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
4626 if (!histedit_in_progress) {
4627 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
4628 error = got_error(GOT_ERR_COMMIT_BRANCH);
4631 cl_arg.branch_name += 11;
4633 cl_arg.repo_path = got_repo_get_path(repo);
4634 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
4635 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
4637 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4638 cl_arg.logmsg_path != NULL)
4639 preserve_logmsg = 1;
4643 error = got_object_id_str(&id_str, id);
4646 printf("Created commit %s\n", id_str);
4648 if (preserve_logmsg) {
4649 fprintf(stderr, "%s: log message preserved in %s\n",
4650 getprogname(), cl_arg.logmsg_path);
4651 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
4653 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
4654 free(cl_arg.logmsg_path);
4656 got_repo_close(repo);
4658 got_worktree_close(worktree);
4661 free(gitconfig_path);
4668 usage_cherrypick(void)
4670 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
4674 static const struct got_error *
4675 cmd_cherrypick(int argc, char *argv[])
4677 const struct got_error *error = NULL;
4678 struct got_worktree *worktree = NULL;
4679 struct got_repository *repo = NULL;
4680 char *cwd = NULL, *commit_id_str = NULL;
4681 struct got_object_id *commit_id = NULL;
4682 struct got_commit_object *commit = NULL;
4683 struct got_object_qid *pid;
4684 struct got_reference *head_ref = NULL;
4685 int ch, did_something = 0;
4687 while ((ch = getopt(argc, argv, "")) != -1) {
4699 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4700 "unveil", NULL) == -1)
4706 cwd = getcwd(NULL, 0);
4708 error = got_error_from_errno("getcwd");
4711 error = got_worktree_open(&worktree, cwd);
4715 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4720 error = apply_unveil(got_repo_get_path(repo), 0,
4721 got_worktree_get_root_path(worktree));
4725 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4726 GOT_OBJ_TYPE_COMMIT, repo);
4727 if (error != NULL) {
4728 struct got_reference *ref;
4729 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4731 error = got_ref_open(&ref, repo, argv[0], 0);
4734 error = got_ref_resolve(&commit_id, repo, ref);
4739 error = got_object_id_str(&commit_id_str, commit_id);
4743 error = got_ref_open(&head_ref, repo,
4744 got_worktree_get_head_ref_name(worktree), 0);
4748 error = check_same_branch(commit_id, head_ref, NULL, repo);
4750 if (error->code != GOT_ERR_ANCESTRY)
4754 error = got_error(GOT_ERR_SAME_BRANCH);
4758 error = got_object_open_as_commit(&commit, repo, commit_id);
4761 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4762 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
4763 commit_id, repo, update_progress, &did_something, check_cancelled,
4769 printf("Merged commit %s\n", commit_id_str);
4772 got_object_commit_close(commit);
4773 free(commit_id_str);
4775 got_ref_close(head_ref);
4777 got_worktree_close(worktree);
4779 got_repo_close(repo);
4786 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
4790 static const struct got_error *
4791 cmd_backout(int argc, char *argv[])
4793 const struct got_error *error = NULL;
4794 struct got_worktree *worktree = NULL;
4795 struct got_repository *repo = NULL;
4796 char *cwd = NULL, *commit_id_str = NULL;
4797 struct got_object_id *commit_id = NULL;
4798 struct got_commit_object *commit = NULL;
4799 struct got_object_qid *pid;
4800 struct got_reference *head_ref = NULL;
4801 int ch, did_something = 0;
4803 while ((ch = getopt(argc, argv, "")) != -1) {
4815 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4816 "unveil", NULL) == -1)
4822 cwd = getcwd(NULL, 0);
4824 error = got_error_from_errno("getcwd");
4827 error = got_worktree_open(&worktree, cwd);
4831 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4836 error = apply_unveil(got_repo_get_path(repo), 0,
4837 got_worktree_get_root_path(worktree));
4841 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4842 GOT_OBJ_TYPE_COMMIT, repo);
4843 if (error != NULL) {
4844 struct got_reference *ref;
4845 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4847 error = got_ref_open(&ref, repo, argv[0], 0);
4850 error = got_ref_resolve(&commit_id, repo, ref);
4855 error = got_object_id_str(&commit_id_str, commit_id);
4859 error = got_ref_open(&head_ref, repo,
4860 got_worktree_get_head_ref_name(worktree), 0);
4864 error = check_same_branch(commit_id, head_ref, NULL, repo);
4868 error = got_object_open_as_commit(&commit, repo, commit_id);
4871 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4873 error = got_error(GOT_ERR_ROOT_COMMIT);
4877 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
4878 update_progress, &did_something, check_cancelled, NULL);
4883 printf("Backed out commit %s\n", commit_id_str);
4886 got_object_commit_close(commit);
4887 free(commit_id_str);
4889 got_ref_close(head_ref);
4891 got_worktree_close(worktree);
4893 got_repo_close(repo);
4900 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
4906 trim_logmsg(char *logmsg, int limit)
4911 len = strlen(logmsg);
4915 nl = strchr(logmsg, '\n');
4920 static const struct got_error *
4921 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
4923 const struct got_error *err;
4924 char *logmsg0 = NULL;
4927 err = got_object_commit_get_logmsg(&logmsg0, commit);
4932 while (isspace((unsigned char)s[0]))
4935 *logmsg = strdup(s);
4936 if (*logmsg == NULL) {
4937 err = got_error_from_errno("strdup");
4941 trim_logmsg(*logmsg, limit);
4947 static const struct got_error *
4948 show_rebase_progress(struct got_commit_object *commit,
4949 struct got_object_id *old_id, struct got_object_id *new_id)
4951 const struct got_error *err;
4952 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
4954 err = got_object_id_str(&old_id_str, old_id);
4959 err = got_object_id_str(&new_id_str, new_id);
4964 old_id_str[12] = '\0';
4966 new_id_str[12] = '\0';
4968 err = get_short_logmsg(&logmsg, 42, commit);
4972 printf("%s -> %s: %s\n", old_id_str,
4973 new_id_str ? new_id_str : "no-op change", logmsg);
4980 static const struct got_error *
4981 rebase_progress(void *arg, unsigned char status, const char *path)
4983 unsigned char *rebase_status = arg;
4985 while (path[0] == '/')
4987 printf("%c %s\n", status, path);
4989 if (*rebase_status == GOT_STATUS_CONFLICT)
4991 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
4992 *rebase_status = status;
4996 static const struct got_error *
4997 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
4998 struct got_reference *branch, struct got_reference *new_base_branch,
4999 struct got_reference *tmp_branch, struct got_repository *repo)
5001 printf("Switching work tree to %s\n", got_ref_get_name(branch));
5002 return got_worktree_rebase_complete(worktree, fileindex,
5003 new_base_branch, tmp_branch, branch, repo);
5006 static const struct got_error *
5007 rebase_commit(struct got_pathlist_head *merged_paths,
5008 struct got_worktree *worktree, struct got_fileindex *fileindex,
5009 struct got_reference *tmp_branch,
5010 struct got_object_id *commit_id, struct got_repository *repo)
5012 const struct got_error *error;
5013 struct got_commit_object *commit;
5014 struct got_object_id *new_commit_id;
5016 error = got_object_open_as_commit(&commit, repo, commit_id);
5020 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5021 worktree, fileindex, tmp_branch, commit, commit_id, repo);
5023 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5025 error = show_rebase_progress(commit, commit_id, NULL);
5027 error = show_rebase_progress(commit, commit_id, new_commit_id);
5028 free(new_commit_id);
5031 got_object_commit_close(commit);
5035 struct check_path_prefix_arg {
5036 const char *path_prefix;
5041 static const struct got_error *
5042 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5043 struct got_blob_object *blob2, struct got_object_id *id1,
5044 struct got_object_id *id2, const char *path1, const char *path2,
5045 mode_t mode1, mode_t mode2, struct got_repository *repo)
5047 struct check_path_prefix_arg *a = arg;
5049 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5050 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5051 return got_error(a->errcode);
5056 static const struct got_error *
5057 check_path_prefix(struct got_object_id *parent_id,
5058 struct got_object_id *commit_id, const char *path_prefix,
5059 int errcode, struct got_repository *repo)
5061 const struct got_error *err;
5062 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
5063 struct got_commit_object *commit = NULL, *parent_commit = NULL;
5064 struct check_path_prefix_arg cpp_arg;
5066 if (got_path_is_root_dir(path_prefix))
5069 err = got_object_open_as_commit(&commit, repo, commit_id);
5073 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
5077 err = got_object_open_as_tree(&tree1, repo,
5078 got_object_commit_get_tree_id(parent_commit));
5082 err = got_object_open_as_tree(&tree2, repo,
5083 got_object_commit_get_tree_id(commit));
5087 cpp_arg.path_prefix = path_prefix;
5088 while (cpp_arg.path_prefix[0] == '/')
5089 cpp_arg.path_prefix++;
5090 cpp_arg.len = strlen(cpp_arg.path_prefix);
5091 cpp_arg.errcode = errcode;
5092 err = got_diff_tree(tree1, tree2, "", "", repo,
5093 check_path_prefix_in_diff, &cpp_arg, 0);
5096 got_object_tree_close(tree1);
5098 got_object_tree_close(tree2);
5100 got_object_commit_close(commit);
5102 got_object_commit_close(parent_commit);
5106 static const struct got_error *
5107 collect_commits(struct got_object_id_queue *commits,
5108 struct got_object_id *initial_commit_id,
5109 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
5110 const char *path_prefix, int path_prefix_errcode,
5111 struct got_repository *repo)
5113 const struct got_error *err = NULL;
5114 struct got_commit_graph *graph = NULL;
5115 struct got_object_id *parent_id = NULL;
5116 struct got_object_qid *qid;
5117 struct got_object_id *commit_id = initial_commit_id;
5119 err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
5123 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
5124 check_cancelled, NULL);
5127 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
5128 err = got_commit_graph_iter_next(&parent_id, graph);
5130 if (err->code == GOT_ERR_ITER_COMPLETED) {
5131 err = got_error_msg(GOT_ERR_ANCESTRY,
5132 "ran out of commits to rebase before "
5133 "youngest common ancestor commit has "
5136 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
5138 err = got_commit_graph_fetch_commits(graph, 1, repo,
5139 check_cancelled, NULL);
5143 err = check_path_prefix(parent_id, commit_id,
5144 path_prefix, path_prefix_errcode, repo);
5148 err = got_object_qid_alloc(&qid, commit_id);
5151 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
5152 commit_id = parent_id;
5156 got_commit_graph_close(graph);
5160 static const struct got_error *
5161 cmd_rebase(int argc, char *argv[])
5163 const struct got_error *error = NULL;
5164 struct got_worktree *worktree = NULL;
5165 struct got_repository *repo = NULL;
5166 struct got_fileindex *fileindex = NULL;
5168 struct got_reference *branch = NULL;
5169 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
5170 struct got_object_id *commit_id = NULL, *parent_id = NULL;
5171 struct got_object_id *resume_commit_id = NULL;
5172 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
5173 struct got_commit_object *commit = NULL;
5174 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
5175 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5176 struct got_object_id_queue commits;
5177 struct got_pathlist_head merged_paths;
5178 const struct got_object_id_queue *parent_ids;
5179 struct got_object_qid *qid, *pid;
5181 SIMPLEQ_INIT(&commits);
5182 TAILQ_INIT(&merged_paths);
5184 while ((ch = getopt(argc, argv, "ac")) != -1) {
5190 continue_rebase = 1;
5202 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5203 "unveil", NULL) == -1)
5206 if (abort_rebase && continue_rebase)
5208 else if (abort_rebase || continue_rebase) {
5211 } else if (argc != 1)
5214 cwd = getcwd(NULL, 0);
5216 error = got_error_from_errno("getcwd");
5219 error = got_worktree_open(&worktree, cwd);
5223 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5228 error = apply_unveil(got_repo_get_path(repo), 0,
5229 got_worktree_get_root_path(worktree));
5233 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5239 if (!rebase_in_progress) {
5240 error = got_error(GOT_ERR_NOT_REBASING);
5243 error = got_worktree_rebase_continue(&resume_commit_id,
5244 &new_base_branch, &tmp_branch, &branch, &fileindex,
5248 printf("Switching work tree to %s\n",
5249 got_ref_get_symref_target(new_base_branch));
5250 error = got_worktree_rebase_abort(worktree, fileindex, repo,
5251 new_base_branch, update_progress, &did_something);
5254 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5255 goto done; /* nothing else to do */
5258 if (continue_rebase) {
5259 if (!rebase_in_progress) {
5260 error = got_error(GOT_ERR_NOT_REBASING);
5263 error = got_worktree_rebase_continue(&resume_commit_id,
5264 &new_base_branch, &tmp_branch, &branch, &fileindex,
5269 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5270 resume_commit_id, repo);
5274 yca_id = got_object_id_dup(resume_commit_id);
5275 if (yca_id == NULL) {
5276 error = got_error_from_errno("got_object_id_dup");
5280 error = got_ref_open(&branch, repo, argv[0], 0);
5285 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5289 if (!continue_rebase) {
5290 struct got_object_id *base_commit_id;
5292 base_commit_id = got_worktree_get_base_commit_id(worktree);
5293 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5294 base_commit_id, branch_head_commit_id, repo,
5295 check_cancelled, NULL);
5298 if (yca_id == NULL) {
5299 error = got_error_msg(GOT_ERR_ANCESTRY,
5300 "specified branch shares no common ancestry "
5301 "with work tree's branch");
5305 error = check_same_branch(base_commit_id, branch, yca_id, repo);
5307 if (error->code != GOT_ERR_ANCESTRY)
5311 error = got_error_msg(GOT_ERR_SAME_BRANCH,
5312 "specified branch resolves to a commit which "
5313 "is already contained in work tree's branch");
5316 error = got_worktree_rebase_prepare(&new_base_branch,
5317 &tmp_branch, &fileindex, worktree, branch, repo);
5322 commit_id = branch_head_commit_id;
5323 error = got_object_open_as_commit(&commit, repo, commit_id);
5327 parent_ids = got_object_commit_get_parent_ids(commit);
5328 pid = SIMPLEQ_FIRST(parent_ids);
5330 if (!continue_rebase) {
5332 error = got_worktree_rebase_abort(worktree, fileindex,
5333 repo, new_base_branch, update_progress,
5337 printf("Rebase of %s aborted\n",
5338 got_ref_get_name(branch));
5340 error = got_error(GOT_ERR_EMPTY_REBASE);
5343 error = collect_commits(&commits, commit_id, pid->id,
5344 yca_id, got_worktree_get_path_prefix(worktree),
5345 GOT_ERR_REBASE_PATH, repo);
5346 got_object_commit_close(commit);
5351 if (SIMPLEQ_EMPTY(&commits)) {
5352 if (continue_rebase)
5353 error = rebase_complete(worktree, fileindex,
5354 branch, new_base_branch, tmp_branch, repo);
5356 error = got_error(GOT_ERR_EMPTY_REBASE);
5361 SIMPLEQ_FOREACH(qid, &commits, entry) {
5362 commit_id = qid->id;
5363 parent_id = pid ? pid->id : yca_id;
5366 error = got_worktree_rebase_merge_files(&merged_paths,
5367 worktree, fileindex, parent_id, commit_id, repo,
5368 rebase_progress, &rebase_status, check_cancelled, NULL);
5372 if (rebase_status == GOT_STATUS_CONFLICT) {
5373 got_worktree_rebase_pathlist_free(&merged_paths);
5377 error = rebase_commit(&merged_paths, worktree, fileindex,
5378 tmp_branch, commit_id, repo);
5379 got_worktree_rebase_pathlist_free(&merged_paths);
5384 if (rebase_status == GOT_STATUS_CONFLICT) {
5385 error = got_worktree_rebase_postpone(worktree, fileindex);
5388 error = got_error_msg(GOT_ERR_CONFLICTS,
5389 "conflicts must be resolved before rebasing can continue");
5391 error = rebase_complete(worktree, fileindex, branch,
5392 new_base_branch, tmp_branch, repo);
5394 got_object_id_queue_free(&commits);
5395 free(branch_head_commit_id);
5396 free(resume_commit_id);
5399 got_object_commit_close(commit);
5401 got_ref_close(branch);
5402 if (new_base_branch)
5403 got_ref_close(new_base_branch);
5405 got_ref_close(tmp_branch);
5407 got_worktree_close(worktree);
5409 got_repo_close(repo);
5414 usage_histedit(void)
5416 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script]\n",
5421 #define GOT_HISTEDIT_PICK 'p'
5422 #define GOT_HISTEDIT_EDIT 'e'
5423 #define GOT_HISTEDIT_FOLD 'f'
5424 #define GOT_HISTEDIT_DROP 'd'
5425 #define GOT_HISTEDIT_MESG 'm'
5427 static struct got_histedit_cmd {
5431 } got_histedit_cmds[] = {
5432 { GOT_HISTEDIT_PICK, "pick", "use commit" },
5433 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5434 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
5435 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5436 { GOT_HISTEDIT_MESG, "mesg",
5437 "single-line log message for commit above (open editor if empty)" },
5440 struct got_histedit_list_entry {
5441 TAILQ_ENTRY(got_histedit_list_entry) entry;
5442 struct got_object_id *commit_id;
5443 const struct got_histedit_cmd *cmd;
5446 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5448 static const struct got_error *
5449 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5450 FILE *f, struct got_repository *repo)
5452 const struct got_error *err = NULL;
5453 char *logmsg = NULL, *id_str = NULL;
5454 struct got_commit_object *commit = NULL;
5457 err = got_object_open_as_commit(&commit, repo, commit_id);
5461 err = get_short_logmsg(&logmsg, 34, commit);
5465 err = got_object_id_str(&id_str, commit_id);
5469 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
5471 err = got_ferror(f, GOT_ERR_IO);
5474 got_object_commit_close(commit);
5480 static const struct got_error *
5481 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
5482 struct got_repository *repo)
5484 const struct got_error *err = NULL;
5485 struct got_object_qid *qid;
5487 if (SIMPLEQ_EMPTY(commits))
5488 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5490 SIMPLEQ_FOREACH(qid, commits, entry) {
5491 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
5500 static const struct got_error *
5501 write_cmd_list(FILE *f)
5503 const struct got_error *err = NULL;
5506 n = fprintf(f, "# Available histedit commands:\n");
5508 return got_ferror(f, GOT_ERR_IO);
5510 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5511 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
5512 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
5515 err = got_ferror(f, GOT_ERR_IO);
5519 n = fprintf(f, "# Commits will be processed in order from top to "
5520 "bottom of this file.\n");
5522 return got_ferror(f, GOT_ERR_IO);
5526 static const struct got_error *
5527 histedit_syntax_error(int lineno)
5529 static char msg[42];
5532 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
5534 if (ret == -1 || ret >= sizeof(msg))
5535 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
5537 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
5540 static const struct got_error *
5541 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
5542 char *logmsg, struct got_repository *repo)
5544 const struct got_error *err;
5545 struct got_commit_object *folded_commit = NULL;
5546 char *id_str, *folded_logmsg = NULL;
5548 err = got_object_id_str(&id_str, hle->commit_id);
5552 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
5556 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
5559 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
5560 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
5561 folded_logmsg) == -1) {
5562 err = got_error_from_errno("asprintf");
5567 got_object_commit_close(folded_commit);
5569 free(folded_logmsg);
5573 static struct got_histedit_list_entry *
5574 get_folded_commits(struct got_histedit_list_entry *hle)
5576 struct got_histedit_list_entry *prev, *folded = NULL;
5578 prev = TAILQ_PREV(hle, got_histedit_list, entry);
5579 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
5580 prev->cmd->code == GOT_HISTEDIT_DROP)) {
5581 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
5583 prev = TAILQ_PREV(prev, got_histedit_list, entry);
5589 static const struct got_error *
5590 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
5591 struct got_repository *repo)
5593 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
5594 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
5595 const struct got_error *err = NULL;
5596 struct got_commit_object *commit = NULL;
5598 struct got_histedit_list_entry *folded = NULL;
5600 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5604 folded = get_folded_commits(hle);
5606 while (folded != hle) {
5607 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
5608 folded = TAILQ_NEXT(folded, entry);
5611 err = append_folded_commit_msg(&new_msg, folded,
5617 folded = TAILQ_NEXT(folded, entry);
5621 err = got_object_id_str(&id_str, hle->commit_id);
5624 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
5627 if (asprintf(&new_msg,
5628 "%s\n# original log message of commit %s: %s",
5629 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
5630 err = got_error_from_errno("asprintf");
5636 err = got_object_id_str(&id_str, hle->commit_id);
5640 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
5644 dprintf(fd, logmsg);
5647 err = get_editor(&editor);
5651 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
5653 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
5655 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
5658 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
5659 err = got_error_from_errno2("unlink", logmsg_path);
5665 got_object_commit_close(commit);
5669 static const struct got_error *
5670 histedit_parse_list(struct got_histedit_list *histedit_cmds,
5671 FILE *f, struct got_repository *repo)
5673 const struct got_error *err = NULL;
5674 char *line = NULL, *p, *end;
5678 const struct got_histedit_cmd *cmd;
5679 struct got_object_id *commit_id = NULL;
5680 struct got_histedit_list_entry *hle = NULL;
5683 len = getline(&line, &size, f);
5685 const struct got_error *getline_err;
5688 getline_err = got_error_from_errno("getline");
5689 err = got_ferror(f, getline_err->code);
5694 while (isspace((unsigned char)p[0]))
5696 if (p[0] == '#' || p[0] == '\0') {
5702 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5703 cmd = &got_histedit_cmds[i];
5704 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
5705 isspace((unsigned char)p[strlen(cmd->name)])) {
5706 p += strlen(cmd->name);
5709 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
5714 if (i == nitems(got_histedit_cmds)) {
5715 err = histedit_syntax_error(lineno);
5718 while (isspace((unsigned char)p[0]))
5720 if (cmd->code == GOT_HISTEDIT_MESG) {
5721 if (hle == NULL || hle->logmsg != NULL) {
5722 err = got_error(GOT_ERR_HISTEDIT_CMD);
5726 err = histedit_edit_logmsg(hle, repo);
5730 hle->logmsg = strdup(p);
5731 if (hle->logmsg == NULL) {
5732 err = got_error_from_errno("strdup");
5741 while (end[0] && !isspace((unsigned char)end[0]))
5745 err = got_object_resolve_id_str(&commit_id, repo, p);
5747 /* override error code */
5748 err = histedit_syntax_error(lineno);
5752 hle = malloc(sizeof(*hle));
5754 err = got_error_from_errno("malloc");
5758 hle->commit_id = commit_id;
5763 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
5771 static const struct got_error *
5772 histedit_check_script(struct got_histedit_list *histedit_cmds,
5773 struct got_object_id_queue *commits, struct got_repository *repo)
5775 const struct got_error *err = NULL;
5776 struct got_object_qid *qid;
5777 struct got_histedit_list_entry *hle;
5778 static char msg[80];
5781 if (TAILQ_EMPTY(histedit_cmds))
5782 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
5783 "histedit script contains no commands");
5784 if (SIMPLEQ_EMPTY(commits))
5785 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5787 SIMPLEQ_FOREACH(qid, commits, entry) {
5788 TAILQ_FOREACH(hle, histedit_cmds, entry) {
5789 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
5793 err = got_object_id_str(&id_str, qid->id);
5796 snprintf(msg, sizeof(msg),
5797 "commit %s missing from histedit script", id_str);
5799 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
5803 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
5804 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
5805 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
5806 "last commit in histedit script cannot be folded");
5811 static const struct got_error *
5812 histedit_run_editor(struct got_histedit_list *histedit_cmds,
5813 const char *path, struct got_object_id_queue *commits,
5814 struct got_repository *repo)
5816 const struct got_error *err = NULL;
5820 err = get_editor(&editor);
5824 if (spawn_editor(editor, path) == -1) {
5825 err = got_error_from_errno("failed spawning editor");
5829 f = fopen(path, "r");
5831 err = got_error_from_errno("fopen");
5834 err = histedit_parse_list(histedit_cmds, f, repo);
5838 err = histedit_check_script(histedit_cmds, commits, repo);
5840 if (f && fclose(f) != 0 && err == NULL)
5841 err = got_error_from_errno("fclose");
5846 static const struct got_error *
5847 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
5848 struct got_object_id_queue *, const char *, struct got_repository *);
5850 static const struct got_error *
5851 histedit_edit_script(struct got_histedit_list *histedit_cmds,
5852 struct got_object_id_queue *commits, struct got_repository *repo)
5854 const struct got_error *err;
5858 err = got_opentemp_named(&path, &f, "got-histedit");
5862 err = write_cmd_list(f);
5866 err = histedit_write_commit_list(commits, f, repo);
5870 if (fclose(f) != 0) {
5871 err = got_error_from_errno("fclose");
5876 err = histedit_run_editor(histedit_cmds, path, commits, repo);
5878 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5879 err->code != GOT_ERR_HISTEDIT_CMD)
5881 err = histedit_edit_list_retry(histedit_cmds, err,
5882 commits, path, repo);
5885 if (f && fclose(f) != 0 && err == NULL)
5886 err = got_error_from_errno("fclose");
5887 if (path && unlink(path) != 0 && err == NULL)
5888 err = got_error_from_errno2("unlink", path);
5893 static const struct got_error *
5894 histedit_save_list(struct got_histedit_list *histedit_cmds,
5895 struct got_worktree *worktree, struct got_repository *repo)
5897 const struct got_error *err = NULL;
5900 struct got_histedit_list_entry *hle;
5901 struct got_commit_object *commit = NULL;
5903 err = got_worktree_get_histedit_script_path(&path, worktree);
5907 f = fopen(path, "w");
5909 err = got_error_from_errno2("fopen", path);
5912 TAILQ_FOREACH(hle, histedit_cmds, entry) {
5913 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
5919 int n = fprintf(f, "%c %s\n",
5920 GOT_HISTEDIT_MESG, hle->logmsg);
5922 err = got_ferror(f, GOT_ERR_IO);
5928 if (f && fclose(f) != 0 && err == NULL)
5929 err = got_error_from_errno("fclose");
5932 got_object_commit_close(commit);
5937 histedit_free_list(struct got_histedit_list *histedit_cmds)
5939 struct got_histedit_list_entry *hle;
5941 while ((hle = TAILQ_FIRST(histedit_cmds))) {
5942 TAILQ_REMOVE(histedit_cmds, hle, entry);
5947 static const struct got_error *
5948 histedit_load_list(struct got_histedit_list *histedit_cmds,
5949 const char *path, struct got_repository *repo)
5951 const struct got_error *err = NULL;
5954 f = fopen(path, "r");
5956 err = got_error_from_errno2("fopen", path);
5960 err = histedit_parse_list(histedit_cmds, f, repo);
5962 if (f && fclose(f) != 0 && err == NULL)
5963 err = got_error_from_errno("fclose");
5967 static const struct got_error *
5968 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
5969 const struct got_error *edit_err, struct got_object_id_queue *commits,
5970 const char *path, struct got_repository *repo)
5972 const struct got_error *err = NULL, *prev_err = edit_err;
5975 while (resp != 'c' && resp != 'r' && resp != 'a') {
5976 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
5977 "or (a)bort: ", getprogname(), prev_err->msg);
5982 histedit_free_list(histedit_cmds);
5983 err = histedit_run_editor(histedit_cmds, path, commits,
5986 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5987 err->code != GOT_ERR_HISTEDIT_CMD)
5994 } else if (resp == 'r') {
5995 histedit_free_list(histedit_cmds);
5996 err = histedit_edit_script(histedit_cmds,
5999 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6000 err->code != GOT_ERR_HISTEDIT_CMD)
6007 } else if (resp == 'a') {
6008 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
6011 printf("invalid response '%c'\n", resp);
6017 static const struct got_error *
6018 histedit_complete(struct got_worktree *worktree,
6019 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6020 struct got_reference *branch, struct got_repository *repo)
6022 printf("Switching work tree to %s\n",
6023 got_ref_get_symref_target(branch));
6024 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
6028 static const struct got_error *
6029 show_histedit_progress(struct got_commit_object *commit,
6030 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
6032 const struct got_error *err;
6033 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6035 err = got_object_id_str(&old_id_str, hle->commit_id);
6040 err = got_object_id_str(&new_id_str, new_id);
6045 old_id_str[12] = '\0';
6047 new_id_str[12] = '\0';
6050 logmsg = strdup(hle->logmsg);
6051 if (logmsg == NULL) {
6052 err = got_error_from_errno("strdup");
6055 trim_logmsg(logmsg, 42);
6057 err = get_short_logmsg(&logmsg, 42, commit);
6062 switch (hle->cmd->code) {
6063 case GOT_HISTEDIT_PICK:
6064 case GOT_HISTEDIT_EDIT:
6065 printf("%s -> %s: %s\n", old_id_str,
6066 new_id_str ? new_id_str : "no-op change", logmsg);
6068 case GOT_HISTEDIT_DROP:
6069 case GOT_HISTEDIT_FOLD:
6070 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
6083 static const struct got_error *
6084 histedit_commit(struct got_pathlist_head *merged_paths,
6085 struct got_worktree *worktree, struct got_fileindex *fileindex,
6086 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
6087 struct got_repository *repo)
6089 const struct got_error *err;
6090 struct got_commit_object *commit;
6091 struct got_object_id *new_commit_id;
6093 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
6094 && hle->logmsg == NULL) {
6095 err = histedit_edit_logmsg(hle, repo);
6100 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6104 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
6105 worktree, fileindex, tmp_branch, commit, hle->commit_id,
6108 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
6110 err = show_histedit_progress(commit, hle, NULL);
6112 err = show_histedit_progress(commit, hle, new_commit_id);
6113 free(new_commit_id);
6116 got_object_commit_close(commit);
6120 static const struct got_error *
6121 histedit_skip_commit(struct got_histedit_list_entry *hle,
6122 struct got_worktree *worktree, struct got_repository *repo)
6124 const struct got_error *error;
6125 struct got_commit_object *commit;
6127 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
6132 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
6136 error = show_histedit_progress(commit, hle, NULL);
6137 got_object_commit_close(commit);
6141 static const struct got_error *
6142 cmd_histedit(int argc, char *argv[])
6144 const struct got_error *error = NULL;
6145 struct got_worktree *worktree = NULL;
6146 struct got_fileindex *fileindex = NULL;
6147 struct got_repository *repo = NULL;
6149 struct got_reference *branch = NULL;
6150 struct got_reference *tmp_branch = NULL;
6151 struct got_object_id *resume_commit_id = NULL;
6152 struct got_object_id *base_commit_id = NULL;
6153 struct got_object_id *head_commit_id = NULL;
6154 struct got_commit_object *commit = NULL;
6155 int ch, rebase_in_progress = 0, did_something;
6156 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
6157 const char *edit_script_path = NULL;
6158 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6159 struct got_object_id_queue commits;
6160 struct got_pathlist_head merged_paths;
6161 const struct got_object_id_queue *parent_ids;
6162 struct got_object_qid *pid;
6163 struct got_histedit_list histedit_cmds;
6164 struct got_histedit_list_entry *hle;
6166 SIMPLEQ_INIT(&commits);
6167 TAILQ_INIT(&histedit_cmds);
6168 TAILQ_INIT(&merged_paths);
6170 while ((ch = getopt(argc, argv, "acF:")) != -1) {
6179 edit_script_path = optarg;
6191 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6192 "unveil", NULL) == -1)
6195 if (abort_edit && continue_edit)
6201 * This command cannot apply unveil(2) in all cases because the
6202 * user may choose to run an editor to edit the histedit script
6203 * and to edit individual commit log messages.
6204 * unveil(2) traverses exec(2); if an editor is used we have to
6205 * apply unveil after edit script and log messages have been written.
6206 * XXX TODO: Make use of unveil(2) where possible.
6209 cwd = getcwd(NULL, 0);
6211 error = got_error_from_errno("getcwd");
6214 error = got_worktree_open(&worktree, cwd);
6218 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6223 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6226 if (rebase_in_progress) {
6227 error = got_error(GOT_ERR_REBASING);
6231 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6235 if (edit_in_progress && abort_edit) {
6236 error = got_worktree_histedit_continue(&resume_commit_id,
6237 &tmp_branch, &branch, &base_commit_id, &fileindex,
6241 printf("Switching work tree to %s\n",
6242 got_ref_get_symref_target(branch));
6243 error = got_worktree_histedit_abort(worktree, fileindex, repo,
6244 branch, base_commit_id, update_progress, &did_something);
6247 printf("Histedit of %s aborted\n",
6248 got_ref_get_symref_target(branch));
6249 goto done; /* nothing else to do */
6250 } else if (abort_edit) {
6251 error = got_error(GOT_ERR_NOT_HISTEDIT);
6255 if (continue_edit) {
6258 if (!edit_in_progress) {
6259 error = got_error(GOT_ERR_NOT_HISTEDIT);
6263 error = got_worktree_get_histedit_script_path(&path, worktree);
6267 error = histedit_load_list(&histedit_cmds, path, repo);
6272 error = got_worktree_histedit_continue(&resume_commit_id,
6273 &tmp_branch, &branch, &base_commit_id, &fileindex,
6278 error = got_ref_resolve(&head_commit_id, repo, branch);
6282 error = got_object_open_as_commit(&commit, repo,
6286 parent_ids = got_object_commit_get_parent_ids(commit);
6287 pid = SIMPLEQ_FIRST(parent_ids);
6289 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6292 error = collect_commits(&commits, head_commit_id, pid->id,
6293 base_commit_id, got_worktree_get_path_prefix(worktree),
6294 GOT_ERR_HISTEDIT_PATH, repo);
6295 got_object_commit_close(commit);
6300 if (edit_in_progress) {
6301 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6305 error = got_ref_open(&branch, repo,
6306 got_worktree_get_head_ref_name(worktree), 0);
6310 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
6311 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
6312 "will not edit commit history of a branch outside "
6313 "the \"refs/heads/\" reference namespace");
6317 error = got_ref_resolve(&head_commit_id, repo, branch);
6318 got_ref_close(branch);
6323 error = got_object_open_as_commit(&commit, repo,
6327 parent_ids = got_object_commit_get_parent_ids(commit);
6328 pid = SIMPLEQ_FIRST(parent_ids);
6330 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6333 error = collect_commits(&commits, head_commit_id, pid->id,
6334 got_worktree_get_base_commit_id(worktree),
6335 got_worktree_get_path_prefix(worktree),
6336 GOT_ERR_HISTEDIT_PATH, repo);
6337 got_object_commit_close(commit);
6342 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
6343 &base_commit_id, &fileindex, worktree, repo);
6347 if (edit_script_path) {
6348 error = histedit_load_list(&histedit_cmds,
6349 edit_script_path, repo);
6351 got_worktree_histedit_abort(worktree, fileindex,
6352 repo, branch, base_commit_id,
6353 update_progress, &did_something);
6357 error = histedit_edit_script(&histedit_cmds, &commits,
6360 got_worktree_histedit_abort(worktree, fileindex,
6361 repo, branch, base_commit_id,
6362 update_progress, &did_something);
6368 error = histedit_save_list(&histedit_cmds, worktree,
6371 got_worktree_histedit_abort(worktree, fileindex,
6372 repo, branch, base_commit_id,
6373 update_progress, &did_something);
6379 error = histedit_check_script(&histedit_cmds, &commits, repo);
6383 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
6384 if (resume_commit_id) {
6385 if (got_object_id_cmp(hle->commit_id,
6386 resume_commit_id) != 0)
6389 resume_commit_id = NULL;
6390 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
6391 hle->cmd->code == GOT_HISTEDIT_FOLD) {
6392 error = histedit_skip_commit(hle, worktree,
6395 error = histedit_commit(NULL, worktree,
6396 fileindex, tmp_branch, hle, repo);
6403 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
6404 error = histedit_skip_commit(hle, worktree, repo);
6410 error = got_object_open_as_commit(&commit, repo,
6414 parent_ids = got_object_commit_get_parent_ids(commit);
6415 pid = SIMPLEQ_FIRST(parent_ids);
6417 error = got_worktree_histedit_merge_files(&merged_paths,
6418 worktree, fileindex, pid->id, hle->commit_id, repo,
6419 rebase_progress, &rebase_status, check_cancelled, NULL);
6422 got_object_commit_close(commit);
6425 if (rebase_status == GOT_STATUS_CONFLICT) {
6426 got_worktree_rebase_pathlist_free(&merged_paths);
6430 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
6432 error = got_object_id_str(&id_str, hle->commit_id);
6435 printf("Stopping histedit for amending commit %s\n",
6438 got_worktree_rebase_pathlist_free(&merged_paths);
6439 error = got_worktree_histedit_postpone(worktree,
6444 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
6445 error = histedit_skip_commit(hle, worktree, repo);
6451 error = histedit_commit(&merged_paths, worktree, fileindex,
6452 tmp_branch, hle, repo);
6453 got_worktree_rebase_pathlist_free(&merged_paths);
6458 if (rebase_status == GOT_STATUS_CONFLICT) {
6459 error = got_worktree_histedit_postpone(worktree, fileindex);
6462 error = got_error_msg(GOT_ERR_CONFLICTS,
6463 "conflicts must be resolved before rebasing can continue");
6465 error = histedit_complete(worktree, fileindex, tmp_branch,
6468 got_object_id_queue_free(&commits);
6469 histedit_free_list(&histedit_cmds);
6470 free(head_commit_id);
6471 free(base_commit_id);
6472 free(resume_commit_id);
6474 got_object_commit_close(commit);
6476 got_ref_close(branch);
6478 got_ref_close(tmp_branch);
6480 got_worktree_close(worktree);
6482 got_repo_close(repo);
6487 usage_integrate(void)
6489 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
6493 static const struct got_error *
6494 cmd_integrate(int argc, char *argv[])
6496 const struct got_error *error = NULL;
6497 struct got_repository *repo = NULL;
6498 struct got_worktree *worktree = NULL;
6499 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
6500 const char *branch_arg = NULL;
6501 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
6502 struct got_fileindex *fileindex = NULL;
6503 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
6504 int ch, did_something = 0;
6506 while ((ch = getopt(argc, argv, "")) != -1) {
6519 branch_arg = argv[0];
6521 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6522 "unveil", NULL) == -1)
6525 cwd = getcwd(NULL, 0);
6527 error = got_error_from_errno("getcwd");
6531 error = got_worktree_open(&worktree, cwd);
6535 error = check_rebase_or_histedit_in_progress(worktree);
6539 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6544 error = apply_unveil(got_repo_get_path(repo), 0,
6545 got_worktree_get_root_path(worktree));
6549 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
6550 error = got_error_from_errno("asprintf");
6554 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
6555 &base_branch_ref, worktree, refname, repo);
6559 refname = strdup(got_ref_get_name(branch_ref));
6560 if (refname == NULL) {
6561 error = got_error_from_errno("strdup");
6562 got_worktree_integrate_abort(worktree, fileindex, repo,
6563 branch_ref, base_branch_ref);
6566 base_refname = strdup(got_ref_get_name(base_branch_ref));
6567 if (base_refname == NULL) {
6568 error = got_error_from_errno("strdup");
6569 got_worktree_integrate_abort(worktree, fileindex, repo,
6570 branch_ref, base_branch_ref);
6574 error = got_ref_resolve(&commit_id, repo, branch_ref);
6578 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
6582 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
6583 error = got_error_msg(GOT_ERR_SAME_BRANCH,
6584 "specified branch has already been integrated");
6585 got_worktree_integrate_abort(worktree, fileindex, repo,
6586 branch_ref, base_branch_ref);
6590 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
6592 if (error->code == GOT_ERR_ANCESTRY)
6593 error = got_error(GOT_ERR_REBASE_REQUIRED);
6594 got_worktree_integrate_abort(worktree, fileindex, repo,
6595 branch_ref, base_branch_ref);
6599 error = got_worktree_integrate_continue(worktree, fileindex, repo,
6600 branch_ref, base_branch_ref, update_progress, &did_something,
6601 check_cancelled, NULL);
6605 printf("Integrated %s into %s\n", refname, base_refname);
6608 got_repo_close(repo);
6610 got_worktree_close(worktree);
6612 free(base_commit_id);
6622 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
6623 "[file-path ...]\n",
6628 static const struct got_error *
6629 print_stage(void *arg, unsigned char status, unsigned char staged_status,
6630 const char *path, struct got_object_id *blob_id,
6631 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
6633 const struct got_error *err = NULL;
6634 char *id_str = NULL;
6636 if (staged_status != GOT_STATUS_ADD &&
6637 staged_status != GOT_STATUS_MODIFY &&
6638 staged_status != GOT_STATUS_DELETE)
6641 if (staged_status == GOT_STATUS_ADD ||
6642 staged_status == GOT_STATUS_MODIFY)
6643 err = got_object_id_str(&id_str, staged_blob_id);
6645 err = got_object_id_str(&id_str, blob_id);
6649 printf("%s %c %s\n", id_str, staged_status, path);
6654 static const struct got_error *
6655 cmd_stage(int argc, char *argv[])
6657 const struct got_error *error = NULL;
6658 struct got_repository *repo = NULL;
6659 struct got_worktree *worktree = NULL;
6661 struct got_pathlist_head paths;
6662 struct got_pathlist_entry *pe;
6663 int ch, list_stage = 0, pflag = 0;
6664 FILE *patch_script_file = NULL;
6665 const char *patch_script_path = NULL;
6666 struct choose_patch_arg cpa;
6670 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
6679 patch_script_path = optarg;
6691 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6692 "unveil", NULL) == -1)
6695 if (list_stage && (pflag || patch_script_path))
6696 errx(1, "-l option cannot be used with other options");
6697 if (patch_script_path && !pflag)
6698 errx(1, "-F option can only be used together with -p option");
6700 cwd = getcwd(NULL, 0);
6702 error = got_error_from_errno("getcwd");
6706 error = got_worktree_open(&worktree, cwd);
6710 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6715 if (patch_script_path) {
6716 patch_script_file = fopen(patch_script_path, "r");
6717 if (patch_script_file == NULL) {
6718 error = got_error_from_errno2("fopen",
6723 error = apply_unveil(got_repo_get_path(repo), 0,
6724 got_worktree_get_root_path(worktree));
6728 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6733 error = got_worktree_status(worktree, &paths, repo,
6734 print_stage, NULL, check_cancelled, NULL);
6736 cpa.patch_script_file = patch_script_file;
6737 cpa.action = "stage";
6738 error = got_worktree_stage(worktree, &paths,
6739 pflag ? NULL : print_status, NULL,
6740 pflag ? choose_patch : NULL, &cpa, repo);
6743 if (patch_script_file && fclose(patch_script_file) == EOF &&
6745 error = got_error_from_errno2("fclose", patch_script_path);
6747 got_repo_close(repo);
6749 got_worktree_close(worktree);
6750 TAILQ_FOREACH(pe, &paths, entry)
6751 free((char *)pe->path);
6752 got_pathlist_free(&paths);
6760 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
6761 "[file-path ...]\n",
6767 static const struct got_error *
6768 cmd_unstage(int argc, char *argv[])
6770 const struct got_error *error = NULL;
6771 struct got_repository *repo = NULL;
6772 struct got_worktree *worktree = NULL;
6774 struct got_pathlist_head paths;
6775 struct got_pathlist_entry *pe;
6776 int ch, did_something = 0, pflag = 0;
6777 FILE *patch_script_file = NULL;
6778 const char *patch_script_path = NULL;
6779 struct choose_patch_arg cpa;
6783 while ((ch = getopt(argc, argv, "pF:")) != -1) {
6789 patch_script_path = optarg;
6801 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6802 "unveil", NULL) == -1)
6805 if (patch_script_path && !pflag)
6806 errx(1, "-F option can only be used together with -p option");
6808 cwd = getcwd(NULL, 0);
6810 error = got_error_from_errno("getcwd");
6814 error = got_worktree_open(&worktree, cwd);
6818 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6823 if (patch_script_path) {
6824 patch_script_file = fopen(patch_script_path, "r");
6825 if (patch_script_file == NULL) {
6826 error = got_error_from_errno2("fopen",
6832 error = apply_unveil(got_repo_get_path(repo), 0,
6833 got_worktree_get_root_path(worktree));
6837 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6841 cpa.patch_script_file = patch_script_file;
6842 cpa.action = "unstage";
6843 error = got_worktree_unstage(worktree, &paths, update_progress,
6844 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
6846 if (patch_script_file && fclose(patch_script_file) == EOF &&
6848 error = got_error_from_errno2("fclose", patch_script_path);
6850 got_repo_close(repo);
6852 got_worktree_close(worktree);
6853 TAILQ_FOREACH(pe, &paths, entry)
6854 free((char *)pe->path);
6855 got_pathlist_free(&paths);
6863 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
6864 "arg1 [arg2 ...]\n", getprogname());
6868 static const struct got_error *
6869 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6871 const struct got_error *err;
6872 struct got_blob_object *blob;
6874 err = got_object_open_as_blob(&blob, repo, id, 8192);
6878 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
6879 got_object_blob_close(blob);
6883 static const struct got_error *
6884 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6886 const struct got_error *err;
6887 struct got_tree_object *tree;
6888 const struct got_tree_entries *entries;
6889 struct got_tree_entry *te;
6891 err = got_object_open_as_tree(&tree, repo, id);
6895 entries = got_object_tree_get_entries(tree);
6896 te = SIMPLEQ_FIRST(&entries->head);
6899 if (sigint_received || sigpipe_received)
6901 err = got_object_id_str(&id_str, te->id);
6904 fprintf(outfile, "%s %.7o %s\n", id_str, te->mode, te->name);
6906 te = SIMPLEQ_NEXT(te, entry);
6909 got_object_tree_close(tree);
6913 static const struct got_error *
6914 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6916 const struct got_error *err;
6917 struct got_commit_object *commit;
6918 const struct got_object_id_queue *parent_ids;
6919 struct got_object_qid *pid;
6920 char *id_str = NULL;
6921 const char *logmsg = NULL;
6923 err = got_object_open_as_commit(&commit, repo, id);
6927 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
6931 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
6932 parent_ids = got_object_commit_get_parent_ids(commit);
6933 fprintf(outfile, "numparents %d\n",
6934 got_object_commit_get_nparents(commit));
6935 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
6937 err = got_object_id_str(&pid_str, pid->id);
6940 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
6943 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
6944 got_object_commit_get_author(commit),
6945 got_object_commit_get_author_time(commit));
6947 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
6948 got_object_commit_get_author(commit),
6949 got_object_commit_get_committer_time(commit));
6951 logmsg = got_object_commit_get_logmsg_raw(commit);
6952 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
6953 fprintf(outfile, "%s", logmsg);
6956 got_object_commit_close(commit);
6960 static const struct got_error *
6961 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6963 const struct got_error *err;
6964 struct got_tag_object *tag;
6965 char *id_str = NULL;
6966 const char *tagmsg = NULL;
6968 err = got_object_open_as_tag(&tag, repo, id);
6972 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
6976 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
6978 switch (got_object_tag_get_object_type(tag)) {
6979 case GOT_OBJ_TYPE_BLOB:
6980 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
6981 GOT_OBJ_LABEL_BLOB);
6983 case GOT_OBJ_TYPE_TREE:
6984 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
6985 GOT_OBJ_LABEL_TREE);
6987 case GOT_OBJ_TYPE_COMMIT:
6988 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
6989 GOT_OBJ_LABEL_COMMIT);
6991 case GOT_OBJ_TYPE_TAG:
6992 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
6999 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
7000 got_object_tag_get_name(tag));
7002 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
7003 got_object_tag_get_tagger(tag),
7004 got_object_tag_get_tagger_time(tag));
7006 tagmsg = got_object_tag_get_message(tag);
7007 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
7008 fprintf(outfile, "%s", tagmsg);
7011 got_object_tag_close(tag);
7015 static const struct got_error *
7016 cmd_cat(int argc, char *argv[])
7018 const struct got_error *error;
7019 struct got_repository *repo = NULL;
7020 struct got_worktree *worktree = NULL;
7021 char *cwd = NULL, *repo_path = NULL, *label = NULL;
7022 const char *commit_id_str = NULL;
7023 struct got_object_id *id = NULL, *commit_id = NULL;
7024 int ch, obj_type, i, force_path = 0;
7027 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7032 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
7035 commit_id_str = optarg;
7038 repo_path = realpath(optarg, NULL);
7039 if (repo_path == NULL)
7040 err(1, "-r option");
7041 got_path_strip_trailing_slashes(repo_path);
7055 cwd = getcwd(NULL, 0);
7057 error = got_error_from_errno("getcwd");
7060 error = got_worktree_open(&worktree, cwd);
7061 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7064 if (repo_path == NULL) {
7066 got_worktree_get_repo_path(worktree));
7067 if (repo_path == NULL) {
7068 error = got_error_from_errno("strdup");
7074 if (repo_path == NULL) {
7075 repo_path = getcwd(NULL, 0);
7076 if (repo_path == NULL)
7077 return got_error_from_errno("getcwd");
7080 error = got_repo_open(&repo, repo_path, NULL);
7085 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7089 if (commit_id_str == NULL)
7090 commit_id_str = GOT_REF_HEAD;
7091 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
7095 for (i = 0; i < argc; i++) {
7097 error = got_object_id_by_path(&id, repo, commit_id,
7102 error = match_object_id(&id, &label, argv[i],
7103 GOT_OBJ_TYPE_ANY, 0, repo);
7105 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
7106 error->code != GOT_ERR_NOT_REF)
7108 error = got_object_id_by_path(&id, repo,
7109 commit_id, argv[i]);
7115 error = got_object_get_type(&obj_type, repo, id);
7120 case GOT_OBJ_TYPE_BLOB:
7121 error = cat_blob(id, repo, stdout);
7123 case GOT_OBJ_TYPE_TREE:
7124 error = cat_tree(id, repo, stdout);
7126 case GOT_OBJ_TYPE_COMMIT:
7127 error = cat_commit(id, repo, stdout);
7129 case GOT_OBJ_TYPE_TAG:
7130 error = cat_tag(id, repo, stdout);
7133 error = got_error(GOT_ERR_OBJ_TYPE);
7149 got_worktree_close(worktree);
7151 const struct got_error *repo_error;
7152 repo_error = got_repo_close(repo);