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);
1425 got_object_tree_close(tree2);
1429 static const struct got_error *
1430 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1431 const char *path, int diff_context, struct got_repository *repo)
1433 const struct got_error *err = NULL;
1434 struct got_commit_object *pcommit = NULL;
1435 char *id_str1 = NULL, *id_str2 = NULL;
1436 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
1437 struct got_object_qid *qid;
1439 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1441 err = got_object_open_as_commit(&pcommit, repo,
1447 if (path && path[0] != '\0') {
1449 err = got_object_id_by_path(&obj_id2, repo, id, path);
1452 err = got_object_id_str(&id_str2, obj_id2);
1458 err = got_object_id_by_path(&obj_id1, repo,
1464 err = got_object_id_str(&id_str1, obj_id1);
1470 err = got_object_get_type(&obj_type, repo, obj_id2);
1475 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1477 case GOT_OBJ_TYPE_BLOB:
1478 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
1481 case GOT_OBJ_TYPE_TREE:
1482 err = diff_trees(obj_id1, obj_id2, path, diff_context,
1486 err = got_error(GOT_ERR_OBJ_TYPE);
1492 obj_id2 = got_object_commit_get_tree_id(commit);
1493 err = got_object_id_str(&id_str2, obj_id2);
1496 obj_id1 = got_object_commit_get_tree_id(pcommit);
1497 err = got_object_id_str(&id_str1, obj_id1);
1500 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1501 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
1508 got_object_commit_close(pcommit);
1513 get_datestr(time_t *time, char *datebuf)
1515 struct tm mytm, *tm;
1518 tm = gmtime_r(time, &mytm);
1521 s = asctime_r(tm, datebuf);
1524 p = strchr(s, '\n');
1530 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1532 static const struct got_error *
1533 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1534 struct got_repository *repo, const char *path, int show_patch,
1535 int diff_context, struct got_reflist_head *refs)
1537 const struct got_error *err = NULL;
1538 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1540 time_t committer_time;
1541 const char *author, *committer;
1542 char *refs_str = NULL;
1543 struct got_reflist_entry *re;
1545 SIMPLEQ_FOREACH(re, refs, entry) {
1548 struct got_tag_object *tag = NULL;
1551 name = got_ref_get_name(re->ref);
1552 if (strcmp(name, GOT_REF_HEAD) == 0)
1554 if (strncmp(name, "refs/", 5) == 0)
1556 if (strncmp(name, "got/", 4) == 0)
1558 if (strncmp(name, "heads/", 6) == 0)
1560 if (strncmp(name, "remotes/", 8) == 0)
1562 if (strncmp(name, "tags/", 5) == 0) {
1563 err = got_object_open_as_tag(&tag, repo, re->id);
1565 if (err->code != GOT_ERR_OBJ_TYPE)
1567 /* Ref points at something other than a tag. */
1572 cmp = got_object_id_cmp(tag ?
1573 got_object_tag_get_object_id(tag) : re->id, id);
1575 got_object_tag_close(tag);
1579 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1581 err = got_error_from_errno("asprintf");
1587 err = got_object_id_str(&id_str, id);
1591 printf(GOT_COMMIT_SEP_STR);
1592 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1593 refs_str ? refs_str : "", refs_str ? ")" : "");
1598 printf("from: %s\n", got_object_commit_get_author(commit));
1599 committer_time = got_object_commit_get_committer_time(commit);
1600 datestr = get_datestr(&committer_time, datebuf);
1602 printf("date: %s UTC\n", datestr);
1603 author = got_object_commit_get_author(commit);
1604 committer = got_object_commit_get_committer(commit);
1605 if (strcmp(author, committer) != 0)
1606 printf("via: %s\n", committer);
1607 if (got_object_commit_get_nparents(commit) > 1) {
1608 const struct got_object_id_queue *parent_ids;
1609 struct got_object_qid *qid;
1611 parent_ids = got_object_commit_get_parent_ids(commit);
1612 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1613 err = got_object_id_str(&id_str, qid->id);
1616 printf("parent %d: %s\n", n++, id_str);
1621 err = got_object_commit_get_logmsg(&logmsg0, commit);
1627 line = strsep(&logmsg, "\n");
1629 printf(" %s\n", line);
1634 err = print_patch(commit, id, path, diff_context, repo);
1639 if (fflush(stdout) != 0 && err == NULL)
1640 err = got_error_from_errno("fflush");
1644 static const struct got_error *
1645 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1646 char *path, int show_patch, int diff_context, int limit,
1647 int first_parent_traversal, struct got_reflist_head *refs)
1649 const struct got_error *err;
1650 struct got_commit_graph *graph;
1652 err = got_commit_graph_open(&graph, root_id, path,
1653 first_parent_traversal, repo);
1656 err = got_commit_graph_iter_start(graph, root_id, repo,
1657 check_cancelled, NULL);
1661 struct got_commit_object *commit;
1662 struct got_object_id *id;
1664 if (sigint_received || sigpipe_received)
1667 err = got_commit_graph_iter_next(&id, graph);
1669 if (err->code == GOT_ERR_ITER_COMPLETED) {
1673 if (err->code != GOT_ERR_ITER_NEED_MORE)
1675 err = got_commit_graph_fetch_commits(graph, 1, repo,
1676 check_cancelled, NULL);
1685 err = got_object_open_as_commit(&commit, repo, id);
1688 err = print_commit(commit, id, repo, path, show_patch,
1689 diff_context, refs);
1690 got_object_commit_close(commit);
1691 if (err || (limit && --limit == 0))
1695 got_commit_graph_close(graph);
1702 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1703 "[-r repository-path] [path]\n", getprogname());
1708 get_default_log_limit(void)
1710 const char *got_default_log_limit;
1714 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
1715 if (got_default_log_limit == NULL)
1717 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
1723 static const struct got_error *
1724 cmd_log(int argc, char *argv[])
1726 const struct got_error *error;
1727 struct got_repository *repo = NULL;
1728 struct got_worktree *worktree = NULL;
1729 struct got_commit_object *commit = NULL;
1730 struct got_object_id *id = NULL;
1731 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1732 char *start_commit = NULL;
1733 int diff_context = 3, ch;
1734 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1736 struct got_reflist_head refs;
1738 SIMPLEQ_INIT(&refs);
1741 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1747 limit = get_default_log_limit();
1749 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
1755 start_commit = optarg;
1758 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1761 err(1, "-C option %s", errstr);
1764 limit = strtonum(optarg, 0, INT_MAX, &errstr);
1766 err(1, "-l option %s", errstr);
1769 first_parent_traversal = 1;
1772 repo_path = realpath(optarg, NULL);
1773 if (repo_path == NULL)
1774 err(1, "-r option");
1775 got_path_strip_trailing_slashes(repo_path);
1786 cwd = getcwd(NULL, 0);
1788 error = got_error_from_errno("getcwd");
1792 error = got_worktree_open(&worktree, cwd);
1793 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1800 error = got_error_from_errno("strdup");
1803 } else if (argc == 1) {
1805 error = got_worktree_resolve_path(&path, worktree,
1810 path = strdup(argv[0]);
1812 error = got_error_from_errno("strdup");
1819 if (repo_path == NULL) {
1820 repo_path = worktree ?
1821 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1823 if (repo_path == NULL) {
1824 error = got_error_from_errno("strdup");
1828 error = got_repo_open(&repo, repo_path, NULL);
1832 error = apply_unveil(got_repo_get_path(repo), 1,
1833 worktree ? got_worktree_get_root_path(worktree) : NULL);
1837 if (start_commit == NULL) {
1838 struct got_reference *head_ref;
1839 error = got_ref_open(&head_ref, repo,
1840 worktree ? got_worktree_get_head_ref_name(worktree)
1844 error = got_ref_resolve(&id, repo, head_ref);
1845 got_ref_close(head_ref);
1848 error = got_object_open_as_commit(&commit, repo, id);
1850 struct got_reference *ref;
1851 error = got_ref_open(&ref, repo, start_commit, 0);
1852 if (error == NULL) {
1854 error = got_ref_resolve(&id, repo, ref);
1858 error = got_object_get_type(&obj_type, repo, id);
1861 if (obj_type == GOT_OBJ_TYPE_TAG) {
1862 struct got_tag_object *tag;
1863 error = got_object_open_as_tag(&tag, repo, id);
1866 if (got_object_tag_get_object_type(tag) !=
1867 GOT_OBJ_TYPE_COMMIT) {
1868 got_object_tag_close(tag);
1869 error = got_error(GOT_ERR_OBJ_TYPE);
1873 id = got_object_id_dup(
1874 got_object_tag_get_object_id(tag));
1876 error = got_error_from_errno(
1877 "got_object_id_dup");
1878 got_object_tag_close(tag);
1881 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1882 error = got_error(GOT_ERR_OBJ_TYPE);
1885 error = got_object_open_as_commit(&commit, repo, id);
1889 if (commit == NULL) {
1890 error = got_repo_match_object_id_prefix(&id,
1891 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1900 const char *prefix = got_worktree_get_path_prefix(worktree);
1902 if (asprintf(&p, "%s%s%s", prefix,
1903 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
1904 error = got_error_from_errno("asprintf");
1907 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1910 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1915 path = in_repo_path;
1918 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
1922 error = print_commits(id, repo, path, show_patch,
1923 diff_context, limit, first_parent_traversal, &refs);
1930 got_worktree_close(worktree);
1932 const struct got_error *repo_error;
1933 repo_error = got_repo_close(repo);
1937 got_ref_list_free(&refs);
1944 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
1945 "[-w] [object1 object2 | path]\n", getprogname());
1949 struct print_diff_arg {
1950 struct got_repository *repo;
1951 struct got_worktree *worktree;
1956 int ignore_whitespace;
1959 static const struct got_error *
1960 print_diff(void *arg, unsigned char status, unsigned char staged_status,
1961 const char *path, struct got_object_id *blob_id,
1962 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
1964 struct print_diff_arg *a = arg;
1965 const struct got_error *err = NULL;
1966 struct got_blob_object *blob1 = NULL;
1968 char *abspath = NULL, *label1 = NULL;
1971 if (a->diff_staged) {
1972 if (staged_status != GOT_STATUS_MODIFY &&
1973 staged_status != GOT_STATUS_ADD &&
1974 staged_status != GOT_STATUS_DELETE)
1977 if (staged_status == GOT_STATUS_DELETE)
1979 if (status == GOT_STATUS_NONEXISTENT)
1980 return got_error_set_errno(ENOENT, path);
1981 if (status != GOT_STATUS_MODIFY &&
1982 status != GOT_STATUS_ADD &&
1983 status != GOT_STATUS_DELETE &&
1984 status != GOT_STATUS_CONFLICT)
1988 if (!a->header_shown) {
1989 printf("diff %s %s%s\n", a->id_str,
1990 got_worktree_get_root_path(a->worktree),
1991 a->diff_staged ? " (staged changes)" : "");
1992 a->header_shown = 1;
1995 if (a->diff_staged) {
1996 const char *label1 = NULL, *label2 = NULL;
1997 switch (staged_status) {
1998 case GOT_STATUS_MODIFY:
2002 case GOT_STATUS_ADD:
2005 case GOT_STATUS_DELETE:
2009 return got_error(GOT_ERR_FILE_STATUS);
2011 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2012 label1, label2, a->diff_context, a->ignore_whitespace,
2016 if (staged_status == GOT_STATUS_ADD ||
2017 staged_status == GOT_STATUS_MODIFY) {
2019 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2023 err = got_object_id_str(&id_str, staged_blob_id);
2026 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2027 err = got_error_from_errno("asprintf");
2032 } else if (status != GOT_STATUS_ADD) {
2033 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2038 if (status != GOT_STATUS_DELETE) {
2039 if (asprintf(&abspath, "%s/%s",
2040 got_worktree_get_root_path(a->worktree), path) == -1) {
2041 err = got_error_from_errno("asprintf");
2045 f2 = fopen(abspath, "r");
2047 err = got_error_from_errno2("fopen", abspath);
2050 if (lstat(abspath, &sb) == -1) {
2051 err = got_error_from_errno2("lstat", abspath);
2057 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2058 a->diff_context, a->ignore_whitespace, stdout);
2061 got_object_blob_close(blob1);
2062 if (f2 && fclose(f2) != 0 && err == NULL)
2063 err = got_error_from_errno("fclose");
2068 static const struct got_error *
2069 match_object_id(struct got_object_id **id, char **label,
2070 const char *id_str, int obj_type, int resolve_tags,
2071 struct got_repository *repo)
2073 const struct got_error *err;
2074 struct got_tag_object *tag;
2075 struct got_reference *ref = NULL;
2081 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
2084 *id = got_object_id_dup(
2085 got_object_tag_get_object_id(tag));
2087 err = got_error_from_errno("got_object_id_dup");
2088 else if (asprintf(label, "refs/tags/%s",
2089 got_object_tag_get_name(tag)) == -1) {
2090 err = got_error_from_errno("asprintf");
2094 got_object_tag_close(tag);
2096 } else if (err->code != GOT_ERR_NO_OBJ)
2100 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
2102 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2104 err = got_ref_open(&ref, repo, id_str, 0);
2107 *label = strdup(got_ref_get_name(ref));
2108 if (*label == NULL) {
2109 err = got_error_from_errno("strdup");
2112 err = got_ref_resolve(id, repo, ref);
2114 err = got_object_id_str(label, *id);
2115 if (*label == NULL) {
2116 err = got_error_from_errno("strdup");
2127 static const struct got_error *
2128 cmd_diff(int argc, char *argv[])
2130 const struct got_error *error;
2131 struct got_repository *repo = NULL;
2132 struct got_worktree *worktree = NULL;
2133 char *cwd = NULL, *repo_path = NULL;
2134 struct got_object_id *id1 = NULL, *id2 = NULL;
2135 const char *id_str1 = NULL, *id_str2 = NULL;
2136 char *label1 = NULL, *label2 = NULL;
2138 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2143 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2148 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2151 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
2153 err(1, "-C option %s", errstr);
2156 repo_path = realpath(optarg, NULL);
2157 if (repo_path == NULL)
2158 err(1, "-r option");
2159 got_path_strip_trailing_slashes(repo_path);
2165 ignore_whitespace = 1;
2176 cwd = getcwd(NULL, 0);
2178 error = got_error_from_errno("getcwd");
2181 error = got_worktree_open(&worktree, cwd);
2182 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2185 if (worktree == NULL) {
2186 error = got_error(GOT_ERR_NOT_WORKTREE);
2191 "-r option can't be used when diffing a work tree");
2192 repo_path = strdup(got_worktree_get_repo_path(worktree));
2193 if (repo_path == NULL) {
2194 error = got_error_from_errno("strdup");
2198 error = got_worktree_resolve_path(&path, worktree,
2205 error = got_error_from_errno("strdup");
2209 } else if (argc == 2) {
2211 errx(1, "-s option can't be used when diffing "
2212 "objects in repository");
2215 if (worktree && repo_path == NULL) {
2217 strdup(got_worktree_get_repo_path(worktree));
2218 if (repo_path == NULL) {
2219 error = got_error_from_errno("strdup");
2226 if (repo_path == NULL) {
2227 repo_path = getcwd(NULL, 0);
2228 if (repo_path == NULL)
2229 return got_error_from_errno("getcwd");
2232 error = got_repo_open(&repo, repo_path, NULL);
2237 error = apply_unveil(got_repo_get_path(repo), 1,
2238 worktree ? got_worktree_get_root_path(worktree) : NULL);
2243 struct print_diff_arg arg;
2244 struct got_pathlist_head paths;
2249 error = got_object_id_str(&id_str,
2250 got_worktree_get_base_commit_id(worktree));
2254 arg.worktree = worktree;
2255 arg.diff_context = diff_context;
2256 arg.id_str = id_str;
2257 arg.header_shown = 0;
2258 arg.diff_staged = diff_staged;
2259 arg.ignore_whitespace = ignore_whitespace;
2261 error = got_pathlist_append(&paths, path, NULL);
2265 error = got_worktree_status(worktree, &paths, repo, print_diff,
2266 &arg, check_cancelled, NULL);
2268 got_pathlist_free(&paths);
2272 error = match_object_id(&id1, &label1, id_str1, GOT_OBJ_TYPE_ANY, 1,
2277 error = match_object_id(&id2, &label2, id_str2, GOT_OBJ_TYPE_ANY, 1,
2282 error = got_object_get_type(&type1, repo, id1);
2286 error = got_object_get_type(&type2, repo, id2);
2290 if (type1 != type2) {
2291 error = got_error(GOT_ERR_OBJ_TYPE);
2296 case GOT_OBJ_TYPE_BLOB:
2297 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2298 diff_context, ignore_whitespace, repo, stdout);
2300 case GOT_OBJ_TYPE_TREE:
2301 error = got_diff_objects_as_trees(id1, id2, "", "",
2302 diff_context, ignore_whitespace, repo, stdout);
2304 case GOT_OBJ_TYPE_COMMIT:
2305 printf("diff %s %s\n", label1, label2);
2306 error = got_diff_objects_as_commits(id1, id2, diff_context,
2307 ignore_whitespace, repo, stdout);
2310 error = got_error(GOT_ERR_OBJ_TYPE);
2320 got_worktree_close(worktree);
2322 const struct got_error *repo_error;
2323 repo_error = got_repo_close(repo);
2334 "usage: %s blame [-c commit] [-r repository-path] path\n",
2343 char datebuf[9]; /* YY-MM-DD + NUL */
2346 struct blame_cb_args {
2347 struct blame_line *lines;
2351 off_t *line_offsets;
2353 struct got_repository *repo;
2356 static const struct got_error *
2357 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2359 const struct got_error *err = NULL;
2360 struct blame_cb_args *a = arg;
2361 struct blame_line *bline;
2363 size_t linesize = 0;
2364 struct got_commit_object *commit = NULL;
2367 time_t committer_time;
2369 if (nlines != a->nlines ||
2370 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2371 return got_error(GOT_ERR_RANGE);
2373 if (sigint_received)
2374 return got_error(GOT_ERR_ITER_COMPLETED);
2377 return NULL; /* no change in this commit */
2379 /* Annotate this line. */
2380 bline = &a->lines[lineno - 1];
2381 if (bline->annotated)
2383 err = got_object_id_str(&bline->id_str, id);
2387 err = got_object_open_as_commit(&commit, a->repo, id);
2391 bline->committer = strdup(got_object_commit_get_committer(commit));
2392 if (bline->committer == NULL) {
2393 err = got_error_from_errno("strdup");
2397 committer_time = got_object_commit_get_committer_time(commit);
2398 if (localtime_r(&committer_time, &tm) == NULL)
2399 return got_error_from_errno("localtime_r");
2400 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%g/%m/%d",
2401 &tm) >= sizeof(bline->datebuf)) {
2402 err = got_error(GOT_ERR_NO_SPACE);
2405 bline->annotated = 1;
2407 /* Print lines annotated so far. */
2408 bline = &a->lines[a->lineno_cur - 1];
2409 if (!bline->annotated)
2412 offset = a->line_offsets[a->lineno_cur - 1];
2413 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2414 err = got_error_from_errno("fseeko");
2418 while (bline->annotated) {
2419 char *smallerthan, *at, *nl, *committer;
2422 if (getline(&line, &linesize, a->f) == -1) {
2424 err = got_error_from_errno("getline");
2428 committer = bline->committer;
2429 smallerthan = strchr(committer, '<');
2430 if (smallerthan && smallerthan[1] != '\0')
2431 committer = smallerthan + 1;
2432 at = strchr(committer, '@');
2435 len = strlen(committer);
2437 committer[8] = '\0';
2439 nl = strchr(line, '\n');
2442 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2443 bline->id_str, bline->datebuf, committer, line);
2446 bline = &a->lines[a->lineno_cur - 1];
2450 got_object_commit_close(commit);
2455 static const struct got_error *
2456 cmd_blame(int argc, char *argv[])
2458 const struct got_error *error;
2459 struct got_repository *repo = NULL;
2460 struct got_worktree *worktree = NULL;
2461 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2462 struct got_object_id *obj_id = NULL;
2463 struct got_object_id *commit_id = NULL;
2464 struct got_blob_object *blob = NULL;
2465 char *commit_id_str = NULL;
2466 struct blame_cb_args bca;
2467 int ch, obj_type, i;
2470 memset(&bca, 0, sizeof(bca));
2473 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2478 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2481 commit_id_str = optarg;
2484 repo_path = realpath(optarg, NULL);
2485 if (repo_path == NULL)
2486 err(1, "-r option");
2487 got_path_strip_trailing_slashes(repo_path);
2503 cwd = getcwd(NULL, 0);
2505 error = got_error_from_errno("getcwd");
2508 if (repo_path == NULL) {
2509 error = got_worktree_open(&worktree, cwd);
2510 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2516 strdup(got_worktree_get_repo_path(worktree));
2517 if (repo_path == NULL)
2518 error = got_error_from_errno("strdup");
2522 repo_path = strdup(cwd);
2523 if (repo_path == NULL) {
2524 error = got_error_from_errno("strdup");
2530 error = got_repo_open(&repo, repo_path, NULL);
2534 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2539 const char *prefix = got_worktree_get_path_prefix(worktree);
2540 char *p, *worktree_subdir = cwd +
2541 strlen(got_worktree_get_root_path(worktree));
2542 if (asprintf(&p, "%s%s%s%s%s",
2543 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2544 worktree_subdir, worktree_subdir[0] ? "/" : "",
2546 error = got_error_from_errno("asprintf");
2549 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2552 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2557 if (commit_id_str == NULL) {
2558 struct got_reference *head_ref;
2559 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2562 error = got_ref_resolve(&commit_id, repo, head_ref);
2563 got_ref_close(head_ref);
2567 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2572 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2575 if (obj_id == NULL) {
2576 error = got_error(GOT_ERR_NO_OBJ);
2580 error = got_object_get_type(&obj_type, repo, obj_id);
2584 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2585 error = got_error(GOT_ERR_OBJ_TYPE);
2589 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2592 bca.f = got_opentemp();
2593 if (bca.f == NULL) {
2594 error = got_error_from_errno("got_opentemp");
2597 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2598 &bca.line_offsets, bca.f, blob);
2599 if (error || bca.nlines == 0)
2602 /* Don't include \n at EOF in the blame line count. */
2603 if (bca.line_offsets[bca.nlines - 1] == filesize)
2606 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2607 if (bca.lines == NULL) {
2608 error = got_error_from_errno("calloc");
2612 bca.nlines_prec = 0;
2620 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2621 check_cancelled, NULL);
2631 got_object_blob_close(blob);
2633 got_worktree_close(worktree);
2635 const struct got_error *repo_error;
2636 repo_error = got_repo_close(repo);
2641 for (i = 0; i < bca.nlines; i++) {
2642 struct blame_line *bline = &bca.lines[i];
2643 free(bline->id_str);
2644 free(bline->committer);
2648 free(bca.line_offsets);
2649 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2650 error = got_error_from_errno("fclose");
2658 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2664 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2665 const char *root_path)
2667 int is_root_path = (strcmp(path, root_path) == 0);
2668 const char *modestr = "";
2670 path += strlen(root_path);
2671 while (path[0] == '/')
2674 if (got_object_tree_entry_is_submodule(te))
2676 else if (S_ISLNK(te->mode))
2678 else if (S_ISDIR(te->mode))
2680 else if (te->mode & S_IXUSR)
2683 printf("%s%s%s%s%s\n", id ? id : "", path,
2684 is_root_path ? "" : "/", te->name, modestr);
2687 static const struct got_error *
2688 print_tree(const char *path, struct got_object_id *commit_id,
2689 int show_ids, int recurse, const char *root_path,
2690 struct got_repository *repo)
2692 const struct got_error *err = NULL;
2693 struct got_object_id *tree_id = NULL;
2694 struct got_tree_object *tree = NULL;
2695 const struct got_tree_entries *entries;
2696 struct got_tree_entry *te;
2698 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2702 err = got_object_open_as_tree(&tree, repo, tree_id);
2705 entries = got_object_tree_get_entries(tree);
2706 te = SIMPLEQ_FIRST(&entries->head);
2710 if (sigint_received || sigpipe_received)
2715 err = got_object_id_str(&id_str, te->id);
2718 if (asprintf(&id, "%s ", id_str) == -1) {
2719 err = got_error_from_errno("asprintf");
2725 print_entry(te, id, path, root_path);
2728 if (recurse && S_ISDIR(te->mode)) {
2730 if (asprintf(&child_path, "%s%s%s", path,
2731 path[0] == '/' && path[1] == '\0' ? "" : "/",
2733 err = got_error_from_errno("asprintf");
2736 err = print_tree(child_path, commit_id, show_ids, 1,
2743 te = SIMPLEQ_NEXT(te, entry);
2747 got_object_tree_close(tree);
2752 static const struct got_error *
2753 cmd_tree(int argc, char *argv[])
2755 const struct got_error *error;
2756 struct got_repository *repo = NULL;
2757 struct got_worktree *worktree = NULL;
2759 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2760 struct got_object_id *commit_id = NULL;
2761 char *commit_id_str = NULL;
2762 int show_ids = 0, recurse = 0;
2766 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2771 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2774 commit_id_str = optarg;
2777 repo_path = realpath(optarg, NULL);
2778 if (repo_path == NULL)
2779 err(1, "-r option");
2780 got_path_strip_trailing_slashes(repo_path);
2804 cwd = getcwd(NULL, 0);
2806 error = got_error_from_errno("getcwd");
2809 if (repo_path == NULL) {
2810 error = got_worktree_open(&worktree, cwd);
2811 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2817 strdup(got_worktree_get_repo_path(worktree));
2818 if (repo_path == NULL)
2819 error = got_error_from_errno("strdup");
2823 repo_path = strdup(cwd);
2824 if (repo_path == NULL) {
2825 error = got_error_from_errno("strdup");
2831 error = got_repo_open(&repo, repo_path, NULL);
2835 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2841 char *p, *worktree_subdir = cwd +
2842 strlen(got_worktree_get_root_path(worktree));
2843 if (asprintf(&p, "%s/%s",
2844 got_worktree_get_path_prefix(worktree),
2845 worktree_subdir) == -1) {
2846 error = got_error_from_errno("asprintf");
2849 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2856 if (in_repo_path == NULL) {
2857 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2862 if (commit_id_str == NULL) {
2863 struct got_reference *head_ref;
2864 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2867 error = got_ref_resolve(&commit_id, repo, head_ref);
2868 got_ref_close(head_ref);
2872 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2877 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2878 in_repo_path, repo);
2885 got_worktree_close(worktree);
2887 const struct got_error *repo_error;
2888 repo_error = got_repo_close(repo);
2898 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
2902 static const struct got_error *
2903 print_status(void *arg, unsigned char status, unsigned char staged_status,
2904 const char *path, struct got_object_id *blob_id,
2905 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
2907 if (status == staged_status && (status == GOT_STATUS_DELETE))
2908 status = GOT_STATUS_NO_CHANGE;
2909 printf("%c%c %s\n", status, staged_status, path);
2913 static const struct got_error *
2914 cmd_status(int argc, char *argv[])
2916 const struct got_error *error = NULL;
2917 struct got_repository *repo = NULL;
2918 struct got_worktree *worktree = NULL;
2920 struct got_pathlist_head paths;
2921 struct got_pathlist_entry *pe;
2926 while ((ch = getopt(argc, argv, "")) != -1) {
2938 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2942 cwd = getcwd(NULL, 0);
2944 error = got_error_from_errno("getcwd");
2948 error = got_worktree_open(&worktree, cwd);
2952 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
2957 error = apply_unveil(got_repo_get_path(repo), 1,
2958 got_worktree_get_root_path(worktree));
2962 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2966 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
2967 check_cancelled, NULL);
2969 TAILQ_FOREACH(pe, &paths, entry)
2970 free((char *)pe->path);
2971 got_pathlist_free(&paths);
2980 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
2985 static const struct got_error *
2986 list_refs(struct got_repository *repo)
2988 static const struct got_error *err = NULL;
2989 struct got_reflist_head refs;
2990 struct got_reflist_entry *re;
2992 SIMPLEQ_INIT(&refs);
2993 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2997 SIMPLEQ_FOREACH(re, &refs, entry) {
2999 refstr = got_ref_to_str(re->ref);
3001 return got_error_from_errno("got_ref_to_str");
3002 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3006 got_ref_list_free(&refs);
3010 static const struct got_error *
3011 delete_ref(struct got_repository *repo, const char *refname)
3013 const struct got_error *err = NULL;
3014 struct got_reference *ref;
3016 err = got_ref_open(&ref, repo, refname, 0);
3020 err = got_ref_delete(ref, repo);
3025 static const struct got_error *
3026 add_ref(struct got_repository *repo, const char *refname, const char *target)
3028 const struct got_error *err = NULL;
3029 struct got_object_id *id;
3030 struct got_reference *ref = NULL;
3033 * Don't let the user create a reference named '-'.
3034 * While technically a valid reference name, this case is usually
3035 * an unintended typo.
3037 if (refname[0] == '-' && refname[1] == '\0')
3038 return got_error(GOT_ERR_BAD_REF_NAME);
3040 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3043 struct got_reference *target_ref;
3045 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3047 err = got_ref_open(&target_ref, repo, target, 0);
3050 err = got_ref_resolve(&id, repo, target_ref);
3051 got_ref_close(target_ref);
3056 err = got_ref_alloc(&ref, refname, id);
3060 err = got_ref_write(ref, repo);
3068 static const struct got_error *
3069 add_symref(struct got_repository *repo, const char *refname, const char *target)
3071 const struct got_error *err = NULL;
3072 struct got_reference *ref = NULL;
3073 struct got_reference *target_ref = NULL;
3076 * Don't let the user create a reference named '-'.
3077 * While technically a valid reference name, this case is usually
3078 * an unintended typo.
3080 if (refname[0] == '-' && refname[1] == '\0')
3081 return got_error(GOT_ERR_BAD_REF_NAME);
3083 err = got_ref_open(&target_ref, repo, target, 0);
3087 err = got_ref_alloc_symref(&ref, refname, target_ref);
3091 err = got_ref_write(ref, repo);
3094 got_ref_close(target_ref);
3100 static const struct got_error *
3101 cmd_ref(int argc, char *argv[])
3103 const struct got_error *error = NULL;
3104 struct got_repository *repo = NULL;
3105 struct got_worktree *worktree = NULL;
3106 char *cwd = NULL, *repo_path = NULL;
3107 int ch, do_list = 0, create_symref = 0;
3108 const char *delref = NULL;
3110 /* TODO: Add -s option for adding symbolic references. */
3111 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3117 repo_path = realpath(optarg, NULL);
3118 if (repo_path == NULL)
3119 err(1, "-r option");
3120 got_path_strip_trailing_slashes(repo_path);
3134 if (do_list && delref)
3135 errx(1, "-l and -d options are mutually exclusive\n");
3140 if (do_list || delref) {
3142 errx(1, "-s option cannot be used together with the "
3143 "-l or -d options");
3146 } else if (argc != 2)
3151 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3155 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3156 "sendfd unveil", NULL) == -1)
3160 cwd = getcwd(NULL, 0);
3162 error = got_error_from_errno("getcwd");
3166 if (repo_path == NULL) {
3167 error = got_worktree_open(&worktree, cwd);
3168 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3174 strdup(got_worktree_get_repo_path(worktree));
3175 if (repo_path == NULL)
3176 error = got_error_from_errno("strdup");
3180 repo_path = strdup(cwd);
3181 if (repo_path == NULL) {
3182 error = got_error_from_errno("strdup");
3188 error = got_repo_open(&repo, repo_path, NULL);
3192 error = apply_unveil(got_repo_get_path(repo), do_list,
3193 worktree ? got_worktree_get_root_path(worktree) : NULL);
3198 error = list_refs(repo);
3200 error = delete_ref(repo, delref);
3201 else if (create_symref)
3202 error = add_symref(repo, argv[0], argv[1]);
3204 error = add_ref(repo, argv[0], argv[1]);
3207 got_repo_close(repo);
3209 got_worktree_close(worktree);
3219 "usage: %s branch [-r repository] [-l] | -d name | "
3220 "[name [commit]]\n", getprogname());
3224 static const struct got_error *
3225 list_branch(struct got_repository *repo, struct got_worktree *worktree,
3226 struct got_reference *ref)
3228 const struct got_error *err = NULL;
3229 const char *refname, *marker = " ";
3232 refname = got_ref_get_name(ref);
3233 if (worktree && strcmp(refname,
3234 got_worktree_get_head_ref_name(worktree)) == 0) {
3235 struct got_object_id *id = NULL;
3237 err = got_ref_resolve(&id, repo, ref);
3240 if (got_object_id_cmp(id,
3241 got_worktree_get_base_commit_id(worktree)) == 0)
3248 if (strncmp(refname, "refs/heads/", 11) == 0)
3250 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3253 refstr = got_ref_to_str(ref);
3255 return got_error_from_errno("got_ref_to_str");
3257 printf("%s%s: %s\n", marker, refname, refstr);
3262 static const struct got_error *
3263 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
3265 const char *refname;
3267 if (worktree == NULL)
3268 return got_error(GOT_ERR_NOT_WORKTREE);
3270 refname = got_worktree_get_head_ref_name(worktree);
3272 if (strncmp(refname, "refs/heads/", 11) == 0)
3274 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3277 printf("%s\n", refname);
3282 static const struct got_error *
3283 list_branches(struct got_repository *repo, struct got_worktree *worktree)
3285 static const struct got_error *err = NULL;
3286 struct got_reflist_head refs;
3287 struct got_reflist_entry *re;
3288 struct got_reference *temp_ref = NULL;
3289 int rebase_in_progress, histedit_in_progress;
3291 SIMPLEQ_INIT(&refs);
3294 err = got_worktree_rebase_in_progress(&rebase_in_progress,
3299 err = got_worktree_histedit_in_progress(&histedit_in_progress,
3304 if (rebase_in_progress || histedit_in_progress) {
3305 err = got_ref_open(&temp_ref, repo,
3306 got_worktree_get_head_ref_name(worktree), 0);
3309 list_branch(repo, worktree, temp_ref);
3310 got_ref_close(temp_ref);
3314 err = got_ref_list(&refs, repo, "refs/heads",
3315 got_ref_cmp_by_name, NULL);
3319 SIMPLEQ_FOREACH(re, &refs, entry)
3320 list_branch(repo, worktree, re->ref);
3322 got_ref_list_free(&refs);
3326 static const struct got_error *
3327 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
3328 const char *branch_name)
3330 const struct got_error *err = NULL;
3331 struct got_reference *ref = NULL;
3334 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3335 return got_error_from_errno("asprintf");
3337 err = got_ref_open(&ref, repo, refname, 0);
3342 strcmp(got_worktree_get_head_ref_name(worktree),
3343 got_ref_get_name(ref)) == 0) {
3344 err = got_error_msg(GOT_ERR_SAME_BRANCH,
3345 "will not delete this work tree's current branch");
3349 err = got_ref_delete(ref, repo);
3357 static const struct got_error *
3358 add_branch(struct got_repository *repo, const char *branch_name,
3359 const char *base_branch)
3361 const struct got_error *err = NULL;
3362 struct got_object_id *id = NULL;
3364 struct got_reference *ref = NULL;
3365 char *base_refname = NULL, *refname = NULL;
3368 * Don't let the user create a branch named '-'.
3369 * While technically a valid reference name, this case is usually
3370 * an unintended typo.
3372 if (branch_name[0] == '-' && branch_name[1] == '\0')
3373 return got_error(GOT_ERR_BAD_REF_NAME);
3375 err = match_object_id(&id, &label, base_branch,
3376 GOT_OBJ_TYPE_COMMIT, 1, repo);
3380 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3381 err = got_error_from_errno("asprintf");
3385 err = got_ref_open(&ref, repo, refname, 0);
3387 err = got_error(GOT_ERR_BRANCH_EXISTS);
3389 } else if (err->code != GOT_ERR_NOT_REF)
3392 err = got_ref_alloc(&ref, refname, id);
3396 err = got_ref_write(ref, repo);
3406 static const struct got_error *
3407 cmd_branch(int argc, char *argv[])
3409 const struct got_error *error = NULL;
3410 struct got_repository *repo = NULL;
3411 struct got_worktree *worktree = NULL;
3412 char *cwd = NULL, *repo_path = NULL;
3413 int ch, do_list = 0, do_show = 0;
3414 const char *delref = NULL;
3416 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
3422 repo_path = realpath(optarg, NULL);
3423 if (repo_path == NULL)
3424 err(1, "-r option");
3425 got_path_strip_trailing_slashes(repo_path);
3436 if (do_list && delref)
3437 errx(1, "-l and -d options are mutually exclusive\n");
3442 if (!do_list && !delref && argc == 0)
3445 if (do_list || delref) {
3448 } else if (!do_show && (argc < 1 || argc > 2))
3452 if (do_list || do_show) {
3453 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3457 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3458 "sendfd unveil", NULL) == -1)
3462 cwd = getcwd(NULL, 0);
3464 error = got_error_from_errno("getcwd");
3468 if (repo_path == NULL) {
3469 error = got_worktree_open(&worktree, cwd);
3470 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3476 strdup(got_worktree_get_repo_path(worktree));
3477 if (repo_path == NULL)
3478 error = got_error_from_errno("strdup");
3482 repo_path = strdup(cwd);
3483 if (repo_path == NULL) {
3484 error = got_error_from_errno("strdup");
3490 error = got_repo_open(&repo, repo_path, NULL);
3494 error = apply_unveil(got_repo_get_path(repo), do_list,
3495 worktree ? got_worktree_get_root_path(worktree) : NULL);
3500 error = show_current_branch(repo, worktree);
3502 error = list_branches(repo, worktree);
3504 error = delete_branch(repo, worktree, delref);
3506 const char *base_branch;
3508 base_branch = worktree ?
3509 got_worktree_get_head_ref_name(worktree) :
3511 if (strncmp(base_branch, "refs/heads/", 11) == 0)
3514 base_branch = argv[1];
3515 error = add_branch(repo, argv[0], base_branch);
3519 got_repo_close(repo);
3521 got_worktree_close(worktree);
3532 "usage: %s tag [-r repository] | -l | "
3533 "[-m message] name [commit]\n", getprogname());
3538 static const struct got_error *
3539 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
3541 const struct got_error *err = NULL;
3542 struct got_reflist_entry *re, *se, *new;
3543 struct got_object_id *re_id, *se_id;
3544 struct got_tag_object *re_tag, *se_tag;
3545 time_t re_time, se_time;
3547 SIMPLEQ_FOREACH(re, tags, entry) {
3548 se = SIMPLEQ_FIRST(sorted);
3550 err = got_reflist_entry_dup(&new, re);
3553 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
3556 err = got_ref_resolve(&re_id, repo, re->ref);
3559 err = got_object_open_as_tag(&re_tag, repo, re_id);
3563 re_time = got_object_tag_get_tagger_time(re_tag);
3564 got_object_tag_close(re_tag);
3568 err = got_ref_resolve(&se_id, repo, re->ref);
3571 err = got_object_open_as_tag(&se_tag, repo, se_id);
3575 se_time = got_object_tag_get_tagger_time(se_tag);
3576 got_object_tag_close(se_tag);
3578 if (se_time > re_time) {
3579 err = got_reflist_entry_dup(&new, re);
3582 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
3585 se = SIMPLEQ_NEXT(se, entry);
3594 static const struct got_error *
3595 cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
3596 struct got_reference *ref2)
3598 const struct got_error *err = NULL;
3599 struct got_repository *repo = arg;
3600 struct got_object_id *id1, *id2 = NULL;
3601 struct got_tag_object *tag1 = NULL, *tag2 = NULL;
3602 time_t time1, time2;
3606 err = got_ref_resolve(&id1, repo, ref1);
3609 err = got_object_open_as_tag(&tag1, repo, id1);
3613 err = got_ref_resolve(&id2, repo, ref2);
3616 err = got_object_open_as_tag(&tag2, repo, id2);
3620 time1 = got_object_tag_get_tagger_time(tag1);
3621 time2 = got_object_tag_get_tagger_time(tag2);
3623 /* Put latest tags first. */
3626 else if (time1 > time2)
3629 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
3634 got_object_tag_close(tag1);
3636 got_object_tag_close(tag2);
3640 static const struct got_error *
3641 list_tags(struct got_repository *repo, struct got_worktree *worktree)
3643 static const struct got_error *err = NULL;
3644 struct got_reflist_head refs;
3645 struct got_reflist_entry *re;
3647 SIMPLEQ_INIT(&refs);
3649 err = got_ref_list(&refs, repo, "refs/tags", cmp_tags, repo);
3653 SIMPLEQ_FOREACH(re, &refs, entry) {
3654 const char *refname;
3655 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
3658 struct got_object_id *id;
3659 struct got_tag_object *tag;
3661 refname = got_ref_get_name(re->ref);
3662 if (strncmp(refname, "refs/tags/", 10) != 0)
3665 refstr = got_ref_to_str(re->ref);
3666 if (refstr == NULL) {
3667 err = got_error_from_errno("got_ref_to_str");
3670 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
3673 err = got_ref_resolve(&id, repo, re->ref);
3676 err = got_object_open_as_tag(&tag, repo, id);
3680 printf("from: %s\n", got_object_tag_get_tagger(tag));
3681 tagger_time = got_object_tag_get_tagger_time(tag);
3682 datestr = get_datestr(&tagger_time, datebuf);
3684 printf("date: %s UTC\n", datestr);
3685 err = got_object_id_str(&id_str,
3686 got_object_tag_get_object_id(tag));
3689 switch (got_object_tag_get_object_type(tag)) {
3690 case GOT_OBJ_TYPE_BLOB:
3691 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB, id_str);
3693 case GOT_OBJ_TYPE_TREE:
3694 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE, id_str);
3696 case GOT_OBJ_TYPE_COMMIT:
3697 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
3699 case GOT_OBJ_TYPE_TAG:
3700 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG, id_str);
3706 tagmsg0 = strdup(got_object_tag_get_message(tag));
3707 got_object_tag_close(tag);
3708 if (tagmsg0 == NULL) {
3709 err = got_error_from_errno("strdup");
3715 line = strsep(&tagmsg, "\n");
3717 printf(" %s\n", line);
3722 got_ref_list_free(&refs);
3726 static const struct got_error *
3727 get_tag_message(char **tagmsg, const char *commit_id_str,
3728 const char *tag_name, const char *repo_path)
3730 const struct got_error *err = NULL;
3731 char *template = NULL, *initial_content = NULL;
3732 char *tagmsg_path = NULL, *editor = NULL;
3735 if (asprintf(&template, "/tmp/got-tagmsg") == -1) {
3736 err = got_error_from_errno("asprintf");
3740 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
3741 commit_id_str, tag_name) == -1) {
3742 err = got_error_from_errno("asprintf");
3746 err = got_opentemp_named_fd(&tagmsg_path, &fd, template);
3750 dprintf(fd, initial_content);
3753 err = get_editor(&editor);
3756 err = edit_logmsg(tagmsg, editor, tagmsg_path, initial_content);
3758 if (err == NULL || err->code == GOT_ERR_COMMIT_MSG_EMPTY) {
3759 unlink(tagmsg_path);
3763 free(initial_content);
3767 /* Editor is done; we can now apply unveil(2) */
3769 err = apply_unveil(repo_path, 0, NULL);
3778 static const struct got_error *
3779 add_tag(struct got_repository *repo, const char *tag_name,
3780 const char *commit_arg, const char *tagmsg_arg)
3782 const struct got_error *err = NULL;
3783 struct got_object_id *commit_id = NULL, *tag_id = NULL;
3784 char *label = NULL, *commit_id_str = NULL;
3785 struct got_reference *ref = NULL;
3786 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
3789 * Don't let the user create a tag named '-'.
3790 * While technically a valid reference name, this case is usually
3791 * an unintended typo.
3793 if (tag_name[0] == '-' && tag_name[1] == '\0')
3794 return got_error(GOT_ERR_BAD_REF_NAME);
3796 err = get_author(&tagger, repo);
3800 err = match_object_id(&commit_id, &label, commit_arg,
3801 GOT_OBJ_TYPE_COMMIT, 1, repo);
3805 err = got_object_id_str(&commit_id_str, commit_id);
3809 if (strncmp("refs/tags/", tag_name, 10) == 0) {
3810 refname = strdup(tag_name);
3811 if (refname == NULL) {
3812 err = got_error_from_errno("strdup");
3816 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
3817 err = got_error_from_errno("asprintf");
3821 err = got_ref_open(&ref, repo, refname, 0);
3823 err = got_error(GOT_ERR_TAG_EXISTS);
3825 } else if (err->code != GOT_ERR_NOT_REF)
3828 if (tagmsg_arg == NULL) {
3829 err = get_tag_message(&tagmsg, commit_id_str,
3830 tag_name, got_repo_get_path(repo));
3835 err = got_object_tag_create(&tag_id, tag_name, commit_id,
3836 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
3840 err = got_ref_alloc(&ref, refname, tag_id);
3844 err = got_ref_write(ref, repo);
3848 err = got_object_id_str(&tag_id_str, tag_id);
3849 printf("Created tag %s\n", tag_id_str);
3856 free(commit_id_str);
3863 static const struct got_error *
3864 cmd_tag(int argc, char *argv[])
3866 const struct got_error *error = NULL;
3867 struct got_repository *repo = NULL;
3868 struct got_worktree *worktree = NULL;
3869 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
3870 char *gitconfig_path = NULL;
3871 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
3872 int ch, do_list = 0;
3874 while ((ch = getopt(argc, argv, "m:r:l")) != -1) {
3880 repo_path = realpath(optarg, NULL);
3881 if (repo_path == NULL)
3882 err(1, "-r option");
3883 got_path_strip_trailing_slashes(repo_path);
3899 errx(1, "-l and -m options are mutually exclusive\n");
3902 } else if (argc < 1 || argc > 2)
3905 commit_id_arg = argv[1];
3910 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3914 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3915 "sendfd unveil", NULL) == -1)
3919 cwd = getcwd(NULL, 0);
3921 error = got_error_from_errno("getcwd");
3925 if (repo_path == NULL) {
3926 error = got_worktree_open(&worktree, cwd);
3927 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3933 strdup(got_worktree_get_repo_path(worktree));
3934 if (repo_path == NULL)
3935 error = got_error_from_errno("strdup");
3939 repo_path = strdup(cwd);
3940 if (repo_path == NULL) {
3941 error = got_error_from_errno("strdup");
3948 error = got_repo_open(&repo, repo_path, NULL);
3951 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3954 error = list_tags(repo, worktree);
3956 error = get_gitconfig_path(&gitconfig_path);
3959 error = got_repo_open(&repo, repo_path, gitconfig_path);
3964 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3969 if (commit_id_arg == NULL) {
3970 struct got_reference *head_ref;
3971 struct got_object_id *commit_id;
3972 error = got_ref_open(&head_ref, repo,
3973 worktree ? got_worktree_get_head_ref_name(worktree)
3977 error = got_ref_resolve(&commit_id, repo, head_ref);
3978 got_ref_close(head_ref);
3981 error = got_object_id_str(&commit_id_str, commit_id);
3987 error = add_tag(repo, tag_name,
3988 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
3992 got_repo_close(repo);
3994 got_worktree_close(worktree);
3997 free(gitconfig_path);
3998 free(commit_id_str);
4005 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
4009 static const struct got_error *
4010 cmd_add(int argc, char *argv[])
4012 const struct got_error *error = NULL;
4013 struct got_repository *repo = NULL;
4014 struct got_worktree *worktree = NULL;
4016 struct got_pathlist_head paths;
4017 struct got_pathlist_entry *pe;
4022 while ((ch = getopt(argc, argv, "")) != -1) {
4034 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4041 cwd = getcwd(NULL, 0);
4043 error = got_error_from_errno("getcwd");
4047 error = got_worktree_open(&worktree, cwd);
4051 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4056 error = apply_unveil(got_repo_get_path(repo), 1,
4057 got_worktree_get_root_path(worktree));
4061 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4065 error = got_worktree_schedule_add(worktree, &paths, print_status,
4069 got_repo_close(repo);
4071 got_worktree_close(worktree);
4072 TAILQ_FOREACH(pe, &paths, entry)
4073 free((char *)pe->path);
4074 got_pathlist_free(&paths);
4082 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
4086 static const struct got_error *
4087 print_remove_status(void *arg, unsigned char status,
4088 unsigned char staged_status, const char *path,
4089 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4090 struct got_object_id *commit_id)
4092 if (status == GOT_STATUS_NONEXISTENT)
4094 if (status == staged_status && (status == GOT_STATUS_DELETE))
4095 status = GOT_STATUS_NO_CHANGE;
4096 printf("%c%c %s\n", status, staged_status, path);
4100 static const struct got_error *
4101 cmd_remove(int argc, char *argv[])
4103 const struct got_error *error = NULL;
4104 struct got_worktree *worktree = NULL;
4105 struct got_repository *repo = NULL;
4107 struct got_pathlist_head paths;
4108 struct got_pathlist_entry *pe;
4109 int ch, delete_local_mods = 0;
4113 while ((ch = getopt(argc, argv, "f")) != -1) {
4116 delete_local_mods = 1;
4128 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4135 cwd = getcwd(NULL, 0);
4137 error = got_error_from_errno("getcwd");
4140 error = got_worktree_open(&worktree, cwd);
4144 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4149 error = apply_unveil(got_repo_get_path(repo), 1,
4150 got_worktree_get_root_path(worktree));
4154 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4158 error = got_worktree_schedule_delete(worktree, &paths,
4159 delete_local_mods, print_remove_status, NULL, repo);
4164 got_repo_close(repo);
4166 got_worktree_close(worktree);
4167 TAILQ_FOREACH(pe, &paths, entry)
4168 free((char *)pe->path);
4169 got_pathlist_free(&paths);
4177 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4178 "path ...\n", getprogname());
4182 static const struct got_error *
4183 revert_progress(void *arg, unsigned char status, const char *path)
4185 while (path[0] == '/')
4187 printf("%c %s\n", status, path);
4191 struct choose_patch_arg {
4192 FILE *patch_script_file;
4196 static const struct got_error *
4197 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4198 int nchanges, const char *action)
4201 size_t linesize = 0;
4205 case GOT_STATUS_ADD:
4206 printf("A %s\n%s this addition? [y/n] ", path, action);
4208 case GOT_STATUS_DELETE:
4209 printf("D %s\n%s this deletion? [y/n] ", path, action);
4211 case GOT_STATUS_MODIFY:
4212 if (fseek(patch_file, 0L, SEEK_SET) == -1)
4213 return got_error_from_errno("fseek");
4214 printf(GOT_COMMIT_SEP_STR);
4215 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4217 if (ferror(patch_file))
4218 return got_error_from_errno("getline");
4219 printf(GOT_COMMIT_SEP_STR);
4220 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4221 path, n, nchanges, action);
4224 return got_error_path(path, GOT_ERR_FILE_STATUS);
4230 static const struct got_error *
4231 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4232 FILE *patch_file, int n, int nchanges)
4234 const struct got_error *err = NULL;
4236 size_t linesize = 0;
4239 struct choose_patch_arg *a = arg;
4241 *choice = GOT_PATCH_CHOICE_NONE;
4243 if (a->patch_script_file) {
4245 err = show_change(status, path, patch_file, n, nchanges,
4249 linelen = getline(&line, &linesize, a->patch_script_file);
4250 if (linelen == -1) {
4251 if (ferror(a->patch_script_file))
4252 return got_error_from_errno("getline");
4255 nl = strchr(line, '\n');
4258 if (strcmp(line, "y") == 0) {
4259 *choice = GOT_PATCH_CHOICE_YES;
4261 } else if (strcmp(line, "n") == 0) {
4262 *choice = GOT_PATCH_CHOICE_NO;
4264 } else if (strcmp(line, "q") == 0 &&
4265 status == GOT_STATUS_MODIFY) {
4266 *choice = GOT_PATCH_CHOICE_QUIT;
4269 printf("invalid response '%s'\n", line);
4274 while (resp != 'y' && resp != 'n' && resp != 'q') {
4275 err = show_change(status, path, patch_file, n, nchanges,
4282 if (status == GOT_STATUS_MODIFY) {
4283 if (resp != 'y' && resp != 'n' && resp != 'q') {
4284 printf("invalid response '%c'\n", resp);
4287 } else if (resp != 'y' && resp != 'n') {
4288 printf("invalid response '%c'\n", resp);
4294 *choice = GOT_PATCH_CHOICE_YES;
4295 else if (resp == 'n')
4296 *choice = GOT_PATCH_CHOICE_NO;
4297 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4298 *choice = GOT_PATCH_CHOICE_QUIT;
4304 static const struct got_error *
4305 cmd_revert(int argc, char *argv[])
4307 const struct got_error *error = NULL;
4308 struct got_worktree *worktree = NULL;
4309 struct got_repository *repo = NULL;
4310 char *cwd = NULL, *path = NULL;
4311 struct got_pathlist_head paths;
4312 struct got_pathlist_entry *pe;
4313 int ch, can_recurse = 0, pflag = 0;
4314 FILE *patch_script_file = NULL;
4315 const char *patch_script_path = NULL;
4316 struct choose_patch_arg cpa;
4320 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4326 patch_script_path = optarg;
4341 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4342 "unveil", NULL) == -1)
4347 if (patch_script_path && !pflag)
4348 errx(1, "-F option can only be used together with -p option");
4350 cwd = getcwd(NULL, 0);
4352 error = got_error_from_errno("getcwd");
4355 error = got_worktree_open(&worktree, cwd);
4359 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4364 if (patch_script_path) {
4365 patch_script_file = fopen(patch_script_path, "r");
4366 if (patch_script_file == NULL) {
4367 error = got_error_from_errno2("fopen",
4372 error = apply_unveil(got_repo_get_path(repo), 1,
4373 got_worktree_get_root_path(worktree));
4377 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4384 TAILQ_FOREACH(pe, &paths, entry) {
4385 if (asprintf(&ondisk_path, "%s/%s",
4386 got_worktree_get_root_path(worktree),
4388 error = got_error_from_errno("asprintf");
4391 if (lstat(ondisk_path, &sb) == -1) {
4392 if (errno == ENOENT) {
4396 error = got_error_from_errno2("lstat",
4402 if (S_ISDIR(sb.st_mode)) {
4403 error = got_error_msg(GOT_ERR_BAD_PATH,
4404 "reverting directories requires -R option");
4410 cpa.patch_script_file = patch_script_file;
4411 cpa.action = "revert";
4412 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4413 pflag ? choose_patch : NULL, &cpa, repo);
4417 if (patch_script_file && fclose(patch_script_file) == EOF &&
4419 error = got_error_from_errno2("fclose", patch_script_path);
4421 got_repo_close(repo);
4423 got_worktree_close(worktree);
4432 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4437 struct collect_commit_logmsg_arg {
4438 const char *cmdline_log;
4440 const char *worktree_path;
4441 const char *branch_name;
4442 const char *repo_path;
4447 static const struct got_error *
4448 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4451 char *initial_content = NULL;
4452 struct got_pathlist_entry *pe;
4453 const struct got_error *err = NULL;
4454 char *template = NULL;
4455 struct collect_commit_logmsg_arg *a = arg;
4459 /* if a message was specified on the command line, just use it */
4460 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4461 len = strlen(a->cmdline_log) + 1;
4462 *logmsg = malloc(len + 1);
4463 if (*logmsg == NULL)
4464 return got_error_from_errno("malloc");
4465 strlcpy(*logmsg, a->cmdline_log, len);
4469 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
4470 return got_error_from_errno("asprintf");
4472 if (asprintf(&initial_content,
4473 "\n# changes to be committed on branch %s:\n",
4474 a->branch_name) == -1)
4475 return got_error_from_errno("asprintf");
4477 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
4481 dprintf(fd, initial_content);
4483 TAILQ_FOREACH(pe, commitable_paths, entry) {
4484 struct got_commitable *ct = pe->data;
4485 dprintf(fd, "# %c %s\n",
4486 got_commitable_get_status(ct),
4487 got_commitable_get_path(ct));
4491 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
4493 if (err == NULL || err->code == GOT_ERR_COMMIT_MSG_EMPTY) {
4494 unlink(a->logmsg_path);
4495 free(a->logmsg_path);
4496 a->logmsg_path = NULL;
4498 free(initial_content);
4501 /* Editor is done; we can now apply unveil(2) */
4503 err = apply_unveil(a->repo_path, 0, a->worktree_path);
4512 static const struct got_error *
4513 cmd_commit(int argc, char *argv[])
4515 const struct got_error *error = NULL;
4516 struct got_worktree *worktree = NULL;
4517 struct got_repository *repo = NULL;
4518 char *cwd = NULL, *id_str = NULL;
4519 struct got_object_id *id = NULL;
4520 const char *logmsg = NULL;
4521 struct collect_commit_logmsg_arg cl_arg;
4522 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
4523 int ch, rebase_in_progress, histedit_in_progress;
4524 struct got_pathlist_head paths;
4527 cl_arg.logmsg_path = NULL;
4529 while ((ch = getopt(argc, argv, "m:")) != -1) {
4544 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4545 "unveil", NULL) == -1)
4548 cwd = getcwd(NULL, 0);
4550 error = got_error_from_errno("getcwd");
4553 error = got_worktree_open(&worktree, cwd);
4557 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4560 if (rebase_in_progress) {
4561 error = got_error(GOT_ERR_REBASING);
4565 error = got_worktree_histedit_in_progress(&histedit_in_progress,
4570 error = get_gitconfig_path(&gitconfig_path);
4573 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4578 error = get_author(&author, repo);
4583 * unveil(2) traverses exec(2); if an editor is used we have
4584 * to apply unveil after the log message has been written.
4586 if (logmsg == NULL || strlen(logmsg) == 0)
4587 error = get_editor(&editor);
4589 error = apply_unveil(got_repo_get_path(repo), 0,
4590 got_worktree_get_root_path(worktree));
4594 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4598 cl_arg.editor = editor;
4599 cl_arg.cmdline_log = logmsg;
4600 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
4601 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
4602 if (!histedit_in_progress) {
4603 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
4604 error = got_error(GOT_ERR_COMMIT_BRANCH);
4607 cl_arg.branch_name += 11;
4609 cl_arg.repo_path = got_repo_get_path(repo);
4610 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
4611 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
4613 if (cl_arg.logmsg_path)
4614 fprintf(stderr, "%s: log message preserved in %s\n",
4615 getprogname(), cl_arg.logmsg_path);
4619 if (cl_arg.logmsg_path)
4620 unlink(cl_arg.logmsg_path);
4622 error = got_object_id_str(&id_str, id);
4625 printf("Created commit %s\n", id_str);
4627 free(cl_arg.logmsg_path);
4629 got_repo_close(repo);
4631 got_worktree_close(worktree);
4634 free(gitconfig_path);
4641 usage_cherrypick(void)
4643 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
4647 static const struct got_error *
4648 cmd_cherrypick(int argc, char *argv[])
4650 const struct got_error *error = NULL;
4651 struct got_worktree *worktree = NULL;
4652 struct got_repository *repo = NULL;
4653 char *cwd = NULL, *commit_id_str = NULL;
4654 struct got_object_id *commit_id = NULL;
4655 struct got_commit_object *commit = NULL;
4656 struct got_object_qid *pid;
4657 struct got_reference *head_ref = NULL;
4658 int ch, did_something = 0;
4660 while ((ch = getopt(argc, argv, "")) != -1) {
4672 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4673 "unveil", NULL) == -1)
4679 cwd = getcwd(NULL, 0);
4681 error = got_error_from_errno("getcwd");
4684 error = got_worktree_open(&worktree, cwd);
4688 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4693 error = apply_unveil(got_repo_get_path(repo), 0,
4694 got_worktree_get_root_path(worktree));
4698 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4699 GOT_OBJ_TYPE_COMMIT, repo);
4700 if (error != NULL) {
4701 struct got_reference *ref;
4702 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4704 error = got_ref_open(&ref, repo, argv[0], 0);
4707 error = got_ref_resolve(&commit_id, repo, ref);
4712 error = got_object_id_str(&commit_id_str, commit_id);
4716 error = got_ref_open(&head_ref, repo,
4717 got_worktree_get_head_ref_name(worktree), 0);
4721 error = check_same_branch(commit_id, head_ref, NULL, repo);
4723 if (error->code != GOT_ERR_ANCESTRY)
4727 error = got_error(GOT_ERR_SAME_BRANCH);
4731 error = got_object_open_as_commit(&commit, repo, commit_id);
4734 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4735 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
4736 commit_id, repo, update_progress, &did_something, check_cancelled,
4742 printf("Merged commit %s\n", commit_id_str);
4745 got_object_commit_close(commit);
4746 free(commit_id_str);
4748 got_ref_close(head_ref);
4750 got_worktree_close(worktree);
4752 got_repo_close(repo);
4759 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
4763 static const struct got_error *
4764 cmd_backout(int argc, char *argv[])
4766 const struct got_error *error = NULL;
4767 struct got_worktree *worktree = NULL;
4768 struct got_repository *repo = NULL;
4769 char *cwd = NULL, *commit_id_str = NULL;
4770 struct got_object_id *commit_id = NULL;
4771 struct got_commit_object *commit = NULL;
4772 struct got_object_qid *pid;
4773 struct got_reference *head_ref = NULL;
4774 int ch, did_something = 0;
4776 while ((ch = getopt(argc, argv, "")) != -1) {
4788 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4789 "unveil", NULL) == -1)
4795 cwd = getcwd(NULL, 0);
4797 error = got_error_from_errno("getcwd");
4800 error = got_worktree_open(&worktree, cwd);
4804 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4809 error = apply_unveil(got_repo_get_path(repo), 0,
4810 got_worktree_get_root_path(worktree));
4814 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4815 GOT_OBJ_TYPE_COMMIT, repo);
4816 if (error != NULL) {
4817 struct got_reference *ref;
4818 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4820 error = got_ref_open(&ref, repo, argv[0], 0);
4823 error = got_ref_resolve(&commit_id, repo, ref);
4828 error = got_object_id_str(&commit_id_str, commit_id);
4832 error = got_ref_open(&head_ref, repo,
4833 got_worktree_get_head_ref_name(worktree), 0);
4837 error = check_same_branch(commit_id, head_ref, NULL, repo);
4841 error = got_object_open_as_commit(&commit, repo, commit_id);
4844 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4846 error = got_error(GOT_ERR_ROOT_COMMIT);
4850 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
4851 update_progress, &did_something, check_cancelled, NULL);
4856 printf("Backed out commit %s\n", commit_id_str);
4859 got_object_commit_close(commit);
4860 free(commit_id_str);
4862 got_ref_close(head_ref);
4864 got_worktree_close(worktree);
4866 got_repo_close(repo);
4873 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
4879 trim_logmsg(char *logmsg, int limit)
4884 len = strlen(logmsg);
4888 nl = strchr(logmsg, '\n');
4893 static const struct got_error *
4894 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
4896 const struct got_error *err;
4897 char *logmsg0 = NULL;
4900 err = got_object_commit_get_logmsg(&logmsg0, commit);
4905 while (isspace((unsigned char)s[0]))
4908 *logmsg = strdup(s);
4909 if (*logmsg == NULL) {
4910 err = got_error_from_errno("strdup");
4914 trim_logmsg(*logmsg, limit);
4920 static const struct got_error *
4921 show_rebase_progress(struct got_commit_object *commit,
4922 struct got_object_id *old_id, struct got_object_id *new_id)
4924 const struct got_error *err;
4925 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
4927 err = got_object_id_str(&old_id_str, old_id);
4932 err = got_object_id_str(&new_id_str, new_id);
4937 old_id_str[12] = '\0';
4939 new_id_str[12] = '\0';
4941 err = get_short_logmsg(&logmsg, 42, commit);
4945 printf("%s -> %s: %s\n", old_id_str,
4946 new_id_str ? new_id_str : "no-op change", logmsg);
4953 static const struct got_error *
4954 rebase_progress(void *arg, unsigned char status, const char *path)
4956 unsigned char *rebase_status = arg;
4958 while (path[0] == '/')
4960 printf("%c %s\n", status, path);
4962 if (*rebase_status == GOT_STATUS_CONFLICT)
4964 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
4965 *rebase_status = status;
4969 static const struct got_error *
4970 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
4971 struct got_reference *branch, struct got_reference *new_base_branch,
4972 struct got_reference *tmp_branch, struct got_repository *repo)
4974 printf("Switching work tree to %s\n", got_ref_get_name(branch));
4975 return got_worktree_rebase_complete(worktree, fileindex,
4976 new_base_branch, tmp_branch, branch, repo);
4979 static const struct got_error *
4980 rebase_commit(struct got_pathlist_head *merged_paths,
4981 struct got_worktree *worktree, struct got_fileindex *fileindex,
4982 struct got_reference *tmp_branch,
4983 struct got_object_id *commit_id, struct got_repository *repo)
4985 const struct got_error *error;
4986 struct got_commit_object *commit;
4987 struct got_object_id *new_commit_id;
4989 error = got_object_open_as_commit(&commit, repo, commit_id);
4993 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
4994 worktree, fileindex, tmp_branch, commit, commit_id, repo);
4996 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
4998 error = show_rebase_progress(commit, commit_id, NULL);
5000 error = show_rebase_progress(commit, commit_id, new_commit_id);
5001 free(new_commit_id);
5004 got_object_commit_close(commit);
5008 struct check_path_prefix_arg {
5009 const char *path_prefix;
5014 static const struct got_error *
5015 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5016 struct got_blob_object *blob2, struct got_object_id *id1,
5017 struct got_object_id *id2, const char *path1, const char *path2,
5018 struct got_repository *repo)
5020 struct check_path_prefix_arg *a = arg;
5022 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5023 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5024 return got_error(a->errcode);
5029 static const struct got_error *
5030 check_path_prefix(struct got_object_id *parent_id,
5031 struct got_object_id *commit_id, const char *path_prefix,
5032 int errcode, struct got_repository *repo)
5034 const struct got_error *err;
5035 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
5036 struct got_commit_object *commit = NULL, *parent_commit = NULL;
5037 struct check_path_prefix_arg cpp_arg;
5039 if (got_path_is_root_dir(path_prefix))
5042 err = got_object_open_as_commit(&commit, repo, commit_id);
5046 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
5050 err = got_object_open_as_tree(&tree1, repo,
5051 got_object_commit_get_tree_id(parent_commit));
5055 err = got_object_open_as_tree(&tree2, repo,
5056 got_object_commit_get_tree_id(commit));
5060 cpp_arg.path_prefix = path_prefix;
5061 while (cpp_arg.path_prefix[0] == '/')
5062 cpp_arg.path_prefix++;
5063 cpp_arg.len = strlen(cpp_arg.path_prefix);
5064 cpp_arg.errcode = errcode;
5065 err = got_diff_tree(tree1, tree2, "", "", repo,
5066 check_path_prefix_in_diff, &cpp_arg, 0);
5069 got_object_tree_close(tree1);
5071 got_object_tree_close(tree2);
5073 got_object_commit_close(commit);
5075 got_object_commit_close(parent_commit);
5079 static const struct got_error *
5080 collect_commits(struct got_object_id_queue *commits,
5081 struct got_object_id *initial_commit_id,
5082 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
5083 const char *path_prefix, int path_prefix_errcode,
5084 struct got_repository *repo)
5086 const struct got_error *err = NULL;
5087 struct got_commit_graph *graph = NULL;
5088 struct got_object_id *parent_id = NULL;
5089 struct got_object_qid *qid;
5090 struct got_object_id *commit_id = initial_commit_id;
5092 err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
5096 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
5097 check_cancelled, NULL);
5100 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
5101 err = got_commit_graph_iter_next(&parent_id, graph);
5103 if (err->code == GOT_ERR_ITER_COMPLETED) {
5104 err = got_error_msg(GOT_ERR_ANCESTRY,
5105 "ran out of commits to rebase before "
5106 "youngest common ancestor commit has "
5109 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
5111 err = got_commit_graph_fetch_commits(graph, 1, repo,
5112 check_cancelled, NULL);
5116 err = check_path_prefix(parent_id, commit_id,
5117 path_prefix, path_prefix_errcode, repo);
5121 err = got_object_qid_alloc(&qid, commit_id);
5124 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
5125 commit_id = parent_id;
5129 got_commit_graph_close(graph);
5133 static const struct got_error *
5134 cmd_rebase(int argc, char *argv[])
5136 const struct got_error *error = NULL;
5137 struct got_worktree *worktree = NULL;
5138 struct got_repository *repo = NULL;
5139 struct got_fileindex *fileindex = NULL;
5141 struct got_reference *branch = NULL;
5142 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
5143 struct got_object_id *commit_id = NULL, *parent_id = NULL;
5144 struct got_object_id *resume_commit_id = NULL;
5145 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
5146 struct got_commit_object *commit = NULL;
5147 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
5148 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5149 struct got_object_id_queue commits;
5150 struct got_pathlist_head merged_paths;
5151 const struct got_object_id_queue *parent_ids;
5152 struct got_object_qid *qid, *pid;
5154 SIMPLEQ_INIT(&commits);
5155 TAILQ_INIT(&merged_paths);
5157 while ((ch = getopt(argc, argv, "ac")) != -1) {
5163 continue_rebase = 1;
5175 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5176 "unveil", NULL) == -1)
5179 if (abort_rebase && continue_rebase)
5181 else if (abort_rebase || continue_rebase) {
5184 } else if (argc != 1)
5187 cwd = getcwd(NULL, 0);
5189 error = got_error_from_errno("getcwd");
5192 error = got_worktree_open(&worktree, cwd);
5196 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5201 error = apply_unveil(got_repo_get_path(repo), 0,
5202 got_worktree_get_root_path(worktree));
5206 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5212 if (!rebase_in_progress) {
5213 error = got_error(GOT_ERR_NOT_REBASING);
5216 error = got_worktree_rebase_continue(&resume_commit_id,
5217 &new_base_branch, &tmp_branch, &branch, &fileindex,
5221 printf("Switching work tree to %s\n",
5222 got_ref_get_symref_target(new_base_branch));
5223 error = got_worktree_rebase_abort(worktree, fileindex, repo,
5224 new_base_branch, update_progress, &did_something);
5227 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5228 goto done; /* nothing else to do */
5231 if (continue_rebase) {
5232 if (!rebase_in_progress) {
5233 error = got_error(GOT_ERR_NOT_REBASING);
5236 error = got_worktree_rebase_continue(&resume_commit_id,
5237 &new_base_branch, &tmp_branch, &branch, &fileindex,
5242 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5243 resume_commit_id, repo);
5247 yca_id = got_object_id_dup(resume_commit_id);
5248 if (yca_id == NULL) {
5249 error = got_error_from_errno("got_object_id_dup");
5253 error = got_ref_open(&branch, repo, argv[0], 0);
5258 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5262 if (!continue_rebase) {
5263 struct got_object_id *base_commit_id;
5265 base_commit_id = got_worktree_get_base_commit_id(worktree);
5266 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5267 base_commit_id, branch_head_commit_id, repo,
5268 check_cancelled, NULL);
5271 if (yca_id == NULL) {
5272 error = got_error_msg(GOT_ERR_ANCESTRY,
5273 "specified branch shares no common ancestry "
5274 "with work tree's branch");
5278 error = check_same_branch(base_commit_id, branch, yca_id, repo);
5280 if (error->code != GOT_ERR_ANCESTRY)
5284 error = got_error_msg(GOT_ERR_SAME_BRANCH,
5285 "specified branch resolves to a commit which "
5286 "is already contained in work tree's branch");
5289 error = got_worktree_rebase_prepare(&new_base_branch,
5290 &tmp_branch, &fileindex, worktree, branch, repo);
5295 commit_id = branch_head_commit_id;
5296 error = got_object_open_as_commit(&commit, repo, commit_id);
5300 parent_ids = got_object_commit_get_parent_ids(commit);
5301 pid = SIMPLEQ_FIRST(parent_ids);
5303 if (!continue_rebase) {
5305 error = got_worktree_rebase_abort(worktree, fileindex,
5306 repo, new_base_branch, update_progress,
5310 printf("Rebase of %s aborted\n",
5311 got_ref_get_name(branch));
5313 error = got_error(GOT_ERR_EMPTY_REBASE);
5316 error = collect_commits(&commits, commit_id, pid->id,
5317 yca_id, got_worktree_get_path_prefix(worktree),
5318 GOT_ERR_REBASE_PATH, repo);
5319 got_object_commit_close(commit);
5324 if (SIMPLEQ_EMPTY(&commits)) {
5325 if (continue_rebase)
5326 error = rebase_complete(worktree, fileindex,
5327 branch, new_base_branch, tmp_branch, repo);
5329 error = got_error(GOT_ERR_EMPTY_REBASE);
5334 SIMPLEQ_FOREACH(qid, &commits, entry) {
5335 commit_id = qid->id;
5336 parent_id = pid ? pid->id : yca_id;
5339 error = got_worktree_rebase_merge_files(&merged_paths,
5340 worktree, fileindex, parent_id, commit_id, repo,
5341 rebase_progress, &rebase_status, check_cancelled, NULL);
5345 if (rebase_status == GOT_STATUS_CONFLICT) {
5346 got_worktree_rebase_pathlist_free(&merged_paths);
5350 error = rebase_commit(&merged_paths, worktree, fileindex,
5351 tmp_branch, commit_id, repo);
5352 got_worktree_rebase_pathlist_free(&merged_paths);
5357 if (rebase_status == GOT_STATUS_CONFLICT) {
5358 error = got_worktree_rebase_postpone(worktree, fileindex);
5361 error = got_error_msg(GOT_ERR_CONFLICTS,
5362 "conflicts must be resolved before rebasing can continue");
5364 error = rebase_complete(worktree, fileindex, branch,
5365 new_base_branch, tmp_branch, repo);
5367 got_object_id_queue_free(&commits);
5368 free(branch_head_commit_id);
5369 free(resume_commit_id);
5372 got_object_commit_close(commit);
5374 got_ref_close(branch);
5375 if (new_base_branch)
5376 got_ref_close(new_base_branch);
5378 got_ref_close(tmp_branch);
5380 got_worktree_close(worktree);
5382 got_repo_close(repo);
5387 usage_histedit(void)
5389 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script]\n",
5394 #define GOT_HISTEDIT_PICK 'p'
5395 #define GOT_HISTEDIT_EDIT 'e'
5396 #define GOT_HISTEDIT_FOLD 'f'
5397 #define GOT_HISTEDIT_DROP 'd'
5398 #define GOT_HISTEDIT_MESG 'm'
5400 static struct got_histedit_cmd {
5404 } got_histedit_cmds[] = {
5405 { GOT_HISTEDIT_PICK, "pick", "use commit" },
5406 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5407 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
5408 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5409 { GOT_HISTEDIT_MESG, "mesg",
5410 "single-line log message for commit above (open editor if empty)" },
5413 struct got_histedit_list_entry {
5414 TAILQ_ENTRY(got_histedit_list_entry) entry;
5415 struct got_object_id *commit_id;
5416 const struct got_histedit_cmd *cmd;
5419 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5421 static const struct got_error *
5422 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5423 FILE *f, struct got_repository *repo)
5425 const struct got_error *err = NULL;
5426 char *logmsg = NULL, *id_str = NULL;
5427 struct got_commit_object *commit = NULL;
5430 err = got_object_open_as_commit(&commit, repo, commit_id);
5434 err = get_short_logmsg(&logmsg, 34, commit);
5438 err = got_object_id_str(&id_str, commit_id);
5442 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
5444 err = got_ferror(f, GOT_ERR_IO);
5447 got_object_commit_close(commit);
5453 static const struct got_error *
5454 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
5455 struct got_repository *repo)
5457 const struct got_error *err = NULL;
5458 struct got_object_qid *qid;
5460 if (SIMPLEQ_EMPTY(commits))
5461 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5463 SIMPLEQ_FOREACH(qid, commits, entry) {
5464 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
5473 static const struct got_error *
5474 write_cmd_list(FILE *f)
5476 const struct got_error *err = NULL;
5479 n = fprintf(f, "# Available histedit commands:\n");
5481 return got_ferror(f, GOT_ERR_IO);
5483 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5484 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
5485 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
5488 err = got_ferror(f, GOT_ERR_IO);
5492 n = fprintf(f, "# Commits will be processed in order from top to "
5493 "bottom of this file.\n");
5495 return got_ferror(f, GOT_ERR_IO);
5499 static const struct got_error *
5500 histedit_syntax_error(int lineno)
5502 static char msg[42];
5505 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
5507 if (ret == -1 || ret >= sizeof(msg))
5508 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
5510 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
5513 static const struct got_error *
5514 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
5515 char *logmsg, struct got_repository *repo)
5517 const struct got_error *err;
5518 struct got_commit_object *folded_commit = NULL;
5519 char *id_str, *folded_logmsg = NULL;
5521 err = got_object_id_str(&id_str, hle->commit_id);
5525 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
5529 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
5532 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
5533 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
5534 folded_logmsg) == -1) {
5535 err = got_error_from_errno("asprintf");
5540 got_object_commit_close(folded_commit);
5542 free(folded_logmsg);
5546 static struct got_histedit_list_entry *
5547 get_folded_commits(struct got_histedit_list_entry *hle)
5549 struct got_histedit_list_entry *prev, *folded = NULL;
5551 prev = TAILQ_PREV(hle, got_histedit_list, entry);
5552 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
5553 prev->cmd->code == GOT_HISTEDIT_DROP)) {
5554 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
5556 prev = TAILQ_PREV(prev, got_histedit_list, entry);
5562 static const struct got_error *
5563 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
5564 struct got_repository *repo)
5566 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
5567 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
5568 const struct got_error *err = NULL;
5569 struct got_commit_object *commit = NULL;
5571 struct got_histedit_list_entry *folded = NULL;
5573 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5577 folded = get_folded_commits(hle);
5579 while (folded != hle) {
5580 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
5581 folded = TAILQ_NEXT(folded, entry);
5584 err = append_folded_commit_msg(&new_msg, folded,
5590 folded = TAILQ_NEXT(folded, entry);
5594 err = got_object_id_str(&id_str, hle->commit_id);
5597 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
5600 if (asprintf(&new_msg,
5601 "%s\n# original log message of commit %s: %s",
5602 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
5603 err = got_error_from_errno("asprintf");
5609 err = got_object_id_str(&id_str, hle->commit_id);
5613 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
5617 dprintf(fd, logmsg);
5620 err = get_editor(&editor);
5624 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
5626 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
5628 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
5631 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
5632 err = got_error_from_errno2("unlink", logmsg_path);
5638 got_object_commit_close(commit);
5642 static const struct got_error *
5643 histedit_parse_list(struct got_histedit_list *histedit_cmds,
5644 FILE *f, struct got_repository *repo)
5646 const struct got_error *err = NULL;
5647 char *line = NULL, *p, *end;
5651 const struct got_histedit_cmd *cmd;
5652 struct got_object_id *commit_id = NULL;
5653 struct got_histedit_list_entry *hle = NULL;
5656 len = getline(&line, &size, f);
5658 const struct got_error *getline_err;
5661 getline_err = got_error_from_errno("getline");
5662 err = got_ferror(f, getline_err->code);
5667 while (isspace((unsigned char)p[0]))
5669 if (p[0] == '#' || p[0] == '\0') {
5675 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5676 cmd = &got_histedit_cmds[i];
5677 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
5678 isspace((unsigned char)p[strlen(cmd->name)])) {
5679 p += strlen(cmd->name);
5682 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
5687 if (i == nitems(got_histedit_cmds)) {
5688 err = histedit_syntax_error(lineno);
5691 while (isspace((unsigned char)p[0]))
5693 if (cmd->code == GOT_HISTEDIT_MESG) {
5694 if (hle == NULL || hle->logmsg != NULL) {
5695 err = got_error(GOT_ERR_HISTEDIT_CMD);
5699 err = histedit_edit_logmsg(hle, repo);
5703 hle->logmsg = strdup(p);
5704 if (hle->logmsg == NULL) {
5705 err = got_error_from_errno("strdup");
5714 while (end[0] && !isspace((unsigned char)end[0]))
5718 err = got_object_resolve_id_str(&commit_id, repo, p);
5720 /* override error code */
5721 err = histedit_syntax_error(lineno);
5725 hle = malloc(sizeof(*hle));
5727 err = got_error_from_errno("malloc");
5731 hle->commit_id = commit_id;
5736 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
5744 static const struct got_error *
5745 histedit_check_script(struct got_histedit_list *histedit_cmds,
5746 struct got_object_id_queue *commits, struct got_repository *repo)
5748 const struct got_error *err = NULL;
5749 struct got_object_qid *qid;
5750 struct got_histedit_list_entry *hle;
5751 static char msg[80];
5754 if (TAILQ_EMPTY(histedit_cmds))
5755 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
5756 "histedit script contains no commands");
5757 if (SIMPLEQ_EMPTY(commits))
5758 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5760 SIMPLEQ_FOREACH(qid, commits, entry) {
5761 TAILQ_FOREACH(hle, histedit_cmds, entry) {
5762 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
5766 err = got_object_id_str(&id_str, qid->id);
5769 snprintf(msg, sizeof(msg),
5770 "commit %s missing from histedit script", id_str);
5772 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
5776 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
5777 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
5778 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
5779 "last commit in histedit script cannot be folded");
5784 static const struct got_error *
5785 histedit_run_editor(struct got_histedit_list *histedit_cmds,
5786 const char *path, struct got_object_id_queue *commits,
5787 struct got_repository *repo)
5789 const struct got_error *err = NULL;
5793 err = get_editor(&editor);
5797 if (spawn_editor(editor, path) == -1) {
5798 err = got_error_from_errno("failed spawning editor");
5802 f = fopen(path, "r");
5804 err = got_error_from_errno("fopen");
5807 err = histedit_parse_list(histedit_cmds, f, repo);
5811 err = histedit_check_script(histedit_cmds, commits, repo);
5813 if (f && fclose(f) != 0 && err == NULL)
5814 err = got_error_from_errno("fclose");
5819 static const struct got_error *
5820 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
5821 struct got_object_id_queue *, const char *, struct got_repository *);
5823 static const struct got_error *
5824 histedit_edit_script(struct got_histedit_list *histedit_cmds,
5825 struct got_object_id_queue *commits, struct got_repository *repo)
5827 const struct got_error *err;
5831 err = got_opentemp_named(&path, &f, "got-histedit");
5835 err = write_cmd_list(f);
5839 err = histedit_write_commit_list(commits, f, repo);
5843 if (fclose(f) != 0) {
5844 err = got_error_from_errno("fclose");
5849 err = histedit_run_editor(histedit_cmds, path, commits, repo);
5851 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5852 err->code != GOT_ERR_HISTEDIT_CMD)
5854 err = histedit_edit_list_retry(histedit_cmds, err,
5855 commits, path, repo);
5858 if (f && fclose(f) != 0 && err == NULL)
5859 err = got_error_from_errno("fclose");
5860 if (path && unlink(path) != 0 && err == NULL)
5861 err = got_error_from_errno2("unlink", path);
5866 static const struct got_error *
5867 histedit_save_list(struct got_histedit_list *histedit_cmds,
5868 struct got_worktree *worktree, struct got_repository *repo)
5870 const struct got_error *err = NULL;
5873 struct got_histedit_list_entry *hle;
5874 struct got_commit_object *commit = NULL;
5876 err = got_worktree_get_histedit_script_path(&path, worktree);
5880 f = fopen(path, "w");
5882 err = got_error_from_errno2("fopen", path);
5885 TAILQ_FOREACH(hle, histedit_cmds, entry) {
5886 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
5892 int n = fprintf(f, "%c %s\n",
5893 GOT_HISTEDIT_MESG, hle->logmsg);
5895 err = got_ferror(f, GOT_ERR_IO);
5901 if (f && fclose(f) != 0 && err == NULL)
5902 err = got_error_from_errno("fclose");
5905 got_object_commit_close(commit);
5910 histedit_free_list(struct got_histedit_list *histedit_cmds)
5912 struct got_histedit_list_entry *hle;
5914 while ((hle = TAILQ_FIRST(histedit_cmds))) {
5915 TAILQ_REMOVE(histedit_cmds, hle, entry);
5920 static const struct got_error *
5921 histedit_load_list(struct got_histedit_list *histedit_cmds,
5922 const char *path, struct got_repository *repo)
5924 const struct got_error *err = NULL;
5927 f = fopen(path, "r");
5929 err = got_error_from_errno2("fopen", path);
5933 err = histedit_parse_list(histedit_cmds, f, repo);
5935 if (f && fclose(f) != 0 && err == NULL)
5936 err = got_error_from_errno("fclose");
5940 static const struct got_error *
5941 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
5942 const struct got_error *edit_err, struct got_object_id_queue *commits,
5943 const char *path, struct got_repository *repo)
5945 const struct got_error *err = NULL, *prev_err = edit_err;
5948 while (resp != 'c' && resp != 'r' && resp != 'a') {
5949 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
5950 "or (a)bort: ", getprogname(), prev_err->msg);
5955 histedit_free_list(histedit_cmds);
5956 err = histedit_run_editor(histedit_cmds, path, commits,
5959 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5960 err->code != GOT_ERR_HISTEDIT_CMD)
5967 } else if (resp == 'r') {
5968 histedit_free_list(histedit_cmds);
5969 err = histedit_edit_script(histedit_cmds,
5972 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5973 err->code != GOT_ERR_HISTEDIT_CMD)
5980 } else if (resp == 'a') {
5981 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
5984 printf("invalid response '%c'\n", resp);
5990 static const struct got_error *
5991 histedit_complete(struct got_worktree *worktree,
5992 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5993 struct got_reference *branch, struct got_repository *repo)
5995 printf("Switching work tree to %s\n",
5996 got_ref_get_symref_target(branch));
5997 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
6001 static const struct got_error *
6002 show_histedit_progress(struct got_commit_object *commit,
6003 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
6005 const struct got_error *err;
6006 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6008 err = got_object_id_str(&old_id_str, hle->commit_id);
6013 err = got_object_id_str(&new_id_str, new_id);
6018 old_id_str[12] = '\0';
6020 new_id_str[12] = '\0';
6023 logmsg = strdup(hle->logmsg);
6024 if (logmsg == NULL) {
6025 err = got_error_from_errno("strdup");
6028 trim_logmsg(logmsg, 42);
6030 err = get_short_logmsg(&logmsg, 42, commit);
6035 switch (hle->cmd->code) {
6036 case GOT_HISTEDIT_PICK:
6037 case GOT_HISTEDIT_EDIT:
6038 printf("%s -> %s: %s\n", old_id_str,
6039 new_id_str ? new_id_str : "no-op change", logmsg);
6041 case GOT_HISTEDIT_DROP:
6042 case GOT_HISTEDIT_FOLD:
6043 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
6056 static const struct got_error *
6057 histedit_commit(struct got_pathlist_head *merged_paths,
6058 struct got_worktree *worktree, struct got_fileindex *fileindex,
6059 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
6060 struct got_repository *repo)
6062 const struct got_error *err;
6063 struct got_commit_object *commit;
6064 struct got_object_id *new_commit_id;
6066 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
6067 && hle->logmsg == NULL) {
6068 err = histedit_edit_logmsg(hle, repo);
6073 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6077 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
6078 worktree, fileindex, tmp_branch, commit, hle->commit_id,
6081 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
6083 err = show_histedit_progress(commit, hle, NULL);
6085 err = show_histedit_progress(commit, hle, new_commit_id);
6086 free(new_commit_id);
6089 got_object_commit_close(commit);
6093 static const struct got_error *
6094 histedit_skip_commit(struct got_histedit_list_entry *hle,
6095 struct got_worktree *worktree, struct got_repository *repo)
6097 const struct got_error *error;
6098 struct got_commit_object *commit;
6100 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
6105 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
6109 error = show_histedit_progress(commit, hle, NULL);
6110 got_object_commit_close(commit);
6114 static const struct got_error *
6115 cmd_histedit(int argc, char *argv[])
6117 const struct got_error *error = NULL;
6118 struct got_worktree *worktree = NULL;
6119 struct got_fileindex *fileindex = NULL;
6120 struct got_repository *repo = NULL;
6122 struct got_reference *branch = NULL;
6123 struct got_reference *tmp_branch = NULL;
6124 struct got_object_id *resume_commit_id = NULL;
6125 struct got_object_id *base_commit_id = NULL;
6126 struct got_object_id *head_commit_id = NULL;
6127 struct got_commit_object *commit = NULL;
6128 int ch, rebase_in_progress = 0, did_something;
6129 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
6130 const char *edit_script_path = NULL;
6131 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6132 struct got_object_id_queue commits;
6133 struct got_pathlist_head merged_paths;
6134 const struct got_object_id_queue *parent_ids;
6135 struct got_object_qid *pid;
6136 struct got_histedit_list histedit_cmds;
6137 struct got_histedit_list_entry *hle;
6139 SIMPLEQ_INIT(&commits);
6140 TAILQ_INIT(&histedit_cmds);
6141 TAILQ_INIT(&merged_paths);
6143 while ((ch = getopt(argc, argv, "acF:")) != -1) {
6152 edit_script_path = optarg;
6164 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6165 "unveil", NULL) == -1)
6168 if (abort_edit && continue_edit)
6174 * This command cannot apply unveil(2) in all cases because the
6175 * user may choose to run an editor to edit the histedit script
6176 * and to edit individual commit log messages.
6177 * unveil(2) traverses exec(2); if an editor is used we have to
6178 * apply unveil after edit script and log messages have been written.
6179 * XXX TODO: Make use of unveil(2) where possible.
6182 cwd = getcwd(NULL, 0);
6184 error = got_error_from_errno("getcwd");
6187 error = got_worktree_open(&worktree, cwd);
6191 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6196 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6199 if (rebase_in_progress) {
6200 error = got_error(GOT_ERR_REBASING);
6204 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6208 if (edit_in_progress && abort_edit) {
6209 error = got_worktree_histedit_continue(&resume_commit_id,
6210 &tmp_branch, &branch, &base_commit_id, &fileindex,
6214 printf("Switching work tree to %s\n",
6215 got_ref_get_symref_target(branch));
6216 error = got_worktree_histedit_abort(worktree, fileindex, repo,
6217 branch, base_commit_id, update_progress, &did_something);
6220 printf("Histedit of %s aborted\n",
6221 got_ref_get_symref_target(branch));
6222 goto done; /* nothing else to do */
6223 } else if (abort_edit) {
6224 error = got_error(GOT_ERR_NOT_HISTEDIT);
6228 if (continue_edit) {
6231 if (!edit_in_progress) {
6232 error = got_error(GOT_ERR_NOT_HISTEDIT);
6236 error = got_worktree_get_histedit_script_path(&path, worktree);
6240 error = histedit_load_list(&histedit_cmds, path, repo);
6245 error = got_worktree_histedit_continue(&resume_commit_id,
6246 &tmp_branch, &branch, &base_commit_id, &fileindex,
6251 error = got_ref_resolve(&head_commit_id, repo, branch);
6255 error = got_object_open_as_commit(&commit, repo,
6259 parent_ids = got_object_commit_get_parent_ids(commit);
6260 pid = SIMPLEQ_FIRST(parent_ids);
6262 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6265 error = collect_commits(&commits, head_commit_id, pid->id,
6266 base_commit_id, got_worktree_get_path_prefix(worktree),
6267 GOT_ERR_HISTEDIT_PATH, repo);
6268 got_object_commit_close(commit);
6273 if (edit_in_progress) {
6274 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6278 error = got_ref_open(&branch, repo,
6279 got_worktree_get_head_ref_name(worktree), 0);
6283 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
6284 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
6285 "will not edit commit history of a branch outside "
6286 "the \"refs/heads/\" reference namespace");
6290 error = got_ref_resolve(&head_commit_id, repo, branch);
6291 got_ref_close(branch);
6296 error = got_object_open_as_commit(&commit, repo,
6300 parent_ids = got_object_commit_get_parent_ids(commit);
6301 pid = SIMPLEQ_FIRST(parent_ids);
6303 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6306 error = collect_commits(&commits, head_commit_id, pid->id,
6307 got_worktree_get_base_commit_id(worktree),
6308 got_worktree_get_path_prefix(worktree),
6309 GOT_ERR_HISTEDIT_PATH, repo);
6310 got_object_commit_close(commit);
6315 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
6316 &base_commit_id, &fileindex, worktree, repo);
6320 if (edit_script_path) {
6321 error = histedit_load_list(&histedit_cmds,
6322 edit_script_path, repo);
6324 got_worktree_histedit_abort(worktree, fileindex,
6325 repo, branch, base_commit_id,
6326 update_progress, &did_something);
6330 error = histedit_edit_script(&histedit_cmds, &commits,
6333 got_worktree_histedit_abort(worktree, fileindex,
6334 repo, branch, base_commit_id,
6335 update_progress, &did_something);
6341 error = histedit_save_list(&histedit_cmds, worktree,
6344 got_worktree_histedit_abort(worktree, fileindex,
6345 repo, branch, base_commit_id,
6346 update_progress, &did_something);
6352 error = histedit_check_script(&histedit_cmds, &commits, repo);
6356 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
6357 if (resume_commit_id) {
6358 if (got_object_id_cmp(hle->commit_id,
6359 resume_commit_id) != 0)
6362 resume_commit_id = NULL;
6363 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
6364 hle->cmd->code == GOT_HISTEDIT_FOLD) {
6365 error = histedit_skip_commit(hle, worktree,
6368 error = histedit_commit(NULL, worktree,
6369 fileindex, tmp_branch, hle, repo);
6376 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
6377 error = histedit_skip_commit(hle, worktree, repo);
6383 error = got_object_open_as_commit(&commit, repo,
6387 parent_ids = got_object_commit_get_parent_ids(commit);
6388 pid = SIMPLEQ_FIRST(parent_ids);
6390 error = got_worktree_histedit_merge_files(&merged_paths,
6391 worktree, fileindex, pid->id, hle->commit_id, repo,
6392 rebase_progress, &rebase_status, check_cancelled, NULL);
6395 got_object_commit_close(commit);
6398 if (rebase_status == GOT_STATUS_CONFLICT) {
6399 got_worktree_rebase_pathlist_free(&merged_paths);
6403 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
6405 error = got_object_id_str(&id_str, hle->commit_id);
6408 printf("Stopping histedit for amending commit %s\n",
6411 got_worktree_rebase_pathlist_free(&merged_paths);
6412 error = got_worktree_histedit_postpone(worktree,
6417 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
6418 error = histedit_skip_commit(hle, worktree, repo);
6424 error = histedit_commit(&merged_paths, worktree, fileindex,
6425 tmp_branch, hle, repo);
6426 got_worktree_rebase_pathlist_free(&merged_paths);
6431 if (rebase_status == GOT_STATUS_CONFLICT) {
6432 error = got_worktree_histedit_postpone(worktree, fileindex);
6435 error = got_error_msg(GOT_ERR_CONFLICTS,
6436 "conflicts must be resolved before rebasing can continue");
6438 error = histedit_complete(worktree, fileindex, tmp_branch,
6441 got_object_id_queue_free(&commits);
6442 histedit_free_list(&histedit_cmds);
6443 free(head_commit_id);
6444 free(base_commit_id);
6445 free(resume_commit_id);
6447 got_object_commit_close(commit);
6449 got_ref_close(branch);
6451 got_ref_close(tmp_branch);
6453 got_worktree_close(worktree);
6455 got_repo_close(repo);
6462 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
6463 "[file-path ...]\n",
6468 static const struct got_error *
6469 print_stage(void *arg, unsigned char status, unsigned char staged_status,
6470 const char *path, struct got_object_id *blob_id,
6471 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
6473 const struct got_error *err = NULL;
6474 char *id_str = NULL;
6476 if (staged_status != GOT_STATUS_ADD &&
6477 staged_status != GOT_STATUS_MODIFY &&
6478 staged_status != GOT_STATUS_DELETE)
6481 if (staged_status == GOT_STATUS_ADD ||
6482 staged_status == GOT_STATUS_MODIFY)
6483 err = got_object_id_str(&id_str, staged_blob_id);
6485 err = got_object_id_str(&id_str, blob_id);
6489 printf("%s %c %s\n", id_str, staged_status, path);
6494 static const struct got_error *
6495 cmd_stage(int argc, char *argv[])
6497 const struct got_error *error = NULL;
6498 struct got_repository *repo = NULL;
6499 struct got_worktree *worktree = NULL;
6501 struct got_pathlist_head paths;
6502 struct got_pathlist_entry *pe;
6503 int ch, list_stage = 0, pflag = 0;
6504 FILE *patch_script_file = NULL;
6505 const char *patch_script_path = NULL;
6506 struct choose_patch_arg cpa;
6510 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
6519 patch_script_path = optarg;
6531 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6532 "unveil", NULL) == -1)
6535 if (list_stage && (pflag || patch_script_path))
6536 errx(1, "-l option cannot be used with other options");
6537 if (patch_script_path && !pflag)
6538 errx(1, "-F option can only be used together with -p option");
6540 cwd = getcwd(NULL, 0);
6542 error = got_error_from_errno("getcwd");
6546 error = got_worktree_open(&worktree, cwd);
6550 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6555 if (patch_script_path) {
6556 patch_script_file = fopen(patch_script_path, "r");
6557 if (patch_script_file == NULL) {
6558 error = got_error_from_errno2("fopen",
6563 error = apply_unveil(got_repo_get_path(repo), 0,
6564 got_worktree_get_root_path(worktree));
6568 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6573 error = got_worktree_status(worktree, &paths, repo,
6574 print_stage, NULL, check_cancelled, NULL);
6576 cpa.patch_script_file = patch_script_file;
6577 cpa.action = "stage";
6578 error = got_worktree_stage(worktree, &paths,
6579 pflag ? NULL : print_status, NULL,
6580 pflag ? choose_patch : NULL, &cpa, repo);
6583 if (patch_script_file && fclose(patch_script_file) == EOF &&
6585 error = got_error_from_errno2("fclose", patch_script_path);
6587 got_repo_close(repo);
6589 got_worktree_close(worktree);
6590 TAILQ_FOREACH(pe, &paths, entry)
6591 free((char *)pe->path);
6592 got_pathlist_free(&paths);
6600 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
6601 "[file-path ...]\n",
6607 static const struct got_error *
6608 cmd_unstage(int argc, char *argv[])
6610 const struct got_error *error = NULL;
6611 struct got_repository *repo = NULL;
6612 struct got_worktree *worktree = NULL;
6614 struct got_pathlist_head paths;
6615 struct got_pathlist_entry *pe;
6616 int ch, did_something = 0, pflag = 0;
6617 FILE *patch_script_file = NULL;
6618 const char *patch_script_path = NULL;
6619 struct choose_patch_arg cpa;
6623 while ((ch = getopt(argc, argv, "pF:")) != -1) {
6629 patch_script_path = optarg;
6641 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6642 "unveil", NULL) == -1)
6645 if (patch_script_path && !pflag)
6646 errx(1, "-F option can only be used together with -p option");
6648 cwd = getcwd(NULL, 0);
6650 error = got_error_from_errno("getcwd");
6654 error = got_worktree_open(&worktree, cwd);
6658 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6663 if (patch_script_path) {
6664 patch_script_file = fopen(patch_script_path, "r");
6665 if (patch_script_file == NULL) {
6666 error = got_error_from_errno2("fopen",
6672 error = apply_unveil(got_repo_get_path(repo), 0,
6673 got_worktree_get_root_path(worktree));
6677 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6681 cpa.patch_script_file = patch_script_file;
6682 cpa.action = "unstage";
6683 error = got_worktree_unstage(worktree, &paths, update_progress,
6684 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
6686 if (patch_script_file && fclose(patch_script_file) == EOF &&
6688 error = got_error_from_errno2("fclose", patch_script_path);
6690 got_repo_close(repo);
6692 got_worktree_close(worktree);
6693 TAILQ_FOREACH(pe, &paths, entry)
6694 free((char *)pe->path);
6695 got_pathlist_free(&paths);
6703 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
6704 "arg1 [arg2 ...]\n", getprogname());
6708 static const struct got_error *
6709 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6711 const struct got_error *err;
6712 struct got_blob_object *blob;
6714 err = got_object_open_as_blob(&blob, repo, id, 8192);
6718 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
6719 got_object_blob_close(blob);
6723 static const struct got_error *
6724 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6726 const struct got_error *err;
6727 struct got_tree_object *tree;
6728 const struct got_tree_entries *entries;
6729 struct got_tree_entry *te;
6731 err = got_object_open_as_tree(&tree, repo, id);
6735 entries = got_object_tree_get_entries(tree);
6736 te = SIMPLEQ_FIRST(&entries->head);
6739 if (sigint_received || sigpipe_received)
6741 err = got_object_id_str(&id_str, te->id);
6744 fprintf(outfile, "%s %.7o %s\n", id_str, te->mode, te->name);
6746 te = SIMPLEQ_NEXT(te, entry);
6749 got_object_tree_close(tree);
6753 static const struct got_error *
6754 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6756 const struct got_error *err;
6757 struct got_commit_object *commit;
6758 const struct got_object_id_queue *parent_ids;
6759 struct got_object_qid *pid;
6760 char *id_str = NULL;
6761 const char *logmsg = NULL;
6763 err = got_object_open_as_commit(&commit, repo, id);
6767 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
6771 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
6772 parent_ids = got_object_commit_get_parent_ids(commit);
6773 fprintf(outfile, "numparents %d\n",
6774 got_object_commit_get_nparents(commit));
6775 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
6777 err = got_object_id_str(&pid_str, pid->id);
6780 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
6783 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
6784 got_object_commit_get_author(commit),
6785 got_object_commit_get_author_time(commit));
6787 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
6788 got_object_commit_get_author(commit),
6789 got_object_commit_get_committer_time(commit));
6791 logmsg = got_object_commit_get_logmsg_raw(commit);
6792 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
6793 fprintf(outfile, "%s", logmsg);
6796 got_object_commit_close(commit);
6800 static const struct got_error *
6801 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6803 const struct got_error *err;
6804 struct got_tag_object *tag;
6805 char *id_str = NULL;
6806 const char *tagmsg = NULL;
6808 err = got_object_open_as_tag(&tag, repo, id);
6812 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
6816 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
6818 switch (got_object_tag_get_object_type(tag)) {
6819 case GOT_OBJ_TYPE_BLOB:
6820 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
6821 GOT_OBJ_LABEL_BLOB);
6823 case GOT_OBJ_TYPE_TREE:
6824 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
6825 GOT_OBJ_LABEL_TREE);
6827 case GOT_OBJ_TYPE_COMMIT:
6828 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
6829 GOT_OBJ_LABEL_COMMIT);
6831 case GOT_OBJ_TYPE_TAG:
6832 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
6839 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
6840 got_object_tag_get_name(tag));
6842 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
6843 got_object_tag_get_tagger(tag),
6844 got_object_tag_get_tagger_time(tag));
6846 tagmsg = got_object_tag_get_message(tag);
6847 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
6848 fprintf(outfile, "%s", tagmsg);
6851 got_object_tag_close(tag);
6855 static const struct got_error *
6856 cmd_cat(int argc, char *argv[])
6858 const struct got_error *error;
6859 struct got_repository *repo = NULL;
6860 struct got_worktree *worktree = NULL;
6861 char *cwd = NULL, *repo_path = NULL, *label = NULL;
6862 const char *commit_id_str = NULL;
6863 struct got_object_id *id = NULL, *commit_id = NULL;
6864 int ch, obj_type, i, force_path = 0;
6867 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6872 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
6875 commit_id_str = optarg;
6878 repo_path = realpath(optarg, NULL);
6879 if (repo_path == NULL)
6880 err(1, "-r option");
6881 got_path_strip_trailing_slashes(repo_path);
6895 cwd = getcwd(NULL, 0);
6897 error = got_error_from_errno("getcwd");
6900 error = got_worktree_open(&worktree, cwd);
6901 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6904 if (repo_path == NULL) {
6906 got_worktree_get_repo_path(worktree));
6907 if (repo_path == NULL) {
6908 error = got_error_from_errno("strdup");
6914 if (repo_path == NULL) {
6915 repo_path = getcwd(NULL, 0);
6916 if (repo_path == NULL)
6917 return got_error_from_errno("getcwd");
6920 error = got_repo_open(&repo, repo_path, NULL);
6925 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6929 if (commit_id_str == NULL)
6930 commit_id_str = GOT_REF_HEAD;
6931 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
6935 for (i = 0; i < argc; i++) {
6937 error = got_object_id_by_path(&id, repo, commit_id,
6942 error = match_object_id(&id, &label, argv[i],
6943 GOT_OBJ_TYPE_ANY, 0, repo);
6945 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
6946 error->code != GOT_ERR_NOT_REF)
6948 error = got_object_id_by_path(&id, repo,
6949 commit_id, argv[i]);
6955 error = got_object_get_type(&obj_type, repo, id);
6960 case GOT_OBJ_TYPE_BLOB:
6961 error = cat_blob(id, repo, stdout);
6963 case GOT_OBJ_TYPE_TREE:
6964 error = cat_tree(id, repo, stdout);
6966 case GOT_OBJ_TYPE_COMMIT:
6967 error = cat_commit(id, repo, stdout);
6969 case GOT_OBJ_TYPE_TAG:
6970 error = cat_tag(id, repo, stdout);
6973 error = got_error(GOT_ERR_OBJ_TYPE);
6989 got_worktree_close(worktree);
6991 const struct got_error *repo_error;
6992 repo_error = got_repo_close(repo);