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, 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 *tagmsg_path = NULL, *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 if (err == NULL || err->code == GOT_ERR_COMMIT_MSG_EMPTY) {
3768 unlink(tagmsg_path);
3772 free(initial_content);
3776 /* Editor is done; we can now apply unveil(2) */
3778 err = apply_unveil(repo_path, 0, NULL);
3787 static const struct got_error *
3788 add_tag(struct got_repository *repo, const char *tag_name,
3789 const char *commit_arg, const char *tagmsg_arg)
3791 const struct got_error *err = NULL;
3792 struct got_object_id *commit_id = NULL, *tag_id = NULL;
3793 char *label = NULL, *commit_id_str = NULL;
3794 struct got_reference *ref = NULL;
3795 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
3798 * Don't let the user create a tag named '-'.
3799 * While technically a valid reference name, this case is usually
3800 * an unintended typo.
3802 if (tag_name[0] == '-' && tag_name[1] == '\0')
3803 return got_error_path(tag_name, GOT_ERR_BAD_REF_NAME);
3805 err = get_author(&tagger, repo);
3809 err = match_object_id(&commit_id, &label, commit_arg,
3810 GOT_OBJ_TYPE_COMMIT, 1, repo);
3814 err = got_object_id_str(&commit_id_str, commit_id);
3818 if (strncmp("refs/tags/", tag_name, 10) == 0) {
3819 refname = strdup(tag_name);
3820 if (refname == NULL) {
3821 err = got_error_from_errno("strdup");
3825 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
3826 err = got_error_from_errno("asprintf");
3830 err = got_ref_open(&ref, repo, refname, 0);
3832 err = got_error(GOT_ERR_TAG_EXISTS);
3834 } else if (err->code != GOT_ERR_NOT_REF)
3837 if (tagmsg_arg == NULL) {
3838 err = get_tag_message(&tagmsg, commit_id_str,
3839 tag_name, got_repo_get_path(repo));
3844 err = got_object_tag_create(&tag_id, tag_name, commit_id,
3845 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
3849 err = got_ref_alloc(&ref, refname, tag_id);
3853 err = got_ref_write(ref, repo);
3857 err = got_object_id_str(&tag_id_str, tag_id);
3858 printf("Created tag %s\n", tag_id_str);
3865 free(commit_id_str);
3872 static const struct got_error *
3873 cmd_tag(int argc, char *argv[])
3875 const struct got_error *error = NULL;
3876 struct got_repository *repo = NULL;
3877 struct got_worktree *worktree = NULL;
3878 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
3879 char *gitconfig_path = NULL;
3880 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
3881 int ch, do_list = 0;
3883 while ((ch = getopt(argc, argv, "m:r:l")) != -1) {
3889 repo_path = realpath(optarg, NULL);
3890 if (repo_path == NULL)
3891 err(1, "-r option");
3892 got_path_strip_trailing_slashes(repo_path);
3908 errx(1, "-l and -m options are mutually exclusive\n");
3911 } else if (argc < 1 || argc > 2)
3914 commit_id_arg = argv[1];
3919 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3923 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3924 "sendfd unveil", NULL) == -1)
3928 cwd = getcwd(NULL, 0);
3930 error = got_error_from_errno("getcwd");
3934 if (repo_path == NULL) {
3935 error = got_worktree_open(&worktree, cwd);
3936 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3942 strdup(got_worktree_get_repo_path(worktree));
3943 if (repo_path == NULL)
3944 error = got_error_from_errno("strdup");
3948 repo_path = strdup(cwd);
3949 if (repo_path == NULL) {
3950 error = got_error_from_errno("strdup");
3957 error = got_repo_open(&repo, repo_path, NULL);
3960 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3963 error = list_tags(repo, worktree);
3965 error = get_gitconfig_path(&gitconfig_path);
3968 error = got_repo_open(&repo, repo_path, gitconfig_path);
3973 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3978 if (commit_id_arg == NULL) {
3979 struct got_reference *head_ref;
3980 struct got_object_id *commit_id;
3981 error = got_ref_open(&head_ref, repo,
3982 worktree ? got_worktree_get_head_ref_name(worktree)
3986 error = got_ref_resolve(&commit_id, repo, head_ref);
3987 got_ref_close(head_ref);
3990 error = got_object_id_str(&commit_id_str, commit_id);
3996 error = add_tag(repo, tag_name,
3997 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4001 got_repo_close(repo);
4003 got_worktree_close(worktree);
4006 free(gitconfig_path);
4007 free(commit_id_str);
4014 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
4018 static const struct got_error *
4019 cmd_add(int argc, char *argv[])
4021 const struct got_error *error = NULL;
4022 struct got_repository *repo = NULL;
4023 struct got_worktree *worktree = NULL;
4025 struct got_pathlist_head paths;
4026 struct got_pathlist_entry *pe;
4031 while ((ch = getopt(argc, argv, "")) != -1) {
4043 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4050 cwd = getcwd(NULL, 0);
4052 error = got_error_from_errno("getcwd");
4056 error = got_worktree_open(&worktree, cwd);
4060 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4065 error = apply_unveil(got_repo_get_path(repo), 1,
4066 got_worktree_get_root_path(worktree));
4070 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4074 error = got_worktree_schedule_add(worktree, &paths, print_status,
4078 got_repo_close(repo);
4080 got_worktree_close(worktree);
4081 TAILQ_FOREACH(pe, &paths, entry)
4082 free((char *)pe->path);
4083 got_pathlist_free(&paths);
4091 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
4095 static const struct got_error *
4096 print_remove_status(void *arg, unsigned char status,
4097 unsigned char staged_status, const char *path,
4098 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4099 struct got_object_id *commit_id)
4101 if (status == GOT_STATUS_NONEXISTENT)
4103 if (status == staged_status && (status == GOT_STATUS_DELETE))
4104 status = GOT_STATUS_NO_CHANGE;
4105 printf("%c%c %s\n", status, staged_status, path);
4109 static const struct got_error *
4110 cmd_remove(int argc, char *argv[])
4112 const struct got_error *error = NULL;
4113 struct got_worktree *worktree = NULL;
4114 struct got_repository *repo = NULL;
4116 struct got_pathlist_head paths;
4117 struct got_pathlist_entry *pe;
4118 int ch, delete_local_mods = 0;
4122 while ((ch = getopt(argc, argv, "f")) != -1) {
4125 delete_local_mods = 1;
4137 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4144 cwd = getcwd(NULL, 0);
4146 error = got_error_from_errno("getcwd");
4149 error = got_worktree_open(&worktree, cwd);
4153 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4158 error = apply_unveil(got_repo_get_path(repo), 1,
4159 got_worktree_get_root_path(worktree));
4163 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4167 error = got_worktree_schedule_delete(worktree, &paths,
4168 delete_local_mods, print_remove_status, NULL, repo);
4173 got_repo_close(repo);
4175 got_worktree_close(worktree);
4176 TAILQ_FOREACH(pe, &paths, entry)
4177 free((char *)pe->path);
4178 got_pathlist_free(&paths);
4186 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4187 "path ...\n", getprogname());
4191 static const struct got_error *
4192 revert_progress(void *arg, unsigned char status, const char *path)
4194 while (path[0] == '/')
4196 printf("%c %s\n", status, path);
4200 struct choose_patch_arg {
4201 FILE *patch_script_file;
4205 static const struct got_error *
4206 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4207 int nchanges, const char *action)
4210 size_t linesize = 0;
4214 case GOT_STATUS_ADD:
4215 printf("A %s\n%s this addition? [y/n] ", path, action);
4217 case GOT_STATUS_DELETE:
4218 printf("D %s\n%s this deletion? [y/n] ", path, action);
4220 case GOT_STATUS_MODIFY:
4221 if (fseek(patch_file, 0L, SEEK_SET) == -1)
4222 return got_error_from_errno("fseek");
4223 printf(GOT_COMMIT_SEP_STR);
4224 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4226 if (ferror(patch_file))
4227 return got_error_from_errno("getline");
4228 printf(GOT_COMMIT_SEP_STR);
4229 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4230 path, n, nchanges, action);
4233 return got_error_path(path, GOT_ERR_FILE_STATUS);
4239 static const struct got_error *
4240 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4241 FILE *patch_file, int n, int nchanges)
4243 const struct got_error *err = NULL;
4245 size_t linesize = 0;
4248 struct choose_patch_arg *a = arg;
4250 *choice = GOT_PATCH_CHOICE_NONE;
4252 if (a->patch_script_file) {
4254 err = show_change(status, path, patch_file, n, nchanges,
4258 linelen = getline(&line, &linesize, a->patch_script_file);
4259 if (linelen == -1) {
4260 if (ferror(a->patch_script_file))
4261 return got_error_from_errno("getline");
4264 nl = strchr(line, '\n');
4267 if (strcmp(line, "y") == 0) {
4268 *choice = GOT_PATCH_CHOICE_YES;
4270 } else if (strcmp(line, "n") == 0) {
4271 *choice = GOT_PATCH_CHOICE_NO;
4273 } else if (strcmp(line, "q") == 0 &&
4274 status == GOT_STATUS_MODIFY) {
4275 *choice = GOT_PATCH_CHOICE_QUIT;
4278 printf("invalid response '%s'\n", line);
4283 while (resp != 'y' && resp != 'n' && resp != 'q') {
4284 err = show_change(status, path, patch_file, n, nchanges,
4291 if (status == GOT_STATUS_MODIFY) {
4292 if (resp != 'y' && resp != 'n' && resp != 'q') {
4293 printf("invalid response '%c'\n", resp);
4296 } else if (resp != 'y' && resp != 'n') {
4297 printf("invalid response '%c'\n", resp);
4303 *choice = GOT_PATCH_CHOICE_YES;
4304 else if (resp == 'n')
4305 *choice = GOT_PATCH_CHOICE_NO;
4306 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4307 *choice = GOT_PATCH_CHOICE_QUIT;
4313 static const struct got_error *
4314 cmd_revert(int argc, char *argv[])
4316 const struct got_error *error = NULL;
4317 struct got_worktree *worktree = NULL;
4318 struct got_repository *repo = NULL;
4319 char *cwd = NULL, *path = NULL;
4320 struct got_pathlist_head paths;
4321 struct got_pathlist_entry *pe;
4322 int ch, can_recurse = 0, pflag = 0;
4323 FILE *patch_script_file = NULL;
4324 const char *patch_script_path = NULL;
4325 struct choose_patch_arg cpa;
4329 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4335 patch_script_path = optarg;
4350 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4351 "unveil", NULL) == -1)
4356 if (patch_script_path && !pflag)
4357 errx(1, "-F option can only be used together with -p option");
4359 cwd = getcwd(NULL, 0);
4361 error = got_error_from_errno("getcwd");
4364 error = got_worktree_open(&worktree, cwd);
4368 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4373 if (patch_script_path) {
4374 patch_script_file = fopen(patch_script_path, "r");
4375 if (patch_script_file == NULL) {
4376 error = got_error_from_errno2("fopen",
4381 error = apply_unveil(got_repo_get_path(repo), 1,
4382 got_worktree_get_root_path(worktree));
4386 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4393 TAILQ_FOREACH(pe, &paths, entry) {
4394 if (asprintf(&ondisk_path, "%s/%s",
4395 got_worktree_get_root_path(worktree),
4397 error = got_error_from_errno("asprintf");
4400 if (lstat(ondisk_path, &sb) == -1) {
4401 if (errno == ENOENT) {
4405 error = got_error_from_errno2("lstat",
4411 if (S_ISDIR(sb.st_mode)) {
4412 error = got_error_msg(GOT_ERR_BAD_PATH,
4413 "reverting directories requires -R option");
4419 cpa.patch_script_file = patch_script_file;
4420 cpa.action = "revert";
4421 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4422 pflag ? choose_patch : NULL, &cpa, repo);
4426 if (patch_script_file && fclose(patch_script_file) == EOF &&
4428 error = got_error_from_errno2("fclose", patch_script_path);
4430 got_repo_close(repo);
4432 got_worktree_close(worktree);
4441 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4446 struct collect_commit_logmsg_arg {
4447 const char *cmdline_log;
4449 const char *worktree_path;
4450 const char *branch_name;
4451 const char *repo_path;
4456 static const struct got_error *
4457 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4460 char *initial_content = NULL;
4461 struct got_pathlist_entry *pe;
4462 const struct got_error *err = NULL;
4463 char *template = NULL;
4464 struct collect_commit_logmsg_arg *a = arg;
4468 /* if a message was specified on the command line, just use it */
4469 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4470 len = strlen(a->cmdline_log) + 1;
4471 *logmsg = malloc(len + 1);
4472 if (*logmsg == NULL)
4473 return got_error_from_errno("malloc");
4474 strlcpy(*logmsg, a->cmdline_log, len);
4478 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
4479 return got_error_from_errno("asprintf");
4481 if (asprintf(&initial_content,
4482 "\n# changes to be committed on branch %s:\n",
4483 a->branch_name) == -1)
4484 return got_error_from_errno("asprintf");
4486 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
4490 dprintf(fd, initial_content);
4492 TAILQ_FOREACH(pe, commitable_paths, entry) {
4493 struct got_commitable *ct = pe->data;
4494 dprintf(fd, "# %c %s\n",
4495 got_commitable_get_status(ct),
4496 got_commitable_get_path(ct));
4500 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
4502 if (err == NULL || err->code == GOT_ERR_COMMIT_MSG_EMPTY) {
4503 unlink(a->logmsg_path);
4504 free(a->logmsg_path);
4505 a->logmsg_path = NULL;
4507 free(initial_content);
4510 /* Editor is done; we can now apply unveil(2) */
4512 err = apply_unveil(a->repo_path, 0, a->worktree_path);
4521 static const struct got_error *
4522 cmd_commit(int argc, char *argv[])
4524 const struct got_error *error = NULL;
4525 struct got_worktree *worktree = NULL;
4526 struct got_repository *repo = NULL;
4527 char *cwd = NULL, *id_str = NULL;
4528 struct got_object_id *id = NULL;
4529 const char *logmsg = NULL;
4530 struct collect_commit_logmsg_arg cl_arg;
4531 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
4532 int ch, rebase_in_progress, histedit_in_progress;
4533 struct got_pathlist_head paths;
4536 cl_arg.logmsg_path = NULL;
4538 while ((ch = getopt(argc, argv, "m:")) != -1) {
4553 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4554 "unveil", NULL) == -1)
4557 cwd = getcwd(NULL, 0);
4559 error = got_error_from_errno("getcwd");
4562 error = got_worktree_open(&worktree, cwd);
4566 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4569 if (rebase_in_progress) {
4570 error = got_error(GOT_ERR_REBASING);
4574 error = got_worktree_histedit_in_progress(&histedit_in_progress,
4579 error = get_gitconfig_path(&gitconfig_path);
4582 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4587 error = get_author(&author, repo);
4592 * unveil(2) traverses exec(2); if an editor is used we have
4593 * to apply unveil after the log message has been written.
4595 if (logmsg == NULL || strlen(logmsg) == 0)
4596 error = get_editor(&editor);
4598 error = apply_unveil(got_repo_get_path(repo), 0,
4599 got_worktree_get_root_path(worktree));
4603 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4607 cl_arg.editor = editor;
4608 cl_arg.cmdline_log = logmsg;
4609 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
4610 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
4611 if (!histedit_in_progress) {
4612 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
4613 error = got_error(GOT_ERR_COMMIT_BRANCH);
4616 cl_arg.branch_name += 11;
4618 cl_arg.repo_path = got_repo_get_path(repo);
4619 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
4620 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
4622 if (cl_arg.logmsg_path)
4623 fprintf(stderr, "%s: log message preserved in %s\n",
4624 getprogname(), cl_arg.logmsg_path);
4628 if (cl_arg.logmsg_path)
4629 unlink(cl_arg.logmsg_path);
4631 error = got_object_id_str(&id_str, id);
4634 printf("Created commit %s\n", id_str);
4636 free(cl_arg.logmsg_path);
4638 got_repo_close(repo);
4640 got_worktree_close(worktree);
4643 free(gitconfig_path);
4650 usage_cherrypick(void)
4652 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
4656 static const struct got_error *
4657 cmd_cherrypick(int argc, char *argv[])
4659 const struct got_error *error = NULL;
4660 struct got_worktree *worktree = NULL;
4661 struct got_repository *repo = NULL;
4662 char *cwd = NULL, *commit_id_str = NULL;
4663 struct got_object_id *commit_id = NULL;
4664 struct got_commit_object *commit = NULL;
4665 struct got_object_qid *pid;
4666 struct got_reference *head_ref = NULL;
4667 int ch, did_something = 0;
4669 while ((ch = getopt(argc, argv, "")) != -1) {
4681 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4682 "unveil", NULL) == -1)
4688 cwd = getcwd(NULL, 0);
4690 error = got_error_from_errno("getcwd");
4693 error = got_worktree_open(&worktree, cwd);
4697 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4702 error = apply_unveil(got_repo_get_path(repo), 0,
4703 got_worktree_get_root_path(worktree));
4707 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4708 GOT_OBJ_TYPE_COMMIT, repo);
4709 if (error != NULL) {
4710 struct got_reference *ref;
4711 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4713 error = got_ref_open(&ref, repo, argv[0], 0);
4716 error = got_ref_resolve(&commit_id, repo, ref);
4721 error = got_object_id_str(&commit_id_str, commit_id);
4725 error = got_ref_open(&head_ref, repo,
4726 got_worktree_get_head_ref_name(worktree), 0);
4730 error = check_same_branch(commit_id, head_ref, NULL, repo);
4732 if (error->code != GOT_ERR_ANCESTRY)
4736 error = got_error(GOT_ERR_SAME_BRANCH);
4740 error = got_object_open_as_commit(&commit, repo, commit_id);
4743 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4744 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
4745 commit_id, repo, update_progress, &did_something, check_cancelled,
4751 printf("Merged commit %s\n", commit_id_str);
4754 got_object_commit_close(commit);
4755 free(commit_id_str);
4757 got_ref_close(head_ref);
4759 got_worktree_close(worktree);
4761 got_repo_close(repo);
4768 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
4772 static const struct got_error *
4773 cmd_backout(int argc, char *argv[])
4775 const struct got_error *error = NULL;
4776 struct got_worktree *worktree = NULL;
4777 struct got_repository *repo = NULL;
4778 char *cwd = NULL, *commit_id_str = NULL;
4779 struct got_object_id *commit_id = NULL;
4780 struct got_commit_object *commit = NULL;
4781 struct got_object_qid *pid;
4782 struct got_reference *head_ref = NULL;
4783 int ch, did_something = 0;
4785 while ((ch = getopt(argc, argv, "")) != -1) {
4797 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4798 "unveil", NULL) == -1)
4804 cwd = getcwd(NULL, 0);
4806 error = got_error_from_errno("getcwd");
4809 error = got_worktree_open(&worktree, cwd);
4813 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4818 error = apply_unveil(got_repo_get_path(repo), 0,
4819 got_worktree_get_root_path(worktree));
4823 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4824 GOT_OBJ_TYPE_COMMIT, repo);
4825 if (error != NULL) {
4826 struct got_reference *ref;
4827 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4829 error = got_ref_open(&ref, repo, argv[0], 0);
4832 error = got_ref_resolve(&commit_id, repo, ref);
4837 error = got_object_id_str(&commit_id_str, commit_id);
4841 error = got_ref_open(&head_ref, repo,
4842 got_worktree_get_head_ref_name(worktree), 0);
4846 error = check_same_branch(commit_id, head_ref, NULL, repo);
4850 error = got_object_open_as_commit(&commit, repo, commit_id);
4853 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4855 error = got_error(GOT_ERR_ROOT_COMMIT);
4859 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
4860 update_progress, &did_something, check_cancelled, NULL);
4865 printf("Backed out commit %s\n", commit_id_str);
4868 got_object_commit_close(commit);
4869 free(commit_id_str);
4871 got_ref_close(head_ref);
4873 got_worktree_close(worktree);
4875 got_repo_close(repo);
4882 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
4888 trim_logmsg(char *logmsg, int limit)
4893 len = strlen(logmsg);
4897 nl = strchr(logmsg, '\n');
4902 static const struct got_error *
4903 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
4905 const struct got_error *err;
4906 char *logmsg0 = NULL;
4909 err = got_object_commit_get_logmsg(&logmsg0, commit);
4914 while (isspace((unsigned char)s[0]))
4917 *logmsg = strdup(s);
4918 if (*logmsg == NULL) {
4919 err = got_error_from_errno("strdup");
4923 trim_logmsg(*logmsg, limit);
4929 static const struct got_error *
4930 show_rebase_progress(struct got_commit_object *commit,
4931 struct got_object_id *old_id, struct got_object_id *new_id)
4933 const struct got_error *err;
4934 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
4936 err = got_object_id_str(&old_id_str, old_id);
4941 err = got_object_id_str(&new_id_str, new_id);
4946 old_id_str[12] = '\0';
4948 new_id_str[12] = '\0';
4950 err = get_short_logmsg(&logmsg, 42, commit);
4954 printf("%s -> %s: %s\n", old_id_str,
4955 new_id_str ? new_id_str : "no-op change", logmsg);
4962 static const struct got_error *
4963 rebase_progress(void *arg, unsigned char status, const char *path)
4965 unsigned char *rebase_status = arg;
4967 while (path[0] == '/')
4969 printf("%c %s\n", status, path);
4971 if (*rebase_status == GOT_STATUS_CONFLICT)
4973 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
4974 *rebase_status = status;
4978 static const struct got_error *
4979 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
4980 struct got_reference *branch, struct got_reference *new_base_branch,
4981 struct got_reference *tmp_branch, struct got_repository *repo)
4983 printf("Switching work tree to %s\n", got_ref_get_name(branch));
4984 return got_worktree_rebase_complete(worktree, fileindex,
4985 new_base_branch, tmp_branch, branch, repo);
4988 static const struct got_error *
4989 rebase_commit(struct got_pathlist_head *merged_paths,
4990 struct got_worktree *worktree, struct got_fileindex *fileindex,
4991 struct got_reference *tmp_branch,
4992 struct got_object_id *commit_id, struct got_repository *repo)
4994 const struct got_error *error;
4995 struct got_commit_object *commit;
4996 struct got_object_id *new_commit_id;
4998 error = got_object_open_as_commit(&commit, repo, commit_id);
5002 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5003 worktree, fileindex, tmp_branch, commit, commit_id, repo);
5005 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5007 error = show_rebase_progress(commit, commit_id, NULL);
5009 error = show_rebase_progress(commit, commit_id, new_commit_id);
5010 free(new_commit_id);
5013 got_object_commit_close(commit);
5017 struct check_path_prefix_arg {
5018 const char *path_prefix;
5023 static const struct got_error *
5024 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5025 struct got_blob_object *blob2, struct got_object_id *id1,
5026 struct got_object_id *id2, const char *path1, const char *path2,
5027 struct got_repository *repo)
5029 struct check_path_prefix_arg *a = arg;
5031 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5032 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5033 return got_error(a->errcode);
5038 static const struct got_error *
5039 check_path_prefix(struct got_object_id *parent_id,
5040 struct got_object_id *commit_id, const char *path_prefix,
5041 int errcode, struct got_repository *repo)
5043 const struct got_error *err;
5044 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
5045 struct got_commit_object *commit = NULL, *parent_commit = NULL;
5046 struct check_path_prefix_arg cpp_arg;
5048 if (got_path_is_root_dir(path_prefix))
5051 err = got_object_open_as_commit(&commit, repo, commit_id);
5055 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
5059 err = got_object_open_as_tree(&tree1, repo,
5060 got_object_commit_get_tree_id(parent_commit));
5064 err = got_object_open_as_tree(&tree2, repo,
5065 got_object_commit_get_tree_id(commit));
5069 cpp_arg.path_prefix = path_prefix;
5070 while (cpp_arg.path_prefix[0] == '/')
5071 cpp_arg.path_prefix++;
5072 cpp_arg.len = strlen(cpp_arg.path_prefix);
5073 cpp_arg.errcode = errcode;
5074 err = got_diff_tree(tree1, tree2, "", "", repo,
5075 check_path_prefix_in_diff, &cpp_arg, 0);
5078 got_object_tree_close(tree1);
5080 got_object_tree_close(tree2);
5082 got_object_commit_close(commit);
5084 got_object_commit_close(parent_commit);
5088 static const struct got_error *
5089 collect_commits(struct got_object_id_queue *commits,
5090 struct got_object_id *initial_commit_id,
5091 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
5092 const char *path_prefix, int path_prefix_errcode,
5093 struct got_repository *repo)
5095 const struct got_error *err = NULL;
5096 struct got_commit_graph *graph = NULL;
5097 struct got_object_id *parent_id = NULL;
5098 struct got_object_qid *qid;
5099 struct got_object_id *commit_id = initial_commit_id;
5101 err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
5105 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
5106 check_cancelled, NULL);
5109 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
5110 err = got_commit_graph_iter_next(&parent_id, graph);
5112 if (err->code == GOT_ERR_ITER_COMPLETED) {
5113 err = got_error_msg(GOT_ERR_ANCESTRY,
5114 "ran out of commits to rebase before "
5115 "youngest common ancestor commit has "
5118 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
5120 err = got_commit_graph_fetch_commits(graph, 1, repo,
5121 check_cancelled, NULL);
5125 err = check_path_prefix(parent_id, commit_id,
5126 path_prefix, path_prefix_errcode, repo);
5130 err = got_object_qid_alloc(&qid, commit_id);
5133 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
5134 commit_id = parent_id;
5138 got_commit_graph_close(graph);
5142 static const struct got_error *
5143 cmd_rebase(int argc, char *argv[])
5145 const struct got_error *error = NULL;
5146 struct got_worktree *worktree = NULL;
5147 struct got_repository *repo = NULL;
5148 struct got_fileindex *fileindex = NULL;
5150 struct got_reference *branch = NULL;
5151 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
5152 struct got_object_id *commit_id = NULL, *parent_id = NULL;
5153 struct got_object_id *resume_commit_id = NULL;
5154 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
5155 struct got_commit_object *commit = NULL;
5156 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
5157 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5158 struct got_object_id_queue commits;
5159 struct got_pathlist_head merged_paths;
5160 const struct got_object_id_queue *parent_ids;
5161 struct got_object_qid *qid, *pid;
5163 SIMPLEQ_INIT(&commits);
5164 TAILQ_INIT(&merged_paths);
5166 while ((ch = getopt(argc, argv, "ac")) != -1) {
5172 continue_rebase = 1;
5184 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5185 "unveil", NULL) == -1)
5188 if (abort_rebase && continue_rebase)
5190 else if (abort_rebase || continue_rebase) {
5193 } else if (argc != 1)
5196 cwd = getcwd(NULL, 0);
5198 error = got_error_from_errno("getcwd");
5201 error = got_worktree_open(&worktree, cwd);
5205 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5210 error = apply_unveil(got_repo_get_path(repo), 0,
5211 got_worktree_get_root_path(worktree));
5215 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5221 if (!rebase_in_progress) {
5222 error = got_error(GOT_ERR_NOT_REBASING);
5225 error = got_worktree_rebase_continue(&resume_commit_id,
5226 &new_base_branch, &tmp_branch, &branch, &fileindex,
5230 printf("Switching work tree to %s\n",
5231 got_ref_get_symref_target(new_base_branch));
5232 error = got_worktree_rebase_abort(worktree, fileindex, repo,
5233 new_base_branch, update_progress, &did_something);
5236 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5237 goto done; /* nothing else to do */
5240 if (continue_rebase) {
5241 if (!rebase_in_progress) {
5242 error = got_error(GOT_ERR_NOT_REBASING);
5245 error = got_worktree_rebase_continue(&resume_commit_id,
5246 &new_base_branch, &tmp_branch, &branch, &fileindex,
5251 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5252 resume_commit_id, repo);
5256 yca_id = got_object_id_dup(resume_commit_id);
5257 if (yca_id == NULL) {
5258 error = got_error_from_errno("got_object_id_dup");
5262 error = got_ref_open(&branch, repo, argv[0], 0);
5267 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5271 if (!continue_rebase) {
5272 struct got_object_id *base_commit_id;
5274 base_commit_id = got_worktree_get_base_commit_id(worktree);
5275 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5276 base_commit_id, branch_head_commit_id, repo,
5277 check_cancelled, NULL);
5280 if (yca_id == NULL) {
5281 error = got_error_msg(GOT_ERR_ANCESTRY,
5282 "specified branch shares no common ancestry "
5283 "with work tree's branch");
5287 error = check_same_branch(base_commit_id, branch, yca_id, repo);
5289 if (error->code != GOT_ERR_ANCESTRY)
5293 error = got_error_msg(GOT_ERR_SAME_BRANCH,
5294 "specified branch resolves to a commit which "
5295 "is already contained in work tree's branch");
5298 error = got_worktree_rebase_prepare(&new_base_branch,
5299 &tmp_branch, &fileindex, worktree, branch, repo);
5304 commit_id = branch_head_commit_id;
5305 error = got_object_open_as_commit(&commit, repo, commit_id);
5309 parent_ids = got_object_commit_get_parent_ids(commit);
5310 pid = SIMPLEQ_FIRST(parent_ids);
5312 if (!continue_rebase) {
5314 error = got_worktree_rebase_abort(worktree, fileindex,
5315 repo, new_base_branch, update_progress,
5319 printf("Rebase of %s aborted\n",
5320 got_ref_get_name(branch));
5322 error = got_error(GOT_ERR_EMPTY_REBASE);
5325 error = collect_commits(&commits, commit_id, pid->id,
5326 yca_id, got_worktree_get_path_prefix(worktree),
5327 GOT_ERR_REBASE_PATH, repo);
5328 got_object_commit_close(commit);
5333 if (SIMPLEQ_EMPTY(&commits)) {
5334 if (continue_rebase)
5335 error = rebase_complete(worktree, fileindex,
5336 branch, new_base_branch, tmp_branch, repo);
5338 error = got_error(GOT_ERR_EMPTY_REBASE);
5343 SIMPLEQ_FOREACH(qid, &commits, entry) {
5344 commit_id = qid->id;
5345 parent_id = pid ? pid->id : yca_id;
5348 error = got_worktree_rebase_merge_files(&merged_paths,
5349 worktree, fileindex, parent_id, commit_id, repo,
5350 rebase_progress, &rebase_status, check_cancelled, NULL);
5354 if (rebase_status == GOT_STATUS_CONFLICT) {
5355 got_worktree_rebase_pathlist_free(&merged_paths);
5359 error = rebase_commit(&merged_paths, worktree, fileindex,
5360 tmp_branch, commit_id, repo);
5361 got_worktree_rebase_pathlist_free(&merged_paths);
5366 if (rebase_status == GOT_STATUS_CONFLICT) {
5367 error = got_worktree_rebase_postpone(worktree, fileindex);
5370 error = got_error_msg(GOT_ERR_CONFLICTS,
5371 "conflicts must be resolved before rebasing can continue");
5373 error = rebase_complete(worktree, fileindex, branch,
5374 new_base_branch, tmp_branch, repo);
5376 got_object_id_queue_free(&commits);
5377 free(branch_head_commit_id);
5378 free(resume_commit_id);
5381 got_object_commit_close(commit);
5383 got_ref_close(branch);
5384 if (new_base_branch)
5385 got_ref_close(new_base_branch);
5387 got_ref_close(tmp_branch);
5389 got_worktree_close(worktree);
5391 got_repo_close(repo);
5396 usage_histedit(void)
5398 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script]\n",
5403 #define GOT_HISTEDIT_PICK 'p'
5404 #define GOT_HISTEDIT_EDIT 'e'
5405 #define GOT_HISTEDIT_FOLD 'f'
5406 #define GOT_HISTEDIT_DROP 'd'
5407 #define GOT_HISTEDIT_MESG 'm'
5409 static struct got_histedit_cmd {
5413 } got_histedit_cmds[] = {
5414 { GOT_HISTEDIT_PICK, "pick", "use commit" },
5415 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5416 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
5417 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5418 { GOT_HISTEDIT_MESG, "mesg",
5419 "single-line log message for commit above (open editor if empty)" },
5422 struct got_histedit_list_entry {
5423 TAILQ_ENTRY(got_histedit_list_entry) entry;
5424 struct got_object_id *commit_id;
5425 const struct got_histedit_cmd *cmd;
5428 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5430 static const struct got_error *
5431 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5432 FILE *f, struct got_repository *repo)
5434 const struct got_error *err = NULL;
5435 char *logmsg = NULL, *id_str = NULL;
5436 struct got_commit_object *commit = NULL;
5439 err = got_object_open_as_commit(&commit, repo, commit_id);
5443 err = get_short_logmsg(&logmsg, 34, commit);
5447 err = got_object_id_str(&id_str, commit_id);
5451 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
5453 err = got_ferror(f, GOT_ERR_IO);
5456 got_object_commit_close(commit);
5462 static const struct got_error *
5463 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
5464 struct got_repository *repo)
5466 const struct got_error *err = NULL;
5467 struct got_object_qid *qid;
5469 if (SIMPLEQ_EMPTY(commits))
5470 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5472 SIMPLEQ_FOREACH(qid, commits, entry) {
5473 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
5482 static const struct got_error *
5483 write_cmd_list(FILE *f)
5485 const struct got_error *err = NULL;
5488 n = fprintf(f, "# Available histedit commands:\n");
5490 return got_ferror(f, GOT_ERR_IO);
5492 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5493 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
5494 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
5497 err = got_ferror(f, GOT_ERR_IO);
5501 n = fprintf(f, "# Commits will be processed in order from top to "
5502 "bottom of this file.\n");
5504 return got_ferror(f, GOT_ERR_IO);
5508 static const struct got_error *
5509 histedit_syntax_error(int lineno)
5511 static char msg[42];
5514 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
5516 if (ret == -1 || ret >= sizeof(msg))
5517 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
5519 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
5522 static const struct got_error *
5523 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
5524 char *logmsg, struct got_repository *repo)
5526 const struct got_error *err;
5527 struct got_commit_object *folded_commit = NULL;
5528 char *id_str, *folded_logmsg = NULL;
5530 err = got_object_id_str(&id_str, hle->commit_id);
5534 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
5538 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
5541 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
5542 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
5543 folded_logmsg) == -1) {
5544 err = got_error_from_errno("asprintf");
5549 got_object_commit_close(folded_commit);
5551 free(folded_logmsg);
5555 static struct got_histedit_list_entry *
5556 get_folded_commits(struct got_histedit_list_entry *hle)
5558 struct got_histedit_list_entry *prev, *folded = NULL;
5560 prev = TAILQ_PREV(hle, got_histedit_list, entry);
5561 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
5562 prev->cmd->code == GOT_HISTEDIT_DROP)) {
5563 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
5565 prev = TAILQ_PREV(prev, got_histedit_list, entry);
5571 static const struct got_error *
5572 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
5573 struct got_repository *repo)
5575 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
5576 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
5577 const struct got_error *err = NULL;
5578 struct got_commit_object *commit = NULL;
5580 struct got_histedit_list_entry *folded = NULL;
5582 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5586 folded = get_folded_commits(hle);
5588 while (folded != hle) {
5589 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
5590 folded = TAILQ_NEXT(folded, entry);
5593 err = append_folded_commit_msg(&new_msg, folded,
5599 folded = TAILQ_NEXT(folded, entry);
5603 err = got_object_id_str(&id_str, hle->commit_id);
5606 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
5609 if (asprintf(&new_msg,
5610 "%s\n# original log message of commit %s: %s",
5611 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
5612 err = got_error_from_errno("asprintf");
5618 err = got_object_id_str(&id_str, hle->commit_id);
5622 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
5626 dprintf(fd, logmsg);
5629 err = get_editor(&editor);
5633 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
5635 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
5637 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
5640 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
5641 err = got_error_from_errno2("unlink", logmsg_path);
5647 got_object_commit_close(commit);
5651 static const struct got_error *
5652 histedit_parse_list(struct got_histedit_list *histedit_cmds,
5653 FILE *f, struct got_repository *repo)
5655 const struct got_error *err = NULL;
5656 char *line = NULL, *p, *end;
5660 const struct got_histedit_cmd *cmd;
5661 struct got_object_id *commit_id = NULL;
5662 struct got_histedit_list_entry *hle = NULL;
5665 len = getline(&line, &size, f);
5667 const struct got_error *getline_err;
5670 getline_err = got_error_from_errno("getline");
5671 err = got_ferror(f, getline_err->code);
5676 while (isspace((unsigned char)p[0]))
5678 if (p[0] == '#' || p[0] == '\0') {
5684 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5685 cmd = &got_histedit_cmds[i];
5686 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
5687 isspace((unsigned char)p[strlen(cmd->name)])) {
5688 p += strlen(cmd->name);
5691 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
5696 if (i == nitems(got_histedit_cmds)) {
5697 err = histedit_syntax_error(lineno);
5700 while (isspace((unsigned char)p[0]))
5702 if (cmd->code == GOT_HISTEDIT_MESG) {
5703 if (hle == NULL || hle->logmsg != NULL) {
5704 err = got_error(GOT_ERR_HISTEDIT_CMD);
5708 err = histedit_edit_logmsg(hle, repo);
5712 hle->logmsg = strdup(p);
5713 if (hle->logmsg == NULL) {
5714 err = got_error_from_errno("strdup");
5723 while (end[0] && !isspace((unsigned char)end[0]))
5727 err = got_object_resolve_id_str(&commit_id, repo, p);
5729 /* override error code */
5730 err = histedit_syntax_error(lineno);
5734 hle = malloc(sizeof(*hle));
5736 err = got_error_from_errno("malloc");
5740 hle->commit_id = commit_id;
5745 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
5753 static const struct got_error *
5754 histedit_check_script(struct got_histedit_list *histedit_cmds,
5755 struct got_object_id_queue *commits, struct got_repository *repo)
5757 const struct got_error *err = NULL;
5758 struct got_object_qid *qid;
5759 struct got_histedit_list_entry *hle;
5760 static char msg[80];
5763 if (TAILQ_EMPTY(histedit_cmds))
5764 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
5765 "histedit script contains no commands");
5766 if (SIMPLEQ_EMPTY(commits))
5767 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5769 SIMPLEQ_FOREACH(qid, commits, entry) {
5770 TAILQ_FOREACH(hle, histedit_cmds, entry) {
5771 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
5775 err = got_object_id_str(&id_str, qid->id);
5778 snprintf(msg, sizeof(msg),
5779 "commit %s missing from histedit script", id_str);
5781 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
5785 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
5786 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
5787 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
5788 "last commit in histedit script cannot be folded");
5793 static const struct got_error *
5794 histedit_run_editor(struct got_histedit_list *histedit_cmds,
5795 const char *path, struct got_object_id_queue *commits,
5796 struct got_repository *repo)
5798 const struct got_error *err = NULL;
5802 err = get_editor(&editor);
5806 if (spawn_editor(editor, path) == -1) {
5807 err = got_error_from_errno("failed spawning editor");
5811 f = fopen(path, "r");
5813 err = got_error_from_errno("fopen");
5816 err = histedit_parse_list(histedit_cmds, f, repo);
5820 err = histedit_check_script(histedit_cmds, commits, repo);
5822 if (f && fclose(f) != 0 && err == NULL)
5823 err = got_error_from_errno("fclose");
5828 static const struct got_error *
5829 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
5830 struct got_object_id_queue *, const char *, struct got_repository *);
5832 static const struct got_error *
5833 histedit_edit_script(struct got_histedit_list *histedit_cmds,
5834 struct got_object_id_queue *commits, struct got_repository *repo)
5836 const struct got_error *err;
5840 err = got_opentemp_named(&path, &f, "got-histedit");
5844 err = write_cmd_list(f);
5848 err = histedit_write_commit_list(commits, f, repo);
5852 if (fclose(f) != 0) {
5853 err = got_error_from_errno("fclose");
5858 err = histedit_run_editor(histedit_cmds, path, commits, repo);
5860 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5861 err->code != GOT_ERR_HISTEDIT_CMD)
5863 err = histedit_edit_list_retry(histedit_cmds, err,
5864 commits, path, repo);
5867 if (f && fclose(f) != 0 && err == NULL)
5868 err = got_error_from_errno("fclose");
5869 if (path && unlink(path) != 0 && err == NULL)
5870 err = got_error_from_errno2("unlink", path);
5875 static const struct got_error *
5876 histedit_save_list(struct got_histedit_list *histedit_cmds,
5877 struct got_worktree *worktree, struct got_repository *repo)
5879 const struct got_error *err = NULL;
5882 struct got_histedit_list_entry *hle;
5883 struct got_commit_object *commit = NULL;
5885 err = got_worktree_get_histedit_script_path(&path, worktree);
5889 f = fopen(path, "w");
5891 err = got_error_from_errno2("fopen", path);
5894 TAILQ_FOREACH(hle, histedit_cmds, entry) {
5895 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
5901 int n = fprintf(f, "%c %s\n",
5902 GOT_HISTEDIT_MESG, hle->logmsg);
5904 err = got_ferror(f, GOT_ERR_IO);
5910 if (f && fclose(f) != 0 && err == NULL)
5911 err = got_error_from_errno("fclose");
5914 got_object_commit_close(commit);
5919 histedit_free_list(struct got_histedit_list *histedit_cmds)
5921 struct got_histedit_list_entry *hle;
5923 while ((hle = TAILQ_FIRST(histedit_cmds))) {
5924 TAILQ_REMOVE(histedit_cmds, hle, entry);
5929 static const struct got_error *
5930 histedit_load_list(struct got_histedit_list *histedit_cmds,
5931 const char *path, struct got_repository *repo)
5933 const struct got_error *err = NULL;
5936 f = fopen(path, "r");
5938 err = got_error_from_errno2("fopen", path);
5942 err = histedit_parse_list(histedit_cmds, f, repo);
5944 if (f && fclose(f) != 0 && err == NULL)
5945 err = got_error_from_errno("fclose");
5949 static const struct got_error *
5950 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
5951 const struct got_error *edit_err, struct got_object_id_queue *commits,
5952 const char *path, struct got_repository *repo)
5954 const struct got_error *err = NULL, *prev_err = edit_err;
5957 while (resp != 'c' && resp != 'r' && resp != 'a') {
5958 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
5959 "or (a)bort: ", getprogname(), prev_err->msg);
5964 histedit_free_list(histedit_cmds);
5965 err = histedit_run_editor(histedit_cmds, path, commits,
5968 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5969 err->code != GOT_ERR_HISTEDIT_CMD)
5976 } else if (resp == 'r') {
5977 histedit_free_list(histedit_cmds);
5978 err = histedit_edit_script(histedit_cmds,
5981 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5982 err->code != GOT_ERR_HISTEDIT_CMD)
5989 } else if (resp == 'a') {
5990 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
5993 printf("invalid response '%c'\n", resp);
5999 static const struct got_error *
6000 histedit_complete(struct got_worktree *worktree,
6001 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6002 struct got_reference *branch, struct got_repository *repo)
6004 printf("Switching work tree to %s\n",
6005 got_ref_get_symref_target(branch));
6006 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
6010 static const struct got_error *
6011 show_histedit_progress(struct got_commit_object *commit,
6012 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
6014 const struct got_error *err;
6015 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6017 err = got_object_id_str(&old_id_str, hle->commit_id);
6022 err = got_object_id_str(&new_id_str, new_id);
6027 old_id_str[12] = '\0';
6029 new_id_str[12] = '\0';
6032 logmsg = strdup(hle->logmsg);
6033 if (logmsg == NULL) {
6034 err = got_error_from_errno("strdup");
6037 trim_logmsg(logmsg, 42);
6039 err = get_short_logmsg(&logmsg, 42, commit);
6044 switch (hle->cmd->code) {
6045 case GOT_HISTEDIT_PICK:
6046 case GOT_HISTEDIT_EDIT:
6047 printf("%s -> %s: %s\n", old_id_str,
6048 new_id_str ? new_id_str : "no-op change", logmsg);
6050 case GOT_HISTEDIT_DROP:
6051 case GOT_HISTEDIT_FOLD:
6052 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
6065 static const struct got_error *
6066 histedit_commit(struct got_pathlist_head *merged_paths,
6067 struct got_worktree *worktree, struct got_fileindex *fileindex,
6068 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
6069 struct got_repository *repo)
6071 const struct got_error *err;
6072 struct got_commit_object *commit;
6073 struct got_object_id *new_commit_id;
6075 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
6076 && hle->logmsg == NULL) {
6077 err = histedit_edit_logmsg(hle, repo);
6082 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6086 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
6087 worktree, fileindex, tmp_branch, commit, hle->commit_id,
6090 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
6092 err = show_histedit_progress(commit, hle, NULL);
6094 err = show_histedit_progress(commit, hle, new_commit_id);
6095 free(new_commit_id);
6098 got_object_commit_close(commit);
6102 static const struct got_error *
6103 histedit_skip_commit(struct got_histedit_list_entry *hle,
6104 struct got_worktree *worktree, struct got_repository *repo)
6106 const struct got_error *error;
6107 struct got_commit_object *commit;
6109 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
6114 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
6118 error = show_histedit_progress(commit, hle, NULL);
6119 got_object_commit_close(commit);
6123 static const struct got_error *
6124 cmd_histedit(int argc, char *argv[])
6126 const struct got_error *error = NULL;
6127 struct got_worktree *worktree = NULL;
6128 struct got_fileindex *fileindex = NULL;
6129 struct got_repository *repo = NULL;
6131 struct got_reference *branch = NULL;
6132 struct got_reference *tmp_branch = NULL;
6133 struct got_object_id *resume_commit_id = NULL;
6134 struct got_object_id *base_commit_id = NULL;
6135 struct got_object_id *head_commit_id = NULL;
6136 struct got_commit_object *commit = NULL;
6137 int ch, rebase_in_progress = 0, did_something;
6138 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
6139 const char *edit_script_path = NULL;
6140 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6141 struct got_object_id_queue commits;
6142 struct got_pathlist_head merged_paths;
6143 const struct got_object_id_queue *parent_ids;
6144 struct got_object_qid *pid;
6145 struct got_histedit_list histedit_cmds;
6146 struct got_histedit_list_entry *hle;
6148 SIMPLEQ_INIT(&commits);
6149 TAILQ_INIT(&histedit_cmds);
6150 TAILQ_INIT(&merged_paths);
6152 while ((ch = getopt(argc, argv, "acF:")) != -1) {
6161 edit_script_path = optarg;
6173 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6174 "unveil", NULL) == -1)
6177 if (abort_edit && continue_edit)
6183 * This command cannot apply unveil(2) in all cases because the
6184 * user may choose to run an editor to edit the histedit script
6185 * and to edit individual commit log messages.
6186 * unveil(2) traverses exec(2); if an editor is used we have to
6187 * apply unveil after edit script and log messages have been written.
6188 * XXX TODO: Make use of unveil(2) where possible.
6191 cwd = getcwd(NULL, 0);
6193 error = got_error_from_errno("getcwd");
6196 error = got_worktree_open(&worktree, cwd);
6200 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6205 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6208 if (rebase_in_progress) {
6209 error = got_error(GOT_ERR_REBASING);
6213 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6217 if (edit_in_progress && abort_edit) {
6218 error = got_worktree_histedit_continue(&resume_commit_id,
6219 &tmp_branch, &branch, &base_commit_id, &fileindex,
6223 printf("Switching work tree to %s\n",
6224 got_ref_get_symref_target(branch));
6225 error = got_worktree_histedit_abort(worktree, fileindex, repo,
6226 branch, base_commit_id, update_progress, &did_something);
6229 printf("Histedit of %s aborted\n",
6230 got_ref_get_symref_target(branch));
6231 goto done; /* nothing else to do */
6232 } else if (abort_edit) {
6233 error = got_error(GOT_ERR_NOT_HISTEDIT);
6237 if (continue_edit) {
6240 if (!edit_in_progress) {
6241 error = got_error(GOT_ERR_NOT_HISTEDIT);
6245 error = got_worktree_get_histedit_script_path(&path, worktree);
6249 error = histedit_load_list(&histedit_cmds, path, repo);
6254 error = got_worktree_histedit_continue(&resume_commit_id,
6255 &tmp_branch, &branch, &base_commit_id, &fileindex,
6260 error = got_ref_resolve(&head_commit_id, repo, branch);
6264 error = got_object_open_as_commit(&commit, repo,
6268 parent_ids = got_object_commit_get_parent_ids(commit);
6269 pid = SIMPLEQ_FIRST(parent_ids);
6271 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6274 error = collect_commits(&commits, head_commit_id, pid->id,
6275 base_commit_id, got_worktree_get_path_prefix(worktree),
6276 GOT_ERR_HISTEDIT_PATH, repo);
6277 got_object_commit_close(commit);
6282 if (edit_in_progress) {
6283 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6287 error = got_ref_open(&branch, repo,
6288 got_worktree_get_head_ref_name(worktree), 0);
6292 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
6293 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
6294 "will not edit commit history of a branch outside "
6295 "the \"refs/heads/\" reference namespace");
6299 error = got_ref_resolve(&head_commit_id, repo, branch);
6300 got_ref_close(branch);
6305 error = got_object_open_as_commit(&commit, repo,
6309 parent_ids = got_object_commit_get_parent_ids(commit);
6310 pid = SIMPLEQ_FIRST(parent_ids);
6312 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6315 error = collect_commits(&commits, head_commit_id, pid->id,
6316 got_worktree_get_base_commit_id(worktree),
6317 got_worktree_get_path_prefix(worktree),
6318 GOT_ERR_HISTEDIT_PATH, repo);
6319 got_object_commit_close(commit);
6324 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
6325 &base_commit_id, &fileindex, worktree, repo);
6329 if (edit_script_path) {
6330 error = histedit_load_list(&histedit_cmds,
6331 edit_script_path, repo);
6333 got_worktree_histedit_abort(worktree, fileindex,
6334 repo, branch, base_commit_id,
6335 update_progress, &did_something);
6339 error = histedit_edit_script(&histedit_cmds, &commits,
6342 got_worktree_histedit_abort(worktree, fileindex,
6343 repo, branch, base_commit_id,
6344 update_progress, &did_something);
6350 error = histedit_save_list(&histedit_cmds, worktree,
6353 got_worktree_histedit_abort(worktree, fileindex,
6354 repo, branch, base_commit_id,
6355 update_progress, &did_something);
6361 error = histedit_check_script(&histedit_cmds, &commits, repo);
6365 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
6366 if (resume_commit_id) {
6367 if (got_object_id_cmp(hle->commit_id,
6368 resume_commit_id) != 0)
6371 resume_commit_id = NULL;
6372 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
6373 hle->cmd->code == GOT_HISTEDIT_FOLD) {
6374 error = histedit_skip_commit(hle, worktree,
6377 error = histedit_commit(NULL, worktree,
6378 fileindex, tmp_branch, hle, repo);
6385 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
6386 error = histedit_skip_commit(hle, worktree, repo);
6392 error = got_object_open_as_commit(&commit, repo,
6396 parent_ids = got_object_commit_get_parent_ids(commit);
6397 pid = SIMPLEQ_FIRST(parent_ids);
6399 error = got_worktree_histedit_merge_files(&merged_paths,
6400 worktree, fileindex, pid->id, hle->commit_id, repo,
6401 rebase_progress, &rebase_status, check_cancelled, NULL);
6404 got_object_commit_close(commit);
6407 if (rebase_status == GOT_STATUS_CONFLICT) {
6408 got_worktree_rebase_pathlist_free(&merged_paths);
6412 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
6414 error = got_object_id_str(&id_str, hle->commit_id);
6417 printf("Stopping histedit for amending commit %s\n",
6420 got_worktree_rebase_pathlist_free(&merged_paths);
6421 error = got_worktree_histedit_postpone(worktree,
6426 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
6427 error = histedit_skip_commit(hle, worktree, repo);
6433 error = histedit_commit(&merged_paths, worktree, fileindex,
6434 tmp_branch, hle, repo);
6435 got_worktree_rebase_pathlist_free(&merged_paths);
6440 if (rebase_status == GOT_STATUS_CONFLICT) {
6441 error = got_worktree_histedit_postpone(worktree, fileindex);
6444 error = got_error_msg(GOT_ERR_CONFLICTS,
6445 "conflicts must be resolved before rebasing can continue");
6447 error = histedit_complete(worktree, fileindex, tmp_branch,
6450 got_object_id_queue_free(&commits);
6451 histedit_free_list(&histedit_cmds);
6452 free(head_commit_id);
6453 free(base_commit_id);
6454 free(resume_commit_id);
6456 got_object_commit_close(commit);
6458 got_ref_close(branch);
6460 got_ref_close(tmp_branch);
6462 got_worktree_close(worktree);
6464 got_repo_close(repo);
6469 usage_integrate(void)
6471 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
6475 static const struct got_error *
6476 cmd_integrate(int argc, char *argv[])
6478 const struct got_error *error = NULL;
6479 struct got_repository *repo = NULL;
6480 struct got_worktree *worktree = NULL;
6481 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
6482 const char *branch_arg = NULL;
6483 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
6484 struct got_fileindex *fileindex = NULL;
6485 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
6486 int ch, did_something = 0;
6488 while ((ch = getopt(argc, argv, "")) != -1) {
6501 branch_arg = argv[0];
6503 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6504 "unveil", NULL) == -1)
6507 cwd = getcwd(NULL, 0);
6509 error = got_error_from_errno("getcwd");
6513 error = got_worktree_open(&worktree, cwd);
6517 error = check_rebase_or_histedit_in_progress(worktree);
6521 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6526 error = apply_unveil(got_repo_get_path(repo), 0,
6527 got_worktree_get_root_path(worktree));
6531 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
6532 error = got_error_from_errno("asprintf");
6536 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
6537 &base_branch_ref, worktree, refname, repo);
6541 refname = strdup(got_ref_get_name(branch_ref));
6542 if (refname == NULL) {
6543 error = got_error_from_errno("strdup");
6544 got_worktree_integrate_abort(worktree, fileindex, repo,
6545 branch_ref, base_branch_ref);
6548 base_refname = strdup(got_ref_get_name(base_branch_ref));
6549 if (base_refname == NULL) {
6550 error = got_error_from_errno("strdup");
6551 got_worktree_integrate_abort(worktree, fileindex, repo,
6552 branch_ref, base_branch_ref);
6556 error = got_ref_resolve(&commit_id, repo, branch_ref);
6560 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
6564 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
6565 error = got_error_msg(GOT_ERR_SAME_BRANCH,
6566 "specified branch has already been integrated");
6567 got_worktree_integrate_abort(worktree, fileindex, repo,
6568 branch_ref, base_branch_ref);
6572 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
6574 if (error->code == GOT_ERR_ANCESTRY)
6575 error = got_error(GOT_ERR_REBASE_REQUIRED);
6576 got_worktree_integrate_abort(worktree, fileindex, repo,
6577 branch_ref, base_branch_ref);
6581 error = got_worktree_integrate_continue(worktree, fileindex, repo,
6582 branch_ref, base_branch_ref, update_progress, &did_something,
6583 check_cancelled, NULL);
6587 printf("Integrated %s into %s\n", refname, base_refname);
6590 got_repo_close(repo);
6592 got_worktree_close(worktree);
6594 free(base_commit_id);
6604 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
6605 "[file-path ...]\n",
6610 static const struct got_error *
6611 print_stage(void *arg, unsigned char status, unsigned char staged_status,
6612 const char *path, struct got_object_id *blob_id,
6613 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
6615 const struct got_error *err = NULL;
6616 char *id_str = NULL;
6618 if (staged_status != GOT_STATUS_ADD &&
6619 staged_status != GOT_STATUS_MODIFY &&
6620 staged_status != GOT_STATUS_DELETE)
6623 if (staged_status == GOT_STATUS_ADD ||
6624 staged_status == GOT_STATUS_MODIFY)
6625 err = got_object_id_str(&id_str, staged_blob_id);
6627 err = got_object_id_str(&id_str, blob_id);
6631 printf("%s %c %s\n", id_str, staged_status, path);
6636 static const struct got_error *
6637 cmd_stage(int argc, char *argv[])
6639 const struct got_error *error = NULL;
6640 struct got_repository *repo = NULL;
6641 struct got_worktree *worktree = NULL;
6643 struct got_pathlist_head paths;
6644 struct got_pathlist_entry *pe;
6645 int ch, list_stage = 0, pflag = 0;
6646 FILE *patch_script_file = NULL;
6647 const char *patch_script_path = NULL;
6648 struct choose_patch_arg cpa;
6652 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
6661 patch_script_path = optarg;
6673 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6674 "unveil", NULL) == -1)
6677 if (list_stage && (pflag || patch_script_path))
6678 errx(1, "-l option cannot be used with other options");
6679 if (patch_script_path && !pflag)
6680 errx(1, "-F option can only be used together with -p option");
6682 cwd = getcwd(NULL, 0);
6684 error = got_error_from_errno("getcwd");
6688 error = got_worktree_open(&worktree, cwd);
6692 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6697 if (patch_script_path) {
6698 patch_script_file = fopen(patch_script_path, "r");
6699 if (patch_script_file == NULL) {
6700 error = got_error_from_errno2("fopen",
6705 error = apply_unveil(got_repo_get_path(repo), 0,
6706 got_worktree_get_root_path(worktree));
6710 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6715 error = got_worktree_status(worktree, &paths, repo,
6716 print_stage, NULL, check_cancelled, NULL);
6718 cpa.patch_script_file = patch_script_file;
6719 cpa.action = "stage";
6720 error = got_worktree_stage(worktree, &paths,
6721 pflag ? NULL : print_status, NULL,
6722 pflag ? choose_patch : NULL, &cpa, repo);
6725 if (patch_script_file && fclose(patch_script_file) == EOF &&
6727 error = got_error_from_errno2("fclose", patch_script_path);
6729 got_repo_close(repo);
6731 got_worktree_close(worktree);
6732 TAILQ_FOREACH(pe, &paths, entry)
6733 free((char *)pe->path);
6734 got_pathlist_free(&paths);
6742 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
6743 "[file-path ...]\n",
6749 static const struct got_error *
6750 cmd_unstage(int argc, char *argv[])
6752 const struct got_error *error = NULL;
6753 struct got_repository *repo = NULL;
6754 struct got_worktree *worktree = NULL;
6756 struct got_pathlist_head paths;
6757 struct got_pathlist_entry *pe;
6758 int ch, did_something = 0, pflag = 0;
6759 FILE *patch_script_file = NULL;
6760 const char *patch_script_path = NULL;
6761 struct choose_patch_arg cpa;
6765 while ((ch = getopt(argc, argv, "pF:")) != -1) {
6771 patch_script_path = optarg;
6783 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6784 "unveil", NULL) == -1)
6787 if (patch_script_path && !pflag)
6788 errx(1, "-F option can only be used together with -p option");
6790 cwd = getcwd(NULL, 0);
6792 error = got_error_from_errno("getcwd");
6796 error = got_worktree_open(&worktree, cwd);
6800 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6805 if (patch_script_path) {
6806 patch_script_file = fopen(patch_script_path, "r");
6807 if (patch_script_file == NULL) {
6808 error = got_error_from_errno2("fopen",
6814 error = apply_unveil(got_repo_get_path(repo), 0,
6815 got_worktree_get_root_path(worktree));
6819 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6823 cpa.patch_script_file = patch_script_file;
6824 cpa.action = "unstage";
6825 error = got_worktree_unstage(worktree, &paths, update_progress,
6826 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
6828 if (patch_script_file && fclose(patch_script_file) == EOF &&
6830 error = got_error_from_errno2("fclose", patch_script_path);
6832 got_repo_close(repo);
6834 got_worktree_close(worktree);
6835 TAILQ_FOREACH(pe, &paths, entry)
6836 free((char *)pe->path);
6837 got_pathlist_free(&paths);
6845 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
6846 "arg1 [arg2 ...]\n", getprogname());
6850 static const struct got_error *
6851 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6853 const struct got_error *err;
6854 struct got_blob_object *blob;
6856 err = got_object_open_as_blob(&blob, repo, id, 8192);
6860 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
6861 got_object_blob_close(blob);
6865 static const struct got_error *
6866 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6868 const struct got_error *err;
6869 struct got_tree_object *tree;
6870 const struct got_tree_entries *entries;
6871 struct got_tree_entry *te;
6873 err = got_object_open_as_tree(&tree, repo, id);
6877 entries = got_object_tree_get_entries(tree);
6878 te = SIMPLEQ_FIRST(&entries->head);
6881 if (sigint_received || sigpipe_received)
6883 err = got_object_id_str(&id_str, te->id);
6886 fprintf(outfile, "%s %.7o %s\n", id_str, te->mode, te->name);
6888 te = SIMPLEQ_NEXT(te, entry);
6891 got_object_tree_close(tree);
6895 static const struct got_error *
6896 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6898 const struct got_error *err;
6899 struct got_commit_object *commit;
6900 const struct got_object_id_queue *parent_ids;
6901 struct got_object_qid *pid;
6902 char *id_str = NULL;
6903 const char *logmsg = NULL;
6905 err = got_object_open_as_commit(&commit, repo, id);
6909 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
6913 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
6914 parent_ids = got_object_commit_get_parent_ids(commit);
6915 fprintf(outfile, "numparents %d\n",
6916 got_object_commit_get_nparents(commit));
6917 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
6919 err = got_object_id_str(&pid_str, pid->id);
6922 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
6925 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
6926 got_object_commit_get_author(commit),
6927 got_object_commit_get_author_time(commit));
6929 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
6930 got_object_commit_get_author(commit),
6931 got_object_commit_get_committer_time(commit));
6933 logmsg = got_object_commit_get_logmsg_raw(commit);
6934 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
6935 fprintf(outfile, "%s", logmsg);
6938 got_object_commit_close(commit);
6942 static const struct got_error *
6943 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6945 const struct got_error *err;
6946 struct got_tag_object *tag;
6947 char *id_str = NULL;
6948 const char *tagmsg = NULL;
6950 err = got_object_open_as_tag(&tag, repo, id);
6954 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
6958 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
6960 switch (got_object_tag_get_object_type(tag)) {
6961 case GOT_OBJ_TYPE_BLOB:
6962 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
6963 GOT_OBJ_LABEL_BLOB);
6965 case GOT_OBJ_TYPE_TREE:
6966 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
6967 GOT_OBJ_LABEL_TREE);
6969 case GOT_OBJ_TYPE_COMMIT:
6970 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
6971 GOT_OBJ_LABEL_COMMIT);
6973 case GOT_OBJ_TYPE_TAG:
6974 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
6981 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
6982 got_object_tag_get_name(tag));
6984 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
6985 got_object_tag_get_tagger(tag),
6986 got_object_tag_get_tagger_time(tag));
6988 tagmsg = got_object_tag_get_message(tag);
6989 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
6990 fprintf(outfile, "%s", tagmsg);
6993 got_object_tag_close(tag);
6997 static const struct got_error *
6998 cmd_cat(int argc, char *argv[])
7000 const struct got_error *error;
7001 struct got_repository *repo = NULL;
7002 struct got_worktree *worktree = NULL;
7003 char *cwd = NULL, *repo_path = NULL, *label = NULL;
7004 const char *commit_id_str = NULL;
7005 struct got_object_id *id = NULL, *commit_id = NULL;
7006 int ch, obj_type, i, force_path = 0;
7009 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7014 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
7017 commit_id_str = optarg;
7020 repo_path = realpath(optarg, NULL);
7021 if (repo_path == NULL)
7022 err(1, "-r option");
7023 got_path_strip_trailing_slashes(repo_path);
7037 cwd = getcwd(NULL, 0);
7039 error = got_error_from_errno("getcwd");
7042 error = got_worktree_open(&worktree, cwd);
7043 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7046 if (repo_path == NULL) {
7048 got_worktree_get_repo_path(worktree));
7049 if (repo_path == NULL) {
7050 error = got_error_from_errno("strdup");
7056 if (repo_path == NULL) {
7057 repo_path = getcwd(NULL, 0);
7058 if (repo_path == NULL)
7059 return got_error_from_errno("getcwd");
7062 error = got_repo_open(&repo, repo_path, NULL);
7067 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7071 if (commit_id_str == NULL)
7072 commit_id_str = GOT_REF_HEAD;
7073 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
7077 for (i = 0; i < argc; i++) {
7079 error = got_object_id_by_path(&id, repo, commit_id,
7084 error = match_object_id(&id, &label, argv[i],
7085 GOT_OBJ_TYPE_ANY, 0, repo);
7087 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
7088 error->code != GOT_ERR_NOT_REF)
7090 error = got_object_id_by_path(&id, repo,
7091 commit_id, argv[i]);
7097 error = got_object_get_type(&obj_type, repo, id);
7102 case GOT_OBJ_TYPE_BLOB:
7103 error = cat_blob(id, repo, stdout);
7105 case GOT_OBJ_TYPE_TREE:
7106 error = cat_tree(id, repo, stdout);
7108 case GOT_OBJ_TYPE_COMMIT:
7109 error = cat_commit(id, repo, stdout);
7111 case GOT_OBJ_TYPE_TAG:
7112 error = cat_tag(id, repo, stdout);
7115 error = got_error(GOT_ERR_OBJ_TYPE);
7131 got_worktree_close(worktree);
7133 const struct got_error *repo_error;
7134 repo_error = got_repo_close(repo);