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>
39 #include "got_version.h"
40 #include "got_error.h"
41 #include "got_object.h"
42 #include "got_reference.h"
43 #include "got_repository.h"
45 #include "got_cancel.h"
46 #include "got_worktree.h"
48 #include "got_commit_graph.h"
49 #include "got_blame.h"
50 #include "got_privsep.h"
51 #include "got_opentemp.h"
54 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
57 static volatile sig_atomic_t sigint_received;
58 static volatile sig_atomic_t sigpipe_received;
61 catch_sigint(int signo)
67 catch_sigpipe(int signo)
75 const struct got_error *(*cmd_main)(int, char *[]);
76 void (*cmd_usage)(void);
77 const char *cmd_alias;
80 __dead static void usage(int);
81 __dead static void usage_init(void);
82 __dead static void usage_import(void);
83 __dead static void usage_checkout(void);
84 __dead static void usage_update(void);
85 __dead static void usage_log(void);
86 __dead static void usage_diff(void);
87 __dead static void usage_blame(void);
88 __dead static void usage_tree(void);
89 __dead static void usage_status(void);
90 __dead static void usage_ref(void);
91 __dead static void usage_branch(void);
92 __dead static void usage_tag(void);
93 __dead static void usage_add(void);
94 __dead static void usage_remove(void);
95 __dead static void usage_revert(void);
96 __dead static void usage_commit(void);
97 __dead static void usage_cherrypick(void);
98 __dead static void usage_backout(void);
99 __dead static void usage_rebase(void);
100 __dead static void usage_histedit(void);
101 __dead static void usage_integrate(void);
102 __dead static void usage_stage(void);
103 __dead static void usage_unstage(void);
104 __dead static void usage_cat(void);
106 static const struct got_error* cmd_init(int, char *[]);
107 static const struct got_error* cmd_import(int, char *[]);
108 static const struct got_error* cmd_checkout(int, char *[]);
109 static const struct got_error* cmd_update(int, char *[]);
110 static const struct got_error* cmd_log(int, char *[]);
111 static const struct got_error* cmd_diff(int, char *[]);
112 static const struct got_error* cmd_blame(int, char *[]);
113 static const struct got_error* cmd_tree(int, char *[]);
114 static const struct got_error* cmd_status(int, char *[]);
115 static const struct got_error* cmd_ref(int, char *[]);
116 static const struct got_error* cmd_branch(int, char *[]);
117 static const struct got_error* cmd_tag(int, char *[]);
118 static const struct got_error* cmd_add(int, char *[]);
119 static const struct got_error* cmd_remove(int, char *[]);
120 static const struct got_error* cmd_revert(int, char *[]);
121 static const struct got_error* cmd_commit(int, char *[]);
122 static const struct got_error* cmd_cherrypick(int, char *[]);
123 static const struct got_error* cmd_backout(int, char *[]);
124 static const struct got_error* cmd_rebase(int, char *[]);
125 static const struct got_error* cmd_histedit(int, char *[]);
126 static const struct got_error* cmd_integrate(int, char *[]);
127 static const struct got_error* cmd_stage(int, char *[]);
128 static const struct got_error* cmd_unstage(int, char *[]);
129 static const struct got_error* cmd_cat(int, char *[]);
131 static struct got_cmd got_commands[] = {
132 { "init", cmd_init, usage_init, "in" },
133 { "import", cmd_import, usage_import, "im" },
134 { "checkout", cmd_checkout, usage_checkout, "co" },
135 { "update", cmd_update, usage_update, "up" },
136 { "log", cmd_log, usage_log, "" },
137 { "diff", cmd_diff, usage_diff, "di" },
138 { "blame", cmd_blame, usage_blame, "bl" },
139 { "tree", cmd_tree, usage_tree, "tr" },
140 { "status", cmd_status, usage_status, "st" },
141 { "ref", cmd_ref, usage_ref, "" },
142 { "branch", cmd_branch, usage_branch, "br" },
143 { "tag", cmd_tag, usage_tag, "" },
144 { "add", cmd_add, usage_add, "" },
145 { "remove", cmd_remove, usage_remove, "rm" },
146 { "revert", cmd_revert, usage_revert, "rv" },
147 { "commit", cmd_commit, usage_commit, "ci" },
148 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
149 { "backout", cmd_backout, usage_backout, "bo" },
150 { "rebase", cmd_rebase, usage_rebase, "rb" },
151 { "histedit", cmd_histedit, usage_histedit, "he" },
152 { "integrate", cmd_integrate, usage_integrate,"ig" },
153 { "stage", cmd_stage, usage_stage, "sg" },
154 { "unstage", cmd_unstage, usage_unstage, "ug" },
155 { "cat", cmd_cat, usage_cat, "" },
163 fprintf(stderr, "commands:");
164 for (i = 0; i < nitems(got_commands); i++) {
165 struct got_cmd *cmd = &got_commands[i];
166 fprintf(stderr, " %s", cmd->cmd_name);
172 main(int argc, char *argv[])
177 int hflag = 0, Vflag = 0;
179 setlocale(LC_CTYPE, "");
181 while ((ch = getopt(argc, argv, "hV")) != -1) {
200 got_version_print_str();
207 signal(SIGINT, catch_sigint);
208 signal(SIGPIPE, catch_sigpipe);
210 for (i = 0; i < nitems(got_commands); i++) {
211 const struct got_error *error;
213 cmd = &got_commands[i];
215 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
216 strcmp(cmd->cmd_alias, argv[0]) != 0)
220 got_commands[i].cmd_usage();
222 error = got_commands[i].cmd_main(argc, argv);
223 if (error && error->code != GOT_ERR_CANCELLED &&
224 error->code != GOT_ERR_PRIVSEP_EXIT &&
225 !(sigpipe_received &&
226 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
228 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
229 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
236 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
244 fprintf(stderr, "usage: %s [-h] [-V] command [arg ...]\n",
251 static const struct got_error *
252 get_editor(char **abspath)
254 const struct got_error *err = NULL;
259 editor = getenv("VISUAL");
261 editor = getenv("EDITOR");
264 err = got_path_find_prog(abspath, editor);
269 if (*abspath == NULL) {
270 *abspath = strdup("/bin/ed");
271 if (*abspath == NULL)
272 return got_error_from_errno("strdup");
278 static const struct got_error *
279 apply_unveil(const char *repo_path, int repo_read_only,
280 const char *worktree_path)
282 const struct got_error *err;
285 if (unveil("gmon.out", "rwc") != 0)
286 return got_error_from_errno2("unveil", "gmon.out");
288 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
289 return got_error_from_errno2("unveil", repo_path);
291 if (worktree_path && unveil(worktree_path, "rwc") != 0)
292 return got_error_from_errno2("unveil", worktree_path);
294 if (unveil("/tmp", "rwc") != 0)
295 return got_error_from_errno2("unveil", "/tmp");
297 err = got_privsep_unveil_exec_helpers();
301 if (unveil(NULL, NULL) != 0)
302 return got_error_from_errno("unveil");
310 fprintf(stderr, "usage: %s init repository-path\n", getprogname());
314 static const struct got_error *
315 cmd_init(int argc, char *argv[])
317 const struct got_error *error = NULL;
318 char *repo_path = NULL;
321 while ((ch = getopt(argc, argv, "")) != -1) {
333 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
339 repo_path = strdup(argv[0]);
340 if (repo_path == NULL)
341 return got_error_from_errno("strdup");
343 got_path_strip_trailing_slashes(repo_path);
345 error = got_path_mkdir(repo_path);
347 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
350 error = apply_unveil(repo_path, 0, NULL);
354 error = got_repo_init(repo_path);
366 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
367 "[-r repository-path] [-I pattern] path\n", getprogname());
372 spawn_editor(const char *editor, const char *file)
375 sig_t sighup, sigint, sigquit;
378 sighup = signal(SIGHUP, SIG_IGN);
379 sigint = signal(SIGINT, SIG_IGN);
380 sigquit = signal(SIGQUIT, SIG_IGN);
382 switch (pid = fork()) {
386 execl(editor, editor, file, (char *)NULL);
390 while (waitpid(pid, &st, 0) == -1)
395 (void)signal(SIGHUP, sighup);
396 (void)signal(SIGINT, sigint);
397 (void)signal(SIGQUIT, sigquit);
399 if (!WIFEXITED(st)) {
404 return WEXITSTATUS(st);
407 static const struct got_error *
408 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
409 const char *initial_content)
411 const struct got_error *err = NULL;
415 int content_changed = 0;
420 if (stat(logmsg_path, &st) == -1)
421 return got_error_from_errno2("stat", logmsg_path);
423 if (spawn_editor(editor, logmsg_path) == -1)
424 return got_error_from_errno("failed spawning editor");
426 if (stat(logmsg_path, &st2) == -1)
427 return got_error_from_errno("stat");
429 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
430 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
431 "no changes made to commit message, aborting");
433 *logmsg = malloc(st2.st_size + 1);
435 return got_error_from_errno("malloc");
439 fp = fopen(logmsg_path, "r");
441 err = got_error_from_errno("fopen");
444 while (fgets(buf, sizeof(buf), fp) != NULL) {
445 if (!content_changed && strcmp(buf, initial_content) != 0)
447 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
448 continue; /* remove comments and leading empty lines */
449 len = strlcat(*logmsg, buf, st2.st_size);
453 while (len > 0 && (*logmsg)[len - 1] == '\n') {
454 (*logmsg)[len - 1] = '\0';
458 if (len == 0 || !content_changed)
459 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
460 "commit message cannot be empty, aborting");
469 static const struct got_error *
470 collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
471 const char *path_dir, const char *branch_name)
473 char *initial_content = NULL;
474 const struct got_error *err = NULL;
477 if (asprintf(&initial_content,
478 "\n# %s to be imported to branch %s\n", path_dir,
480 return got_error_from_errno("asprintf");
482 err = got_opentemp_named_fd(logmsg_path, &fd, "/tmp/got-importmsg");
486 dprintf(fd, initial_content);
489 err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content);
491 free(initial_content);
495 static const struct got_error *
496 import_progress(void *arg, const char *path)
498 printf("A %s\n", path);
502 static const struct got_error *
503 get_author(char **author, struct got_repository *repo)
505 const struct got_error *err = NULL;
506 const char *got_author, *name, *email;
510 name = got_repo_get_gitconfig_author_name(repo);
511 email = got_repo_get_gitconfig_author_email(repo);
513 if (asprintf(author, "%s <%s>", name, email) == -1)
514 return got_error_from_errno("asprintf");
518 got_author = getenv("GOT_AUTHOR");
519 if (got_author == NULL) {
520 name = got_repo_get_global_gitconfig_author_name(repo);
521 email = got_repo_get_global_gitconfig_author_email(repo);
523 if (asprintf(author, "%s <%s>", name, email) == -1)
524 return got_error_from_errno("asprintf");
527 /* TODO: Look up user in password database? */
528 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
531 *author = strdup(got_author);
533 return got_error_from_errno("strdup");
536 * Really dumb email address check; we're only doing this to
537 * avoid git's object parser breaking on commits we create.
539 while (*got_author && *got_author != '<')
541 if (*got_author != '<') {
542 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
545 while (*got_author && *got_author != '@')
547 if (*got_author != '@') {
548 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
551 while (*got_author && *got_author != '>')
553 if (*got_author != '>')
554 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
563 static const struct got_error *
564 get_gitconfig_path(char **gitconfig_path)
566 const char *homedir = getenv("HOME");
568 *gitconfig_path = NULL;
570 if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
571 return got_error_from_errno("asprintf");
577 static const struct got_error *
578 cmd_import(int argc, char *argv[])
580 const struct got_error *error = NULL;
581 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
582 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
583 const char *branch_name = "main";
584 char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
585 struct got_repository *repo = NULL;
586 struct got_reference *branch_ref = NULL, *head_ref = NULL;
587 struct got_object_id *new_commit_id = NULL;
589 struct got_pathlist_head ignores;
590 struct got_pathlist_entry *pe;
591 int preserve_logmsg = 0;
593 TAILQ_INIT(&ignores);
595 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
598 branch_name = optarg;
601 logmsg = strdup(optarg);
602 if (logmsg == NULL) {
603 error = got_error_from_errno("strdup");
608 repo_path = realpath(optarg, NULL);
609 if (repo_path == NULL) {
610 error = got_error_from_errno2("realpath",
616 if (optarg[0] == '\0')
618 error = got_pathlist_insert(&pe, &ignores, optarg,
633 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
641 if (repo_path == NULL) {
642 repo_path = getcwd(NULL, 0);
643 if (repo_path == NULL)
644 return got_error_from_errno("getcwd");
646 got_path_strip_trailing_slashes(repo_path);
647 error = get_gitconfig_path(&gitconfig_path);
650 error = got_repo_open(&repo, repo_path, gitconfig_path);
654 error = get_author(&author, repo);
659 * Don't let the user create a branch name with a leading '-'.
660 * While technically a valid reference name, this case is usually
661 * an unintended typo.
663 if (branch_name[0] == '-')
664 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
666 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
667 error = got_error_from_errno("asprintf");
671 error = got_ref_open(&branch_ref, repo, refname, 0);
673 if (error->code != GOT_ERR_NOT_REF)
676 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
677 "import target branch already exists");
681 path_dir = realpath(argv[0], NULL);
682 if (path_dir == NULL) {
683 error = got_error_from_errno2("realpath", argv[0]);
686 got_path_strip_trailing_slashes(path_dir);
689 * unveil(2) traverses exec(2); if an editor is used we have
690 * to apply unveil after the log message has been written.
692 if (logmsg == NULL || strlen(logmsg) == 0) {
693 error = get_editor(&editor);
697 error = collect_import_msg(&logmsg, &logmsg_path, editor,
700 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
707 if (unveil(path_dir, "r") != 0) {
708 error = got_error_from_errno2("unveil", path_dir);
714 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
721 error = got_repo_import(&new_commit_id, path_dir, logmsg,
722 author, &ignores, repo, import_progress, NULL);
729 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
736 error = got_ref_write(branch_ref, repo);
743 error = got_object_id_str(&id_str, new_commit_id);
750 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
752 if (error->code != GOT_ERR_NOT_REF) {
758 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
766 error = got_ref_write(head_ref, repo);
774 printf("Created branch %s with commit %s\n",
775 got_ref_get_name(branch_ref), id_str);
777 if (preserve_logmsg) {
778 fprintf(stderr, "%s: log message preserved in %s\n",
779 getprogname(), logmsg_path);
780 } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
781 error = got_error_from_errno2("unlink", logmsg_path);
790 free(gitconfig_path);
792 got_ref_close(branch_ref);
794 got_ref_close(head_ref);
801 fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
802 "[-p prefix] repository-path [worktree-path]\n", getprogname());
806 static const struct got_error *
807 checkout_progress(void *arg, unsigned char status, const char *path)
809 char *worktree_path = arg;
811 /* Base commit bump happens silently. */
812 if (status == GOT_STATUS_BUMP_BASE)
815 while (path[0] == '/')
818 printf("%c %s/%s\n", status, worktree_path, path);
822 static const struct got_error *
823 check_cancelled(void *arg)
825 if (sigint_received || sigpipe_received)
826 return got_error(GOT_ERR_CANCELLED);
830 static const struct got_error *
831 check_linear_ancestry(struct got_object_id *commit_id,
832 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
833 struct got_repository *repo)
835 const struct got_error *err = NULL;
836 struct got_object_id *yca_id;
838 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
839 commit_id, base_commit_id, repo, check_cancelled, NULL);
844 return got_error(GOT_ERR_ANCESTRY);
847 * Require a straight line of history between the target commit
848 * and the work tree's base commit.
850 * Non-linear situations such as this require a rebase:
852 * (commit) D F (base_commit)
860 * 'got update' only handles linear cases:
861 * Update forwards in time: A (base/yca) - B - C - D (commit)
862 * Update backwards in time: D (base) - C - B - A (commit/yca)
864 if (allow_forwards_in_time_only) {
865 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
866 return got_error(GOT_ERR_ANCESTRY);
867 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
868 got_object_id_cmp(base_commit_id, yca_id) != 0)
869 return got_error(GOT_ERR_ANCESTRY);
875 static const struct got_error *
876 check_same_branch(struct got_object_id *commit_id,
877 struct got_reference *head_ref, struct got_object_id *yca_id,
878 struct got_repository *repo)
880 const struct got_error *err = NULL;
881 struct got_commit_graph *graph = NULL;
882 struct got_object_id *head_commit_id = NULL;
883 int is_same_branch = 0;
885 err = got_ref_resolve(&head_commit_id, repo, head_ref);
889 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
893 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
898 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
902 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
903 check_cancelled, NULL);
908 struct got_object_id *id;
909 err = got_commit_graph_iter_next(&id, graph);
911 if (err->code == GOT_ERR_ITER_COMPLETED) {
914 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
916 err = got_commit_graph_fetch_commits(graph, 1,
917 repo, check_cancelled, NULL);
923 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
925 if (got_object_id_cmp(id, commit_id) == 0) {
933 got_commit_graph_close(graph);
934 free(head_commit_id);
935 if (!err && !is_same_branch)
936 err = got_error(GOT_ERR_ANCESTRY);
940 static const struct got_error *
941 resolve_commit_arg(struct got_object_id **commit_id,
942 const char *commit_id_arg, struct got_repository *repo)
944 const struct got_error *err;
945 struct got_reference *ref;
946 struct got_tag_object *tag;
948 err = got_repo_object_match_tag(&tag, commit_id_arg,
949 GOT_OBJ_TYPE_COMMIT, repo);
951 *commit_id = got_object_id_dup(
952 got_object_tag_get_object_id(tag));
953 if (*commit_id == NULL)
954 err = got_error_from_errno("got_object_id_dup");
955 got_object_tag_close(tag);
957 } else if (err->code != GOT_ERR_NO_OBJ)
960 err = got_ref_open(&ref, repo, commit_id_arg, 0);
962 err = got_ref_resolve(commit_id, repo, ref);
965 if (err->code != GOT_ERR_NOT_REF)
967 err = got_repo_match_object_id_prefix(commit_id,
968 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
973 static const struct got_error *
974 cmd_checkout(int argc, char *argv[])
976 const struct got_error *error = NULL;
977 struct got_repository *repo = NULL;
978 struct got_reference *head_ref = NULL;
979 struct got_worktree *worktree = NULL;
980 char *repo_path = NULL;
981 char *worktree_path = NULL;
982 const char *path_prefix = "";
983 const char *branch_name = GOT_REF_HEAD;
984 char *commit_id_str = NULL;
985 int ch, same_path_prefix;
986 struct got_pathlist_head paths;
990 while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
993 branch_name = optarg;
996 commit_id_str = strdup(optarg);
997 if (commit_id_str == NULL)
998 return got_error_from_errno("strdup");
1001 path_prefix = optarg;
1013 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1014 "unveil", NULL) == -1)
1018 char *cwd, *base, *dotgit;
1019 repo_path = realpath(argv[0], NULL);
1020 if (repo_path == NULL)
1021 return got_error_from_errno2("realpath", argv[0]);
1022 cwd = getcwd(NULL, 0);
1024 error = got_error_from_errno("getcwd");
1027 if (path_prefix[0]) {
1028 base = basename(path_prefix);
1030 error = got_error_from_errno2("basename",
1035 base = basename(repo_path);
1037 error = got_error_from_errno2("basename",
1042 dotgit = strstr(base, ".git");
1045 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1046 error = got_error_from_errno("asprintf");
1051 } else if (argc == 2) {
1052 repo_path = realpath(argv[0], NULL);
1053 if (repo_path == NULL) {
1054 error = got_error_from_errno2("realpath", argv[0]);
1057 worktree_path = realpath(argv[1], NULL);
1058 if (worktree_path == NULL) {
1059 if (errno != ENOENT) {
1060 error = got_error_from_errno2("realpath",
1064 worktree_path = strdup(argv[1]);
1065 if (worktree_path == NULL) {
1066 error = got_error_from_errno("strdup");
1073 got_path_strip_trailing_slashes(repo_path);
1074 got_path_strip_trailing_slashes(worktree_path);
1076 error = got_repo_open(&repo, repo_path, NULL);
1080 /* Pre-create work tree path for unveil(2) */
1081 error = got_path_mkdir(worktree_path);
1083 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1084 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1086 if (!got_path_dir_is_empty(worktree_path)) {
1087 error = got_error_path(worktree_path,
1088 GOT_ERR_DIR_NOT_EMPTY);
1093 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1097 error = got_ref_open(&head_ref, repo, branch_name, 0);
1101 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1102 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1105 error = got_worktree_open(&worktree, worktree_path);
1109 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1113 if (!same_path_prefix) {
1114 error = got_error(GOT_ERR_PATH_PREFIX);
1118 if (commit_id_str) {
1119 struct got_object_id *commit_id;
1120 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1123 error = check_linear_ancestry(commit_id,
1124 got_worktree_get_base_commit_id(worktree), 0, repo);
1125 if (error != NULL) {
1129 error = check_same_branch(commit_id, head_ref, NULL, repo);
1132 error = got_worktree_set_base_commit_id(worktree, repo,
1139 error = got_pathlist_append(&paths, "", NULL);
1142 error = got_worktree_checkout_files(worktree, &paths, repo,
1143 checkout_progress, worktree_path, check_cancelled, NULL);
1147 printf("Now shut up and hack\n");
1150 got_pathlist_free(&paths);
1151 free(commit_id_str);
1153 free(worktree_path);
1160 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1165 static const struct got_error *
1166 update_progress(void *arg, unsigned char status, const char *path)
1168 int *did_something = arg;
1170 if (status == GOT_STATUS_EXISTS)
1175 /* Base commit bump happens silently. */
1176 if (status == GOT_STATUS_BUMP_BASE)
1179 while (path[0] == '/')
1181 printf("%c %s\n", status, path);
1185 static const struct got_error *
1186 switch_head_ref(struct got_reference *head_ref,
1187 struct got_object_id *commit_id, struct got_worktree *worktree,
1188 struct got_repository *repo)
1190 const struct got_error *err = NULL;
1192 int ref_has_moved = 0;
1194 /* Trivial case: switching between two different references. */
1195 if (strcmp(got_ref_get_name(head_ref),
1196 got_worktree_get_head_ref_name(worktree)) != 0) {
1197 printf("Switching work tree from %s to %s\n",
1198 got_worktree_get_head_ref_name(worktree),
1199 got_ref_get_name(head_ref));
1200 return got_worktree_set_head_ref(worktree, head_ref);
1203 err = check_linear_ancestry(commit_id,
1204 got_worktree_get_base_commit_id(worktree), 0, repo);
1206 if (err->code != GOT_ERR_ANCESTRY)
1213 /* Switching to a rebased branch with the same reference name. */
1214 err = got_object_id_str(&base_id_str,
1215 got_worktree_get_base_commit_id(worktree));
1218 printf("Reference %s now points at a different branch\n",
1219 got_worktree_get_head_ref_name(worktree));
1220 printf("Switching work tree from %s to %s\n", base_id_str,
1221 got_worktree_get_head_ref_name(worktree));
1225 static const struct got_error *
1226 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1228 const struct got_error *err;
1231 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1235 return got_error(GOT_ERR_REBASING);
1237 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1241 return got_error(GOT_ERR_HISTEDIT_BUSY);
1246 static const struct got_error *
1247 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1248 char *argv[], struct got_worktree *worktree)
1250 const struct got_error *err = NULL;
1257 return got_error_from_errno("strdup");
1258 return got_pathlist_append(paths, path, NULL);
1261 for (i = 0; i < argc; i++) {
1262 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1265 err = got_pathlist_append(paths, path, NULL);
1275 static const struct got_error *
1276 cmd_update(int argc, char *argv[])
1278 const struct got_error *error = NULL;
1279 struct got_repository *repo = NULL;
1280 struct got_worktree *worktree = NULL;
1281 char *worktree_path = NULL;
1282 struct got_object_id *commit_id = NULL;
1283 char *commit_id_str = NULL;
1284 const char *branch_name = NULL;
1285 struct got_reference *head_ref = NULL;
1286 struct got_pathlist_head paths;
1287 struct got_pathlist_entry *pe;
1288 int ch, did_something = 0;
1292 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1295 branch_name = optarg;
1298 commit_id_str = strdup(optarg);
1299 if (commit_id_str == NULL)
1300 return got_error_from_errno("strdup");
1312 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1313 "unveil", NULL) == -1)
1316 worktree_path = getcwd(NULL, 0);
1317 if (worktree_path == NULL) {
1318 error = got_error_from_errno("getcwd");
1321 error = got_worktree_open(&worktree, worktree_path);
1325 error = check_rebase_or_histedit_in_progress(worktree);
1329 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
1334 error = apply_unveil(got_repo_get_path(repo), 0,
1335 got_worktree_get_root_path(worktree));
1339 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1343 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1344 got_worktree_get_head_ref_name(worktree), 0);
1347 if (commit_id_str == NULL) {
1348 error = got_ref_resolve(&commit_id, repo, head_ref);
1351 error = got_object_id_str(&commit_id_str, commit_id);
1355 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1356 free(commit_id_str);
1357 commit_id_str = NULL;
1360 error = got_object_id_str(&commit_id_str, commit_id);
1366 struct got_object_id *head_commit_id;
1367 TAILQ_FOREACH(pe, &paths, entry) {
1368 if (pe->path_len == 0)
1370 error = got_error_msg(GOT_ERR_BAD_PATH,
1371 "switching between branches requires that "
1372 "the entire work tree gets updated");
1375 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1378 error = check_linear_ancestry(commit_id, head_commit_id, 0,
1380 free(head_commit_id);
1383 error = check_same_branch(commit_id, head_ref, NULL, repo);
1386 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1390 error = check_linear_ancestry(commit_id,
1391 got_worktree_get_base_commit_id(worktree), 0, repo);
1392 if (error != NULL) {
1393 if (error->code == GOT_ERR_ANCESTRY)
1394 error = got_error(GOT_ERR_BRANCH_MOVED);
1397 error = check_same_branch(commit_id, head_ref, NULL, repo);
1402 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1404 error = got_worktree_set_base_commit_id(worktree, repo,
1410 error = got_worktree_checkout_files(worktree, &paths, repo,
1411 update_progress, &did_something, check_cancelled, NULL);
1416 printf("Updated to commit %s\n", commit_id_str);
1418 printf("Already up-to-date\n");
1420 free(worktree_path);
1421 TAILQ_FOREACH(pe, &paths, entry)
1422 free((char *)pe->path);
1423 got_pathlist_free(&paths);
1425 free(commit_id_str);
1429 static const struct got_error *
1430 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
1431 const char *path, int diff_context, int ignore_whitespace,
1432 struct got_repository *repo)
1434 const struct got_error *err = NULL;
1435 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1438 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
1443 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
1447 while (path[0] == '/')
1449 err = got_diff_blob(blob1, blob2, path, path, diff_context,
1450 ignore_whitespace, stdout);
1453 got_object_blob_close(blob1);
1454 got_object_blob_close(blob2);
1458 static const struct got_error *
1459 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
1460 const char *path, int diff_context, int ignore_whitespace,
1461 struct got_repository *repo)
1463 const struct got_error *err = NULL;
1464 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1465 struct got_diff_blob_output_unidiff_arg arg;
1468 err = got_object_open_as_tree(&tree1, repo, tree_id1);
1473 err = got_object_open_as_tree(&tree2, repo, tree_id2);
1477 arg.diff_context = diff_context;
1478 arg.ignore_whitespace = ignore_whitespace;
1479 arg.outfile = stdout;
1480 while (path[0] == '/')
1482 err = got_diff_tree(tree1, tree2, path, path, repo,
1483 got_diff_blob_output_unidiff, &arg, 1);
1486 got_object_tree_close(tree1);
1488 got_object_tree_close(tree2);
1492 static const struct got_error *
1493 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1494 const char *path, int diff_context, struct got_repository *repo)
1496 const struct got_error *err = NULL;
1497 struct got_commit_object *pcommit = NULL;
1498 char *id_str1 = NULL, *id_str2 = NULL;
1499 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
1500 struct got_object_qid *qid;
1502 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1504 err = got_object_open_as_commit(&pcommit, repo,
1510 if (path && path[0] != '\0') {
1512 err = got_object_id_by_path(&obj_id2, repo, id, path);
1515 err = got_object_id_str(&id_str2, obj_id2);
1521 err = got_object_id_by_path(&obj_id1, repo,
1527 err = got_object_id_str(&id_str1, obj_id1);
1533 err = got_object_get_type(&obj_type, repo, obj_id2);
1538 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1540 case GOT_OBJ_TYPE_BLOB:
1541 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
1544 case GOT_OBJ_TYPE_TREE:
1545 err = diff_trees(obj_id1, obj_id2, path, diff_context,
1549 err = got_error(GOT_ERR_OBJ_TYPE);
1555 obj_id2 = got_object_commit_get_tree_id(commit);
1556 err = got_object_id_str(&id_str2, obj_id2);
1559 obj_id1 = got_object_commit_get_tree_id(pcommit);
1560 err = got_object_id_str(&id_str1, obj_id1);
1563 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1564 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
1571 got_object_commit_close(pcommit);
1576 get_datestr(time_t *time, char *datebuf)
1578 struct tm mytm, *tm;
1581 tm = gmtime_r(time, &mytm);
1584 s = asctime_r(tm, datebuf);
1587 p = strchr(s, '\n');
1593 static const struct got_error *
1594 match_logmsg(int *have_match, struct got_object_id *id,
1595 struct got_commit_object *commit, regex_t *regex)
1597 const struct got_error *err = NULL;
1598 regmatch_t regmatch;
1599 char *id_str = NULL, *logmsg = NULL;
1603 err = got_object_id_str(&id_str, id);
1607 err = got_object_commit_get_logmsg(&logmsg, commit);
1611 if (regexec(regex, logmsg, 1, ®match, 0) == 0)
1619 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1621 static const struct got_error *
1622 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1623 struct got_repository *repo, const char *path, int show_patch,
1624 int diff_context, struct got_reflist_head *refs)
1626 const struct got_error *err = NULL;
1627 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1629 time_t committer_time;
1630 const char *author, *committer;
1631 char *refs_str = NULL;
1632 struct got_reflist_entry *re;
1634 SIMPLEQ_FOREACH(re, refs, entry) {
1637 struct got_tag_object *tag = NULL;
1640 name = got_ref_get_name(re->ref);
1641 if (strcmp(name, GOT_REF_HEAD) == 0)
1643 if (strncmp(name, "refs/", 5) == 0)
1645 if (strncmp(name, "got/", 4) == 0)
1647 if (strncmp(name, "heads/", 6) == 0)
1649 if (strncmp(name, "remotes/", 8) == 0)
1651 if (strncmp(name, "tags/", 5) == 0) {
1652 err = got_object_open_as_tag(&tag, repo, re->id);
1654 if (err->code != GOT_ERR_OBJ_TYPE)
1656 /* Ref points at something other than a tag. */
1661 cmp = got_object_id_cmp(tag ?
1662 got_object_tag_get_object_id(tag) : re->id, id);
1664 got_object_tag_close(tag);
1668 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1670 err = got_error_from_errno("asprintf");
1676 err = got_object_id_str(&id_str, id);
1680 printf(GOT_COMMIT_SEP_STR);
1681 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1682 refs_str ? refs_str : "", refs_str ? ")" : "");
1687 printf("from: %s\n", got_object_commit_get_author(commit));
1688 committer_time = got_object_commit_get_committer_time(commit);
1689 datestr = get_datestr(&committer_time, datebuf);
1691 printf("date: %s UTC\n", datestr);
1692 author = got_object_commit_get_author(commit);
1693 committer = got_object_commit_get_committer(commit);
1694 if (strcmp(author, committer) != 0)
1695 printf("via: %s\n", committer);
1696 if (got_object_commit_get_nparents(commit) > 1) {
1697 const struct got_object_id_queue *parent_ids;
1698 struct got_object_qid *qid;
1700 parent_ids = got_object_commit_get_parent_ids(commit);
1701 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1702 err = got_object_id_str(&id_str, qid->id);
1705 printf("parent %d: %s\n", n++, id_str);
1710 err = got_object_commit_get_logmsg(&logmsg0, commit);
1716 line = strsep(&logmsg, "\n");
1718 printf(" %s\n", line);
1723 err = print_patch(commit, id, path, diff_context, repo);
1728 if (fflush(stdout) != 0 && err == NULL)
1729 err = got_error_from_errno("fflush");
1733 static const struct got_error *
1734 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1735 char *path, int show_patch, char *search_pattern, int diff_context,
1736 int limit, int first_parent_traversal, struct got_reflist_head *refs)
1738 const struct got_error *err;
1739 struct got_commit_graph *graph;
1743 if (search_pattern &&
1744 regcomp(®ex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
1745 return got_error_msg(GOT_ERR_REGEX, search_pattern);
1747 err = got_commit_graph_open(&graph, root_id, path,
1748 first_parent_traversal, repo);
1751 err = got_commit_graph_iter_start(graph, root_id, repo,
1752 check_cancelled, NULL);
1756 struct got_commit_object *commit;
1757 struct got_object_id *id;
1759 if (sigint_received || sigpipe_received)
1762 err = got_commit_graph_iter_next(&id, graph);
1764 if (err->code == GOT_ERR_ITER_COMPLETED) {
1768 if (err->code != GOT_ERR_ITER_NEED_MORE)
1770 err = got_commit_graph_fetch_commits(graph, 1, repo,
1771 check_cancelled, NULL);
1780 err = got_object_open_as_commit(&commit, repo, id);
1784 if (search_pattern) {
1785 err = match_logmsg(&have_match, id, commit, ®ex);
1787 got_object_commit_close(commit);
1790 if (have_match == 0) {
1791 got_object_commit_close(commit);
1796 err = print_commit(commit, id, repo, path, show_patch,
1797 diff_context, refs);
1798 got_object_commit_close(commit);
1799 if (err || (limit && --limit == 0))
1805 got_commit_graph_close(graph);
1812 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1813 "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
1818 get_default_log_limit(void)
1820 const char *got_default_log_limit;
1824 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
1825 if (got_default_log_limit == NULL)
1827 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
1833 static const struct got_error *
1834 cmd_log(int argc, char *argv[])
1836 const struct got_error *error;
1837 struct got_repository *repo = NULL;
1838 struct got_worktree *worktree = NULL;
1839 struct got_commit_object *commit = NULL;
1840 struct got_object_id *id = NULL;
1841 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1842 char *start_commit = NULL, *search_pattern = NULL;
1843 int diff_context = 3, ch;
1844 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1846 struct got_reflist_head refs;
1848 SIMPLEQ_INIT(&refs);
1851 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1857 limit = get_default_log_limit();
1859 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:s:")) != -1) {
1865 start_commit = optarg;
1868 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1871 err(1, "-C option %s", errstr);
1874 limit = strtonum(optarg, 0, INT_MAX, &errstr);
1876 err(1, "-l option %s", errstr);
1879 first_parent_traversal = 1;
1882 repo_path = realpath(optarg, NULL);
1883 if (repo_path == NULL)
1884 return got_error_from_errno2("realpath",
1886 got_path_strip_trailing_slashes(repo_path);
1889 search_pattern = optarg;
1900 cwd = getcwd(NULL, 0);
1902 error = got_error_from_errno("getcwd");
1906 error = got_worktree_open(&worktree, cwd);
1907 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1914 error = got_error_from_errno("strdup");
1917 } else if (argc == 1) {
1919 error = got_worktree_resolve_path(&path, worktree,
1924 path = strdup(argv[0]);
1926 error = got_error_from_errno("strdup");
1933 if (repo_path == NULL) {
1934 repo_path = worktree ?
1935 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1937 if (repo_path == NULL) {
1938 error = got_error_from_errno("strdup");
1942 error = got_repo_open(&repo, repo_path, NULL);
1946 error = apply_unveil(got_repo_get_path(repo), 1,
1947 worktree ? got_worktree_get_root_path(worktree) : NULL);
1951 if (start_commit == NULL) {
1952 struct got_reference *head_ref;
1953 error = got_ref_open(&head_ref, repo,
1954 worktree ? got_worktree_get_head_ref_name(worktree)
1958 error = got_ref_resolve(&id, repo, head_ref);
1959 got_ref_close(head_ref);
1962 error = got_object_open_as_commit(&commit, repo, id);
1964 struct got_reference *ref;
1965 error = got_ref_open(&ref, repo, start_commit, 0);
1966 if (error == NULL) {
1968 error = got_ref_resolve(&id, repo, ref);
1972 error = got_object_get_type(&obj_type, repo, id);
1975 if (obj_type == GOT_OBJ_TYPE_TAG) {
1976 struct got_tag_object *tag;
1977 error = got_object_open_as_tag(&tag, repo, id);
1980 if (got_object_tag_get_object_type(tag) !=
1981 GOT_OBJ_TYPE_COMMIT) {
1982 got_object_tag_close(tag);
1983 error = got_error(GOT_ERR_OBJ_TYPE);
1987 id = got_object_id_dup(
1988 got_object_tag_get_object_id(tag));
1990 error = got_error_from_errno(
1991 "got_object_id_dup");
1992 got_object_tag_close(tag);
1995 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1996 error = got_error(GOT_ERR_OBJ_TYPE);
1999 error = got_object_open_as_commit(&commit, repo, id);
2003 if (commit == NULL) {
2004 error = got_repo_match_object_id_prefix(&id,
2005 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2014 const char *prefix = got_worktree_get_path_prefix(worktree);
2016 if (asprintf(&p, "%s%s%s", prefix,
2017 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2018 error = got_error_from_errno("asprintf");
2021 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2024 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2029 path = in_repo_path;
2032 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2036 error = print_commits(id, repo, path, show_patch, search_pattern,
2037 diff_context, limit, first_parent_traversal, &refs);
2044 got_worktree_close(worktree);
2046 const struct got_error *repo_error;
2047 repo_error = got_repo_close(repo);
2051 got_ref_list_free(&refs);
2058 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2059 "[-w] [object1 object2 | path]\n", getprogname());
2063 struct print_diff_arg {
2064 struct got_repository *repo;
2065 struct got_worktree *worktree;
2070 int ignore_whitespace;
2073 static const struct got_error *
2074 print_diff(void *arg, unsigned char status, unsigned char staged_status,
2075 const char *path, struct got_object_id *blob_id,
2076 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
2078 struct print_diff_arg *a = arg;
2079 const struct got_error *err = NULL;
2080 struct got_blob_object *blob1 = NULL;
2082 char *abspath = NULL, *label1 = NULL;
2085 if (a->diff_staged) {
2086 if (staged_status != GOT_STATUS_MODIFY &&
2087 staged_status != GOT_STATUS_ADD &&
2088 staged_status != GOT_STATUS_DELETE)
2091 if (staged_status == GOT_STATUS_DELETE)
2093 if (status == GOT_STATUS_NONEXISTENT)
2094 return got_error_set_errno(ENOENT, path);
2095 if (status != GOT_STATUS_MODIFY &&
2096 status != GOT_STATUS_ADD &&
2097 status != GOT_STATUS_DELETE &&
2098 status != GOT_STATUS_CONFLICT)
2102 if (!a->header_shown) {
2103 printf("diff %s %s%s\n", a->id_str,
2104 got_worktree_get_root_path(a->worktree),
2105 a->diff_staged ? " (staged changes)" : "");
2106 a->header_shown = 1;
2109 if (a->diff_staged) {
2110 const char *label1 = NULL, *label2 = NULL;
2111 switch (staged_status) {
2112 case GOT_STATUS_MODIFY:
2116 case GOT_STATUS_ADD:
2119 case GOT_STATUS_DELETE:
2123 return got_error(GOT_ERR_FILE_STATUS);
2125 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2126 label1, label2, a->diff_context, a->ignore_whitespace,
2130 if (staged_status == GOT_STATUS_ADD ||
2131 staged_status == GOT_STATUS_MODIFY) {
2133 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2137 err = got_object_id_str(&id_str, staged_blob_id);
2140 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2141 err = got_error_from_errno("asprintf");
2146 } else if (status != GOT_STATUS_ADD) {
2147 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2152 if (status != GOT_STATUS_DELETE) {
2153 if (asprintf(&abspath, "%s/%s",
2154 got_worktree_get_root_path(a->worktree), path) == -1) {
2155 err = got_error_from_errno("asprintf");
2159 f2 = fopen(abspath, "r");
2161 err = got_error_from_errno2("fopen", abspath);
2164 if (lstat(abspath, &sb) == -1) {
2165 err = got_error_from_errno2("lstat", abspath);
2171 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2172 a->diff_context, a->ignore_whitespace, stdout);
2175 got_object_blob_close(blob1);
2176 if (f2 && fclose(f2) != 0 && err == NULL)
2177 err = got_error_from_errno("fclose");
2182 static const struct got_error *
2183 match_object_id(struct got_object_id **id, char **label,
2184 const char *id_str, int obj_type, int resolve_tags,
2185 struct got_repository *repo)
2187 const struct got_error *err;
2188 struct got_tag_object *tag;
2189 struct got_reference *ref = NULL;
2195 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
2198 *id = got_object_id_dup(
2199 got_object_tag_get_object_id(tag));
2201 err = got_error_from_errno("got_object_id_dup");
2202 else if (asprintf(label, "refs/tags/%s",
2203 got_object_tag_get_name(tag)) == -1) {
2204 err = got_error_from_errno("asprintf");
2208 got_object_tag_close(tag);
2210 } else if (err->code != GOT_ERR_NO_OBJ)
2214 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
2216 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2218 err = got_ref_open(&ref, repo, id_str, 0);
2221 *label = strdup(got_ref_get_name(ref));
2222 if (*label == NULL) {
2223 err = got_error_from_errno("strdup");
2226 err = got_ref_resolve(id, repo, ref);
2228 err = got_object_id_str(label, *id);
2229 if (*label == NULL) {
2230 err = got_error_from_errno("strdup");
2241 static const struct got_error *
2242 cmd_diff(int argc, char *argv[])
2244 const struct got_error *error;
2245 struct got_repository *repo = NULL;
2246 struct got_worktree *worktree = NULL;
2247 char *cwd = NULL, *repo_path = NULL;
2248 struct got_object_id *id1 = NULL, *id2 = NULL;
2249 const char *id_str1 = NULL, *id_str2 = NULL;
2250 char *label1 = NULL, *label2 = NULL;
2252 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2257 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2262 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2265 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
2267 err(1, "-C option %s", errstr);
2270 repo_path = realpath(optarg, NULL);
2271 if (repo_path == NULL)
2272 return got_error_from_errno2("realpath",
2274 got_path_strip_trailing_slashes(repo_path);
2280 ignore_whitespace = 1;
2291 cwd = getcwd(NULL, 0);
2293 error = got_error_from_errno("getcwd");
2296 error = got_worktree_open(&worktree, cwd);
2297 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2300 if (worktree == NULL) {
2301 error = got_error(GOT_ERR_NOT_WORKTREE);
2306 "-r option can't be used when diffing a work tree");
2307 repo_path = strdup(got_worktree_get_repo_path(worktree));
2308 if (repo_path == NULL) {
2309 error = got_error_from_errno("strdup");
2313 error = got_worktree_resolve_path(&path, worktree,
2320 error = got_error_from_errno("strdup");
2324 } else if (argc == 2) {
2326 errx(1, "-s option can't be used when diffing "
2327 "objects in repository");
2330 if (worktree && repo_path == NULL) {
2332 strdup(got_worktree_get_repo_path(worktree));
2333 if (repo_path == NULL) {
2334 error = got_error_from_errno("strdup");
2341 if (repo_path == NULL) {
2342 repo_path = getcwd(NULL, 0);
2343 if (repo_path == NULL)
2344 return got_error_from_errno("getcwd");
2347 error = got_repo_open(&repo, repo_path, NULL);
2352 error = apply_unveil(got_repo_get_path(repo), 1,
2353 worktree ? got_worktree_get_root_path(worktree) : NULL);
2358 struct print_diff_arg arg;
2359 struct got_pathlist_head paths;
2364 error = got_object_id_str(&id_str,
2365 got_worktree_get_base_commit_id(worktree));
2369 arg.worktree = worktree;
2370 arg.diff_context = diff_context;
2371 arg.id_str = id_str;
2372 arg.header_shown = 0;
2373 arg.diff_staged = diff_staged;
2374 arg.ignore_whitespace = ignore_whitespace;
2376 error = got_pathlist_append(&paths, path, NULL);
2380 error = got_worktree_status(worktree, &paths, repo, print_diff,
2381 &arg, check_cancelled, NULL);
2383 got_pathlist_free(&paths);
2387 error = match_object_id(&id1, &label1, id_str1, GOT_OBJ_TYPE_ANY, 1,
2392 error = match_object_id(&id2, &label2, id_str2, GOT_OBJ_TYPE_ANY, 1,
2397 error = got_object_get_type(&type1, repo, id1);
2401 error = got_object_get_type(&type2, repo, id2);
2405 if (type1 != type2) {
2406 error = got_error(GOT_ERR_OBJ_TYPE);
2411 case GOT_OBJ_TYPE_BLOB:
2412 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2413 diff_context, ignore_whitespace, repo, stdout);
2415 case GOT_OBJ_TYPE_TREE:
2416 error = got_diff_objects_as_trees(id1, id2, "", "",
2417 diff_context, ignore_whitespace, repo, stdout);
2419 case GOT_OBJ_TYPE_COMMIT:
2420 printf("diff %s %s\n", label1, label2);
2421 error = got_diff_objects_as_commits(id1, id2, diff_context,
2422 ignore_whitespace, repo, stdout);
2425 error = got_error(GOT_ERR_OBJ_TYPE);
2435 got_worktree_close(worktree);
2437 const struct got_error *repo_error;
2438 repo_error = got_repo_close(repo);
2449 "usage: %s blame [-c commit] [-r repository-path] path\n",
2458 char datebuf[11]; /* YYYY-MM-DD + NUL */
2461 struct blame_cb_args {
2462 struct blame_line *lines;
2466 off_t *line_offsets;
2468 struct got_repository *repo;
2471 static const struct got_error *
2472 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2474 const struct got_error *err = NULL;
2475 struct blame_cb_args *a = arg;
2476 struct blame_line *bline;
2478 size_t linesize = 0;
2479 struct got_commit_object *commit = NULL;
2482 time_t committer_time;
2484 if (nlines != a->nlines ||
2485 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2486 return got_error(GOT_ERR_RANGE);
2488 if (sigint_received)
2489 return got_error(GOT_ERR_ITER_COMPLETED);
2492 return NULL; /* no change in this commit */
2494 /* Annotate this line. */
2495 bline = &a->lines[lineno - 1];
2496 if (bline->annotated)
2498 err = got_object_id_str(&bline->id_str, id);
2502 err = got_object_open_as_commit(&commit, a->repo, id);
2506 bline->committer = strdup(got_object_commit_get_committer(commit));
2507 if (bline->committer == NULL) {
2508 err = got_error_from_errno("strdup");
2512 committer_time = got_object_commit_get_committer_time(commit);
2513 if (localtime_r(&committer_time, &tm) == NULL)
2514 return got_error_from_errno("localtime_r");
2515 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G/%m/%d",
2516 &tm) >= sizeof(bline->datebuf)) {
2517 err = got_error(GOT_ERR_NO_SPACE);
2520 bline->annotated = 1;
2522 /* Print lines annotated so far. */
2523 bline = &a->lines[a->lineno_cur - 1];
2524 if (!bline->annotated)
2527 offset = a->line_offsets[a->lineno_cur - 1];
2528 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2529 err = got_error_from_errno("fseeko");
2533 while (bline->annotated) {
2534 char *smallerthan, *at, *nl, *committer;
2537 if (getline(&line, &linesize, a->f) == -1) {
2539 err = got_error_from_errno("getline");
2543 committer = bline->committer;
2544 smallerthan = strchr(committer, '<');
2545 if (smallerthan && smallerthan[1] != '\0')
2546 committer = smallerthan + 1;
2547 at = strchr(committer, '@');
2550 len = strlen(committer);
2552 committer[8] = '\0';
2554 nl = strchr(line, '\n');
2557 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2558 bline->id_str, bline->datebuf, committer, line);
2561 bline = &a->lines[a->lineno_cur - 1];
2565 got_object_commit_close(commit);
2570 static const struct got_error *
2571 cmd_blame(int argc, char *argv[])
2573 const struct got_error *error;
2574 struct got_repository *repo = NULL;
2575 struct got_worktree *worktree = NULL;
2576 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2577 struct got_object_id *obj_id = NULL;
2578 struct got_object_id *commit_id = NULL;
2579 struct got_blob_object *blob = NULL;
2580 char *commit_id_str = NULL;
2581 struct blame_cb_args bca;
2582 int ch, obj_type, i;
2585 memset(&bca, 0, sizeof(bca));
2588 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2593 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2596 commit_id_str = optarg;
2599 repo_path = realpath(optarg, NULL);
2600 if (repo_path == NULL)
2601 return got_error_from_errno2("realpath",
2603 got_path_strip_trailing_slashes(repo_path);
2619 cwd = getcwd(NULL, 0);
2621 error = got_error_from_errno("getcwd");
2624 if (repo_path == NULL) {
2625 error = got_worktree_open(&worktree, cwd);
2626 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2632 strdup(got_worktree_get_repo_path(worktree));
2633 if (repo_path == NULL)
2634 error = got_error_from_errno("strdup");
2638 repo_path = strdup(cwd);
2639 if (repo_path == NULL) {
2640 error = got_error_from_errno("strdup");
2646 error = got_repo_open(&repo, repo_path, NULL);
2650 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2655 const char *prefix = got_worktree_get_path_prefix(worktree);
2656 char *p, *worktree_subdir = cwd +
2657 strlen(got_worktree_get_root_path(worktree));
2658 if (asprintf(&p, "%s%s%s%s%s",
2659 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2660 worktree_subdir, worktree_subdir[0] ? "/" : "",
2662 error = got_error_from_errno("asprintf");
2665 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2668 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2673 if (commit_id_str == NULL) {
2674 struct got_reference *head_ref;
2675 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2678 error = got_ref_resolve(&commit_id, repo, head_ref);
2679 got_ref_close(head_ref);
2683 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2688 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2691 if (obj_id == NULL) {
2692 error = got_error(GOT_ERR_NO_OBJ);
2696 error = got_object_get_type(&obj_type, repo, obj_id);
2700 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2701 error = got_error(GOT_ERR_OBJ_TYPE);
2705 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2708 bca.f = got_opentemp();
2709 if (bca.f == NULL) {
2710 error = got_error_from_errno("got_opentemp");
2713 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2714 &bca.line_offsets, bca.f, blob);
2715 if (error || bca.nlines == 0)
2718 /* Don't include \n at EOF in the blame line count. */
2719 if (bca.line_offsets[bca.nlines - 1] == filesize)
2722 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2723 if (bca.lines == NULL) {
2724 error = got_error_from_errno("calloc");
2728 bca.nlines_prec = 0;
2736 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2737 check_cancelled, NULL);
2747 got_object_blob_close(blob);
2749 got_worktree_close(worktree);
2751 const struct got_error *repo_error;
2752 repo_error = got_repo_close(repo);
2757 for (i = 0; i < bca.nlines; i++) {
2758 struct blame_line *bline = &bca.lines[i];
2759 free(bline->id_str);
2760 free(bline->committer);
2764 free(bca.line_offsets);
2765 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2766 error = got_error_from_errno("fclose");
2774 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2780 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2781 const char *root_path)
2783 int is_root_path = (strcmp(path, root_path) == 0);
2784 const char *modestr = "";
2785 mode_t mode = got_tree_entry_get_mode(te);
2787 path += strlen(root_path);
2788 while (path[0] == '/')
2791 if (got_object_tree_entry_is_submodule(te))
2793 else if (S_ISLNK(mode))
2795 else if (S_ISDIR(mode))
2797 else if (mode & S_IXUSR)
2800 printf("%s%s%s%s%s\n", id ? id : "", path,
2801 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
2804 static const struct got_error *
2805 print_tree(const char *path, struct got_object_id *commit_id,
2806 int show_ids, int recurse, const char *root_path,
2807 struct got_repository *repo)
2809 const struct got_error *err = NULL;
2810 struct got_object_id *tree_id = NULL;
2811 struct got_tree_object *tree = NULL;
2814 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2818 err = got_object_open_as_tree(&tree, repo, tree_id);
2821 nentries = got_object_tree_get_nentries(tree);
2822 for (i = 0; i < nentries; i++) {
2823 struct got_tree_entry *te;
2826 if (sigint_received || sigpipe_received)
2829 te = got_object_tree_get_entry(tree, i);
2832 err = got_object_id_str(&id_str,
2833 got_tree_entry_get_id(te));
2836 if (asprintf(&id, "%s ", id_str) == -1) {
2837 err = got_error_from_errno("asprintf");
2843 print_entry(te, id, path, root_path);
2846 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
2848 if (asprintf(&child_path, "%s%s%s", path,
2849 path[0] == '/' && path[1] == '\0' ? "" : "/",
2850 got_tree_entry_get_name(te)) == -1) {
2851 err = got_error_from_errno("asprintf");
2854 err = print_tree(child_path, commit_id, show_ids, 1,
2863 got_object_tree_close(tree);
2868 static const struct got_error *
2869 cmd_tree(int argc, char *argv[])
2871 const struct got_error *error;
2872 struct got_repository *repo = NULL;
2873 struct got_worktree *worktree = NULL;
2875 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2876 struct got_object_id *commit_id = NULL;
2877 char *commit_id_str = NULL;
2878 int show_ids = 0, recurse = 0;
2882 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2887 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2890 commit_id_str = optarg;
2893 repo_path = realpath(optarg, NULL);
2894 if (repo_path == NULL)
2895 return got_error_from_errno2("realpath",
2897 got_path_strip_trailing_slashes(repo_path);
2921 cwd = getcwd(NULL, 0);
2923 error = got_error_from_errno("getcwd");
2926 if (repo_path == NULL) {
2927 error = got_worktree_open(&worktree, cwd);
2928 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2934 strdup(got_worktree_get_repo_path(worktree));
2935 if (repo_path == NULL)
2936 error = got_error_from_errno("strdup");
2940 repo_path = strdup(cwd);
2941 if (repo_path == NULL) {
2942 error = got_error_from_errno("strdup");
2948 error = got_repo_open(&repo, repo_path, NULL);
2952 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2958 char *p, *worktree_subdir = cwd +
2959 strlen(got_worktree_get_root_path(worktree));
2960 if (asprintf(&p, "%s/%s",
2961 got_worktree_get_path_prefix(worktree),
2962 worktree_subdir) == -1) {
2963 error = got_error_from_errno("asprintf");
2966 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2973 if (in_repo_path == NULL) {
2974 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2979 if (commit_id_str == NULL) {
2980 struct got_reference *head_ref;
2981 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2984 error = got_ref_resolve(&commit_id, repo, head_ref);
2985 got_ref_close(head_ref);
2989 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2994 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2995 in_repo_path, repo);
3002 got_worktree_close(worktree);
3004 const struct got_error *repo_error;
3005 repo_error = got_repo_close(repo);
3015 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
3019 static const struct got_error *
3020 print_status(void *arg, unsigned char status, unsigned char staged_status,
3021 const char *path, struct got_object_id *blob_id,
3022 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
3024 if (status == staged_status && (status == GOT_STATUS_DELETE))
3025 status = GOT_STATUS_NO_CHANGE;
3026 printf("%c%c %s\n", status, staged_status, path);
3030 static const struct got_error *
3031 cmd_status(int argc, char *argv[])
3033 const struct got_error *error = NULL;
3034 struct got_repository *repo = NULL;
3035 struct got_worktree *worktree = NULL;
3037 struct got_pathlist_head paths;
3038 struct got_pathlist_entry *pe;
3043 while ((ch = getopt(argc, argv, "")) != -1) {
3055 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3059 cwd = getcwd(NULL, 0);
3061 error = got_error_from_errno("getcwd");
3065 error = got_worktree_open(&worktree, cwd);
3069 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3074 error = apply_unveil(got_repo_get_path(repo), 1,
3075 got_worktree_get_root_path(worktree));
3079 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3083 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3084 check_cancelled, NULL);
3086 TAILQ_FOREACH(pe, &paths, entry)
3087 free((char *)pe->path);
3088 got_pathlist_free(&paths);
3097 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3102 static const struct got_error *
3103 list_refs(struct got_repository *repo)
3105 static const struct got_error *err = NULL;
3106 struct got_reflist_head refs;
3107 struct got_reflist_entry *re;
3109 SIMPLEQ_INIT(&refs);
3110 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3114 SIMPLEQ_FOREACH(re, &refs, entry) {
3116 refstr = got_ref_to_str(re->ref);
3118 return got_error_from_errno("got_ref_to_str");
3119 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3123 got_ref_list_free(&refs);
3127 static const struct got_error *
3128 delete_ref(struct got_repository *repo, const char *refname)
3130 const struct got_error *err = NULL;
3131 struct got_reference *ref;
3133 err = got_ref_open(&ref, repo, refname, 0);
3137 err = got_ref_delete(ref, repo);
3142 static const struct got_error *
3143 add_ref(struct got_repository *repo, const char *refname, const char *target)
3145 const struct got_error *err = NULL;
3146 struct got_object_id *id;
3147 struct got_reference *ref = NULL;
3150 * Don't let the user create a reference name with a leading '-'.
3151 * While technically a valid reference name, this case is usually
3152 * an unintended typo.
3154 if (refname[0] == '-')
3155 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3157 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3160 struct got_reference *target_ref;
3162 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3164 err = got_ref_open(&target_ref, repo, target, 0);
3167 err = got_ref_resolve(&id, repo, target_ref);
3168 got_ref_close(target_ref);
3173 err = got_ref_alloc(&ref, refname, id);
3177 err = got_ref_write(ref, repo);
3185 static const struct got_error *
3186 add_symref(struct got_repository *repo, const char *refname, const char *target)
3188 const struct got_error *err = NULL;
3189 struct got_reference *ref = NULL;
3190 struct got_reference *target_ref = NULL;
3193 * Don't let the user create a reference name with a leading '-'.
3194 * While technically a valid reference name, this case is usually
3195 * an unintended typo.
3197 if (refname[0] == '-')
3198 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3200 err = got_ref_open(&target_ref, repo, target, 0);
3204 err = got_ref_alloc_symref(&ref, refname, target_ref);
3208 err = got_ref_write(ref, repo);
3211 got_ref_close(target_ref);
3217 static const struct got_error *
3218 cmd_ref(int argc, char *argv[])
3220 const struct got_error *error = NULL;
3221 struct got_repository *repo = NULL;
3222 struct got_worktree *worktree = NULL;
3223 char *cwd = NULL, *repo_path = NULL;
3224 int ch, do_list = 0, create_symref = 0;
3225 const char *delref = NULL;
3227 /* TODO: Add -s option for adding symbolic references. */
3228 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3234 repo_path = realpath(optarg, NULL);
3235 if (repo_path == NULL)
3236 return got_error_from_errno2("realpath",
3238 got_path_strip_trailing_slashes(repo_path);
3252 if (do_list && delref)
3253 errx(1, "-l and -d options are mutually exclusive\n");
3258 if (do_list || delref) {
3260 errx(1, "-s option cannot be used together with the "
3261 "-l or -d options");
3264 } else if (argc != 2)
3269 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3273 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3274 "sendfd unveil", NULL) == -1)
3278 cwd = getcwd(NULL, 0);
3280 error = got_error_from_errno("getcwd");
3284 if (repo_path == NULL) {
3285 error = got_worktree_open(&worktree, cwd);
3286 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3292 strdup(got_worktree_get_repo_path(worktree));
3293 if (repo_path == NULL)
3294 error = got_error_from_errno("strdup");
3298 repo_path = strdup(cwd);
3299 if (repo_path == NULL) {
3300 error = got_error_from_errno("strdup");
3306 error = got_repo_open(&repo, repo_path, NULL);
3310 error = apply_unveil(got_repo_get_path(repo), do_list,
3311 worktree ? got_worktree_get_root_path(worktree) : NULL);
3316 error = list_refs(repo);
3318 error = delete_ref(repo, delref);
3319 else if (create_symref)
3320 error = add_symref(repo, argv[0], argv[1]);
3322 error = add_ref(repo, argv[0], argv[1]);
3325 got_repo_close(repo);
3327 got_worktree_close(worktree);
3337 "usage: %s branch [-c commit] [-r repository] [-l] | -d name | "
3338 "[name]\n", getprogname());
3342 static const struct got_error *
3343 list_branch(struct got_repository *repo, struct got_worktree *worktree,
3344 struct got_reference *ref)
3346 const struct got_error *err = NULL;
3347 const char *refname, *marker = " ";
3350 refname = got_ref_get_name(ref);
3351 if (worktree && strcmp(refname,
3352 got_worktree_get_head_ref_name(worktree)) == 0) {
3353 struct got_object_id *id = NULL;
3355 err = got_ref_resolve(&id, repo, ref);
3358 if (got_object_id_cmp(id,
3359 got_worktree_get_base_commit_id(worktree)) == 0)
3366 if (strncmp(refname, "refs/heads/", 11) == 0)
3368 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3371 refstr = got_ref_to_str(ref);
3373 return got_error_from_errno("got_ref_to_str");
3375 printf("%s%s: %s\n", marker, refname, refstr);
3380 static const struct got_error *
3381 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
3383 const char *refname;
3385 if (worktree == NULL)
3386 return got_error(GOT_ERR_NOT_WORKTREE);
3388 refname = got_worktree_get_head_ref_name(worktree);
3390 if (strncmp(refname, "refs/heads/", 11) == 0)
3392 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3395 printf("%s\n", refname);
3400 static const struct got_error *
3401 list_branches(struct got_repository *repo, struct got_worktree *worktree)
3403 static const struct got_error *err = NULL;
3404 struct got_reflist_head refs;
3405 struct got_reflist_entry *re;
3406 struct got_reference *temp_ref = NULL;
3407 int rebase_in_progress, histedit_in_progress;
3409 SIMPLEQ_INIT(&refs);
3412 err = got_worktree_rebase_in_progress(&rebase_in_progress,
3417 err = got_worktree_histedit_in_progress(&histedit_in_progress,
3422 if (rebase_in_progress || histedit_in_progress) {
3423 err = got_ref_open(&temp_ref, repo,
3424 got_worktree_get_head_ref_name(worktree), 0);
3427 list_branch(repo, worktree, temp_ref);
3428 got_ref_close(temp_ref);
3432 err = got_ref_list(&refs, repo, "refs/heads",
3433 got_ref_cmp_by_name, NULL);
3437 SIMPLEQ_FOREACH(re, &refs, entry)
3438 list_branch(repo, worktree, re->ref);
3440 got_ref_list_free(&refs);
3444 static const struct got_error *
3445 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
3446 const char *branch_name)
3448 const struct got_error *err = NULL;
3449 struct got_reference *ref = NULL;
3452 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3453 return got_error_from_errno("asprintf");
3455 err = got_ref_open(&ref, repo, refname, 0);
3460 strcmp(got_worktree_get_head_ref_name(worktree),
3461 got_ref_get_name(ref)) == 0) {
3462 err = got_error_msg(GOT_ERR_SAME_BRANCH,
3463 "will not delete this work tree's current branch");
3467 err = got_ref_delete(ref, repo);
3475 static const struct got_error *
3476 add_branch(struct got_repository *repo, const char *branch_name,
3477 struct got_object_id *base_commit_id)
3479 const struct got_error *err = NULL;
3480 struct got_reference *ref = NULL;
3481 char *base_refname = NULL, *refname = NULL;
3484 * Don't let the user create a branch name with a leading '-'.
3485 * While technically a valid reference name, this case is usually
3486 * an unintended typo.
3488 if (branch_name[0] == '-')
3489 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
3491 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3492 err = got_error_from_errno("asprintf");
3496 err = got_ref_open(&ref, repo, refname, 0);
3498 err = got_error(GOT_ERR_BRANCH_EXISTS);
3500 } else if (err->code != GOT_ERR_NOT_REF)
3503 err = got_ref_alloc(&ref, refname, base_commit_id);
3507 err = got_ref_write(ref, repo);
3516 static const struct got_error *
3517 cmd_branch(int argc, char *argv[])
3519 const struct got_error *error = NULL;
3520 struct got_repository *repo = NULL;
3521 struct got_worktree *worktree = NULL;
3522 char *cwd = NULL, *repo_path = NULL;
3523 int ch, do_list = 0, do_show = 0;
3524 const char *delref = NULL, *commit_id_arg = NULL;
3526 while ((ch = getopt(argc, argv, "c:d:r:l")) != -1) {
3529 commit_id_arg = optarg;
3535 repo_path = realpath(optarg, NULL);
3536 if (repo_path == NULL)
3537 return got_error_from_errno2("realpath",
3539 got_path_strip_trailing_slashes(repo_path);
3550 if (do_list && delref)
3551 errx(1, "-l and -d options are mutually exclusive\n");
3556 if (!do_list && !delref && argc == 0)
3559 if ((do_list || delref || do_show) && commit_id_arg != NULL)
3560 errx(1, "-c option can only be used when creating a branch");
3562 if (do_list || delref) {
3565 } else if (!do_show && argc != 1)
3569 if (do_list || do_show) {
3570 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3574 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3575 "sendfd unveil", NULL) == -1)
3579 cwd = getcwd(NULL, 0);
3581 error = got_error_from_errno("getcwd");
3585 if (repo_path == NULL) {
3586 error = got_worktree_open(&worktree, cwd);
3587 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3593 strdup(got_worktree_get_repo_path(worktree));
3594 if (repo_path == NULL)
3595 error = got_error_from_errno("strdup");
3599 repo_path = strdup(cwd);
3600 if (repo_path == NULL) {
3601 error = got_error_from_errno("strdup");
3607 error = got_repo_open(&repo, repo_path, NULL);
3611 error = apply_unveil(got_repo_get_path(repo), do_list,
3612 worktree ? got_worktree_get_root_path(worktree) : NULL);
3617 error = show_current_branch(repo, worktree);
3619 error = list_branches(repo, worktree);
3621 error = delete_branch(repo, worktree, delref);
3623 struct got_object_id *commit_id;
3624 if (commit_id_arg == NULL)
3625 commit_id_arg = worktree ?
3626 got_worktree_get_head_ref_name(worktree) :
3628 error = resolve_commit_arg(&commit_id, commit_id_arg, repo);
3631 error = add_branch(repo, argv[0], commit_id);
3636 got_repo_close(repo);
3638 got_worktree_close(worktree);
3649 "usage: %s tag [-r repository] | -l | "
3650 "[-m message] name [commit]\n", getprogname());
3655 static const struct got_error *
3656 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
3658 const struct got_error *err = NULL;
3659 struct got_reflist_entry *re, *se, *new;
3660 struct got_object_id *re_id, *se_id;
3661 struct got_tag_object *re_tag, *se_tag;
3662 time_t re_time, se_time;
3664 SIMPLEQ_FOREACH(re, tags, entry) {
3665 se = SIMPLEQ_FIRST(sorted);
3667 err = got_reflist_entry_dup(&new, re);
3670 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
3673 err = got_ref_resolve(&re_id, repo, re->ref);
3676 err = got_object_open_as_tag(&re_tag, repo, re_id);
3680 re_time = got_object_tag_get_tagger_time(re_tag);
3681 got_object_tag_close(re_tag);
3685 err = got_ref_resolve(&se_id, repo, re->ref);
3688 err = got_object_open_as_tag(&se_tag, repo, se_id);
3692 se_time = got_object_tag_get_tagger_time(se_tag);
3693 got_object_tag_close(se_tag);
3695 if (se_time > re_time) {
3696 err = got_reflist_entry_dup(&new, re);
3699 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
3702 se = SIMPLEQ_NEXT(se, entry);
3711 static const struct got_error *
3712 cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
3713 struct got_reference *ref2)
3715 const struct got_error *err = NULL;
3716 struct got_repository *repo = arg;
3717 struct got_object_id *id1, *id2 = NULL;
3718 struct got_tag_object *tag1 = NULL, *tag2 = NULL;
3719 time_t time1, time2;
3723 err = got_ref_resolve(&id1, repo, ref1);
3726 err = got_object_open_as_tag(&tag1, repo, id1);
3730 err = got_ref_resolve(&id2, repo, ref2);
3733 err = got_object_open_as_tag(&tag2, repo, id2);
3737 time1 = got_object_tag_get_tagger_time(tag1);
3738 time2 = got_object_tag_get_tagger_time(tag2);
3740 /* Put latest tags first. */
3743 else if (time1 > time2)
3746 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
3751 got_object_tag_close(tag1);
3753 got_object_tag_close(tag2);
3757 static const struct got_error *
3758 list_tags(struct got_repository *repo, struct got_worktree *worktree)
3760 static const struct got_error *err = NULL;
3761 struct got_reflist_head refs;
3762 struct got_reflist_entry *re;
3764 SIMPLEQ_INIT(&refs);
3766 err = got_ref_list(&refs, repo, "refs/tags", cmp_tags, repo);
3770 SIMPLEQ_FOREACH(re, &refs, entry) {
3771 const char *refname;
3772 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
3775 struct got_object_id *id;
3776 struct got_tag_object *tag;
3778 refname = got_ref_get_name(re->ref);
3779 if (strncmp(refname, "refs/tags/", 10) != 0)
3782 refstr = got_ref_to_str(re->ref);
3783 if (refstr == NULL) {
3784 err = got_error_from_errno("got_ref_to_str");
3787 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
3790 err = got_ref_resolve(&id, repo, re->ref);
3793 err = got_object_open_as_tag(&tag, repo, id);
3797 printf("from: %s\n", got_object_tag_get_tagger(tag));
3798 tagger_time = got_object_tag_get_tagger_time(tag);
3799 datestr = get_datestr(&tagger_time, datebuf);
3801 printf("date: %s UTC\n", datestr);
3802 err = got_object_id_str(&id_str,
3803 got_object_tag_get_object_id(tag));
3806 switch (got_object_tag_get_object_type(tag)) {
3807 case GOT_OBJ_TYPE_BLOB:
3808 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB, id_str);
3810 case GOT_OBJ_TYPE_TREE:
3811 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE, id_str);
3813 case GOT_OBJ_TYPE_COMMIT:
3814 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
3816 case GOT_OBJ_TYPE_TAG:
3817 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG, id_str);
3823 tagmsg0 = strdup(got_object_tag_get_message(tag));
3824 got_object_tag_close(tag);
3825 if (tagmsg0 == NULL) {
3826 err = got_error_from_errno("strdup");
3832 line = strsep(&tagmsg, "\n");
3834 printf(" %s\n", line);
3839 got_ref_list_free(&refs);
3843 static const struct got_error *
3844 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
3845 const char *tag_name, const char *repo_path)
3847 const struct got_error *err = NULL;
3848 char *template = NULL, *initial_content = NULL;
3849 char *editor = NULL;
3852 if (asprintf(&template, "/tmp/got-tagmsg") == -1) {
3853 err = got_error_from_errno("asprintf");
3857 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
3858 commit_id_str, tag_name) == -1) {
3859 err = got_error_from_errno("asprintf");
3863 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
3867 dprintf(fd, initial_content);
3870 err = get_editor(&editor);
3873 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
3875 free(initial_content);
3879 /* Editor is done; we can now apply unveil(2) */
3881 err = apply_unveil(repo_path, 0, NULL);
3890 static const struct got_error *
3891 add_tag(struct got_repository *repo, const char *tag_name,
3892 const char *commit_arg, const char *tagmsg_arg)
3894 const struct got_error *err = NULL;
3895 struct got_object_id *commit_id = NULL, *tag_id = NULL;
3896 char *label = NULL, *commit_id_str = NULL;
3897 struct got_reference *ref = NULL;
3898 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
3899 char *tagmsg_path = NULL, *tag_id_str = NULL;
3900 int preserve_tagmsg = 0;
3903 * Don't let the user create a tag name with a leading '-'.
3904 * While technically a valid reference name, this case is usually
3905 * an unintended typo.
3907 if (tag_name[0] == '-')
3908 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
3910 err = get_author(&tagger, repo);
3914 err = match_object_id(&commit_id, &label, commit_arg,
3915 GOT_OBJ_TYPE_COMMIT, 1, repo);
3919 err = got_object_id_str(&commit_id_str, commit_id);
3923 if (strncmp("refs/tags/", tag_name, 10) == 0) {
3924 refname = strdup(tag_name);
3925 if (refname == NULL) {
3926 err = got_error_from_errno("strdup");
3930 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
3931 err = got_error_from_errno("asprintf");
3935 err = got_ref_open(&ref, repo, refname, 0);
3937 err = got_error(GOT_ERR_TAG_EXISTS);
3939 } else if (err->code != GOT_ERR_NOT_REF)
3942 if (tagmsg_arg == NULL) {
3943 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
3944 tag_name, got_repo_get_path(repo));
3946 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
3947 tagmsg_path != NULL)
3948 preserve_tagmsg = 1;
3953 err = got_object_tag_create(&tag_id, tag_name, commit_id,
3954 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
3957 preserve_tagmsg = 1;
3961 err = got_ref_alloc(&ref, refname, tag_id);
3964 preserve_tagmsg = 1;
3968 err = got_ref_write(ref, repo);
3971 preserve_tagmsg = 1;
3975 err = got_object_id_str(&tag_id_str, tag_id);
3978 preserve_tagmsg = 1;
3981 printf("Created tag %s\n", tag_id_str);
3983 if (preserve_tagmsg) {
3984 fprintf(stderr, "%s: tag message preserved in %s\n",
3985 getprogname(), tagmsg_path);
3986 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
3987 err = got_error_from_errno2("unlink", tagmsg_path);
3992 free(commit_id_str);
4000 static const struct got_error *
4001 cmd_tag(int argc, char *argv[])
4003 const struct got_error *error = NULL;
4004 struct got_repository *repo = NULL;
4005 struct got_worktree *worktree = NULL;
4006 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
4007 char *gitconfig_path = NULL;
4008 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
4009 int ch, do_list = 0;
4011 while ((ch = getopt(argc, argv, "m:r:l")) != -1) {
4017 repo_path = realpath(optarg, NULL);
4018 if (repo_path == NULL)
4019 return got_error_from_errno2("realpath",
4021 got_path_strip_trailing_slashes(repo_path);
4037 errx(1, "-l and -m options are mutually exclusive\n");
4040 } else if (argc < 1 || argc > 2)
4043 commit_id_arg = argv[1];
4048 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4052 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4053 "sendfd unveil", NULL) == -1)
4057 cwd = getcwd(NULL, 0);
4059 error = got_error_from_errno("getcwd");
4063 if (repo_path == NULL) {
4064 error = got_worktree_open(&worktree, cwd);
4065 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4071 strdup(got_worktree_get_repo_path(worktree));
4072 if (repo_path == NULL)
4073 error = got_error_from_errno("strdup");
4077 repo_path = strdup(cwd);
4078 if (repo_path == NULL) {
4079 error = got_error_from_errno("strdup");
4086 error = got_repo_open(&repo, repo_path, NULL);
4089 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4092 error = list_tags(repo, worktree);
4094 error = get_gitconfig_path(&gitconfig_path);
4097 error = got_repo_open(&repo, repo_path, gitconfig_path);
4102 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4107 if (commit_id_arg == NULL) {
4108 struct got_reference *head_ref;
4109 struct got_object_id *commit_id;
4110 error = got_ref_open(&head_ref, repo,
4111 worktree ? got_worktree_get_head_ref_name(worktree)
4115 error = got_ref_resolve(&commit_id, repo, head_ref);
4116 got_ref_close(head_ref);
4119 error = got_object_id_str(&commit_id_str, commit_id);
4125 error = add_tag(repo, tag_name,
4126 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4130 got_repo_close(repo);
4132 got_worktree_close(worktree);
4135 free(gitconfig_path);
4136 free(commit_id_str);
4143 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
4147 static const struct got_error *
4148 add_progress(void *arg, unsigned char status, const char *path)
4150 while (path[0] == '/')
4152 printf("%c %s\n", status, path);
4156 static const struct got_error *
4157 cmd_add(int argc, char *argv[])
4159 const struct got_error *error = NULL;
4160 struct got_repository *repo = NULL;
4161 struct got_worktree *worktree = NULL;
4163 struct got_pathlist_head paths;
4164 struct got_pathlist_entry *pe;
4165 int ch, can_recurse = 0;
4169 while ((ch = getopt(argc, argv, "R")) != -1) {
4184 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4191 cwd = getcwd(NULL, 0);
4193 error = got_error_from_errno("getcwd");
4197 error = got_worktree_open(&worktree, cwd);
4201 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4206 error = apply_unveil(got_repo_get_path(repo), 1,
4207 got_worktree_get_root_path(worktree));
4211 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4218 TAILQ_FOREACH(pe, &paths, entry) {
4219 if (asprintf(&ondisk_path, "%s/%s",
4220 got_worktree_get_root_path(worktree),
4222 error = got_error_from_errno("asprintf");
4225 if (lstat(ondisk_path, &sb) == -1) {
4226 if (errno == ENOENT) {
4230 error = got_error_from_errno2("lstat",
4236 if (S_ISDIR(sb.st_mode)) {
4237 error = got_error_msg(GOT_ERR_BAD_PATH,
4238 "adding directories requires -R option");
4243 error = got_worktree_schedule_add(worktree, &paths, add_progress,
4247 got_repo_close(repo);
4249 got_worktree_close(worktree);
4250 TAILQ_FOREACH(pe, &paths, entry)
4251 free((char *)pe->path);
4252 got_pathlist_free(&paths);
4260 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
4264 static const struct got_error *
4265 print_remove_status(void *arg, unsigned char status,
4266 unsigned char staged_status, const char *path,
4267 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4268 struct got_object_id *commit_id)
4270 if (status == GOT_STATUS_NONEXISTENT)
4272 if (status == staged_status && (status == GOT_STATUS_DELETE))
4273 status = GOT_STATUS_NO_CHANGE;
4274 printf("%c%c %s\n", status, staged_status, path);
4278 static const struct got_error *
4279 cmd_remove(int argc, char *argv[])
4281 const struct got_error *error = NULL;
4282 struct got_worktree *worktree = NULL;
4283 struct got_repository *repo = NULL;
4285 struct got_pathlist_head paths;
4286 struct got_pathlist_entry *pe;
4287 int ch, delete_local_mods = 0;
4291 while ((ch = getopt(argc, argv, "f")) != -1) {
4294 delete_local_mods = 1;
4306 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4313 cwd = getcwd(NULL, 0);
4315 error = got_error_from_errno("getcwd");
4318 error = got_worktree_open(&worktree, cwd);
4322 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4327 error = apply_unveil(got_repo_get_path(repo), 1,
4328 got_worktree_get_root_path(worktree));
4332 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4336 error = got_worktree_schedule_delete(worktree, &paths,
4337 delete_local_mods, print_remove_status, NULL, repo);
4342 got_repo_close(repo);
4344 got_worktree_close(worktree);
4345 TAILQ_FOREACH(pe, &paths, entry)
4346 free((char *)pe->path);
4347 got_pathlist_free(&paths);
4355 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4356 "path ...\n", getprogname());
4360 static const struct got_error *
4361 revert_progress(void *arg, unsigned char status, const char *path)
4363 while (path[0] == '/')
4365 printf("%c %s\n", status, path);
4369 struct choose_patch_arg {
4370 FILE *patch_script_file;
4374 static const struct got_error *
4375 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4376 int nchanges, const char *action)
4379 size_t linesize = 0;
4383 case GOT_STATUS_ADD:
4384 printf("A %s\n%s this addition? [y/n] ", path, action);
4386 case GOT_STATUS_DELETE:
4387 printf("D %s\n%s this deletion? [y/n] ", path, action);
4389 case GOT_STATUS_MODIFY:
4390 if (fseek(patch_file, 0L, SEEK_SET) == -1)
4391 return got_error_from_errno("fseek");
4392 printf(GOT_COMMIT_SEP_STR);
4393 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4395 if (ferror(patch_file))
4396 return got_error_from_errno("getline");
4397 printf(GOT_COMMIT_SEP_STR);
4398 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4399 path, n, nchanges, action);
4402 return got_error_path(path, GOT_ERR_FILE_STATUS);
4408 static const struct got_error *
4409 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4410 FILE *patch_file, int n, int nchanges)
4412 const struct got_error *err = NULL;
4414 size_t linesize = 0;
4417 struct choose_patch_arg *a = arg;
4419 *choice = GOT_PATCH_CHOICE_NONE;
4421 if (a->patch_script_file) {
4423 err = show_change(status, path, patch_file, n, nchanges,
4427 linelen = getline(&line, &linesize, a->patch_script_file);
4428 if (linelen == -1) {
4429 if (ferror(a->patch_script_file))
4430 return got_error_from_errno("getline");
4433 nl = strchr(line, '\n');
4436 if (strcmp(line, "y") == 0) {
4437 *choice = GOT_PATCH_CHOICE_YES;
4439 } else if (strcmp(line, "n") == 0) {
4440 *choice = GOT_PATCH_CHOICE_NO;
4442 } else if (strcmp(line, "q") == 0 &&
4443 status == GOT_STATUS_MODIFY) {
4444 *choice = GOT_PATCH_CHOICE_QUIT;
4447 printf("invalid response '%s'\n", line);
4452 while (resp != 'y' && resp != 'n' && resp != 'q') {
4453 err = show_change(status, path, patch_file, n, nchanges,
4460 if (status == GOT_STATUS_MODIFY) {
4461 if (resp != 'y' && resp != 'n' && resp != 'q') {
4462 printf("invalid response '%c'\n", resp);
4465 } else if (resp != 'y' && resp != 'n') {
4466 printf("invalid response '%c'\n", resp);
4472 *choice = GOT_PATCH_CHOICE_YES;
4473 else if (resp == 'n')
4474 *choice = GOT_PATCH_CHOICE_NO;
4475 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4476 *choice = GOT_PATCH_CHOICE_QUIT;
4482 static const struct got_error *
4483 cmd_revert(int argc, char *argv[])
4485 const struct got_error *error = NULL;
4486 struct got_worktree *worktree = NULL;
4487 struct got_repository *repo = NULL;
4488 char *cwd = NULL, *path = NULL;
4489 struct got_pathlist_head paths;
4490 struct got_pathlist_entry *pe;
4491 int ch, can_recurse = 0, pflag = 0;
4492 FILE *patch_script_file = NULL;
4493 const char *patch_script_path = NULL;
4494 struct choose_patch_arg cpa;
4498 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4504 patch_script_path = optarg;
4519 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4520 "unveil", NULL) == -1)
4525 if (patch_script_path && !pflag)
4526 errx(1, "-F option can only be used together with -p option");
4528 cwd = getcwd(NULL, 0);
4530 error = got_error_from_errno("getcwd");
4533 error = got_worktree_open(&worktree, cwd);
4537 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4542 if (patch_script_path) {
4543 patch_script_file = fopen(patch_script_path, "r");
4544 if (patch_script_file == NULL) {
4545 error = got_error_from_errno2("fopen",
4550 error = apply_unveil(got_repo_get_path(repo), 1,
4551 got_worktree_get_root_path(worktree));
4555 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4562 TAILQ_FOREACH(pe, &paths, entry) {
4563 if (asprintf(&ondisk_path, "%s/%s",
4564 got_worktree_get_root_path(worktree),
4566 error = got_error_from_errno("asprintf");
4569 if (lstat(ondisk_path, &sb) == -1) {
4570 if (errno == ENOENT) {
4574 error = got_error_from_errno2("lstat",
4580 if (S_ISDIR(sb.st_mode)) {
4581 error = got_error_msg(GOT_ERR_BAD_PATH,
4582 "reverting directories requires -R option");
4588 cpa.patch_script_file = patch_script_file;
4589 cpa.action = "revert";
4590 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4591 pflag ? choose_patch : NULL, &cpa, repo);
4595 if (patch_script_file && fclose(patch_script_file) == EOF &&
4597 error = got_error_from_errno2("fclose", patch_script_path);
4599 got_repo_close(repo);
4601 got_worktree_close(worktree);
4610 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4615 struct collect_commit_logmsg_arg {
4616 const char *cmdline_log;
4618 const char *worktree_path;
4619 const char *branch_name;
4620 const char *repo_path;
4625 static const struct got_error *
4626 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4629 char *initial_content = NULL;
4630 struct got_pathlist_entry *pe;
4631 const struct got_error *err = NULL;
4632 char *template = NULL;
4633 struct collect_commit_logmsg_arg *a = arg;
4637 /* if a message was specified on the command line, just use it */
4638 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4639 len = strlen(a->cmdline_log) + 1;
4640 *logmsg = malloc(len + 1);
4641 if (*logmsg == NULL)
4642 return got_error_from_errno("malloc");
4643 strlcpy(*logmsg, a->cmdline_log, len);
4647 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
4648 return got_error_from_errno("asprintf");
4650 if (asprintf(&initial_content,
4651 "\n# changes to be committed on branch %s:\n",
4652 a->branch_name) == -1)
4653 return got_error_from_errno("asprintf");
4655 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
4659 dprintf(fd, initial_content);
4661 TAILQ_FOREACH(pe, commitable_paths, entry) {
4662 struct got_commitable *ct = pe->data;
4663 dprintf(fd, "# %c %s\n",
4664 got_commitable_get_status(ct),
4665 got_commitable_get_path(ct));
4669 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
4671 free(initial_content);
4674 /* Editor is done; we can now apply unveil(2) */
4676 err = apply_unveil(a->repo_path, 0, a->worktree_path);
4685 static const struct got_error *
4686 cmd_commit(int argc, char *argv[])
4688 const struct got_error *error = NULL;
4689 struct got_worktree *worktree = NULL;
4690 struct got_repository *repo = NULL;
4691 char *cwd = NULL, *id_str = NULL;
4692 struct got_object_id *id = NULL;
4693 const char *logmsg = NULL;
4694 struct collect_commit_logmsg_arg cl_arg;
4695 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
4696 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
4697 struct got_pathlist_head paths;
4700 cl_arg.logmsg_path = NULL;
4702 while ((ch = getopt(argc, argv, "m:")) != -1) {
4717 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4718 "unveil", NULL) == -1)
4721 cwd = getcwd(NULL, 0);
4723 error = got_error_from_errno("getcwd");
4726 error = got_worktree_open(&worktree, cwd);
4730 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4733 if (rebase_in_progress) {
4734 error = got_error(GOT_ERR_REBASING);
4738 error = got_worktree_histedit_in_progress(&histedit_in_progress,
4743 error = get_gitconfig_path(&gitconfig_path);
4746 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4751 error = get_author(&author, repo);
4756 * unveil(2) traverses exec(2); if an editor is used we have
4757 * to apply unveil after the log message has been written.
4759 if (logmsg == NULL || strlen(logmsg) == 0)
4760 error = get_editor(&editor);
4762 error = apply_unveil(got_repo_get_path(repo), 0,
4763 got_worktree_get_root_path(worktree));
4767 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4771 cl_arg.editor = editor;
4772 cl_arg.cmdline_log = logmsg;
4773 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
4774 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
4775 if (!histedit_in_progress) {
4776 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
4777 error = got_error(GOT_ERR_COMMIT_BRANCH);
4780 cl_arg.branch_name += 11;
4782 cl_arg.repo_path = got_repo_get_path(repo);
4783 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
4784 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
4786 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4787 cl_arg.logmsg_path != NULL)
4788 preserve_logmsg = 1;
4792 error = got_object_id_str(&id_str, id);
4795 printf("Created commit %s\n", id_str);
4797 if (preserve_logmsg) {
4798 fprintf(stderr, "%s: log message preserved in %s\n",
4799 getprogname(), cl_arg.logmsg_path);
4800 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
4802 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
4803 free(cl_arg.logmsg_path);
4805 got_repo_close(repo);
4807 got_worktree_close(worktree);
4810 free(gitconfig_path);
4817 usage_cherrypick(void)
4819 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
4823 static const struct got_error *
4824 cmd_cherrypick(int argc, char *argv[])
4826 const struct got_error *error = NULL;
4827 struct got_worktree *worktree = NULL;
4828 struct got_repository *repo = NULL;
4829 char *cwd = NULL, *commit_id_str = NULL;
4830 struct got_object_id *commit_id = NULL;
4831 struct got_commit_object *commit = NULL;
4832 struct got_object_qid *pid;
4833 struct got_reference *head_ref = NULL;
4834 int ch, did_something = 0;
4836 while ((ch = getopt(argc, argv, "")) != -1) {
4848 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4849 "unveil", NULL) == -1)
4855 cwd = getcwd(NULL, 0);
4857 error = got_error_from_errno("getcwd");
4860 error = got_worktree_open(&worktree, cwd);
4864 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4869 error = apply_unveil(got_repo_get_path(repo), 0,
4870 got_worktree_get_root_path(worktree));
4874 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4875 GOT_OBJ_TYPE_COMMIT, repo);
4876 if (error != NULL) {
4877 struct got_reference *ref;
4878 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4880 error = got_ref_open(&ref, repo, argv[0], 0);
4883 error = got_ref_resolve(&commit_id, repo, ref);
4888 error = got_object_id_str(&commit_id_str, commit_id);
4892 error = got_ref_open(&head_ref, repo,
4893 got_worktree_get_head_ref_name(worktree), 0);
4897 error = check_same_branch(commit_id, head_ref, NULL, repo);
4899 if (error->code != GOT_ERR_ANCESTRY)
4903 error = got_error(GOT_ERR_SAME_BRANCH);
4907 error = got_object_open_as_commit(&commit, repo, commit_id);
4910 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4911 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
4912 commit_id, repo, update_progress, &did_something, check_cancelled,
4918 printf("Merged commit %s\n", commit_id_str);
4921 got_object_commit_close(commit);
4922 free(commit_id_str);
4924 got_ref_close(head_ref);
4926 got_worktree_close(worktree);
4928 got_repo_close(repo);
4935 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
4939 static const struct got_error *
4940 cmd_backout(int argc, char *argv[])
4942 const struct got_error *error = NULL;
4943 struct got_worktree *worktree = NULL;
4944 struct got_repository *repo = NULL;
4945 char *cwd = NULL, *commit_id_str = NULL;
4946 struct got_object_id *commit_id = NULL;
4947 struct got_commit_object *commit = NULL;
4948 struct got_object_qid *pid;
4949 struct got_reference *head_ref = NULL;
4950 int ch, did_something = 0;
4952 while ((ch = getopt(argc, argv, "")) != -1) {
4964 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4965 "unveil", NULL) == -1)
4971 cwd = getcwd(NULL, 0);
4973 error = got_error_from_errno("getcwd");
4976 error = got_worktree_open(&worktree, cwd);
4980 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4985 error = apply_unveil(got_repo_get_path(repo), 0,
4986 got_worktree_get_root_path(worktree));
4990 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4991 GOT_OBJ_TYPE_COMMIT, repo);
4992 if (error != NULL) {
4993 struct got_reference *ref;
4994 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4996 error = got_ref_open(&ref, repo, argv[0], 0);
4999 error = got_ref_resolve(&commit_id, repo, ref);
5004 error = got_object_id_str(&commit_id_str, commit_id);
5008 error = got_ref_open(&head_ref, repo,
5009 got_worktree_get_head_ref_name(worktree), 0);
5013 error = check_same_branch(commit_id, head_ref, NULL, repo);
5017 error = got_object_open_as_commit(&commit, repo, commit_id);
5020 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5022 error = got_error(GOT_ERR_ROOT_COMMIT);
5026 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
5027 update_progress, &did_something, check_cancelled, NULL);
5032 printf("Backed out commit %s\n", commit_id_str);
5035 got_object_commit_close(commit);
5036 free(commit_id_str);
5038 got_ref_close(head_ref);
5040 got_worktree_close(worktree);
5042 got_repo_close(repo);
5049 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
5055 trim_logmsg(char *logmsg, int limit)
5060 len = strlen(logmsg);
5064 nl = strchr(logmsg, '\n');
5069 static const struct got_error *
5070 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5072 const struct got_error *err;
5073 char *logmsg0 = NULL;
5076 err = got_object_commit_get_logmsg(&logmsg0, commit);
5081 while (isspace((unsigned char)s[0]))
5084 *logmsg = strdup(s);
5085 if (*logmsg == NULL) {
5086 err = got_error_from_errno("strdup");
5090 trim_logmsg(*logmsg, limit);
5096 static const struct got_error *
5097 show_rebase_progress(struct got_commit_object *commit,
5098 struct got_object_id *old_id, struct got_object_id *new_id)
5100 const struct got_error *err;
5101 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5103 err = got_object_id_str(&old_id_str, old_id);
5108 err = got_object_id_str(&new_id_str, new_id);
5113 old_id_str[12] = '\0';
5115 new_id_str[12] = '\0';
5117 err = get_short_logmsg(&logmsg, 42, commit);
5121 printf("%s -> %s: %s\n", old_id_str,
5122 new_id_str ? new_id_str : "no-op change", logmsg);
5129 static const struct got_error *
5130 rebase_progress(void *arg, unsigned char status, const char *path)
5132 unsigned char *rebase_status = arg;
5134 while (path[0] == '/')
5136 printf("%c %s\n", status, path);
5138 if (*rebase_status == GOT_STATUS_CONFLICT)
5140 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5141 *rebase_status = status;
5145 static const struct got_error *
5146 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5147 struct got_reference *branch, struct got_reference *new_base_branch,
5148 struct got_reference *tmp_branch, struct got_repository *repo)
5150 printf("Switching work tree to %s\n", got_ref_get_name(branch));
5151 return got_worktree_rebase_complete(worktree, fileindex,
5152 new_base_branch, tmp_branch, branch, repo);
5155 static const struct got_error *
5156 rebase_commit(struct got_pathlist_head *merged_paths,
5157 struct got_worktree *worktree, struct got_fileindex *fileindex,
5158 struct got_reference *tmp_branch,
5159 struct got_object_id *commit_id, struct got_repository *repo)
5161 const struct got_error *error;
5162 struct got_commit_object *commit;
5163 struct got_object_id *new_commit_id;
5165 error = got_object_open_as_commit(&commit, repo, commit_id);
5169 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5170 worktree, fileindex, tmp_branch, commit, commit_id, repo);
5172 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5174 error = show_rebase_progress(commit, commit_id, NULL);
5176 error = show_rebase_progress(commit, commit_id, new_commit_id);
5177 free(new_commit_id);
5180 got_object_commit_close(commit);
5184 struct check_path_prefix_arg {
5185 const char *path_prefix;
5190 static const struct got_error *
5191 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5192 struct got_blob_object *blob2, struct got_object_id *id1,
5193 struct got_object_id *id2, const char *path1, const char *path2,
5194 mode_t mode1, mode_t mode2, struct got_repository *repo)
5196 struct check_path_prefix_arg *a = arg;
5198 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5199 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5200 return got_error(a->errcode);
5205 static const struct got_error *
5206 check_path_prefix(struct got_object_id *parent_id,
5207 struct got_object_id *commit_id, const char *path_prefix,
5208 int errcode, struct got_repository *repo)
5210 const struct got_error *err;
5211 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
5212 struct got_commit_object *commit = NULL, *parent_commit = NULL;
5213 struct check_path_prefix_arg cpp_arg;
5215 if (got_path_is_root_dir(path_prefix))
5218 err = got_object_open_as_commit(&commit, repo, commit_id);
5222 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
5226 err = got_object_open_as_tree(&tree1, repo,
5227 got_object_commit_get_tree_id(parent_commit));
5231 err = got_object_open_as_tree(&tree2, repo,
5232 got_object_commit_get_tree_id(commit));
5236 cpp_arg.path_prefix = path_prefix;
5237 while (cpp_arg.path_prefix[0] == '/')
5238 cpp_arg.path_prefix++;
5239 cpp_arg.len = strlen(cpp_arg.path_prefix);
5240 cpp_arg.errcode = errcode;
5241 err = got_diff_tree(tree1, tree2, "", "", repo,
5242 check_path_prefix_in_diff, &cpp_arg, 0);
5245 got_object_tree_close(tree1);
5247 got_object_tree_close(tree2);
5249 got_object_commit_close(commit);
5251 got_object_commit_close(parent_commit);
5255 static const struct got_error *
5256 collect_commits(struct got_object_id_queue *commits,
5257 struct got_object_id *initial_commit_id,
5258 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
5259 const char *path_prefix, int path_prefix_errcode,
5260 struct got_repository *repo)
5262 const struct got_error *err = NULL;
5263 struct got_commit_graph *graph = NULL;
5264 struct got_object_id *parent_id = NULL;
5265 struct got_object_qid *qid;
5266 struct got_object_id *commit_id = initial_commit_id;
5268 err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
5272 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
5273 check_cancelled, NULL);
5276 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
5277 err = got_commit_graph_iter_next(&parent_id, graph);
5279 if (err->code == GOT_ERR_ITER_COMPLETED) {
5280 err = got_error_msg(GOT_ERR_ANCESTRY,
5281 "ran out of commits to rebase before "
5282 "youngest common ancestor commit has "
5285 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
5287 err = got_commit_graph_fetch_commits(graph, 1, repo,
5288 check_cancelled, NULL);
5292 err = check_path_prefix(parent_id, commit_id,
5293 path_prefix, path_prefix_errcode, repo);
5297 err = got_object_qid_alloc(&qid, commit_id);
5300 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
5301 commit_id = parent_id;
5305 got_commit_graph_close(graph);
5309 static const struct got_error *
5310 cmd_rebase(int argc, char *argv[])
5312 const struct got_error *error = NULL;
5313 struct got_worktree *worktree = NULL;
5314 struct got_repository *repo = NULL;
5315 struct got_fileindex *fileindex = NULL;
5317 struct got_reference *branch = NULL;
5318 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
5319 struct got_object_id *commit_id = NULL, *parent_id = NULL;
5320 struct got_object_id *resume_commit_id = NULL;
5321 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
5322 struct got_commit_object *commit = NULL;
5323 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
5324 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5325 struct got_object_id_queue commits;
5326 struct got_pathlist_head merged_paths;
5327 const struct got_object_id_queue *parent_ids;
5328 struct got_object_qid *qid, *pid;
5330 SIMPLEQ_INIT(&commits);
5331 TAILQ_INIT(&merged_paths);
5333 while ((ch = getopt(argc, argv, "ac")) != -1) {
5339 continue_rebase = 1;
5351 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5352 "unveil", NULL) == -1)
5355 if (abort_rebase && continue_rebase)
5357 else if (abort_rebase || continue_rebase) {
5360 } else if (argc != 1)
5363 cwd = getcwd(NULL, 0);
5365 error = got_error_from_errno("getcwd");
5368 error = got_worktree_open(&worktree, cwd);
5372 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5377 error = apply_unveil(got_repo_get_path(repo), 0,
5378 got_worktree_get_root_path(worktree));
5382 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5388 if (!rebase_in_progress) {
5389 error = got_error(GOT_ERR_NOT_REBASING);
5392 error = got_worktree_rebase_continue(&resume_commit_id,
5393 &new_base_branch, &tmp_branch, &branch, &fileindex,
5397 printf("Switching work tree to %s\n",
5398 got_ref_get_symref_target(new_base_branch));
5399 error = got_worktree_rebase_abort(worktree, fileindex, repo,
5400 new_base_branch, update_progress, &did_something);
5403 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5404 goto done; /* nothing else to do */
5407 if (continue_rebase) {
5408 if (!rebase_in_progress) {
5409 error = got_error(GOT_ERR_NOT_REBASING);
5412 error = got_worktree_rebase_continue(&resume_commit_id,
5413 &new_base_branch, &tmp_branch, &branch, &fileindex,
5418 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5419 resume_commit_id, repo);
5423 yca_id = got_object_id_dup(resume_commit_id);
5424 if (yca_id == NULL) {
5425 error = got_error_from_errno("got_object_id_dup");
5429 error = got_ref_open(&branch, repo, argv[0], 0);
5434 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5438 if (!continue_rebase) {
5439 struct got_object_id *base_commit_id;
5441 base_commit_id = got_worktree_get_base_commit_id(worktree);
5442 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5443 base_commit_id, branch_head_commit_id, repo,
5444 check_cancelled, NULL);
5447 if (yca_id == NULL) {
5448 error = got_error_msg(GOT_ERR_ANCESTRY,
5449 "specified branch shares no common ancestry "
5450 "with work tree's branch");
5454 error = check_same_branch(base_commit_id, branch, yca_id, repo);
5456 if (error->code != GOT_ERR_ANCESTRY)
5460 error = got_error_msg(GOT_ERR_SAME_BRANCH,
5461 "specified branch resolves to a commit which "
5462 "is already contained in work tree's branch");
5465 error = got_worktree_rebase_prepare(&new_base_branch,
5466 &tmp_branch, &fileindex, worktree, branch, repo);
5471 commit_id = branch_head_commit_id;
5472 error = got_object_open_as_commit(&commit, repo, commit_id);
5476 parent_ids = got_object_commit_get_parent_ids(commit);
5477 pid = SIMPLEQ_FIRST(parent_ids);
5479 if (!continue_rebase) {
5481 error = got_worktree_rebase_abort(worktree, fileindex,
5482 repo, new_base_branch, update_progress,
5486 printf("Rebase of %s aborted\n",
5487 got_ref_get_name(branch));
5489 error = got_error(GOT_ERR_EMPTY_REBASE);
5492 error = collect_commits(&commits, commit_id, pid->id,
5493 yca_id, got_worktree_get_path_prefix(worktree),
5494 GOT_ERR_REBASE_PATH, repo);
5495 got_object_commit_close(commit);
5500 if (SIMPLEQ_EMPTY(&commits)) {
5501 if (continue_rebase) {
5502 error = rebase_complete(worktree, fileindex,
5503 branch, new_base_branch, tmp_branch, repo);
5506 /* Fast-forward the reference of the branch. */
5507 struct got_object_id *new_head_commit_id;
5509 error = got_ref_resolve(&new_head_commit_id, repo,
5513 error = got_object_id_str(&id_str, new_head_commit_id);
5514 printf("Forwarding %s to commit %s\n",
5515 got_ref_get_name(branch), id_str);
5517 error = got_ref_change_ref(branch,
5518 new_head_commit_id);
5525 SIMPLEQ_FOREACH(qid, &commits, entry) {
5526 commit_id = qid->id;
5527 parent_id = pid ? pid->id : yca_id;
5530 error = got_worktree_rebase_merge_files(&merged_paths,
5531 worktree, fileindex, parent_id, commit_id, repo,
5532 rebase_progress, &rebase_status, check_cancelled, NULL);
5536 if (rebase_status == GOT_STATUS_CONFLICT) {
5537 got_worktree_rebase_pathlist_free(&merged_paths);
5541 error = rebase_commit(&merged_paths, worktree, fileindex,
5542 tmp_branch, commit_id, repo);
5543 got_worktree_rebase_pathlist_free(&merged_paths);
5548 if (rebase_status == GOT_STATUS_CONFLICT) {
5549 error = got_worktree_rebase_postpone(worktree, fileindex);
5552 error = got_error_msg(GOT_ERR_CONFLICTS,
5553 "conflicts must be resolved before rebasing can continue");
5555 error = rebase_complete(worktree, fileindex, branch,
5556 new_base_branch, tmp_branch, repo);
5558 got_object_id_queue_free(&commits);
5559 free(branch_head_commit_id);
5560 free(resume_commit_id);
5563 got_object_commit_close(commit);
5565 got_ref_close(branch);
5566 if (new_base_branch)
5567 got_ref_close(new_base_branch);
5569 got_ref_close(tmp_branch);
5571 got_worktree_close(worktree);
5573 got_repo_close(repo);
5578 usage_histedit(void)
5580 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script]\n",
5585 #define GOT_HISTEDIT_PICK 'p'
5586 #define GOT_HISTEDIT_EDIT 'e'
5587 #define GOT_HISTEDIT_FOLD 'f'
5588 #define GOT_HISTEDIT_DROP 'd'
5589 #define GOT_HISTEDIT_MESG 'm'
5591 static struct got_histedit_cmd {
5595 } got_histedit_cmds[] = {
5596 { GOT_HISTEDIT_PICK, "pick", "use commit" },
5597 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5598 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
5599 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5600 { GOT_HISTEDIT_MESG, "mesg",
5601 "single-line log message for commit above (open editor if empty)" },
5604 struct got_histedit_list_entry {
5605 TAILQ_ENTRY(got_histedit_list_entry) entry;
5606 struct got_object_id *commit_id;
5607 const struct got_histedit_cmd *cmd;
5610 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5612 static const struct got_error *
5613 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5614 FILE *f, struct got_repository *repo)
5616 const struct got_error *err = NULL;
5617 char *logmsg = NULL, *id_str = NULL;
5618 struct got_commit_object *commit = NULL;
5621 err = got_object_open_as_commit(&commit, repo, commit_id);
5625 err = get_short_logmsg(&logmsg, 34, commit);
5629 err = got_object_id_str(&id_str, commit_id);
5633 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
5635 err = got_ferror(f, GOT_ERR_IO);
5638 got_object_commit_close(commit);
5644 static const struct got_error *
5645 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
5646 struct got_repository *repo)
5648 const struct got_error *err = NULL;
5649 struct got_object_qid *qid;
5651 if (SIMPLEQ_EMPTY(commits))
5652 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5654 SIMPLEQ_FOREACH(qid, commits, entry) {
5655 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
5664 static const struct got_error *
5665 write_cmd_list(FILE *f)
5667 const struct got_error *err = NULL;
5670 n = fprintf(f, "# Available histedit commands:\n");
5672 return got_ferror(f, GOT_ERR_IO);
5674 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5675 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
5676 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
5679 err = got_ferror(f, GOT_ERR_IO);
5683 n = fprintf(f, "# Commits will be processed in order from top to "
5684 "bottom of this file.\n");
5686 return got_ferror(f, GOT_ERR_IO);
5690 static const struct got_error *
5691 histedit_syntax_error(int lineno)
5693 static char msg[42];
5696 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
5698 if (ret == -1 || ret >= sizeof(msg))
5699 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
5701 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
5704 static const struct got_error *
5705 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
5706 char *logmsg, struct got_repository *repo)
5708 const struct got_error *err;
5709 struct got_commit_object *folded_commit = NULL;
5710 char *id_str, *folded_logmsg = NULL;
5712 err = got_object_id_str(&id_str, hle->commit_id);
5716 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
5720 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
5723 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
5724 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
5725 folded_logmsg) == -1) {
5726 err = got_error_from_errno("asprintf");
5731 got_object_commit_close(folded_commit);
5733 free(folded_logmsg);
5737 static struct got_histedit_list_entry *
5738 get_folded_commits(struct got_histedit_list_entry *hle)
5740 struct got_histedit_list_entry *prev, *folded = NULL;
5742 prev = TAILQ_PREV(hle, got_histedit_list, entry);
5743 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
5744 prev->cmd->code == GOT_HISTEDIT_DROP)) {
5745 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
5747 prev = TAILQ_PREV(prev, got_histedit_list, entry);
5753 static const struct got_error *
5754 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
5755 struct got_repository *repo)
5757 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
5758 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
5759 const struct got_error *err = NULL;
5760 struct got_commit_object *commit = NULL;
5762 struct got_histedit_list_entry *folded = NULL;
5764 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5768 folded = get_folded_commits(hle);
5770 while (folded != hle) {
5771 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
5772 folded = TAILQ_NEXT(folded, entry);
5775 err = append_folded_commit_msg(&new_msg, folded,
5781 folded = TAILQ_NEXT(folded, entry);
5785 err = got_object_id_str(&id_str, hle->commit_id);
5788 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
5791 if (asprintf(&new_msg,
5792 "%s\n# original log message of commit %s: %s",
5793 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
5794 err = got_error_from_errno("asprintf");
5800 err = got_object_id_str(&id_str, hle->commit_id);
5804 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
5808 dprintf(fd, logmsg);
5811 err = get_editor(&editor);
5815 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
5817 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
5819 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
5822 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
5823 err = got_error_from_errno2("unlink", logmsg_path);
5829 got_object_commit_close(commit);
5833 static const struct got_error *
5834 histedit_parse_list(struct got_histedit_list *histedit_cmds,
5835 FILE *f, struct got_repository *repo)
5837 const struct got_error *err = NULL;
5838 char *line = NULL, *p, *end;
5842 const struct got_histedit_cmd *cmd;
5843 struct got_object_id *commit_id = NULL;
5844 struct got_histedit_list_entry *hle = NULL;
5847 len = getline(&line, &size, f);
5849 const struct got_error *getline_err;
5852 getline_err = got_error_from_errno("getline");
5853 err = got_ferror(f, getline_err->code);
5858 while (isspace((unsigned char)p[0]))
5860 if (p[0] == '#' || p[0] == '\0') {
5866 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5867 cmd = &got_histedit_cmds[i];
5868 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
5869 isspace((unsigned char)p[strlen(cmd->name)])) {
5870 p += strlen(cmd->name);
5873 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
5878 if (i == nitems(got_histedit_cmds)) {
5879 err = histedit_syntax_error(lineno);
5882 while (isspace((unsigned char)p[0]))
5884 if (cmd->code == GOT_HISTEDIT_MESG) {
5885 if (hle == NULL || hle->logmsg != NULL) {
5886 err = got_error(GOT_ERR_HISTEDIT_CMD);
5890 err = histedit_edit_logmsg(hle, repo);
5894 hle->logmsg = strdup(p);
5895 if (hle->logmsg == NULL) {
5896 err = got_error_from_errno("strdup");
5905 while (end[0] && !isspace((unsigned char)end[0]))
5909 err = got_object_resolve_id_str(&commit_id, repo, p);
5911 /* override error code */
5912 err = histedit_syntax_error(lineno);
5916 hle = malloc(sizeof(*hle));
5918 err = got_error_from_errno("malloc");
5922 hle->commit_id = commit_id;
5927 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
5935 static const struct got_error *
5936 histedit_check_script(struct got_histedit_list *histedit_cmds,
5937 struct got_object_id_queue *commits, struct got_repository *repo)
5939 const struct got_error *err = NULL;
5940 struct got_object_qid *qid;
5941 struct got_histedit_list_entry *hle;
5942 static char msg[80];
5945 if (TAILQ_EMPTY(histedit_cmds))
5946 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
5947 "histedit script contains no commands");
5948 if (SIMPLEQ_EMPTY(commits))
5949 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5951 SIMPLEQ_FOREACH(qid, commits, entry) {
5952 TAILQ_FOREACH(hle, histedit_cmds, entry) {
5953 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
5957 err = got_object_id_str(&id_str, qid->id);
5960 snprintf(msg, sizeof(msg),
5961 "commit %s missing from histedit script", id_str);
5963 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
5967 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
5968 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
5969 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
5970 "last commit in histedit script cannot be folded");
5975 static const struct got_error *
5976 histedit_run_editor(struct got_histedit_list *histedit_cmds,
5977 const char *path, struct got_object_id_queue *commits,
5978 struct got_repository *repo)
5980 const struct got_error *err = NULL;
5984 err = get_editor(&editor);
5988 if (spawn_editor(editor, path) == -1) {
5989 err = got_error_from_errno("failed spawning editor");
5993 f = fopen(path, "r");
5995 err = got_error_from_errno("fopen");
5998 err = histedit_parse_list(histedit_cmds, f, repo);
6002 err = histedit_check_script(histedit_cmds, commits, repo);
6004 if (f && fclose(f) != 0 && err == NULL)
6005 err = got_error_from_errno("fclose");
6010 static const struct got_error *
6011 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
6012 struct got_object_id_queue *, const char *, struct got_repository *);
6014 static const struct got_error *
6015 histedit_edit_script(struct got_histedit_list *histedit_cmds,
6016 struct got_object_id_queue *commits, struct got_repository *repo)
6018 const struct got_error *err;
6022 err = got_opentemp_named(&path, &f, "got-histedit");
6026 err = write_cmd_list(f);
6030 err = histedit_write_commit_list(commits, f, repo);
6034 if (fclose(f) != 0) {
6035 err = got_error_from_errno("fclose");
6040 err = histedit_run_editor(histedit_cmds, path, commits, repo);
6042 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6043 err->code != GOT_ERR_HISTEDIT_CMD)
6045 err = histedit_edit_list_retry(histedit_cmds, err,
6046 commits, path, repo);
6049 if (f && fclose(f) != 0 && err == NULL)
6050 err = got_error_from_errno("fclose");
6051 if (path && unlink(path) != 0 && err == NULL)
6052 err = got_error_from_errno2("unlink", path);
6057 static const struct got_error *
6058 histedit_save_list(struct got_histedit_list *histedit_cmds,
6059 struct got_worktree *worktree, struct got_repository *repo)
6061 const struct got_error *err = NULL;
6064 struct got_histedit_list_entry *hle;
6065 struct got_commit_object *commit = NULL;
6067 err = got_worktree_get_histedit_script_path(&path, worktree);
6071 f = fopen(path, "w");
6073 err = got_error_from_errno2("fopen", path);
6076 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6077 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6083 int n = fprintf(f, "%c %s\n",
6084 GOT_HISTEDIT_MESG, hle->logmsg);
6086 err = got_ferror(f, GOT_ERR_IO);
6092 if (f && fclose(f) != 0 && err == NULL)
6093 err = got_error_from_errno("fclose");
6096 got_object_commit_close(commit);
6101 histedit_free_list(struct got_histedit_list *histedit_cmds)
6103 struct got_histedit_list_entry *hle;
6105 while ((hle = TAILQ_FIRST(histedit_cmds))) {
6106 TAILQ_REMOVE(histedit_cmds, hle, entry);
6111 static const struct got_error *
6112 histedit_load_list(struct got_histedit_list *histedit_cmds,
6113 const char *path, struct got_repository *repo)
6115 const struct got_error *err = NULL;
6118 f = fopen(path, "r");
6120 err = got_error_from_errno2("fopen", path);
6124 err = histedit_parse_list(histedit_cmds, f, repo);
6126 if (f && fclose(f) != 0 && err == NULL)
6127 err = got_error_from_errno("fclose");
6131 static const struct got_error *
6132 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
6133 const struct got_error *edit_err, struct got_object_id_queue *commits,
6134 const char *path, struct got_repository *repo)
6136 const struct got_error *err = NULL, *prev_err = edit_err;
6139 while (resp != 'c' && resp != 'r' && resp != 'a') {
6140 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
6141 "or (a)bort: ", getprogname(), prev_err->msg);
6146 histedit_free_list(histedit_cmds);
6147 err = histedit_run_editor(histedit_cmds, path, commits,
6150 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6151 err->code != GOT_ERR_HISTEDIT_CMD)
6158 } else if (resp == 'r') {
6159 histedit_free_list(histedit_cmds);
6160 err = histedit_edit_script(histedit_cmds,
6163 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6164 err->code != GOT_ERR_HISTEDIT_CMD)
6171 } else if (resp == 'a') {
6172 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
6175 printf("invalid response '%c'\n", resp);
6181 static const struct got_error *
6182 histedit_complete(struct got_worktree *worktree,
6183 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6184 struct got_reference *branch, struct got_repository *repo)
6186 printf("Switching work tree to %s\n",
6187 got_ref_get_symref_target(branch));
6188 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
6192 static const struct got_error *
6193 show_histedit_progress(struct got_commit_object *commit,
6194 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
6196 const struct got_error *err;
6197 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6199 err = got_object_id_str(&old_id_str, hle->commit_id);
6204 err = got_object_id_str(&new_id_str, new_id);
6209 old_id_str[12] = '\0';
6211 new_id_str[12] = '\0';
6214 logmsg = strdup(hle->logmsg);
6215 if (logmsg == NULL) {
6216 err = got_error_from_errno("strdup");
6219 trim_logmsg(logmsg, 42);
6221 err = get_short_logmsg(&logmsg, 42, commit);
6226 switch (hle->cmd->code) {
6227 case GOT_HISTEDIT_PICK:
6228 case GOT_HISTEDIT_EDIT:
6229 printf("%s -> %s: %s\n", old_id_str,
6230 new_id_str ? new_id_str : "no-op change", logmsg);
6232 case GOT_HISTEDIT_DROP:
6233 case GOT_HISTEDIT_FOLD:
6234 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
6247 static const struct got_error *
6248 histedit_commit(struct got_pathlist_head *merged_paths,
6249 struct got_worktree *worktree, struct got_fileindex *fileindex,
6250 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
6251 struct got_repository *repo)
6253 const struct got_error *err;
6254 struct got_commit_object *commit;
6255 struct got_object_id *new_commit_id;
6257 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
6258 && hle->logmsg == NULL) {
6259 err = histedit_edit_logmsg(hle, repo);
6264 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6268 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
6269 worktree, fileindex, tmp_branch, commit, hle->commit_id,
6272 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
6274 err = show_histedit_progress(commit, hle, NULL);
6276 err = show_histedit_progress(commit, hle, new_commit_id);
6277 free(new_commit_id);
6280 got_object_commit_close(commit);
6284 static const struct got_error *
6285 histedit_skip_commit(struct got_histedit_list_entry *hle,
6286 struct got_worktree *worktree, struct got_repository *repo)
6288 const struct got_error *error;
6289 struct got_commit_object *commit;
6291 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
6296 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
6300 error = show_histedit_progress(commit, hle, NULL);
6301 got_object_commit_close(commit);
6305 static const struct got_error *
6306 cmd_histedit(int argc, char *argv[])
6308 const struct got_error *error = NULL;
6309 struct got_worktree *worktree = NULL;
6310 struct got_fileindex *fileindex = NULL;
6311 struct got_repository *repo = NULL;
6313 struct got_reference *branch = NULL;
6314 struct got_reference *tmp_branch = NULL;
6315 struct got_object_id *resume_commit_id = NULL;
6316 struct got_object_id *base_commit_id = NULL;
6317 struct got_object_id *head_commit_id = NULL;
6318 struct got_commit_object *commit = NULL;
6319 int ch, rebase_in_progress = 0, did_something;
6320 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
6321 const char *edit_script_path = NULL;
6322 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6323 struct got_object_id_queue commits;
6324 struct got_pathlist_head merged_paths;
6325 const struct got_object_id_queue *parent_ids;
6326 struct got_object_qid *pid;
6327 struct got_histedit_list histedit_cmds;
6328 struct got_histedit_list_entry *hle;
6330 SIMPLEQ_INIT(&commits);
6331 TAILQ_INIT(&histedit_cmds);
6332 TAILQ_INIT(&merged_paths);
6334 while ((ch = getopt(argc, argv, "acF:")) != -1) {
6343 edit_script_path = optarg;
6355 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6356 "unveil", NULL) == -1)
6359 if (abort_edit && continue_edit)
6365 * This command cannot apply unveil(2) in all cases because the
6366 * user may choose to run an editor to edit the histedit script
6367 * and to edit individual commit log messages.
6368 * unveil(2) traverses exec(2); if an editor is used we have to
6369 * apply unveil after edit script and log messages have been written.
6370 * XXX TODO: Make use of unveil(2) where possible.
6373 cwd = getcwd(NULL, 0);
6375 error = got_error_from_errno("getcwd");
6378 error = got_worktree_open(&worktree, cwd);
6382 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6387 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6390 if (rebase_in_progress) {
6391 error = got_error(GOT_ERR_REBASING);
6395 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6399 if (edit_in_progress && abort_edit) {
6400 error = got_worktree_histedit_continue(&resume_commit_id,
6401 &tmp_branch, &branch, &base_commit_id, &fileindex,
6405 printf("Switching work tree to %s\n",
6406 got_ref_get_symref_target(branch));
6407 error = got_worktree_histedit_abort(worktree, fileindex, repo,
6408 branch, base_commit_id, update_progress, &did_something);
6411 printf("Histedit of %s aborted\n",
6412 got_ref_get_symref_target(branch));
6413 goto done; /* nothing else to do */
6414 } else if (abort_edit) {
6415 error = got_error(GOT_ERR_NOT_HISTEDIT);
6419 if (continue_edit) {
6422 if (!edit_in_progress) {
6423 error = got_error(GOT_ERR_NOT_HISTEDIT);
6427 error = got_worktree_get_histedit_script_path(&path, worktree);
6431 error = histedit_load_list(&histedit_cmds, path, repo);
6436 error = got_worktree_histedit_continue(&resume_commit_id,
6437 &tmp_branch, &branch, &base_commit_id, &fileindex,
6442 error = got_ref_resolve(&head_commit_id, repo, branch);
6446 error = got_object_open_as_commit(&commit, repo,
6450 parent_ids = got_object_commit_get_parent_ids(commit);
6451 pid = SIMPLEQ_FIRST(parent_ids);
6453 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6456 error = collect_commits(&commits, head_commit_id, pid->id,
6457 base_commit_id, got_worktree_get_path_prefix(worktree),
6458 GOT_ERR_HISTEDIT_PATH, repo);
6459 got_object_commit_close(commit);
6464 if (edit_in_progress) {
6465 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6469 error = got_ref_open(&branch, repo,
6470 got_worktree_get_head_ref_name(worktree), 0);
6474 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
6475 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
6476 "will not edit commit history of a branch outside "
6477 "the \"refs/heads/\" reference namespace");
6481 error = got_ref_resolve(&head_commit_id, repo, branch);
6482 got_ref_close(branch);
6487 error = got_object_open_as_commit(&commit, repo,
6491 parent_ids = got_object_commit_get_parent_ids(commit);
6492 pid = SIMPLEQ_FIRST(parent_ids);
6494 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6497 error = collect_commits(&commits, head_commit_id, pid->id,
6498 got_worktree_get_base_commit_id(worktree),
6499 got_worktree_get_path_prefix(worktree),
6500 GOT_ERR_HISTEDIT_PATH, repo);
6501 got_object_commit_close(commit);
6506 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
6507 &base_commit_id, &fileindex, worktree, repo);
6511 if (edit_script_path) {
6512 error = histedit_load_list(&histedit_cmds,
6513 edit_script_path, repo);
6515 got_worktree_histedit_abort(worktree, fileindex,
6516 repo, branch, base_commit_id,
6517 update_progress, &did_something);
6521 error = histedit_edit_script(&histedit_cmds, &commits,
6524 got_worktree_histedit_abort(worktree, fileindex,
6525 repo, branch, base_commit_id,
6526 update_progress, &did_something);
6532 error = histedit_save_list(&histedit_cmds, worktree,
6535 got_worktree_histedit_abort(worktree, fileindex,
6536 repo, branch, base_commit_id,
6537 update_progress, &did_something);
6543 error = histedit_check_script(&histedit_cmds, &commits, repo);
6547 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
6548 if (resume_commit_id) {
6549 if (got_object_id_cmp(hle->commit_id,
6550 resume_commit_id) != 0)
6553 resume_commit_id = NULL;
6554 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
6555 hle->cmd->code == GOT_HISTEDIT_FOLD) {
6556 error = histedit_skip_commit(hle, worktree,
6559 error = histedit_commit(NULL, worktree,
6560 fileindex, tmp_branch, hle, repo);
6567 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
6568 error = histedit_skip_commit(hle, worktree, repo);
6574 error = got_object_open_as_commit(&commit, repo,
6578 parent_ids = got_object_commit_get_parent_ids(commit);
6579 pid = SIMPLEQ_FIRST(parent_ids);
6581 error = got_worktree_histedit_merge_files(&merged_paths,
6582 worktree, fileindex, pid->id, hle->commit_id, repo,
6583 rebase_progress, &rebase_status, check_cancelled, NULL);
6586 got_object_commit_close(commit);
6589 if (rebase_status == GOT_STATUS_CONFLICT) {
6590 got_worktree_rebase_pathlist_free(&merged_paths);
6594 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
6596 error = got_object_id_str(&id_str, hle->commit_id);
6599 printf("Stopping histedit for amending commit %s\n",
6602 got_worktree_rebase_pathlist_free(&merged_paths);
6603 error = got_worktree_histedit_postpone(worktree,
6608 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
6609 error = histedit_skip_commit(hle, worktree, repo);
6615 error = histedit_commit(&merged_paths, worktree, fileindex,
6616 tmp_branch, hle, repo);
6617 got_worktree_rebase_pathlist_free(&merged_paths);
6622 if (rebase_status == GOT_STATUS_CONFLICT) {
6623 error = got_worktree_histedit_postpone(worktree, fileindex);
6626 error = got_error_msg(GOT_ERR_CONFLICTS,
6627 "conflicts must be resolved before rebasing can continue");
6629 error = histedit_complete(worktree, fileindex, tmp_branch,
6632 got_object_id_queue_free(&commits);
6633 histedit_free_list(&histedit_cmds);
6634 free(head_commit_id);
6635 free(base_commit_id);
6636 free(resume_commit_id);
6638 got_object_commit_close(commit);
6640 got_ref_close(branch);
6642 got_ref_close(tmp_branch);
6644 got_worktree_close(worktree);
6646 got_repo_close(repo);
6651 usage_integrate(void)
6653 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
6657 static const struct got_error *
6658 cmd_integrate(int argc, char *argv[])
6660 const struct got_error *error = NULL;
6661 struct got_repository *repo = NULL;
6662 struct got_worktree *worktree = NULL;
6663 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
6664 const char *branch_arg = NULL;
6665 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
6666 struct got_fileindex *fileindex = NULL;
6667 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
6668 int ch, did_something = 0;
6670 while ((ch = getopt(argc, argv, "")) != -1) {
6683 branch_arg = argv[0];
6685 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6686 "unveil", NULL) == -1)
6689 cwd = getcwd(NULL, 0);
6691 error = got_error_from_errno("getcwd");
6695 error = got_worktree_open(&worktree, cwd);
6699 error = check_rebase_or_histedit_in_progress(worktree);
6703 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6708 error = apply_unveil(got_repo_get_path(repo), 0,
6709 got_worktree_get_root_path(worktree));
6713 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
6714 error = got_error_from_errno("asprintf");
6718 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
6719 &base_branch_ref, worktree, refname, repo);
6723 refname = strdup(got_ref_get_name(branch_ref));
6724 if (refname == NULL) {
6725 error = got_error_from_errno("strdup");
6726 got_worktree_integrate_abort(worktree, fileindex, repo,
6727 branch_ref, base_branch_ref);
6730 base_refname = strdup(got_ref_get_name(base_branch_ref));
6731 if (base_refname == NULL) {
6732 error = got_error_from_errno("strdup");
6733 got_worktree_integrate_abort(worktree, fileindex, repo,
6734 branch_ref, base_branch_ref);
6738 error = got_ref_resolve(&commit_id, repo, branch_ref);
6742 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
6746 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
6747 error = got_error_msg(GOT_ERR_SAME_BRANCH,
6748 "specified branch has already been integrated");
6749 got_worktree_integrate_abort(worktree, fileindex, repo,
6750 branch_ref, base_branch_ref);
6754 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
6756 if (error->code == GOT_ERR_ANCESTRY)
6757 error = got_error(GOT_ERR_REBASE_REQUIRED);
6758 got_worktree_integrate_abort(worktree, fileindex, repo,
6759 branch_ref, base_branch_ref);
6763 error = got_worktree_integrate_continue(worktree, fileindex, repo,
6764 branch_ref, base_branch_ref, update_progress, &did_something,
6765 check_cancelled, NULL);
6769 printf("Integrated %s into %s\n", refname, base_refname);
6772 got_repo_close(repo);
6774 got_worktree_close(worktree);
6776 free(base_commit_id);
6786 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
6787 "[file-path ...]\n",
6792 static const struct got_error *
6793 print_stage(void *arg, unsigned char status, unsigned char staged_status,
6794 const char *path, struct got_object_id *blob_id,
6795 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
6797 const struct got_error *err = NULL;
6798 char *id_str = NULL;
6800 if (staged_status != GOT_STATUS_ADD &&
6801 staged_status != GOT_STATUS_MODIFY &&
6802 staged_status != GOT_STATUS_DELETE)
6805 if (staged_status == GOT_STATUS_ADD ||
6806 staged_status == GOT_STATUS_MODIFY)
6807 err = got_object_id_str(&id_str, staged_blob_id);
6809 err = got_object_id_str(&id_str, blob_id);
6813 printf("%s %c %s\n", id_str, staged_status, path);
6818 static const struct got_error *
6819 cmd_stage(int argc, char *argv[])
6821 const struct got_error *error = NULL;
6822 struct got_repository *repo = NULL;
6823 struct got_worktree *worktree = NULL;
6825 struct got_pathlist_head paths;
6826 struct got_pathlist_entry *pe;
6827 int ch, list_stage = 0, pflag = 0;
6828 FILE *patch_script_file = NULL;
6829 const char *patch_script_path = NULL;
6830 struct choose_patch_arg cpa;
6834 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
6843 patch_script_path = optarg;
6855 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6856 "unveil", NULL) == -1)
6859 if (list_stage && (pflag || patch_script_path))
6860 errx(1, "-l option cannot be used with other options");
6861 if (patch_script_path && !pflag)
6862 errx(1, "-F option can only be used together with -p option");
6864 cwd = getcwd(NULL, 0);
6866 error = got_error_from_errno("getcwd");
6870 error = got_worktree_open(&worktree, cwd);
6874 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6879 if (patch_script_path) {
6880 patch_script_file = fopen(patch_script_path, "r");
6881 if (patch_script_file == NULL) {
6882 error = got_error_from_errno2("fopen",
6887 error = apply_unveil(got_repo_get_path(repo), 0,
6888 got_worktree_get_root_path(worktree));
6892 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6897 error = got_worktree_status(worktree, &paths, repo,
6898 print_stage, NULL, check_cancelled, NULL);
6900 cpa.patch_script_file = patch_script_file;
6901 cpa.action = "stage";
6902 error = got_worktree_stage(worktree, &paths,
6903 pflag ? NULL : print_status, NULL,
6904 pflag ? choose_patch : NULL, &cpa, repo);
6907 if (patch_script_file && fclose(patch_script_file) == EOF &&
6909 error = got_error_from_errno2("fclose", patch_script_path);
6911 got_repo_close(repo);
6913 got_worktree_close(worktree);
6914 TAILQ_FOREACH(pe, &paths, entry)
6915 free((char *)pe->path);
6916 got_pathlist_free(&paths);
6924 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
6925 "[file-path ...]\n",
6931 static const struct got_error *
6932 cmd_unstage(int argc, char *argv[])
6934 const struct got_error *error = NULL;
6935 struct got_repository *repo = NULL;
6936 struct got_worktree *worktree = NULL;
6938 struct got_pathlist_head paths;
6939 struct got_pathlist_entry *pe;
6940 int ch, did_something = 0, pflag = 0;
6941 FILE *patch_script_file = NULL;
6942 const char *patch_script_path = NULL;
6943 struct choose_patch_arg cpa;
6947 while ((ch = getopt(argc, argv, "pF:")) != -1) {
6953 patch_script_path = optarg;
6965 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6966 "unveil", NULL) == -1)
6969 if (patch_script_path && !pflag)
6970 errx(1, "-F option can only be used together with -p option");
6972 cwd = getcwd(NULL, 0);
6974 error = got_error_from_errno("getcwd");
6978 error = got_worktree_open(&worktree, cwd);
6982 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6987 if (patch_script_path) {
6988 patch_script_file = fopen(patch_script_path, "r");
6989 if (patch_script_file == NULL) {
6990 error = got_error_from_errno2("fopen",
6996 error = apply_unveil(got_repo_get_path(repo), 0,
6997 got_worktree_get_root_path(worktree));
7001 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7005 cpa.patch_script_file = patch_script_file;
7006 cpa.action = "unstage";
7007 error = got_worktree_unstage(worktree, &paths, update_progress,
7008 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
7010 if (patch_script_file && fclose(patch_script_file) == EOF &&
7012 error = got_error_from_errno2("fclose", patch_script_path);
7014 got_repo_close(repo);
7016 got_worktree_close(worktree);
7017 TAILQ_FOREACH(pe, &paths, entry)
7018 free((char *)pe->path);
7019 got_pathlist_free(&paths);
7027 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
7028 "arg1 [arg2 ...]\n", getprogname());
7032 static const struct got_error *
7033 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7035 const struct got_error *err;
7036 struct got_blob_object *blob;
7038 err = got_object_open_as_blob(&blob, repo, id, 8192);
7042 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
7043 got_object_blob_close(blob);
7047 static const struct got_error *
7048 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7050 const struct got_error *err;
7051 struct got_tree_object *tree;
7054 err = got_object_open_as_tree(&tree, repo, id);
7058 nentries = got_object_tree_get_nentries(tree);
7059 for (i = 0; i < nentries; i++) {
7060 struct got_tree_entry *te;
7062 if (sigint_received || sigpipe_received)
7064 te = got_object_tree_get_entry(tree, i);
7065 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
7068 fprintf(outfile, "%s %.7o %s\n", id_str,
7069 got_tree_entry_get_mode(te),
7070 got_tree_entry_get_name(te));
7074 got_object_tree_close(tree);
7078 static const struct got_error *
7079 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7081 const struct got_error *err;
7082 struct got_commit_object *commit;
7083 const struct got_object_id_queue *parent_ids;
7084 struct got_object_qid *pid;
7085 char *id_str = NULL;
7086 const char *logmsg = NULL;
7088 err = got_object_open_as_commit(&commit, repo, id);
7092 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
7096 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
7097 parent_ids = got_object_commit_get_parent_ids(commit);
7098 fprintf(outfile, "numparents %d\n",
7099 got_object_commit_get_nparents(commit));
7100 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
7102 err = got_object_id_str(&pid_str, pid->id);
7105 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
7108 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
7109 got_object_commit_get_author(commit),
7110 got_object_commit_get_author_time(commit));
7112 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
7113 got_object_commit_get_author(commit),
7114 got_object_commit_get_committer_time(commit));
7116 logmsg = got_object_commit_get_logmsg_raw(commit);
7117 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
7118 fprintf(outfile, "%s", logmsg);
7121 got_object_commit_close(commit);
7125 static const struct got_error *
7126 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7128 const struct got_error *err;
7129 struct got_tag_object *tag;
7130 char *id_str = NULL;
7131 const char *tagmsg = NULL;
7133 err = got_object_open_as_tag(&tag, repo, id);
7137 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
7141 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
7143 switch (got_object_tag_get_object_type(tag)) {
7144 case GOT_OBJ_TYPE_BLOB:
7145 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7146 GOT_OBJ_LABEL_BLOB);
7148 case GOT_OBJ_TYPE_TREE:
7149 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7150 GOT_OBJ_LABEL_TREE);
7152 case GOT_OBJ_TYPE_COMMIT:
7153 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7154 GOT_OBJ_LABEL_COMMIT);
7156 case GOT_OBJ_TYPE_TAG:
7157 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7164 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
7165 got_object_tag_get_name(tag));
7167 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
7168 got_object_tag_get_tagger(tag),
7169 got_object_tag_get_tagger_time(tag));
7171 tagmsg = got_object_tag_get_message(tag);
7172 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
7173 fprintf(outfile, "%s", tagmsg);
7176 got_object_tag_close(tag);
7180 static const struct got_error *
7181 cmd_cat(int argc, char *argv[])
7183 const struct got_error *error;
7184 struct got_repository *repo = NULL;
7185 struct got_worktree *worktree = NULL;
7186 char *cwd = NULL, *repo_path = NULL, *label = NULL;
7187 const char *commit_id_str = NULL;
7188 struct got_object_id *id = NULL, *commit_id = NULL;
7189 int ch, obj_type, i, force_path = 0;
7192 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7197 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
7200 commit_id_str = optarg;
7203 repo_path = realpath(optarg, NULL);
7204 if (repo_path == NULL)
7205 return got_error_from_errno2("realpath",
7207 got_path_strip_trailing_slashes(repo_path);
7221 cwd = getcwd(NULL, 0);
7223 error = got_error_from_errno("getcwd");
7226 error = got_worktree_open(&worktree, cwd);
7227 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7230 if (repo_path == NULL) {
7232 got_worktree_get_repo_path(worktree));
7233 if (repo_path == NULL) {
7234 error = got_error_from_errno("strdup");
7240 if (repo_path == NULL) {
7241 repo_path = getcwd(NULL, 0);
7242 if (repo_path == NULL)
7243 return got_error_from_errno("getcwd");
7246 error = got_repo_open(&repo, repo_path, NULL);
7251 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7255 if (commit_id_str == NULL)
7256 commit_id_str = GOT_REF_HEAD;
7257 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
7261 for (i = 0; i < argc; i++) {
7263 error = got_object_id_by_path(&id, repo, commit_id,
7268 error = match_object_id(&id, &label, argv[i],
7269 GOT_OBJ_TYPE_ANY, 0, repo);
7271 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
7272 error->code != GOT_ERR_NOT_REF)
7274 error = got_object_id_by_path(&id, repo,
7275 commit_id, argv[i]);
7281 error = got_object_get_type(&obj_type, repo, id);
7286 case GOT_OBJ_TYPE_BLOB:
7287 error = cat_blob(id, repo, stdout);
7289 case GOT_OBJ_TYPE_TREE:
7290 error = cat_tree(id, repo, stdout);
7292 case GOT_OBJ_TYPE_COMMIT:
7293 error = cat_commit(id, repo, stdout);
7295 case GOT_OBJ_TYPE_TAG:
7296 error = cat_tag(id, repo, stdout);
7299 error = got_error(GOT_ERR_OBJ_TYPE);
7315 got_worktree_close(worktree);
7317 const struct got_error *repo_error;
7318 repo_error = got_repo_close(repo);