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(const char **author)
497 const char *got_author;
501 got_author = getenv("GOT_AUTHOR");
502 if (got_author == NULL) {
503 /* TODO: Look up user in password database? */
504 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
507 *author = got_author;
510 * Really dumb email address check; we're only doing this to
511 * avoid git's object parser breaking on commits we create.
513 while (*got_author && *got_author != '<')
515 if (*got_author != '<')
516 return got_error(GOT_ERR_COMMIT_NO_EMAIL);
517 while (*got_author && *got_author != '@')
519 if (*got_author != '@')
520 return got_error(GOT_ERR_COMMIT_NO_EMAIL);
521 while (*got_author && *got_author != '>')
523 if (*got_author != '>')
524 return got_error(GOT_ERR_COMMIT_NO_EMAIL);
529 static const struct got_error *
530 cmd_import(int argc, char *argv[])
532 const struct got_error *error = NULL;
533 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
536 const char *branch_name = "master";
537 char *refname = NULL, *id_str = NULL;
538 struct got_repository *repo = NULL;
539 struct got_reference *branch_ref = NULL, *head_ref = NULL;
540 struct got_object_id *new_commit_id = NULL;
542 struct got_pathlist_head ignores;
543 struct got_pathlist_entry *pe;
545 TAILQ_INIT(&ignores);
547 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
550 branch_name = optarg;
553 logmsg = strdup(optarg);
554 if (logmsg == NULL) {
555 error = got_error_from_errno("strdup");
560 repo_path = realpath(optarg, NULL);
561 if (repo_path == NULL) {
562 error = got_error_from_errno("realpath");
567 if (optarg[0] == '\0')
569 error = got_pathlist_insert(&pe, &ignores, optarg,
584 if (pledge("stdio rpath wpath cpath fattr flock proc exec unveil",
591 error = get_author(&author);
595 if (repo_path == NULL) {
596 repo_path = getcwd(NULL, 0);
597 if (repo_path == NULL)
598 return got_error_from_errno("getcwd");
600 got_path_strip_trailing_slashes(repo_path);
601 error = got_repo_open(&repo, repo_path);
605 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
606 error = got_error_from_errno("asprintf");
610 error = got_ref_open(&branch_ref, repo, refname, 0);
612 if (error->code != GOT_ERR_NOT_REF)
615 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
616 "import target branch already exists");
620 path_dir = realpath(argv[0], NULL);
621 if (path_dir == NULL) {
622 error = got_error_from_errno("realpath");
625 got_path_strip_trailing_slashes(path_dir);
628 * unveil(2) traverses exec(2); if an editor is used we have
629 * to apply unveil after the log message has been written.
631 if (logmsg == NULL || strlen(logmsg) == 0) {
632 error = get_editor(&editor);
635 error = collect_import_msg(&logmsg, editor, path_dir, refname);
640 if (unveil(path_dir, "r") != 0)
641 return got_error_from_errno2("unveil", path_dir);
643 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
647 error = got_repo_import(&new_commit_id, path_dir, logmsg,
648 author, &ignores, repo, import_progress, NULL);
652 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
656 error = got_ref_write(branch_ref, repo);
660 error = got_object_id_str(&id_str, new_commit_id);
664 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
666 if (error->code != GOT_ERR_NOT_REF)
669 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
674 error = got_ref_write(head_ref, repo);
679 printf("Created branch %s with commit %s\n",
680 got_ref_get_name(branch_ref), id_str);
688 got_ref_close(branch_ref);
690 got_ref_close(head_ref);
697 fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
698 "[-p prefix] repository-path [worktree-path]\n", getprogname());
702 static const struct got_error *
703 checkout_progress(void *arg, unsigned char status, const char *path)
705 char *worktree_path = arg;
707 /* Base commit bump happens silently. */
708 if (status == GOT_STATUS_BUMP_BASE)
711 while (path[0] == '/')
714 printf("%c %s/%s\n", status, worktree_path, path);
718 static const struct got_error *
719 check_cancelled(void *arg)
721 if (sigint_received || sigpipe_received)
722 return got_error(GOT_ERR_CANCELLED);
726 static const struct got_error *
727 check_linear_ancestry(struct got_object_id *commit_id,
728 struct got_object_id *base_commit_id, struct got_repository *repo)
730 const struct got_error *err = NULL;
731 struct got_object_id *yca_id;
733 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
734 commit_id, base_commit_id, repo, check_cancelled, NULL);
739 return got_error(GOT_ERR_ANCESTRY);
742 * Require a straight line of history between the target commit
743 * and the work tree's base commit.
745 * Non-linear situations such as this require a rebase:
747 * (commit) D F (base_commit)
755 * 'got update' only handles linear cases:
756 * Update forwards in time: A (base/yca) - B - C - D (commit)
757 * Update backwards in time: D (base) - C - B - A (commit/yca)
759 if (got_object_id_cmp(commit_id, yca_id) != 0 &&
760 got_object_id_cmp(base_commit_id, yca_id) != 0)
761 return got_error(GOT_ERR_ANCESTRY);
767 static const struct got_error *
768 check_same_branch(struct got_object_id *commit_id,
769 struct got_reference *head_ref, struct got_object_id *yca_id,
770 struct got_repository *repo)
772 const struct got_error *err = NULL;
773 struct got_commit_graph *graph = NULL;
774 struct got_object_id *head_commit_id = NULL;
775 int is_same_branch = 0;
777 err = got_ref_resolve(&head_commit_id, repo, head_ref);
781 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
785 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
790 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
794 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
795 check_cancelled, NULL);
800 struct got_object_id *id;
801 err = got_commit_graph_iter_next(&id, graph);
803 if (err->code == GOT_ERR_ITER_COMPLETED) {
806 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
808 err = got_commit_graph_fetch_commits(graph, 1,
809 repo, check_cancelled, NULL);
815 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
817 if (got_object_id_cmp(id, commit_id) == 0) {
825 got_commit_graph_close(graph);
826 free(head_commit_id);
827 if (!err && !is_same_branch)
828 err = got_error(GOT_ERR_ANCESTRY);
832 static const struct got_error *
833 resolve_commit_arg(struct got_object_id **commit_id,
834 const char *commit_id_arg, struct got_repository *repo)
836 const struct got_error *err;
837 struct got_reference *ref;
838 struct got_tag_object *tag;
840 err = got_repo_object_match_tag(&tag, commit_id_arg,
841 GOT_OBJ_TYPE_COMMIT, repo);
843 *commit_id = got_object_id_dup(
844 got_object_tag_get_object_id(tag));
845 if (*commit_id == NULL)
846 err = got_error_from_errno("got_object_id_dup");
847 got_object_tag_close(tag);
849 } else if (err->code != GOT_ERR_NO_OBJ)
852 err = got_ref_open(&ref, repo, commit_id_arg, 0);
854 err = got_ref_resolve(commit_id, repo, ref);
857 if (err->code != GOT_ERR_NOT_REF)
859 err = got_repo_match_object_id_prefix(commit_id,
860 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
865 static const struct got_error *
866 cmd_checkout(int argc, char *argv[])
868 const struct got_error *error = NULL;
869 struct got_repository *repo = NULL;
870 struct got_reference *head_ref = NULL;
871 struct got_worktree *worktree = NULL;
872 char *repo_path = NULL;
873 char *worktree_path = NULL;
874 const char *path_prefix = "";
875 const char *branch_name = GOT_REF_HEAD;
876 char *commit_id_str = NULL;
877 int ch, same_path_prefix;
878 struct got_pathlist_head paths;
882 while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
885 branch_name = optarg;
888 commit_id_str = strdup(optarg);
889 if (commit_id_str == NULL)
890 return got_error_from_errno("strdup");
893 path_prefix = optarg;
905 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
906 "unveil", NULL) == -1)
910 char *cwd, *base, *dotgit;
911 repo_path = realpath(argv[0], NULL);
912 if (repo_path == NULL)
913 return got_error_from_errno2("realpath", argv[0]);
914 cwd = getcwd(NULL, 0);
916 error = got_error_from_errno("getcwd");
919 if (path_prefix[0]) {
920 base = basename(path_prefix);
922 error = got_error_from_errno2("basename",
927 base = basename(repo_path);
929 error = got_error_from_errno2("basename",
934 dotgit = strstr(base, ".git");
937 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
938 error = got_error_from_errno("asprintf");
943 } else if (argc == 2) {
944 repo_path = realpath(argv[0], NULL);
945 if (repo_path == NULL) {
946 error = got_error_from_errno2("realpath", argv[0]);
949 worktree_path = realpath(argv[1], NULL);
950 if (worktree_path == NULL) {
951 if (errno != ENOENT) {
952 error = got_error_from_errno2("realpath",
956 worktree_path = strdup(argv[1]);
957 if (worktree_path == NULL) {
958 error = got_error_from_errno("strdup");
965 got_path_strip_trailing_slashes(repo_path);
966 got_path_strip_trailing_slashes(worktree_path);
968 error = got_repo_open(&repo, repo_path);
972 /* Pre-create work tree path for unveil(2) */
973 error = got_path_mkdir(worktree_path);
975 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
976 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
978 if (!got_path_dir_is_empty(worktree_path)) {
979 error = got_error_path(worktree_path,
980 GOT_ERR_DIR_NOT_EMPTY);
985 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
989 error = got_ref_open(&head_ref, repo, branch_name, 0);
993 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
994 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
997 error = got_worktree_open(&worktree, worktree_path);
1001 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1005 if (!same_path_prefix) {
1006 error = got_error(GOT_ERR_PATH_PREFIX);
1010 if (commit_id_str) {
1011 struct got_object_id *commit_id;
1012 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1015 error = check_linear_ancestry(commit_id,
1016 got_worktree_get_base_commit_id(worktree), repo);
1017 if (error != NULL) {
1021 error = check_same_branch(commit_id, head_ref, NULL, repo);
1024 error = got_worktree_set_base_commit_id(worktree, repo,
1031 error = got_pathlist_append(&paths, "", NULL);
1034 error = got_worktree_checkout_files(worktree, &paths, repo,
1035 checkout_progress, worktree_path, check_cancelled, NULL);
1039 printf("Now shut up and hack\n");
1042 got_pathlist_free(&paths);
1043 free(commit_id_str);
1045 free(worktree_path);
1052 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1057 static const struct got_error *
1058 update_progress(void *arg, unsigned char status, const char *path)
1060 int *did_something = arg;
1062 if (status == GOT_STATUS_EXISTS)
1067 /* Base commit bump happens silently. */
1068 if (status == GOT_STATUS_BUMP_BASE)
1071 while (path[0] == '/')
1073 printf("%c %s\n", status, path);
1077 static const struct got_error *
1078 switch_head_ref(struct got_reference *head_ref,
1079 struct got_object_id *commit_id, struct got_worktree *worktree,
1080 struct got_repository *repo)
1082 const struct got_error *err = NULL;
1084 int ref_has_moved = 0;
1086 /* Trivial case: switching between two different references. */
1087 if (strcmp(got_ref_get_name(head_ref),
1088 got_worktree_get_head_ref_name(worktree)) != 0) {
1089 printf("Switching work tree from %s to %s\n",
1090 got_worktree_get_head_ref_name(worktree),
1091 got_ref_get_name(head_ref));
1092 return got_worktree_set_head_ref(worktree, head_ref);
1095 err = check_linear_ancestry(commit_id,
1096 got_worktree_get_base_commit_id(worktree), repo);
1098 if (err->code != GOT_ERR_ANCESTRY)
1105 /* Switching to a rebased branch with the same reference name. */
1106 err = got_object_id_str(&base_id_str,
1107 got_worktree_get_base_commit_id(worktree));
1110 printf("Reference %s now points at a different branch\n",
1111 got_worktree_get_head_ref_name(worktree));
1112 printf("Switching work tree from %s to %s\n", base_id_str,
1113 got_worktree_get_head_ref_name(worktree));
1117 static const struct got_error *
1118 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1120 const struct got_error *err;
1123 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1127 return got_error(GOT_ERR_REBASING);
1129 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1133 return got_error(GOT_ERR_HISTEDIT_BUSY);
1138 static const struct got_error *
1139 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1140 char *argv[], struct got_worktree *worktree)
1142 const struct got_error *err = NULL;
1149 return got_error_from_errno("strdup");
1150 return got_pathlist_append(paths, path, NULL);
1153 for (i = 0; i < argc; i++) {
1154 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1157 err = got_pathlist_append(paths, path, NULL);
1167 static const struct got_error *
1168 cmd_update(int argc, char *argv[])
1170 const struct got_error *error = NULL;
1171 struct got_repository *repo = NULL;
1172 struct got_worktree *worktree = NULL;
1173 char *worktree_path = NULL;
1174 struct got_object_id *commit_id = NULL;
1175 char *commit_id_str = NULL;
1176 const char *branch_name = NULL;
1177 struct got_reference *head_ref = NULL;
1178 struct got_pathlist_head paths;
1179 struct got_pathlist_entry *pe;
1180 int ch, did_something = 0;
1184 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1187 branch_name = optarg;
1190 commit_id_str = strdup(optarg);
1191 if (commit_id_str == NULL)
1192 return got_error_from_errno("strdup");
1204 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1205 "unveil", NULL) == -1)
1208 worktree_path = getcwd(NULL, 0);
1209 if (worktree_path == NULL) {
1210 error = got_error_from_errno("getcwd");
1213 error = got_worktree_open(&worktree, worktree_path);
1217 error = check_rebase_or_histedit_in_progress(worktree);
1221 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1225 error = apply_unveil(got_repo_get_path(repo), 0,
1226 got_worktree_get_root_path(worktree));
1230 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1234 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1235 got_worktree_get_head_ref_name(worktree), 0);
1238 if (commit_id_str == NULL) {
1239 error = got_ref_resolve(&commit_id, repo, head_ref);
1242 error = got_object_id_str(&commit_id_str, commit_id);
1246 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1247 free(commit_id_str);
1248 commit_id_str = NULL;
1251 error = got_object_id_str(&commit_id_str, commit_id);
1257 struct got_object_id *head_commit_id;
1258 TAILQ_FOREACH(pe, &paths, entry) {
1259 if (pe->path_len == 0)
1261 error = got_error_msg(GOT_ERR_BAD_PATH,
1262 "switching between branches requires that "
1263 "the entire work tree gets updated");
1266 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1269 error = check_linear_ancestry(commit_id, head_commit_id, repo);
1270 free(head_commit_id);
1273 error = check_same_branch(commit_id, head_ref, NULL, repo);
1276 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1280 error = check_linear_ancestry(commit_id,
1281 got_worktree_get_base_commit_id(worktree), repo);
1282 if (error != NULL) {
1283 if (error->code == GOT_ERR_ANCESTRY)
1284 error = got_error(GOT_ERR_BRANCH_MOVED);
1287 error = check_same_branch(commit_id, head_ref, NULL, repo);
1292 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1294 error = got_worktree_set_base_commit_id(worktree, repo,
1300 error = got_worktree_checkout_files(worktree, &paths, repo,
1301 update_progress, &did_something, check_cancelled, NULL);
1306 printf("Updated to commit %s\n", commit_id_str);
1308 printf("Already up-to-date\n");
1310 free(worktree_path);
1311 TAILQ_FOREACH(pe, &paths, entry)
1312 free((char *)pe->path);
1313 got_pathlist_free(&paths);
1315 free(commit_id_str);
1319 static const struct got_error *
1320 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
1321 const char *path, int diff_context, struct got_repository *repo)
1323 const struct got_error *err = NULL;
1324 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1327 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
1332 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
1336 while (path[0] == '/')
1338 err = got_diff_blob(blob1, blob2, path, path, diff_context,
1342 got_object_blob_close(blob1);
1343 got_object_blob_close(blob2);
1347 static const struct got_error *
1348 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
1349 const char *path, int diff_context, struct got_repository *repo)
1351 const struct got_error *err = NULL;
1352 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1353 struct got_diff_blob_output_unidiff_arg arg;
1356 err = got_object_open_as_tree(&tree1, repo, tree_id1);
1361 err = got_object_open_as_tree(&tree2, repo, tree_id2);
1365 arg.diff_context = diff_context;
1366 arg.outfile = stdout;
1367 while (path[0] == '/')
1369 err = got_diff_tree(tree1, tree2, path, path, repo,
1370 got_diff_blob_output_unidiff, &arg, 1);
1373 got_object_tree_close(tree1);
1374 got_object_tree_close(tree2);
1378 static const struct got_error *
1379 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1380 const char *path, int diff_context, struct got_repository *repo)
1382 const struct got_error *err = NULL;
1383 struct got_commit_object *pcommit = NULL;
1384 char *id_str1 = NULL, *id_str2 = NULL;
1385 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
1386 struct got_object_qid *qid;
1388 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1390 err = got_object_open_as_commit(&pcommit, repo,
1396 if (path && path[0] != '\0') {
1398 err = got_object_id_by_path(&obj_id2, repo, id, path);
1401 err = got_object_id_str(&id_str2, obj_id2);
1407 err = got_object_id_by_path(&obj_id1, repo,
1413 err = got_object_id_str(&id_str1, obj_id1);
1419 err = got_object_get_type(&obj_type, repo, obj_id2);
1424 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1426 case GOT_OBJ_TYPE_BLOB:
1427 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
1430 case GOT_OBJ_TYPE_TREE:
1431 err = diff_trees(obj_id1, obj_id2, path, diff_context,
1435 err = got_error(GOT_ERR_OBJ_TYPE);
1441 obj_id2 = got_object_commit_get_tree_id(commit);
1442 err = got_object_id_str(&id_str2, obj_id2);
1445 obj_id1 = got_object_commit_get_tree_id(pcommit);
1446 err = got_object_id_str(&id_str1, obj_id1);
1449 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1450 err = diff_trees(obj_id1, obj_id2, "", diff_context, repo);
1457 got_object_commit_close(pcommit);
1462 get_datestr(time_t *time, char *datebuf)
1464 struct tm mytm, *tm;
1467 tm = gmtime_r(time, &mytm);
1470 s = asctime_r(tm, datebuf);
1473 p = strchr(s, '\n');
1479 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1481 static const struct got_error *
1482 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1483 struct got_repository *repo, const char *path, int show_patch,
1484 int diff_context, struct got_reflist_head *refs)
1486 const struct got_error *err = NULL;
1487 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1489 time_t committer_time;
1490 const char *author, *committer;
1491 char *refs_str = NULL;
1492 struct got_reflist_entry *re;
1494 SIMPLEQ_FOREACH(re, refs, entry) {
1497 struct got_tag_object *tag = NULL;
1500 name = got_ref_get_name(re->ref);
1501 if (strcmp(name, GOT_REF_HEAD) == 0)
1503 if (strncmp(name, "refs/", 5) == 0)
1505 if (strncmp(name, "got/", 4) == 0)
1507 if (strncmp(name, "heads/", 6) == 0)
1509 if (strncmp(name, "remotes/", 8) == 0)
1511 if (strncmp(name, "tags/", 5) == 0) {
1512 err = got_object_open_as_tag(&tag, repo, re->id);
1514 if (err->code != GOT_ERR_OBJ_TYPE)
1516 /* Ref points at something other than a tag. */
1521 cmp = got_object_id_cmp(tag ?
1522 got_object_tag_get_object_id(tag) : re->id, id);
1524 got_object_tag_close(tag);
1528 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1530 err = got_error_from_errno("asprintf");
1536 err = got_object_id_str(&id_str, id);
1540 printf(GOT_COMMIT_SEP_STR);
1541 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1542 refs_str ? refs_str : "", refs_str ? ")" : "");
1547 printf("from: %s\n", got_object_commit_get_author(commit));
1548 committer_time = got_object_commit_get_committer_time(commit);
1549 datestr = get_datestr(&committer_time, datebuf);
1551 printf("date: %s UTC\n", datestr);
1552 author = got_object_commit_get_author(commit);
1553 committer = got_object_commit_get_committer(commit);
1554 if (strcmp(author, committer) != 0)
1555 printf("via: %s\n", committer);
1556 if (got_object_commit_get_nparents(commit) > 1) {
1557 const struct got_object_id_queue *parent_ids;
1558 struct got_object_qid *qid;
1560 parent_ids = got_object_commit_get_parent_ids(commit);
1561 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1562 err = got_object_id_str(&id_str, qid->id);
1565 printf("parent %d: %s\n", n++, id_str);
1570 err = got_object_commit_get_logmsg(&logmsg0, commit);
1576 line = strsep(&logmsg, "\n");
1578 printf(" %s\n", line);
1583 err = print_patch(commit, id, path, diff_context, repo);
1588 if (fflush(stdout) != 0 && err == NULL)
1589 err = got_error_from_errno("fflush");
1593 static const struct got_error *
1594 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1595 char *path, int show_patch, int diff_context, int limit,
1596 int first_parent_traversal, struct got_reflist_head *refs)
1598 const struct got_error *err;
1599 struct got_commit_graph *graph;
1601 err = got_commit_graph_open(&graph, root_id, path,
1602 first_parent_traversal, repo);
1605 err = got_commit_graph_iter_start(graph, root_id, repo,
1606 check_cancelled, NULL);
1610 struct got_commit_object *commit;
1611 struct got_object_id *id;
1613 if (sigint_received || sigpipe_received)
1616 err = got_commit_graph_iter_next(&id, graph);
1618 if (err->code == GOT_ERR_ITER_COMPLETED) {
1622 if (err->code != GOT_ERR_ITER_NEED_MORE)
1624 err = got_commit_graph_fetch_commits(graph, 1, repo,
1625 check_cancelled, NULL);
1634 err = got_object_open_as_commit(&commit, repo, id);
1637 err = print_commit(commit, id, repo, path, show_patch,
1638 diff_context, refs);
1639 got_object_commit_close(commit);
1640 if (err || (limit && --limit == 0))
1644 got_commit_graph_close(graph);
1651 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1652 "[-r repository-path] [path]\n", getprogname());
1657 get_default_log_limit(void)
1659 const char *got_default_log_limit;
1663 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
1664 if (got_default_log_limit == NULL)
1666 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
1672 static const struct got_error *
1673 cmd_log(int argc, char *argv[])
1675 const struct got_error *error;
1676 struct got_repository *repo = NULL;
1677 struct got_worktree *worktree = NULL;
1678 struct got_commit_object *commit = NULL;
1679 struct got_object_id *id = NULL;
1680 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1681 char *start_commit = NULL;
1682 int diff_context = 3, ch;
1683 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1685 struct got_reflist_head refs;
1687 SIMPLEQ_INIT(&refs);
1690 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1696 limit = get_default_log_limit();
1698 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
1704 start_commit = optarg;
1707 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1710 err(1, "-C option %s", errstr);
1713 limit = strtonum(optarg, 0, INT_MAX, &errstr);
1715 err(1, "-l option %s", errstr);
1718 first_parent_traversal = 1;
1721 repo_path = realpath(optarg, NULL);
1722 if (repo_path == NULL)
1723 err(1, "-r option");
1724 got_path_strip_trailing_slashes(repo_path);
1735 cwd = getcwd(NULL, 0);
1737 error = got_error_from_errno("getcwd");
1741 error = got_worktree_open(&worktree, cwd);
1742 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1749 error = got_error_from_errno("strdup");
1752 } else if (argc == 1) {
1754 error = got_worktree_resolve_path(&path, worktree,
1759 path = strdup(argv[0]);
1761 error = got_error_from_errno("strdup");
1768 if (repo_path == NULL) {
1769 repo_path = worktree ?
1770 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1772 if (repo_path == NULL) {
1773 error = got_error_from_errno("strdup");
1777 error = got_repo_open(&repo, repo_path);
1781 error = apply_unveil(got_repo_get_path(repo), 1,
1782 worktree ? got_worktree_get_root_path(worktree) : NULL);
1786 if (start_commit == NULL) {
1787 struct got_reference *head_ref;
1788 error = got_ref_open(&head_ref, repo,
1789 worktree ? got_worktree_get_head_ref_name(worktree)
1793 error = got_ref_resolve(&id, repo, head_ref);
1794 got_ref_close(head_ref);
1797 error = got_object_open_as_commit(&commit, repo, id);
1799 struct got_reference *ref;
1800 error = got_ref_open(&ref, repo, start_commit, 0);
1801 if (error == NULL) {
1803 error = got_ref_resolve(&id, repo, ref);
1807 error = got_object_get_type(&obj_type, repo, id);
1810 if (obj_type == GOT_OBJ_TYPE_TAG) {
1811 struct got_tag_object *tag;
1812 error = got_object_open_as_tag(&tag, repo, id);
1815 if (got_object_tag_get_object_type(tag) !=
1816 GOT_OBJ_TYPE_COMMIT) {
1817 got_object_tag_close(tag);
1818 error = got_error(GOT_ERR_OBJ_TYPE);
1822 id = got_object_id_dup(
1823 got_object_tag_get_object_id(tag));
1825 error = got_error_from_errno(
1826 "got_object_id_dup");
1827 got_object_tag_close(tag);
1830 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1831 error = got_error(GOT_ERR_OBJ_TYPE);
1834 error = got_object_open_as_commit(&commit, repo, id);
1838 if (commit == NULL) {
1839 error = got_repo_match_object_id_prefix(&id,
1840 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1849 const char *prefix = got_worktree_get_path_prefix(worktree);
1851 if (asprintf(&p, "%s%s%s", prefix,
1852 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
1853 error = got_error_from_errno("asprintf");
1856 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1859 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1864 path = in_repo_path;
1867 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
1871 error = print_commits(id, repo, path, show_patch,
1872 diff_context, limit, first_parent_traversal, &refs);
1879 got_worktree_close(worktree);
1881 const struct got_error *repo_error;
1882 repo_error = got_repo_close(repo);
1886 got_ref_list_free(&refs);
1893 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
1894 "[object1 object2 | path]\n", getprogname());
1898 struct print_diff_arg {
1899 struct got_repository *repo;
1900 struct got_worktree *worktree;
1907 static const struct got_error *
1908 print_diff(void *arg, unsigned char status, unsigned char staged_status,
1909 const char *path, struct got_object_id *blob_id,
1910 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
1912 struct print_diff_arg *a = arg;
1913 const struct got_error *err = NULL;
1914 struct got_blob_object *blob1 = NULL;
1916 char *abspath = NULL, *label1 = NULL;
1919 if (a->diff_staged) {
1920 if (staged_status != GOT_STATUS_MODIFY &&
1921 staged_status != GOT_STATUS_ADD &&
1922 staged_status != GOT_STATUS_DELETE)
1925 if (staged_status == GOT_STATUS_DELETE)
1927 if (status == GOT_STATUS_NONEXISTENT)
1928 return got_error_set_errno(ENOENT, path);
1929 if (status != GOT_STATUS_MODIFY &&
1930 status != GOT_STATUS_ADD &&
1931 status != GOT_STATUS_DELETE &&
1932 status != GOT_STATUS_CONFLICT)
1936 if (!a->header_shown) {
1937 printf("diff %s %s%s\n", a->id_str,
1938 got_worktree_get_root_path(a->worktree),
1939 a->diff_staged ? " (staged changes)" : "");
1940 a->header_shown = 1;
1943 if (a->diff_staged) {
1944 const char *label1 = NULL, *label2 = NULL;
1945 switch (staged_status) {
1946 case GOT_STATUS_MODIFY:
1950 case GOT_STATUS_ADD:
1953 case GOT_STATUS_DELETE:
1957 return got_error(GOT_ERR_FILE_STATUS);
1959 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
1960 label1, label2, a->diff_context, a->repo, stdout);
1963 if (staged_status == GOT_STATUS_ADD ||
1964 staged_status == GOT_STATUS_MODIFY) {
1966 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
1970 err = got_object_id_str(&id_str, staged_blob_id);
1973 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
1974 err = got_error_from_errno("asprintf");
1979 } else if (status != GOT_STATUS_ADD) {
1980 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1985 if (status != GOT_STATUS_DELETE) {
1986 if (asprintf(&abspath, "%s/%s",
1987 got_worktree_get_root_path(a->worktree), path) == -1) {
1988 err = got_error_from_errno("asprintf");
1992 f2 = fopen(abspath, "r");
1994 err = got_error_from_errno2("fopen", abspath);
1997 if (lstat(abspath, &sb) == -1) {
1998 err = got_error_from_errno2("lstat", abspath);
2004 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2005 a->diff_context, stdout);
2008 got_object_blob_close(blob1);
2009 if (f2 && fclose(f2) != 0 && err == NULL)
2010 err = got_error_from_errno("fclose");
2015 static const struct got_error *
2016 match_object_id(struct got_object_id **id, char **label,
2017 const char *id_str, int obj_type, int resolve_tags,
2018 struct got_repository *repo)
2020 const struct got_error *err;
2021 struct got_tag_object *tag;
2022 struct got_reference *ref = NULL;
2028 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
2031 *id = got_object_id_dup(
2032 got_object_tag_get_object_id(tag));
2034 err = got_error_from_errno("got_object_id_dup");
2035 else if (asprintf(label, "refs/tags/%s",
2036 got_object_tag_get_name(tag)) == -1) {
2037 err = got_error_from_errno("asprintf");
2041 got_object_tag_close(tag);
2043 } else if (err->code != GOT_ERR_NO_OBJ)
2047 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
2049 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2051 err = got_ref_open(&ref, repo, id_str, 0);
2054 *label = strdup(got_ref_get_name(ref));
2055 if (*label == NULL) {
2056 err = got_error_from_errno("strdup");
2059 err = got_ref_resolve(id, repo, ref);
2061 err = got_object_id_str(label, *id);
2062 if (*label == NULL) {
2063 err = got_error_from_errno("strdup");
2074 static const struct got_error *
2075 cmd_diff(int argc, char *argv[])
2077 const struct got_error *error;
2078 struct got_repository *repo = NULL;
2079 struct got_worktree *worktree = NULL;
2080 char *cwd = NULL, *repo_path = NULL;
2081 struct got_object_id *id1 = NULL, *id2 = NULL;
2082 const char *id_str1 = NULL, *id_str2 = NULL;
2083 char *label1 = NULL, *label2 = NULL;
2085 int diff_context = 3, diff_staged = 0, ch;
2090 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2095 while ((ch = getopt(argc, argv, "C:r:s")) != -1) {
2098 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
2100 err(1, "-C option %s", errstr);
2103 repo_path = realpath(optarg, NULL);
2104 if (repo_path == NULL)
2105 err(1, "-r option");
2106 got_path_strip_trailing_slashes(repo_path);
2120 cwd = getcwd(NULL, 0);
2122 error = got_error_from_errno("getcwd");
2125 error = got_worktree_open(&worktree, cwd);
2126 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2129 if (worktree == NULL) {
2130 error = got_error(GOT_ERR_NOT_WORKTREE);
2135 "-r option can't be used when diffing a work tree");
2136 repo_path = strdup(got_worktree_get_repo_path(worktree));
2137 if (repo_path == NULL) {
2138 error = got_error_from_errno("strdup");
2142 error = got_worktree_resolve_path(&path, worktree,
2149 error = got_error_from_errno("strdup");
2153 } else if (argc == 2) {
2155 errx(1, "-s option can't be used when diffing "
2156 "objects in repository");
2159 if (worktree && repo_path == NULL) {
2161 strdup(got_worktree_get_repo_path(worktree));
2162 if (repo_path == NULL) {
2163 error = got_error_from_errno("strdup");
2170 if (repo_path == NULL) {
2171 repo_path = getcwd(NULL, 0);
2172 if (repo_path == NULL)
2173 return got_error_from_errno("getcwd");
2176 error = got_repo_open(&repo, repo_path);
2181 error = apply_unveil(got_repo_get_path(repo), 1,
2182 worktree ? got_worktree_get_root_path(worktree) : NULL);
2187 struct print_diff_arg arg;
2188 struct got_pathlist_head paths;
2193 error = got_object_id_str(&id_str,
2194 got_worktree_get_base_commit_id(worktree));
2198 arg.worktree = worktree;
2199 arg.diff_context = diff_context;
2200 arg.id_str = id_str;
2201 arg.header_shown = 0;
2202 arg.diff_staged = diff_staged;
2204 error = got_pathlist_append(&paths, path, NULL);
2208 error = got_worktree_status(worktree, &paths, repo, print_diff,
2209 &arg, check_cancelled, NULL);
2211 got_pathlist_free(&paths);
2215 error = match_object_id(&id1, &label1, id_str1, GOT_OBJ_TYPE_ANY, 1,
2220 error = match_object_id(&id2, &label2, id_str2, GOT_OBJ_TYPE_ANY, 1,
2225 error = got_object_get_type(&type1, repo, id1);
2229 error = got_object_get_type(&type2, repo, id2);
2233 if (type1 != type2) {
2234 error = got_error(GOT_ERR_OBJ_TYPE);
2239 case GOT_OBJ_TYPE_BLOB:
2240 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2241 diff_context, repo, stdout);
2243 case GOT_OBJ_TYPE_TREE:
2244 error = got_diff_objects_as_trees(id1, id2, "", "",
2245 diff_context, repo, stdout);
2247 case GOT_OBJ_TYPE_COMMIT:
2248 printf("diff %s %s\n", label1, label2);
2249 error = got_diff_objects_as_commits(id1, id2, diff_context,
2253 error = got_error(GOT_ERR_OBJ_TYPE);
2263 got_worktree_close(worktree);
2265 const struct got_error *repo_error;
2266 repo_error = got_repo_close(repo);
2277 "usage: %s blame [-c commit] [-r repository-path] path\n",
2286 char datebuf[9]; /* YY-MM-DD + NUL */
2289 struct blame_cb_args {
2290 struct blame_line *lines;
2294 off_t *line_offsets;
2296 struct got_repository *repo;
2299 static const struct got_error *
2300 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2302 const struct got_error *err = NULL;
2303 struct blame_cb_args *a = arg;
2304 struct blame_line *bline;
2306 size_t linesize = 0;
2307 struct got_commit_object *commit = NULL;
2310 time_t committer_time;
2312 if (nlines != a->nlines ||
2313 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2314 return got_error(GOT_ERR_RANGE);
2316 if (sigint_received)
2317 return got_error(GOT_ERR_ITER_COMPLETED);
2320 return NULL; /* no change in this commit */
2322 /* Annotate this line. */
2323 bline = &a->lines[lineno - 1];
2324 if (bline->annotated)
2326 err = got_object_id_str(&bline->id_str, id);
2330 err = got_object_open_as_commit(&commit, a->repo, id);
2334 bline->committer = strdup(got_object_commit_get_committer(commit));
2335 if (bline->committer == NULL) {
2336 err = got_error_from_errno("strdup");
2340 committer_time = got_object_commit_get_committer_time(commit);
2341 if (localtime_r(&committer_time, &tm) == NULL)
2342 return got_error_from_errno("localtime_r");
2343 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%g/%m/%d",
2344 &tm) >= sizeof(bline->datebuf)) {
2345 err = got_error(GOT_ERR_NO_SPACE);
2348 bline->annotated = 1;
2350 /* Print lines annotated so far. */
2351 bline = &a->lines[a->lineno_cur - 1];
2352 if (!bline->annotated)
2355 offset = a->line_offsets[a->lineno_cur - 1];
2356 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2357 err = got_error_from_errno("fseeko");
2361 while (bline->annotated) {
2362 char *smallerthan, *at, *nl, *committer;
2365 if (getline(&line, &linesize, a->f) == (ssize_t)-1) {
2367 err = got_error_from_errno("getline");
2371 committer = bline->committer;
2372 smallerthan = strchr(committer, '<');
2373 if (smallerthan && smallerthan[1] != '\0')
2374 committer = smallerthan + 1;
2375 at = strchr(committer, '@');
2378 len = strlen(committer);
2380 committer[8] = '\0';
2382 nl = strchr(line, '\n');
2385 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2386 bline->id_str, bline->datebuf, committer, line);
2389 bline = &a->lines[a->lineno_cur - 1];
2393 got_object_commit_close(commit);
2398 static const struct got_error *
2399 cmd_blame(int argc, char *argv[])
2401 const struct got_error *error;
2402 struct got_repository *repo = NULL;
2403 struct got_worktree *worktree = NULL;
2404 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2405 struct got_object_id *obj_id = NULL;
2406 struct got_object_id *commit_id = NULL;
2407 struct got_blob_object *blob = NULL;
2408 char *commit_id_str = NULL;
2409 struct blame_cb_args bca;
2410 int ch, obj_type, i;
2413 memset(&bca, 0, sizeof(bca));
2416 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2421 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2424 commit_id_str = optarg;
2427 repo_path = realpath(optarg, NULL);
2428 if (repo_path == NULL)
2429 err(1, "-r option");
2430 got_path_strip_trailing_slashes(repo_path);
2446 cwd = getcwd(NULL, 0);
2448 error = got_error_from_errno("getcwd");
2451 if (repo_path == NULL) {
2452 error = got_worktree_open(&worktree, cwd);
2453 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2459 strdup(got_worktree_get_repo_path(worktree));
2460 if (repo_path == NULL)
2461 error = got_error_from_errno("strdup");
2465 repo_path = strdup(cwd);
2466 if (repo_path == NULL) {
2467 error = got_error_from_errno("strdup");
2473 error = got_repo_open(&repo, repo_path);
2477 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2482 const char *prefix = got_worktree_get_path_prefix(worktree);
2483 char *p, *worktree_subdir = cwd +
2484 strlen(got_worktree_get_root_path(worktree));
2485 if (asprintf(&p, "%s%s%s%s%s",
2486 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2487 worktree_subdir, worktree_subdir[0] ? "/" : "",
2489 error = got_error_from_errno("asprintf");
2492 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2495 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2500 if (commit_id_str == NULL) {
2501 struct got_reference *head_ref;
2502 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2505 error = got_ref_resolve(&commit_id, repo, head_ref);
2506 got_ref_close(head_ref);
2510 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2515 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2518 if (obj_id == NULL) {
2519 error = got_error(GOT_ERR_NO_OBJ);
2523 error = got_object_get_type(&obj_type, repo, obj_id);
2527 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2528 error = got_error(GOT_ERR_OBJ_TYPE);
2532 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2535 bca.f = got_opentemp();
2536 if (bca.f == NULL) {
2537 error = got_error_from_errno("got_opentemp");
2540 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2541 &bca.line_offsets, bca.f, blob);
2542 if (error || bca.nlines == 0)
2545 /* Don't include \n at EOF in the blame line count. */
2546 if (bca.line_offsets[bca.nlines - 1] == filesize)
2549 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2550 if (bca.lines == NULL) {
2551 error = got_error_from_errno("calloc");
2555 bca.nlines_prec = 0;
2563 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2564 check_cancelled, NULL);
2574 got_object_blob_close(blob);
2576 got_worktree_close(worktree);
2578 const struct got_error *repo_error;
2579 repo_error = got_repo_close(repo);
2583 for (i = 0; i < bca.nlines; i++) {
2584 struct blame_line *bline = &bca.lines[i];
2585 free(bline->id_str);
2586 free(bline->committer);
2589 free(bca.line_offsets);
2590 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2591 error = got_error_from_errno("fclose");
2599 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2605 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2606 const char *root_path)
2608 int is_root_path = (strcmp(path, root_path) == 0);
2609 const char *modestr = "";
2611 path += strlen(root_path);
2612 while (path[0] == '/')
2615 if (got_object_tree_entry_is_submodule(te))
2617 else if (S_ISLNK(te->mode))
2619 else if (S_ISDIR(te->mode))
2621 else if (te->mode & S_IXUSR)
2624 printf("%s%s%s%s%s\n", id ? id : "", path,
2625 is_root_path ? "" : "/", te->name, modestr);
2628 static const struct got_error *
2629 print_tree(const char *path, struct got_object_id *commit_id,
2630 int show_ids, int recurse, const char *root_path,
2631 struct got_repository *repo)
2633 const struct got_error *err = NULL;
2634 struct got_object_id *tree_id = NULL;
2635 struct got_tree_object *tree = NULL;
2636 const struct got_tree_entries *entries;
2637 struct got_tree_entry *te;
2639 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2643 err = got_object_open_as_tree(&tree, repo, tree_id);
2646 entries = got_object_tree_get_entries(tree);
2647 te = SIMPLEQ_FIRST(&entries->head);
2651 if (sigint_received || sigpipe_received)
2656 err = got_object_id_str(&id_str, te->id);
2659 if (asprintf(&id, "%s ", id_str) == -1) {
2660 err = got_error_from_errno("asprintf");
2666 print_entry(te, id, path, root_path);
2669 if (recurse && S_ISDIR(te->mode)) {
2671 if (asprintf(&child_path, "%s%s%s", path,
2672 path[0] == '/' && path[1] == '\0' ? "" : "/",
2674 err = got_error_from_errno("asprintf");
2677 err = print_tree(child_path, commit_id, show_ids, 1,
2684 te = SIMPLEQ_NEXT(te, entry);
2688 got_object_tree_close(tree);
2693 static const struct got_error *
2694 cmd_tree(int argc, char *argv[])
2696 const struct got_error *error;
2697 struct got_repository *repo = NULL;
2698 struct got_worktree *worktree = NULL;
2700 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2701 struct got_object_id *commit_id = NULL;
2702 char *commit_id_str = NULL;
2703 int show_ids = 0, recurse = 0;
2707 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2712 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2715 commit_id_str = optarg;
2718 repo_path = realpath(optarg, NULL);
2719 if (repo_path == NULL)
2720 err(1, "-r option");
2721 got_path_strip_trailing_slashes(repo_path);
2745 cwd = getcwd(NULL, 0);
2747 error = got_error_from_errno("getcwd");
2750 if (repo_path == NULL) {
2751 error = got_worktree_open(&worktree, cwd);
2752 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2758 strdup(got_worktree_get_repo_path(worktree));
2759 if (repo_path == NULL)
2760 error = got_error_from_errno("strdup");
2764 repo_path = strdup(cwd);
2765 if (repo_path == NULL) {
2766 error = got_error_from_errno("strdup");
2772 error = got_repo_open(&repo, repo_path);
2776 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2782 char *p, *worktree_subdir = cwd +
2783 strlen(got_worktree_get_root_path(worktree));
2784 if (asprintf(&p, "%s/%s",
2785 got_worktree_get_path_prefix(worktree),
2786 worktree_subdir) == -1) {
2787 error = got_error_from_errno("asprintf");
2790 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2797 if (in_repo_path == NULL) {
2798 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2803 if (commit_id_str == NULL) {
2804 struct got_reference *head_ref;
2805 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2808 error = got_ref_resolve(&commit_id, repo, head_ref);
2809 got_ref_close(head_ref);
2813 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2818 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2819 in_repo_path, repo);
2826 got_worktree_close(worktree);
2828 const struct got_error *repo_error;
2829 repo_error = got_repo_close(repo);
2839 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
2843 static const struct got_error *
2844 print_status(void *arg, unsigned char status, unsigned char staged_status,
2845 const char *path, struct got_object_id *blob_id,
2846 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
2848 if (status == staged_status && (status == GOT_STATUS_DELETE))
2849 status = GOT_STATUS_NO_CHANGE;
2850 printf("%c%c %s\n", status, staged_status, path);
2854 static const struct got_error *
2855 cmd_status(int argc, char *argv[])
2857 const struct got_error *error = NULL;
2858 struct got_repository *repo = NULL;
2859 struct got_worktree *worktree = NULL;
2861 struct got_pathlist_head paths;
2862 struct got_pathlist_entry *pe;
2867 while ((ch = getopt(argc, argv, "")) != -1) {
2879 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2883 cwd = getcwd(NULL, 0);
2885 error = got_error_from_errno("getcwd");
2889 error = got_worktree_open(&worktree, cwd);
2893 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2897 error = apply_unveil(got_repo_get_path(repo), 1,
2898 got_worktree_get_root_path(worktree));
2902 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2906 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
2907 check_cancelled, NULL);
2909 TAILQ_FOREACH(pe, &paths, entry)
2910 free((char *)pe->path);
2911 got_pathlist_free(&paths);
2920 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
2925 static const struct got_error *
2926 list_refs(struct got_repository *repo)
2928 static const struct got_error *err = NULL;
2929 struct got_reflist_head refs;
2930 struct got_reflist_entry *re;
2932 SIMPLEQ_INIT(&refs);
2933 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2937 SIMPLEQ_FOREACH(re, &refs, entry) {
2939 refstr = got_ref_to_str(re->ref);
2941 return got_error_from_errno("got_ref_to_str");
2942 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2946 got_ref_list_free(&refs);
2950 static const struct got_error *
2951 delete_ref(struct got_repository *repo, const char *refname)
2953 const struct got_error *err = NULL;
2954 struct got_reference *ref;
2956 err = got_ref_open(&ref, repo, refname, 0);
2960 err = got_ref_delete(ref, repo);
2965 static const struct got_error *
2966 add_ref(struct got_repository *repo, const char *refname, const char *target)
2968 const struct got_error *err = NULL;
2969 struct got_object_id *id;
2970 struct got_reference *ref = NULL;
2973 * Don't let the user create a reference named '-'.
2974 * While technically a valid reference name, this case is usually
2975 * an unintended typo.
2977 if (refname[0] == '-' && refname[1] == '\0')
2978 return got_error(GOT_ERR_BAD_REF_NAME);
2980 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2983 struct got_reference *target_ref;
2985 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2987 err = got_ref_open(&target_ref, repo, target, 0);
2990 err = got_ref_resolve(&id, repo, target_ref);
2991 got_ref_close(target_ref);
2996 err = got_ref_alloc(&ref, refname, id);
3000 err = got_ref_write(ref, repo);
3008 static const struct got_error *
3009 add_symref(struct got_repository *repo, const char *refname, const char *target)
3011 const struct got_error *err = NULL;
3012 struct got_reference *ref = NULL;
3013 struct got_reference *target_ref = NULL;
3016 * Don't let the user create a reference named '-'.
3017 * While technically a valid reference name, this case is usually
3018 * an unintended typo.
3020 if (refname[0] == '-' && refname[1] == '\0')
3021 return got_error(GOT_ERR_BAD_REF_NAME);
3023 err = got_ref_open(&target_ref, repo, target, 0);
3027 err = got_ref_alloc_symref(&ref, refname, target_ref);
3031 err = got_ref_write(ref, repo);
3034 got_ref_close(target_ref);
3040 static const struct got_error *
3041 cmd_ref(int argc, char *argv[])
3043 const struct got_error *error = NULL;
3044 struct got_repository *repo = NULL;
3045 struct got_worktree *worktree = NULL;
3046 char *cwd = NULL, *repo_path = NULL;
3047 int ch, do_list = 0, create_symref = 0;
3048 const char *delref = NULL;
3050 /* TODO: Add -s option for adding symbolic references. */
3051 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3057 repo_path = realpath(optarg, NULL);
3058 if (repo_path == NULL)
3059 err(1, "-r option");
3060 got_path_strip_trailing_slashes(repo_path);
3074 if (do_list && delref)
3075 errx(1, "-l and -d options are mutually exclusive\n");
3080 if (do_list || delref) {
3082 errx(1, "-s option cannot be used together with the "
3083 "-l or -d options");
3086 } else if (argc != 2)
3091 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3095 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3096 "sendfd unveil", NULL) == -1)
3100 cwd = getcwd(NULL, 0);
3102 error = got_error_from_errno("getcwd");
3106 if (repo_path == NULL) {
3107 error = got_worktree_open(&worktree, cwd);
3108 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3114 strdup(got_worktree_get_repo_path(worktree));
3115 if (repo_path == NULL)
3116 error = got_error_from_errno("strdup");
3120 repo_path = strdup(cwd);
3121 if (repo_path == NULL) {
3122 error = got_error_from_errno("strdup");
3128 error = got_repo_open(&repo, repo_path);
3132 error = apply_unveil(got_repo_get_path(repo), do_list,
3133 worktree ? got_worktree_get_root_path(worktree) : NULL);
3138 error = list_refs(repo);
3140 error = delete_ref(repo, delref);
3141 else if (create_symref)
3142 error = add_symref(repo, argv[0], argv[1]);
3144 error = add_ref(repo, argv[0], argv[1]);
3147 got_repo_close(repo);
3149 got_worktree_close(worktree);
3159 "usage: %s branch [-r repository] -l | -d name | "
3160 "name [commit]\n", getprogname());
3164 static const struct got_error *
3165 list_branches(struct got_repository *repo, struct got_worktree *worktree)
3167 static const struct got_error *err = NULL;
3168 struct got_reflist_head refs;
3169 struct got_reflist_entry *re;
3171 SIMPLEQ_INIT(&refs);
3173 err = got_ref_list(&refs, repo, "refs/heads",
3174 got_ref_cmp_by_name, NULL);
3178 SIMPLEQ_FOREACH(re, &refs, entry) {
3179 const char *refname, *marker = " ";
3181 refname = got_ref_get_name(re->ref);
3182 if (worktree && strcmp(refname,
3183 got_worktree_get_head_ref_name(worktree)) == 0) {
3184 struct got_object_id *id = NULL;
3185 err = got_ref_resolve(&id, repo, re->ref);
3188 if (got_object_id_cmp(id,
3189 got_worktree_get_base_commit_id(worktree)) == 0)
3195 refname += strlen("refs/heads/");
3196 refstr = got_ref_to_str(re->ref);
3198 return got_error_from_errno("got_ref_to_str");
3199 printf("%s%s: %s\n", marker, refname, refstr);
3203 got_ref_list_free(&refs);
3207 static const struct got_error *
3208 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
3209 const char *branch_name)
3211 const struct got_error *err = NULL;
3212 struct got_reference *ref = NULL;
3215 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3216 return got_error_from_errno("asprintf");
3218 err = got_ref_open(&ref, repo, refname, 0);
3223 strcmp(got_worktree_get_head_ref_name(worktree),
3224 got_ref_get_name(ref)) == 0) {
3225 err = got_error_msg(GOT_ERR_SAME_BRANCH,
3226 "will not delete this work tree's current branch");
3230 err = got_ref_delete(ref, repo);
3238 static const struct got_error *
3239 add_branch(struct got_repository *repo, const char *branch_name,
3240 const char *base_branch)
3242 const struct got_error *err = NULL;
3243 struct got_object_id *id = NULL;
3245 struct got_reference *ref = NULL;
3246 char *base_refname = NULL, *refname = NULL;
3249 * Don't let the user create a branch named '-'.
3250 * While technically a valid reference name, this case is usually
3251 * an unintended typo.
3253 if (branch_name[0] == '-' && branch_name[1] == '\0')
3254 return got_error(GOT_ERR_BAD_REF_NAME);
3256 err = match_object_id(&id, &label, base_branch,
3257 GOT_OBJ_TYPE_COMMIT, 1, repo);
3261 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3262 err = got_error_from_errno("asprintf");
3266 err = got_ref_open(&ref, repo, refname, 0);
3268 err = got_error(GOT_ERR_BRANCH_EXISTS);
3270 } else if (err->code != GOT_ERR_NOT_REF)
3273 err = got_ref_alloc(&ref, refname, id);
3277 err = got_ref_write(ref, repo);
3287 static const struct got_error *
3288 cmd_branch(int argc, char *argv[])
3290 const struct got_error *error = NULL;
3291 struct got_repository *repo = NULL;
3292 struct got_worktree *worktree = NULL;
3293 char *cwd = NULL, *repo_path = NULL;
3294 int ch, do_list = 0;
3295 const char *delref = NULL;
3297 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
3303 repo_path = realpath(optarg, NULL);
3304 if (repo_path == NULL)
3305 err(1, "-r option");
3306 got_path_strip_trailing_slashes(repo_path);
3317 if (do_list && delref)
3318 errx(1, "-l and -d options are mutually exclusive\n");
3323 if (do_list || delref) {
3326 } else if (argc < 1 || argc > 2)
3331 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3335 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3336 "sendfd unveil", NULL) == -1)
3340 cwd = getcwd(NULL, 0);
3342 error = got_error_from_errno("getcwd");
3346 if (repo_path == NULL) {
3347 error = got_worktree_open(&worktree, cwd);
3348 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3354 strdup(got_worktree_get_repo_path(worktree));
3355 if (repo_path == NULL)
3356 error = got_error_from_errno("strdup");
3360 repo_path = strdup(cwd);
3361 if (repo_path == NULL) {
3362 error = got_error_from_errno("strdup");
3368 error = got_repo_open(&repo, repo_path);
3372 error = apply_unveil(got_repo_get_path(repo), do_list,
3373 worktree ? got_worktree_get_root_path(worktree) : NULL);
3378 error = list_branches(repo, worktree);
3380 error = delete_branch(repo, worktree, delref);
3382 const char *base_branch;
3384 base_branch = worktree ?
3385 got_worktree_get_head_ref_name(worktree) :
3387 if (strncmp(base_branch, "refs/heads/", 11) == 0)
3390 base_branch = argv[1];
3391 error = add_branch(repo, argv[0], base_branch);
3395 got_repo_close(repo);
3397 got_worktree_close(worktree);
3408 "usage: %s tag [-r repository] | -l | "
3409 "[-m message] name [commit]\n", getprogname());
3414 static const struct got_error *
3415 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
3417 const struct got_error *err = NULL;
3418 struct got_reflist_entry *re, *se, *new;
3419 struct got_object_id *re_id, *se_id;
3420 struct got_tag_object *re_tag, *se_tag;
3421 time_t re_time, se_time;
3423 SIMPLEQ_FOREACH(re, tags, entry) {
3424 se = SIMPLEQ_FIRST(sorted);
3426 err = got_reflist_entry_dup(&new, re);
3429 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
3432 err = got_ref_resolve(&re_id, repo, re->ref);
3435 err = got_object_open_as_tag(&re_tag, repo, re_id);
3439 re_time = got_object_tag_get_tagger_time(re_tag);
3440 got_object_tag_close(re_tag);
3444 err = got_ref_resolve(&se_id, repo, re->ref);
3447 err = got_object_open_as_tag(&se_tag, repo, se_id);
3451 se_time = got_object_tag_get_tagger_time(se_tag);
3452 got_object_tag_close(se_tag);
3454 if (se_time > re_time) {
3455 err = got_reflist_entry_dup(&new, re);
3458 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
3461 se = SIMPLEQ_NEXT(se, entry);
3470 static const struct got_error *
3471 cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
3472 struct got_reference *ref2)
3474 const struct got_error *err = NULL;
3475 struct got_repository *repo = arg;
3476 struct got_object_id *id1, *id2 = NULL;
3477 struct got_tag_object *tag1 = NULL, *tag2 = NULL;
3478 time_t time1, time2;
3482 err = got_ref_resolve(&id1, repo, ref1);
3485 err = got_object_open_as_tag(&tag1, repo, id1);
3489 err = got_ref_resolve(&id2, repo, ref2);
3492 err = got_object_open_as_tag(&tag2, repo, id2);
3496 time1 = got_object_tag_get_tagger_time(tag1);
3497 time2 = got_object_tag_get_tagger_time(tag2);
3499 /* Put latest tags first. */
3502 else if (time1 > time2)
3505 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
3510 got_object_tag_close(tag1);
3512 got_object_tag_close(tag2);
3516 static const struct got_error *
3517 list_tags(struct got_repository *repo, struct got_worktree *worktree)
3519 static const struct got_error *err = NULL;
3520 struct got_reflist_head refs;
3521 struct got_reflist_entry *re;
3523 SIMPLEQ_INIT(&refs);
3525 err = got_ref_list(&refs, repo, "refs/tags", cmp_tags, repo);
3529 SIMPLEQ_FOREACH(re, &refs, entry) {
3530 const char *refname;
3531 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
3534 struct got_object_id *id;
3535 struct got_tag_object *tag;
3537 refname = got_ref_get_name(re->ref);
3538 if (strncmp(refname, "refs/tags/", 10) != 0)
3541 refstr = got_ref_to_str(re->ref);
3542 if (refstr == NULL) {
3543 err = got_error_from_errno("got_ref_to_str");
3546 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
3549 err = got_ref_resolve(&id, repo, re->ref);
3552 err = got_object_open_as_tag(&tag, repo, id);
3556 printf("from: %s\n", got_object_tag_get_tagger(tag));
3557 tagger_time = got_object_tag_get_tagger_time(tag);
3558 datestr = get_datestr(&tagger_time, datebuf);
3560 printf("date: %s UTC\n", datestr);
3561 err = got_object_id_str(&id_str,
3562 got_object_tag_get_object_id(tag));
3565 switch (got_object_tag_get_object_type(tag)) {
3566 case GOT_OBJ_TYPE_BLOB:
3567 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB, id_str);
3569 case GOT_OBJ_TYPE_TREE:
3570 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE, id_str);
3572 case GOT_OBJ_TYPE_COMMIT:
3573 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
3575 case GOT_OBJ_TYPE_TAG:
3576 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG, id_str);
3582 tagmsg0 = strdup(got_object_tag_get_message(tag));
3583 got_object_tag_close(tag);
3584 if (tagmsg0 == NULL) {
3585 err = got_error_from_errno("strdup");
3591 line = strsep(&tagmsg, "\n");
3593 printf(" %s\n", line);
3598 got_ref_list_free(&refs);
3602 static const struct got_error *
3603 get_tag_message(char **tagmsg, const char *commit_id_str,
3604 const char *tag_name, const char *repo_path)
3606 const struct got_error *err = NULL;
3607 char *template = NULL, *initial_content = NULL;
3608 char *tagmsg_path = NULL, *editor = NULL;
3611 if (asprintf(&template, "/tmp/got-tagmsg") == -1) {
3612 err = got_error_from_errno("asprintf");
3616 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
3617 commit_id_str, tag_name) == -1) {
3618 err = got_error_from_errno("asprintf");
3622 err = got_opentemp_named_fd(&tagmsg_path, &fd, template);
3626 dprintf(fd, initial_content);
3629 err = get_editor(&editor);
3632 err = edit_logmsg(tagmsg, editor, tagmsg_path, initial_content);
3634 if (err == NULL || err->code == GOT_ERR_COMMIT_MSG_EMPTY) {
3635 unlink(tagmsg_path);
3639 free(initial_content);
3643 /* Editor is done; we can now apply unveil(2) */
3645 err = apply_unveil(repo_path, 0, NULL);
3654 static const struct got_error *
3655 add_tag(struct got_repository *repo, const char *tag_name,
3656 const char *commit_arg, const char *tagmsg_arg)
3658 const struct got_error *err = NULL;
3659 struct got_object_id *commit_id = NULL, *tag_id = NULL;
3660 char *label = NULL, *commit_id_str = NULL;
3661 struct got_reference *ref = NULL;
3662 char *refname = NULL, *tagmsg = NULL;
3666 * Don't let the user create a tag named '-'.
3667 * While technically a valid reference name, this case is usually
3668 * an unintended typo.
3670 if (tag_name[0] == '-' && tag_name[1] == '\0')
3671 return got_error(GOT_ERR_BAD_REF_NAME);
3673 err = get_author(&tagger);
3677 err = match_object_id(&commit_id, &label, commit_arg,
3678 GOT_OBJ_TYPE_COMMIT, 1, repo);
3682 err = got_object_id_str(&commit_id_str, commit_id);
3686 if (strncmp("refs/tags/", tag_name, 10) == 0) {
3687 refname = strdup(tag_name);
3688 if (refname == NULL) {
3689 err = got_error_from_errno("strdup");
3693 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
3694 err = got_error_from_errno("asprintf");
3698 err = got_ref_open(&ref, repo, refname, 0);
3700 err = got_error(GOT_ERR_TAG_EXISTS);
3702 } else if (err->code != GOT_ERR_NOT_REF)
3705 if (tagmsg_arg == NULL) {
3706 err = get_tag_message(&tagmsg, commit_id_str,
3707 tag_name, got_repo_get_path(repo));
3712 err = got_object_tag_create(&tag_id, tag_name, commit_id,
3713 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
3717 err = got_ref_alloc(&ref, refname, tag_id);
3721 err = got_ref_write(ref, repo);
3725 err = got_object_id_str(&tag_id_str, tag_id);
3726 printf("Created tag %s\n", tag_id_str);
3733 free(commit_id_str);
3739 static const struct got_error *
3740 cmd_tag(int argc, char *argv[])
3742 const struct got_error *error = NULL;
3743 struct got_repository *repo = NULL;
3744 struct got_worktree *worktree = NULL;
3745 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
3746 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
3747 int ch, do_list = 0;
3749 while ((ch = getopt(argc, argv, "m:r:l")) != -1) {
3755 repo_path = realpath(optarg, NULL);
3756 if (repo_path == NULL)
3757 err(1, "-r option");
3758 got_path_strip_trailing_slashes(repo_path);
3774 errx(1, "-l and -m options are mutually exclusive\n");
3777 } else if (argc < 1 || argc > 2)
3780 commit_id_arg = argv[1];
3785 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3789 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3790 "sendfd unveil", NULL) == -1)
3794 cwd = getcwd(NULL, 0);
3796 error = got_error_from_errno("getcwd");
3800 if (repo_path == NULL) {
3801 error = got_worktree_open(&worktree, cwd);
3802 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3808 strdup(got_worktree_get_repo_path(worktree));
3809 if (repo_path == NULL)
3810 error = got_error_from_errno("strdup");
3814 repo_path = strdup(cwd);
3815 if (repo_path == NULL) {
3816 error = got_error_from_errno("strdup");
3822 error = got_repo_open(&repo, repo_path);
3828 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3831 error = list_tags(repo, worktree);
3834 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3839 if (commit_id_arg == NULL) {
3840 struct got_reference *head_ref;
3841 struct got_object_id *commit_id;
3842 error = got_ref_open(&head_ref, repo,
3843 worktree ? got_worktree_get_head_ref_name(worktree)
3847 error = got_ref_resolve(&commit_id, repo, head_ref);
3848 got_ref_close(head_ref);
3851 error = got_object_id_str(&commit_id_str, commit_id);
3857 error = add_tag(repo, tag_name,
3858 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
3862 got_repo_close(repo);
3864 got_worktree_close(worktree);
3867 free(commit_id_str);
3874 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
3878 static const struct got_error *
3879 cmd_add(int argc, char *argv[])
3881 const struct got_error *error = NULL;
3882 struct got_repository *repo = NULL;
3883 struct got_worktree *worktree = NULL;
3885 struct got_pathlist_head paths;
3886 struct got_pathlist_entry *pe;
3891 while ((ch = getopt(argc, argv, "")) != -1) {
3903 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3910 cwd = getcwd(NULL, 0);
3912 error = got_error_from_errno("getcwd");
3916 error = got_worktree_open(&worktree, cwd);
3920 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3924 error = apply_unveil(got_repo_get_path(repo), 1,
3925 got_worktree_get_root_path(worktree));
3929 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3933 error = got_worktree_schedule_add(worktree, &paths, print_status,
3937 got_repo_close(repo);
3939 got_worktree_close(worktree);
3940 TAILQ_FOREACH(pe, &paths, entry)
3941 free((char *)pe->path);
3942 got_pathlist_free(&paths);
3950 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
3954 static const struct got_error *
3955 print_remove_status(void *arg, unsigned char status,
3956 unsigned char staged_status, const char *path,
3957 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3958 struct got_object_id *commit_id)
3960 if (status == GOT_STATUS_NONEXISTENT)
3962 if (status == staged_status && (status == GOT_STATUS_DELETE))
3963 status = GOT_STATUS_NO_CHANGE;
3964 printf("%c%c %s\n", status, staged_status, path);
3968 static const struct got_error *
3969 cmd_remove(int argc, char *argv[])
3971 const struct got_error *error = NULL;
3972 struct got_worktree *worktree = NULL;
3973 struct got_repository *repo = NULL;
3975 struct got_pathlist_head paths;
3976 struct got_pathlist_entry *pe;
3977 int ch, delete_local_mods = 0;
3981 while ((ch = getopt(argc, argv, "f")) != -1) {
3984 delete_local_mods = 1;
3996 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4003 cwd = getcwd(NULL, 0);
4005 error = got_error_from_errno("getcwd");
4008 error = got_worktree_open(&worktree, cwd);
4012 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4016 error = apply_unveil(got_repo_get_path(repo), 1,
4017 got_worktree_get_root_path(worktree));
4021 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4025 error = got_worktree_schedule_delete(worktree, &paths,
4026 delete_local_mods, print_remove_status, NULL, repo);
4031 got_repo_close(repo);
4033 got_worktree_close(worktree);
4034 TAILQ_FOREACH(pe, &paths, entry)
4035 free((char *)pe->path);
4036 got_pathlist_free(&paths);
4044 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4045 "path ...\n", getprogname());
4049 static const struct got_error *
4050 revert_progress(void *arg, unsigned char status, const char *path)
4052 while (path[0] == '/')
4054 printf("%c %s\n", status, path);
4058 struct choose_patch_arg {
4059 FILE *patch_script_file;
4063 static const struct got_error *
4064 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4065 int nchanges, const char *action)
4068 size_t linesize = 0;
4072 case GOT_STATUS_ADD:
4073 printf("A %s\n%s this addition? [y/n] ", path, action);
4075 case GOT_STATUS_DELETE:
4076 printf("D %s\n%s this deletion? [y/n] ", path, action);
4078 case GOT_STATUS_MODIFY:
4079 if (fseek(patch_file, 0L, SEEK_SET) == -1)
4080 return got_error_from_errno("fseek");
4081 printf(GOT_COMMIT_SEP_STR);
4082 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4084 if (ferror(patch_file))
4085 return got_error_from_errno("getline");
4086 printf(GOT_COMMIT_SEP_STR);
4087 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4088 path, n, nchanges, action);
4091 return got_error_path(path, GOT_ERR_FILE_STATUS);
4097 static const struct got_error *
4098 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4099 FILE *patch_file, int n, int nchanges)
4101 const struct got_error *err = NULL;
4103 size_t linesize = 0;
4106 struct choose_patch_arg *a = arg;
4108 *choice = GOT_PATCH_CHOICE_NONE;
4110 if (a->patch_script_file) {
4112 err = show_change(status, path, patch_file, n, nchanges,
4116 linelen = getline(&line, &linesize, a->patch_script_file);
4117 if (linelen == -1) {
4118 if (ferror(a->patch_script_file))
4119 return got_error_from_errno("getline");
4122 nl = strchr(line, '\n');
4125 if (strcmp(line, "y") == 0) {
4126 *choice = GOT_PATCH_CHOICE_YES;
4128 } else if (strcmp(line, "n") == 0) {
4129 *choice = GOT_PATCH_CHOICE_NO;
4131 } else if (strcmp(line, "q") == 0 &&
4132 status == GOT_STATUS_MODIFY) {
4133 *choice = GOT_PATCH_CHOICE_QUIT;
4136 printf("invalid response '%s'\n", line);
4141 while (resp != 'y' && resp != 'n' && resp != 'q') {
4142 err = show_change(status, path, patch_file, n, nchanges,
4149 if (status == GOT_STATUS_MODIFY) {
4150 if (resp != 'y' && resp != 'n' && resp != 'q') {
4151 printf("invalid response '%c'\n", resp);
4154 } else if (resp != 'y' && resp != 'n') {
4155 printf("invalid response '%c'\n", resp);
4161 *choice = GOT_PATCH_CHOICE_YES;
4162 else if (resp == 'n')
4163 *choice = GOT_PATCH_CHOICE_NO;
4164 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4165 *choice = GOT_PATCH_CHOICE_QUIT;
4171 static const struct got_error *
4172 cmd_revert(int argc, char *argv[])
4174 const struct got_error *error = NULL;
4175 struct got_worktree *worktree = NULL;
4176 struct got_repository *repo = NULL;
4177 char *cwd = NULL, *path = NULL;
4178 struct got_pathlist_head paths;
4179 struct got_pathlist_entry *pe;
4180 int ch, can_recurse = 0, pflag = 0;
4181 FILE *patch_script_file = NULL;
4182 const char *patch_script_path = NULL;
4183 struct choose_patch_arg cpa;
4187 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4193 patch_script_path = optarg;
4208 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4209 "unveil", NULL) == -1)
4214 if (patch_script_path && !pflag)
4215 errx(1, "-F option can only be used together with -p option");
4217 cwd = getcwd(NULL, 0);
4219 error = got_error_from_errno("getcwd");
4222 error = got_worktree_open(&worktree, cwd);
4226 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4230 if (patch_script_path) {
4231 patch_script_file = fopen(patch_script_path, "r");
4232 if (patch_script_file == NULL) {
4233 error = got_error_from_errno2("fopen",
4238 error = apply_unveil(got_repo_get_path(repo), 1,
4239 got_worktree_get_root_path(worktree));
4243 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4250 TAILQ_FOREACH(pe, &paths, entry) {
4251 if (asprintf(&ondisk_path, "%s/%s",
4252 got_worktree_get_root_path(worktree),
4254 error = got_error_from_errno("asprintf");
4257 if (lstat(ondisk_path, &sb) == -1) {
4258 if (errno == ENOENT) {
4262 error = got_error_from_errno2("lstat",
4268 if (S_ISDIR(sb.st_mode)) {
4269 error = got_error_msg(GOT_ERR_BAD_PATH,
4270 "reverting directories requires -R option");
4276 cpa.patch_script_file = patch_script_file;
4277 cpa.action = "revert";
4278 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4279 pflag ? choose_patch : NULL, &cpa, repo);
4283 if (patch_script_file && fclose(patch_script_file) == EOF &&
4285 error = got_error_from_errno2("fclose", patch_script_path);
4287 got_repo_close(repo);
4289 got_worktree_close(worktree);
4298 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4303 struct collect_commit_logmsg_arg {
4304 const char *cmdline_log;
4306 const char *worktree_path;
4307 const char *branch_name;
4308 const char *repo_path;
4313 static const struct got_error *
4314 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4317 char *initial_content = NULL;
4318 struct got_pathlist_entry *pe;
4319 const struct got_error *err = NULL;
4320 char *template = NULL;
4321 struct collect_commit_logmsg_arg *a = arg;
4325 /* if a message was specified on the command line, just use it */
4326 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4327 len = strlen(a->cmdline_log) + 1;
4328 *logmsg = malloc(len + 1);
4329 if (*logmsg == NULL)
4330 return got_error_from_errno("malloc");
4331 strlcpy(*logmsg, a->cmdline_log, len);
4335 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
4336 return got_error_from_errno("asprintf");
4338 if (asprintf(&initial_content,
4339 "\n# changes to be committed on branch %s:\n",
4340 a->branch_name) == -1)
4341 return got_error_from_errno("asprintf");
4343 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
4347 dprintf(fd, initial_content);
4349 TAILQ_FOREACH(pe, commitable_paths, entry) {
4350 struct got_commitable *ct = pe->data;
4351 dprintf(fd, "# %c %s\n",
4352 got_commitable_get_status(ct),
4353 got_commitable_get_path(ct));
4357 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
4359 if (err == NULL || err->code == GOT_ERR_COMMIT_MSG_EMPTY) {
4360 unlink(a->logmsg_path);
4361 free(a->logmsg_path);
4362 a->logmsg_path = NULL;
4364 free(initial_content);
4367 /* Editor is done; we can now apply unveil(2) */
4369 err = apply_unveil(a->repo_path, 0, a->worktree_path);
4378 static const struct got_error *
4379 cmd_commit(int argc, char *argv[])
4381 const struct got_error *error = NULL;
4382 struct got_worktree *worktree = NULL;
4383 struct got_repository *repo = NULL;
4384 char *cwd = NULL, *id_str = NULL;
4385 struct got_object_id *id = NULL;
4386 const char *logmsg = NULL;
4388 struct collect_commit_logmsg_arg cl_arg;
4389 char *editor = NULL;
4390 int ch, rebase_in_progress, histedit_in_progress;
4391 struct got_pathlist_head paths;
4394 cl_arg.logmsg_path = NULL;
4396 while ((ch = getopt(argc, argv, "m:")) != -1) {
4411 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4412 "unveil", NULL) == -1)
4415 error = get_author(&author);
4419 cwd = getcwd(NULL, 0);
4421 error = got_error_from_errno("getcwd");
4424 error = got_worktree_open(&worktree, cwd);
4428 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4431 if (rebase_in_progress) {
4432 error = got_error(GOT_ERR_REBASING);
4436 error = got_worktree_histedit_in_progress(&histedit_in_progress,
4441 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4446 * unveil(2) traverses exec(2); if an editor is used we have
4447 * to apply unveil after the log message has been written.
4449 if (logmsg == NULL || strlen(logmsg) == 0)
4450 error = get_editor(&editor);
4452 error = apply_unveil(got_repo_get_path(repo), 0,
4453 got_worktree_get_root_path(worktree));
4457 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4461 cl_arg.editor = editor;
4462 cl_arg.cmdline_log = logmsg;
4463 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
4464 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
4465 if (!histedit_in_progress) {
4466 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
4467 error = got_error(GOT_ERR_COMMIT_BRANCH);
4470 cl_arg.branch_name += 11;
4472 cl_arg.repo_path = got_repo_get_path(repo);
4473 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
4474 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
4476 if (cl_arg.logmsg_path)
4477 fprintf(stderr, "%s: log message preserved in %s\n",
4478 getprogname(), cl_arg.logmsg_path);
4482 if (cl_arg.logmsg_path)
4483 unlink(cl_arg.logmsg_path);
4485 error = got_object_id_str(&id_str, id);
4488 printf("Created commit %s\n", id_str);
4490 free(cl_arg.logmsg_path);
4492 got_repo_close(repo);
4494 got_worktree_close(worktree);
4502 usage_cherrypick(void)
4504 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
4508 static const struct got_error *
4509 cmd_cherrypick(int argc, char *argv[])
4511 const struct got_error *error = NULL;
4512 struct got_worktree *worktree = NULL;
4513 struct got_repository *repo = NULL;
4514 char *cwd = NULL, *commit_id_str = NULL;
4515 struct got_object_id *commit_id = NULL;
4516 struct got_commit_object *commit = NULL;
4517 struct got_object_qid *pid;
4518 struct got_reference *head_ref = NULL;
4519 int ch, did_something = 0;
4521 while ((ch = getopt(argc, argv, "")) != -1) {
4533 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4534 "unveil", NULL) == -1)
4540 cwd = getcwd(NULL, 0);
4542 error = got_error_from_errno("getcwd");
4545 error = got_worktree_open(&worktree, cwd);
4549 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4553 error = apply_unveil(got_repo_get_path(repo), 0,
4554 got_worktree_get_root_path(worktree));
4558 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4559 GOT_OBJ_TYPE_COMMIT, repo);
4560 if (error != NULL) {
4561 struct got_reference *ref;
4562 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4564 error = got_ref_open(&ref, repo, argv[0], 0);
4567 error = got_ref_resolve(&commit_id, repo, ref);
4572 error = got_object_id_str(&commit_id_str, commit_id);
4576 error = got_ref_open(&head_ref, repo,
4577 got_worktree_get_head_ref_name(worktree), 0);
4581 error = check_same_branch(commit_id, head_ref, NULL, repo);
4583 if (error->code != GOT_ERR_ANCESTRY)
4587 error = got_error(GOT_ERR_SAME_BRANCH);
4591 error = got_object_open_as_commit(&commit, repo, commit_id);
4594 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4595 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
4596 commit_id, repo, update_progress, &did_something, check_cancelled,
4602 printf("Merged commit %s\n", commit_id_str);
4605 got_object_commit_close(commit);
4606 free(commit_id_str);
4608 got_ref_close(head_ref);
4610 got_worktree_close(worktree);
4612 got_repo_close(repo);
4619 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
4623 static const struct got_error *
4624 cmd_backout(int argc, char *argv[])
4626 const struct got_error *error = NULL;
4627 struct got_worktree *worktree = NULL;
4628 struct got_repository *repo = NULL;
4629 char *cwd = NULL, *commit_id_str = NULL;
4630 struct got_object_id *commit_id = NULL;
4631 struct got_commit_object *commit = NULL;
4632 struct got_object_qid *pid;
4633 struct got_reference *head_ref = NULL;
4634 int ch, did_something = 0;
4636 while ((ch = getopt(argc, argv, "")) != -1) {
4648 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4649 "unveil", NULL) == -1)
4655 cwd = getcwd(NULL, 0);
4657 error = got_error_from_errno("getcwd");
4660 error = got_worktree_open(&worktree, cwd);
4664 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4668 error = apply_unveil(got_repo_get_path(repo), 0,
4669 got_worktree_get_root_path(worktree));
4673 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4674 GOT_OBJ_TYPE_COMMIT, repo);
4675 if (error != NULL) {
4676 struct got_reference *ref;
4677 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4679 error = got_ref_open(&ref, repo, argv[0], 0);
4682 error = got_ref_resolve(&commit_id, repo, ref);
4687 error = got_object_id_str(&commit_id_str, commit_id);
4691 error = got_ref_open(&head_ref, repo,
4692 got_worktree_get_head_ref_name(worktree), 0);
4696 error = check_same_branch(commit_id, head_ref, NULL, repo);
4700 error = got_object_open_as_commit(&commit, repo, commit_id);
4703 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4705 error = got_error(GOT_ERR_ROOT_COMMIT);
4709 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
4710 update_progress, &did_something, check_cancelled, NULL);
4715 printf("Backed out commit %s\n", commit_id_str);
4718 got_object_commit_close(commit);
4719 free(commit_id_str);
4721 got_ref_close(head_ref);
4723 got_worktree_close(worktree);
4725 got_repo_close(repo);
4732 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
4738 trim_logmsg(char *logmsg, int limit)
4743 len = strlen(logmsg);
4747 nl = strchr(logmsg, '\n');
4752 static const struct got_error *
4753 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
4755 const struct got_error *err;
4756 char *logmsg0 = NULL;
4759 err = got_object_commit_get_logmsg(&logmsg0, commit);
4764 while (isspace((unsigned char)s[0]))
4767 *logmsg = strdup(s);
4768 if (*logmsg == NULL) {
4769 err = got_error_from_errno("strdup");
4773 trim_logmsg(*logmsg, limit);
4779 static const struct got_error *
4780 show_rebase_progress(struct got_commit_object *commit,
4781 struct got_object_id *old_id, struct got_object_id *new_id)
4783 const struct got_error *err;
4784 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
4786 err = got_object_id_str(&old_id_str, old_id);
4791 err = got_object_id_str(&new_id_str, new_id);
4796 old_id_str[12] = '\0';
4798 new_id_str[12] = '\0';
4800 err = get_short_logmsg(&logmsg, 42, commit);
4804 printf("%s -> %s: %s\n", old_id_str,
4805 new_id_str ? new_id_str : "no-op change", logmsg);
4812 static const struct got_error *
4813 rebase_progress(void *arg, unsigned char status, const char *path)
4815 unsigned char *rebase_status = arg;
4817 while (path[0] == '/')
4819 printf("%c %s\n", status, path);
4821 if (*rebase_status == GOT_STATUS_CONFLICT)
4823 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
4824 *rebase_status = status;
4828 static const struct got_error *
4829 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
4830 struct got_reference *branch, struct got_reference *new_base_branch,
4831 struct got_reference *tmp_branch, struct got_repository *repo)
4833 printf("Switching work tree to %s\n", got_ref_get_name(branch));
4834 return got_worktree_rebase_complete(worktree, fileindex,
4835 new_base_branch, tmp_branch, branch, repo);
4838 static const struct got_error *
4839 rebase_commit(struct got_pathlist_head *merged_paths,
4840 struct got_worktree *worktree, struct got_fileindex *fileindex,
4841 struct got_reference *tmp_branch,
4842 struct got_object_id *commit_id, struct got_repository *repo)
4844 const struct got_error *error;
4845 struct got_commit_object *commit;
4846 struct got_object_id *new_commit_id;
4848 error = got_object_open_as_commit(&commit, repo, commit_id);
4852 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
4853 worktree, fileindex, tmp_branch, commit, commit_id, repo);
4855 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
4857 error = show_rebase_progress(commit, commit_id, NULL);
4859 error = show_rebase_progress(commit, commit_id, new_commit_id);
4860 free(new_commit_id);
4863 got_object_commit_close(commit);
4867 struct check_path_prefix_arg {
4868 const char *path_prefix;
4873 static const struct got_error *
4874 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
4875 struct got_blob_object *blob2, struct got_object_id *id1,
4876 struct got_object_id *id2, const char *path1, const char *path2,
4877 struct got_repository *repo)
4879 struct check_path_prefix_arg *a = arg;
4881 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
4882 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
4883 return got_error(a->errcode);
4888 static const struct got_error *
4889 check_path_prefix(struct got_object_id *parent_id,
4890 struct got_object_id *commit_id, const char *path_prefix,
4891 int errcode, struct got_repository *repo)
4893 const struct got_error *err;
4894 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4895 struct got_commit_object *commit = NULL, *parent_commit = NULL;
4896 struct check_path_prefix_arg cpp_arg;
4898 if (got_path_is_root_dir(path_prefix))
4901 err = got_object_open_as_commit(&commit, repo, commit_id);
4905 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
4909 err = got_object_open_as_tree(&tree1, repo,
4910 got_object_commit_get_tree_id(parent_commit));
4914 err = got_object_open_as_tree(&tree2, repo,
4915 got_object_commit_get_tree_id(commit));
4919 cpp_arg.path_prefix = path_prefix;
4920 while (cpp_arg.path_prefix[0] == '/')
4921 cpp_arg.path_prefix++;
4922 cpp_arg.len = strlen(cpp_arg.path_prefix);
4923 cpp_arg.errcode = errcode;
4924 err = got_diff_tree(tree1, tree2, "", "", repo,
4925 check_path_prefix_in_diff, &cpp_arg, 0);
4928 got_object_tree_close(tree1);
4930 got_object_tree_close(tree2);
4932 got_object_commit_close(commit);
4934 got_object_commit_close(parent_commit);
4938 static const struct got_error *
4939 collect_commits(struct got_object_id_queue *commits,
4940 struct got_object_id *initial_commit_id,
4941 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
4942 const char *path_prefix, int path_prefix_errcode,
4943 struct got_repository *repo)
4945 const struct got_error *err = NULL;
4946 struct got_commit_graph *graph = NULL;
4947 struct got_object_id *parent_id = NULL;
4948 struct got_object_qid *qid;
4949 struct got_object_id *commit_id = initial_commit_id;
4951 err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
4955 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
4956 check_cancelled, NULL);
4959 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
4960 err = got_commit_graph_iter_next(&parent_id, graph);
4962 if (err->code == GOT_ERR_ITER_COMPLETED) {
4963 err = got_error_msg(GOT_ERR_ANCESTRY,
4964 "ran out of commits to rebase before "
4965 "youngest common ancestor commit has "
4968 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
4970 err = got_commit_graph_fetch_commits(graph, 1, repo,
4971 check_cancelled, NULL);
4975 err = check_path_prefix(parent_id, commit_id,
4976 path_prefix, path_prefix_errcode, repo);
4980 err = got_object_qid_alloc(&qid, commit_id);
4983 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
4984 commit_id = parent_id;
4988 got_commit_graph_close(graph);
4992 static const struct got_error *
4993 cmd_rebase(int argc, char *argv[])
4995 const struct got_error *error = NULL;
4996 struct got_worktree *worktree = NULL;
4997 struct got_repository *repo = NULL;
4998 struct got_fileindex *fileindex = NULL;
5000 struct got_reference *branch = NULL;
5001 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
5002 struct got_object_id *commit_id = NULL, *parent_id = NULL;
5003 struct got_object_id *resume_commit_id = NULL;
5004 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
5005 struct got_commit_object *commit = NULL;
5006 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
5007 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5008 struct got_object_id_queue commits;
5009 struct got_pathlist_head merged_paths;
5010 const struct got_object_id_queue *parent_ids;
5011 struct got_object_qid *qid, *pid;
5013 SIMPLEQ_INIT(&commits);
5014 TAILQ_INIT(&merged_paths);
5016 while ((ch = getopt(argc, argv, "ac")) != -1) {
5022 continue_rebase = 1;
5034 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5035 "unveil", NULL) == -1)
5038 if (abort_rebase && continue_rebase)
5040 else if (abort_rebase || continue_rebase) {
5043 } else if (argc != 1)
5046 cwd = getcwd(NULL, 0);
5048 error = got_error_from_errno("getcwd");
5051 error = got_worktree_open(&worktree, cwd);
5055 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5059 error = apply_unveil(got_repo_get_path(repo), 0,
5060 got_worktree_get_root_path(worktree));
5064 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5070 if (!rebase_in_progress) {
5071 error = got_error(GOT_ERR_NOT_REBASING);
5074 error = got_worktree_rebase_continue(&resume_commit_id,
5075 &new_base_branch, &tmp_branch, &branch, &fileindex,
5079 printf("Switching work tree to %s\n",
5080 got_ref_get_symref_target(new_base_branch));
5081 error = got_worktree_rebase_abort(worktree, fileindex, repo,
5082 new_base_branch, update_progress, &did_something);
5085 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5086 goto done; /* nothing else to do */
5089 if (continue_rebase) {
5090 if (!rebase_in_progress) {
5091 error = got_error(GOT_ERR_NOT_REBASING);
5094 error = got_worktree_rebase_continue(&resume_commit_id,
5095 &new_base_branch, &tmp_branch, &branch, &fileindex,
5100 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5101 resume_commit_id, repo);
5105 yca_id = got_object_id_dup(resume_commit_id);
5106 if (yca_id == NULL) {
5107 error = got_error_from_errno("got_object_id_dup");
5111 error = got_ref_open(&branch, repo, argv[0], 0);
5116 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5120 if (!continue_rebase) {
5121 struct got_object_id *base_commit_id;
5123 base_commit_id = got_worktree_get_base_commit_id(worktree);
5124 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5125 base_commit_id, branch_head_commit_id, repo,
5126 check_cancelled, NULL);
5129 if (yca_id == NULL) {
5130 error = got_error_msg(GOT_ERR_ANCESTRY,
5131 "specified branch shares no common ancestry "
5132 "with work tree's branch");
5136 error = check_same_branch(base_commit_id, branch, yca_id, repo);
5138 if (error->code != GOT_ERR_ANCESTRY)
5142 error = got_error_msg(GOT_ERR_SAME_BRANCH,
5143 "specified branch resolves to a commit which "
5144 "is already contained in work tree's branch");
5147 error = got_worktree_rebase_prepare(&new_base_branch,
5148 &tmp_branch, &fileindex, worktree, branch, repo);
5153 commit_id = branch_head_commit_id;
5154 error = got_object_open_as_commit(&commit, repo, commit_id);
5158 parent_ids = got_object_commit_get_parent_ids(commit);
5159 pid = SIMPLEQ_FIRST(parent_ids);
5161 if (!continue_rebase) {
5163 error = got_worktree_rebase_abort(worktree, fileindex,
5164 repo, new_base_branch, update_progress,
5168 printf("Rebase of %s aborted\n",
5169 got_ref_get_name(branch));
5171 error = got_error(GOT_ERR_EMPTY_REBASE);
5174 error = collect_commits(&commits, commit_id, pid->id,
5175 yca_id, got_worktree_get_path_prefix(worktree),
5176 GOT_ERR_REBASE_PATH, repo);
5177 got_object_commit_close(commit);
5182 if (SIMPLEQ_EMPTY(&commits)) {
5183 if (continue_rebase)
5184 error = rebase_complete(worktree, fileindex,
5185 branch, new_base_branch, tmp_branch, repo);
5187 error = got_error(GOT_ERR_EMPTY_REBASE);
5192 SIMPLEQ_FOREACH(qid, &commits, entry) {
5193 commit_id = qid->id;
5194 parent_id = pid ? pid->id : yca_id;
5197 error = got_worktree_rebase_merge_files(&merged_paths,
5198 worktree, fileindex, parent_id, commit_id, repo,
5199 rebase_progress, &rebase_status, check_cancelled, NULL);
5203 if (rebase_status == GOT_STATUS_CONFLICT) {
5204 got_worktree_rebase_pathlist_free(&merged_paths);
5208 error = rebase_commit(&merged_paths, worktree, fileindex,
5209 tmp_branch, commit_id, repo);
5210 got_worktree_rebase_pathlist_free(&merged_paths);
5215 if (rebase_status == GOT_STATUS_CONFLICT) {
5216 error = got_worktree_rebase_postpone(worktree, fileindex);
5219 error = got_error_msg(GOT_ERR_CONFLICTS,
5220 "conflicts must be resolved before rebasing can continue");
5222 error = rebase_complete(worktree, fileindex, branch,
5223 new_base_branch, tmp_branch, repo);
5225 got_object_id_queue_free(&commits);
5226 free(branch_head_commit_id);
5227 free(resume_commit_id);
5230 got_object_commit_close(commit);
5232 got_ref_close(branch);
5233 if (new_base_branch)
5234 got_ref_close(new_base_branch);
5236 got_ref_close(tmp_branch);
5238 got_worktree_close(worktree);
5240 got_repo_close(repo);
5245 usage_histedit(void)
5247 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script]\n",
5252 #define GOT_HISTEDIT_PICK 'p'
5253 #define GOT_HISTEDIT_EDIT 'e'
5254 #define GOT_HISTEDIT_FOLD 'f'
5255 #define GOT_HISTEDIT_DROP 'd'
5256 #define GOT_HISTEDIT_MESG 'm'
5258 static struct got_histedit_cmd {
5262 } got_histedit_cmds[] = {
5263 { GOT_HISTEDIT_PICK, "pick", "use commit" },
5264 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5265 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
5266 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5267 { GOT_HISTEDIT_MESG, "mesg",
5268 "single-line log message for commit above (open editor if empty)" },
5271 struct got_histedit_list_entry {
5272 TAILQ_ENTRY(got_histedit_list_entry) entry;
5273 struct got_object_id *commit_id;
5274 const struct got_histedit_cmd *cmd;
5277 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5279 static const struct got_error *
5280 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5281 FILE *f, struct got_repository *repo)
5283 const struct got_error *err = NULL;
5284 char *logmsg = NULL, *id_str = NULL;
5285 struct got_commit_object *commit = NULL;
5288 err = got_object_open_as_commit(&commit, repo, commit_id);
5292 err = get_short_logmsg(&logmsg, 34, commit);
5296 err = got_object_id_str(&id_str, commit_id);
5300 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
5302 err = got_ferror(f, GOT_ERR_IO);
5305 got_object_commit_close(commit);
5311 static const struct got_error *
5312 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
5313 struct got_repository *repo)
5315 const struct got_error *err = NULL;
5316 struct got_object_qid *qid;
5318 if (SIMPLEQ_EMPTY(commits))
5319 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5321 SIMPLEQ_FOREACH(qid, commits, entry) {
5322 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
5331 static const struct got_error *
5332 write_cmd_list(FILE *f)
5334 const struct got_error *err = NULL;
5337 n = fprintf(f, "# Available histedit commands:\n");
5339 return got_ferror(f, GOT_ERR_IO);
5341 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5342 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
5343 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
5346 err = got_ferror(f, GOT_ERR_IO);
5350 n = fprintf(f, "# Commits will be processed in order from top to "
5351 "bottom of this file.\n");
5353 return got_ferror(f, GOT_ERR_IO);
5357 static const struct got_error *
5358 histedit_syntax_error(int lineno)
5360 static char msg[42];
5363 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
5365 if (ret == -1 || ret >= sizeof(msg))
5366 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
5368 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
5371 static const struct got_error *
5372 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
5373 char *logmsg, struct got_repository *repo)
5375 const struct got_error *err;
5376 struct got_commit_object *folded_commit = NULL;
5377 char *id_str, *folded_logmsg = NULL;
5379 err = got_object_id_str(&id_str, hle->commit_id);
5383 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
5387 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
5390 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
5391 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
5392 folded_logmsg) == -1) {
5393 err = got_error_from_errno("asprintf");
5398 got_object_commit_close(folded_commit);
5400 free(folded_logmsg);
5404 static struct got_histedit_list_entry *
5405 get_folded_commits(struct got_histedit_list_entry *hle)
5407 struct got_histedit_list_entry *prev, *folded = NULL;
5409 prev = TAILQ_PREV(hle, got_histedit_list, entry);
5410 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
5411 prev->cmd->code == GOT_HISTEDIT_DROP)) {
5412 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
5414 prev = TAILQ_PREV(prev, got_histedit_list, entry);
5420 static const struct got_error *
5421 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
5422 struct got_repository *repo)
5424 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
5425 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
5426 const struct got_error *err = NULL;
5427 struct got_commit_object *commit = NULL;
5429 struct got_histedit_list_entry *folded = NULL;
5431 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5435 folded = get_folded_commits(hle);
5437 while (folded != hle) {
5438 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
5439 folded = TAILQ_NEXT(folded, entry);
5442 err = append_folded_commit_msg(&new_msg, folded,
5448 folded = TAILQ_NEXT(folded, entry);
5452 err = got_object_id_str(&id_str, hle->commit_id);
5455 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
5458 if (asprintf(&new_msg,
5459 "%s\n# original log message of commit %s: %s",
5460 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
5461 err = got_error_from_errno("asprintf");
5467 err = got_object_id_str(&id_str, hle->commit_id);
5471 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
5475 dprintf(fd, logmsg);
5478 err = get_editor(&editor);
5482 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
5484 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
5486 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
5489 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
5490 err = got_error_from_errno2("unlink", logmsg_path);
5496 got_object_commit_close(commit);
5500 static const struct got_error *
5501 histedit_parse_list(struct got_histedit_list *histedit_cmds,
5502 FILE *f, struct got_repository *repo)
5504 const struct got_error *err = NULL;
5505 char *line = NULL, *p, *end;
5509 const struct got_histedit_cmd *cmd;
5510 struct got_object_id *commit_id = NULL;
5511 struct got_histedit_list_entry *hle = NULL;
5514 len = getline(&line, &size, f);
5516 const struct got_error *getline_err;
5519 getline_err = got_error_from_errno("getline");
5520 err = got_ferror(f, getline_err->code);
5525 while (isspace((unsigned char)p[0]))
5527 if (p[0] == '#' || p[0] == '\0') {
5533 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5534 cmd = &got_histedit_cmds[i];
5535 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
5536 isspace((unsigned char)p[strlen(cmd->name)])) {
5537 p += strlen(cmd->name);
5540 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
5545 if (i == nitems(got_histedit_cmds)) {
5546 err = histedit_syntax_error(lineno);
5549 while (isspace((unsigned char)p[0]))
5551 if (cmd->code == GOT_HISTEDIT_MESG) {
5552 if (hle == NULL || hle->logmsg != NULL) {
5553 err = got_error(GOT_ERR_HISTEDIT_CMD);
5557 err = histedit_edit_logmsg(hle, repo);
5561 hle->logmsg = strdup(p);
5562 if (hle->logmsg == NULL) {
5563 err = got_error_from_errno("strdup");
5572 while (end[0] && !isspace((unsigned char)end[0]))
5576 err = got_object_resolve_id_str(&commit_id, repo, p);
5578 /* override error code */
5579 err = histedit_syntax_error(lineno);
5583 hle = malloc(sizeof(*hle));
5585 err = got_error_from_errno("malloc");
5589 hle->commit_id = commit_id;
5594 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
5602 static const struct got_error *
5603 histedit_check_script(struct got_histedit_list *histedit_cmds,
5604 struct got_object_id_queue *commits, struct got_repository *repo)
5606 const struct got_error *err = NULL;
5607 struct got_object_qid *qid;
5608 struct got_histedit_list_entry *hle;
5609 static char msg[80];
5612 if (TAILQ_EMPTY(histedit_cmds))
5613 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
5614 "histedit script contains no commands");
5615 if (SIMPLEQ_EMPTY(commits))
5616 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5618 SIMPLEQ_FOREACH(qid, commits, entry) {
5619 TAILQ_FOREACH(hle, histedit_cmds, entry) {
5620 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
5624 err = got_object_id_str(&id_str, qid->id);
5627 snprintf(msg, sizeof(msg),
5628 "commit %s missing from histedit script", id_str);
5630 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
5634 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
5635 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
5636 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
5637 "last commit in histedit script cannot be folded");
5642 static const struct got_error *
5643 histedit_run_editor(struct got_histedit_list *histedit_cmds,
5644 const char *path, struct got_object_id_queue *commits,
5645 struct got_repository *repo)
5647 const struct got_error *err = NULL;
5651 err = get_editor(&editor);
5655 if (spawn_editor(editor, path) == -1) {
5656 err = got_error_from_errno("failed spawning editor");
5660 f = fopen(path, "r");
5662 err = got_error_from_errno("fopen");
5665 err = histedit_parse_list(histedit_cmds, f, repo);
5669 err = histedit_check_script(histedit_cmds, commits, repo);
5671 if (f && fclose(f) != 0 && err == NULL)
5672 err = got_error_from_errno("fclose");
5677 static const struct got_error *
5678 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
5679 struct got_object_id_queue *, const char *, struct got_repository *);
5681 static const struct got_error *
5682 histedit_edit_script(struct got_histedit_list *histedit_cmds,
5683 struct got_object_id_queue *commits, struct got_repository *repo)
5685 const struct got_error *err;
5689 err = got_opentemp_named(&path, &f, "got-histedit");
5693 err = write_cmd_list(f);
5697 err = histedit_write_commit_list(commits, f, repo);
5701 if (fclose(f) != 0) {
5702 err = got_error_from_errno("fclose");
5707 err = histedit_run_editor(histedit_cmds, path, commits, repo);
5709 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5710 err->code != GOT_ERR_HISTEDIT_CMD)
5712 err = histedit_edit_list_retry(histedit_cmds, err,
5713 commits, path, repo);
5716 if (f && fclose(f) != 0 && err == NULL)
5717 err = got_error_from_errno("fclose");
5718 if (path && unlink(path) != 0 && err == NULL)
5719 err = got_error_from_errno2("unlink", path);
5724 static const struct got_error *
5725 histedit_save_list(struct got_histedit_list *histedit_cmds,
5726 struct got_worktree *worktree, struct got_repository *repo)
5728 const struct got_error *err = NULL;
5731 struct got_histedit_list_entry *hle;
5732 struct got_commit_object *commit = NULL;
5734 err = got_worktree_get_histedit_script_path(&path, worktree);
5738 f = fopen(path, "w");
5740 err = got_error_from_errno2("fopen", path);
5743 TAILQ_FOREACH(hle, histedit_cmds, entry) {
5744 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
5750 int n = fprintf(f, "%c %s\n",
5751 GOT_HISTEDIT_MESG, hle->logmsg);
5753 err = got_ferror(f, GOT_ERR_IO);
5759 if (f && fclose(f) != 0 && err == NULL)
5760 err = got_error_from_errno("fclose");
5763 got_object_commit_close(commit);
5768 histedit_free_list(struct got_histedit_list *histedit_cmds)
5770 struct got_histedit_list_entry *hle;
5772 while ((hle = TAILQ_FIRST(histedit_cmds))) {
5773 TAILQ_REMOVE(histedit_cmds, hle, entry);
5778 static const struct got_error *
5779 histedit_load_list(struct got_histedit_list *histedit_cmds,
5780 const char *path, struct got_repository *repo)
5782 const struct got_error *err = NULL;
5785 f = fopen(path, "r");
5787 err = got_error_from_errno2("fopen", path);
5791 err = histedit_parse_list(histedit_cmds, f, repo);
5793 if (f && fclose(f) != 0 && err == NULL)
5794 err = got_error_from_errno("fclose");
5798 static const struct got_error *
5799 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
5800 const struct got_error *edit_err, struct got_object_id_queue *commits,
5801 const char *path, struct got_repository *repo)
5803 const struct got_error *err = NULL, *prev_err = edit_err;
5806 while (resp != 'c' && resp != 'r' && resp != 'a') {
5807 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
5808 "or (a)bort: ", getprogname(), prev_err->msg);
5813 histedit_free_list(histedit_cmds);
5814 err = histedit_run_editor(histedit_cmds, path, commits,
5817 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5818 err->code != GOT_ERR_HISTEDIT_CMD)
5825 } else if (resp == 'r') {
5826 histedit_free_list(histedit_cmds);
5827 err = histedit_edit_script(histedit_cmds,
5830 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5831 err->code != GOT_ERR_HISTEDIT_CMD)
5838 } else if (resp == 'a') {
5839 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
5842 printf("invalid response '%c'\n", resp);
5848 static const struct got_error *
5849 histedit_complete(struct got_worktree *worktree,
5850 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5851 struct got_reference *branch, struct got_repository *repo)
5853 printf("Switching work tree to %s\n",
5854 got_ref_get_symref_target(branch));
5855 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
5859 static const struct got_error *
5860 show_histedit_progress(struct got_commit_object *commit,
5861 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
5863 const struct got_error *err;
5864 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5866 err = got_object_id_str(&old_id_str, hle->commit_id);
5871 err = got_object_id_str(&new_id_str, new_id);
5876 old_id_str[12] = '\0';
5878 new_id_str[12] = '\0';
5881 logmsg = strdup(hle->logmsg);
5882 if (logmsg == NULL) {
5883 err = got_error_from_errno("strdup");
5886 trim_logmsg(logmsg, 42);
5888 err = get_short_logmsg(&logmsg, 42, commit);
5893 switch (hle->cmd->code) {
5894 case GOT_HISTEDIT_PICK:
5895 case GOT_HISTEDIT_EDIT:
5896 printf("%s -> %s: %s\n", old_id_str,
5897 new_id_str ? new_id_str : "no-op change", logmsg);
5899 case GOT_HISTEDIT_DROP:
5900 case GOT_HISTEDIT_FOLD:
5901 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
5914 static const struct got_error *
5915 histedit_commit(struct got_pathlist_head *merged_paths,
5916 struct got_worktree *worktree, struct got_fileindex *fileindex,
5917 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
5918 struct got_repository *repo)
5920 const struct got_error *err;
5921 struct got_commit_object *commit;
5922 struct got_object_id *new_commit_id;
5924 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
5925 && hle->logmsg == NULL) {
5926 err = histedit_edit_logmsg(hle, repo);
5931 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5935 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
5936 worktree, fileindex, tmp_branch, commit, hle->commit_id,
5939 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
5941 err = show_histedit_progress(commit, hle, NULL);
5943 err = show_histedit_progress(commit, hle, new_commit_id);
5944 free(new_commit_id);
5947 got_object_commit_close(commit);
5951 static const struct got_error *
5952 histedit_skip_commit(struct got_histedit_list_entry *hle,
5953 struct got_worktree *worktree, struct got_repository *repo)
5955 const struct got_error *error;
5956 struct got_commit_object *commit;
5958 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
5963 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
5967 error = show_histedit_progress(commit, hle, NULL);
5968 got_object_commit_close(commit);
5972 static const struct got_error *
5973 cmd_histedit(int argc, char *argv[])
5975 const struct got_error *error = NULL;
5976 struct got_worktree *worktree = NULL;
5977 struct got_fileindex *fileindex = NULL;
5978 struct got_repository *repo = NULL;
5980 struct got_reference *branch = NULL;
5981 struct got_reference *tmp_branch = NULL;
5982 struct got_object_id *resume_commit_id = NULL;
5983 struct got_object_id *base_commit_id = NULL;
5984 struct got_object_id *head_commit_id = NULL;
5985 struct got_commit_object *commit = NULL;
5986 int ch, rebase_in_progress = 0, did_something;
5987 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
5988 const char *edit_script_path = NULL;
5989 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5990 struct got_object_id_queue commits;
5991 struct got_pathlist_head merged_paths;
5992 const struct got_object_id_queue *parent_ids;
5993 struct got_object_qid *pid;
5994 struct got_histedit_list histedit_cmds;
5995 struct got_histedit_list_entry *hle;
5997 SIMPLEQ_INIT(&commits);
5998 TAILQ_INIT(&histedit_cmds);
5999 TAILQ_INIT(&merged_paths);
6001 while ((ch = getopt(argc, argv, "acF:")) != -1) {
6010 edit_script_path = optarg;
6022 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6023 "unveil", NULL) == -1)
6026 if (abort_edit && continue_edit)
6032 * This command cannot apply unveil(2) in all cases because the
6033 * user may choose to run an editor to edit the histedit script
6034 * and to edit individual commit log messages.
6035 * unveil(2) traverses exec(2); if an editor is used we have to
6036 * apply unveil after edit script and log messages have been written.
6037 * XXX TODO: Make use of unveil(2) where possible.
6040 cwd = getcwd(NULL, 0);
6042 error = got_error_from_errno("getcwd");
6045 error = got_worktree_open(&worktree, cwd);
6049 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
6053 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6056 if (rebase_in_progress) {
6057 error = got_error(GOT_ERR_REBASING);
6061 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6065 if (edit_in_progress && abort_edit) {
6066 error = got_worktree_histedit_continue(&resume_commit_id,
6067 &tmp_branch, &branch, &base_commit_id, &fileindex,
6071 printf("Switching work tree to %s\n",
6072 got_ref_get_symref_target(branch));
6073 error = got_worktree_histedit_abort(worktree, fileindex, repo,
6074 branch, base_commit_id, update_progress, &did_something);
6077 printf("Histedit of %s aborted\n",
6078 got_ref_get_symref_target(branch));
6079 goto done; /* nothing else to do */
6080 } else if (abort_edit) {
6081 error = got_error(GOT_ERR_NOT_HISTEDIT);
6085 if (continue_edit) {
6088 if (!edit_in_progress) {
6089 error = got_error(GOT_ERR_NOT_HISTEDIT);
6093 error = got_worktree_get_histedit_script_path(&path, worktree);
6097 error = histedit_load_list(&histedit_cmds, path, repo);
6102 error = got_worktree_histedit_continue(&resume_commit_id,
6103 &tmp_branch, &branch, &base_commit_id, &fileindex,
6108 error = got_ref_resolve(&head_commit_id, repo, branch);
6112 error = got_object_open_as_commit(&commit, repo,
6116 parent_ids = got_object_commit_get_parent_ids(commit);
6117 pid = SIMPLEQ_FIRST(parent_ids);
6119 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6122 error = collect_commits(&commits, head_commit_id, pid->id,
6123 base_commit_id, got_worktree_get_path_prefix(worktree),
6124 GOT_ERR_HISTEDIT_PATH, repo);
6125 got_object_commit_close(commit);
6130 if (edit_in_progress) {
6131 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6135 error = got_ref_open(&branch, repo,
6136 got_worktree_get_head_ref_name(worktree), 0);
6140 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
6141 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
6142 "will not edit commit history of a branch outside "
6143 "the \"refs/heads/\" reference namespace");
6147 error = got_ref_resolve(&head_commit_id, repo, branch);
6148 got_ref_close(branch);
6153 error = got_object_open_as_commit(&commit, repo,
6157 parent_ids = got_object_commit_get_parent_ids(commit);
6158 pid = SIMPLEQ_FIRST(parent_ids);
6160 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6163 error = collect_commits(&commits, head_commit_id, pid->id,
6164 got_worktree_get_base_commit_id(worktree),
6165 got_worktree_get_path_prefix(worktree),
6166 GOT_ERR_HISTEDIT_PATH, repo);
6167 got_object_commit_close(commit);
6172 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
6173 &base_commit_id, &fileindex, worktree, repo);
6177 if (edit_script_path) {
6178 error = histedit_load_list(&histedit_cmds,
6179 edit_script_path, repo);
6181 got_worktree_histedit_abort(worktree, fileindex,
6182 repo, branch, base_commit_id,
6183 update_progress, &did_something);
6187 error = histedit_edit_script(&histedit_cmds, &commits,
6190 got_worktree_histedit_abort(worktree, fileindex,
6191 repo, branch, base_commit_id,
6192 update_progress, &did_something);
6198 error = histedit_save_list(&histedit_cmds, worktree,
6201 got_worktree_histedit_abort(worktree, fileindex,
6202 repo, branch, base_commit_id,
6203 update_progress, &did_something);
6209 error = histedit_check_script(&histedit_cmds, &commits, repo);
6213 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
6214 if (resume_commit_id) {
6215 if (got_object_id_cmp(hle->commit_id,
6216 resume_commit_id) != 0)
6219 resume_commit_id = NULL;
6220 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
6221 hle->cmd->code == GOT_HISTEDIT_FOLD) {
6222 error = histedit_skip_commit(hle, worktree,
6225 error = histedit_commit(NULL, worktree,
6226 fileindex, tmp_branch, hle, repo);
6233 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
6234 error = histedit_skip_commit(hle, worktree, repo);
6240 error = got_object_open_as_commit(&commit, repo,
6244 parent_ids = got_object_commit_get_parent_ids(commit);
6245 pid = SIMPLEQ_FIRST(parent_ids);
6247 error = got_worktree_histedit_merge_files(&merged_paths,
6248 worktree, fileindex, pid->id, hle->commit_id, repo,
6249 rebase_progress, &rebase_status, check_cancelled, NULL);
6252 got_object_commit_close(commit);
6255 if (rebase_status == GOT_STATUS_CONFLICT) {
6256 got_worktree_rebase_pathlist_free(&merged_paths);
6260 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
6262 error = got_object_id_str(&id_str, hle->commit_id);
6265 printf("Stopping histedit for amending commit %s\n",
6268 got_worktree_rebase_pathlist_free(&merged_paths);
6269 error = got_worktree_histedit_postpone(worktree,
6274 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
6275 error = histedit_skip_commit(hle, worktree, repo);
6281 error = histedit_commit(&merged_paths, worktree, fileindex,
6282 tmp_branch, hle, repo);
6283 got_worktree_rebase_pathlist_free(&merged_paths);
6288 if (rebase_status == GOT_STATUS_CONFLICT) {
6289 error = got_worktree_histedit_postpone(worktree, fileindex);
6292 error = got_error_msg(GOT_ERR_CONFLICTS,
6293 "conflicts must be resolved before rebasing can continue");
6295 error = histedit_complete(worktree, fileindex, tmp_branch,
6298 got_object_id_queue_free(&commits);
6299 histedit_free_list(&histedit_cmds);
6300 free(head_commit_id);
6301 free(base_commit_id);
6302 free(resume_commit_id);
6304 got_object_commit_close(commit);
6306 got_ref_close(branch);
6308 got_ref_close(tmp_branch);
6310 got_worktree_close(worktree);
6312 got_repo_close(repo);
6319 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
6320 "[file-path ...]\n",
6325 static const struct got_error *
6326 print_stage(void *arg, unsigned char status, unsigned char staged_status,
6327 const char *path, struct got_object_id *blob_id,
6328 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
6330 const struct got_error *err = NULL;
6331 char *id_str = NULL;
6333 if (staged_status != GOT_STATUS_ADD &&
6334 staged_status != GOT_STATUS_MODIFY &&
6335 staged_status != GOT_STATUS_DELETE)
6338 if (staged_status == GOT_STATUS_ADD ||
6339 staged_status == GOT_STATUS_MODIFY)
6340 err = got_object_id_str(&id_str, staged_blob_id);
6342 err = got_object_id_str(&id_str, blob_id);
6346 printf("%s %c %s\n", id_str, staged_status, path);
6351 static const struct got_error *
6352 cmd_stage(int argc, char *argv[])
6354 const struct got_error *error = NULL;
6355 struct got_repository *repo = NULL;
6356 struct got_worktree *worktree = NULL;
6358 struct got_pathlist_head paths;
6359 struct got_pathlist_entry *pe;
6360 int ch, list_stage = 0, pflag = 0;
6361 FILE *patch_script_file = NULL;
6362 const char *patch_script_path = NULL;
6363 struct choose_patch_arg cpa;
6367 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
6376 patch_script_path = optarg;
6388 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6389 "unveil", NULL) == -1)
6392 if (list_stage && (pflag || patch_script_path))
6393 errx(1, "-l option cannot be used with other options");
6394 if (patch_script_path && !pflag)
6395 errx(1, "-F option can only be used together with -p option");
6397 cwd = getcwd(NULL, 0);
6399 error = got_error_from_errno("getcwd");
6403 error = got_worktree_open(&worktree, cwd);
6407 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
6411 if (patch_script_path) {
6412 patch_script_file = fopen(patch_script_path, "r");
6413 if (patch_script_file == NULL) {
6414 error = got_error_from_errno2("fopen",
6419 error = apply_unveil(got_repo_get_path(repo), 1,
6420 got_worktree_get_root_path(worktree));
6424 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6429 error = got_worktree_status(worktree, &paths, repo,
6430 print_stage, NULL, check_cancelled, NULL);
6432 cpa.patch_script_file = patch_script_file;
6433 cpa.action = "stage";
6434 error = got_worktree_stage(worktree, &paths,
6435 pflag ? NULL : print_status, NULL,
6436 pflag ? choose_patch : NULL, &cpa, repo);
6439 if (patch_script_file && fclose(patch_script_file) == EOF &&
6441 error = got_error_from_errno2("fclose", patch_script_path);
6443 got_repo_close(repo);
6445 got_worktree_close(worktree);
6446 TAILQ_FOREACH(pe, &paths, entry)
6447 free((char *)pe->path);
6448 got_pathlist_free(&paths);
6456 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
6457 "[file-path ...]\n",
6463 static const struct got_error *
6464 cmd_unstage(int argc, char *argv[])
6466 const struct got_error *error = NULL;
6467 struct got_repository *repo = NULL;
6468 struct got_worktree *worktree = NULL;
6470 struct got_pathlist_head paths;
6471 struct got_pathlist_entry *pe;
6472 int ch, did_something = 0, pflag = 0;
6473 FILE *patch_script_file = NULL;
6474 const char *patch_script_path = NULL;
6475 struct choose_patch_arg cpa;
6479 while ((ch = getopt(argc, argv, "pF:")) != -1) {
6485 patch_script_path = optarg;
6497 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6498 "unveil", NULL) == -1)
6501 if (patch_script_path && !pflag)
6502 errx(1, "-F option can only be used together with -p option");
6504 cwd = getcwd(NULL, 0);
6506 error = got_error_from_errno("getcwd");
6510 error = got_worktree_open(&worktree, cwd);
6514 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
6518 if (patch_script_path) {
6519 patch_script_file = fopen(patch_script_path, "r");
6520 if (patch_script_file == NULL) {
6521 error = got_error_from_errno2("fopen",
6527 error = apply_unveil(got_repo_get_path(repo), 1,
6528 got_worktree_get_root_path(worktree));
6532 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6536 cpa.patch_script_file = patch_script_file;
6537 cpa.action = "unstage";
6538 error = got_worktree_unstage(worktree, &paths, update_progress,
6539 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
6541 if (patch_script_file && fclose(patch_script_file) == EOF &&
6543 error = got_error_from_errno2("fclose", patch_script_path);
6545 got_repo_close(repo);
6547 got_worktree_close(worktree);
6548 TAILQ_FOREACH(pe, &paths, entry)
6549 free((char *)pe->path);
6550 got_pathlist_free(&paths);
6558 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
6559 "arg1 [arg2 ...]\n", getprogname());
6563 static const struct got_error *
6564 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6566 const struct got_error *err;
6567 struct got_blob_object *blob;
6569 err = got_object_open_as_blob(&blob, repo, id, 8192);
6573 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
6574 got_object_blob_close(blob);
6578 static const struct got_error *
6579 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6581 const struct got_error *err;
6582 struct got_tree_object *tree;
6583 const struct got_tree_entries *entries;
6584 struct got_tree_entry *te;
6586 err = got_object_open_as_tree(&tree, repo, id);
6590 entries = got_object_tree_get_entries(tree);
6591 te = SIMPLEQ_FIRST(&entries->head);
6594 if (sigint_received || sigpipe_received)
6596 err = got_object_id_str(&id_str, te->id);
6599 fprintf(outfile, "%s %.7o %s\n", id_str, te->mode, te->name);
6601 te = SIMPLEQ_NEXT(te, entry);
6604 got_object_tree_close(tree);
6608 static const struct got_error *
6609 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6611 const struct got_error *err;
6612 struct got_commit_object *commit;
6613 const struct got_object_id_queue *parent_ids;
6614 struct got_object_qid *pid;
6615 char *id_str = NULL;
6616 const char *logmsg = NULL;
6618 err = got_object_open_as_commit(&commit, repo, id);
6622 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
6626 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
6627 parent_ids = got_object_commit_get_parent_ids(commit);
6628 fprintf(outfile, "numparents %d\n",
6629 got_object_commit_get_nparents(commit));
6630 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
6632 err = got_object_id_str(&pid_str, pid->id);
6635 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
6638 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
6639 got_object_commit_get_author(commit),
6640 got_object_commit_get_author_time(commit));
6642 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
6643 got_object_commit_get_author(commit),
6644 got_object_commit_get_committer_time(commit));
6646 logmsg = got_object_commit_get_logmsg_raw(commit);
6647 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
6648 fprintf(outfile, "%s", logmsg);
6651 got_object_commit_close(commit);
6655 static const struct got_error *
6656 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6658 const struct got_error *err;
6659 struct got_tag_object *tag;
6660 char *id_str = NULL;
6661 const char *tagmsg = NULL;
6663 err = got_object_open_as_tag(&tag, repo, id);
6667 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
6671 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
6673 switch (got_object_tag_get_object_type(tag)) {
6674 case GOT_OBJ_TYPE_BLOB:
6675 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
6676 GOT_OBJ_LABEL_BLOB);
6678 case GOT_OBJ_TYPE_TREE:
6679 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
6680 GOT_OBJ_LABEL_TREE);
6682 case GOT_OBJ_TYPE_COMMIT:
6683 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
6684 GOT_OBJ_LABEL_COMMIT);
6686 case GOT_OBJ_TYPE_TAG:
6687 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
6694 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
6695 got_object_tag_get_name(tag));
6697 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
6698 got_object_tag_get_tagger(tag),
6699 got_object_tag_get_tagger_time(tag));
6701 tagmsg = got_object_tag_get_message(tag);
6702 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
6703 fprintf(outfile, "%s", tagmsg);
6706 got_object_tag_close(tag);
6710 static const struct got_error *
6711 cmd_cat(int argc, char *argv[])
6713 const struct got_error *error;
6714 struct got_repository *repo = NULL;
6715 struct got_worktree *worktree = NULL;
6716 char *cwd = NULL, *repo_path = NULL, *label = NULL;
6717 const char *commit_id_str = NULL;
6718 struct got_object_id *id = NULL, *commit_id = NULL;
6719 int ch, obj_type, i, force_path = 0;
6722 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6727 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
6730 commit_id_str = optarg;
6733 repo_path = realpath(optarg, NULL);
6734 if (repo_path == NULL)
6735 err(1, "-r option");
6736 got_path_strip_trailing_slashes(repo_path);
6750 cwd = getcwd(NULL, 0);
6752 error = got_error_from_errno("getcwd");
6755 error = got_worktree_open(&worktree, cwd);
6756 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6759 if (repo_path == NULL) {
6761 got_worktree_get_repo_path(worktree));
6762 if (repo_path == NULL) {
6763 error = got_error_from_errno("strdup");
6769 if (repo_path == NULL) {
6770 repo_path = getcwd(NULL, 0);
6771 if (repo_path == NULL)
6772 return got_error_from_errno("getcwd");
6775 error = got_repo_open(&repo, repo_path);
6780 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6784 if (commit_id_str == NULL)
6785 commit_id_str = GOT_REF_HEAD;
6786 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
6790 for (i = 0; i < argc; i++) {
6792 error = got_object_id_by_path(&id, repo, commit_id,
6797 error = match_object_id(&id, &label, argv[i],
6798 GOT_OBJ_TYPE_ANY, 0, repo);
6800 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
6801 error->code != GOT_ERR_NOT_REF)
6803 error = got_object_id_by_path(&id, repo,
6804 commit_id, argv[i]);
6810 error = got_object_get_type(&obj_type, repo, id);
6815 case GOT_OBJ_TYPE_BLOB:
6816 error = cat_blob(id, repo, stdout);
6818 case GOT_OBJ_TYPE_TREE:
6819 error = cat_tree(id, repo, stdout);
6821 case GOT_OBJ_TYPE_COMMIT:
6822 error = cat_commit(id, repo, stdout);
6824 case GOT_OBJ_TYPE_TAG:
6825 error = cat_tag(id, repo, stdout);
6828 error = got_error(GOT_ERR_OBJ_TYPE);
6844 got_worktree_close(worktree);
6846 const struct got_error *repo_error;
6847 repo_error = got_repo_close(repo);