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>
42 #include "got_compat.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 [-CNnS] [-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;
9086 int show_diff = 1, commit_conflicts = 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:CF:m:NnS")) != -1) {
9106 error = valid_author(author);
9111 commit_conflicts = 1;
9115 option_conflict('F', 'm');
9116 prepared_logmsg = realpath(optarg, NULL);
9117 if (prepared_logmsg == NULL)
9118 return got_error_from_errno2("realpath",
9122 if (prepared_logmsg)
9123 option_conflict('m', 'F');
9127 non_interactive = 1;
9133 allow_bad_symlinks = 1;
9144 cwd = getcwd(NULL, 0);
9146 error = got_error_from_errno("getcwd");
9150 error = got_repo_pack_fds_open(&pack_fds);
9154 error = got_worktree_open(&worktree, cwd);
9156 if (error->code == GOT_ERR_NOT_WORKTREE)
9157 error = wrap_not_worktree_error(error, "commit", cwd);
9161 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
9164 if (rebase_in_progress) {
9165 error = got_error(GOT_ERR_REBASING);
9169 error = got_worktree_histedit_in_progress(&histedit_in_progress,
9174 error = get_gitconfig_path(&gitconfig_path);
9177 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9178 gitconfig_path, pack_fds);
9182 error = got_worktree_merge_in_progress(&merge_in_progress, worktree, repo);
9185 if (merge_in_progress) {
9186 error = got_error(GOT_ERR_MERGE_BUSY);
9190 error = get_author(&committer, repo, worktree);
9198 * unveil(2) traverses exec(2); if an editor is used we have
9199 * to apply unveil after the log message has been written.
9201 if (logmsg == NULL || strlen(logmsg) == 0)
9202 error = get_editor(&editor);
9204 error = apply_unveil(got_repo_get_path(repo), 0,
9205 got_worktree_get_root_path(worktree));
9209 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
9213 if (prepared_logmsg == NULL) {
9214 error = lookup_logmsg_ref(&merged_logmsg,
9215 argc > 0 ? &paths : NULL, &refs, worktree, repo);
9220 cl_arg.editor = editor;
9221 cl_arg.cmdline_log = logmsg;
9222 cl_arg.prepared_log = prepared_logmsg;
9223 cl_arg.merged_log = merged_logmsg;
9224 cl_arg.non_interactive = non_interactive;
9225 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
9226 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
9227 if (!histedit_in_progress) {
9228 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
9229 error = got_error(GOT_ERR_COMMIT_BRANCH);
9232 cl_arg.branch_name += 11;
9234 cl_arg.repo_path = got_repo_get_path(repo);
9235 error = got_worktree_commit(&id, worktree, &paths, author, committer,
9236 allow_bad_symlinks, show_diff, commit_conflicts,
9237 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
9239 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
9240 cl_arg.logmsg_path != NULL)
9241 preserve_logmsg = 1;
9245 error = got_object_id_str(&id_str, id);
9248 printf("Created commit %s\n", id_str);
9250 TAILQ_FOREACH(re, &refs, entry) {
9251 error = got_ref_delete(re->ref, repo);
9257 if (preserve_logmsg) {
9258 fprintf(stderr, "%s: log message preserved in %s\n",
9259 getprogname(), cl_arg.logmsg_path);
9260 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
9262 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
9263 free(cl_arg.logmsg_path);
9264 if (merged_logmsg && unlink(merged_logmsg) == -1 && error == NULL)
9265 error = got_error_from_errno2("unlink", merged_logmsg);
9266 free(merged_logmsg);
9268 const struct got_error *close_err = got_repo_close(repo);
9273 got_worktree_close(worktree);
9275 const struct got_error *pack_err =
9276 got_repo_pack_fds_close(pack_fds);
9280 got_ref_list_free(&refs);
9281 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
9284 free(gitconfig_path);
9287 free(prepared_logmsg);
9294 fprintf(stderr, "usage: %s send [-afqTv] [-b branch] [-d branch] "
9295 "[-r repository-path] [-t tag] [remote-repository]\n",
9301 print_load_info(int print_colored, int print_found, int print_trees,
9302 int ncolored, int nfound, int ntrees)
9304 if (print_colored) {
9305 printf("%d commit%s colored", ncolored,
9306 ncolored == 1 ? "" : "s");
9309 printf("%s%d object%s found",
9310 ncolored > 0 ? "; " : "",
9311 nfound, nfound == 1 ? "" : "s");
9314 printf("; %d tree%s scanned", ntrees,
9315 ntrees == 1 ? "" : "s");
9319 struct got_send_progress_arg {
9320 char last_scaled_packsize[FMT_SCALED_STRSIZE];
9327 int last_nobj_total;
9331 int printed_something;
9333 struct got_pathlist_head *delete_branches;
9336 static const struct got_error *
9337 send_progress(void *arg, int ncolored, int nfound, int ntrees,
9338 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
9339 int nobj_written, off_t bytes_sent, const char *refname,
9340 const char *errmsg, int success)
9342 struct got_send_progress_arg *a = arg;
9343 char scaled_packsize[FMT_SCALED_STRSIZE];
9344 char scaled_sent[FMT_SCALED_STRSIZE];
9345 int p_deltify = 0, p_written = 0, p_sent = 0;
9346 int print_colored = 0, print_found = 0, print_trees = 0;
9347 int print_searching = 0, print_total = 0;
9348 int print_deltify = 0, print_written = 0, print_sent = 0;
9350 if (a->verbosity < 0)
9354 const char *status = success ? "accepted" : "rejected";
9357 struct got_pathlist_entry *pe;
9358 TAILQ_FOREACH(pe, a->delete_branches, entry) {
9359 const char *branchname = pe->path;
9360 if (got_path_cmp(branchname, refname,
9361 strlen(branchname), strlen(refname)) == 0) {
9363 a->sent_something = 1;
9369 if (a->printed_something)
9371 printf("Server has %s %s", status, refname);
9373 printf(": %s", errmsg);
9374 a->printed_something = 1;
9378 if (a->last_ncolored != ncolored) {
9380 a->last_ncolored = ncolored;
9383 if (a->last_nfound != nfound) {
9386 a->last_nfound = nfound;
9389 if (a->last_ntrees != ntrees) {
9393 a->last_ntrees = ntrees;
9396 if ((print_colored || print_found || print_trees) &&
9399 print_load_info(print_colored, print_found, print_trees,
9400 ncolored, nfound, ntrees);
9401 a->printed_something = 1;
9404 } else if (!a->loading_done) {
9406 print_load_info(1, 1, 1, ncolored, nfound, ntrees);
9408 a->loading_done = 1;
9411 if (fmt_scaled(packfile_size, scaled_packsize) == -1)
9412 return got_error_from_errno("fmt_scaled");
9413 if (fmt_scaled(bytes_sent, scaled_sent) == -1)
9414 return got_error_from_errno("fmt_scaled");
9416 if (a->last_ncommits != ncommits) {
9417 print_searching = 1;
9418 a->last_ncommits = ncommits;
9421 if (a->last_nobj_total != nobj_total) {
9422 print_searching = 1;
9424 a->last_nobj_total = nobj_total;
9427 if (packfile_size > 0 && (a->last_scaled_packsize[0] == '\0' ||
9428 strcmp(scaled_packsize, a->last_scaled_packsize)) != 0) {
9429 if (strlcpy(a->last_scaled_packsize, scaled_packsize,
9430 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
9431 return got_error(GOT_ERR_NO_SPACE);
9434 if (nobj_deltify > 0 || nobj_written > 0) {
9435 if (nobj_deltify > 0) {
9436 p_deltify = (nobj_deltify * 100) / nobj_total;
9437 if (p_deltify != a->last_p_deltify) {
9438 a->last_p_deltify = p_deltify;
9439 print_searching = 1;
9444 if (nobj_written > 0) {
9445 p_written = (nobj_written * 100) / nobj_total;
9446 if (p_written != a->last_p_written) {
9447 a->last_p_written = p_written;
9448 print_searching = 1;
9456 if (bytes_sent > 0) {
9457 p_sent = (bytes_sent * 100) / packfile_size;
9458 if (p_sent != a->last_p_sent) {
9459 a->last_p_sent = p_sent;
9460 print_searching = 1;
9466 a->sent_something = 1;
9469 if (print_searching || print_total || print_deltify || print_written ||
9472 if (print_searching)
9473 printf("packing %d reference%s", ncommits,
9474 ncommits == 1 ? "" : "s");
9476 printf("; %d object%s", nobj_total,
9477 nobj_total == 1 ? "" : "s");
9479 printf("; deltify: %d%%", p_deltify);
9481 printf("; uploading pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
9482 scaled_packsize, p_sent);
9483 else if (print_written)
9484 printf("; writing pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
9485 scaled_packsize, p_written);
9486 if (print_searching || print_total || print_deltify ||
9487 print_written || print_sent) {
9488 a->printed_something = 1;
9494 static const struct got_error *
9495 cmd_send(int argc, char *argv[])
9497 const struct got_error *error = NULL;
9498 char *cwd = NULL, *repo_path = NULL;
9499 const char *remote_name;
9500 char *proto = NULL, *host = NULL, *port = NULL;
9501 char *repo_name = NULL, *server_path = NULL;
9502 const struct got_remote_repo *remotes, *remote = NULL;
9503 int nremotes, nbranches = 0, ndelete_branches = 0;
9504 struct got_repository *repo = NULL;
9505 struct got_worktree *worktree = NULL;
9506 const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
9507 struct got_pathlist_head branches;
9508 struct got_pathlist_head tags;
9509 struct got_reflist_head all_branches;
9510 struct got_reflist_head all_tags;
9511 struct got_pathlist_head delete_args;
9512 struct got_pathlist_head delete_branches;
9513 struct got_reflist_entry *re;
9514 struct got_pathlist_entry *pe;
9515 int i, ch, sendfd = -1, sendstatus;
9517 struct got_send_progress_arg spa;
9518 int verbosity = 0, overwrite_refs = 0;
9519 int send_all_branches = 0, send_all_tags = 0;
9520 struct got_reference *ref = NULL;
9521 int *pack_fds = NULL;
9523 TAILQ_INIT(&branches);
9525 TAILQ_INIT(&all_branches);
9526 TAILQ_INIT(&all_tags);
9527 TAILQ_INIT(&delete_args);
9528 TAILQ_INIT(&delete_branches);
9530 while ((ch = getopt(argc, argv, "ab:d:fqr:Tt:v")) != -1) {
9533 send_all_branches = 1;
9536 error = got_pathlist_append(&branches, optarg, NULL);
9542 error = got_pathlist_append(&delete_args, optarg, NULL);
9553 repo_path = realpath(optarg, NULL);
9554 if (repo_path == NULL)
9555 return got_error_from_errno2("realpath",
9557 got_path_strip_trailing_slashes(repo_path);
9563 error = got_pathlist_append(&tags, optarg, NULL);
9570 else if (verbosity < 3)
9581 if (send_all_branches && !TAILQ_EMPTY(&branches))
9582 option_conflict('a', 'b');
9583 if (send_all_tags && !TAILQ_EMPTY(&tags))
9584 option_conflict('T', 't');
9588 remote_name = GOT_SEND_DEFAULT_REMOTE_NAME;
9590 remote_name = argv[0];
9594 cwd = getcwd(NULL, 0);
9596 error = got_error_from_errno("getcwd");
9600 error = got_repo_pack_fds_open(&pack_fds);
9604 if (repo_path == NULL) {
9605 error = got_worktree_open(&worktree, cwd);
9606 if (error && error->code != GOT_ERR_NOT_WORKTREE)
9612 strdup(got_worktree_get_repo_path(worktree));
9613 if (repo_path == NULL)
9614 error = got_error_from_errno("strdup");
9618 repo_path = strdup(cwd);
9619 if (repo_path == NULL) {
9620 error = got_error_from_errno("strdup");
9626 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
9631 worktree_conf = got_worktree_get_gotconfig(worktree);
9632 if (worktree_conf) {
9633 got_gotconfig_get_remotes(&nremotes, &remotes,
9635 for (i = 0; i < nremotes; i++) {
9636 if (strcmp(remotes[i].name, remote_name) == 0) {
9637 remote = &remotes[i];
9643 if (remote == NULL) {
9644 repo_conf = got_repo_get_gotconfig(repo);
9646 got_gotconfig_get_remotes(&nremotes, &remotes,
9648 for (i = 0; i < nremotes; i++) {
9649 if (strcmp(remotes[i].name, remote_name) == 0) {
9650 remote = &remotes[i];
9656 if (remote == NULL) {
9657 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
9658 for (i = 0; i < nremotes; i++) {
9659 if (strcmp(remotes[i].name, remote_name) == 0) {
9660 remote = &remotes[i];
9665 if (remote == NULL) {
9666 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
9670 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
9671 &repo_name, remote->send_url);
9675 if (strcmp(proto, "git") == 0) {
9677 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
9678 "sendfd dns inet unveil", NULL) == -1)
9681 } else if (strcmp(proto, "git+ssh") == 0 ||
9682 strcmp(proto, "ssh") == 0) {
9684 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
9685 "sendfd unveil", NULL) == -1)
9688 } else if (strcmp(proto, "http") == 0 ||
9689 strcmp(proto, "git+http") == 0) {
9690 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
9693 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
9697 error = got_dial_apply_unveil(proto);
9701 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
9705 if (send_all_branches) {
9706 error = got_ref_list(&all_branches, repo, "refs/heads",
9707 got_ref_cmp_by_name, NULL);
9710 TAILQ_FOREACH(re, &all_branches, entry) {
9711 const char *branchname = got_ref_get_name(re->ref);
9712 error = got_pathlist_append(&branches,
9718 } else if (nbranches == 0) {
9719 for (i = 0; i < remote->nsend_branches; i++) {
9720 error = got_pathlist_append(&branches,
9721 remote->send_branches[i], NULL);
9727 if (send_all_tags) {
9728 error = got_ref_list(&all_tags, repo, "refs/tags",
9729 got_ref_cmp_by_name, NULL);
9732 TAILQ_FOREACH(re, &all_tags, entry) {
9733 const char *tagname = got_ref_get_name(re->ref);
9734 error = got_pathlist_append(&tags,
9742 * To prevent accidents only branches in refs/heads/ can be deleted
9743 * with 'got send -d'.
9744 * Deleting anything else requires local repository access or Git.
9746 TAILQ_FOREACH(pe, &delete_args, entry) {
9747 const char *branchname = pe->path;
9749 struct got_pathlist_entry *new;
9750 if (strncmp(branchname, "refs/heads/", 11) == 0) {
9751 s = strdup(branchname);
9753 error = got_error_from_errno("strdup");
9757 if (asprintf(&s, "refs/heads/%s", branchname) == -1) {
9758 error = got_error_from_errno("asprintf");
9762 error = got_pathlist_insert(&new, &delete_branches, s, NULL);
9763 if (error || new == NULL /* duplicate */)
9770 if (nbranches == 0 && ndelete_branches == 0) {
9771 struct got_reference *head_ref;
9773 error = got_ref_open(&head_ref, repo,
9774 got_worktree_get_head_ref_name(worktree), 0);
9776 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
9779 if (got_ref_is_symbolic(head_ref)) {
9780 error = got_ref_resolve_symbolic(&ref, repo, head_ref);
9781 got_ref_close(head_ref);
9786 error = got_pathlist_append(&branches, got_ref_get_name(ref),
9793 if (verbosity >= 0) {
9794 printf("Connecting to \"%s\" %s://%s%s%s%s%s\n",
9795 remote->name, proto, host,
9796 port ? ":" : "", port ? port : "",
9797 *server_path == '/' ? "" : "/", server_path);
9800 error = got_send_connect(&sendpid, &sendfd, proto, host, port,
9801 server_path, verbosity);
9805 memset(&spa, 0, sizeof(spa));
9806 spa.last_scaled_packsize[0] = '\0';
9807 spa.last_p_deltify = -1;
9808 spa.last_p_written = -1;
9809 spa.verbosity = verbosity;
9810 spa.delete_branches = &delete_branches;
9811 error = got_send_pack(remote_name, &branches, &tags, &delete_branches,
9812 verbosity, overwrite_refs, sendfd, repo, send_progress, &spa,
9813 check_cancelled, NULL);
9814 if (spa.printed_something)
9818 if (!spa.sent_something && verbosity >= 0)
9819 printf("Already up-to-date\n");
9822 if (kill(sendpid, SIGTERM) == -1)
9823 error = got_error_from_errno("kill");
9824 if (waitpid(sendpid, &sendstatus, 0) == -1 && error == NULL)
9825 error = got_error_from_errno("waitpid");
9827 if (sendfd != -1 && close(sendfd) == -1 && error == NULL)
9828 error = got_error_from_errno("close");
9830 const struct got_error *close_err = got_repo_close(repo);
9835 got_worktree_close(worktree);
9837 const struct got_error *pack_err =
9838 got_repo_pack_fds_close(pack_fds);
9844 got_pathlist_free(&branches, GOT_PATHLIST_FREE_NONE);
9845 got_pathlist_free(&tags, GOT_PATHLIST_FREE_NONE);
9846 got_ref_list_free(&all_branches);
9847 got_ref_list_free(&all_tags);
9848 got_pathlist_free(&delete_args, GOT_PATHLIST_FREE_NONE);
9849 got_pathlist_free(&delete_branches, GOT_PATHLIST_FREE_PATH);
9861 * Print and if delete is set delete all ref_prefix references.
9862 * If wanted_ref is not NULL, only print or delete this reference.
9864 static const struct got_error *
9865 process_logmsg_refs(const char *ref_prefix, size_t prefix_len,
9866 const char *wanted_ref, int delete, struct got_worktree *worktree,
9867 struct got_repository *repo)
9869 const struct got_error *err;
9870 struct got_pathlist_head paths;
9871 struct got_reflist_head refs;
9872 struct got_reflist_entry *re;
9873 struct got_reflist_object_id_map *refs_idmap = NULL;
9874 struct got_commit_object *commit = NULL;
9875 struct got_object_id *id = NULL;
9876 const char *header_prefix;
9877 char *uuidstr = NULL;
9883 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, repo);
9887 err = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
9891 if (worktree != NULL) {
9892 err = got_worktree_get_uuid(&uuidstr, worktree);
9898 if (strncmp(wanted_ref, "refs/heads/", 11) == 0)
9902 if (strcmp(ref_prefix, GOT_WORKTREE_BACKOUT_REF_PREFIX) == 0)
9903 header_prefix = "backout";
9905 header_prefix = "cherrypick";
9907 TAILQ_FOREACH(re, &refs, entry) {
9908 const char *refname, *wt;
9910 refname = got_ref_get_name(re->ref);
9912 err = check_cancelled(NULL);
9916 if (strncmp(refname, ref_prefix, prefix_len) == 0)
9917 refname += prefix_len + 1; /* skip '-' delimiter */
9923 if (worktree == NULL || strncmp(refname, uuidstr,
9924 GOT_WORKTREE_UUID_STRLEN) == 0)
9925 refname += GOT_WORKTREE_UUID_STRLEN + 1; /* skip '-' */
9929 err = got_repo_match_object_id(&id, NULL, refname,
9930 GOT_OBJ_TYPE_COMMIT, NULL, repo);
9934 err = got_object_open_as_commit(&commit, repo, id);
9939 found = strncmp(wanted_ref, refname,
9940 strlen(wanted_ref)) == 0;
9941 if (wanted_ref && !found) {
9942 struct got_reflist_head *ci_refs;
9944 ci_refs = got_reflist_object_id_map_lookup(refs_idmap,
9948 char *refs_str = NULL;
9949 char const *r = NULL;
9951 err = build_refs_str(&refs_str, ci_refs, id,
9958 if (strncmp(r, wanted_ref,
9959 strlen(wanted_ref)) == 0) {
9971 if (wanted_ref == NULL || found) {
9973 err = got_ref_delete(re->ref, repo);
9976 printf("Deleted: ");
9977 err = print_commit_oneline(commit, id, repo,
9981 * Print paths modified by commit to help
9982 * associate commits with worktree changes.
9984 err = get_changed_paths(&paths, commit,
9989 err = print_commit(commit, id, repo, NULL,
9990 &paths, NULL, 0, 0, refs_idmap, NULL,
9992 got_pathlist_free(&paths,
9993 GOT_PATHLIST_FREE_ALL);
9995 if (worktree == NULL)
9996 printf("work tree: %.*s\n\n",
9997 GOT_WORKTREE_UUID_STRLEN, wt);
10003 got_object_commit_close(commit);
10009 if (wanted_ref != NULL && !found)
10010 err = got_error_fmt(GOT_ERR_NOT_REF, "%s", wanted_ref);
10015 got_ref_list_free(&refs);
10016 got_pathlist_free(&paths, GOT_PATHLIST_FREE_ALL);
10018 got_reflist_object_id_map_free(refs_idmap);
10020 got_object_commit_close(commit);
10025 * Create new temp "logmsg" ref of the backed-out or cherrypicked commit
10026 * identified by id for log messages to prepopulate the editor on commit.
10028 static const struct got_error *
10029 logmsg_ref(struct got_object_id *id, const char *prefix,
10030 struct got_worktree *worktree, struct got_repository *repo)
10032 const struct got_error *err = NULL;
10033 char *idstr, *ref = NULL, *refname = NULL;
10034 int histedit_in_progress;
10035 int rebase_in_progress, merge_in_progress;
10038 * Silenty refuse to create merge reference if any histedit, merge,
10039 * or rebase operation is in progress.
10041 err = got_worktree_histedit_in_progress(&histedit_in_progress,
10045 if (histedit_in_progress)
10048 err = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
10051 if (rebase_in_progress)
10054 err = got_worktree_merge_in_progress(&merge_in_progress, worktree,
10058 if (merge_in_progress)
10061 err = got_object_id_str(&idstr, id);
10065 err = got_worktree_get_logmsg_ref_name(&refname, worktree, prefix);
10069 if (asprintf(&ref, "%s-%s", refname, idstr) == -1) {
10070 err = got_error_from_errno("asprintf");
10074 err = create_ref(ref, got_worktree_get_base_commit_id(worktree),
10084 usage_cherrypick(void)
10086 fprintf(stderr, "usage: %s cherrypick [-lX] [commit-id]\n",
10091 static const struct got_error *
10092 cmd_cherrypick(int argc, char *argv[])
10094 const struct got_error *error = NULL;
10095 struct got_worktree *worktree = NULL;
10096 struct got_repository *repo = NULL;
10097 char *cwd = NULL, *commit_id_str = NULL;
10098 struct got_object_id *commit_id = NULL;
10099 struct got_commit_object *commit = NULL;
10100 struct got_object_qid *pid;
10101 int ch, list_refs = 0, remove_refs = 0;
10102 struct got_update_progress_arg upa;
10103 int *pack_fds = NULL;
10106 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10107 "unveil", NULL) == -1)
10111 while ((ch = getopt(argc, argv, "lX")) != -1) {
10120 usage_cherrypick();
10128 if (list_refs || remove_refs) {
10129 if (argc != 0 && argc != 1)
10130 usage_cherrypick();
10131 } else if (argc != 1)
10132 usage_cherrypick();
10133 if (list_refs && remove_refs)
10134 option_conflict('l', 'X');
10136 cwd = getcwd(NULL, 0);
10138 error = got_error_from_errno("getcwd");
10142 error = got_repo_pack_fds_open(&pack_fds);
10146 error = got_worktree_open(&worktree, cwd);
10148 if (list_refs || remove_refs) {
10149 if (error->code != GOT_ERR_NOT_WORKTREE)
10152 if (error->code == GOT_ERR_NOT_WORKTREE)
10153 error = wrap_not_worktree_error(error,
10154 "cherrypick", cwd);
10159 error = got_repo_open(&repo,
10160 worktree ? got_worktree_get_repo_path(worktree) : cwd,
10165 error = apply_unveil(got_repo_get_path(repo), 0,
10166 worktree ? got_worktree_get_root_path(worktree) : NULL);
10170 if (list_refs || remove_refs) {
10171 error = process_logmsg_refs(GOT_WORKTREE_CHERRYPICK_REF_PREFIX,
10172 GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN,
10173 argc == 1 ? argv[0] : NULL, remove_refs, worktree, repo);
10177 error = got_repo_match_object_id(&commit_id, NULL, argv[0],
10178 GOT_OBJ_TYPE_COMMIT, NULL, repo);
10181 error = got_object_id_str(&commit_id_str, commit_id);
10185 error = got_object_open_as_commit(&commit, repo, commit_id);
10188 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
10189 memset(&upa, 0, sizeof(upa));
10190 error = got_worktree_merge_files(worktree, pid ? &pid->id : NULL,
10191 commit_id, repo, update_progress, &upa, check_cancelled,
10196 if (upa.did_something) {
10197 error = logmsg_ref(commit_id,
10198 GOT_WORKTREE_CHERRYPICK_REF_PREFIX, worktree, repo);
10201 printf("Merged commit %s\n", commit_id_str);
10203 print_merge_progress_stats(&upa);
10207 got_object_commit_close(commit);
10208 free(commit_id_str);
10210 got_worktree_close(worktree);
10212 const struct got_error *close_err = got_repo_close(repo);
10217 const struct got_error *pack_err =
10218 got_repo_pack_fds_close(pack_fds);
10227 usage_backout(void)
10229 fprintf(stderr, "usage: %s backout [-lX] [commit-id]\n", getprogname());
10233 static const struct got_error *
10234 cmd_backout(int argc, char *argv[])
10236 const struct got_error *error = NULL;
10237 struct got_worktree *worktree = NULL;
10238 struct got_repository *repo = NULL;
10239 char *cwd = NULL, *commit_id_str = NULL;
10240 struct got_object_id *commit_id = NULL;
10241 struct got_commit_object *commit = NULL;
10242 struct got_object_qid *pid;
10243 int ch, list_refs = 0, remove_refs = 0;
10244 struct got_update_progress_arg upa;
10245 int *pack_fds = NULL;
10248 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10249 "unveil", NULL) == -1)
10253 while ((ch = getopt(argc, argv, "lX")) != -1) {
10270 if (list_refs || remove_refs) {
10271 if (argc != 0 && argc != 1)
10273 } else if (argc != 1)
10275 if (list_refs && remove_refs)
10276 option_conflict('l', 'X');
10278 cwd = getcwd(NULL, 0);
10280 error = got_error_from_errno("getcwd");
10284 error = got_repo_pack_fds_open(&pack_fds);
10288 error = got_worktree_open(&worktree, cwd);
10290 if (list_refs || remove_refs) {
10291 if (error->code != GOT_ERR_NOT_WORKTREE)
10294 if (error->code == GOT_ERR_NOT_WORKTREE)
10295 error = wrap_not_worktree_error(error,
10301 error = got_repo_open(&repo,
10302 worktree ? got_worktree_get_repo_path(worktree) : cwd,
10307 error = apply_unveil(got_repo_get_path(repo), 0,
10308 worktree ? got_worktree_get_root_path(worktree) : NULL);
10312 if (list_refs || remove_refs) {
10313 error = process_logmsg_refs(GOT_WORKTREE_BACKOUT_REF_PREFIX,
10314 GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN,
10315 argc == 1 ? argv[0] : NULL, remove_refs, worktree, repo);
10319 error = got_repo_match_object_id(&commit_id, NULL, argv[0],
10320 GOT_OBJ_TYPE_COMMIT, NULL, repo);
10323 error = got_object_id_str(&commit_id_str, commit_id);
10327 error = got_object_open_as_commit(&commit, repo, commit_id);
10330 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
10332 error = got_error(GOT_ERR_ROOT_COMMIT);
10336 memset(&upa, 0, sizeof(upa));
10337 error = got_worktree_merge_files(worktree, commit_id, &pid->id,
10338 repo, update_progress, &upa, check_cancelled, NULL);
10342 if (upa.did_something) {
10343 error = logmsg_ref(commit_id, GOT_WORKTREE_BACKOUT_REF_PREFIX,
10347 printf("Backed out commit %s\n", commit_id_str);
10349 print_merge_progress_stats(&upa);
10353 got_object_commit_close(commit);
10354 free(commit_id_str);
10356 got_worktree_close(worktree);
10358 const struct got_error *close_err = got_repo_close(repo);
10363 const struct got_error *pack_err =
10364 got_repo_pack_fds_close(pack_fds);
10374 fprintf(stderr, "usage: %s rebase [-aCclX] [branch]\n", getprogname());
10379 trim_logmsg(char *logmsg, int limit)
10384 len = strlen(logmsg);
10387 logmsg[len] = '\0';
10388 nl = strchr(logmsg, '\n');
10393 static const struct got_error *
10394 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
10396 const struct got_error *err;
10397 char *logmsg0 = NULL;
10400 err = got_object_commit_get_logmsg(&logmsg0, commit);
10405 while (isspace((unsigned char)s[0]))
10408 *logmsg = strdup(s);
10409 if (*logmsg == NULL) {
10410 err = got_error_from_errno("strdup");
10414 trim_logmsg(*logmsg, limit);
10420 static const struct got_error *
10421 show_rebase_merge_conflict(struct got_object_id *id,
10422 struct got_repository *repo)
10424 const struct got_error *err;
10425 struct got_commit_object *commit = NULL;
10426 char *id_str = NULL, *logmsg = NULL;
10428 err = got_object_open_as_commit(&commit, repo, id);
10432 err = got_object_id_str(&id_str, id);
10438 err = get_short_logmsg(&logmsg, 42, commit);
10442 printf("%s -> merge conflict: %s\n", id_str, logmsg);
10445 got_object_commit_close(commit);
10450 static const struct got_error *
10451 show_rebase_progress(struct got_commit_object *commit,
10452 struct got_object_id *old_id, struct got_object_id *new_id)
10454 const struct got_error *err;
10455 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
10457 err = got_object_id_str(&old_id_str, old_id);
10462 err = got_object_id_str(&new_id_str, new_id);
10467 old_id_str[12] = '\0';
10469 new_id_str[12] = '\0';
10471 err = get_short_logmsg(&logmsg, 42, commit);
10475 printf("%s -> %s: %s\n", old_id_str,
10476 new_id_str ? new_id_str : "no-op change", logmsg);
10484 static const struct got_error *
10485 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
10486 struct got_reference *branch, struct got_reference *tmp_branch,
10487 struct got_repository *repo, int create_backup)
10489 printf("Switching work tree to %s\n", got_ref_get_name(branch));
10490 return got_worktree_rebase_complete(worktree, fileindex,
10491 tmp_branch, branch, repo, create_backup);
10494 static const struct got_error *
10495 rebase_commit(struct got_pathlist_head *merged_paths,
10496 struct got_worktree *worktree, struct got_fileindex *fileindex,
10497 struct got_reference *tmp_branch, const char *committer,
10498 struct got_object_id *commit_id, int allow_conflict,
10499 struct got_repository *repo)
10501 const struct got_error *error;
10502 struct got_commit_object *commit;
10503 struct got_object_id *new_commit_id;
10505 error = got_object_open_as_commit(&commit, repo, commit_id);
10509 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
10510 worktree, fileindex, tmp_branch, committer, commit, commit_id,
10511 allow_conflict, repo);
10513 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
10515 error = show_rebase_progress(commit, commit_id, NULL);
10517 error = show_rebase_progress(commit, commit_id, new_commit_id);
10518 free(new_commit_id);
10521 got_object_commit_close(commit);
10525 struct check_path_prefix_arg {
10526 const char *path_prefix;
10531 static const struct got_error *
10532 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
10533 struct got_blob_object *blob2, FILE *f1, FILE *f2,
10534 struct got_object_id *id1, struct got_object_id *id2,
10535 const char *path1, const char *path2,
10536 mode_t mode1, mode_t mode2, struct got_repository *repo)
10538 struct check_path_prefix_arg *a = arg;
10540 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
10541 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
10542 return got_error(a->errcode);
10547 static const struct got_error *
10548 check_path_prefix(struct got_object_id *parent_id,
10549 struct got_object_id *commit_id, const char *path_prefix,
10550 int errcode, struct got_repository *repo)
10552 const struct got_error *err;
10553 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
10554 struct got_commit_object *commit = NULL, *parent_commit = NULL;
10555 struct check_path_prefix_arg cpp_arg;
10557 if (got_path_is_root_dir(path_prefix))
10560 err = got_object_open_as_commit(&commit, repo, commit_id);
10564 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
10568 err = got_object_open_as_tree(&tree1, repo,
10569 got_object_commit_get_tree_id(parent_commit));
10573 err = got_object_open_as_tree(&tree2, repo,
10574 got_object_commit_get_tree_id(commit));
10578 cpp_arg.path_prefix = path_prefix;
10579 while (cpp_arg.path_prefix[0] == '/')
10580 cpp_arg.path_prefix++;
10581 cpp_arg.len = strlen(cpp_arg.path_prefix);
10582 cpp_arg.errcode = errcode;
10583 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
10584 check_path_prefix_in_diff, &cpp_arg, 0);
10587 got_object_tree_close(tree1);
10589 got_object_tree_close(tree2);
10591 got_object_commit_close(commit);
10593 got_object_commit_close(parent_commit);
10597 static const struct got_error *
10598 collect_commits(struct got_object_id_queue *commits,
10599 struct got_object_id *initial_commit_id,
10600 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
10601 const char *path_prefix, int path_prefix_errcode,
10602 struct got_repository *repo)
10604 const struct got_error *err = NULL;
10605 struct got_commit_graph *graph = NULL;
10606 struct got_object_id parent_id, commit_id;
10607 struct got_object_qid *qid;
10609 err = got_commit_graph_open(&graph, "/", 1);
10613 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
10614 check_cancelled, NULL);
10618 memcpy(&commit_id, initial_commit_id, sizeof(commit_id));
10619 while (got_object_id_cmp(&commit_id, iter_stop_id) != 0) {
10620 err = got_commit_graph_iter_next(&parent_id, graph, repo,
10621 check_cancelled, NULL);
10623 if (err->code == GOT_ERR_ITER_COMPLETED) {
10624 err = got_error_msg(GOT_ERR_ANCESTRY,
10625 "ran out of commits to rebase before "
10626 "youngest common ancestor commit has "
10627 "been reached?!?");
10631 err = check_path_prefix(&parent_id, &commit_id,
10632 path_prefix, path_prefix_errcode, repo);
10636 err = got_object_qid_alloc(&qid, &commit_id);
10639 STAILQ_INSERT_HEAD(commits, qid, entry);
10641 memcpy(&commit_id, &parent_id, sizeof(commit_id));
10645 got_commit_graph_close(graph);
10649 static const struct got_error *
10650 get_commit_brief_str(char **brief_str, struct got_commit_object *commit)
10652 const struct got_error *err = NULL;
10653 time_t committer_time;
10655 char datebuf[11]; /* YYYY-MM-DD + NUL */
10656 char *author0 = NULL, *author, *smallerthan;
10657 char *logmsg0 = NULL, *logmsg, *newline;
10659 committer_time = got_object_commit_get_committer_time(commit);
10660 if (gmtime_r(&committer_time, &tm) == NULL)
10661 return got_error_from_errno("gmtime_r");
10662 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d", &tm) == 0)
10663 return got_error(GOT_ERR_NO_SPACE);
10665 author0 = strdup(got_object_commit_get_author(commit));
10666 if (author0 == NULL)
10667 return got_error_from_errno("strdup");
10669 smallerthan = strchr(author, '<');
10670 if (smallerthan && smallerthan[1] != '\0')
10671 author = smallerthan + 1;
10672 author[strcspn(author, "@>")] = '\0';
10674 err = got_object_commit_get_logmsg(&logmsg0, commit);
10678 while (*logmsg == '\n')
10680 newline = strchr(logmsg, '\n');
10684 if (asprintf(brief_str, "%s %s %s",
10685 datebuf, author, logmsg) == -1)
10686 err = got_error_from_errno("asprintf");
10693 static const struct got_error *
10694 delete_backup_ref(struct got_reference *ref, struct got_object_id *id,
10695 struct got_repository *repo)
10697 const struct got_error *err;
10700 err = got_object_id_str(&id_str, id);
10704 err = got_ref_delete(ref, repo);
10708 printf("Deleted %s: %s\n", got_ref_get_name(ref), id_str);
10714 static const struct got_error *
10715 print_backup_ref(const char *branch_name, const char *new_id_str,
10716 struct got_object_id *old_commit_id, struct got_commit_object *old_commit,
10717 struct got_reflist_object_id_map *refs_idmap,
10718 struct got_repository *repo)
10720 const struct got_error *err = NULL;
10721 struct got_reflist_head *refs;
10722 char *refs_str = NULL;
10723 struct got_object_id *new_commit_id = NULL;
10724 struct got_commit_object *new_commit = NULL;
10725 char *new_commit_brief_str = NULL;
10726 struct got_object_id *yca_id = NULL;
10727 struct got_commit_object *yca_commit = NULL;
10728 char *yca_id_str = NULL, *yca_brief_str = NULL;
10729 char *custom_refs_str;
10731 if (asprintf(&custom_refs_str, "formerly %s", branch_name) == -1)
10732 return got_error_from_errno("asprintf");
10734 err = print_commit(old_commit, old_commit_id, repo, NULL, NULL, NULL,
10735 0, 0, refs_idmap, custom_refs_str, NULL);
10739 err = got_object_resolve_id_str(&new_commit_id, repo, new_id_str);
10743 refs = got_reflist_object_id_map_lookup(refs_idmap, new_commit_id);
10745 err = build_refs_str(&refs_str, refs, new_commit_id, repo, 0);
10750 err = got_object_open_as_commit(&new_commit, repo, new_commit_id);
10754 err = get_commit_brief_str(&new_commit_brief_str, new_commit);
10758 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
10759 old_commit_id, new_commit_id, 1, repo, check_cancelled, NULL);
10763 printf("has become commit %s%s%s%s\n %s\n", new_id_str,
10764 refs_str ? " (" : "", refs_str ? refs_str : "",
10765 refs_str ? ")" : "", new_commit_brief_str);
10766 if (yca_id && got_object_id_cmp(yca_id, new_commit_id) != 0 &&
10767 got_object_id_cmp(yca_id, old_commit_id) != 0) {
10771 err = got_object_open_as_commit(&yca_commit, repo, yca_id);
10775 err = get_commit_brief_str(&yca_brief_str, yca_commit);
10779 err = got_object_id_str(&yca_id_str, yca_id);
10783 refs = got_reflist_object_id_map_lookup(refs_idmap, yca_id);
10785 err = build_refs_str(&refs_str, refs, yca_id, repo, 0);
10789 printf("history forked at %s%s%s%s\n %s\n",
10791 refs_str ? " (" : "", refs_str ? refs_str : "",
10792 refs_str ? ")" : "", yca_brief_str);
10795 free(custom_refs_str);
10796 free(new_commit_id);
10800 free(yca_brief_str);
10802 got_object_commit_close(new_commit);
10804 got_object_commit_close(yca_commit);
10809 static const struct got_error *
10810 worktree_has_logmsg_ref(const char *caller, struct got_worktree *worktree,
10811 struct got_repository *repo)
10813 const struct got_error *err;
10814 struct got_reflist_head refs;
10815 struct got_reflist_entry *re;
10816 char *uuidstr = NULL;
10817 static char msg[160];
10821 err = got_worktree_get_uuid(&uuidstr, worktree);
10825 err = got_ref_list(&refs, repo, "refs/got/worktree",
10826 got_ref_cmp_by_name, repo);
10830 TAILQ_FOREACH(re, &refs, entry) {
10831 const char *cmd, *refname, *type;
10833 refname = got_ref_get_name(re->ref);
10835 if (strncmp(refname, GOT_WORKTREE_CHERRYPICK_REF_PREFIX,
10836 GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN) == 0) {
10837 refname += GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN + 1;
10838 cmd = "cherrypick";
10839 type = "cherrypicked";
10840 } else if (strncmp(refname, GOT_WORKTREE_BACKOUT_REF_PREFIX,
10841 GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN) == 0) {
10842 refname += GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN + 1;
10844 type = "backed-out";
10848 if (strncmp(refname, uuidstr, GOT_WORKTREE_UUID_STRLEN) != 0)
10851 snprintf(msg, sizeof(msg),
10852 "work tree has references created by %s commits which "
10853 "must be removed with 'got %s -X' before running the %s "
10854 "command", type, cmd, caller);
10855 err = got_error_msg(GOT_ERR_WORKTREE_META, msg);
10861 got_ref_list_free(&refs);
10865 static const struct got_error *
10866 process_backup_refs(const char *backup_ref_prefix,
10867 const char *wanted_branch_name,
10868 int delete, struct got_repository *repo)
10870 const struct got_error *err;
10871 struct got_reflist_head refs, backup_refs;
10872 struct got_reflist_entry *re;
10873 const size_t backup_ref_prefix_len = strlen(backup_ref_prefix);
10874 struct got_object_id *old_commit_id = NULL;
10875 char *branch_name = NULL;
10876 struct got_commit_object *old_commit = NULL;
10877 struct got_reflist_object_id_map *refs_idmap = NULL;
10878 int wanted_branch_found = 0;
10881 TAILQ_INIT(&backup_refs);
10883 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
10887 err = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
10891 if (wanted_branch_name) {
10892 if (strncmp(wanted_branch_name, "refs/heads/", 11) == 0)
10893 wanted_branch_name += 11;
10896 err = got_ref_list(&backup_refs, repo, backup_ref_prefix,
10897 got_ref_cmp_by_commit_timestamp_descending, repo);
10901 TAILQ_FOREACH(re, &backup_refs, entry) {
10902 const char *refname = got_ref_get_name(re->ref);
10905 err = check_cancelled(NULL);
10909 err = got_ref_resolve(&old_commit_id, repo, re->ref);
10913 err = got_object_open_as_commit(&old_commit, repo,
10918 if (strncmp(backup_ref_prefix, refname,
10919 backup_ref_prefix_len) == 0)
10920 refname += backup_ref_prefix_len;
10922 while (refname[0] == '/')
10925 branch_name = strdup(refname);
10926 if (branch_name == NULL) {
10927 err = got_error_from_errno("strdup");
10930 slash = strrchr(branch_name, '/');
10933 refname += strlen(branch_name) + 1;
10936 if (wanted_branch_name == NULL ||
10937 strcmp(wanted_branch_name, branch_name) == 0) {
10938 wanted_branch_found = 1;
10940 err = delete_backup_ref(re->ref,
10941 old_commit_id, repo);
10943 err = print_backup_ref(branch_name, refname,
10944 old_commit_id, old_commit, refs_idmap,
10951 free(old_commit_id);
10952 old_commit_id = NULL;
10954 branch_name = NULL;
10955 got_object_commit_close(old_commit);
10959 if (wanted_branch_name && !wanted_branch_found) {
10960 err = got_error_fmt(GOT_ERR_NOT_REF,
10961 "%s/%s/", backup_ref_prefix, wanted_branch_name);
10965 got_reflist_object_id_map_free(refs_idmap);
10966 got_ref_list_free(&refs);
10967 got_ref_list_free(&backup_refs);
10968 free(old_commit_id);
10971 got_object_commit_close(old_commit);
10975 static const struct got_error *
10976 abort_progress(void *arg, unsigned char status, const char *path)
10979 * Unversioned files should not clutter progress output when
10980 * an operation is aborted.
10982 if (status == GOT_STATUS_UNVERSIONED)
10985 return update_progress(arg, status, path);
10988 static const struct got_error *
10989 cmd_rebase(int argc, char *argv[])
10991 const struct got_error *error = NULL;
10992 struct got_worktree *worktree = NULL;
10993 struct got_repository *repo = NULL;
10994 struct got_fileindex *fileindex = NULL;
10995 char *cwd = NULL, *committer = NULL, *gitconfig_path = NULL;
10996 struct got_reference *branch = NULL;
10997 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
10998 struct got_object_id *commit_id = NULL, *parent_id = NULL;
10999 struct got_object_id *resume_commit_id = NULL;
11000 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
11001 struct got_object_id *head_commit_id = NULL;
11002 struct got_reference *head_ref = NULL;
11003 struct got_commit_object *commit = NULL;
11004 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
11005 int histedit_in_progress = 0, merge_in_progress = 0;
11006 int create_backup = 1, list_backups = 0, delete_backups = 0;
11007 int allow_conflict = 0;
11008 struct got_object_id_queue commits;
11009 struct got_pathlist_head merged_paths;
11010 const struct got_object_id_queue *parent_ids;
11011 struct got_object_qid *qid, *pid;
11012 struct got_update_progress_arg upa;
11013 int *pack_fds = NULL;
11015 STAILQ_INIT(&commits);
11016 TAILQ_INIT(&merged_paths);
11017 memset(&upa, 0, sizeof(upa));
11020 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
11021 "unveil", NULL) == -1)
11025 while ((ch = getopt(argc, argv, "aCclX")) != -1) {
11031 allow_conflict = 1;
11034 continue_rebase = 1;
11040 delete_backups = 1;
11051 if (list_backups) {
11053 option_conflict('l', 'a');
11054 if (allow_conflict)
11055 option_conflict('l', 'C');
11056 if (continue_rebase)
11057 option_conflict('l', 'c');
11058 if (delete_backups)
11059 option_conflict('l', 'X');
11060 if (argc != 0 && argc != 1)
11062 } else if (delete_backups) {
11064 option_conflict('X', 'a');
11065 if (allow_conflict)
11066 option_conflict('X', 'C');
11067 if (continue_rebase)
11068 option_conflict('X', 'c');
11070 option_conflict('l', 'X');
11071 if (argc != 0 && argc != 1)
11073 } else if (allow_conflict) {
11075 option_conflict('C', 'a');
11076 if (!continue_rebase)
11077 errx(1, "-C option requires -c");
11079 if (abort_rebase && continue_rebase)
11081 else if (abort_rebase || continue_rebase) {
11084 } else if (argc != 1)
11088 cwd = getcwd(NULL, 0);
11090 error = got_error_from_errno("getcwd");
11094 error = got_repo_pack_fds_open(&pack_fds);
11098 error = got_worktree_open(&worktree, cwd);
11100 if (list_backups || delete_backups) {
11101 if (error->code != GOT_ERR_NOT_WORKTREE)
11104 if (error->code == GOT_ERR_NOT_WORKTREE)
11105 error = wrap_not_worktree_error(error,
11111 error = get_gitconfig_path(&gitconfig_path);
11114 error = got_repo_open(&repo,
11115 worktree ? got_worktree_get_repo_path(worktree) : cwd,
11116 gitconfig_path, pack_fds);
11120 if (worktree != NULL && !list_backups && !delete_backups) {
11121 error = worktree_has_logmsg_ref("rebase", worktree, repo);
11126 error = get_author(&committer, repo, worktree);
11127 if (error && error->code != GOT_ERR_COMMIT_NO_AUTHOR)
11130 error = apply_unveil(got_repo_get_path(repo), 0,
11131 worktree ? got_worktree_get_root_path(worktree) : NULL);
11135 if (list_backups || delete_backups) {
11136 error = process_backup_refs(
11137 GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
11138 argc == 1 ? argv[0] : NULL, delete_backups, repo);
11139 goto done; /* nothing else to do */
11142 error = got_worktree_histedit_in_progress(&histedit_in_progress,
11146 if (histedit_in_progress) {
11147 error = got_error(GOT_ERR_HISTEDIT_BUSY);
11151 error = got_worktree_merge_in_progress(&merge_in_progress,
11155 if (merge_in_progress) {
11156 error = got_error(GOT_ERR_MERGE_BUSY);
11160 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
11164 if (abort_rebase) {
11165 if (!rebase_in_progress) {
11166 error = got_error(GOT_ERR_NOT_REBASING);
11169 error = got_worktree_rebase_continue(&resume_commit_id,
11170 &new_base_branch, &tmp_branch, &branch, &fileindex,
11174 printf("Switching work tree to %s\n",
11175 got_ref_get_symref_target(new_base_branch));
11176 error = got_worktree_rebase_abort(worktree, fileindex, repo,
11177 new_base_branch, abort_progress, &upa);
11180 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
11181 print_merge_progress_stats(&upa);
11182 goto done; /* nothing else to do */
11185 if (continue_rebase) {
11186 if (!rebase_in_progress) {
11187 error = got_error(GOT_ERR_NOT_REBASING);
11190 error = got_worktree_rebase_continue(&resume_commit_id,
11191 &new_base_branch, &tmp_branch, &branch, &fileindex,
11196 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
11197 committer, resume_commit_id, allow_conflict, repo);
11201 yca_id = got_object_id_dup(resume_commit_id);
11202 if (yca_id == NULL) {
11203 error = got_error_from_errno("got_object_id_dup");
11207 error = got_ref_open(&branch, repo, argv[0], 0);
11210 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
11211 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
11212 "will not rebase a branch which lives outside "
11213 "the \"refs/heads/\" reference namespace");
11218 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
11222 if (!continue_rebase) {
11223 struct got_object_id *base_commit_id;
11225 error = got_ref_open(&head_ref, repo,
11226 got_worktree_get_head_ref_name(worktree), 0);
11229 error = got_ref_resolve(&head_commit_id, repo, head_ref);
11232 base_commit_id = got_worktree_get_base_commit_id(worktree);
11233 if (got_object_id_cmp(base_commit_id, head_commit_id) != 0) {
11234 error = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
11238 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
11239 base_commit_id, branch_head_commit_id, 1, repo,
11240 check_cancelled, NULL);
11242 if (error->code == GOT_ERR_ANCESTRY) {
11243 error = got_error_msg(GOT_ERR_ANCESTRY,
11244 "specified branch shares no common "
11245 "ancestry with work tree's branch");
11250 error = check_same_branch(base_commit_id, branch, yca_id, repo);
11252 if (error->code != GOT_ERR_ANCESTRY)
11256 struct got_pathlist_head paths;
11257 printf("%s is already based on %s\n",
11258 got_ref_get_name(branch),
11259 got_worktree_get_head_ref_name(worktree));
11260 error = switch_head_ref(branch, branch_head_commit_id,
11264 error = got_worktree_set_base_commit_id(worktree, repo,
11265 branch_head_commit_id);
11268 TAILQ_INIT(&paths);
11269 error = got_pathlist_append(&paths, "", NULL);
11272 error = got_worktree_checkout_files(worktree,
11273 &paths, repo, update_progress, &upa,
11274 check_cancelled, NULL);
11275 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
11278 if (upa.did_something) {
11280 error = got_object_id_str(&id_str,
11281 branch_head_commit_id);
11284 printf("Updated to %s: %s\n",
11285 got_worktree_get_head_ref_name(worktree),
11289 printf("Already up-to-date\n");
11290 print_update_progress_stats(&upa);
11295 commit_id = branch_head_commit_id;
11296 error = got_object_open_as_commit(&commit, repo, commit_id);
11300 parent_ids = got_object_commit_get_parent_ids(commit);
11301 pid = STAILQ_FIRST(parent_ids);
11303 error = collect_commits(&commits, commit_id, &pid->id,
11304 yca_id, got_worktree_get_path_prefix(worktree),
11305 GOT_ERR_REBASE_PATH, repo);
11310 got_object_commit_close(commit);
11313 if (!continue_rebase) {
11314 error = got_worktree_rebase_prepare(&new_base_branch,
11315 &tmp_branch, &fileindex, worktree, branch, repo);
11320 if (STAILQ_EMPTY(&commits)) {
11321 if (continue_rebase) {
11322 error = rebase_complete(worktree, fileindex,
11323 branch, tmp_branch, repo, create_backup);
11326 /* Fast-forward the reference of the branch. */
11327 struct got_object_id *new_head_commit_id;
11329 error = got_ref_resolve(&new_head_commit_id, repo,
11333 error = got_object_id_str(&id_str, new_head_commit_id);
11336 printf("Forwarding %s to commit %s\n",
11337 got_ref_get_name(branch), id_str);
11339 error = got_ref_change_ref(branch,
11340 new_head_commit_id);
11343 /* No backup needed since objects did not change. */
11349 STAILQ_FOREACH(qid, &commits, entry) {
11351 commit_id = &qid->id;
11352 parent_id = pid ? &pid->id : yca_id;
11355 memset(&upa, 0, sizeof(upa));
11356 error = got_worktree_rebase_merge_files(&merged_paths,
11357 worktree, fileindex, parent_id, commit_id, repo,
11358 update_progress, &upa, check_cancelled, NULL);
11362 print_merge_progress_stats(&upa);
11363 if (upa.conflicts > 0 || upa.missing > 0 ||
11364 upa.not_deleted > 0 || upa.unversioned > 0) {
11365 if (upa.conflicts > 0) {
11366 error = show_rebase_merge_conflict(&qid->id,
11371 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
11375 error = rebase_commit(&merged_paths, worktree, fileindex,
11376 tmp_branch, committer, commit_id, 0, repo);
11377 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
11382 if (upa.conflicts > 0 || upa.missing > 0 ||
11383 upa.not_deleted > 0 || upa.unversioned > 0) {
11384 error = got_worktree_rebase_postpone(worktree, fileindex);
11387 if (upa.conflicts > 0 && upa.missing == 0 &&
11388 upa.not_deleted == 0 && upa.unversioned == 0) {
11389 error = got_error_msg(GOT_ERR_CONFLICTS,
11390 "conflicts must be resolved before rebasing "
11392 } else if (upa.conflicts > 0) {
11393 error = got_error_msg(GOT_ERR_CONFLICTS,
11394 "conflicts must be resolved before rebasing "
11395 "can continue; changes destined for some "
11396 "files were not yet merged and should be "
11397 "merged manually if required before the "
11398 "rebase operation is continued");
11400 error = got_error_msg(GOT_ERR_CONFLICTS,
11401 "changes destined for some files were not "
11402 "yet merged and should be merged manually "
11403 "if required before the rebase operation "
11407 error = rebase_complete(worktree, fileindex, branch,
11408 tmp_branch, repo, create_backup);
11412 free(gitconfig_path);
11413 got_object_id_queue_free(&commits);
11414 free(branch_head_commit_id);
11415 free(resume_commit_id);
11416 free(head_commit_id);
11419 got_object_commit_close(commit);
11421 got_ref_close(branch);
11422 if (new_base_branch)
11423 got_ref_close(new_base_branch);
11425 got_ref_close(tmp_branch);
11427 got_ref_close(head_ref);
11429 got_worktree_close(worktree);
11431 const struct got_error *close_err = got_repo_close(repo);
11436 const struct got_error *pack_err =
11437 got_repo_pack_fds_close(pack_fds);
11445 usage_histedit(void)
11447 fprintf(stderr, "usage: %s histedit [-aCcdeflmX] [-F histedit-script] "
11448 "[branch]\n", getprogname());
11452 #define GOT_HISTEDIT_PICK 'p'
11453 #define GOT_HISTEDIT_EDIT 'e'
11454 #define GOT_HISTEDIT_FOLD 'f'
11455 #define GOT_HISTEDIT_DROP 'd'
11456 #define GOT_HISTEDIT_MESG 'm'
11458 static const struct got_histedit_cmd {
11459 unsigned char code;
11462 } got_histedit_cmds[] = {
11463 { GOT_HISTEDIT_PICK, "pick", "use commit" },
11464 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
11465 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
11467 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
11468 { GOT_HISTEDIT_MESG, "mesg",
11469 "single-line log message for commit above (open editor if empty)" },
11472 struct got_histedit_list_entry {
11473 TAILQ_ENTRY(got_histedit_list_entry) entry;
11474 struct got_object_id *commit_id;
11475 const struct got_histedit_cmd *cmd;
11478 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
11480 static const struct got_error *
11481 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
11482 FILE *f, struct got_repository *repo)
11484 const struct got_error *err = NULL;
11485 char *logmsg = NULL, *id_str = NULL;
11486 struct got_commit_object *commit = NULL;
11489 err = got_object_open_as_commit(&commit, repo, commit_id);
11493 err = get_short_logmsg(&logmsg, 34, commit);
11497 err = got_object_id_str(&id_str, commit_id);
11501 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
11503 err = got_ferror(f, GOT_ERR_IO);
11506 got_object_commit_close(commit);
11512 static const struct got_error *
11513 histedit_write_commit_list(struct got_object_id_queue *commits,
11514 FILE *f, int edit_logmsg_only, int fold_only, int drop_only,
11515 int edit_only, struct got_repository *repo)
11517 const struct got_error *err = NULL;
11518 struct got_object_qid *qid;
11519 const char *histedit_cmd = NULL;
11521 if (STAILQ_EMPTY(commits))
11522 return got_error(GOT_ERR_EMPTY_HISTEDIT);
11524 STAILQ_FOREACH(qid, commits, entry) {
11525 histedit_cmd = got_histedit_cmds[0].name;
11527 histedit_cmd = "drop";
11528 else if (edit_only)
11529 histedit_cmd = "edit";
11530 else if (fold_only && STAILQ_NEXT(qid, entry) != NULL)
11531 histedit_cmd = "fold";
11532 err = histedit_write_commit(&qid->id, histedit_cmd, f, repo);
11535 if (edit_logmsg_only) {
11536 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
11538 err = got_ferror(f, GOT_ERR_IO);
11547 static const struct got_error *
11548 write_cmd_list(FILE *f, const char *branch_name,
11549 struct got_object_id_queue *commits)
11551 const struct got_error *err = NULL;
11555 struct got_object_qid *qid;
11557 qid = STAILQ_FIRST(commits);
11558 err = got_object_id_str(&id_str, &qid->id);
11563 "# Editing the history of branch '%s' starting at\n"
11565 "# Commits will be processed in order from top to "
11566 "bottom of this file.\n", branch_name, id_str);
11568 err = got_ferror(f, GOT_ERR_IO);
11572 n = fprintf(f, "# Available histedit commands:\n");
11574 err = got_ferror(f, GOT_ERR_IO);
11578 for (i = 0; i < nitems(got_histedit_cmds); i++) {
11579 const struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
11580 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
11583 err = got_ferror(f, GOT_ERR_IO);
11592 static const struct got_error *
11593 histedit_syntax_error(int lineno)
11595 static char msg[42];
11598 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
11600 if (ret < 0 || (size_t)ret >= sizeof(msg))
11601 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
11603 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
11606 static const struct got_error *
11607 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
11608 char *logmsg, struct got_repository *repo)
11610 const struct got_error *err;
11611 struct got_commit_object *folded_commit = NULL;
11612 char *id_str, *folded_logmsg = NULL;
11614 err = got_object_id_str(&id_str, hle->commit_id);
11618 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
11622 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
11625 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
11626 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
11627 folded_logmsg) == -1) {
11628 err = got_error_from_errno("asprintf");
11632 got_object_commit_close(folded_commit);
11634 free(folded_logmsg);
11638 static struct got_histedit_list_entry *
11639 get_folded_commits(struct got_histedit_list_entry *hle)
11641 struct got_histedit_list_entry *prev, *folded = NULL;
11643 prev = TAILQ_PREV(hle, got_histedit_list, entry);
11644 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
11645 prev->cmd->code == GOT_HISTEDIT_DROP)) {
11646 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
11648 prev = TAILQ_PREV(prev, got_histedit_list, entry);
11654 static const struct got_error *
11655 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
11656 struct got_repository *repo)
11658 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
11659 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
11660 const struct got_error *err = NULL;
11661 struct got_commit_object *commit = NULL;
11664 struct got_histedit_list_entry *folded = NULL;
11666 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
11670 folded = get_folded_commits(hle);
11672 while (folded != hle) {
11673 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
11674 folded = TAILQ_NEXT(folded, entry);
11677 err = append_folded_commit_msg(&new_msg, folded,
11683 folded = TAILQ_NEXT(folded, entry);
11687 err = got_object_id_str(&id_str, hle->commit_id);
11690 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
11693 logmsg_len = asprintf(&new_msg,
11694 "%s\n# original log message of commit %s: %s",
11695 logmsg ? logmsg : "", id_str, orig_logmsg);
11696 if (logmsg_len == -1) {
11697 err = got_error_from_errno("asprintf");
11703 err = got_object_id_str(&id_str, hle->commit_id);
11707 err = got_opentemp_named_fd(&logmsg_path, &fd,
11708 GOT_TMPDIR_STR "/got-logmsg", "");
11712 write(fd, logmsg, logmsg_len);
11715 err = get_editor(&editor);
11719 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg,
11722 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
11725 hle->logmsg = strdup(new_msg);
11726 if (hle->logmsg == NULL)
11727 err = got_error_from_errno("strdup");
11730 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
11731 err = got_error_from_errno2("unlink", logmsg_path);
11737 got_object_commit_close(commit);
11741 static const struct got_error *
11742 histedit_parse_list(struct got_histedit_list *histedit_cmds,
11743 FILE *f, struct got_repository *repo)
11745 const struct got_error *err = NULL;
11746 char *line = NULL, *p, *end;
11749 int lineno = 0, lastcmd = -1;
11750 const struct got_histedit_cmd *cmd;
11751 struct got_object_id *commit_id = NULL;
11752 struct got_histedit_list_entry *hle = NULL;
11755 len = getline(&line, &size, f);
11757 const struct got_error *getline_err;
11760 getline_err = got_error_from_errno("getline");
11761 err = got_ferror(f, getline_err->code);
11766 while (isspace((unsigned char)p[0]))
11768 if (p[0] == '#' || p[0] == '\0') {
11774 for (i = 0; i < nitems(got_histedit_cmds); i++) {
11775 cmd = &got_histedit_cmds[i];
11776 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
11777 isspace((unsigned char)p[strlen(cmd->name)])) {
11778 p += strlen(cmd->name);
11781 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
11786 if (i == nitems(got_histedit_cmds)) {
11787 err = histedit_syntax_error(lineno);
11790 while (isspace((unsigned char)p[0]))
11792 if (cmd->code == GOT_HISTEDIT_MESG) {
11793 if (lastcmd != GOT_HISTEDIT_PICK &&
11794 lastcmd != GOT_HISTEDIT_EDIT) {
11795 err = got_error(GOT_ERR_HISTEDIT_CMD);
11798 if (p[0] == '\0') {
11799 err = histedit_edit_logmsg(hle, repo);
11803 hle->logmsg = strdup(p);
11804 if (hle->logmsg == NULL) {
11805 err = got_error_from_errno("strdup");
11811 lastcmd = cmd->code;
11815 while (end[0] && !isspace((unsigned char)end[0]))
11819 err = got_object_resolve_id_str(&commit_id, repo, p);
11821 /* override error code */
11822 err = histedit_syntax_error(lineno);
11826 hle = malloc(sizeof(*hle));
11828 err = got_error_from_errno("malloc");
11832 hle->commit_id = commit_id;
11833 hle->logmsg = NULL;
11837 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
11838 lastcmd = cmd->code;
11846 static const struct got_error *
11847 histedit_check_script(struct got_histedit_list *histedit_cmds,
11848 struct got_object_id_queue *commits, struct got_repository *repo)
11850 const struct got_error *err = NULL;
11851 struct got_object_qid *qid;
11852 struct got_histedit_list_entry *hle;
11853 static char msg[92];
11856 if (TAILQ_EMPTY(histedit_cmds))
11857 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
11858 "histedit script contains no commands");
11859 if (STAILQ_EMPTY(commits))
11860 return got_error(GOT_ERR_EMPTY_HISTEDIT);
11862 TAILQ_FOREACH(hle, histedit_cmds, entry) {
11863 struct got_histedit_list_entry *hle2;
11864 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
11867 if (got_object_id_cmp(hle->commit_id,
11868 hle2->commit_id) != 0)
11870 err = got_object_id_str(&id_str, hle->commit_id);
11873 snprintf(msg, sizeof(msg), "commit %s is listed "
11874 "more than once in histedit script", id_str);
11876 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
11880 STAILQ_FOREACH(qid, commits, entry) {
11881 TAILQ_FOREACH(hle, histedit_cmds, entry) {
11882 if (got_object_id_cmp(&qid->id, hle->commit_id) == 0)
11886 err = got_object_id_str(&id_str, &qid->id);
11889 snprintf(msg, sizeof(msg),
11890 "commit %s missing from histedit script", id_str);
11892 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
11896 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
11897 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
11898 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
11899 "last commit in histedit script cannot be folded");
11904 static const struct got_error *
11905 histedit_run_editor(struct got_histedit_list *histedit_cmds,
11906 const char *path, struct got_object_id_queue *commits,
11907 struct got_repository *repo)
11909 const struct got_error *err = NULL;
11913 err = get_editor(&editor);
11917 if (spawn_editor(editor, path) == -1) {
11918 err = got_error_from_errno("failed spawning editor");
11922 f = fopen(path, "re");
11924 err = got_error_from_errno("fopen");
11927 err = histedit_parse_list(histedit_cmds, f, repo);
11931 err = histedit_check_script(histedit_cmds, commits, repo);
11933 if (f && fclose(f) == EOF && err == NULL)
11934 err = got_error_from_errno("fclose");
11939 static const struct got_error *
11940 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
11941 struct got_object_id_queue *, const char *, const char *,
11942 struct got_repository *);
11944 static const struct got_error *
11945 histedit_edit_script(struct got_histedit_list *histedit_cmds,
11946 struct got_object_id_queue *commits, const char *branch_name,
11947 int edit_logmsg_only, int fold_only, int drop_only, int edit_only,
11948 struct got_repository *repo)
11950 const struct got_error *err;
11954 err = got_opentemp_named(&path, &f, "got-histedit", "");
11958 err = write_cmd_list(f, branch_name, commits);
11962 err = histedit_write_commit_list(commits, f, edit_logmsg_only,
11963 fold_only, drop_only, edit_only, repo);
11967 if (drop_only || edit_logmsg_only || fold_only || edit_only) {
11969 err = histedit_parse_list(histedit_cmds, f, repo);
11971 if (fclose(f) == EOF) {
11972 err = got_error_from_errno("fclose");
11976 err = histedit_run_editor(histedit_cmds, path, commits, repo);
11978 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
11979 err->code != GOT_ERR_HISTEDIT_CMD)
11981 err = histedit_edit_list_retry(histedit_cmds, err,
11982 commits, path, branch_name, repo);
11986 if (f && fclose(f) == EOF && err == NULL)
11987 err = got_error_from_errno("fclose");
11988 if (path && unlink(path) != 0 && err == NULL)
11989 err = got_error_from_errno2("unlink", path);
11994 static const struct got_error *
11995 histedit_save_list(struct got_histedit_list *histedit_cmds,
11996 struct got_worktree *worktree, struct got_repository *repo)
11998 const struct got_error *err = NULL;
12001 struct got_histedit_list_entry *hle;
12002 struct got_commit_object *commit = NULL;
12004 err = got_worktree_get_histedit_script_path(&path, worktree);
12008 f = fopen(path, "we");
12010 err = got_error_from_errno2("fopen", path);
12013 TAILQ_FOREACH(hle, histedit_cmds, entry) {
12014 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
12020 int n = fprintf(f, "%c %s\n",
12021 GOT_HISTEDIT_MESG, hle->logmsg);
12023 err = got_ferror(f, GOT_ERR_IO);
12029 if (f && fclose(f) == EOF && err == NULL)
12030 err = got_error_from_errno("fclose");
12033 got_object_commit_close(commit);
12038 histedit_free_list(struct got_histedit_list *histedit_cmds)
12040 struct got_histedit_list_entry *hle;
12042 while ((hle = TAILQ_FIRST(histedit_cmds))) {
12043 TAILQ_REMOVE(histedit_cmds, hle, entry);
12048 static const struct got_error *
12049 histedit_load_list(struct got_histedit_list *histedit_cmds,
12050 const char *path, struct got_repository *repo)
12052 const struct got_error *err = NULL;
12055 f = fopen(path, "re");
12057 err = got_error_from_errno2("fopen", path);
12061 err = histedit_parse_list(histedit_cmds, f, repo);
12063 if (f && fclose(f) == EOF && err == NULL)
12064 err = got_error_from_errno("fclose");
12068 static const struct got_error *
12069 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
12070 const struct got_error *edit_err, struct got_object_id_queue *commits,
12071 const char *path, const char *branch_name, struct got_repository *repo)
12073 const struct got_error *err = NULL, *prev_err = edit_err;
12076 while (resp != 'c' && resp != 'r' && resp != 'a') {
12077 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
12078 "or (a)bort: ", getprogname(), prev_err->msg);
12083 histedit_free_list(histedit_cmds);
12084 err = histedit_run_editor(histedit_cmds, path, commits,
12087 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
12088 err->code != GOT_ERR_HISTEDIT_CMD)
12095 } else if (resp == 'r') {
12096 histedit_free_list(histedit_cmds);
12097 err = histedit_edit_script(histedit_cmds,
12098 commits, branch_name, 0, 0, 0, 0, repo);
12100 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
12101 err->code != GOT_ERR_HISTEDIT_CMD)
12108 } else if (resp == 'a') {
12109 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
12112 printf("invalid response '%c'\n", resp);
12118 static const struct got_error *
12119 histedit_complete(struct got_worktree *worktree,
12120 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
12121 struct got_reference *branch, struct got_repository *repo)
12123 printf("Switching work tree to %s\n",
12124 got_ref_get_symref_target(branch));
12125 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
12129 static const struct got_error *
12130 show_histedit_progress(struct got_commit_object *commit,
12131 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
12133 const struct got_error *err;
12134 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
12136 err = got_object_id_str(&old_id_str, hle->commit_id);
12141 err = got_object_id_str(&new_id_str, new_id);
12146 old_id_str[12] = '\0';
12148 new_id_str[12] = '\0';
12151 logmsg = strdup(hle->logmsg);
12152 if (logmsg == NULL) {
12153 err = got_error_from_errno("strdup");
12156 trim_logmsg(logmsg, 42);
12158 err = get_short_logmsg(&logmsg, 42, commit);
12163 switch (hle->cmd->code) {
12164 case GOT_HISTEDIT_PICK:
12165 case GOT_HISTEDIT_EDIT:
12166 printf("%s -> %s: %s\n", old_id_str,
12167 new_id_str ? new_id_str : "no-op change", logmsg);
12169 case GOT_HISTEDIT_DROP:
12170 case GOT_HISTEDIT_FOLD:
12171 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
12183 static const struct got_error *
12184 histedit_commit(struct got_pathlist_head *merged_paths,
12185 struct got_worktree *worktree, struct got_fileindex *fileindex,
12186 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
12187 const char *committer, int allow_conflict, struct got_repository *repo)
12189 const struct got_error *err;
12190 struct got_commit_object *commit;
12191 struct got_object_id *new_commit_id;
12193 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
12194 && hle->logmsg == NULL) {
12195 err = histedit_edit_logmsg(hle, repo);
12200 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
12204 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
12205 worktree, fileindex, tmp_branch, committer, commit, hle->commit_id,
12206 hle->logmsg, allow_conflict, repo);
12208 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
12210 err = show_histedit_progress(commit, hle, NULL);
12212 err = show_histedit_progress(commit, hle, new_commit_id);
12213 free(new_commit_id);
12216 got_object_commit_close(commit);
12220 static const struct got_error *
12221 histedit_skip_commit(struct got_histedit_list_entry *hle,
12222 struct got_worktree *worktree, struct got_repository *repo)
12224 const struct got_error *error;
12225 struct got_commit_object *commit;
12227 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
12232 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
12236 error = show_histedit_progress(commit, hle, NULL);
12237 got_object_commit_close(commit);
12241 static const struct got_error *
12242 check_local_changes(void *arg, unsigned char status,
12243 unsigned char staged_status, const char *path,
12244 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
12245 struct got_object_id *commit_id, int dirfd, const char *de_name)
12247 int *have_local_changes = arg;
12250 case GOT_STATUS_ADD:
12251 case GOT_STATUS_DELETE:
12252 case GOT_STATUS_MODIFY:
12253 case GOT_STATUS_CONFLICT:
12254 *have_local_changes = 1;
12255 return got_error(GOT_ERR_CANCELLED);
12260 switch (staged_status) {
12261 case GOT_STATUS_ADD:
12262 case GOT_STATUS_DELETE:
12263 case GOT_STATUS_MODIFY:
12264 *have_local_changes = 1;
12265 return got_error(GOT_ERR_CANCELLED);
12273 static const struct got_error *
12274 cmd_histedit(int argc, char *argv[])
12276 const struct got_error *error = NULL;
12277 struct got_worktree *worktree = NULL;
12278 struct got_fileindex *fileindex = NULL;
12279 struct got_repository *repo = NULL;
12280 char *cwd = NULL, *committer = NULL, *gitconfig_path = NULL;
12281 struct got_reference *branch = NULL;
12282 struct got_reference *tmp_branch = NULL;
12283 struct got_object_id *resume_commit_id = NULL;
12284 struct got_object_id *base_commit_id = NULL;
12285 struct got_object_id *head_commit_id = NULL;
12286 struct got_commit_object *commit = NULL;
12287 int ch, rebase_in_progress = 0, merge_in_progress = 0;
12288 struct got_update_progress_arg upa;
12289 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
12290 int drop_only = 0, edit_logmsg_only = 0, fold_only = 0, edit_only = 0;
12291 int allow_conflict = 0, list_backups = 0, delete_backups = 0;
12292 const char *edit_script_path = NULL;
12293 struct got_object_id_queue commits;
12294 struct got_pathlist_head merged_paths;
12295 const struct got_object_id_queue *parent_ids;
12296 struct got_object_qid *pid;
12297 struct got_histedit_list histedit_cmds;
12298 struct got_histedit_list_entry *hle;
12299 int *pack_fds = NULL;
12301 STAILQ_INIT(&commits);
12302 TAILQ_INIT(&histedit_cmds);
12303 TAILQ_INIT(&merged_paths);
12304 memset(&upa, 0, sizeof(upa));
12307 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
12308 "unveil", NULL) == -1)
12312 while ((ch = getopt(argc, argv, "aCcdeF:flmX")) != -1) {
12318 allow_conflict = 1;
12330 edit_script_path = optarg;
12339 edit_logmsg_only = 1;
12342 delete_backups = 1;
12353 if (abort_edit && allow_conflict)
12354 option_conflict('a', 'C');
12355 if (abort_edit && continue_edit)
12356 option_conflict('a', 'c');
12357 if (edit_script_path && allow_conflict)
12358 option_conflict('F', 'C');
12359 if (edit_script_path && edit_logmsg_only)
12360 option_conflict('F', 'm');
12361 if (abort_edit && edit_logmsg_only)
12362 option_conflict('a', 'm');
12363 if (edit_logmsg_only && allow_conflict)
12364 option_conflict('m', 'C');
12365 if (continue_edit && edit_logmsg_only)
12366 option_conflict('c', 'm');
12367 if (abort_edit && fold_only)
12368 option_conflict('a', 'f');
12369 if (fold_only && allow_conflict)
12370 option_conflict('f', 'C');
12371 if (continue_edit && fold_only)
12372 option_conflict('c', 'f');
12373 if (fold_only && edit_logmsg_only)
12374 option_conflict('f', 'm');
12375 if (edit_script_path && fold_only)
12376 option_conflict('F', 'f');
12377 if (abort_edit && edit_only)
12378 option_conflict('a', 'e');
12379 if (continue_edit && edit_only)
12380 option_conflict('c', 'e');
12381 if (edit_only && edit_logmsg_only)
12382 option_conflict('e', 'm');
12383 if (edit_script_path && edit_only)
12384 option_conflict('F', 'e');
12385 if (fold_only && edit_only)
12386 option_conflict('f', 'e');
12387 if (drop_only && abort_edit)
12388 option_conflict('d', 'a');
12389 if (drop_only && allow_conflict)
12390 option_conflict('d', 'C');
12391 if (drop_only && continue_edit)
12392 option_conflict('d', 'c');
12393 if (drop_only && edit_logmsg_only)
12394 option_conflict('d', 'm');
12395 if (drop_only && edit_only)
12396 option_conflict('d', 'e');
12397 if (drop_only && edit_script_path)
12398 option_conflict('d', 'F');
12399 if (drop_only && fold_only)
12400 option_conflict('d', 'f');
12401 if (list_backups) {
12403 option_conflict('l', 'a');
12404 if (allow_conflict)
12405 option_conflict('l', 'C');
12407 option_conflict('l', 'c');
12408 if (edit_script_path)
12409 option_conflict('l', 'F');
12410 if (edit_logmsg_only)
12411 option_conflict('l', 'm');
12413 option_conflict('l', 'd');
12415 option_conflict('l', 'f');
12417 option_conflict('l', 'e');
12418 if (delete_backups)
12419 option_conflict('l', 'X');
12420 if (argc != 0 && argc != 1)
12422 } else if (delete_backups) {
12424 option_conflict('X', 'a');
12425 if (allow_conflict)
12426 option_conflict('X', 'C');
12428 option_conflict('X', 'c');
12430 option_conflict('X', 'd');
12431 if (edit_script_path)
12432 option_conflict('X', 'F');
12433 if (edit_logmsg_only)
12434 option_conflict('X', 'm');
12436 option_conflict('X', 'f');
12438 option_conflict('X', 'e');
12440 option_conflict('X', 'l');
12441 if (argc != 0 && argc != 1)
12443 } else if (allow_conflict && !continue_edit)
12444 errx(1, "-C option requires -c");
12445 else if (argc != 0)
12449 * This command cannot apply unveil(2) in all cases because the
12450 * user may choose to run an editor to edit the histedit script
12451 * and to edit individual commit log messages.
12452 * unveil(2) traverses exec(2); if an editor is used we have to
12453 * apply unveil after edit script and log messages have been written.
12454 * XXX TODO: Make use of unveil(2) where possible.
12457 cwd = getcwd(NULL, 0);
12459 error = got_error_from_errno("getcwd");
12463 error = got_repo_pack_fds_open(&pack_fds);
12467 error = got_worktree_open(&worktree, cwd);
12469 if (list_backups || delete_backups) {
12470 if (error->code != GOT_ERR_NOT_WORKTREE)
12473 if (error->code == GOT_ERR_NOT_WORKTREE)
12474 error = wrap_not_worktree_error(error,
12480 if (list_backups || delete_backups) {
12481 error = got_repo_open(&repo,
12482 worktree ? got_worktree_get_repo_path(worktree) : cwd,
12486 error = apply_unveil(got_repo_get_path(repo), 0,
12487 worktree ? got_worktree_get_root_path(worktree) : NULL);
12490 error = process_backup_refs(
12491 GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
12492 argc == 1 ? argv[0] : NULL, delete_backups, repo);
12493 goto done; /* nothing else to do */
12496 error = get_gitconfig_path(&gitconfig_path);
12499 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
12500 gitconfig_path, pack_fds);
12504 if (worktree != NULL && !list_backups && !delete_backups) {
12505 error = worktree_has_logmsg_ref("histedit", worktree, repo);
12510 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
12513 if (rebase_in_progress) {
12514 error = got_error(GOT_ERR_REBASING);
12518 error = got_worktree_merge_in_progress(&merge_in_progress, worktree,
12522 if (merge_in_progress) {
12523 error = got_error(GOT_ERR_MERGE_BUSY);
12527 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
12531 if (edit_in_progress && edit_logmsg_only) {
12532 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
12533 "histedit operation is in progress in this "
12534 "work tree and must be continued or aborted "
12535 "before the -m option can be used");
12538 if (edit_in_progress && drop_only) {
12539 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
12540 "histedit operation is in progress in this "
12541 "work tree and must be continued or aborted "
12542 "before the -d option can be used");
12545 if (edit_in_progress && fold_only) {
12546 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
12547 "histedit operation is in progress in this "
12548 "work tree and must be continued or aborted "
12549 "before the -f option can be used");
12552 if (edit_in_progress && edit_only) {
12553 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
12554 "histedit operation is in progress in this "
12555 "work tree and must be continued or aborted "
12556 "before the -e option can be used");
12560 if (edit_in_progress && abort_edit) {
12561 error = got_worktree_histedit_continue(&resume_commit_id,
12562 &tmp_branch, &branch, &base_commit_id, &fileindex,
12566 printf("Switching work tree to %s\n",
12567 got_ref_get_symref_target(branch));
12568 error = got_worktree_histedit_abort(worktree, fileindex, repo,
12569 branch, base_commit_id, abort_progress, &upa);
12572 printf("Histedit of %s aborted\n",
12573 got_ref_get_symref_target(branch));
12574 print_merge_progress_stats(&upa);
12575 goto done; /* nothing else to do */
12576 } else if (abort_edit) {
12577 error = got_error(GOT_ERR_NOT_HISTEDIT);
12581 error = get_author(&committer, repo, worktree);
12585 if (continue_edit) {
12588 if (!edit_in_progress) {
12589 error = got_error(GOT_ERR_NOT_HISTEDIT);
12593 error = got_worktree_get_histedit_script_path(&path, worktree);
12597 error = histedit_load_list(&histedit_cmds, path, repo);
12602 error = got_worktree_histedit_continue(&resume_commit_id,
12603 &tmp_branch, &branch, &base_commit_id, &fileindex,
12608 error = got_ref_resolve(&head_commit_id, repo, branch);
12612 error = got_object_open_as_commit(&commit, repo,
12616 parent_ids = got_object_commit_get_parent_ids(commit);
12617 pid = STAILQ_FIRST(parent_ids);
12619 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
12622 error = collect_commits(&commits, head_commit_id, &pid->id,
12623 base_commit_id, got_worktree_get_path_prefix(worktree),
12624 GOT_ERR_HISTEDIT_PATH, repo);
12625 got_object_commit_close(commit);
12630 if (edit_in_progress) {
12631 error = got_error(GOT_ERR_HISTEDIT_BUSY);
12635 error = got_ref_open(&branch, repo,
12636 got_worktree_get_head_ref_name(worktree), 0);
12640 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
12641 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
12642 "will not edit commit history of a branch outside "
12643 "the \"refs/heads/\" reference namespace");
12647 error = got_ref_resolve(&head_commit_id, repo, branch);
12648 got_ref_close(branch);
12653 error = got_object_open_as_commit(&commit, repo,
12657 parent_ids = got_object_commit_get_parent_ids(commit);
12658 pid = STAILQ_FIRST(parent_ids);
12660 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
12663 error = collect_commits(&commits, head_commit_id, &pid->id,
12664 got_worktree_get_base_commit_id(worktree),
12665 got_worktree_get_path_prefix(worktree),
12666 GOT_ERR_HISTEDIT_PATH, repo);
12667 got_object_commit_close(commit);
12672 if (STAILQ_EMPTY(&commits)) {
12673 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
12677 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
12678 &base_commit_id, &fileindex, worktree, repo);
12682 if (edit_script_path) {
12683 error = histedit_load_list(&histedit_cmds,
12684 edit_script_path, repo);
12686 got_worktree_histedit_abort(worktree, fileindex,
12687 repo, branch, base_commit_id,
12688 abort_progress, &upa);
12689 print_merge_progress_stats(&upa);
12693 const char *branch_name;
12694 branch_name = got_ref_get_symref_target(branch);
12695 if (strncmp(branch_name, "refs/heads/", 11) == 0)
12697 error = histedit_edit_script(&histedit_cmds, &commits,
12698 branch_name, edit_logmsg_only, fold_only,
12699 drop_only, edit_only, repo);
12701 got_worktree_histedit_abort(worktree, fileindex,
12702 repo, branch, base_commit_id,
12703 abort_progress, &upa);
12704 print_merge_progress_stats(&upa);
12710 error = histedit_save_list(&histedit_cmds, worktree,
12713 got_worktree_histedit_abort(worktree, fileindex,
12714 repo, branch, base_commit_id,
12715 abort_progress, &upa);
12716 print_merge_progress_stats(&upa);
12722 error = histedit_check_script(&histedit_cmds, &commits, repo);
12726 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
12727 if (resume_commit_id) {
12728 if (got_object_id_cmp(hle->commit_id,
12729 resume_commit_id) != 0)
12732 resume_commit_id = NULL;
12733 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
12734 hle->cmd->code == GOT_HISTEDIT_FOLD) {
12735 error = histedit_skip_commit(hle, worktree,
12740 struct got_pathlist_head paths;
12741 int have_changes = 0;
12743 TAILQ_INIT(&paths);
12744 error = got_pathlist_append(&paths, "", NULL);
12747 error = got_worktree_status(worktree, &paths,
12748 repo, 0, check_local_changes, &have_changes,
12749 check_cancelled, NULL);
12750 got_pathlist_free(&paths,
12751 GOT_PATHLIST_FREE_NONE);
12753 if (error->code != GOT_ERR_CANCELLED)
12755 if (sigint_received || sigpipe_received)
12758 if (have_changes) {
12759 error = histedit_commit(NULL, worktree,
12760 fileindex, tmp_branch, hle,
12761 committer, allow_conflict, repo);
12765 error = got_object_open_as_commit(
12766 &commit, repo, hle->commit_id);
12769 error = show_histedit_progress(commit,
12771 got_object_commit_close(commit);
12780 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
12781 error = histedit_skip_commit(hle, worktree, repo);
12787 error = got_object_open_as_commit(&commit, repo,
12791 parent_ids = got_object_commit_get_parent_ids(commit);
12792 pid = STAILQ_FIRST(parent_ids);
12794 error = got_worktree_histedit_merge_files(&merged_paths,
12795 worktree, fileindex, &pid->id, hle->commit_id, repo,
12796 update_progress, &upa, check_cancelled, NULL);
12799 got_object_commit_close(commit);
12802 print_merge_progress_stats(&upa);
12803 if (upa.conflicts > 0 || upa.missing > 0 ||
12804 upa.not_deleted > 0 || upa.unversioned > 0) {
12805 if (upa.conflicts > 0) {
12806 error = show_rebase_merge_conflict(
12807 hle->commit_id, repo);
12811 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
12815 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
12817 error = got_object_id_str(&id_str, hle->commit_id);
12820 printf("Stopping histedit for amending commit %s\n",
12823 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
12824 error = got_worktree_histedit_postpone(worktree,
12829 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
12830 error = histedit_skip_commit(hle, worktree, repo);
12836 error = histedit_commit(&merged_paths, worktree, fileindex,
12837 tmp_branch, hle, committer, allow_conflict, repo);
12838 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
12843 if (upa.conflicts > 0 || upa.missing > 0 ||
12844 upa.not_deleted > 0 || upa.unversioned > 0) {
12845 error = got_worktree_histedit_postpone(worktree, fileindex);
12848 if (upa.conflicts > 0 && upa.missing == 0 &&
12849 upa.not_deleted == 0 && upa.unversioned == 0) {
12850 error = got_error_msg(GOT_ERR_CONFLICTS,
12851 "conflicts must be resolved before histedit "
12853 } else if (upa.conflicts > 0) {
12854 error = got_error_msg(GOT_ERR_CONFLICTS,
12855 "conflicts must be resolved before histedit "
12856 "can continue; changes destined for some "
12857 "files were not yet merged and should be "
12858 "merged manually if required before the "
12859 "histedit operation is continued");
12861 error = got_error_msg(GOT_ERR_CONFLICTS,
12862 "changes destined for some files were not "
12863 "yet merged and should be merged manually "
12864 "if required before the histedit operation "
12868 error = histedit_complete(worktree, fileindex, tmp_branch,
12873 free(gitconfig_path);
12874 got_object_id_queue_free(&commits);
12875 histedit_free_list(&histedit_cmds);
12876 free(head_commit_id);
12877 free(base_commit_id);
12878 free(resume_commit_id);
12880 got_object_commit_close(commit);
12882 got_ref_close(branch);
12884 got_ref_close(tmp_branch);
12886 got_worktree_close(worktree);
12888 const struct got_error *close_err = got_repo_close(repo);
12893 const struct got_error *pack_err =
12894 got_repo_pack_fds_close(pack_fds);
12902 usage_integrate(void)
12904 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
12908 static const struct got_error *
12909 cmd_integrate(int argc, char *argv[])
12911 const struct got_error *error = NULL;
12912 struct got_repository *repo = NULL;
12913 struct got_worktree *worktree = NULL;
12914 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
12915 const char *branch_arg = NULL;
12916 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
12917 struct got_fileindex *fileindex = NULL;
12918 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
12920 struct got_update_progress_arg upa;
12921 int *pack_fds = NULL;
12924 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
12925 "unveil", NULL) == -1)
12929 while ((ch = getopt(argc, argv, "")) != -1) {
12942 branch_arg = argv[0];
12944 cwd = getcwd(NULL, 0);
12946 error = got_error_from_errno("getcwd");
12950 error = got_repo_pack_fds_open(&pack_fds);
12954 error = got_worktree_open(&worktree, cwd);
12956 if (error->code == GOT_ERR_NOT_WORKTREE)
12957 error = wrap_not_worktree_error(error, "integrate",
12962 error = check_rebase_or_histedit_in_progress(worktree);
12966 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
12971 error = apply_unveil(got_repo_get_path(repo), 0,
12972 got_worktree_get_root_path(worktree));
12976 error = check_merge_in_progress(worktree, repo);
12980 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
12981 error = got_error_from_errno("asprintf");
12985 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
12986 &base_branch_ref, worktree, refname, repo);
12990 refname = strdup(got_ref_get_name(branch_ref));
12991 if (refname == NULL) {
12992 error = got_error_from_errno("strdup");
12993 got_worktree_integrate_abort(worktree, fileindex, repo,
12994 branch_ref, base_branch_ref);
12997 base_refname = strdup(got_ref_get_name(base_branch_ref));
12998 if (base_refname == NULL) {
12999 error = got_error_from_errno("strdup");
13000 got_worktree_integrate_abort(worktree, fileindex, repo,
13001 branch_ref, base_branch_ref);
13004 if (strncmp(base_refname, "refs/heads/", 11) != 0) {
13005 error = got_error(GOT_ERR_INTEGRATE_BRANCH);
13006 got_worktree_integrate_abort(worktree, fileindex, repo,
13007 branch_ref, base_branch_ref);
13011 error = got_ref_resolve(&commit_id, repo, branch_ref);
13015 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
13019 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
13020 error = got_error_msg(GOT_ERR_SAME_BRANCH,
13021 "specified branch has already been integrated");
13022 got_worktree_integrate_abort(worktree, fileindex, repo,
13023 branch_ref, base_branch_ref);
13027 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
13029 if (error->code == GOT_ERR_ANCESTRY)
13030 error = got_error(GOT_ERR_REBASE_REQUIRED);
13031 got_worktree_integrate_abort(worktree, fileindex, repo,
13032 branch_ref, base_branch_ref);
13036 memset(&upa, 0, sizeof(upa));
13037 error = got_worktree_integrate_continue(worktree, fileindex, repo,
13038 branch_ref, base_branch_ref, update_progress, &upa,
13039 check_cancelled, NULL);
13043 printf("Integrated %s into %s\n", refname, base_refname);
13044 print_update_progress_stats(&upa);
13047 const struct got_error *close_err = got_repo_close(repo);
13052 got_worktree_close(worktree);
13054 const struct got_error *pack_err =
13055 got_repo_pack_fds_close(pack_fds);
13060 free(base_commit_id);
13063 free(base_refname);
13070 fprintf(stderr, "usage: %s merge [-aCcn] [branch]\n", getprogname());
13074 static const struct got_error *
13075 cmd_merge(int argc, char *argv[])
13077 const struct got_error *error = NULL;
13078 struct got_worktree *worktree = NULL;
13079 struct got_repository *repo = NULL;
13080 struct got_fileindex *fileindex = NULL;
13081 char *cwd = NULL, *id_str = NULL, *author = NULL;
13082 char *gitconfig_path = NULL;
13083 struct got_reference *branch = NULL, *wt_branch = NULL;
13084 struct got_object_id *branch_tip = NULL, *yca_id = NULL;
13085 struct got_object_id *wt_branch_tip = NULL;
13086 int ch, merge_in_progress = 0, abort_merge = 0, continue_merge = 0;
13087 int allow_conflict = 0, interrupt_merge = 0;
13088 struct got_update_progress_arg upa;
13089 struct got_object_id *merge_commit_id = NULL;
13090 char *branch_name = NULL;
13091 int *pack_fds = NULL;
13093 memset(&upa, 0, sizeof(upa));
13096 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
13097 "unveil", NULL) == -1)
13101 while ((ch = getopt(argc, argv, "aCcn")) != -1) {
13107 allow_conflict = 1;
13109 continue_merge = 1;
13112 interrupt_merge = 1;
13123 if (allow_conflict) {
13125 option_conflict('a', 'C');
13126 if (!continue_merge)
13127 errx(1, "-C option requires -c");
13129 if (abort_merge && continue_merge)
13130 option_conflict('a', 'c');
13131 if (abort_merge || continue_merge) {
13134 } else if (argc != 1)
13137 cwd = getcwd(NULL, 0);
13139 error = got_error_from_errno("getcwd");
13143 error = got_repo_pack_fds_open(&pack_fds);
13147 error = got_worktree_open(&worktree, cwd);
13149 if (error->code == GOT_ERR_NOT_WORKTREE)
13150 error = wrap_not_worktree_error(error,
13155 error = get_gitconfig_path(&gitconfig_path);
13158 error = got_repo_open(&repo,
13159 worktree ? got_worktree_get_repo_path(worktree) : cwd,
13160 gitconfig_path, pack_fds);
13164 if (worktree != NULL) {
13165 error = worktree_has_logmsg_ref("merge", worktree, repo);
13170 error = apply_unveil(got_repo_get_path(repo), 0,
13171 worktree ? got_worktree_get_root_path(worktree) : NULL);
13175 error = check_rebase_or_histedit_in_progress(worktree);
13179 error = got_worktree_merge_in_progress(&merge_in_progress, worktree,
13185 if (!merge_in_progress) {
13186 error = got_error(GOT_ERR_NOT_MERGING);
13189 error = got_worktree_merge_continue(&branch_name,
13190 &branch_tip, &fileindex, worktree, repo);
13193 error = got_worktree_merge_abort(worktree, fileindex, repo,
13194 abort_progress, &upa);
13197 printf("Merge of %s aborted\n", branch_name);
13198 goto done; /* nothing else to do */
13201 error = get_author(&author, repo, worktree);
13205 if (continue_merge) {
13206 if (!merge_in_progress) {
13207 error = got_error(GOT_ERR_NOT_MERGING);
13210 error = got_worktree_merge_continue(&branch_name,
13211 &branch_tip, &fileindex, worktree, repo);
13215 error = got_ref_open(&branch, repo, argv[0], 0);
13218 branch_name = strdup(got_ref_get_name(branch));
13219 if (branch_name == NULL) {
13220 error = got_error_from_errno("strdup");
13223 error = got_ref_resolve(&branch_tip, repo, branch);
13228 error = got_ref_open(&wt_branch, repo,
13229 got_worktree_get_head_ref_name(worktree), 0);
13232 error = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
13235 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
13236 wt_branch_tip, branch_tip, 0, repo,
13237 check_cancelled, NULL);
13238 if (error && error->code != GOT_ERR_ANCESTRY)
13241 if (!continue_merge) {
13242 error = check_path_prefix(wt_branch_tip, branch_tip,
13243 got_worktree_get_path_prefix(worktree),
13244 GOT_ERR_MERGE_PATH, repo);
13248 error = check_same_branch(wt_branch_tip, branch,
13251 if (error->code != GOT_ERR_ANCESTRY)
13255 static char msg[512];
13256 snprintf(msg, sizeof(msg),
13257 "cannot create a merge commit because "
13258 "%s is based on %s; %s can be integrated "
13259 "with 'got integrate' instead", branch_name,
13260 got_worktree_get_head_ref_name(worktree),
13262 error = got_error_msg(GOT_ERR_SAME_BRANCH, msg);
13266 error = got_worktree_merge_prepare(&fileindex, worktree,
13271 error = got_worktree_merge_branch(worktree, fileindex,
13272 yca_id, branch_tip, repo, update_progress, &upa,
13273 check_cancelled, NULL);
13276 print_merge_progress_stats(&upa);
13277 if (!upa.did_something) {
13278 error = got_worktree_merge_abort(worktree, fileindex,
13279 repo, abort_progress, &upa);
13282 printf("Already up-to-date\n");
13287 if (interrupt_merge) {
13288 error = got_worktree_merge_postpone(worktree, fileindex);
13291 printf("Merge of %s interrupted on request\n", branch_name);
13292 } else if (upa.conflicts > 0 || upa.missing > 0 ||
13293 upa.not_deleted > 0 || upa.unversioned > 0) {
13294 error = got_worktree_merge_postpone(worktree, fileindex);
13297 if (upa.conflicts > 0 && upa.missing == 0 &&
13298 upa.not_deleted == 0 && upa.unversioned == 0) {
13299 error = got_error_msg(GOT_ERR_CONFLICTS,
13300 "conflicts must be resolved before merging "
13302 } else if (upa.conflicts > 0) {
13303 error = got_error_msg(GOT_ERR_CONFLICTS,
13304 "conflicts must be resolved before merging "
13305 "can continue; changes destined for some "
13306 "files were not yet merged and "
13307 "should be merged manually if required before the "
13308 "merge operation is continued");
13310 error = got_error_msg(GOT_ERR_CONFLICTS,
13311 "changes destined for some "
13312 "files were not yet merged and should be "
13313 "merged manually if required before the "
13314 "merge operation is continued");
13318 error = got_worktree_merge_commit(&merge_commit_id, worktree,
13319 fileindex, author, NULL, 1, branch_tip, branch_name,
13320 allow_conflict, repo, continue_merge ? print_status : NULL,
13324 error = got_worktree_merge_complete(worktree, fileindex, repo);
13327 error = got_object_id_str(&id_str, merge_commit_id);
13330 printf("Merged %s into %s: %s\n", branch_name,
13331 got_worktree_get_head_ref_name(worktree),
13336 free(gitconfig_path);
13338 free(merge_commit_id);
13344 got_ref_close(branch);
13346 got_ref_close(wt_branch);
13348 got_worktree_close(worktree);
13350 const struct got_error *close_err = got_repo_close(repo);
13355 const struct got_error *pack_err =
13356 got_repo_pack_fds_close(pack_fds);
13366 fprintf(stderr, "usage: %s stage [-lpS] [-F response-script] "
13367 "[path ...]\n", getprogname());
13371 static const struct got_error *
13372 print_stage(void *arg, unsigned char status, unsigned char staged_status,
13373 const char *path, struct got_object_id *blob_id,
13374 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
13375 int dirfd, const char *de_name)
13377 const struct got_error *err = NULL;
13378 char *id_str = NULL;
13380 if (staged_status != GOT_STATUS_ADD &&
13381 staged_status != GOT_STATUS_MODIFY &&
13382 staged_status != GOT_STATUS_DELETE)
13385 if (staged_status == GOT_STATUS_ADD ||
13386 staged_status == GOT_STATUS_MODIFY)
13387 err = got_object_id_str(&id_str, staged_blob_id);
13389 err = got_object_id_str(&id_str, blob_id);
13393 printf("%s %c %s\n", id_str, staged_status, path);
13398 static const struct got_error *
13399 cmd_stage(int argc, char *argv[])
13401 const struct got_error *error = NULL;
13402 struct got_repository *repo = NULL;
13403 struct got_worktree *worktree = NULL;
13405 struct got_pathlist_head paths;
13406 int ch, list_stage = 0, pflag = 0, allow_bad_symlinks = 0;
13407 FILE *patch_script_file = NULL;
13408 const char *patch_script_path = NULL;
13409 struct choose_patch_arg cpa;
13410 int *pack_fds = NULL;
13412 TAILQ_INIT(&paths);
13415 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
13416 "unveil", NULL) == -1)
13420 while ((ch = getopt(argc, argv, "F:lpS")) != -1) {
13423 patch_script_path = optarg;
13432 allow_bad_symlinks = 1;
13443 if (list_stage && (pflag || patch_script_path))
13444 errx(1, "-l option cannot be used with other options");
13445 if (patch_script_path && !pflag)
13446 errx(1, "-F option can only be used together with -p option");
13448 cwd = getcwd(NULL, 0);
13450 error = got_error_from_errno("getcwd");
13454 error = got_repo_pack_fds_open(&pack_fds);
13458 error = got_worktree_open(&worktree, cwd);
13460 if (error->code == GOT_ERR_NOT_WORKTREE)
13461 error = wrap_not_worktree_error(error, "stage", cwd);
13465 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
13470 if (patch_script_path) {
13471 patch_script_file = fopen(patch_script_path, "re");
13472 if (patch_script_file == NULL) {
13473 error = got_error_from_errno2("fopen",
13474 patch_script_path);
13478 error = apply_unveil(got_repo_get_path(repo), 0,
13479 got_worktree_get_root_path(worktree));
13483 error = check_merge_in_progress(worktree, repo);
13487 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
13492 error = got_worktree_status(worktree, &paths, repo, 0,
13493 print_stage, NULL, check_cancelled, NULL);
13495 cpa.patch_script_file = patch_script_file;
13496 cpa.action = "stage";
13497 error = got_worktree_stage(worktree, &paths,
13498 pflag ? NULL : print_status, NULL,
13499 pflag ? choose_patch : NULL, &cpa,
13500 allow_bad_symlinks, repo);
13503 if (patch_script_file && fclose(patch_script_file) == EOF &&
13505 error = got_error_from_errno2("fclose", patch_script_path);
13507 const struct got_error *close_err = got_repo_close(repo);
13512 got_worktree_close(worktree);
13514 const struct got_error *pack_err =
13515 got_repo_pack_fds_close(pack_fds);
13519 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
13525 usage_unstage(void)
13527 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
13528 "[path ...]\n", getprogname());
13533 static const struct got_error *
13534 cmd_unstage(int argc, char *argv[])
13536 const struct got_error *error = NULL;
13537 struct got_repository *repo = NULL;
13538 struct got_worktree *worktree = NULL;
13540 struct got_pathlist_head paths;
13542 struct got_update_progress_arg upa;
13543 FILE *patch_script_file = NULL;
13544 const char *patch_script_path = NULL;
13545 struct choose_patch_arg cpa;
13546 int *pack_fds = NULL;
13548 TAILQ_INIT(&paths);
13551 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
13552 "unveil", NULL) == -1)
13556 while ((ch = getopt(argc, argv, "F:p")) != -1) {
13559 patch_script_path = optarg;
13573 if (patch_script_path && !pflag)
13574 errx(1, "-F option can only be used together with -p option");
13576 cwd = getcwd(NULL, 0);
13578 error = got_error_from_errno("getcwd");
13582 error = got_repo_pack_fds_open(&pack_fds);
13586 error = got_worktree_open(&worktree, cwd);
13588 if (error->code == GOT_ERR_NOT_WORKTREE)
13589 error = wrap_not_worktree_error(error, "unstage", cwd);
13593 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
13598 if (patch_script_path) {
13599 patch_script_file = fopen(patch_script_path, "re");
13600 if (patch_script_file == NULL) {
13601 error = got_error_from_errno2("fopen",
13602 patch_script_path);
13607 error = apply_unveil(got_repo_get_path(repo), 0,
13608 got_worktree_get_root_path(worktree));
13612 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
13616 cpa.patch_script_file = patch_script_file;
13617 cpa.action = "unstage";
13618 memset(&upa, 0, sizeof(upa));
13619 error = got_worktree_unstage(worktree, &paths, update_progress,
13620 &upa, pflag ? choose_patch : NULL, &cpa, repo);
13622 print_merge_progress_stats(&upa);
13624 if (patch_script_file && fclose(patch_script_file) == EOF &&
13626 error = got_error_from_errno2("fclose", patch_script_path);
13628 const struct got_error *close_err = got_repo_close(repo);
13633 got_worktree_close(worktree);
13635 const struct got_error *pack_err =
13636 got_repo_pack_fds_close(pack_fds);
13640 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
13648 fprintf(stderr, "usage: %s cat [-P] [-c commit] [-r repository-path] "
13649 "arg ...\n", getprogname());
13653 static const struct got_error *
13654 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
13656 const struct got_error *err;
13657 struct got_blob_object *blob;
13660 fd = got_opentempfd();
13662 return got_error_from_errno("got_opentempfd");
13664 err = got_object_open_as_blob(&blob, repo, id, 8192, fd);
13668 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
13670 if (fd != -1 && close(fd) == -1 && err == NULL)
13671 err = got_error_from_errno("close");
13673 got_object_blob_close(blob);
13677 static const struct got_error *
13678 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
13680 const struct got_error *err;
13681 struct got_tree_object *tree;
13684 err = got_object_open_as_tree(&tree, repo, id);
13688 nentries = got_object_tree_get_nentries(tree);
13689 for (i = 0; i < nentries; i++) {
13690 struct got_tree_entry *te;
13692 if (sigint_received || sigpipe_received)
13694 te = got_object_tree_get_entry(tree, i);
13695 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
13698 fprintf(outfile, "%s %.7o %s\n", id_str,
13699 got_tree_entry_get_mode(te),
13700 got_tree_entry_get_name(te));
13704 got_object_tree_close(tree);
13708 static const struct got_error *
13709 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
13711 const struct got_error *err;
13712 struct got_commit_object *commit;
13713 const struct got_object_id_queue *parent_ids;
13714 struct got_object_qid *pid;
13715 char *id_str = NULL;
13716 const char *logmsg = NULL;
13719 err = got_object_open_as_commit(&commit, repo, id);
13723 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
13727 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
13728 parent_ids = got_object_commit_get_parent_ids(commit);
13729 fprintf(outfile, "numparents %d\n",
13730 got_object_commit_get_nparents(commit));
13731 STAILQ_FOREACH(pid, parent_ids, entry) {
13733 err = got_object_id_str(&pid_str, &pid->id);
13736 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
13739 got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
13740 got_object_commit_get_author_gmtoff(commit));
13741 fprintf(outfile, "%s%s %lld %s\n", GOT_COMMIT_LABEL_AUTHOR,
13742 got_object_commit_get_author(commit),
13743 (long long)got_object_commit_get_author_time(commit),
13746 got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
13747 got_object_commit_get_committer_gmtoff(commit));
13748 fprintf(outfile, "%s%s %lld %s\n", GOT_COMMIT_LABEL_COMMITTER,
13749 got_object_commit_get_committer(commit),
13750 (long long)got_object_commit_get_committer_time(commit),
13753 logmsg = got_object_commit_get_logmsg_raw(commit);
13754 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
13755 fprintf(outfile, "%s", logmsg);
13758 got_object_commit_close(commit);
13762 static const struct got_error *
13763 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
13765 const struct got_error *err;
13766 struct got_tag_object *tag;
13767 char *id_str = NULL;
13768 const char *tagmsg = NULL;
13771 err = got_object_open_as_tag(&tag, repo, id);
13775 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
13779 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
13781 switch (got_object_tag_get_object_type(tag)) {
13782 case GOT_OBJ_TYPE_BLOB:
13783 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
13784 GOT_OBJ_LABEL_BLOB);
13786 case GOT_OBJ_TYPE_TREE:
13787 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
13788 GOT_OBJ_LABEL_TREE);
13790 case GOT_OBJ_TYPE_COMMIT:
13791 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
13792 GOT_OBJ_LABEL_COMMIT);
13794 case GOT_OBJ_TYPE_TAG:
13795 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
13796 GOT_OBJ_LABEL_TAG);
13802 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
13803 got_object_tag_get_name(tag));
13805 got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
13806 got_object_tag_get_tagger_gmtoff(tag));
13807 fprintf(outfile, "%s%s %lld %s\n", GOT_TAG_LABEL_TAGGER,
13808 got_object_tag_get_tagger(tag),
13809 (long long)got_object_tag_get_tagger_time(tag),
13812 tagmsg = got_object_tag_get_message(tag);
13813 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
13814 fprintf(outfile, "%s", tagmsg);
13817 got_object_tag_close(tag);
13821 static const struct got_error *
13822 cmd_cat(int argc, char *argv[])
13824 const struct got_error *error;
13825 struct got_repository *repo = NULL;
13826 struct got_worktree *worktree = NULL;
13827 char *cwd = NULL, *repo_path = NULL, *label = NULL;
13828 const char *commit_id_str = NULL;
13829 struct got_object_id *id = NULL, *commit_id = NULL;
13830 struct got_commit_object *commit = NULL;
13831 int ch, obj_type, i, force_path = 0;
13832 struct got_reflist_head refs;
13833 int *pack_fds = NULL;
13838 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
13843 while ((ch = getopt(argc, argv, "c:Pr:")) != -1) {
13846 commit_id_str = optarg;
13852 repo_path = realpath(optarg, NULL);
13853 if (repo_path == NULL)
13854 return got_error_from_errno2("realpath",
13856 got_path_strip_trailing_slashes(repo_path);
13867 cwd = getcwd(NULL, 0);
13869 error = got_error_from_errno("getcwd");
13873 error = got_repo_pack_fds_open(&pack_fds);
13877 if (repo_path == NULL) {
13878 error = got_worktree_open(&worktree, cwd);
13879 if (error && error->code != GOT_ERR_NOT_WORKTREE)
13882 repo_path = strdup(
13883 got_worktree_get_repo_path(worktree));
13884 if (repo_path == NULL) {
13885 error = got_error_from_errno("strdup");
13889 /* Release work tree lock. */
13890 got_worktree_close(worktree);
13895 if (repo_path == NULL) {
13896 repo_path = strdup(cwd);
13897 if (repo_path == NULL)
13898 return got_error_from_errno("strdup");
13901 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
13906 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
13910 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
13914 if (commit_id_str == NULL)
13915 commit_id_str = GOT_REF_HEAD;
13916 error = got_repo_match_object_id(&commit_id, NULL,
13917 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
13921 error = got_object_open_as_commit(&commit, repo, commit_id);
13925 for (i = 0; i < argc; i++) {
13927 error = got_object_id_by_path(&id, repo, commit,
13932 error = got_repo_match_object_id(&id, &label, argv[i],
13933 GOT_OBJ_TYPE_ANY, NULL /* do not resolve tags */,
13936 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
13937 error->code != GOT_ERR_NOT_REF)
13939 error = got_object_id_by_path(&id, repo,
13946 error = got_object_get_type(&obj_type, repo, id);
13950 switch (obj_type) {
13951 case GOT_OBJ_TYPE_BLOB:
13952 error = cat_blob(id, repo, stdout);
13954 case GOT_OBJ_TYPE_TREE:
13955 error = cat_tree(id, repo, stdout);
13957 case GOT_OBJ_TYPE_COMMIT:
13958 error = cat_commit(id, repo, stdout);
13960 case GOT_OBJ_TYPE_TAG:
13961 error = cat_tag(id, repo, stdout);
13964 error = got_error(GOT_ERR_OBJ_TYPE);
13979 got_object_commit_close(commit);
13981 got_worktree_close(worktree);
13983 const struct got_error *close_err = got_repo_close(repo);
13988 const struct got_error *pack_err =
13989 got_repo_pack_fds_close(pack_fds);
13994 got_ref_list_free(&refs);
14001 fprintf(stderr, "usage: %s info [path ...]\n",
14006 static const struct got_error *
14007 print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
14008 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
14009 struct got_object_id *commit_id)
14011 const struct got_error *err = NULL;
14012 char *id_str = NULL;
14014 struct tm mytm, *tm;
14015 struct got_pathlist_head *paths = arg;
14016 struct got_pathlist_entry *pe;
14019 * Clear error indication from any of the path arguments which
14020 * would cause this file index entry to be displayed.
14022 TAILQ_FOREACH(pe, paths, entry) {
14023 if (got_path_cmp(path, pe->path, strlen(path),
14024 pe->path_len) == 0 ||
14025 got_path_is_child(path, pe->path, pe->path_len))
14026 pe->data = NULL; /* no error */
14029 printf(GOT_COMMIT_SEP_STR);
14031 printf("symlink: %s\n", path);
14032 else if (S_ISREG(mode)) {
14033 printf("file: %s\n", path);
14034 printf("mode: %o\n", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
14035 } else if (S_ISDIR(mode))
14036 printf("directory: %s\n", path);
14038 printf("something: %s\n", path);
14040 tm = localtime_r(&mtime, &mytm);
14043 if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) == 0)
14044 return got_error(GOT_ERR_NO_SPACE);
14045 printf("timestamp: %s\n", datebuf);
14048 err = got_object_id_str(&id_str, blob_id);
14051 printf("based on blob: %s\n", id_str);
14055 if (staged_blob_id) {
14056 err = got_object_id_str(&id_str, staged_blob_id);
14059 printf("based on staged blob: %s\n", id_str);
14064 err = got_object_id_str(&id_str, commit_id);
14067 printf("based on commit: %s\n", id_str);
14074 static const struct got_error *
14075 cmd_info(int argc, char *argv[])
14077 const struct got_error *error = NULL;
14078 struct got_worktree *worktree = NULL;
14079 char *cwd = NULL, *id_str = NULL;
14080 struct got_pathlist_head paths;
14081 char *uuidstr = NULL;
14082 int ch, show_files = 0;
14084 TAILQ_INIT(&paths);
14087 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
14092 while ((ch = getopt(argc, argv, "")) != -1) {
14103 cwd = getcwd(NULL, 0);
14105 error = got_error_from_errno("getcwd");
14109 error = got_worktree_open(&worktree, cwd);
14111 if (error->code == GOT_ERR_NOT_WORKTREE)
14112 error = wrap_not_worktree_error(error, "info", cwd);
14117 /* Remove "wpath cpath proc exec sendfd" promises. */
14118 if (pledge("stdio rpath flock unveil", NULL) == -1)
14121 error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
14126 error = get_worktree_paths_from_argv(&paths, argc, argv,
14133 error = got_object_id_str(&id_str,
14134 got_worktree_get_base_commit_id(worktree));
14138 error = got_worktree_get_uuid(&uuidstr, worktree);
14142 printf("work tree: %s\n", got_worktree_get_root_path(worktree));
14143 printf("work tree base commit: %s\n", id_str);
14144 printf("work tree path prefix: %s\n",
14145 got_worktree_get_path_prefix(worktree));
14146 printf("work tree branch reference: %s\n",
14147 got_worktree_get_head_ref_name(worktree));
14148 printf("work tree UUID: %s\n", uuidstr);
14149 printf("repository: %s\n", got_worktree_get_repo_path(worktree));
14152 struct got_pathlist_entry *pe;
14153 TAILQ_FOREACH(pe, &paths, entry) {
14154 if (pe->path_len == 0)
14157 * Assume this path will fail. This will be corrected
14158 * in print_path_info() in case the path does suceeed.
14160 pe->data = (void *)got_error(GOT_ERR_BAD_PATH);
14162 error = got_worktree_path_info(worktree, &paths,
14163 print_path_info, &paths, check_cancelled, NULL);
14166 TAILQ_FOREACH(pe, &paths, entry) {
14167 if (pe->data != NULL) {
14168 const struct got_error *perr;
14171 error = got_error_fmt(perr->code, "%s",
14179 got_worktree_close(worktree);
14180 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);