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);
679 error = collect_import_msg(&logmsg, editor, path_dir, refname);
684 if (unveil(path_dir, "r") != 0)
685 return got_error_from_errno2("unveil", path_dir);
687 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
691 error = got_repo_import(&new_commit_id, path_dir, logmsg,
692 author, &ignores, repo, import_progress, NULL);
696 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
700 error = got_ref_write(branch_ref, repo);
704 error = got_object_id_str(&id_str, new_commit_id);
708 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
710 if (error->code != GOT_ERR_NOT_REF)
713 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
718 error = got_ref_write(head_ref, repo);
723 printf("Created branch %s with commit %s\n",
724 got_ref_get_name(branch_ref), id_str);
733 free(gitconfig_path);
735 got_ref_close(branch_ref);
737 got_ref_close(head_ref);
744 fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
745 "[-p prefix] repository-path [worktree-path]\n", getprogname());
749 static const struct got_error *
750 checkout_progress(void *arg, unsigned char status, const char *path)
752 char *worktree_path = arg;
754 /* Base commit bump happens silently. */
755 if (status == GOT_STATUS_BUMP_BASE)
758 while (path[0] == '/')
761 printf("%c %s/%s\n", status, worktree_path, path);
765 static const struct got_error *
766 check_cancelled(void *arg)
768 if (sigint_received || sigpipe_received)
769 return got_error(GOT_ERR_CANCELLED);
773 static const struct got_error *
774 check_linear_ancestry(struct got_object_id *commit_id,
775 struct got_object_id *base_commit_id, struct got_repository *repo)
777 const struct got_error *err = NULL;
778 struct got_object_id *yca_id;
780 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
781 commit_id, base_commit_id, repo, check_cancelled, NULL);
786 return got_error(GOT_ERR_ANCESTRY);
789 * Require a straight line of history between the target commit
790 * and the work tree's base commit.
792 * Non-linear situations such as this require a rebase:
794 * (commit) D F (base_commit)
802 * 'got update' only handles linear cases:
803 * Update forwards in time: A (base/yca) - B - C - D (commit)
804 * Update backwards in time: D (base) - C - B - A (commit/yca)
806 if (got_object_id_cmp(commit_id, yca_id) != 0 &&
807 got_object_id_cmp(base_commit_id, yca_id) != 0)
808 return got_error(GOT_ERR_ANCESTRY);
814 static const struct got_error *
815 check_same_branch(struct got_object_id *commit_id,
816 struct got_reference *head_ref, struct got_object_id *yca_id,
817 struct got_repository *repo)
819 const struct got_error *err = NULL;
820 struct got_commit_graph *graph = NULL;
821 struct got_object_id *head_commit_id = NULL;
822 int is_same_branch = 0;
824 err = got_ref_resolve(&head_commit_id, repo, head_ref);
828 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
832 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
837 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
841 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
842 check_cancelled, NULL);
847 struct got_object_id *id;
848 err = got_commit_graph_iter_next(&id, graph);
850 if (err->code == GOT_ERR_ITER_COMPLETED) {
853 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
855 err = got_commit_graph_fetch_commits(graph, 1,
856 repo, check_cancelled, NULL);
862 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
864 if (got_object_id_cmp(id, commit_id) == 0) {
872 got_commit_graph_close(graph);
873 free(head_commit_id);
874 if (!err && !is_same_branch)
875 err = got_error(GOT_ERR_ANCESTRY);
879 static const struct got_error *
880 resolve_commit_arg(struct got_object_id **commit_id,
881 const char *commit_id_arg, struct got_repository *repo)
883 const struct got_error *err;
884 struct got_reference *ref;
885 struct got_tag_object *tag;
887 err = got_repo_object_match_tag(&tag, commit_id_arg,
888 GOT_OBJ_TYPE_COMMIT, repo);
890 *commit_id = got_object_id_dup(
891 got_object_tag_get_object_id(tag));
892 if (*commit_id == NULL)
893 err = got_error_from_errno("got_object_id_dup");
894 got_object_tag_close(tag);
896 } else if (err->code != GOT_ERR_NO_OBJ)
899 err = got_ref_open(&ref, repo, commit_id_arg, 0);
901 err = got_ref_resolve(commit_id, repo, ref);
904 if (err->code != GOT_ERR_NOT_REF)
906 err = got_repo_match_object_id_prefix(commit_id,
907 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
912 static const struct got_error *
913 cmd_checkout(int argc, char *argv[])
915 const struct got_error *error = NULL;
916 struct got_repository *repo = NULL;
917 struct got_reference *head_ref = NULL;
918 struct got_worktree *worktree = NULL;
919 char *repo_path = NULL;
920 char *worktree_path = NULL;
921 const char *path_prefix = "";
922 const char *branch_name = GOT_REF_HEAD;
923 char *commit_id_str = NULL;
924 int ch, same_path_prefix;
925 struct got_pathlist_head paths;
929 while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
932 branch_name = optarg;
935 commit_id_str = strdup(optarg);
936 if (commit_id_str == NULL)
937 return got_error_from_errno("strdup");
940 path_prefix = optarg;
952 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
953 "unveil", NULL) == -1)
957 char *cwd, *base, *dotgit;
958 repo_path = realpath(argv[0], NULL);
959 if (repo_path == NULL)
960 return got_error_from_errno2("realpath", argv[0]);
961 cwd = getcwd(NULL, 0);
963 error = got_error_from_errno("getcwd");
966 if (path_prefix[0]) {
967 base = basename(path_prefix);
969 error = got_error_from_errno2("basename",
974 base = basename(repo_path);
976 error = got_error_from_errno2("basename",
981 dotgit = strstr(base, ".git");
984 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
985 error = got_error_from_errno("asprintf");
990 } else if (argc == 2) {
991 repo_path = realpath(argv[0], NULL);
992 if (repo_path == NULL) {
993 error = got_error_from_errno2("realpath", argv[0]);
996 worktree_path = realpath(argv[1], NULL);
997 if (worktree_path == NULL) {
998 if (errno != ENOENT) {
999 error = got_error_from_errno2("realpath",
1003 worktree_path = strdup(argv[1]);
1004 if (worktree_path == NULL) {
1005 error = got_error_from_errno("strdup");
1012 got_path_strip_trailing_slashes(repo_path);
1013 got_path_strip_trailing_slashes(worktree_path);
1015 error = got_repo_open(&repo, repo_path, NULL);
1019 /* Pre-create work tree path for unveil(2) */
1020 error = got_path_mkdir(worktree_path);
1022 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1023 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1025 if (!got_path_dir_is_empty(worktree_path)) {
1026 error = got_error_path(worktree_path,
1027 GOT_ERR_DIR_NOT_EMPTY);
1032 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1036 error = got_ref_open(&head_ref, repo, branch_name, 0);
1040 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1041 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1044 error = got_worktree_open(&worktree, worktree_path);
1048 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1052 if (!same_path_prefix) {
1053 error = got_error(GOT_ERR_PATH_PREFIX);
1057 if (commit_id_str) {
1058 struct got_object_id *commit_id;
1059 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1062 error = check_linear_ancestry(commit_id,
1063 got_worktree_get_base_commit_id(worktree), repo);
1064 if (error != NULL) {
1068 error = check_same_branch(commit_id, head_ref, NULL, repo);
1071 error = got_worktree_set_base_commit_id(worktree, repo,
1078 error = got_pathlist_append(&paths, "", NULL);
1081 error = got_worktree_checkout_files(worktree, &paths, repo,
1082 checkout_progress, worktree_path, check_cancelled, NULL);
1086 printf("Now shut up and hack\n");
1089 got_pathlist_free(&paths);
1090 free(commit_id_str);
1092 free(worktree_path);
1099 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1104 static const struct got_error *
1105 update_progress(void *arg, unsigned char status, const char *path)
1107 int *did_something = arg;
1109 if (status == GOT_STATUS_EXISTS)
1114 /* Base commit bump happens silently. */
1115 if (status == GOT_STATUS_BUMP_BASE)
1118 while (path[0] == '/')
1120 printf("%c %s\n", status, path);
1124 static const struct got_error *
1125 switch_head_ref(struct got_reference *head_ref,
1126 struct got_object_id *commit_id, struct got_worktree *worktree,
1127 struct got_repository *repo)
1129 const struct got_error *err = NULL;
1131 int ref_has_moved = 0;
1133 /* Trivial case: switching between two different references. */
1134 if (strcmp(got_ref_get_name(head_ref),
1135 got_worktree_get_head_ref_name(worktree)) != 0) {
1136 printf("Switching work tree from %s to %s\n",
1137 got_worktree_get_head_ref_name(worktree),
1138 got_ref_get_name(head_ref));
1139 return got_worktree_set_head_ref(worktree, head_ref);
1142 err = check_linear_ancestry(commit_id,
1143 got_worktree_get_base_commit_id(worktree), repo);
1145 if (err->code != GOT_ERR_ANCESTRY)
1152 /* Switching to a rebased branch with the same reference name. */
1153 err = got_object_id_str(&base_id_str,
1154 got_worktree_get_base_commit_id(worktree));
1157 printf("Reference %s now points at a different branch\n",
1158 got_worktree_get_head_ref_name(worktree));
1159 printf("Switching work tree from %s to %s\n", base_id_str,
1160 got_worktree_get_head_ref_name(worktree));
1164 static const struct got_error *
1165 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1167 const struct got_error *err;
1170 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1174 return got_error(GOT_ERR_REBASING);
1176 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1180 return got_error(GOT_ERR_HISTEDIT_BUSY);
1185 static const struct got_error *
1186 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1187 char *argv[], struct got_worktree *worktree)
1189 const struct got_error *err = NULL;
1196 return got_error_from_errno("strdup");
1197 return got_pathlist_append(paths, path, NULL);
1200 for (i = 0; i < argc; i++) {
1201 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1204 err = got_pathlist_append(paths, path, NULL);
1214 static const struct got_error *
1215 cmd_update(int argc, char *argv[])
1217 const struct got_error *error = NULL;
1218 struct got_repository *repo = NULL;
1219 struct got_worktree *worktree = NULL;
1220 char *worktree_path = NULL;
1221 struct got_object_id *commit_id = NULL;
1222 char *commit_id_str = NULL;
1223 const char *branch_name = NULL;
1224 struct got_reference *head_ref = NULL;
1225 struct got_pathlist_head paths;
1226 struct got_pathlist_entry *pe;
1227 int ch, did_something = 0;
1231 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1234 branch_name = optarg;
1237 commit_id_str = strdup(optarg);
1238 if (commit_id_str == NULL)
1239 return got_error_from_errno("strdup");
1251 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1252 "unveil", NULL) == -1)
1255 worktree_path = getcwd(NULL, 0);
1256 if (worktree_path == NULL) {
1257 error = got_error_from_errno("getcwd");
1260 error = got_worktree_open(&worktree, worktree_path);
1264 error = check_rebase_or_histedit_in_progress(worktree);
1268 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
1273 error = apply_unveil(got_repo_get_path(repo), 0,
1274 got_worktree_get_root_path(worktree));
1278 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1282 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1283 got_worktree_get_head_ref_name(worktree), 0);
1286 if (commit_id_str == NULL) {
1287 error = got_ref_resolve(&commit_id, repo, head_ref);
1290 error = got_object_id_str(&commit_id_str, commit_id);
1294 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1295 free(commit_id_str);
1296 commit_id_str = NULL;
1299 error = got_object_id_str(&commit_id_str, commit_id);
1305 struct got_object_id *head_commit_id;
1306 TAILQ_FOREACH(pe, &paths, entry) {
1307 if (pe->path_len == 0)
1309 error = got_error_msg(GOT_ERR_BAD_PATH,
1310 "switching between branches requires that "
1311 "the entire work tree gets updated");
1314 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1317 error = check_linear_ancestry(commit_id, head_commit_id, repo);
1318 free(head_commit_id);
1321 error = check_same_branch(commit_id, head_ref, NULL, repo);
1324 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1328 error = check_linear_ancestry(commit_id,
1329 got_worktree_get_base_commit_id(worktree), repo);
1330 if (error != NULL) {
1331 if (error->code == GOT_ERR_ANCESTRY)
1332 error = got_error(GOT_ERR_BRANCH_MOVED);
1335 error = check_same_branch(commit_id, head_ref, NULL, repo);
1340 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1342 error = got_worktree_set_base_commit_id(worktree, repo,
1348 error = got_worktree_checkout_files(worktree, &paths, repo,
1349 update_progress, &did_something, check_cancelled, NULL);
1354 printf("Updated to commit %s\n", commit_id_str);
1356 printf("Already up-to-date\n");
1358 free(worktree_path);
1359 TAILQ_FOREACH(pe, &paths, entry)
1360 free((char *)pe->path);
1361 got_pathlist_free(&paths);
1363 free(commit_id_str);
1367 static const struct got_error *
1368 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
1369 const char *path, int diff_context, int ignore_whitespace,
1370 struct got_repository *repo)
1372 const struct got_error *err = NULL;
1373 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1376 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
1381 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
1385 while (path[0] == '/')
1387 err = got_diff_blob(blob1, blob2, path, path, diff_context,
1388 ignore_whitespace, stdout);
1391 got_object_blob_close(blob1);
1392 got_object_blob_close(blob2);
1396 static const struct got_error *
1397 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
1398 const char *path, int diff_context, int ignore_whitespace,
1399 struct got_repository *repo)
1401 const struct got_error *err = NULL;
1402 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1403 struct got_diff_blob_output_unidiff_arg arg;
1406 err = got_object_open_as_tree(&tree1, repo, tree_id1);
1411 err = got_object_open_as_tree(&tree2, repo, tree_id2);
1415 arg.diff_context = diff_context;
1416 arg.ignore_whitespace = ignore_whitespace;
1417 arg.outfile = stdout;
1418 while (path[0] == '/')
1420 err = got_diff_tree(tree1, tree2, path, path, repo,
1421 got_diff_blob_output_unidiff, &arg, 1);
1424 got_object_tree_close(tree1);
1426 got_object_tree_close(tree2);
1430 static const struct got_error *
1431 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1432 const char *path, int diff_context, struct got_repository *repo)
1434 const struct got_error *err = NULL;
1435 struct got_commit_object *pcommit = NULL;
1436 char *id_str1 = NULL, *id_str2 = NULL;
1437 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
1438 struct got_object_qid *qid;
1440 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1442 err = got_object_open_as_commit(&pcommit, repo,
1448 if (path && path[0] != '\0') {
1450 err = got_object_id_by_path(&obj_id2, repo, id, path);
1453 err = got_object_id_str(&id_str2, obj_id2);
1459 err = got_object_id_by_path(&obj_id1, repo,
1465 err = got_object_id_str(&id_str1, obj_id1);
1471 err = got_object_get_type(&obj_type, repo, obj_id2);
1476 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1478 case GOT_OBJ_TYPE_BLOB:
1479 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
1482 case GOT_OBJ_TYPE_TREE:
1483 err = diff_trees(obj_id1, obj_id2, path, diff_context,
1487 err = got_error(GOT_ERR_OBJ_TYPE);
1493 obj_id2 = got_object_commit_get_tree_id(commit);
1494 err = got_object_id_str(&id_str2, obj_id2);
1497 obj_id1 = got_object_commit_get_tree_id(pcommit);
1498 err = got_object_id_str(&id_str1, obj_id1);
1501 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1502 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
1509 got_object_commit_close(pcommit);
1514 get_datestr(time_t *time, char *datebuf)
1516 struct tm mytm, *tm;
1519 tm = gmtime_r(time, &mytm);
1522 s = asctime_r(tm, datebuf);
1525 p = strchr(s, '\n');
1531 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1533 static const struct got_error *
1534 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1535 struct got_repository *repo, const char *path, int show_patch,
1536 int diff_context, struct got_reflist_head *refs)
1538 const struct got_error *err = NULL;
1539 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1541 time_t committer_time;
1542 const char *author, *committer;
1543 char *refs_str = NULL;
1544 struct got_reflist_entry *re;
1546 SIMPLEQ_FOREACH(re, refs, entry) {
1549 struct got_tag_object *tag = NULL;
1552 name = got_ref_get_name(re->ref);
1553 if (strcmp(name, GOT_REF_HEAD) == 0)
1555 if (strncmp(name, "refs/", 5) == 0)
1557 if (strncmp(name, "got/", 4) == 0)
1559 if (strncmp(name, "heads/", 6) == 0)
1561 if (strncmp(name, "remotes/", 8) == 0)
1563 if (strncmp(name, "tags/", 5) == 0) {
1564 err = got_object_open_as_tag(&tag, repo, re->id);
1566 if (err->code != GOT_ERR_OBJ_TYPE)
1568 /* Ref points at something other than a tag. */
1573 cmp = got_object_id_cmp(tag ?
1574 got_object_tag_get_object_id(tag) : re->id, id);
1576 got_object_tag_close(tag);
1580 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1582 err = got_error_from_errno("asprintf");
1588 err = got_object_id_str(&id_str, id);
1592 printf(GOT_COMMIT_SEP_STR);
1593 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1594 refs_str ? refs_str : "", refs_str ? ")" : "");
1599 printf("from: %s\n", got_object_commit_get_author(commit));
1600 committer_time = got_object_commit_get_committer_time(commit);
1601 datestr = get_datestr(&committer_time, datebuf);
1603 printf("date: %s UTC\n", datestr);
1604 author = got_object_commit_get_author(commit);
1605 committer = got_object_commit_get_committer(commit);
1606 if (strcmp(author, committer) != 0)
1607 printf("via: %s\n", committer);
1608 if (got_object_commit_get_nparents(commit) > 1) {
1609 const struct got_object_id_queue *parent_ids;
1610 struct got_object_qid *qid;
1612 parent_ids = got_object_commit_get_parent_ids(commit);
1613 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1614 err = got_object_id_str(&id_str, qid->id);
1617 printf("parent %d: %s\n", n++, id_str);
1622 err = got_object_commit_get_logmsg(&logmsg0, commit);
1628 line = strsep(&logmsg, "\n");
1630 printf(" %s\n", line);
1635 err = print_patch(commit, id, path, diff_context, repo);
1640 if (fflush(stdout) != 0 && err == NULL)
1641 err = got_error_from_errno("fflush");
1645 static const struct got_error *
1646 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1647 char *path, int show_patch, int diff_context, int limit,
1648 int first_parent_traversal, struct got_reflist_head *refs)
1650 const struct got_error *err;
1651 struct got_commit_graph *graph;
1653 err = got_commit_graph_open(&graph, root_id, path,
1654 first_parent_traversal, repo);
1657 err = got_commit_graph_iter_start(graph, root_id, repo,
1658 check_cancelled, NULL);
1662 struct got_commit_object *commit;
1663 struct got_object_id *id;
1665 if (sigint_received || sigpipe_received)
1668 err = got_commit_graph_iter_next(&id, graph);
1670 if (err->code == GOT_ERR_ITER_COMPLETED) {
1674 if (err->code != GOT_ERR_ITER_NEED_MORE)
1676 err = got_commit_graph_fetch_commits(graph, 1, repo,
1677 check_cancelled, NULL);
1686 err = got_object_open_as_commit(&commit, repo, id);
1689 err = print_commit(commit, id, repo, path, show_patch,
1690 diff_context, refs);
1691 got_object_commit_close(commit);
1692 if (err || (limit && --limit == 0))
1696 got_commit_graph_close(graph);
1703 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1704 "[-r repository-path] [path]\n", getprogname());
1709 get_default_log_limit(void)
1711 const char *got_default_log_limit;
1715 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
1716 if (got_default_log_limit == NULL)
1718 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
1724 static const struct got_error *
1725 cmd_log(int argc, char *argv[])
1727 const struct got_error *error;
1728 struct got_repository *repo = NULL;
1729 struct got_worktree *worktree = NULL;
1730 struct got_commit_object *commit = NULL;
1731 struct got_object_id *id = NULL;
1732 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1733 char *start_commit = NULL;
1734 int diff_context = 3, ch;
1735 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1737 struct got_reflist_head refs;
1739 SIMPLEQ_INIT(&refs);
1742 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1748 limit = get_default_log_limit();
1750 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
1756 start_commit = optarg;
1759 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1762 err(1, "-C option %s", errstr);
1765 limit = strtonum(optarg, 0, INT_MAX, &errstr);
1767 err(1, "-l option %s", errstr);
1770 first_parent_traversal = 1;
1773 repo_path = realpath(optarg, NULL);
1774 if (repo_path == NULL)
1775 err(1, "-r option");
1776 got_path_strip_trailing_slashes(repo_path);
1787 cwd = getcwd(NULL, 0);
1789 error = got_error_from_errno("getcwd");
1793 error = got_worktree_open(&worktree, cwd);
1794 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1801 error = got_error_from_errno("strdup");
1804 } else if (argc == 1) {
1806 error = got_worktree_resolve_path(&path, worktree,
1811 path = strdup(argv[0]);
1813 error = got_error_from_errno("strdup");
1820 if (repo_path == NULL) {
1821 repo_path = worktree ?
1822 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1824 if (repo_path == NULL) {
1825 error = got_error_from_errno("strdup");
1829 error = got_repo_open(&repo, repo_path, NULL);
1833 error = apply_unveil(got_repo_get_path(repo), 1,
1834 worktree ? got_worktree_get_root_path(worktree) : NULL);
1838 if (start_commit == NULL) {
1839 struct got_reference *head_ref;
1840 error = got_ref_open(&head_ref, repo,
1841 worktree ? got_worktree_get_head_ref_name(worktree)
1845 error = got_ref_resolve(&id, repo, head_ref);
1846 got_ref_close(head_ref);
1849 error = got_object_open_as_commit(&commit, repo, id);
1851 struct got_reference *ref;
1852 error = got_ref_open(&ref, repo, start_commit, 0);
1853 if (error == NULL) {
1855 error = got_ref_resolve(&id, repo, ref);
1859 error = got_object_get_type(&obj_type, repo, id);
1862 if (obj_type == GOT_OBJ_TYPE_TAG) {
1863 struct got_tag_object *tag;
1864 error = got_object_open_as_tag(&tag, repo, id);
1867 if (got_object_tag_get_object_type(tag) !=
1868 GOT_OBJ_TYPE_COMMIT) {
1869 got_object_tag_close(tag);
1870 error = got_error(GOT_ERR_OBJ_TYPE);
1874 id = got_object_id_dup(
1875 got_object_tag_get_object_id(tag));
1877 error = got_error_from_errno(
1878 "got_object_id_dup");
1879 got_object_tag_close(tag);
1882 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1883 error = got_error(GOT_ERR_OBJ_TYPE);
1886 error = got_object_open_as_commit(&commit, repo, id);
1890 if (commit == NULL) {
1891 error = got_repo_match_object_id_prefix(&id,
1892 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1901 const char *prefix = got_worktree_get_path_prefix(worktree);
1903 if (asprintf(&p, "%s%s%s", prefix,
1904 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
1905 error = got_error_from_errno("asprintf");
1908 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1911 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1916 path = in_repo_path;
1919 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
1923 error = print_commits(id, repo, path, show_patch,
1924 diff_context, limit, first_parent_traversal, &refs);
1931 got_worktree_close(worktree);
1933 const struct got_error *repo_error;
1934 repo_error = got_repo_close(repo);
1938 got_ref_list_free(&refs);
1945 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
1946 "[-w] [object1 object2 | path]\n", getprogname());
1950 struct print_diff_arg {
1951 struct got_repository *repo;
1952 struct got_worktree *worktree;
1957 int ignore_whitespace;
1960 static const struct got_error *
1961 print_diff(void *arg, unsigned char status, unsigned char staged_status,
1962 const char *path, struct got_object_id *blob_id,
1963 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
1965 struct print_diff_arg *a = arg;
1966 const struct got_error *err = NULL;
1967 struct got_blob_object *blob1 = NULL;
1969 char *abspath = NULL, *label1 = NULL;
1972 if (a->diff_staged) {
1973 if (staged_status != GOT_STATUS_MODIFY &&
1974 staged_status != GOT_STATUS_ADD &&
1975 staged_status != GOT_STATUS_DELETE)
1978 if (staged_status == GOT_STATUS_DELETE)
1980 if (status == GOT_STATUS_NONEXISTENT)
1981 return got_error_set_errno(ENOENT, path);
1982 if (status != GOT_STATUS_MODIFY &&
1983 status != GOT_STATUS_ADD &&
1984 status != GOT_STATUS_DELETE &&
1985 status != GOT_STATUS_CONFLICT)
1989 if (!a->header_shown) {
1990 printf("diff %s %s%s\n", a->id_str,
1991 got_worktree_get_root_path(a->worktree),
1992 a->diff_staged ? " (staged changes)" : "");
1993 a->header_shown = 1;
1996 if (a->diff_staged) {
1997 const char *label1 = NULL, *label2 = NULL;
1998 switch (staged_status) {
1999 case GOT_STATUS_MODIFY:
2003 case GOT_STATUS_ADD:
2006 case GOT_STATUS_DELETE:
2010 return got_error(GOT_ERR_FILE_STATUS);
2012 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2013 label1, label2, a->diff_context, a->ignore_whitespace,
2017 if (staged_status == GOT_STATUS_ADD ||
2018 staged_status == GOT_STATUS_MODIFY) {
2020 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2024 err = got_object_id_str(&id_str, staged_blob_id);
2027 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2028 err = got_error_from_errno("asprintf");
2033 } else if (status != GOT_STATUS_ADD) {
2034 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2039 if (status != GOT_STATUS_DELETE) {
2040 if (asprintf(&abspath, "%s/%s",
2041 got_worktree_get_root_path(a->worktree), path) == -1) {
2042 err = got_error_from_errno("asprintf");
2046 f2 = fopen(abspath, "r");
2048 err = got_error_from_errno2("fopen", abspath);
2051 if (lstat(abspath, &sb) == -1) {
2052 err = got_error_from_errno2("lstat", abspath);
2058 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2059 a->diff_context, a->ignore_whitespace, stdout);
2062 got_object_blob_close(blob1);
2063 if (f2 && fclose(f2) != 0 && err == NULL)
2064 err = got_error_from_errno("fclose");
2069 static const struct got_error *
2070 match_object_id(struct got_object_id **id, char **label,
2071 const char *id_str, int obj_type, int resolve_tags,
2072 struct got_repository *repo)
2074 const struct got_error *err;
2075 struct got_tag_object *tag;
2076 struct got_reference *ref = NULL;
2082 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
2085 *id = got_object_id_dup(
2086 got_object_tag_get_object_id(tag));
2088 err = got_error_from_errno("got_object_id_dup");
2089 else if (asprintf(label, "refs/tags/%s",
2090 got_object_tag_get_name(tag)) == -1) {
2091 err = got_error_from_errno("asprintf");
2095 got_object_tag_close(tag);
2097 } else if (err->code != GOT_ERR_NO_OBJ)
2101 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
2103 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2105 err = got_ref_open(&ref, repo, id_str, 0);
2108 *label = strdup(got_ref_get_name(ref));
2109 if (*label == NULL) {
2110 err = got_error_from_errno("strdup");
2113 err = got_ref_resolve(id, repo, ref);
2115 err = got_object_id_str(label, *id);
2116 if (*label == NULL) {
2117 err = got_error_from_errno("strdup");
2128 static const struct got_error *
2129 cmd_diff(int argc, char *argv[])
2131 const struct got_error *error;
2132 struct got_repository *repo = NULL;
2133 struct got_worktree *worktree = NULL;
2134 char *cwd = NULL, *repo_path = NULL;
2135 struct got_object_id *id1 = NULL, *id2 = NULL;
2136 const char *id_str1 = NULL, *id_str2 = NULL;
2137 char *label1 = NULL, *label2 = NULL;
2139 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2144 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2149 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2152 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
2154 err(1, "-C option %s", errstr);
2157 repo_path = realpath(optarg, NULL);
2158 if (repo_path == NULL)
2159 err(1, "-r option");
2160 got_path_strip_trailing_slashes(repo_path);
2166 ignore_whitespace = 1;
2177 cwd = getcwd(NULL, 0);
2179 error = got_error_from_errno("getcwd");
2182 error = got_worktree_open(&worktree, cwd);
2183 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2186 if (worktree == NULL) {
2187 error = got_error(GOT_ERR_NOT_WORKTREE);
2192 "-r option can't be used when diffing a work tree");
2193 repo_path = strdup(got_worktree_get_repo_path(worktree));
2194 if (repo_path == NULL) {
2195 error = got_error_from_errno("strdup");
2199 error = got_worktree_resolve_path(&path, worktree,
2206 error = got_error_from_errno("strdup");
2210 } else if (argc == 2) {
2212 errx(1, "-s option can't be used when diffing "
2213 "objects in repository");
2216 if (worktree && repo_path == NULL) {
2218 strdup(got_worktree_get_repo_path(worktree));
2219 if (repo_path == NULL) {
2220 error = got_error_from_errno("strdup");
2227 if (repo_path == NULL) {
2228 repo_path = getcwd(NULL, 0);
2229 if (repo_path == NULL)
2230 return got_error_from_errno("getcwd");
2233 error = got_repo_open(&repo, repo_path, NULL);
2238 error = apply_unveil(got_repo_get_path(repo), 1,
2239 worktree ? got_worktree_get_root_path(worktree) : NULL);
2244 struct print_diff_arg arg;
2245 struct got_pathlist_head paths;
2250 error = got_object_id_str(&id_str,
2251 got_worktree_get_base_commit_id(worktree));
2255 arg.worktree = worktree;
2256 arg.diff_context = diff_context;
2257 arg.id_str = id_str;
2258 arg.header_shown = 0;
2259 arg.diff_staged = diff_staged;
2260 arg.ignore_whitespace = ignore_whitespace;
2262 error = got_pathlist_append(&paths, path, NULL);
2266 error = got_worktree_status(worktree, &paths, repo, print_diff,
2267 &arg, check_cancelled, NULL);
2269 got_pathlist_free(&paths);
2273 error = match_object_id(&id1, &label1, id_str1, GOT_OBJ_TYPE_ANY, 1,
2278 error = match_object_id(&id2, &label2, id_str2, GOT_OBJ_TYPE_ANY, 1,
2283 error = got_object_get_type(&type1, repo, id1);
2287 error = got_object_get_type(&type2, repo, id2);
2291 if (type1 != type2) {
2292 error = got_error(GOT_ERR_OBJ_TYPE);
2297 case GOT_OBJ_TYPE_BLOB:
2298 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2299 diff_context, ignore_whitespace, repo, stdout);
2301 case GOT_OBJ_TYPE_TREE:
2302 error = got_diff_objects_as_trees(id1, id2, "", "",
2303 diff_context, ignore_whitespace, repo, stdout);
2305 case GOT_OBJ_TYPE_COMMIT:
2306 printf("diff %s %s\n", label1, label2);
2307 error = got_diff_objects_as_commits(id1, id2, diff_context,
2308 ignore_whitespace, repo, stdout);
2311 error = got_error(GOT_ERR_OBJ_TYPE);
2321 got_worktree_close(worktree);
2323 const struct got_error *repo_error;
2324 repo_error = got_repo_close(repo);
2335 "usage: %s blame [-c commit] [-r repository-path] path\n",
2344 char datebuf[9]; /* YY-MM-DD + NUL */
2347 struct blame_cb_args {
2348 struct blame_line *lines;
2352 off_t *line_offsets;
2354 struct got_repository *repo;
2357 static const struct got_error *
2358 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2360 const struct got_error *err = NULL;
2361 struct blame_cb_args *a = arg;
2362 struct blame_line *bline;
2364 size_t linesize = 0;
2365 struct got_commit_object *commit = NULL;
2368 time_t committer_time;
2370 if (nlines != a->nlines ||
2371 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2372 return got_error(GOT_ERR_RANGE);
2374 if (sigint_received)
2375 return got_error(GOT_ERR_ITER_COMPLETED);
2378 return NULL; /* no change in this commit */
2380 /* Annotate this line. */
2381 bline = &a->lines[lineno - 1];
2382 if (bline->annotated)
2384 err = got_object_id_str(&bline->id_str, id);
2388 err = got_object_open_as_commit(&commit, a->repo, id);
2392 bline->committer = strdup(got_object_commit_get_committer(commit));
2393 if (bline->committer == NULL) {
2394 err = got_error_from_errno("strdup");
2398 committer_time = got_object_commit_get_committer_time(commit);
2399 if (localtime_r(&committer_time, &tm) == NULL)
2400 return got_error_from_errno("localtime_r");
2401 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%g/%m/%d",
2402 &tm) >= sizeof(bline->datebuf)) {
2403 err = got_error(GOT_ERR_NO_SPACE);
2406 bline->annotated = 1;
2408 /* Print lines annotated so far. */
2409 bline = &a->lines[a->lineno_cur - 1];
2410 if (!bline->annotated)
2413 offset = a->line_offsets[a->lineno_cur - 1];
2414 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2415 err = got_error_from_errno("fseeko");
2419 while (bline->annotated) {
2420 char *smallerthan, *at, *nl, *committer;
2423 if (getline(&line, &linesize, a->f) == -1) {
2425 err = got_error_from_errno("getline");
2429 committer = bline->committer;
2430 smallerthan = strchr(committer, '<');
2431 if (smallerthan && smallerthan[1] != '\0')
2432 committer = smallerthan + 1;
2433 at = strchr(committer, '@');
2436 len = strlen(committer);
2438 committer[8] = '\0';
2440 nl = strchr(line, '\n');
2443 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2444 bline->id_str, bline->datebuf, committer, line);
2447 bline = &a->lines[a->lineno_cur - 1];
2451 got_object_commit_close(commit);
2456 static const struct got_error *
2457 cmd_blame(int argc, char *argv[])
2459 const struct got_error *error;
2460 struct got_repository *repo = NULL;
2461 struct got_worktree *worktree = NULL;
2462 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2463 struct got_object_id *obj_id = NULL;
2464 struct got_object_id *commit_id = NULL;
2465 struct got_blob_object *blob = NULL;
2466 char *commit_id_str = NULL;
2467 struct blame_cb_args bca;
2468 int ch, obj_type, i;
2471 memset(&bca, 0, sizeof(bca));
2474 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2479 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2482 commit_id_str = optarg;
2485 repo_path = realpath(optarg, NULL);
2486 if (repo_path == NULL)
2487 err(1, "-r option");
2488 got_path_strip_trailing_slashes(repo_path);
2504 cwd = getcwd(NULL, 0);
2506 error = got_error_from_errno("getcwd");
2509 if (repo_path == NULL) {
2510 error = got_worktree_open(&worktree, cwd);
2511 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2517 strdup(got_worktree_get_repo_path(worktree));
2518 if (repo_path == NULL)
2519 error = got_error_from_errno("strdup");
2523 repo_path = strdup(cwd);
2524 if (repo_path == NULL) {
2525 error = got_error_from_errno("strdup");
2531 error = got_repo_open(&repo, repo_path, NULL);
2535 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2540 const char *prefix = got_worktree_get_path_prefix(worktree);
2541 char *p, *worktree_subdir = cwd +
2542 strlen(got_worktree_get_root_path(worktree));
2543 if (asprintf(&p, "%s%s%s%s%s",
2544 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2545 worktree_subdir, worktree_subdir[0] ? "/" : "",
2547 error = got_error_from_errno("asprintf");
2550 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2553 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2558 if (commit_id_str == NULL) {
2559 struct got_reference *head_ref;
2560 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2563 error = got_ref_resolve(&commit_id, repo, head_ref);
2564 got_ref_close(head_ref);
2568 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2573 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2576 if (obj_id == NULL) {
2577 error = got_error(GOT_ERR_NO_OBJ);
2581 error = got_object_get_type(&obj_type, repo, obj_id);
2585 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2586 error = got_error(GOT_ERR_OBJ_TYPE);
2590 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2593 bca.f = got_opentemp();
2594 if (bca.f == NULL) {
2595 error = got_error_from_errno("got_opentemp");
2598 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2599 &bca.line_offsets, bca.f, blob);
2600 if (error || bca.nlines == 0)
2603 /* Don't include \n at EOF in the blame line count. */
2604 if (bca.line_offsets[bca.nlines - 1] == filesize)
2607 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2608 if (bca.lines == NULL) {
2609 error = got_error_from_errno("calloc");
2613 bca.nlines_prec = 0;
2621 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2622 check_cancelled, NULL);
2632 got_object_blob_close(blob);
2634 got_worktree_close(worktree);
2636 const struct got_error *repo_error;
2637 repo_error = got_repo_close(repo);
2642 for (i = 0; i < bca.nlines; i++) {
2643 struct blame_line *bline = &bca.lines[i];
2644 free(bline->id_str);
2645 free(bline->committer);
2649 free(bca.line_offsets);
2650 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2651 error = got_error_from_errno("fclose");
2659 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2665 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2666 const char *root_path)
2668 int is_root_path = (strcmp(path, root_path) == 0);
2669 const char *modestr = "";
2671 path += strlen(root_path);
2672 while (path[0] == '/')
2675 if (got_object_tree_entry_is_submodule(te))
2677 else if (S_ISLNK(te->mode))
2679 else if (S_ISDIR(te->mode))
2681 else if (te->mode & S_IXUSR)
2684 printf("%s%s%s%s%s\n", id ? id : "", path,
2685 is_root_path ? "" : "/", te->name, modestr);
2688 static const struct got_error *
2689 print_tree(const char *path, struct got_object_id *commit_id,
2690 int show_ids, int recurse, const char *root_path,
2691 struct got_repository *repo)
2693 const struct got_error *err = NULL;
2694 struct got_object_id *tree_id = NULL;
2695 struct got_tree_object *tree = NULL;
2696 const struct got_tree_entries *entries;
2697 struct got_tree_entry *te;
2699 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2703 err = got_object_open_as_tree(&tree, repo, tree_id);
2706 entries = got_object_tree_get_entries(tree);
2707 te = SIMPLEQ_FIRST(&entries->head);
2711 if (sigint_received || sigpipe_received)
2716 err = got_object_id_str(&id_str, te->id);
2719 if (asprintf(&id, "%s ", id_str) == -1) {
2720 err = got_error_from_errno("asprintf");
2726 print_entry(te, id, path, root_path);
2729 if (recurse && S_ISDIR(te->mode)) {
2731 if (asprintf(&child_path, "%s%s%s", path,
2732 path[0] == '/' && path[1] == '\0' ? "" : "/",
2734 err = got_error_from_errno("asprintf");
2737 err = print_tree(child_path, commit_id, show_ids, 1,
2744 te = SIMPLEQ_NEXT(te, entry);
2748 got_object_tree_close(tree);
2753 static const struct got_error *
2754 cmd_tree(int argc, char *argv[])
2756 const struct got_error *error;
2757 struct got_repository *repo = NULL;
2758 struct got_worktree *worktree = NULL;
2760 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2761 struct got_object_id *commit_id = NULL;
2762 char *commit_id_str = NULL;
2763 int show_ids = 0, recurse = 0;
2767 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2772 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2775 commit_id_str = optarg;
2778 repo_path = realpath(optarg, NULL);
2779 if (repo_path == NULL)
2780 err(1, "-r option");
2781 got_path_strip_trailing_slashes(repo_path);
2805 cwd = getcwd(NULL, 0);
2807 error = got_error_from_errno("getcwd");
2810 if (repo_path == NULL) {
2811 error = got_worktree_open(&worktree, cwd);
2812 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2818 strdup(got_worktree_get_repo_path(worktree));
2819 if (repo_path == NULL)
2820 error = got_error_from_errno("strdup");
2824 repo_path = strdup(cwd);
2825 if (repo_path == NULL) {
2826 error = got_error_from_errno("strdup");
2832 error = got_repo_open(&repo, repo_path, NULL);
2836 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2842 char *p, *worktree_subdir = cwd +
2843 strlen(got_worktree_get_root_path(worktree));
2844 if (asprintf(&p, "%s/%s",
2845 got_worktree_get_path_prefix(worktree),
2846 worktree_subdir) == -1) {
2847 error = got_error_from_errno("asprintf");
2850 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2857 if (in_repo_path == NULL) {
2858 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2863 if (commit_id_str == NULL) {
2864 struct got_reference *head_ref;
2865 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2868 error = got_ref_resolve(&commit_id, repo, head_ref);
2869 got_ref_close(head_ref);
2873 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2878 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2879 in_repo_path, repo);
2886 got_worktree_close(worktree);
2888 const struct got_error *repo_error;
2889 repo_error = got_repo_close(repo);
2899 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
2903 static const struct got_error *
2904 print_status(void *arg, unsigned char status, unsigned char staged_status,
2905 const char *path, struct got_object_id *blob_id,
2906 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
2908 if (status == staged_status && (status == GOT_STATUS_DELETE))
2909 status = GOT_STATUS_NO_CHANGE;
2910 printf("%c%c %s\n", status, staged_status, path);
2914 static const struct got_error *
2915 cmd_status(int argc, char *argv[])
2917 const struct got_error *error = NULL;
2918 struct got_repository *repo = NULL;
2919 struct got_worktree *worktree = NULL;
2921 struct got_pathlist_head paths;
2922 struct got_pathlist_entry *pe;
2927 while ((ch = getopt(argc, argv, "")) != -1) {
2939 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2943 cwd = getcwd(NULL, 0);
2945 error = got_error_from_errno("getcwd");
2949 error = got_worktree_open(&worktree, cwd);
2953 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
2958 error = apply_unveil(got_repo_get_path(repo), 1,
2959 got_worktree_get_root_path(worktree));
2963 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2967 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
2968 check_cancelled, NULL);
2970 TAILQ_FOREACH(pe, &paths, entry)
2971 free((char *)pe->path);
2972 got_pathlist_free(&paths);
2981 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
2986 static const struct got_error *
2987 list_refs(struct got_repository *repo)
2989 static const struct got_error *err = NULL;
2990 struct got_reflist_head refs;
2991 struct got_reflist_entry *re;
2993 SIMPLEQ_INIT(&refs);
2994 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2998 SIMPLEQ_FOREACH(re, &refs, entry) {
3000 refstr = got_ref_to_str(re->ref);
3002 return got_error_from_errno("got_ref_to_str");
3003 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3007 got_ref_list_free(&refs);
3011 static const struct got_error *
3012 delete_ref(struct got_repository *repo, const char *refname)
3014 const struct got_error *err = NULL;
3015 struct got_reference *ref;
3017 err = got_ref_open(&ref, repo, refname, 0);
3021 err = got_ref_delete(ref, repo);
3026 static const struct got_error *
3027 add_ref(struct got_repository *repo, const char *refname, const char *target)
3029 const struct got_error *err = NULL;
3030 struct got_object_id *id;
3031 struct got_reference *ref = NULL;
3034 * Don't let the user create a reference named '-'.
3035 * While technically a valid reference name, this case is usually
3036 * an unintended typo.
3038 if (refname[0] == '-' && refname[1] == '\0')
3039 return got_error_path(refname, GOT_ERR_BAD_REF_NAME);
3041 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3044 struct got_reference *target_ref;
3046 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3048 err = got_ref_open(&target_ref, repo, target, 0);
3051 err = got_ref_resolve(&id, repo, target_ref);
3052 got_ref_close(target_ref);
3057 err = got_ref_alloc(&ref, refname, id);
3061 err = got_ref_write(ref, repo);
3069 static const struct got_error *
3070 add_symref(struct got_repository *repo, const char *refname, const char *target)
3072 const struct got_error *err = NULL;
3073 struct got_reference *ref = NULL;
3074 struct got_reference *target_ref = NULL;
3077 * Don't let the user create a reference named '-'.
3078 * While technically a valid reference name, this case is usually
3079 * an unintended typo.
3081 if (refname[0] == '-' && refname[1] == '\0')
3082 return got_error_path(refname, GOT_ERR_BAD_REF_NAME);
3084 err = got_ref_open(&target_ref, repo, target, 0);
3088 err = got_ref_alloc_symref(&ref, refname, target_ref);
3092 err = got_ref_write(ref, repo);
3095 got_ref_close(target_ref);
3101 static const struct got_error *
3102 cmd_ref(int argc, char *argv[])
3104 const struct got_error *error = NULL;
3105 struct got_repository *repo = NULL;
3106 struct got_worktree *worktree = NULL;
3107 char *cwd = NULL, *repo_path = NULL;
3108 int ch, do_list = 0, create_symref = 0;
3109 const char *delref = NULL;
3111 /* TODO: Add -s option for adding symbolic references. */
3112 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3118 repo_path = realpath(optarg, NULL);
3119 if (repo_path == NULL)
3120 err(1, "-r option");
3121 got_path_strip_trailing_slashes(repo_path);
3135 if (do_list && delref)
3136 errx(1, "-l and -d options are mutually exclusive\n");
3141 if (do_list || delref) {
3143 errx(1, "-s option cannot be used together with the "
3144 "-l or -d options");
3147 } else if (argc != 2)
3152 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3156 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3157 "sendfd unveil", NULL) == -1)
3161 cwd = getcwd(NULL, 0);
3163 error = got_error_from_errno("getcwd");
3167 if (repo_path == NULL) {
3168 error = got_worktree_open(&worktree, cwd);
3169 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3175 strdup(got_worktree_get_repo_path(worktree));
3176 if (repo_path == NULL)
3177 error = got_error_from_errno("strdup");
3181 repo_path = strdup(cwd);
3182 if (repo_path == NULL) {
3183 error = got_error_from_errno("strdup");
3189 error = got_repo_open(&repo, repo_path, NULL);
3193 error = apply_unveil(got_repo_get_path(repo), do_list,
3194 worktree ? got_worktree_get_root_path(worktree) : NULL);
3199 error = list_refs(repo);
3201 error = delete_ref(repo, delref);
3202 else if (create_symref)
3203 error = add_symref(repo, argv[0], argv[1]);
3205 error = add_ref(repo, argv[0], argv[1]);
3208 got_repo_close(repo);
3210 got_worktree_close(worktree);
3220 "usage: %s branch [-r repository] [-l] | -d name | "
3221 "[name [commit]]\n", getprogname());
3225 static const struct got_error *
3226 list_branch(struct got_repository *repo, struct got_worktree *worktree,
3227 struct got_reference *ref)
3229 const struct got_error *err = NULL;
3230 const char *refname, *marker = " ";
3233 refname = got_ref_get_name(ref);
3234 if (worktree && strcmp(refname,
3235 got_worktree_get_head_ref_name(worktree)) == 0) {
3236 struct got_object_id *id = NULL;
3238 err = got_ref_resolve(&id, repo, ref);
3241 if (got_object_id_cmp(id,
3242 got_worktree_get_base_commit_id(worktree)) == 0)
3249 if (strncmp(refname, "refs/heads/", 11) == 0)
3251 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3254 refstr = got_ref_to_str(ref);
3256 return got_error_from_errno("got_ref_to_str");
3258 printf("%s%s: %s\n", marker, refname, refstr);
3263 static const struct got_error *
3264 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
3266 const char *refname;
3268 if (worktree == NULL)
3269 return got_error(GOT_ERR_NOT_WORKTREE);
3271 refname = got_worktree_get_head_ref_name(worktree);
3273 if (strncmp(refname, "refs/heads/", 11) == 0)
3275 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3278 printf("%s\n", refname);
3283 static const struct got_error *
3284 list_branches(struct got_repository *repo, struct got_worktree *worktree)
3286 static const struct got_error *err = NULL;
3287 struct got_reflist_head refs;
3288 struct got_reflist_entry *re;
3289 struct got_reference *temp_ref = NULL;
3290 int rebase_in_progress, histedit_in_progress;
3292 SIMPLEQ_INIT(&refs);
3295 err = got_worktree_rebase_in_progress(&rebase_in_progress,
3300 err = got_worktree_histedit_in_progress(&histedit_in_progress,
3305 if (rebase_in_progress || histedit_in_progress) {
3306 err = got_ref_open(&temp_ref, repo,
3307 got_worktree_get_head_ref_name(worktree), 0);
3310 list_branch(repo, worktree, temp_ref);
3311 got_ref_close(temp_ref);
3315 err = got_ref_list(&refs, repo, "refs/heads",
3316 got_ref_cmp_by_name, NULL);
3320 SIMPLEQ_FOREACH(re, &refs, entry)
3321 list_branch(repo, worktree, re->ref);
3323 got_ref_list_free(&refs);
3327 static const struct got_error *
3328 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
3329 const char *branch_name)
3331 const struct got_error *err = NULL;
3332 struct got_reference *ref = NULL;
3335 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3336 return got_error_from_errno("asprintf");
3338 err = got_ref_open(&ref, repo, refname, 0);
3343 strcmp(got_worktree_get_head_ref_name(worktree),
3344 got_ref_get_name(ref)) == 0) {
3345 err = got_error_msg(GOT_ERR_SAME_BRANCH,
3346 "will not delete this work tree's current branch");
3350 err = got_ref_delete(ref, repo);
3358 static const struct got_error *
3359 add_branch(struct got_repository *repo, const char *branch_name,
3360 const char *base_branch)
3362 const struct got_error *err = NULL;
3363 struct got_object_id *id = NULL;
3365 struct got_reference *ref = NULL;
3366 char *base_refname = NULL, *refname = NULL;
3369 * Don't let the user create a branch named '-'.
3370 * While technically a valid reference name, this case is usually
3371 * an unintended typo.
3373 if (branch_name[0] == '-' && branch_name[1] == '\0')
3374 return got_error_path(branch_name, GOT_ERR_BAD_REF_NAME);
3376 err = match_object_id(&id, &label, base_branch,
3377 GOT_OBJ_TYPE_COMMIT, 1, repo);
3381 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3382 err = got_error_from_errno("asprintf");
3386 err = got_ref_open(&ref, repo, refname, 0);
3388 err = got_error(GOT_ERR_BRANCH_EXISTS);
3390 } else if (err->code != GOT_ERR_NOT_REF)
3393 err = got_ref_alloc(&ref, refname, id);
3397 err = got_ref_write(ref, repo);
3407 static const struct got_error *
3408 cmd_branch(int argc, char *argv[])
3410 const struct got_error *error = NULL;
3411 struct got_repository *repo = NULL;
3412 struct got_worktree *worktree = NULL;
3413 char *cwd = NULL, *repo_path = NULL;
3414 int ch, do_list = 0, do_show = 0;
3415 const char *delref = NULL;
3417 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
3423 repo_path = realpath(optarg, NULL);
3424 if (repo_path == NULL)
3425 err(1, "-r option");
3426 got_path_strip_trailing_slashes(repo_path);
3437 if (do_list && delref)
3438 errx(1, "-l and -d options are mutually exclusive\n");
3443 if (!do_list && !delref && argc == 0)
3446 if (do_list || delref) {
3449 } else if (!do_show && (argc < 1 || argc > 2))
3453 if (do_list || do_show) {
3454 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3458 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3459 "sendfd unveil", NULL) == -1)
3463 cwd = getcwd(NULL, 0);
3465 error = got_error_from_errno("getcwd");
3469 if (repo_path == NULL) {
3470 error = got_worktree_open(&worktree, cwd);
3471 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3477 strdup(got_worktree_get_repo_path(worktree));
3478 if (repo_path == NULL)
3479 error = got_error_from_errno("strdup");
3483 repo_path = strdup(cwd);
3484 if (repo_path == NULL) {
3485 error = got_error_from_errno("strdup");
3491 error = got_repo_open(&repo, repo_path, NULL);
3495 error = apply_unveil(got_repo_get_path(repo), do_list,
3496 worktree ? got_worktree_get_root_path(worktree) : NULL);
3501 error = show_current_branch(repo, worktree);
3503 error = list_branches(repo, worktree);
3505 error = delete_branch(repo, worktree, delref);
3507 const char *base_branch;
3509 base_branch = worktree ?
3510 got_worktree_get_head_ref_name(worktree) :
3512 if (strncmp(base_branch, "refs/heads/", 11) == 0)
3515 base_branch = argv[1];
3516 error = add_branch(repo, argv[0], base_branch);
3520 got_repo_close(repo);
3522 got_worktree_close(worktree);
3533 "usage: %s tag [-r repository] | -l | "
3534 "[-m message] name [commit]\n", getprogname());
3539 static const struct got_error *
3540 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
3542 const struct got_error *err = NULL;
3543 struct got_reflist_entry *re, *se, *new;
3544 struct got_object_id *re_id, *se_id;
3545 struct got_tag_object *re_tag, *se_tag;
3546 time_t re_time, se_time;
3548 SIMPLEQ_FOREACH(re, tags, entry) {
3549 se = SIMPLEQ_FIRST(sorted);
3551 err = got_reflist_entry_dup(&new, re);
3554 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
3557 err = got_ref_resolve(&re_id, repo, re->ref);
3560 err = got_object_open_as_tag(&re_tag, repo, re_id);
3564 re_time = got_object_tag_get_tagger_time(re_tag);
3565 got_object_tag_close(re_tag);
3569 err = got_ref_resolve(&se_id, repo, re->ref);
3572 err = got_object_open_as_tag(&se_tag, repo, se_id);
3576 se_time = got_object_tag_get_tagger_time(se_tag);
3577 got_object_tag_close(se_tag);
3579 if (se_time > re_time) {
3580 err = got_reflist_entry_dup(&new, re);
3583 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
3586 se = SIMPLEQ_NEXT(se, entry);
3595 static const struct got_error *
3596 cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
3597 struct got_reference *ref2)
3599 const struct got_error *err = NULL;
3600 struct got_repository *repo = arg;
3601 struct got_object_id *id1, *id2 = NULL;
3602 struct got_tag_object *tag1 = NULL, *tag2 = NULL;
3603 time_t time1, time2;
3607 err = got_ref_resolve(&id1, repo, ref1);
3610 err = got_object_open_as_tag(&tag1, repo, id1);
3614 err = got_ref_resolve(&id2, repo, ref2);
3617 err = got_object_open_as_tag(&tag2, repo, id2);
3621 time1 = got_object_tag_get_tagger_time(tag1);
3622 time2 = got_object_tag_get_tagger_time(tag2);
3624 /* Put latest tags first. */
3627 else if (time1 > time2)
3630 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
3635 got_object_tag_close(tag1);
3637 got_object_tag_close(tag2);
3641 static const struct got_error *
3642 list_tags(struct got_repository *repo, struct got_worktree *worktree)
3644 static const struct got_error *err = NULL;
3645 struct got_reflist_head refs;
3646 struct got_reflist_entry *re;
3648 SIMPLEQ_INIT(&refs);
3650 err = got_ref_list(&refs, repo, "refs/tags", cmp_tags, repo);
3654 SIMPLEQ_FOREACH(re, &refs, entry) {
3655 const char *refname;
3656 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
3659 struct got_object_id *id;
3660 struct got_tag_object *tag;
3662 refname = got_ref_get_name(re->ref);
3663 if (strncmp(refname, "refs/tags/", 10) != 0)
3666 refstr = got_ref_to_str(re->ref);
3667 if (refstr == NULL) {
3668 err = got_error_from_errno("got_ref_to_str");
3671 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
3674 err = got_ref_resolve(&id, repo, re->ref);
3677 err = got_object_open_as_tag(&tag, repo, id);
3681 printf("from: %s\n", got_object_tag_get_tagger(tag));
3682 tagger_time = got_object_tag_get_tagger_time(tag);
3683 datestr = get_datestr(&tagger_time, datebuf);
3685 printf("date: %s UTC\n", datestr);
3686 err = got_object_id_str(&id_str,
3687 got_object_tag_get_object_id(tag));
3690 switch (got_object_tag_get_object_type(tag)) {
3691 case GOT_OBJ_TYPE_BLOB:
3692 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB, id_str);
3694 case GOT_OBJ_TYPE_TREE:
3695 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE, id_str);
3697 case GOT_OBJ_TYPE_COMMIT:
3698 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
3700 case GOT_OBJ_TYPE_TAG:
3701 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG, id_str);
3707 tagmsg0 = strdup(got_object_tag_get_message(tag));
3708 got_object_tag_close(tag);
3709 if (tagmsg0 == NULL) {
3710 err = got_error_from_errno("strdup");
3716 line = strsep(&tagmsg, "\n");
3718 printf(" %s\n", line);
3723 got_ref_list_free(&refs);
3727 static const struct got_error *
3728 get_tag_message(char **tagmsg, const char *commit_id_str,
3729 const char *tag_name, const char *repo_path)
3731 const struct got_error *err = NULL;
3732 char *template = NULL, *initial_content = NULL;
3733 char *tagmsg_path = NULL, *editor = NULL;
3736 if (asprintf(&template, "/tmp/got-tagmsg") == -1) {
3737 err = got_error_from_errno("asprintf");
3741 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
3742 commit_id_str, tag_name) == -1) {
3743 err = got_error_from_errno("asprintf");
3747 err = got_opentemp_named_fd(&tagmsg_path, &fd, template);
3751 dprintf(fd, initial_content);
3754 err = get_editor(&editor);
3757 err = edit_logmsg(tagmsg, editor, tagmsg_path, initial_content);
3759 if (err == NULL || err->code == GOT_ERR_COMMIT_MSG_EMPTY) {
3760 unlink(tagmsg_path);
3764 free(initial_content);
3768 /* Editor is done; we can now apply unveil(2) */
3770 err = apply_unveil(repo_path, 0, NULL);
3779 static const struct got_error *
3780 add_tag(struct got_repository *repo, const char *tag_name,
3781 const char *commit_arg, const char *tagmsg_arg)
3783 const struct got_error *err = NULL;
3784 struct got_object_id *commit_id = NULL, *tag_id = NULL;
3785 char *label = NULL, *commit_id_str = NULL;
3786 struct got_reference *ref = NULL;
3787 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
3790 * Don't let the user create a tag named '-'.
3791 * While technically a valid reference name, this case is usually
3792 * an unintended typo.
3794 if (tag_name[0] == '-' && tag_name[1] == '\0')
3795 return got_error_path(tag_name, GOT_ERR_BAD_REF_NAME);
3797 err = get_author(&tagger, repo);
3801 err = match_object_id(&commit_id, &label, commit_arg,
3802 GOT_OBJ_TYPE_COMMIT, 1, repo);
3806 err = got_object_id_str(&commit_id_str, commit_id);
3810 if (strncmp("refs/tags/", tag_name, 10) == 0) {
3811 refname = strdup(tag_name);
3812 if (refname == NULL) {
3813 err = got_error_from_errno("strdup");
3817 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
3818 err = got_error_from_errno("asprintf");
3822 err = got_ref_open(&ref, repo, refname, 0);
3824 err = got_error(GOT_ERR_TAG_EXISTS);
3826 } else if (err->code != GOT_ERR_NOT_REF)
3829 if (tagmsg_arg == NULL) {
3830 err = get_tag_message(&tagmsg, commit_id_str,
3831 tag_name, got_repo_get_path(repo));
3836 err = got_object_tag_create(&tag_id, tag_name, commit_id,
3837 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
3841 err = got_ref_alloc(&ref, refname, tag_id);
3845 err = got_ref_write(ref, repo);
3849 err = got_object_id_str(&tag_id_str, tag_id);
3850 printf("Created tag %s\n", tag_id_str);
3857 free(commit_id_str);
3864 static const struct got_error *
3865 cmd_tag(int argc, char *argv[])
3867 const struct got_error *error = NULL;
3868 struct got_repository *repo = NULL;
3869 struct got_worktree *worktree = NULL;
3870 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
3871 char *gitconfig_path = NULL;
3872 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
3873 int ch, do_list = 0;
3875 while ((ch = getopt(argc, argv, "m:r:l")) != -1) {
3881 repo_path = realpath(optarg, NULL);
3882 if (repo_path == NULL)
3883 err(1, "-r option");
3884 got_path_strip_trailing_slashes(repo_path);
3900 errx(1, "-l and -m options are mutually exclusive\n");
3903 } else if (argc < 1 || argc > 2)
3906 commit_id_arg = argv[1];
3911 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3915 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3916 "sendfd unveil", NULL) == -1)
3920 cwd = getcwd(NULL, 0);
3922 error = got_error_from_errno("getcwd");
3926 if (repo_path == NULL) {
3927 error = got_worktree_open(&worktree, cwd);
3928 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3934 strdup(got_worktree_get_repo_path(worktree));
3935 if (repo_path == NULL)
3936 error = got_error_from_errno("strdup");
3940 repo_path = strdup(cwd);
3941 if (repo_path == NULL) {
3942 error = got_error_from_errno("strdup");
3949 error = got_repo_open(&repo, repo_path, NULL);
3952 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3955 error = list_tags(repo, worktree);
3957 error = get_gitconfig_path(&gitconfig_path);
3960 error = got_repo_open(&repo, repo_path, gitconfig_path);
3965 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3970 if (commit_id_arg == NULL) {
3971 struct got_reference *head_ref;
3972 struct got_object_id *commit_id;
3973 error = got_ref_open(&head_ref, repo,
3974 worktree ? got_worktree_get_head_ref_name(worktree)
3978 error = got_ref_resolve(&commit_id, repo, head_ref);
3979 got_ref_close(head_ref);
3982 error = got_object_id_str(&commit_id_str, commit_id);
3988 error = add_tag(repo, tag_name,
3989 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
3993 got_repo_close(repo);
3995 got_worktree_close(worktree);
3998 free(gitconfig_path);
3999 free(commit_id_str);
4006 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
4010 static const struct got_error *
4011 cmd_add(int argc, char *argv[])
4013 const struct got_error *error = NULL;
4014 struct got_repository *repo = NULL;
4015 struct got_worktree *worktree = NULL;
4017 struct got_pathlist_head paths;
4018 struct got_pathlist_entry *pe;
4023 while ((ch = getopt(argc, argv, "")) != -1) {
4035 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4042 cwd = getcwd(NULL, 0);
4044 error = got_error_from_errno("getcwd");
4048 error = got_worktree_open(&worktree, cwd);
4052 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4057 error = apply_unveil(got_repo_get_path(repo), 1,
4058 got_worktree_get_root_path(worktree));
4062 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4066 error = got_worktree_schedule_add(worktree, &paths, print_status,
4070 got_repo_close(repo);
4072 got_worktree_close(worktree);
4073 TAILQ_FOREACH(pe, &paths, entry)
4074 free((char *)pe->path);
4075 got_pathlist_free(&paths);
4083 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
4087 static const struct got_error *
4088 print_remove_status(void *arg, unsigned char status,
4089 unsigned char staged_status, const char *path,
4090 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4091 struct got_object_id *commit_id)
4093 if (status == GOT_STATUS_NONEXISTENT)
4095 if (status == staged_status && (status == GOT_STATUS_DELETE))
4096 status = GOT_STATUS_NO_CHANGE;
4097 printf("%c%c %s\n", status, staged_status, path);
4101 static const struct got_error *
4102 cmd_remove(int argc, char *argv[])
4104 const struct got_error *error = NULL;
4105 struct got_worktree *worktree = NULL;
4106 struct got_repository *repo = NULL;
4108 struct got_pathlist_head paths;
4109 struct got_pathlist_entry *pe;
4110 int ch, delete_local_mods = 0;
4114 while ((ch = getopt(argc, argv, "f")) != -1) {
4117 delete_local_mods = 1;
4129 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4136 cwd = getcwd(NULL, 0);
4138 error = got_error_from_errno("getcwd");
4141 error = got_worktree_open(&worktree, cwd);
4145 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4150 error = apply_unveil(got_repo_get_path(repo), 1,
4151 got_worktree_get_root_path(worktree));
4155 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4159 error = got_worktree_schedule_delete(worktree, &paths,
4160 delete_local_mods, print_remove_status, NULL, repo);
4165 got_repo_close(repo);
4167 got_worktree_close(worktree);
4168 TAILQ_FOREACH(pe, &paths, entry)
4169 free((char *)pe->path);
4170 got_pathlist_free(&paths);
4178 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4179 "path ...\n", getprogname());
4183 static const struct got_error *
4184 revert_progress(void *arg, unsigned char status, const char *path)
4186 while (path[0] == '/')
4188 printf("%c %s\n", status, path);
4192 struct choose_patch_arg {
4193 FILE *patch_script_file;
4197 static const struct got_error *
4198 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4199 int nchanges, const char *action)
4202 size_t linesize = 0;
4206 case GOT_STATUS_ADD:
4207 printf("A %s\n%s this addition? [y/n] ", path, action);
4209 case GOT_STATUS_DELETE:
4210 printf("D %s\n%s this deletion? [y/n] ", path, action);
4212 case GOT_STATUS_MODIFY:
4213 if (fseek(patch_file, 0L, SEEK_SET) == -1)
4214 return got_error_from_errno("fseek");
4215 printf(GOT_COMMIT_SEP_STR);
4216 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4218 if (ferror(patch_file))
4219 return got_error_from_errno("getline");
4220 printf(GOT_COMMIT_SEP_STR);
4221 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4222 path, n, nchanges, action);
4225 return got_error_path(path, GOT_ERR_FILE_STATUS);
4231 static const struct got_error *
4232 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4233 FILE *patch_file, int n, int nchanges)
4235 const struct got_error *err = NULL;
4237 size_t linesize = 0;
4240 struct choose_patch_arg *a = arg;
4242 *choice = GOT_PATCH_CHOICE_NONE;
4244 if (a->patch_script_file) {
4246 err = show_change(status, path, patch_file, n, nchanges,
4250 linelen = getline(&line, &linesize, a->patch_script_file);
4251 if (linelen == -1) {
4252 if (ferror(a->patch_script_file))
4253 return got_error_from_errno("getline");
4256 nl = strchr(line, '\n');
4259 if (strcmp(line, "y") == 0) {
4260 *choice = GOT_PATCH_CHOICE_YES;
4262 } else if (strcmp(line, "n") == 0) {
4263 *choice = GOT_PATCH_CHOICE_NO;
4265 } else if (strcmp(line, "q") == 0 &&
4266 status == GOT_STATUS_MODIFY) {
4267 *choice = GOT_PATCH_CHOICE_QUIT;
4270 printf("invalid response '%s'\n", line);
4275 while (resp != 'y' && resp != 'n' && resp != 'q') {
4276 err = show_change(status, path, patch_file, n, nchanges,
4283 if (status == GOT_STATUS_MODIFY) {
4284 if (resp != 'y' && resp != 'n' && resp != 'q') {
4285 printf("invalid response '%c'\n", resp);
4288 } else if (resp != 'y' && resp != 'n') {
4289 printf("invalid response '%c'\n", resp);
4295 *choice = GOT_PATCH_CHOICE_YES;
4296 else if (resp == 'n')
4297 *choice = GOT_PATCH_CHOICE_NO;
4298 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4299 *choice = GOT_PATCH_CHOICE_QUIT;
4305 static const struct got_error *
4306 cmd_revert(int argc, char *argv[])
4308 const struct got_error *error = NULL;
4309 struct got_worktree *worktree = NULL;
4310 struct got_repository *repo = NULL;
4311 char *cwd = NULL, *path = NULL;
4312 struct got_pathlist_head paths;
4313 struct got_pathlist_entry *pe;
4314 int ch, can_recurse = 0, pflag = 0;
4315 FILE *patch_script_file = NULL;
4316 const char *patch_script_path = NULL;
4317 struct choose_patch_arg cpa;
4321 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4327 patch_script_path = optarg;
4342 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4343 "unveil", NULL) == -1)
4348 if (patch_script_path && !pflag)
4349 errx(1, "-F option can only be used together with -p option");
4351 cwd = getcwd(NULL, 0);
4353 error = got_error_from_errno("getcwd");
4356 error = got_worktree_open(&worktree, cwd);
4360 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4365 if (patch_script_path) {
4366 patch_script_file = fopen(patch_script_path, "r");
4367 if (patch_script_file == NULL) {
4368 error = got_error_from_errno2("fopen",
4373 error = apply_unveil(got_repo_get_path(repo), 1,
4374 got_worktree_get_root_path(worktree));
4378 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4385 TAILQ_FOREACH(pe, &paths, entry) {
4386 if (asprintf(&ondisk_path, "%s/%s",
4387 got_worktree_get_root_path(worktree),
4389 error = got_error_from_errno("asprintf");
4392 if (lstat(ondisk_path, &sb) == -1) {
4393 if (errno == ENOENT) {
4397 error = got_error_from_errno2("lstat",
4403 if (S_ISDIR(sb.st_mode)) {
4404 error = got_error_msg(GOT_ERR_BAD_PATH,
4405 "reverting directories requires -R option");
4411 cpa.patch_script_file = patch_script_file;
4412 cpa.action = "revert";
4413 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4414 pflag ? choose_patch : NULL, &cpa, repo);
4418 if (patch_script_file && fclose(patch_script_file) == EOF &&
4420 error = got_error_from_errno2("fclose", patch_script_path);
4422 got_repo_close(repo);
4424 got_worktree_close(worktree);
4433 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4438 struct collect_commit_logmsg_arg {
4439 const char *cmdline_log;
4441 const char *worktree_path;
4442 const char *branch_name;
4443 const char *repo_path;
4448 static const struct got_error *
4449 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4452 char *initial_content = NULL;
4453 struct got_pathlist_entry *pe;
4454 const struct got_error *err = NULL;
4455 char *template = NULL;
4456 struct collect_commit_logmsg_arg *a = arg;
4460 /* if a message was specified on the command line, just use it */
4461 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4462 len = strlen(a->cmdline_log) + 1;
4463 *logmsg = malloc(len + 1);
4464 if (*logmsg == NULL)
4465 return got_error_from_errno("malloc");
4466 strlcpy(*logmsg, a->cmdline_log, len);
4470 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
4471 return got_error_from_errno("asprintf");
4473 if (asprintf(&initial_content,
4474 "\n# changes to be committed on branch %s:\n",
4475 a->branch_name) == -1)
4476 return got_error_from_errno("asprintf");
4478 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
4482 dprintf(fd, initial_content);
4484 TAILQ_FOREACH(pe, commitable_paths, entry) {
4485 struct got_commitable *ct = pe->data;
4486 dprintf(fd, "# %c %s\n",
4487 got_commitable_get_status(ct),
4488 got_commitable_get_path(ct));
4492 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
4494 if (err == NULL || err->code == GOT_ERR_COMMIT_MSG_EMPTY) {
4495 unlink(a->logmsg_path);
4496 free(a->logmsg_path);
4497 a->logmsg_path = NULL;
4499 free(initial_content);
4502 /* Editor is done; we can now apply unveil(2) */
4504 err = apply_unveil(a->repo_path, 0, a->worktree_path);
4513 static const struct got_error *
4514 cmd_commit(int argc, char *argv[])
4516 const struct got_error *error = NULL;
4517 struct got_worktree *worktree = NULL;
4518 struct got_repository *repo = NULL;
4519 char *cwd = NULL, *id_str = NULL;
4520 struct got_object_id *id = NULL;
4521 const char *logmsg = NULL;
4522 struct collect_commit_logmsg_arg cl_arg;
4523 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
4524 int ch, rebase_in_progress, histedit_in_progress;
4525 struct got_pathlist_head paths;
4528 cl_arg.logmsg_path = NULL;
4530 while ((ch = getopt(argc, argv, "m:")) != -1) {
4545 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4546 "unveil", NULL) == -1)
4549 cwd = getcwd(NULL, 0);
4551 error = got_error_from_errno("getcwd");
4554 error = got_worktree_open(&worktree, cwd);
4558 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4561 if (rebase_in_progress) {
4562 error = got_error(GOT_ERR_REBASING);
4566 error = got_worktree_histedit_in_progress(&histedit_in_progress,
4571 error = get_gitconfig_path(&gitconfig_path);
4574 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4579 error = get_author(&author, repo);
4584 * unveil(2) traverses exec(2); if an editor is used we have
4585 * to apply unveil after the log message has been written.
4587 if (logmsg == NULL || strlen(logmsg) == 0)
4588 error = get_editor(&editor);
4590 error = apply_unveil(got_repo_get_path(repo), 0,
4591 got_worktree_get_root_path(worktree));
4595 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4599 cl_arg.editor = editor;
4600 cl_arg.cmdline_log = logmsg;
4601 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
4602 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
4603 if (!histedit_in_progress) {
4604 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
4605 error = got_error(GOT_ERR_COMMIT_BRANCH);
4608 cl_arg.branch_name += 11;
4610 cl_arg.repo_path = got_repo_get_path(repo);
4611 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
4612 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
4614 if (cl_arg.logmsg_path)
4615 fprintf(stderr, "%s: log message preserved in %s\n",
4616 getprogname(), cl_arg.logmsg_path);
4620 if (cl_arg.logmsg_path)
4621 unlink(cl_arg.logmsg_path);
4623 error = got_object_id_str(&id_str, id);
4626 printf("Created commit %s\n", id_str);
4628 free(cl_arg.logmsg_path);
4630 got_repo_close(repo);
4632 got_worktree_close(worktree);
4635 free(gitconfig_path);
4642 usage_cherrypick(void)
4644 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
4648 static const struct got_error *
4649 cmd_cherrypick(int argc, char *argv[])
4651 const struct got_error *error = NULL;
4652 struct got_worktree *worktree = NULL;
4653 struct got_repository *repo = NULL;
4654 char *cwd = NULL, *commit_id_str = NULL;
4655 struct got_object_id *commit_id = NULL;
4656 struct got_commit_object *commit = NULL;
4657 struct got_object_qid *pid;
4658 struct got_reference *head_ref = NULL;
4659 int ch, did_something = 0;
4661 while ((ch = getopt(argc, argv, "")) != -1) {
4673 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4674 "unveil", NULL) == -1)
4680 cwd = getcwd(NULL, 0);
4682 error = got_error_from_errno("getcwd");
4685 error = got_worktree_open(&worktree, cwd);
4689 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4694 error = apply_unveil(got_repo_get_path(repo), 0,
4695 got_worktree_get_root_path(worktree));
4699 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4700 GOT_OBJ_TYPE_COMMIT, repo);
4701 if (error != NULL) {
4702 struct got_reference *ref;
4703 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4705 error = got_ref_open(&ref, repo, argv[0], 0);
4708 error = got_ref_resolve(&commit_id, repo, ref);
4713 error = got_object_id_str(&commit_id_str, commit_id);
4717 error = got_ref_open(&head_ref, repo,
4718 got_worktree_get_head_ref_name(worktree), 0);
4722 error = check_same_branch(commit_id, head_ref, NULL, repo);
4724 if (error->code != GOT_ERR_ANCESTRY)
4728 error = got_error(GOT_ERR_SAME_BRANCH);
4732 error = got_object_open_as_commit(&commit, repo, commit_id);
4735 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4736 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
4737 commit_id, repo, update_progress, &did_something, check_cancelled,
4743 printf("Merged commit %s\n", commit_id_str);
4746 got_object_commit_close(commit);
4747 free(commit_id_str);
4749 got_ref_close(head_ref);
4751 got_worktree_close(worktree);
4753 got_repo_close(repo);
4760 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
4764 static const struct got_error *
4765 cmd_backout(int argc, char *argv[])
4767 const struct got_error *error = NULL;
4768 struct got_worktree *worktree = NULL;
4769 struct got_repository *repo = NULL;
4770 char *cwd = NULL, *commit_id_str = NULL;
4771 struct got_object_id *commit_id = NULL;
4772 struct got_commit_object *commit = NULL;
4773 struct got_object_qid *pid;
4774 struct got_reference *head_ref = NULL;
4775 int ch, did_something = 0;
4777 while ((ch = getopt(argc, argv, "")) != -1) {
4789 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4790 "unveil", NULL) == -1)
4796 cwd = getcwd(NULL, 0);
4798 error = got_error_from_errno("getcwd");
4801 error = got_worktree_open(&worktree, cwd);
4805 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4810 error = apply_unveil(got_repo_get_path(repo), 0,
4811 got_worktree_get_root_path(worktree));
4815 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4816 GOT_OBJ_TYPE_COMMIT, repo);
4817 if (error != NULL) {
4818 struct got_reference *ref;
4819 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4821 error = got_ref_open(&ref, repo, argv[0], 0);
4824 error = got_ref_resolve(&commit_id, repo, ref);
4829 error = got_object_id_str(&commit_id_str, commit_id);
4833 error = got_ref_open(&head_ref, repo,
4834 got_worktree_get_head_ref_name(worktree), 0);
4838 error = check_same_branch(commit_id, head_ref, NULL, repo);
4842 error = got_object_open_as_commit(&commit, repo, commit_id);
4845 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4847 error = got_error(GOT_ERR_ROOT_COMMIT);
4851 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
4852 update_progress, &did_something, check_cancelled, NULL);
4857 printf("Backed out commit %s\n", commit_id_str);
4860 got_object_commit_close(commit);
4861 free(commit_id_str);
4863 got_ref_close(head_ref);
4865 got_worktree_close(worktree);
4867 got_repo_close(repo);
4874 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
4880 trim_logmsg(char *logmsg, int limit)
4885 len = strlen(logmsg);
4889 nl = strchr(logmsg, '\n');
4894 static const struct got_error *
4895 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
4897 const struct got_error *err;
4898 char *logmsg0 = NULL;
4901 err = got_object_commit_get_logmsg(&logmsg0, commit);
4906 while (isspace((unsigned char)s[0]))
4909 *logmsg = strdup(s);
4910 if (*logmsg == NULL) {
4911 err = got_error_from_errno("strdup");
4915 trim_logmsg(*logmsg, limit);
4921 static const struct got_error *
4922 show_rebase_progress(struct got_commit_object *commit,
4923 struct got_object_id *old_id, struct got_object_id *new_id)
4925 const struct got_error *err;
4926 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
4928 err = got_object_id_str(&old_id_str, old_id);
4933 err = got_object_id_str(&new_id_str, new_id);
4938 old_id_str[12] = '\0';
4940 new_id_str[12] = '\0';
4942 err = get_short_logmsg(&logmsg, 42, commit);
4946 printf("%s -> %s: %s\n", old_id_str,
4947 new_id_str ? new_id_str : "no-op change", logmsg);
4954 static const struct got_error *
4955 rebase_progress(void *arg, unsigned char status, const char *path)
4957 unsigned char *rebase_status = arg;
4959 while (path[0] == '/')
4961 printf("%c %s\n", status, path);
4963 if (*rebase_status == GOT_STATUS_CONFLICT)
4965 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
4966 *rebase_status = status;
4970 static const struct got_error *
4971 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
4972 struct got_reference *branch, struct got_reference *new_base_branch,
4973 struct got_reference *tmp_branch, struct got_repository *repo)
4975 printf("Switching work tree to %s\n", got_ref_get_name(branch));
4976 return got_worktree_rebase_complete(worktree, fileindex,
4977 new_base_branch, tmp_branch, branch, repo);
4980 static const struct got_error *
4981 rebase_commit(struct got_pathlist_head *merged_paths,
4982 struct got_worktree *worktree, struct got_fileindex *fileindex,
4983 struct got_reference *tmp_branch,
4984 struct got_object_id *commit_id, struct got_repository *repo)
4986 const struct got_error *error;
4987 struct got_commit_object *commit;
4988 struct got_object_id *new_commit_id;
4990 error = got_object_open_as_commit(&commit, repo, commit_id);
4994 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
4995 worktree, fileindex, tmp_branch, commit, commit_id, repo);
4997 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
4999 error = show_rebase_progress(commit, commit_id, NULL);
5001 error = show_rebase_progress(commit, commit_id, new_commit_id);
5002 free(new_commit_id);
5005 got_object_commit_close(commit);
5009 struct check_path_prefix_arg {
5010 const char *path_prefix;
5015 static const struct got_error *
5016 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5017 struct got_blob_object *blob2, struct got_object_id *id1,
5018 struct got_object_id *id2, const char *path1, const char *path2,
5019 struct got_repository *repo)
5021 struct check_path_prefix_arg *a = arg;
5023 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5024 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5025 return got_error(a->errcode);
5030 static const struct got_error *
5031 check_path_prefix(struct got_object_id *parent_id,
5032 struct got_object_id *commit_id, const char *path_prefix,
5033 int errcode, struct got_repository *repo)
5035 const struct got_error *err;
5036 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
5037 struct got_commit_object *commit = NULL, *parent_commit = NULL;
5038 struct check_path_prefix_arg cpp_arg;
5040 if (got_path_is_root_dir(path_prefix))
5043 err = got_object_open_as_commit(&commit, repo, commit_id);
5047 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
5051 err = got_object_open_as_tree(&tree1, repo,
5052 got_object_commit_get_tree_id(parent_commit));
5056 err = got_object_open_as_tree(&tree2, repo,
5057 got_object_commit_get_tree_id(commit));
5061 cpp_arg.path_prefix = path_prefix;
5062 while (cpp_arg.path_prefix[0] == '/')
5063 cpp_arg.path_prefix++;
5064 cpp_arg.len = strlen(cpp_arg.path_prefix);
5065 cpp_arg.errcode = errcode;
5066 err = got_diff_tree(tree1, tree2, "", "", repo,
5067 check_path_prefix_in_diff, &cpp_arg, 0);
5070 got_object_tree_close(tree1);
5072 got_object_tree_close(tree2);
5074 got_object_commit_close(commit);
5076 got_object_commit_close(parent_commit);
5080 static const struct got_error *
5081 collect_commits(struct got_object_id_queue *commits,
5082 struct got_object_id *initial_commit_id,
5083 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
5084 const char *path_prefix, int path_prefix_errcode,
5085 struct got_repository *repo)
5087 const struct got_error *err = NULL;
5088 struct got_commit_graph *graph = NULL;
5089 struct got_object_id *parent_id = NULL;
5090 struct got_object_qid *qid;
5091 struct got_object_id *commit_id = initial_commit_id;
5093 err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
5097 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
5098 check_cancelled, NULL);
5101 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
5102 err = got_commit_graph_iter_next(&parent_id, graph);
5104 if (err->code == GOT_ERR_ITER_COMPLETED) {
5105 err = got_error_msg(GOT_ERR_ANCESTRY,
5106 "ran out of commits to rebase before "
5107 "youngest common ancestor commit has "
5110 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
5112 err = got_commit_graph_fetch_commits(graph, 1, repo,
5113 check_cancelled, NULL);
5117 err = check_path_prefix(parent_id, commit_id,
5118 path_prefix, path_prefix_errcode, repo);
5122 err = got_object_qid_alloc(&qid, commit_id);
5125 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
5126 commit_id = parent_id;
5130 got_commit_graph_close(graph);
5134 static const struct got_error *
5135 cmd_rebase(int argc, char *argv[])
5137 const struct got_error *error = NULL;
5138 struct got_worktree *worktree = NULL;
5139 struct got_repository *repo = NULL;
5140 struct got_fileindex *fileindex = NULL;
5142 struct got_reference *branch = NULL;
5143 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
5144 struct got_object_id *commit_id = NULL, *parent_id = NULL;
5145 struct got_object_id *resume_commit_id = NULL;
5146 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
5147 struct got_commit_object *commit = NULL;
5148 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
5149 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5150 struct got_object_id_queue commits;
5151 struct got_pathlist_head merged_paths;
5152 const struct got_object_id_queue *parent_ids;
5153 struct got_object_qid *qid, *pid;
5155 SIMPLEQ_INIT(&commits);
5156 TAILQ_INIT(&merged_paths);
5158 while ((ch = getopt(argc, argv, "ac")) != -1) {
5164 continue_rebase = 1;
5176 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5177 "unveil", NULL) == -1)
5180 if (abort_rebase && continue_rebase)
5182 else if (abort_rebase || continue_rebase) {
5185 } else if (argc != 1)
5188 cwd = getcwd(NULL, 0);
5190 error = got_error_from_errno("getcwd");
5193 error = got_worktree_open(&worktree, cwd);
5197 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5202 error = apply_unveil(got_repo_get_path(repo), 0,
5203 got_worktree_get_root_path(worktree));
5207 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5213 if (!rebase_in_progress) {
5214 error = got_error(GOT_ERR_NOT_REBASING);
5217 error = got_worktree_rebase_continue(&resume_commit_id,
5218 &new_base_branch, &tmp_branch, &branch, &fileindex,
5222 printf("Switching work tree to %s\n",
5223 got_ref_get_symref_target(new_base_branch));
5224 error = got_worktree_rebase_abort(worktree, fileindex, repo,
5225 new_base_branch, update_progress, &did_something);
5228 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5229 goto done; /* nothing else to do */
5232 if (continue_rebase) {
5233 if (!rebase_in_progress) {
5234 error = got_error(GOT_ERR_NOT_REBASING);
5237 error = got_worktree_rebase_continue(&resume_commit_id,
5238 &new_base_branch, &tmp_branch, &branch, &fileindex,
5243 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5244 resume_commit_id, repo);
5248 yca_id = got_object_id_dup(resume_commit_id);
5249 if (yca_id == NULL) {
5250 error = got_error_from_errno("got_object_id_dup");
5254 error = got_ref_open(&branch, repo, argv[0], 0);
5259 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5263 if (!continue_rebase) {
5264 struct got_object_id *base_commit_id;
5266 base_commit_id = got_worktree_get_base_commit_id(worktree);
5267 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5268 base_commit_id, branch_head_commit_id, repo,
5269 check_cancelled, NULL);
5272 if (yca_id == NULL) {
5273 error = got_error_msg(GOT_ERR_ANCESTRY,
5274 "specified branch shares no common ancestry "
5275 "with work tree's branch");
5279 error = check_same_branch(base_commit_id, branch, yca_id, repo);
5281 if (error->code != GOT_ERR_ANCESTRY)
5285 error = got_error_msg(GOT_ERR_SAME_BRANCH,
5286 "specified branch resolves to a commit which "
5287 "is already contained in work tree's branch");
5290 error = got_worktree_rebase_prepare(&new_base_branch,
5291 &tmp_branch, &fileindex, worktree, branch, repo);
5296 commit_id = branch_head_commit_id;
5297 error = got_object_open_as_commit(&commit, repo, commit_id);
5301 parent_ids = got_object_commit_get_parent_ids(commit);
5302 pid = SIMPLEQ_FIRST(parent_ids);
5304 if (!continue_rebase) {
5306 error = got_worktree_rebase_abort(worktree, fileindex,
5307 repo, new_base_branch, update_progress,
5311 printf("Rebase of %s aborted\n",
5312 got_ref_get_name(branch));
5314 error = got_error(GOT_ERR_EMPTY_REBASE);
5317 error = collect_commits(&commits, commit_id, pid->id,
5318 yca_id, got_worktree_get_path_prefix(worktree),
5319 GOT_ERR_REBASE_PATH, repo);
5320 got_object_commit_close(commit);
5325 if (SIMPLEQ_EMPTY(&commits)) {
5326 if (continue_rebase)
5327 error = rebase_complete(worktree, fileindex,
5328 branch, new_base_branch, tmp_branch, repo);
5330 error = got_error(GOT_ERR_EMPTY_REBASE);
5335 SIMPLEQ_FOREACH(qid, &commits, entry) {
5336 commit_id = qid->id;
5337 parent_id = pid ? pid->id : yca_id;
5340 error = got_worktree_rebase_merge_files(&merged_paths,
5341 worktree, fileindex, parent_id, commit_id, repo,
5342 rebase_progress, &rebase_status, check_cancelled, NULL);
5346 if (rebase_status == GOT_STATUS_CONFLICT) {
5347 got_worktree_rebase_pathlist_free(&merged_paths);
5351 error = rebase_commit(&merged_paths, worktree, fileindex,
5352 tmp_branch, commit_id, repo);
5353 got_worktree_rebase_pathlist_free(&merged_paths);
5358 if (rebase_status == GOT_STATUS_CONFLICT) {
5359 error = got_worktree_rebase_postpone(worktree, fileindex);
5362 error = got_error_msg(GOT_ERR_CONFLICTS,
5363 "conflicts must be resolved before rebasing can continue");
5365 error = rebase_complete(worktree, fileindex, branch,
5366 new_base_branch, tmp_branch, repo);
5368 got_object_id_queue_free(&commits);
5369 free(branch_head_commit_id);
5370 free(resume_commit_id);
5373 got_object_commit_close(commit);
5375 got_ref_close(branch);
5376 if (new_base_branch)
5377 got_ref_close(new_base_branch);
5379 got_ref_close(tmp_branch);
5381 got_worktree_close(worktree);
5383 got_repo_close(repo);
5388 usage_histedit(void)
5390 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script]\n",
5395 #define GOT_HISTEDIT_PICK 'p'
5396 #define GOT_HISTEDIT_EDIT 'e'
5397 #define GOT_HISTEDIT_FOLD 'f'
5398 #define GOT_HISTEDIT_DROP 'd'
5399 #define GOT_HISTEDIT_MESG 'm'
5401 static struct got_histedit_cmd {
5405 } got_histedit_cmds[] = {
5406 { GOT_HISTEDIT_PICK, "pick", "use commit" },
5407 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5408 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
5409 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5410 { GOT_HISTEDIT_MESG, "mesg",
5411 "single-line log message for commit above (open editor if empty)" },
5414 struct got_histedit_list_entry {
5415 TAILQ_ENTRY(got_histedit_list_entry) entry;
5416 struct got_object_id *commit_id;
5417 const struct got_histedit_cmd *cmd;
5420 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5422 static const struct got_error *
5423 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5424 FILE *f, struct got_repository *repo)
5426 const struct got_error *err = NULL;
5427 char *logmsg = NULL, *id_str = NULL;
5428 struct got_commit_object *commit = NULL;
5431 err = got_object_open_as_commit(&commit, repo, commit_id);
5435 err = get_short_logmsg(&logmsg, 34, commit);
5439 err = got_object_id_str(&id_str, commit_id);
5443 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
5445 err = got_ferror(f, GOT_ERR_IO);
5448 got_object_commit_close(commit);
5454 static const struct got_error *
5455 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
5456 struct got_repository *repo)
5458 const struct got_error *err = NULL;
5459 struct got_object_qid *qid;
5461 if (SIMPLEQ_EMPTY(commits))
5462 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5464 SIMPLEQ_FOREACH(qid, commits, entry) {
5465 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
5474 static const struct got_error *
5475 write_cmd_list(FILE *f)
5477 const struct got_error *err = NULL;
5480 n = fprintf(f, "# Available histedit commands:\n");
5482 return got_ferror(f, GOT_ERR_IO);
5484 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5485 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
5486 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
5489 err = got_ferror(f, GOT_ERR_IO);
5493 n = fprintf(f, "# Commits will be processed in order from top to "
5494 "bottom of this file.\n");
5496 return got_ferror(f, GOT_ERR_IO);
5500 static const struct got_error *
5501 histedit_syntax_error(int lineno)
5503 static char msg[42];
5506 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
5508 if (ret == -1 || ret >= sizeof(msg))
5509 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
5511 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
5514 static const struct got_error *
5515 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
5516 char *logmsg, struct got_repository *repo)
5518 const struct got_error *err;
5519 struct got_commit_object *folded_commit = NULL;
5520 char *id_str, *folded_logmsg = NULL;
5522 err = got_object_id_str(&id_str, hle->commit_id);
5526 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
5530 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
5533 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
5534 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
5535 folded_logmsg) == -1) {
5536 err = got_error_from_errno("asprintf");
5541 got_object_commit_close(folded_commit);
5543 free(folded_logmsg);
5547 static struct got_histedit_list_entry *
5548 get_folded_commits(struct got_histedit_list_entry *hle)
5550 struct got_histedit_list_entry *prev, *folded = NULL;
5552 prev = TAILQ_PREV(hle, got_histedit_list, entry);
5553 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
5554 prev->cmd->code == GOT_HISTEDIT_DROP)) {
5555 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
5557 prev = TAILQ_PREV(prev, got_histedit_list, entry);
5563 static const struct got_error *
5564 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
5565 struct got_repository *repo)
5567 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
5568 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
5569 const struct got_error *err = NULL;
5570 struct got_commit_object *commit = NULL;
5572 struct got_histedit_list_entry *folded = NULL;
5574 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5578 folded = get_folded_commits(hle);
5580 while (folded != hle) {
5581 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
5582 folded = TAILQ_NEXT(folded, entry);
5585 err = append_folded_commit_msg(&new_msg, folded,
5591 folded = TAILQ_NEXT(folded, entry);
5595 err = got_object_id_str(&id_str, hle->commit_id);
5598 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
5601 if (asprintf(&new_msg,
5602 "%s\n# original log message of commit %s: %s",
5603 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
5604 err = got_error_from_errno("asprintf");
5610 err = got_object_id_str(&id_str, hle->commit_id);
5614 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
5618 dprintf(fd, logmsg);
5621 err = get_editor(&editor);
5625 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
5627 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
5629 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
5632 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
5633 err = got_error_from_errno2("unlink", logmsg_path);
5639 got_object_commit_close(commit);
5643 static const struct got_error *
5644 histedit_parse_list(struct got_histedit_list *histedit_cmds,
5645 FILE *f, struct got_repository *repo)
5647 const struct got_error *err = NULL;
5648 char *line = NULL, *p, *end;
5652 const struct got_histedit_cmd *cmd;
5653 struct got_object_id *commit_id = NULL;
5654 struct got_histedit_list_entry *hle = NULL;
5657 len = getline(&line, &size, f);
5659 const struct got_error *getline_err;
5662 getline_err = got_error_from_errno("getline");
5663 err = got_ferror(f, getline_err->code);
5668 while (isspace((unsigned char)p[0]))
5670 if (p[0] == '#' || p[0] == '\0') {
5676 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5677 cmd = &got_histedit_cmds[i];
5678 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
5679 isspace((unsigned char)p[strlen(cmd->name)])) {
5680 p += strlen(cmd->name);
5683 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
5688 if (i == nitems(got_histedit_cmds)) {
5689 err = histedit_syntax_error(lineno);
5692 while (isspace((unsigned char)p[0]))
5694 if (cmd->code == GOT_HISTEDIT_MESG) {
5695 if (hle == NULL || hle->logmsg != NULL) {
5696 err = got_error(GOT_ERR_HISTEDIT_CMD);
5700 err = histedit_edit_logmsg(hle, repo);
5704 hle->logmsg = strdup(p);
5705 if (hle->logmsg == NULL) {
5706 err = got_error_from_errno("strdup");
5715 while (end[0] && !isspace((unsigned char)end[0]))
5719 err = got_object_resolve_id_str(&commit_id, repo, p);
5721 /* override error code */
5722 err = histedit_syntax_error(lineno);
5726 hle = malloc(sizeof(*hle));
5728 err = got_error_from_errno("malloc");
5732 hle->commit_id = commit_id;
5737 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
5745 static const struct got_error *
5746 histedit_check_script(struct got_histedit_list *histedit_cmds,
5747 struct got_object_id_queue *commits, struct got_repository *repo)
5749 const struct got_error *err = NULL;
5750 struct got_object_qid *qid;
5751 struct got_histedit_list_entry *hle;
5752 static char msg[80];
5755 if (TAILQ_EMPTY(histedit_cmds))
5756 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
5757 "histedit script contains no commands");
5758 if (SIMPLEQ_EMPTY(commits))
5759 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5761 SIMPLEQ_FOREACH(qid, commits, entry) {
5762 TAILQ_FOREACH(hle, histedit_cmds, entry) {
5763 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
5767 err = got_object_id_str(&id_str, qid->id);
5770 snprintf(msg, sizeof(msg),
5771 "commit %s missing from histedit script", id_str);
5773 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
5777 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
5778 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
5779 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
5780 "last commit in histedit script cannot be folded");
5785 static const struct got_error *
5786 histedit_run_editor(struct got_histedit_list *histedit_cmds,
5787 const char *path, struct got_object_id_queue *commits,
5788 struct got_repository *repo)
5790 const struct got_error *err = NULL;
5794 err = get_editor(&editor);
5798 if (spawn_editor(editor, path) == -1) {
5799 err = got_error_from_errno("failed spawning editor");
5803 f = fopen(path, "r");
5805 err = got_error_from_errno("fopen");
5808 err = histedit_parse_list(histedit_cmds, f, repo);
5812 err = histedit_check_script(histedit_cmds, commits, repo);
5814 if (f && fclose(f) != 0 && err == NULL)
5815 err = got_error_from_errno("fclose");
5820 static const struct got_error *
5821 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
5822 struct got_object_id_queue *, const char *, struct got_repository *);
5824 static const struct got_error *
5825 histedit_edit_script(struct got_histedit_list *histedit_cmds,
5826 struct got_object_id_queue *commits, struct got_repository *repo)
5828 const struct got_error *err;
5832 err = got_opentemp_named(&path, &f, "got-histedit");
5836 err = write_cmd_list(f);
5840 err = histedit_write_commit_list(commits, f, repo);
5844 if (fclose(f) != 0) {
5845 err = got_error_from_errno("fclose");
5850 err = histedit_run_editor(histedit_cmds, path, commits, repo);
5852 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5853 err->code != GOT_ERR_HISTEDIT_CMD)
5855 err = histedit_edit_list_retry(histedit_cmds, err,
5856 commits, path, repo);
5859 if (f && fclose(f) != 0 && err == NULL)
5860 err = got_error_from_errno("fclose");
5861 if (path && unlink(path) != 0 && err == NULL)
5862 err = got_error_from_errno2("unlink", path);
5867 static const struct got_error *
5868 histedit_save_list(struct got_histedit_list *histedit_cmds,
5869 struct got_worktree *worktree, struct got_repository *repo)
5871 const struct got_error *err = NULL;
5874 struct got_histedit_list_entry *hle;
5875 struct got_commit_object *commit = NULL;
5877 err = got_worktree_get_histedit_script_path(&path, worktree);
5881 f = fopen(path, "w");
5883 err = got_error_from_errno2("fopen", path);
5886 TAILQ_FOREACH(hle, histedit_cmds, entry) {
5887 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
5893 int n = fprintf(f, "%c %s\n",
5894 GOT_HISTEDIT_MESG, hle->logmsg);
5896 err = got_ferror(f, GOT_ERR_IO);
5902 if (f && fclose(f) != 0 && err == NULL)
5903 err = got_error_from_errno("fclose");
5906 got_object_commit_close(commit);
5911 histedit_free_list(struct got_histedit_list *histedit_cmds)
5913 struct got_histedit_list_entry *hle;
5915 while ((hle = TAILQ_FIRST(histedit_cmds))) {
5916 TAILQ_REMOVE(histedit_cmds, hle, entry);
5921 static const struct got_error *
5922 histedit_load_list(struct got_histedit_list *histedit_cmds,
5923 const char *path, struct got_repository *repo)
5925 const struct got_error *err = NULL;
5928 f = fopen(path, "r");
5930 err = got_error_from_errno2("fopen", path);
5934 err = histedit_parse_list(histedit_cmds, f, repo);
5936 if (f && fclose(f) != 0 && err == NULL)
5937 err = got_error_from_errno("fclose");
5941 static const struct got_error *
5942 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
5943 const struct got_error *edit_err, struct got_object_id_queue *commits,
5944 const char *path, struct got_repository *repo)
5946 const struct got_error *err = NULL, *prev_err = edit_err;
5949 while (resp != 'c' && resp != 'r' && resp != 'a') {
5950 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
5951 "or (a)bort: ", getprogname(), prev_err->msg);
5956 histedit_free_list(histedit_cmds);
5957 err = histedit_run_editor(histedit_cmds, path, commits,
5960 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5961 err->code != GOT_ERR_HISTEDIT_CMD)
5968 } else if (resp == 'r') {
5969 histedit_free_list(histedit_cmds);
5970 err = histedit_edit_script(histedit_cmds,
5973 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5974 err->code != GOT_ERR_HISTEDIT_CMD)
5981 } else if (resp == 'a') {
5982 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
5985 printf("invalid response '%c'\n", resp);
5991 static const struct got_error *
5992 histedit_complete(struct got_worktree *worktree,
5993 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5994 struct got_reference *branch, struct got_repository *repo)
5996 printf("Switching work tree to %s\n",
5997 got_ref_get_symref_target(branch));
5998 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
6002 static const struct got_error *
6003 show_histedit_progress(struct got_commit_object *commit,
6004 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
6006 const struct got_error *err;
6007 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6009 err = got_object_id_str(&old_id_str, hle->commit_id);
6014 err = got_object_id_str(&new_id_str, new_id);
6019 old_id_str[12] = '\0';
6021 new_id_str[12] = '\0';
6024 logmsg = strdup(hle->logmsg);
6025 if (logmsg == NULL) {
6026 err = got_error_from_errno("strdup");
6029 trim_logmsg(logmsg, 42);
6031 err = get_short_logmsg(&logmsg, 42, commit);
6036 switch (hle->cmd->code) {
6037 case GOT_HISTEDIT_PICK:
6038 case GOT_HISTEDIT_EDIT:
6039 printf("%s -> %s: %s\n", old_id_str,
6040 new_id_str ? new_id_str : "no-op change", logmsg);
6042 case GOT_HISTEDIT_DROP:
6043 case GOT_HISTEDIT_FOLD:
6044 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
6057 static const struct got_error *
6058 histedit_commit(struct got_pathlist_head *merged_paths,
6059 struct got_worktree *worktree, struct got_fileindex *fileindex,
6060 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
6061 struct got_repository *repo)
6063 const struct got_error *err;
6064 struct got_commit_object *commit;
6065 struct got_object_id *new_commit_id;
6067 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
6068 && hle->logmsg == NULL) {
6069 err = histedit_edit_logmsg(hle, repo);
6074 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6078 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
6079 worktree, fileindex, tmp_branch, commit, hle->commit_id,
6082 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
6084 err = show_histedit_progress(commit, hle, NULL);
6086 err = show_histedit_progress(commit, hle, new_commit_id);
6087 free(new_commit_id);
6090 got_object_commit_close(commit);
6094 static const struct got_error *
6095 histedit_skip_commit(struct got_histedit_list_entry *hle,
6096 struct got_worktree *worktree, struct got_repository *repo)
6098 const struct got_error *error;
6099 struct got_commit_object *commit;
6101 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
6106 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
6110 error = show_histedit_progress(commit, hle, NULL);
6111 got_object_commit_close(commit);
6115 static const struct got_error *
6116 cmd_histedit(int argc, char *argv[])
6118 const struct got_error *error = NULL;
6119 struct got_worktree *worktree = NULL;
6120 struct got_fileindex *fileindex = NULL;
6121 struct got_repository *repo = NULL;
6123 struct got_reference *branch = NULL;
6124 struct got_reference *tmp_branch = NULL;
6125 struct got_object_id *resume_commit_id = NULL;
6126 struct got_object_id *base_commit_id = NULL;
6127 struct got_object_id *head_commit_id = NULL;
6128 struct got_commit_object *commit = NULL;
6129 int ch, rebase_in_progress = 0, did_something;
6130 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
6131 const char *edit_script_path = NULL;
6132 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6133 struct got_object_id_queue commits;
6134 struct got_pathlist_head merged_paths;
6135 const struct got_object_id_queue *parent_ids;
6136 struct got_object_qid *pid;
6137 struct got_histedit_list histedit_cmds;
6138 struct got_histedit_list_entry *hle;
6140 SIMPLEQ_INIT(&commits);
6141 TAILQ_INIT(&histedit_cmds);
6142 TAILQ_INIT(&merged_paths);
6144 while ((ch = getopt(argc, argv, "acF:")) != -1) {
6153 edit_script_path = optarg;
6165 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6166 "unveil", NULL) == -1)
6169 if (abort_edit && continue_edit)
6175 * This command cannot apply unveil(2) in all cases because the
6176 * user may choose to run an editor to edit the histedit script
6177 * and to edit individual commit log messages.
6178 * unveil(2) traverses exec(2); if an editor is used we have to
6179 * apply unveil after edit script and log messages have been written.
6180 * XXX TODO: Make use of unveil(2) where possible.
6183 cwd = getcwd(NULL, 0);
6185 error = got_error_from_errno("getcwd");
6188 error = got_worktree_open(&worktree, cwd);
6192 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6197 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6200 if (rebase_in_progress) {
6201 error = got_error(GOT_ERR_REBASING);
6205 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6209 if (edit_in_progress && abort_edit) {
6210 error = got_worktree_histedit_continue(&resume_commit_id,
6211 &tmp_branch, &branch, &base_commit_id, &fileindex,
6215 printf("Switching work tree to %s\n",
6216 got_ref_get_symref_target(branch));
6217 error = got_worktree_histedit_abort(worktree, fileindex, repo,
6218 branch, base_commit_id, update_progress, &did_something);
6221 printf("Histedit of %s aborted\n",
6222 got_ref_get_symref_target(branch));
6223 goto done; /* nothing else to do */
6224 } else if (abort_edit) {
6225 error = got_error(GOT_ERR_NOT_HISTEDIT);
6229 if (continue_edit) {
6232 if (!edit_in_progress) {
6233 error = got_error(GOT_ERR_NOT_HISTEDIT);
6237 error = got_worktree_get_histedit_script_path(&path, worktree);
6241 error = histedit_load_list(&histedit_cmds, path, repo);
6246 error = got_worktree_histedit_continue(&resume_commit_id,
6247 &tmp_branch, &branch, &base_commit_id, &fileindex,
6252 error = got_ref_resolve(&head_commit_id, repo, branch);
6256 error = got_object_open_as_commit(&commit, repo,
6260 parent_ids = got_object_commit_get_parent_ids(commit);
6261 pid = SIMPLEQ_FIRST(parent_ids);
6263 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6266 error = collect_commits(&commits, head_commit_id, pid->id,
6267 base_commit_id, got_worktree_get_path_prefix(worktree),
6268 GOT_ERR_HISTEDIT_PATH, repo);
6269 got_object_commit_close(commit);
6274 if (edit_in_progress) {
6275 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6279 error = got_ref_open(&branch, repo,
6280 got_worktree_get_head_ref_name(worktree), 0);
6284 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
6285 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
6286 "will not edit commit history of a branch outside "
6287 "the \"refs/heads/\" reference namespace");
6291 error = got_ref_resolve(&head_commit_id, repo, branch);
6292 got_ref_close(branch);
6297 error = got_object_open_as_commit(&commit, repo,
6301 parent_ids = got_object_commit_get_parent_ids(commit);
6302 pid = SIMPLEQ_FIRST(parent_ids);
6304 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6307 error = collect_commits(&commits, head_commit_id, pid->id,
6308 got_worktree_get_base_commit_id(worktree),
6309 got_worktree_get_path_prefix(worktree),
6310 GOT_ERR_HISTEDIT_PATH, repo);
6311 got_object_commit_close(commit);
6316 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
6317 &base_commit_id, &fileindex, worktree, repo);
6321 if (edit_script_path) {
6322 error = histedit_load_list(&histedit_cmds,
6323 edit_script_path, repo);
6325 got_worktree_histedit_abort(worktree, fileindex,
6326 repo, branch, base_commit_id,
6327 update_progress, &did_something);
6331 error = histedit_edit_script(&histedit_cmds, &commits,
6334 got_worktree_histedit_abort(worktree, fileindex,
6335 repo, branch, base_commit_id,
6336 update_progress, &did_something);
6342 error = histedit_save_list(&histedit_cmds, worktree,
6345 got_worktree_histedit_abort(worktree, fileindex,
6346 repo, branch, base_commit_id,
6347 update_progress, &did_something);
6353 error = histedit_check_script(&histedit_cmds, &commits, repo);
6357 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
6358 if (resume_commit_id) {
6359 if (got_object_id_cmp(hle->commit_id,
6360 resume_commit_id) != 0)
6363 resume_commit_id = NULL;
6364 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
6365 hle->cmd->code == GOT_HISTEDIT_FOLD) {
6366 error = histedit_skip_commit(hle, worktree,
6369 error = histedit_commit(NULL, worktree,
6370 fileindex, tmp_branch, hle, repo);
6377 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
6378 error = histedit_skip_commit(hle, worktree, repo);
6384 error = got_object_open_as_commit(&commit, repo,
6388 parent_ids = got_object_commit_get_parent_ids(commit);
6389 pid = SIMPLEQ_FIRST(parent_ids);
6391 error = got_worktree_histedit_merge_files(&merged_paths,
6392 worktree, fileindex, pid->id, hle->commit_id, repo,
6393 rebase_progress, &rebase_status, check_cancelled, NULL);
6396 got_object_commit_close(commit);
6399 if (rebase_status == GOT_STATUS_CONFLICT) {
6400 got_worktree_rebase_pathlist_free(&merged_paths);
6404 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
6406 error = got_object_id_str(&id_str, hle->commit_id);
6409 printf("Stopping histedit for amending commit %s\n",
6412 got_worktree_rebase_pathlist_free(&merged_paths);
6413 error = got_worktree_histedit_postpone(worktree,
6418 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
6419 error = histedit_skip_commit(hle, worktree, repo);
6425 error = histedit_commit(&merged_paths, worktree, fileindex,
6426 tmp_branch, hle, repo);
6427 got_worktree_rebase_pathlist_free(&merged_paths);
6432 if (rebase_status == GOT_STATUS_CONFLICT) {
6433 error = got_worktree_histedit_postpone(worktree, fileindex);
6436 error = got_error_msg(GOT_ERR_CONFLICTS,
6437 "conflicts must be resolved before rebasing can continue");
6439 error = histedit_complete(worktree, fileindex, tmp_branch,
6442 got_object_id_queue_free(&commits);
6443 histedit_free_list(&histedit_cmds);
6444 free(head_commit_id);
6445 free(base_commit_id);
6446 free(resume_commit_id);
6448 got_object_commit_close(commit);
6450 got_ref_close(branch);
6452 got_ref_close(tmp_branch);
6454 got_worktree_close(worktree);
6456 got_repo_close(repo);
6463 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
6464 "[file-path ...]\n",
6469 static const struct got_error *
6470 print_stage(void *arg, unsigned char status, unsigned char staged_status,
6471 const char *path, struct got_object_id *blob_id,
6472 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
6474 const struct got_error *err = NULL;
6475 char *id_str = NULL;
6477 if (staged_status != GOT_STATUS_ADD &&
6478 staged_status != GOT_STATUS_MODIFY &&
6479 staged_status != GOT_STATUS_DELETE)
6482 if (staged_status == GOT_STATUS_ADD ||
6483 staged_status == GOT_STATUS_MODIFY)
6484 err = got_object_id_str(&id_str, staged_blob_id);
6486 err = got_object_id_str(&id_str, blob_id);
6490 printf("%s %c %s\n", id_str, staged_status, path);
6495 static const struct got_error *
6496 cmd_stage(int argc, char *argv[])
6498 const struct got_error *error = NULL;
6499 struct got_repository *repo = NULL;
6500 struct got_worktree *worktree = NULL;
6502 struct got_pathlist_head paths;
6503 struct got_pathlist_entry *pe;
6504 int ch, list_stage = 0, pflag = 0;
6505 FILE *patch_script_file = NULL;
6506 const char *patch_script_path = NULL;
6507 struct choose_patch_arg cpa;
6511 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
6520 patch_script_path = optarg;
6532 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6533 "unveil", NULL) == -1)
6536 if (list_stage && (pflag || patch_script_path))
6537 errx(1, "-l option cannot be used with other options");
6538 if (patch_script_path && !pflag)
6539 errx(1, "-F option can only be used together with -p option");
6541 cwd = getcwd(NULL, 0);
6543 error = got_error_from_errno("getcwd");
6547 error = got_worktree_open(&worktree, cwd);
6551 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6556 if (patch_script_path) {
6557 patch_script_file = fopen(patch_script_path, "r");
6558 if (patch_script_file == NULL) {
6559 error = got_error_from_errno2("fopen",
6564 error = apply_unveil(got_repo_get_path(repo), 0,
6565 got_worktree_get_root_path(worktree));
6569 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6574 error = got_worktree_status(worktree, &paths, repo,
6575 print_stage, NULL, check_cancelled, NULL);
6577 cpa.patch_script_file = patch_script_file;
6578 cpa.action = "stage";
6579 error = got_worktree_stage(worktree, &paths,
6580 pflag ? NULL : print_status, NULL,
6581 pflag ? choose_patch : NULL, &cpa, repo);
6584 if (patch_script_file && fclose(patch_script_file) == EOF &&
6586 error = got_error_from_errno2("fclose", patch_script_path);
6588 got_repo_close(repo);
6590 got_worktree_close(worktree);
6591 TAILQ_FOREACH(pe, &paths, entry)
6592 free((char *)pe->path);
6593 got_pathlist_free(&paths);
6601 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
6602 "[file-path ...]\n",
6608 static const struct got_error *
6609 cmd_unstage(int argc, char *argv[])
6611 const struct got_error *error = NULL;
6612 struct got_repository *repo = NULL;
6613 struct got_worktree *worktree = NULL;
6615 struct got_pathlist_head paths;
6616 struct got_pathlist_entry *pe;
6617 int ch, did_something = 0, pflag = 0;
6618 FILE *patch_script_file = NULL;
6619 const char *patch_script_path = NULL;
6620 struct choose_patch_arg cpa;
6624 while ((ch = getopt(argc, argv, "pF:")) != -1) {
6630 patch_script_path = optarg;
6642 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6643 "unveil", NULL) == -1)
6646 if (patch_script_path && !pflag)
6647 errx(1, "-F option can only be used together with -p option");
6649 cwd = getcwd(NULL, 0);
6651 error = got_error_from_errno("getcwd");
6655 error = got_worktree_open(&worktree, cwd);
6659 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6664 if (patch_script_path) {
6665 patch_script_file = fopen(patch_script_path, "r");
6666 if (patch_script_file == NULL) {
6667 error = got_error_from_errno2("fopen",
6673 error = apply_unveil(got_repo_get_path(repo), 0,
6674 got_worktree_get_root_path(worktree));
6678 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6682 cpa.patch_script_file = patch_script_file;
6683 cpa.action = "unstage";
6684 error = got_worktree_unstage(worktree, &paths, update_progress,
6685 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
6687 if (patch_script_file && fclose(patch_script_file) == EOF &&
6689 error = got_error_from_errno2("fclose", patch_script_path);
6691 got_repo_close(repo);
6693 got_worktree_close(worktree);
6694 TAILQ_FOREACH(pe, &paths, entry)
6695 free((char *)pe->path);
6696 got_pathlist_free(&paths);
6704 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
6705 "arg1 [arg2 ...]\n", getprogname());
6709 static const struct got_error *
6710 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6712 const struct got_error *err;
6713 struct got_blob_object *blob;
6715 err = got_object_open_as_blob(&blob, repo, id, 8192);
6719 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
6720 got_object_blob_close(blob);
6724 static const struct got_error *
6725 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6727 const struct got_error *err;
6728 struct got_tree_object *tree;
6729 const struct got_tree_entries *entries;
6730 struct got_tree_entry *te;
6732 err = got_object_open_as_tree(&tree, repo, id);
6736 entries = got_object_tree_get_entries(tree);
6737 te = SIMPLEQ_FIRST(&entries->head);
6740 if (sigint_received || sigpipe_received)
6742 err = got_object_id_str(&id_str, te->id);
6745 fprintf(outfile, "%s %.7o %s\n", id_str, te->mode, te->name);
6747 te = SIMPLEQ_NEXT(te, entry);
6750 got_object_tree_close(tree);
6754 static const struct got_error *
6755 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6757 const struct got_error *err;
6758 struct got_commit_object *commit;
6759 const struct got_object_id_queue *parent_ids;
6760 struct got_object_qid *pid;
6761 char *id_str = NULL;
6762 const char *logmsg = NULL;
6764 err = got_object_open_as_commit(&commit, repo, id);
6768 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
6772 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
6773 parent_ids = got_object_commit_get_parent_ids(commit);
6774 fprintf(outfile, "numparents %d\n",
6775 got_object_commit_get_nparents(commit));
6776 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
6778 err = got_object_id_str(&pid_str, pid->id);
6781 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
6784 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
6785 got_object_commit_get_author(commit),
6786 got_object_commit_get_author_time(commit));
6788 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
6789 got_object_commit_get_author(commit),
6790 got_object_commit_get_committer_time(commit));
6792 logmsg = got_object_commit_get_logmsg_raw(commit);
6793 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
6794 fprintf(outfile, "%s", logmsg);
6797 got_object_commit_close(commit);
6801 static const struct got_error *
6802 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6804 const struct got_error *err;
6805 struct got_tag_object *tag;
6806 char *id_str = NULL;
6807 const char *tagmsg = NULL;
6809 err = got_object_open_as_tag(&tag, repo, id);
6813 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
6817 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
6819 switch (got_object_tag_get_object_type(tag)) {
6820 case GOT_OBJ_TYPE_BLOB:
6821 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
6822 GOT_OBJ_LABEL_BLOB);
6824 case GOT_OBJ_TYPE_TREE:
6825 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
6826 GOT_OBJ_LABEL_TREE);
6828 case GOT_OBJ_TYPE_COMMIT:
6829 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
6830 GOT_OBJ_LABEL_COMMIT);
6832 case GOT_OBJ_TYPE_TAG:
6833 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
6840 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
6841 got_object_tag_get_name(tag));
6843 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
6844 got_object_tag_get_tagger(tag),
6845 got_object_tag_get_tagger_time(tag));
6847 tagmsg = got_object_tag_get_message(tag);
6848 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
6849 fprintf(outfile, "%s", tagmsg);
6852 got_object_tag_close(tag);
6856 static const struct got_error *
6857 cmd_cat(int argc, char *argv[])
6859 const struct got_error *error;
6860 struct got_repository *repo = NULL;
6861 struct got_worktree *worktree = NULL;
6862 char *cwd = NULL, *repo_path = NULL, *label = NULL;
6863 const char *commit_id_str = NULL;
6864 struct got_object_id *id = NULL, *commit_id = NULL;
6865 int ch, obj_type, i, force_path = 0;
6868 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6873 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
6876 commit_id_str = optarg;
6879 repo_path = realpath(optarg, NULL);
6880 if (repo_path == NULL)
6881 err(1, "-r option");
6882 got_path_strip_trailing_slashes(repo_path);
6896 cwd = getcwd(NULL, 0);
6898 error = got_error_from_errno("getcwd");
6901 error = got_worktree_open(&worktree, cwd);
6902 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6905 if (repo_path == NULL) {
6907 got_worktree_get_repo_path(worktree));
6908 if (repo_path == NULL) {
6909 error = got_error_from_errno("strdup");
6915 if (repo_path == NULL) {
6916 repo_path = getcwd(NULL, 0);
6917 if (repo_path == NULL)
6918 return got_error_from_errno("getcwd");
6921 error = got_repo_open(&repo, repo_path, NULL);
6926 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6930 if (commit_id_str == NULL)
6931 commit_id_str = GOT_REF_HEAD;
6932 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
6936 for (i = 0; i < argc; i++) {
6938 error = got_object_id_by_path(&id, repo, commit_id,
6943 error = match_object_id(&id, &label, argv[i],
6944 GOT_OBJ_TYPE_ANY, 0, repo);
6946 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
6947 error->code != GOT_ERR_NOT_REF)
6949 error = got_object_id_by_path(&id, repo,
6950 commit_id, argv[i]);
6956 error = got_object_get_type(&obj_type, repo, id);
6961 case GOT_OBJ_TYPE_BLOB:
6962 error = cat_blob(id, repo, stdout);
6964 case GOT_OBJ_TYPE_TREE:
6965 error = cat_tree(id, repo, stdout);
6967 case GOT_OBJ_TYPE_COMMIT:
6968 error = cat_commit(id, repo, stdout);
6970 case GOT_OBJ_TYPE_TAG:
6971 error = cat_tag(id, repo, stdout);
6974 error = got_error(GOT_ERR_OBJ_TYPE);
6990 got_worktree_close(worktree);
6992 const struct got_error *repo_error;
6993 repo_error = got_repo_close(repo);