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 const char *path, int show_patch, const char *search_pattern,
1736 int diff_context, int limit, int first_parent_traversal,
1737 struct got_reflist_head *refs)
1739 const struct got_error *err;
1740 struct got_commit_graph *graph;
1744 if (search_pattern &&
1745 regcomp(®ex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
1746 return got_error_msg(GOT_ERR_REGEX, search_pattern);
1748 err = got_commit_graph_open(&graph, root_id, path,
1749 first_parent_traversal, repo);
1752 err = got_commit_graph_iter_start(graph, root_id, repo,
1753 check_cancelled, NULL);
1757 struct got_commit_object *commit;
1758 struct got_object_id *id;
1760 if (sigint_received || sigpipe_received)
1763 err = got_commit_graph_iter_next(&id, graph);
1765 if (err->code == GOT_ERR_ITER_COMPLETED) {
1769 if (err->code != GOT_ERR_ITER_NEED_MORE)
1771 err = got_commit_graph_fetch_commits(graph, 1, repo,
1772 check_cancelled, NULL);
1781 err = got_object_open_as_commit(&commit, repo, id);
1785 if (search_pattern) {
1786 err = match_logmsg(&have_match, id, commit, ®ex);
1788 got_object_commit_close(commit);
1791 if (have_match == 0) {
1792 got_object_commit_close(commit);
1797 err = print_commit(commit, id, repo, path, show_patch,
1798 diff_context, refs);
1799 got_object_commit_close(commit);
1800 if (err || (limit && --limit == 0))
1806 got_commit_graph_close(graph);
1813 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1814 "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
1819 get_default_log_limit(void)
1821 const char *got_default_log_limit;
1825 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
1826 if (got_default_log_limit == NULL)
1828 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
1834 static const struct got_error *
1835 cmd_log(int argc, char *argv[])
1837 const struct got_error *error;
1838 struct got_repository *repo = NULL;
1839 struct got_worktree *worktree = NULL;
1840 struct got_commit_object *commit = NULL;
1841 struct got_object_id *id = NULL;
1842 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1843 const char *start_commit = NULL, *search_pattern = NULL;
1844 int diff_context = -1, ch;
1845 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1847 struct got_reflist_head refs;
1849 SIMPLEQ_INIT(&refs);
1852 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1858 limit = get_default_log_limit();
1860 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:s:")) != -1) {
1866 start_commit = optarg;
1869 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1872 err(1, "-C option %s", errstr);
1875 limit = strtonum(optarg, 0, INT_MAX, &errstr);
1877 err(1, "-l option %s", errstr);
1880 first_parent_traversal = 1;
1883 repo_path = realpath(optarg, NULL);
1884 if (repo_path == NULL)
1885 return got_error_from_errno2("realpath",
1887 got_path_strip_trailing_slashes(repo_path);
1890 search_pattern = optarg;
1901 if (diff_context == -1)
1903 else if (!show_patch)
1904 errx(1, "-C reguires -p");
1906 cwd = getcwd(NULL, 0);
1908 error = got_error_from_errno("getcwd");
1912 error = got_worktree_open(&worktree, cwd);
1913 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1920 error = got_error_from_errno("strdup");
1923 } else if (argc == 1) {
1925 error = got_worktree_resolve_path(&path, worktree,
1930 path = strdup(argv[0]);
1932 error = got_error_from_errno("strdup");
1939 if (repo_path == NULL) {
1940 repo_path = worktree ?
1941 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1943 if (repo_path == NULL) {
1944 error = got_error_from_errno("strdup");
1948 error = got_repo_open(&repo, repo_path, NULL);
1952 error = apply_unveil(got_repo_get_path(repo), 1,
1953 worktree ? got_worktree_get_root_path(worktree) : NULL);
1957 if (start_commit == NULL) {
1958 struct got_reference *head_ref;
1959 error = got_ref_open(&head_ref, repo,
1960 worktree ? got_worktree_get_head_ref_name(worktree)
1964 error = got_ref_resolve(&id, repo, head_ref);
1965 got_ref_close(head_ref);
1968 error = got_object_open_as_commit(&commit, repo, id);
1970 struct got_reference *ref;
1971 error = got_ref_open(&ref, repo, start_commit, 0);
1972 if (error == NULL) {
1974 error = got_ref_resolve(&id, repo, ref);
1978 error = got_object_get_type(&obj_type, repo, id);
1981 if (obj_type == GOT_OBJ_TYPE_TAG) {
1982 struct got_tag_object *tag;
1983 error = got_object_open_as_tag(&tag, repo, id);
1986 if (got_object_tag_get_object_type(tag) !=
1987 GOT_OBJ_TYPE_COMMIT) {
1988 got_object_tag_close(tag);
1989 error = got_error(GOT_ERR_OBJ_TYPE);
1993 id = got_object_id_dup(
1994 got_object_tag_get_object_id(tag));
1996 error = got_error_from_errno(
1997 "got_object_id_dup");
1998 got_object_tag_close(tag);
2001 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2002 error = got_error(GOT_ERR_OBJ_TYPE);
2005 error = got_object_open_as_commit(&commit, repo, id);
2009 if (commit == NULL) {
2010 error = got_repo_match_object_id_prefix(&id,
2011 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2020 const char *prefix = got_worktree_get_path_prefix(worktree);
2022 if (asprintf(&p, "%s%s%s", prefix,
2023 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2024 error = got_error_from_errno("asprintf");
2027 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2030 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2035 path = in_repo_path;
2038 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2042 error = print_commits(id, repo, path, show_patch, search_pattern,
2043 diff_context, limit, first_parent_traversal, &refs);
2050 got_worktree_close(worktree);
2052 const struct got_error *repo_error;
2053 repo_error = got_repo_close(repo);
2057 got_ref_list_free(&refs);
2064 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2065 "[-w] [object1 object2 | path]\n", getprogname());
2069 struct print_diff_arg {
2070 struct got_repository *repo;
2071 struct got_worktree *worktree;
2076 int ignore_whitespace;
2079 static const struct got_error *
2080 print_diff(void *arg, unsigned char status, unsigned char staged_status,
2081 const char *path, struct got_object_id *blob_id,
2082 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
2084 struct print_diff_arg *a = arg;
2085 const struct got_error *err = NULL;
2086 struct got_blob_object *blob1 = NULL;
2088 char *abspath = NULL, *label1 = NULL;
2091 if (a->diff_staged) {
2092 if (staged_status != GOT_STATUS_MODIFY &&
2093 staged_status != GOT_STATUS_ADD &&
2094 staged_status != GOT_STATUS_DELETE)
2097 if (staged_status == GOT_STATUS_DELETE)
2099 if (status == GOT_STATUS_NONEXISTENT)
2100 return got_error_set_errno(ENOENT, path);
2101 if (status != GOT_STATUS_MODIFY &&
2102 status != GOT_STATUS_ADD &&
2103 status != GOT_STATUS_DELETE &&
2104 status != GOT_STATUS_CONFLICT)
2108 if (!a->header_shown) {
2109 printf("diff %s %s%s\n", a->id_str,
2110 got_worktree_get_root_path(a->worktree),
2111 a->diff_staged ? " (staged changes)" : "");
2112 a->header_shown = 1;
2115 if (a->diff_staged) {
2116 const char *label1 = NULL, *label2 = NULL;
2117 switch (staged_status) {
2118 case GOT_STATUS_MODIFY:
2122 case GOT_STATUS_ADD:
2125 case GOT_STATUS_DELETE:
2129 return got_error(GOT_ERR_FILE_STATUS);
2131 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2132 label1, label2, a->diff_context, a->ignore_whitespace,
2136 if (staged_status == GOT_STATUS_ADD ||
2137 staged_status == GOT_STATUS_MODIFY) {
2139 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2143 err = got_object_id_str(&id_str, staged_blob_id);
2146 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2147 err = got_error_from_errno("asprintf");
2152 } else if (status != GOT_STATUS_ADD) {
2153 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2158 if (status != GOT_STATUS_DELETE) {
2159 if (asprintf(&abspath, "%s/%s",
2160 got_worktree_get_root_path(a->worktree), path) == -1) {
2161 err = got_error_from_errno("asprintf");
2165 f2 = fopen(abspath, "r");
2167 err = got_error_from_errno2("fopen", abspath);
2170 if (lstat(abspath, &sb) == -1) {
2171 err = got_error_from_errno2("lstat", abspath);
2177 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2178 a->diff_context, a->ignore_whitespace, stdout);
2181 got_object_blob_close(blob1);
2182 if (f2 && fclose(f2) != 0 && err == NULL)
2183 err = got_error_from_errno("fclose");
2188 static const struct got_error *
2189 match_object_id(struct got_object_id **id, char **label,
2190 const char *id_str, int obj_type, int resolve_tags,
2191 struct got_repository *repo)
2193 const struct got_error *err;
2194 struct got_tag_object *tag;
2195 struct got_reference *ref = NULL;
2201 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
2204 *id = got_object_id_dup(
2205 got_object_tag_get_object_id(tag));
2207 err = got_error_from_errno("got_object_id_dup");
2208 else if (asprintf(label, "refs/tags/%s",
2209 got_object_tag_get_name(tag)) == -1) {
2210 err = got_error_from_errno("asprintf");
2214 got_object_tag_close(tag);
2216 } else if (err->code != GOT_ERR_NO_OBJ)
2220 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
2222 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2224 err = got_ref_open(&ref, repo, id_str, 0);
2227 *label = strdup(got_ref_get_name(ref));
2228 if (*label == NULL) {
2229 err = got_error_from_errno("strdup");
2232 err = got_ref_resolve(id, repo, ref);
2234 err = got_object_id_str(label, *id);
2235 if (*label == NULL) {
2236 err = got_error_from_errno("strdup");
2247 static const struct got_error *
2248 cmd_diff(int argc, char *argv[])
2250 const struct got_error *error;
2251 struct got_repository *repo = NULL;
2252 struct got_worktree *worktree = NULL;
2253 char *cwd = NULL, *repo_path = NULL;
2254 struct got_object_id *id1 = NULL, *id2 = NULL;
2255 const char *id_str1 = NULL, *id_str2 = NULL;
2256 char *label1 = NULL, *label2 = NULL;
2258 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2263 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2268 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2271 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2274 err(1, "-C option %s", errstr);
2277 repo_path = realpath(optarg, NULL);
2278 if (repo_path == NULL)
2279 return got_error_from_errno2("realpath",
2281 got_path_strip_trailing_slashes(repo_path);
2287 ignore_whitespace = 1;
2298 cwd = getcwd(NULL, 0);
2300 error = got_error_from_errno("getcwd");
2303 error = got_worktree_open(&worktree, cwd);
2304 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2307 if (worktree == NULL) {
2308 error = got_error(GOT_ERR_NOT_WORKTREE);
2313 "-r option can't be used when diffing a work tree");
2314 repo_path = strdup(got_worktree_get_repo_path(worktree));
2315 if (repo_path == NULL) {
2316 error = got_error_from_errno("strdup");
2320 error = got_worktree_resolve_path(&path, worktree,
2327 error = got_error_from_errno("strdup");
2331 } else if (argc == 2) {
2333 errx(1, "-s option can't be used when diffing "
2334 "objects in repository");
2337 if (worktree && repo_path == NULL) {
2339 strdup(got_worktree_get_repo_path(worktree));
2340 if (repo_path == NULL) {
2341 error = got_error_from_errno("strdup");
2348 if (repo_path == NULL) {
2349 repo_path = getcwd(NULL, 0);
2350 if (repo_path == NULL)
2351 return got_error_from_errno("getcwd");
2354 error = got_repo_open(&repo, repo_path, NULL);
2359 error = apply_unveil(got_repo_get_path(repo), 1,
2360 worktree ? got_worktree_get_root_path(worktree) : NULL);
2365 struct print_diff_arg arg;
2366 struct got_pathlist_head paths;
2371 error = got_object_id_str(&id_str,
2372 got_worktree_get_base_commit_id(worktree));
2376 arg.worktree = worktree;
2377 arg.diff_context = diff_context;
2378 arg.id_str = id_str;
2379 arg.header_shown = 0;
2380 arg.diff_staged = diff_staged;
2381 arg.ignore_whitespace = ignore_whitespace;
2383 error = got_pathlist_append(&paths, path, NULL);
2387 error = got_worktree_status(worktree, &paths, repo, print_diff,
2388 &arg, check_cancelled, NULL);
2390 got_pathlist_free(&paths);
2394 error = match_object_id(&id1, &label1, id_str1, GOT_OBJ_TYPE_ANY, 1,
2399 error = match_object_id(&id2, &label2, id_str2, GOT_OBJ_TYPE_ANY, 1,
2404 error = got_object_get_type(&type1, repo, id1);
2408 error = got_object_get_type(&type2, repo, id2);
2412 if (type1 != type2) {
2413 error = got_error(GOT_ERR_OBJ_TYPE);
2418 case GOT_OBJ_TYPE_BLOB:
2419 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2420 diff_context, ignore_whitespace, repo, stdout);
2422 case GOT_OBJ_TYPE_TREE:
2423 error = got_diff_objects_as_trees(id1, id2, "", "",
2424 diff_context, ignore_whitespace, repo, stdout);
2426 case GOT_OBJ_TYPE_COMMIT:
2427 printf("diff %s %s\n", label1, label2);
2428 error = got_diff_objects_as_commits(id1, id2, diff_context,
2429 ignore_whitespace, repo, stdout);
2432 error = got_error(GOT_ERR_OBJ_TYPE);
2442 got_worktree_close(worktree);
2444 const struct got_error *repo_error;
2445 repo_error = got_repo_close(repo);
2456 "usage: %s blame [-c commit] [-r repository-path] path\n",
2465 char datebuf[11]; /* YYYY-MM-DD + NUL */
2468 struct blame_cb_args {
2469 struct blame_line *lines;
2473 off_t *line_offsets;
2475 struct got_repository *repo;
2478 static const struct got_error *
2479 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2481 const struct got_error *err = NULL;
2482 struct blame_cb_args *a = arg;
2483 struct blame_line *bline;
2485 size_t linesize = 0;
2486 struct got_commit_object *commit = NULL;
2489 time_t committer_time;
2491 if (nlines != a->nlines ||
2492 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2493 return got_error(GOT_ERR_RANGE);
2495 if (sigint_received)
2496 return got_error(GOT_ERR_ITER_COMPLETED);
2499 return NULL; /* no change in this commit */
2501 /* Annotate this line. */
2502 bline = &a->lines[lineno - 1];
2503 if (bline->annotated)
2505 err = got_object_id_str(&bline->id_str, id);
2509 err = got_object_open_as_commit(&commit, a->repo, id);
2513 bline->committer = strdup(got_object_commit_get_committer(commit));
2514 if (bline->committer == NULL) {
2515 err = got_error_from_errno("strdup");
2519 committer_time = got_object_commit_get_committer_time(commit);
2520 if (localtime_r(&committer_time, &tm) == NULL)
2521 return got_error_from_errno("localtime_r");
2522 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G/%m/%d",
2523 &tm) >= sizeof(bline->datebuf)) {
2524 err = got_error(GOT_ERR_NO_SPACE);
2527 bline->annotated = 1;
2529 /* Print lines annotated so far. */
2530 bline = &a->lines[a->lineno_cur - 1];
2531 if (!bline->annotated)
2534 offset = a->line_offsets[a->lineno_cur - 1];
2535 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2536 err = got_error_from_errno("fseeko");
2540 while (bline->annotated) {
2541 char *smallerthan, *at, *nl, *committer;
2544 if (getline(&line, &linesize, a->f) == -1) {
2546 err = got_error_from_errno("getline");
2550 committer = bline->committer;
2551 smallerthan = strchr(committer, '<');
2552 if (smallerthan && smallerthan[1] != '\0')
2553 committer = smallerthan + 1;
2554 at = strchr(committer, '@');
2557 len = strlen(committer);
2559 committer[8] = '\0';
2561 nl = strchr(line, '\n');
2564 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2565 bline->id_str, bline->datebuf, committer, line);
2568 bline = &a->lines[a->lineno_cur - 1];
2572 got_object_commit_close(commit);
2577 static const struct got_error *
2578 cmd_blame(int argc, char *argv[])
2580 const struct got_error *error;
2581 struct got_repository *repo = NULL;
2582 struct got_worktree *worktree = NULL;
2583 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2584 struct got_object_id *obj_id = NULL;
2585 struct got_object_id *commit_id = NULL;
2586 struct got_blob_object *blob = NULL;
2587 char *commit_id_str = NULL;
2588 struct blame_cb_args bca;
2589 int ch, obj_type, i;
2592 memset(&bca, 0, sizeof(bca));
2595 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2600 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2603 commit_id_str = optarg;
2606 repo_path = realpath(optarg, NULL);
2607 if (repo_path == NULL)
2608 return got_error_from_errno2("realpath",
2610 got_path_strip_trailing_slashes(repo_path);
2626 cwd = getcwd(NULL, 0);
2628 error = got_error_from_errno("getcwd");
2631 if (repo_path == NULL) {
2632 error = got_worktree_open(&worktree, cwd);
2633 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2639 strdup(got_worktree_get_repo_path(worktree));
2640 if (repo_path == NULL)
2641 error = got_error_from_errno("strdup");
2645 repo_path = strdup(cwd);
2646 if (repo_path == NULL) {
2647 error = got_error_from_errno("strdup");
2653 error = got_repo_open(&repo, repo_path, NULL);
2657 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2662 const char *prefix = got_worktree_get_path_prefix(worktree);
2663 char *p, *worktree_subdir = cwd +
2664 strlen(got_worktree_get_root_path(worktree));
2665 if (asprintf(&p, "%s%s%s%s%s",
2666 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2667 worktree_subdir, worktree_subdir[0] ? "/" : "",
2669 error = got_error_from_errno("asprintf");
2672 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2675 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2680 if (commit_id_str == NULL) {
2681 struct got_reference *head_ref;
2682 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2685 error = got_ref_resolve(&commit_id, repo, head_ref);
2686 got_ref_close(head_ref);
2690 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2695 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2698 if (obj_id == NULL) {
2699 error = got_error(GOT_ERR_NO_OBJ);
2703 error = got_object_get_type(&obj_type, repo, obj_id);
2707 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2708 error = got_error(GOT_ERR_OBJ_TYPE);
2712 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2715 bca.f = got_opentemp();
2716 if (bca.f == NULL) {
2717 error = got_error_from_errno("got_opentemp");
2720 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2721 &bca.line_offsets, bca.f, blob);
2722 if (error || bca.nlines == 0)
2725 /* Don't include \n at EOF in the blame line count. */
2726 if (bca.line_offsets[bca.nlines - 1] == filesize)
2729 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2730 if (bca.lines == NULL) {
2731 error = got_error_from_errno("calloc");
2735 bca.nlines_prec = 0;
2743 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2744 check_cancelled, NULL);
2754 got_object_blob_close(blob);
2756 got_worktree_close(worktree);
2758 const struct got_error *repo_error;
2759 repo_error = got_repo_close(repo);
2764 for (i = 0; i < bca.nlines; i++) {
2765 struct blame_line *bline = &bca.lines[i];
2766 free(bline->id_str);
2767 free(bline->committer);
2771 free(bca.line_offsets);
2772 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2773 error = got_error_from_errno("fclose");
2781 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2787 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2788 const char *root_path)
2790 int is_root_path = (strcmp(path, root_path) == 0);
2791 const char *modestr = "";
2792 mode_t mode = got_tree_entry_get_mode(te);
2794 path += strlen(root_path);
2795 while (path[0] == '/')
2798 if (got_object_tree_entry_is_submodule(te))
2800 else if (S_ISLNK(mode))
2802 else if (S_ISDIR(mode))
2804 else if (mode & S_IXUSR)
2807 printf("%s%s%s%s%s\n", id ? id : "", path,
2808 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
2811 static const struct got_error *
2812 print_tree(const char *path, struct got_object_id *commit_id,
2813 int show_ids, int recurse, const char *root_path,
2814 struct got_repository *repo)
2816 const struct got_error *err = NULL;
2817 struct got_object_id *tree_id = NULL;
2818 struct got_tree_object *tree = NULL;
2821 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2825 err = got_object_open_as_tree(&tree, repo, tree_id);
2828 nentries = got_object_tree_get_nentries(tree);
2829 for (i = 0; i < nentries; i++) {
2830 struct got_tree_entry *te;
2833 if (sigint_received || sigpipe_received)
2836 te = got_object_tree_get_entry(tree, i);
2839 err = got_object_id_str(&id_str,
2840 got_tree_entry_get_id(te));
2843 if (asprintf(&id, "%s ", id_str) == -1) {
2844 err = got_error_from_errno("asprintf");
2850 print_entry(te, id, path, root_path);
2853 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
2855 if (asprintf(&child_path, "%s%s%s", path,
2856 path[0] == '/' && path[1] == '\0' ? "" : "/",
2857 got_tree_entry_get_name(te)) == -1) {
2858 err = got_error_from_errno("asprintf");
2861 err = print_tree(child_path, commit_id, show_ids, 1,
2870 got_object_tree_close(tree);
2875 static const struct got_error *
2876 cmd_tree(int argc, char *argv[])
2878 const struct got_error *error;
2879 struct got_repository *repo = NULL;
2880 struct got_worktree *worktree = NULL;
2882 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2883 struct got_object_id *commit_id = NULL;
2884 char *commit_id_str = NULL;
2885 int show_ids = 0, recurse = 0;
2889 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2894 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2897 commit_id_str = optarg;
2900 repo_path = realpath(optarg, NULL);
2901 if (repo_path == NULL)
2902 return got_error_from_errno2("realpath",
2904 got_path_strip_trailing_slashes(repo_path);
2928 cwd = getcwd(NULL, 0);
2930 error = got_error_from_errno("getcwd");
2933 if (repo_path == NULL) {
2934 error = got_worktree_open(&worktree, cwd);
2935 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2941 strdup(got_worktree_get_repo_path(worktree));
2942 if (repo_path == NULL)
2943 error = got_error_from_errno("strdup");
2947 repo_path = strdup(cwd);
2948 if (repo_path == NULL) {
2949 error = got_error_from_errno("strdup");
2955 error = got_repo_open(&repo, repo_path, NULL);
2959 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2965 char *p, *worktree_subdir = cwd +
2966 strlen(got_worktree_get_root_path(worktree));
2967 if (asprintf(&p, "%s/%s",
2968 got_worktree_get_path_prefix(worktree),
2969 worktree_subdir) == -1) {
2970 error = got_error_from_errno("asprintf");
2973 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2980 if (in_repo_path == NULL) {
2981 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2986 if (commit_id_str == NULL) {
2987 struct got_reference *head_ref;
2988 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2991 error = got_ref_resolve(&commit_id, repo, head_ref);
2992 got_ref_close(head_ref);
2996 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
3001 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
3002 in_repo_path, repo);
3009 got_worktree_close(worktree);
3011 const struct got_error *repo_error;
3012 repo_error = got_repo_close(repo);
3022 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
3026 static const struct got_error *
3027 print_status(void *arg, unsigned char status, unsigned char staged_status,
3028 const char *path, struct got_object_id *blob_id,
3029 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
3031 if (status == staged_status && (status == GOT_STATUS_DELETE))
3032 status = GOT_STATUS_NO_CHANGE;
3033 printf("%c%c %s\n", status, staged_status, path);
3037 static const struct got_error *
3038 cmd_status(int argc, char *argv[])
3040 const struct got_error *error = NULL;
3041 struct got_repository *repo = NULL;
3042 struct got_worktree *worktree = NULL;
3044 struct got_pathlist_head paths;
3045 struct got_pathlist_entry *pe;
3050 while ((ch = getopt(argc, argv, "")) != -1) {
3062 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3066 cwd = getcwd(NULL, 0);
3068 error = got_error_from_errno("getcwd");
3072 error = got_worktree_open(&worktree, cwd);
3076 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3081 error = apply_unveil(got_repo_get_path(repo), 1,
3082 got_worktree_get_root_path(worktree));
3086 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3090 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3091 check_cancelled, NULL);
3093 TAILQ_FOREACH(pe, &paths, entry)
3094 free((char *)pe->path);
3095 got_pathlist_free(&paths);
3104 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3109 static const struct got_error *
3110 list_refs(struct got_repository *repo)
3112 static const struct got_error *err = NULL;
3113 struct got_reflist_head refs;
3114 struct got_reflist_entry *re;
3116 SIMPLEQ_INIT(&refs);
3117 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3121 SIMPLEQ_FOREACH(re, &refs, entry) {
3123 refstr = got_ref_to_str(re->ref);
3125 return got_error_from_errno("got_ref_to_str");
3126 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3130 got_ref_list_free(&refs);
3134 static const struct got_error *
3135 delete_ref(struct got_repository *repo, const char *refname)
3137 const struct got_error *err = NULL;
3138 struct got_reference *ref;
3140 err = got_ref_open(&ref, repo, refname, 0);
3144 err = got_ref_delete(ref, repo);
3149 static const struct got_error *
3150 add_ref(struct got_repository *repo, const char *refname, const char *target)
3152 const struct got_error *err = NULL;
3153 struct got_object_id *id;
3154 struct got_reference *ref = NULL;
3157 * Don't let the user create a reference name with a leading '-'.
3158 * While technically a valid reference name, this case is usually
3159 * an unintended typo.
3161 if (refname[0] == '-')
3162 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3164 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3167 struct got_reference *target_ref;
3169 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3171 err = got_ref_open(&target_ref, repo, target, 0);
3174 err = got_ref_resolve(&id, repo, target_ref);
3175 got_ref_close(target_ref);
3180 err = got_ref_alloc(&ref, refname, id);
3184 err = got_ref_write(ref, repo);
3192 static const struct got_error *
3193 add_symref(struct got_repository *repo, const char *refname, const char *target)
3195 const struct got_error *err = NULL;
3196 struct got_reference *ref = NULL;
3197 struct got_reference *target_ref = NULL;
3200 * Don't let the user create a reference name with a leading '-'.
3201 * While technically a valid reference name, this case is usually
3202 * an unintended typo.
3204 if (refname[0] == '-')
3205 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3207 err = got_ref_open(&target_ref, repo, target, 0);
3211 err = got_ref_alloc_symref(&ref, refname, target_ref);
3215 err = got_ref_write(ref, repo);
3218 got_ref_close(target_ref);
3224 static const struct got_error *
3225 cmd_ref(int argc, char *argv[])
3227 const struct got_error *error = NULL;
3228 struct got_repository *repo = NULL;
3229 struct got_worktree *worktree = NULL;
3230 char *cwd = NULL, *repo_path = NULL;
3231 int ch, do_list = 0, create_symref = 0;
3232 const char *delref = NULL;
3234 /* TODO: Add -s option for adding symbolic references. */
3235 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3241 repo_path = realpath(optarg, NULL);
3242 if (repo_path == NULL)
3243 return got_error_from_errno2("realpath",
3245 got_path_strip_trailing_slashes(repo_path);
3259 if (do_list && delref)
3260 errx(1, "-l and -d options are mutually exclusive\n");
3265 if (do_list || delref) {
3267 errx(1, "-s option cannot be used together with the "
3268 "-l or -d options");
3271 } else if (argc != 2)
3276 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3280 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3281 "sendfd unveil", NULL) == -1)
3285 cwd = getcwd(NULL, 0);
3287 error = got_error_from_errno("getcwd");
3291 if (repo_path == NULL) {
3292 error = got_worktree_open(&worktree, cwd);
3293 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3299 strdup(got_worktree_get_repo_path(worktree));
3300 if (repo_path == NULL)
3301 error = got_error_from_errno("strdup");
3305 repo_path = strdup(cwd);
3306 if (repo_path == NULL) {
3307 error = got_error_from_errno("strdup");
3313 error = got_repo_open(&repo, repo_path, NULL);
3317 error = apply_unveil(got_repo_get_path(repo), do_list,
3318 worktree ? got_worktree_get_root_path(worktree) : NULL);
3323 error = list_refs(repo);
3325 error = delete_ref(repo, delref);
3326 else if (create_symref)
3327 error = add_symref(repo, argv[0], argv[1]);
3329 error = add_ref(repo, argv[0], argv[1]);
3332 got_repo_close(repo);
3334 got_worktree_close(worktree);
3344 "usage: %s branch [-c commit] [-r repository] [-l] | -d name | "
3345 "[name]\n", getprogname());
3349 static const struct got_error *
3350 list_branch(struct got_repository *repo, struct got_worktree *worktree,
3351 struct got_reference *ref)
3353 const struct got_error *err = NULL;
3354 const char *refname, *marker = " ";
3357 refname = got_ref_get_name(ref);
3358 if (worktree && strcmp(refname,
3359 got_worktree_get_head_ref_name(worktree)) == 0) {
3360 struct got_object_id *id = NULL;
3362 err = got_ref_resolve(&id, repo, ref);
3365 if (got_object_id_cmp(id,
3366 got_worktree_get_base_commit_id(worktree)) == 0)
3373 if (strncmp(refname, "refs/heads/", 11) == 0)
3375 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3378 refstr = got_ref_to_str(ref);
3380 return got_error_from_errno("got_ref_to_str");
3382 printf("%s%s: %s\n", marker, refname, refstr);
3387 static const struct got_error *
3388 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
3390 const char *refname;
3392 if (worktree == NULL)
3393 return got_error(GOT_ERR_NOT_WORKTREE);
3395 refname = got_worktree_get_head_ref_name(worktree);
3397 if (strncmp(refname, "refs/heads/", 11) == 0)
3399 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3402 printf("%s\n", refname);
3407 static const struct got_error *
3408 list_branches(struct got_repository *repo, struct got_worktree *worktree)
3410 static const struct got_error *err = NULL;
3411 struct got_reflist_head refs;
3412 struct got_reflist_entry *re;
3413 struct got_reference *temp_ref = NULL;
3414 int rebase_in_progress, histedit_in_progress;
3416 SIMPLEQ_INIT(&refs);
3419 err = got_worktree_rebase_in_progress(&rebase_in_progress,
3424 err = got_worktree_histedit_in_progress(&histedit_in_progress,
3429 if (rebase_in_progress || histedit_in_progress) {
3430 err = got_ref_open(&temp_ref, repo,
3431 got_worktree_get_head_ref_name(worktree), 0);
3434 list_branch(repo, worktree, temp_ref);
3435 got_ref_close(temp_ref);
3439 err = got_ref_list(&refs, repo, "refs/heads",
3440 got_ref_cmp_by_name, NULL);
3444 SIMPLEQ_FOREACH(re, &refs, entry)
3445 list_branch(repo, worktree, re->ref);
3447 got_ref_list_free(&refs);
3451 static const struct got_error *
3452 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
3453 const char *branch_name)
3455 const struct got_error *err = NULL;
3456 struct got_reference *ref = NULL;
3459 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3460 return got_error_from_errno("asprintf");
3462 err = got_ref_open(&ref, repo, refname, 0);
3467 strcmp(got_worktree_get_head_ref_name(worktree),
3468 got_ref_get_name(ref)) == 0) {
3469 err = got_error_msg(GOT_ERR_SAME_BRANCH,
3470 "will not delete this work tree's current branch");
3474 err = got_ref_delete(ref, repo);
3482 static const struct got_error *
3483 add_branch(struct got_repository *repo, const char *branch_name,
3484 struct got_object_id *base_commit_id)
3486 const struct got_error *err = NULL;
3487 struct got_reference *ref = NULL;
3488 char *base_refname = NULL, *refname = NULL;
3491 * Don't let the user create a branch name with a leading '-'.
3492 * While technically a valid reference name, this case is usually
3493 * an unintended typo.
3495 if (branch_name[0] == '-')
3496 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
3498 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3499 err = got_error_from_errno("asprintf");
3503 err = got_ref_open(&ref, repo, refname, 0);
3505 err = got_error(GOT_ERR_BRANCH_EXISTS);
3507 } else if (err->code != GOT_ERR_NOT_REF)
3510 err = got_ref_alloc(&ref, refname, base_commit_id);
3514 err = got_ref_write(ref, repo);
3523 static const struct got_error *
3524 cmd_branch(int argc, char *argv[])
3526 const struct got_error *error = NULL;
3527 struct got_repository *repo = NULL;
3528 struct got_worktree *worktree = NULL;
3529 char *cwd = NULL, *repo_path = NULL;
3530 int ch, do_list = 0, do_show = 0;
3531 const char *delref = NULL, *commit_id_arg = NULL;
3533 while ((ch = getopt(argc, argv, "c:d:r:l")) != -1) {
3536 commit_id_arg = optarg;
3542 repo_path = realpath(optarg, NULL);
3543 if (repo_path == NULL)
3544 return got_error_from_errno2("realpath",
3546 got_path_strip_trailing_slashes(repo_path);
3557 if (do_list && delref)
3558 errx(1, "-l and -d options are mutually exclusive\n");
3563 if (!do_list && !delref && argc == 0)
3566 if ((do_list || delref || do_show) && commit_id_arg != NULL)
3567 errx(1, "-c option can only be used when creating a branch");
3569 if (do_list || delref) {
3572 } else if (!do_show && argc != 1)
3576 if (do_list || do_show) {
3577 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3581 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3582 "sendfd unveil", NULL) == -1)
3586 cwd = getcwd(NULL, 0);
3588 error = got_error_from_errno("getcwd");
3592 if (repo_path == NULL) {
3593 error = got_worktree_open(&worktree, cwd);
3594 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3600 strdup(got_worktree_get_repo_path(worktree));
3601 if (repo_path == NULL)
3602 error = got_error_from_errno("strdup");
3606 repo_path = strdup(cwd);
3607 if (repo_path == NULL) {
3608 error = got_error_from_errno("strdup");
3614 error = got_repo_open(&repo, repo_path, NULL);
3618 error = apply_unveil(got_repo_get_path(repo), do_list,
3619 worktree ? got_worktree_get_root_path(worktree) : NULL);
3624 error = show_current_branch(repo, worktree);
3626 error = list_branches(repo, worktree);
3628 error = delete_branch(repo, worktree, delref);
3630 struct got_object_id *commit_id;
3631 if (commit_id_arg == NULL)
3632 commit_id_arg = worktree ?
3633 got_worktree_get_head_ref_name(worktree) :
3635 error = resolve_commit_arg(&commit_id, commit_id_arg, repo);
3638 error = add_branch(repo, argv[0], commit_id);
3643 got_repo_close(repo);
3645 got_worktree_close(worktree);
3656 "usage: %s tag [-r repository] | -l | "
3657 "[-m message] name [commit]\n", getprogname());
3662 static const struct got_error *
3663 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
3665 const struct got_error *err = NULL;
3666 struct got_reflist_entry *re, *se, *new;
3667 struct got_object_id *re_id, *se_id;
3668 struct got_tag_object *re_tag, *se_tag;
3669 time_t re_time, se_time;
3671 SIMPLEQ_FOREACH(re, tags, entry) {
3672 se = SIMPLEQ_FIRST(sorted);
3674 err = got_reflist_entry_dup(&new, re);
3677 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
3680 err = got_ref_resolve(&re_id, repo, re->ref);
3683 err = got_object_open_as_tag(&re_tag, repo, re_id);
3687 re_time = got_object_tag_get_tagger_time(re_tag);
3688 got_object_tag_close(re_tag);
3692 err = got_ref_resolve(&se_id, repo, re->ref);
3695 err = got_object_open_as_tag(&se_tag, repo, se_id);
3699 se_time = got_object_tag_get_tagger_time(se_tag);
3700 got_object_tag_close(se_tag);
3702 if (se_time > re_time) {
3703 err = got_reflist_entry_dup(&new, re);
3706 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
3709 se = SIMPLEQ_NEXT(se, entry);
3718 static const struct got_error *
3719 cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
3720 struct got_reference *ref2)
3722 const struct got_error *err = NULL;
3723 struct got_repository *repo = arg;
3724 struct got_object_id *id1, *id2 = NULL;
3725 struct got_tag_object *tag1 = NULL, *tag2 = NULL;
3726 time_t time1, time2;
3730 err = got_ref_resolve(&id1, repo, ref1);
3733 err = got_object_open_as_tag(&tag1, repo, id1);
3737 err = got_ref_resolve(&id2, repo, ref2);
3740 err = got_object_open_as_tag(&tag2, repo, id2);
3744 time1 = got_object_tag_get_tagger_time(tag1);
3745 time2 = got_object_tag_get_tagger_time(tag2);
3747 /* Put latest tags first. */
3750 else if (time1 > time2)
3753 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
3758 got_object_tag_close(tag1);
3760 got_object_tag_close(tag2);
3764 static const struct got_error *
3765 list_tags(struct got_repository *repo, struct got_worktree *worktree)
3767 static const struct got_error *err = NULL;
3768 struct got_reflist_head refs;
3769 struct got_reflist_entry *re;
3771 SIMPLEQ_INIT(&refs);
3773 err = got_ref_list(&refs, repo, "refs/tags", cmp_tags, repo);
3777 SIMPLEQ_FOREACH(re, &refs, entry) {
3778 const char *refname;
3779 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
3782 struct got_object_id *id;
3783 struct got_tag_object *tag;
3785 refname = got_ref_get_name(re->ref);
3786 if (strncmp(refname, "refs/tags/", 10) != 0)
3789 refstr = got_ref_to_str(re->ref);
3790 if (refstr == NULL) {
3791 err = got_error_from_errno("got_ref_to_str");
3794 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
3797 err = got_ref_resolve(&id, repo, re->ref);
3800 err = got_object_open_as_tag(&tag, repo, id);
3804 printf("from: %s\n", got_object_tag_get_tagger(tag));
3805 tagger_time = got_object_tag_get_tagger_time(tag);
3806 datestr = get_datestr(&tagger_time, datebuf);
3808 printf("date: %s UTC\n", datestr);
3809 err = got_object_id_str(&id_str,
3810 got_object_tag_get_object_id(tag));
3813 switch (got_object_tag_get_object_type(tag)) {
3814 case GOT_OBJ_TYPE_BLOB:
3815 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB, id_str);
3817 case GOT_OBJ_TYPE_TREE:
3818 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE, id_str);
3820 case GOT_OBJ_TYPE_COMMIT:
3821 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
3823 case GOT_OBJ_TYPE_TAG:
3824 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG, id_str);
3830 tagmsg0 = strdup(got_object_tag_get_message(tag));
3831 got_object_tag_close(tag);
3832 if (tagmsg0 == NULL) {
3833 err = got_error_from_errno("strdup");
3839 line = strsep(&tagmsg, "\n");
3841 printf(" %s\n", line);
3846 got_ref_list_free(&refs);
3850 static const struct got_error *
3851 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
3852 const char *tag_name, const char *repo_path)
3854 const struct got_error *err = NULL;
3855 char *template = NULL, *initial_content = NULL;
3856 char *editor = NULL;
3859 if (asprintf(&template, "/tmp/got-tagmsg") == -1) {
3860 err = got_error_from_errno("asprintf");
3864 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
3865 commit_id_str, tag_name) == -1) {
3866 err = got_error_from_errno("asprintf");
3870 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
3874 dprintf(fd, initial_content);
3877 err = get_editor(&editor);
3880 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
3882 free(initial_content);
3886 /* Editor is done; we can now apply unveil(2) */
3888 err = apply_unveil(repo_path, 0, NULL);
3897 static const struct got_error *
3898 add_tag(struct got_repository *repo, const char *tag_name,
3899 const char *commit_arg, const char *tagmsg_arg)
3901 const struct got_error *err = NULL;
3902 struct got_object_id *commit_id = NULL, *tag_id = NULL;
3903 char *label = NULL, *commit_id_str = NULL;
3904 struct got_reference *ref = NULL;
3905 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
3906 char *tagmsg_path = NULL, *tag_id_str = NULL;
3907 int preserve_tagmsg = 0;
3910 * Don't let the user create a tag name with a leading '-'.
3911 * While technically a valid reference name, this case is usually
3912 * an unintended typo.
3914 if (tag_name[0] == '-')
3915 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
3917 err = get_author(&tagger, repo);
3921 err = match_object_id(&commit_id, &label, commit_arg,
3922 GOT_OBJ_TYPE_COMMIT, 1, repo);
3926 err = got_object_id_str(&commit_id_str, commit_id);
3930 if (strncmp("refs/tags/", tag_name, 10) == 0) {
3931 refname = strdup(tag_name);
3932 if (refname == NULL) {
3933 err = got_error_from_errno("strdup");
3937 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
3938 err = got_error_from_errno("asprintf");
3942 err = got_ref_open(&ref, repo, refname, 0);
3944 err = got_error(GOT_ERR_TAG_EXISTS);
3946 } else if (err->code != GOT_ERR_NOT_REF)
3949 if (tagmsg_arg == NULL) {
3950 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
3951 tag_name, got_repo_get_path(repo));
3953 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
3954 tagmsg_path != NULL)
3955 preserve_tagmsg = 1;
3960 err = got_object_tag_create(&tag_id, tag_name, commit_id,
3961 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
3964 preserve_tagmsg = 1;
3968 err = got_ref_alloc(&ref, refname, tag_id);
3971 preserve_tagmsg = 1;
3975 err = got_ref_write(ref, repo);
3978 preserve_tagmsg = 1;
3982 err = got_object_id_str(&tag_id_str, tag_id);
3985 preserve_tagmsg = 1;
3988 printf("Created tag %s\n", tag_id_str);
3990 if (preserve_tagmsg) {
3991 fprintf(stderr, "%s: tag message preserved in %s\n",
3992 getprogname(), tagmsg_path);
3993 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
3994 err = got_error_from_errno2("unlink", tagmsg_path);
3999 free(commit_id_str);
4007 static const struct got_error *
4008 cmd_tag(int argc, char *argv[])
4010 const struct got_error *error = NULL;
4011 struct got_repository *repo = NULL;
4012 struct got_worktree *worktree = NULL;
4013 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
4014 char *gitconfig_path = NULL;
4015 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
4016 int ch, do_list = 0;
4018 while ((ch = getopt(argc, argv, "m:r:l")) != -1) {
4024 repo_path = realpath(optarg, NULL);
4025 if (repo_path == NULL)
4026 return got_error_from_errno2("realpath",
4028 got_path_strip_trailing_slashes(repo_path);
4044 errx(1, "-l and -m options are mutually exclusive\n");
4047 } else if (argc < 1 || argc > 2)
4050 commit_id_arg = argv[1];
4055 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4059 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4060 "sendfd unveil", NULL) == -1)
4064 cwd = getcwd(NULL, 0);
4066 error = got_error_from_errno("getcwd");
4070 if (repo_path == NULL) {
4071 error = got_worktree_open(&worktree, cwd);
4072 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4078 strdup(got_worktree_get_repo_path(worktree));
4079 if (repo_path == NULL)
4080 error = got_error_from_errno("strdup");
4084 repo_path = strdup(cwd);
4085 if (repo_path == NULL) {
4086 error = got_error_from_errno("strdup");
4093 error = got_repo_open(&repo, repo_path, NULL);
4096 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4099 error = list_tags(repo, worktree);
4101 error = get_gitconfig_path(&gitconfig_path);
4104 error = got_repo_open(&repo, repo_path, gitconfig_path);
4109 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4114 if (commit_id_arg == NULL) {
4115 struct got_reference *head_ref;
4116 struct got_object_id *commit_id;
4117 error = got_ref_open(&head_ref, repo,
4118 worktree ? got_worktree_get_head_ref_name(worktree)
4122 error = got_ref_resolve(&commit_id, repo, head_ref);
4123 got_ref_close(head_ref);
4126 error = got_object_id_str(&commit_id_str, commit_id);
4132 error = add_tag(repo, tag_name,
4133 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4137 got_repo_close(repo);
4139 got_worktree_close(worktree);
4142 free(gitconfig_path);
4143 free(commit_id_str);
4150 fprintf(stderr, "usage: %s add [-R] [-I] file-path ...\n",
4155 static const struct got_error *
4156 add_progress(void *arg, unsigned char status, const char *path)
4158 while (path[0] == '/')
4160 printf("%c %s\n", status, path);
4164 static const struct got_error *
4165 cmd_add(int argc, char *argv[])
4167 const struct got_error *error = NULL;
4168 struct got_repository *repo = NULL;
4169 struct got_worktree *worktree = NULL;
4171 struct got_pathlist_head paths;
4172 struct got_pathlist_entry *pe;
4173 int ch, can_recurse = 0, no_ignores = 0;
4177 while ((ch = getopt(argc, argv, "IR")) != -1) {
4195 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4202 cwd = getcwd(NULL, 0);
4204 error = got_error_from_errno("getcwd");
4208 error = got_worktree_open(&worktree, cwd);
4212 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4217 error = apply_unveil(got_repo_get_path(repo), 1,
4218 got_worktree_get_root_path(worktree));
4222 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4226 if (!can_recurse && no_ignores) {
4227 error = got_error_msg(GOT_ERR_BAD_PATH,
4228 "disregarding ignores requires -R option");
4236 TAILQ_FOREACH(pe, &paths, entry) {
4237 if (asprintf(&ondisk_path, "%s/%s",
4238 got_worktree_get_root_path(worktree),
4240 error = got_error_from_errno("asprintf");
4243 if (lstat(ondisk_path, &sb) == -1) {
4244 if (errno == ENOENT) {
4248 error = got_error_from_errno2("lstat",
4254 if (S_ISDIR(sb.st_mode)) {
4255 error = got_error_msg(GOT_ERR_BAD_PATH,
4256 "adding directories requires -R option");
4262 error = got_worktree_schedule_add(worktree, &paths, add_progress,
4263 NULL, repo, no_ignores);
4266 got_repo_close(repo);
4268 got_worktree_close(worktree);
4269 TAILQ_FOREACH(pe, &paths, entry)
4270 free((char *)pe->path);
4271 got_pathlist_free(&paths);
4279 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
4283 static const struct got_error *
4284 print_remove_status(void *arg, unsigned char status,
4285 unsigned char staged_status, const char *path,
4286 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4287 struct got_object_id *commit_id)
4289 if (status == GOT_STATUS_NONEXISTENT)
4291 if (status == staged_status && (status == GOT_STATUS_DELETE))
4292 status = GOT_STATUS_NO_CHANGE;
4293 printf("%c%c %s\n", status, staged_status, path);
4297 static const struct got_error *
4298 cmd_remove(int argc, char *argv[])
4300 const struct got_error *error = NULL;
4301 struct got_worktree *worktree = NULL;
4302 struct got_repository *repo = NULL;
4304 struct got_pathlist_head paths;
4305 struct got_pathlist_entry *pe;
4306 int ch, delete_local_mods = 0;
4310 while ((ch = getopt(argc, argv, "f")) != -1) {
4313 delete_local_mods = 1;
4325 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4332 cwd = getcwd(NULL, 0);
4334 error = got_error_from_errno("getcwd");
4337 error = got_worktree_open(&worktree, cwd);
4341 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4346 error = apply_unveil(got_repo_get_path(repo), 1,
4347 got_worktree_get_root_path(worktree));
4351 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4355 error = got_worktree_schedule_delete(worktree, &paths,
4356 delete_local_mods, print_remove_status, NULL, repo);
4361 got_repo_close(repo);
4363 got_worktree_close(worktree);
4364 TAILQ_FOREACH(pe, &paths, entry)
4365 free((char *)pe->path);
4366 got_pathlist_free(&paths);
4374 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4375 "path ...\n", getprogname());
4379 static const struct got_error *
4380 revert_progress(void *arg, unsigned char status, const char *path)
4382 while (path[0] == '/')
4384 printf("%c %s\n", status, path);
4388 struct choose_patch_arg {
4389 FILE *patch_script_file;
4393 static const struct got_error *
4394 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4395 int nchanges, const char *action)
4398 size_t linesize = 0;
4402 case GOT_STATUS_ADD:
4403 printf("A %s\n%s this addition? [y/n] ", path, action);
4405 case GOT_STATUS_DELETE:
4406 printf("D %s\n%s this deletion? [y/n] ", path, action);
4408 case GOT_STATUS_MODIFY:
4409 if (fseek(patch_file, 0L, SEEK_SET) == -1)
4410 return got_error_from_errno("fseek");
4411 printf(GOT_COMMIT_SEP_STR);
4412 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4414 if (ferror(patch_file))
4415 return got_error_from_errno("getline");
4416 printf(GOT_COMMIT_SEP_STR);
4417 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4418 path, n, nchanges, action);
4421 return got_error_path(path, GOT_ERR_FILE_STATUS);
4427 static const struct got_error *
4428 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4429 FILE *patch_file, int n, int nchanges)
4431 const struct got_error *err = NULL;
4433 size_t linesize = 0;
4436 struct choose_patch_arg *a = arg;
4438 *choice = GOT_PATCH_CHOICE_NONE;
4440 if (a->patch_script_file) {
4442 err = show_change(status, path, patch_file, n, nchanges,
4446 linelen = getline(&line, &linesize, a->patch_script_file);
4447 if (linelen == -1) {
4448 if (ferror(a->patch_script_file))
4449 return got_error_from_errno("getline");
4452 nl = strchr(line, '\n');
4455 if (strcmp(line, "y") == 0) {
4456 *choice = GOT_PATCH_CHOICE_YES;
4458 } else if (strcmp(line, "n") == 0) {
4459 *choice = GOT_PATCH_CHOICE_NO;
4461 } else if (strcmp(line, "q") == 0 &&
4462 status == GOT_STATUS_MODIFY) {
4463 *choice = GOT_PATCH_CHOICE_QUIT;
4466 printf("invalid response '%s'\n", line);
4471 while (resp != 'y' && resp != 'n' && resp != 'q') {
4472 err = show_change(status, path, patch_file, n, nchanges,
4479 if (status == GOT_STATUS_MODIFY) {
4480 if (resp != 'y' && resp != 'n' && resp != 'q') {
4481 printf("invalid response '%c'\n", resp);
4484 } else if (resp != 'y' && resp != 'n') {
4485 printf("invalid response '%c'\n", resp);
4491 *choice = GOT_PATCH_CHOICE_YES;
4492 else if (resp == 'n')
4493 *choice = GOT_PATCH_CHOICE_NO;
4494 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4495 *choice = GOT_PATCH_CHOICE_QUIT;
4501 static const struct got_error *
4502 cmd_revert(int argc, char *argv[])
4504 const struct got_error *error = NULL;
4505 struct got_worktree *worktree = NULL;
4506 struct got_repository *repo = NULL;
4507 char *cwd = NULL, *path = NULL;
4508 struct got_pathlist_head paths;
4509 struct got_pathlist_entry *pe;
4510 int ch, can_recurse = 0, pflag = 0;
4511 FILE *patch_script_file = NULL;
4512 const char *patch_script_path = NULL;
4513 struct choose_patch_arg cpa;
4517 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4523 patch_script_path = optarg;
4538 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4539 "unveil", NULL) == -1)
4544 if (patch_script_path && !pflag)
4545 errx(1, "-F option can only be used together with -p option");
4547 cwd = getcwd(NULL, 0);
4549 error = got_error_from_errno("getcwd");
4552 error = got_worktree_open(&worktree, cwd);
4556 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4561 if (patch_script_path) {
4562 patch_script_file = fopen(patch_script_path, "r");
4563 if (patch_script_file == NULL) {
4564 error = got_error_from_errno2("fopen",
4569 error = apply_unveil(got_repo_get_path(repo), 1,
4570 got_worktree_get_root_path(worktree));
4574 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4581 TAILQ_FOREACH(pe, &paths, entry) {
4582 if (asprintf(&ondisk_path, "%s/%s",
4583 got_worktree_get_root_path(worktree),
4585 error = got_error_from_errno("asprintf");
4588 if (lstat(ondisk_path, &sb) == -1) {
4589 if (errno == ENOENT) {
4593 error = got_error_from_errno2("lstat",
4599 if (S_ISDIR(sb.st_mode)) {
4600 error = got_error_msg(GOT_ERR_BAD_PATH,
4601 "reverting directories requires -R option");
4607 cpa.patch_script_file = patch_script_file;
4608 cpa.action = "revert";
4609 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4610 pflag ? choose_patch : NULL, &cpa, repo);
4614 if (patch_script_file && fclose(patch_script_file) == EOF &&
4616 error = got_error_from_errno2("fclose", patch_script_path);
4618 got_repo_close(repo);
4620 got_worktree_close(worktree);
4629 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4634 struct collect_commit_logmsg_arg {
4635 const char *cmdline_log;
4637 const char *worktree_path;
4638 const char *branch_name;
4639 const char *repo_path;
4644 static const struct got_error *
4645 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4648 char *initial_content = NULL;
4649 struct got_pathlist_entry *pe;
4650 const struct got_error *err = NULL;
4651 char *template = NULL;
4652 struct collect_commit_logmsg_arg *a = arg;
4656 /* if a message was specified on the command line, just use it */
4657 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4658 len = strlen(a->cmdline_log) + 1;
4659 *logmsg = malloc(len + 1);
4660 if (*logmsg == NULL)
4661 return got_error_from_errno("malloc");
4662 strlcpy(*logmsg, a->cmdline_log, len);
4666 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
4667 return got_error_from_errno("asprintf");
4669 if (asprintf(&initial_content,
4670 "\n# changes to be committed on branch %s:\n",
4671 a->branch_name) == -1)
4672 return got_error_from_errno("asprintf");
4674 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
4678 dprintf(fd, initial_content);
4680 TAILQ_FOREACH(pe, commitable_paths, entry) {
4681 struct got_commitable *ct = pe->data;
4682 dprintf(fd, "# %c %s\n",
4683 got_commitable_get_status(ct),
4684 got_commitable_get_path(ct));
4688 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
4690 free(initial_content);
4693 /* Editor is done; we can now apply unveil(2) */
4695 err = apply_unveil(a->repo_path, 0, a->worktree_path);
4704 static const struct got_error *
4705 cmd_commit(int argc, char *argv[])
4707 const struct got_error *error = NULL;
4708 struct got_worktree *worktree = NULL;
4709 struct got_repository *repo = NULL;
4710 char *cwd = NULL, *id_str = NULL;
4711 struct got_object_id *id = NULL;
4712 const char *logmsg = NULL;
4713 struct collect_commit_logmsg_arg cl_arg;
4714 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
4715 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
4716 struct got_pathlist_head paths;
4719 cl_arg.logmsg_path = NULL;
4721 while ((ch = getopt(argc, argv, "m:")) != -1) {
4736 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4737 "unveil", NULL) == -1)
4740 cwd = getcwd(NULL, 0);
4742 error = got_error_from_errno("getcwd");
4745 error = got_worktree_open(&worktree, cwd);
4749 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4752 if (rebase_in_progress) {
4753 error = got_error(GOT_ERR_REBASING);
4757 error = got_worktree_histedit_in_progress(&histedit_in_progress,
4762 error = get_gitconfig_path(&gitconfig_path);
4765 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4770 error = get_author(&author, repo);
4775 * unveil(2) traverses exec(2); if an editor is used we have
4776 * to apply unveil after the log message has been written.
4778 if (logmsg == NULL || strlen(logmsg) == 0)
4779 error = get_editor(&editor);
4781 error = apply_unveil(got_repo_get_path(repo), 0,
4782 got_worktree_get_root_path(worktree));
4786 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4790 cl_arg.editor = editor;
4791 cl_arg.cmdline_log = logmsg;
4792 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
4793 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
4794 if (!histedit_in_progress) {
4795 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
4796 error = got_error(GOT_ERR_COMMIT_BRANCH);
4799 cl_arg.branch_name += 11;
4801 cl_arg.repo_path = got_repo_get_path(repo);
4802 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
4803 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
4805 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4806 cl_arg.logmsg_path != NULL)
4807 preserve_logmsg = 1;
4811 error = got_object_id_str(&id_str, id);
4814 printf("Created commit %s\n", id_str);
4816 if (preserve_logmsg) {
4817 fprintf(stderr, "%s: log message preserved in %s\n",
4818 getprogname(), cl_arg.logmsg_path);
4819 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
4821 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
4822 free(cl_arg.logmsg_path);
4824 got_repo_close(repo);
4826 got_worktree_close(worktree);
4829 free(gitconfig_path);
4836 usage_cherrypick(void)
4838 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
4842 static const struct got_error *
4843 cmd_cherrypick(int argc, char *argv[])
4845 const struct got_error *error = NULL;
4846 struct got_worktree *worktree = NULL;
4847 struct got_repository *repo = NULL;
4848 char *cwd = NULL, *commit_id_str = NULL;
4849 struct got_object_id *commit_id = NULL;
4850 struct got_commit_object *commit = NULL;
4851 struct got_object_qid *pid;
4852 struct got_reference *head_ref = NULL;
4853 int ch, did_something = 0;
4855 while ((ch = getopt(argc, argv, "")) != -1) {
4867 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4868 "unveil", NULL) == -1)
4874 cwd = getcwd(NULL, 0);
4876 error = got_error_from_errno("getcwd");
4879 error = got_worktree_open(&worktree, cwd);
4883 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4888 error = apply_unveil(got_repo_get_path(repo), 0,
4889 got_worktree_get_root_path(worktree));
4893 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4894 GOT_OBJ_TYPE_COMMIT, repo);
4895 if (error != NULL) {
4896 struct got_reference *ref;
4897 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4899 error = got_ref_open(&ref, repo, argv[0], 0);
4902 error = got_ref_resolve(&commit_id, repo, ref);
4907 error = got_object_id_str(&commit_id_str, commit_id);
4911 error = got_ref_open(&head_ref, repo,
4912 got_worktree_get_head_ref_name(worktree), 0);
4916 error = check_same_branch(commit_id, head_ref, NULL, repo);
4918 if (error->code != GOT_ERR_ANCESTRY)
4922 error = got_error(GOT_ERR_SAME_BRANCH);
4926 error = got_object_open_as_commit(&commit, repo, commit_id);
4929 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4930 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
4931 commit_id, repo, update_progress, &did_something, check_cancelled,
4937 printf("Merged commit %s\n", commit_id_str);
4940 got_object_commit_close(commit);
4941 free(commit_id_str);
4943 got_ref_close(head_ref);
4945 got_worktree_close(worktree);
4947 got_repo_close(repo);
4954 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
4958 static const struct got_error *
4959 cmd_backout(int argc, char *argv[])
4961 const struct got_error *error = NULL;
4962 struct got_worktree *worktree = NULL;
4963 struct got_repository *repo = NULL;
4964 char *cwd = NULL, *commit_id_str = NULL;
4965 struct got_object_id *commit_id = NULL;
4966 struct got_commit_object *commit = NULL;
4967 struct got_object_qid *pid;
4968 struct got_reference *head_ref = NULL;
4969 int ch, did_something = 0;
4971 while ((ch = getopt(argc, argv, "")) != -1) {
4983 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4984 "unveil", NULL) == -1)
4990 cwd = getcwd(NULL, 0);
4992 error = got_error_from_errno("getcwd");
4995 error = got_worktree_open(&worktree, cwd);
4999 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5004 error = apply_unveil(got_repo_get_path(repo), 0,
5005 got_worktree_get_root_path(worktree));
5009 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5010 GOT_OBJ_TYPE_COMMIT, repo);
5011 if (error != NULL) {
5012 struct got_reference *ref;
5013 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5015 error = got_ref_open(&ref, repo, argv[0], 0);
5018 error = got_ref_resolve(&commit_id, repo, ref);
5023 error = got_object_id_str(&commit_id_str, commit_id);
5027 error = got_ref_open(&head_ref, repo,
5028 got_worktree_get_head_ref_name(worktree), 0);
5032 error = check_same_branch(commit_id, head_ref, NULL, repo);
5036 error = got_object_open_as_commit(&commit, repo, commit_id);
5039 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5041 error = got_error(GOT_ERR_ROOT_COMMIT);
5045 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
5046 update_progress, &did_something, check_cancelled, NULL);
5051 printf("Backed out commit %s\n", commit_id_str);
5054 got_object_commit_close(commit);
5055 free(commit_id_str);
5057 got_ref_close(head_ref);
5059 got_worktree_close(worktree);
5061 got_repo_close(repo);
5068 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
5074 trim_logmsg(char *logmsg, int limit)
5079 len = strlen(logmsg);
5083 nl = strchr(logmsg, '\n');
5088 static const struct got_error *
5089 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5091 const struct got_error *err;
5092 char *logmsg0 = NULL;
5095 err = got_object_commit_get_logmsg(&logmsg0, commit);
5100 while (isspace((unsigned char)s[0]))
5103 *logmsg = strdup(s);
5104 if (*logmsg == NULL) {
5105 err = got_error_from_errno("strdup");
5109 trim_logmsg(*logmsg, limit);
5115 static const struct got_error *
5116 show_rebase_progress(struct got_commit_object *commit,
5117 struct got_object_id *old_id, struct got_object_id *new_id)
5119 const struct got_error *err;
5120 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5122 err = got_object_id_str(&old_id_str, old_id);
5127 err = got_object_id_str(&new_id_str, new_id);
5132 old_id_str[12] = '\0';
5134 new_id_str[12] = '\0';
5136 err = get_short_logmsg(&logmsg, 42, commit);
5140 printf("%s -> %s: %s\n", old_id_str,
5141 new_id_str ? new_id_str : "no-op change", logmsg);
5148 static const struct got_error *
5149 rebase_progress(void *arg, unsigned char status, const char *path)
5151 unsigned char *rebase_status = arg;
5153 while (path[0] == '/')
5155 printf("%c %s\n", status, path);
5157 if (*rebase_status == GOT_STATUS_CONFLICT)
5159 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5160 *rebase_status = status;
5164 static const struct got_error *
5165 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5166 struct got_reference *branch, struct got_reference *new_base_branch,
5167 struct got_reference *tmp_branch, struct got_repository *repo)
5169 printf("Switching work tree to %s\n", got_ref_get_name(branch));
5170 return got_worktree_rebase_complete(worktree, fileindex,
5171 new_base_branch, tmp_branch, branch, repo);
5174 static const struct got_error *
5175 rebase_commit(struct got_pathlist_head *merged_paths,
5176 struct got_worktree *worktree, struct got_fileindex *fileindex,
5177 struct got_reference *tmp_branch,
5178 struct got_object_id *commit_id, struct got_repository *repo)
5180 const struct got_error *error;
5181 struct got_commit_object *commit;
5182 struct got_object_id *new_commit_id;
5184 error = got_object_open_as_commit(&commit, repo, commit_id);
5188 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5189 worktree, fileindex, tmp_branch, commit, commit_id, repo);
5191 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5193 error = show_rebase_progress(commit, commit_id, NULL);
5195 error = show_rebase_progress(commit, commit_id, new_commit_id);
5196 free(new_commit_id);
5199 got_object_commit_close(commit);
5203 struct check_path_prefix_arg {
5204 const char *path_prefix;
5209 static const struct got_error *
5210 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5211 struct got_blob_object *blob2, struct got_object_id *id1,
5212 struct got_object_id *id2, const char *path1, const char *path2,
5213 mode_t mode1, mode_t mode2, struct got_repository *repo)
5215 struct check_path_prefix_arg *a = arg;
5217 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5218 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5219 return got_error(a->errcode);
5224 static const struct got_error *
5225 check_path_prefix(struct got_object_id *parent_id,
5226 struct got_object_id *commit_id, const char *path_prefix,
5227 int errcode, struct got_repository *repo)
5229 const struct got_error *err;
5230 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
5231 struct got_commit_object *commit = NULL, *parent_commit = NULL;
5232 struct check_path_prefix_arg cpp_arg;
5234 if (got_path_is_root_dir(path_prefix))
5237 err = got_object_open_as_commit(&commit, repo, commit_id);
5241 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
5245 err = got_object_open_as_tree(&tree1, repo,
5246 got_object_commit_get_tree_id(parent_commit));
5250 err = got_object_open_as_tree(&tree2, repo,
5251 got_object_commit_get_tree_id(commit));
5255 cpp_arg.path_prefix = path_prefix;
5256 while (cpp_arg.path_prefix[0] == '/')
5257 cpp_arg.path_prefix++;
5258 cpp_arg.len = strlen(cpp_arg.path_prefix);
5259 cpp_arg.errcode = errcode;
5260 err = got_diff_tree(tree1, tree2, "", "", repo,
5261 check_path_prefix_in_diff, &cpp_arg, 0);
5264 got_object_tree_close(tree1);
5266 got_object_tree_close(tree2);
5268 got_object_commit_close(commit);
5270 got_object_commit_close(parent_commit);
5274 static const struct got_error *
5275 collect_commits(struct got_object_id_queue *commits,
5276 struct got_object_id *initial_commit_id,
5277 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
5278 const char *path_prefix, int path_prefix_errcode,
5279 struct got_repository *repo)
5281 const struct got_error *err = NULL;
5282 struct got_commit_graph *graph = NULL;
5283 struct got_object_id *parent_id = NULL;
5284 struct got_object_qid *qid;
5285 struct got_object_id *commit_id = initial_commit_id;
5287 err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
5291 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
5292 check_cancelled, NULL);
5295 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
5296 err = got_commit_graph_iter_next(&parent_id, graph);
5298 if (err->code == GOT_ERR_ITER_COMPLETED) {
5299 err = got_error_msg(GOT_ERR_ANCESTRY,
5300 "ran out of commits to rebase before "
5301 "youngest common ancestor commit has "
5304 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
5306 err = got_commit_graph_fetch_commits(graph, 1, repo,
5307 check_cancelled, NULL);
5311 err = check_path_prefix(parent_id, commit_id,
5312 path_prefix, path_prefix_errcode, repo);
5316 err = got_object_qid_alloc(&qid, commit_id);
5319 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
5320 commit_id = parent_id;
5324 got_commit_graph_close(graph);
5328 static const struct got_error *
5329 cmd_rebase(int argc, char *argv[])
5331 const struct got_error *error = NULL;
5332 struct got_worktree *worktree = NULL;
5333 struct got_repository *repo = NULL;
5334 struct got_fileindex *fileindex = NULL;
5336 struct got_reference *branch = NULL;
5337 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
5338 struct got_object_id *commit_id = NULL, *parent_id = NULL;
5339 struct got_object_id *resume_commit_id = NULL;
5340 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
5341 struct got_commit_object *commit = NULL;
5342 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
5343 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5344 struct got_object_id_queue commits;
5345 struct got_pathlist_head merged_paths;
5346 const struct got_object_id_queue *parent_ids;
5347 struct got_object_qid *qid, *pid;
5349 SIMPLEQ_INIT(&commits);
5350 TAILQ_INIT(&merged_paths);
5352 while ((ch = getopt(argc, argv, "ac")) != -1) {
5358 continue_rebase = 1;
5370 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5371 "unveil", NULL) == -1)
5374 if (abort_rebase && continue_rebase)
5376 else if (abort_rebase || continue_rebase) {
5379 } else if (argc != 1)
5382 cwd = getcwd(NULL, 0);
5384 error = got_error_from_errno("getcwd");
5387 error = got_worktree_open(&worktree, cwd);
5391 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5396 error = apply_unveil(got_repo_get_path(repo), 0,
5397 got_worktree_get_root_path(worktree));
5401 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5407 if (!rebase_in_progress) {
5408 error = got_error(GOT_ERR_NOT_REBASING);
5411 error = got_worktree_rebase_continue(&resume_commit_id,
5412 &new_base_branch, &tmp_branch, &branch, &fileindex,
5416 printf("Switching work tree to %s\n",
5417 got_ref_get_symref_target(new_base_branch));
5418 error = got_worktree_rebase_abort(worktree, fileindex, repo,
5419 new_base_branch, update_progress, &did_something);
5422 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5423 goto done; /* nothing else to do */
5426 if (continue_rebase) {
5427 if (!rebase_in_progress) {
5428 error = got_error(GOT_ERR_NOT_REBASING);
5431 error = got_worktree_rebase_continue(&resume_commit_id,
5432 &new_base_branch, &tmp_branch, &branch, &fileindex,
5437 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5438 resume_commit_id, repo);
5442 yca_id = got_object_id_dup(resume_commit_id);
5443 if (yca_id == NULL) {
5444 error = got_error_from_errno("got_object_id_dup");
5448 error = got_ref_open(&branch, repo, argv[0], 0);
5453 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5457 if (!continue_rebase) {
5458 struct got_object_id *base_commit_id;
5460 base_commit_id = got_worktree_get_base_commit_id(worktree);
5461 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5462 base_commit_id, branch_head_commit_id, repo,
5463 check_cancelled, NULL);
5466 if (yca_id == NULL) {
5467 error = got_error_msg(GOT_ERR_ANCESTRY,
5468 "specified branch shares no common ancestry "
5469 "with work tree's branch");
5473 error = check_same_branch(base_commit_id, branch, yca_id, repo);
5475 if (error->code != GOT_ERR_ANCESTRY)
5479 error = got_error_msg(GOT_ERR_SAME_BRANCH,
5480 "specified branch resolves to a commit which "
5481 "is already contained in work tree's branch");
5484 error = got_worktree_rebase_prepare(&new_base_branch,
5485 &tmp_branch, &fileindex, worktree, branch, repo);
5490 commit_id = branch_head_commit_id;
5491 error = got_object_open_as_commit(&commit, repo, commit_id);
5495 parent_ids = got_object_commit_get_parent_ids(commit);
5496 pid = SIMPLEQ_FIRST(parent_ids);
5498 if (!continue_rebase) {
5500 error = got_worktree_rebase_abort(worktree, fileindex,
5501 repo, new_base_branch, update_progress,
5505 printf("Rebase of %s aborted\n",
5506 got_ref_get_name(branch));
5508 error = got_error(GOT_ERR_EMPTY_REBASE);
5511 error = collect_commits(&commits, commit_id, pid->id,
5512 yca_id, got_worktree_get_path_prefix(worktree),
5513 GOT_ERR_REBASE_PATH, repo);
5514 got_object_commit_close(commit);
5519 if (SIMPLEQ_EMPTY(&commits)) {
5520 if (continue_rebase) {
5521 error = rebase_complete(worktree, fileindex,
5522 branch, new_base_branch, tmp_branch, repo);
5525 /* Fast-forward the reference of the branch. */
5526 struct got_object_id *new_head_commit_id;
5528 error = got_ref_resolve(&new_head_commit_id, repo,
5532 error = got_object_id_str(&id_str, new_head_commit_id);
5533 printf("Forwarding %s to commit %s\n",
5534 got_ref_get_name(branch), id_str);
5536 error = got_ref_change_ref(branch,
5537 new_head_commit_id);
5544 SIMPLEQ_FOREACH(qid, &commits, entry) {
5545 commit_id = qid->id;
5546 parent_id = pid ? pid->id : yca_id;
5549 error = got_worktree_rebase_merge_files(&merged_paths,
5550 worktree, fileindex, parent_id, commit_id, repo,
5551 rebase_progress, &rebase_status, check_cancelled, NULL);
5555 if (rebase_status == GOT_STATUS_CONFLICT) {
5556 got_worktree_rebase_pathlist_free(&merged_paths);
5560 error = rebase_commit(&merged_paths, worktree, fileindex,
5561 tmp_branch, commit_id, repo);
5562 got_worktree_rebase_pathlist_free(&merged_paths);
5567 if (rebase_status == GOT_STATUS_CONFLICT) {
5568 error = got_worktree_rebase_postpone(worktree, fileindex);
5571 error = got_error_msg(GOT_ERR_CONFLICTS,
5572 "conflicts must be resolved before rebasing can continue");
5574 error = rebase_complete(worktree, fileindex, branch,
5575 new_base_branch, tmp_branch, repo);
5577 got_object_id_queue_free(&commits);
5578 free(branch_head_commit_id);
5579 free(resume_commit_id);
5582 got_object_commit_close(commit);
5584 got_ref_close(branch);
5585 if (new_base_branch)
5586 got_ref_close(new_base_branch);
5588 got_ref_close(tmp_branch);
5590 got_worktree_close(worktree);
5592 got_repo_close(repo);
5597 usage_histedit(void)
5599 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script]\n",
5604 #define GOT_HISTEDIT_PICK 'p'
5605 #define GOT_HISTEDIT_EDIT 'e'
5606 #define GOT_HISTEDIT_FOLD 'f'
5607 #define GOT_HISTEDIT_DROP 'd'
5608 #define GOT_HISTEDIT_MESG 'm'
5610 static struct got_histedit_cmd {
5614 } got_histedit_cmds[] = {
5615 { GOT_HISTEDIT_PICK, "pick", "use commit" },
5616 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5617 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
5618 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5619 { GOT_HISTEDIT_MESG, "mesg",
5620 "single-line log message for commit above (open editor if empty)" },
5623 struct got_histedit_list_entry {
5624 TAILQ_ENTRY(got_histedit_list_entry) entry;
5625 struct got_object_id *commit_id;
5626 const struct got_histedit_cmd *cmd;
5629 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5631 static const struct got_error *
5632 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5633 FILE *f, struct got_repository *repo)
5635 const struct got_error *err = NULL;
5636 char *logmsg = NULL, *id_str = NULL;
5637 struct got_commit_object *commit = NULL;
5640 err = got_object_open_as_commit(&commit, repo, commit_id);
5644 err = get_short_logmsg(&logmsg, 34, commit);
5648 err = got_object_id_str(&id_str, commit_id);
5652 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
5654 err = got_ferror(f, GOT_ERR_IO);
5657 got_object_commit_close(commit);
5663 static const struct got_error *
5664 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
5665 struct got_repository *repo)
5667 const struct got_error *err = NULL;
5668 struct got_object_qid *qid;
5670 if (SIMPLEQ_EMPTY(commits))
5671 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5673 SIMPLEQ_FOREACH(qid, commits, entry) {
5674 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
5683 static const struct got_error *
5684 write_cmd_list(FILE *f)
5686 const struct got_error *err = NULL;
5689 n = fprintf(f, "# Available histedit commands:\n");
5691 return got_ferror(f, GOT_ERR_IO);
5693 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5694 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
5695 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
5698 err = got_ferror(f, GOT_ERR_IO);
5702 n = fprintf(f, "# Commits will be processed in order from top to "
5703 "bottom of this file.\n");
5705 return got_ferror(f, GOT_ERR_IO);
5709 static const struct got_error *
5710 histedit_syntax_error(int lineno)
5712 static char msg[42];
5715 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
5717 if (ret == -1 || ret >= sizeof(msg))
5718 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
5720 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
5723 static const struct got_error *
5724 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
5725 char *logmsg, struct got_repository *repo)
5727 const struct got_error *err;
5728 struct got_commit_object *folded_commit = NULL;
5729 char *id_str, *folded_logmsg = NULL;
5731 err = got_object_id_str(&id_str, hle->commit_id);
5735 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
5739 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
5742 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
5743 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
5744 folded_logmsg) == -1) {
5745 err = got_error_from_errno("asprintf");
5750 got_object_commit_close(folded_commit);
5752 free(folded_logmsg);
5756 static struct got_histedit_list_entry *
5757 get_folded_commits(struct got_histedit_list_entry *hle)
5759 struct got_histedit_list_entry *prev, *folded = NULL;
5761 prev = TAILQ_PREV(hle, got_histedit_list, entry);
5762 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
5763 prev->cmd->code == GOT_HISTEDIT_DROP)) {
5764 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
5766 prev = TAILQ_PREV(prev, got_histedit_list, entry);
5772 static const struct got_error *
5773 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
5774 struct got_repository *repo)
5776 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
5777 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
5778 const struct got_error *err = NULL;
5779 struct got_commit_object *commit = NULL;
5781 struct got_histedit_list_entry *folded = NULL;
5783 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5787 folded = get_folded_commits(hle);
5789 while (folded != hle) {
5790 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
5791 folded = TAILQ_NEXT(folded, entry);
5794 err = append_folded_commit_msg(&new_msg, folded,
5800 folded = TAILQ_NEXT(folded, entry);
5804 err = got_object_id_str(&id_str, hle->commit_id);
5807 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
5810 if (asprintf(&new_msg,
5811 "%s\n# original log message of commit %s: %s",
5812 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
5813 err = got_error_from_errno("asprintf");
5819 err = got_object_id_str(&id_str, hle->commit_id);
5823 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
5827 dprintf(fd, logmsg);
5830 err = get_editor(&editor);
5834 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
5836 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
5838 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
5841 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
5842 err = got_error_from_errno2("unlink", logmsg_path);
5848 got_object_commit_close(commit);
5852 static const struct got_error *
5853 histedit_parse_list(struct got_histedit_list *histedit_cmds,
5854 FILE *f, struct got_repository *repo)
5856 const struct got_error *err = NULL;
5857 char *line = NULL, *p, *end;
5861 const struct got_histedit_cmd *cmd;
5862 struct got_object_id *commit_id = NULL;
5863 struct got_histedit_list_entry *hle = NULL;
5866 len = getline(&line, &size, f);
5868 const struct got_error *getline_err;
5871 getline_err = got_error_from_errno("getline");
5872 err = got_ferror(f, getline_err->code);
5877 while (isspace((unsigned char)p[0]))
5879 if (p[0] == '#' || p[0] == '\0') {
5885 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5886 cmd = &got_histedit_cmds[i];
5887 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
5888 isspace((unsigned char)p[strlen(cmd->name)])) {
5889 p += strlen(cmd->name);
5892 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
5897 if (i == nitems(got_histedit_cmds)) {
5898 err = histedit_syntax_error(lineno);
5901 while (isspace((unsigned char)p[0]))
5903 if (cmd->code == GOT_HISTEDIT_MESG) {
5904 if (hle == NULL || hle->logmsg != NULL) {
5905 err = got_error(GOT_ERR_HISTEDIT_CMD);
5909 err = histedit_edit_logmsg(hle, repo);
5913 hle->logmsg = strdup(p);
5914 if (hle->logmsg == NULL) {
5915 err = got_error_from_errno("strdup");
5924 while (end[0] && !isspace((unsigned char)end[0]))
5928 err = got_object_resolve_id_str(&commit_id, repo, p);
5930 /* override error code */
5931 err = histedit_syntax_error(lineno);
5935 hle = malloc(sizeof(*hle));
5937 err = got_error_from_errno("malloc");
5941 hle->commit_id = commit_id;
5946 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
5954 static const struct got_error *
5955 histedit_check_script(struct got_histedit_list *histedit_cmds,
5956 struct got_object_id_queue *commits, struct got_repository *repo)
5958 const struct got_error *err = NULL;
5959 struct got_object_qid *qid;
5960 struct got_histedit_list_entry *hle;
5961 static char msg[80];
5964 if (TAILQ_EMPTY(histedit_cmds))
5965 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
5966 "histedit script contains no commands");
5967 if (SIMPLEQ_EMPTY(commits))
5968 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5970 SIMPLEQ_FOREACH(qid, commits, entry) {
5971 TAILQ_FOREACH(hle, histedit_cmds, entry) {
5972 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
5976 err = got_object_id_str(&id_str, qid->id);
5979 snprintf(msg, sizeof(msg),
5980 "commit %s missing from histedit script", id_str);
5982 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
5986 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
5987 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
5988 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
5989 "last commit in histedit script cannot be folded");
5994 static const struct got_error *
5995 histedit_run_editor(struct got_histedit_list *histedit_cmds,
5996 const char *path, struct got_object_id_queue *commits,
5997 struct got_repository *repo)
5999 const struct got_error *err = NULL;
6003 err = get_editor(&editor);
6007 if (spawn_editor(editor, path) == -1) {
6008 err = got_error_from_errno("failed spawning editor");
6012 f = fopen(path, "r");
6014 err = got_error_from_errno("fopen");
6017 err = histedit_parse_list(histedit_cmds, f, repo);
6021 err = histedit_check_script(histedit_cmds, commits, repo);
6023 if (f && fclose(f) != 0 && err == NULL)
6024 err = got_error_from_errno("fclose");
6029 static const struct got_error *
6030 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
6031 struct got_object_id_queue *, const char *, struct got_repository *);
6033 static const struct got_error *
6034 histedit_edit_script(struct got_histedit_list *histedit_cmds,
6035 struct got_object_id_queue *commits, struct got_repository *repo)
6037 const struct got_error *err;
6041 err = got_opentemp_named(&path, &f, "got-histedit");
6045 err = write_cmd_list(f);
6049 err = histedit_write_commit_list(commits, f, repo);
6053 if (fclose(f) != 0) {
6054 err = got_error_from_errno("fclose");
6059 err = histedit_run_editor(histedit_cmds, path, commits, repo);
6061 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6062 err->code != GOT_ERR_HISTEDIT_CMD)
6064 err = histedit_edit_list_retry(histedit_cmds, err,
6065 commits, path, repo);
6068 if (f && fclose(f) != 0 && err == NULL)
6069 err = got_error_from_errno("fclose");
6070 if (path && unlink(path) != 0 && err == NULL)
6071 err = got_error_from_errno2("unlink", path);
6076 static const struct got_error *
6077 histedit_save_list(struct got_histedit_list *histedit_cmds,
6078 struct got_worktree *worktree, struct got_repository *repo)
6080 const struct got_error *err = NULL;
6083 struct got_histedit_list_entry *hle;
6084 struct got_commit_object *commit = NULL;
6086 err = got_worktree_get_histedit_script_path(&path, worktree);
6090 f = fopen(path, "w");
6092 err = got_error_from_errno2("fopen", path);
6095 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6096 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6102 int n = fprintf(f, "%c %s\n",
6103 GOT_HISTEDIT_MESG, hle->logmsg);
6105 err = got_ferror(f, GOT_ERR_IO);
6111 if (f && fclose(f) != 0 && err == NULL)
6112 err = got_error_from_errno("fclose");
6115 got_object_commit_close(commit);
6120 histedit_free_list(struct got_histedit_list *histedit_cmds)
6122 struct got_histedit_list_entry *hle;
6124 while ((hle = TAILQ_FIRST(histedit_cmds))) {
6125 TAILQ_REMOVE(histedit_cmds, hle, entry);
6130 static const struct got_error *
6131 histedit_load_list(struct got_histedit_list *histedit_cmds,
6132 const char *path, struct got_repository *repo)
6134 const struct got_error *err = NULL;
6137 f = fopen(path, "r");
6139 err = got_error_from_errno2("fopen", path);
6143 err = histedit_parse_list(histedit_cmds, f, repo);
6145 if (f && fclose(f) != 0 && err == NULL)
6146 err = got_error_from_errno("fclose");
6150 static const struct got_error *
6151 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
6152 const struct got_error *edit_err, struct got_object_id_queue *commits,
6153 const char *path, struct got_repository *repo)
6155 const struct got_error *err = NULL, *prev_err = edit_err;
6158 while (resp != 'c' && resp != 'r' && resp != 'a') {
6159 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
6160 "or (a)bort: ", getprogname(), prev_err->msg);
6165 histedit_free_list(histedit_cmds);
6166 err = histedit_run_editor(histedit_cmds, path, commits,
6169 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6170 err->code != GOT_ERR_HISTEDIT_CMD)
6177 } else if (resp == 'r') {
6178 histedit_free_list(histedit_cmds);
6179 err = histedit_edit_script(histedit_cmds,
6182 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6183 err->code != GOT_ERR_HISTEDIT_CMD)
6190 } else if (resp == 'a') {
6191 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
6194 printf("invalid response '%c'\n", resp);
6200 static const struct got_error *
6201 histedit_complete(struct got_worktree *worktree,
6202 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6203 struct got_reference *branch, struct got_repository *repo)
6205 printf("Switching work tree to %s\n",
6206 got_ref_get_symref_target(branch));
6207 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
6211 static const struct got_error *
6212 show_histedit_progress(struct got_commit_object *commit,
6213 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
6215 const struct got_error *err;
6216 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6218 err = got_object_id_str(&old_id_str, hle->commit_id);
6223 err = got_object_id_str(&new_id_str, new_id);
6228 old_id_str[12] = '\0';
6230 new_id_str[12] = '\0';
6233 logmsg = strdup(hle->logmsg);
6234 if (logmsg == NULL) {
6235 err = got_error_from_errno("strdup");
6238 trim_logmsg(logmsg, 42);
6240 err = get_short_logmsg(&logmsg, 42, commit);
6245 switch (hle->cmd->code) {
6246 case GOT_HISTEDIT_PICK:
6247 case GOT_HISTEDIT_EDIT:
6248 printf("%s -> %s: %s\n", old_id_str,
6249 new_id_str ? new_id_str : "no-op change", logmsg);
6251 case GOT_HISTEDIT_DROP:
6252 case GOT_HISTEDIT_FOLD:
6253 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
6266 static const struct got_error *
6267 histedit_commit(struct got_pathlist_head *merged_paths,
6268 struct got_worktree *worktree, struct got_fileindex *fileindex,
6269 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
6270 struct got_repository *repo)
6272 const struct got_error *err;
6273 struct got_commit_object *commit;
6274 struct got_object_id *new_commit_id;
6276 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
6277 && hle->logmsg == NULL) {
6278 err = histedit_edit_logmsg(hle, repo);
6283 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6287 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
6288 worktree, fileindex, tmp_branch, commit, hle->commit_id,
6291 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
6293 err = show_histedit_progress(commit, hle, NULL);
6295 err = show_histedit_progress(commit, hle, new_commit_id);
6296 free(new_commit_id);
6299 got_object_commit_close(commit);
6303 static const struct got_error *
6304 histedit_skip_commit(struct got_histedit_list_entry *hle,
6305 struct got_worktree *worktree, struct got_repository *repo)
6307 const struct got_error *error;
6308 struct got_commit_object *commit;
6310 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
6315 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
6319 error = show_histedit_progress(commit, hle, NULL);
6320 got_object_commit_close(commit);
6324 static const struct got_error *
6325 cmd_histedit(int argc, char *argv[])
6327 const struct got_error *error = NULL;
6328 struct got_worktree *worktree = NULL;
6329 struct got_fileindex *fileindex = NULL;
6330 struct got_repository *repo = NULL;
6332 struct got_reference *branch = NULL;
6333 struct got_reference *tmp_branch = NULL;
6334 struct got_object_id *resume_commit_id = NULL;
6335 struct got_object_id *base_commit_id = NULL;
6336 struct got_object_id *head_commit_id = NULL;
6337 struct got_commit_object *commit = NULL;
6338 int ch, rebase_in_progress = 0, did_something;
6339 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
6340 const char *edit_script_path = NULL;
6341 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6342 struct got_object_id_queue commits;
6343 struct got_pathlist_head merged_paths;
6344 const struct got_object_id_queue *parent_ids;
6345 struct got_object_qid *pid;
6346 struct got_histedit_list histedit_cmds;
6347 struct got_histedit_list_entry *hle;
6349 SIMPLEQ_INIT(&commits);
6350 TAILQ_INIT(&histedit_cmds);
6351 TAILQ_INIT(&merged_paths);
6353 while ((ch = getopt(argc, argv, "acF:")) != -1) {
6362 edit_script_path = optarg;
6374 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6375 "unveil", NULL) == -1)
6378 if (abort_edit && continue_edit)
6384 * This command cannot apply unveil(2) in all cases because the
6385 * user may choose to run an editor to edit the histedit script
6386 * and to edit individual commit log messages.
6387 * unveil(2) traverses exec(2); if an editor is used we have to
6388 * apply unveil after edit script and log messages have been written.
6389 * XXX TODO: Make use of unveil(2) where possible.
6392 cwd = getcwd(NULL, 0);
6394 error = got_error_from_errno("getcwd");
6397 error = got_worktree_open(&worktree, cwd);
6401 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6406 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6409 if (rebase_in_progress) {
6410 error = got_error(GOT_ERR_REBASING);
6414 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6418 if (edit_in_progress && abort_edit) {
6419 error = got_worktree_histedit_continue(&resume_commit_id,
6420 &tmp_branch, &branch, &base_commit_id, &fileindex,
6424 printf("Switching work tree to %s\n",
6425 got_ref_get_symref_target(branch));
6426 error = got_worktree_histedit_abort(worktree, fileindex, repo,
6427 branch, base_commit_id, update_progress, &did_something);
6430 printf("Histedit of %s aborted\n",
6431 got_ref_get_symref_target(branch));
6432 goto done; /* nothing else to do */
6433 } else if (abort_edit) {
6434 error = got_error(GOT_ERR_NOT_HISTEDIT);
6438 if (continue_edit) {
6441 if (!edit_in_progress) {
6442 error = got_error(GOT_ERR_NOT_HISTEDIT);
6446 error = got_worktree_get_histedit_script_path(&path, worktree);
6450 error = histedit_load_list(&histedit_cmds, path, repo);
6455 error = got_worktree_histedit_continue(&resume_commit_id,
6456 &tmp_branch, &branch, &base_commit_id, &fileindex,
6461 error = got_ref_resolve(&head_commit_id, repo, branch);
6465 error = got_object_open_as_commit(&commit, repo,
6469 parent_ids = got_object_commit_get_parent_ids(commit);
6470 pid = SIMPLEQ_FIRST(parent_ids);
6472 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6475 error = collect_commits(&commits, head_commit_id, pid->id,
6476 base_commit_id, got_worktree_get_path_prefix(worktree),
6477 GOT_ERR_HISTEDIT_PATH, repo);
6478 got_object_commit_close(commit);
6483 if (edit_in_progress) {
6484 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6488 error = got_ref_open(&branch, repo,
6489 got_worktree_get_head_ref_name(worktree), 0);
6493 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
6494 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
6495 "will not edit commit history of a branch outside "
6496 "the \"refs/heads/\" reference namespace");
6500 error = got_ref_resolve(&head_commit_id, repo, branch);
6501 got_ref_close(branch);
6506 error = got_object_open_as_commit(&commit, repo,
6510 parent_ids = got_object_commit_get_parent_ids(commit);
6511 pid = SIMPLEQ_FIRST(parent_ids);
6513 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6516 error = collect_commits(&commits, head_commit_id, pid->id,
6517 got_worktree_get_base_commit_id(worktree),
6518 got_worktree_get_path_prefix(worktree),
6519 GOT_ERR_HISTEDIT_PATH, repo);
6520 got_object_commit_close(commit);
6525 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
6526 &base_commit_id, &fileindex, worktree, repo);
6530 if (edit_script_path) {
6531 error = histedit_load_list(&histedit_cmds,
6532 edit_script_path, repo);
6534 got_worktree_histedit_abort(worktree, fileindex,
6535 repo, branch, base_commit_id,
6536 update_progress, &did_something);
6540 error = histedit_edit_script(&histedit_cmds, &commits,
6543 got_worktree_histedit_abort(worktree, fileindex,
6544 repo, branch, base_commit_id,
6545 update_progress, &did_something);
6551 error = histedit_save_list(&histedit_cmds, worktree,
6554 got_worktree_histedit_abort(worktree, fileindex,
6555 repo, branch, base_commit_id,
6556 update_progress, &did_something);
6562 error = histedit_check_script(&histedit_cmds, &commits, repo);
6566 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
6567 if (resume_commit_id) {
6568 if (got_object_id_cmp(hle->commit_id,
6569 resume_commit_id) != 0)
6572 resume_commit_id = NULL;
6573 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
6574 hle->cmd->code == GOT_HISTEDIT_FOLD) {
6575 error = histedit_skip_commit(hle, worktree,
6578 error = histedit_commit(NULL, worktree,
6579 fileindex, tmp_branch, hle, repo);
6586 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
6587 error = histedit_skip_commit(hle, worktree, repo);
6593 error = got_object_open_as_commit(&commit, repo,
6597 parent_ids = got_object_commit_get_parent_ids(commit);
6598 pid = SIMPLEQ_FIRST(parent_ids);
6600 error = got_worktree_histedit_merge_files(&merged_paths,
6601 worktree, fileindex, pid->id, hle->commit_id, repo,
6602 rebase_progress, &rebase_status, check_cancelled, NULL);
6605 got_object_commit_close(commit);
6608 if (rebase_status == GOT_STATUS_CONFLICT) {
6609 got_worktree_rebase_pathlist_free(&merged_paths);
6613 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
6615 error = got_object_id_str(&id_str, hle->commit_id);
6618 printf("Stopping histedit for amending commit %s\n",
6621 got_worktree_rebase_pathlist_free(&merged_paths);
6622 error = got_worktree_histedit_postpone(worktree,
6627 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
6628 error = histedit_skip_commit(hle, worktree, repo);
6634 error = histedit_commit(&merged_paths, worktree, fileindex,
6635 tmp_branch, hle, repo);
6636 got_worktree_rebase_pathlist_free(&merged_paths);
6641 if (rebase_status == GOT_STATUS_CONFLICT) {
6642 error = got_worktree_histedit_postpone(worktree, fileindex);
6645 error = got_error_msg(GOT_ERR_CONFLICTS,
6646 "conflicts must be resolved before rebasing can continue");
6648 error = histedit_complete(worktree, fileindex, tmp_branch,
6651 got_object_id_queue_free(&commits);
6652 histedit_free_list(&histedit_cmds);
6653 free(head_commit_id);
6654 free(base_commit_id);
6655 free(resume_commit_id);
6657 got_object_commit_close(commit);
6659 got_ref_close(branch);
6661 got_ref_close(tmp_branch);
6663 got_worktree_close(worktree);
6665 got_repo_close(repo);
6670 usage_integrate(void)
6672 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
6676 static const struct got_error *
6677 cmd_integrate(int argc, char *argv[])
6679 const struct got_error *error = NULL;
6680 struct got_repository *repo = NULL;
6681 struct got_worktree *worktree = NULL;
6682 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
6683 const char *branch_arg = NULL;
6684 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
6685 struct got_fileindex *fileindex = NULL;
6686 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
6687 int ch, did_something = 0;
6689 while ((ch = getopt(argc, argv, "")) != -1) {
6702 branch_arg = argv[0];
6704 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6705 "unveil", NULL) == -1)
6708 cwd = getcwd(NULL, 0);
6710 error = got_error_from_errno("getcwd");
6714 error = got_worktree_open(&worktree, cwd);
6718 error = check_rebase_or_histedit_in_progress(worktree);
6722 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6727 error = apply_unveil(got_repo_get_path(repo), 0,
6728 got_worktree_get_root_path(worktree));
6732 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
6733 error = got_error_from_errno("asprintf");
6737 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
6738 &base_branch_ref, worktree, refname, repo);
6742 refname = strdup(got_ref_get_name(branch_ref));
6743 if (refname == NULL) {
6744 error = got_error_from_errno("strdup");
6745 got_worktree_integrate_abort(worktree, fileindex, repo,
6746 branch_ref, base_branch_ref);
6749 base_refname = strdup(got_ref_get_name(base_branch_ref));
6750 if (base_refname == NULL) {
6751 error = got_error_from_errno("strdup");
6752 got_worktree_integrate_abort(worktree, fileindex, repo,
6753 branch_ref, base_branch_ref);
6757 error = got_ref_resolve(&commit_id, repo, branch_ref);
6761 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
6765 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
6766 error = got_error_msg(GOT_ERR_SAME_BRANCH,
6767 "specified branch has already been integrated");
6768 got_worktree_integrate_abort(worktree, fileindex, repo,
6769 branch_ref, base_branch_ref);
6773 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
6775 if (error->code == GOT_ERR_ANCESTRY)
6776 error = got_error(GOT_ERR_REBASE_REQUIRED);
6777 got_worktree_integrate_abort(worktree, fileindex, repo,
6778 branch_ref, base_branch_ref);
6782 error = got_worktree_integrate_continue(worktree, fileindex, repo,
6783 branch_ref, base_branch_ref, update_progress, &did_something,
6784 check_cancelled, NULL);
6788 printf("Integrated %s into %s\n", refname, base_refname);
6791 got_repo_close(repo);
6793 got_worktree_close(worktree);
6795 free(base_commit_id);
6805 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
6806 "[file-path ...]\n",
6811 static const struct got_error *
6812 print_stage(void *arg, unsigned char status, unsigned char staged_status,
6813 const char *path, struct got_object_id *blob_id,
6814 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
6816 const struct got_error *err = NULL;
6817 char *id_str = NULL;
6819 if (staged_status != GOT_STATUS_ADD &&
6820 staged_status != GOT_STATUS_MODIFY &&
6821 staged_status != GOT_STATUS_DELETE)
6824 if (staged_status == GOT_STATUS_ADD ||
6825 staged_status == GOT_STATUS_MODIFY)
6826 err = got_object_id_str(&id_str, staged_blob_id);
6828 err = got_object_id_str(&id_str, blob_id);
6832 printf("%s %c %s\n", id_str, staged_status, path);
6837 static const struct got_error *
6838 cmd_stage(int argc, char *argv[])
6840 const struct got_error *error = NULL;
6841 struct got_repository *repo = NULL;
6842 struct got_worktree *worktree = NULL;
6844 struct got_pathlist_head paths;
6845 struct got_pathlist_entry *pe;
6846 int ch, list_stage = 0, pflag = 0;
6847 FILE *patch_script_file = NULL;
6848 const char *patch_script_path = NULL;
6849 struct choose_patch_arg cpa;
6853 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
6862 patch_script_path = optarg;
6874 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6875 "unveil", NULL) == -1)
6878 if (list_stage && (pflag || patch_script_path))
6879 errx(1, "-l option cannot be used with other options");
6880 if (patch_script_path && !pflag)
6881 errx(1, "-F option can only be used together with -p option");
6883 cwd = getcwd(NULL, 0);
6885 error = got_error_from_errno("getcwd");
6889 error = got_worktree_open(&worktree, cwd);
6893 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6898 if (patch_script_path) {
6899 patch_script_file = fopen(patch_script_path, "r");
6900 if (patch_script_file == NULL) {
6901 error = got_error_from_errno2("fopen",
6906 error = apply_unveil(got_repo_get_path(repo), 0,
6907 got_worktree_get_root_path(worktree));
6911 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6916 error = got_worktree_status(worktree, &paths, repo,
6917 print_stage, NULL, check_cancelled, NULL);
6919 cpa.patch_script_file = patch_script_file;
6920 cpa.action = "stage";
6921 error = got_worktree_stage(worktree, &paths,
6922 pflag ? NULL : print_status, NULL,
6923 pflag ? choose_patch : NULL, &cpa, repo);
6926 if (patch_script_file && fclose(patch_script_file) == EOF &&
6928 error = got_error_from_errno2("fclose", patch_script_path);
6930 got_repo_close(repo);
6932 got_worktree_close(worktree);
6933 TAILQ_FOREACH(pe, &paths, entry)
6934 free((char *)pe->path);
6935 got_pathlist_free(&paths);
6943 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
6944 "[file-path ...]\n",
6950 static const struct got_error *
6951 cmd_unstage(int argc, char *argv[])
6953 const struct got_error *error = NULL;
6954 struct got_repository *repo = NULL;
6955 struct got_worktree *worktree = NULL;
6957 struct got_pathlist_head paths;
6958 struct got_pathlist_entry *pe;
6959 int ch, did_something = 0, pflag = 0;
6960 FILE *patch_script_file = NULL;
6961 const char *patch_script_path = NULL;
6962 struct choose_patch_arg cpa;
6966 while ((ch = getopt(argc, argv, "pF:")) != -1) {
6972 patch_script_path = optarg;
6984 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6985 "unveil", NULL) == -1)
6988 if (patch_script_path && !pflag)
6989 errx(1, "-F option can only be used together with -p option");
6991 cwd = getcwd(NULL, 0);
6993 error = got_error_from_errno("getcwd");
6997 error = got_worktree_open(&worktree, cwd);
7001 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7006 if (patch_script_path) {
7007 patch_script_file = fopen(patch_script_path, "r");
7008 if (patch_script_file == NULL) {
7009 error = got_error_from_errno2("fopen",
7015 error = apply_unveil(got_repo_get_path(repo), 0,
7016 got_worktree_get_root_path(worktree));
7020 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7024 cpa.patch_script_file = patch_script_file;
7025 cpa.action = "unstage";
7026 error = got_worktree_unstage(worktree, &paths, update_progress,
7027 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
7029 if (patch_script_file && fclose(patch_script_file) == EOF &&
7031 error = got_error_from_errno2("fclose", patch_script_path);
7033 got_repo_close(repo);
7035 got_worktree_close(worktree);
7036 TAILQ_FOREACH(pe, &paths, entry)
7037 free((char *)pe->path);
7038 got_pathlist_free(&paths);
7046 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
7047 "arg1 [arg2 ...]\n", getprogname());
7051 static const struct got_error *
7052 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7054 const struct got_error *err;
7055 struct got_blob_object *blob;
7057 err = got_object_open_as_blob(&blob, repo, id, 8192);
7061 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
7062 got_object_blob_close(blob);
7066 static const struct got_error *
7067 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7069 const struct got_error *err;
7070 struct got_tree_object *tree;
7073 err = got_object_open_as_tree(&tree, repo, id);
7077 nentries = got_object_tree_get_nentries(tree);
7078 for (i = 0; i < nentries; i++) {
7079 struct got_tree_entry *te;
7081 if (sigint_received || sigpipe_received)
7083 te = got_object_tree_get_entry(tree, i);
7084 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
7087 fprintf(outfile, "%s %.7o %s\n", id_str,
7088 got_tree_entry_get_mode(te),
7089 got_tree_entry_get_name(te));
7093 got_object_tree_close(tree);
7097 static const struct got_error *
7098 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7100 const struct got_error *err;
7101 struct got_commit_object *commit;
7102 const struct got_object_id_queue *parent_ids;
7103 struct got_object_qid *pid;
7104 char *id_str = NULL;
7105 const char *logmsg = NULL;
7107 err = got_object_open_as_commit(&commit, repo, id);
7111 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
7115 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
7116 parent_ids = got_object_commit_get_parent_ids(commit);
7117 fprintf(outfile, "numparents %d\n",
7118 got_object_commit_get_nparents(commit));
7119 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
7121 err = got_object_id_str(&pid_str, pid->id);
7124 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
7127 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
7128 got_object_commit_get_author(commit),
7129 got_object_commit_get_author_time(commit));
7131 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
7132 got_object_commit_get_author(commit),
7133 got_object_commit_get_committer_time(commit));
7135 logmsg = got_object_commit_get_logmsg_raw(commit);
7136 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
7137 fprintf(outfile, "%s", logmsg);
7140 got_object_commit_close(commit);
7144 static const struct got_error *
7145 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7147 const struct got_error *err;
7148 struct got_tag_object *tag;
7149 char *id_str = NULL;
7150 const char *tagmsg = NULL;
7152 err = got_object_open_as_tag(&tag, repo, id);
7156 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
7160 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
7162 switch (got_object_tag_get_object_type(tag)) {
7163 case GOT_OBJ_TYPE_BLOB:
7164 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7165 GOT_OBJ_LABEL_BLOB);
7167 case GOT_OBJ_TYPE_TREE:
7168 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7169 GOT_OBJ_LABEL_TREE);
7171 case GOT_OBJ_TYPE_COMMIT:
7172 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7173 GOT_OBJ_LABEL_COMMIT);
7175 case GOT_OBJ_TYPE_TAG:
7176 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7183 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
7184 got_object_tag_get_name(tag));
7186 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
7187 got_object_tag_get_tagger(tag),
7188 got_object_tag_get_tagger_time(tag));
7190 tagmsg = got_object_tag_get_message(tag);
7191 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
7192 fprintf(outfile, "%s", tagmsg);
7195 got_object_tag_close(tag);
7199 static const struct got_error *
7200 cmd_cat(int argc, char *argv[])
7202 const struct got_error *error;
7203 struct got_repository *repo = NULL;
7204 struct got_worktree *worktree = NULL;
7205 char *cwd = NULL, *repo_path = NULL, *label = NULL;
7206 const char *commit_id_str = NULL;
7207 struct got_object_id *id = NULL, *commit_id = NULL;
7208 int ch, obj_type, i, force_path = 0;
7211 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7216 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
7219 commit_id_str = optarg;
7222 repo_path = realpath(optarg, NULL);
7223 if (repo_path == NULL)
7224 return got_error_from_errno2("realpath",
7226 got_path_strip_trailing_slashes(repo_path);
7240 cwd = getcwd(NULL, 0);
7242 error = got_error_from_errno("getcwd");
7245 error = got_worktree_open(&worktree, cwd);
7246 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7249 if (repo_path == NULL) {
7251 got_worktree_get_repo_path(worktree));
7252 if (repo_path == NULL) {
7253 error = got_error_from_errno("strdup");
7259 if (repo_path == NULL) {
7260 repo_path = getcwd(NULL, 0);
7261 if (repo_path == NULL)
7262 return got_error_from_errno("getcwd");
7265 error = got_repo_open(&repo, repo_path, NULL);
7270 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7274 if (commit_id_str == NULL)
7275 commit_id_str = GOT_REF_HEAD;
7276 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
7280 for (i = 0; i < argc; i++) {
7282 error = got_object_id_by_path(&id, repo, commit_id,
7287 error = match_object_id(&id, &label, argv[i],
7288 GOT_OBJ_TYPE_ANY, 0, repo);
7290 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
7291 error->code != GOT_ERR_NOT_REF)
7293 error = got_object_id_by_path(&id, repo,
7294 commit_id, argv[i]);
7300 error = got_object_get_type(&obj_type, repo, id);
7305 case GOT_OBJ_TYPE_BLOB:
7306 error = cat_blob(id, repo, stdout);
7308 case GOT_OBJ_TYPE_TREE:
7309 error = cat_tree(id, repo, stdout);
7311 case GOT_OBJ_TYPE_COMMIT:
7312 error = cat_commit(id, repo, stdout);
7314 case GOT_OBJ_TYPE_TAG:
7315 error = cat_tag(id, repo, stdout);
7318 error = got_error(GOT_ERR_OBJ_TYPE);
7334 got_worktree_close(worktree);
7336 const struct got_error *repo_error;
7337 repo_error = got_repo_close(repo);