2 * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Copyright (c) 2020 Ori Bernstein <ori@openbsd.org>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/queue.h>
21 #include <sys/types.h>
44 #include "got_version.h"
45 #include "got_error.h"
46 #include "got_object.h"
47 #include "got_reference.h"
48 #include "got_repository.h"
50 #include "got_cancel.h"
51 #include "got_worktree.h"
53 #include "got_commit_graph.h"
54 #include "got_fetch.h"
56 #include "got_blame.h"
57 #include "got_privsep.h"
58 #include "got_opentemp.h"
59 #include "got_gotconfig.h"
61 #include "got_patch.h"
66 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
69 static volatile sig_atomic_t sigint_received;
70 static volatile sig_atomic_t sigpipe_received;
73 catch_sigint(int signo)
79 catch_sigpipe(int signo)
87 const struct got_error *(*cmd_main)(int, char *[]);
88 void (*cmd_usage)(void);
89 const char *cmd_alias;
92 __dead static void usage(int, int);
93 __dead static void usage_import(void);
94 __dead static void usage_clone(void);
95 __dead static void usage_fetch(void);
96 __dead static void usage_checkout(void);
97 __dead static void usage_update(void);
98 __dead static void usage_log(void);
99 __dead static void usage_diff(void);
100 __dead static void usage_blame(void);
101 __dead static void usage_tree(void);
102 __dead static void usage_status(void);
103 __dead static void usage_ref(void);
104 __dead static void usage_branch(void);
105 __dead static void usage_tag(void);
106 __dead static void usage_add(void);
107 __dead static void usage_remove(void);
108 __dead static void usage_patch(void);
109 __dead static void usage_revert(void);
110 __dead static void usage_commit(void);
111 __dead static void usage_send(void);
112 __dead static void usage_cherrypick(void);
113 __dead static void usage_backout(void);
114 __dead static void usage_rebase(void);
115 __dead static void usage_histedit(void);
116 __dead static void usage_integrate(void);
117 __dead static void usage_merge(void);
118 __dead static void usage_stage(void);
119 __dead static void usage_unstage(void);
120 __dead static void usage_cat(void);
121 __dead static void usage_info(void);
123 static const struct got_error* cmd_import(int, char *[]);
124 static const struct got_error* cmd_clone(int, char *[]);
125 static const struct got_error* cmd_fetch(int, char *[]);
126 static const struct got_error* cmd_checkout(int, char *[]);
127 static const struct got_error* cmd_update(int, char *[]);
128 static const struct got_error* cmd_log(int, char *[]);
129 static const struct got_error* cmd_diff(int, char *[]);
130 static const struct got_error* cmd_blame(int, char *[]);
131 static const struct got_error* cmd_tree(int, char *[]);
132 static const struct got_error* cmd_status(int, char *[]);
133 static const struct got_error* cmd_ref(int, char *[]);
134 static const struct got_error* cmd_branch(int, char *[]);
135 static const struct got_error* cmd_tag(int, char *[]);
136 static const struct got_error* cmd_add(int, char *[]);
137 static const struct got_error* cmd_remove(int, char *[]);
138 static const struct got_error* cmd_patch(int, char *[]);
139 static const struct got_error* cmd_revert(int, char *[]);
140 static const struct got_error* cmd_commit(int, char *[]);
141 static const struct got_error* cmd_send(int, char *[]);
142 static const struct got_error* cmd_cherrypick(int, char *[]);
143 static const struct got_error* cmd_backout(int, char *[]);
144 static const struct got_error* cmd_rebase(int, char *[]);
145 static const struct got_error* cmd_histedit(int, char *[]);
146 static const struct got_error* cmd_integrate(int, char *[]);
147 static const struct got_error* cmd_merge(int, char *[]);
148 static const struct got_error* cmd_stage(int, char *[]);
149 static const struct got_error* cmd_unstage(int, char *[]);
150 static const struct got_error* cmd_cat(int, char *[]);
151 static const struct got_error* cmd_info(int, char *[]);
153 static const struct got_cmd got_commands[] = {
154 { "import", cmd_import, usage_import, "im" },
155 { "clone", cmd_clone, usage_clone, "cl" },
156 { "fetch", cmd_fetch, usage_fetch, "fe" },
157 { "checkout", cmd_checkout, usage_checkout, "co" },
158 { "update", cmd_update, usage_update, "up" },
159 { "log", cmd_log, usage_log, "" },
160 { "diff", cmd_diff, usage_diff, "di" },
161 { "blame", cmd_blame, usage_blame, "bl" },
162 { "tree", cmd_tree, usage_tree, "tr" },
163 { "status", cmd_status, usage_status, "st" },
164 { "ref", cmd_ref, usage_ref, "" },
165 { "branch", cmd_branch, usage_branch, "br" },
166 { "tag", cmd_tag, usage_tag, "" },
167 { "add", cmd_add, usage_add, "" },
168 { "remove", cmd_remove, usage_remove, "rm" },
169 { "patch", cmd_patch, usage_patch, "pa" },
170 { "revert", cmd_revert, usage_revert, "rv" },
171 { "commit", cmd_commit, usage_commit, "ci" },
172 { "send", cmd_send, usage_send, "se" },
173 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
174 { "backout", cmd_backout, usage_backout, "bo" },
175 { "rebase", cmd_rebase, usage_rebase, "rb" },
176 { "histedit", cmd_histedit, usage_histedit, "he" },
177 { "integrate", cmd_integrate, usage_integrate,"ig" },
178 { "merge", cmd_merge, usage_merge, "mg" },
179 { "stage", cmd_stage, usage_stage, "sg" },
180 { "unstage", cmd_unstage, usage_unstage, "ug" },
181 { "cat", cmd_cat, usage_cat, "" },
182 { "info", cmd_info, usage_info, "" },
186 list_commands(FILE *fp)
190 fprintf(fp, "commands:");
191 for (i = 0; i < nitems(got_commands); i++) {
192 const struct got_cmd *cmd = &got_commands[i];
193 fprintf(fp, " %s", cmd->cmd_name);
199 option_conflict(char a, char b)
201 errx(1, "-%c and -%c options are mutually exclusive", a, b);
205 main(int argc, char *argv[])
207 const struct got_cmd *cmd;
210 int hflag = 0, Vflag = 0;
211 static const struct option longopts[] = {
212 { "version", no_argument, NULL, 'V' },
216 setlocale(LC_CTYPE, "");
218 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
238 got_version_print_str();
243 usage(hflag, hflag ? 0 : 1);
245 signal(SIGINT, catch_sigint);
246 signal(SIGPIPE, catch_sigpipe);
248 for (i = 0; i < nitems(got_commands); i++) {
249 const struct got_error *error;
251 cmd = &got_commands[i];
253 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
254 strcmp(cmd->cmd_alias, argv[0]) != 0)
260 error = cmd->cmd_main(argc, argv);
261 if (error && error->code != GOT_ERR_CANCELLED &&
262 error->code != GOT_ERR_PRIVSEP_EXIT &&
263 !(sigpipe_received &&
264 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
266 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
267 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
274 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
275 list_commands(stderr);
280 usage(int hflag, int status)
282 FILE *fp = (status == 0) ? stdout : stderr;
284 fprintf(fp, "usage: %s [-hV] command [arg ...]\n",
291 static const struct got_error *
292 get_editor(char **abspath)
294 const struct got_error *err = NULL;
299 editor = getenv("VISUAL");
301 editor = getenv("EDITOR");
304 err = got_path_find_prog(abspath, editor);
309 if (*abspath == NULL) {
310 *abspath = strdup("/bin/ed");
311 if (*abspath == NULL)
312 return got_error_from_errno("strdup");
318 static const struct got_error *
319 apply_unveil(const char *repo_path, int repo_read_only,
320 const char *worktree_path)
322 const struct got_error *err;
325 if (unveil("gmon.out", "rwc") != 0)
326 return got_error_from_errno2("unveil", "gmon.out");
328 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
329 return got_error_from_errno2("unveil", repo_path);
331 if (worktree_path && unveil(worktree_path, "rwc") != 0)
332 return got_error_from_errno2("unveil", worktree_path);
334 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
335 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
337 err = got_privsep_unveil_exec_helpers();
341 if (unveil(NULL, NULL) != 0)
342 return got_error_from_errno("unveil");
350 fprintf(stderr, "usage: %s import [-b branch] [-I pattern] [-m message] "
351 "[-r repository-path] directory\n", getprogname());
356 spawn_editor(const char *editor, const char *file)
359 sig_t sighup, sigint, sigquit;
362 sighup = signal(SIGHUP, SIG_IGN);
363 sigint = signal(SIGINT, SIG_IGN);
364 sigquit = signal(SIGQUIT, SIG_IGN);
366 switch (pid = fork()) {
370 execl(editor, editor, file, (char *)NULL);
374 while (waitpid(pid, &st, 0) == -1)
379 (void)signal(SIGHUP, sighup);
380 (void)signal(SIGINT, sigint);
381 (void)signal(SIGQUIT, sigquit);
383 if (!WIFEXITED(st)) {
388 return WEXITSTATUS(st);
391 static const struct got_error *
392 read_logmsg(char **logmsg, size_t *len, FILE *fp, size_t filesize)
394 const struct got_error *err = NULL;
401 if (fseeko(fp, 0L, SEEK_SET) == -1)
402 return got_error_from_errno("fseeko");
404 *logmsg = malloc(filesize + 1);
406 return got_error_from_errno("malloc");
409 while (getline(&line, &linesize, fp) != -1) {
410 if (line[0] == '#' || (*len == 0 && line[0] == '\n'))
411 continue; /* remove comments and leading empty lines */
412 *len = strlcat(*logmsg, line, filesize + 1);
413 if (*len >= filesize + 1) {
414 err = got_error(GOT_ERR_NO_SPACE);
419 err = got_ferror(fp, GOT_ERR_IO);
423 while (*len > 0 && (*logmsg)[*len - 1] == '\n') {
424 (*logmsg)[*len - 1] = '\0';
437 static const struct got_error *
438 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
439 const char *initial_content, size_t initial_content_len,
440 int require_modification)
442 const struct got_error *err = NULL;
449 if (stat(logmsg_path, &st) == -1)
450 return got_error_from_errno2("stat", logmsg_path);
452 if (spawn_editor(editor, logmsg_path) == -1)
453 return got_error_from_errno("failed spawning editor");
455 if (require_modification) {
456 struct timespec timeout;
460 nanosleep(&timeout, NULL);
463 if (stat(logmsg_path, &st2) == -1)
464 return got_error_from_errno("stat");
466 if (require_modification && st.st_size == st2.st_size &&
467 timespeccmp(&st.st_mtim, &st2.st_mtim, ==))
468 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
469 "no changes made to commit message, aborting");
471 fp = fopen(logmsg_path, "re");
473 err = got_error_from_errno("fopen");
477 /* strip comments and leading/trailing newlines */
478 err = read_logmsg(logmsg, &logmsg_len, fp, st2.st_size);
481 if (logmsg_len == 0) {
482 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
483 "commit message cannot be empty, aborting");
487 if (fp && fclose(fp) == EOF && err == NULL)
488 err = got_error_from_errno("fclose");
496 static const struct got_error *
497 collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
498 const char *path_dir, const char *branch_name)
500 char *initial_content = NULL;
501 const struct got_error *err = NULL;
502 int initial_content_len;
505 initial_content_len = asprintf(&initial_content,
506 "\n# %s to be imported to branch %s\n", path_dir,
508 if (initial_content_len == -1)
509 return got_error_from_errno("asprintf");
511 err = got_opentemp_named_fd(logmsg_path, &fd,
512 GOT_TMPDIR_STR "/got-importmsg", "");
516 if (write(fd, initial_content, initial_content_len) == -1) {
517 err = got_error_from_errno2("write", *logmsg_path);
521 err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content,
522 initial_content_len, 1);
524 if (fd != -1 && close(fd) == -1 && err == NULL)
525 err = got_error_from_errno2("close", *logmsg_path);
526 free(initial_content);
534 static const struct got_error *
535 import_progress(void *arg, const char *path)
537 printf("A %s\n", path);
541 static const struct got_error *
542 valid_author(const char *author)
544 const char *email = author;
547 * Git' expects the author (or committer) to be in the form
548 * "name <email>", which are mostly free form (see the
549 * "committer" description in git-fast-import(1)). We're only
550 * doing this to avoid git's object parser breaking on commits
554 while (*author && *author != '\n' && *author != '<' && *author != '>')
556 if (author != email && *author == '<' && *(author - 1) != ' ')
557 return got_error_fmt(GOT_ERR_COMMIT_BAD_AUTHOR, "%s: space "
558 "between author name and email required", email);
559 if (*author++ != '<')
560 return got_error_fmt(GOT_ERR_COMMIT_NO_EMAIL, "%s", email);
561 while (*author && *author != '\n' && *author != '<' && *author != '>')
563 if (strcmp(author, ">") != 0)
564 return got_error_fmt(GOT_ERR_COMMIT_NO_EMAIL, "%s", email);
568 static const struct got_error *
569 get_author(char **author, struct got_repository *repo,
570 struct got_worktree *worktree)
572 const struct got_error *err = NULL;
573 const char *got_author = NULL, *name, *email;
574 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
579 worktree_conf = got_worktree_get_gotconfig(worktree);
580 repo_conf = got_repo_get_gotconfig(repo);
583 * Priority of potential author information sources, from most
584 * significant to least significant:
585 * 1) work tree's .got/got.conf file
586 * 2) repository's got.conf file
587 * 3) repository's git config file
588 * 4) environment variables
589 * 5) global git config files (in user's home directory or /etc)
593 got_author = got_gotconfig_get_author(worktree_conf);
594 if (got_author == NULL)
595 got_author = got_gotconfig_get_author(repo_conf);
596 if (got_author == NULL) {
597 name = got_repo_get_gitconfig_author_name(repo);
598 email = got_repo_get_gitconfig_author_email(repo);
600 if (asprintf(author, "%s <%s>", name, email) == -1)
601 return got_error_from_errno("asprintf");
605 got_author = getenv("GOT_AUTHOR");
606 if (got_author == NULL) {
607 name = got_repo_get_global_gitconfig_author_name(repo);
608 email = got_repo_get_global_gitconfig_author_email(
611 if (asprintf(author, "%s <%s>", name, email)
613 return got_error_from_errno("asprintf");
616 /* TODO: Look up user in password database? */
617 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
621 *author = strdup(got_author);
623 return got_error_from_errno("strdup");
625 err = valid_author(*author);
633 static const struct got_error *
634 get_allowed_signers(char **allowed_signers, struct got_repository *repo,
635 struct got_worktree *worktree)
637 const char *got_allowed_signers = NULL;
638 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
640 *allowed_signers = NULL;
643 worktree_conf = got_worktree_get_gotconfig(worktree);
644 repo_conf = got_repo_get_gotconfig(repo);
647 * Priority of potential author information sources, from most
648 * significant to least significant:
649 * 1) work tree's .got/got.conf file
650 * 2) repository's got.conf file
654 got_allowed_signers = got_gotconfig_get_allowed_signers_file(
656 if (got_allowed_signers == NULL)
657 got_allowed_signers = got_gotconfig_get_allowed_signers_file(
660 if (got_allowed_signers) {
661 *allowed_signers = strdup(got_allowed_signers);
662 if (*allowed_signers == NULL)
663 return got_error_from_errno("strdup");
668 static const struct got_error *
669 get_revoked_signers(char **revoked_signers, struct got_repository *repo,
670 struct got_worktree *worktree)
672 const char *got_revoked_signers = NULL;
673 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
675 *revoked_signers = NULL;
678 worktree_conf = got_worktree_get_gotconfig(worktree);
679 repo_conf = got_repo_get_gotconfig(repo);
682 * Priority of potential author information sources, from most
683 * significant to least significant:
684 * 1) work tree's .got/got.conf file
685 * 2) repository's got.conf file
689 got_revoked_signers = got_gotconfig_get_revoked_signers_file(
691 if (got_revoked_signers == NULL)
692 got_revoked_signers = got_gotconfig_get_revoked_signers_file(
695 if (got_revoked_signers) {
696 *revoked_signers = strdup(got_revoked_signers);
697 if (*revoked_signers == NULL)
698 return got_error_from_errno("strdup");
704 get_signer_id(struct got_repository *repo, struct got_worktree *worktree)
706 const char *got_signer_id = NULL;
707 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
710 worktree_conf = got_worktree_get_gotconfig(worktree);
711 repo_conf = got_repo_get_gotconfig(repo);
714 * Priority of potential author information sources, from most
715 * significant to least significant:
716 * 1) work tree's .got/got.conf file
717 * 2) repository's got.conf file
721 got_signer_id = got_gotconfig_get_signer_id(worktree_conf);
722 if (got_signer_id == NULL)
723 got_signer_id = got_gotconfig_get_signer_id(repo_conf);
725 return got_signer_id;
728 static const struct got_error *
729 get_gitconfig_path(char **gitconfig_path)
731 const char *homedir = getenv("HOME");
733 *gitconfig_path = NULL;
735 if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
736 return got_error_from_errno("asprintf");
742 static const struct got_error *
743 cmd_import(int argc, char *argv[])
745 const struct got_error *error = NULL;
746 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
747 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
748 const char *branch_name = NULL;
749 char *id_str = NULL, *logmsg_path = NULL;
750 char refname[PATH_MAX] = "refs/heads/";
751 struct got_repository *repo = NULL;
752 struct got_reference *branch_ref = NULL, *head_ref = NULL;
753 struct got_object_id *new_commit_id = NULL;
755 struct got_pathlist_head ignores;
756 struct got_pathlist_entry *pe;
757 int preserve_logmsg = 0;
758 int *pack_fds = NULL;
760 TAILQ_INIT(&ignores);
763 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
769 while ((ch = getopt(argc, argv, "b:I:m:r:")) != -1) {
772 branch_name = optarg;
775 if (optarg[0] == '\0')
777 error = got_pathlist_insert(&pe, &ignores, optarg,
783 logmsg = strdup(optarg);
784 if (logmsg == NULL) {
785 error = got_error_from_errno("strdup");
790 repo_path = realpath(optarg, NULL);
791 if (repo_path == NULL) {
792 error = got_error_from_errno2("realpath",
809 if (repo_path == NULL) {
810 repo_path = getcwd(NULL, 0);
811 if (repo_path == NULL)
812 return got_error_from_errno("getcwd");
814 got_path_strip_trailing_slashes(repo_path);
815 error = get_gitconfig_path(&gitconfig_path);
818 error = got_repo_pack_fds_open(&pack_fds);
821 error = got_repo_open(&repo, repo_path, gitconfig_path, pack_fds);
825 error = get_author(&author, repo, NULL);
830 * Don't let the user create a branch name with a leading '-'.
831 * While technically a valid reference name, this case is usually
832 * an unintended typo.
834 if (branch_name && branch_name[0] == '-')
835 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
837 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
838 if (error && error->code != GOT_ERR_NOT_REF)
842 n = strlcat(refname, branch_name, sizeof(refname));
843 else if (head_ref && got_ref_is_symbolic(head_ref))
844 n = strlcpy(refname, got_ref_get_symref_target(head_ref),
847 n = strlcat(refname, "main", sizeof(refname));
848 if (n >= sizeof(refname)) {
849 error = got_error(GOT_ERR_NO_SPACE);
853 error = got_ref_open(&branch_ref, repo, refname, 0);
855 if (error->code != GOT_ERR_NOT_REF)
858 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
859 "import target branch already exists");
863 path_dir = realpath(argv[0], NULL);
864 if (path_dir == NULL) {
865 error = got_error_from_errno2("realpath", argv[0]);
868 got_path_strip_trailing_slashes(path_dir);
871 * unveil(2) traverses exec(2); if an editor is used we have
872 * to apply unveil after the log message has been written.
874 if (logmsg == NULL || strlen(logmsg) == 0) {
875 error = get_editor(&editor);
879 error = collect_import_msg(&logmsg, &logmsg_path, editor,
882 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
889 if (unveil(path_dir, "r") != 0) {
890 error = got_error_from_errno2("unveil", path_dir);
896 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
903 error = got_repo_import(&new_commit_id, path_dir, logmsg,
904 author, &ignores, repo, import_progress, NULL);
911 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
918 error = got_ref_write(branch_ref, repo);
925 error = got_object_id_str(&id_str, new_commit_id);
932 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
934 if (error->code != GOT_ERR_NOT_REF) {
940 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
948 error = got_ref_write(head_ref, repo);
956 printf("Created branch %s with commit %s\n",
957 got_ref_get_name(branch_ref), id_str);
960 const struct got_error *pack_err =
961 got_repo_pack_fds_close(pack_fds);
965 if (preserve_logmsg) {
966 fprintf(stderr, "%s: log message preserved in %s\n",
967 getprogname(), logmsg_path);
968 } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
969 error = got_error_from_errno2("unlink", logmsg_path);
977 free(gitconfig_path);
979 got_ref_close(branch_ref);
981 got_ref_close(head_ref);
988 fprintf(stderr, "usage: %s clone [-almqv] [-b branch] [-R reference] "
989 "repository-URL [directory]\n", getprogname());
993 struct got_fetch_progress_arg {
994 char last_scaled_size[FMT_SCALED_STRSIZE];
999 struct got_repository *repo;
1002 int configs_created;
1004 struct got_pathlist_head *symrefs;
1005 struct got_pathlist_head *wanted_branches;
1006 struct got_pathlist_head *wanted_refs;
1010 const char *remote_repo_path;
1011 const char *git_url;
1012 int fetch_all_branches;
1013 int mirror_references;
1017 /* XXX forward declaration */
1018 static const struct got_error *
1019 create_config_files(const char *proto, const char *host, const char *port,
1020 const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1021 int mirror_references, struct got_pathlist_head *symrefs,
1022 struct got_pathlist_head *wanted_branches,
1023 struct got_pathlist_head *wanted_refs, struct got_repository *repo);
1025 static const struct got_error *
1026 fetch_progress(void *arg, const char *message, off_t packfile_size,
1027 int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
1029 const struct got_error *err = NULL;
1030 struct got_fetch_progress_arg *a = arg;
1031 char scaled_size[FMT_SCALED_STRSIZE];
1032 int p_indexed, p_resolved;
1033 int print_size = 0, print_indexed = 0, print_resolved = 0;
1036 * In order to allow a failed clone to be resumed with 'got fetch'
1037 * we try to create configuration files as soon as possible.
1038 * Once the server has sent information about its default branch
1039 * we have all required information.
1041 if (a->create_configs && !a->configs_created &&
1042 !TAILQ_EMPTY(a->config_info.symrefs)) {
1043 err = create_config_files(a->config_info.proto,
1044 a->config_info.host, a->config_info.port,
1045 a->config_info.remote_repo_path,
1046 a->config_info.git_url,
1047 a->config_info.fetch_all_branches,
1048 a->config_info.mirror_references,
1049 a->config_info.symrefs,
1050 a->config_info.wanted_branches,
1051 a->config_info.wanted_refs, a->repo);
1054 a->configs_created = 1;
1057 if (a->verbosity < 0)
1060 if (message && message[0] != '\0') {
1061 printf("\rserver: %s", message);
1066 if (packfile_size > 0 || nobj_indexed > 0) {
1067 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
1068 (a->last_scaled_size[0] == '\0' ||
1069 strcmp(scaled_size, a->last_scaled_size)) != 0) {
1071 if (strlcpy(a->last_scaled_size, scaled_size,
1072 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
1073 return got_error(GOT_ERR_NO_SPACE);
1075 if (nobj_indexed > 0) {
1076 p_indexed = (nobj_indexed * 100) / nobj_total;
1077 if (p_indexed != a->last_p_indexed) {
1078 a->last_p_indexed = p_indexed;
1083 if (nobj_resolved > 0) {
1084 p_resolved = (nobj_resolved * 100) /
1085 (nobj_total - nobj_loose);
1086 if (p_resolved != a->last_p_resolved) {
1087 a->last_p_resolved = p_resolved;
1095 if (print_size || print_indexed || print_resolved)
1098 printf("%*s fetched", FMT_SCALED_STRSIZE - 2, scaled_size);
1100 printf("; indexing %d%%", p_indexed);
1102 printf("; resolving deltas %d%%", p_resolved);
1103 if (print_size || print_indexed || print_resolved)
1109 static const struct got_error *
1110 create_symref(const char *refname, struct got_reference *target_ref,
1111 int verbosity, struct got_repository *repo)
1113 const struct got_error *err;
1114 struct got_reference *head_symref;
1116 err = got_ref_alloc_symref(&head_symref, refname, target_ref);
1120 err = got_ref_write(head_symref, repo);
1121 if (err == NULL && verbosity > 0) {
1122 printf("Created reference %s: %s\n", GOT_REF_HEAD,
1123 got_ref_get_name(target_ref));
1125 got_ref_close(head_symref);
1129 static const struct got_error *
1130 list_remote_refs(struct got_pathlist_head *symrefs,
1131 struct got_pathlist_head *refs)
1133 const struct got_error *err;
1134 struct got_pathlist_entry *pe;
1136 TAILQ_FOREACH(pe, symrefs, entry) {
1137 const char *refname = pe->path;
1138 const char *targetref = pe->data;
1140 printf("%s: %s\n", refname, targetref);
1143 TAILQ_FOREACH(pe, refs, entry) {
1144 const char *refname = pe->path;
1145 struct got_object_id *id = pe->data;
1148 err = got_object_id_str(&id_str, id);
1151 printf("%s: %s\n", refname, id_str);
1158 static const struct got_error *
1159 create_ref(const char *refname, struct got_object_id *id,
1160 int verbosity, struct got_repository *repo)
1162 const struct got_error *err = NULL;
1163 struct got_reference *ref;
1166 err = got_object_id_str(&id_str, id);
1170 err = got_ref_alloc(&ref, refname, id);
1174 err = got_ref_write(ref, repo);
1177 if (err == NULL && verbosity >= 0)
1178 printf("Created reference %s: %s\n", refname, id_str);
1185 match_wanted_ref(const char *refname, const char *wanted_ref)
1187 if (strncmp(refname, "refs/", 5) != 0)
1192 * Prevent fetching of references that won't make any
1193 * sense outside of the remote repository's context.
1195 if (strncmp(refname, "got/", 4) == 0)
1197 if (strncmp(refname, "remotes/", 8) == 0)
1200 if (strncmp(wanted_ref, "refs/", 5) == 0)
1203 /* Allow prefix match. */
1204 if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
1207 /* Allow exact match. */
1208 return (strcmp(refname, wanted_ref) == 0);
1212 is_wanted_ref(struct got_pathlist_head *wanted_refs, const char *refname)
1214 struct got_pathlist_entry *pe;
1216 TAILQ_FOREACH(pe, wanted_refs, entry) {
1217 if (match_wanted_ref(refname, pe->path))
1224 static const struct got_error *
1225 create_wanted_ref(const char *refname, struct got_object_id *id,
1226 const char *remote_repo_name, int verbosity, struct got_repository *repo)
1228 const struct got_error *err;
1229 char *remote_refname;
1231 if (strncmp("refs/", refname, 5) == 0)
1234 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1235 remote_repo_name, refname) == -1)
1236 return got_error_from_errno("asprintf");
1238 err = create_ref(remote_refname, id, verbosity, repo);
1239 free(remote_refname);
1243 static const struct got_error *
1244 create_gotconfig(const char *proto, const char *host, const char *port,
1245 const char *remote_repo_path, const char *default_branch,
1246 int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1247 struct got_pathlist_head *wanted_refs, int mirror_references,
1248 struct got_repository *repo)
1250 const struct got_error *err = NULL;
1251 char *gotconfig_path = NULL;
1252 char *gotconfig = NULL;
1253 FILE *gotconfig_file = NULL;
1254 const char *branchname = NULL;
1255 char *branches = NULL, *refs = NULL;
1258 if (!fetch_all_branches && !TAILQ_EMPTY(wanted_branches)) {
1259 struct got_pathlist_entry *pe;
1260 TAILQ_FOREACH(pe, wanted_branches, entry) {
1262 branchname = pe->path;
1263 if (strncmp(branchname, "refs/heads/", 11) == 0)
1265 if (asprintf(&s, "%s\"%s\" ",
1266 branches ? branches : "", branchname) == -1) {
1267 err = got_error_from_errno("asprintf");
1273 } else if (!fetch_all_branches && default_branch) {
1274 branchname = default_branch;
1275 if (strncmp(branchname, "refs/heads/", 11) == 0)
1277 if (asprintf(&branches, "\"%s\" ", branchname) == -1) {
1278 err = got_error_from_errno("asprintf");
1282 if (!TAILQ_EMPTY(wanted_refs)) {
1283 struct got_pathlist_entry *pe;
1284 TAILQ_FOREACH(pe, wanted_refs, entry) {
1286 const char *refname = pe->path;
1287 if (strncmp(refname, "refs/", 5) == 0)
1289 if (asprintf(&s, "%s\"%s\" ",
1290 refs ? refs : "", refname) == -1) {
1291 err = got_error_from_errno("asprintf");
1299 /* Create got.conf(5). */
1300 gotconfig_path = got_repo_get_path_gotconfig(repo);
1301 if (gotconfig_path == NULL) {
1302 err = got_error_from_errno("got_repo_get_path_gotconfig");
1305 gotconfig_file = fopen(gotconfig_path, "ae");
1306 if (gotconfig_file == NULL) {
1307 err = got_error_from_errno2("fopen", gotconfig_path);
1310 if (asprintf(&gotconfig,
1315 "\trepository \"%s\"\n"
1321 GOT_FETCH_DEFAULT_REMOTE_NAME, host, proto,
1322 port ? "\tport " : "", port ? port : "", port ? "\n" : "",
1323 remote_repo_path, branches ? "\tbranch { " : "",
1324 branches ? branches : "", branches ? "}\n" : "",
1325 refs ? "\treference { " : "", refs ? refs : "", refs ? "}\n" : "",
1326 mirror_references ? "\tmirror_references yes\n" : "",
1327 fetch_all_branches ? "\tfetch_all_branches yes\n" : "") == -1) {
1328 err = got_error_from_errno("asprintf");
1331 n = fwrite(gotconfig, 1, strlen(gotconfig), gotconfig_file);
1332 if (n != strlen(gotconfig)) {
1333 err = got_ferror(gotconfig_file, GOT_ERR_IO);
1338 if (gotconfig_file && fclose(gotconfig_file) == EOF && err == NULL)
1339 err = got_error_from_errno2("fclose", gotconfig_path);
1340 free(gotconfig_path);
1345 static const struct got_error *
1346 create_gitconfig(const char *git_url, const char *default_branch,
1347 int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1348 struct got_pathlist_head *wanted_refs, int mirror_references,
1349 struct got_repository *repo)
1351 const struct got_error *err = NULL;
1352 char *gitconfig_path = NULL;
1353 char *gitconfig = NULL;
1354 FILE *gitconfig_file = NULL;
1355 char *branches = NULL, *refs = NULL;
1356 const char *branchname;
1359 /* Create a config file Git can understand. */
1360 gitconfig_path = got_repo_get_path_gitconfig(repo);
1361 if (gitconfig_path == NULL) {
1362 err = got_error_from_errno("got_repo_get_path_gitconfig");
1365 gitconfig_file = fopen(gitconfig_path, "ae");
1366 if (gitconfig_file == NULL) {
1367 err = got_error_from_errno2("fopen", gitconfig_path);
1370 if (fetch_all_branches) {
1371 if (mirror_references) {
1372 if (asprintf(&branches,
1373 "\tfetch = refs/heads/*:refs/heads/*\n") == -1) {
1374 err = got_error_from_errno("asprintf");
1377 } else if (asprintf(&branches,
1378 "\tfetch = refs/heads/*:refs/remotes/%s/*\n",
1379 GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1380 err = got_error_from_errno("asprintf");
1383 } else if (!TAILQ_EMPTY(wanted_branches)) {
1384 struct got_pathlist_entry *pe;
1385 TAILQ_FOREACH(pe, wanted_branches, entry) {
1387 branchname = pe->path;
1388 if (strncmp(branchname, "refs/heads/", 11) == 0)
1390 if (mirror_references) {
1392 "%s\tfetch = refs/heads/%s:refs/heads/%s\n",
1393 branches ? branches : "",
1394 branchname, branchname) == -1) {
1395 err = got_error_from_errno("asprintf");
1398 } else if (asprintf(&s,
1399 "%s\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1400 branches ? branches : "",
1401 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1402 branchname) == -1) {
1403 err = got_error_from_errno("asprintf");
1411 * If the server specified a default branch, use just that one.
1412 * Otherwise fall back to fetching all branches on next fetch.
1414 if (default_branch) {
1415 branchname = default_branch;
1416 if (strncmp(branchname, "refs/heads/", 11) == 0)
1419 branchname = "*"; /* fall back to all branches */
1420 if (mirror_references) {
1421 if (asprintf(&branches,
1422 "\tfetch = refs/heads/%s:refs/heads/%s\n",
1423 branchname, branchname) == -1) {
1424 err = got_error_from_errno("asprintf");
1427 } else if (asprintf(&branches,
1428 "\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1429 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1430 branchname) == -1) {
1431 err = got_error_from_errno("asprintf");
1435 if (!TAILQ_EMPTY(wanted_refs)) {
1436 struct got_pathlist_entry *pe;
1437 TAILQ_FOREACH(pe, wanted_refs, entry) {
1439 const char *refname = pe->path;
1440 if (strncmp(refname, "refs/", 5) == 0)
1442 if (mirror_references) {
1444 "%s\tfetch = refs/%s:refs/%s\n",
1445 refs ? refs : "", refname, refname) == -1) {
1446 err = got_error_from_errno("asprintf");
1449 } else if (asprintf(&s,
1450 "%s\tfetch = refs/%s:refs/remotes/%s/%s\n",
1452 refname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1454 err = got_error_from_errno("asprintf");
1462 if (asprintf(&gitconfig,
1467 "\tfetch = refs/tags/*:refs/tags/*\n",
1468 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url, branches ? branches : "",
1469 refs ? refs : "") == -1) {
1470 err = got_error_from_errno("asprintf");
1473 n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1474 if (n != strlen(gitconfig)) {
1475 err = got_ferror(gitconfig_file, GOT_ERR_IO);
1479 if (gitconfig_file && fclose(gitconfig_file) == EOF && err == NULL)
1480 err = got_error_from_errno2("fclose", gitconfig_path);
1481 free(gitconfig_path);
1486 static const struct got_error *
1487 create_config_files(const char *proto, const char *host, const char *port,
1488 const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1489 int mirror_references, struct got_pathlist_head *symrefs,
1490 struct got_pathlist_head *wanted_branches,
1491 struct got_pathlist_head *wanted_refs, struct got_repository *repo)
1493 const struct got_error *err = NULL;
1494 const char *default_branch = NULL;
1495 struct got_pathlist_entry *pe;
1498 * If we asked for a set of wanted branches then use the first
1501 if (!TAILQ_EMPTY(wanted_branches)) {
1502 pe = TAILQ_FIRST(wanted_branches);
1503 default_branch = pe->path;
1505 /* First HEAD ref listed by server is the default branch. */
1506 TAILQ_FOREACH(pe, symrefs, entry) {
1507 const char *refname = pe->path;
1508 const char *target = pe->data;
1510 if (strcmp(refname, GOT_REF_HEAD) != 0)
1513 default_branch = target;
1518 /* Create got.conf(5). */
1519 err = create_gotconfig(proto, host, port, remote_repo_path,
1520 default_branch, fetch_all_branches, wanted_branches,
1521 wanted_refs, mirror_references, repo);
1525 /* Create a config file Git can understand. */
1526 return create_gitconfig(git_url, default_branch, fetch_all_branches,
1527 wanted_branches, wanted_refs, mirror_references, repo);
1530 static const struct got_error *
1531 cmd_clone(int argc, char *argv[])
1533 const struct got_error *error = NULL;
1534 const char *uri, *dirname;
1535 char *proto, *host, *port, *repo_name, *server_path;
1536 char *default_destdir = NULL, *id_str = NULL;
1537 const char *repo_path;
1538 struct got_repository *repo = NULL;
1539 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1540 struct got_pathlist_entry *pe;
1541 struct got_object_id *pack_hash = NULL;
1542 int ch, fetchfd = -1, fetchstatus;
1543 pid_t fetchpid = -1;
1544 struct got_fetch_progress_arg fpa;
1545 char *git_url = NULL;
1546 int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
1547 int bflag = 0, list_refs_only = 0;
1548 int *pack_fds = NULL;
1551 TAILQ_INIT(&symrefs);
1552 TAILQ_INIT(&wanted_branches);
1553 TAILQ_INIT(&wanted_refs);
1555 while ((ch = getopt(argc, argv, "ab:lmqR:v")) != -1) {
1558 fetch_all_branches = 1;
1561 error = got_pathlist_append(&wanted_branches,
1571 mirror_references = 1;
1577 error = got_pathlist_append(&wanted_refs,
1585 else if (verbosity < 3)
1596 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1597 option_conflict('a', 'b');
1598 if (list_refs_only) {
1599 if (!TAILQ_EMPTY(&wanted_branches))
1600 option_conflict('l', 'b');
1601 if (fetch_all_branches)
1602 option_conflict('l', 'a');
1603 if (mirror_references)
1604 option_conflict('l', 'm');
1605 if (!TAILQ_EMPTY(&wanted_refs))
1606 option_conflict('l', 'R');
1618 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
1623 if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1624 host, port ? ":" : "", port ? port : "",
1625 server_path[0] != '/' ? "/" : "", server_path) == -1) {
1626 error = got_error_from_errno("asprintf");
1630 if (strcmp(proto, "git") == 0) {
1632 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1633 "sendfd dns inet unveil", NULL) == -1)
1636 } else if (strcmp(proto, "git+ssh") == 0 ||
1637 strcmp(proto, "ssh") == 0) {
1639 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1640 "sendfd unveil", NULL) == -1)
1643 } else if (strcmp(proto, "http") == 0 ||
1644 strcmp(proto, "git+http") == 0) {
1645 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1648 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1651 if (dirname == NULL) {
1652 if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1653 error = got_error_from_errno("asprintf");
1656 repo_path = default_destdir;
1658 repo_path = dirname;
1660 if (!list_refs_only) {
1661 error = got_path_mkdir(repo_path);
1663 (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1664 !(error->code == GOT_ERR_ERRNO && errno == EEXIST)))
1666 if (!got_path_dir_is_empty(repo_path)) {
1667 error = got_error_path(repo_path,
1668 GOT_ERR_DIR_NOT_EMPTY);
1673 error = got_dial_apply_unveil(proto);
1677 error = apply_unveil(repo_path, 0, NULL);
1682 printf("Connecting to %s\n", git_url);
1684 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1685 server_path, verbosity);
1689 if (!list_refs_only) {
1690 error = got_repo_init(repo_path, NULL);
1693 error = got_repo_pack_fds_open(&pack_fds);
1696 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
1701 fpa.last_scaled_size[0] = '\0';
1702 fpa.last_p_indexed = -1;
1703 fpa.last_p_resolved = -1;
1704 fpa.verbosity = verbosity;
1705 fpa.create_configs = 1;
1706 fpa.configs_created = 0;
1708 fpa.config_info.symrefs = &symrefs;
1709 fpa.config_info.wanted_branches = &wanted_branches;
1710 fpa.config_info.wanted_refs = &wanted_refs;
1711 fpa.config_info.proto = proto;
1712 fpa.config_info.host = host;
1713 fpa.config_info.port = port;
1714 fpa.config_info.remote_repo_path = server_path;
1715 fpa.config_info.git_url = git_url;
1716 fpa.config_info.fetch_all_branches = fetch_all_branches;
1717 fpa.config_info.mirror_references = mirror_references;
1718 error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1719 GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1720 fetch_all_branches, &wanted_branches, &wanted_refs,
1721 list_refs_only, verbosity, fetchfd, repo, NULL, NULL, bflag,
1722 fetch_progress, &fpa);
1726 if (list_refs_only) {
1727 error = list_remote_refs(&symrefs, &refs);
1731 if (pack_hash == NULL) {
1732 error = got_error_fmt(GOT_ERR_FETCH_FAILED, "%s",
1733 "server sent an empty pack file");
1736 error = got_object_id_str(&id_str, pack_hash);
1740 printf("\nFetched %s.pack\n", id_str);
1743 /* Set up references provided with the pack file. */
1744 TAILQ_FOREACH(pe, &refs, entry) {
1745 const char *refname = pe->path;
1746 struct got_object_id *id = pe->data;
1747 char *remote_refname;
1749 if (is_wanted_ref(&wanted_refs, refname) &&
1750 !mirror_references) {
1751 error = create_wanted_ref(refname, id,
1752 GOT_FETCH_DEFAULT_REMOTE_NAME,
1753 verbosity - 1, repo);
1759 error = create_ref(refname, id, verbosity - 1, repo);
1763 if (mirror_references)
1766 if (strncmp("refs/heads/", refname, 11) != 0)
1769 if (asprintf(&remote_refname,
1770 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1771 refname + 11) == -1) {
1772 error = got_error_from_errno("asprintf");
1775 error = create_ref(remote_refname, id, verbosity - 1, repo);
1776 free(remote_refname);
1781 /* Set the HEAD reference if the server provided one. */
1782 TAILQ_FOREACH(pe, &symrefs, entry) {
1783 struct got_reference *target_ref;
1784 const char *refname = pe->path;
1785 const char *target = pe->data;
1786 char *remote_refname = NULL, *remote_target = NULL;
1788 if (strcmp(refname, GOT_REF_HEAD) != 0)
1791 error = got_ref_open(&target_ref, repo, target, 0);
1793 if (error->code == GOT_ERR_NOT_REF) {
1800 error = create_symref(refname, target_ref, verbosity, repo);
1801 got_ref_close(target_ref);
1805 if (mirror_references)
1808 if (strncmp("refs/heads/", target, 11) != 0)
1811 if (asprintf(&remote_refname,
1812 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1814 error = got_error_from_errno("asprintf");
1817 if (asprintf(&remote_target,
1818 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1819 target + 11) == -1) {
1820 error = got_error_from_errno("asprintf");
1821 free(remote_refname);
1824 error = got_ref_open(&target_ref, repo, remote_target, 0);
1826 free(remote_refname);
1827 free(remote_target);
1828 if (error->code == GOT_ERR_NOT_REF) {
1834 error = create_symref(remote_refname, target_ref,
1835 verbosity - 1, repo);
1836 free(remote_refname);
1837 free(remote_target);
1838 got_ref_close(target_ref);
1844 * We failed to set the HEAD reference. If we asked for
1845 * a set of wanted branches use the first of one of those
1846 * which could be fetched instead.
1848 TAILQ_FOREACH(pe, &wanted_branches, entry) {
1849 const char *target = pe->path;
1850 struct got_reference *target_ref;
1852 error = got_ref_open(&target_ref, repo, target, 0);
1854 if (error->code == GOT_ERR_NOT_REF) {
1861 error = create_symref(GOT_REF_HEAD, target_ref,
1863 got_ref_close(target_ref);
1869 if (!fpa.configs_created && pe != NULL) {
1870 error = create_config_files(fpa.config_info.proto,
1871 fpa.config_info.host, fpa.config_info.port,
1872 fpa.config_info.remote_repo_path,
1873 fpa.config_info.git_url,
1874 fpa.config_info.fetch_all_branches,
1875 fpa.config_info.mirror_references,
1876 fpa.config_info.symrefs,
1877 fpa.config_info.wanted_branches,
1878 fpa.config_info.wanted_refs, fpa.repo);
1885 printf("Created %s repository '%s'\n",
1886 mirror_references ? "mirrored" : "cloned", repo_path);
1889 const struct got_error *pack_err =
1890 got_repo_pack_fds_close(pack_fds);
1895 if (kill(fetchpid, SIGTERM) == -1)
1896 error = got_error_from_errno("kill");
1897 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1898 error = got_error_from_errno("waitpid");
1900 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1901 error = got_error_from_errno("close");
1903 const struct got_error *close_err = got_repo_close(repo);
1907 got_pathlist_free(&refs, GOT_PATHLIST_FREE_ALL);
1908 got_pathlist_free(&symrefs, GOT_PATHLIST_FREE_ALL);
1909 got_pathlist_free(&wanted_branches, GOT_PATHLIST_FREE_NONE);
1910 got_pathlist_free(&wanted_refs, GOT_PATHLIST_FREE_NONE);
1917 free(default_destdir);
1922 static const struct got_error *
1923 update_ref(struct got_reference *ref, struct got_object_id *new_id,
1924 int replace_tags, int verbosity, struct got_repository *repo)
1926 const struct got_error *err = NULL;
1927 char *new_id_str = NULL;
1928 struct got_object_id *old_id = NULL;
1930 err = got_object_id_str(&new_id_str, new_id);
1934 if (!replace_tags &&
1935 strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1936 err = got_ref_resolve(&old_id, repo, ref);
1939 if (got_object_id_cmp(old_id, new_id) == 0)
1941 if (verbosity >= 0) {
1942 printf("Rejecting update of existing tag %s: %s\n",
1943 got_ref_get_name(ref), new_id_str);
1948 if (got_ref_is_symbolic(ref)) {
1949 if (verbosity >= 0) {
1950 printf("Replacing reference %s: %s\n",
1951 got_ref_get_name(ref),
1952 got_ref_get_symref_target(ref));
1954 err = got_ref_change_symref_to_ref(ref, new_id);
1957 err = got_ref_write(ref, repo);
1961 err = got_ref_resolve(&old_id, repo, ref);
1964 if (got_object_id_cmp(old_id, new_id) == 0)
1967 err = got_ref_change_ref(ref, new_id);
1970 err = got_ref_write(ref, repo);
1976 printf("Updated %s: %s\n", got_ref_get_name(ref),
1984 static const struct got_error *
1985 update_symref(const char *refname, struct got_reference *target_ref,
1986 int verbosity, struct got_repository *repo)
1988 const struct got_error *err = NULL, *unlock_err;
1989 struct got_reference *symref;
1990 int symref_is_locked = 0;
1992 err = got_ref_open(&symref, repo, refname, 1);
1994 if (err->code != GOT_ERR_NOT_REF)
1996 err = got_ref_alloc_symref(&symref, refname, target_ref);
2000 err = got_ref_write(symref, repo);
2005 printf("Created reference %s: %s\n",
2006 got_ref_get_name(symref),
2007 got_ref_get_symref_target(symref));
2009 symref_is_locked = 1;
2011 if (strcmp(got_ref_get_symref_target(symref),
2012 got_ref_get_name(target_ref)) == 0)
2015 err = got_ref_change_symref(symref,
2016 got_ref_get_name(target_ref));
2020 err = got_ref_write(symref, repo);
2025 printf("Updated %s: %s\n", got_ref_get_name(symref),
2026 got_ref_get_symref_target(symref));
2030 if (symref_is_locked) {
2031 unlock_err = got_ref_unlock(symref);
2032 if (unlock_err && err == NULL)
2035 got_ref_close(symref);
2042 fprintf(stderr, "usage: %s fetch [-adlqtvX] [-b branch] "
2043 "[-R reference] [-r repository-path] [remote-repository]\n",
2048 static const struct got_error *
2049 delete_missing_ref(struct got_reference *ref,
2050 int verbosity, struct got_repository *repo)
2052 const struct got_error *err = NULL;
2053 struct got_object_id *id = NULL;
2054 char *id_str = NULL;
2056 if (got_ref_is_symbolic(ref)) {
2057 err = got_ref_delete(ref, repo);
2060 if (verbosity >= 0) {
2061 printf("Deleted %s: %s\n",
2062 got_ref_get_name(ref),
2063 got_ref_get_symref_target(ref));
2066 err = got_ref_resolve(&id, repo, ref);
2069 err = got_object_id_str(&id_str, id);
2073 err = got_ref_delete(ref, repo);
2076 if (verbosity >= 0) {
2077 printf("Deleted %s: %s\n",
2078 got_ref_get_name(ref), id_str);
2087 static const struct got_error *
2088 delete_missing_refs(struct got_pathlist_head *their_refs,
2089 struct got_pathlist_head *their_symrefs,
2090 const struct got_remote_repo *remote,
2091 int verbosity, struct got_repository *repo)
2093 const struct got_error *err = NULL, *unlock_err;
2094 struct got_reflist_head my_refs;
2095 struct got_reflist_entry *re;
2096 struct got_pathlist_entry *pe;
2097 char *remote_namespace = NULL;
2098 char *local_refname = NULL;
2100 TAILQ_INIT(&my_refs);
2102 if (asprintf(&remote_namespace, "refs/remotes/%s/", remote->name)
2104 return got_error_from_errno("asprintf");
2106 err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
2110 TAILQ_FOREACH(re, &my_refs, entry) {
2111 const char *refname = got_ref_get_name(re->ref);
2112 const char *their_refname;
2114 if (remote->mirror_references) {
2115 their_refname = refname;
2117 if (strncmp(refname, remote_namespace,
2118 strlen(remote_namespace)) == 0) {
2119 if (strcmp(refname + strlen(remote_namespace),
2122 if (asprintf(&local_refname, "refs/heads/%s",
2123 refname + strlen(remote_namespace)) == -1) {
2124 err = got_error_from_errno("asprintf");
2127 } else if (strncmp(refname, "refs/tags/", 10) != 0)
2130 their_refname = local_refname;
2133 TAILQ_FOREACH(pe, their_refs, entry) {
2134 if (strcmp(their_refname, pe->path) == 0)
2140 TAILQ_FOREACH(pe, their_symrefs, entry) {
2141 if (strcmp(their_refname, pe->path) == 0)
2147 err = delete_missing_ref(re->ref, verbosity, repo);
2151 if (local_refname) {
2152 struct got_reference *ref;
2153 err = got_ref_open(&ref, repo, local_refname, 1);
2155 if (err->code != GOT_ERR_NOT_REF)
2157 free(local_refname);
2158 local_refname = NULL;
2161 err = delete_missing_ref(ref, verbosity, repo);
2164 unlock_err = got_ref_unlock(ref);
2166 if (unlock_err && err == NULL) {
2171 free(local_refname);
2172 local_refname = NULL;
2176 got_ref_list_free(&my_refs);
2177 free(remote_namespace);
2178 free(local_refname);
2182 static const struct got_error *
2183 update_wanted_ref(const char *refname, struct got_object_id *id,
2184 const char *remote_repo_name, int verbosity, struct got_repository *repo)
2186 const struct got_error *err, *unlock_err;
2187 char *remote_refname;
2188 struct got_reference *ref;
2190 if (strncmp("refs/", refname, 5) == 0)
2193 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2194 remote_repo_name, refname) == -1)
2195 return got_error_from_errno("asprintf");
2197 err = got_ref_open(&ref, repo, remote_refname, 1);
2199 if (err->code != GOT_ERR_NOT_REF)
2201 err = create_ref(remote_refname, id, verbosity, repo);
2203 err = update_ref(ref, id, 0, verbosity, repo);
2204 unlock_err = got_ref_unlock(ref);
2205 if (unlock_err && err == NULL)
2210 free(remote_refname);
2214 static const struct got_error *
2215 delete_ref(struct got_repository *repo, struct got_reference *ref)
2217 const struct got_error *err = NULL;
2218 struct got_object_id *id = NULL;
2219 char *id_str = NULL;
2222 if (got_ref_is_symbolic(ref)) {
2223 target = got_ref_get_symref_target(ref);
2225 err = got_ref_resolve(&id, repo, ref);
2228 err = got_object_id_str(&id_str, id);
2234 err = got_ref_delete(ref, repo);
2238 printf("Deleted %s: %s\n", got_ref_get_name(ref), target);
2245 static const struct got_error *
2246 delete_refs_for_remote(struct got_repository *repo, const char *remote_name)
2248 const struct got_error *err = NULL;
2249 struct got_reflist_head refs;
2250 struct got_reflist_entry *re;
2255 if (asprintf(&prefix, "refs/remotes/%s", remote_name) == -1) {
2256 err = got_error_from_errno("asprintf");
2259 err = got_ref_list(&refs, repo, prefix, got_ref_cmp_by_name, NULL);
2263 TAILQ_FOREACH(re, &refs, entry)
2264 delete_ref(repo, re->ref);
2266 got_ref_list_free(&refs);
2270 static const struct got_error *
2271 cmd_fetch(int argc, char *argv[])
2273 const struct got_error *error = NULL, *unlock_err;
2274 char *cwd = NULL, *repo_path = NULL;
2275 const char *remote_name;
2276 char *proto = NULL, *host = NULL, *port = NULL;
2277 char *repo_name = NULL, *server_path = NULL;
2278 const struct got_remote_repo *remotes, *remote = NULL;
2280 char *id_str = NULL;
2281 struct got_repository *repo = NULL;
2282 struct got_worktree *worktree = NULL;
2283 const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
2284 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
2285 struct got_pathlist_entry *pe;
2286 struct got_reflist_head remote_refs;
2287 struct got_reflist_entry *re;
2288 struct got_object_id *pack_hash = NULL;
2289 int i, ch, fetchfd = -1, fetchstatus;
2290 pid_t fetchpid = -1;
2291 struct got_fetch_progress_arg fpa;
2292 int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
2293 int delete_refs = 0, replace_tags = 0, delete_remote = 0;
2294 int *pack_fds = NULL, have_bflag = 0;
2295 const char *remote_head = NULL, *worktree_branch = NULL;
2298 TAILQ_INIT(&symrefs);
2299 TAILQ_INIT(&remote_refs);
2300 TAILQ_INIT(&wanted_branches);
2301 TAILQ_INIT(&wanted_refs);
2303 while ((ch = getopt(argc, argv, "ab:dlqR:r:tvX")) != -1) {
2306 fetch_all_branches = 1;
2309 error = got_pathlist_append(&wanted_branches,
2325 error = got_pathlist_append(&wanted_refs,
2331 repo_path = realpath(optarg, NULL);
2332 if (repo_path == NULL)
2333 return got_error_from_errno2("realpath",
2335 got_path_strip_trailing_slashes(repo_path);
2343 else if (verbosity < 3)
2357 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
2358 option_conflict('a', 'b');
2359 if (list_refs_only) {
2360 if (!TAILQ_EMPTY(&wanted_branches))
2361 option_conflict('l', 'b');
2362 if (fetch_all_branches)
2363 option_conflict('l', 'a');
2365 option_conflict('l', 'd');
2367 option_conflict('l', 'X');
2369 if (delete_remote) {
2370 if (fetch_all_branches)
2371 option_conflict('X', 'a');
2372 if (!TAILQ_EMPTY(&wanted_branches))
2373 option_conflict('X', 'b');
2375 option_conflict('X', 'd');
2377 option_conflict('X', 't');
2378 if (!TAILQ_EMPTY(&wanted_refs))
2379 option_conflict('X', 'R');
2384 errx(1, "-X option requires a remote name");
2385 remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
2386 } else if (argc == 1)
2387 remote_name = argv[0];
2391 cwd = getcwd(NULL, 0);
2393 error = got_error_from_errno("getcwd");
2397 error = got_repo_pack_fds_open(&pack_fds);
2401 if (repo_path == NULL) {
2402 error = got_worktree_open(&worktree, cwd);
2403 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2409 strdup(got_worktree_get_repo_path(worktree));
2410 if (repo_path == NULL)
2411 error = got_error_from_errno("strdup");
2415 repo_path = strdup(cwd);
2416 if (repo_path == NULL) {
2417 error = got_error_from_errno("strdup");
2423 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
2427 if (delete_remote) {
2428 error = delete_refs_for_remote(repo, remote_name);
2429 goto done; /* nothing else to do */
2433 worktree_conf = got_worktree_get_gotconfig(worktree);
2434 if (worktree_conf) {
2435 got_gotconfig_get_remotes(&nremotes, &remotes,
2437 for (i = 0; i < nremotes; i++) {
2438 if (strcmp(remotes[i].name, remote_name) == 0) {
2439 remote = &remotes[i];
2445 if (remote == NULL) {
2446 repo_conf = got_repo_get_gotconfig(repo);
2448 got_gotconfig_get_remotes(&nremotes, &remotes,
2450 for (i = 0; i < nremotes; i++) {
2451 if (strcmp(remotes[i].name, remote_name) == 0) {
2452 remote = &remotes[i];
2458 if (remote == NULL) {
2459 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
2460 for (i = 0; i < nremotes; i++) {
2461 if (strcmp(remotes[i].name, remote_name) == 0) {
2462 remote = &remotes[i];
2467 if (remote == NULL) {
2468 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
2472 if (TAILQ_EMPTY(&wanted_branches)) {
2473 if (!fetch_all_branches)
2474 fetch_all_branches = remote->fetch_all_branches;
2475 for (i = 0; i < remote->nfetch_branches; i++) {
2476 error = got_pathlist_append(&wanted_branches,
2477 remote->fetch_branches[i], NULL);
2482 if (TAILQ_EMPTY(&wanted_refs)) {
2483 for (i = 0; i < remote->nfetch_refs; i++) {
2484 error = got_pathlist_append(&wanted_refs,
2485 remote->fetch_refs[i], NULL);
2491 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
2492 &repo_name, remote->fetch_url);
2496 if (strcmp(proto, "git") == 0) {
2498 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2499 "sendfd dns inet unveil", NULL) == -1)
2502 } else if (strcmp(proto, "git+ssh") == 0 ||
2503 strcmp(proto, "ssh") == 0) {
2505 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2506 "sendfd unveil", NULL) == -1)
2509 } else if (strcmp(proto, "http") == 0 ||
2510 strcmp(proto, "git+http") == 0) {
2511 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
2514 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
2518 error = got_dial_apply_unveil(proto);
2522 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
2526 if (verbosity >= 0) {
2527 printf("Connecting to \"%s\" %s://%s%s%s%s%s\n",
2528 remote->name, proto, host,
2529 port ? ":" : "", port ? port : "",
2530 *server_path == '/' ? "" : "/", server_path);
2533 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
2534 server_path, verbosity);
2540 * If set, get this remote's HEAD ref target so
2541 * if it has changed on the server we can fetch it.
2543 error = got_ref_list(&remote_refs, repo, "refs/remotes",
2544 got_ref_cmp_by_name, repo);
2548 TAILQ_FOREACH(re, &remote_refs, entry) {
2549 const char *remote_refname, *remote_target;
2550 size_t remote_name_len;
2552 if (!got_ref_is_symbolic(re->ref))
2555 remote_name_len = strlen(remote->name);
2556 remote_refname = got_ref_get_name(re->ref);
2558 /* we only want refs/remotes/$remote->name/HEAD */
2559 if (strncmp(remote_refname + 13, remote->name,
2560 remote_name_len) != 0)
2563 if (strcmp(remote_refname + remote_name_len + 14,
2568 * Take the name itself because we already
2569 * only match with refs/heads/ in fetch_pack().
2571 remote_target = got_ref_get_symref_target(re->ref);
2572 remote_head = remote_target + remote_name_len + 14;
2577 const char *refname;
2579 refname = got_worktree_get_head_ref_name(worktree);
2580 if (strncmp(refname, "refs/heads/", 11) == 0)
2581 worktree_branch = refname;
2585 fpa.last_scaled_size[0] = '\0';
2586 fpa.last_p_indexed = -1;
2587 fpa.last_p_resolved = -1;
2588 fpa.verbosity = verbosity;
2590 fpa.create_configs = 0;
2591 fpa.configs_created = 0;
2592 memset(&fpa.config_info, 0, sizeof(fpa.config_info));
2594 error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
2595 remote->mirror_references, fetch_all_branches, &wanted_branches,
2596 &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
2597 worktree_branch, remote_head, have_bflag, fetch_progress, &fpa);
2601 if (list_refs_only) {
2602 error = list_remote_refs(&symrefs, &refs);
2606 if (pack_hash == NULL) {
2608 printf("Already up-to-date\n");
2609 } else if (verbosity >= 0) {
2610 error = got_object_id_str(&id_str, pack_hash);
2613 printf("\nFetched %s.pack\n", id_str);
2618 /* Update references provided with the pack file. */
2619 TAILQ_FOREACH(pe, &refs, entry) {
2620 const char *refname = pe->path;
2621 struct got_object_id *id = pe->data;
2622 struct got_reference *ref;
2623 char *remote_refname;
2625 if (is_wanted_ref(&wanted_refs, refname) &&
2626 !remote->mirror_references) {
2627 error = update_wanted_ref(refname, id,
2628 remote->name, verbosity, repo);
2634 if (remote->mirror_references ||
2635 strncmp("refs/tags/", refname, 10) == 0) {
2636 error = got_ref_open(&ref, repo, refname, 1);
2638 if (error->code != GOT_ERR_NOT_REF)
2640 error = create_ref(refname, id, verbosity,
2645 error = update_ref(ref, id, replace_tags,
2647 unlock_err = got_ref_unlock(ref);
2648 if (unlock_err && error == NULL)
2654 } else if (strncmp("refs/heads/", refname, 11) == 0) {
2655 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2656 remote_name, refname + 11) == -1) {
2657 error = got_error_from_errno("asprintf");
2661 error = got_ref_open(&ref, repo, remote_refname, 1);
2663 if (error->code != GOT_ERR_NOT_REF)
2665 error = create_ref(remote_refname, id,
2670 error = update_ref(ref, id, replace_tags,
2672 unlock_err = got_ref_unlock(ref);
2673 if (unlock_err && error == NULL)
2680 /* Also create a local branch if none exists yet. */
2681 error = got_ref_open(&ref, repo, refname, 1);
2683 if (error->code != GOT_ERR_NOT_REF)
2685 error = create_ref(refname, id, verbosity,
2690 unlock_err = got_ref_unlock(ref);
2691 if (unlock_err && error == NULL)
2698 error = delete_missing_refs(&refs, &symrefs, remote,
2704 if (!remote->mirror_references) {
2705 /* Update remote HEAD reference if the server provided one. */
2706 TAILQ_FOREACH(pe, &symrefs, entry) {
2707 struct got_reference *target_ref;
2708 const char *refname = pe->path;
2709 const char *target = pe->data;
2710 char *remote_refname = NULL, *remote_target = NULL;
2712 if (strcmp(refname, GOT_REF_HEAD) != 0)
2715 if (strncmp("refs/heads/", target, 11) != 0)
2718 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2719 remote->name, refname) == -1) {
2720 error = got_error_from_errno("asprintf");
2723 if (asprintf(&remote_target, "refs/remotes/%s/%s",
2724 remote->name, target + 11) == -1) {
2725 error = got_error_from_errno("asprintf");
2726 free(remote_refname);
2730 error = got_ref_open(&target_ref, repo, remote_target,
2733 free(remote_refname);
2734 free(remote_target);
2735 if (error->code == GOT_ERR_NOT_REF) {
2741 error = update_symref(remote_refname, target_ref,
2743 free(remote_refname);
2744 free(remote_target);
2745 got_ref_close(target_ref);
2752 if (kill(fetchpid, SIGTERM) == -1)
2753 error = got_error_from_errno("kill");
2754 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
2755 error = got_error_from_errno("waitpid");
2757 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
2758 error = got_error_from_errno("close");
2760 const struct got_error *close_err = got_repo_close(repo);
2765 got_worktree_close(worktree);
2767 const struct got_error *pack_err =
2768 got_repo_pack_fds_close(pack_fds);
2772 got_pathlist_free(&refs, GOT_PATHLIST_FREE_ALL);
2773 got_pathlist_free(&symrefs, GOT_PATHLIST_FREE_ALL);
2774 got_pathlist_free(&wanted_branches, GOT_PATHLIST_FREE_NONE);
2775 got_pathlist_free(&wanted_refs, GOT_PATHLIST_FREE_NONE);
2776 got_ref_list_free(&remote_refs);
2791 usage_checkout(void)
2793 fprintf(stderr, "usage: %s checkout [-Eq] [-b branch] [-c commit] "
2794 "[-p path-prefix] repository-path [work-tree-path]\n",
2800 show_worktree_base_ref_warning(void)
2802 fprintf(stderr, "%s: warning: could not create a reference "
2803 "to the work tree's base commit; the commit could be "
2804 "garbage-collected by Git or 'gotadmin cleanup'; making the "
2805 "repository writable and running 'got update' will prevent this\n",
2809 struct got_checkout_progress_arg {
2810 const char *worktree_path;
2811 int had_base_commit_ref_error;
2815 static const struct got_error *
2816 checkout_progress(void *arg, unsigned char status, const char *path)
2818 struct got_checkout_progress_arg *a = arg;
2820 /* Base commit bump happens silently. */
2821 if (status == GOT_STATUS_BUMP_BASE)
2824 if (status == GOT_STATUS_BASE_REF_ERR) {
2825 a->had_base_commit_ref_error = 1;
2829 while (path[0] == '/')
2832 if (a->verbosity >= 0)
2833 printf("%c %s/%s\n", status, a->worktree_path, path);
2838 static const struct got_error *
2839 check_cancelled(void *arg)
2841 if (sigint_received || sigpipe_received)
2842 return got_error(GOT_ERR_CANCELLED);
2846 static const struct got_error *
2847 check_linear_ancestry(struct got_object_id *commit_id,
2848 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2849 struct got_repository *repo)
2851 const struct got_error *err = NULL;
2852 struct got_object_id *yca_id;
2854 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2855 commit_id, base_commit_id, 1, repo, check_cancelled, NULL);
2860 return got_error(GOT_ERR_ANCESTRY);
2863 * Require a straight line of history between the target commit
2864 * and the work tree's base commit.
2866 * Non-linear situations such as this require a rebase:
2868 * (commit) D F (base_commit)
2876 * 'got update' only handles linear cases:
2877 * Update forwards in time: A (base/yca) - B - C - D (commit)
2878 * Update backwards in time: D (base) - C - B - A (commit/yca)
2880 if (allow_forwards_in_time_only) {
2881 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2882 return got_error(GOT_ERR_ANCESTRY);
2883 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2884 got_object_id_cmp(base_commit_id, yca_id) != 0)
2885 return got_error(GOT_ERR_ANCESTRY);
2891 static const struct got_error *
2892 check_same_branch(struct got_object_id *commit_id,
2893 struct got_reference *head_ref, struct got_object_id *yca_id,
2894 struct got_repository *repo)
2896 const struct got_error *err = NULL;
2897 struct got_commit_graph *graph = NULL;
2898 struct got_object_id *head_commit_id = NULL;
2899 int is_same_branch = 0;
2901 err = got_ref_resolve(&head_commit_id, repo, head_ref);
2905 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
2909 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
2914 err = got_commit_graph_open(&graph, "/", 1);
2918 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2919 check_cancelled, NULL);
2924 struct got_object_id id;
2926 err = got_commit_graph_iter_next(&id, graph, repo,
2927 check_cancelled, NULL);
2929 if (err->code == GOT_ERR_ITER_COMPLETED)
2934 if (yca_id && got_object_id_cmp(&id, yca_id) == 0)
2936 if (got_object_id_cmp(&id, commit_id) == 0) {
2943 got_commit_graph_close(graph);
2944 free(head_commit_id);
2945 if (!err && !is_same_branch)
2946 err = got_error(GOT_ERR_ANCESTRY);
2950 static const struct got_error *
2951 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2953 static char msg[512];
2954 const char *branch_name;
2956 if (got_ref_is_symbolic(ref))
2957 branch_name = got_ref_get_symref_target(ref);
2959 branch_name = got_ref_get_name(ref);
2961 if (strncmp("refs/heads/", branch_name, 11) == 0)
2964 snprintf(msg, sizeof(msg),
2965 "target commit is not contained in branch '%s'; "
2966 "the branch to use must be specified with -b; "
2967 "if necessary a new branch can be created for "
2968 "this commit with 'got branch -c %s BRANCH_NAME'",
2969 branch_name, commit_id_str);
2971 return got_error_msg(GOT_ERR_ANCESTRY, msg);
2974 static const struct got_error *
2975 cmd_checkout(int argc, char *argv[])
2977 const struct got_error *error = NULL;
2978 struct got_repository *repo = NULL;
2979 struct got_reference *head_ref = NULL, *ref = NULL;
2980 struct got_worktree *worktree = NULL;
2981 char *repo_path = NULL;
2982 char *worktree_path = NULL;
2983 const char *path_prefix = "";
2984 const char *branch_name = GOT_REF_HEAD, *refname = NULL;
2985 char *commit_id_str = NULL;
2986 struct got_object_id *commit_id = NULL;
2988 int ch, same_path_prefix, allow_nonempty = 0, verbosity = 0;
2989 struct got_pathlist_head paths;
2990 struct got_checkout_progress_arg cpa;
2991 int *pack_fds = NULL;
2996 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2997 "unveil", NULL) == -1)
3001 while ((ch = getopt(argc, argv, "b:c:Ep:q")) != -1) {
3004 branch_name = optarg;
3007 commit_id_str = strdup(optarg);
3008 if (commit_id_str == NULL)
3009 return got_error_from_errno("strdup");
3015 path_prefix = optarg;
3030 char *base, *dotgit;
3032 repo_path = realpath(argv[0], NULL);
3033 if (repo_path == NULL)
3034 return got_error_from_errno2("realpath", argv[0]);
3035 cwd = getcwd(NULL, 0);
3037 error = got_error_from_errno("getcwd");
3044 error = got_path_basename(&base, path);
3047 dotgit = strstr(base, ".git");
3050 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
3051 error = got_error_from_errno("asprintf");
3056 } else if (argc == 2) {
3057 repo_path = realpath(argv[0], NULL);
3058 if (repo_path == NULL) {
3059 error = got_error_from_errno2("realpath", argv[0]);
3062 worktree_path = realpath(argv[1], NULL);
3063 if (worktree_path == NULL) {
3064 if (errno != ENOENT) {
3065 error = got_error_from_errno2("realpath",
3069 worktree_path = strdup(argv[1]);
3070 if (worktree_path == NULL) {
3071 error = got_error_from_errno("strdup");
3078 got_path_strip_trailing_slashes(repo_path);
3079 got_path_strip_trailing_slashes(worktree_path);
3081 error = got_repo_pack_fds_open(&pack_fds);
3085 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3089 /* Pre-create work tree path for unveil(2) */
3090 error = got_path_mkdir(worktree_path);
3092 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
3093 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
3095 if (!allow_nonempty &&
3096 !got_path_dir_is_empty(worktree_path)) {
3097 error = got_error_path(worktree_path,
3098 GOT_ERR_DIR_NOT_EMPTY);
3103 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
3107 error = got_ref_open(&head_ref, repo, branch_name, 0);
3111 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
3112 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
3115 error = got_worktree_open(&worktree, worktree_path);
3119 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
3123 if (!same_path_prefix) {
3124 error = got_error(GOT_ERR_PATH_PREFIX);
3128 if (commit_id_str) {
3129 struct got_reflist_head refs;
3131 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3135 error = got_repo_match_object_id(&commit_id, NULL,
3136 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3137 got_ref_list_free(&refs);
3140 error = check_linear_ancestry(commit_id,
3141 got_worktree_get_base_commit_id(worktree), 0, repo);
3142 if (error != NULL) {
3143 if (error->code == GOT_ERR_ANCESTRY) {
3144 error = checkout_ancestry_error(
3145 head_ref, commit_id_str);
3149 error = check_same_branch(commit_id, head_ref, NULL, repo);
3151 if (error->code == GOT_ERR_ANCESTRY) {
3152 error = checkout_ancestry_error(
3153 head_ref, commit_id_str);
3157 error = got_worktree_set_base_commit_id(worktree, repo,
3161 /* Expand potentially abbreviated commit ID string. */
3162 free(commit_id_str);
3163 error = got_object_id_str(&commit_id_str, commit_id);
3167 commit_id = got_object_id_dup(
3168 got_worktree_get_base_commit_id(worktree));
3169 if (commit_id == NULL) {
3170 error = got_error_from_errno("got_object_id_dup");
3173 error = got_object_id_str(&commit_id_str, commit_id);
3178 error = got_pathlist_append(&paths, "", NULL);
3181 cpa.worktree_path = worktree_path;
3182 cpa.had_base_commit_ref_error = 0;
3183 cpa.verbosity = verbosity;
3184 error = got_worktree_checkout_files(worktree, &paths, repo,
3185 checkout_progress, &cpa, check_cancelled, NULL);
3189 if (got_ref_is_symbolic(head_ref)) {
3190 error = got_ref_resolve_symbolic(&ref, repo, head_ref);
3193 refname = got_ref_get_name(ref);
3195 refname = got_ref_get_name(head_ref);
3196 printf("Checked out %s: %s\n", refname, commit_id_str);
3197 printf("Now shut up and hack\n");
3198 if (cpa.had_base_commit_ref_error)
3199 show_worktree_base_ref_warning();
3202 const struct got_error *pack_err =
3203 got_repo_pack_fds_close(pack_fds);
3208 got_ref_close(head_ref);
3211 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
3212 free(commit_id_str);
3215 free(worktree_path);
3220 struct got_update_progress_arg {
3232 print_update_progress_stats(struct got_update_progress_arg *upa)
3234 if (!upa->did_something)
3237 if (upa->conflicts > 0)
3238 printf("Files with new merge conflicts: %d\n", upa->conflicts);
3239 if (upa->obstructed > 0)
3240 printf("File paths obstructed by a non-regular file: %d\n",
3242 if (upa->not_updated > 0)
3243 printf("Files not updated because of existing merge "
3244 "conflicts: %d\n", upa->not_updated);
3248 * The meaning of some status codes differs between merge-style operations and
3249 * update operations. For example, the ! status code means "file was missing"
3250 * if changes were merged into the work tree, and "missing file was restored"
3251 * if the work tree was updated. This function should be used by any operation
3252 * which merges changes into the work tree without updating the work tree.
3255 print_merge_progress_stats(struct got_update_progress_arg *upa)
3257 if (!upa->did_something)
3260 if (upa->conflicts > 0)
3261 printf("Files with new merge conflicts: %d\n", upa->conflicts);
3262 if (upa->obstructed > 0)
3263 printf("File paths obstructed by a non-regular file: %d\n",
3265 if (upa->missing > 0)
3266 printf("Files which had incoming changes but could not be "
3267 "found in the work tree: %d\n", upa->missing);
3268 if (upa->not_deleted > 0)
3269 printf("Files not deleted due to differences in deleted "
3270 "content: %d\n", upa->not_deleted);
3271 if (upa->unversioned > 0)
3272 printf("Files not merged because an unversioned file was "
3273 "found in the work tree: %d\n", upa->unversioned);
3279 fprintf(stderr, "usage: %s update [-q] [-b branch] [-c commit] "
3280 "[path ...]\n", getprogname());
3284 static const struct got_error *
3285 update_progress(void *arg, unsigned char status, const char *path)
3287 struct got_update_progress_arg *upa = arg;
3289 if (status == GOT_STATUS_EXISTS ||
3290 status == GOT_STATUS_BASE_REF_ERR)
3293 upa->did_something = 1;
3295 /* Base commit bump happens silently. */
3296 if (status == GOT_STATUS_BUMP_BASE)
3299 if (status == GOT_STATUS_CONFLICT)
3301 if (status == GOT_STATUS_OBSTRUCTED)
3303 if (status == GOT_STATUS_CANNOT_UPDATE)
3305 if (status == GOT_STATUS_MISSING)
3307 if (status == GOT_STATUS_CANNOT_DELETE)
3309 if (status == GOT_STATUS_UNVERSIONED)
3312 while (path[0] == '/')
3314 if (upa->verbosity >= 0)
3315 printf("%c %s\n", status, path);
3320 static const struct got_error *
3321 switch_head_ref(struct got_reference *head_ref,
3322 struct got_object_id *commit_id, struct got_worktree *worktree,
3323 struct got_repository *repo)
3325 const struct got_error *err = NULL;
3327 int ref_has_moved = 0;
3329 /* Trivial case: switching between two different references. */
3330 if (strcmp(got_ref_get_name(head_ref),
3331 got_worktree_get_head_ref_name(worktree)) != 0) {
3332 printf("Switching work tree from %s to %s\n",
3333 got_worktree_get_head_ref_name(worktree),
3334 got_ref_get_name(head_ref));
3335 return got_worktree_set_head_ref(worktree, head_ref);
3338 err = check_linear_ancestry(commit_id,
3339 got_worktree_get_base_commit_id(worktree), 0, repo);
3341 if (err->code != GOT_ERR_ANCESTRY)
3348 /* Switching to a rebased branch with the same reference name. */
3349 err = got_object_id_str(&base_id_str,
3350 got_worktree_get_base_commit_id(worktree));
3353 printf("Reference %s now points at a different branch\n",
3354 got_worktree_get_head_ref_name(worktree));
3355 printf("Switching work tree from %s to %s\n", base_id_str,
3356 got_worktree_get_head_ref_name(worktree));
3360 static const struct got_error *
3361 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
3363 const struct got_error *err;
3366 err = got_worktree_rebase_in_progress(&in_progress, worktree);
3370 return got_error(GOT_ERR_REBASING);
3372 err = got_worktree_histedit_in_progress(&in_progress, worktree);
3376 return got_error(GOT_ERR_HISTEDIT_BUSY);
3381 static const struct got_error *
3382 check_merge_in_progress(struct got_worktree *worktree,
3383 struct got_repository *repo)
3385 const struct got_error *err;
3388 err = got_worktree_merge_in_progress(&in_progress, worktree, repo);
3392 return got_error(GOT_ERR_MERGE_BUSY);
3397 static const struct got_error *
3398 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
3399 char *argv[], struct got_worktree *worktree)
3401 const struct got_error *err = NULL;
3403 struct got_pathlist_entry *new;
3409 return got_error_from_errno("strdup");
3410 return got_pathlist_append(paths, path, NULL);
3413 for (i = 0; i < argc; i++) {
3414 err = got_worktree_resolve_path(&path, worktree, argv[i]);
3417 err = got_pathlist_insert(&new, paths, path, NULL);
3418 if (err || new == NULL /* duplicate */) {
3428 static const struct got_error *
3429 wrap_not_worktree_error(const struct got_error *orig_err,
3430 const char *cmdname, const char *path)
3432 const struct got_error *err;
3433 struct got_repository *repo;
3434 static char msg[512];
3435 int *pack_fds = NULL;
3437 err = got_repo_pack_fds_open(&pack_fds);
3441 err = got_repo_open(&repo, path, NULL, pack_fds);
3445 snprintf(msg, sizeof(msg),
3446 "'got %s' needs a work tree in addition to a git repository\n"
3447 "Work trees can be checked out from this Git repository with "
3449 "The got(1) manual page contains more information.", cmdname);
3450 err = got_error_msg(GOT_ERR_NOT_WORKTREE, msg);
3451 got_repo_close(repo);
3453 const struct got_error *pack_err =
3454 got_repo_pack_fds_close(pack_fds);
3461 static const struct got_error *
3462 cmd_update(int argc, char *argv[])
3464 const struct got_error *error = NULL;
3465 struct got_repository *repo = NULL;
3466 struct got_worktree *worktree = NULL;
3467 char *worktree_path = NULL;
3468 struct got_object_id *commit_id = NULL;
3469 char *commit_id_str = NULL;
3470 const char *branch_name = NULL;
3471 struct got_reference *head_ref = NULL;
3472 struct got_pathlist_head paths;
3473 struct got_pathlist_entry *pe;
3474 int ch, verbosity = 0;
3475 struct got_update_progress_arg upa;
3476 int *pack_fds = NULL;
3481 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3482 "unveil", NULL) == -1)
3486 while ((ch = getopt(argc, argv, "b:c:q")) != -1) {
3489 branch_name = optarg;
3492 commit_id_str = strdup(optarg);
3493 if (commit_id_str == NULL)
3494 return got_error_from_errno("strdup");
3508 worktree_path = getcwd(NULL, 0);
3509 if (worktree_path == NULL) {
3510 error = got_error_from_errno("getcwd");
3514 error = got_repo_pack_fds_open(&pack_fds);
3518 error = got_worktree_open(&worktree, worktree_path);
3520 if (error->code == GOT_ERR_NOT_WORKTREE)
3521 error = wrap_not_worktree_error(error, "update",
3526 error = check_rebase_or_histedit_in_progress(worktree);
3530 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3535 error = apply_unveil(got_repo_get_path(repo), 0,
3536 got_worktree_get_root_path(worktree));
3540 error = check_merge_in_progress(worktree, repo);
3544 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3548 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
3549 got_worktree_get_head_ref_name(worktree), 0);
3552 if (commit_id_str == NULL) {
3553 error = got_ref_resolve(&commit_id, repo, head_ref);
3556 error = got_object_id_str(&commit_id_str, commit_id);
3560 struct got_reflist_head refs;
3562 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3566 error = got_repo_match_object_id(&commit_id, NULL,
3567 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3568 got_ref_list_free(&refs);
3569 free(commit_id_str);
3570 commit_id_str = NULL;
3573 error = got_object_id_str(&commit_id_str, commit_id);
3579 struct got_object_id *head_commit_id;
3580 TAILQ_FOREACH(pe, &paths, entry) {
3581 if (pe->path_len == 0)
3583 error = got_error_msg(GOT_ERR_BAD_PATH,
3584 "switching between branches requires that "
3585 "the entire work tree gets updated");
3588 error = got_ref_resolve(&head_commit_id, repo, head_ref);
3591 error = check_linear_ancestry(commit_id, head_commit_id, 0,
3593 free(head_commit_id);
3596 error = check_same_branch(commit_id, head_ref, NULL, repo);
3599 error = switch_head_ref(head_ref, commit_id, worktree, repo);
3603 error = check_linear_ancestry(commit_id,
3604 got_worktree_get_base_commit_id(worktree), 0, repo);
3605 if (error != NULL) {
3606 if (error->code == GOT_ERR_ANCESTRY)
3607 error = got_error(GOT_ERR_BRANCH_MOVED);
3610 error = check_same_branch(commit_id, head_ref, NULL, repo);
3615 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
3617 error = got_worktree_set_base_commit_id(worktree, repo,
3623 memset(&upa, 0, sizeof(upa));
3624 upa.verbosity = verbosity;
3625 error = got_worktree_checkout_files(worktree, &paths, repo,
3626 update_progress, &upa, check_cancelled, NULL);
3630 if (upa.did_something) {
3631 printf("Updated to %s: %s\n",
3632 got_worktree_get_head_ref_name(worktree), commit_id_str);
3634 printf("Already up-to-date\n");
3636 print_update_progress_stats(&upa);
3639 const struct got_error *pack_err =
3640 got_repo_pack_fds_close(pack_fds);
3644 free(worktree_path);
3645 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
3647 free(commit_id_str);
3651 static const struct got_error *
3652 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
3653 const char *path, int diff_context, int ignore_whitespace,
3654 int force_text_diff, struct got_diffstat_cb_arg *dsa,
3655 struct got_repository *repo, FILE *outfile)
3657 const struct got_error *err = NULL;
3658 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
3659 FILE *f1 = NULL, *f2 = NULL;
3660 int fd1 = -1, fd2 = -1;
3662 fd1 = got_opentempfd();
3664 return got_error_from_errno("got_opentempfd");
3665 fd2 = got_opentempfd();
3667 err = got_error_from_errno("got_opentempfd");
3672 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192,
3678 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192, fd2);
3682 f1 = got_opentemp();
3684 err = got_error_from_errno("got_opentemp");
3687 f2 = got_opentemp();
3689 err = got_error_from_errno("got_opentemp");
3693 while (path[0] == '/')
3695 err = got_diff_blob(NULL, NULL, blob1, blob2, f1, f2, path, path,
3696 GOT_DIFF_ALGORITHM_PATIENCE, diff_context, ignore_whitespace,
3697 force_text_diff, dsa, outfile);
3699 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3700 err = got_error_from_errno("close");
3702 got_object_blob_close(blob1);
3703 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3704 err = got_error_from_errno("close");
3705 got_object_blob_close(blob2);
3706 if (f1 && fclose(f1) == EOF && err == NULL)
3707 err = got_error_from_errno("fclose");
3708 if (f2 && fclose(f2) == EOF && err == NULL)
3709 err = got_error_from_errno("fclose");
3713 static const struct got_error *
3714 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
3715 const char *path, int diff_context, int ignore_whitespace,
3716 int force_text_diff, struct got_diffstat_cb_arg *dsa,
3717 struct got_repository *repo, FILE *outfile)
3719 const struct got_error *err = NULL;
3720 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3721 struct got_diff_blob_output_unidiff_arg arg;
3722 FILE *f1 = NULL, *f2 = NULL;
3723 int fd1 = -1, fd2 = -1;
3726 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3729 fd1 = got_opentempfd();
3731 err = got_error_from_errno("got_opentempfd");
3736 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3740 f1 = got_opentemp();
3742 err = got_error_from_errno("got_opentemp");
3746 f2 = got_opentemp();
3748 err = got_error_from_errno("got_opentemp");
3751 fd2 = got_opentempfd();
3753 err = got_error_from_errno("got_opentempfd");
3756 arg.diff_context = diff_context;
3757 arg.ignore_whitespace = ignore_whitespace;
3758 arg.force_text_diff = force_text_diff;
3760 arg.diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
3761 arg.outfile = outfile;
3764 while (path[0] == '/')
3766 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, path, path, repo,
3767 got_diff_blob_output_unidiff, &arg, 1);
3770 got_object_tree_close(tree1);
3772 got_object_tree_close(tree2);
3773 if (f1 && fclose(f1) == EOF && err == NULL)
3774 err = got_error_from_errno("fclose");
3775 if (f2 && fclose(f2) == EOF && err == NULL)
3776 err = got_error_from_errno("fclose");
3777 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3778 err = got_error_from_errno("close");
3779 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3780 err = got_error_from_errno("close");
3784 static const struct got_error *
3785 get_changed_paths(struct got_pathlist_head *paths,
3786 struct got_commit_object *commit, struct got_repository *repo,
3787 struct got_diffstat_cb_arg *dsa)
3789 const struct got_error *err = NULL;
3790 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3791 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3792 struct got_object_qid *qid;
3793 got_diff_blob_cb cb = got_diff_tree_collect_changed_paths;
3794 FILE *f1 = NULL, *f2 = NULL;
3795 int fd1 = -1, fd2 = -1;
3798 cb = got_diff_tree_compute_diffstat;
3800 f1 = got_opentemp();
3802 err = got_error_from_errno("got_opentemp");
3805 f2 = got_opentemp();
3807 err = got_error_from_errno("got_opentemp");
3810 fd1 = got_opentempfd();
3812 err = got_error_from_errno("got_opentempfd");
3815 fd2 = got_opentempfd();
3817 err = got_error_from_errno("got_opentempfd");
3822 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3824 struct got_commit_object *pcommit;
3825 err = got_object_open_as_commit(&pcommit, repo,
3830 tree_id1 = got_object_id_dup(
3831 got_object_commit_get_tree_id(pcommit));
3832 if (tree_id1 == NULL) {
3833 got_object_commit_close(pcommit);
3834 return got_error_from_errno("got_object_id_dup");
3836 got_object_commit_close(pcommit);
3841 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3846 tree_id2 = got_object_commit_get_tree_id(commit);
3847 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3851 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3852 cb, dsa ? (void *)dsa : paths, dsa ? 1 : 0);
3855 got_object_tree_close(tree1);
3857 got_object_tree_close(tree2);
3858 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3859 err = got_error_from_errno("close");
3860 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3861 err = got_error_from_errno("close");
3862 if (f1 && fclose(f1) == EOF && err == NULL)
3863 err = got_error_from_errno("fclose");
3864 if (f2 && fclose(f2) == EOF && err == NULL)
3865 err = got_error_from_errno("fclose");
3870 static const struct got_error *
3871 print_patch(struct got_commit_object *commit, struct got_object_id *id,
3872 const char *path, int diff_context, struct got_diffstat_cb_arg *dsa,
3873 struct got_repository *repo, FILE *outfile)
3875 const struct got_error *err = NULL;
3876 struct got_commit_object *pcommit = NULL;
3877 char *id_str1 = NULL, *id_str2 = NULL;
3878 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
3879 struct got_object_qid *qid;
3881 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3883 err = got_object_open_as_commit(&pcommit, repo,
3887 err = got_object_id_str(&id_str1, &qid->id);
3892 err = got_object_id_str(&id_str2, id);
3896 if (path && path[0] != '\0') {
3898 err = got_object_id_by_path(&obj_id2, repo, commit, path);
3902 err = got_object_id_by_path(&obj_id1, repo,
3905 if (err->code != GOT_ERR_NO_TREE_ENTRY) {
3911 err = got_object_get_type(&obj_type, repo, obj_id2);
3917 "diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3918 fprintf(outfile, "commit - %s\n",
3919 id_str1 ? id_str1 : "/dev/null");
3920 fprintf(outfile, "commit + %s\n", id_str2);
3922 case GOT_OBJ_TYPE_BLOB:
3923 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
3924 0, 0, dsa, repo, outfile);
3926 case GOT_OBJ_TYPE_TREE:
3927 err = diff_trees(obj_id1, obj_id2, path, diff_context,
3928 0, 0, dsa, repo, outfile);
3931 err = got_error(GOT_ERR_OBJ_TYPE);
3937 obj_id2 = got_object_commit_get_tree_id(commit);
3939 obj_id1 = got_object_commit_get_tree_id(pcommit);
3941 "diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3942 fprintf(outfile, "commit - %s\n",
3943 id_str1 ? id_str1 : "/dev/null");
3944 fprintf(outfile, "commit + %s\n", id_str2);
3945 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, 0,
3946 dsa, repo, outfile);
3952 got_object_commit_close(pcommit);
3957 get_datestr(time_t *time, char *datebuf)
3959 struct tm mytm, *tm;
3962 tm = gmtime_r(time, &mytm);
3965 s = asctime_r(tm, datebuf);
3968 p = strchr(s, '\n');
3974 static const struct got_error *
3975 match_commit(int *have_match, struct got_object_id *id,
3976 struct got_commit_object *commit, regex_t *regex)
3978 const struct got_error *err = NULL;
3979 regmatch_t regmatch;
3980 char *id_str = NULL, *logmsg = NULL;
3984 err = got_object_id_str(&id_str, id);
3988 err = got_object_commit_get_logmsg(&logmsg, commit);
3992 if (regexec(regex, got_object_commit_get_author(commit), 1,
3993 ®match, 0) == 0 ||
3994 regexec(regex, got_object_commit_get_committer(commit), 1,
3995 ®match, 0) == 0 ||
3996 regexec(regex, id_str, 1, ®match, 0) == 0 ||
3997 regexec(regex, logmsg, 1, ®match, 0) == 0)
4006 match_changed_paths(int *have_match, struct got_pathlist_head *changed_paths,
4009 regmatch_t regmatch;
4010 struct got_pathlist_entry *pe;
4014 TAILQ_FOREACH(pe, changed_paths, entry) {
4015 if (regexec(regex, pe->path, 1, ®match, 0) == 0) {
4022 static const struct got_error *
4023 match_patch(int *have_match, struct got_commit_object *commit,
4024 struct got_object_id *id, const char *path, int diff_context,
4025 struct got_repository *repo, regex_t *regex, FILE *f)
4027 const struct got_error *err = NULL;
4029 size_t linesize = 0;
4030 regmatch_t regmatch;
4034 err = got_opentemp_truncate(f);
4038 err = print_patch(commit, id, path, diff_context, NULL, repo, f);
4042 if (fseeko(f, 0L, SEEK_SET) == -1) {
4043 err = got_error_from_errno("fseeko");
4047 while (getline(&line, &linesize, f) != -1) {
4048 if (regexec(regex, line, 1, ®match, 0) == 0) {
4058 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
4060 static const struct got_error*
4061 build_refs_str(char **refs_str, struct got_reflist_head *refs,
4062 struct got_object_id *id, struct got_repository *repo,
4065 static const struct got_error *err = NULL;
4066 struct got_reflist_entry *re;
4072 TAILQ_FOREACH(re, refs, entry) {
4073 struct got_tag_object *tag = NULL;
4074 struct got_object_id *ref_id;
4077 name = got_ref_get_name(re->ref);
4078 if (strcmp(name, GOT_REF_HEAD) == 0)
4080 if (strncmp(name, "refs/", 5) == 0)
4082 if (strncmp(name, "got/", 4) == 0)
4084 if (strncmp(name, "heads/", 6) == 0)
4086 if (strncmp(name, "remotes/", 8) == 0) {
4090 s = strstr(name, "/" GOT_REF_HEAD);
4091 if (s != NULL && s[strlen(s)] == '\0')
4094 err = got_ref_resolve(&ref_id, repo, re->ref);
4097 if (strncmp(name, "tags/", 5) == 0) {
4098 err = got_object_open_as_tag(&tag, repo, ref_id);
4100 if (err->code != GOT_ERR_OBJ_TYPE) {
4104 /* Ref points at something other than a tag. */
4109 cmp = got_object_id_cmp(tag ?
4110 got_object_tag_get_object_id(tag) : ref_id, id);
4113 got_object_tag_close(tag);
4117 if (asprintf(refs_str, "%s%s%s", s ? s : "",
4118 s ? ", " : "", name) == -1) {
4119 err = got_error_from_errno("asprintf");
4130 static const struct got_error *
4131 print_commit_oneline(struct got_commit_object *commit, struct got_object_id *id,
4132 struct got_repository *repo, struct got_reflist_object_id_map *refs_idmap)
4134 const struct got_error *err = NULL;
4135 char *ref_str = NULL, *id_str = NULL, *logmsg0 = NULL;
4136 char *comma, *s, *nl;
4137 struct got_reflist_head *refs;
4138 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
4140 time_t committer_time;
4142 refs = got_reflist_object_id_map_lookup(refs_idmap, id);
4144 err = build_refs_str(&ref_str, refs, id, repo, 1);
4148 /* Display the first matching ref only. */
4149 if (ref_str && (comma = strchr(ref_str, ',')) != NULL)
4153 if (ref_str == NULL) {
4154 err = got_object_id_str(&id_str, id);
4159 committer_time = got_object_commit_get_committer_time(commit);
4160 if (gmtime_r(&committer_time, &tm) == NULL) {
4161 err = got_error_from_errno("gmtime_r");
4164 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0) {
4165 err = got_error(GOT_ERR_NO_SPACE);
4169 err = got_object_commit_get_logmsg(&logmsg0, commit);
4174 while (isspace((unsigned char)s[0]))
4177 nl = strchr(s, '\n');
4183 printf("%s%-7s %s\n", datebuf, ref_str, s);
4185 printf("%s%.7s %s\n", datebuf, id_str, s);
4187 if (fflush(stdout) != 0 && err == NULL)
4188 err = got_error_from_errno("fflush");
4196 static const struct got_error *
4197 print_diffstat(struct got_diffstat_cb_arg *dsa, const char *header)
4199 struct got_pathlist_entry *pe;
4202 printf("%s\n", header);
4204 TAILQ_FOREACH(pe, dsa->paths, entry) {
4205 struct got_diff_changed_path *cp = pe->data;
4206 int pad = dsa->max_path_len - pe->path_len + 1;
4208 printf(" %c %s%*c | %*d+ %*d-\n", cp->status, pe->path, pad,
4209 ' ', dsa->add_cols + 1, cp->add, dsa->rm_cols + 1, cp->rm);
4211 printf("\n%d file%s changed, %d insertion%s(+), %d deletion%s(-)\n\n",
4212 dsa->nfiles, dsa->nfiles > 1 ? "s" : "", dsa->ins,
4213 dsa->ins != 1 ? "s" : "", dsa->del, dsa->del != 1 ? "s" : "");
4215 if (fflush(stdout) != 0)
4216 return got_error_from_errno("fflush");
4221 static const struct got_error *
4227 if (fseeko(f, 0L, SEEK_SET) == -1)
4228 return got_error_from_errno("fseek");
4231 r = fread(buf, 1, sizeof(buf), f);
4234 return got_error_from_errno("fread");
4238 if (fwrite(buf, 1, r, stdout) != r)
4239 return got_ferror(stdout, GOT_ERR_IO);
4245 static const struct got_error *
4246 print_commit(struct got_commit_object *commit, struct got_object_id *id,
4247 struct got_repository *repo, const char *path,
4248 struct got_pathlist_head *changed_paths,
4249 struct got_diffstat_cb_arg *diffstat, int show_patch, int diff_context,
4250 struct got_reflist_object_id_map *refs_idmap, const char *custom_refs_str,
4253 const struct got_error *err = NULL;
4255 char *id_str, *datestr, *logmsg0, *logmsg, *line;
4257 time_t committer_time;
4258 const char *author, *committer;
4259 char *refs_str = NULL;
4261 err = got_object_id_str(&id_str, id);
4265 if (custom_refs_str == NULL) {
4266 struct got_reflist_head *refs;
4267 refs = got_reflist_object_id_map_lookup(refs_idmap, id);
4269 err = build_refs_str(&refs_str, refs, id, repo, 0);
4275 printf(GOT_COMMIT_SEP_STR);
4276 if (custom_refs_str)
4277 printf("%s %s (%s)\n", prefix ? prefix : "commit", id_str,
4280 printf("%s %s%s%s%s\n", prefix ? prefix : "commit", id_str,
4281 refs_str ? " (" : "", refs_str ? refs_str : "",
4282 refs_str ? ")" : "");
4287 printf("from: %s\n", got_object_commit_get_author(commit));
4288 author = got_object_commit_get_author(commit);
4289 committer = got_object_commit_get_committer(commit);
4290 if (strcmp(author, committer) != 0)
4291 printf("via: %s\n", committer);
4292 committer_time = got_object_commit_get_committer_time(commit);
4293 datestr = get_datestr(&committer_time, datebuf);
4295 printf("date: %s UTC\n", datestr);
4296 if (got_object_commit_get_nparents(commit) > 1) {
4297 const struct got_object_id_queue *parent_ids;
4298 struct got_object_qid *qid;
4300 parent_ids = got_object_commit_get_parent_ids(commit);
4301 STAILQ_FOREACH(qid, parent_ids, entry) {
4302 err = got_object_id_str(&id_str, &qid->id);
4305 printf("parent %d: %s\n", n++, id_str);
4311 err = got_object_commit_get_logmsg(&logmsg0, commit);
4317 line = strsep(&logmsg, "\n");
4319 printf(" %s\n", line);
4323 if (changed_paths && diffstat == NULL) {
4324 struct got_pathlist_entry *pe;
4326 TAILQ_FOREACH(pe, changed_paths, entry) {
4327 struct got_diff_changed_path *cp = pe->data;
4329 printf(" %c %s\n", cp->status, pe->path);
4337 err = got_error_from_errno("got_opentemp");
4342 err = print_patch(commit, id, path, diff_context, diffstat,
4343 repo, diffstat == NULL ? stdout : f);
4348 err = print_diffstat(diffstat, NULL);
4360 if (fflush(stdout) != 0 && err == NULL)
4361 err = got_error_from_errno("fflush");
4363 if (f && fclose(f) == EOF && err == NULL)
4364 err = got_error_from_errno("fclose");
4370 static const struct got_error *
4371 print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
4372 struct got_repository *repo, const char *path, int show_changed_paths,
4373 int show_diffstat, int show_patch, const char *search_pattern,
4374 int diff_context, int limit, int log_branches, int reverse_display_order,
4375 struct got_reflist_object_id_map *refs_idmap, int one_line,
4378 const struct got_error *err;
4379 struct got_commit_graph *graph;
4382 struct got_object_id_queue reversed_commits;
4383 struct got_object_qid *qid;
4384 struct got_commit_object *commit;
4385 struct got_pathlist_head changed_paths;
4387 STAILQ_INIT(&reversed_commits);
4388 TAILQ_INIT(&changed_paths);
4390 if (search_pattern && regcomp(®ex, search_pattern,
4391 REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
4392 return got_error_msg(GOT_ERR_REGEX, search_pattern);
4394 err = got_commit_graph_open(&graph, path, !log_branches);
4397 err = got_commit_graph_iter_start(graph, root_id, repo,
4398 check_cancelled, NULL);
4402 struct got_object_id id;
4403 struct got_diffstat_cb_arg dsa = { 0, 0, 0, 0, 0, 0,
4404 &changed_paths, 0, 0, GOT_DIFF_ALGORITHM_PATIENCE };
4406 if (sigint_received || sigpipe_received)
4409 err = got_commit_graph_iter_next(&id, graph, repo,
4410 check_cancelled, NULL);
4412 if (err->code == GOT_ERR_ITER_COMPLETED)
4417 err = got_object_open_as_commit(&commit, repo, &id);
4421 if ((show_changed_paths || (show_diffstat && !show_patch))
4422 && !reverse_display_order) {
4423 err = get_changed_paths(&changed_paths, commit, repo,
4424 show_diffstat ? &dsa : NULL);
4429 if (search_pattern) {
4430 err = match_commit(&have_match, &id, commit, ®ex);
4432 got_object_commit_close(commit);
4435 if (have_match == 0 && show_changed_paths)
4436 match_changed_paths(&have_match,
4437 &changed_paths, ®ex);
4438 if (have_match == 0 && show_patch) {
4439 err = match_patch(&have_match, commit, &id,
4440 path, diff_context, repo, ®ex, tmpfile);
4444 if (have_match == 0) {
4445 got_object_commit_close(commit);
4446 got_pathlist_free(&changed_paths,
4447 GOT_PATHLIST_FREE_ALL);
4452 if (reverse_display_order) {
4453 err = got_object_qid_alloc(&qid, &id);
4456 STAILQ_INSERT_HEAD(&reversed_commits, qid, entry);
4457 got_object_commit_close(commit);
4460 err = print_commit_oneline(commit, &id,
4463 err = print_commit(commit, &id, repo, path,
4464 (show_changed_paths || show_diffstat) ?
4465 &changed_paths : NULL,
4466 show_diffstat ? &dsa : NULL, show_patch,
4467 diff_context, refs_idmap, NULL, NULL);
4468 got_object_commit_close(commit);
4472 if ((limit && --limit == 0) ||
4473 (end_id && got_object_id_cmp(&id, end_id) == 0))
4476 got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
4478 if (reverse_display_order) {
4479 STAILQ_FOREACH(qid, &reversed_commits, entry) {
4480 struct got_diffstat_cb_arg dsa = { 0, 0, 0, 0, 0, 0,
4481 &changed_paths, 0, 0, GOT_DIFF_ALGORITHM_PATIENCE };
4483 err = got_object_open_as_commit(&commit, repo,
4487 if (show_changed_paths ||
4488 (show_diffstat && !show_patch)) {
4489 err = get_changed_paths(&changed_paths, commit,
4490 repo, show_diffstat ? &dsa : NULL);
4495 err = print_commit_oneline(commit, &qid->id,
4498 err = print_commit(commit, &qid->id, repo, path,
4499 (show_changed_paths || show_diffstat) ?
4500 &changed_paths : NULL,
4501 show_diffstat ? &dsa : NULL, show_patch,
4502 diff_context, refs_idmap, NULL, NULL);
4503 got_object_commit_close(commit);
4506 got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
4510 while (!STAILQ_EMPTY(&reversed_commits)) {
4511 qid = STAILQ_FIRST(&reversed_commits);
4512 STAILQ_REMOVE_HEAD(&reversed_commits, entry);
4513 got_object_qid_free(qid);
4515 got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
4518 got_commit_graph_close(graph);
4525 fprintf(stderr, "usage: %s log [-bdPpRs] [-C number] [-c commit] "
4526 "[-l N] [-r repository-path] [-S search-pattern] [-x commit] "
4527 "[path]\n", getprogname());
4532 get_default_log_limit(void)
4534 const char *got_default_log_limit;
4538 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
4539 if (got_default_log_limit == NULL)
4541 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
4547 static const struct got_error *
4548 cmd_log(int argc, char *argv[])
4550 const struct got_error *error;
4551 struct got_repository *repo = NULL;
4552 struct got_worktree *worktree = NULL;
4553 struct got_object_id *start_id = NULL, *end_id = NULL;
4554 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
4555 const char *start_commit = NULL, *end_commit = NULL;
4556 const char *search_pattern = NULL;
4557 int diff_context = -1, ch;
4558 int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
4559 int show_diffstat = 0, reverse_display_order = 0, one_line = 0;
4561 struct got_reflist_head refs;
4562 struct got_reflist_object_id_map *refs_idmap = NULL;
4563 FILE *tmpfile = NULL;
4564 int *pack_fds = NULL;
4569 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4575 limit = get_default_log_limit();
4577 while ((ch = getopt(argc, argv, "bC:c:dl:PpRr:S:sx:")) != -1) {
4583 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4586 errx(1, "number of context lines is %s: %s",
4590 start_commit = optarg;
4596 limit = strtonum(optarg, 0, INT_MAX, &errstr);
4598 errx(1, "number of commits is %s: %s",
4602 show_changed_paths = 1;
4608 reverse_display_order = 1;
4611 repo_path = realpath(optarg, NULL);
4612 if (repo_path == NULL)
4613 return got_error_from_errno2("realpath",
4615 got_path_strip_trailing_slashes(repo_path);
4618 search_pattern = optarg;
4624 end_commit = optarg;
4635 if (diff_context == -1)
4637 else if (!show_patch)
4638 errx(1, "-C requires -p");
4640 if (one_line && (show_patch || show_changed_paths || show_diffstat))
4641 errx(1, "cannot use -s with -d, -p or -P");
4643 cwd = getcwd(NULL, 0);
4645 error = got_error_from_errno("getcwd");
4649 error = got_repo_pack_fds_open(&pack_fds);
4653 if (repo_path == NULL) {
4654 error = got_worktree_open(&worktree, cwd);
4655 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4662 error = got_worktree_resolve_path(&path, worktree,
4667 path = strdup(argv[0]);
4669 error = got_error_from_errno("strdup");
4673 } else if (argc != 0)
4676 if (repo_path == NULL) {
4677 repo_path = worktree ?
4678 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
4680 if (repo_path == NULL) {
4681 error = got_error_from_errno("strdup");
4685 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4689 error = apply_unveil(got_repo_get_path(repo), 1,
4690 worktree ? got_worktree_get_root_path(worktree) : NULL);
4694 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4698 error = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
4702 if (start_commit == NULL) {
4703 struct got_reference *head_ref;
4704 struct got_commit_object *commit = NULL;
4705 error = got_ref_open(&head_ref, repo,
4706 worktree ? got_worktree_get_head_ref_name(worktree)
4710 error = got_ref_resolve(&start_id, repo, head_ref);
4711 got_ref_close(head_ref);
4714 error = got_object_open_as_commit(&commit, repo,
4718 got_object_commit_close(commit);
4720 error = got_repo_match_object_id(&start_id, NULL,
4721 start_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4725 if (end_commit != NULL) {
4726 error = got_repo_match_object_id(&end_id, NULL,
4727 end_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4734 * If a path was specified on the command line it was resolved
4735 * to a path in the work tree above. Prepend the work tree's
4736 * path prefix to obtain the corresponding in-repository path.
4740 prefix = got_worktree_get_path_prefix(worktree);
4741 if (asprintf(&in_repo_path, "%s%s%s", prefix,
4742 (path[0] != '\0') ? "/" : "", path) == -1) {
4743 error = got_error_from_errno("asprintf");
4748 error = got_repo_map_path(&in_repo_path, repo,
4754 path = in_repo_path;
4758 /* Release work tree lock. */
4759 got_worktree_close(worktree);
4763 if (search_pattern && show_patch) {
4764 tmpfile = got_opentemp();
4765 if (tmpfile == NULL) {
4766 error = got_error_from_errno("got_opentemp");
4771 error = print_commits(start_id, end_id, repo, path ? path : "",
4772 show_changed_paths, show_diffstat, show_patch, search_pattern,
4773 diff_context, limit, log_branches, reverse_display_order,
4774 refs_idmap, one_line, tmpfile);
4780 got_worktree_close(worktree);
4782 const struct got_error *close_err = got_repo_close(repo);
4787 const struct got_error *pack_err =
4788 got_repo_pack_fds_close(pack_fds);
4793 got_reflist_object_id_map_free(refs_idmap);
4794 if (tmpfile && fclose(tmpfile) == EOF && error == NULL)
4795 error = got_error_from_errno("fclose");
4796 got_ref_list_free(&refs);
4803 fprintf(stderr, "usage: %s diff [-adPsw] [-C number] [-c commit] "
4804 "[-r repository-path] [object1 object2 | path ...]\n",
4809 struct print_diff_arg {
4810 struct got_repository *repo;
4811 struct got_worktree *worktree;
4812 struct got_diffstat_cb_arg *diffstat;
4817 enum got_diff_algorithm diff_algo;
4818 int ignore_whitespace;
4819 int force_text_diff;
4826 * Create a file which contains the target path of a symlink so we can feed
4827 * it as content to the diff engine.
4829 static const struct got_error *
4830 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
4831 const char *abspath)
4833 const struct got_error *err = NULL;
4834 char target_path[PATH_MAX];
4835 ssize_t target_len, outlen;
4840 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
4841 if (target_len == -1)
4842 return got_error_from_errno2("readlinkat", abspath);
4844 target_len = readlink(abspath, target_path, PATH_MAX);
4845 if (target_len == -1)
4846 return got_error_from_errno2("readlink", abspath);
4849 *fd = got_opentempfd();
4851 return got_error_from_errno("got_opentempfd");
4853 outlen = write(*fd, target_path, target_len);
4855 err = got_error_from_errno("got_opentempfd");
4859 if (lseek(*fd, 0, SEEK_SET) == -1) {
4860 err = got_error_from_errno2("lseek", abspath);
4871 static const struct got_error *
4872 print_diff(void *arg, unsigned char status, unsigned char staged_status,
4873 const char *path, struct got_object_id *blob_id,
4874 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4875 int dirfd, const char *de_name)
4877 struct print_diff_arg *a = arg;
4878 const struct got_error *err = NULL;
4879 struct got_blob_object *blob1 = NULL;
4880 int fd = -1, fd1 = -1, fd2 = -1;
4882 char *abspath = NULL, *label1 = NULL;
4887 memset(&sb, 0, sizeof(sb));
4889 if (a->diff_staged) {
4890 if (staged_status != GOT_STATUS_MODIFY &&
4891 staged_status != GOT_STATUS_ADD &&
4892 staged_status != GOT_STATUS_DELETE)
4895 if (staged_status == GOT_STATUS_DELETE)
4897 if (status == GOT_STATUS_NONEXISTENT)
4898 return got_error_set_errno(ENOENT, path);
4899 if (status != GOT_STATUS_MODIFY &&
4900 status != GOT_STATUS_ADD &&
4901 status != GOT_STATUS_DELETE &&
4902 status != GOT_STATUS_CONFLICT)
4906 err = got_opentemp_truncate(a->f1);
4908 return got_error_from_errno("got_opentemp_truncate");
4909 err = got_opentemp_truncate(a->f2);
4911 return got_error_from_errno("got_opentemp_truncate");
4913 if (!a->header_shown) {
4914 if (fprintf(a->outfile, "diff %s%s\n",
4915 a->diff_staged ? "-s " : "",
4916 got_worktree_get_root_path(a->worktree)) < 0) {
4917 err = got_error_from_errno("fprintf");
4920 if (fprintf(a->outfile, "commit - %s\n", a->id_str) < 0) {
4921 err = got_error_from_errno("fprintf");
4924 if (fprintf(a->outfile, "path + %s%s\n",
4925 got_worktree_get_root_path(a->worktree),
4926 a->diff_staged ? " (staged changes)" : "") < 0) {
4927 err = got_error_from_errno("fprintf");
4930 a->header_shown = 1;
4933 if (a->diff_staged) {
4934 const char *label1 = NULL, *label2 = NULL;
4935 switch (staged_status) {
4936 case GOT_STATUS_MODIFY:
4940 case GOT_STATUS_ADD:
4943 case GOT_STATUS_DELETE:
4947 return got_error(GOT_ERR_FILE_STATUS);
4949 fd1 = got_opentempfd();
4951 err = got_error_from_errno("got_opentempfd");
4954 fd2 = got_opentempfd();
4956 err = got_error_from_errno("got_opentempfd");
4959 err = got_diff_objects_as_blobs(NULL, NULL, a->f1, a->f2,
4960 fd1, fd2, blob_id, staged_blob_id, label1, label2,
4961 a->diff_algo, a->diff_context, a->ignore_whitespace,
4962 a->force_text_diff, a->diffstat, a->repo, a->outfile);
4966 fd1 = got_opentempfd();
4968 err = got_error_from_errno("got_opentempfd");
4972 if (staged_status == GOT_STATUS_ADD ||
4973 staged_status == GOT_STATUS_MODIFY) {
4975 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
4979 err = got_object_id_str(&id_str, staged_blob_id);
4982 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
4983 err = got_error_from_errno("asprintf");
4988 } else if (status != GOT_STATUS_ADD) {
4989 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192,
4995 if (status != GOT_STATUS_DELETE) {
4996 if (asprintf(&abspath, "%s/%s",
4997 got_worktree_get_root_path(a->worktree), path) == -1) {
4998 err = got_error_from_errno("asprintf");
5003 fd = openat(dirfd, de_name,
5004 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5006 if (!got_err_open_nofollow_on_symlink()) {
5007 err = got_error_from_errno2("openat",
5011 err = get_symlink_target_file(&fd, dirfd,
5017 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5019 if (!got_err_open_nofollow_on_symlink()) {
5020 err = got_error_from_errno2("open",
5024 err = get_symlink_target_file(&fd, dirfd,
5030 if (fstatat(fd, abspath, &sb, AT_SYMLINK_NOFOLLOW) == -1) {
5031 err = got_error_from_errno2("fstatat", abspath);
5034 f2 = fdopen(fd, "r");
5036 err = got_error_from_errno2("fdopen", abspath);
5044 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5050 err = got_diff_blob_file(blob1, a->f1, size1, label1, f2 ? f2 : a->f2,
5051 f2_exists, &sb, path, GOT_DIFF_ALGORITHM_PATIENCE, a->diff_context,
5052 a->ignore_whitespace, a->force_text_diff, a->diffstat, a->outfile);
5054 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5055 err = got_error_from_errno("close");
5056 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5057 err = got_error_from_errno("close");
5059 got_object_blob_close(blob1);
5060 if (fd != -1 && close(fd) == -1 && err == NULL)
5061 err = got_error_from_errno("close");
5062 if (f2 && fclose(f2) == EOF && err == NULL)
5063 err = got_error_from_errno("fclose");
5068 static const struct got_error *
5069 cmd_diff(int argc, char *argv[])
5071 const struct got_error *error;
5072 struct got_repository *repo = NULL;
5073 struct got_worktree *worktree = NULL;
5074 char *cwd = NULL, *repo_path = NULL;
5075 const char *commit_args[2] = { NULL, NULL };
5076 int ncommit_args = 0;
5077 struct got_object_id *ids[2] = { NULL, NULL };
5078 char *labels[2] = { NULL, NULL };
5079 int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
5080 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch, i;
5081 int force_text_diff = 0, force_path = 0, rflag = 0, show_diffstat = 0;
5083 struct got_reflist_head refs;
5084 struct got_pathlist_head diffstat_paths, paths;
5085 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
5086 int fd1 = -1, fd2 = -1;
5087 int *pack_fds = NULL;
5088 struct got_diffstat_cb_arg dsa;
5090 memset(&dsa, 0, sizeof(dsa));
5094 TAILQ_INIT(&diffstat_paths);
5097 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5102 while ((ch = getopt(argc, argv, "aC:c:dPr:sw")) != -1) {
5105 force_text_diff = 1;
5108 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
5111 errx(1, "number of context lines is %s: %s",
5115 if (ncommit_args >= 2)
5116 errx(1, "too many -c options used");
5117 commit_args[ncommit_args++] = optarg;
5126 repo_path = realpath(optarg, NULL);
5127 if (repo_path == NULL)
5128 return got_error_from_errno2("realpath",
5130 got_path_strip_trailing_slashes(repo_path);
5137 ignore_whitespace = 1;
5148 cwd = getcwd(NULL, 0);
5150 error = got_error_from_errno("getcwd");
5154 error = got_repo_pack_fds_open(&pack_fds);
5158 if (repo_path == NULL) {
5159 error = got_worktree_open(&worktree, cwd);
5160 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5166 strdup(got_worktree_get_repo_path(worktree));
5167 if (repo_path == NULL) {
5168 error = got_error_from_errno("strdup");
5172 repo_path = strdup(cwd);
5173 if (repo_path == NULL) {
5174 error = got_error_from_errno("strdup");
5180 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5185 if (show_diffstat) {
5186 dsa.paths = &diffstat_paths;
5187 dsa.force_text = force_text_diff;
5188 dsa.ignore_ws = ignore_whitespace;
5189 dsa.diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
5192 if (rflag || worktree == NULL || ncommit_args > 0) {
5194 error = got_error_msg(GOT_ERR_NOT_IMPL,
5195 "-P option can only be used when diffing "
5200 error = got_error_msg(GOT_ERR_NOT_IMPL,
5201 "-s option can only be used when diffing "
5207 error = apply_unveil(got_repo_get_path(repo), 1,
5208 worktree ? got_worktree_get_root_path(worktree) : NULL);
5212 if ((!force_path && argc == 2) || ncommit_args > 0) {
5213 int obj_type = (ncommit_args > 0 ?
5214 GOT_OBJ_TYPE_COMMIT : GOT_OBJ_TYPE_ANY);
5215 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5219 for (i = 0; i < (ncommit_args > 0 ? ncommit_args : argc); i++) {
5221 if (ncommit_args > 0)
5222 arg = commit_args[i];
5225 error = got_repo_match_object_id(&ids[i], &labels[i],
5226 arg, obj_type, &refs, repo);
5228 if (error->code != GOT_ERR_NOT_REF &&
5229 error->code != GOT_ERR_NO_OBJ)
5231 if (ncommit_args > 0)
5239 f1 = got_opentemp();
5241 error = got_error_from_errno("got_opentemp");
5245 f2 = got_opentemp();
5247 error = got_error_from_errno("got_opentemp");
5251 outfile = got_opentemp();
5252 if (outfile == NULL) {
5253 error = got_error_from_errno("got_opentemp");
5257 if (ncommit_args == 0 && (ids[0] == NULL || ids[1] == NULL)) {
5258 struct print_diff_arg arg;
5261 if (worktree == NULL) {
5262 if (argc == 2 && ids[0] == NULL) {
5263 error = got_error_path(argv[0], GOT_ERR_NO_OBJ);
5265 } else if (argc == 2 && ids[1] == NULL) {
5266 error = got_error_path(argv[1], GOT_ERR_NO_OBJ);
5268 } else if (argc > 0) {
5269 error = got_error_fmt(GOT_ERR_NOT_WORKTREE,
5270 "%s", "specified paths cannot be resolved");
5273 error = got_error(GOT_ERR_NOT_WORKTREE);
5278 error = get_worktree_paths_from_argv(&paths, argc, argv,
5283 error = got_object_id_str(&id_str,
5284 got_worktree_get_base_commit_id(worktree));
5288 arg.worktree = worktree;
5289 arg.diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
5290 arg.diff_context = diff_context;
5291 arg.id_str = id_str;
5292 arg.header_shown = 0;
5293 arg.diff_staged = diff_staged;
5294 arg.ignore_whitespace = ignore_whitespace;
5295 arg.force_text_diff = force_text_diff;
5296 arg.diffstat = show_diffstat ? &dsa : NULL;
5299 arg.outfile = outfile;
5301 error = got_worktree_status(worktree, &paths, repo, 0,
5302 print_diff, &arg, check_cancelled, NULL);
5307 if (show_diffstat && dsa.nfiles > 0) {
5310 if (asprintf(&header, "diffstat %s%s",
5311 diff_staged ? "-s " : "",
5312 got_worktree_get_root_path(worktree)) == -1) {
5313 error = got_error_from_errno("asprintf");
5317 error = print_diffstat(&dsa, header);
5323 error = printfile(outfile);
5327 if (ncommit_args == 1) {
5328 struct got_commit_object *commit;
5329 error = got_object_open_as_commit(&commit, repo, ids[0]);
5333 labels[1] = labels[0];
5335 if (got_object_commit_get_nparents(commit) > 0) {
5336 const struct got_object_id_queue *pids;
5337 struct got_object_qid *pid;
5338 pids = got_object_commit_get_parent_ids(commit);
5339 pid = STAILQ_FIRST(pids);
5340 ids[0] = got_object_id_dup(&pid->id);
5341 if (ids[0] == NULL) {
5342 error = got_error_from_errno(
5343 "got_object_id_dup");
5344 got_object_commit_close(commit);
5347 error = got_object_id_str(&labels[0], ids[0]);
5349 got_object_commit_close(commit);
5354 labels[0] = strdup("/dev/null");
5355 if (labels[0] == NULL) {
5356 error = got_error_from_errno("strdup");
5357 got_object_commit_close(commit);
5362 got_object_commit_close(commit);
5365 if (ncommit_args == 0 && argc > 2) {
5366 error = got_error_msg(GOT_ERR_BAD_PATH,
5367 "path arguments cannot be used when diffing two objects");
5372 error = got_object_get_type(&type1, repo, ids[0]);
5377 error = got_object_get_type(&type2, repo, ids[1]);
5380 if (type1 != GOT_OBJ_TYPE_ANY && type1 != type2) {
5381 error = got_error(GOT_ERR_OBJ_TYPE);
5384 if (type1 == GOT_OBJ_TYPE_BLOB && argc > 2) {
5385 error = got_error_msg(GOT_ERR_OBJ_TYPE,
5386 "path arguments cannot be used when diffing blobs");
5390 for (i = 0; ncommit_args > 0 && i < argc; i++) {
5392 struct got_pathlist_entry *new;
5396 error = got_worktree_resolve_path(&p, worktree,
5400 prefix = got_worktree_get_path_prefix(worktree);
5401 while (prefix[0] == '/')
5403 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5404 (p[0] != '\0' && prefix[0] != '\0') ? "/" : "",
5406 error = got_error_from_errno("asprintf");
5412 char *mapped_path, *s;
5413 error = got_repo_map_path(&mapped_path, repo, argv[i]);
5419 in_repo_path = strdup(s);
5420 if (in_repo_path == NULL) {
5421 error = got_error_from_errno("asprintf");
5428 error = got_pathlist_insert(&new, &paths, in_repo_path, NULL);
5429 if (error || new == NULL /* duplicate */)
5436 /* Release work tree lock. */
5437 got_worktree_close(worktree);
5441 fd1 = got_opentempfd();
5443 error = got_error_from_errno("got_opentempfd");
5447 fd2 = got_opentempfd();
5449 error = got_error_from_errno("got_opentempfd");
5453 switch (type1 == GOT_OBJ_TYPE_ANY ? type2 : type1) {
5454 case GOT_OBJ_TYPE_BLOB:
5455 error = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5456 fd1, fd2, ids[0], ids[1], NULL, NULL,
5457 GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
5458 ignore_whitespace, force_text_diff,
5459 show_diffstat ? &dsa : NULL, repo, outfile);
5461 case GOT_OBJ_TYPE_TREE:
5462 error = got_diff_objects_as_trees(NULL, NULL, f1, f2, fd1, fd2,
5463 ids[0], ids[1], &paths, "", "",
5464 GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
5465 ignore_whitespace, force_text_diff,
5466 show_diffstat ? &dsa : NULL, repo, outfile);
5468 case GOT_OBJ_TYPE_COMMIT:
5469 fprintf(outfile, "diff %s %s\n", labels[0], labels[1]);
5470 error = got_diff_objects_as_commits(NULL, NULL, f1, f2,
5471 fd1, fd2, ids[0], ids[1], &paths,
5472 GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
5473 ignore_whitespace, force_text_diff,
5474 show_diffstat ? &dsa : NULL, repo, outfile);
5477 error = got_error(GOT_ERR_OBJ_TYPE);
5482 if (show_diffstat && dsa.nfiles > 0) {
5483 char *header = NULL;
5485 if (asprintf(&header, "diffstat %s %s",
5486 labels[0], labels[1]) == -1) {
5487 error = got_error_from_errno("asprintf");
5491 error = print_diffstat(&dsa, header);
5497 error = printfile(outfile);
5505 got_worktree_close(worktree);
5507 const struct got_error *close_err = got_repo_close(repo);
5512 const struct got_error *pack_err =
5513 got_repo_pack_fds_close(pack_fds);
5517 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
5518 got_pathlist_free(&diffstat_paths, GOT_PATHLIST_FREE_ALL);
5519 got_ref_list_free(&refs);
5520 if (outfile && fclose(outfile) == EOF && error == NULL)
5521 error = got_error_from_errno("fclose");
5522 if (f1 && fclose(f1) == EOF && error == NULL)
5523 error = got_error_from_errno("fclose");
5524 if (f2 && fclose(f2) == EOF && error == NULL)
5525 error = got_error_from_errno("fclose");
5526 if (fd1 != -1 && close(fd1) == -1 && error == NULL)
5527 error = got_error_from_errno("close");
5528 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
5529 error = got_error_from_errno("close");
5537 "usage: %s blame [-c commit] [-r repository-path] path\n",
5546 char datebuf[11]; /* YYYY-MM-DD + NUL */
5549 struct blame_cb_args {
5550 struct blame_line *lines;
5554 off_t *line_offsets;
5556 struct got_repository *repo;
5559 static const struct got_error *
5560 blame_cb(void *arg, int nlines, int lineno,
5561 struct got_commit_object *commit, struct got_object_id *id)
5563 const struct got_error *err = NULL;
5564 struct blame_cb_args *a = arg;
5565 struct blame_line *bline;
5567 size_t linesize = 0;
5570 time_t committer_time;
5572 if (nlines != a->nlines ||
5573 (lineno != -1 && lineno < 1) || lineno > a->nlines)
5574 return got_error(GOT_ERR_RANGE);
5576 if (sigint_received)
5577 return got_error(GOT_ERR_ITER_COMPLETED);
5580 return NULL; /* no change in this commit */
5582 /* Annotate this line. */
5583 bline = &a->lines[lineno - 1];
5584 if (bline->annotated)
5586 err = got_object_id_str(&bline->id_str, id);
5590 bline->committer = strdup(got_object_commit_get_committer(commit));
5591 if (bline->committer == NULL) {
5592 err = got_error_from_errno("strdup");
5596 committer_time = got_object_commit_get_committer_time(commit);
5597 if (gmtime_r(&committer_time, &tm) == NULL)
5598 return got_error_from_errno("gmtime_r");
5599 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
5601 err = got_error(GOT_ERR_NO_SPACE);
5604 bline->annotated = 1;
5606 /* Print lines annotated so far. */
5607 bline = &a->lines[a->lineno_cur - 1];
5608 if (!bline->annotated)
5611 offset = a->line_offsets[a->lineno_cur - 1];
5612 if (fseeko(a->f, offset, SEEK_SET) == -1) {
5613 err = got_error_from_errno("fseeko");
5617 while (a->lineno_cur <= a->nlines && bline->annotated) {
5618 char *smallerthan, *at, *nl, *committer;
5621 if (getline(&line, &linesize, a->f) == -1) {
5623 err = got_error_from_errno("getline");
5627 committer = bline->committer;
5628 smallerthan = strchr(committer, '<');
5629 if (smallerthan && smallerthan[1] != '\0')
5630 committer = smallerthan + 1;
5631 at = strchr(committer, '@');
5634 len = strlen(committer);
5636 committer[8] = '\0';
5638 nl = strchr(line, '\n');
5641 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
5642 bline->id_str, bline->datebuf, committer, line);
5645 bline = &a->lines[a->lineno_cur - 1];
5652 static const struct got_error *
5653 cmd_blame(int argc, char *argv[])
5655 const struct got_error *error;
5656 struct got_repository *repo = NULL;
5657 struct got_worktree *worktree = NULL;
5658 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5659 char *link_target = NULL;
5660 struct got_object_id *obj_id = NULL;
5661 struct got_object_id *commit_id = NULL;
5662 struct got_commit_object *commit = NULL;
5663 struct got_blob_object *blob = NULL;
5664 char *commit_id_str = NULL;
5665 struct blame_cb_args bca;
5666 int ch, obj_type, i, fd1 = -1, fd2 = -1, fd3 = -1;
5668 int *pack_fds = NULL;
5669 FILE *f1 = NULL, *f2 = NULL;
5671 fd1 = got_opentempfd();
5673 return got_error_from_errno("got_opentempfd");
5675 memset(&bca, 0, sizeof(bca));
5678 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5683 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5686 commit_id_str = optarg;
5689 repo_path = realpath(optarg, NULL);
5690 if (repo_path == NULL)
5691 return got_error_from_errno2("realpath",
5693 got_path_strip_trailing_slashes(repo_path);
5709 cwd = getcwd(NULL, 0);
5711 error = got_error_from_errno("getcwd");
5715 error = got_repo_pack_fds_open(&pack_fds);
5719 if (repo_path == NULL) {
5720 error = got_worktree_open(&worktree, cwd);
5721 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5727 strdup(got_worktree_get_repo_path(worktree));
5728 if (repo_path == NULL) {
5729 error = got_error_from_errno("strdup");
5734 repo_path = strdup(cwd);
5735 if (repo_path == NULL) {
5736 error = got_error_from_errno("strdup");
5742 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5747 const char *prefix = got_worktree_get_path_prefix(worktree);
5750 error = got_worktree_resolve_path(&p, worktree, path);
5753 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5754 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
5756 error = got_error_from_errno("asprintf");
5761 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5763 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5766 error = got_repo_map_path(&in_repo_path, repo, path);
5771 if (commit_id_str == NULL) {
5772 struct got_reference *head_ref;
5773 error = got_ref_open(&head_ref, repo, worktree ?
5774 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5777 error = got_ref_resolve(&commit_id, repo, head_ref);
5778 got_ref_close(head_ref);
5782 struct got_reflist_head refs;
5784 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5788 error = got_repo_match_object_id(&commit_id, NULL,
5789 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5790 got_ref_list_free(&refs);
5796 /* Release work tree lock. */
5797 got_worktree_close(worktree);
5801 error = got_object_open_as_commit(&commit, repo, commit_id);
5805 error = got_object_resolve_symlinks(&link_target, in_repo_path,
5810 error = got_object_id_by_path(&obj_id, repo, commit,
5811 link_target ? link_target : in_repo_path);
5815 error = got_object_get_type(&obj_type, repo, obj_id);
5819 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5820 error = got_error_path(link_target ? link_target : in_repo_path,
5825 error = got_object_open_as_blob(&blob, repo, obj_id, 8192, fd1);
5828 bca.f = got_opentemp();
5829 if (bca.f == NULL) {
5830 error = got_error_from_errno("got_opentemp");
5833 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
5834 &bca.line_offsets, bca.f, blob);
5835 if (error || bca.nlines == 0)
5838 /* Don't include \n at EOF in the blame line count. */
5839 if (bca.line_offsets[bca.nlines - 1] == filesize)
5842 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
5843 if (bca.lines == NULL) {
5844 error = got_error_from_errno("calloc");
5848 bca.nlines_prec = 0;
5856 fd2 = got_opentempfd();
5858 error = got_error_from_errno("got_opentempfd");
5861 fd3 = got_opentempfd();
5863 error = got_error_from_errno("got_opentempfd");
5866 f1 = got_opentemp();
5868 error = got_error_from_errno("got_opentemp");
5871 f2 = got_opentemp();
5873 error = got_error_from_errno("got_opentemp");
5876 error = got_blame(link_target ? link_target : in_repo_path, commit_id,
5877 repo, GOT_DIFF_ALGORITHM_PATIENCE, blame_cb, &bca,
5878 check_cancelled, NULL, fd2, fd3, f1, f2);
5887 got_object_commit_close(commit);
5889 if (fd1 != -1 && close(fd1) == -1 && error == NULL)
5890 error = got_error_from_errno("close");
5891 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
5892 error = got_error_from_errno("close");
5893 if (fd3 != -1 && close(fd3) == -1 && error == NULL)
5894 error = got_error_from_errno("close");
5895 if (f1 && fclose(f1) == EOF && error == NULL)
5896 error = got_error_from_errno("fclose");
5897 if (f2 && fclose(f2) == EOF && error == NULL)
5898 error = got_error_from_errno("fclose");
5901 got_object_blob_close(blob);
5903 got_worktree_close(worktree);
5905 const struct got_error *close_err = got_repo_close(repo);
5910 const struct got_error *pack_err =
5911 got_repo_pack_fds_close(pack_fds);
5916 for (i = 0; i < bca.nlines; i++) {
5917 struct blame_line *bline = &bca.lines[i];
5918 free(bline->id_str);
5919 free(bline->committer);
5923 free(bca.line_offsets);
5924 if (bca.f && fclose(bca.f) == EOF && error == NULL)
5925 error = got_error_from_errno("fclose");
5932 fprintf(stderr, "usage: %s tree [-iR] [-c commit] [-r repository-path] "
5933 "[path]\n", getprogname());
5937 static const struct got_error *
5938 print_entry(struct got_tree_entry *te, const char *id, const char *path,
5939 const char *root_path, struct got_repository *repo)
5941 const struct got_error *err = NULL;
5942 int is_root_path = (strcmp(path, root_path) == 0);
5943 const char *modestr = "";
5944 mode_t mode = got_tree_entry_get_mode(te);
5945 char *link_target = NULL;
5947 path += strlen(root_path);
5948 while (path[0] == '/')
5951 if (got_object_tree_entry_is_submodule(te))
5953 else if (S_ISLNK(mode)) {
5956 err = got_tree_entry_get_symlink_target(&link_target, te, repo);
5959 for (i = 0; i < strlen(link_target); i++) {
5960 if (!isprint((unsigned char)link_target[i]))
5961 link_target[i] = '?';
5966 else if (S_ISDIR(mode))
5968 else if (mode & S_IXUSR)
5971 printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
5972 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
5973 link_target ? " -> ": "", link_target ? link_target : "");
5979 static const struct got_error *
5980 print_tree(const char *path, struct got_commit_object *commit,
5981 int show_ids, int recurse, const char *root_path,
5982 struct got_repository *repo)
5984 const struct got_error *err = NULL;
5985 struct got_object_id *tree_id = NULL;
5986 struct got_tree_object *tree = NULL;
5989 err = got_object_id_by_path(&tree_id, repo, commit, path);
5993 err = got_object_open_as_tree(&tree, repo, tree_id);
5996 nentries = got_object_tree_get_nentries(tree);
5997 for (i = 0; i < nentries; i++) {
5998 struct got_tree_entry *te;
6001 if (sigint_received || sigpipe_received)
6004 te = got_object_tree_get_entry(tree, i);
6007 err = got_object_id_str(&id_str,
6008 got_tree_entry_get_id(te));
6011 if (asprintf(&id, "%s ", id_str) == -1) {
6012 err = got_error_from_errno("asprintf");
6018 err = print_entry(te, id, path, root_path, repo);
6023 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
6025 if (asprintf(&child_path, "%s%s%s", path,
6026 path[0] == '/' && path[1] == '\0' ? "" : "/",
6027 got_tree_entry_get_name(te)) == -1) {
6028 err = got_error_from_errno("asprintf");
6031 err = print_tree(child_path, commit, show_ids, 1,
6040 got_object_tree_close(tree);
6045 static const struct got_error *
6046 cmd_tree(int argc, char *argv[])
6048 const struct got_error *error;
6049 struct got_repository *repo = NULL;
6050 struct got_worktree *worktree = NULL;
6051 const char *path, *refname = NULL;
6052 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6053 struct got_object_id *commit_id = NULL;
6054 struct got_commit_object *commit = NULL;
6055 char *commit_id_str = NULL;
6056 int show_ids = 0, recurse = 0;
6058 int *pack_fds = NULL;
6061 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6066 while ((ch = getopt(argc, argv, "c:iRr:")) != -1) {
6069 commit_id_str = optarg;
6078 repo_path = realpath(optarg, NULL);
6079 if (repo_path == NULL)
6080 return got_error_from_errno2("realpath",
6082 got_path_strip_trailing_slashes(repo_path);
6100 cwd = getcwd(NULL, 0);
6102 error = got_error_from_errno("getcwd");
6106 error = got_repo_pack_fds_open(&pack_fds);
6110 if (repo_path == NULL) {
6111 error = got_worktree_open(&worktree, cwd);
6112 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6118 strdup(got_worktree_get_repo_path(worktree));
6119 if (repo_path == NULL)
6120 error = got_error_from_errno("strdup");
6124 repo_path = strdup(cwd);
6125 if (repo_path == NULL) {
6126 error = got_error_from_errno("strdup");
6132 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6137 const char *prefix = got_worktree_get_path_prefix(worktree);
6142 error = got_worktree_resolve_path(&p, worktree, path);
6145 if (asprintf(&in_repo_path, "%s%s%s", prefix,
6146 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
6148 error = got_error_from_errno("asprintf");
6153 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6157 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6162 error = got_repo_map_path(&in_repo_path, repo, path);
6167 if (commit_id_str == NULL) {
6168 struct got_reference *head_ref;
6170 refname = got_worktree_get_head_ref_name(worktree);
6172 refname = GOT_REF_HEAD;
6173 error = got_ref_open(&head_ref, repo, refname, 0);
6176 error = got_ref_resolve(&commit_id, repo, head_ref);
6177 got_ref_close(head_ref);
6181 struct got_reflist_head refs;
6183 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
6187 error = got_repo_match_object_id(&commit_id, NULL,
6188 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
6189 got_ref_list_free(&refs);
6195 /* Release work tree lock. */
6196 got_worktree_close(worktree);
6200 error = got_object_open_as_commit(&commit, repo, commit_id);
6204 error = print_tree(in_repo_path, commit, show_ids, recurse,
6205 in_repo_path, repo);
6212 got_object_commit_close(commit);
6214 got_worktree_close(worktree);
6216 const struct got_error *close_err = got_repo_close(repo);
6221 const struct got_error *pack_err =
6222 got_repo_pack_fds_close(pack_fds);
6232 fprintf(stderr, "usage: %s status [-I] [-S status-codes] "
6233 "[-s status-codes] [path ...]\n", getprogname());
6237 struct got_status_arg {
6242 static const struct got_error *
6243 print_status(void *arg, unsigned char status, unsigned char staged_status,
6244 const char *path, struct got_object_id *blob_id,
6245 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6246 int dirfd, const char *de_name)
6248 struct got_status_arg *st = arg;
6250 if (status == staged_status && (status == GOT_STATUS_DELETE))
6251 status = GOT_STATUS_NO_CHANGE;
6252 if (st != NULL && st->status_codes) {
6253 size_t ncodes = strlen(st->status_codes);
6256 for (i = 0; i < ncodes ; i++) {
6258 if (status == st->status_codes[i] ||
6259 staged_status == st->status_codes[i]) {
6264 if (status == st->status_codes[i] ||
6265 staged_status == st->status_codes[i])
6270 if (st->suppress && j == 0)
6277 printf("%c%c %s\n", status, staged_status, path);
6281 static const struct got_error *
6282 cmd_status(int argc, char *argv[])
6284 const struct got_error *error = NULL;
6285 struct got_repository *repo = NULL;
6286 struct got_worktree *worktree = NULL;
6287 struct got_status_arg st;
6289 struct got_pathlist_head paths;
6290 int ch, i, no_ignores = 0;
6291 int *pack_fds = NULL;
6295 memset(&st, 0, sizeof(st));
6296 st.status_codes = NULL;
6300 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6305 while ((ch = getopt(argc, argv, "IS:s:")) != -1) {
6311 if (st.status_codes != NULL && st.suppress == 0)
6312 option_conflict('S', 's');
6316 for (i = 0; i < strlen(optarg); i++) {
6317 switch (optarg[i]) {
6318 case GOT_STATUS_MODIFY:
6319 case GOT_STATUS_ADD:
6320 case GOT_STATUS_DELETE:
6321 case GOT_STATUS_CONFLICT:
6322 case GOT_STATUS_MISSING:
6323 case GOT_STATUS_OBSTRUCTED:
6324 case GOT_STATUS_UNVERSIONED:
6325 case GOT_STATUS_MODE_CHANGE:
6326 case GOT_STATUS_NONEXISTENT:
6329 errx(1, "invalid status code '%c'",
6333 if (ch == 's' && st.suppress)
6334 option_conflict('s', 'S');
6335 st.status_codes = optarg;
6346 cwd = getcwd(NULL, 0);
6348 error = got_error_from_errno("getcwd");
6352 error = got_repo_pack_fds_open(&pack_fds);
6356 error = got_worktree_open(&worktree, cwd);
6358 if (error->code == GOT_ERR_NOT_WORKTREE)
6359 error = wrap_not_worktree_error(error, "status", cwd);
6363 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6368 error = apply_unveil(got_repo_get_path(repo), 1,
6369 got_worktree_get_root_path(worktree));
6373 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6377 error = got_worktree_status(worktree, &paths, repo, no_ignores,
6378 print_status, &st, check_cancelled, NULL);
6381 const struct got_error *pack_err =
6382 got_repo_pack_fds_close(pack_fds);
6387 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
6395 fprintf(stderr, "usage: %s ref [-dlt] [-c object] [-r repository-path] "
6396 "[-s reference] [name]\n", getprogname());
6400 static const struct got_error *
6401 list_refs(struct got_repository *repo, const char *refname, int sort_by_time)
6403 static const struct got_error *err = NULL;
6404 struct got_reflist_head refs;
6405 struct got_reflist_entry *re;
6408 err = got_ref_list(&refs, repo, refname, sort_by_time ?
6409 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6414 TAILQ_FOREACH(re, &refs, entry) {
6416 refstr = got_ref_to_str(re->ref);
6417 if (refstr == NULL) {
6418 err = got_error_from_errno("got_ref_to_str");
6421 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
6425 got_ref_list_free(&refs);
6429 static const struct got_error *
6430 delete_ref_by_name(struct got_repository *repo, const char *refname)
6432 const struct got_error *err;
6433 struct got_reference *ref;
6435 err = got_ref_open(&ref, repo, refname, 0);
6439 err = delete_ref(repo, ref);
6444 static const struct got_error *
6445 add_ref(struct got_repository *repo, const char *refname, const char *target)
6447 const struct got_error *err = NULL;
6448 struct got_object_id *id = NULL;
6449 struct got_reference *ref = NULL;
6450 struct got_reflist_head refs;
6453 * Don't let the user create a reference name with a leading '-'.
6454 * While technically a valid reference name, this case is usually
6455 * an unintended typo.
6457 if (refname[0] == '-')
6458 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
6461 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
6464 err = got_repo_match_object_id(&id, NULL, target, GOT_OBJ_TYPE_ANY,
6466 got_ref_list_free(&refs);
6470 err = got_ref_alloc(&ref, refname, id);
6474 err = got_ref_write(ref, repo);
6482 static const struct got_error *
6483 add_symref(struct got_repository *repo, const char *refname, const char *target)
6485 const struct got_error *err = NULL;
6486 struct got_reference *ref = NULL;
6487 struct got_reference *target_ref = NULL;
6490 * Don't let the user create a reference name with a leading '-'.
6491 * While technically a valid reference name, this case is usually
6492 * an unintended typo.
6494 if (refname[0] == '-')
6495 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
6497 err = got_ref_open(&target_ref, repo, target, 0);
6501 err = got_ref_alloc_symref(&ref, refname, target_ref);
6505 err = got_ref_write(ref, repo);
6508 got_ref_close(target_ref);
6514 static const struct got_error *
6515 cmd_ref(int argc, char *argv[])
6517 const struct got_error *error = NULL;
6518 struct got_repository *repo = NULL;
6519 struct got_worktree *worktree = NULL;
6520 char *cwd = NULL, *repo_path = NULL;
6521 int ch, do_list = 0, do_delete = 0, sort_by_time = 0;
6522 const char *obj_arg = NULL, *symref_target= NULL;
6523 char *refname = NULL;
6524 int *pack_fds = NULL;
6527 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6528 "sendfd unveil", NULL) == -1)
6532 while ((ch = getopt(argc, argv, "c:dlr:s:t")) != -1) {
6544 repo_path = realpath(optarg, NULL);
6545 if (repo_path == NULL)
6546 return got_error_from_errno2("realpath",
6548 got_path_strip_trailing_slashes(repo_path);
6551 symref_target = optarg;
6562 if (obj_arg && do_list)
6563 option_conflict('c', 'l');
6564 if (obj_arg && do_delete)
6565 option_conflict('c', 'd');
6566 if (obj_arg && symref_target)
6567 option_conflict('c', 's');
6568 if (symref_target && do_delete)
6569 option_conflict('s', 'd');
6570 if (symref_target && do_list)
6571 option_conflict('s', 'l');
6572 if (do_delete && do_list)
6573 option_conflict('d', 'l');
6574 if (sort_by_time && !do_list)
6575 errx(1, "-t option requires -l option");
6581 if (argc != 0 && argc != 1)
6584 refname = strdup(argv[0]);
6585 if (refname == NULL) {
6586 error = got_error_from_errno("strdup");
6593 refname = strdup(argv[0]);
6594 if (refname == NULL) {
6595 error = got_error_from_errno("strdup");
6601 got_path_strip_trailing_slashes(refname);
6603 cwd = getcwd(NULL, 0);
6605 error = got_error_from_errno("getcwd");
6609 error = got_repo_pack_fds_open(&pack_fds);
6613 if (repo_path == NULL) {
6614 error = got_worktree_open(&worktree, cwd);
6615 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6621 strdup(got_worktree_get_repo_path(worktree));
6622 if (repo_path == NULL)
6623 error = got_error_from_errno("strdup");
6627 repo_path = strdup(cwd);
6628 if (repo_path == NULL) {
6629 error = got_error_from_errno("strdup");
6635 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6641 /* Remove "cpath" promise. */
6642 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6648 error = apply_unveil(got_repo_get_path(repo), do_list,
6649 worktree ? got_worktree_get_root_path(worktree) : NULL);
6654 error = list_refs(repo, refname, sort_by_time);
6656 error = delete_ref_by_name(repo, refname);
6657 else if (symref_target)
6658 error = add_symref(repo, refname, symref_target);
6660 if (obj_arg == NULL)
6662 error = add_ref(repo, refname, obj_arg);
6667 const struct got_error *close_err = got_repo_close(repo);
6672 got_worktree_close(worktree);
6674 const struct got_error *pack_err =
6675 got_repo_pack_fds_close(pack_fds);
6687 fprintf(stderr, "usage: %s branch [-lnt] [-c commit] [-d name] "
6688 "[-r repository-path] [name]\n", getprogname());
6692 static const struct got_error *
6693 list_branch(struct got_repository *repo, struct got_worktree *worktree,
6694 struct got_reference *ref)
6696 const struct got_error *err = NULL;
6697 const char *refname, *marker = " ";
6700 refname = got_ref_get_name(ref);
6701 if (worktree && strcmp(refname,
6702 got_worktree_get_head_ref_name(worktree)) == 0) {
6703 struct got_object_id *id = NULL;
6705 err = got_ref_resolve(&id, repo, ref);
6708 if (got_object_id_cmp(id,
6709 got_worktree_get_base_commit_id(worktree)) == 0)
6716 if (strncmp(refname, "refs/heads/", 11) == 0)
6718 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
6720 if (strncmp(refname, "refs/remotes/", 13) == 0)
6723 refstr = got_ref_to_str(ref);
6725 return got_error_from_errno("got_ref_to_str");
6727 printf("%s%s: %s\n", marker, refname, refstr);
6732 static const struct got_error *
6733 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
6735 const char *refname;
6737 if (worktree == NULL)
6738 return got_error(GOT_ERR_NOT_WORKTREE);
6740 refname = got_worktree_get_head_ref_name(worktree);
6742 if (strncmp(refname, "refs/heads/", 11) == 0)
6744 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
6747 printf("%s\n", refname);
6752 static const struct got_error *
6753 list_branches(struct got_repository *repo, struct got_worktree *worktree,
6756 static const struct got_error *err = NULL;
6757 struct got_reflist_head refs;
6758 struct got_reflist_entry *re;
6759 struct got_reference *temp_ref = NULL;
6760 int rebase_in_progress, histedit_in_progress;
6765 err = got_worktree_rebase_in_progress(&rebase_in_progress,
6770 err = got_worktree_histedit_in_progress(&histedit_in_progress,
6775 if (rebase_in_progress || histedit_in_progress) {
6776 err = got_ref_open(&temp_ref, repo,
6777 got_worktree_get_head_ref_name(worktree), 0);
6780 list_branch(repo, worktree, temp_ref);
6781 got_ref_close(temp_ref);
6785 err = got_ref_list(&refs, repo, "refs/heads", sort_by_time ?
6786 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6791 TAILQ_FOREACH(re, &refs, entry)
6792 list_branch(repo, worktree, re->ref);
6794 got_ref_list_free(&refs);
6796 err = got_ref_list(&refs, repo, "refs/remotes", sort_by_time ?
6797 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6802 TAILQ_FOREACH(re, &refs, entry)
6803 list_branch(repo, worktree, re->ref);
6805 got_ref_list_free(&refs);
6810 static const struct got_error *
6811 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
6812 const char *branch_name)
6814 const struct got_error *err = NULL;
6815 struct got_reference *ref = NULL;
6816 char *refname, *remote_refname = NULL;
6818 if (strncmp(branch_name, "refs/", 5) == 0)
6820 if (strncmp(branch_name, "heads/", 6) == 0)
6822 else if (strncmp(branch_name, "remotes/", 8) == 0)
6825 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
6826 return got_error_from_errno("asprintf");
6828 if (asprintf(&remote_refname, "refs/remotes/%s",
6829 branch_name) == -1) {
6830 err = got_error_from_errno("asprintf");
6834 err = got_ref_open(&ref, repo, refname, 0);
6836 const struct got_error *err2;
6837 if (err->code != GOT_ERR_NOT_REF)
6840 * Keep 'err' intact such that if neither branch exists
6841 * we report "refs/heads" rather than "refs/remotes" in
6842 * our error message.
6844 err2 = got_ref_open(&ref, repo, remote_refname, 0);
6851 strcmp(got_worktree_get_head_ref_name(worktree),
6852 got_ref_get_name(ref)) == 0) {
6853 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6854 "will not delete this work tree's current branch");
6858 err = delete_ref(repo, ref);
6863 free(remote_refname);
6867 static const struct got_error *
6868 add_branch(struct got_repository *repo, const char *branch_name,
6869 struct got_object_id *base_commit_id)
6871 const struct got_error *err = NULL;
6872 struct got_reference *ref = NULL;
6873 char *refname = NULL;
6876 * Don't let the user create a branch name with a leading '-'.
6877 * While technically a valid reference name, this case is usually
6878 * an unintended typo.
6880 if (branch_name[0] == '-')
6881 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
6883 if (strncmp(branch_name, "refs/heads/", 11) == 0)
6886 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
6887 err = got_error_from_errno("asprintf");
6891 err = got_ref_open(&ref, repo, refname, 0);
6893 err = got_error(GOT_ERR_BRANCH_EXISTS);
6895 } else if (err->code != GOT_ERR_NOT_REF)
6898 err = got_ref_alloc(&ref, refname, base_commit_id);
6902 err = got_ref_write(ref, repo);
6910 static const struct got_error *
6911 cmd_branch(int argc, char *argv[])
6913 const struct got_error *error = NULL;
6914 struct got_repository *repo = NULL;
6915 struct got_worktree *worktree = NULL;
6916 char *cwd = NULL, *repo_path = NULL;
6917 int ch, do_list = 0, do_show = 0, do_update = 1, sort_by_time = 0;
6918 const char *delref = NULL, *commit_id_arg = NULL;
6919 struct got_reference *ref = NULL;
6920 struct got_pathlist_head paths;
6921 struct got_object_id *commit_id = NULL;
6922 char *commit_id_str = NULL;
6923 int *pack_fds = NULL;
6928 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6929 "sendfd unveil", NULL) == -1)
6933 while ((ch = getopt(argc, argv, "c:d:lnr:t")) != -1) {
6936 commit_id_arg = optarg;
6948 repo_path = realpath(optarg, NULL);
6949 if (repo_path == NULL)
6950 return got_error_from_errno2("realpath",
6952 got_path_strip_trailing_slashes(repo_path);
6963 if (do_list && delref)
6964 option_conflict('l', 'd');
6965 if (sort_by_time && !do_list)
6966 errx(1, "-t option requires -l option");
6971 if (!do_list && !delref && argc == 0)
6974 if ((do_list || delref || do_show) && commit_id_arg != NULL)
6975 errx(1, "-c option can only be used when creating a branch");
6977 if (do_list || delref) {
6980 } else if (!do_show && argc != 1)
6983 cwd = getcwd(NULL, 0);
6985 error = got_error_from_errno("getcwd");
6989 error = got_repo_pack_fds_open(&pack_fds);
6993 if (repo_path == NULL) {
6994 error = got_worktree_open(&worktree, cwd);
6995 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7001 strdup(got_worktree_get_repo_path(worktree));
7002 if (repo_path == NULL)
7003 error = got_error_from_errno("strdup");
7007 repo_path = strdup(cwd);
7008 if (repo_path == NULL) {
7009 error = got_error_from_errno("strdup");
7015 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7020 if (do_list || do_show) {
7021 /* Remove "cpath" promise. */
7022 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
7028 error = apply_unveil(got_repo_get_path(repo), do_list,
7029 worktree ? got_worktree_get_root_path(worktree) : NULL);
7034 error = show_current_branch(repo, worktree);
7036 error = list_branches(repo, worktree, sort_by_time);
7038 error = delete_branch(repo, worktree, delref);
7040 struct got_reflist_head refs;
7042 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
7046 if (commit_id_arg == NULL)
7047 commit_id_arg = worktree ?
7048 got_worktree_get_head_ref_name(worktree) :
7050 error = got_repo_match_object_id(&commit_id, NULL,
7051 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &refs, repo);
7052 got_ref_list_free(&refs);
7055 error = add_branch(repo, argv[0], commit_id);
7058 if (worktree && do_update) {
7059 struct got_update_progress_arg upa;
7060 char *branch_refname = NULL;
7062 error = got_object_id_str(&commit_id_str, commit_id);
7065 error = get_worktree_paths_from_argv(&paths, 0, NULL,
7069 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
7071 error = got_error_from_errno("asprintf");
7074 error = got_ref_open(&ref, repo, branch_refname, 0);
7075 free(branch_refname);
7078 error = switch_head_ref(ref, commit_id, worktree,
7082 error = got_worktree_set_base_commit_id(worktree, repo,
7086 memset(&upa, 0, sizeof(upa));
7087 error = got_worktree_checkout_files(worktree, &paths,
7088 repo, update_progress, &upa, check_cancelled,
7092 if (upa.did_something) {
7093 printf("Updated to %s: %s\n",
7094 got_worktree_get_head_ref_name(worktree),
7097 print_update_progress_stats(&upa);
7104 const struct got_error *close_err = got_repo_close(repo);
7109 got_worktree_close(worktree);
7111 const struct got_error *pack_err =
7112 got_repo_pack_fds_close(pack_fds);
7119 free(commit_id_str);
7120 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
7128 fprintf(stderr, "usage: %s tag [-lVv] [-c commit] [-m message] "
7129 "[-r repository-path] [-s signer-id] name\n", getprogname());
7134 static const struct got_error *
7135 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
7137 const struct got_error *err = NULL;
7138 struct got_reflist_entry *re, *se, *new;
7139 struct got_object_id *re_id, *se_id;
7140 struct got_tag_object *re_tag, *se_tag;
7141 time_t re_time, se_time;
7143 STAILQ_FOREACH(re, tags, entry) {
7144 se = STAILQ_FIRST(sorted);
7146 err = got_reflist_entry_dup(&new, re);
7149 STAILQ_INSERT_HEAD(sorted, new, entry);
7152 err = got_ref_resolve(&re_id, repo, re->ref);
7155 err = got_object_open_as_tag(&re_tag, repo, re_id);
7159 re_time = got_object_tag_get_tagger_time(re_tag);
7160 got_object_tag_close(re_tag);
7164 err = got_ref_resolve(&se_id, repo, re->ref);
7167 err = got_object_open_as_tag(&se_tag, repo, se_id);
7171 se_time = got_object_tag_get_tagger_time(se_tag);
7172 got_object_tag_close(se_tag);
7174 if (se_time > re_time) {
7175 err = got_reflist_entry_dup(&new, re);
7178 STAILQ_INSERT_AFTER(sorted, se, new, entry);
7181 se = STAILQ_NEXT(se, entry);
7190 static const struct got_error *
7191 get_tag_refname(char **refname, const char *tag_name)
7193 const struct got_error *err;
7195 if (strncmp("refs/tags/", tag_name, 10) == 0) {
7196 *refname = strdup(tag_name);
7197 if (*refname == NULL)
7198 return got_error_from_errno("strdup");
7199 } else if (asprintf(refname, "refs/tags/%s", tag_name) == -1) {
7200 err = got_error_from_errno("asprintf");
7208 static const struct got_error *
7209 list_tags(struct got_repository *repo, const char *tag_name, int verify_tags,
7210 const char *allowed_signers, const char *revoked_signers, int verbosity)
7212 static const struct got_error *err = NULL;
7213 struct got_reflist_head refs;
7214 struct got_reflist_entry *re;
7215 char *wanted_refname = NULL;
7220 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
7225 struct got_reference *ref;
7226 err = get_tag_refname(&wanted_refname, tag_name);
7229 /* Wanted tag reference should exist. */
7230 err = got_ref_open(&ref, repo, wanted_refname, 0);
7236 TAILQ_FOREACH(re, &refs, entry) {
7237 const char *refname;
7238 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
7240 const char *tagger, *ssh_sig = NULL;
7241 char *sig_msg = NULL;
7243 struct got_object_id *id;
7244 struct got_tag_object *tag;
7245 struct got_commit_object *commit = NULL;
7247 refname = got_ref_get_name(re->ref);
7248 if (strncmp(refname, "refs/tags/", 10) != 0 ||
7249 (wanted_refname && strcmp(refname, wanted_refname) != 0))
7252 refstr = got_ref_to_str(re->ref);
7253 if (refstr == NULL) {
7254 err = got_error_from_errno("got_ref_to_str");
7258 err = got_ref_resolve(&id, repo, re->ref);
7261 err = got_object_open_as_tag(&tag, repo, id);
7263 if (err->code != GOT_ERR_OBJ_TYPE) {
7267 /* "lightweight" tag */
7268 err = got_object_open_as_commit(&commit, repo, id);
7273 tagger = got_object_commit_get_committer(commit);
7275 got_object_commit_get_committer_time(commit);
7276 err = got_object_id_str(&id_str, id);
7282 tagger = got_object_tag_get_tagger(tag);
7283 tagger_time = got_object_tag_get_tagger_time(tag);
7284 err = got_object_id_str(&id_str,
7285 got_object_tag_get_object_id(tag));
7290 if (tag && verify_tags) {
7291 ssh_sig = got_sigs_get_tagmsg_ssh_signature(
7292 got_object_tag_get_message(tag));
7293 if (ssh_sig && allowed_signers == NULL) {
7294 err = got_error_msg(
7295 GOT_ERR_VERIFY_TAG_SIGNATURE,
7296 "SSH signature verification requires "
7297 "setting allowed_signers in "
7303 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
7305 printf("from: %s\n", tagger);
7306 datestr = get_datestr(&tagger_time, datebuf);
7308 printf("date: %s UTC\n", datestr);
7310 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
7312 switch (got_object_tag_get_object_type(tag)) {
7313 case GOT_OBJ_TYPE_BLOB:
7314 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
7317 case GOT_OBJ_TYPE_TREE:
7318 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
7321 case GOT_OBJ_TYPE_COMMIT:
7322 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
7325 case GOT_OBJ_TYPE_TAG:
7326 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
7336 err = got_sigs_verify_tag_ssh(&sig_msg, tag, ssh_sig,
7337 allowed_signers, revoked_signers, verbosity);
7338 if (err && err->code == GOT_ERR_BAD_TAG_SIGNATURE)
7342 printf("signature: %s", sig_msg);
7348 err = got_object_commit_get_logmsg(&tagmsg0, commit);
7351 got_object_commit_close(commit);
7353 tagmsg0 = strdup(got_object_tag_get_message(tag));
7354 got_object_tag_close(tag);
7355 if (tagmsg0 == NULL) {
7356 err = got_error_from_errno("strdup");
7363 line = strsep(&tagmsg, "\n");
7365 printf(" %s\n", line);
7370 got_ref_list_free(&refs);
7371 free(wanted_refname);
7373 if (err == NULL && bad_sigs)
7374 err = got_error(GOT_ERR_BAD_TAG_SIGNATURE);
7378 static const struct got_error *
7379 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
7380 const char *tag_name, const char *repo_path)
7382 const struct got_error *err = NULL;
7383 char *template = NULL, *initial_content = NULL;
7384 char *editor = NULL;
7385 int initial_content_len;
7388 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
7389 err = got_error_from_errno("asprintf");
7393 initial_content_len = asprintf(&initial_content,
7394 "\n# tagging commit %s as %s\n",
7395 commit_id_str, tag_name);
7396 if (initial_content_len == -1) {
7397 err = got_error_from_errno("asprintf");
7401 err = got_opentemp_named_fd(tagmsg_path, &fd, template, "");
7405 if (write(fd, initial_content, initial_content_len) == -1) {
7406 err = got_error_from_errno2("write", *tagmsg_path);
7410 err = get_editor(&editor);
7413 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content,
7414 initial_content_len, 1);
7416 free(initial_content);
7420 if (fd != -1 && close(fd) == -1 && err == NULL)
7421 err = got_error_from_errno2("close", *tagmsg_path);
7430 static const struct got_error *
7431 add_tag(struct got_repository *repo, const char *tagger,
7432 const char *tag_name, const char *commit_arg, const char *tagmsg_arg,
7433 const char *signer_id, int verbosity)
7435 const struct got_error *err = NULL;
7436 struct got_object_id *commit_id = NULL, *tag_id = NULL;
7437 char *label = NULL, *commit_id_str = NULL;
7438 struct got_reference *ref = NULL;
7439 char *refname = NULL, *tagmsg = NULL;
7440 char *tagmsg_path = NULL, *tag_id_str = NULL;
7441 int preserve_tagmsg = 0;
7442 struct got_reflist_head refs;
7447 * Don't let the user create a tag name with a leading '-'.
7448 * While technically a valid reference name, this case is usually
7449 * an unintended typo.
7451 if (tag_name[0] == '-')
7452 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
7454 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
7458 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
7459 GOT_OBJ_TYPE_COMMIT, &refs, repo);
7463 err = got_object_id_str(&commit_id_str, commit_id);
7467 err = get_tag_refname(&refname, tag_name);
7470 if (strncmp("refs/tags/", tag_name, 10) == 0)
7473 err = got_ref_open(&ref, repo, refname, 0);
7475 err = got_error(GOT_ERR_TAG_EXISTS);
7477 } else if (err->code != GOT_ERR_NOT_REF)
7480 if (tagmsg_arg == NULL) {
7481 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
7482 tag_name, got_repo_get_path(repo));
7484 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
7485 tagmsg_path != NULL)
7486 preserve_tagmsg = 1;
7489 /* Editor is done; we can now apply unveil(2) */
7490 err = got_sigs_apply_unveil();
7493 err = apply_unveil(got_repo_get_path(repo), 0, NULL);
7498 err = got_object_tag_create(&tag_id, tag_name, commit_id,
7499 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, signer_id, repo,
7503 preserve_tagmsg = 1;
7507 err = got_ref_alloc(&ref, refname, tag_id);
7510 preserve_tagmsg = 1;
7514 err = got_ref_write(ref, repo);
7517 preserve_tagmsg = 1;
7521 err = got_object_id_str(&tag_id_str, tag_id);
7524 preserve_tagmsg = 1;
7527 printf("Created tag %s\n", tag_id_str);
7529 if (preserve_tagmsg) {
7530 fprintf(stderr, "%s: tag message preserved in %s\n",
7531 getprogname(), tagmsg_path);
7532 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
7533 err = got_error_from_errno2("unlink", tagmsg_path);
7538 free(commit_id_str);
7542 got_ref_list_free(&refs);
7546 static const struct got_error *
7547 cmd_tag(int argc, char *argv[])
7549 const struct got_error *error = NULL;
7550 struct got_repository *repo = NULL;
7551 struct got_worktree *worktree = NULL;
7552 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
7553 char *gitconfig_path = NULL, *tagger = NULL;
7554 char *allowed_signers = NULL, *revoked_signers = NULL;
7555 const char *signer_id = NULL;
7556 const char *tag_name = NULL, *commit_id_arg = NULL, *tagmsg = NULL;
7557 int ch, do_list = 0, verify_tags = 0, verbosity = 0;
7558 int *pack_fds = NULL;
7561 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
7562 "sendfd unveil", NULL) == -1)
7566 while ((ch = getopt(argc, argv, "c:lm:r:s:Vv")) != -1) {
7569 commit_id_arg = optarg;
7578 repo_path = realpath(optarg, NULL);
7579 if (repo_path == NULL) {
7580 error = got_error_from_errno2("realpath",
7584 got_path_strip_trailing_slashes(repo_path);
7595 else if (verbosity < 3)
7607 if (do_list || verify_tags) {
7608 if (commit_id_arg != NULL)
7610 "-c option can only be used when creating a tag");
7613 option_conflict('l', 'm');
7615 option_conflict('V', 'm');
7619 option_conflict('l', 's');
7621 option_conflict('V', 's');
7625 } else if (argc != 1)
7631 cwd = getcwd(NULL, 0);
7633 error = got_error_from_errno("getcwd");
7637 error = got_repo_pack_fds_open(&pack_fds);
7641 if (repo_path == NULL) {
7642 error = got_worktree_open(&worktree, cwd);
7643 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7649 strdup(got_worktree_get_repo_path(worktree));
7650 if (repo_path == NULL)
7651 error = got_error_from_errno("strdup");
7655 repo_path = strdup(cwd);
7656 if (repo_path == NULL) {
7657 error = got_error_from_errno("strdup");
7663 if (do_list || verify_tags) {
7664 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7667 error = get_allowed_signers(&allowed_signers, repo, worktree);
7670 error = get_revoked_signers(&revoked_signers, repo, worktree);
7674 /* Release work tree lock. */
7675 got_worktree_close(worktree);
7680 * Remove "cpath" promise unless needed for signature tmpfile
7684 got_sigs_apply_unveil();
7687 if (pledge("stdio rpath wpath flock proc exec sendfd "
7688 "unveil", NULL) == -1)
7692 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7695 error = list_tags(repo, tag_name, verify_tags, allowed_signers,
7696 revoked_signers, verbosity);
7698 error = get_gitconfig_path(&gitconfig_path);
7701 error = got_repo_open(&repo, repo_path, gitconfig_path,
7706 error = get_author(&tagger, repo, worktree);
7709 if (signer_id == NULL)
7710 signer_id = get_signer_id(repo, worktree);
7714 error = got_sigs_apply_unveil();
7718 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
7723 if (commit_id_arg == NULL) {
7724 struct got_reference *head_ref;
7725 struct got_object_id *commit_id;
7726 error = got_ref_open(&head_ref, repo,
7727 worktree ? got_worktree_get_head_ref_name(worktree)
7731 error = got_ref_resolve(&commit_id, repo, head_ref);
7732 got_ref_close(head_ref);
7735 error = got_object_id_str(&commit_id_str, commit_id);
7742 /* Release work tree lock. */
7743 got_worktree_close(worktree);
7747 error = add_tag(repo, tagger, tag_name,
7748 commit_id_str ? commit_id_str : commit_id_arg, tagmsg,
7749 signer_id, verbosity);
7753 const struct got_error *close_err = got_repo_close(repo);
7758 got_worktree_close(worktree);
7760 const struct got_error *pack_err =
7761 got_repo_pack_fds_close(pack_fds);
7767 free(gitconfig_path);
7768 free(commit_id_str);
7770 free(allowed_signers);
7771 free(revoked_signers);
7778 fprintf(stderr, "usage: %s add [-IR] path ...\n", getprogname());
7782 static const struct got_error *
7783 add_progress(void *arg, unsigned char status, const char *path)
7785 while (path[0] == '/')
7787 printf("%c %s\n", status, path);
7791 static const struct got_error *
7792 cmd_add(int argc, char *argv[])
7794 const struct got_error *error = NULL;
7795 struct got_repository *repo = NULL;
7796 struct got_worktree *worktree = NULL;
7798 struct got_pathlist_head paths;
7799 struct got_pathlist_entry *pe;
7800 int ch, can_recurse = 0, no_ignores = 0;
7801 int *pack_fds = NULL;
7806 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7811 while ((ch = getopt(argc, argv, "IR")) != -1) {
7831 cwd = getcwd(NULL, 0);
7833 error = got_error_from_errno("getcwd");
7837 error = got_repo_pack_fds_open(&pack_fds);
7841 error = got_worktree_open(&worktree, cwd);
7843 if (error->code == GOT_ERR_NOT_WORKTREE)
7844 error = wrap_not_worktree_error(error, "add", cwd);
7848 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7853 error = apply_unveil(got_repo_get_path(repo), 1,
7854 got_worktree_get_root_path(worktree));
7858 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7865 TAILQ_FOREACH(pe, &paths, entry) {
7866 if (asprintf(&ondisk_path, "%s/%s",
7867 got_worktree_get_root_path(worktree),
7869 error = got_error_from_errno("asprintf");
7872 if (lstat(ondisk_path, &sb) == -1) {
7873 if (errno == ENOENT) {
7877 error = got_error_from_errno2("lstat",
7883 if (S_ISDIR(sb.st_mode)) {
7884 error = got_error_msg(GOT_ERR_BAD_PATH,
7885 "adding directories requires -R option");
7891 error = got_worktree_schedule_add(worktree, &paths, add_progress,
7892 NULL, repo, no_ignores);
7895 const struct got_error *close_err = got_repo_close(repo);
7900 got_worktree_close(worktree);
7902 const struct got_error *pack_err =
7903 got_repo_pack_fds_close(pack_fds);
7907 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
7915 fprintf(stderr, "usage: %s remove [-fkR] [-s status-codes] path ...\n",
7920 static const struct got_error *
7921 print_remove_status(void *arg, unsigned char status,
7922 unsigned char staged_status, const char *path)
7924 while (path[0] == '/')
7926 if (status == GOT_STATUS_NONEXISTENT)
7928 if (status == staged_status && (status == GOT_STATUS_DELETE))
7929 status = GOT_STATUS_NO_CHANGE;
7930 printf("%c%c %s\n", status, staged_status, path);
7934 static const struct got_error *
7935 cmd_remove(int argc, char *argv[])
7937 const struct got_error *error = NULL;
7938 struct got_worktree *worktree = NULL;
7939 struct got_repository *repo = NULL;
7940 const char *status_codes = NULL;
7942 struct got_pathlist_head paths;
7943 struct got_pathlist_entry *pe;
7944 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0, i;
7945 int ignore_missing_paths = 0;
7946 int *pack_fds = NULL;
7951 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7956 while ((ch = getopt(argc, argv, "fkRs:")) != -1) {
7959 delete_local_mods = 1;
7960 ignore_missing_paths = 1;
7969 for (i = 0; i < strlen(optarg); i++) {
7970 switch (optarg[i]) {
7971 case GOT_STATUS_MODIFY:
7972 delete_local_mods = 1;
7974 case GOT_STATUS_MISSING:
7975 ignore_missing_paths = 1;
7978 errx(1, "invalid status code '%c'",
7982 status_codes = optarg;
7996 cwd = getcwd(NULL, 0);
7998 error = got_error_from_errno("getcwd");
8002 error = got_repo_pack_fds_open(&pack_fds);
8006 error = got_worktree_open(&worktree, cwd);
8008 if (error->code == GOT_ERR_NOT_WORKTREE)
8009 error = wrap_not_worktree_error(error, "remove", cwd);
8013 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8018 error = apply_unveil(got_repo_get_path(repo), 1,
8019 got_worktree_get_root_path(worktree));
8023 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8030 TAILQ_FOREACH(pe, &paths, entry) {
8031 if (asprintf(&ondisk_path, "%s/%s",
8032 got_worktree_get_root_path(worktree),
8034 error = got_error_from_errno("asprintf");
8037 if (lstat(ondisk_path, &sb) == -1) {
8038 if (errno == ENOENT) {
8042 error = got_error_from_errno2("lstat",
8048 if (S_ISDIR(sb.st_mode)) {
8049 error = got_error_msg(GOT_ERR_BAD_PATH,
8050 "removing directories requires -R option");
8056 error = got_worktree_schedule_delete(worktree, &paths,
8057 delete_local_mods, status_codes, print_remove_status, NULL,
8058 repo, keep_on_disk, ignore_missing_paths);
8061 const struct got_error *close_err = got_repo_close(repo);
8066 got_worktree_close(worktree);
8068 const struct got_error *pack_err =
8069 got_repo_pack_fds_close(pack_fds);
8073 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
8081 fprintf(stderr, "usage: %s patch [-nR] [-c commit] [-p strip-count] "
8082 "[patchfile]\n", getprogname());
8086 static const struct got_error *
8087 patch_from_stdin(int *patchfd)
8089 const struct got_error *err = NULL;
8092 sig_t sighup, sigint, sigquit;
8094 *patchfd = got_opentempfd();
8096 return got_error_from_errno("got_opentempfd");
8098 sighup = signal(SIGHUP, SIG_DFL);
8099 sigint = signal(SIGINT, SIG_DFL);
8100 sigquit = signal(SIGQUIT, SIG_DFL);
8103 r = read(0, buf, sizeof(buf));
8105 err = got_error_from_errno("read");
8110 if (write(*patchfd, buf, r) == -1) {
8111 err = got_error_from_errno("write");
8116 signal(SIGHUP, sighup);
8117 signal(SIGINT, sigint);
8118 signal(SIGQUIT, sigquit);
8120 if (err == NULL && lseek(*patchfd, 0, SEEK_SET) == -1)
8121 err = got_error_from_errno("lseek");
8131 static const struct got_error *
8132 patch_progress(void *arg, const char *old, const char *new,
8133 unsigned char status, const struct got_error *error, int old_from,
8134 int old_lines, int new_from, int new_lines, int offset,
8135 int ws_mangled, const struct got_error *hunk_err)
8137 const char *path = new == NULL ? old : new;
8139 while (*path == '/')
8143 printf("%c %s\n", status, path);
8146 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
8148 if (offset != 0 || hunk_err != NULL || ws_mangled) {
8149 printf("@@ -%d,%d +%d,%d @@ ", old_from,
8150 old_lines, new_from, new_lines);
8151 if (hunk_err != NULL)
8152 printf("%s\n", hunk_err->msg);
8153 else if (offset != 0)
8154 printf("applied with offset %d\n", offset);
8156 printf("hunk contains mangled whitespace\n");
8162 static const struct got_error *
8163 cmd_patch(int argc, char *argv[])
8165 const struct got_error *error = NULL, *close_error = NULL;
8166 struct got_worktree *worktree = NULL;
8167 struct got_repository *repo = NULL;
8168 struct got_reflist_head refs;
8169 struct got_object_id *commit_id = NULL;
8170 const char *commit_id_str = NULL;
8174 int ch, nop = 0, strip = -1, reverse = 0;
8176 int *pack_fds = NULL;
8181 if (pledge("stdio rpath wpath cpath fattr proc exec sendfd flock "
8182 "unveil", NULL) == -1)
8186 while ((ch = getopt(argc, argv, "c:np:R")) != -1) {
8189 commit_id_str = optarg;
8195 strip = strtonum(optarg, 0, INT_MAX, &errstr);
8197 errx(1, "pathname strip count is %s: %s",
8213 error = patch_from_stdin(&patchfd);
8216 } else if (argc == 1) {
8217 patchfd = open(argv[0], O_RDONLY);
8218 if (patchfd == -1) {
8219 error = got_error_from_errno2("open", argv[0]);
8222 if (fstat(patchfd, &sb) == -1) {
8223 error = got_error_from_errno2("fstat", argv[0]);
8226 if (!S_ISREG(sb.st_mode)) {
8227 error = got_error_path(argv[0], GOT_ERR_BAD_FILETYPE);
8233 if ((cwd = getcwd(NULL, 0)) == NULL) {
8234 error = got_error_from_errno("getcwd");
8238 error = got_repo_pack_fds_open(&pack_fds);
8242 error = got_worktree_open(&worktree, cwd);
8246 const char *repo_path = got_worktree_get_repo_path(worktree);
8247 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8251 error = apply_unveil(got_repo_get_path(repo), 0,
8252 got_worktree_get_root_path(worktree));
8256 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
8260 if (commit_id_str != NULL) {
8261 error = got_repo_match_object_id(&commit_id, NULL,
8262 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
8267 error = got_patch(patchfd, worktree, repo, nop, strip, reverse,
8268 commit_id, &patch_progress, NULL, check_cancelled, NULL);
8271 got_ref_list_free(&refs);
8274 close_error = got_repo_close(repo);
8276 error = close_error;
8278 if (worktree != NULL) {
8279 close_error = got_worktree_close(worktree);
8281 error = close_error;
8284 const struct got_error *pack_err =
8285 got_repo_pack_fds_close(pack_fds);
8296 fprintf(stderr, "usage: %s revert [-pR] [-F response-script] path ...\n",
8301 static const struct got_error *
8302 revert_progress(void *arg, unsigned char status, const char *path)
8304 if (status == GOT_STATUS_UNVERSIONED)
8307 while (path[0] == '/')
8309 printf("%c %s\n", status, path);
8313 struct choose_patch_arg {
8314 FILE *patch_script_file;
8318 static const struct got_error *
8319 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
8320 int nchanges, const char *action)
8322 const struct got_error *err;
8324 size_t linesize = 0;
8328 case GOT_STATUS_ADD:
8329 printf("A %s\n%s this addition? [y/n] ", path, action);
8331 case GOT_STATUS_DELETE:
8332 printf("D %s\n%s this deletion? [y/n] ", path, action);
8334 case GOT_STATUS_MODIFY:
8335 if (fseek(patch_file, 0L, SEEK_SET) == -1)
8336 return got_error_from_errno("fseek");
8337 printf(GOT_COMMIT_SEP_STR);
8338 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
8340 if (linelen == -1 && ferror(patch_file)) {
8341 err = got_error_from_errno("getline");
8346 printf(GOT_COMMIT_SEP_STR);
8347 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
8348 path, n, nchanges, action);
8351 return got_error_path(path, GOT_ERR_FILE_STATUS);
8358 static const struct got_error *
8359 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
8360 FILE *patch_file, int n, int nchanges)
8362 const struct got_error *err = NULL;
8364 size_t linesize = 0;
8367 struct choose_patch_arg *a = arg;
8369 *choice = GOT_PATCH_CHOICE_NONE;
8371 if (a->patch_script_file) {
8373 err = show_change(status, path, patch_file, n, nchanges,
8377 linelen = getline(&line, &linesize, a->patch_script_file);
8378 if (linelen == -1) {
8379 if (ferror(a->patch_script_file))
8380 return got_error_from_errno("getline");
8383 nl = strchr(line, '\n');
8386 if (strcmp(line, "y") == 0) {
8387 *choice = GOT_PATCH_CHOICE_YES;
8389 } else if (strcmp(line, "n") == 0) {
8390 *choice = GOT_PATCH_CHOICE_NO;
8392 } else if (strcmp(line, "q") == 0 &&
8393 status == GOT_STATUS_MODIFY) {
8394 *choice = GOT_PATCH_CHOICE_QUIT;
8397 printf("invalid response '%s'\n", line);
8402 while (resp != 'y' && resp != 'n' && resp != 'q') {
8403 err = show_change(status, path, patch_file, n, nchanges,
8410 if (status == GOT_STATUS_MODIFY) {
8411 if (resp != 'y' && resp != 'n' && resp != 'q') {
8412 printf("invalid response '%c'\n", resp);
8415 } else if (resp != 'y' && resp != 'n') {
8416 printf("invalid response '%c'\n", resp);
8422 *choice = GOT_PATCH_CHOICE_YES;
8423 else if (resp == 'n')
8424 *choice = GOT_PATCH_CHOICE_NO;
8425 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
8426 *choice = GOT_PATCH_CHOICE_QUIT;
8431 struct wt_commitable_path_arg {
8432 struct got_pathlist_head *commit_paths;
8437 * Shortcut work tree status callback to determine if the set of paths scanned
8438 * has at least one versioned path that is being modified and, if not NULL, is
8439 * in the arg->commit_paths list. Set arg and return GOT_ERR_FILE_MODIFIED as
8440 * soon as a path is passed with a status that satisfies this criteria.
8442 static const struct got_error *
8443 worktree_has_commitable_path(void *arg, unsigned char status,
8444 unsigned char staged_status, const char *path,
8445 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8446 struct got_object_id *commit_id, int dirfd, const char *de_name)
8448 struct wt_commitable_path_arg *a = arg;
8450 if (status == staged_status && (status == GOT_STATUS_DELETE))
8451 status = GOT_STATUS_NO_CHANGE;
8453 if (!(status == GOT_STATUS_NO_CHANGE ||
8454 status == GOT_STATUS_UNVERSIONED) ||
8455 staged_status != GOT_STATUS_NO_CHANGE) {
8456 if (a->commit_paths != NULL) {
8457 struct got_pathlist_entry *pe;
8459 TAILQ_FOREACH(pe, a->commit_paths, entry) {
8460 if (strncmp(path, pe->path,
8461 pe->path_len) == 0) {
8462 *a->has_changes = 1;
8467 *a->has_changes = 1;
8469 if (*a->has_changes)
8470 return got_error(GOT_ERR_FILE_MODIFIED);
8477 * Check that the changeset of the commit identified by id is
8478 * comprised of at least one modified path that is being committed.
8480 static const struct got_error *
8481 commit_path_changed_in_worktree(struct wt_commitable_path_arg *wcpa,
8482 struct got_object_id *id, struct got_worktree *worktree,
8483 struct got_repository *repo)
8485 const struct got_error *err;
8486 struct got_pathlist_head paths;
8487 struct got_commit_object *commit = NULL, *pcommit = NULL;
8488 struct got_tree_object *tree = NULL, *ptree = NULL;
8489 struct got_object_qid *pid;
8493 err = got_object_open_as_commit(&commit, repo, id);
8497 err = got_object_open_as_tree(&tree, repo,
8498 got_object_commit_get_tree_id(commit));
8502 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
8504 err = got_object_open_as_commit(&pcommit, repo, &pid->id);
8508 err = got_object_open_as_tree(&ptree, repo,
8509 got_object_commit_get_tree_id(pcommit));
8514 err = got_diff_tree(ptree, tree, NULL, NULL, -1, -1, "", "", repo,
8515 got_diff_tree_collect_changed_paths, &paths, 0);
8519 err = got_worktree_status(worktree, &paths, repo, 0,
8520 worktree_has_commitable_path, wcpa, check_cancelled, NULL);
8521 if (err && err->code == GOT_ERR_FILE_MODIFIED) {
8523 * At least one changed path in the referenced commit is
8524 * modified in the work tree, that's all we need to know!
8530 got_pathlist_free(&paths, GOT_PATHLIST_FREE_ALL);
8532 got_object_commit_close(commit);
8534 got_object_commit_close(pcommit);
8536 got_object_tree_close(tree);
8538 got_object_tree_close(ptree);
8543 * Remove any "logmsg" reference comprised entirely of paths that have
8544 * been reverted in this work tree. If any path in the logmsg ref changeset
8545 * remains in a changed state in the worktree, do not remove the reference.
8547 static const struct got_error *
8548 rm_logmsg_ref(struct got_worktree *worktree, struct got_repository *repo)
8550 const struct got_error *err;
8551 struct got_reflist_head refs;
8552 struct got_reflist_entry *re;
8553 struct got_commit_object *commit = NULL;
8554 struct got_object_id *commit_id = NULL;
8555 struct wt_commitable_path_arg wcpa;
8556 char *uuidstr = NULL;
8560 err = got_worktree_get_uuid(&uuidstr, worktree);
8564 err = got_ref_list(&refs, repo, "refs/got/worktree",
8565 got_ref_cmp_by_name, repo);
8569 TAILQ_FOREACH(re, &refs, entry) {
8570 const char *refname;
8571 int has_changes = 0;
8573 refname = got_ref_get_name(re->ref);
8575 if (!strncmp(refname, GOT_WORKTREE_CHERRYPICK_REF_PREFIX,
8576 GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN))
8577 refname += GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN + 1;
8578 else if (!strncmp(refname, GOT_WORKTREE_BACKOUT_REF_PREFIX,
8579 GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN))
8580 refname += GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN + 1;
8584 if (strncmp(refname, uuidstr, GOT_WORKTREE_UUID_STRLEN) == 0)
8585 refname += GOT_WORKTREE_UUID_STRLEN + 1; /* skip '-' */
8589 err = got_repo_match_object_id(&commit_id, NULL, refname,
8590 GOT_OBJ_TYPE_COMMIT, NULL, repo);
8594 err = got_object_open_as_commit(&commit, repo, commit_id);
8598 wcpa.commit_paths = NULL;
8599 wcpa.has_changes = &has_changes;
8601 err = commit_path_changed_in_worktree(&wcpa, commit_id,
8607 err = got_ref_delete(re->ref, repo);
8612 got_object_commit_close(commit);
8621 got_ref_list_free(&refs);
8623 got_object_commit_close(commit);
8627 static const struct got_error *
8628 cmd_revert(int argc, char *argv[])
8630 const struct got_error *error = NULL;
8631 struct got_worktree *worktree = NULL;
8632 struct got_repository *repo = NULL;
8633 char *cwd = NULL, *path = NULL;
8634 struct got_pathlist_head paths;
8635 struct got_pathlist_entry *pe;
8636 int ch, can_recurse = 0, pflag = 0;
8637 FILE *patch_script_file = NULL;
8638 const char *patch_script_path = NULL;
8639 struct choose_patch_arg cpa;
8640 int *pack_fds = NULL;
8645 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8646 "unveil", NULL) == -1)
8650 while ((ch = getopt(argc, argv, "F:pR")) != -1) {
8653 patch_script_path = optarg;
8672 if (patch_script_path && !pflag)
8673 errx(1, "-F option can only be used together with -p option");
8675 cwd = getcwd(NULL, 0);
8677 error = got_error_from_errno("getcwd");
8681 error = got_repo_pack_fds_open(&pack_fds);
8685 error = got_worktree_open(&worktree, cwd);
8687 if (error->code == GOT_ERR_NOT_WORKTREE)
8688 error = wrap_not_worktree_error(error, "revert", cwd);
8692 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8697 if (patch_script_path) {
8698 patch_script_file = fopen(patch_script_path, "re");
8699 if (patch_script_file == NULL) {
8700 error = got_error_from_errno2("fopen",
8707 * XXX "c" perm needed on repo dir to delete merge references.
8709 error = apply_unveil(got_repo_get_path(repo), 0,
8710 got_worktree_get_root_path(worktree));
8714 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8721 TAILQ_FOREACH(pe, &paths, entry) {
8722 if (asprintf(&ondisk_path, "%s/%s",
8723 got_worktree_get_root_path(worktree),
8725 error = got_error_from_errno("asprintf");
8728 if (lstat(ondisk_path, &sb) == -1) {
8729 if (errno == ENOENT) {
8733 error = got_error_from_errno2("lstat",
8739 if (S_ISDIR(sb.st_mode)) {
8740 error = got_error_msg(GOT_ERR_BAD_PATH,
8741 "reverting directories requires -R option");
8747 cpa.patch_script_file = patch_script_file;
8748 cpa.action = "revert";
8749 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
8750 pflag ? choose_patch : NULL, &cpa, repo);
8752 error = rm_logmsg_ref(worktree, repo);
8754 if (patch_script_file && fclose(patch_script_file) == EOF &&
8756 error = got_error_from_errno2("fclose", patch_script_path);
8758 const struct got_error *close_err = got_repo_close(repo);
8763 got_worktree_close(worktree);
8765 const struct got_error *pack_err =
8766 got_repo_pack_fds_close(pack_fds);
8770 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
8779 fprintf(stderr, "usage: %s commit [-NnS] [-A author] [-F path] "
8780 "[-m message] [path ...]\n", getprogname());
8784 struct collect_commit_logmsg_arg {
8785 const char *cmdline_log;
8786 const char *prepared_log;
8787 const char *merged_log;
8788 int non_interactive;
8790 const char *worktree_path;
8791 const char *branch_name;
8792 const char *repo_path;
8797 static const struct got_error *
8798 read_prepared_logmsg(char **logmsg, const char *path)
8800 const struct got_error *err = NULL;
8806 memset(&sb, 0, sizeof(sb));
8808 f = fopen(path, "re");
8810 return got_error_from_errno2("fopen", path);
8812 if (fstat(fileno(f), &sb) == -1) {
8813 err = got_error_from_errno2("fstat", path);
8816 if (sb.st_size == 0) {
8817 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
8821 *logmsg = malloc(sb.st_size + 1);
8822 if (*logmsg == NULL) {
8823 err = got_error_from_errno("malloc");
8827 r = fread(*logmsg, 1, sb.st_size, f);
8828 if (r != sb.st_size) {
8830 err = got_error_from_errno2("fread", path);
8832 err = got_error(GOT_ERR_IO);
8835 (*logmsg)[sb.st_size] = '\0';
8837 if (fclose(f) == EOF && err == NULL)
8838 err = got_error_from_errno2("fclose", path);
8846 static const struct got_error *
8847 collect_commit_logmsg(struct got_pathlist_head *commitable_paths,
8848 const char *diff_path, char **logmsg, void *arg)
8850 char *initial_content = NULL;
8851 struct got_pathlist_entry *pe;
8852 const struct got_error *err = NULL;
8853 char *template = NULL;
8854 char *prepared_msg = NULL, *merged_msg = NULL;
8855 struct collect_commit_logmsg_arg *a = arg;
8856 int initial_content_len;
8860 /* if a message was specified on the command line, just use it */
8861 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
8862 len = strlen(a->cmdline_log) + 1;
8863 *logmsg = malloc(len + 1);
8864 if (*logmsg == NULL)
8865 return got_error_from_errno("malloc");
8866 strlcpy(*logmsg, a->cmdline_log, len);
8868 } else if (a->prepared_log != NULL && a->non_interactive)
8869 return read_prepared_logmsg(logmsg, a->prepared_log);
8871 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
8872 return got_error_from_errno("asprintf");
8874 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template, "");
8878 if (a->prepared_log) {
8879 err = read_prepared_logmsg(&prepared_msg, a->prepared_log);
8882 } else if (a->merged_log) {
8883 err = read_prepared_logmsg(&merged_msg, a->merged_log);
8888 initial_content_len = asprintf(&initial_content,
8889 "%s%s\n# changes to be committed on branch %s:\n",
8890 prepared_msg ? prepared_msg : "",
8891 merged_msg ? merged_msg : "", a->branch_name);
8892 if (initial_content_len == -1) {
8893 err = got_error_from_errno("asprintf");
8897 if (write(fd, initial_content, initial_content_len) == -1) {
8898 err = got_error_from_errno2("write", a->logmsg_path);
8902 TAILQ_FOREACH(pe, commitable_paths, entry) {
8903 struct got_commitable *ct = pe->data;
8904 dprintf(fd, "# %c %s\n",
8905 got_commitable_get_status(ct),
8906 got_commitable_get_path(ct));
8910 dprintf(fd, "# detailed changes can be viewed in %s\n",
8914 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content,
8915 initial_content_len, a->prepared_log ? 0 : 1);
8917 free(initial_content);
8922 if (fd != -1 && close(fd) == -1 && err == NULL)
8923 err = got_error_from_errno2("close", a->logmsg_path);
8925 /* Editor is done; we can now apply unveil(2) */
8927 err = apply_unveil(a->repo_path, 0, a->worktree_path);
8935 static const struct got_error *
8936 cat_logmsg(FILE *f, struct got_commit_object *commit, const char *idstr,
8937 const char *type, int has_content)
8939 const struct got_error *err = NULL;
8940 char *logmsg = NULL;
8942 err = got_object_commit_get_logmsg(&logmsg, commit);
8946 if (fprintf(f, "%s# log message of %s commit %s:%s",
8947 has_content ? "\n" : "", type, idstr, logmsg) < 0)
8948 err = got_ferror(f, GOT_ERR_IO);
8955 * Lookup "logmsg" references of backed-out and cherrypicked commits
8956 * belonging to the current work tree. If found, and the worktree has
8957 * at least one modified file that was changed in the referenced commit,
8958 * add its log message to a new temporary file at *logmsg_path.
8959 * Add all refs found to matched_refs to be scheduled for removal on
8960 * successful commit.
8962 static const struct got_error *
8963 lookup_logmsg_ref(char **logmsg_path, struct got_pathlist_head *paths,
8964 struct got_reflist_head *matched_refs, struct got_worktree *worktree,
8965 struct got_repository *repo)
8967 const struct got_error *err;
8968 struct got_commit_object *commit = NULL;
8969 struct got_object_id *id = NULL;
8970 struct got_reflist_head refs;
8971 struct got_reflist_entry *re, *re_match;
8973 char *uuidstr = NULL;
8974 int added_logmsg = 0;
8978 *logmsg_path = NULL;
8980 err = got_worktree_get_uuid(&uuidstr, worktree);
8984 err = got_ref_list(&refs, repo, "refs/got/worktree",
8985 got_ref_cmp_by_name, repo);
8989 TAILQ_FOREACH(re, &refs, entry) {
8990 const char *refname, *type;
8991 struct wt_commitable_path_arg wcpa;
8994 refname = got_ref_get_name(re->ref);
8996 if (strncmp(refname, GOT_WORKTREE_CHERRYPICK_REF_PREFIX,
8997 GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN) == 0) {
8998 refname += GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN + 1;
8999 type = "cherrypicked";
9000 } else if (strncmp(refname, GOT_WORKTREE_BACKOUT_REF_PREFIX,
9001 GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN) == 0) {
9002 refname += GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN + 1;
9003 type = "backed-out";
9007 if (strncmp(refname, uuidstr, GOT_WORKTREE_UUID_STRLEN) == 0)
9008 refname += GOT_WORKTREE_UUID_STRLEN + 1; /* skip '-' */
9012 err = got_repo_match_object_id(&id, NULL, refname,
9013 GOT_OBJ_TYPE_COMMIT, NULL, repo);
9017 err = got_object_open_as_commit(&commit, repo, id);
9021 wcpa.commit_paths = paths;
9022 wcpa.has_changes = &add_logmsg;
9024 err = commit_path_changed_in_worktree(&wcpa, id,
9031 err = got_opentemp_named(logmsg_path, &f,
9032 "got-commit-logmsg", "");
9036 err = cat_logmsg(f, commit, refname, type,
9043 err = got_reflist_entry_dup(&re_match, re);
9046 TAILQ_INSERT_HEAD(matched_refs, re_match, entry);
9049 got_object_commit_close(commit);
9058 got_ref_list_free(&refs);
9060 got_object_commit_close(commit);
9061 if (f && fclose(f) == EOF && err == NULL)
9062 err = got_error_from_errno("fclose");
9063 if (!added_logmsg) {
9064 if (*logmsg_path && unlink(*logmsg_path) != 0 && err == NULL)
9065 err = got_error_from_errno2("unlink", *logmsg_path);
9066 *logmsg_path = NULL;
9071 static const struct got_error *
9072 cmd_commit(int argc, char *argv[])
9074 const struct got_error *error = NULL;
9075 struct got_worktree *worktree = NULL;
9076 struct got_repository *repo = NULL;
9077 char *cwd = NULL, *id_str = NULL;
9078 struct got_object_id *id = NULL;
9079 const char *logmsg = NULL;
9080 char *prepared_logmsg = NULL, *merged_logmsg = NULL;
9081 struct collect_commit_logmsg_arg cl_arg;
9082 const char *author = NULL;
9083 char *gitconfig_path = NULL, *editor = NULL, *committer = NULL;
9084 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
9085 int allow_bad_symlinks = 0, non_interactive = 0, merge_in_progress = 0;
9087 struct got_pathlist_head paths;
9088 struct got_reflist_head refs;
9089 struct got_reflist_entry *re;
9090 int *pack_fds = NULL;
9094 cl_arg.logmsg_path = NULL;
9097 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9098 "unveil", NULL) == -1)
9102 while ((ch = getopt(argc, argv, "A:F:m:NnS")) != -1) {
9106 error = valid_author(author);
9112 option_conflict('F', 'm');
9113 prepared_logmsg = realpath(optarg, NULL);
9114 if (prepared_logmsg == NULL)
9115 return got_error_from_errno2("realpath",
9119 if (prepared_logmsg)
9120 option_conflict('m', 'F');
9124 non_interactive = 1;
9130 allow_bad_symlinks = 1;
9141 cwd = getcwd(NULL, 0);
9143 error = got_error_from_errno("getcwd");
9147 error = got_repo_pack_fds_open(&pack_fds);
9151 error = got_worktree_open(&worktree, cwd);
9153 if (error->code == GOT_ERR_NOT_WORKTREE)
9154 error = wrap_not_worktree_error(error, "commit", cwd);
9158 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
9161 if (rebase_in_progress) {
9162 error = got_error(GOT_ERR_REBASING);
9166 error = got_worktree_histedit_in_progress(&histedit_in_progress,
9171 error = get_gitconfig_path(&gitconfig_path);
9174 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9175 gitconfig_path, pack_fds);
9179 error = got_worktree_merge_in_progress(&merge_in_progress, worktree, repo);
9182 if (merge_in_progress) {
9183 error = got_error(GOT_ERR_MERGE_BUSY);
9187 error = get_author(&committer, repo, worktree);
9195 * unveil(2) traverses exec(2); if an editor is used we have
9196 * to apply unveil after the log message has been written.
9198 if (logmsg == NULL || strlen(logmsg) == 0)
9199 error = get_editor(&editor);
9201 error = apply_unveil(got_repo_get_path(repo), 0,
9202 got_worktree_get_root_path(worktree));
9206 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
9210 if (prepared_logmsg == NULL) {
9211 error = lookup_logmsg_ref(&merged_logmsg,
9212 argc > 0 ? &paths : NULL, &refs, worktree, repo);
9217 cl_arg.editor = editor;
9218 cl_arg.cmdline_log = logmsg;
9219 cl_arg.prepared_log = prepared_logmsg;
9220 cl_arg.merged_log = merged_logmsg;
9221 cl_arg.non_interactive = non_interactive;
9222 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
9223 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
9224 if (!histedit_in_progress) {
9225 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
9226 error = got_error(GOT_ERR_COMMIT_BRANCH);
9229 cl_arg.branch_name += 11;
9231 cl_arg.repo_path = got_repo_get_path(repo);
9232 error = got_worktree_commit(&id, worktree, &paths, author, committer,
9233 allow_bad_symlinks, show_diff, collect_commit_logmsg, &cl_arg,
9234 print_status, NULL, repo);
9236 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
9237 cl_arg.logmsg_path != NULL)
9238 preserve_logmsg = 1;
9242 error = got_object_id_str(&id_str, id);
9245 printf("Created commit %s\n", id_str);
9247 TAILQ_FOREACH(re, &refs, entry) {
9248 error = got_ref_delete(re->ref, repo);
9254 if (preserve_logmsg) {
9255 fprintf(stderr, "%s: log message preserved in %s\n",
9256 getprogname(), cl_arg.logmsg_path);
9257 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
9259 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
9260 free(cl_arg.logmsg_path);
9261 if (merged_logmsg && unlink(merged_logmsg) == -1 && error == NULL)
9262 error = got_error_from_errno2("unlink", merged_logmsg);
9263 free(merged_logmsg);
9265 const struct got_error *close_err = got_repo_close(repo);
9270 got_worktree_close(worktree);
9272 const struct got_error *pack_err =
9273 got_repo_pack_fds_close(pack_fds);
9277 got_ref_list_free(&refs);
9278 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
9281 free(gitconfig_path);
9284 free(prepared_logmsg);
9291 fprintf(stderr, "usage: %s send [-afqTv] [-b branch] [-d branch] "
9292 "[-r repository-path] [-t tag] [remote-repository]\n",
9298 print_load_info(int print_colored, int print_found, int print_trees,
9299 int ncolored, int nfound, int ntrees)
9301 if (print_colored) {
9302 printf("%d commit%s colored", ncolored,
9303 ncolored == 1 ? "" : "s");
9306 printf("%s%d object%s found",
9307 ncolored > 0 ? "; " : "",
9308 nfound, nfound == 1 ? "" : "s");
9311 printf("; %d tree%s scanned", ntrees,
9312 ntrees == 1 ? "" : "s");
9316 struct got_send_progress_arg {
9317 char last_scaled_packsize[FMT_SCALED_STRSIZE];
9324 int last_nobj_total;
9328 int printed_something;
9330 struct got_pathlist_head *delete_branches;
9333 static const struct got_error *
9334 send_progress(void *arg, int ncolored, int nfound, int ntrees,
9335 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
9336 int nobj_written, off_t bytes_sent, const char *refname,
9337 const char *errmsg, int success)
9339 struct got_send_progress_arg *a = arg;
9340 char scaled_packsize[FMT_SCALED_STRSIZE];
9341 char scaled_sent[FMT_SCALED_STRSIZE];
9342 int p_deltify = 0, p_written = 0, p_sent = 0;
9343 int print_colored = 0, print_found = 0, print_trees = 0;
9344 int print_searching = 0, print_total = 0;
9345 int print_deltify = 0, print_written = 0, print_sent = 0;
9347 if (a->verbosity < 0)
9351 const char *status = success ? "accepted" : "rejected";
9354 struct got_pathlist_entry *pe;
9355 TAILQ_FOREACH(pe, a->delete_branches, entry) {
9356 const char *branchname = pe->path;
9357 if (got_path_cmp(branchname, refname,
9358 strlen(branchname), strlen(refname)) == 0) {
9360 a->sent_something = 1;
9366 if (a->printed_something)
9368 printf("Server has %s %s", status, refname);
9370 printf(": %s", errmsg);
9371 a->printed_something = 1;
9375 if (a->last_ncolored != ncolored) {
9377 a->last_ncolored = ncolored;
9380 if (a->last_nfound != nfound) {
9383 a->last_nfound = nfound;
9386 if (a->last_ntrees != ntrees) {
9390 a->last_ntrees = ntrees;
9393 if ((print_colored || print_found || print_trees) &&
9396 print_load_info(print_colored, print_found, print_trees,
9397 ncolored, nfound, ntrees);
9398 a->printed_something = 1;
9401 } else if (!a->loading_done) {
9403 print_load_info(1, 1, 1, ncolored, nfound, ntrees);
9405 a->loading_done = 1;
9408 if (fmt_scaled(packfile_size, scaled_packsize) == -1)
9409 return got_error_from_errno("fmt_scaled");
9410 if (fmt_scaled(bytes_sent, scaled_sent) == -1)
9411 return got_error_from_errno("fmt_scaled");
9413 if (a->last_ncommits != ncommits) {
9414 print_searching = 1;
9415 a->last_ncommits = ncommits;
9418 if (a->last_nobj_total != nobj_total) {
9419 print_searching = 1;
9421 a->last_nobj_total = nobj_total;
9424 if (packfile_size > 0 && (a->last_scaled_packsize[0] == '\0' ||
9425 strcmp(scaled_packsize, a->last_scaled_packsize)) != 0) {
9426 if (strlcpy(a->last_scaled_packsize, scaled_packsize,
9427 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
9428 return got_error(GOT_ERR_NO_SPACE);
9431 if (nobj_deltify > 0 || nobj_written > 0) {
9432 if (nobj_deltify > 0) {
9433 p_deltify = (nobj_deltify * 100) / nobj_total;
9434 if (p_deltify != a->last_p_deltify) {
9435 a->last_p_deltify = p_deltify;
9436 print_searching = 1;
9441 if (nobj_written > 0) {
9442 p_written = (nobj_written * 100) / nobj_total;
9443 if (p_written != a->last_p_written) {
9444 a->last_p_written = p_written;
9445 print_searching = 1;
9453 if (bytes_sent > 0) {
9454 p_sent = (bytes_sent * 100) / packfile_size;
9455 if (p_sent != a->last_p_sent) {
9456 a->last_p_sent = p_sent;
9457 print_searching = 1;
9463 a->sent_something = 1;
9466 if (print_searching || print_total || print_deltify || print_written ||
9469 if (print_searching)
9470 printf("packing %d reference%s", ncommits,
9471 ncommits == 1 ? "" : "s");
9473 printf("; %d object%s", nobj_total,
9474 nobj_total == 1 ? "" : "s");
9476 printf("; deltify: %d%%", p_deltify);
9478 printf("; uploading pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
9479 scaled_packsize, p_sent);
9480 else if (print_written)
9481 printf("; writing pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
9482 scaled_packsize, p_written);
9483 if (print_searching || print_total || print_deltify ||
9484 print_written || print_sent) {
9485 a->printed_something = 1;
9491 static const struct got_error *
9492 cmd_send(int argc, char *argv[])
9494 const struct got_error *error = NULL;
9495 char *cwd = NULL, *repo_path = NULL;
9496 const char *remote_name;
9497 char *proto = NULL, *host = NULL, *port = NULL;
9498 char *repo_name = NULL, *server_path = NULL;
9499 const struct got_remote_repo *remotes, *remote = NULL;
9500 int nremotes, nbranches = 0, ndelete_branches = 0;
9501 struct got_repository *repo = NULL;
9502 struct got_worktree *worktree = NULL;
9503 const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
9504 struct got_pathlist_head branches;
9505 struct got_pathlist_head tags;
9506 struct got_reflist_head all_branches;
9507 struct got_reflist_head all_tags;
9508 struct got_pathlist_head delete_args;
9509 struct got_pathlist_head delete_branches;
9510 struct got_reflist_entry *re;
9511 struct got_pathlist_entry *pe;
9512 int i, ch, sendfd = -1, sendstatus;
9514 struct got_send_progress_arg spa;
9515 int verbosity = 0, overwrite_refs = 0;
9516 int send_all_branches = 0, send_all_tags = 0;
9517 struct got_reference *ref = NULL;
9518 int *pack_fds = NULL;
9520 TAILQ_INIT(&branches);
9522 TAILQ_INIT(&all_branches);
9523 TAILQ_INIT(&all_tags);
9524 TAILQ_INIT(&delete_args);
9525 TAILQ_INIT(&delete_branches);
9527 while ((ch = getopt(argc, argv, "ab:d:fqr:Tt:v")) != -1) {
9530 send_all_branches = 1;
9533 error = got_pathlist_append(&branches, optarg, NULL);
9539 error = got_pathlist_append(&delete_args, optarg, NULL);
9550 repo_path = realpath(optarg, NULL);
9551 if (repo_path == NULL)
9552 return got_error_from_errno2("realpath",
9554 got_path_strip_trailing_slashes(repo_path);
9560 error = got_pathlist_append(&tags, optarg, NULL);
9567 else if (verbosity < 3)
9578 if (send_all_branches && !TAILQ_EMPTY(&branches))
9579 option_conflict('a', 'b');
9580 if (send_all_tags && !TAILQ_EMPTY(&tags))
9581 option_conflict('T', 't');
9585 remote_name = GOT_SEND_DEFAULT_REMOTE_NAME;
9587 remote_name = argv[0];
9591 cwd = getcwd(NULL, 0);
9593 error = got_error_from_errno("getcwd");
9597 error = got_repo_pack_fds_open(&pack_fds);
9601 if (repo_path == NULL) {
9602 error = got_worktree_open(&worktree, cwd);
9603 if (error && error->code != GOT_ERR_NOT_WORKTREE)
9609 strdup(got_worktree_get_repo_path(worktree));
9610 if (repo_path == NULL)
9611 error = got_error_from_errno("strdup");
9615 repo_path = strdup(cwd);
9616 if (repo_path == NULL) {
9617 error = got_error_from_errno("strdup");
9623 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
9628 worktree_conf = got_worktree_get_gotconfig(worktree);
9629 if (worktree_conf) {
9630 got_gotconfig_get_remotes(&nremotes, &remotes,
9632 for (i = 0; i < nremotes; i++) {
9633 if (strcmp(remotes[i].name, remote_name) == 0) {
9634 remote = &remotes[i];
9640 if (remote == NULL) {
9641 repo_conf = got_repo_get_gotconfig(repo);
9643 got_gotconfig_get_remotes(&nremotes, &remotes,
9645 for (i = 0; i < nremotes; i++) {
9646 if (strcmp(remotes[i].name, remote_name) == 0) {
9647 remote = &remotes[i];
9653 if (remote == NULL) {
9654 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
9655 for (i = 0; i < nremotes; i++) {
9656 if (strcmp(remotes[i].name, remote_name) == 0) {
9657 remote = &remotes[i];
9662 if (remote == NULL) {
9663 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
9667 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
9668 &repo_name, remote->send_url);
9672 if (strcmp(proto, "git") == 0) {
9674 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
9675 "sendfd dns inet unveil", NULL) == -1)
9678 } else if (strcmp(proto, "git+ssh") == 0 ||
9679 strcmp(proto, "ssh") == 0) {
9681 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
9682 "sendfd unveil", NULL) == -1)
9685 } else if (strcmp(proto, "http") == 0 ||
9686 strcmp(proto, "git+http") == 0) {
9687 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
9690 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
9694 error = got_dial_apply_unveil(proto);
9698 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
9702 if (send_all_branches) {
9703 error = got_ref_list(&all_branches, repo, "refs/heads",
9704 got_ref_cmp_by_name, NULL);
9707 TAILQ_FOREACH(re, &all_branches, entry) {
9708 const char *branchname = got_ref_get_name(re->ref);
9709 error = got_pathlist_append(&branches,
9715 } else if (nbranches == 0) {
9716 for (i = 0; i < remote->nsend_branches; i++) {
9717 error = got_pathlist_append(&branches,
9718 remote->send_branches[i], NULL);
9724 if (send_all_tags) {
9725 error = got_ref_list(&all_tags, repo, "refs/tags",
9726 got_ref_cmp_by_name, NULL);
9729 TAILQ_FOREACH(re, &all_tags, entry) {
9730 const char *tagname = got_ref_get_name(re->ref);
9731 error = got_pathlist_append(&tags,
9739 * To prevent accidents only branches in refs/heads/ can be deleted
9740 * with 'got send -d'.
9741 * Deleting anything else requires local repository access or Git.
9743 TAILQ_FOREACH(pe, &delete_args, entry) {
9744 const char *branchname = pe->path;
9746 struct got_pathlist_entry *new;
9747 if (strncmp(branchname, "refs/heads/", 11) == 0) {
9748 s = strdup(branchname);
9750 error = got_error_from_errno("strdup");
9754 if (asprintf(&s, "refs/heads/%s", branchname) == -1) {
9755 error = got_error_from_errno("asprintf");
9759 error = got_pathlist_insert(&new, &delete_branches, s, NULL);
9760 if (error || new == NULL /* duplicate */)
9767 if (nbranches == 0 && ndelete_branches == 0) {
9768 struct got_reference *head_ref;
9770 error = got_ref_open(&head_ref, repo,
9771 got_worktree_get_head_ref_name(worktree), 0);
9773 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
9776 if (got_ref_is_symbolic(head_ref)) {
9777 error = got_ref_resolve_symbolic(&ref, repo, head_ref);
9778 got_ref_close(head_ref);
9783 error = got_pathlist_append(&branches, got_ref_get_name(ref),
9790 if (verbosity >= 0) {
9791 printf("Connecting to \"%s\" %s://%s%s%s%s%s\n",
9792 remote->name, proto, host,
9793 port ? ":" : "", port ? port : "",
9794 *server_path == '/' ? "" : "/", server_path);
9797 error = got_send_connect(&sendpid, &sendfd, proto, host, port,
9798 server_path, verbosity);
9802 memset(&spa, 0, sizeof(spa));
9803 spa.last_scaled_packsize[0] = '\0';
9804 spa.last_p_deltify = -1;
9805 spa.last_p_written = -1;
9806 spa.verbosity = verbosity;
9807 spa.delete_branches = &delete_branches;
9808 error = got_send_pack(remote_name, &branches, &tags, &delete_branches,
9809 verbosity, overwrite_refs, sendfd, repo, send_progress, &spa,
9810 check_cancelled, NULL);
9811 if (spa.printed_something)
9815 if (!spa.sent_something && verbosity >= 0)
9816 printf("Already up-to-date\n");
9819 if (kill(sendpid, SIGTERM) == -1)
9820 error = got_error_from_errno("kill");
9821 if (waitpid(sendpid, &sendstatus, 0) == -1 && error == NULL)
9822 error = got_error_from_errno("waitpid");
9824 if (sendfd != -1 && close(sendfd) == -1 && error == NULL)
9825 error = got_error_from_errno("close");
9827 const struct got_error *close_err = got_repo_close(repo);
9832 got_worktree_close(worktree);
9834 const struct got_error *pack_err =
9835 got_repo_pack_fds_close(pack_fds);
9841 got_pathlist_free(&branches, GOT_PATHLIST_FREE_NONE);
9842 got_pathlist_free(&tags, GOT_PATHLIST_FREE_NONE);
9843 got_ref_list_free(&all_branches);
9844 got_ref_list_free(&all_tags);
9845 got_pathlist_free(&delete_args, GOT_PATHLIST_FREE_NONE);
9846 got_pathlist_free(&delete_branches, GOT_PATHLIST_FREE_PATH);
9858 * Print and if delete is set delete all ref_prefix references.
9859 * If wanted_ref is not NULL, only print or delete this reference.
9861 static const struct got_error *
9862 process_logmsg_refs(const char *ref_prefix, size_t prefix_len,
9863 const char *wanted_ref, int delete, struct got_worktree *worktree,
9864 struct got_repository *repo)
9866 const struct got_error *err;
9867 struct got_pathlist_head paths;
9868 struct got_reflist_head refs;
9869 struct got_reflist_entry *re;
9870 struct got_reflist_object_id_map *refs_idmap = NULL;
9871 struct got_commit_object *commit = NULL;
9872 struct got_object_id *id = NULL;
9873 const char *header_prefix;
9874 char *uuidstr = NULL;
9880 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, repo);
9884 err = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
9888 if (worktree != NULL) {
9889 err = got_worktree_get_uuid(&uuidstr, worktree);
9895 if (strncmp(wanted_ref, "refs/heads/", 11) == 0)
9899 if (strcmp(ref_prefix, GOT_WORKTREE_BACKOUT_REF_PREFIX) == 0)
9900 header_prefix = "backout";
9902 header_prefix = "cherrypick";
9904 TAILQ_FOREACH(re, &refs, entry) {
9905 const char *refname, *wt;
9907 refname = got_ref_get_name(re->ref);
9909 err = check_cancelled(NULL);
9913 if (strncmp(refname, ref_prefix, prefix_len) == 0)
9914 refname += prefix_len + 1; /* skip '-' delimiter */
9920 if (worktree == NULL || strncmp(refname, uuidstr,
9921 GOT_WORKTREE_UUID_STRLEN) == 0)
9922 refname += GOT_WORKTREE_UUID_STRLEN + 1; /* skip '-' */
9926 err = got_repo_match_object_id(&id, NULL, refname,
9927 GOT_OBJ_TYPE_COMMIT, NULL, repo);
9931 err = got_object_open_as_commit(&commit, repo, id);
9936 found = strncmp(wanted_ref, refname,
9937 strlen(wanted_ref)) == 0;
9938 if (wanted_ref && !found) {
9939 struct got_reflist_head *ci_refs;
9941 ci_refs = got_reflist_object_id_map_lookup(refs_idmap,
9945 char *refs_str = NULL;
9946 char const *r = NULL;
9948 err = build_refs_str(&refs_str, ci_refs, id,
9955 if (strncmp(r, wanted_ref,
9956 strlen(wanted_ref)) == 0) {
9968 if (wanted_ref == NULL || found) {
9970 err = got_ref_delete(re->ref, repo);
9973 printf("Deleted: ");
9974 err = print_commit_oneline(commit, id, repo,
9978 * Print paths modified by commit to help
9979 * associate commits with worktree changes.
9981 err = get_changed_paths(&paths, commit,
9986 err = print_commit(commit, id, repo, NULL,
9987 &paths, NULL, 0, 0, refs_idmap, NULL,
9989 got_pathlist_free(&paths,
9990 GOT_PATHLIST_FREE_ALL);
9992 if (worktree == NULL)
9993 printf("work tree: %.*s\n\n",
9994 GOT_WORKTREE_UUID_STRLEN, wt);
10000 got_object_commit_close(commit);
10006 if (wanted_ref != NULL && !found)
10007 err = got_error_fmt(GOT_ERR_NOT_REF, "%s", wanted_ref);
10012 got_ref_list_free(&refs);
10013 got_pathlist_free(&paths, GOT_PATHLIST_FREE_ALL);
10015 got_reflist_object_id_map_free(refs_idmap);
10017 got_object_commit_close(commit);
10022 * Create new temp "logmsg" ref of the backed-out or cherrypicked commit
10023 * identified by id for log messages to prepopulate the editor on commit.
10025 static const struct got_error *
10026 logmsg_ref(struct got_object_id *id, const char *prefix,
10027 struct got_worktree *worktree, struct got_repository *repo)
10029 const struct got_error *err = NULL;
10030 char *idstr, *ref = NULL, *refname = NULL;
10031 int histedit_in_progress;
10032 int rebase_in_progress, merge_in_progress;
10035 * Silenty refuse to create merge reference if any histedit, merge,
10036 * or rebase operation is in progress.
10038 err = got_worktree_histedit_in_progress(&histedit_in_progress,
10042 if (histedit_in_progress)
10045 err = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
10048 if (rebase_in_progress)
10051 err = got_worktree_merge_in_progress(&merge_in_progress, worktree,
10055 if (merge_in_progress)
10058 err = got_object_id_str(&idstr, id);
10062 err = got_worktree_get_logmsg_ref_name(&refname, worktree, prefix);
10066 if (asprintf(&ref, "%s-%s", refname, idstr) == -1) {
10067 err = got_error_from_errno("asprintf");
10071 err = create_ref(ref, got_worktree_get_base_commit_id(worktree),
10081 usage_cherrypick(void)
10083 fprintf(stderr, "usage: %s cherrypick [-lX] [commit-id]\n",
10088 static const struct got_error *
10089 cmd_cherrypick(int argc, char *argv[])
10091 const struct got_error *error = NULL;
10092 struct got_worktree *worktree = NULL;
10093 struct got_repository *repo = NULL;
10094 char *cwd = NULL, *commit_id_str = NULL;
10095 struct got_object_id *commit_id = NULL;
10096 struct got_commit_object *commit = NULL;
10097 struct got_object_qid *pid;
10098 int ch, list_refs = 0, remove_refs = 0;
10099 struct got_update_progress_arg upa;
10100 int *pack_fds = NULL;
10103 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10104 "unveil", NULL) == -1)
10108 while ((ch = getopt(argc, argv, "lX")) != -1) {
10117 usage_cherrypick();
10125 if (list_refs || remove_refs) {
10126 if (argc != 0 && argc != 1)
10127 usage_cherrypick();
10128 } else if (argc != 1)
10129 usage_cherrypick();
10130 if (list_refs && remove_refs)
10131 option_conflict('l', 'X');
10133 cwd = getcwd(NULL, 0);
10135 error = got_error_from_errno("getcwd");
10139 error = got_repo_pack_fds_open(&pack_fds);
10143 error = got_worktree_open(&worktree, cwd);
10145 if (list_refs || remove_refs) {
10146 if (error->code != GOT_ERR_NOT_WORKTREE)
10149 if (error->code == GOT_ERR_NOT_WORKTREE)
10150 error = wrap_not_worktree_error(error,
10151 "cherrypick", cwd);
10156 error = got_repo_open(&repo,
10157 worktree ? got_worktree_get_repo_path(worktree) : cwd,
10162 error = apply_unveil(got_repo_get_path(repo), 0,
10163 worktree ? got_worktree_get_root_path(worktree) : NULL);
10167 if (list_refs || remove_refs) {
10168 error = process_logmsg_refs(GOT_WORKTREE_CHERRYPICK_REF_PREFIX,
10169 GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN,
10170 argc == 1 ? argv[0] : NULL, remove_refs, worktree, repo);
10174 error = got_repo_match_object_id(&commit_id, NULL, argv[0],
10175 GOT_OBJ_TYPE_COMMIT, NULL, repo);
10178 error = got_object_id_str(&commit_id_str, commit_id);
10182 error = got_object_open_as_commit(&commit, repo, commit_id);
10185 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
10186 memset(&upa, 0, sizeof(upa));
10187 error = got_worktree_merge_files(worktree, pid ? &pid->id : NULL,
10188 commit_id, repo, update_progress, &upa, check_cancelled,
10193 if (upa.did_something) {
10194 error = logmsg_ref(commit_id,
10195 GOT_WORKTREE_CHERRYPICK_REF_PREFIX, worktree, repo);
10198 printf("Merged commit %s\n", commit_id_str);
10200 print_merge_progress_stats(&upa);
10204 got_object_commit_close(commit);
10205 free(commit_id_str);
10207 got_worktree_close(worktree);
10209 const struct got_error *close_err = got_repo_close(repo);
10214 const struct got_error *pack_err =
10215 got_repo_pack_fds_close(pack_fds);
10224 usage_backout(void)
10226 fprintf(stderr, "usage: %s backout [-lX] [commit-id]\n", getprogname());
10230 static const struct got_error *
10231 cmd_backout(int argc, char *argv[])
10233 const struct got_error *error = NULL;
10234 struct got_worktree *worktree = NULL;
10235 struct got_repository *repo = NULL;
10236 char *cwd = NULL, *commit_id_str = NULL;
10237 struct got_object_id *commit_id = NULL;
10238 struct got_commit_object *commit = NULL;
10239 struct got_object_qid *pid;
10240 int ch, list_refs = 0, remove_refs = 0;
10241 struct got_update_progress_arg upa;
10242 int *pack_fds = NULL;
10245 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10246 "unveil", NULL) == -1)
10250 while ((ch = getopt(argc, argv, "lX")) != -1) {
10267 if (list_refs || remove_refs) {
10268 if (argc != 0 && argc != 1)
10270 } else if (argc != 1)
10272 if (list_refs && remove_refs)
10273 option_conflict('l', 'X');
10275 cwd = getcwd(NULL, 0);
10277 error = got_error_from_errno("getcwd");
10281 error = got_repo_pack_fds_open(&pack_fds);
10285 error = got_worktree_open(&worktree, cwd);
10287 if (list_refs || remove_refs) {
10288 if (error->code != GOT_ERR_NOT_WORKTREE)
10291 if (error->code == GOT_ERR_NOT_WORKTREE)
10292 error = wrap_not_worktree_error(error,
10298 error = got_repo_open(&repo,
10299 worktree ? got_worktree_get_repo_path(worktree) : cwd,
10304 error = apply_unveil(got_repo_get_path(repo), 0,
10305 worktree ? got_worktree_get_root_path(worktree) : NULL);
10309 if (list_refs || remove_refs) {
10310 error = process_logmsg_refs(GOT_WORKTREE_BACKOUT_REF_PREFIX,
10311 GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN,
10312 argc == 1 ? argv[0] : NULL, remove_refs, worktree, repo);
10316 error = got_repo_match_object_id(&commit_id, NULL, argv[0],
10317 GOT_OBJ_TYPE_COMMIT, NULL, repo);
10320 error = got_object_id_str(&commit_id_str, commit_id);
10324 error = got_object_open_as_commit(&commit, repo, commit_id);
10327 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
10329 error = got_error(GOT_ERR_ROOT_COMMIT);
10333 memset(&upa, 0, sizeof(upa));
10334 error = got_worktree_merge_files(worktree, commit_id, &pid->id,
10335 repo, update_progress, &upa, check_cancelled, NULL);
10339 if (upa.did_something) {
10340 error = logmsg_ref(commit_id, GOT_WORKTREE_BACKOUT_REF_PREFIX,
10344 printf("Backed out commit %s\n", commit_id_str);
10346 print_merge_progress_stats(&upa);
10350 got_object_commit_close(commit);
10351 free(commit_id_str);
10353 got_worktree_close(worktree);
10355 const struct got_error *close_err = got_repo_close(repo);
10360 const struct got_error *pack_err =
10361 got_repo_pack_fds_close(pack_fds);
10371 fprintf(stderr, "usage: %s rebase [-aclX] [branch]\n", getprogname());
10376 trim_logmsg(char *logmsg, int limit)
10381 len = strlen(logmsg);
10384 logmsg[len] = '\0';
10385 nl = strchr(logmsg, '\n');
10390 static const struct got_error *
10391 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
10393 const struct got_error *err;
10394 char *logmsg0 = NULL;
10397 err = got_object_commit_get_logmsg(&logmsg0, commit);
10402 while (isspace((unsigned char)s[0]))
10405 *logmsg = strdup(s);
10406 if (*logmsg == NULL) {
10407 err = got_error_from_errno("strdup");
10411 trim_logmsg(*logmsg, limit);
10417 static const struct got_error *
10418 show_rebase_merge_conflict(struct got_object_id *id,
10419 struct got_repository *repo)
10421 const struct got_error *err;
10422 struct got_commit_object *commit = NULL;
10423 char *id_str = NULL, *logmsg = NULL;
10425 err = got_object_open_as_commit(&commit, repo, id);
10429 err = got_object_id_str(&id_str, id);
10435 err = get_short_logmsg(&logmsg, 42, commit);
10439 printf("%s -> merge conflict: %s\n", id_str, logmsg);
10442 got_object_commit_close(commit);
10447 static const struct got_error *
10448 show_rebase_progress(struct got_commit_object *commit,
10449 struct got_object_id *old_id, struct got_object_id *new_id)
10451 const struct got_error *err;
10452 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
10454 err = got_object_id_str(&old_id_str, old_id);
10459 err = got_object_id_str(&new_id_str, new_id);
10464 old_id_str[12] = '\0';
10466 new_id_str[12] = '\0';
10468 err = get_short_logmsg(&logmsg, 42, commit);
10472 printf("%s -> %s: %s\n", old_id_str,
10473 new_id_str ? new_id_str : "no-op change", logmsg);
10481 static const struct got_error *
10482 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
10483 struct got_reference *branch, struct got_reference *tmp_branch,
10484 struct got_repository *repo, int create_backup)
10486 printf("Switching work tree to %s\n", got_ref_get_name(branch));
10487 return got_worktree_rebase_complete(worktree, fileindex,
10488 tmp_branch, branch, repo, create_backup);
10491 static const struct got_error *
10492 rebase_commit(struct got_pathlist_head *merged_paths,
10493 struct got_worktree *worktree, struct got_fileindex *fileindex,
10494 struct got_reference *tmp_branch, const char *committer,
10495 struct got_object_id *commit_id, struct got_repository *repo)
10497 const struct got_error *error;
10498 struct got_commit_object *commit;
10499 struct got_object_id *new_commit_id;
10501 error = got_object_open_as_commit(&commit, repo, commit_id);
10505 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
10506 worktree, fileindex, tmp_branch, committer, commit, commit_id,
10509 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
10511 error = show_rebase_progress(commit, commit_id, NULL);
10513 error = show_rebase_progress(commit, commit_id, new_commit_id);
10514 free(new_commit_id);
10517 got_object_commit_close(commit);
10521 struct check_path_prefix_arg {
10522 const char *path_prefix;
10527 static const struct got_error *
10528 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
10529 struct got_blob_object *blob2, FILE *f1, FILE *f2,
10530 struct got_object_id *id1, struct got_object_id *id2,
10531 const char *path1, const char *path2,
10532 mode_t mode1, mode_t mode2, struct got_repository *repo)
10534 struct check_path_prefix_arg *a = arg;
10536 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
10537 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
10538 return got_error(a->errcode);
10543 static const struct got_error *
10544 check_path_prefix(struct got_object_id *parent_id,
10545 struct got_object_id *commit_id, const char *path_prefix,
10546 int errcode, struct got_repository *repo)
10548 const struct got_error *err;
10549 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
10550 struct got_commit_object *commit = NULL, *parent_commit = NULL;
10551 struct check_path_prefix_arg cpp_arg;
10553 if (got_path_is_root_dir(path_prefix))
10556 err = got_object_open_as_commit(&commit, repo, commit_id);
10560 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
10564 err = got_object_open_as_tree(&tree1, repo,
10565 got_object_commit_get_tree_id(parent_commit));
10569 err = got_object_open_as_tree(&tree2, repo,
10570 got_object_commit_get_tree_id(commit));
10574 cpp_arg.path_prefix = path_prefix;
10575 while (cpp_arg.path_prefix[0] == '/')
10576 cpp_arg.path_prefix++;
10577 cpp_arg.len = strlen(cpp_arg.path_prefix);
10578 cpp_arg.errcode = errcode;
10579 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
10580 check_path_prefix_in_diff, &cpp_arg, 0);
10583 got_object_tree_close(tree1);
10585 got_object_tree_close(tree2);
10587 got_object_commit_close(commit);
10589 got_object_commit_close(parent_commit);
10593 static const struct got_error *
10594 collect_commits(struct got_object_id_queue *commits,
10595 struct got_object_id *initial_commit_id,
10596 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
10597 const char *path_prefix, int path_prefix_errcode,
10598 struct got_repository *repo)
10600 const struct got_error *err = NULL;
10601 struct got_commit_graph *graph = NULL;
10602 struct got_object_id parent_id, commit_id;
10603 struct got_object_qid *qid;
10605 err = got_commit_graph_open(&graph, "/", 1);
10609 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
10610 check_cancelled, NULL);
10614 memcpy(&commit_id, initial_commit_id, sizeof(commit_id));
10615 while (got_object_id_cmp(&commit_id, iter_stop_id) != 0) {
10616 err = got_commit_graph_iter_next(&parent_id, graph, repo,
10617 check_cancelled, NULL);
10619 if (err->code == GOT_ERR_ITER_COMPLETED) {
10620 err = got_error_msg(GOT_ERR_ANCESTRY,
10621 "ran out of commits to rebase before "
10622 "youngest common ancestor commit has "
10623 "been reached?!?");
10627 err = check_path_prefix(&parent_id, &commit_id,
10628 path_prefix, path_prefix_errcode, repo);
10632 err = got_object_qid_alloc(&qid, &commit_id);
10635 STAILQ_INSERT_HEAD(commits, qid, entry);
10637 memcpy(&commit_id, &parent_id, sizeof(commit_id));
10641 got_commit_graph_close(graph);
10645 static const struct got_error *
10646 get_commit_brief_str(char **brief_str, struct got_commit_object *commit)
10648 const struct got_error *err = NULL;
10649 time_t committer_time;
10651 char datebuf[11]; /* YYYY-MM-DD + NUL */
10652 char *author0 = NULL, *author, *smallerthan;
10653 char *logmsg0 = NULL, *logmsg, *newline;
10655 committer_time = got_object_commit_get_committer_time(commit);
10656 if (gmtime_r(&committer_time, &tm) == NULL)
10657 return got_error_from_errno("gmtime_r");
10658 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d", &tm) == 0)
10659 return got_error(GOT_ERR_NO_SPACE);
10661 author0 = strdup(got_object_commit_get_author(commit));
10662 if (author0 == NULL)
10663 return got_error_from_errno("strdup");
10665 smallerthan = strchr(author, '<');
10666 if (smallerthan && smallerthan[1] != '\0')
10667 author = smallerthan + 1;
10668 author[strcspn(author, "@>")] = '\0';
10670 err = got_object_commit_get_logmsg(&logmsg0, commit);
10674 while (*logmsg == '\n')
10676 newline = strchr(logmsg, '\n');
10680 if (asprintf(brief_str, "%s %s %s",
10681 datebuf, author, logmsg) == -1)
10682 err = got_error_from_errno("asprintf");
10689 static const struct got_error *
10690 delete_backup_ref(struct got_reference *ref, struct got_object_id *id,
10691 struct got_repository *repo)
10693 const struct got_error *err;
10696 err = got_object_id_str(&id_str, id);
10700 err = got_ref_delete(ref, repo);
10704 printf("Deleted %s: %s\n", got_ref_get_name(ref), id_str);
10710 static const struct got_error *
10711 print_backup_ref(const char *branch_name, const char *new_id_str,
10712 struct got_object_id *old_commit_id, struct got_commit_object *old_commit,
10713 struct got_reflist_object_id_map *refs_idmap,
10714 struct got_repository *repo)
10716 const struct got_error *err = NULL;
10717 struct got_reflist_head *refs;
10718 char *refs_str = NULL;
10719 struct got_object_id *new_commit_id = NULL;
10720 struct got_commit_object *new_commit = NULL;
10721 char *new_commit_brief_str = NULL;
10722 struct got_object_id *yca_id = NULL;
10723 struct got_commit_object *yca_commit = NULL;
10724 char *yca_id_str = NULL, *yca_brief_str = NULL;
10725 char *custom_refs_str;
10727 if (asprintf(&custom_refs_str, "formerly %s", branch_name) == -1)
10728 return got_error_from_errno("asprintf");
10730 err = print_commit(old_commit, old_commit_id, repo, NULL, NULL, NULL,
10731 0, 0, refs_idmap, custom_refs_str, NULL);
10735 err = got_object_resolve_id_str(&new_commit_id, repo, new_id_str);
10739 refs = got_reflist_object_id_map_lookup(refs_idmap, new_commit_id);
10741 err = build_refs_str(&refs_str, refs, new_commit_id, repo, 0);
10746 err = got_object_open_as_commit(&new_commit, repo, new_commit_id);
10750 err = get_commit_brief_str(&new_commit_brief_str, new_commit);
10754 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
10755 old_commit_id, new_commit_id, 1, repo, check_cancelled, NULL);
10759 printf("has become commit %s%s%s%s\n %s\n", new_id_str,
10760 refs_str ? " (" : "", refs_str ? refs_str : "",
10761 refs_str ? ")" : "", new_commit_brief_str);
10762 if (yca_id && got_object_id_cmp(yca_id, new_commit_id) != 0 &&
10763 got_object_id_cmp(yca_id, old_commit_id) != 0) {
10767 err = got_object_open_as_commit(&yca_commit, repo, yca_id);
10771 err = get_commit_brief_str(&yca_brief_str, yca_commit);
10775 err = got_object_id_str(&yca_id_str, yca_id);
10779 refs = got_reflist_object_id_map_lookup(refs_idmap, yca_id);
10781 err = build_refs_str(&refs_str, refs, yca_id, repo, 0);
10785 printf("history forked at %s%s%s%s\n %s\n",
10787 refs_str ? " (" : "", refs_str ? refs_str : "",
10788 refs_str ? ")" : "", yca_brief_str);
10791 free(custom_refs_str);
10792 free(new_commit_id);
10796 free(yca_brief_str);
10798 got_object_commit_close(new_commit);
10800 got_object_commit_close(yca_commit);
10805 static const struct got_error *
10806 worktree_has_logmsg_ref(const char *caller, struct got_worktree *worktree,
10807 struct got_repository *repo)
10809 const struct got_error *err;
10810 struct got_reflist_head refs;
10811 struct got_reflist_entry *re;
10812 char *uuidstr = NULL;
10813 static char msg[160];
10817 err = got_worktree_get_uuid(&uuidstr, worktree);
10821 err = got_ref_list(&refs, repo, "refs/got/worktree",
10822 got_ref_cmp_by_name, repo);
10826 TAILQ_FOREACH(re, &refs, entry) {
10827 const char *cmd, *refname, *type;
10829 refname = got_ref_get_name(re->ref);
10831 if (strncmp(refname, GOT_WORKTREE_CHERRYPICK_REF_PREFIX,
10832 GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN) == 0) {
10833 refname += GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN + 1;
10834 cmd = "cherrypick";
10835 type = "cherrypicked";
10836 } else if (strncmp(refname, GOT_WORKTREE_BACKOUT_REF_PREFIX,
10837 GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN) == 0) {
10838 refname += GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN + 1;
10840 type = "backed-out";
10844 if (strncmp(refname, uuidstr, GOT_WORKTREE_UUID_STRLEN) != 0)
10847 snprintf(msg, sizeof(msg),
10848 "work tree has references created by %s commits which "
10849 "must be removed with 'got %s -X' before running the %s "
10850 "command", type, cmd, caller);
10851 err = got_error_msg(GOT_ERR_WORKTREE_META, msg);
10857 got_ref_list_free(&refs);
10861 static const struct got_error *
10862 process_backup_refs(const char *backup_ref_prefix,
10863 const char *wanted_branch_name,
10864 int delete, struct got_repository *repo)
10866 const struct got_error *err;
10867 struct got_reflist_head refs, backup_refs;
10868 struct got_reflist_entry *re;
10869 const size_t backup_ref_prefix_len = strlen(backup_ref_prefix);
10870 struct got_object_id *old_commit_id = NULL;
10871 char *branch_name = NULL;
10872 struct got_commit_object *old_commit = NULL;
10873 struct got_reflist_object_id_map *refs_idmap = NULL;
10874 int wanted_branch_found = 0;
10877 TAILQ_INIT(&backup_refs);
10879 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
10883 err = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
10887 if (wanted_branch_name) {
10888 if (strncmp(wanted_branch_name, "refs/heads/", 11) == 0)
10889 wanted_branch_name += 11;
10892 err = got_ref_list(&backup_refs, repo, backup_ref_prefix,
10893 got_ref_cmp_by_commit_timestamp_descending, repo);
10897 TAILQ_FOREACH(re, &backup_refs, entry) {
10898 const char *refname = got_ref_get_name(re->ref);
10901 err = check_cancelled(NULL);
10905 err = got_ref_resolve(&old_commit_id, repo, re->ref);
10909 err = got_object_open_as_commit(&old_commit, repo,
10914 if (strncmp(backup_ref_prefix, refname,
10915 backup_ref_prefix_len) == 0)
10916 refname += backup_ref_prefix_len;
10918 while (refname[0] == '/')
10921 branch_name = strdup(refname);
10922 if (branch_name == NULL) {
10923 err = got_error_from_errno("strdup");
10926 slash = strrchr(branch_name, '/');
10929 refname += strlen(branch_name) + 1;
10932 if (wanted_branch_name == NULL ||
10933 strcmp(wanted_branch_name, branch_name) == 0) {
10934 wanted_branch_found = 1;
10936 err = delete_backup_ref(re->ref,
10937 old_commit_id, repo);
10939 err = print_backup_ref(branch_name, refname,
10940 old_commit_id, old_commit, refs_idmap,
10947 free(old_commit_id);
10948 old_commit_id = NULL;
10950 branch_name = NULL;
10951 got_object_commit_close(old_commit);
10955 if (wanted_branch_name && !wanted_branch_found) {
10956 err = got_error_fmt(GOT_ERR_NOT_REF,
10957 "%s/%s/", backup_ref_prefix, wanted_branch_name);
10961 got_reflist_object_id_map_free(refs_idmap);
10962 got_ref_list_free(&refs);
10963 got_ref_list_free(&backup_refs);
10964 free(old_commit_id);
10967 got_object_commit_close(old_commit);
10971 static const struct got_error *
10972 abort_progress(void *arg, unsigned char status, const char *path)
10975 * Unversioned files should not clutter progress output when
10976 * an operation is aborted.
10978 if (status == GOT_STATUS_UNVERSIONED)
10981 return update_progress(arg, status, path);
10984 static const struct got_error *
10985 cmd_rebase(int argc, char *argv[])
10987 const struct got_error *error = NULL;
10988 struct got_worktree *worktree = NULL;
10989 struct got_repository *repo = NULL;
10990 struct got_fileindex *fileindex = NULL;
10991 char *cwd = NULL, *committer = NULL, *gitconfig_path = NULL;
10992 struct got_reference *branch = NULL;
10993 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
10994 struct got_object_id *commit_id = NULL, *parent_id = NULL;
10995 struct got_object_id *resume_commit_id = NULL;
10996 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
10997 struct got_object_id *head_commit_id = NULL;
10998 struct got_reference *head_ref = NULL;
10999 struct got_commit_object *commit = NULL;
11000 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
11001 int histedit_in_progress = 0, merge_in_progress = 0;
11002 int create_backup = 1, list_backups = 0, delete_backups = 0;
11003 struct got_object_id_queue commits;
11004 struct got_pathlist_head merged_paths;
11005 const struct got_object_id_queue *parent_ids;
11006 struct got_object_qid *qid, *pid;
11007 struct got_update_progress_arg upa;
11008 int *pack_fds = NULL;
11010 STAILQ_INIT(&commits);
11011 TAILQ_INIT(&merged_paths);
11012 memset(&upa, 0, sizeof(upa));
11015 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
11016 "unveil", NULL) == -1)
11020 while ((ch = getopt(argc, argv, "aclX")) != -1) {
11026 continue_rebase = 1;
11032 delete_backups = 1;
11043 if (list_backups) {
11045 option_conflict('l', 'a');
11046 if (continue_rebase)
11047 option_conflict('l', 'c');
11048 if (delete_backups)
11049 option_conflict('l', 'X');
11050 if (argc != 0 && argc != 1)
11052 } else if (delete_backups) {
11054 option_conflict('X', 'a');
11055 if (continue_rebase)
11056 option_conflict('X', 'c');
11058 option_conflict('l', 'X');
11059 if (argc != 0 && argc != 1)
11062 if (abort_rebase && continue_rebase)
11064 else if (abort_rebase || continue_rebase) {
11067 } else if (argc != 1)
11071 cwd = getcwd(NULL, 0);
11073 error = got_error_from_errno("getcwd");
11077 error = got_repo_pack_fds_open(&pack_fds);
11081 error = got_worktree_open(&worktree, cwd);
11083 if (list_backups || delete_backups) {
11084 if (error->code != GOT_ERR_NOT_WORKTREE)
11087 if (error->code == GOT_ERR_NOT_WORKTREE)
11088 error = wrap_not_worktree_error(error,
11094 error = get_gitconfig_path(&gitconfig_path);
11097 error = got_repo_open(&repo,
11098 worktree ? got_worktree_get_repo_path(worktree) : cwd,
11099 gitconfig_path, pack_fds);
11103 if (worktree != NULL && !list_backups && !delete_backups) {
11104 error = worktree_has_logmsg_ref("rebase", worktree, repo);
11109 error = get_author(&committer, repo, worktree);
11110 if (error && error->code != GOT_ERR_COMMIT_NO_AUTHOR)
11113 error = apply_unveil(got_repo_get_path(repo), 0,
11114 worktree ? got_worktree_get_root_path(worktree) : NULL);
11118 if (list_backups || delete_backups) {
11119 error = process_backup_refs(
11120 GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
11121 argc == 1 ? argv[0] : NULL, delete_backups, repo);
11122 goto done; /* nothing else to do */
11125 error = got_worktree_histedit_in_progress(&histedit_in_progress,
11129 if (histedit_in_progress) {
11130 error = got_error(GOT_ERR_HISTEDIT_BUSY);
11134 error = got_worktree_merge_in_progress(&merge_in_progress,
11138 if (merge_in_progress) {
11139 error = got_error(GOT_ERR_MERGE_BUSY);
11143 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
11147 if (abort_rebase) {
11148 if (!rebase_in_progress) {
11149 error = got_error(GOT_ERR_NOT_REBASING);
11152 error = got_worktree_rebase_continue(&resume_commit_id,
11153 &new_base_branch, &tmp_branch, &branch, &fileindex,
11157 printf("Switching work tree to %s\n",
11158 got_ref_get_symref_target(new_base_branch));
11159 error = got_worktree_rebase_abort(worktree, fileindex, repo,
11160 new_base_branch, abort_progress, &upa);
11163 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
11164 print_merge_progress_stats(&upa);
11165 goto done; /* nothing else to do */
11168 if (continue_rebase) {
11169 if (!rebase_in_progress) {
11170 error = got_error(GOT_ERR_NOT_REBASING);
11173 error = got_worktree_rebase_continue(&resume_commit_id,
11174 &new_base_branch, &tmp_branch, &branch, &fileindex,
11179 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
11180 committer, resume_commit_id, repo);
11184 yca_id = got_object_id_dup(resume_commit_id);
11185 if (yca_id == NULL) {
11186 error = got_error_from_errno("got_object_id_dup");
11190 error = got_ref_open(&branch, repo, argv[0], 0);
11193 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
11194 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
11195 "will not rebase a branch which lives outside "
11196 "the \"refs/heads/\" reference namespace");
11201 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
11205 if (!continue_rebase) {
11206 struct got_object_id *base_commit_id;
11208 error = got_ref_open(&head_ref, repo,
11209 got_worktree_get_head_ref_name(worktree), 0);
11212 error = got_ref_resolve(&head_commit_id, repo, head_ref);
11215 base_commit_id = got_worktree_get_base_commit_id(worktree);
11216 if (got_object_id_cmp(base_commit_id, head_commit_id) != 0) {
11217 error = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
11221 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
11222 base_commit_id, branch_head_commit_id, 1, repo,
11223 check_cancelled, NULL);
11225 if (error->code == GOT_ERR_ANCESTRY) {
11226 error = got_error_msg(GOT_ERR_ANCESTRY,
11227 "specified branch shares no common "
11228 "ancestry with work tree's branch");
11233 error = check_same_branch(base_commit_id, branch, yca_id, repo);
11235 if (error->code != GOT_ERR_ANCESTRY)
11239 struct got_pathlist_head paths;
11240 printf("%s is already based on %s\n",
11241 got_ref_get_name(branch),
11242 got_worktree_get_head_ref_name(worktree));
11243 error = switch_head_ref(branch, branch_head_commit_id,
11247 error = got_worktree_set_base_commit_id(worktree, repo,
11248 branch_head_commit_id);
11251 TAILQ_INIT(&paths);
11252 error = got_pathlist_append(&paths, "", NULL);
11255 error = got_worktree_checkout_files(worktree,
11256 &paths, repo, update_progress, &upa,
11257 check_cancelled, NULL);
11258 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
11261 if (upa.did_something) {
11263 error = got_object_id_str(&id_str,
11264 branch_head_commit_id);
11267 printf("Updated to %s: %s\n",
11268 got_worktree_get_head_ref_name(worktree),
11272 printf("Already up-to-date\n");
11273 print_update_progress_stats(&upa);
11278 commit_id = branch_head_commit_id;
11279 error = got_object_open_as_commit(&commit, repo, commit_id);
11283 parent_ids = got_object_commit_get_parent_ids(commit);
11284 pid = STAILQ_FIRST(parent_ids);
11286 error = collect_commits(&commits, commit_id, &pid->id,
11287 yca_id, got_worktree_get_path_prefix(worktree),
11288 GOT_ERR_REBASE_PATH, repo);
11293 got_object_commit_close(commit);
11296 if (!continue_rebase) {
11297 error = got_worktree_rebase_prepare(&new_base_branch,
11298 &tmp_branch, &fileindex, worktree, branch, repo);
11303 if (STAILQ_EMPTY(&commits)) {
11304 if (continue_rebase) {
11305 error = rebase_complete(worktree, fileindex,
11306 branch, tmp_branch, repo, create_backup);
11309 /* Fast-forward the reference of the branch. */
11310 struct got_object_id *new_head_commit_id;
11312 error = got_ref_resolve(&new_head_commit_id, repo,
11316 error = got_object_id_str(&id_str, new_head_commit_id);
11319 printf("Forwarding %s to commit %s\n",
11320 got_ref_get_name(branch), id_str);
11322 error = got_ref_change_ref(branch,
11323 new_head_commit_id);
11326 /* No backup needed since objects did not change. */
11332 STAILQ_FOREACH(qid, &commits, entry) {
11334 commit_id = &qid->id;
11335 parent_id = pid ? &pid->id : yca_id;
11338 memset(&upa, 0, sizeof(upa));
11339 error = got_worktree_rebase_merge_files(&merged_paths,
11340 worktree, fileindex, parent_id, commit_id, repo,
11341 update_progress, &upa, check_cancelled, NULL);
11345 print_merge_progress_stats(&upa);
11346 if (upa.conflicts > 0 || upa.missing > 0 ||
11347 upa.not_deleted > 0 || upa.unversioned > 0) {
11348 if (upa.conflicts > 0) {
11349 error = show_rebase_merge_conflict(&qid->id,
11354 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
11358 error = rebase_commit(&merged_paths, worktree, fileindex,
11359 tmp_branch, committer, commit_id, repo);
11360 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
11365 if (upa.conflicts > 0 || upa.missing > 0 ||
11366 upa.not_deleted > 0 || upa.unversioned > 0) {
11367 error = got_worktree_rebase_postpone(worktree, fileindex);
11370 if (upa.conflicts > 0 && upa.missing == 0 &&
11371 upa.not_deleted == 0 && upa.unversioned == 0) {
11372 error = got_error_msg(GOT_ERR_CONFLICTS,
11373 "conflicts must be resolved before rebasing "
11375 } else if (upa.conflicts > 0) {
11376 error = got_error_msg(GOT_ERR_CONFLICTS,
11377 "conflicts must be resolved before rebasing "
11378 "can continue; changes destined for some "
11379 "files were not yet merged and should be "
11380 "merged manually if required before the "
11381 "rebase operation is continued");
11383 error = got_error_msg(GOT_ERR_CONFLICTS,
11384 "changes destined for some files were not "
11385 "yet merged and should be merged manually "
11386 "if required before the rebase operation "
11390 error = rebase_complete(worktree, fileindex, branch,
11391 tmp_branch, repo, create_backup);
11395 free(gitconfig_path);
11396 got_object_id_queue_free(&commits);
11397 free(branch_head_commit_id);
11398 free(resume_commit_id);
11399 free(head_commit_id);
11402 got_object_commit_close(commit);
11404 got_ref_close(branch);
11405 if (new_base_branch)
11406 got_ref_close(new_base_branch);
11408 got_ref_close(tmp_branch);
11410 got_ref_close(head_ref);
11412 got_worktree_close(worktree);
11414 const struct got_error *close_err = got_repo_close(repo);
11419 const struct got_error *pack_err =
11420 got_repo_pack_fds_close(pack_fds);
11428 usage_histedit(void)
11430 fprintf(stderr, "usage: %s histedit [-acdeflmX] [-F histedit-script] "
11431 "[branch]\n", getprogname());
11435 #define GOT_HISTEDIT_PICK 'p'
11436 #define GOT_HISTEDIT_EDIT 'e'
11437 #define GOT_HISTEDIT_FOLD 'f'
11438 #define GOT_HISTEDIT_DROP 'd'
11439 #define GOT_HISTEDIT_MESG 'm'
11441 static const struct got_histedit_cmd {
11442 unsigned char code;
11445 } got_histedit_cmds[] = {
11446 { GOT_HISTEDIT_PICK, "pick", "use commit" },
11447 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
11448 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
11450 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
11451 { GOT_HISTEDIT_MESG, "mesg",
11452 "single-line log message for commit above (open editor if empty)" },
11455 struct got_histedit_list_entry {
11456 TAILQ_ENTRY(got_histedit_list_entry) entry;
11457 struct got_object_id *commit_id;
11458 const struct got_histedit_cmd *cmd;
11461 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
11463 static const struct got_error *
11464 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
11465 FILE *f, struct got_repository *repo)
11467 const struct got_error *err = NULL;
11468 char *logmsg = NULL, *id_str = NULL;
11469 struct got_commit_object *commit = NULL;
11472 err = got_object_open_as_commit(&commit, repo, commit_id);
11476 err = get_short_logmsg(&logmsg, 34, commit);
11480 err = got_object_id_str(&id_str, commit_id);
11484 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
11486 err = got_ferror(f, GOT_ERR_IO);
11489 got_object_commit_close(commit);
11495 static const struct got_error *
11496 histedit_write_commit_list(struct got_object_id_queue *commits,
11497 FILE *f, int edit_logmsg_only, int fold_only, int drop_only,
11498 int edit_only, struct got_repository *repo)
11500 const struct got_error *err = NULL;
11501 struct got_object_qid *qid;
11502 const char *histedit_cmd = NULL;
11504 if (STAILQ_EMPTY(commits))
11505 return got_error(GOT_ERR_EMPTY_HISTEDIT);
11507 STAILQ_FOREACH(qid, commits, entry) {
11508 histedit_cmd = got_histedit_cmds[0].name;
11510 histedit_cmd = "drop";
11511 else if (edit_only)
11512 histedit_cmd = "edit";
11513 else if (fold_only && STAILQ_NEXT(qid, entry) != NULL)
11514 histedit_cmd = "fold";
11515 err = histedit_write_commit(&qid->id, histedit_cmd, f, repo);
11518 if (edit_logmsg_only) {
11519 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
11521 err = got_ferror(f, GOT_ERR_IO);
11530 static const struct got_error *
11531 write_cmd_list(FILE *f, const char *branch_name,
11532 struct got_object_id_queue *commits)
11534 const struct got_error *err = NULL;
11538 struct got_object_qid *qid;
11540 qid = STAILQ_FIRST(commits);
11541 err = got_object_id_str(&id_str, &qid->id);
11546 "# Editing the history of branch '%s' starting at\n"
11548 "# Commits will be processed in order from top to "
11549 "bottom of this file.\n", branch_name, id_str);
11551 err = got_ferror(f, GOT_ERR_IO);
11555 n = fprintf(f, "# Available histedit commands:\n");
11557 err = got_ferror(f, GOT_ERR_IO);
11561 for (i = 0; i < nitems(got_histedit_cmds); i++) {
11562 const struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
11563 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
11566 err = got_ferror(f, GOT_ERR_IO);
11575 static const struct got_error *
11576 histedit_syntax_error(int lineno)
11578 static char msg[42];
11581 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
11583 if (ret < 0 || (size_t)ret >= sizeof(msg))
11584 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
11586 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
11589 static const struct got_error *
11590 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
11591 char *logmsg, struct got_repository *repo)
11593 const struct got_error *err;
11594 struct got_commit_object *folded_commit = NULL;
11595 char *id_str, *folded_logmsg = NULL;
11597 err = got_object_id_str(&id_str, hle->commit_id);
11601 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
11605 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
11608 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
11609 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
11610 folded_logmsg) == -1) {
11611 err = got_error_from_errno("asprintf");
11615 got_object_commit_close(folded_commit);
11617 free(folded_logmsg);
11621 static struct got_histedit_list_entry *
11622 get_folded_commits(struct got_histedit_list_entry *hle)
11624 struct got_histedit_list_entry *prev, *folded = NULL;
11626 prev = TAILQ_PREV(hle, got_histedit_list, entry);
11627 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
11628 prev->cmd->code == GOT_HISTEDIT_DROP)) {
11629 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
11631 prev = TAILQ_PREV(prev, got_histedit_list, entry);
11637 static const struct got_error *
11638 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
11639 struct got_repository *repo)
11641 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
11642 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
11643 const struct got_error *err = NULL;
11644 struct got_commit_object *commit = NULL;
11647 struct got_histedit_list_entry *folded = NULL;
11649 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
11653 folded = get_folded_commits(hle);
11655 while (folded != hle) {
11656 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
11657 folded = TAILQ_NEXT(folded, entry);
11660 err = append_folded_commit_msg(&new_msg, folded,
11666 folded = TAILQ_NEXT(folded, entry);
11670 err = got_object_id_str(&id_str, hle->commit_id);
11673 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
11676 logmsg_len = asprintf(&new_msg,
11677 "%s\n# original log message of commit %s: %s",
11678 logmsg ? logmsg : "", id_str, orig_logmsg);
11679 if (logmsg_len == -1) {
11680 err = got_error_from_errno("asprintf");
11686 err = got_object_id_str(&id_str, hle->commit_id);
11690 err = got_opentemp_named_fd(&logmsg_path, &fd,
11691 GOT_TMPDIR_STR "/got-logmsg", "");
11695 write(fd, logmsg, logmsg_len);
11698 err = get_editor(&editor);
11702 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg,
11705 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
11708 hle->logmsg = strdup(new_msg);
11709 if (hle->logmsg == NULL)
11710 err = got_error_from_errno("strdup");
11713 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
11714 err = got_error_from_errno2("unlink", logmsg_path);
11720 got_object_commit_close(commit);
11724 static const struct got_error *
11725 histedit_parse_list(struct got_histedit_list *histedit_cmds,
11726 FILE *f, struct got_repository *repo)
11728 const struct got_error *err = NULL;
11729 char *line = NULL, *p, *end;
11732 int lineno = 0, lastcmd = -1;
11733 const struct got_histedit_cmd *cmd;
11734 struct got_object_id *commit_id = NULL;
11735 struct got_histedit_list_entry *hle = NULL;
11738 len = getline(&line, &size, f);
11740 const struct got_error *getline_err;
11743 getline_err = got_error_from_errno("getline");
11744 err = got_ferror(f, getline_err->code);
11749 while (isspace((unsigned char)p[0]))
11751 if (p[0] == '#' || p[0] == '\0') {
11757 for (i = 0; i < nitems(got_histedit_cmds); i++) {
11758 cmd = &got_histedit_cmds[i];
11759 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
11760 isspace((unsigned char)p[strlen(cmd->name)])) {
11761 p += strlen(cmd->name);
11764 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
11769 if (i == nitems(got_histedit_cmds)) {
11770 err = histedit_syntax_error(lineno);
11773 while (isspace((unsigned char)p[0]))
11775 if (cmd->code == GOT_HISTEDIT_MESG) {
11776 if (lastcmd != GOT_HISTEDIT_PICK &&
11777 lastcmd != GOT_HISTEDIT_EDIT) {
11778 err = got_error(GOT_ERR_HISTEDIT_CMD);
11781 if (p[0] == '\0') {
11782 err = histedit_edit_logmsg(hle, repo);
11786 hle->logmsg = strdup(p);
11787 if (hle->logmsg == NULL) {
11788 err = got_error_from_errno("strdup");
11794 lastcmd = cmd->code;
11798 while (end[0] && !isspace((unsigned char)end[0]))
11802 err = got_object_resolve_id_str(&commit_id, repo, p);
11804 /* override error code */
11805 err = histedit_syntax_error(lineno);
11809 hle = malloc(sizeof(*hle));
11811 err = got_error_from_errno("malloc");
11815 hle->commit_id = commit_id;
11816 hle->logmsg = NULL;
11820 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
11821 lastcmd = cmd->code;
11829 static const struct got_error *
11830 histedit_check_script(struct got_histedit_list *histedit_cmds,
11831 struct got_object_id_queue *commits, struct got_repository *repo)
11833 const struct got_error *err = NULL;
11834 struct got_object_qid *qid;
11835 struct got_histedit_list_entry *hle;
11836 static char msg[92];
11839 if (TAILQ_EMPTY(histedit_cmds))
11840 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
11841 "histedit script contains no commands");
11842 if (STAILQ_EMPTY(commits))
11843 return got_error(GOT_ERR_EMPTY_HISTEDIT);
11845 TAILQ_FOREACH(hle, histedit_cmds, entry) {
11846 struct got_histedit_list_entry *hle2;
11847 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
11850 if (got_object_id_cmp(hle->commit_id,
11851 hle2->commit_id) != 0)
11853 err = got_object_id_str(&id_str, hle->commit_id);
11856 snprintf(msg, sizeof(msg), "commit %s is listed "
11857 "more than once in histedit script", id_str);
11859 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
11863 STAILQ_FOREACH(qid, commits, entry) {
11864 TAILQ_FOREACH(hle, histedit_cmds, entry) {
11865 if (got_object_id_cmp(&qid->id, hle->commit_id) == 0)
11869 err = got_object_id_str(&id_str, &qid->id);
11872 snprintf(msg, sizeof(msg),
11873 "commit %s missing from histedit script", id_str);
11875 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
11879 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
11880 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
11881 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
11882 "last commit in histedit script cannot be folded");
11887 static const struct got_error *
11888 histedit_run_editor(struct got_histedit_list *histedit_cmds,
11889 const char *path, struct got_object_id_queue *commits,
11890 struct got_repository *repo)
11892 const struct got_error *err = NULL;
11896 err = get_editor(&editor);
11900 if (spawn_editor(editor, path) == -1) {
11901 err = got_error_from_errno("failed spawning editor");
11905 f = fopen(path, "re");
11907 err = got_error_from_errno("fopen");
11910 err = histedit_parse_list(histedit_cmds, f, repo);
11914 err = histedit_check_script(histedit_cmds, commits, repo);
11916 if (f && fclose(f) == EOF && err == NULL)
11917 err = got_error_from_errno("fclose");
11922 static const struct got_error *
11923 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
11924 struct got_object_id_queue *, const char *, const char *,
11925 struct got_repository *);
11927 static const struct got_error *
11928 histedit_edit_script(struct got_histedit_list *histedit_cmds,
11929 struct got_object_id_queue *commits, const char *branch_name,
11930 int edit_logmsg_only, int fold_only, int drop_only, int edit_only,
11931 struct got_repository *repo)
11933 const struct got_error *err;
11937 err = got_opentemp_named(&path, &f, "got-histedit", "");
11941 err = write_cmd_list(f, branch_name, commits);
11945 err = histedit_write_commit_list(commits, f, edit_logmsg_only,
11946 fold_only, drop_only, edit_only, repo);
11950 if (drop_only || edit_logmsg_only || fold_only || edit_only) {
11952 err = histedit_parse_list(histedit_cmds, f, repo);
11954 if (fclose(f) == EOF) {
11955 err = got_error_from_errno("fclose");
11959 err = histedit_run_editor(histedit_cmds, path, commits, repo);
11961 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
11962 err->code != GOT_ERR_HISTEDIT_CMD)
11964 err = histedit_edit_list_retry(histedit_cmds, err,
11965 commits, path, branch_name, repo);
11969 if (f && fclose(f) == EOF && err == NULL)
11970 err = got_error_from_errno("fclose");
11971 if (path && unlink(path) != 0 && err == NULL)
11972 err = got_error_from_errno2("unlink", path);
11977 static const struct got_error *
11978 histedit_save_list(struct got_histedit_list *histedit_cmds,
11979 struct got_worktree *worktree, struct got_repository *repo)
11981 const struct got_error *err = NULL;
11984 struct got_histedit_list_entry *hle;
11985 struct got_commit_object *commit = NULL;
11987 err = got_worktree_get_histedit_script_path(&path, worktree);
11991 f = fopen(path, "we");
11993 err = got_error_from_errno2("fopen", path);
11996 TAILQ_FOREACH(hle, histedit_cmds, entry) {
11997 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
12003 int n = fprintf(f, "%c %s\n",
12004 GOT_HISTEDIT_MESG, hle->logmsg);
12006 err = got_ferror(f, GOT_ERR_IO);
12012 if (f && fclose(f) == EOF && err == NULL)
12013 err = got_error_from_errno("fclose");
12016 got_object_commit_close(commit);
12021 histedit_free_list(struct got_histedit_list *histedit_cmds)
12023 struct got_histedit_list_entry *hle;
12025 while ((hle = TAILQ_FIRST(histedit_cmds))) {
12026 TAILQ_REMOVE(histedit_cmds, hle, entry);
12031 static const struct got_error *
12032 histedit_load_list(struct got_histedit_list *histedit_cmds,
12033 const char *path, struct got_repository *repo)
12035 const struct got_error *err = NULL;
12038 f = fopen(path, "re");
12040 err = got_error_from_errno2("fopen", path);
12044 err = histedit_parse_list(histedit_cmds, f, repo);
12046 if (f && fclose(f) == EOF && err == NULL)
12047 err = got_error_from_errno("fclose");
12051 static const struct got_error *
12052 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
12053 const struct got_error *edit_err, struct got_object_id_queue *commits,
12054 const char *path, const char *branch_name, struct got_repository *repo)
12056 const struct got_error *err = NULL, *prev_err = edit_err;
12059 while (resp != 'c' && resp != 'r' && resp != 'a') {
12060 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
12061 "or (a)bort: ", getprogname(), prev_err->msg);
12066 histedit_free_list(histedit_cmds);
12067 err = histedit_run_editor(histedit_cmds, path, commits,
12070 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
12071 err->code != GOT_ERR_HISTEDIT_CMD)
12078 } else if (resp == 'r') {
12079 histedit_free_list(histedit_cmds);
12080 err = histedit_edit_script(histedit_cmds,
12081 commits, branch_name, 0, 0, 0, 0, repo);
12083 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
12084 err->code != GOT_ERR_HISTEDIT_CMD)
12091 } else if (resp == 'a') {
12092 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
12095 printf("invalid response '%c'\n", resp);
12101 static const struct got_error *
12102 histedit_complete(struct got_worktree *worktree,
12103 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
12104 struct got_reference *branch, struct got_repository *repo)
12106 printf("Switching work tree to %s\n",
12107 got_ref_get_symref_target(branch));
12108 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
12112 static const struct got_error *
12113 show_histedit_progress(struct got_commit_object *commit,
12114 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
12116 const struct got_error *err;
12117 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
12119 err = got_object_id_str(&old_id_str, hle->commit_id);
12124 err = got_object_id_str(&new_id_str, new_id);
12129 old_id_str[12] = '\0';
12131 new_id_str[12] = '\0';
12134 logmsg = strdup(hle->logmsg);
12135 if (logmsg == NULL) {
12136 err = got_error_from_errno("strdup");
12139 trim_logmsg(logmsg, 42);
12141 err = get_short_logmsg(&logmsg, 42, commit);
12146 switch (hle->cmd->code) {
12147 case GOT_HISTEDIT_PICK:
12148 case GOT_HISTEDIT_EDIT:
12149 printf("%s -> %s: %s\n", old_id_str,
12150 new_id_str ? new_id_str : "no-op change", logmsg);
12152 case GOT_HISTEDIT_DROP:
12153 case GOT_HISTEDIT_FOLD:
12154 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
12166 static const struct got_error *
12167 histedit_commit(struct got_pathlist_head *merged_paths,
12168 struct got_worktree *worktree, struct got_fileindex *fileindex,
12169 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
12170 const char *committer, struct got_repository *repo)
12172 const struct got_error *err;
12173 struct got_commit_object *commit;
12174 struct got_object_id *new_commit_id;
12176 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
12177 && hle->logmsg == NULL) {
12178 err = histedit_edit_logmsg(hle, repo);
12183 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
12187 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
12188 worktree, fileindex, tmp_branch, committer, commit, hle->commit_id,
12189 hle->logmsg, repo);
12191 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
12193 err = show_histedit_progress(commit, hle, NULL);
12195 err = show_histedit_progress(commit, hle, new_commit_id);
12196 free(new_commit_id);
12199 got_object_commit_close(commit);
12203 static const struct got_error *
12204 histedit_skip_commit(struct got_histedit_list_entry *hle,
12205 struct got_worktree *worktree, struct got_repository *repo)
12207 const struct got_error *error;
12208 struct got_commit_object *commit;
12210 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
12215 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
12219 error = show_histedit_progress(commit, hle, NULL);
12220 got_object_commit_close(commit);
12224 static const struct got_error *
12225 check_local_changes(void *arg, unsigned char status,
12226 unsigned char staged_status, const char *path,
12227 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
12228 struct got_object_id *commit_id, int dirfd, const char *de_name)
12230 int *have_local_changes = arg;
12233 case GOT_STATUS_ADD:
12234 case GOT_STATUS_DELETE:
12235 case GOT_STATUS_MODIFY:
12236 case GOT_STATUS_CONFLICT:
12237 *have_local_changes = 1;
12238 return got_error(GOT_ERR_CANCELLED);
12243 switch (staged_status) {
12244 case GOT_STATUS_ADD:
12245 case GOT_STATUS_DELETE:
12246 case GOT_STATUS_MODIFY:
12247 *have_local_changes = 1;
12248 return got_error(GOT_ERR_CANCELLED);
12256 static const struct got_error *
12257 cmd_histedit(int argc, char *argv[])
12259 const struct got_error *error = NULL;
12260 struct got_worktree *worktree = NULL;
12261 struct got_fileindex *fileindex = NULL;
12262 struct got_repository *repo = NULL;
12263 char *cwd = NULL, *committer = NULL, *gitconfig_path = NULL;
12264 struct got_reference *branch = NULL;
12265 struct got_reference *tmp_branch = NULL;
12266 struct got_object_id *resume_commit_id = NULL;
12267 struct got_object_id *base_commit_id = NULL;
12268 struct got_object_id *head_commit_id = NULL;
12269 struct got_commit_object *commit = NULL;
12270 int ch, rebase_in_progress = 0, merge_in_progress = 0;
12271 struct got_update_progress_arg upa;
12272 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
12273 int drop_only = 0, edit_logmsg_only = 0, fold_only = 0, edit_only = 0;
12274 int list_backups = 0, delete_backups = 0;
12275 const char *edit_script_path = NULL;
12276 struct got_object_id_queue commits;
12277 struct got_pathlist_head merged_paths;
12278 const struct got_object_id_queue *parent_ids;
12279 struct got_object_qid *pid;
12280 struct got_histedit_list histedit_cmds;
12281 struct got_histedit_list_entry *hle;
12282 int *pack_fds = NULL;
12284 STAILQ_INIT(&commits);
12285 TAILQ_INIT(&histedit_cmds);
12286 TAILQ_INIT(&merged_paths);
12287 memset(&upa, 0, sizeof(upa));
12290 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
12291 "unveil", NULL) == -1)
12295 while ((ch = getopt(argc, argv, "acdeF:flmX")) != -1) {
12310 edit_script_path = optarg;
12319 edit_logmsg_only = 1;
12322 delete_backups = 1;
12333 if (abort_edit && continue_edit)
12334 option_conflict('a', 'c');
12335 if (edit_script_path && edit_logmsg_only)
12336 option_conflict('F', 'm');
12337 if (abort_edit && edit_logmsg_only)
12338 option_conflict('a', 'm');
12339 if (continue_edit && edit_logmsg_only)
12340 option_conflict('c', 'm');
12341 if (abort_edit && fold_only)
12342 option_conflict('a', 'f');
12343 if (continue_edit && fold_only)
12344 option_conflict('c', 'f');
12345 if (fold_only && edit_logmsg_only)
12346 option_conflict('f', 'm');
12347 if (edit_script_path && fold_only)
12348 option_conflict('F', 'f');
12349 if (abort_edit && edit_only)
12350 option_conflict('a', 'e');
12351 if (continue_edit && edit_only)
12352 option_conflict('c', 'e');
12353 if (edit_only && edit_logmsg_only)
12354 option_conflict('e', 'm');
12355 if (edit_script_path && edit_only)
12356 option_conflict('F', 'e');
12357 if (fold_only && edit_only)
12358 option_conflict('f', 'e');
12359 if (drop_only && abort_edit)
12360 option_conflict('d', 'a');
12361 if (drop_only && continue_edit)
12362 option_conflict('d', 'c');
12363 if (drop_only && edit_logmsg_only)
12364 option_conflict('d', 'm');
12365 if (drop_only && edit_only)
12366 option_conflict('d', 'e');
12367 if (drop_only && edit_script_path)
12368 option_conflict('d', 'F');
12369 if (drop_only && fold_only)
12370 option_conflict('d', 'f');
12371 if (list_backups) {
12373 option_conflict('l', 'a');
12375 option_conflict('l', 'c');
12376 if (edit_script_path)
12377 option_conflict('l', 'F');
12378 if (edit_logmsg_only)
12379 option_conflict('l', 'm');
12381 option_conflict('l', 'd');
12383 option_conflict('l', 'f');
12385 option_conflict('l', 'e');
12386 if (delete_backups)
12387 option_conflict('l', 'X');
12388 if (argc != 0 && argc != 1)
12390 } else if (delete_backups) {
12392 option_conflict('X', 'a');
12394 option_conflict('X', 'c');
12396 option_conflict('X', 'd');
12397 if (edit_script_path)
12398 option_conflict('X', 'F');
12399 if (edit_logmsg_only)
12400 option_conflict('X', 'm');
12402 option_conflict('X', 'f');
12404 option_conflict('X', 'e');
12406 option_conflict('X', 'l');
12407 if (argc != 0 && argc != 1)
12409 } else if (argc != 0)
12413 * This command cannot apply unveil(2) in all cases because the
12414 * user may choose to run an editor to edit the histedit script
12415 * and to edit individual commit log messages.
12416 * unveil(2) traverses exec(2); if an editor is used we have to
12417 * apply unveil after edit script and log messages have been written.
12418 * XXX TODO: Make use of unveil(2) where possible.
12421 cwd = getcwd(NULL, 0);
12423 error = got_error_from_errno("getcwd");
12427 error = got_repo_pack_fds_open(&pack_fds);
12431 error = got_worktree_open(&worktree, cwd);
12433 if (list_backups || delete_backups) {
12434 if (error->code != GOT_ERR_NOT_WORKTREE)
12437 if (error->code == GOT_ERR_NOT_WORKTREE)
12438 error = wrap_not_worktree_error(error,
12444 if (list_backups || delete_backups) {
12445 error = got_repo_open(&repo,
12446 worktree ? got_worktree_get_repo_path(worktree) : cwd,
12450 error = apply_unveil(got_repo_get_path(repo), 0,
12451 worktree ? got_worktree_get_root_path(worktree) : NULL);
12454 error = process_backup_refs(
12455 GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
12456 argc == 1 ? argv[0] : NULL, delete_backups, repo);
12457 goto done; /* nothing else to do */
12460 error = get_gitconfig_path(&gitconfig_path);
12463 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
12464 gitconfig_path, pack_fds);
12468 if (worktree != NULL && !list_backups && !delete_backups) {
12469 error = worktree_has_logmsg_ref("histedit", worktree, repo);
12474 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
12477 if (rebase_in_progress) {
12478 error = got_error(GOT_ERR_REBASING);
12482 error = got_worktree_merge_in_progress(&merge_in_progress, worktree,
12486 if (merge_in_progress) {
12487 error = got_error(GOT_ERR_MERGE_BUSY);
12491 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
12495 if (edit_in_progress && edit_logmsg_only) {
12496 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
12497 "histedit operation is in progress in this "
12498 "work tree and must be continued or aborted "
12499 "before the -m option can be used");
12502 if (edit_in_progress && drop_only) {
12503 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
12504 "histedit operation is in progress in this "
12505 "work tree and must be continued or aborted "
12506 "before the -d option can be used");
12509 if (edit_in_progress && fold_only) {
12510 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
12511 "histedit operation is in progress in this "
12512 "work tree and must be continued or aborted "
12513 "before the -f option can be used");
12516 if (edit_in_progress && edit_only) {
12517 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
12518 "histedit operation is in progress in this "
12519 "work tree and must be continued or aborted "
12520 "before the -e option can be used");
12524 if (edit_in_progress && abort_edit) {
12525 error = got_worktree_histedit_continue(&resume_commit_id,
12526 &tmp_branch, &branch, &base_commit_id, &fileindex,
12530 printf("Switching work tree to %s\n",
12531 got_ref_get_symref_target(branch));
12532 error = got_worktree_histedit_abort(worktree, fileindex, repo,
12533 branch, base_commit_id, abort_progress, &upa);
12536 printf("Histedit of %s aborted\n",
12537 got_ref_get_symref_target(branch));
12538 print_merge_progress_stats(&upa);
12539 goto done; /* nothing else to do */
12540 } else if (abort_edit) {
12541 error = got_error(GOT_ERR_NOT_HISTEDIT);
12545 error = get_author(&committer, repo, worktree);
12549 if (continue_edit) {
12552 if (!edit_in_progress) {
12553 error = got_error(GOT_ERR_NOT_HISTEDIT);
12557 error = got_worktree_get_histedit_script_path(&path, worktree);
12561 error = histedit_load_list(&histedit_cmds, path, repo);
12566 error = got_worktree_histedit_continue(&resume_commit_id,
12567 &tmp_branch, &branch, &base_commit_id, &fileindex,
12572 error = got_ref_resolve(&head_commit_id, repo, branch);
12576 error = got_object_open_as_commit(&commit, repo,
12580 parent_ids = got_object_commit_get_parent_ids(commit);
12581 pid = STAILQ_FIRST(parent_ids);
12583 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
12586 error = collect_commits(&commits, head_commit_id, &pid->id,
12587 base_commit_id, got_worktree_get_path_prefix(worktree),
12588 GOT_ERR_HISTEDIT_PATH, repo);
12589 got_object_commit_close(commit);
12594 if (edit_in_progress) {
12595 error = got_error(GOT_ERR_HISTEDIT_BUSY);
12599 error = got_ref_open(&branch, repo,
12600 got_worktree_get_head_ref_name(worktree), 0);
12604 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
12605 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
12606 "will not edit commit history of a branch outside "
12607 "the \"refs/heads/\" reference namespace");
12611 error = got_ref_resolve(&head_commit_id, repo, branch);
12612 got_ref_close(branch);
12617 error = got_object_open_as_commit(&commit, repo,
12621 parent_ids = got_object_commit_get_parent_ids(commit);
12622 pid = STAILQ_FIRST(parent_ids);
12624 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
12627 error = collect_commits(&commits, head_commit_id, &pid->id,
12628 got_worktree_get_base_commit_id(worktree),
12629 got_worktree_get_path_prefix(worktree),
12630 GOT_ERR_HISTEDIT_PATH, repo);
12631 got_object_commit_close(commit);
12636 if (STAILQ_EMPTY(&commits)) {
12637 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
12641 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
12642 &base_commit_id, &fileindex, worktree, repo);
12646 if (edit_script_path) {
12647 error = histedit_load_list(&histedit_cmds,
12648 edit_script_path, repo);
12650 got_worktree_histedit_abort(worktree, fileindex,
12651 repo, branch, base_commit_id,
12652 abort_progress, &upa);
12653 print_merge_progress_stats(&upa);
12657 const char *branch_name;
12658 branch_name = got_ref_get_symref_target(branch);
12659 if (strncmp(branch_name, "refs/heads/", 11) == 0)
12661 error = histedit_edit_script(&histedit_cmds, &commits,
12662 branch_name, edit_logmsg_only, fold_only,
12663 drop_only, edit_only, repo);
12665 got_worktree_histedit_abort(worktree, fileindex,
12666 repo, branch, base_commit_id,
12667 abort_progress, &upa);
12668 print_merge_progress_stats(&upa);
12674 error = histedit_save_list(&histedit_cmds, worktree,
12677 got_worktree_histedit_abort(worktree, fileindex,
12678 repo, branch, base_commit_id,
12679 abort_progress, &upa);
12680 print_merge_progress_stats(&upa);
12686 error = histedit_check_script(&histedit_cmds, &commits, repo);
12690 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
12691 if (resume_commit_id) {
12692 if (got_object_id_cmp(hle->commit_id,
12693 resume_commit_id) != 0)
12696 resume_commit_id = NULL;
12697 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
12698 hle->cmd->code == GOT_HISTEDIT_FOLD) {
12699 error = histedit_skip_commit(hle, worktree,
12704 struct got_pathlist_head paths;
12705 int have_changes = 0;
12707 TAILQ_INIT(&paths);
12708 error = got_pathlist_append(&paths, "", NULL);
12711 error = got_worktree_status(worktree, &paths,
12712 repo, 0, check_local_changes, &have_changes,
12713 check_cancelled, NULL);
12714 got_pathlist_free(&paths,
12715 GOT_PATHLIST_FREE_NONE);
12717 if (error->code != GOT_ERR_CANCELLED)
12719 if (sigint_received || sigpipe_received)
12722 if (have_changes) {
12723 error = histedit_commit(NULL, worktree,
12724 fileindex, tmp_branch, hle,
12729 error = got_object_open_as_commit(
12730 &commit, repo, hle->commit_id);
12733 error = show_histedit_progress(commit,
12735 got_object_commit_close(commit);
12744 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
12745 error = histedit_skip_commit(hle, worktree, repo);
12751 error = got_object_open_as_commit(&commit, repo,
12755 parent_ids = got_object_commit_get_parent_ids(commit);
12756 pid = STAILQ_FIRST(parent_ids);
12758 error = got_worktree_histedit_merge_files(&merged_paths,
12759 worktree, fileindex, &pid->id, hle->commit_id, repo,
12760 update_progress, &upa, check_cancelled, NULL);
12763 got_object_commit_close(commit);
12766 print_merge_progress_stats(&upa);
12767 if (upa.conflicts > 0 || upa.missing > 0 ||
12768 upa.not_deleted > 0 || upa.unversioned > 0) {
12769 if (upa.conflicts > 0) {
12770 error = show_rebase_merge_conflict(
12771 hle->commit_id, repo);
12775 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
12779 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
12781 error = got_object_id_str(&id_str, hle->commit_id);
12784 printf("Stopping histedit for amending commit %s\n",
12787 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
12788 error = got_worktree_histedit_postpone(worktree,
12793 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
12794 error = histedit_skip_commit(hle, worktree, repo);
12800 error = histedit_commit(&merged_paths, worktree, fileindex,
12801 tmp_branch, hle, committer, repo);
12802 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
12807 if (upa.conflicts > 0 || upa.missing > 0 ||
12808 upa.not_deleted > 0 || upa.unversioned > 0) {
12809 error = got_worktree_histedit_postpone(worktree, fileindex);
12812 if (upa.conflicts > 0 && upa.missing == 0 &&
12813 upa.not_deleted == 0 && upa.unversioned == 0) {
12814 error = got_error_msg(GOT_ERR_CONFLICTS,
12815 "conflicts must be resolved before histedit "
12817 } else if (upa.conflicts > 0) {
12818 error = got_error_msg(GOT_ERR_CONFLICTS,
12819 "conflicts must be resolved before histedit "
12820 "can continue; changes destined for some "
12821 "files were not yet merged and should be "
12822 "merged manually if required before the "
12823 "histedit operation is continued");
12825 error = got_error_msg(GOT_ERR_CONFLICTS,
12826 "changes destined for some files were not "
12827 "yet merged and should be merged manually "
12828 "if required before the histedit operation "
12832 error = histedit_complete(worktree, fileindex, tmp_branch,
12837 free(gitconfig_path);
12838 got_object_id_queue_free(&commits);
12839 histedit_free_list(&histedit_cmds);
12840 free(head_commit_id);
12841 free(base_commit_id);
12842 free(resume_commit_id);
12844 got_object_commit_close(commit);
12846 got_ref_close(branch);
12848 got_ref_close(tmp_branch);
12850 got_worktree_close(worktree);
12852 const struct got_error *close_err = got_repo_close(repo);
12857 const struct got_error *pack_err =
12858 got_repo_pack_fds_close(pack_fds);
12866 usage_integrate(void)
12868 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
12872 static const struct got_error *
12873 cmd_integrate(int argc, char *argv[])
12875 const struct got_error *error = NULL;
12876 struct got_repository *repo = NULL;
12877 struct got_worktree *worktree = NULL;
12878 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
12879 const char *branch_arg = NULL;
12880 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
12881 struct got_fileindex *fileindex = NULL;
12882 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
12884 struct got_update_progress_arg upa;
12885 int *pack_fds = NULL;
12888 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
12889 "unveil", NULL) == -1)
12893 while ((ch = getopt(argc, argv, "")) != -1) {
12906 branch_arg = argv[0];
12908 cwd = getcwd(NULL, 0);
12910 error = got_error_from_errno("getcwd");
12914 error = got_repo_pack_fds_open(&pack_fds);
12918 error = got_worktree_open(&worktree, cwd);
12920 if (error->code == GOT_ERR_NOT_WORKTREE)
12921 error = wrap_not_worktree_error(error, "integrate",
12926 error = check_rebase_or_histedit_in_progress(worktree);
12930 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
12935 error = apply_unveil(got_repo_get_path(repo), 0,
12936 got_worktree_get_root_path(worktree));
12940 error = check_merge_in_progress(worktree, repo);
12944 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
12945 error = got_error_from_errno("asprintf");
12949 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
12950 &base_branch_ref, worktree, refname, repo);
12954 refname = strdup(got_ref_get_name(branch_ref));
12955 if (refname == NULL) {
12956 error = got_error_from_errno("strdup");
12957 got_worktree_integrate_abort(worktree, fileindex, repo,
12958 branch_ref, base_branch_ref);
12961 base_refname = strdup(got_ref_get_name(base_branch_ref));
12962 if (base_refname == NULL) {
12963 error = got_error_from_errno("strdup");
12964 got_worktree_integrate_abort(worktree, fileindex, repo,
12965 branch_ref, base_branch_ref);
12968 if (strncmp(base_refname, "refs/heads/", 11) != 0) {
12969 error = got_error(GOT_ERR_INTEGRATE_BRANCH);
12970 got_worktree_integrate_abort(worktree, fileindex, repo,
12971 branch_ref, base_branch_ref);
12975 error = got_ref_resolve(&commit_id, repo, branch_ref);
12979 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
12983 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
12984 error = got_error_msg(GOT_ERR_SAME_BRANCH,
12985 "specified branch has already been integrated");
12986 got_worktree_integrate_abort(worktree, fileindex, repo,
12987 branch_ref, base_branch_ref);
12991 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
12993 if (error->code == GOT_ERR_ANCESTRY)
12994 error = got_error(GOT_ERR_REBASE_REQUIRED);
12995 got_worktree_integrate_abort(worktree, fileindex, repo,
12996 branch_ref, base_branch_ref);
13000 memset(&upa, 0, sizeof(upa));
13001 error = got_worktree_integrate_continue(worktree, fileindex, repo,
13002 branch_ref, base_branch_ref, update_progress, &upa,
13003 check_cancelled, NULL);
13007 printf("Integrated %s into %s\n", refname, base_refname);
13008 print_update_progress_stats(&upa);
13011 const struct got_error *close_err = got_repo_close(repo);
13016 got_worktree_close(worktree);
13018 const struct got_error *pack_err =
13019 got_repo_pack_fds_close(pack_fds);
13024 free(base_commit_id);
13027 free(base_refname);
13034 fprintf(stderr, "usage: %s merge [-acn] [branch]\n", getprogname());
13038 static const struct got_error *
13039 cmd_merge(int argc, char *argv[])
13041 const struct got_error *error = NULL;
13042 struct got_worktree *worktree = NULL;
13043 struct got_repository *repo = NULL;
13044 struct got_fileindex *fileindex = NULL;
13045 char *cwd = NULL, *id_str = NULL, *author = NULL;
13046 struct got_reference *branch = NULL, *wt_branch = NULL;
13047 struct got_object_id *branch_tip = NULL, *yca_id = NULL;
13048 struct got_object_id *wt_branch_tip = NULL;
13049 int ch, merge_in_progress = 0, abort_merge = 0, continue_merge = 0;
13050 int interrupt_merge = 0;
13051 struct got_update_progress_arg upa;
13052 struct got_object_id *merge_commit_id = NULL;
13053 char *branch_name = NULL;
13054 int *pack_fds = NULL;
13056 memset(&upa, 0, sizeof(upa));
13059 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
13060 "unveil", NULL) == -1)
13064 while ((ch = getopt(argc, argv, "acn")) != -1) {
13070 continue_merge = 1;
13073 interrupt_merge = 1;
13084 if (abort_merge && continue_merge)
13085 option_conflict('a', 'c');
13086 if (abort_merge || continue_merge) {
13089 } else if (argc != 1)
13092 cwd = getcwd(NULL, 0);
13094 error = got_error_from_errno("getcwd");
13098 error = got_repo_pack_fds_open(&pack_fds);
13102 error = got_worktree_open(&worktree, cwd);
13104 if (error->code == GOT_ERR_NOT_WORKTREE)
13105 error = wrap_not_worktree_error(error,
13110 error = got_repo_open(&repo,
13111 worktree ? got_worktree_get_repo_path(worktree) : cwd, NULL,
13116 if (worktree != NULL) {
13117 error = worktree_has_logmsg_ref("merge", worktree, repo);
13122 error = apply_unveil(got_repo_get_path(repo), 0,
13123 worktree ? got_worktree_get_root_path(worktree) : NULL);
13127 error = check_rebase_or_histedit_in_progress(worktree);
13131 error = got_worktree_merge_in_progress(&merge_in_progress, worktree,
13137 if (!merge_in_progress) {
13138 error = got_error(GOT_ERR_NOT_MERGING);
13141 error = got_worktree_merge_continue(&branch_name,
13142 &branch_tip, &fileindex, worktree, repo);
13145 error = got_worktree_merge_abort(worktree, fileindex, repo,
13146 abort_progress, &upa);
13149 printf("Merge of %s aborted\n", branch_name);
13150 goto done; /* nothing else to do */
13153 error = get_author(&author, repo, worktree);
13157 if (continue_merge) {
13158 if (!merge_in_progress) {
13159 error = got_error(GOT_ERR_NOT_MERGING);
13162 error = got_worktree_merge_continue(&branch_name,
13163 &branch_tip, &fileindex, worktree, repo);
13167 error = got_ref_open(&branch, repo, argv[0], 0);
13170 branch_name = strdup(got_ref_get_name(branch));
13171 if (branch_name == NULL) {
13172 error = got_error_from_errno("strdup");
13175 error = got_ref_resolve(&branch_tip, repo, branch);
13180 error = got_ref_open(&wt_branch, repo,
13181 got_worktree_get_head_ref_name(worktree), 0);
13184 error = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
13187 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
13188 wt_branch_tip, branch_tip, 0, repo,
13189 check_cancelled, NULL);
13190 if (error && error->code != GOT_ERR_ANCESTRY)
13193 if (!continue_merge) {
13194 error = check_path_prefix(wt_branch_tip, branch_tip,
13195 got_worktree_get_path_prefix(worktree),
13196 GOT_ERR_MERGE_PATH, repo);
13200 error = check_same_branch(wt_branch_tip, branch,
13203 if (error->code != GOT_ERR_ANCESTRY)
13207 static char msg[512];
13208 snprintf(msg, sizeof(msg),
13209 "cannot create a merge commit because "
13210 "%s is based on %s; %s can be integrated "
13211 "with 'got integrate' instead", branch_name,
13212 got_worktree_get_head_ref_name(worktree),
13214 error = got_error_msg(GOT_ERR_SAME_BRANCH, msg);
13218 error = got_worktree_merge_prepare(&fileindex, worktree,
13223 error = got_worktree_merge_branch(worktree, fileindex,
13224 yca_id, branch_tip, repo, update_progress, &upa,
13225 check_cancelled, NULL);
13228 print_merge_progress_stats(&upa);
13229 if (!upa.did_something) {
13230 error = got_worktree_merge_abort(worktree, fileindex,
13231 repo, abort_progress, &upa);
13234 printf("Already up-to-date\n");
13239 if (interrupt_merge) {
13240 error = got_worktree_merge_postpone(worktree, fileindex);
13243 printf("Merge of %s interrupted on request\n", branch_name);
13244 } else if (upa.conflicts > 0 || upa.missing > 0 ||
13245 upa.not_deleted > 0 || upa.unversioned > 0) {
13246 error = got_worktree_merge_postpone(worktree, fileindex);
13249 if (upa.conflicts > 0 && upa.missing == 0 &&
13250 upa.not_deleted == 0 && upa.unversioned == 0) {
13251 error = got_error_msg(GOT_ERR_CONFLICTS,
13252 "conflicts must be resolved before merging "
13254 } else if (upa.conflicts > 0) {
13255 error = got_error_msg(GOT_ERR_CONFLICTS,
13256 "conflicts must be resolved before merging "
13257 "can continue; changes destined for some "
13258 "files were not yet merged and "
13259 "should be merged manually if required before the "
13260 "merge operation is continued");
13262 error = got_error_msg(GOT_ERR_CONFLICTS,
13263 "changes destined for some "
13264 "files were not yet merged and should be "
13265 "merged manually if required before the "
13266 "merge operation is continued");
13270 error = got_worktree_merge_commit(&merge_commit_id, worktree,
13271 fileindex, author, NULL, 1, branch_tip, branch_name,
13272 repo, continue_merge ? print_status : NULL, NULL);
13275 error = got_worktree_merge_complete(worktree, fileindex, repo);
13278 error = got_object_id_str(&id_str, merge_commit_id);
13281 printf("Merged %s into %s: %s\n", branch_name,
13282 got_worktree_get_head_ref_name(worktree),
13288 free(merge_commit_id);
13294 got_ref_close(branch);
13296 got_ref_close(wt_branch);
13298 got_worktree_close(worktree);
13300 const struct got_error *close_err = got_repo_close(repo);
13305 const struct got_error *pack_err =
13306 got_repo_pack_fds_close(pack_fds);
13316 fprintf(stderr, "usage: %s stage [-lpS] [-F response-script] "
13317 "[path ...]\n", getprogname());
13321 static const struct got_error *
13322 print_stage(void *arg, unsigned char status, unsigned char staged_status,
13323 const char *path, struct got_object_id *blob_id,
13324 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
13325 int dirfd, const char *de_name)
13327 const struct got_error *err = NULL;
13328 char *id_str = NULL;
13330 if (staged_status != GOT_STATUS_ADD &&
13331 staged_status != GOT_STATUS_MODIFY &&
13332 staged_status != GOT_STATUS_DELETE)
13335 if (staged_status == GOT_STATUS_ADD ||
13336 staged_status == GOT_STATUS_MODIFY)
13337 err = got_object_id_str(&id_str, staged_blob_id);
13339 err = got_object_id_str(&id_str, blob_id);
13343 printf("%s %c %s\n", id_str, staged_status, path);
13348 static const struct got_error *
13349 cmd_stage(int argc, char *argv[])
13351 const struct got_error *error = NULL;
13352 struct got_repository *repo = NULL;
13353 struct got_worktree *worktree = NULL;
13355 struct got_pathlist_head paths;
13356 int ch, list_stage = 0, pflag = 0, allow_bad_symlinks = 0;
13357 FILE *patch_script_file = NULL;
13358 const char *patch_script_path = NULL;
13359 struct choose_patch_arg cpa;
13360 int *pack_fds = NULL;
13362 TAILQ_INIT(&paths);
13365 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
13366 "unveil", NULL) == -1)
13370 while ((ch = getopt(argc, argv, "F:lpS")) != -1) {
13373 patch_script_path = optarg;
13382 allow_bad_symlinks = 1;
13393 if (list_stage && (pflag || patch_script_path))
13394 errx(1, "-l option cannot be used with other options");
13395 if (patch_script_path && !pflag)
13396 errx(1, "-F option can only be used together with -p option");
13398 cwd = getcwd(NULL, 0);
13400 error = got_error_from_errno("getcwd");
13404 error = got_repo_pack_fds_open(&pack_fds);
13408 error = got_worktree_open(&worktree, cwd);
13410 if (error->code == GOT_ERR_NOT_WORKTREE)
13411 error = wrap_not_worktree_error(error, "stage", cwd);
13415 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
13420 if (patch_script_path) {
13421 patch_script_file = fopen(patch_script_path, "re");
13422 if (patch_script_file == NULL) {
13423 error = got_error_from_errno2("fopen",
13424 patch_script_path);
13428 error = apply_unveil(got_repo_get_path(repo), 0,
13429 got_worktree_get_root_path(worktree));
13433 error = check_merge_in_progress(worktree, repo);
13437 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
13442 error = got_worktree_status(worktree, &paths, repo, 0,
13443 print_stage, NULL, check_cancelled, NULL);
13445 cpa.patch_script_file = patch_script_file;
13446 cpa.action = "stage";
13447 error = got_worktree_stage(worktree, &paths,
13448 pflag ? NULL : print_status, NULL,
13449 pflag ? choose_patch : NULL, &cpa,
13450 allow_bad_symlinks, repo);
13453 if (patch_script_file && fclose(patch_script_file) == EOF &&
13455 error = got_error_from_errno2("fclose", patch_script_path);
13457 const struct got_error *close_err = got_repo_close(repo);
13462 got_worktree_close(worktree);
13464 const struct got_error *pack_err =
13465 got_repo_pack_fds_close(pack_fds);
13469 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
13475 usage_unstage(void)
13477 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
13478 "[path ...]\n", getprogname());
13483 static const struct got_error *
13484 cmd_unstage(int argc, char *argv[])
13486 const struct got_error *error = NULL;
13487 struct got_repository *repo = NULL;
13488 struct got_worktree *worktree = NULL;
13490 struct got_pathlist_head paths;
13492 struct got_update_progress_arg upa;
13493 FILE *patch_script_file = NULL;
13494 const char *patch_script_path = NULL;
13495 struct choose_patch_arg cpa;
13496 int *pack_fds = NULL;
13498 TAILQ_INIT(&paths);
13501 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
13502 "unveil", NULL) == -1)
13506 while ((ch = getopt(argc, argv, "F:p")) != -1) {
13509 patch_script_path = optarg;
13523 if (patch_script_path && !pflag)
13524 errx(1, "-F option can only be used together with -p option");
13526 cwd = getcwd(NULL, 0);
13528 error = got_error_from_errno("getcwd");
13532 error = got_repo_pack_fds_open(&pack_fds);
13536 error = got_worktree_open(&worktree, cwd);
13538 if (error->code == GOT_ERR_NOT_WORKTREE)
13539 error = wrap_not_worktree_error(error, "unstage", cwd);
13543 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
13548 if (patch_script_path) {
13549 patch_script_file = fopen(patch_script_path, "re");
13550 if (patch_script_file == NULL) {
13551 error = got_error_from_errno2("fopen",
13552 patch_script_path);
13557 error = apply_unveil(got_repo_get_path(repo), 0,
13558 got_worktree_get_root_path(worktree));
13562 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
13566 cpa.patch_script_file = patch_script_file;
13567 cpa.action = "unstage";
13568 memset(&upa, 0, sizeof(upa));
13569 error = got_worktree_unstage(worktree, &paths, update_progress,
13570 &upa, pflag ? choose_patch : NULL, &cpa, repo);
13572 print_merge_progress_stats(&upa);
13574 if (patch_script_file && fclose(patch_script_file) == EOF &&
13576 error = got_error_from_errno2("fclose", patch_script_path);
13578 const struct got_error *close_err = got_repo_close(repo);
13583 got_worktree_close(worktree);
13585 const struct got_error *pack_err =
13586 got_repo_pack_fds_close(pack_fds);
13590 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
13598 fprintf(stderr, "usage: %s cat [-P] [-c commit] [-r repository-path] "
13599 "arg ...\n", getprogname());
13603 static const struct got_error *
13604 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
13606 const struct got_error *err;
13607 struct got_blob_object *blob;
13610 fd = got_opentempfd();
13612 return got_error_from_errno("got_opentempfd");
13614 err = got_object_open_as_blob(&blob, repo, id, 8192, fd);
13618 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
13620 if (fd != -1 && close(fd) == -1 && err == NULL)
13621 err = got_error_from_errno("close");
13623 got_object_blob_close(blob);
13627 static const struct got_error *
13628 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
13630 const struct got_error *err;
13631 struct got_tree_object *tree;
13634 err = got_object_open_as_tree(&tree, repo, id);
13638 nentries = got_object_tree_get_nentries(tree);
13639 for (i = 0; i < nentries; i++) {
13640 struct got_tree_entry *te;
13642 if (sigint_received || sigpipe_received)
13644 te = got_object_tree_get_entry(tree, i);
13645 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
13648 fprintf(outfile, "%s %.7o %s\n", id_str,
13649 got_tree_entry_get_mode(te),
13650 got_tree_entry_get_name(te));
13654 got_object_tree_close(tree);
13658 static const struct got_error *
13659 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
13661 const struct got_error *err;
13662 struct got_commit_object *commit;
13663 const struct got_object_id_queue *parent_ids;
13664 struct got_object_qid *pid;
13665 char *id_str = NULL;
13666 const char *logmsg = NULL;
13669 err = got_object_open_as_commit(&commit, repo, id);
13673 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
13677 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
13678 parent_ids = got_object_commit_get_parent_ids(commit);
13679 fprintf(outfile, "numparents %d\n",
13680 got_object_commit_get_nparents(commit));
13681 STAILQ_FOREACH(pid, parent_ids, entry) {
13683 err = got_object_id_str(&pid_str, &pid->id);
13686 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
13689 got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
13690 got_object_commit_get_author_gmtoff(commit));
13691 fprintf(outfile, "%s%s %lld %s\n", GOT_COMMIT_LABEL_AUTHOR,
13692 got_object_commit_get_author(commit),
13693 (long long)got_object_commit_get_author_time(commit),
13696 got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
13697 got_object_commit_get_committer_gmtoff(commit));
13698 fprintf(outfile, "%s%s %lld %s\n", GOT_COMMIT_LABEL_COMMITTER,
13699 got_object_commit_get_committer(commit),
13700 (long long)got_object_commit_get_committer_time(commit),
13703 logmsg = got_object_commit_get_logmsg_raw(commit);
13704 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
13705 fprintf(outfile, "%s", logmsg);
13708 got_object_commit_close(commit);
13712 static const struct got_error *
13713 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
13715 const struct got_error *err;
13716 struct got_tag_object *tag;
13717 char *id_str = NULL;
13718 const char *tagmsg = NULL;
13721 err = got_object_open_as_tag(&tag, repo, id);
13725 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
13729 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
13731 switch (got_object_tag_get_object_type(tag)) {
13732 case GOT_OBJ_TYPE_BLOB:
13733 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
13734 GOT_OBJ_LABEL_BLOB);
13736 case GOT_OBJ_TYPE_TREE:
13737 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
13738 GOT_OBJ_LABEL_TREE);
13740 case GOT_OBJ_TYPE_COMMIT:
13741 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
13742 GOT_OBJ_LABEL_COMMIT);
13744 case GOT_OBJ_TYPE_TAG:
13745 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
13746 GOT_OBJ_LABEL_TAG);
13752 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
13753 got_object_tag_get_name(tag));
13755 got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
13756 got_object_tag_get_tagger_gmtoff(tag));
13757 fprintf(outfile, "%s%s %lld %s\n", GOT_TAG_LABEL_TAGGER,
13758 got_object_tag_get_tagger(tag),
13759 (long long)got_object_tag_get_tagger_time(tag),
13762 tagmsg = got_object_tag_get_message(tag);
13763 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
13764 fprintf(outfile, "%s", tagmsg);
13767 got_object_tag_close(tag);
13771 static const struct got_error *
13772 cmd_cat(int argc, char *argv[])
13774 const struct got_error *error;
13775 struct got_repository *repo = NULL;
13776 struct got_worktree *worktree = NULL;
13777 char *cwd = NULL, *repo_path = NULL, *label = NULL;
13778 const char *commit_id_str = NULL;
13779 struct got_object_id *id = NULL, *commit_id = NULL;
13780 struct got_commit_object *commit = NULL;
13781 int ch, obj_type, i, force_path = 0;
13782 struct got_reflist_head refs;
13783 int *pack_fds = NULL;
13788 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
13793 while ((ch = getopt(argc, argv, "c:Pr:")) != -1) {
13796 commit_id_str = optarg;
13802 repo_path = realpath(optarg, NULL);
13803 if (repo_path == NULL)
13804 return got_error_from_errno2("realpath",
13806 got_path_strip_trailing_slashes(repo_path);
13817 cwd = getcwd(NULL, 0);
13819 error = got_error_from_errno("getcwd");
13823 error = got_repo_pack_fds_open(&pack_fds);
13827 if (repo_path == NULL) {
13828 error = got_worktree_open(&worktree, cwd);
13829 if (error && error->code != GOT_ERR_NOT_WORKTREE)
13832 repo_path = strdup(
13833 got_worktree_get_repo_path(worktree));
13834 if (repo_path == NULL) {
13835 error = got_error_from_errno("strdup");
13839 /* Release work tree lock. */
13840 got_worktree_close(worktree);
13845 if (repo_path == NULL) {
13846 repo_path = strdup(cwd);
13847 if (repo_path == NULL)
13848 return got_error_from_errno("strdup");
13851 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
13856 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
13860 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
13864 if (commit_id_str == NULL)
13865 commit_id_str = GOT_REF_HEAD;
13866 error = got_repo_match_object_id(&commit_id, NULL,
13867 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
13871 error = got_object_open_as_commit(&commit, repo, commit_id);
13875 for (i = 0; i < argc; i++) {
13877 error = got_object_id_by_path(&id, repo, commit,
13882 error = got_repo_match_object_id(&id, &label, argv[i],
13883 GOT_OBJ_TYPE_ANY, NULL /* do not resolve tags */,
13886 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
13887 error->code != GOT_ERR_NOT_REF)
13889 error = got_object_id_by_path(&id, repo,
13896 error = got_object_get_type(&obj_type, repo, id);
13900 switch (obj_type) {
13901 case GOT_OBJ_TYPE_BLOB:
13902 error = cat_blob(id, repo, stdout);
13904 case GOT_OBJ_TYPE_TREE:
13905 error = cat_tree(id, repo, stdout);
13907 case GOT_OBJ_TYPE_COMMIT:
13908 error = cat_commit(id, repo, stdout);
13910 case GOT_OBJ_TYPE_TAG:
13911 error = cat_tag(id, repo, stdout);
13914 error = got_error(GOT_ERR_OBJ_TYPE);
13929 got_object_commit_close(commit);
13931 got_worktree_close(worktree);
13933 const struct got_error *close_err = got_repo_close(repo);
13938 const struct got_error *pack_err =
13939 got_repo_pack_fds_close(pack_fds);
13944 got_ref_list_free(&refs);
13951 fprintf(stderr, "usage: %s info [path ...]\n",
13956 static const struct got_error *
13957 print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
13958 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
13959 struct got_object_id *commit_id)
13961 const struct got_error *err = NULL;
13962 char *id_str = NULL;
13964 struct tm mytm, *tm;
13965 struct got_pathlist_head *paths = arg;
13966 struct got_pathlist_entry *pe;
13969 * Clear error indication from any of the path arguments which
13970 * would cause this file index entry to be displayed.
13972 TAILQ_FOREACH(pe, paths, entry) {
13973 if (got_path_cmp(path, pe->path, strlen(path),
13974 pe->path_len) == 0 ||
13975 got_path_is_child(path, pe->path, pe->path_len))
13976 pe->data = NULL; /* no error */
13979 printf(GOT_COMMIT_SEP_STR);
13981 printf("symlink: %s\n", path);
13982 else if (S_ISREG(mode)) {
13983 printf("file: %s\n", path);
13984 printf("mode: %o\n", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
13985 } else if (S_ISDIR(mode))
13986 printf("directory: %s\n", path);
13988 printf("something: %s\n", path);
13990 tm = localtime_r(&mtime, &mytm);
13993 if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) == 0)
13994 return got_error(GOT_ERR_NO_SPACE);
13995 printf("timestamp: %s\n", datebuf);
13998 err = got_object_id_str(&id_str, blob_id);
14001 printf("based on blob: %s\n", id_str);
14005 if (staged_blob_id) {
14006 err = got_object_id_str(&id_str, staged_blob_id);
14009 printf("based on staged blob: %s\n", id_str);
14014 err = got_object_id_str(&id_str, commit_id);
14017 printf("based on commit: %s\n", id_str);
14024 static const struct got_error *
14025 cmd_info(int argc, char *argv[])
14027 const struct got_error *error = NULL;
14028 struct got_worktree *worktree = NULL;
14029 char *cwd = NULL, *id_str = NULL;
14030 struct got_pathlist_head paths;
14031 char *uuidstr = NULL;
14032 int ch, show_files = 0;
14034 TAILQ_INIT(&paths);
14037 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
14042 while ((ch = getopt(argc, argv, "")) != -1) {
14053 cwd = getcwd(NULL, 0);
14055 error = got_error_from_errno("getcwd");
14059 error = got_worktree_open(&worktree, cwd);
14061 if (error->code == GOT_ERR_NOT_WORKTREE)
14062 error = wrap_not_worktree_error(error, "info", cwd);
14067 /* Remove "wpath cpath proc exec sendfd" promises. */
14068 if (pledge("stdio rpath flock unveil", NULL) == -1)
14071 error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
14076 error = get_worktree_paths_from_argv(&paths, argc, argv,
14083 error = got_object_id_str(&id_str,
14084 got_worktree_get_base_commit_id(worktree));
14088 error = got_worktree_get_uuid(&uuidstr, worktree);
14092 printf("work tree: %s\n", got_worktree_get_root_path(worktree));
14093 printf("work tree base commit: %s\n", id_str);
14094 printf("work tree path prefix: %s\n",
14095 got_worktree_get_path_prefix(worktree));
14096 printf("work tree branch reference: %s\n",
14097 got_worktree_get_head_ref_name(worktree));
14098 printf("work tree UUID: %s\n", uuidstr);
14099 printf("repository: %s\n", got_worktree_get_repo_path(worktree));
14102 struct got_pathlist_entry *pe;
14103 TAILQ_FOREACH(pe, &paths, entry) {
14104 if (pe->path_len == 0)
14107 * Assume this path will fail. This will be corrected
14108 * in print_path_info() in case the path does suceeed.
14110 pe->data = (void *)got_error(GOT_ERR_BAD_PATH);
14112 error = got_worktree_path_info(worktree, &paths,
14113 print_path_info, &paths, check_cancelled, NULL);
14116 TAILQ_FOREACH(pe, &paths, entry) {
14117 if (pe->data != NULL) {
14118 const struct got_error *perr;
14121 error = got_error_fmt(perr->code, "%s",
14129 got_worktree_close(worktree);
14130 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);