2 * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/queue.h>
19 #include <sys/types.h>
21 #include <sys/param.h>
38 #include "got_version.h"
39 #include "got_error.h"
40 #include "got_object.h"
41 #include "got_reference.h"
42 #include "got_repository.h"
44 #include "got_cancel.h"
45 #include "got_worktree.h"
47 #include "got_commit_graph.h"
48 #include "got_blame.h"
49 #include "got_privsep.h"
50 #include "got_opentemp.h"
53 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
56 static volatile sig_atomic_t sigint_received;
57 static volatile sig_atomic_t sigpipe_received;
60 catch_sigint(int signo)
66 catch_sigpipe(int signo)
74 const struct got_error *(*cmd_main)(int, char *[]);
75 void (*cmd_usage)(void);
76 const char *cmd_alias;
79 __dead static void usage(int);
80 __dead static void usage_init(void);
81 __dead static void usage_import(void);
82 __dead static void usage_checkout(void);
83 __dead static void usage_update(void);
84 __dead static void usage_log(void);
85 __dead static void usage_diff(void);
86 __dead static void usage_blame(void);
87 __dead static void usage_tree(void);
88 __dead static void usage_status(void);
89 __dead static void usage_ref(void);
90 __dead static void usage_branch(void);
91 __dead static void usage_tag(void);
92 __dead static void usage_add(void);
93 __dead static void usage_remove(void);
94 __dead static void usage_revert(void);
95 __dead static void usage_commit(void);
96 __dead static void usage_cherrypick(void);
97 __dead static void usage_backout(void);
98 __dead static void usage_rebase(void);
99 __dead static void usage_histedit(void);
100 __dead static void usage_stage(void);
101 __dead static void usage_unstage(void);
102 __dead static void usage_cat(void);
104 static const struct got_error* cmd_init(int, char *[]);
105 static const struct got_error* cmd_import(int, char *[]);
106 static const struct got_error* cmd_checkout(int, char *[]);
107 static const struct got_error* cmd_update(int, char *[]);
108 static const struct got_error* cmd_log(int, char *[]);
109 static const struct got_error* cmd_diff(int, char *[]);
110 static const struct got_error* cmd_blame(int, char *[]);
111 static const struct got_error* cmd_tree(int, char *[]);
112 static const struct got_error* cmd_status(int, char *[]);
113 static const struct got_error* cmd_ref(int, char *[]);
114 static const struct got_error* cmd_branch(int, char *[]);
115 static const struct got_error* cmd_tag(int, char *[]);
116 static const struct got_error* cmd_add(int, char *[]);
117 static const struct got_error* cmd_remove(int, char *[]);
118 static const struct got_error* cmd_revert(int, char *[]);
119 static const struct got_error* cmd_commit(int, char *[]);
120 static const struct got_error* cmd_cherrypick(int, char *[]);
121 static const struct got_error* cmd_backout(int, char *[]);
122 static const struct got_error* cmd_rebase(int, char *[]);
123 static const struct got_error* cmd_histedit(int, char *[]);
124 static const struct got_error* cmd_stage(int, char *[]);
125 static const struct got_error* cmd_unstage(int, char *[]);
126 static const struct got_error* cmd_cat(int, char *[]);
128 static struct got_cmd got_commands[] = {
129 { "init", cmd_init, usage_init, "in" },
130 { "import", cmd_import, usage_import, "im" },
131 { "checkout", cmd_checkout, usage_checkout, "co" },
132 { "update", cmd_update, usage_update, "up" },
133 { "log", cmd_log, usage_log, "" },
134 { "diff", cmd_diff, usage_diff, "di" },
135 { "blame", cmd_blame, usage_blame, "bl" },
136 { "tree", cmd_tree, usage_tree, "tr" },
137 { "status", cmd_status, usage_status, "st" },
138 { "ref", cmd_ref, usage_ref, "" },
139 { "branch", cmd_branch, usage_branch, "br" },
140 { "tag", cmd_tag, usage_tag, "" },
141 { "add", cmd_add, usage_add, "" },
142 { "remove", cmd_remove, usage_remove, "rm" },
143 { "revert", cmd_revert, usage_revert, "rv" },
144 { "commit", cmd_commit, usage_commit, "ci" },
145 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
146 { "backout", cmd_backout, usage_backout, "bo" },
147 { "rebase", cmd_rebase, usage_rebase, "rb" },
148 { "histedit", cmd_histedit, usage_histedit, "he" },
149 { "stage", cmd_stage, usage_stage, "sg" },
150 { "unstage", cmd_unstage, usage_unstage, "ug" },
151 { "cat", cmd_cat, usage_cat, "" },
159 fprintf(stderr, "commands:");
160 for (i = 0; i < nitems(got_commands); i++) {
161 struct got_cmd *cmd = &got_commands[i];
162 fprintf(stderr, " %s", cmd->cmd_name);
168 main(int argc, char *argv[])
173 int hflag = 0, Vflag = 0;
175 setlocale(LC_CTYPE, "");
177 while ((ch = getopt(argc, argv, "hV")) != -1) {
196 got_version_print_str();
203 signal(SIGINT, catch_sigint);
204 signal(SIGPIPE, catch_sigpipe);
206 for (i = 0; i < nitems(got_commands); i++) {
207 const struct got_error *error;
209 cmd = &got_commands[i];
211 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
212 strcmp(cmd->cmd_alias, argv[0]) != 0)
216 got_commands[i].cmd_usage();
218 error = got_commands[i].cmd_main(argc, argv);
219 if (error && !(sigint_received || sigpipe_received)) {
220 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
227 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
235 fprintf(stderr, "usage: %s [-h] [-V] command [arg ...]\n",
242 static const struct got_error *
243 get_editor(char **abspath)
245 const struct got_error *err = NULL;
250 editor = getenv("VISUAL");
252 editor = getenv("EDITOR");
255 err = got_path_find_prog(abspath, editor);
260 if (*abspath == NULL) {
261 *abspath = strdup("/bin/ed");
262 if (*abspath == NULL)
263 return got_error_from_errno("strdup");
269 static const struct got_error *
270 apply_unveil(const char *repo_path, int repo_read_only,
271 const char *worktree_path)
273 const struct got_error *err;
276 if (unveil("gmon.out", "rwc") != 0)
277 return got_error_from_errno2("unveil", "gmon.out");
279 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
280 return got_error_from_errno2("unveil", repo_path);
282 if (worktree_path && unveil(worktree_path, "rwc") != 0)
283 return got_error_from_errno2("unveil", worktree_path);
285 if (unveil("/tmp", "rwc") != 0)
286 return got_error_from_errno2("unveil", "/tmp");
288 err = got_privsep_unveil_exec_helpers();
292 if (unveil(NULL, NULL) != 0)
293 return got_error_from_errno("unveil");
301 fprintf(stderr, "usage: %s init repository-path\n", getprogname());
305 static const struct got_error *
306 cmd_init(int argc, char *argv[])
308 const struct got_error *error = NULL;
309 char *repo_path = NULL;
312 while ((ch = getopt(argc, argv, "")) != -1) {
324 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
330 repo_path = strdup(argv[0]);
331 if (repo_path == NULL)
332 return got_error_from_errno("strdup");
334 got_path_strip_trailing_slashes(repo_path);
336 error = got_path_mkdir(repo_path);
338 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
341 error = apply_unveil(repo_path, 0, NULL);
345 error = got_repo_init(repo_path);
357 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
358 "[-r repository-path] [-I pattern] path\n", getprogname());
363 spawn_editor(const char *editor, const char *file)
366 sig_t sighup, sigint, sigquit;
369 sighup = signal(SIGHUP, SIG_IGN);
370 sigint = signal(SIGINT, SIG_IGN);
371 sigquit = signal(SIGQUIT, SIG_IGN);
373 switch (pid = fork()) {
377 execl(editor, editor, file, (char *)NULL);
381 while (waitpid(pid, &st, 0) == -1)
386 (void)signal(SIGHUP, sighup);
387 (void)signal(SIGINT, sigint);
388 (void)signal(SIGQUIT, sigquit);
390 if (!WIFEXITED(st)) {
395 return WEXITSTATUS(st);
398 static const struct got_error *
399 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
400 const char *initial_content)
402 const struct got_error *err = NULL;
406 int content_changed = 0;
411 if (stat(logmsg_path, &st) == -1)
412 return got_error_from_errno2("stat", logmsg_path);
414 if (spawn_editor(editor, logmsg_path) == -1)
415 return got_error_from_errno("failed spawning editor");
417 if (stat(logmsg_path, &st2) == -1)
418 return got_error_from_errno("stat");
420 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
421 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
422 "no changes made to commit message, aborting");
424 *logmsg = malloc(st2.st_size + 1);
426 return got_error_from_errno("malloc");
430 fp = fopen(logmsg_path, "r");
432 err = got_error_from_errno("fopen");
435 while (fgets(buf, sizeof(buf), fp) != NULL) {
436 if (!content_changed && strcmp(buf, initial_content) != 0)
438 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
439 continue; /* remove comments and leading empty lines */
440 len = strlcat(*logmsg, buf, st2.st_size);
444 while (len > 0 && (*logmsg)[len - 1] == '\n') {
445 (*logmsg)[len - 1] = '\0';
449 if (len == 0 || !content_changed)
450 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
451 "commit message cannot be empty, aborting");
460 static const struct got_error *
461 collect_import_msg(char **logmsg, const char *editor, const char *path_dir,
462 const char *branch_name)
464 char *initial_content = NULL, *logmsg_path = NULL;
465 const struct got_error *err = NULL;
468 if (asprintf(&initial_content,
469 "\n# %s to be imported to branch %s\n", path_dir,
471 return got_error_from_errno("asprintf");
473 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-importmsg");
477 dprintf(fd, initial_content);
480 err = edit_logmsg(logmsg, editor, logmsg_path, initial_content);
482 free(initial_content);
487 static const struct got_error *
488 import_progress(void *arg, const char *path)
490 printf("A %s\n", path);
494 static const struct got_error *
495 get_author(char **author, struct got_repository *repo)
497 const struct got_error *err = NULL;
498 const char *got_author, *name, *email;
502 name = got_repo_get_gitconfig_author_name(repo);
503 email = got_repo_get_gitconfig_author_email(repo);
505 if (asprintf(author, "%s <%s>", name, email) == -1)
506 return got_error_from_errno("asprintf");
510 got_author = getenv("GOT_AUTHOR");
511 if (got_author == NULL) {
512 name = got_repo_get_global_gitconfig_author_name(repo);
513 email = got_repo_get_global_gitconfig_author_email(repo);
515 if (asprintf(author, "%s <%s>", name, email) == -1)
516 return got_error_from_errno("asprintf");
519 /* TODO: Look up user in password database? */
520 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
523 *author = strdup(got_author);
525 return got_error_from_errno("strdup");
528 * Really dumb email address check; we're only doing this to
529 * avoid git's object parser breaking on commits we create.
531 while (*got_author && *got_author != '<')
533 if (*got_author != '<') {
534 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
537 while (*got_author && *got_author != '@')
539 if (*got_author != '@') {
540 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
543 while (*got_author && *got_author != '>')
545 if (*got_author != '>')
546 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
555 static const struct got_error *
556 get_gitconfig_path(char **gitconfig_path)
558 const char *homedir = getenv("HOME");
560 *gitconfig_path = NULL;
562 if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
563 return got_error_from_errno("asprintf");
569 static const struct got_error *
570 cmd_import(int argc, char *argv[])
572 const struct got_error *error = NULL;
573 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
574 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
575 const char *branch_name = "master";
576 char *refname = NULL, *id_str = NULL;
577 struct got_repository *repo = NULL;
578 struct got_reference *branch_ref = NULL, *head_ref = NULL;
579 struct got_object_id *new_commit_id = NULL;
581 struct got_pathlist_head ignores;
582 struct got_pathlist_entry *pe;
584 TAILQ_INIT(&ignores);
586 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
589 branch_name = optarg;
592 logmsg = strdup(optarg);
593 if (logmsg == NULL) {
594 error = got_error_from_errno("strdup");
599 repo_path = realpath(optarg, NULL);
600 if (repo_path == NULL) {
601 error = got_error_from_errno("realpath");
606 if (optarg[0] == '\0')
608 error = got_pathlist_insert(&pe, &ignores, optarg,
623 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
631 if (repo_path == NULL) {
632 repo_path = getcwd(NULL, 0);
633 if (repo_path == NULL)
634 return got_error_from_errno("getcwd");
636 got_path_strip_trailing_slashes(repo_path);
637 error = get_gitconfig_path(&gitconfig_path);
640 error = got_repo_open(&repo, repo_path, gitconfig_path);
644 error = get_author(&author, repo);
648 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
649 error = got_error_from_errno("asprintf");
653 error = got_ref_open(&branch_ref, repo, refname, 0);
655 if (error->code != GOT_ERR_NOT_REF)
658 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
659 "import target branch already exists");
663 path_dir = realpath(argv[0], NULL);
664 if (path_dir == NULL) {
665 error = got_error_from_errno("realpath");
668 got_path_strip_trailing_slashes(path_dir);
671 * unveil(2) traverses exec(2); if an editor is used we have
672 * to apply unveil after the log message has been written.
674 if (logmsg == NULL || strlen(logmsg) == 0) {
675 error = get_editor(&editor);
678 error = collect_import_msg(&logmsg, editor, path_dir, refname);
683 if (unveil(path_dir, "r") != 0)
684 return got_error_from_errno2("unveil", path_dir);
686 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
690 error = got_repo_import(&new_commit_id, path_dir, logmsg,
691 author, &ignores, repo, import_progress, NULL);
695 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
699 error = got_ref_write(branch_ref, repo);
703 error = got_object_id_str(&id_str, new_commit_id);
707 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
709 if (error->code != GOT_ERR_NOT_REF)
712 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
717 error = got_ref_write(head_ref, repo);
722 printf("Created branch %s with commit %s\n",
723 got_ref_get_name(branch_ref), id_str);
731 free(gitconfig_path);
733 got_ref_close(branch_ref);
735 got_ref_close(head_ref);
742 fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
743 "[-p prefix] repository-path [worktree-path]\n", getprogname());
747 static const struct got_error *
748 checkout_progress(void *arg, unsigned char status, const char *path)
750 char *worktree_path = arg;
752 /* Base commit bump happens silently. */
753 if (status == GOT_STATUS_BUMP_BASE)
756 while (path[0] == '/')
759 printf("%c %s/%s\n", status, worktree_path, path);
763 static const struct got_error *
764 check_cancelled(void *arg)
766 if (sigint_received || sigpipe_received)
767 return got_error(GOT_ERR_CANCELLED);
771 static const struct got_error *
772 check_linear_ancestry(struct got_object_id *commit_id,
773 struct got_object_id *base_commit_id, struct got_repository *repo)
775 const struct got_error *err = NULL;
776 struct got_object_id *yca_id;
778 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
779 commit_id, base_commit_id, repo, check_cancelled, NULL);
784 return got_error(GOT_ERR_ANCESTRY);
787 * Require a straight line of history between the target commit
788 * and the work tree's base commit.
790 * Non-linear situations such as this require a rebase:
792 * (commit) D F (base_commit)
800 * 'got update' only handles linear cases:
801 * Update forwards in time: A (base/yca) - B - C - D (commit)
802 * Update backwards in time: D (base) - C - B - A (commit/yca)
804 if (got_object_id_cmp(commit_id, yca_id) != 0 &&
805 got_object_id_cmp(base_commit_id, yca_id) != 0)
806 return got_error(GOT_ERR_ANCESTRY);
812 static const struct got_error *
813 check_same_branch(struct got_object_id *commit_id,
814 struct got_reference *head_ref, struct got_object_id *yca_id,
815 struct got_repository *repo)
817 const struct got_error *err = NULL;
818 struct got_commit_graph *graph = NULL;
819 struct got_object_id *head_commit_id = NULL;
820 int is_same_branch = 0;
822 err = got_ref_resolve(&head_commit_id, repo, head_ref);
826 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
830 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
835 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
839 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
840 check_cancelled, NULL);
845 struct got_object_id *id;
846 err = got_commit_graph_iter_next(&id, graph);
848 if (err->code == GOT_ERR_ITER_COMPLETED) {
851 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
853 err = got_commit_graph_fetch_commits(graph, 1,
854 repo, check_cancelled, NULL);
860 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
862 if (got_object_id_cmp(id, commit_id) == 0) {
870 got_commit_graph_close(graph);
871 free(head_commit_id);
872 if (!err && !is_same_branch)
873 err = got_error(GOT_ERR_ANCESTRY);
877 static const struct got_error *
878 resolve_commit_arg(struct got_object_id **commit_id,
879 const char *commit_id_arg, struct got_repository *repo)
881 const struct got_error *err;
882 struct got_reference *ref;
883 struct got_tag_object *tag;
885 err = got_repo_object_match_tag(&tag, commit_id_arg,
886 GOT_OBJ_TYPE_COMMIT, repo);
888 *commit_id = got_object_id_dup(
889 got_object_tag_get_object_id(tag));
890 if (*commit_id == NULL)
891 err = got_error_from_errno("got_object_id_dup");
892 got_object_tag_close(tag);
894 } else if (err->code != GOT_ERR_NO_OBJ)
897 err = got_ref_open(&ref, repo, commit_id_arg, 0);
899 err = got_ref_resolve(commit_id, repo, ref);
902 if (err->code != GOT_ERR_NOT_REF)
904 err = got_repo_match_object_id_prefix(commit_id,
905 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
910 static const struct got_error *
911 cmd_checkout(int argc, char *argv[])
913 const struct got_error *error = NULL;
914 struct got_repository *repo = NULL;
915 struct got_reference *head_ref = NULL;
916 struct got_worktree *worktree = NULL;
917 char *repo_path = NULL;
918 char *worktree_path = NULL;
919 const char *path_prefix = "";
920 const char *branch_name = GOT_REF_HEAD;
921 char *commit_id_str = NULL;
922 int ch, same_path_prefix;
923 struct got_pathlist_head paths;
927 while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
930 branch_name = optarg;
933 commit_id_str = strdup(optarg);
934 if (commit_id_str == NULL)
935 return got_error_from_errno("strdup");
938 path_prefix = optarg;
950 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
951 "unveil", NULL) == -1)
955 char *cwd, *base, *dotgit;
956 repo_path = realpath(argv[0], NULL);
957 if (repo_path == NULL)
958 return got_error_from_errno2("realpath", argv[0]);
959 cwd = getcwd(NULL, 0);
961 error = got_error_from_errno("getcwd");
964 if (path_prefix[0]) {
965 base = basename(path_prefix);
967 error = got_error_from_errno2("basename",
972 base = basename(repo_path);
974 error = got_error_from_errno2("basename",
979 dotgit = strstr(base, ".git");
982 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
983 error = got_error_from_errno("asprintf");
988 } else if (argc == 2) {
989 repo_path = realpath(argv[0], NULL);
990 if (repo_path == NULL) {
991 error = got_error_from_errno2("realpath", argv[0]);
994 worktree_path = realpath(argv[1], NULL);
995 if (worktree_path == NULL) {
996 if (errno != ENOENT) {
997 error = got_error_from_errno2("realpath",
1001 worktree_path = strdup(argv[1]);
1002 if (worktree_path == NULL) {
1003 error = got_error_from_errno("strdup");
1010 got_path_strip_trailing_slashes(repo_path);
1011 got_path_strip_trailing_slashes(worktree_path);
1013 error = got_repo_open(&repo, repo_path, NULL);
1017 /* Pre-create work tree path for unveil(2) */
1018 error = got_path_mkdir(worktree_path);
1020 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1021 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1023 if (!got_path_dir_is_empty(worktree_path)) {
1024 error = got_error_path(worktree_path,
1025 GOT_ERR_DIR_NOT_EMPTY);
1030 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1034 error = got_ref_open(&head_ref, repo, branch_name, 0);
1038 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1039 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1042 error = got_worktree_open(&worktree, worktree_path);
1046 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1050 if (!same_path_prefix) {
1051 error = got_error(GOT_ERR_PATH_PREFIX);
1055 if (commit_id_str) {
1056 struct got_object_id *commit_id;
1057 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1060 error = check_linear_ancestry(commit_id,
1061 got_worktree_get_base_commit_id(worktree), repo);
1062 if (error != NULL) {
1066 error = check_same_branch(commit_id, head_ref, NULL, repo);
1069 error = got_worktree_set_base_commit_id(worktree, repo,
1076 error = got_pathlist_append(&paths, "", NULL);
1079 error = got_worktree_checkout_files(worktree, &paths, repo,
1080 checkout_progress, worktree_path, check_cancelled, NULL);
1084 printf("Now shut up and hack\n");
1087 got_pathlist_free(&paths);
1088 free(commit_id_str);
1090 free(worktree_path);
1097 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1102 static const struct got_error *
1103 update_progress(void *arg, unsigned char status, const char *path)
1105 int *did_something = arg;
1107 if (status == GOT_STATUS_EXISTS)
1112 /* Base commit bump happens silently. */
1113 if (status == GOT_STATUS_BUMP_BASE)
1116 while (path[0] == '/')
1118 printf("%c %s\n", status, path);
1122 static const struct got_error *
1123 switch_head_ref(struct got_reference *head_ref,
1124 struct got_object_id *commit_id, struct got_worktree *worktree,
1125 struct got_repository *repo)
1127 const struct got_error *err = NULL;
1129 int ref_has_moved = 0;
1131 /* Trivial case: switching between two different references. */
1132 if (strcmp(got_ref_get_name(head_ref),
1133 got_worktree_get_head_ref_name(worktree)) != 0) {
1134 printf("Switching work tree from %s to %s\n",
1135 got_worktree_get_head_ref_name(worktree),
1136 got_ref_get_name(head_ref));
1137 return got_worktree_set_head_ref(worktree, head_ref);
1140 err = check_linear_ancestry(commit_id,
1141 got_worktree_get_base_commit_id(worktree), repo);
1143 if (err->code != GOT_ERR_ANCESTRY)
1150 /* Switching to a rebased branch with the same reference name. */
1151 err = got_object_id_str(&base_id_str,
1152 got_worktree_get_base_commit_id(worktree));
1155 printf("Reference %s now points at a different branch\n",
1156 got_worktree_get_head_ref_name(worktree));
1157 printf("Switching work tree from %s to %s\n", base_id_str,
1158 got_worktree_get_head_ref_name(worktree));
1162 static const struct got_error *
1163 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1165 const struct got_error *err;
1168 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1172 return got_error(GOT_ERR_REBASING);
1174 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1178 return got_error(GOT_ERR_HISTEDIT_BUSY);
1183 static const struct got_error *
1184 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1185 char *argv[], struct got_worktree *worktree)
1187 const struct got_error *err = NULL;
1194 return got_error_from_errno("strdup");
1195 return got_pathlist_append(paths, path, NULL);
1198 for (i = 0; i < argc; i++) {
1199 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1202 err = got_pathlist_append(paths, path, NULL);
1212 static const struct got_error *
1213 cmd_update(int argc, char *argv[])
1215 const struct got_error *error = NULL;
1216 struct got_repository *repo = NULL;
1217 struct got_worktree *worktree = NULL;
1218 char *worktree_path = NULL;
1219 struct got_object_id *commit_id = NULL;
1220 char *commit_id_str = NULL;
1221 const char *branch_name = NULL;
1222 struct got_reference *head_ref = NULL;
1223 struct got_pathlist_head paths;
1224 struct got_pathlist_entry *pe;
1225 int ch, did_something = 0;
1229 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1232 branch_name = optarg;
1235 commit_id_str = strdup(optarg);
1236 if (commit_id_str == NULL)
1237 return got_error_from_errno("strdup");
1249 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1250 "unveil", NULL) == -1)
1253 worktree_path = getcwd(NULL, 0);
1254 if (worktree_path == NULL) {
1255 error = got_error_from_errno("getcwd");
1258 error = got_worktree_open(&worktree, worktree_path);
1262 error = check_rebase_or_histedit_in_progress(worktree);
1266 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
1271 error = apply_unveil(got_repo_get_path(repo), 0,
1272 got_worktree_get_root_path(worktree));
1276 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1280 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1281 got_worktree_get_head_ref_name(worktree), 0);
1284 if (commit_id_str == NULL) {
1285 error = got_ref_resolve(&commit_id, repo, head_ref);
1288 error = got_object_id_str(&commit_id_str, commit_id);
1292 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1293 free(commit_id_str);
1294 commit_id_str = NULL;
1297 error = got_object_id_str(&commit_id_str, commit_id);
1303 struct got_object_id *head_commit_id;
1304 TAILQ_FOREACH(pe, &paths, entry) {
1305 if (pe->path_len == 0)
1307 error = got_error_msg(GOT_ERR_BAD_PATH,
1308 "switching between branches requires that "
1309 "the entire work tree gets updated");
1312 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1315 error = check_linear_ancestry(commit_id, head_commit_id, repo);
1316 free(head_commit_id);
1319 error = check_same_branch(commit_id, head_ref, NULL, repo);
1322 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1326 error = check_linear_ancestry(commit_id,
1327 got_worktree_get_base_commit_id(worktree), repo);
1328 if (error != NULL) {
1329 if (error->code == GOT_ERR_ANCESTRY)
1330 error = got_error(GOT_ERR_BRANCH_MOVED);
1333 error = check_same_branch(commit_id, head_ref, NULL, repo);
1338 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1340 error = got_worktree_set_base_commit_id(worktree, repo,
1346 error = got_worktree_checkout_files(worktree, &paths, repo,
1347 update_progress, &did_something, check_cancelled, NULL);
1352 printf("Updated to commit %s\n", commit_id_str);
1354 printf("Already up-to-date\n");
1356 free(worktree_path);
1357 TAILQ_FOREACH(pe, &paths, entry)
1358 free((char *)pe->path);
1359 got_pathlist_free(&paths);
1361 free(commit_id_str);
1365 static const struct got_error *
1366 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
1367 const char *path, int diff_context, struct got_repository *repo)
1369 const struct got_error *err = NULL;
1370 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1373 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
1378 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
1382 while (path[0] == '/')
1384 err = got_diff_blob(blob1, blob2, path, path, diff_context,
1388 got_object_blob_close(blob1);
1389 got_object_blob_close(blob2);
1393 static const struct got_error *
1394 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
1395 const char *path, int diff_context, struct got_repository *repo)
1397 const struct got_error *err = NULL;
1398 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1399 struct got_diff_blob_output_unidiff_arg arg;
1402 err = got_object_open_as_tree(&tree1, repo, tree_id1);
1407 err = got_object_open_as_tree(&tree2, repo, tree_id2);
1411 arg.diff_context = diff_context;
1412 arg.outfile = stdout;
1413 while (path[0] == '/')
1415 err = got_diff_tree(tree1, tree2, path, path, repo,
1416 got_diff_blob_output_unidiff, &arg, 1);
1419 got_object_tree_close(tree1);
1420 got_object_tree_close(tree2);
1424 static const struct got_error *
1425 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1426 const char *path, int diff_context, struct got_repository *repo)
1428 const struct got_error *err = NULL;
1429 struct got_commit_object *pcommit = NULL;
1430 char *id_str1 = NULL, *id_str2 = NULL;
1431 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
1432 struct got_object_qid *qid;
1434 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1436 err = got_object_open_as_commit(&pcommit, repo,
1442 if (path && path[0] != '\0') {
1444 err = got_object_id_by_path(&obj_id2, repo, id, path);
1447 err = got_object_id_str(&id_str2, obj_id2);
1453 err = got_object_id_by_path(&obj_id1, repo,
1459 err = got_object_id_str(&id_str1, obj_id1);
1465 err = got_object_get_type(&obj_type, repo, obj_id2);
1470 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1472 case GOT_OBJ_TYPE_BLOB:
1473 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
1476 case GOT_OBJ_TYPE_TREE:
1477 err = diff_trees(obj_id1, obj_id2, path, diff_context,
1481 err = got_error(GOT_ERR_OBJ_TYPE);
1487 obj_id2 = got_object_commit_get_tree_id(commit);
1488 err = got_object_id_str(&id_str2, obj_id2);
1491 obj_id1 = got_object_commit_get_tree_id(pcommit);
1492 err = got_object_id_str(&id_str1, obj_id1);
1495 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1496 err = diff_trees(obj_id1, obj_id2, "", diff_context, repo);
1503 got_object_commit_close(pcommit);
1508 get_datestr(time_t *time, char *datebuf)
1510 struct tm mytm, *tm;
1513 tm = gmtime_r(time, &mytm);
1516 s = asctime_r(tm, datebuf);
1519 p = strchr(s, '\n');
1525 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1527 static const struct got_error *
1528 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1529 struct got_repository *repo, const char *path, int show_patch,
1530 int diff_context, struct got_reflist_head *refs)
1532 const struct got_error *err = NULL;
1533 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1535 time_t committer_time;
1536 const char *author, *committer;
1537 char *refs_str = NULL;
1538 struct got_reflist_entry *re;
1540 SIMPLEQ_FOREACH(re, refs, entry) {
1543 struct got_tag_object *tag = NULL;
1546 name = got_ref_get_name(re->ref);
1547 if (strcmp(name, GOT_REF_HEAD) == 0)
1549 if (strncmp(name, "refs/", 5) == 0)
1551 if (strncmp(name, "got/", 4) == 0)
1553 if (strncmp(name, "heads/", 6) == 0)
1555 if (strncmp(name, "remotes/", 8) == 0)
1557 if (strncmp(name, "tags/", 5) == 0) {
1558 err = got_object_open_as_tag(&tag, repo, re->id);
1560 if (err->code != GOT_ERR_OBJ_TYPE)
1562 /* Ref points at something other than a tag. */
1567 cmp = got_object_id_cmp(tag ?
1568 got_object_tag_get_object_id(tag) : re->id, id);
1570 got_object_tag_close(tag);
1574 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1576 err = got_error_from_errno("asprintf");
1582 err = got_object_id_str(&id_str, id);
1586 printf(GOT_COMMIT_SEP_STR);
1587 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1588 refs_str ? refs_str : "", refs_str ? ")" : "");
1593 printf("from: %s\n", got_object_commit_get_author(commit));
1594 committer_time = got_object_commit_get_committer_time(commit);
1595 datestr = get_datestr(&committer_time, datebuf);
1597 printf("date: %s UTC\n", datestr);
1598 author = got_object_commit_get_author(commit);
1599 committer = got_object_commit_get_committer(commit);
1600 if (strcmp(author, committer) != 0)
1601 printf("via: %s\n", committer);
1602 if (got_object_commit_get_nparents(commit) > 1) {
1603 const struct got_object_id_queue *parent_ids;
1604 struct got_object_qid *qid;
1606 parent_ids = got_object_commit_get_parent_ids(commit);
1607 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1608 err = got_object_id_str(&id_str, qid->id);
1611 printf("parent %d: %s\n", n++, id_str);
1616 err = got_object_commit_get_logmsg(&logmsg0, commit);
1622 line = strsep(&logmsg, "\n");
1624 printf(" %s\n", line);
1629 err = print_patch(commit, id, path, diff_context, repo);
1634 if (fflush(stdout) != 0 && err == NULL)
1635 err = got_error_from_errno("fflush");
1639 static const struct got_error *
1640 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1641 char *path, int show_patch, int diff_context, int limit,
1642 int first_parent_traversal, struct got_reflist_head *refs)
1644 const struct got_error *err;
1645 struct got_commit_graph *graph;
1647 err = got_commit_graph_open(&graph, root_id, path,
1648 first_parent_traversal, repo);
1651 err = got_commit_graph_iter_start(graph, root_id, repo,
1652 check_cancelled, NULL);
1656 struct got_commit_object *commit;
1657 struct got_object_id *id;
1659 if (sigint_received || sigpipe_received)
1662 err = got_commit_graph_iter_next(&id, graph);
1664 if (err->code == GOT_ERR_ITER_COMPLETED) {
1668 if (err->code != GOT_ERR_ITER_NEED_MORE)
1670 err = got_commit_graph_fetch_commits(graph, 1, repo,
1671 check_cancelled, NULL);
1680 err = got_object_open_as_commit(&commit, repo, id);
1683 err = print_commit(commit, id, repo, path, show_patch,
1684 diff_context, refs);
1685 got_object_commit_close(commit);
1686 if (err || (limit && --limit == 0))
1690 got_commit_graph_close(graph);
1697 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1698 "[-r repository-path] [path]\n", getprogname());
1703 get_default_log_limit(void)
1705 const char *got_default_log_limit;
1709 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
1710 if (got_default_log_limit == NULL)
1712 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
1718 static const struct got_error *
1719 cmd_log(int argc, char *argv[])
1721 const struct got_error *error;
1722 struct got_repository *repo = NULL;
1723 struct got_worktree *worktree = NULL;
1724 struct got_commit_object *commit = NULL;
1725 struct got_object_id *id = NULL;
1726 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1727 char *start_commit = NULL;
1728 int diff_context = 3, ch;
1729 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1731 struct got_reflist_head refs;
1733 SIMPLEQ_INIT(&refs);
1736 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1742 limit = get_default_log_limit();
1744 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
1750 start_commit = optarg;
1753 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1756 err(1, "-C option %s", errstr);
1759 limit = strtonum(optarg, 0, INT_MAX, &errstr);
1761 err(1, "-l option %s", errstr);
1764 first_parent_traversal = 1;
1767 repo_path = realpath(optarg, NULL);
1768 if (repo_path == NULL)
1769 err(1, "-r option");
1770 got_path_strip_trailing_slashes(repo_path);
1781 cwd = getcwd(NULL, 0);
1783 error = got_error_from_errno("getcwd");
1787 error = got_worktree_open(&worktree, cwd);
1788 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1795 error = got_error_from_errno("strdup");
1798 } else if (argc == 1) {
1800 error = got_worktree_resolve_path(&path, worktree,
1805 path = strdup(argv[0]);
1807 error = got_error_from_errno("strdup");
1814 if (repo_path == NULL) {
1815 repo_path = worktree ?
1816 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1818 if (repo_path == NULL) {
1819 error = got_error_from_errno("strdup");
1823 error = got_repo_open(&repo, repo_path, NULL);
1827 error = apply_unveil(got_repo_get_path(repo), 1,
1828 worktree ? got_worktree_get_root_path(worktree) : NULL);
1832 if (start_commit == NULL) {
1833 struct got_reference *head_ref;
1834 error = got_ref_open(&head_ref, repo,
1835 worktree ? got_worktree_get_head_ref_name(worktree)
1839 error = got_ref_resolve(&id, repo, head_ref);
1840 got_ref_close(head_ref);
1843 error = got_object_open_as_commit(&commit, repo, id);
1845 struct got_reference *ref;
1846 error = got_ref_open(&ref, repo, start_commit, 0);
1847 if (error == NULL) {
1849 error = got_ref_resolve(&id, repo, ref);
1853 error = got_object_get_type(&obj_type, repo, id);
1856 if (obj_type == GOT_OBJ_TYPE_TAG) {
1857 struct got_tag_object *tag;
1858 error = got_object_open_as_tag(&tag, repo, id);
1861 if (got_object_tag_get_object_type(tag) !=
1862 GOT_OBJ_TYPE_COMMIT) {
1863 got_object_tag_close(tag);
1864 error = got_error(GOT_ERR_OBJ_TYPE);
1868 id = got_object_id_dup(
1869 got_object_tag_get_object_id(tag));
1871 error = got_error_from_errno(
1872 "got_object_id_dup");
1873 got_object_tag_close(tag);
1876 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1877 error = got_error(GOT_ERR_OBJ_TYPE);
1880 error = got_object_open_as_commit(&commit, repo, id);
1884 if (commit == NULL) {
1885 error = got_repo_match_object_id_prefix(&id,
1886 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1895 const char *prefix = got_worktree_get_path_prefix(worktree);
1897 if (asprintf(&p, "%s%s%s", prefix,
1898 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
1899 error = got_error_from_errno("asprintf");
1902 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1905 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1910 path = in_repo_path;
1913 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
1917 error = print_commits(id, repo, path, show_patch,
1918 diff_context, limit, first_parent_traversal, &refs);
1925 got_worktree_close(worktree);
1927 const struct got_error *repo_error;
1928 repo_error = got_repo_close(repo);
1932 got_ref_list_free(&refs);
1939 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
1940 "[object1 object2 | path]\n", getprogname());
1944 struct print_diff_arg {
1945 struct got_repository *repo;
1946 struct got_worktree *worktree;
1953 static const struct got_error *
1954 print_diff(void *arg, unsigned char status, unsigned char staged_status,
1955 const char *path, struct got_object_id *blob_id,
1956 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
1958 struct print_diff_arg *a = arg;
1959 const struct got_error *err = NULL;
1960 struct got_blob_object *blob1 = NULL;
1962 char *abspath = NULL, *label1 = NULL;
1965 if (a->diff_staged) {
1966 if (staged_status != GOT_STATUS_MODIFY &&
1967 staged_status != GOT_STATUS_ADD &&
1968 staged_status != GOT_STATUS_DELETE)
1971 if (staged_status == GOT_STATUS_DELETE)
1973 if (status == GOT_STATUS_NONEXISTENT)
1974 return got_error_set_errno(ENOENT, path);
1975 if (status != GOT_STATUS_MODIFY &&
1976 status != GOT_STATUS_ADD &&
1977 status != GOT_STATUS_DELETE &&
1978 status != GOT_STATUS_CONFLICT)
1982 if (!a->header_shown) {
1983 printf("diff %s %s%s\n", a->id_str,
1984 got_worktree_get_root_path(a->worktree),
1985 a->diff_staged ? " (staged changes)" : "");
1986 a->header_shown = 1;
1989 if (a->diff_staged) {
1990 const char *label1 = NULL, *label2 = NULL;
1991 switch (staged_status) {
1992 case GOT_STATUS_MODIFY:
1996 case GOT_STATUS_ADD:
1999 case GOT_STATUS_DELETE:
2003 return got_error(GOT_ERR_FILE_STATUS);
2005 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2006 label1, label2, a->diff_context, a->repo, stdout);
2009 if (staged_status == GOT_STATUS_ADD ||
2010 staged_status == GOT_STATUS_MODIFY) {
2012 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2016 err = got_object_id_str(&id_str, staged_blob_id);
2019 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2020 err = got_error_from_errno("asprintf");
2025 } else if (status != GOT_STATUS_ADD) {
2026 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2031 if (status != GOT_STATUS_DELETE) {
2032 if (asprintf(&abspath, "%s/%s",
2033 got_worktree_get_root_path(a->worktree), path) == -1) {
2034 err = got_error_from_errno("asprintf");
2038 f2 = fopen(abspath, "r");
2040 err = got_error_from_errno2("fopen", abspath);
2043 if (lstat(abspath, &sb) == -1) {
2044 err = got_error_from_errno2("lstat", abspath);
2050 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2051 a->diff_context, stdout);
2054 got_object_blob_close(blob1);
2055 if (f2 && fclose(f2) != 0 && err == NULL)
2056 err = got_error_from_errno("fclose");
2061 static const struct got_error *
2062 match_object_id(struct got_object_id **id, char **label,
2063 const char *id_str, int obj_type, int resolve_tags,
2064 struct got_repository *repo)
2066 const struct got_error *err;
2067 struct got_tag_object *tag;
2068 struct got_reference *ref = NULL;
2074 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
2077 *id = got_object_id_dup(
2078 got_object_tag_get_object_id(tag));
2080 err = got_error_from_errno("got_object_id_dup");
2081 else if (asprintf(label, "refs/tags/%s",
2082 got_object_tag_get_name(tag)) == -1) {
2083 err = got_error_from_errno("asprintf");
2087 got_object_tag_close(tag);
2089 } else if (err->code != GOT_ERR_NO_OBJ)
2093 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
2095 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2097 err = got_ref_open(&ref, repo, id_str, 0);
2100 *label = strdup(got_ref_get_name(ref));
2101 if (*label == NULL) {
2102 err = got_error_from_errno("strdup");
2105 err = got_ref_resolve(id, repo, ref);
2107 err = got_object_id_str(label, *id);
2108 if (*label == NULL) {
2109 err = got_error_from_errno("strdup");
2120 static const struct got_error *
2121 cmd_diff(int argc, char *argv[])
2123 const struct got_error *error;
2124 struct got_repository *repo = NULL;
2125 struct got_worktree *worktree = NULL;
2126 char *cwd = NULL, *repo_path = NULL;
2127 struct got_object_id *id1 = NULL, *id2 = NULL;
2128 const char *id_str1 = NULL, *id_str2 = NULL;
2129 char *label1 = NULL, *label2 = NULL;
2131 int diff_context = 3, diff_staged = 0, ch;
2136 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2141 while ((ch = getopt(argc, argv, "C:r:s")) != -1) {
2144 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
2146 err(1, "-C option %s", errstr);
2149 repo_path = realpath(optarg, NULL);
2150 if (repo_path == NULL)
2151 err(1, "-r option");
2152 got_path_strip_trailing_slashes(repo_path);
2166 cwd = getcwd(NULL, 0);
2168 error = got_error_from_errno("getcwd");
2171 error = got_worktree_open(&worktree, cwd);
2172 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2175 if (worktree == NULL) {
2176 error = got_error(GOT_ERR_NOT_WORKTREE);
2181 "-r option can't be used when diffing a work tree");
2182 repo_path = strdup(got_worktree_get_repo_path(worktree));
2183 if (repo_path == NULL) {
2184 error = got_error_from_errno("strdup");
2188 error = got_worktree_resolve_path(&path, worktree,
2195 error = got_error_from_errno("strdup");
2199 } else if (argc == 2) {
2201 errx(1, "-s option can't be used when diffing "
2202 "objects in repository");
2205 if (worktree && repo_path == NULL) {
2207 strdup(got_worktree_get_repo_path(worktree));
2208 if (repo_path == NULL) {
2209 error = got_error_from_errno("strdup");
2216 if (repo_path == NULL) {
2217 repo_path = getcwd(NULL, 0);
2218 if (repo_path == NULL)
2219 return got_error_from_errno("getcwd");
2222 error = got_repo_open(&repo, repo_path, NULL);
2227 error = apply_unveil(got_repo_get_path(repo), 1,
2228 worktree ? got_worktree_get_root_path(worktree) : NULL);
2233 struct print_diff_arg arg;
2234 struct got_pathlist_head paths;
2239 error = got_object_id_str(&id_str,
2240 got_worktree_get_base_commit_id(worktree));
2244 arg.worktree = worktree;
2245 arg.diff_context = diff_context;
2246 arg.id_str = id_str;
2247 arg.header_shown = 0;
2248 arg.diff_staged = diff_staged;
2250 error = got_pathlist_append(&paths, path, NULL);
2254 error = got_worktree_status(worktree, &paths, repo, print_diff,
2255 &arg, check_cancelled, NULL);
2257 got_pathlist_free(&paths);
2261 error = match_object_id(&id1, &label1, id_str1, GOT_OBJ_TYPE_ANY, 1,
2266 error = match_object_id(&id2, &label2, id_str2, GOT_OBJ_TYPE_ANY, 1,
2271 error = got_object_get_type(&type1, repo, id1);
2275 error = got_object_get_type(&type2, repo, id2);
2279 if (type1 != type2) {
2280 error = got_error(GOT_ERR_OBJ_TYPE);
2285 case GOT_OBJ_TYPE_BLOB:
2286 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2287 diff_context, repo, stdout);
2289 case GOT_OBJ_TYPE_TREE:
2290 error = got_diff_objects_as_trees(id1, id2, "", "",
2291 diff_context, repo, stdout);
2293 case GOT_OBJ_TYPE_COMMIT:
2294 printf("diff %s %s\n", label1, label2);
2295 error = got_diff_objects_as_commits(id1, id2, diff_context,
2299 error = got_error(GOT_ERR_OBJ_TYPE);
2309 got_worktree_close(worktree);
2311 const struct got_error *repo_error;
2312 repo_error = got_repo_close(repo);
2323 "usage: %s blame [-c commit] [-r repository-path] path\n",
2332 char datebuf[9]; /* YY-MM-DD + NUL */
2335 struct blame_cb_args {
2336 struct blame_line *lines;
2340 off_t *line_offsets;
2342 struct got_repository *repo;
2345 static const struct got_error *
2346 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2348 const struct got_error *err = NULL;
2349 struct blame_cb_args *a = arg;
2350 struct blame_line *bline;
2352 size_t linesize = 0;
2353 struct got_commit_object *commit = NULL;
2356 time_t committer_time;
2358 if (nlines != a->nlines ||
2359 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2360 return got_error(GOT_ERR_RANGE);
2362 if (sigint_received)
2363 return got_error(GOT_ERR_ITER_COMPLETED);
2366 return NULL; /* no change in this commit */
2368 /* Annotate this line. */
2369 bline = &a->lines[lineno - 1];
2370 if (bline->annotated)
2372 err = got_object_id_str(&bline->id_str, id);
2376 err = got_object_open_as_commit(&commit, a->repo, id);
2380 bline->committer = strdup(got_object_commit_get_committer(commit));
2381 if (bline->committer == NULL) {
2382 err = got_error_from_errno("strdup");
2386 committer_time = got_object_commit_get_committer_time(commit);
2387 if (localtime_r(&committer_time, &tm) == NULL)
2388 return got_error_from_errno("localtime_r");
2389 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%g/%m/%d",
2390 &tm) >= sizeof(bline->datebuf)) {
2391 err = got_error(GOT_ERR_NO_SPACE);
2394 bline->annotated = 1;
2396 /* Print lines annotated so far. */
2397 bline = &a->lines[a->lineno_cur - 1];
2398 if (!bline->annotated)
2401 offset = a->line_offsets[a->lineno_cur - 1];
2402 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2403 err = got_error_from_errno("fseeko");
2407 while (bline->annotated) {
2408 char *smallerthan, *at, *nl, *committer;
2411 if (getline(&line, &linesize, a->f) == (ssize_t)-1) {
2413 err = got_error_from_errno("getline");
2417 committer = bline->committer;
2418 smallerthan = strchr(committer, '<');
2419 if (smallerthan && smallerthan[1] != '\0')
2420 committer = smallerthan + 1;
2421 at = strchr(committer, '@');
2424 len = strlen(committer);
2426 committer[8] = '\0';
2428 nl = strchr(line, '\n');
2431 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2432 bline->id_str, bline->datebuf, committer, line);
2435 bline = &a->lines[a->lineno_cur - 1];
2439 got_object_commit_close(commit);
2444 static const struct got_error *
2445 cmd_blame(int argc, char *argv[])
2447 const struct got_error *error;
2448 struct got_repository *repo = NULL;
2449 struct got_worktree *worktree = NULL;
2450 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2451 struct got_object_id *obj_id = NULL;
2452 struct got_object_id *commit_id = NULL;
2453 struct got_blob_object *blob = NULL;
2454 char *commit_id_str = NULL;
2455 struct blame_cb_args bca;
2456 int ch, obj_type, i;
2459 memset(&bca, 0, sizeof(bca));
2462 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2467 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2470 commit_id_str = optarg;
2473 repo_path = realpath(optarg, NULL);
2474 if (repo_path == NULL)
2475 err(1, "-r option");
2476 got_path_strip_trailing_slashes(repo_path);
2492 cwd = getcwd(NULL, 0);
2494 error = got_error_from_errno("getcwd");
2497 if (repo_path == NULL) {
2498 error = got_worktree_open(&worktree, cwd);
2499 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2505 strdup(got_worktree_get_repo_path(worktree));
2506 if (repo_path == NULL)
2507 error = got_error_from_errno("strdup");
2511 repo_path = strdup(cwd);
2512 if (repo_path == NULL) {
2513 error = got_error_from_errno("strdup");
2519 error = got_repo_open(&repo, repo_path, NULL);
2523 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2528 const char *prefix = got_worktree_get_path_prefix(worktree);
2529 char *p, *worktree_subdir = cwd +
2530 strlen(got_worktree_get_root_path(worktree));
2531 if (asprintf(&p, "%s%s%s%s%s",
2532 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2533 worktree_subdir, worktree_subdir[0] ? "/" : "",
2535 error = got_error_from_errno("asprintf");
2538 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2541 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2546 if (commit_id_str == NULL) {
2547 struct got_reference *head_ref;
2548 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2551 error = got_ref_resolve(&commit_id, repo, head_ref);
2552 got_ref_close(head_ref);
2556 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2561 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2564 if (obj_id == NULL) {
2565 error = got_error(GOT_ERR_NO_OBJ);
2569 error = got_object_get_type(&obj_type, repo, obj_id);
2573 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2574 error = got_error(GOT_ERR_OBJ_TYPE);
2578 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2581 bca.f = got_opentemp();
2582 if (bca.f == NULL) {
2583 error = got_error_from_errno("got_opentemp");
2586 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2587 &bca.line_offsets, bca.f, blob);
2588 if (error || bca.nlines == 0)
2591 /* Don't include \n at EOF in the blame line count. */
2592 if (bca.line_offsets[bca.nlines - 1] == filesize)
2595 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2596 if (bca.lines == NULL) {
2597 error = got_error_from_errno("calloc");
2601 bca.nlines_prec = 0;
2609 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2610 check_cancelled, NULL);
2620 got_object_blob_close(blob);
2622 got_worktree_close(worktree);
2624 const struct got_error *repo_error;
2625 repo_error = got_repo_close(repo);
2629 for (i = 0; i < bca.nlines; i++) {
2630 struct blame_line *bline = &bca.lines[i];
2631 free(bline->id_str);
2632 free(bline->committer);
2635 free(bca.line_offsets);
2636 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2637 error = got_error_from_errno("fclose");
2645 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2651 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2652 const char *root_path)
2654 int is_root_path = (strcmp(path, root_path) == 0);
2655 const char *modestr = "";
2657 path += strlen(root_path);
2658 while (path[0] == '/')
2661 if (got_object_tree_entry_is_submodule(te))
2663 else if (S_ISLNK(te->mode))
2665 else if (S_ISDIR(te->mode))
2667 else if (te->mode & S_IXUSR)
2670 printf("%s%s%s%s%s\n", id ? id : "", path,
2671 is_root_path ? "" : "/", te->name, modestr);
2674 static const struct got_error *
2675 print_tree(const char *path, struct got_object_id *commit_id,
2676 int show_ids, int recurse, const char *root_path,
2677 struct got_repository *repo)
2679 const struct got_error *err = NULL;
2680 struct got_object_id *tree_id = NULL;
2681 struct got_tree_object *tree = NULL;
2682 const struct got_tree_entries *entries;
2683 struct got_tree_entry *te;
2685 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2689 err = got_object_open_as_tree(&tree, repo, tree_id);
2692 entries = got_object_tree_get_entries(tree);
2693 te = SIMPLEQ_FIRST(&entries->head);
2697 if (sigint_received || sigpipe_received)
2702 err = got_object_id_str(&id_str, te->id);
2705 if (asprintf(&id, "%s ", id_str) == -1) {
2706 err = got_error_from_errno("asprintf");
2712 print_entry(te, id, path, root_path);
2715 if (recurse && S_ISDIR(te->mode)) {
2717 if (asprintf(&child_path, "%s%s%s", path,
2718 path[0] == '/' && path[1] == '\0' ? "" : "/",
2720 err = got_error_from_errno("asprintf");
2723 err = print_tree(child_path, commit_id, show_ids, 1,
2730 te = SIMPLEQ_NEXT(te, entry);
2734 got_object_tree_close(tree);
2739 static const struct got_error *
2740 cmd_tree(int argc, char *argv[])
2742 const struct got_error *error;
2743 struct got_repository *repo = NULL;
2744 struct got_worktree *worktree = NULL;
2746 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2747 struct got_object_id *commit_id = NULL;
2748 char *commit_id_str = NULL;
2749 int show_ids = 0, recurse = 0;
2753 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2758 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2761 commit_id_str = optarg;
2764 repo_path = realpath(optarg, NULL);
2765 if (repo_path == NULL)
2766 err(1, "-r option");
2767 got_path_strip_trailing_slashes(repo_path);
2791 cwd = getcwd(NULL, 0);
2793 error = got_error_from_errno("getcwd");
2796 if (repo_path == NULL) {
2797 error = got_worktree_open(&worktree, cwd);
2798 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2804 strdup(got_worktree_get_repo_path(worktree));
2805 if (repo_path == NULL)
2806 error = got_error_from_errno("strdup");
2810 repo_path = strdup(cwd);
2811 if (repo_path == NULL) {
2812 error = got_error_from_errno("strdup");
2818 error = got_repo_open(&repo, repo_path, NULL);
2822 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2828 char *p, *worktree_subdir = cwd +
2829 strlen(got_worktree_get_root_path(worktree));
2830 if (asprintf(&p, "%s/%s",
2831 got_worktree_get_path_prefix(worktree),
2832 worktree_subdir) == -1) {
2833 error = got_error_from_errno("asprintf");
2836 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2843 if (in_repo_path == NULL) {
2844 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2849 if (commit_id_str == NULL) {
2850 struct got_reference *head_ref;
2851 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2854 error = got_ref_resolve(&commit_id, repo, head_ref);
2855 got_ref_close(head_ref);
2859 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2864 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2865 in_repo_path, repo);
2872 got_worktree_close(worktree);
2874 const struct got_error *repo_error;
2875 repo_error = got_repo_close(repo);
2885 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
2889 static const struct got_error *
2890 print_status(void *arg, unsigned char status, unsigned char staged_status,
2891 const char *path, struct got_object_id *blob_id,
2892 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
2894 if (status == staged_status && (status == GOT_STATUS_DELETE))
2895 status = GOT_STATUS_NO_CHANGE;
2896 printf("%c%c %s\n", status, staged_status, path);
2900 static const struct got_error *
2901 cmd_status(int argc, char *argv[])
2903 const struct got_error *error = NULL;
2904 struct got_repository *repo = NULL;
2905 struct got_worktree *worktree = NULL;
2907 struct got_pathlist_head paths;
2908 struct got_pathlist_entry *pe;
2913 while ((ch = getopt(argc, argv, "")) != -1) {
2925 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2929 cwd = getcwd(NULL, 0);
2931 error = got_error_from_errno("getcwd");
2935 error = got_worktree_open(&worktree, cwd);
2939 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
2944 error = apply_unveil(got_repo_get_path(repo), 1,
2945 got_worktree_get_root_path(worktree));
2949 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2953 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
2954 check_cancelled, NULL);
2956 TAILQ_FOREACH(pe, &paths, entry)
2957 free((char *)pe->path);
2958 got_pathlist_free(&paths);
2967 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
2972 static const struct got_error *
2973 list_refs(struct got_repository *repo)
2975 static const struct got_error *err = NULL;
2976 struct got_reflist_head refs;
2977 struct got_reflist_entry *re;
2979 SIMPLEQ_INIT(&refs);
2980 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2984 SIMPLEQ_FOREACH(re, &refs, entry) {
2986 refstr = got_ref_to_str(re->ref);
2988 return got_error_from_errno("got_ref_to_str");
2989 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2993 got_ref_list_free(&refs);
2997 static const struct got_error *
2998 delete_ref(struct got_repository *repo, const char *refname)
3000 const struct got_error *err = NULL;
3001 struct got_reference *ref;
3003 err = got_ref_open(&ref, repo, refname, 0);
3007 err = got_ref_delete(ref, repo);
3012 static const struct got_error *
3013 add_ref(struct got_repository *repo, const char *refname, const char *target)
3015 const struct got_error *err = NULL;
3016 struct got_object_id *id;
3017 struct got_reference *ref = NULL;
3020 * Don't let the user create a reference named '-'.
3021 * While technically a valid reference name, this case is usually
3022 * an unintended typo.
3024 if (refname[0] == '-' && refname[1] == '\0')
3025 return got_error(GOT_ERR_BAD_REF_NAME);
3027 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3030 struct got_reference *target_ref;
3032 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3034 err = got_ref_open(&target_ref, repo, target, 0);
3037 err = got_ref_resolve(&id, repo, target_ref);
3038 got_ref_close(target_ref);
3043 err = got_ref_alloc(&ref, refname, id);
3047 err = got_ref_write(ref, repo);
3055 static const struct got_error *
3056 add_symref(struct got_repository *repo, const char *refname, const char *target)
3058 const struct got_error *err = NULL;
3059 struct got_reference *ref = NULL;
3060 struct got_reference *target_ref = NULL;
3063 * Don't let the user create a reference named '-'.
3064 * While technically a valid reference name, this case is usually
3065 * an unintended typo.
3067 if (refname[0] == '-' && refname[1] == '\0')
3068 return got_error(GOT_ERR_BAD_REF_NAME);
3070 err = got_ref_open(&target_ref, repo, target, 0);
3074 err = got_ref_alloc_symref(&ref, refname, target_ref);
3078 err = got_ref_write(ref, repo);
3081 got_ref_close(target_ref);
3087 static const struct got_error *
3088 cmd_ref(int argc, char *argv[])
3090 const struct got_error *error = NULL;
3091 struct got_repository *repo = NULL;
3092 struct got_worktree *worktree = NULL;
3093 char *cwd = NULL, *repo_path = NULL;
3094 int ch, do_list = 0, create_symref = 0;
3095 const char *delref = NULL;
3097 /* TODO: Add -s option for adding symbolic references. */
3098 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3104 repo_path = realpath(optarg, NULL);
3105 if (repo_path == NULL)
3106 err(1, "-r option");
3107 got_path_strip_trailing_slashes(repo_path);
3121 if (do_list && delref)
3122 errx(1, "-l and -d options are mutually exclusive\n");
3127 if (do_list || delref) {
3129 errx(1, "-s option cannot be used together with the "
3130 "-l or -d options");
3133 } else if (argc != 2)
3138 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3142 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3143 "sendfd unveil", NULL) == -1)
3147 cwd = getcwd(NULL, 0);
3149 error = got_error_from_errno("getcwd");
3153 if (repo_path == NULL) {
3154 error = got_worktree_open(&worktree, cwd);
3155 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3161 strdup(got_worktree_get_repo_path(worktree));
3162 if (repo_path == NULL)
3163 error = got_error_from_errno("strdup");
3167 repo_path = strdup(cwd);
3168 if (repo_path == NULL) {
3169 error = got_error_from_errno("strdup");
3175 error = got_repo_open(&repo, repo_path, NULL);
3179 error = apply_unveil(got_repo_get_path(repo), do_list,
3180 worktree ? got_worktree_get_root_path(worktree) : NULL);
3185 error = list_refs(repo);
3187 error = delete_ref(repo, delref);
3188 else if (create_symref)
3189 error = add_symref(repo, argv[0], argv[1]);
3191 error = add_ref(repo, argv[0], argv[1]);
3194 got_repo_close(repo);
3196 got_worktree_close(worktree);
3206 "usage: %s branch [-r repository] -l | -d name | "
3207 "name [commit]\n", getprogname());
3211 static const struct got_error *
3212 list_branches(struct got_repository *repo, struct got_worktree *worktree)
3214 static const struct got_error *err = NULL;
3215 struct got_reflist_head refs;
3216 struct got_reflist_entry *re;
3218 SIMPLEQ_INIT(&refs);
3220 err = got_ref_list(&refs, repo, "refs/heads",
3221 got_ref_cmp_by_name, NULL);
3225 SIMPLEQ_FOREACH(re, &refs, entry) {
3226 const char *refname, *marker = " ";
3228 refname = got_ref_get_name(re->ref);
3229 if (worktree && strcmp(refname,
3230 got_worktree_get_head_ref_name(worktree)) == 0) {
3231 struct got_object_id *id = NULL;
3232 err = got_ref_resolve(&id, repo, re->ref);
3235 if (got_object_id_cmp(id,
3236 got_worktree_get_base_commit_id(worktree)) == 0)
3242 refname += strlen("refs/heads/");
3243 refstr = got_ref_to_str(re->ref);
3245 return got_error_from_errno("got_ref_to_str");
3246 printf("%s%s: %s\n", marker, refname, refstr);
3250 got_ref_list_free(&refs);
3254 static const struct got_error *
3255 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
3256 const char *branch_name)
3258 const struct got_error *err = NULL;
3259 struct got_reference *ref = NULL;
3262 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3263 return got_error_from_errno("asprintf");
3265 err = got_ref_open(&ref, repo, refname, 0);
3270 strcmp(got_worktree_get_head_ref_name(worktree),
3271 got_ref_get_name(ref)) == 0) {
3272 err = got_error_msg(GOT_ERR_SAME_BRANCH,
3273 "will not delete this work tree's current branch");
3277 err = got_ref_delete(ref, repo);
3285 static const struct got_error *
3286 add_branch(struct got_repository *repo, const char *branch_name,
3287 const char *base_branch)
3289 const struct got_error *err = NULL;
3290 struct got_object_id *id = NULL;
3292 struct got_reference *ref = NULL;
3293 char *base_refname = NULL, *refname = NULL;
3296 * Don't let the user create a branch named '-'.
3297 * While technically a valid reference name, this case is usually
3298 * an unintended typo.
3300 if (branch_name[0] == '-' && branch_name[1] == '\0')
3301 return got_error(GOT_ERR_BAD_REF_NAME);
3303 err = match_object_id(&id, &label, base_branch,
3304 GOT_OBJ_TYPE_COMMIT, 1, repo);
3308 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3309 err = got_error_from_errno("asprintf");
3313 err = got_ref_open(&ref, repo, refname, 0);
3315 err = got_error(GOT_ERR_BRANCH_EXISTS);
3317 } else if (err->code != GOT_ERR_NOT_REF)
3320 err = got_ref_alloc(&ref, refname, id);
3324 err = got_ref_write(ref, repo);
3334 static const struct got_error *
3335 cmd_branch(int argc, char *argv[])
3337 const struct got_error *error = NULL;
3338 struct got_repository *repo = NULL;
3339 struct got_worktree *worktree = NULL;
3340 char *cwd = NULL, *repo_path = NULL;
3341 int ch, do_list = 0;
3342 const char *delref = NULL;
3344 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
3350 repo_path = realpath(optarg, NULL);
3351 if (repo_path == NULL)
3352 err(1, "-r option");
3353 got_path_strip_trailing_slashes(repo_path);
3364 if (do_list && delref)
3365 errx(1, "-l and -d options are mutually exclusive\n");
3370 if (do_list || delref) {
3373 } else if (argc < 1 || argc > 2)
3378 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3382 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3383 "sendfd unveil", NULL) == -1)
3387 cwd = getcwd(NULL, 0);
3389 error = got_error_from_errno("getcwd");
3393 if (repo_path == NULL) {
3394 error = got_worktree_open(&worktree, cwd);
3395 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3401 strdup(got_worktree_get_repo_path(worktree));
3402 if (repo_path == NULL)
3403 error = got_error_from_errno("strdup");
3407 repo_path = strdup(cwd);
3408 if (repo_path == NULL) {
3409 error = got_error_from_errno("strdup");
3415 error = got_repo_open(&repo, repo_path, NULL);
3419 error = apply_unveil(got_repo_get_path(repo), do_list,
3420 worktree ? got_worktree_get_root_path(worktree) : NULL);
3425 error = list_branches(repo, worktree);
3427 error = delete_branch(repo, worktree, delref);
3429 const char *base_branch;
3431 base_branch = worktree ?
3432 got_worktree_get_head_ref_name(worktree) :
3434 if (strncmp(base_branch, "refs/heads/", 11) == 0)
3437 base_branch = argv[1];
3438 error = add_branch(repo, argv[0], base_branch);
3442 got_repo_close(repo);
3444 got_worktree_close(worktree);
3455 "usage: %s tag [-r repository] | -l | "
3456 "[-m message] name [commit]\n", getprogname());
3461 static const struct got_error *
3462 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
3464 const struct got_error *err = NULL;
3465 struct got_reflist_entry *re, *se, *new;
3466 struct got_object_id *re_id, *se_id;
3467 struct got_tag_object *re_tag, *se_tag;
3468 time_t re_time, se_time;
3470 SIMPLEQ_FOREACH(re, tags, entry) {
3471 se = SIMPLEQ_FIRST(sorted);
3473 err = got_reflist_entry_dup(&new, re);
3476 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
3479 err = got_ref_resolve(&re_id, repo, re->ref);
3482 err = got_object_open_as_tag(&re_tag, repo, re_id);
3486 re_time = got_object_tag_get_tagger_time(re_tag);
3487 got_object_tag_close(re_tag);
3491 err = got_ref_resolve(&se_id, repo, re->ref);
3494 err = got_object_open_as_tag(&se_tag, repo, se_id);
3498 se_time = got_object_tag_get_tagger_time(se_tag);
3499 got_object_tag_close(se_tag);
3501 if (se_time > re_time) {
3502 err = got_reflist_entry_dup(&new, re);
3505 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
3508 se = SIMPLEQ_NEXT(se, entry);
3517 static const struct got_error *
3518 cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
3519 struct got_reference *ref2)
3521 const struct got_error *err = NULL;
3522 struct got_repository *repo = arg;
3523 struct got_object_id *id1, *id2 = NULL;
3524 struct got_tag_object *tag1 = NULL, *tag2 = NULL;
3525 time_t time1, time2;
3529 err = got_ref_resolve(&id1, repo, ref1);
3532 err = got_object_open_as_tag(&tag1, repo, id1);
3536 err = got_ref_resolve(&id2, repo, ref2);
3539 err = got_object_open_as_tag(&tag2, repo, id2);
3543 time1 = got_object_tag_get_tagger_time(tag1);
3544 time2 = got_object_tag_get_tagger_time(tag2);
3546 /* Put latest tags first. */
3549 else if (time1 > time2)
3552 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
3557 got_object_tag_close(tag1);
3559 got_object_tag_close(tag2);
3563 static const struct got_error *
3564 list_tags(struct got_repository *repo, struct got_worktree *worktree)
3566 static const struct got_error *err = NULL;
3567 struct got_reflist_head refs;
3568 struct got_reflist_entry *re;
3570 SIMPLEQ_INIT(&refs);
3572 err = got_ref_list(&refs, repo, "refs/tags", cmp_tags, repo);
3576 SIMPLEQ_FOREACH(re, &refs, entry) {
3577 const char *refname;
3578 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
3581 struct got_object_id *id;
3582 struct got_tag_object *tag;
3584 refname = got_ref_get_name(re->ref);
3585 if (strncmp(refname, "refs/tags/", 10) != 0)
3588 refstr = got_ref_to_str(re->ref);
3589 if (refstr == NULL) {
3590 err = got_error_from_errno("got_ref_to_str");
3593 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
3596 err = got_ref_resolve(&id, repo, re->ref);
3599 err = got_object_open_as_tag(&tag, repo, id);
3603 printf("from: %s\n", got_object_tag_get_tagger(tag));
3604 tagger_time = got_object_tag_get_tagger_time(tag);
3605 datestr = get_datestr(&tagger_time, datebuf);
3607 printf("date: %s UTC\n", datestr);
3608 err = got_object_id_str(&id_str,
3609 got_object_tag_get_object_id(tag));
3612 switch (got_object_tag_get_object_type(tag)) {
3613 case GOT_OBJ_TYPE_BLOB:
3614 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB, id_str);
3616 case GOT_OBJ_TYPE_TREE:
3617 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE, id_str);
3619 case GOT_OBJ_TYPE_COMMIT:
3620 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
3622 case GOT_OBJ_TYPE_TAG:
3623 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG, id_str);
3629 tagmsg0 = strdup(got_object_tag_get_message(tag));
3630 got_object_tag_close(tag);
3631 if (tagmsg0 == NULL) {
3632 err = got_error_from_errno("strdup");
3638 line = strsep(&tagmsg, "\n");
3640 printf(" %s\n", line);
3645 got_ref_list_free(&refs);
3649 static const struct got_error *
3650 get_tag_message(char **tagmsg, const char *commit_id_str,
3651 const char *tag_name, const char *repo_path)
3653 const struct got_error *err = NULL;
3654 char *template = NULL, *initial_content = NULL;
3655 char *tagmsg_path = NULL, *editor = NULL;
3658 if (asprintf(&template, "/tmp/got-tagmsg") == -1) {
3659 err = got_error_from_errno("asprintf");
3663 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
3664 commit_id_str, tag_name) == -1) {
3665 err = got_error_from_errno("asprintf");
3669 err = got_opentemp_named_fd(&tagmsg_path, &fd, template);
3673 dprintf(fd, initial_content);
3676 err = get_editor(&editor);
3679 err = edit_logmsg(tagmsg, editor, tagmsg_path, initial_content);
3681 if (err == NULL || err->code == GOT_ERR_COMMIT_MSG_EMPTY) {
3682 unlink(tagmsg_path);
3686 free(initial_content);
3690 /* Editor is done; we can now apply unveil(2) */
3692 err = apply_unveil(repo_path, 0, NULL);
3701 static const struct got_error *
3702 add_tag(struct got_repository *repo, const char *tag_name,
3703 const char *commit_arg, const char *tagmsg_arg)
3705 const struct got_error *err = NULL;
3706 struct got_object_id *commit_id = NULL, *tag_id = NULL;
3707 char *label = NULL, *commit_id_str = NULL;
3708 struct got_reference *ref = NULL;
3709 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
3712 * Don't let the user create a tag named '-'.
3713 * While technically a valid reference name, this case is usually
3714 * an unintended typo.
3716 if (tag_name[0] == '-' && tag_name[1] == '\0')
3717 return got_error(GOT_ERR_BAD_REF_NAME);
3719 err = get_author(&tagger, repo);
3723 err = match_object_id(&commit_id, &label, commit_arg,
3724 GOT_OBJ_TYPE_COMMIT, 1, repo);
3728 err = got_object_id_str(&commit_id_str, commit_id);
3732 if (strncmp("refs/tags/", tag_name, 10) == 0) {
3733 refname = strdup(tag_name);
3734 if (refname == NULL) {
3735 err = got_error_from_errno("strdup");
3739 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
3740 err = got_error_from_errno("asprintf");
3744 err = got_ref_open(&ref, repo, refname, 0);
3746 err = got_error(GOT_ERR_TAG_EXISTS);
3748 } else if (err->code != GOT_ERR_NOT_REF)
3751 if (tagmsg_arg == NULL) {
3752 err = get_tag_message(&tagmsg, commit_id_str,
3753 tag_name, got_repo_get_path(repo));
3758 err = got_object_tag_create(&tag_id, tag_name, commit_id,
3759 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
3763 err = got_ref_alloc(&ref, refname, tag_id);
3767 err = got_ref_write(ref, repo);
3771 err = got_object_id_str(&tag_id_str, tag_id);
3772 printf("Created tag %s\n", tag_id_str);
3779 free(commit_id_str);
3786 static const struct got_error *
3787 cmd_tag(int argc, char *argv[])
3789 const struct got_error *error = NULL;
3790 struct got_repository *repo = NULL;
3791 struct got_worktree *worktree = NULL;
3792 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
3793 char *gitconfig_path = NULL;
3794 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
3795 int ch, do_list = 0;
3797 while ((ch = getopt(argc, argv, "m:r:l")) != -1) {
3803 repo_path = realpath(optarg, NULL);
3804 if (repo_path == NULL)
3805 err(1, "-r option");
3806 got_path_strip_trailing_slashes(repo_path);
3822 errx(1, "-l and -m options are mutually exclusive\n");
3825 } else if (argc < 1 || argc > 2)
3828 commit_id_arg = argv[1];
3833 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3837 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3838 "sendfd unveil", NULL) == -1)
3842 cwd = getcwd(NULL, 0);
3844 error = got_error_from_errno("getcwd");
3848 if (repo_path == NULL) {
3849 error = got_worktree_open(&worktree, cwd);
3850 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3856 strdup(got_worktree_get_repo_path(worktree));
3857 if (repo_path == NULL)
3858 error = got_error_from_errno("strdup");
3862 repo_path = strdup(cwd);
3863 if (repo_path == NULL) {
3864 error = got_error_from_errno("strdup");
3871 error = got_repo_open(&repo, repo_path, NULL);
3874 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3877 error = list_tags(repo, worktree);
3879 error = get_gitconfig_path(&gitconfig_path);
3882 error = got_repo_open(&repo, repo_path, gitconfig_path);
3887 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3892 if (commit_id_arg == NULL) {
3893 struct got_reference *head_ref;
3894 struct got_object_id *commit_id;
3895 error = got_ref_open(&head_ref, repo,
3896 worktree ? got_worktree_get_head_ref_name(worktree)
3900 error = got_ref_resolve(&commit_id, repo, head_ref);
3901 got_ref_close(head_ref);
3904 error = got_object_id_str(&commit_id_str, commit_id);
3910 error = add_tag(repo, tag_name,
3911 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
3915 got_repo_close(repo);
3917 got_worktree_close(worktree);
3920 free(gitconfig_path);
3921 free(commit_id_str);
3928 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
3932 static const struct got_error *
3933 cmd_add(int argc, char *argv[])
3935 const struct got_error *error = NULL;
3936 struct got_repository *repo = NULL;
3937 struct got_worktree *worktree = NULL;
3939 struct got_pathlist_head paths;
3940 struct got_pathlist_entry *pe;
3945 while ((ch = getopt(argc, argv, "")) != -1) {
3957 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3964 cwd = getcwd(NULL, 0);
3966 error = got_error_from_errno("getcwd");
3970 error = got_worktree_open(&worktree, cwd);
3974 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3979 error = apply_unveil(got_repo_get_path(repo), 1,
3980 got_worktree_get_root_path(worktree));
3984 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3988 error = got_worktree_schedule_add(worktree, &paths, print_status,
3992 got_repo_close(repo);
3994 got_worktree_close(worktree);
3995 TAILQ_FOREACH(pe, &paths, entry)
3996 free((char *)pe->path);
3997 got_pathlist_free(&paths);
4005 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
4009 static const struct got_error *
4010 print_remove_status(void *arg, unsigned char status,
4011 unsigned char staged_status, const char *path,
4012 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4013 struct got_object_id *commit_id)
4015 if (status == GOT_STATUS_NONEXISTENT)
4017 if (status == staged_status && (status == GOT_STATUS_DELETE))
4018 status = GOT_STATUS_NO_CHANGE;
4019 printf("%c%c %s\n", status, staged_status, path);
4023 static const struct got_error *
4024 cmd_remove(int argc, char *argv[])
4026 const struct got_error *error = NULL;
4027 struct got_worktree *worktree = NULL;
4028 struct got_repository *repo = NULL;
4030 struct got_pathlist_head paths;
4031 struct got_pathlist_entry *pe;
4032 int ch, delete_local_mods = 0;
4036 while ((ch = getopt(argc, argv, "f")) != -1) {
4039 delete_local_mods = 1;
4051 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4058 cwd = getcwd(NULL, 0);
4060 error = got_error_from_errno("getcwd");
4063 error = got_worktree_open(&worktree, cwd);
4067 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4072 error = apply_unveil(got_repo_get_path(repo), 1,
4073 got_worktree_get_root_path(worktree));
4077 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4081 error = got_worktree_schedule_delete(worktree, &paths,
4082 delete_local_mods, print_remove_status, NULL, repo);
4087 got_repo_close(repo);
4089 got_worktree_close(worktree);
4090 TAILQ_FOREACH(pe, &paths, entry)
4091 free((char *)pe->path);
4092 got_pathlist_free(&paths);
4100 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4101 "path ...\n", getprogname());
4105 static const struct got_error *
4106 revert_progress(void *arg, unsigned char status, const char *path)
4108 while (path[0] == '/')
4110 printf("%c %s\n", status, path);
4114 struct choose_patch_arg {
4115 FILE *patch_script_file;
4119 static const struct got_error *
4120 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4121 int nchanges, const char *action)
4124 size_t linesize = 0;
4128 case GOT_STATUS_ADD:
4129 printf("A %s\n%s this addition? [y/n] ", path, action);
4131 case GOT_STATUS_DELETE:
4132 printf("D %s\n%s this deletion? [y/n] ", path, action);
4134 case GOT_STATUS_MODIFY:
4135 if (fseek(patch_file, 0L, SEEK_SET) == -1)
4136 return got_error_from_errno("fseek");
4137 printf(GOT_COMMIT_SEP_STR);
4138 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4140 if (ferror(patch_file))
4141 return got_error_from_errno("getline");
4142 printf(GOT_COMMIT_SEP_STR);
4143 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4144 path, n, nchanges, action);
4147 return got_error_path(path, GOT_ERR_FILE_STATUS);
4153 static const struct got_error *
4154 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4155 FILE *patch_file, int n, int nchanges)
4157 const struct got_error *err = NULL;
4159 size_t linesize = 0;
4162 struct choose_patch_arg *a = arg;
4164 *choice = GOT_PATCH_CHOICE_NONE;
4166 if (a->patch_script_file) {
4168 err = show_change(status, path, patch_file, n, nchanges,
4172 linelen = getline(&line, &linesize, a->patch_script_file);
4173 if (linelen == -1) {
4174 if (ferror(a->patch_script_file))
4175 return got_error_from_errno("getline");
4178 nl = strchr(line, '\n');
4181 if (strcmp(line, "y") == 0) {
4182 *choice = GOT_PATCH_CHOICE_YES;
4184 } else if (strcmp(line, "n") == 0) {
4185 *choice = GOT_PATCH_CHOICE_NO;
4187 } else if (strcmp(line, "q") == 0 &&
4188 status == GOT_STATUS_MODIFY) {
4189 *choice = GOT_PATCH_CHOICE_QUIT;
4192 printf("invalid response '%s'\n", line);
4197 while (resp != 'y' && resp != 'n' && resp != 'q') {
4198 err = show_change(status, path, patch_file, n, nchanges,
4205 if (status == GOT_STATUS_MODIFY) {
4206 if (resp != 'y' && resp != 'n' && resp != 'q') {
4207 printf("invalid response '%c'\n", resp);
4210 } else if (resp != 'y' && resp != 'n') {
4211 printf("invalid response '%c'\n", resp);
4217 *choice = GOT_PATCH_CHOICE_YES;
4218 else if (resp == 'n')
4219 *choice = GOT_PATCH_CHOICE_NO;
4220 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4221 *choice = GOT_PATCH_CHOICE_QUIT;
4227 static const struct got_error *
4228 cmd_revert(int argc, char *argv[])
4230 const struct got_error *error = NULL;
4231 struct got_worktree *worktree = NULL;
4232 struct got_repository *repo = NULL;
4233 char *cwd = NULL, *path = NULL;
4234 struct got_pathlist_head paths;
4235 struct got_pathlist_entry *pe;
4236 int ch, can_recurse = 0, pflag = 0;
4237 FILE *patch_script_file = NULL;
4238 const char *patch_script_path = NULL;
4239 struct choose_patch_arg cpa;
4243 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4249 patch_script_path = optarg;
4264 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4265 "unveil", NULL) == -1)
4270 if (patch_script_path && !pflag)
4271 errx(1, "-F option can only be used together with -p option");
4273 cwd = getcwd(NULL, 0);
4275 error = got_error_from_errno("getcwd");
4278 error = got_worktree_open(&worktree, cwd);
4282 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4287 if (patch_script_path) {
4288 patch_script_file = fopen(patch_script_path, "r");
4289 if (patch_script_file == NULL) {
4290 error = got_error_from_errno2("fopen",
4295 error = apply_unveil(got_repo_get_path(repo), 1,
4296 got_worktree_get_root_path(worktree));
4300 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4307 TAILQ_FOREACH(pe, &paths, entry) {
4308 if (asprintf(&ondisk_path, "%s/%s",
4309 got_worktree_get_root_path(worktree),
4311 error = got_error_from_errno("asprintf");
4314 if (lstat(ondisk_path, &sb) == -1) {
4315 if (errno == ENOENT) {
4319 error = got_error_from_errno2("lstat",
4325 if (S_ISDIR(sb.st_mode)) {
4326 error = got_error_msg(GOT_ERR_BAD_PATH,
4327 "reverting directories requires -R option");
4333 cpa.patch_script_file = patch_script_file;
4334 cpa.action = "revert";
4335 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4336 pflag ? choose_patch : NULL, &cpa, repo);
4340 if (patch_script_file && fclose(patch_script_file) == EOF &&
4342 error = got_error_from_errno2("fclose", patch_script_path);
4344 got_repo_close(repo);
4346 got_worktree_close(worktree);
4355 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4360 struct collect_commit_logmsg_arg {
4361 const char *cmdline_log;
4363 const char *worktree_path;
4364 const char *branch_name;
4365 const char *repo_path;
4370 static const struct got_error *
4371 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4374 char *initial_content = NULL;
4375 struct got_pathlist_entry *pe;
4376 const struct got_error *err = NULL;
4377 char *template = NULL;
4378 struct collect_commit_logmsg_arg *a = arg;
4382 /* if a message was specified on the command line, just use it */
4383 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4384 len = strlen(a->cmdline_log) + 1;
4385 *logmsg = malloc(len + 1);
4386 if (*logmsg == NULL)
4387 return got_error_from_errno("malloc");
4388 strlcpy(*logmsg, a->cmdline_log, len);
4392 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
4393 return got_error_from_errno("asprintf");
4395 if (asprintf(&initial_content,
4396 "\n# changes to be committed on branch %s:\n",
4397 a->branch_name) == -1)
4398 return got_error_from_errno("asprintf");
4400 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
4404 dprintf(fd, initial_content);
4406 TAILQ_FOREACH(pe, commitable_paths, entry) {
4407 struct got_commitable *ct = pe->data;
4408 dprintf(fd, "# %c %s\n",
4409 got_commitable_get_status(ct),
4410 got_commitable_get_path(ct));
4414 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
4416 if (err == NULL || err->code == GOT_ERR_COMMIT_MSG_EMPTY) {
4417 unlink(a->logmsg_path);
4418 free(a->logmsg_path);
4419 a->logmsg_path = NULL;
4421 free(initial_content);
4424 /* Editor is done; we can now apply unveil(2) */
4426 err = apply_unveil(a->repo_path, 0, a->worktree_path);
4435 static const struct got_error *
4436 cmd_commit(int argc, char *argv[])
4438 const struct got_error *error = NULL;
4439 struct got_worktree *worktree = NULL;
4440 struct got_repository *repo = NULL;
4441 char *cwd = NULL, *id_str = NULL;
4442 struct got_object_id *id = NULL;
4443 const char *logmsg = NULL;
4444 struct collect_commit_logmsg_arg cl_arg;
4445 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
4446 int ch, rebase_in_progress, histedit_in_progress;
4447 struct got_pathlist_head paths;
4450 cl_arg.logmsg_path = NULL;
4452 while ((ch = getopt(argc, argv, "m:")) != -1) {
4467 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4468 "unveil", NULL) == -1)
4471 cwd = getcwd(NULL, 0);
4473 error = got_error_from_errno("getcwd");
4476 error = got_worktree_open(&worktree, cwd);
4480 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4483 if (rebase_in_progress) {
4484 error = got_error(GOT_ERR_REBASING);
4488 error = got_worktree_histedit_in_progress(&histedit_in_progress,
4493 error = get_gitconfig_path(&gitconfig_path);
4496 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4501 error = get_author(&author, repo);
4506 * unveil(2) traverses exec(2); if an editor is used we have
4507 * to apply unveil after the log message has been written.
4509 if (logmsg == NULL || strlen(logmsg) == 0)
4510 error = get_editor(&editor);
4512 error = apply_unveil(got_repo_get_path(repo), 0,
4513 got_worktree_get_root_path(worktree));
4517 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4521 cl_arg.editor = editor;
4522 cl_arg.cmdline_log = logmsg;
4523 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
4524 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
4525 if (!histedit_in_progress) {
4526 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
4527 error = got_error(GOT_ERR_COMMIT_BRANCH);
4530 cl_arg.branch_name += 11;
4532 cl_arg.repo_path = got_repo_get_path(repo);
4533 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
4534 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
4536 if (cl_arg.logmsg_path)
4537 fprintf(stderr, "%s: log message preserved in %s\n",
4538 getprogname(), cl_arg.logmsg_path);
4542 if (cl_arg.logmsg_path)
4543 unlink(cl_arg.logmsg_path);
4545 error = got_object_id_str(&id_str, id);
4548 printf("Created commit %s\n", id_str);
4550 free(cl_arg.logmsg_path);
4552 got_repo_close(repo);
4554 got_worktree_close(worktree);
4557 free(gitconfig_path);
4564 usage_cherrypick(void)
4566 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
4570 static const struct got_error *
4571 cmd_cherrypick(int argc, char *argv[])
4573 const struct got_error *error = NULL;
4574 struct got_worktree *worktree = NULL;
4575 struct got_repository *repo = NULL;
4576 char *cwd = NULL, *commit_id_str = NULL;
4577 struct got_object_id *commit_id = NULL;
4578 struct got_commit_object *commit = NULL;
4579 struct got_object_qid *pid;
4580 struct got_reference *head_ref = NULL;
4581 int ch, did_something = 0;
4583 while ((ch = getopt(argc, argv, "")) != -1) {
4595 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4596 "unveil", NULL) == -1)
4602 cwd = getcwd(NULL, 0);
4604 error = got_error_from_errno("getcwd");
4607 error = got_worktree_open(&worktree, cwd);
4611 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4616 error = apply_unveil(got_repo_get_path(repo), 0,
4617 got_worktree_get_root_path(worktree));
4621 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4622 GOT_OBJ_TYPE_COMMIT, repo);
4623 if (error != NULL) {
4624 struct got_reference *ref;
4625 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4627 error = got_ref_open(&ref, repo, argv[0], 0);
4630 error = got_ref_resolve(&commit_id, repo, ref);
4635 error = got_object_id_str(&commit_id_str, commit_id);
4639 error = got_ref_open(&head_ref, repo,
4640 got_worktree_get_head_ref_name(worktree), 0);
4644 error = check_same_branch(commit_id, head_ref, NULL, repo);
4646 if (error->code != GOT_ERR_ANCESTRY)
4650 error = got_error(GOT_ERR_SAME_BRANCH);
4654 error = got_object_open_as_commit(&commit, repo, commit_id);
4657 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4658 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
4659 commit_id, repo, update_progress, &did_something, check_cancelled,
4665 printf("Merged commit %s\n", commit_id_str);
4668 got_object_commit_close(commit);
4669 free(commit_id_str);
4671 got_ref_close(head_ref);
4673 got_worktree_close(worktree);
4675 got_repo_close(repo);
4682 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
4686 static const struct got_error *
4687 cmd_backout(int argc, char *argv[])
4689 const struct got_error *error = NULL;
4690 struct got_worktree *worktree = NULL;
4691 struct got_repository *repo = NULL;
4692 char *cwd = NULL, *commit_id_str = NULL;
4693 struct got_object_id *commit_id = NULL;
4694 struct got_commit_object *commit = NULL;
4695 struct got_object_qid *pid;
4696 struct got_reference *head_ref = NULL;
4697 int ch, did_something = 0;
4699 while ((ch = getopt(argc, argv, "")) != -1) {
4711 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4712 "unveil", NULL) == -1)
4718 cwd = getcwd(NULL, 0);
4720 error = got_error_from_errno("getcwd");
4723 error = got_worktree_open(&worktree, cwd);
4727 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4732 error = apply_unveil(got_repo_get_path(repo), 0,
4733 got_worktree_get_root_path(worktree));
4737 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4738 GOT_OBJ_TYPE_COMMIT, repo);
4739 if (error != NULL) {
4740 struct got_reference *ref;
4741 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4743 error = got_ref_open(&ref, repo, argv[0], 0);
4746 error = got_ref_resolve(&commit_id, repo, ref);
4751 error = got_object_id_str(&commit_id_str, commit_id);
4755 error = got_ref_open(&head_ref, repo,
4756 got_worktree_get_head_ref_name(worktree), 0);
4760 error = check_same_branch(commit_id, head_ref, NULL, repo);
4764 error = got_object_open_as_commit(&commit, repo, commit_id);
4767 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4769 error = got_error(GOT_ERR_ROOT_COMMIT);
4773 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
4774 update_progress, &did_something, check_cancelled, NULL);
4779 printf("Backed out commit %s\n", commit_id_str);
4782 got_object_commit_close(commit);
4783 free(commit_id_str);
4785 got_ref_close(head_ref);
4787 got_worktree_close(worktree);
4789 got_repo_close(repo);
4796 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
4802 trim_logmsg(char *logmsg, int limit)
4807 len = strlen(logmsg);
4811 nl = strchr(logmsg, '\n');
4816 static const struct got_error *
4817 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
4819 const struct got_error *err;
4820 char *logmsg0 = NULL;
4823 err = got_object_commit_get_logmsg(&logmsg0, commit);
4828 while (isspace((unsigned char)s[0]))
4831 *logmsg = strdup(s);
4832 if (*logmsg == NULL) {
4833 err = got_error_from_errno("strdup");
4837 trim_logmsg(*logmsg, limit);
4843 static const struct got_error *
4844 show_rebase_progress(struct got_commit_object *commit,
4845 struct got_object_id *old_id, struct got_object_id *new_id)
4847 const struct got_error *err;
4848 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
4850 err = got_object_id_str(&old_id_str, old_id);
4855 err = got_object_id_str(&new_id_str, new_id);
4860 old_id_str[12] = '\0';
4862 new_id_str[12] = '\0';
4864 err = get_short_logmsg(&logmsg, 42, commit);
4868 printf("%s -> %s: %s\n", old_id_str,
4869 new_id_str ? new_id_str : "no-op change", logmsg);
4876 static const struct got_error *
4877 rebase_progress(void *arg, unsigned char status, const char *path)
4879 unsigned char *rebase_status = arg;
4881 while (path[0] == '/')
4883 printf("%c %s\n", status, path);
4885 if (*rebase_status == GOT_STATUS_CONFLICT)
4887 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
4888 *rebase_status = status;
4892 static const struct got_error *
4893 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
4894 struct got_reference *branch, struct got_reference *new_base_branch,
4895 struct got_reference *tmp_branch, struct got_repository *repo)
4897 printf("Switching work tree to %s\n", got_ref_get_name(branch));
4898 return got_worktree_rebase_complete(worktree, fileindex,
4899 new_base_branch, tmp_branch, branch, repo);
4902 static const struct got_error *
4903 rebase_commit(struct got_pathlist_head *merged_paths,
4904 struct got_worktree *worktree, struct got_fileindex *fileindex,
4905 struct got_reference *tmp_branch,
4906 struct got_object_id *commit_id, struct got_repository *repo)
4908 const struct got_error *error;
4909 struct got_commit_object *commit;
4910 struct got_object_id *new_commit_id;
4912 error = got_object_open_as_commit(&commit, repo, commit_id);
4916 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
4917 worktree, fileindex, tmp_branch, commit, commit_id, repo);
4919 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
4921 error = show_rebase_progress(commit, commit_id, NULL);
4923 error = show_rebase_progress(commit, commit_id, new_commit_id);
4924 free(new_commit_id);
4927 got_object_commit_close(commit);
4931 struct check_path_prefix_arg {
4932 const char *path_prefix;
4937 static const struct got_error *
4938 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
4939 struct got_blob_object *blob2, struct got_object_id *id1,
4940 struct got_object_id *id2, const char *path1, const char *path2,
4941 struct got_repository *repo)
4943 struct check_path_prefix_arg *a = arg;
4945 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
4946 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
4947 return got_error(a->errcode);
4952 static const struct got_error *
4953 check_path_prefix(struct got_object_id *parent_id,
4954 struct got_object_id *commit_id, const char *path_prefix,
4955 int errcode, struct got_repository *repo)
4957 const struct got_error *err;
4958 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4959 struct got_commit_object *commit = NULL, *parent_commit = NULL;
4960 struct check_path_prefix_arg cpp_arg;
4962 if (got_path_is_root_dir(path_prefix))
4965 err = got_object_open_as_commit(&commit, repo, commit_id);
4969 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
4973 err = got_object_open_as_tree(&tree1, repo,
4974 got_object_commit_get_tree_id(parent_commit));
4978 err = got_object_open_as_tree(&tree2, repo,
4979 got_object_commit_get_tree_id(commit));
4983 cpp_arg.path_prefix = path_prefix;
4984 while (cpp_arg.path_prefix[0] == '/')
4985 cpp_arg.path_prefix++;
4986 cpp_arg.len = strlen(cpp_arg.path_prefix);
4987 cpp_arg.errcode = errcode;
4988 err = got_diff_tree(tree1, tree2, "", "", repo,
4989 check_path_prefix_in_diff, &cpp_arg, 0);
4992 got_object_tree_close(tree1);
4994 got_object_tree_close(tree2);
4996 got_object_commit_close(commit);
4998 got_object_commit_close(parent_commit);
5002 static const struct got_error *
5003 collect_commits(struct got_object_id_queue *commits,
5004 struct got_object_id *initial_commit_id,
5005 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
5006 const char *path_prefix, int path_prefix_errcode,
5007 struct got_repository *repo)
5009 const struct got_error *err = NULL;
5010 struct got_commit_graph *graph = NULL;
5011 struct got_object_id *parent_id = NULL;
5012 struct got_object_qid *qid;
5013 struct got_object_id *commit_id = initial_commit_id;
5015 err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
5019 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
5020 check_cancelled, NULL);
5023 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
5024 err = got_commit_graph_iter_next(&parent_id, graph);
5026 if (err->code == GOT_ERR_ITER_COMPLETED) {
5027 err = got_error_msg(GOT_ERR_ANCESTRY,
5028 "ran out of commits to rebase before "
5029 "youngest common ancestor commit has "
5032 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
5034 err = got_commit_graph_fetch_commits(graph, 1, repo,
5035 check_cancelled, NULL);
5039 err = check_path_prefix(parent_id, commit_id,
5040 path_prefix, path_prefix_errcode, repo);
5044 err = got_object_qid_alloc(&qid, commit_id);
5047 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
5048 commit_id = parent_id;
5052 got_commit_graph_close(graph);
5056 static const struct got_error *
5057 cmd_rebase(int argc, char *argv[])
5059 const struct got_error *error = NULL;
5060 struct got_worktree *worktree = NULL;
5061 struct got_repository *repo = NULL;
5062 struct got_fileindex *fileindex = NULL;
5064 struct got_reference *branch = NULL;
5065 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
5066 struct got_object_id *commit_id = NULL, *parent_id = NULL;
5067 struct got_object_id *resume_commit_id = NULL;
5068 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
5069 struct got_commit_object *commit = NULL;
5070 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
5071 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5072 struct got_object_id_queue commits;
5073 struct got_pathlist_head merged_paths;
5074 const struct got_object_id_queue *parent_ids;
5075 struct got_object_qid *qid, *pid;
5077 SIMPLEQ_INIT(&commits);
5078 TAILQ_INIT(&merged_paths);
5080 while ((ch = getopt(argc, argv, "ac")) != -1) {
5086 continue_rebase = 1;
5098 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5099 "unveil", NULL) == -1)
5102 if (abort_rebase && continue_rebase)
5104 else if (abort_rebase || continue_rebase) {
5107 } else if (argc != 1)
5110 cwd = getcwd(NULL, 0);
5112 error = got_error_from_errno("getcwd");
5115 error = got_worktree_open(&worktree, cwd);
5119 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5124 error = apply_unveil(got_repo_get_path(repo), 0,
5125 got_worktree_get_root_path(worktree));
5129 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5135 if (!rebase_in_progress) {
5136 error = got_error(GOT_ERR_NOT_REBASING);
5139 error = got_worktree_rebase_continue(&resume_commit_id,
5140 &new_base_branch, &tmp_branch, &branch, &fileindex,
5144 printf("Switching work tree to %s\n",
5145 got_ref_get_symref_target(new_base_branch));
5146 error = got_worktree_rebase_abort(worktree, fileindex, repo,
5147 new_base_branch, update_progress, &did_something);
5150 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5151 goto done; /* nothing else to do */
5154 if (continue_rebase) {
5155 if (!rebase_in_progress) {
5156 error = got_error(GOT_ERR_NOT_REBASING);
5159 error = got_worktree_rebase_continue(&resume_commit_id,
5160 &new_base_branch, &tmp_branch, &branch, &fileindex,
5165 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5166 resume_commit_id, repo);
5170 yca_id = got_object_id_dup(resume_commit_id);
5171 if (yca_id == NULL) {
5172 error = got_error_from_errno("got_object_id_dup");
5176 error = got_ref_open(&branch, repo, argv[0], 0);
5181 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5185 if (!continue_rebase) {
5186 struct got_object_id *base_commit_id;
5188 base_commit_id = got_worktree_get_base_commit_id(worktree);
5189 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5190 base_commit_id, branch_head_commit_id, repo,
5191 check_cancelled, NULL);
5194 if (yca_id == NULL) {
5195 error = got_error_msg(GOT_ERR_ANCESTRY,
5196 "specified branch shares no common ancestry "
5197 "with work tree's branch");
5201 error = check_same_branch(base_commit_id, branch, yca_id, repo);
5203 if (error->code != GOT_ERR_ANCESTRY)
5207 error = got_error_msg(GOT_ERR_SAME_BRANCH,
5208 "specified branch resolves to a commit which "
5209 "is already contained in work tree's branch");
5212 error = got_worktree_rebase_prepare(&new_base_branch,
5213 &tmp_branch, &fileindex, worktree, branch, repo);
5218 commit_id = branch_head_commit_id;
5219 error = got_object_open_as_commit(&commit, repo, commit_id);
5223 parent_ids = got_object_commit_get_parent_ids(commit);
5224 pid = SIMPLEQ_FIRST(parent_ids);
5226 if (!continue_rebase) {
5228 error = got_worktree_rebase_abort(worktree, fileindex,
5229 repo, new_base_branch, update_progress,
5233 printf("Rebase of %s aborted\n",
5234 got_ref_get_name(branch));
5236 error = got_error(GOT_ERR_EMPTY_REBASE);
5239 error = collect_commits(&commits, commit_id, pid->id,
5240 yca_id, got_worktree_get_path_prefix(worktree),
5241 GOT_ERR_REBASE_PATH, repo);
5242 got_object_commit_close(commit);
5247 if (SIMPLEQ_EMPTY(&commits)) {
5248 if (continue_rebase)
5249 error = rebase_complete(worktree, fileindex,
5250 branch, new_base_branch, tmp_branch, repo);
5252 error = got_error(GOT_ERR_EMPTY_REBASE);
5257 SIMPLEQ_FOREACH(qid, &commits, entry) {
5258 commit_id = qid->id;
5259 parent_id = pid ? pid->id : yca_id;
5262 error = got_worktree_rebase_merge_files(&merged_paths,
5263 worktree, fileindex, parent_id, commit_id, repo,
5264 rebase_progress, &rebase_status, check_cancelled, NULL);
5268 if (rebase_status == GOT_STATUS_CONFLICT) {
5269 got_worktree_rebase_pathlist_free(&merged_paths);
5273 error = rebase_commit(&merged_paths, worktree, fileindex,
5274 tmp_branch, commit_id, repo);
5275 got_worktree_rebase_pathlist_free(&merged_paths);
5280 if (rebase_status == GOT_STATUS_CONFLICT) {
5281 error = got_worktree_rebase_postpone(worktree, fileindex);
5284 error = got_error_msg(GOT_ERR_CONFLICTS,
5285 "conflicts must be resolved before rebasing can continue");
5287 error = rebase_complete(worktree, fileindex, branch,
5288 new_base_branch, tmp_branch, repo);
5290 got_object_id_queue_free(&commits);
5291 free(branch_head_commit_id);
5292 free(resume_commit_id);
5295 got_object_commit_close(commit);
5297 got_ref_close(branch);
5298 if (new_base_branch)
5299 got_ref_close(new_base_branch);
5301 got_ref_close(tmp_branch);
5303 got_worktree_close(worktree);
5305 got_repo_close(repo);
5310 usage_histedit(void)
5312 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script]\n",
5317 #define GOT_HISTEDIT_PICK 'p'
5318 #define GOT_HISTEDIT_EDIT 'e'
5319 #define GOT_HISTEDIT_FOLD 'f'
5320 #define GOT_HISTEDIT_DROP 'd'
5321 #define GOT_HISTEDIT_MESG 'm'
5323 static struct got_histedit_cmd {
5327 } got_histedit_cmds[] = {
5328 { GOT_HISTEDIT_PICK, "pick", "use commit" },
5329 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5330 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
5331 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5332 { GOT_HISTEDIT_MESG, "mesg",
5333 "single-line log message for commit above (open editor if empty)" },
5336 struct got_histedit_list_entry {
5337 TAILQ_ENTRY(got_histedit_list_entry) entry;
5338 struct got_object_id *commit_id;
5339 const struct got_histedit_cmd *cmd;
5342 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5344 static const struct got_error *
5345 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5346 FILE *f, struct got_repository *repo)
5348 const struct got_error *err = NULL;
5349 char *logmsg = NULL, *id_str = NULL;
5350 struct got_commit_object *commit = NULL;
5353 err = got_object_open_as_commit(&commit, repo, commit_id);
5357 err = get_short_logmsg(&logmsg, 34, commit);
5361 err = got_object_id_str(&id_str, commit_id);
5365 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
5367 err = got_ferror(f, GOT_ERR_IO);
5370 got_object_commit_close(commit);
5376 static const struct got_error *
5377 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
5378 struct got_repository *repo)
5380 const struct got_error *err = NULL;
5381 struct got_object_qid *qid;
5383 if (SIMPLEQ_EMPTY(commits))
5384 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5386 SIMPLEQ_FOREACH(qid, commits, entry) {
5387 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
5396 static const struct got_error *
5397 write_cmd_list(FILE *f)
5399 const struct got_error *err = NULL;
5402 n = fprintf(f, "# Available histedit commands:\n");
5404 return got_ferror(f, GOT_ERR_IO);
5406 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5407 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
5408 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
5411 err = got_ferror(f, GOT_ERR_IO);
5415 n = fprintf(f, "# Commits will be processed in order from top to "
5416 "bottom of this file.\n");
5418 return got_ferror(f, GOT_ERR_IO);
5422 static const struct got_error *
5423 histedit_syntax_error(int lineno)
5425 static char msg[42];
5428 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
5430 if (ret == -1 || ret >= sizeof(msg))
5431 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
5433 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
5436 static const struct got_error *
5437 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
5438 char *logmsg, struct got_repository *repo)
5440 const struct got_error *err;
5441 struct got_commit_object *folded_commit = NULL;
5442 char *id_str, *folded_logmsg = NULL;
5444 err = got_object_id_str(&id_str, hle->commit_id);
5448 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
5452 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
5455 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
5456 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
5457 folded_logmsg) == -1) {
5458 err = got_error_from_errno("asprintf");
5463 got_object_commit_close(folded_commit);
5465 free(folded_logmsg);
5469 static struct got_histedit_list_entry *
5470 get_folded_commits(struct got_histedit_list_entry *hle)
5472 struct got_histedit_list_entry *prev, *folded = NULL;
5474 prev = TAILQ_PREV(hle, got_histedit_list, entry);
5475 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
5476 prev->cmd->code == GOT_HISTEDIT_DROP)) {
5477 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
5479 prev = TAILQ_PREV(prev, got_histedit_list, entry);
5485 static const struct got_error *
5486 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
5487 struct got_repository *repo)
5489 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
5490 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
5491 const struct got_error *err = NULL;
5492 struct got_commit_object *commit = NULL;
5494 struct got_histedit_list_entry *folded = NULL;
5496 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5500 folded = get_folded_commits(hle);
5502 while (folded != hle) {
5503 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
5504 folded = TAILQ_NEXT(folded, entry);
5507 err = append_folded_commit_msg(&new_msg, folded,
5513 folded = TAILQ_NEXT(folded, entry);
5517 err = got_object_id_str(&id_str, hle->commit_id);
5520 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
5523 if (asprintf(&new_msg,
5524 "%s\n# original log message of commit %s: %s",
5525 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
5526 err = got_error_from_errno("asprintf");
5532 err = got_object_id_str(&id_str, hle->commit_id);
5536 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
5540 dprintf(fd, logmsg);
5543 err = get_editor(&editor);
5547 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
5549 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
5551 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
5554 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
5555 err = got_error_from_errno2("unlink", logmsg_path);
5561 got_object_commit_close(commit);
5565 static const struct got_error *
5566 histedit_parse_list(struct got_histedit_list *histedit_cmds,
5567 FILE *f, struct got_repository *repo)
5569 const struct got_error *err = NULL;
5570 char *line = NULL, *p, *end;
5574 const struct got_histedit_cmd *cmd;
5575 struct got_object_id *commit_id = NULL;
5576 struct got_histedit_list_entry *hle = NULL;
5579 len = getline(&line, &size, f);
5581 const struct got_error *getline_err;
5584 getline_err = got_error_from_errno("getline");
5585 err = got_ferror(f, getline_err->code);
5590 while (isspace((unsigned char)p[0]))
5592 if (p[0] == '#' || p[0] == '\0') {
5598 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5599 cmd = &got_histedit_cmds[i];
5600 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
5601 isspace((unsigned char)p[strlen(cmd->name)])) {
5602 p += strlen(cmd->name);
5605 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
5610 if (i == nitems(got_histedit_cmds)) {
5611 err = histedit_syntax_error(lineno);
5614 while (isspace((unsigned char)p[0]))
5616 if (cmd->code == GOT_HISTEDIT_MESG) {
5617 if (hle == NULL || hle->logmsg != NULL) {
5618 err = got_error(GOT_ERR_HISTEDIT_CMD);
5622 err = histedit_edit_logmsg(hle, repo);
5626 hle->logmsg = strdup(p);
5627 if (hle->logmsg == NULL) {
5628 err = got_error_from_errno("strdup");
5637 while (end[0] && !isspace((unsigned char)end[0]))
5641 err = got_object_resolve_id_str(&commit_id, repo, p);
5643 /* override error code */
5644 err = histedit_syntax_error(lineno);
5648 hle = malloc(sizeof(*hle));
5650 err = got_error_from_errno("malloc");
5654 hle->commit_id = commit_id;
5659 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
5667 static const struct got_error *
5668 histedit_check_script(struct got_histedit_list *histedit_cmds,
5669 struct got_object_id_queue *commits, struct got_repository *repo)
5671 const struct got_error *err = NULL;
5672 struct got_object_qid *qid;
5673 struct got_histedit_list_entry *hle;
5674 static char msg[80];
5677 if (TAILQ_EMPTY(histedit_cmds))
5678 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
5679 "histedit script contains no commands");
5680 if (SIMPLEQ_EMPTY(commits))
5681 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5683 SIMPLEQ_FOREACH(qid, commits, entry) {
5684 TAILQ_FOREACH(hle, histedit_cmds, entry) {
5685 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
5689 err = got_object_id_str(&id_str, qid->id);
5692 snprintf(msg, sizeof(msg),
5693 "commit %s missing from histedit script", id_str);
5695 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
5699 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
5700 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
5701 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
5702 "last commit in histedit script cannot be folded");
5707 static const struct got_error *
5708 histedit_run_editor(struct got_histedit_list *histedit_cmds,
5709 const char *path, struct got_object_id_queue *commits,
5710 struct got_repository *repo)
5712 const struct got_error *err = NULL;
5716 err = get_editor(&editor);
5720 if (spawn_editor(editor, path) == -1) {
5721 err = got_error_from_errno("failed spawning editor");
5725 f = fopen(path, "r");
5727 err = got_error_from_errno("fopen");
5730 err = histedit_parse_list(histedit_cmds, f, repo);
5734 err = histedit_check_script(histedit_cmds, commits, repo);
5736 if (f && fclose(f) != 0 && err == NULL)
5737 err = got_error_from_errno("fclose");
5742 static const struct got_error *
5743 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
5744 struct got_object_id_queue *, const char *, struct got_repository *);
5746 static const struct got_error *
5747 histedit_edit_script(struct got_histedit_list *histedit_cmds,
5748 struct got_object_id_queue *commits, struct got_repository *repo)
5750 const struct got_error *err;
5754 err = got_opentemp_named(&path, &f, "got-histedit");
5758 err = write_cmd_list(f);
5762 err = histedit_write_commit_list(commits, f, repo);
5766 if (fclose(f) != 0) {
5767 err = got_error_from_errno("fclose");
5772 err = histedit_run_editor(histedit_cmds, path, commits, repo);
5774 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5775 err->code != GOT_ERR_HISTEDIT_CMD)
5777 err = histedit_edit_list_retry(histedit_cmds, err,
5778 commits, path, repo);
5781 if (f && fclose(f) != 0 && err == NULL)
5782 err = got_error_from_errno("fclose");
5783 if (path && unlink(path) != 0 && err == NULL)
5784 err = got_error_from_errno2("unlink", path);
5789 static const struct got_error *
5790 histedit_save_list(struct got_histedit_list *histedit_cmds,
5791 struct got_worktree *worktree, struct got_repository *repo)
5793 const struct got_error *err = NULL;
5796 struct got_histedit_list_entry *hle;
5797 struct got_commit_object *commit = NULL;
5799 err = got_worktree_get_histedit_script_path(&path, worktree);
5803 f = fopen(path, "w");
5805 err = got_error_from_errno2("fopen", path);
5808 TAILQ_FOREACH(hle, histedit_cmds, entry) {
5809 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
5815 int n = fprintf(f, "%c %s\n",
5816 GOT_HISTEDIT_MESG, hle->logmsg);
5818 err = got_ferror(f, GOT_ERR_IO);
5824 if (f && fclose(f) != 0 && err == NULL)
5825 err = got_error_from_errno("fclose");
5828 got_object_commit_close(commit);
5833 histedit_free_list(struct got_histedit_list *histedit_cmds)
5835 struct got_histedit_list_entry *hle;
5837 while ((hle = TAILQ_FIRST(histedit_cmds))) {
5838 TAILQ_REMOVE(histedit_cmds, hle, entry);
5843 static const struct got_error *
5844 histedit_load_list(struct got_histedit_list *histedit_cmds,
5845 const char *path, struct got_repository *repo)
5847 const struct got_error *err = NULL;
5850 f = fopen(path, "r");
5852 err = got_error_from_errno2("fopen", path);
5856 err = histedit_parse_list(histedit_cmds, f, repo);
5858 if (f && fclose(f) != 0 && err == NULL)
5859 err = got_error_from_errno("fclose");
5863 static const struct got_error *
5864 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
5865 const struct got_error *edit_err, struct got_object_id_queue *commits,
5866 const char *path, struct got_repository *repo)
5868 const struct got_error *err = NULL, *prev_err = edit_err;
5871 while (resp != 'c' && resp != 'r' && resp != 'a') {
5872 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
5873 "or (a)bort: ", getprogname(), prev_err->msg);
5878 histedit_free_list(histedit_cmds);
5879 err = histedit_run_editor(histedit_cmds, path, commits,
5882 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5883 err->code != GOT_ERR_HISTEDIT_CMD)
5890 } else if (resp == 'r') {
5891 histedit_free_list(histedit_cmds);
5892 err = histedit_edit_script(histedit_cmds,
5895 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5896 err->code != GOT_ERR_HISTEDIT_CMD)
5903 } else if (resp == 'a') {
5904 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
5907 printf("invalid response '%c'\n", resp);
5913 static const struct got_error *
5914 histedit_complete(struct got_worktree *worktree,
5915 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5916 struct got_reference *branch, struct got_repository *repo)
5918 printf("Switching work tree to %s\n",
5919 got_ref_get_symref_target(branch));
5920 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
5924 static const struct got_error *
5925 show_histedit_progress(struct got_commit_object *commit,
5926 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
5928 const struct got_error *err;
5929 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5931 err = got_object_id_str(&old_id_str, hle->commit_id);
5936 err = got_object_id_str(&new_id_str, new_id);
5941 old_id_str[12] = '\0';
5943 new_id_str[12] = '\0';
5946 logmsg = strdup(hle->logmsg);
5947 if (logmsg == NULL) {
5948 err = got_error_from_errno("strdup");
5951 trim_logmsg(logmsg, 42);
5953 err = get_short_logmsg(&logmsg, 42, commit);
5958 switch (hle->cmd->code) {
5959 case GOT_HISTEDIT_PICK:
5960 case GOT_HISTEDIT_EDIT:
5961 printf("%s -> %s: %s\n", old_id_str,
5962 new_id_str ? new_id_str : "no-op change", logmsg);
5964 case GOT_HISTEDIT_DROP:
5965 case GOT_HISTEDIT_FOLD:
5966 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
5979 static const struct got_error *
5980 histedit_commit(struct got_pathlist_head *merged_paths,
5981 struct got_worktree *worktree, struct got_fileindex *fileindex,
5982 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
5983 struct got_repository *repo)
5985 const struct got_error *err;
5986 struct got_commit_object *commit;
5987 struct got_object_id *new_commit_id;
5989 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
5990 && hle->logmsg == NULL) {
5991 err = histedit_edit_logmsg(hle, repo);
5996 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6000 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
6001 worktree, fileindex, tmp_branch, commit, hle->commit_id,
6004 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
6006 err = show_histedit_progress(commit, hle, NULL);
6008 err = show_histedit_progress(commit, hle, new_commit_id);
6009 free(new_commit_id);
6012 got_object_commit_close(commit);
6016 static const struct got_error *
6017 histedit_skip_commit(struct got_histedit_list_entry *hle,
6018 struct got_worktree *worktree, struct got_repository *repo)
6020 const struct got_error *error;
6021 struct got_commit_object *commit;
6023 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
6028 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
6032 error = show_histedit_progress(commit, hle, NULL);
6033 got_object_commit_close(commit);
6037 static const struct got_error *
6038 cmd_histedit(int argc, char *argv[])
6040 const struct got_error *error = NULL;
6041 struct got_worktree *worktree = NULL;
6042 struct got_fileindex *fileindex = NULL;
6043 struct got_repository *repo = NULL;
6045 struct got_reference *branch = NULL;
6046 struct got_reference *tmp_branch = NULL;
6047 struct got_object_id *resume_commit_id = NULL;
6048 struct got_object_id *base_commit_id = NULL;
6049 struct got_object_id *head_commit_id = NULL;
6050 struct got_commit_object *commit = NULL;
6051 int ch, rebase_in_progress = 0, did_something;
6052 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
6053 const char *edit_script_path = NULL;
6054 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6055 struct got_object_id_queue commits;
6056 struct got_pathlist_head merged_paths;
6057 const struct got_object_id_queue *parent_ids;
6058 struct got_object_qid *pid;
6059 struct got_histedit_list histedit_cmds;
6060 struct got_histedit_list_entry *hle;
6062 SIMPLEQ_INIT(&commits);
6063 TAILQ_INIT(&histedit_cmds);
6064 TAILQ_INIT(&merged_paths);
6066 while ((ch = getopt(argc, argv, "acF:")) != -1) {
6075 edit_script_path = optarg;
6087 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6088 "unveil", NULL) == -1)
6091 if (abort_edit && continue_edit)
6097 * This command cannot apply unveil(2) in all cases because the
6098 * user may choose to run an editor to edit the histedit script
6099 * and to edit individual commit log messages.
6100 * unveil(2) traverses exec(2); if an editor is used we have to
6101 * apply unveil after edit script and log messages have been written.
6102 * XXX TODO: Make use of unveil(2) where possible.
6105 cwd = getcwd(NULL, 0);
6107 error = got_error_from_errno("getcwd");
6110 error = got_worktree_open(&worktree, cwd);
6114 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6119 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6122 if (rebase_in_progress) {
6123 error = got_error(GOT_ERR_REBASING);
6127 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6131 if (edit_in_progress && abort_edit) {
6132 error = got_worktree_histedit_continue(&resume_commit_id,
6133 &tmp_branch, &branch, &base_commit_id, &fileindex,
6137 printf("Switching work tree to %s\n",
6138 got_ref_get_symref_target(branch));
6139 error = got_worktree_histedit_abort(worktree, fileindex, repo,
6140 branch, base_commit_id, update_progress, &did_something);
6143 printf("Histedit of %s aborted\n",
6144 got_ref_get_symref_target(branch));
6145 goto done; /* nothing else to do */
6146 } else if (abort_edit) {
6147 error = got_error(GOT_ERR_NOT_HISTEDIT);
6151 if (continue_edit) {
6154 if (!edit_in_progress) {
6155 error = got_error(GOT_ERR_NOT_HISTEDIT);
6159 error = got_worktree_get_histedit_script_path(&path, worktree);
6163 error = histedit_load_list(&histedit_cmds, path, repo);
6168 error = got_worktree_histedit_continue(&resume_commit_id,
6169 &tmp_branch, &branch, &base_commit_id, &fileindex,
6174 error = got_ref_resolve(&head_commit_id, repo, branch);
6178 error = got_object_open_as_commit(&commit, repo,
6182 parent_ids = got_object_commit_get_parent_ids(commit);
6183 pid = SIMPLEQ_FIRST(parent_ids);
6185 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6188 error = collect_commits(&commits, head_commit_id, pid->id,
6189 base_commit_id, got_worktree_get_path_prefix(worktree),
6190 GOT_ERR_HISTEDIT_PATH, repo);
6191 got_object_commit_close(commit);
6196 if (edit_in_progress) {
6197 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6201 error = got_ref_open(&branch, repo,
6202 got_worktree_get_head_ref_name(worktree), 0);
6206 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
6207 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
6208 "will not edit commit history of a branch outside "
6209 "the \"refs/heads/\" reference namespace");
6213 error = got_ref_resolve(&head_commit_id, repo, branch);
6214 got_ref_close(branch);
6219 error = got_object_open_as_commit(&commit, repo,
6223 parent_ids = got_object_commit_get_parent_ids(commit);
6224 pid = SIMPLEQ_FIRST(parent_ids);
6226 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6229 error = collect_commits(&commits, head_commit_id, pid->id,
6230 got_worktree_get_base_commit_id(worktree),
6231 got_worktree_get_path_prefix(worktree),
6232 GOT_ERR_HISTEDIT_PATH, repo);
6233 got_object_commit_close(commit);
6238 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
6239 &base_commit_id, &fileindex, worktree, repo);
6243 if (edit_script_path) {
6244 error = histedit_load_list(&histedit_cmds,
6245 edit_script_path, repo);
6247 got_worktree_histedit_abort(worktree, fileindex,
6248 repo, branch, base_commit_id,
6249 update_progress, &did_something);
6253 error = histedit_edit_script(&histedit_cmds, &commits,
6256 got_worktree_histedit_abort(worktree, fileindex,
6257 repo, branch, base_commit_id,
6258 update_progress, &did_something);
6264 error = histedit_save_list(&histedit_cmds, worktree,
6267 got_worktree_histedit_abort(worktree, fileindex,
6268 repo, branch, base_commit_id,
6269 update_progress, &did_something);
6275 error = histedit_check_script(&histedit_cmds, &commits, repo);
6279 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
6280 if (resume_commit_id) {
6281 if (got_object_id_cmp(hle->commit_id,
6282 resume_commit_id) != 0)
6285 resume_commit_id = NULL;
6286 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
6287 hle->cmd->code == GOT_HISTEDIT_FOLD) {
6288 error = histedit_skip_commit(hle, worktree,
6291 error = histedit_commit(NULL, worktree,
6292 fileindex, tmp_branch, hle, repo);
6299 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
6300 error = histedit_skip_commit(hle, worktree, repo);
6306 error = got_object_open_as_commit(&commit, repo,
6310 parent_ids = got_object_commit_get_parent_ids(commit);
6311 pid = SIMPLEQ_FIRST(parent_ids);
6313 error = got_worktree_histedit_merge_files(&merged_paths,
6314 worktree, fileindex, pid->id, hle->commit_id, repo,
6315 rebase_progress, &rebase_status, check_cancelled, NULL);
6318 got_object_commit_close(commit);
6321 if (rebase_status == GOT_STATUS_CONFLICT) {
6322 got_worktree_rebase_pathlist_free(&merged_paths);
6326 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
6328 error = got_object_id_str(&id_str, hle->commit_id);
6331 printf("Stopping histedit for amending commit %s\n",
6334 got_worktree_rebase_pathlist_free(&merged_paths);
6335 error = got_worktree_histedit_postpone(worktree,
6340 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
6341 error = histedit_skip_commit(hle, worktree, repo);
6347 error = histedit_commit(&merged_paths, worktree, fileindex,
6348 tmp_branch, hle, repo);
6349 got_worktree_rebase_pathlist_free(&merged_paths);
6354 if (rebase_status == GOT_STATUS_CONFLICT) {
6355 error = got_worktree_histedit_postpone(worktree, fileindex);
6358 error = got_error_msg(GOT_ERR_CONFLICTS,
6359 "conflicts must be resolved before rebasing can continue");
6361 error = histedit_complete(worktree, fileindex, tmp_branch,
6364 got_object_id_queue_free(&commits);
6365 histedit_free_list(&histedit_cmds);
6366 free(head_commit_id);
6367 free(base_commit_id);
6368 free(resume_commit_id);
6370 got_object_commit_close(commit);
6372 got_ref_close(branch);
6374 got_ref_close(tmp_branch);
6376 got_worktree_close(worktree);
6378 got_repo_close(repo);
6385 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
6386 "[file-path ...]\n",
6391 static const struct got_error *
6392 print_stage(void *arg, unsigned char status, unsigned char staged_status,
6393 const char *path, struct got_object_id *blob_id,
6394 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
6396 const struct got_error *err = NULL;
6397 char *id_str = NULL;
6399 if (staged_status != GOT_STATUS_ADD &&
6400 staged_status != GOT_STATUS_MODIFY &&
6401 staged_status != GOT_STATUS_DELETE)
6404 if (staged_status == GOT_STATUS_ADD ||
6405 staged_status == GOT_STATUS_MODIFY)
6406 err = got_object_id_str(&id_str, staged_blob_id);
6408 err = got_object_id_str(&id_str, blob_id);
6412 printf("%s %c %s\n", id_str, staged_status, path);
6417 static const struct got_error *
6418 cmd_stage(int argc, char *argv[])
6420 const struct got_error *error = NULL;
6421 struct got_repository *repo = NULL;
6422 struct got_worktree *worktree = NULL;
6424 struct got_pathlist_head paths;
6425 struct got_pathlist_entry *pe;
6426 int ch, list_stage = 0, pflag = 0;
6427 FILE *patch_script_file = NULL;
6428 const char *patch_script_path = NULL;
6429 struct choose_patch_arg cpa;
6433 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
6442 patch_script_path = optarg;
6454 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6455 "unveil", NULL) == -1)
6458 if (list_stage && (pflag || patch_script_path))
6459 errx(1, "-l option cannot be used with other options");
6460 if (patch_script_path && !pflag)
6461 errx(1, "-F option can only be used together with -p option");
6463 cwd = getcwd(NULL, 0);
6465 error = got_error_from_errno("getcwd");
6469 error = got_worktree_open(&worktree, cwd);
6473 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6478 if (patch_script_path) {
6479 patch_script_file = fopen(patch_script_path, "r");
6480 if (patch_script_file == NULL) {
6481 error = got_error_from_errno2("fopen",
6486 error = apply_unveil(got_repo_get_path(repo), 0,
6487 got_worktree_get_root_path(worktree));
6491 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6496 error = got_worktree_status(worktree, &paths, repo,
6497 print_stage, NULL, check_cancelled, NULL);
6499 cpa.patch_script_file = patch_script_file;
6500 cpa.action = "stage";
6501 error = got_worktree_stage(worktree, &paths,
6502 pflag ? NULL : print_status, NULL,
6503 pflag ? choose_patch : NULL, &cpa, repo);
6506 if (patch_script_file && fclose(patch_script_file) == EOF &&
6508 error = got_error_from_errno2("fclose", patch_script_path);
6510 got_repo_close(repo);
6512 got_worktree_close(worktree);
6513 TAILQ_FOREACH(pe, &paths, entry)
6514 free((char *)pe->path);
6515 got_pathlist_free(&paths);
6523 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
6524 "[file-path ...]\n",
6530 static const struct got_error *
6531 cmd_unstage(int argc, char *argv[])
6533 const struct got_error *error = NULL;
6534 struct got_repository *repo = NULL;
6535 struct got_worktree *worktree = NULL;
6537 struct got_pathlist_head paths;
6538 struct got_pathlist_entry *pe;
6539 int ch, did_something = 0, pflag = 0;
6540 FILE *patch_script_file = NULL;
6541 const char *patch_script_path = NULL;
6542 struct choose_patch_arg cpa;
6546 while ((ch = getopt(argc, argv, "pF:")) != -1) {
6552 patch_script_path = optarg;
6564 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6565 "unveil", NULL) == -1)
6568 if (patch_script_path && !pflag)
6569 errx(1, "-F option can only be used together with -p option");
6571 cwd = getcwd(NULL, 0);
6573 error = got_error_from_errno("getcwd");
6577 error = got_worktree_open(&worktree, cwd);
6581 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6586 if (patch_script_path) {
6587 patch_script_file = fopen(patch_script_path, "r");
6588 if (patch_script_file == NULL) {
6589 error = got_error_from_errno2("fopen",
6595 error = apply_unveil(got_repo_get_path(repo), 0,
6596 got_worktree_get_root_path(worktree));
6600 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6604 cpa.patch_script_file = patch_script_file;
6605 cpa.action = "unstage";
6606 error = got_worktree_unstage(worktree, &paths, update_progress,
6607 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
6609 if (patch_script_file && fclose(patch_script_file) == EOF &&
6611 error = got_error_from_errno2("fclose", patch_script_path);
6613 got_repo_close(repo);
6615 got_worktree_close(worktree);
6616 TAILQ_FOREACH(pe, &paths, entry)
6617 free((char *)pe->path);
6618 got_pathlist_free(&paths);
6626 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
6627 "arg1 [arg2 ...]\n", getprogname());
6631 static const struct got_error *
6632 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6634 const struct got_error *err;
6635 struct got_blob_object *blob;
6637 err = got_object_open_as_blob(&blob, repo, id, 8192);
6641 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
6642 got_object_blob_close(blob);
6646 static const struct got_error *
6647 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6649 const struct got_error *err;
6650 struct got_tree_object *tree;
6651 const struct got_tree_entries *entries;
6652 struct got_tree_entry *te;
6654 err = got_object_open_as_tree(&tree, repo, id);
6658 entries = got_object_tree_get_entries(tree);
6659 te = SIMPLEQ_FIRST(&entries->head);
6662 if (sigint_received || sigpipe_received)
6664 err = got_object_id_str(&id_str, te->id);
6667 fprintf(outfile, "%s %.7o %s\n", id_str, te->mode, te->name);
6669 te = SIMPLEQ_NEXT(te, entry);
6672 got_object_tree_close(tree);
6676 static const struct got_error *
6677 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6679 const struct got_error *err;
6680 struct got_commit_object *commit;
6681 const struct got_object_id_queue *parent_ids;
6682 struct got_object_qid *pid;
6683 char *id_str = NULL;
6684 const char *logmsg = NULL;
6686 err = got_object_open_as_commit(&commit, repo, id);
6690 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
6694 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
6695 parent_ids = got_object_commit_get_parent_ids(commit);
6696 fprintf(outfile, "numparents %d\n",
6697 got_object_commit_get_nparents(commit));
6698 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
6700 err = got_object_id_str(&pid_str, pid->id);
6703 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
6706 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
6707 got_object_commit_get_author(commit),
6708 got_object_commit_get_author_time(commit));
6710 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
6711 got_object_commit_get_author(commit),
6712 got_object_commit_get_committer_time(commit));
6714 logmsg = got_object_commit_get_logmsg_raw(commit);
6715 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
6716 fprintf(outfile, "%s", logmsg);
6719 got_object_commit_close(commit);
6723 static const struct got_error *
6724 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6726 const struct got_error *err;
6727 struct got_tag_object *tag;
6728 char *id_str = NULL;
6729 const char *tagmsg = NULL;
6731 err = got_object_open_as_tag(&tag, repo, id);
6735 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
6739 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
6741 switch (got_object_tag_get_object_type(tag)) {
6742 case GOT_OBJ_TYPE_BLOB:
6743 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
6744 GOT_OBJ_LABEL_BLOB);
6746 case GOT_OBJ_TYPE_TREE:
6747 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
6748 GOT_OBJ_LABEL_TREE);
6750 case GOT_OBJ_TYPE_COMMIT:
6751 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
6752 GOT_OBJ_LABEL_COMMIT);
6754 case GOT_OBJ_TYPE_TAG:
6755 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
6762 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
6763 got_object_tag_get_name(tag));
6765 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
6766 got_object_tag_get_tagger(tag),
6767 got_object_tag_get_tagger_time(tag));
6769 tagmsg = got_object_tag_get_message(tag);
6770 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
6771 fprintf(outfile, "%s", tagmsg);
6774 got_object_tag_close(tag);
6778 static const struct got_error *
6779 cmd_cat(int argc, char *argv[])
6781 const struct got_error *error;
6782 struct got_repository *repo = NULL;
6783 struct got_worktree *worktree = NULL;
6784 char *cwd = NULL, *repo_path = NULL, *label = NULL;
6785 const char *commit_id_str = NULL;
6786 struct got_object_id *id = NULL, *commit_id = NULL;
6787 int ch, obj_type, i, force_path = 0;
6790 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6795 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
6798 commit_id_str = optarg;
6801 repo_path = realpath(optarg, NULL);
6802 if (repo_path == NULL)
6803 err(1, "-r option");
6804 got_path_strip_trailing_slashes(repo_path);
6818 cwd = getcwd(NULL, 0);
6820 error = got_error_from_errno("getcwd");
6823 error = got_worktree_open(&worktree, cwd);
6824 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6827 if (repo_path == NULL) {
6829 got_worktree_get_repo_path(worktree));
6830 if (repo_path == NULL) {
6831 error = got_error_from_errno("strdup");
6837 if (repo_path == NULL) {
6838 repo_path = getcwd(NULL, 0);
6839 if (repo_path == NULL)
6840 return got_error_from_errno("getcwd");
6843 error = got_repo_open(&repo, repo_path, NULL);
6848 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6852 if (commit_id_str == NULL)
6853 commit_id_str = GOT_REF_HEAD;
6854 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
6858 for (i = 0; i < argc; i++) {
6860 error = got_object_id_by_path(&id, repo, commit_id,
6865 error = match_object_id(&id, &label, argv[i],
6866 GOT_OBJ_TYPE_ANY, 0, repo);
6868 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
6869 error->code != GOT_ERR_NOT_REF)
6871 error = got_object_id_by_path(&id, repo,
6872 commit_id, argv[i]);
6878 error = got_object_get_type(&obj_type, repo, id);
6883 case GOT_OBJ_TYPE_BLOB:
6884 error = cat_blob(id, repo, stdout);
6886 case GOT_OBJ_TYPE_TREE:
6887 error = cat_tree(id, repo, stdout);
6889 case GOT_OBJ_TYPE_COMMIT:
6890 error = cat_commit(id, repo, stdout);
6892 case GOT_OBJ_TYPE_TAG:
6893 error = cat_tag(id, repo, stdout);
6896 error = got_error(GOT_ERR_OBJ_TYPE);
6912 got_worktree_close(worktree);
6914 const struct got_error *repo_error;
6915 repo_error = got_repo_close(repo);