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>
45 #include "got_version.h"
46 #include "got_error.h"
47 #include "got_object.h"
48 #include "got_reference.h"
49 #include "got_repository.h"
51 #include "got_cancel.h"
52 #include "got_worktree.h"
54 #include "got_commit_graph.h"
55 #include "got_fetch.h"
57 #include "got_blame.h"
58 #include "got_privsep.h"
59 #include "got_opentemp.h"
60 #include "got_gotconfig.h"
62 #include "got_patch.h"
65 #include "got_keyword.h"
68 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
71 static volatile sig_atomic_t sigint_received;
72 static volatile sig_atomic_t sigpipe_received;
75 catch_sigint(int signo)
81 catch_sigpipe(int signo)
89 const struct got_error *(*cmd_main)(int, char *[]);
90 void (*cmd_usage)(void);
91 const char *cmd_alias;
94 __dead static void usage(int, int);
95 __dead static void usage_import(void);
96 __dead static void usage_clone(void);
97 __dead static void usage_fetch(void);
98 __dead static void usage_checkout(void);
99 __dead static void usage_update(void);
100 __dead static void usage_log(void);
101 __dead static void usage_diff(void);
102 __dead static void usage_blame(void);
103 __dead static void usage_tree(void);
104 __dead static void usage_status(void);
105 __dead static void usage_ref(void);
106 __dead static void usage_branch(void);
107 __dead static void usage_tag(void);
108 __dead static void usage_add(void);
109 __dead static void usage_remove(void);
110 __dead static void usage_patch(void);
111 __dead static void usage_revert(void);
112 __dead static void usage_commit(void);
113 __dead static void usage_send(void);
114 __dead static void usage_cherrypick(void);
115 __dead static void usage_backout(void);
116 __dead static void usage_rebase(void);
117 __dead static void usage_histedit(void);
118 __dead static void usage_integrate(void);
119 __dead static void usage_merge(void);
120 __dead static void usage_stage(void);
121 __dead static void usage_unstage(void);
122 __dead static void usage_cat(void);
123 __dead static void usage_info(void);
125 static const struct got_error* cmd_import(int, char *[]);
126 static const struct got_error* cmd_clone(int, char *[]);
127 static const struct got_error* cmd_fetch(int, char *[]);
128 static const struct got_error* cmd_checkout(int, char *[]);
129 static const struct got_error* cmd_update(int, char *[]);
130 static const struct got_error* cmd_log(int, char *[]);
131 static const struct got_error* cmd_diff(int, char *[]);
132 static const struct got_error* cmd_blame(int, char *[]);
133 static const struct got_error* cmd_tree(int, char *[]);
134 static const struct got_error* cmd_status(int, char *[]);
135 static const struct got_error* cmd_ref(int, char *[]);
136 static const struct got_error* cmd_branch(int, char *[]);
137 static const struct got_error* cmd_tag(int, char *[]);
138 static const struct got_error* cmd_add(int, char *[]);
139 static const struct got_error* cmd_remove(int, char *[]);
140 static const struct got_error* cmd_patch(int, char *[]);
141 static const struct got_error* cmd_revert(int, char *[]);
142 static const struct got_error* cmd_commit(int, char *[]);
143 static const struct got_error* cmd_send(int, char *[]);
144 static const struct got_error* cmd_cherrypick(int, char *[]);
145 static const struct got_error* cmd_backout(int, char *[]);
146 static const struct got_error* cmd_rebase(int, char *[]);
147 static const struct got_error* cmd_histedit(int, char *[]);
148 static const struct got_error* cmd_integrate(int, char *[]);
149 static const struct got_error* cmd_merge(int, char *[]);
150 static const struct got_error* cmd_stage(int, char *[]);
151 static const struct got_error* cmd_unstage(int, char *[]);
152 static const struct got_error* cmd_cat(int, char *[]);
153 static const struct got_error* cmd_info(int, char *[]);
155 static const struct got_cmd got_commands[] = {
156 { "import", cmd_import, usage_import, "im" },
157 { "clone", cmd_clone, usage_clone, "cl" },
158 { "fetch", cmd_fetch, usage_fetch, "fe" },
159 { "checkout", cmd_checkout, usage_checkout, "co" },
160 { "update", cmd_update, usage_update, "up" },
161 { "log", cmd_log, usage_log, "" },
162 { "diff", cmd_diff, usage_diff, "di" },
163 { "blame", cmd_blame, usage_blame, "bl" },
164 { "tree", cmd_tree, usage_tree, "tr" },
165 { "status", cmd_status, usage_status, "st" },
166 { "ref", cmd_ref, usage_ref, "" },
167 { "branch", cmd_branch, usage_branch, "br" },
168 { "tag", cmd_tag, usage_tag, "" },
169 { "add", cmd_add, usage_add, "" },
170 { "remove", cmd_remove, usage_remove, "rm" },
171 { "patch", cmd_patch, usage_patch, "pa" },
172 { "revert", cmd_revert, usage_revert, "rv" },
173 { "commit", cmd_commit, usage_commit, "ci" },
174 { "send", cmd_send, usage_send, "se" },
175 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
176 { "backout", cmd_backout, usage_backout, "bo" },
177 { "rebase", cmd_rebase, usage_rebase, "rb" },
178 { "histedit", cmd_histedit, usage_histedit, "he" },
179 { "integrate", cmd_integrate, usage_integrate,"ig" },
180 { "merge", cmd_merge, usage_merge, "mg" },
181 { "stage", cmd_stage, usage_stage, "sg" },
182 { "unstage", cmd_unstage, usage_unstage, "ug" },
183 { "cat", cmd_cat, usage_cat, "" },
184 { "info", cmd_info, usage_info, "" },
188 list_commands(FILE *fp)
192 fprintf(fp, "commands:");
193 for (i = 0; i < nitems(got_commands); i++) {
194 const struct got_cmd *cmd = &got_commands[i];
195 fprintf(fp, " %s", cmd->cmd_name);
201 option_conflict(char a, char b)
203 errx(1, "-%c and -%c options are mutually exclusive", a, b);
207 main(int argc, char *argv[])
209 const struct got_cmd *cmd;
212 int hflag = 0, Vflag = 0;
213 static const struct option longopts[] = {
214 { "version", no_argument, NULL, 'V' },
218 setlocale(LC_CTYPE, "");
220 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
240 got_version_print_str();
245 usage(hflag, hflag ? 0 : 1);
247 signal(SIGINT, catch_sigint);
248 signal(SIGPIPE, catch_sigpipe);
250 for (i = 0; i < nitems(got_commands); i++) {
251 const struct got_error *error;
253 cmd = &got_commands[i];
255 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
256 strcmp(cmd->cmd_alias, argv[0]) != 0)
262 error = cmd->cmd_main(argc, argv);
263 if (error && error->code != GOT_ERR_CANCELLED &&
264 error->code != GOT_ERR_PRIVSEP_EXIT &&
265 !(sigpipe_received &&
266 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
268 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
270 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
277 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
278 list_commands(stderr);
283 usage(int hflag, int status)
285 FILE *fp = (status == 0) ? stdout : stderr;
287 fprintf(fp, "usage: %s [-hV] command [arg ...]\n",
294 static const struct got_error *
295 get_editor(char **abspath)
297 const struct got_error *err = NULL;
302 editor = getenv("VISUAL");
304 editor = getenv("EDITOR");
307 err = got_path_find_prog(abspath, editor);
312 if (*abspath == NULL) {
313 *abspath = strdup("/usr/bin/vi");
314 if (*abspath == NULL)
315 return got_error_from_errno("strdup");
321 static const struct got_error *
322 apply_unveil(const char *repo_path, int repo_read_only,
323 const char *worktree_path)
325 const struct got_error *err;
328 if (unveil("gmon.out", "rwc") != 0)
329 return got_error_from_errno2("unveil", "gmon.out");
331 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
332 return got_error_from_errno2("unveil", repo_path);
334 if (worktree_path && unveil(worktree_path, "rwc") != 0)
335 return got_error_from_errno2("unveil", worktree_path);
337 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
338 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
340 err = got_privsep_unveil_exec_helpers();
344 if (unveil(NULL, NULL) != 0)
345 return got_error_from_errno("unveil");
353 fprintf(stderr, "usage: %s import [-b branch] [-I pattern] [-m message] "
354 "[-r repository-path] directory\n", getprogname());
359 spawn_editor(const char *editor, const char *file)
362 sig_t sighup, sigint, sigquit;
365 sighup = signal(SIGHUP, SIG_IGN);
366 sigint = signal(SIGINT, SIG_IGN);
367 sigquit = signal(SIGQUIT, SIG_IGN);
369 switch (pid = fork()) {
373 execl(editor, editor, file, (char *)NULL);
377 while (waitpid(pid, &st, 0) == -1)
382 (void)signal(SIGHUP, sighup);
383 (void)signal(SIGINT, sigint);
384 (void)signal(SIGQUIT, sigquit);
386 if (!WIFEXITED(st)) {
391 return WEXITSTATUS(st);
394 static const struct got_error *
395 read_logmsg(char **logmsg, size_t *len, FILE *fp, size_t filesize)
397 const struct got_error *err = NULL;
404 if (fseeko(fp, 0L, SEEK_SET) == -1)
405 return got_error_from_errno("fseeko");
407 *logmsg = malloc(filesize + 1);
409 return got_error_from_errno("malloc");
412 while (getline(&line, &linesize, fp) != -1) {
413 if (line[0] == '#' || (*len == 0 && line[0] == '\n'))
414 continue; /* remove comments and leading empty lines */
415 *len = strlcat(*logmsg, line, filesize + 1);
416 if (*len >= filesize + 1) {
417 err = got_error(GOT_ERR_NO_SPACE);
422 err = got_ferror(fp, GOT_ERR_IO);
426 while (*len > 0 && (*logmsg)[*len - 1] == '\n') {
427 (*logmsg)[*len - 1] = '\0';
440 static const struct got_error *
441 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
442 const char *initial_content, size_t initial_content_len,
443 int require_modification)
445 const struct got_error *err = NULL;
452 if (stat(logmsg_path, &st) == -1)
453 return got_error_from_errno2("stat", logmsg_path);
455 if (spawn_editor(editor, logmsg_path) == -1)
456 return got_error_from_errno("failed spawning editor");
458 if (require_modification) {
459 struct timespec timeout;
463 nanosleep(&timeout, NULL);
466 if (stat(logmsg_path, &st2) == -1)
467 return got_error_from_errno2("stat", logmsg_path);
469 if (require_modification && st.st_size == st2.st_size &&
470 timespeccmp(&st.st_mtim, &st2.st_mtim, ==))
471 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
472 "no changes made to commit message, aborting");
474 fp = fopen(logmsg_path, "re");
476 err = got_error_from_errno("fopen");
480 /* strip comments and leading/trailing newlines */
481 err = read_logmsg(logmsg, &logmsg_len, fp, st2.st_size);
484 if (logmsg_len == 0) {
485 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
486 "commit message cannot be empty, aborting");
490 if (fp && fclose(fp) == EOF && err == NULL)
491 err = got_error_from_errno("fclose");
499 static const struct got_error *
500 collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
501 const char *path_dir, const char *branch_name)
503 char *initial_content = NULL;
504 const struct got_error *err = NULL;
505 int initial_content_len;
508 initial_content_len = asprintf(&initial_content,
509 "\n# %s to be imported to branch %s\n", path_dir,
511 if (initial_content_len == -1)
512 return got_error_from_errno("asprintf");
514 err = got_opentemp_named_fd(logmsg_path, &fd,
515 GOT_TMPDIR_STR "/got-importmsg", "");
519 if (write(fd, initial_content, initial_content_len) == -1) {
520 err = got_error_from_errno2("write", *logmsg_path);
523 if (close(fd) == -1) {
524 err = got_error_from_errno2("close", *logmsg_path);
529 err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content,
530 initial_content_len, 1);
532 if (fd != -1 && close(fd) == -1 && err == NULL)
533 err = got_error_from_errno2("close", *logmsg_path);
534 free(initial_content);
542 static const struct got_error *
543 import_progress(void *arg, const char *path)
545 printf("A %s\n", path);
549 static const struct got_error *
550 valid_author(const char *author)
552 const char *email = author;
555 * Git' expects the author (or committer) to be in the form
556 * "name <email>", which are mostly free form (see the
557 * "committer" description in git-fast-import(1)). We're only
558 * doing this to avoid git's object parser breaking on commits
562 while (*author && *author != '\n' && *author != '<' && *author != '>')
564 if (author != email && *author == '<' && *(author - 1) != ' ')
565 return got_error_fmt(GOT_ERR_COMMIT_BAD_AUTHOR, "%s: space "
566 "between author name and email required", email);
567 if (*author++ != '<')
568 return got_error_fmt(GOT_ERR_COMMIT_NO_EMAIL, "%s", email);
569 while (*author && *author != '\n' && *author != '<' && *author != '>')
571 if (strcmp(author, ">") != 0)
572 return got_error_fmt(GOT_ERR_COMMIT_NO_EMAIL, "%s", email);
576 static const struct got_error *
577 get_author(char **author, struct got_repository *repo,
578 struct got_worktree *worktree)
580 const struct got_error *err = NULL;
581 const char *got_author = NULL, *name, *email;
582 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
587 worktree_conf = got_worktree_get_gotconfig(worktree);
588 repo_conf = got_repo_get_gotconfig(repo);
591 * Priority of potential author information sources, from most
592 * significant to least significant:
593 * 1) work tree's .got/got.conf file
594 * 2) repository's got.conf file
595 * 3) repository's git config file
596 * 4) environment variables
597 * 5) global git config files (in user's home directory or /etc)
601 got_author = got_gotconfig_get_author(worktree_conf);
602 if (got_author == NULL)
603 got_author = got_gotconfig_get_author(repo_conf);
604 if (got_author == NULL) {
605 name = got_repo_get_gitconfig_author_name(repo);
606 email = got_repo_get_gitconfig_author_email(repo);
608 if (asprintf(author, "%s <%s>", name, email) == -1)
609 return got_error_from_errno("asprintf");
613 got_author = getenv("GOT_AUTHOR");
614 if (got_author == NULL) {
615 name = got_repo_get_global_gitconfig_author_name(repo);
616 email = got_repo_get_global_gitconfig_author_email(
619 if (asprintf(author, "%s <%s>", name, email)
621 return got_error_from_errno("asprintf");
624 /* TODO: Look up user in password database? */
625 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
629 *author = strdup(got_author);
631 return got_error_from_errno("strdup");
633 err = valid_author(*author);
641 static const struct got_error *
642 get_allowed_signers(char **allowed_signers, struct got_repository *repo,
643 struct got_worktree *worktree)
645 const char *got_allowed_signers = NULL;
646 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
648 *allowed_signers = NULL;
651 worktree_conf = got_worktree_get_gotconfig(worktree);
652 repo_conf = got_repo_get_gotconfig(repo);
655 * Priority of potential author information sources, from most
656 * significant to least significant:
657 * 1) work tree's .got/got.conf file
658 * 2) repository's got.conf file
662 got_allowed_signers = got_gotconfig_get_allowed_signers_file(
664 if (got_allowed_signers == NULL)
665 got_allowed_signers = got_gotconfig_get_allowed_signers_file(
668 if (got_allowed_signers) {
669 *allowed_signers = strdup(got_allowed_signers);
670 if (*allowed_signers == NULL)
671 return got_error_from_errno("strdup");
676 static const struct got_error *
677 get_revoked_signers(char **revoked_signers, struct got_repository *repo,
678 struct got_worktree *worktree)
680 const char *got_revoked_signers = NULL;
681 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
683 *revoked_signers = NULL;
686 worktree_conf = got_worktree_get_gotconfig(worktree);
687 repo_conf = got_repo_get_gotconfig(repo);
690 * Priority of potential author information sources, from most
691 * significant to least significant:
692 * 1) work tree's .got/got.conf file
693 * 2) repository's got.conf file
697 got_revoked_signers = got_gotconfig_get_revoked_signers_file(
699 if (got_revoked_signers == NULL)
700 got_revoked_signers = got_gotconfig_get_revoked_signers_file(
703 if (got_revoked_signers) {
704 *revoked_signers = strdup(got_revoked_signers);
705 if (*revoked_signers == NULL)
706 return got_error_from_errno("strdup");
712 get_signer_id(struct got_repository *repo, struct got_worktree *worktree)
714 const char *got_signer_id = NULL;
715 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
718 worktree_conf = got_worktree_get_gotconfig(worktree);
719 repo_conf = got_repo_get_gotconfig(repo);
722 * Priority of potential author information sources, from most
723 * significant to least significant:
724 * 1) work tree's .got/got.conf file
725 * 2) repository's got.conf file
729 got_signer_id = got_gotconfig_get_signer_id(worktree_conf);
730 if (got_signer_id == NULL)
731 got_signer_id = got_gotconfig_get_signer_id(repo_conf);
733 return got_signer_id;
736 static const struct got_error *
737 get_gitconfig_path(char **gitconfig_path)
739 const char *homedir = getenv("HOME");
741 *gitconfig_path = NULL;
743 if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
744 return got_error_from_errno("asprintf");
750 static const struct got_error *
751 cmd_import(int argc, char *argv[])
753 const struct got_error *error = NULL;
754 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
755 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
756 const char *branch_name = NULL;
757 char *id_str = NULL, *logmsg_path = NULL;
758 char refname[PATH_MAX] = "refs/heads/";
759 struct got_repository *repo = NULL;
760 struct got_reference *branch_ref = NULL, *head_ref = NULL;
761 struct got_object_id *new_commit_id = NULL;
763 struct got_pathlist_head ignores;
764 struct got_pathlist_entry *pe;
765 int preserve_logmsg = 0;
766 int *pack_fds = NULL;
768 TAILQ_INIT(&ignores);
771 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
777 while ((ch = getopt(argc, argv, "b:I:m:r:")) != -1) {
780 branch_name = optarg;
783 if (optarg[0] == '\0')
785 error = got_pathlist_insert(&pe, &ignores, optarg,
791 logmsg = strdup(optarg);
792 if (logmsg == NULL) {
793 error = got_error_from_errno("strdup");
798 repo_path = realpath(optarg, NULL);
799 if (repo_path == NULL) {
800 error = got_error_from_errno2("realpath",
817 if (repo_path == NULL) {
818 repo_path = getcwd(NULL, 0);
819 if (repo_path == NULL)
820 return got_error_from_errno("getcwd");
822 got_path_strip_trailing_slashes(repo_path);
823 error = get_gitconfig_path(&gitconfig_path);
826 error = got_repo_pack_fds_open(&pack_fds);
829 error = got_repo_open(&repo, repo_path, gitconfig_path, pack_fds);
833 error = get_author(&author, repo, NULL);
838 * Don't let the user create a branch name with a leading '-'.
839 * While technically a valid reference name, this case is usually
840 * an unintended typo.
842 if (branch_name && branch_name[0] == '-')
843 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
845 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
846 if (error && error->code != GOT_ERR_NOT_REF)
850 n = strlcat(refname, branch_name, sizeof(refname));
851 else if (head_ref && got_ref_is_symbolic(head_ref))
852 n = strlcpy(refname, got_ref_get_symref_target(head_ref),
855 n = strlcat(refname, "main", sizeof(refname));
856 if (n >= sizeof(refname)) {
857 error = got_error(GOT_ERR_NO_SPACE);
861 error = got_ref_open(&branch_ref, repo, refname, 0);
863 if (error->code != GOT_ERR_NOT_REF)
866 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
867 "import target branch already exists");
871 path_dir = realpath(argv[0], NULL);
872 if (path_dir == NULL) {
873 error = got_error_from_errno2("realpath", argv[0]);
876 got_path_strip_trailing_slashes(path_dir);
879 * unveil(2) traverses exec(2); if an editor is used we have
880 * to apply unveil after the log message has been written.
882 if (logmsg == NULL || *logmsg == '\0') {
883 error = get_editor(&editor);
887 error = collect_import_msg(&logmsg, &logmsg_path, editor,
890 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
897 if (unveil(path_dir, "r") != 0) {
898 error = got_error_from_errno2("unveil", path_dir);
904 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
911 error = got_repo_import(&new_commit_id, path_dir, logmsg,
912 author, &ignores, repo, import_progress, NULL);
919 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
926 error = got_ref_write(branch_ref, repo);
933 error = got_object_id_str(&id_str, new_commit_id);
940 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
942 if (error->code != GOT_ERR_NOT_REF) {
948 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
956 error = got_ref_write(head_ref, repo);
964 printf("Created branch %s with commit %s\n",
965 got_ref_get_name(branch_ref), id_str);
968 const struct got_error *pack_err =
969 got_repo_pack_fds_close(pack_fds);
974 const struct got_error *close_err = got_repo_close(repo);
978 if (preserve_logmsg) {
979 fprintf(stderr, "%s: log message preserved in %s\n",
980 getprogname(), logmsg_path);
981 } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
982 error = got_error_from_errno2("unlink", logmsg_path);
990 free(gitconfig_path);
992 got_ref_close(branch_ref);
994 got_ref_close(head_ref);
1001 fprintf(stderr, "usage: %s clone [-almqv] [-b branch] [-R reference] "
1002 "repository-URL [directory]\n", getprogname());
1006 struct got_fetch_progress_arg {
1007 char last_scaled_size[FMT_SCALED_STRSIZE];
1009 int last_p_resolved;
1012 struct got_repository *repo;
1015 int configs_created;
1017 struct got_pathlist_head *symrefs;
1018 struct got_pathlist_head *wanted_branches;
1019 struct got_pathlist_head *wanted_refs;
1023 const char *remote_repo_path;
1024 const char *git_url;
1025 int fetch_all_branches;
1026 int mirror_references;
1030 /* XXX forward declaration */
1031 static const struct got_error *
1032 create_config_files(const char *proto, const char *host, const char *port,
1033 const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1034 int mirror_references, struct got_pathlist_head *symrefs,
1035 struct got_pathlist_head *wanted_branches,
1036 struct got_pathlist_head *wanted_refs, struct got_repository *repo);
1038 static const struct got_error *
1039 fetch_progress(void *arg, const char *message, off_t packfile_size,
1040 int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
1042 const struct got_error *err = NULL;
1043 struct got_fetch_progress_arg *a = arg;
1044 char scaled_size[FMT_SCALED_STRSIZE];
1045 int p_indexed, p_resolved;
1046 int print_size = 0, print_indexed = 0, print_resolved = 0;
1049 * In order to allow a failed clone to be resumed with 'got fetch'
1050 * we try to create configuration files as soon as possible.
1051 * Once the server has sent information about its default branch
1052 * we have all required information.
1054 if (a->create_configs && !a->configs_created &&
1055 !TAILQ_EMPTY(a->config_info.symrefs)) {
1056 err = create_config_files(a->config_info.proto,
1057 a->config_info.host, a->config_info.port,
1058 a->config_info.remote_repo_path,
1059 a->config_info.git_url,
1060 a->config_info.fetch_all_branches,
1061 a->config_info.mirror_references,
1062 a->config_info.symrefs,
1063 a->config_info.wanted_branches,
1064 a->config_info.wanted_refs, a->repo);
1067 a->configs_created = 1;
1070 if (a->verbosity < 0)
1073 if (message && message[0] != '\0') {
1074 printf("\rserver: %s", message);
1079 if (packfile_size > 0 || nobj_indexed > 0) {
1080 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
1081 (a->last_scaled_size[0] == '\0' ||
1082 strcmp(scaled_size, a->last_scaled_size)) != 0) {
1084 if (strlcpy(a->last_scaled_size, scaled_size,
1085 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
1086 return got_error(GOT_ERR_NO_SPACE);
1088 if (nobj_indexed > 0) {
1089 p_indexed = (nobj_indexed * 100) / nobj_total;
1090 if (p_indexed != a->last_p_indexed) {
1091 a->last_p_indexed = p_indexed;
1096 if (nobj_resolved > 0) {
1097 p_resolved = (nobj_resolved * 100) /
1098 (nobj_total - nobj_loose);
1099 if (p_resolved != a->last_p_resolved) {
1100 a->last_p_resolved = p_resolved;
1108 if (print_size || print_indexed || print_resolved)
1111 printf("%*s fetched", FMT_SCALED_STRSIZE - 2, scaled_size);
1113 printf("; indexing %d%%", p_indexed);
1115 printf("; resolving deltas %d%%", p_resolved);
1116 if (print_size || print_indexed || print_resolved)
1122 static const struct got_error *
1123 create_symref(const char *refname, struct got_reference *target_ref,
1124 int verbosity, struct got_repository *repo)
1126 const struct got_error *err;
1127 struct got_reference *head_symref;
1129 err = got_ref_alloc_symref(&head_symref, refname, target_ref);
1133 err = got_ref_write(head_symref, repo);
1134 if (err == NULL && verbosity > 0) {
1135 printf("Created reference %s: %s\n", GOT_REF_HEAD,
1136 got_ref_get_name(target_ref));
1138 got_ref_close(head_symref);
1142 static const struct got_error *
1143 list_remote_refs(struct got_pathlist_head *symrefs,
1144 struct got_pathlist_head *refs)
1146 const struct got_error *err;
1147 struct got_pathlist_entry *pe;
1149 TAILQ_FOREACH(pe, symrefs, entry) {
1150 const char *refname = pe->path;
1151 const char *targetref = pe->data;
1153 printf("%s: %s\n", refname, targetref);
1156 TAILQ_FOREACH(pe, refs, entry) {
1157 const char *refname = pe->path;
1158 struct got_object_id *id = pe->data;
1161 err = got_object_id_str(&id_str, id);
1164 printf("%s: %s\n", refname, id_str);
1171 static const struct got_error *
1172 create_ref(const char *refname, struct got_object_id *id,
1173 int verbosity, struct got_repository *repo)
1175 const struct got_error *err = NULL;
1176 struct got_reference *ref;
1179 err = got_object_id_str(&id_str, id);
1183 err = got_ref_alloc(&ref, refname, id);
1187 err = got_ref_write(ref, repo);
1190 if (err == NULL && verbosity >= 0)
1191 printf("Created reference %s: %s\n", refname, id_str);
1198 match_wanted_ref(const char *refname, const char *wanted_ref)
1200 if (strncmp(refname, "refs/", 5) != 0)
1205 * Prevent fetching of references that won't make any
1206 * sense outside of the remote repository's context.
1208 if (strncmp(refname, "got/", 4) == 0)
1210 if (strncmp(refname, "remotes/", 8) == 0)
1213 if (strncmp(wanted_ref, "refs/", 5) == 0)
1216 /* Allow prefix match. */
1217 if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
1220 /* Allow exact match. */
1221 return (strcmp(refname, wanted_ref) == 0);
1225 is_wanted_ref(struct got_pathlist_head *wanted_refs, const char *refname)
1227 struct got_pathlist_entry *pe;
1229 TAILQ_FOREACH(pe, wanted_refs, entry) {
1230 if (match_wanted_ref(refname, pe->path))
1237 static const struct got_error *
1238 create_wanted_ref(const char *refname, struct got_object_id *id,
1239 const char *remote_repo_name, int verbosity, struct got_repository *repo)
1241 const struct got_error *err;
1242 char *remote_refname;
1244 if (strncmp("refs/", refname, 5) == 0)
1247 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1248 remote_repo_name, refname) == -1)
1249 return got_error_from_errno("asprintf");
1251 err = create_ref(remote_refname, id, verbosity, repo);
1252 free(remote_refname);
1256 static const struct got_error *
1257 create_gotconfig(const char *proto, const char *host, const char *port,
1258 const char *remote_repo_path, const char *default_branch,
1259 int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1260 struct got_pathlist_head *wanted_refs, int mirror_references,
1261 struct got_repository *repo)
1263 const struct got_error *err = NULL;
1264 char *gotconfig_path = NULL;
1265 char *gotconfig = NULL;
1266 FILE *gotconfig_file = NULL;
1267 const char *branchname = NULL;
1268 char *branches = NULL, *refs = NULL;
1271 if (!fetch_all_branches && !TAILQ_EMPTY(wanted_branches)) {
1272 struct got_pathlist_entry *pe;
1273 TAILQ_FOREACH(pe, wanted_branches, entry) {
1275 branchname = pe->path;
1276 if (strncmp(branchname, "refs/heads/", 11) == 0)
1278 if (asprintf(&s, "%s\"%s\" ",
1279 branches ? branches : "", branchname) == -1) {
1280 err = got_error_from_errno("asprintf");
1286 } else if (!fetch_all_branches && default_branch) {
1287 branchname = default_branch;
1288 if (strncmp(branchname, "refs/heads/", 11) == 0)
1290 if (asprintf(&branches, "\"%s\" ", branchname) == -1) {
1291 err = got_error_from_errno("asprintf");
1295 if (!TAILQ_EMPTY(wanted_refs)) {
1296 struct got_pathlist_entry *pe;
1297 TAILQ_FOREACH(pe, wanted_refs, entry) {
1299 const char *refname = pe->path;
1300 if (strncmp(refname, "refs/", 5) == 0)
1302 if (asprintf(&s, "%s\"%s\" ",
1303 refs ? refs : "", refname) == -1) {
1304 err = got_error_from_errno("asprintf");
1312 /* Create got.conf(5). */
1313 gotconfig_path = got_repo_get_path_gotconfig(repo);
1314 if (gotconfig_path == NULL) {
1315 err = got_error_from_errno("got_repo_get_path_gotconfig");
1318 gotconfig_file = fopen(gotconfig_path, "ae");
1319 if (gotconfig_file == NULL) {
1320 err = got_error_from_errno2("fopen", gotconfig_path);
1323 if (asprintf(&gotconfig,
1328 "\trepository \"%s\"\n"
1334 GOT_FETCH_DEFAULT_REMOTE_NAME, host, proto,
1335 port ? "\tport " : "", port ? port : "", port ? "\n" : "",
1336 remote_repo_path, branches ? "\tbranch { " : "",
1337 branches ? branches : "", branches ? "}\n" : "",
1338 refs ? "\treference { " : "", refs ? refs : "", refs ? "}\n" : "",
1339 mirror_references ? "\tmirror_references yes\n" : "",
1340 fetch_all_branches ? "\tfetch_all_branches yes\n" : "") == -1) {
1341 err = got_error_from_errno("asprintf");
1344 n = fwrite(gotconfig, 1, strlen(gotconfig), gotconfig_file);
1345 if (n != strlen(gotconfig)) {
1346 err = got_ferror(gotconfig_file, GOT_ERR_IO);
1351 if (gotconfig_file && fclose(gotconfig_file) == EOF && err == NULL)
1352 err = got_error_from_errno2("fclose", gotconfig_path);
1353 free(gotconfig_path);
1358 static const struct got_error *
1359 create_gitconfig(const char *git_url, const char *default_branch,
1360 int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1361 struct got_pathlist_head *wanted_refs, int mirror_references,
1362 struct got_repository *repo)
1364 const struct got_error *err = NULL;
1365 char *gitconfig_path = NULL;
1366 char *gitconfig = NULL;
1367 FILE *gitconfig_file = NULL;
1368 char *branches = NULL, *refs = NULL;
1369 const char *branchname;
1372 /* Create a config file Git can understand. */
1373 gitconfig_path = got_repo_get_path_gitconfig(repo);
1374 if (gitconfig_path == NULL) {
1375 err = got_error_from_errno("got_repo_get_path_gitconfig");
1378 gitconfig_file = fopen(gitconfig_path, "ae");
1379 if (gitconfig_file == NULL) {
1380 err = got_error_from_errno2("fopen", gitconfig_path);
1383 if (fetch_all_branches) {
1384 if (mirror_references) {
1385 if (asprintf(&branches,
1386 "\tfetch = refs/heads/*:refs/heads/*\n") == -1) {
1387 err = got_error_from_errno("asprintf");
1390 } else if (asprintf(&branches,
1391 "\tfetch = refs/heads/*:refs/remotes/%s/*\n",
1392 GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1393 err = got_error_from_errno("asprintf");
1396 } else if (!TAILQ_EMPTY(wanted_branches)) {
1397 struct got_pathlist_entry *pe;
1398 TAILQ_FOREACH(pe, wanted_branches, entry) {
1400 branchname = pe->path;
1401 if (strncmp(branchname, "refs/heads/", 11) == 0)
1403 if (mirror_references) {
1405 "%s\tfetch = refs/heads/%s:refs/heads/%s\n",
1406 branches ? branches : "",
1407 branchname, branchname) == -1) {
1408 err = got_error_from_errno("asprintf");
1411 } else if (asprintf(&s,
1412 "%s\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1413 branches ? branches : "",
1414 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1415 branchname) == -1) {
1416 err = got_error_from_errno("asprintf");
1424 * If the server specified a default branch, use just that one.
1425 * Otherwise fall back to fetching all branches on next fetch.
1427 if (default_branch) {
1428 branchname = default_branch;
1429 if (strncmp(branchname, "refs/heads/", 11) == 0)
1432 branchname = "*"; /* fall back to all branches */
1433 if (mirror_references) {
1434 if (asprintf(&branches,
1435 "\tfetch = refs/heads/%s:refs/heads/%s\n",
1436 branchname, branchname) == -1) {
1437 err = got_error_from_errno("asprintf");
1440 } else if (asprintf(&branches,
1441 "\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1442 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1443 branchname) == -1) {
1444 err = got_error_from_errno("asprintf");
1448 if (!TAILQ_EMPTY(wanted_refs)) {
1449 struct got_pathlist_entry *pe;
1450 TAILQ_FOREACH(pe, wanted_refs, entry) {
1452 const char *refname = pe->path;
1453 if (strncmp(refname, "refs/", 5) == 0)
1455 if (mirror_references) {
1457 "%s\tfetch = refs/%s:refs/%s\n",
1458 refs ? refs : "", refname, refname) == -1) {
1459 err = got_error_from_errno("asprintf");
1462 } else if (asprintf(&s,
1463 "%s\tfetch = refs/%s:refs/remotes/%s/%s\n",
1465 refname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1467 err = got_error_from_errno("asprintf");
1475 if (asprintf(&gitconfig,
1480 "\tfetch = refs/tags/*:refs/tags/*\n",
1481 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url, branches ? branches : "",
1482 refs ? refs : "") == -1) {
1483 err = got_error_from_errno("asprintf");
1486 n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1487 if (n != strlen(gitconfig)) {
1488 err = got_ferror(gitconfig_file, GOT_ERR_IO);
1492 if (gitconfig_file && fclose(gitconfig_file) == EOF && err == NULL)
1493 err = got_error_from_errno2("fclose", gitconfig_path);
1494 free(gitconfig_path);
1499 static const struct got_error *
1500 create_config_files(const char *proto, const char *host, const char *port,
1501 const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1502 int mirror_references, struct got_pathlist_head *symrefs,
1503 struct got_pathlist_head *wanted_branches,
1504 struct got_pathlist_head *wanted_refs, struct got_repository *repo)
1506 const struct got_error *err = NULL;
1507 const char *default_branch = NULL;
1508 struct got_pathlist_entry *pe;
1511 * If we asked for a set of wanted branches then use the first
1514 if (!TAILQ_EMPTY(wanted_branches)) {
1515 pe = TAILQ_FIRST(wanted_branches);
1516 default_branch = pe->path;
1518 /* First HEAD ref listed by server is the default branch. */
1519 TAILQ_FOREACH(pe, symrefs, entry) {
1520 const char *refname = pe->path;
1521 const char *target = pe->data;
1523 if (strcmp(refname, GOT_REF_HEAD) != 0)
1526 default_branch = target;
1531 /* Create got.conf(5). */
1532 err = create_gotconfig(proto, host, port, remote_repo_path,
1533 default_branch, fetch_all_branches, wanted_branches,
1534 wanted_refs, mirror_references, repo);
1538 /* Create a config file Git can understand. */
1539 return create_gitconfig(git_url, default_branch, fetch_all_branches,
1540 wanted_branches, wanted_refs, mirror_references, repo);
1543 static const struct got_error *
1544 cmd_clone(int argc, char *argv[])
1546 const struct got_error *error = NULL;
1547 const char *uri, *dirname;
1548 char *proto, *host, *port, *repo_name, *server_path;
1549 char *default_destdir = NULL, *id_str = NULL;
1550 const char *repo_path;
1551 struct got_repository *repo = NULL;
1552 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1553 struct got_pathlist_entry *pe;
1554 struct got_object_id *pack_hash = NULL;
1555 int ch, fetchfd = -1, fetchstatus;
1556 pid_t fetchpid = -1;
1557 struct got_fetch_progress_arg fpa;
1558 char *git_url = NULL;
1559 int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
1560 int bflag = 0, list_refs_only = 0;
1561 int *pack_fds = NULL;
1564 TAILQ_INIT(&symrefs);
1565 TAILQ_INIT(&wanted_branches);
1566 TAILQ_INIT(&wanted_refs);
1568 while ((ch = getopt(argc, argv, "ab:lmqR:v")) != -1) {
1571 fetch_all_branches = 1;
1574 error = got_pathlist_append(&wanted_branches,
1584 mirror_references = 1;
1590 error = got_pathlist_append(&wanted_refs,
1598 else if (verbosity < 3)
1609 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1610 option_conflict('a', 'b');
1611 if (list_refs_only) {
1612 if (!TAILQ_EMPTY(&wanted_branches))
1613 option_conflict('l', 'b');
1614 if (fetch_all_branches)
1615 option_conflict('l', 'a');
1616 if (mirror_references)
1617 option_conflict('l', 'm');
1618 if (!TAILQ_EMPTY(&wanted_refs))
1619 option_conflict('l', 'R');
1631 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
1636 if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1637 host, port ? ":" : "", port ? port : "",
1638 server_path[0] != '/' ? "/" : "", server_path) == -1) {
1639 error = got_error_from_errno("asprintf");
1643 if (strcmp(proto, "git") == 0) {
1645 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1646 "sendfd dns inet unveil", NULL) == -1)
1649 } else if (strcmp(proto, "git+ssh") == 0 ||
1650 strcmp(proto, "ssh") == 0) {
1652 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1653 "sendfd unveil", NULL) == -1)
1656 } else if (strcmp(proto, "http") == 0 ||
1657 strcmp(proto, "git+http") == 0) {
1658 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1661 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1664 if (dirname == NULL) {
1665 if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1666 error = got_error_from_errno("asprintf");
1669 repo_path = default_destdir;
1671 repo_path = dirname;
1673 if (!list_refs_only) {
1674 error = got_path_mkdir(repo_path);
1676 (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1677 !(error->code == GOT_ERR_ERRNO && errno == EEXIST)))
1679 if (!got_path_dir_is_empty(repo_path)) {
1680 error = got_error_path(repo_path,
1681 GOT_ERR_DIR_NOT_EMPTY);
1686 error = got_dial_apply_unveil(proto);
1690 error = apply_unveil(repo_path, 0, NULL);
1695 printf("Connecting to %s\n", git_url);
1697 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1698 server_path, verbosity);
1702 if (!list_refs_only) {
1703 error = got_repo_init(repo_path, NULL);
1706 error = got_repo_pack_fds_open(&pack_fds);
1709 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
1714 fpa.last_scaled_size[0] = '\0';
1715 fpa.last_p_indexed = -1;
1716 fpa.last_p_resolved = -1;
1717 fpa.verbosity = verbosity;
1718 fpa.create_configs = 1;
1719 fpa.configs_created = 0;
1721 fpa.config_info.symrefs = &symrefs;
1722 fpa.config_info.wanted_branches = &wanted_branches;
1723 fpa.config_info.wanted_refs = &wanted_refs;
1724 fpa.config_info.proto = proto;
1725 fpa.config_info.host = host;
1726 fpa.config_info.port = port;
1727 fpa.config_info.remote_repo_path = server_path;
1728 fpa.config_info.git_url = git_url;
1729 fpa.config_info.fetch_all_branches = fetch_all_branches;
1730 fpa.config_info.mirror_references = mirror_references;
1731 error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1732 GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1733 fetch_all_branches, &wanted_branches, &wanted_refs,
1734 list_refs_only, verbosity, fetchfd, repo, NULL, NULL, bflag,
1735 fetch_progress, &fpa);
1739 if (list_refs_only) {
1740 error = list_remote_refs(&symrefs, &refs);
1744 if (pack_hash == NULL) {
1745 error = got_error_fmt(GOT_ERR_FETCH_FAILED, "%s",
1746 "server sent an empty pack file");
1749 error = got_object_id_str(&id_str, pack_hash);
1753 printf("\nFetched %s.pack\n", id_str);
1756 /* Set up references provided with the pack file. */
1757 TAILQ_FOREACH(pe, &refs, entry) {
1758 const char *refname = pe->path;
1759 struct got_object_id *id = pe->data;
1760 char *remote_refname;
1762 if (is_wanted_ref(&wanted_refs, refname) &&
1763 !mirror_references) {
1764 error = create_wanted_ref(refname, id,
1765 GOT_FETCH_DEFAULT_REMOTE_NAME,
1766 verbosity - 1, repo);
1772 error = create_ref(refname, id, verbosity - 1, repo);
1776 if (mirror_references)
1779 if (strncmp("refs/heads/", refname, 11) != 0)
1782 if (asprintf(&remote_refname,
1783 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1784 refname + 11) == -1) {
1785 error = got_error_from_errno("asprintf");
1788 error = create_ref(remote_refname, id, verbosity - 1, repo);
1789 free(remote_refname);
1794 /* Set the HEAD reference if the server provided one. */
1795 TAILQ_FOREACH(pe, &symrefs, entry) {
1796 struct got_reference *target_ref;
1797 const char *refname = pe->path;
1798 const char *target = pe->data;
1799 char *remote_refname = NULL, *remote_target = NULL;
1801 if (strcmp(refname, GOT_REF_HEAD) != 0)
1804 error = got_ref_open(&target_ref, repo, target, 0);
1806 if (error->code == GOT_ERR_NOT_REF) {
1813 error = create_symref(refname, target_ref, verbosity, repo);
1814 got_ref_close(target_ref);
1818 if (mirror_references)
1821 if (strncmp("refs/heads/", target, 11) != 0)
1824 if (asprintf(&remote_refname,
1825 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1827 error = got_error_from_errno("asprintf");
1830 if (asprintf(&remote_target,
1831 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1832 target + 11) == -1) {
1833 error = got_error_from_errno("asprintf");
1834 free(remote_refname);
1837 error = got_ref_open(&target_ref, repo, remote_target, 0);
1839 free(remote_refname);
1840 free(remote_target);
1841 if (error->code == GOT_ERR_NOT_REF) {
1847 error = create_symref(remote_refname, target_ref,
1848 verbosity - 1, repo);
1849 free(remote_refname);
1850 free(remote_target);
1851 got_ref_close(target_ref);
1857 * We failed to set the HEAD reference. If we asked for
1858 * a set of wanted branches use the first of one of those
1859 * which could be fetched instead.
1861 TAILQ_FOREACH(pe, &wanted_branches, entry) {
1862 const char *target = pe->path;
1863 struct got_reference *target_ref;
1865 error = got_ref_open(&target_ref, repo, target, 0);
1867 if (error->code == GOT_ERR_NOT_REF) {
1874 error = create_symref(GOT_REF_HEAD, target_ref,
1876 got_ref_close(target_ref);
1882 if (!fpa.configs_created && pe != NULL) {
1883 error = create_config_files(fpa.config_info.proto,
1884 fpa.config_info.host, fpa.config_info.port,
1885 fpa.config_info.remote_repo_path,
1886 fpa.config_info.git_url,
1887 fpa.config_info.fetch_all_branches,
1888 fpa.config_info.mirror_references,
1889 fpa.config_info.symrefs,
1890 fpa.config_info.wanted_branches,
1891 fpa.config_info.wanted_refs, fpa.repo);
1898 printf("Created %s repository '%s'\n",
1899 mirror_references ? "mirrored" : "cloned", repo_path);
1902 const struct got_error *pack_err =
1903 got_repo_pack_fds_close(pack_fds);
1908 if (kill(fetchpid, SIGTERM) == -1)
1909 error = got_error_from_errno("kill");
1910 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1911 error = got_error_from_errno("waitpid");
1913 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1914 error = got_error_from_errno("close");
1916 const struct got_error *close_err = got_repo_close(repo);
1920 got_pathlist_free(&refs, GOT_PATHLIST_FREE_ALL);
1921 got_pathlist_free(&symrefs, GOT_PATHLIST_FREE_ALL);
1922 got_pathlist_free(&wanted_branches, GOT_PATHLIST_FREE_NONE);
1923 got_pathlist_free(&wanted_refs, GOT_PATHLIST_FREE_NONE);
1930 free(default_destdir);
1935 static const struct got_error *
1936 update_ref(struct got_reference *ref, struct got_object_id *new_id,
1937 int replace_tags, int verbosity, struct got_repository *repo)
1939 const struct got_error *err = NULL;
1940 char *new_id_str = NULL;
1941 struct got_object_id *old_id = NULL;
1943 err = got_object_id_str(&new_id_str, new_id);
1947 if (!replace_tags &&
1948 strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1949 err = got_ref_resolve(&old_id, repo, ref);
1952 if (got_object_id_cmp(old_id, new_id) == 0)
1954 if (verbosity >= 0) {
1955 printf("Rejecting update of existing tag %s: %s\n",
1956 got_ref_get_name(ref), new_id_str);
1961 if (got_ref_is_symbolic(ref)) {
1962 if (verbosity >= 0) {
1963 printf("Replacing reference %s: %s\n",
1964 got_ref_get_name(ref),
1965 got_ref_get_symref_target(ref));
1967 err = got_ref_change_symref_to_ref(ref, new_id);
1970 err = got_ref_write(ref, repo);
1974 err = got_ref_resolve(&old_id, repo, ref);
1977 if (got_object_id_cmp(old_id, new_id) == 0)
1980 err = got_ref_change_ref(ref, new_id);
1983 err = got_ref_write(ref, repo);
1989 printf("Updated %s: %s\n", got_ref_get_name(ref),
1997 static const struct got_error *
1998 update_symref(const char *refname, struct got_reference *target_ref,
1999 int verbosity, struct got_repository *repo)
2001 const struct got_error *err = NULL, *unlock_err;
2002 struct got_reference *symref;
2003 int symref_is_locked = 0;
2005 err = got_ref_open(&symref, repo, refname, 1);
2007 if (err->code != GOT_ERR_NOT_REF)
2009 err = got_ref_alloc_symref(&symref, refname, target_ref);
2013 err = got_ref_write(symref, repo);
2018 printf("Created reference %s: %s\n",
2019 got_ref_get_name(symref),
2020 got_ref_get_symref_target(symref));
2022 symref_is_locked = 1;
2024 if (strcmp(got_ref_get_symref_target(symref),
2025 got_ref_get_name(target_ref)) == 0)
2028 err = got_ref_change_symref(symref,
2029 got_ref_get_name(target_ref));
2033 err = got_ref_write(symref, repo);
2038 printf("Updated %s: %s\n", got_ref_get_name(symref),
2039 got_ref_get_symref_target(symref));
2043 if (symref_is_locked) {
2044 unlock_err = got_ref_unlock(symref);
2045 if (unlock_err && err == NULL)
2048 got_ref_close(symref);
2055 fprintf(stderr, "usage: %s fetch [-adlqtvX] [-b branch] "
2056 "[-R reference] [-r repository-path] [remote-repository]\n",
2061 static const struct got_error *
2062 delete_missing_ref(struct got_reference *ref,
2063 int verbosity, struct got_repository *repo)
2065 const struct got_error *err = NULL;
2066 struct got_object_id *id = NULL;
2067 char *id_str = NULL;
2069 if (got_ref_is_symbolic(ref)) {
2070 err = got_ref_delete(ref, repo);
2073 if (verbosity >= 0) {
2074 printf("Deleted %s: %s\n",
2075 got_ref_get_name(ref),
2076 got_ref_get_symref_target(ref));
2079 err = got_ref_resolve(&id, repo, ref);
2082 err = got_object_id_str(&id_str, id);
2086 err = got_ref_delete(ref, repo);
2089 if (verbosity >= 0) {
2090 printf("Deleted %s: %s\n",
2091 got_ref_get_name(ref), id_str);
2100 static const struct got_error *
2101 delete_missing_refs(struct got_pathlist_head *their_refs,
2102 struct got_pathlist_head *their_symrefs,
2103 const struct got_remote_repo *remote,
2104 int verbosity, struct got_repository *repo)
2106 const struct got_error *err = NULL, *unlock_err;
2107 struct got_reflist_head my_refs;
2108 struct got_reflist_entry *re;
2109 struct got_pathlist_entry *pe;
2110 char *remote_namespace = NULL;
2111 char *local_refname = NULL;
2113 TAILQ_INIT(&my_refs);
2115 if (asprintf(&remote_namespace, "refs/remotes/%s/", remote->name)
2117 return got_error_from_errno("asprintf");
2119 err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
2123 TAILQ_FOREACH(re, &my_refs, entry) {
2124 const char *refname = got_ref_get_name(re->ref);
2125 const char *their_refname;
2127 if (remote->mirror_references) {
2128 their_refname = refname;
2130 if (strncmp(refname, remote_namespace,
2131 strlen(remote_namespace)) == 0) {
2132 if (strcmp(refname + strlen(remote_namespace),
2135 if (asprintf(&local_refname, "refs/heads/%s",
2136 refname + strlen(remote_namespace)) == -1) {
2137 err = got_error_from_errno("asprintf");
2140 } else if (strncmp(refname, "refs/tags/", 10) != 0)
2143 their_refname = local_refname;
2146 TAILQ_FOREACH(pe, their_refs, entry) {
2147 if (strcmp(their_refname, pe->path) == 0)
2153 TAILQ_FOREACH(pe, their_symrefs, entry) {
2154 if (strcmp(their_refname, pe->path) == 0)
2160 err = delete_missing_ref(re->ref, verbosity, repo);
2164 if (local_refname) {
2165 struct got_reference *ref;
2166 err = got_ref_open(&ref, repo, local_refname, 1);
2168 if (err->code != GOT_ERR_NOT_REF)
2170 free(local_refname);
2171 local_refname = NULL;
2174 err = delete_missing_ref(ref, verbosity, repo);
2177 unlock_err = got_ref_unlock(ref);
2179 if (unlock_err && err == NULL) {
2184 free(local_refname);
2185 local_refname = NULL;
2189 got_ref_list_free(&my_refs);
2190 free(remote_namespace);
2191 free(local_refname);
2195 static const struct got_error *
2196 update_wanted_ref(const char *refname, struct got_object_id *id,
2197 const char *remote_repo_name, int verbosity, struct got_repository *repo)
2199 const struct got_error *err, *unlock_err;
2200 char *remote_refname;
2201 struct got_reference *ref;
2203 if (strncmp("refs/", refname, 5) == 0)
2206 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2207 remote_repo_name, refname) == -1)
2208 return got_error_from_errno("asprintf");
2210 err = got_ref_open(&ref, repo, remote_refname, 1);
2212 if (err->code != GOT_ERR_NOT_REF)
2214 err = create_ref(remote_refname, id, verbosity, repo);
2216 err = update_ref(ref, id, 0, verbosity, repo);
2217 unlock_err = got_ref_unlock(ref);
2218 if (unlock_err && err == NULL)
2223 free(remote_refname);
2227 static const struct got_error *
2228 delete_ref(struct got_repository *repo, struct got_reference *ref)
2230 const struct got_error *err = NULL;
2231 struct got_object_id *id = NULL;
2232 char *id_str = NULL;
2235 if (got_ref_is_symbolic(ref)) {
2236 target = got_ref_get_symref_target(ref);
2238 err = got_ref_resolve(&id, repo, ref);
2241 err = got_object_id_str(&id_str, id);
2247 err = got_ref_delete(ref, repo);
2251 printf("Deleted %s: %s\n", got_ref_get_name(ref), target);
2258 static const struct got_error *
2259 delete_refs_for_remote(struct got_repository *repo, const char *remote_name)
2261 const struct got_error *err = NULL;
2262 struct got_reflist_head refs;
2263 struct got_reflist_entry *re;
2268 if (asprintf(&prefix, "refs/remotes/%s", remote_name) == -1) {
2269 err = got_error_from_errno("asprintf");
2272 err = got_ref_list(&refs, repo, prefix, got_ref_cmp_by_name, NULL);
2276 TAILQ_FOREACH(re, &refs, entry)
2277 delete_ref(repo, re->ref);
2279 got_ref_list_free(&refs);
2283 static const struct got_error *
2284 cmd_fetch(int argc, char *argv[])
2286 const struct got_error *error = NULL, *unlock_err;
2287 char *cwd = NULL, *repo_path = NULL;
2288 const char *remote_name;
2289 char *proto = NULL, *host = NULL, *port = NULL;
2290 char *repo_name = NULL, *server_path = NULL;
2291 const struct got_remote_repo *remotes, *remote = NULL;
2293 char *id_str = NULL;
2294 struct got_repository *repo = NULL;
2295 struct got_worktree *worktree = NULL;
2296 const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
2297 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
2298 struct got_pathlist_entry *pe;
2299 struct got_reflist_head remote_refs;
2300 struct got_reflist_entry *re;
2301 struct got_object_id *pack_hash = NULL;
2302 int i, ch, fetchfd = -1, fetchstatus;
2303 pid_t fetchpid = -1;
2304 struct got_fetch_progress_arg fpa;
2305 int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
2306 int delete_refs = 0, replace_tags = 0, delete_remote = 0;
2307 int *pack_fds = NULL, have_bflag = 0;
2308 const char *remote_head = NULL, *worktree_branch = NULL;
2311 TAILQ_INIT(&symrefs);
2312 TAILQ_INIT(&remote_refs);
2313 TAILQ_INIT(&wanted_branches);
2314 TAILQ_INIT(&wanted_refs);
2316 while ((ch = getopt(argc, argv, "ab:dlqR:r:tvX")) != -1) {
2319 fetch_all_branches = 1;
2322 error = got_pathlist_append(&wanted_branches,
2338 error = got_pathlist_append(&wanted_refs,
2344 repo_path = realpath(optarg, NULL);
2345 if (repo_path == NULL)
2346 return got_error_from_errno2("realpath",
2348 got_path_strip_trailing_slashes(repo_path);
2356 else if (verbosity < 3)
2370 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
2371 option_conflict('a', 'b');
2372 if (list_refs_only) {
2373 if (!TAILQ_EMPTY(&wanted_branches))
2374 option_conflict('l', 'b');
2375 if (fetch_all_branches)
2376 option_conflict('l', 'a');
2378 option_conflict('l', 'd');
2380 option_conflict('l', 'X');
2382 if (delete_remote) {
2383 if (fetch_all_branches)
2384 option_conflict('X', 'a');
2385 if (!TAILQ_EMPTY(&wanted_branches))
2386 option_conflict('X', 'b');
2388 option_conflict('X', 'd');
2390 option_conflict('X', 't');
2391 if (!TAILQ_EMPTY(&wanted_refs))
2392 option_conflict('X', 'R');
2397 errx(1, "-X option requires a remote name");
2398 remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
2399 } else if (argc == 1)
2400 remote_name = argv[0];
2404 cwd = getcwd(NULL, 0);
2406 error = got_error_from_errno("getcwd");
2410 error = got_repo_pack_fds_open(&pack_fds);
2414 if (repo_path == NULL) {
2415 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
2416 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2422 strdup(got_worktree_get_repo_path(worktree));
2423 if (repo_path == NULL)
2424 error = got_error_from_errno("strdup");
2428 repo_path = strdup(cwd);
2429 if (repo_path == NULL) {
2430 error = got_error_from_errno("strdup");
2436 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
2440 if (delete_remote) {
2441 error = delete_refs_for_remote(repo, remote_name);
2442 goto done; /* nothing else to do */
2446 worktree_conf = got_worktree_get_gotconfig(worktree);
2447 if (worktree_conf) {
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 repo_conf = got_repo_get_gotconfig(repo);
2461 got_gotconfig_get_remotes(&nremotes, &remotes,
2463 for (i = 0; i < nremotes; i++) {
2464 if (strcmp(remotes[i].name, remote_name) == 0) {
2465 remote = &remotes[i];
2471 if (remote == NULL) {
2472 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
2473 for (i = 0; i < nremotes; i++) {
2474 if (strcmp(remotes[i].name, remote_name) == 0) {
2475 remote = &remotes[i];
2480 if (remote == NULL) {
2481 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
2485 if (TAILQ_EMPTY(&wanted_branches)) {
2486 if (!fetch_all_branches)
2487 fetch_all_branches = remote->fetch_all_branches;
2488 for (i = 0; i < remote->nfetch_branches; i++) {
2489 error = got_pathlist_append(&wanted_branches,
2490 remote->fetch_branches[i], NULL);
2495 if (TAILQ_EMPTY(&wanted_refs)) {
2496 for (i = 0; i < remote->nfetch_refs; i++) {
2497 error = got_pathlist_append(&wanted_refs,
2498 remote->fetch_refs[i], NULL);
2504 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
2505 &repo_name, remote->fetch_url);
2509 if (strcmp(proto, "git") == 0) {
2511 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2512 "sendfd dns inet unveil", NULL) == -1)
2515 } else if (strcmp(proto, "git+ssh") == 0 ||
2516 strcmp(proto, "ssh") == 0) {
2518 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2519 "sendfd unveil", NULL) == -1)
2522 } else if (strcmp(proto, "http") == 0 ||
2523 strcmp(proto, "git+http") == 0) {
2524 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
2527 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
2531 error = got_dial_apply_unveil(proto);
2535 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
2539 if (verbosity >= 0) {
2540 printf("Connecting to \"%s\" %s://%s%s%s%s%s\n",
2541 remote->name, proto, host,
2542 port ? ":" : "", port ? port : "",
2543 *server_path == '/' ? "" : "/", server_path);
2546 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
2547 server_path, verbosity);
2553 * If set, get this remote's HEAD ref target so
2554 * if it has changed on the server we can fetch it.
2556 error = got_ref_list(&remote_refs, repo, "refs/remotes",
2557 got_ref_cmp_by_name, repo);
2561 TAILQ_FOREACH(re, &remote_refs, entry) {
2562 const char *remote_refname, *remote_target;
2563 size_t remote_name_len;
2565 if (!got_ref_is_symbolic(re->ref))
2568 remote_name_len = strlen(remote->name);
2569 remote_refname = got_ref_get_name(re->ref);
2571 /* we only want refs/remotes/$remote->name/HEAD */
2572 if (strncmp(remote_refname + 13, remote->name,
2573 remote_name_len) != 0)
2576 if (strcmp(remote_refname + remote_name_len + 14,
2581 * Take the name itself because we already
2582 * only match with refs/heads/ in fetch_pack().
2584 remote_target = got_ref_get_symref_target(re->ref);
2585 remote_head = remote_target + remote_name_len + 14;
2590 const char *refname;
2592 refname = got_worktree_get_head_ref_name(worktree);
2593 if (strncmp(refname, "refs/heads/", 11) == 0)
2594 worktree_branch = refname;
2598 fpa.last_scaled_size[0] = '\0';
2599 fpa.last_p_indexed = -1;
2600 fpa.last_p_resolved = -1;
2601 fpa.verbosity = verbosity;
2603 fpa.create_configs = 0;
2604 fpa.configs_created = 0;
2605 memset(&fpa.config_info, 0, sizeof(fpa.config_info));
2607 error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
2608 remote->mirror_references, fetch_all_branches, &wanted_branches,
2609 &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
2610 worktree_branch, remote_head, have_bflag, fetch_progress, &fpa);
2614 if (list_refs_only) {
2615 error = list_remote_refs(&symrefs, &refs);
2619 if (pack_hash == NULL) {
2621 printf("Already up-to-date\n");
2622 } else if (verbosity >= 0) {
2623 error = got_object_id_str(&id_str, pack_hash);
2626 printf("\nFetched %s.pack\n", id_str);
2631 /* Update references provided with the pack file. */
2632 TAILQ_FOREACH(pe, &refs, entry) {
2633 const char *refname = pe->path;
2634 struct got_object_id *id = pe->data;
2635 struct got_reference *ref;
2636 char *remote_refname;
2638 if (is_wanted_ref(&wanted_refs, refname) &&
2639 !remote->mirror_references) {
2640 error = update_wanted_ref(refname, id,
2641 remote->name, verbosity, repo);
2647 if (remote->mirror_references ||
2648 strncmp("refs/tags/", refname, 10) == 0) {
2649 error = got_ref_open(&ref, repo, refname, 1);
2651 if (error->code != GOT_ERR_NOT_REF)
2653 error = create_ref(refname, id, verbosity,
2658 error = update_ref(ref, id, replace_tags,
2660 unlock_err = got_ref_unlock(ref);
2661 if (unlock_err && error == NULL)
2667 } else if (strncmp("refs/heads/", refname, 11) == 0) {
2668 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2669 remote_name, refname + 11) == -1) {
2670 error = got_error_from_errno("asprintf");
2674 error = got_ref_open(&ref, repo, remote_refname, 1);
2676 if (error->code != GOT_ERR_NOT_REF)
2678 error = create_ref(remote_refname, id,
2683 error = update_ref(ref, id, replace_tags,
2685 unlock_err = got_ref_unlock(ref);
2686 if (unlock_err && error == NULL)
2693 /* Also create a local branch if none exists yet. */
2694 error = got_ref_open(&ref, repo, refname, 1);
2696 if (error->code != GOT_ERR_NOT_REF)
2698 error = create_ref(refname, id, verbosity,
2703 unlock_err = got_ref_unlock(ref);
2704 if (unlock_err && error == NULL)
2711 error = delete_missing_refs(&refs, &symrefs, remote,
2717 if (!remote->mirror_references) {
2718 /* Update remote HEAD reference if the server provided one. */
2719 TAILQ_FOREACH(pe, &symrefs, entry) {
2720 struct got_reference *target_ref;
2721 const char *refname = pe->path;
2722 const char *target = pe->data;
2723 char *remote_refname = NULL, *remote_target = NULL;
2725 if (strcmp(refname, GOT_REF_HEAD) != 0)
2728 if (strncmp("refs/heads/", target, 11) != 0)
2731 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2732 remote->name, refname) == -1) {
2733 error = got_error_from_errno("asprintf");
2736 if (asprintf(&remote_target, "refs/remotes/%s/%s",
2737 remote->name, target + 11) == -1) {
2738 error = got_error_from_errno("asprintf");
2739 free(remote_refname);
2743 error = got_ref_open(&target_ref, repo, remote_target,
2746 free(remote_refname);
2747 free(remote_target);
2748 if (error->code == GOT_ERR_NOT_REF) {
2754 error = update_symref(remote_refname, target_ref,
2756 free(remote_refname);
2757 free(remote_target);
2758 got_ref_close(target_ref);
2765 if (kill(fetchpid, SIGTERM) == -1)
2766 error = got_error_from_errno("kill");
2767 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
2768 error = got_error_from_errno("waitpid");
2770 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
2771 error = got_error_from_errno("close");
2773 const struct got_error *close_err = got_repo_close(repo);
2778 got_worktree_close(worktree);
2780 const struct got_error *pack_err =
2781 got_repo_pack_fds_close(pack_fds);
2785 got_pathlist_free(&refs, GOT_PATHLIST_FREE_ALL);
2786 got_pathlist_free(&symrefs, GOT_PATHLIST_FREE_ALL);
2787 got_pathlist_free(&wanted_branches, GOT_PATHLIST_FREE_NONE);
2788 got_pathlist_free(&wanted_refs, GOT_PATHLIST_FREE_NONE);
2789 got_ref_list_free(&remote_refs);
2804 usage_checkout(void)
2806 fprintf(stderr, "usage: %s checkout [-Eq] [-b branch] [-c commit] "
2807 "[-p path-prefix] repository-path [work-tree-path]\n",
2813 show_worktree_base_ref_warning(void)
2815 fprintf(stderr, "%s: warning: could not create a reference "
2816 "to the work tree's base commit; the commit could be "
2817 "garbage-collected by Git or 'gotadmin cleanup'; making the "
2818 "repository writable and running 'got update' will prevent this\n",
2822 struct got_checkout_progress_arg {
2823 const char *worktree_path;
2824 int had_base_commit_ref_error;
2828 static const struct got_error *
2829 checkout_progress(void *arg, unsigned char status, const char *path)
2831 struct got_checkout_progress_arg *a = arg;
2833 /* Base commit bump happens silently. */
2834 if (status == GOT_STATUS_BUMP_BASE)
2837 if (status == GOT_STATUS_BASE_REF_ERR) {
2838 a->had_base_commit_ref_error = 1;
2842 while (path[0] == '/')
2845 if (a->verbosity >= 0)
2846 printf("%c %s/%s\n", status, a->worktree_path, path);
2851 static const struct got_error *
2852 check_cancelled(void *arg)
2854 if (sigint_received || sigpipe_received)
2855 return got_error(GOT_ERR_CANCELLED);
2859 static const struct got_error *
2860 check_linear_ancestry(struct got_object_id *commit_id,
2861 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2862 struct got_repository *repo)
2864 const struct got_error *err = NULL;
2865 struct got_object_id *yca_id;
2867 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2868 commit_id, base_commit_id, 1, repo, check_cancelled, NULL);
2873 return got_error(GOT_ERR_ANCESTRY);
2876 * Require a straight line of history between the target commit
2877 * and the work tree's base commit.
2879 * Non-linear situations such as this require a rebase:
2881 * (commit) D F (base_commit)
2889 * 'got update' only handles linear cases:
2890 * Update forwards in time: A (base/yca) - B - C - D (commit)
2891 * Update backwards in time: D (base) - C - B - A (commit/yca)
2893 if (allow_forwards_in_time_only) {
2894 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2895 return got_error(GOT_ERR_ANCESTRY);
2896 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2897 got_object_id_cmp(base_commit_id, yca_id) != 0)
2898 return got_error(GOT_ERR_ANCESTRY);
2904 static const struct got_error *
2905 check_same_branch(struct got_object_id *commit_id,
2906 struct got_reference *head_ref, struct got_repository *repo)
2908 const struct got_error *err = NULL;
2909 struct got_commit_graph *graph = NULL;
2910 struct got_object_id *head_commit_id = NULL;
2912 err = got_ref_resolve(&head_commit_id, repo, head_ref);
2916 if (got_object_id_cmp(head_commit_id, commit_id) == 0)
2919 err = got_commit_graph_open(&graph, "/", 1);
2923 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2924 check_cancelled, NULL);
2929 struct got_object_id id;
2931 err = got_commit_graph_iter_next(&id, graph, repo,
2932 check_cancelled, NULL);
2934 if (err->code == GOT_ERR_ITER_COMPLETED)
2935 err = got_error(GOT_ERR_ANCESTRY);
2939 if (got_object_id_cmp(&id, commit_id) == 0)
2944 got_commit_graph_close(graph);
2945 free(head_commit_id);
2949 static const struct got_error *
2950 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2952 static char msg[512];
2953 const char *branch_name;
2955 if (got_ref_is_symbolic(ref))
2956 branch_name = got_ref_get_symref_target(ref);
2958 branch_name = got_ref_get_name(ref);
2960 if (strncmp("refs/heads/", branch_name, 11) == 0)
2963 snprintf(msg, sizeof(msg),
2964 "target commit is not contained in branch '%s'; "
2965 "the branch to use must be specified with -b; "
2966 "if necessary a new branch can be created for "
2967 "this commit with 'got branch -c %s BRANCH_NAME'",
2968 branch_name, commit_id_str);
2970 return got_error_msg(GOT_ERR_ANCESTRY, msg);
2973 static const struct got_error *
2974 cmd_checkout(int argc, char *argv[])
2976 const struct got_error *error = NULL;
2977 struct got_repository *repo = NULL;
2978 struct got_reference *head_ref = NULL, *ref = NULL;
2979 struct got_worktree *worktree = NULL;
2980 char *repo_path = NULL;
2981 char *worktree_path = NULL;
2982 const char *path_prefix = "";
2983 const char *branch_name = GOT_REF_HEAD, *refname = NULL;
2984 char *commit_id_str = NULL, *keyword_idstr = NULL;
2985 struct got_object_id *commit_id = NULL;
2987 int ch, same_path_prefix, allow_nonempty = 0, verbosity = 0;
2988 struct got_pathlist_head paths;
2989 struct got_checkout_progress_arg cpa;
2990 int *pack_fds = NULL;
2995 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2996 "unveil", NULL) == -1)
3000 while ((ch = getopt(argc, argv, "b:c:Ep:q")) != -1) {
3003 branch_name = optarg;
3006 commit_id_str = strdup(optarg);
3007 if (commit_id_str == NULL)
3008 return got_error_from_errno("strdup");
3014 path_prefix = optarg;
3029 char *base, *dotgit;
3031 repo_path = realpath(argv[0], NULL);
3032 if (repo_path == NULL)
3033 return got_error_from_errno2("realpath", argv[0]);
3034 cwd = getcwd(NULL, 0);
3036 error = got_error_from_errno("getcwd");
3043 error = got_path_basename(&base, path);
3046 dotgit = strstr(base, ".git");
3049 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
3050 error = got_error_from_errno("asprintf");
3055 } else if (argc == 2) {
3056 repo_path = realpath(argv[0], NULL);
3057 if (repo_path == NULL) {
3058 error = got_error_from_errno2("realpath", argv[0]);
3061 worktree_path = realpath(argv[1], NULL);
3062 if (worktree_path == NULL) {
3063 if (errno != ENOENT) {
3064 error = got_error_from_errno2("realpath",
3068 worktree_path = strdup(argv[1]);
3069 if (worktree_path == NULL) {
3070 error = got_error_from_errno("strdup");
3077 got_path_strip_trailing_slashes(repo_path);
3078 got_path_strip_trailing_slashes(worktree_path);
3080 error = got_repo_pack_fds_open(&pack_fds);
3084 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3088 /* Pre-create work tree path for unveil(2) */
3089 error = got_path_mkdir(worktree_path);
3091 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
3092 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
3094 if (!allow_nonempty &&
3095 !got_path_dir_is_empty(worktree_path)) {
3096 error = got_error_path(worktree_path,
3097 GOT_ERR_DIR_NOT_EMPTY);
3102 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
3106 error = got_ref_open(&head_ref, repo, branch_name, 0);
3110 error = got_worktree_init(worktree_path, head_ref, path_prefix,
3111 GOT_WORKTREE_GOT_DIR, repo);
3112 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
3115 error = got_worktree_open(&worktree, worktree_path,
3116 GOT_WORKTREE_GOT_DIR);
3120 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
3124 if (!same_path_prefix) {
3125 error = got_error(GOT_ERR_PATH_PREFIX);
3129 if (commit_id_str) {
3130 struct got_reflist_head refs;
3132 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3137 error = got_keyword_to_idstr(&keyword_idstr, commit_id_str,
3141 if (keyword_idstr != NULL) {
3142 free(commit_id_str);
3143 commit_id_str = keyword_idstr;
3146 error = got_repo_match_object_id(&commit_id, NULL,
3147 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3148 got_ref_list_free(&refs);
3151 error = check_linear_ancestry(commit_id,
3152 got_worktree_get_base_commit_id(worktree), 0, repo);
3153 if (error != NULL) {
3154 if (error->code == GOT_ERR_ANCESTRY) {
3155 error = checkout_ancestry_error(
3156 head_ref, commit_id_str);
3160 error = check_same_branch(commit_id, head_ref, repo);
3162 if (error->code == GOT_ERR_ANCESTRY) {
3163 error = checkout_ancestry_error(
3164 head_ref, commit_id_str);
3168 error = got_worktree_set_base_commit_id(worktree, repo,
3172 /* Expand potentially abbreviated commit ID string. */
3173 free(commit_id_str);
3174 error = got_object_id_str(&commit_id_str, commit_id);
3178 commit_id = got_object_id_dup(
3179 got_worktree_get_base_commit_id(worktree));
3180 if (commit_id == NULL) {
3181 error = got_error_from_errno("got_object_id_dup");
3184 error = got_object_id_str(&commit_id_str, commit_id);
3189 error = got_pathlist_append(&paths, "", NULL);
3192 cpa.worktree_path = worktree_path;
3193 cpa.had_base_commit_ref_error = 0;
3194 cpa.verbosity = verbosity;
3195 error = got_worktree_checkout_files(worktree, &paths, repo,
3196 checkout_progress, &cpa, check_cancelled, NULL);
3200 if (got_ref_is_symbolic(head_ref)) {
3201 error = got_ref_resolve_symbolic(&ref, repo, head_ref);
3204 refname = got_ref_get_name(ref);
3206 refname = got_ref_get_name(head_ref);
3207 printf("Checked out %s: %s\n", refname, commit_id_str);
3208 printf("Now shut up and hack\n");
3209 if (cpa.had_base_commit_ref_error)
3210 show_worktree_base_ref_warning();
3213 const struct got_error *pack_err =
3214 got_repo_pack_fds_close(pack_fds);
3219 got_ref_close(head_ref);
3223 const struct got_error *close_err = got_repo_close(repo);
3227 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
3228 free(commit_id_str);
3231 free(worktree_path);
3236 struct got_update_progress_arg {
3248 print_update_progress_stats(struct got_update_progress_arg *upa)
3250 if (!upa->did_something)
3253 if (upa->conflicts > 0)
3254 printf("Files with new merge conflicts: %d\n", upa->conflicts);
3255 if (upa->obstructed > 0)
3256 printf("File paths obstructed by a non-regular file: %d\n",
3258 if (upa->not_updated > 0)
3259 printf("Files not updated because of existing merge "
3260 "conflicts: %d\n", upa->not_updated);
3264 * The meaning of some status codes differs between merge-style operations and
3265 * update operations. For example, the ! status code means "file was missing"
3266 * if changes were merged into the work tree, and "missing file was restored"
3267 * if the work tree was updated. This function should be used by any operation
3268 * which merges changes into the work tree without updating the work tree.
3271 print_merge_progress_stats(struct got_update_progress_arg *upa)
3273 if (!upa->did_something)
3276 if (upa->conflicts > 0)
3277 printf("Files with new merge conflicts: %d\n", upa->conflicts);
3278 if (upa->obstructed > 0)
3279 printf("File paths obstructed by a non-regular file: %d\n",
3281 if (upa->missing > 0)
3282 printf("Files which had incoming changes but could not be "
3283 "found in the work tree: %d\n", upa->missing);
3284 if (upa->not_deleted > 0)
3285 printf("Files not deleted due to differences in deleted "
3286 "content: %d\n", upa->not_deleted);
3287 if (upa->unversioned > 0)
3288 printf("Files not merged because an unversioned file was "
3289 "found in the work tree: %d\n", upa->unversioned);
3295 fprintf(stderr, "usage: %s update [-q] [-b branch] [-c commit] "
3296 "[path ...]\n", getprogname());
3300 static const struct got_error *
3301 update_progress(void *arg, unsigned char status, const char *path)
3303 struct got_update_progress_arg *upa = arg;
3305 if (status == GOT_STATUS_EXISTS ||
3306 status == GOT_STATUS_BASE_REF_ERR)
3309 upa->did_something = 1;
3311 /* Base commit bump happens silently. */
3312 if (status == GOT_STATUS_BUMP_BASE)
3315 if (status == GOT_STATUS_CONFLICT)
3317 if (status == GOT_STATUS_OBSTRUCTED)
3319 if (status == GOT_STATUS_CANNOT_UPDATE)
3321 if (status == GOT_STATUS_MISSING)
3323 if (status == GOT_STATUS_CANNOT_DELETE)
3325 if (status == GOT_STATUS_UNVERSIONED)
3328 while (path[0] == '/')
3330 if (upa->verbosity >= 0)
3331 printf("%c %s\n", status, path);
3336 static const struct got_error *
3337 switch_head_ref(struct got_reference *head_ref,
3338 struct got_object_id *commit_id, struct got_worktree *worktree,
3339 struct got_repository *repo)
3341 const struct got_error *err = NULL;
3343 int ref_has_moved = 0;
3345 /* Trivial case: switching between two different references. */
3346 if (strcmp(got_ref_get_name(head_ref),
3347 got_worktree_get_head_ref_name(worktree)) != 0) {
3348 printf("Switching work tree from %s to %s\n",
3349 got_worktree_get_head_ref_name(worktree),
3350 got_ref_get_name(head_ref));
3351 return got_worktree_set_head_ref(worktree, head_ref);
3354 err = check_linear_ancestry(commit_id,
3355 got_worktree_get_base_commit_id(worktree), 0, repo);
3357 if (err->code != GOT_ERR_ANCESTRY)
3364 /* Switching to a rebased branch with the same reference name. */
3365 err = got_object_id_str(&base_id_str,
3366 got_worktree_get_base_commit_id(worktree));
3369 printf("Reference %s now points at a different branch\n",
3370 got_worktree_get_head_ref_name(worktree));
3371 printf("Switching work tree from %s to %s\n", base_id_str,
3372 got_worktree_get_head_ref_name(worktree));
3376 static const struct got_error *
3377 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
3379 const struct got_error *err;
3382 err = got_worktree_rebase_in_progress(&in_progress, worktree);
3386 return got_error(GOT_ERR_REBASING);
3388 err = got_worktree_histedit_in_progress(&in_progress, worktree);
3392 return got_error(GOT_ERR_HISTEDIT_BUSY);
3397 static const struct got_error *
3398 check_merge_in_progress(struct got_worktree *worktree,
3399 struct got_repository *repo)
3401 const struct got_error *err;
3404 err = got_worktree_merge_in_progress(&in_progress, worktree, repo);
3408 return got_error(GOT_ERR_MERGE_BUSY);
3413 static const struct got_error *
3414 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
3415 char *argv[], struct got_worktree *worktree)
3417 const struct got_error *err = NULL;
3419 struct got_pathlist_entry *new;
3425 return got_error_from_errno("strdup");
3426 return got_pathlist_append(paths, path, NULL);
3429 for (i = 0; i < argc; i++) {
3430 err = got_worktree_resolve_path(&path, worktree, argv[i]);
3433 err = got_pathlist_insert(&new, paths, path, NULL);
3434 if (err || new == NULL /* duplicate */) {
3444 static const struct got_error *
3445 wrap_not_worktree_error(const struct got_error *orig_err,
3446 const char *cmdname, const char *path)
3448 const struct got_error *err;
3449 struct got_repository *repo;
3450 static char msg[512];
3451 int *pack_fds = NULL;
3453 err = got_repo_pack_fds_open(&pack_fds);
3457 err = got_repo_open(&repo, path, NULL, pack_fds);
3461 snprintf(msg, sizeof(msg),
3462 "'got %s' needs a work tree in addition to a git repository\n"
3463 "Work trees can be checked out from this Git repository with "
3465 "The got(1) manual page contains more information.", cmdname);
3466 err = got_error_msg(GOT_ERR_NOT_WORKTREE, msg);
3468 const struct got_error *close_err = got_repo_close(repo);
3473 const struct got_error *pack_err =
3474 got_repo_pack_fds_close(pack_fds);
3481 static const struct got_error *
3482 cmd_update(int argc, char *argv[])
3484 const struct got_error *error = NULL;
3485 struct got_repository *repo = NULL;
3486 struct got_worktree *worktree = NULL;
3487 char *worktree_path = NULL;
3488 struct got_object_id *commit_id = NULL;
3489 char *commit_id_str = NULL;
3490 const char *branch_name = NULL;
3491 struct got_reference *head_ref = NULL;
3492 struct got_pathlist_head paths;
3493 struct got_pathlist_entry *pe;
3494 int ch, verbosity = 0;
3495 struct got_update_progress_arg upa;
3496 int *pack_fds = NULL;
3501 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3502 "unveil", NULL) == -1)
3506 while ((ch = getopt(argc, argv, "b:c:q")) != -1) {
3509 branch_name = optarg;
3512 commit_id_str = strdup(optarg);
3513 if (commit_id_str == NULL)
3514 return got_error_from_errno("strdup");
3528 worktree_path = getcwd(NULL, 0);
3529 if (worktree_path == NULL) {
3530 error = got_error_from_errno("getcwd");
3534 error = got_repo_pack_fds_open(&pack_fds);
3538 error = got_worktree_open(&worktree, worktree_path,
3539 GOT_WORKTREE_GOT_DIR);
3541 if (error->code == GOT_ERR_NOT_WORKTREE)
3542 error = wrap_not_worktree_error(error, "update",
3547 error = check_rebase_or_histedit_in_progress(worktree);
3551 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3556 error = apply_unveil(got_repo_get_path(repo), 0,
3557 got_worktree_get_root_path(worktree));
3561 error = check_merge_in_progress(worktree, repo);
3565 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3569 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
3570 got_worktree_get_head_ref_name(worktree), 0);
3573 if (commit_id_str == NULL) {
3574 error = got_ref_resolve(&commit_id, repo, head_ref);
3577 error = got_object_id_str(&commit_id_str, commit_id);
3581 struct got_reflist_head refs;
3582 char *keyword_idstr = NULL;
3586 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3591 error = got_keyword_to_idstr(&keyword_idstr, commit_id_str,
3595 if (keyword_idstr != NULL) {
3596 free(commit_id_str);
3597 commit_id_str = keyword_idstr;
3600 error = got_repo_match_object_id(&commit_id, NULL,
3601 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3602 got_ref_list_free(&refs);
3603 free(commit_id_str);
3604 commit_id_str = NULL;
3607 error = got_object_id_str(&commit_id_str, commit_id);
3613 struct got_object_id *head_commit_id;
3614 TAILQ_FOREACH(pe, &paths, entry) {
3615 if (pe->path_len == 0)
3617 error = got_error_msg(GOT_ERR_BAD_PATH,
3618 "switching between branches requires that "
3619 "the entire work tree gets updated");
3622 error = got_ref_resolve(&head_commit_id, repo, head_ref);
3625 error = check_linear_ancestry(commit_id, head_commit_id, 0,
3627 free(head_commit_id);
3630 error = check_same_branch(commit_id, head_ref, repo);
3633 error = switch_head_ref(head_ref, commit_id, worktree, repo);
3637 error = check_linear_ancestry(commit_id,
3638 got_worktree_get_base_commit_id(worktree), 0, repo);
3639 if (error != NULL) {
3640 if (error->code == GOT_ERR_ANCESTRY)
3641 error = got_error(GOT_ERR_BRANCH_MOVED);
3644 error = check_same_branch(commit_id, head_ref, repo);
3649 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
3651 error = got_worktree_set_base_commit_id(worktree, repo,
3657 memset(&upa, 0, sizeof(upa));
3658 upa.verbosity = verbosity;
3659 error = got_worktree_checkout_files(worktree, &paths, repo,
3660 update_progress, &upa, check_cancelled, NULL);
3664 if (upa.did_something) {
3665 printf("Updated to %s: %s\n",
3666 got_worktree_get_head_ref_name(worktree), commit_id_str);
3668 printf("Already up-to-date\n");
3670 print_update_progress_stats(&upa);
3673 const struct got_error *pack_err =
3674 got_repo_pack_fds_close(pack_fds);
3679 const struct got_error *close_err = got_repo_close(repo);
3683 if (head_ref != NULL)
3684 got_ref_close(head_ref);
3685 free(worktree_path);
3686 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
3688 free(commit_id_str);
3692 static const struct got_error *
3693 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
3694 const char *path, int diff_context, int ignore_whitespace,
3695 int force_text_diff, struct got_diffstat_cb_arg *dsa,
3696 struct got_repository *repo, FILE *outfile)
3698 const struct got_error *err = NULL;
3699 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
3700 FILE *f1 = NULL, *f2 = NULL;
3701 int fd1 = -1, fd2 = -1;
3703 fd1 = got_opentempfd();
3705 return got_error_from_errno("got_opentempfd");
3706 fd2 = got_opentempfd();
3708 err = got_error_from_errno("got_opentempfd");
3713 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192,
3719 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192, fd2);
3723 f1 = got_opentemp();
3725 err = got_error_from_errno("got_opentemp");
3728 f2 = got_opentemp();
3730 err = got_error_from_errno("got_opentemp");
3734 while (path[0] == '/')
3736 err = got_diff_blob(NULL, NULL, blob1, blob2, f1, f2, path, path,
3737 GOT_DIFF_ALGORITHM_PATIENCE, diff_context, ignore_whitespace,
3738 force_text_diff, dsa, outfile);
3740 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3741 err = got_error_from_errno("close");
3743 got_object_blob_close(blob1);
3744 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3745 err = got_error_from_errno("close");
3747 got_object_blob_close(blob2);
3748 if (f1 && fclose(f1) == EOF && err == NULL)
3749 err = got_error_from_errno("fclose");
3750 if (f2 && fclose(f2) == EOF && err == NULL)
3751 err = got_error_from_errno("fclose");
3755 static const struct got_error *
3756 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
3757 const char *path, int diff_context, int ignore_whitespace,
3758 int force_text_diff, struct got_diffstat_cb_arg *dsa,
3759 struct got_repository *repo, FILE *outfile)
3761 const struct got_error *err = NULL;
3762 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3763 struct got_diff_blob_output_unidiff_arg arg;
3764 FILE *f1 = NULL, *f2 = NULL;
3765 int fd1 = -1, fd2 = -1;
3768 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3771 fd1 = got_opentempfd();
3773 err = got_error_from_errno("got_opentempfd");
3778 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3782 f1 = got_opentemp();
3784 err = got_error_from_errno("got_opentemp");
3788 f2 = got_opentemp();
3790 err = got_error_from_errno("got_opentemp");
3793 fd2 = got_opentempfd();
3795 err = got_error_from_errno("got_opentempfd");
3798 arg.diff_context = diff_context;
3799 arg.ignore_whitespace = ignore_whitespace;
3800 arg.force_text_diff = force_text_diff;
3802 arg.diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
3803 arg.outfile = outfile;
3806 while (path[0] == '/')
3808 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, path, path, repo,
3809 got_diff_blob_output_unidiff, &arg, 1);
3812 got_object_tree_close(tree1);
3814 got_object_tree_close(tree2);
3815 if (f1 && fclose(f1) == EOF && err == NULL)
3816 err = got_error_from_errno("fclose");
3817 if (f2 && fclose(f2) == EOF && err == NULL)
3818 err = got_error_from_errno("fclose");
3819 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3820 err = got_error_from_errno("close");
3821 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3822 err = got_error_from_errno("close");
3826 static const struct got_error *
3827 get_changed_paths(struct got_pathlist_head *paths,
3828 struct got_commit_object *commit, struct got_repository *repo,
3829 struct got_diffstat_cb_arg *dsa)
3831 const struct got_error *err = NULL;
3832 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3833 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3834 struct got_object_qid *qid;
3835 got_diff_blob_cb cb = got_diff_tree_collect_changed_paths;
3836 FILE *f1 = NULL, *f2 = NULL;
3837 int fd1 = -1, fd2 = -1;
3840 cb = got_diff_tree_compute_diffstat;
3842 f1 = got_opentemp();
3844 err = got_error_from_errno("got_opentemp");
3847 f2 = got_opentemp();
3849 err = got_error_from_errno("got_opentemp");
3852 fd1 = got_opentempfd();
3854 err = got_error_from_errno("got_opentempfd");
3857 fd2 = got_opentempfd();
3859 err = got_error_from_errno("got_opentempfd");
3864 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3866 struct got_commit_object *pcommit;
3867 err = got_object_open_as_commit(&pcommit, repo,
3872 tree_id1 = got_object_id_dup(
3873 got_object_commit_get_tree_id(pcommit));
3874 if (tree_id1 == NULL) {
3875 got_object_commit_close(pcommit);
3876 return got_error_from_errno("got_object_id_dup");
3878 got_object_commit_close(pcommit);
3883 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3888 tree_id2 = got_object_commit_get_tree_id(commit);
3889 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3893 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3894 cb, dsa ? (void *)dsa : paths, dsa ? 1 : 0);
3897 got_object_tree_close(tree1);
3899 got_object_tree_close(tree2);
3900 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3901 err = got_error_from_errno("close");
3902 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3903 err = got_error_from_errno("close");
3904 if (f1 && fclose(f1) == EOF && err == NULL)
3905 err = got_error_from_errno("fclose");
3906 if (f2 && fclose(f2) == EOF && err == NULL)
3907 err = got_error_from_errno("fclose");
3912 static const struct got_error *
3913 print_patch(struct got_commit_object *commit, struct got_object_id *id,
3914 const char *path, int diff_context, struct got_diffstat_cb_arg *dsa,
3915 struct got_repository *repo, FILE *outfile)
3917 const struct got_error *err = NULL;
3918 struct got_commit_object *pcommit = NULL;
3919 char *id_str1 = NULL, *id_str2 = NULL;
3920 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
3921 struct got_object_qid *qid;
3923 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3925 err = got_object_open_as_commit(&pcommit, repo,
3929 err = got_object_id_str(&id_str1, &qid->id);
3934 err = got_object_id_str(&id_str2, id);
3938 if (path && path[0] != '\0') {
3940 err = got_object_id_by_path(&obj_id2, repo, commit, path);
3944 err = got_object_id_by_path(&obj_id1, repo,
3947 if (err->code != GOT_ERR_NO_TREE_ENTRY) {
3953 err = got_object_get_type(&obj_type, repo, obj_id2);
3959 "diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3960 fprintf(outfile, "commit - %s\n",
3961 id_str1 ? id_str1 : "/dev/null");
3962 fprintf(outfile, "commit + %s\n", id_str2);
3964 case GOT_OBJ_TYPE_BLOB:
3965 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
3966 0, 0, dsa, repo, outfile);
3968 case GOT_OBJ_TYPE_TREE:
3969 err = diff_trees(obj_id1, obj_id2, path, diff_context,
3970 0, 0, dsa, repo, outfile);
3973 err = got_error(GOT_ERR_OBJ_TYPE);
3979 obj_id2 = got_object_commit_get_tree_id(commit);
3981 obj_id1 = got_object_commit_get_tree_id(pcommit);
3983 "diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3984 fprintf(outfile, "commit - %s\n",
3985 id_str1 ? id_str1 : "/dev/null");
3986 fprintf(outfile, "commit + %s\n", id_str2);
3987 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, 0,
3988 dsa, repo, outfile);
3994 got_object_commit_close(pcommit);
3999 get_datestr(time_t *time, char *datebuf)
4001 struct tm mytm, *tm;
4004 tm = gmtime_r(time, &mytm);
4007 s = asctime_r(tm, datebuf);
4010 p = strchr(s, '\n');
4016 static const struct got_error *
4017 match_commit(int *have_match, struct got_object_id *id,
4018 struct got_commit_object *commit, regex_t *regex)
4020 const struct got_error *err = NULL;
4021 regmatch_t regmatch;
4022 char *id_str = NULL, *logmsg = NULL;
4026 err = got_object_id_str(&id_str, id);
4030 err = got_object_commit_get_logmsg(&logmsg, commit);
4034 if (regexec(regex, got_object_commit_get_author(commit), 1,
4035 ®match, 0) == 0 ||
4036 regexec(regex, got_object_commit_get_committer(commit), 1,
4037 ®match, 0) == 0 ||
4038 regexec(regex, id_str, 1, ®match, 0) == 0 ||
4039 regexec(regex, logmsg, 1, ®match, 0) == 0)
4048 match_changed_paths(int *have_match, struct got_pathlist_head *changed_paths,
4051 regmatch_t regmatch;
4052 struct got_pathlist_entry *pe;
4056 TAILQ_FOREACH(pe, changed_paths, entry) {
4057 if (regexec(regex, pe->path, 1, ®match, 0) == 0) {
4064 static const struct got_error *
4065 match_patch(int *have_match, struct got_commit_object *commit,
4066 struct got_object_id *id, const char *path, int diff_context,
4067 struct got_repository *repo, regex_t *regex, FILE *f)
4069 const struct got_error *err = NULL;
4071 size_t linesize = 0;
4072 regmatch_t regmatch;
4076 err = got_opentemp_truncate(f);
4080 err = print_patch(commit, id, path, diff_context, NULL, repo, f);
4084 if (fseeko(f, 0L, SEEK_SET) == -1) {
4085 err = got_error_from_errno("fseeko");
4089 while (getline(&line, &linesize, f) != -1) {
4090 if (regexec(regex, line, 1, ®match, 0) == 0) {
4100 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
4102 static const struct got_error*
4103 build_refs_str(char **refs_str, struct got_reflist_head *refs,
4104 struct got_object_id *id, struct got_repository *repo,
4107 static const struct got_error *err = NULL;
4108 struct got_reflist_entry *re;
4114 TAILQ_FOREACH(re, refs, entry) {
4115 struct got_tag_object *tag = NULL;
4116 struct got_object_id *ref_id;
4119 name = got_ref_get_name(re->ref);
4120 if (strcmp(name, GOT_REF_HEAD) == 0)
4122 if (strncmp(name, "refs/", 5) == 0)
4124 if (strncmp(name, "got/", 4) == 0)
4126 if (strncmp(name, "heads/", 6) == 0)
4128 if (strncmp(name, "remotes/", 8) == 0) {
4132 s = strstr(name, "/" GOT_REF_HEAD);
4133 if (s != NULL && strcmp(s, "/" GOT_REF_HEAD) == 0)
4136 err = got_ref_resolve(&ref_id, repo, re->ref);
4139 if (strncmp(name, "tags/", 5) == 0) {
4140 err = got_object_open_as_tag(&tag, repo, ref_id);
4142 if (err->code != GOT_ERR_OBJ_TYPE) {
4146 /* Ref points at something other than a tag. */
4151 cmp = got_object_id_cmp(tag ?
4152 got_object_tag_get_object_id(tag) : ref_id, id);
4155 got_object_tag_close(tag);
4159 if (asprintf(refs_str, "%s%s%s", s ? s : "",
4160 s ? ", " : "", name) == -1) {
4161 err = got_error_from_errno("asprintf");
4172 static const struct got_error *
4173 print_commit_oneline(struct got_commit_object *commit, struct got_object_id *id,
4174 struct got_repository *repo, struct got_reflist_object_id_map *refs_idmap)
4176 const struct got_error *err = NULL;
4177 char *ref_str = NULL, *id_str = NULL, *logmsg0 = NULL;
4178 char *comma, *s, *nl;
4179 struct got_reflist_head *refs;
4180 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
4182 time_t committer_time;
4184 refs = got_reflist_object_id_map_lookup(refs_idmap, id);
4186 err = build_refs_str(&ref_str, refs, id, repo, 1);
4190 /* Display the first matching ref only. */
4191 if (ref_str && (comma = strchr(ref_str, ',')) != NULL)
4195 if (ref_str == NULL) {
4196 err = got_object_id_str(&id_str, id);
4201 committer_time = got_object_commit_get_committer_time(commit);
4202 if (gmtime_r(&committer_time, &tm) == NULL) {
4203 err = got_error_from_errno("gmtime_r");
4206 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0) {
4207 err = got_error(GOT_ERR_NO_SPACE);
4211 err = got_object_commit_get_logmsg(&logmsg0, commit);
4216 while (isspace((unsigned char)s[0]))
4219 nl = strchr(s, '\n');
4225 printf("%s%-7s %s\n", datebuf, ref_str, s);
4227 printf("%s%.7s %s\n", datebuf, id_str, s);
4229 if (fflush(stdout) != 0 && err == NULL)
4230 err = got_error_from_errno("fflush");
4238 static const struct got_error *
4239 print_diffstat(struct got_diffstat_cb_arg *dsa, const char *header)
4241 struct got_pathlist_entry *pe;
4244 printf("%s\n", header);
4246 TAILQ_FOREACH(pe, dsa->paths, entry) {
4247 struct got_diff_changed_path *cp = pe->data;
4248 int pad = dsa->max_path_len - pe->path_len + 1;
4250 printf(" %c %s%*c | %*d+ %*d-\n", cp->status, pe->path, pad,
4251 ' ', dsa->add_cols + 1, cp->add, dsa->rm_cols + 1, cp->rm);
4253 printf("\n%d file%s changed, %d insertion%s(+), %d deletion%s(-)\n\n",
4254 dsa->nfiles, dsa->nfiles > 1 ? "s" : "", dsa->ins,
4255 dsa->ins != 1 ? "s" : "", dsa->del, dsa->del != 1 ? "s" : "");
4257 if (fflush(stdout) != 0)
4258 return got_error_from_errno("fflush");
4263 static const struct got_error *
4269 if (fseeko(f, 0L, SEEK_SET) == -1)
4270 return got_error_from_errno("fseek");
4273 r = fread(buf, 1, sizeof(buf), f);
4276 return got_error_from_errno("fread");
4280 if (fwrite(buf, 1, r, stdout) != r)
4281 return got_ferror(stdout, GOT_ERR_IO);
4287 static const struct got_error *
4288 print_commit(struct got_commit_object *commit, struct got_object_id *id,
4289 struct got_repository *repo, const char *path,
4290 struct got_pathlist_head *changed_paths,
4291 struct got_diffstat_cb_arg *diffstat, int show_patch, int diff_context,
4292 struct got_reflist_object_id_map *refs_idmap, const char *custom_refs_str,
4295 const struct got_error *err = NULL;
4297 char *id_str, *datestr, *logmsg0, *logmsg, *line;
4299 time_t committer_time;
4300 const char *author, *committer;
4301 char *refs_str = NULL;
4303 err = got_object_id_str(&id_str, id);
4307 if (custom_refs_str == NULL) {
4308 struct got_reflist_head *refs;
4309 refs = got_reflist_object_id_map_lookup(refs_idmap, id);
4311 err = build_refs_str(&refs_str, refs, id, repo, 0);
4317 printf(GOT_COMMIT_SEP_STR);
4318 if (custom_refs_str)
4319 printf("%s %s (%s)\n", prefix ? prefix : "commit", id_str,
4322 printf("%s %s%s%s%s\n", prefix ? prefix : "commit", id_str,
4323 refs_str ? " (" : "", refs_str ? refs_str : "",
4324 refs_str ? ")" : "");
4329 printf("from: %s\n", got_object_commit_get_author(commit));
4330 author = got_object_commit_get_author(commit);
4331 committer = got_object_commit_get_committer(commit);
4332 if (strcmp(author, committer) != 0)
4333 printf("via: %s\n", committer);
4334 committer_time = got_object_commit_get_committer_time(commit);
4335 datestr = get_datestr(&committer_time, datebuf);
4337 printf("date: %s UTC\n", datestr);
4338 if (got_object_commit_get_nparents(commit) > 1) {
4339 const struct got_object_id_queue *parent_ids;
4340 struct got_object_qid *qid;
4342 parent_ids = got_object_commit_get_parent_ids(commit);
4343 STAILQ_FOREACH(qid, parent_ids, entry) {
4344 err = got_object_id_str(&id_str, &qid->id);
4347 printf("parent %d: %s\n", n++, id_str);
4353 err = got_object_commit_get_logmsg(&logmsg0, commit);
4359 line = strsep(&logmsg, "\n");
4361 printf(" %s\n", line);
4365 if (changed_paths && diffstat == NULL) {
4366 struct got_pathlist_entry *pe;
4368 TAILQ_FOREACH(pe, changed_paths, entry) {
4369 struct got_diff_changed_path *cp = pe->data;
4371 printf(" %c %s\n", cp->status, pe->path);
4379 err = got_error_from_errno("got_opentemp");
4384 err = print_patch(commit, id, path, diff_context, diffstat,
4385 repo, diffstat == NULL ? stdout : f);
4390 err = print_diffstat(diffstat, NULL);
4402 if (fflush(stdout) != 0 && err == NULL)
4403 err = got_error_from_errno("fflush");
4405 if (f && fclose(f) == EOF && err == NULL)
4406 err = got_error_from_errno("fclose");
4412 static const struct got_error *
4413 print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
4414 struct got_repository *repo, const char *path, int show_changed_paths,
4415 int show_diffstat, int show_patch, const char *search_pattern,
4416 int diff_context, int limit, int log_branches, int reverse_display_order,
4417 struct got_reflist_object_id_map *refs_idmap, int one_line,
4420 const struct got_error *err;
4421 struct got_commit_graph *graph;
4424 struct got_object_id_queue reversed_commits;
4425 struct got_object_qid *qid;
4426 struct got_commit_object *commit;
4427 struct got_pathlist_head changed_paths;
4429 STAILQ_INIT(&reversed_commits);
4430 TAILQ_INIT(&changed_paths);
4432 if (search_pattern && regcomp(®ex, search_pattern,
4433 REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
4434 return got_error_msg(GOT_ERR_REGEX, search_pattern);
4436 err = got_commit_graph_open(&graph, path, !log_branches);
4439 err = got_commit_graph_iter_start(graph, root_id, repo,
4440 check_cancelled, NULL);
4444 struct got_object_id id;
4445 struct got_diffstat_cb_arg dsa = { 0, 0, 0, 0, 0, 0,
4446 &changed_paths, 0, 0, GOT_DIFF_ALGORITHM_PATIENCE };
4448 if (sigint_received || sigpipe_received)
4451 err = got_commit_graph_iter_next(&id, graph, repo,
4452 check_cancelled, NULL);
4454 if (err->code == GOT_ERR_ITER_COMPLETED)
4459 err = got_object_open_as_commit(&commit, repo, &id);
4463 if ((show_changed_paths || (show_diffstat && !show_patch))
4464 && !reverse_display_order) {
4465 err = get_changed_paths(&changed_paths, commit, repo,
4466 show_diffstat ? &dsa : NULL);
4471 if (search_pattern) {
4472 err = match_commit(&have_match, &id, commit, ®ex);
4474 got_object_commit_close(commit);
4477 if (have_match == 0 && show_changed_paths)
4478 match_changed_paths(&have_match,
4479 &changed_paths, ®ex);
4480 if (have_match == 0 && show_patch) {
4481 err = match_patch(&have_match, commit, &id,
4482 path, diff_context, repo, ®ex, tmpfile);
4486 if (have_match == 0) {
4487 got_object_commit_close(commit);
4488 got_pathlist_free(&changed_paths,
4489 GOT_PATHLIST_FREE_ALL);
4494 if (reverse_display_order) {
4495 err = got_object_qid_alloc(&qid, &id);
4498 STAILQ_INSERT_HEAD(&reversed_commits, qid, entry);
4499 got_object_commit_close(commit);
4502 err = print_commit_oneline(commit, &id,
4505 err = print_commit(commit, &id, repo, path,
4506 (show_changed_paths || show_diffstat) ?
4507 &changed_paths : NULL,
4508 show_diffstat ? &dsa : NULL, show_patch,
4509 diff_context, refs_idmap, NULL, NULL);
4510 got_object_commit_close(commit);
4514 if ((limit && --limit == 0) ||
4515 (end_id && got_object_id_cmp(&id, end_id) == 0))
4518 got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
4520 if (reverse_display_order) {
4521 STAILQ_FOREACH(qid, &reversed_commits, entry) {
4522 struct got_diffstat_cb_arg dsa = { 0, 0, 0, 0, 0, 0,
4523 &changed_paths, 0, 0, GOT_DIFF_ALGORITHM_PATIENCE };
4525 err = got_object_open_as_commit(&commit, repo,
4529 if (show_changed_paths ||
4530 (show_diffstat && !show_patch)) {
4531 err = get_changed_paths(&changed_paths, commit,
4532 repo, show_diffstat ? &dsa : NULL);
4537 err = print_commit_oneline(commit, &qid->id,
4540 err = print_commit(commit, &qid->id, repo, path,
4541 (show_changed_paths || show_diffstat) ?
4542 &changed_paths : NULL,
4543 show_diffstat ? &dsa : NULL, show_patch,
4544 diff_context, refs_idmap, NULL, NULL);
4545 got_object_commit_close(commit);
4548 got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
4552 while (!STAILQ_EMPTY(&reversed_commits)) {
4553 qid = STAILQ_FIRST(&reversed_commits);
4554 STAILQ_REMOVE_HEAD(&reversed_commits, entry);
4555 got_object_qid_free(qid);
4557 got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
4560 got_commit_graph_close(graph);
4567 fprintf(stderr, "usage: %s log [-bdPpRs] [-C number] [-c commit] "
4568 "[-l N] [-r repository-path] [-S search-pattern] [-x commit] "
4569 "[path]\n", getprogname());
4574 get_default_log_limit(void)
4576 const char *got_default_log_limit;
4580 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
4581 if (got_default_log_limit == NULL)
4583 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
4589 static const struct got_error *
4590 cmd_log(int argc, char *argv[])
4592 const struct got_error *error;
4593 struct got_repository *repo = NULL;
4594 struct got_worktree *worktree = NULL;
4595 struct got_object_id *start_id = NULL, *end_id = NULL;
4596 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
4597 const char *start_commit = NULL, *end_commit = NULL;
4598 const char *search_pattern = NULL;
4599 int diff_context = -1, ch;
4600 int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
4601 int show_diffstat = 0, reverse_display_order = 0, one_line = 0;
4603 struct got_reflist_head refs;
4604 struct got_reflist_object_id_map *refs_idmap = NULL;
4605 FILE *tmpfile = NULL;
4606 int *pack_fds = NULL;
4611 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4617 limit = get_default_log_limit();
4619 while ((ch = getopt(argc, argv, "bC:c:dl:PpRr:S:sx:")) != -1) {
4625 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4628 errx(1, "number of context lines is %s: %s",
4632 start_commit = optarg;
4638 limit = strtonum(optarg, 0, INT_MAX, &errstr);
4640 errx(1, "number of commits is %s: %s",
4644 show_changed_paths = 1;
4650 reverse_display_order = 1;
4653 repo_path = realpath(optarg, NULL);
4654 if (repo_path == NULL)
4655 return got_error_from_errno2("realpath",
4657 got_path_strip_trailing_slashes(repo_path);
4660 search_pattern = optarg;
4666 end_commit = optarg;
4677 if (diff_context == -1)
4679 else if (!show_patch)
4680 errx(1, "-C requires -p");
4682 if (one_line && (show_patch || show_changed_paths || show_diffstat))
4683 errx(1, "cannot use -s with -d, -p or -P");
4685 cwd = getcwd(NULL, 0);
4687 error = got_error_from_errno("getcwd");
4691 error = got_repo_pack_fds_open(&pack_fds);
4695 if (repo_path == NULL) {
4696 error = got_worktree_open(&worktree, cwd,
4697 GOT_WORKTREE_GOT_DIR);
4698 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4705 error = got_worktree_resolve_path(&path, worktree,
4710 path = strdup(argv[0]);
4712 error = got_error_from_errno("strdup");
4716 } else if (argc != 0)
4719 if (repo_path == NULL) {
4720 repo_path = worktree ?
4721 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
4723 if (repo_path == NULL) {
4724 error = got_error_from_errno("strdup");
4728 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4732 error = apply_unveil(got_repo_get_path(repo), 1,
4733 worktree ? got_worktree_get_root_path(worktree) : NULL);
4737 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4741 error = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
4745 if (start_commit == NULL) {
4746 struct got_reference *head_ref;
4747 struct got_commit_object *commit = NULL;
4748 error = got_ref_open(&head_ref, repo,
4749 worktree ? got_worktree_get_head_ref_name(worktree)
4753 error = got_ref_resolve(&start_id, repo, head_ref);
4754 got_ref_close(head_ref);
4757 error = got_object_open_as_commit(&commit, repo,
4761 got_object_commit_close(commit);
4763 char *keyword_idstr = NULL;
4765 error = got_keyword_to_idstr(&keyword_idstr, start_commit,
4769 if (keyword_idstr != NULL)
4770 start_commit = keyword_idstr;
4772 error = got_repo_match_object_id(&start_id, NULL,
4773 start_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4774 free(keyword_idstr);
4778 if (end_commit != NULL) {
4779 error = got_repo_match_object_id(&end_id, NULL,
4780 end_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4787 * If a path was specified on the command line it was resolved
4788 * to a path in the work tree above. Prepend the work tree's
4789 * path prefix to obtain the corresponding in-repository path.
4793 prefix = got_worktree_get_path_prefix(worktree);
4794 if (asprintf(&in_repo_path, "%s%s%s", prefix,
4795 (path[0] != '\0') ? "/" : "", path) == -1) {
4796 error = got_error_from_errno("asprintf");
4801 error = got_repo_map_path(&in_repo_path, repo,
4807 path = in_repo_path;
4811 /* Release work tree lock. */
4812 got_worktree_close(worktree);
4816 if (search_pattern && show_patch) {
4817 tmpfile = got_opentemp();
4818 if (tmpfile == NULL) {
4819 error = got_error_from_errno("got_opentemp");
4824 error = print_commits(start_id, end_id, repo, path ? path : "",
4825 show_changed_paths, show_diffstat, show_patch, search_pattern,
4826 diff_context, limit, log_branches, reverse_display_order,
4827 refs_idmap, one_line, tmpfile);
4835 got_worktree_close(worktree);
4837 const struct got_error *close_err = got_repo_close(repo);
4842 const struct got_error *pack_err =
4843 got_repo_pack_fds_close(pack_fds);
4848 got_reflist_object_id_map_free(refs_idmap);
4849 if (tmpfile && fclose(tmpfile) == EOF && error == NULL)
4850 error = got_error_from_errno("fclose");
4851 got_ref_list_free(&refs);
4858 fprintf(stderr, "usage: %s diff [-adPsw] [-C number] [-c commit] "
4859 "[-r repository-path] [object1 object2 | path ...]\n",
4864 struct print_diff_arg {
4865 struct got_repository *repo;
4866 struct got_worktree *worktree;
4867 struct got_diffstat_cb_arg *diffstat;
4872 enum got_diff_algorithm diff_algo;
4873 int ignore_whitespace;
4874 int force_text_diff;
4881 * Create a file which contains the target path of a symlink so we can feed
4882 * it as content to the diff engine.
4884 static const struct got_error *
4885 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
4886 const char *abspath)
4888 const struct got_error *err = NULL;
4889 char target_path[PATH_MAX];
4890 ssize_t target_len, outlen;
4895 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
4896 if (target_len == -1)
4897 return got_error_from_errno2("readlinkat", abspath);
4899 target_len = readlink(abspath, target_path, PATH_MAX);
4900 if (target_len == -1)
4901 return got_error_from_errno2("readlink", abspath);
4904 *fd = got_opentempfd();
4906 return got_error_from_errno("got_opentempfd");
4908 outlen = write(*fd, target_path, target_len);
4910 err = got_error_from_errno("got_opentempfd");
4914 if (lseek(*fd, 0, SEEK_SET) == -1) {
4915 err = got_error_from_errno2("lseek", abspath);
4926 static const struct got_error *
4927 print_diff(void *arg, unsigned char status, unsigned char staged_status,
4928 const char *path, struct got_object_id *blob_id,
4929 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4930 int dirfd, const char *de_name)
4932 struct print_diff_arg *a = arg;
4933 const struct got_error *err = NULL;
4934 struct got_blob_object *blob1 = NULL;
4935 int fd = -1, fd1 = -1, fd2 = -1;
4937 char *abspath = NULL, *label1 = NULL;
4942 memset(&sb, 0, sizeof(sb));
4944 if (a->diff_staged) {
4945 if (staged_status != GOT_STATUS_MODIFY &&
4946 staged_status != GOT_STATUS_ADD &&
4947 staged_status != GOT_STATUS_DELETE)
4950 if (staged_status == GOT_STATUS_DELETE)
4952 if (status == GOT_STATUS_NONEXISTENT)
4953 return got_error_set_errno(ENOENT, path);
4954 if (status != GOT_STATUS_MODIFY &&
4955 status != GOT_STATUS_ADD &&
4956 status != GOT_STATUS_DELETE &&
4957 status != GOT_STATUS_CONFLICT)
4961 err = got_opentemp_truncate(a->f1);
4963 return got_error_from_errno("got_opentemp_truncate");
4964 err = got_opentemp_truncate(a->f2);
4966 return got_error_from_errno("got_opentemp_truncate");
4968 if (!a->header_shown) {
4969 if (fprintf(a->outfile, "diff %s%s\n",
4970 a->diff_staged ? "-s " : "",
4971 got_worktree_get_root_path(a->worktree)) < 0) {
4972 err = got_error_from_errno("fprintf");
4975 if (fprintf(a->outfile, "commit - %s\n", a->id_str) < 0) {
4976 err = got_error_from_errno("fprintf");
4979 if (fprintf(a->outfile, "path + %s%s\n",
4980 got_worktree_get_root_path(a->worktree),
4981 a->diff_staged ? " (staged changes)" : "") < 0) {
4982 err = got_error_from_errno("fprintf");
4985 a->header_shown = 1;
4988 if (a->diff_staged) {
4989 const char *label1 = NULL, *label2 = NULL;
4990 switch (staged_status) {
4991 case GOT_STATUS_MODIFY:
4995 case GOT_STATUS_ADD:
4998 case GOT_STATUS_DELETE:
5002 return got_error(GOT_ERR_FILE_STATUS);
5004 fd1 = got_opentempfd();
5006 err = got_error_from_errno("got_opentempfd");
5009 fd2 = got_opentempfd();
5011 err = got_error_from_errno("got_opentempfd");
5014 err = got_diff_objects_as_blobs(NULL, NULL, a->f1, a->f2,
5015 fd1, fd2, blob_id, staged_blob_id, label1, label2,
5016 a->diff_algo, a->diff_context, a->ignore_whitespace,
5017 a->force_text_diff, a->diffstat, a->repo, a->outfile);
5021 fd1 = got_opentempfd();
5023 err = got_error_from_errno("got_opentempfd");
5027 if (staged_status == GOT_STATUS_ADD ||
5028 staged_status == GOT_STATUS_MODIFY) {
5030 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
5034 err = got_object_id_str(&id_str, staged_blob_id);
5037 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
5038 err = got_error_from_errno("asprintf");
5043 } else if (status != GOT_STATUS_ADD) {
5044 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192,
5050 if (status != GOT_STATUS_DELETE) {
5051 if (asprintf(&abspath, "%s/%s",
5052 got_worktree_get_root_path(a->worktree), path) == -1) {
5053 err = got_error_from_errno("asprintf");
5058 fd = openat(dirfd, de_name,
5059 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5061 if (!got_err_open_nofollow_on_symlink()) {
5062 err = got_error_from_errno2("openat",
5066 err = get_symlink_target_file(&fd, dirfd,
5072 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5074 if (!got_err_open_nofollow_on_symlink()) {
5075 err = got_error_from_errno2("open",
5079 err = get_symlink_target_file(&fd, dirfd,
5085 if (fstatat(fd, abspath, &sb, AT_SYMLINK_NOFOLLOW) == -1) {
5086 err = got_error_from_errno2("fstatat", abspath);
5089 f2 = fdopen(fd, "r");
5091 err = got_error_from_errno2("fdopen", abspath);
5099 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5105 err = got_diff_blob_file(blob1, a->f1, size1, label1, f2 ? f2 : a->f2,
5106 f2_exists, &sb, path, GOT_DIFF_ALGORITHM_PATIENCE, a->diff_context,
5107 a->ignore_whitespace, a->force_text_diff, a->diffstat, a->outfile);
5109 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5110 err = got_error_from_errno("close");
5111 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5112 err = got_error_from_errno("close");
5114 got_object_blob_close(blob1);
5115 if (fd != -1 && close(fd) == -1 && err == NULL)
5116 err = got_error_from_errno("close");
5117 if (f2 && fclose(f2) == EOF && err == NULL)
5118 err = got_error_from_errno("fclose");
5123 static const struct got_error *
5124 cmd_diff(int argc, char *argv[])
5126 const struct got_error *error;
5127 struct got_repository *repo = NULL;
5128 struct got_worktree *worktree = NULL;
5129 char *cwd = NULL, *repo_path = NULL;
5130 const char *commit_args[2] = { NULL, NULL };
5131 int ncommit_args = 0;
5132 struct got_object_id *ids[2] = { NULL, NULL };
5133 char *labels[2] = { NULL, NULL };
5134 int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
5135 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch, i;
5136 int force_text_diff = 0, force_path = 0, rflag = 0, show_diffstat = 0;
5138 struct got_reflist_head refs;
5139 struct got_pathlist_head diffstat_paths, paths;
5140 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
5141 int fd1 = -1, fd2 = -1;
5142 int *pack_fds = NULL;
5143 struct got_diffstat_cb_arg dsa;
5145 memset(&dsa, 0, sizeof(dsa));
5149 TAILQ_INIT(&diffstat_paths);
5152 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5157 while ((ch = getopt(argc, argv, "aC:c:dPr:sw")) != -1) {
5160 force_text_diff = 1;
5163 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
5166 errx(1, "number of context lines is %s: %s",
5170 if (ncommit_args >= 2)
5171 errx(1, "too many -c options used");
5172 commit_args[ncommit_args++] = optarg;
5181 repo_path = realpath(optarg, NULL);
5182 if (repo_path == NULL)
5183 return got_error_from_errno2("realpath",
5185 got_path_strip_trailing_slashes(repo_path);
5192 ignore_whitespace = 1;
5203 cwd = getcwd(NULL, 0);
5205 error = got_error_from_errno("getcwd");
5209 error = got_repo_pack_fds_open(&pack_fds);
5213 if (repo_path == NULL) {
5214 error = got_worktree_open(&worktree, cwd,
5215 GOT_WORKTREE_GOT_DIR);
5216 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5222 strdup(got_worktree_get_repo_path(worktree));
5223 if (repo_path == NULL) {
5224 error = got_error_from_errno("strdup");
5228 repo_path = strdup(cwd);
5229 if (repo_path == NULL) {
5230 error = got_error_from_errno("strdup");
5236 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5241 if (show_diffstat) {
5242 dsa.paths = &diffstat_paths;
5243 dsa.force_text = force_text_diff;
5244 dsa.ignore_ws = ignore_whitespace;
5245 dsa.diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
5248 if (rflag || worktree == NULL || ncommit_args > 0) {
5250 error = got_error_msg(GOT_ERR_NOT_IMPL,
5251 "-P option can only be used when diffing "
5256 error = got_error_msg(GOT_ERR_NOT_IMPL,
5257 "-s option can only be used when diffing "
5263 error = apply_unveil(got_repo_get_path(repo), 1,
5264 worktree ? got_worktree_get_root_path(worktree) : NULL);
5268 if ((!force_path && argc == 2) || ncommit_args > 0) {
5269 int obj_type = (ncommit_args > 0 ?
5270 GOT_OBJ_TYPE_COMMIT : GOT_OBJ_TYPE_ANY);
5271 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5275 for (i = 0; i < (ncommit_args > 0 ? ncommit_args : argc); i++) {
5277 char *keyword_idstr = NULL;
5279 if (ncommit_args > 0)
5280 arg = commit_args[i];
5284 error = got_keyword_to_idstr(&keyword_idstr, arg,
5288 if (keyword_idstr != NULL)
5289 arg = keyword_idstr;
5291 error = got_repo_match_object_id(&ids[i], &labels[i],
5292 arg, obj_type, &refs, repo);
5293 free(keyword_idstr);
5295 if (error->code != GOT_ERR_NOT_REF &&
5296 error->code != GOT_ERR_NO_OBJ)
5298 if (ncommit_args > 0)
5306 f1 = got_opentemp();
5308 error = got_error_from_errno("got_opentemp");
5312 f2 = got_opentemp();
5314 error = got_error_from_errno("got_opentemp");
5318 outfile = got_opentemp();
5319 if (outfile == NULL) {
5320 error = got_error_from_errno("got_opentemp");
5324 if (ncommit_args == 0 && (ids[0] == NULL || ids[1] == NULL)) {
5325 struct print_diff_arg arg;
5328 if (worktree == NULL) {
5329 if (argc == 2 && ids[0] == NULL) {
5330 error = got_error_path(argv[0], GOT_ERR_NO_OBJ);
5332 } else if (argc == 2 && ids[1] == NULL) {
5333 error = got_error_path(argv[1], GOT_ERR_NO_OBJ);
5335 } else if (argc > 0) {
5336 error = got_error_fmt(GOT_ERR_NOT_WORKTREE,
5337 "%s", "specified paths cannot be resolved");
5340 error = got_error(GOT_ERR_NOT_WORKTREE);
5345 error = get_worktree_paths_from_argv(&paths, argc, argv,
5350 error = got_object_id_str(&id_str,
5351 got_worktree_get_base_commit_id(worktree));
5355 arg.worktree = worktree;
5356 arg.diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
5357 arg.diff_context = diff_context;
5358 arg.id_str = id_str;
5359 arg.header_shown = 0;
5360 arg.diff_staged = diff_staged;
5361 arg.ignore_whitespace = ignore_whitespace;
5362 arg.force_text_diff = force_text_diff;
5363 arg.diffstat = show_diffstat ? &dsa : NULL;
5366 arg.outfile = outfile;
5368 error = got_worktree_status(worktree, &paths, repo, 0,
5369 print_diff, &arg, check_cancelled, NULL);
5374 if (show_diffstat && dsa.nfiles > 0) {
5377 if (asprintf(&header, "diffstat %s%s",
5378 diff_staged ? "-s " : "",
5379 got_worktree_get_root_path(worktree)) == -1) {
5380 error = got_error_from_errno("asprintf");
5384 error = print_diffstat(&dsa, header);
5390 error = printfile(outfile);
5394 if (ncommit_args == 1) {
5395 struct got_commit_object *commit;
5396 error = got_object_open_as_commit(&commit, repo, ids[0]);
5400 labels[1] = labels[0];
5402 if (got_object_commit_get_nparents(commit) > 0) {
5403 const struct got_object_id_queue *pids;
5404 struct got_object_qid *pid;
5405 pids = got_object_commit_get_parent_ids(commit);
5406 pid = STAILQ_FIRST(pids);
5407 ids[0] = got_object_id_dup(&pid->id);
5408 if (ids[0] == NULL) {
5409 error = got_error_from_errno(
5410 "got_object_id_dup");
5411 got_object_commit_close(commit);
5414 error = got_object_id_str(&labels[0], ids[0]);
5416 got_object_commit_close(commit);
5421 labels[0] = strdup("/dev/null");
5422 if (labels[0] == NULL) {
5423 error = got_error_from_errno("strdup");
5424 got_object_commit_close(commit);
5429 got_object_commit_close(commit);
5432 if (ncommit_args == 0 && argc > 2) {
5433 error = got_error_msg(GOT_ERR_BAD_PATH,
5434 "path arguments cannot be used when diffing two objects");
5439 error = got_object_get_type(&type1, repo, ids[0]);
5444 error = got_object_get_type(&type2, repo, ids[1]);
5447 if (type1 != GOT_OBJ_TYPE_ANY && type1 != type2) {
5448 error = got_error(GOT_ERR_OBJ_TYPE);
5451 if (type1 == GOT_OBJ_TYPE_BLOB && argc > 2) {
5452 error = got_error_msg(GOT_ERR_OBJ_TYPE,
5453 "path arguments cannot be used when diffing blobs");
5457 for (i = 0; ncommit_args > 0 && i < argc; i++) {
5459 struct got_pathlist_entry *new;
5463 error = got_worktree_resolve_path(&p, worktree,
5467 prefix = got_worktree_get_path_prefix(worktree);
5468 while (prefix[0] == '/')
5470 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5471 (p[0] != '\0' && prefix[0] != '\0') ? "/" : "",
5473 error = got_error_from_errno("asprintf");
5479 char *mapped_path, *s;
5480 error = got_repo_map_path(&mapped_path, repo, argv[i]);
5486 in_repo_path = strdup(s);
5487 if (in_repo_path == NULL) {
5488 error = got_error_from_errno("asprintf");
5495 error = got_pathlist_insert(&new, &paths, in_repo_path, NULL);
5496 if (error || new == NULL /* duplicate */)
5503 /* Release work tree lock. */
5504 got_worktree_close(worktree);
5508 fd1 = got_opentempfd();
5510 error = got_error_from_errno("got_opentempfd");
5514 fd2 = got_opentempfd();
5516 error = got_error_from_errno("got_opentempfd");
5520 switch (type1 == GOT_OBJ_TYPE_ANY ? type2 : type1) {
5521 case GOT_OBJ_TYPE_BLOB:
5522 error = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5523 fd1, fd2, ids[0], ids[1], NULL, NULL,
5524 GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
5525 ignore_whitespace, force_text_diff,
5526 show_diffstat ? &dsa : NULL, repo, outfile);
5528 case GOT_OBJ_TYPE_TREE:
5529 error = got_diff_objects_as_trees(NULL, NULL, f1, f2, fd1, fd2,
5530 ids[0], ids[1], &paths, "", "",
5531 GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
5532 ignore_whitespace, force_text_diff,
5533 show_diffstat ? &dsa : NULL, repo, outfile);
5535 case GOT_OBJ_TYPE_COMMIT:
5536 fprintf(outfile, "diff %s %s\n", labels[0], labels[1]);
5537 error = got_diff_objects_as_commits(NULL, NULL, f1, f2,
5538 fd1, fd2, ids[0], ids[1], &paths,
5539 GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
5540 ignore_whitespace, force_text_diff,
5541 show_diffstat ? &dsa : NULL, repo, outfile);
5544 error = got_error(GOT_ERR_OBJ_TYPE);
5549 if (show_diffstat && dsa.nfiles > 0) {
5550 char *header = NULL;
5552 if (asprintf(&header, "diffstat %s %s",
5553 labels[0], labels[1]) == -1) {
5554 error = got_error_from_errno("asprintf");
5558 error = print_diffstat(&dsa, header);
5564 error = printfile(outfile);
5572 got_worktree_close(worktree);
5574 const struct got_error *close_err = got_repo_close(repo);
5579 const struct got_error *pack_err =
5580 got_repo_pack_fds_close(pack_fds);
5584 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
5585 got_pathlist_free(&diffstat_paths, GOT_PATHLIST_FREE_ALL);
5586 got_ref_list_free(&refs);
5587 if (outfile && fclose(outfile) == EOF && error == NULL)
5588 error = got_error_from_errno("fclose");
5589 if (f1 && fclose(f1) == EOF && error == NULL)
5590 error = got_error_from_errno("fclose");
5591 if (f2 && fclose(f2) == EOF && error == NULL)
5592 error = got_error_from_errno("fclose");
5593 if (fd1 != -1 && close(fd1) == -1 && error == NULL)
5594 error = got_error_from_errno("close");
5595 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
5596 error = got_error_from_errno("close");
5604 "usage: %s blame [-c commit] [-r repository-path] path\n",
5613 char datebuf[11]; /* YYYY-MM-DD + NUL */
5616 struct blame_cb_args {
5617 struct blame_line *lines;
5621 off_t *line_offsets;
5623 struct got_repository *repo;
5626 static const struct got_error *
5627 blame_cb(void *arg, int nlines, int lineno,
5628 struct got_commit_object *commit, struct got_object_id *id)
5630 const struct got_error *err = NULL;
5631 struct blame_cb_args *a = arg;
5632 struct blame_line *bline;
5634 size_t linesize = 0;
5637 time_t committer_time;
5639 if (nlines != a->nlines ||
5640 (lineno != -1 && lineno < 1) || lineno > a->nlines)
5641 return got_error(GOT_ERR_RANGE);
5643 if (sigint_received)
5644 return got_error(GOT_ERR_ITER_COMPLETED);
5647 return NULL; /* no change in this commit */
5649 /* Annotate this line. */
5650 bline = &a->lines[lineno - 1];
5651 if (bline->annotated)
5653 err = got_object_id_str(&bline->id_str, id);
5657 bline->committer = strdup(got_object_commit_get_committer(commit));
5658 if (bline->committer == NULL) {
5659 err = got_error_from_errno("strdup");
5663 committer_time = got_object_commit_get_committer_time(commit);
5664 if (gmtime_r(&committer_time, &tm) == NULL)
5665 return got_error_from_errno("gmtime_r");
5666 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
5668 err = got_error(GOT_ERR_NO_SPACE);
5671 bline->annotated = 1;
5673 /* Print lines annotated so far. */
5674 bline = &a->lines[a->lineno_cur - 1];
5675 if (!bline->annotated)
5678 offset = a->line_offsets[a->lineno_cur - 1];
5679 if (fseeko(a->f, offset, SEEK_SET) == -1) {
5680 err = got_error_from_errno("fseeko");
5684 while (a->lineno_cur <= a->nlines && bline->annotated) {
5685 char *smallerthan, *at, *nl, *committer;
5688 if (getline(&line, &linesize, a->f) == -1) {
5690 err = got_error_from_errno("getline");
5694 committer = bline->committer;
5695 smallerthan = strchr(committer, '<');
5696 if (smallerthan && smallerthan[1] != '\0')
5697 committer = smallerthan + 1;
5698 at = strchr(committer, '@');
5701 len = strlen(committer);
5703 committer[8] = '\0';
5705 nl = strchr(line, '\n');
5708 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
5709 bline->id_str, bline->datebuf, committer, line);
5712 bline = &a->lines[a->lineno_cur - 1];
5719 static const struct got_error *
5720 cmd_blame(int argc, char *argv[])
5722 const struct got_error *error;
5723 struct got_repository *repo = NULL;
5724 struct got_worktree *worktree = NULL;
5725 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5726 char *link_target = NULL;
5727 struct got_object_id *obj_id = NULL;
5728 struct got_object_id *commit_id = NULL;
5729 struct got_commit_object *commit = NULL;
5730 struct got_blob_object *blob = NULL;
5731 char *commit_id_str = NULL, *keyword_idstr = NULL;
5732 struct blame_cb_args bca;
5733 int ch, obj_type, i, fd1 = -1, fd2 = -1, fd3 = -1;
5735 int *pack_fds = NULL;
5736 FILE *f1 = NULL, *f2 = NULL;
5738 fd1 = got_opentempfd();
5740 return got_error_from_errno("got_opentempfd");
5742 memset(&bca, 0, sizeof(bca));
5745 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5750 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5753 commit_id_str = optarg;
5756 repo_path = realpath(optarg, NULL);
5757 if (repo_path == NULL)
5758 return got_error_from_errno2("realpath",
5760 got_path_strip_trailing_slashes(repo_path);
5776 cwd = getcwd(NULL, 0);
5778 error = got_error_from_errno("getcwd");
5782 error = got_repo_pack_fds_open(&pack_fds);
5786 if (repo_path == NULL) {
5787 error = got_worktree_open(&worktree, cwd,
5788 GOT_WORKTREE_GOT_DIR);
5789 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5795 strdup(got_worktree_get_repo_path(worktree));
5796 if (repo_path == NULL) {
5797 error = got_error_from_errno("strdup");
5802 repo_path = strdup(cwd);
5803 if (repo_path == NULL) {
5804 error = got_error_from_errno("strdup");
5810 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5815 const char *prefix = got_worktree_get_path_prefix(worktree);
5818 error = got_worktree_resolve_path(&p, worktree, path);
5821 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5822 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
5824 error = got_error_from_errno("asprintf");
5829 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5831 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5834 error = got_repo_map_path(&in_repo_path, repo, path);
5839 if (commit_id_str == NULL) {
5840 struct got_reference *head_ref;
5841 error = got_ref_open(&head_ref, repo, worktree ?
5842 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5845 error = got_ref_resolve(&commit_id, repo, head_ref);
5846 got_ref_close(head_ref);
5850 struct got_reflist_head refs;
5853 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5858 error = got_keyword_to_idstr(&keyword_idstr, commit_id_str,
5862 if (keyword_idstr != NULL)
5863 commit_id_str = keyword_idstr;
5865 error = got_repo_match_object_id(&commit_id, NULL,
5866 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5867 got_ref_list_free(&refs);
5873 /* Release work tree lock. */
5874 got_worktree_close(worktree);
5878 error = got_object_open_as_commit(&commit, repo, commit_id);
5882 error = got_object_resolve_symlinks(&link_target, in_repo_path,
5887 error = got_object_id_by_path(&obj_id, repo, commit,
5888 link_target ? link_target : in_repo_path);
5892 error = got_object_get_type(&obj_type, repo, obj_id);
5896 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5897 error = got_error_path(link_target ? link_target : in_repo_path,
5902 error = got_object_open_as_blob(&blob, repo, obj_id, 8192, fd1);
5905 bca.f = got_opentemp();
5906 if (bca.f == NULL) {
5907 error = got_error_from_errno("got_opentemp");
5910 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
5911 &bca.line_offsets, bca.f, blob);
5912 if (error || bca.nlines == 0)
5915 /* Don't include \n at EOF in the blame line count. */
5916 if (bca.line_offsets[bca.nlines - 1] == filesize)
5919 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
5920 if (bca.lines == NULL) {
5921 error = got_error_from_errno("calloc");
5925 bca.nlines_prec = 0;
5933 fd2 = got_opentempfd();
5935 error = got_error_from_errno("got_opentempfd");
5938 fd3 = got_opentempfd();
5940 error = got_error_from_errno("got_opentempfd");
5943 f1 = got_opentemp();
5945 error = got_error_from_errno("got_opentemp");
5948 f2 = got_opentemp();
5950 error = got_error_from_errno("got_opentemp");
5953 error = got_blame(link_target ? link_target : in_repo_path, commit_id,
5954 repo, GOT_DIFF_ALGORITHM_PATIENCE, blame_cb, &bca,
5955 check_cancelled, NULL, fd2, fd3, f1, f2);
5957 free(keyword_idstr);
5965 got_object_commit_close(commit);
5967 if (fd1 != -1 && close(fd1) == -1 && error == NULL)
5968 error = got_error_from_errno("close");
5969 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
5970 error = got_error_from_errno("close");
5971 if (fd3 != -1 && close(fd3) == -1 && error == NULL)
5972 error = got_error_from_errno("close");
5973 if (f1 && fclose(f1) == EOF && error == NULL)
5974 error = got_error_from_errno("fclose");
5975 if (f2 && fclose(f2) == EOF && error == NULL)
5976 error = got_error_from_errno("fclose");
5979 got_object_blob_close(blob);
5981 got_worktree_close(worktree);
5983 const struct got_error *close_err = got_repo_close(repo);
5988 const struct got_error *pack_err =
5989 got_repo_pack_fds_close(pack_fds);
5994 for (i = 0; i < bca.nlines; i++) {
5995 struct blame_line *bline = &bca.lines[i];
5996 free(bline->id_str);
5997 free(bline->committer);
6001 free(bca.line_offsets);
6002 if (bca.f && fclose(bca.f) == EOF && error == NULL)
6003 error = got_error_from_errno("fclose");
6010 fprintf(stderr, "usage: %s tree [-iR] [-c commit] [-r repository-path] "
6011 "[path]\n", getprogname());
6015 static const struct got_error *
6016 print_entry(struct got_tree_entry *te, const char *id, const char *path,
6017 const char *root_path, struct got_repository *repo)
6019 const struct got_error *err = NULL;
6020 int is_root_path = (strcmp(path, root_path) == 0);
6021 const char *modestr = "";
6022 mode_t mode = got_tree_entry_get_mode(te);
6023 char *link_target = NULL;
6025 path += strlen(root_path);
6026 while (path[0] == '/')
6029 if (got_object_tree_entry_is_submodule(te))
6031 else if (S_ISLNK(mode)) {
6034 err = got_tree_entry_get_symlink_target(&link_target, te, repo);
6037 for (i = 0; link_target[i] != '\0'; i++) {
6038 if (!isprint((unsigned char)link_target[i]))
6039 link_target[i] = '?';
6044 else if (S_ISDIR(mode))
6046 else if (mode & S_IXUSR)
6049 printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
6050 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
6051 link_target ? " -> ": "", link_target ? link_target : "");
6057 static const struct got_error *
6058 print_tree(const char *path, struct got_commit_object *commit,
6059 int show_ids, int recurse, const char *root_path,
6060 struct got_repository *repo)
6062 const struct got_error *err = NULL;
6063 struct got_object_id *tree_id = NULL;
6064 struct got_tree_object *tree = NULL;
6067 err = got_object_id_by_path(&tree_id, repo, commit, path);
6071 err = got_object_open_as_tree(&tree, repo, tree_id);
6074 nentries = got_object_tree_get_nentries(tree);
6075 for (i = 0; i < nentries; i++) {
6076 struct got_tree_entry *te;
6079 if (sigint_received || sigpipe_received)
6082 te = got_object_tree_get_entry(tree, i);
6085 err = got_object_id_str(&id_str,
6086 got_tree_entry_get_id(te));
6089 if (asprintf(&id, "%s ", id_str) == -1) {
6090 err = got_error_from_errno("asprintf");
6096 err = print_entry(te, id, path, root_path, repo);
6101 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
6103 if (asprintf(&child_path, "%s%s%s", path,
6104 path[0] == '/' && path[1] == '\0' ? "" : "/",
6105 got_tree_entry_get_name(te)) == -1) {
6106 err = got_error_from_errno("asprintf");
6109 err = print_tree(child_path, commit, show_ids, 1,
6118 got_object_tree_close(tree);
6123 static const struct got_error *
6124 cmd_tree(int argc, char *argv[])
6126 const struct got_error *error;
6127 struct got_repository *repo = NULL;
6128 struct got_worktree *worktree = NULL;
6129 const char *path, *refname = NULL;
6130 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6131 struct got_object_id *commit_id = NULL;
6132 struct got_commit_object *commit = NULL;
6133 char *commit_id_str = NULL, *keyword_idstr = NULL;
6134 int show_ids = 0, recurse = 0;
6136 int *pack_fds = NULL;
6139 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6144 while ((ch = getopt(argc, argv, "c:iRr:")) != -1) {
6147 commit_id_str = optarg;
6156 repo_path = realpath(optarg, NULL);
6157 if (repo_path == NULL)
6158 return got_error_from_errno2("realpath",
6160 got_path_strip_trailing_slashes(repo_path);
6178 cwd = getcwd(NULL, 0);
6180 error = got_error_from_errno("getcwd");
6184 error = got_repo_pack_fds_open(&pack_fds);
6188 if (repo_path == NULL) {
6189 error = got_worktree_open(&worktree, cwd,
6190 GOT_WORKTREE_GOT_DIR);
6191 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6197 strdup(got_worktree_get_repo_path(worktree));
6198 if (repo_path == NULL)
6199 error = got_error_from_errno("strdup");
6203 repo_path = strdup(cwd);
6204 if (repo_path == NULL) {
6205 error = got_error_from_errno("strdup");
6211 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6216 const char *prefix = got_worktree_get_path_prefix(worktree);
6219 if (path == NULL || got_path_is_root_dir(path))
6221 error = got_worktree_resolve_path(&p, worktree, path);
6224 if (asprintf(&in_repo_path, "%s%s%s", prefix,
6225 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
6227 error = got_error_from_errno("asprintf");
6232 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6236 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6241 error = got_repo_map_path(&in_repo_path, repo, path);
6246 if (commit_id_str == NULL) {
6247 struct got_reference *head_ref;
6249 refname = got_worktree_get_head_ref_name(worktree);
6251 refname = GOT_REF_HEAD;
6252 error = got_ref_open(&head_ref, repo, refname, 0);
6255 error = got_ref_resolve(&commit_id, repo, head_ref);
6256 got_ref_close(head_ref);
6260 struct got_reflist_head refs;
6263 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
6268 error = got_keyword_to_idstr(&keyword_idstr, commit_id_str,
6272 if (keyword_idstr != NULL)
6273 commit_id_str = keyword_idstr;
6275 error = got_repo_match_object_id(&commit_id, NULL,
6276 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
6277 got_ref_list_free(&refs);
6283 /* Release work tree lock. */
6284 got_worktree_close(worktree);
6288 error = got_object_open_as_commit(&commit, repo, commit_id);
6292 error = print_tree(in_repo_path, commit, show_ids, recurse,
6293 in_repo_path, repo);
6295 free(keyword_idstr);
6301 got_object_commit_close(commit);
6303 got_worktree_close(worktree);
6305 const struct got_error *close_err = got_repo_close(repo);
6310 const struct got_error *pack_err =
6311 got_repo_pack_fds_close(pack_fds);
6321 fprintf(stderr, "usage: %s status [-I] [-S status-codes] "
6322 "[-s status-codes] [path ...]\n", getprogname());
6326 struct got_status_arg {
6331 static const struct got_error *
6332 print_status(void *arg, unsigned char status, unsigned char staged_status,
6333 const char *path, struct got_object_id *blob_id,
6334 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6335 int dirfd, const char *de_name)
6337 struct got_status_arg *st = arg;
6339 if (status == staged_status && (status == GOT_STATUS_DELETE))
6340 status = GOT_STATUS_NO_CHANGE;
6341 if (st != NULL && st->status_codes) {
6342 size_t ncodes = strlen(st->status_codes);
6345 for (i = 0; i < ncodes ; i++) {
6347 if (status == st->status_codes[i] ||
6348 staged_status == st->status_codes[i]) {
6353 if (status == st->status_codes[i] ||
6354 staged_status == st->status_codes[i])
6359 if (st->suppress && j == 0)
6366 printf("%c%c %s\n", status, staged_status, path);
6370 static const struct got_error *
6371 cmd_status(int argc, char *argv[])
6373 const struct got_error *error = NULL;
6374 struct got_repository *repo = NULL;
6375 struct got_worktree *worktree = NULL;
6376 struct got_status_arg st;
6378 struct got_pathlist_head paths;
6379 int ch, i, no_ignores = 0;
6380 int *pack_fds = NULL;
6384 memset(&st, 0, sizeof(st));
6385 st.status_codes = NULL;
6389 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6394 while ((ch = getopt(argc, argv, "IS:s:")) != -1) {
6400 if (st.status_codes != NULL && st.suppress == 0)
6401 option_conflict('S', 's');
6405 for (i = 0; optarg[i] != '\0'; i++) {
6406 switch (optarg[i]) {
6407 case GOT_STATUS_MODIFY:
6408 case GOT_STATUS_ADD:
6409 case GOT_STATUS_DELETE:
6410 case GOT_STATUS_CONFLICT:
6411 case GOT_STATUS_MISSING:
6412 case GOT_STATUS_OBSTRUCTED:
6413 case GOT_STATUS_UNVERSIONED:
6414 case GOT_STATUS_MODE_CHANGE:
6415 case GOT_STATUS_NONEXISTENT:
6418 errx(1, "invalid status code '%c'",
6422 if (ch == 's' && st.suppress)
6423 option_conflict('s', 'S');
6424 st.status_codes = optarg;
6435 cwd = getcwd(NULL, 0);
6437 error = got_error_from_errno("getcwd");
6441 error = got_repo_pack_fds_open(&pack_fds);
6445 error = got_worktree_open(&worktree, cwd,
6446 GOT_WORKTREE_GOT_DIR);
6448 if (error->code == GOT_ERR_NOT_WORKTREE)
6449 error = wrap_not_worktree_error(error, "status", cwd);
6453 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6458 error = apply_unveil(got_repo_get_path(repo), 1,
6459 got_worktree_get_root_path(worktree));
6463 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6467 error = got_worktree_status(worktree, &paths, repo, no_ignores,
6468 print_status, &st, check_cancelled, NULL);
6471 const struct got_error *pack_err =
6472 got_repo_pack_fds_close(pack_fds);
6477 const struct got_error *close_err = got_repo_close(repo);
6482 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
6490 fprintf(stderr, "usage: %s ref [-dlt] [-c object] [-r repository-path] "
6491 "[-s reference] [name]\n", getprogname());
6495 static const struct got_error *
6496 list_refs(struct got_repository *repo, const char *refname, int sort_by_time)
6498 static const struct got_error *err = NULL;
6499 struct got_reflist_head refs;
6500 struct got_reflist_entry *re;
6503 err = got_ref_list(&refs, repo, refname, sort_by_time ?
6504 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6509 TAILQ_FOREACH(re, &refs, entry) {
6511 refstr = got_ref_to_str(re->ref);
6512 if (refstr == NULL) {
6513 err = got_error_from_errno("got_ref_to_str");
6516 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
6520 got_ref_list_free(&refs);
6524 static const struct got_error *
6525 delete_ref_by_name(struct got_repository *repo, const char *refname)
6527 const struct got_error *err;
6528 struct got_reference *ref;
6530 err = got_ref_open(&ref, repo, refname, 0);
6534 err = delete_ref(repo, ref);
6539 static const struct got_error *
6540 add_ref(struct got_repository *repo, const char *refname, const char *target)
6542 const struct got_error *err = NULL;
6543 struct got_object_id *id = NULL;
6544 struct got_reference *ref = NULL;
6545 struct got_reflist_head refs;
6548 * Don't let the user create a reference name with a leading '-'.
6549 * While technically a valid reference name, this case is usually
6550 * an unintended typo.
6552 if (refname[0] == '-')
6553 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
6556 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
6559 err = got_repo_match_object_id(&id, NULL, target, GOT_OBJ_TYPE_ANY,
6561 got_ref_list_free(&refs);
6565 err = got_ref_alloc(&ref, refname, id);
6569 err = got_ref_write(ref, repo);
6577 static const struct got_error *
6578 add_symref(struct got_repository *repo, const char *refname, const char *target)
6580 const struct got_error *err = NULL;
6581 struct got_reference *ref = NULL;
6582 struct got_reference *target_ref = NULL;
6585 * Don't let the user create a reference name with a leading '-'.
6586 * While technically a valid reference name, this case is usually
6587 * an unintended typo.
6589 if (refname[0] == '-')
6590 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
6592 err = got_ref_open(&target_ref, repo, target, 0);
6596 err = got_ref_alloc_symref(&ref, refname, target_ref);
6600 err = got_ref_write(ref, repo);
6603 got_ref_close(target_ref);
6609 static const struct got_error *
6610 cmd_ref(int argc, char *argv[])
6612 const struct got_error *error = NULL;
6613 struct got_repository *repo = NULL;
6614 struct got_worktree *worktree = NULL;
6615 char *cwd = NULL, *repo_path = NULL;
6616 int ch, do_list = 0, do_delete = 0, sort_by_time = 0;
6617 const char *obj_arg = NULL, *symref_target= NULL;
6618 char *refname = NULL, *keyword_idstr = NULL;
6619 int *pack_fds = NULL;
6622 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6623 "sendfd unveil", NULL) == -1)
6627 while ((ch = getopt(argc, argv, "c:dlr:s:t")) != -1) {
6639 repo_path = realpath(optarg, NULL);
6640 if (repo_path == NULL)
6641 return got_error_from_errno2("realpath",
6643 got_path_strip_trailing_slashes(repo_path);
6646 symref_target = optarg;
6657 if (obj_arg && do_list)
6658 option_conflict('c', 'l');
6659 if (obj_arg && do_delete)
6660 option_conflict('c', 'd');
6661 if (obj_arg && symref_target)
6662 option_conflict('c', 's');
6663 if (symref_target && do_delete)
6664 option_conflict('s', 'd');
6665 if (symref_target && do_list)
6666 option_conflict('s', 'l');
6667 if (do_delete && do_list)
6668 option_conflict('d', 'l');
6669 if (sort_by_time && !do_list)
6670 errx(1, "-t option requires -l option");
6676 if (argc != 0 && argc != 1)
6679 refname = strdup(argv[0]);
6680 if (refname == NULL) {
6681 error = got_error_from_errno("strdup");
6688 refname = strdup(argv[0]);
6689 if (refname == NULL) {
6690 error = got_error_from_errno("strdup");
6696 got_path_strip_trailing_slashes(refname);
6698 cwd = getcwd(NULL, 0);
6700 error = got_error_from_errno("getcwd");
6704 error = got_repo_pack_fds_open(&pack_fds);
6708 if (repo_path == NULL) {
6709 error = got_worktree_open(&worktree, cwd,
6710 GOT_WORKTREE_GOT_DIR);
6711 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6717 strdup(got_worktree_get_repo_path(worktree));
6718 if (repo_path == NULL)
6719 error = got_error_from_errno("strdup");
6723 repo_path = strdup(cwd);
6724 if (repo_path == NULL) {
6725 error = got_error_from_errno("strdup");
6731 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6737 /* Remove "cpath" promise. */
6738 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6744 error = apply_unveil(got_repo_get_path(repo), do_list,
6745 worktree ? got_worktree_get_root_path(worktree) : NULL);
6750 error = list_refs(repo, refname, sort_by_time);
6752 error = delete_ref_by_name(repo, refname);
6753 else if (symref_target)
6754 error = add_symref(repo, refname, symref_target);
6756 if (obj_arg == NULL)
6759 error = got_keyword_to_idstr(&keyword_idstr, obj_arg,
6763 if (keyword_idstr != NULL)
6764 obj_arg = keyword_idstr;
6766 error = add_ref(repo, refname, obj_arg);
6771 const struct got_error *close_err = got_repo_close(repo);
6776 got_worktree_close(worktree);
6778 const struct got_error *pack_err =
6779 got_repo_pack_fds_close(pack_fds);
6785 free(keyword_idstr);
6792 fprintf(stderr, "usage: %s branch [-lnt] [-c commit] [-d name] "
6793 "[-r repository-path] [name]\n", getprogname());
6797 static const struct got_error *
6798 list_branch(struct got_repository *repo, struct got_worktree *worktree,
6799 struct got_reference *ref)
6801 const struct got_error *err = NULL;
6802 const char *refname;
6806 refname = got_ref_get_name(ref);
6807 if (worktree && strcmp(refname,
6808 got_worktree_get_head_ref_name(worktree)) == 0) {
6809 err = got_worktree_get_state(&marker, repo, worktree,
6810 check_cancelled, NULL);
6815 if (strncmp(refname, "refs/heads/", 11) == 0)
6817 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
6819 if (strncmp(refname, "refs/remotes/", 13) == 0)
6822 refstr = got_ref_to_str(ref);
6824 return got_error_from_errno("got_ref_to_str");
6826 printf("%c %s: %s\n", marker, refname, refstr);
6831 static const struct got_error *
6832 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
6834 const char *refname;
6836 if (worktree == NULL)
6837 return got_error(GOT_ERR_NOT_WORKTREE);
6839 refname = got_worktree_get_head_ref_name(worktree);
6841 if (strncmp(refname, "refs/heads/", 11) == 0)
6843 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
6846 printf("%s\n", refname);
6851 static const struct got_error *
6852 list_branches(struct got_repository *repo, struct got_worktree *worktree,
6855 static const struct got_error *err = NULL;
6856 struct got_reflist_head refs;
6857 struct got_reflist_entry *re;
6858 struct got_reference *temp_ref = NULL;
6859 int rebase_in_progress, histedit_in_progress;
6864 err = got_worktree_rebase_in_progress(&rebase_in_progress,
6869 err = got_worktree_histedit_in_progress(&histedit_in_progress,
6874 if (rebase_in_progress || histedit_in_progress) {
6875 err = got_ref_open(&temp_ref, repo,
6876 got_worktree_get_head_ref_name(worktree), 0);
6879 list_branch(repo, worktree, temp_ref);
6880 got_ref_close(temp_ref);
6884 err = got_ref_list(&refs, repo, "refs/heads", sort_by_time ?
6885 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6890 TAILQ_FOREACH(re, &refs, entry)
6891 list_branch(repo, worktree, re->ref);
6893 got_ref_list_free(&refs);
6895 err = got_ref_list(&refs, repo, "refs/remotes", sort_by_time ?
6896 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6901 TAILQ_FOREACH(re, &refs, entry)
6902 list_branch(repo, worktree, re->ref);
6904 got_ref_list_free(&refs);
6909 static const struct got_error *
6910 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
6911 const char *branch_name)
6913 const struct got_error *err = NULL;
6914 struct got_reference *ref = NULL;
6915 char *refname, *remote_refname = NULL;
6917 if (strncmp(branch_name, "refs/", 5) == 0)
6919 if (strncmp(branch_name, "heads/", 6) == 0)
6921 else if (strncmp(branch_name, "remotes/", 8) == 0)
6924 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
6925 return got_error_from_errno("asprintf");
6927 if (asprintf(&remote_refname, "refs/remotes/%s",
6928 branch_name) == -1) {
6929 err = got_error_from_errno("asprintf");
6933 err = got_ref_open(&ref, repo, refname, 0);
6935 const struct got_error *err2;
6936 if (err->code != GOT_ERR_NOT_REF)
6939 * Keep 'err' intact such that if neither branch exists
6940 * we report "refs/heads" rather than "refs/remotes" in
6941 * our error message.
6943 err2 = got_ref_open(&ref, repo, remote_refname, 0);
6950 strcmp(got_worktree_get_head_ref_name(worktree),
6951 got_ref_get_name(ref)) == 0) {
6952 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6953 "will not delete this work tree's current branch");
6957 err = delete_ref(repo, ref);
6962 free(remote_refname);
6966 static const struct got_error *
6967 add_branch(struct got_repository *repo, const char *branch_name,
6968 struct got_object_id *base_commit_id)
6970 const struct got_error *err = NULL;
6971 struct got_reference *ref = NULL;
6972 char *refname = NULL;
6975 * Don't let the user create a branch name with a leading '-'.
6976 * While technically a valid reference name, this case is usually
6977 * an unintended typo.
6979 if (branch_name[0] == '-')
6980 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
6982 if (strncmp(branch_name, "refs/heads/", 11) == 0)
6985 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
6986 err = got_error_from_errno("asprintf");
6990 err = got_ref_open(&ref, repo, refname, 0);
6992 err = got_error(GOT_ERR_BRANCH_EXISTS);
6994 } else if (err->code != GOT_ERR_NOT_REF)
6997 err = got_ref_alloc(&ref, refname, base_commit_id);
7001 err = got_ref_write(ref, repo);
7009 static const struct got_error *
7010 cmd_branch(int argc, char *argv[])
7012 const struct got_error *error = NULL;
7013 struct got_repository *repo = NULL;
7014 struct got_worktree *worktree = NULL;
7015 char *cwd = NULL, *repo_path = NULL;
7016 int ch, do_list = 0, do_show = 0, do_update = 1, sort_by_time = 0;
7017 const char *delref = NULL, *commit_id_arg = NULL;
7018 struct got_reference *ref = NULL;
7019 struct got_pathlist_head paths;
7020 struct got_object_id *commit_id = NULL;
7021 char *commit_id_str = NULL, *keyword_idstr = NULL;;
7022 int *pack_fds = NULL;
7027 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
7028 "sendfd unveil", NULL) == -1)
7032 while ((ch = getopt(argc, argv, "c:d:lnr:t")) != -1) {
7035 commit_id_arg = optarg;
7047 repo_path = realpath(optarg, NULL);
7048 if (repo_path == NULL)
7049 return got_error_from_errno2("realpath",
7051 got_path_strip_trailing_slashes(repo_path);
7062 if (do_list && delref)
7063 option_conflict('l', 'd');
7064 if (sort_by_time && !do_list)
7065 errx(1, "-t option requires -l option");
7070 if (!do_list && !delref && argc == 0)
7073 if ((do_list || delref || do_show) && commit_id_arg != NULL)
7074 errx(1, "-c option can only be used when creating a branch");
7076 if (do_list || delref) {
7079 } else if (!do_show && argc != 1)
7082 cwd = getcwd(NULL, 0);
7084 error = got_error_from_errno("getcwd");
7088 error = got_repo_pack_fds_open(&pack_fds);
7092 if (repo_path == NULL) {
7093 error = got_worktree_open(&worktree, cwd,
7094 GOT_WORKTREE_GOT_DIR);
7095 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7101 strdup(got_worktree_get_repo_path(worktree));
7102 if (repo_path == NULL)
7103 error = got_error_from_errno("strdup");
7107 repo_path = strdup(cwd);
7108 if (repo_path == NULL) {
7109 error = got_error_from_errno("strdup");
7115 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7120 if (do_list || do_show) {
7121 /* Remove "cpath" promise. */
7122 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
7128 error = apply_unveil(got_repo_get_path(repo), do_list,
7129 worktree ? got_worktree_get_root_path(worktree) : NULL);
7134 error = show_current_branch(repo, worktree);
7136 error = list_branches(repo, worktree, sort_by_time);
7138 error = delete_branch(repo, worktree, delref);
7140 struct got_reflist_head refs;
7142 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
7146 if (commit_id_arg == NULL)
7147 commit_id_arg = worktree ?
7148 got_worktree_get_head_ref_name(worktree) :
7151 error = got_keyword_to_idstr(&keyword_idstr,
7152 commit_id_arg, repo, worktree);
7155 if (keyword_idstr != NULL)
7156 commit_id_arg = keyword_idstr;
7158 error = got_repo_match_object_id(&commit_id, NULL,
7159 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &refs, repo);
7160 got_ref_list_free(&refs);
7163 error = add_branch(repo, argv[0], commit_id);
7166 if (worktree && do_update) {
7167 struct got_update_progress_arg upa;
7168 char *branch_refname = NULL;
7170 error = got_object_id_str(&commit_id_str, commit_id);
7173 error = get_worktree_paths_from_argv(&paths, 0, NULL,
7177 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
7179 error = got_error_from_errno("asprintf");
7182 error = got_ref_open(&ref, repo, branch_refname, 0);
7183 free(branch_refname);
7186 error = switch_head_ref(ref, commit_id, worktree,
7190 error = got_worktree_set_base_commit_id(worktree, repo,
7194 memset(&upa, 0, sizeof(upa));
7195 error = got_worktree_checkout_files(worktree, &paths,
7196 repo, update_progress, &upa, check_cancelled,
7200 if (upa.did_something) {
7201 printf("Updated to %s: %s\n",
7202 got_worktree_get_head_ref_name(worktree),
7205 print_update_progress_stats(&upa);
7209 free(keyword_idstr);
7213 const struct got_error *close_err = got_repo_close(repo);
7218 got_worktree_close(worktree);
7220 const struct got_error *pack_err =
7221 got_repo_pack_fds_close(pack_fds);
7228 free(commit_id_str);
7229 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
7237 fprintf(stderr, "usage: %s tag [-lVv] [-c commit] [-m message] "
7238 "[-r repository-path] [-s signer-id] name\n", getprogname());
7243 static const struct got_error *
7244 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
7246 const struct got_error *err = NULL;
7247 struct got_reflist_entry *re, *se, *new;
7248 struct got_object_id *re_id, *se_id;
7249 struct got_tag_object *re_tag, *se_tag;
7250 time_t re_time, se_time;
7252 STAILQ_FOREACH(re, tags, entry) {
7253 se = STAILQ_FIRST(sorted);
7255 err = got_reflist_entry_dup(&new, re);
7258 STAILQ_INSERT_HEAD(sorted, new, entry);
7261 err = got_ref_resolve(&re_id, repo, re->ref);
7264 err = got_object_open_as_tag(&re_tag, repo, re_id);
7268 re_time = got_object_tag_get_tagger_time(re_tag);
7269 got_object_tag_close(re_tag);
7273 err = got_ref_resolve(&se_id, repo, re->ref);
7276 err = got_object_open_as_tag(&se_tag, repo, se_id);
7280 se_time = got_object_tag_get_tagger_time(se_tag);
7281 got_object_tag_close(se_tag);
7283 if (se_time > re_time) {
7284 err = got_reflist_entry_dup(&new, re);
7287 STAILQ_INSERT_AFTER(sorted, se, new, entry);
7290 se = STAILQ_NEXT(se, entry);
7299 static const struct got_error *
7300 get_tag_refname(char **refname, const char *tag_name)
7302 const struct got_error *err;
7304 if (strncmp("refs/tags/", tag_name, 10) == 0) {
7305 *refname = strdup(tag_name);
7306 if (*refname == NULL)
7307 return got_error_from_errno("strdup");
7308 } else if (asprintf(refname, "refs/tags/%s", tag_name) == -1) {
7309 err = got_error_from_errno("asprintf");
7317 static const struct got_error *
7318 list_tags(struct got_repository *repo, const char *tag_name, int verify_tags,
7319 const char *allowed_signers, const char *revoked_signers, int verbosity)
7321 static const struct got_error *err = NULL;
7322 struct got_reflist_head refs;
7323 struct got_reflist_entry *re;
7324 char *wanted_refname = NULL;
7329 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
7334 struct got_reference *ref;
7335 err = get_tag_refname(&wanted_refname, tag_name);
7338 /* Wanted tag reference should exist. */
7339 err = got_ref_open(&ref, repo, wanted_refname, 0);
7345 TAILQ_FOREACH(re, &refs, entry) {
7346 const char *refname;
7347 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
7349 const char *tagger, *ssh_sig = NULL;
7350 char *sig_msg = NULL;
7352 struct got_object_id *id;
7353 struct got_tag_object *tag;
7354 struct got_commit_object *commit = NULL;
7356 refname = got_ref_get_name(re->ref);
7357 if (strncmp(refname, "refs/tags/", 10) != 0 ||
7358 (wanted_refname && strcmp(refname, wanted_refname) != 0))
7361 refstr = got_ref_to_str(re->ref);
7362 if (refstr == NULL) {
7363 err = got_error_from_errno("got_ref_to_str");
7367 err = got_ref_resolve(&id, repo, re->ref);
7370 err = got_object_open_as_tag(&tag, repo, id);
7372 if (err->code != GOT_ERR_OBJ_TYPE) {
7376 /* "lightweight" tag */
7377 err = got_object_open_as_commit(&commit, repo, id);
7382 tagger = got_object_commit_get_committer(commit);
7384 got_object_commit_get_committer_time(commit);
7385 err = got_object_id_str(&id_str, id);
7391 tagger = got_object_tag_get_tagger(tag);
7392 tagger_time = got_object_tag_get_tagger_time(tag);
7393 err = got_object_id_str(&id_str,
7394 got_object_tag_get_object_id(tag));
7399 if (tag && verify_tags) {
7400 ssh_sig = got_sigs_get_tagmsg_ssh_signature(
7401 got_object_tag_get_message(tag));
7402 if (ssh_sig && allowed_signers == NULL) {
7403 err = got_error_msg(
7404 GOT_ERR_VERIFY_TAG_SIGNATURE,
7405 "SSH signature verification requires "
7406 "setting allowed_signers in "
7412 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
7414 printf("from: %s\n", tagger);
7415 datestr = get_datestr(&tagger_time, datebuf);
7417 printf("date: %s UTC\n", datestr);
7419 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
7421 switch (got_object_tag_get_object_type(tag)) {
7422 case GOT_OBJ_TYPE_BLOB:
7423 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
7426 case GOT_OBJ_TYPE_TREE:
7427 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
7430 case GOT_OBJ_TYPE_COMMIT:
7431 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
7434 case GOT_OBJ_TYPE_TAG:
7435 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
7445 err = got_sigs_verify_tag_ssh(&sig_msg, tag, ssh_sig,
7446 allowed_signers, revoked_signers, verbosity);
7447 if (err && err->code == GOT_ERR_BAD_TAG_SIGNATURE)
7451 printf("signature: %s", sig_msg);
7457 err = got_object_commit_get_logmsg(&tagmsg0, commit);
7460 got_object_commit_close(commit);
7462 tagmsg0 = strdup(got_object_tag_get_message(tag));
7463 got_object_tag_close(tag);
7464 if (tagmsg0 == NULL) {
7465 err = got_error_from_errno("strdup");
7472 line = strsep(&tagmsg, "\n");
7474 printf(" %s\n", line);
7479 got_ref_list_free(&refs);
7480 free(wanted_refname);
7482 if (err == NULL && bad_sigs)
7483 err = got_error(GOT_ERR_BAD_TAG_SIGNATURE);
7487 static const struct got_error *
7488 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
7489 const char *tag_name, const char *repo_path)
7491 const struct got_error *err = NULL;
7492 char *template = NULL, *initial_content = NULL;
7493 char *editor = NULL;
7494 int initial_content_len;
7497 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
7498 err = got_error_from_errno("asprintf");
7502 initial_content_len = asprintf(&initial_content,
7503 "\n# tagging commit %s as %s\n",
7504 commit_id_str, tag_name);
7505 if (initial_content_len == -1) {
7506 err = got_error_from_errno("asprintf");
7510 err = got_opentemp_named_fd(tagmsg_path, &fd, template, "");
7514 if (write(fd, initial_content, initial_content_len) == -1) {
7515 err = got_error_from_errno2("write", *tagmsg_path);
7518 if (close(fd) == -1) {
7519 err = got_error_from_errno2("close", *tagmsg_path);
7524 err = get_editor(&editor);
7527 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content,
7528 initial_content_len, 1);
7530 free(initial_content);
7534 if (fd != -1 && close(fd) == -1 && err == NULL)
7535 err = got_error_from_errno2("close", *tagmsg_path);
7544 static const struct got_error *
7545 add_tag(struct got_repository *repo, const char *tagger,
7546 const char *tag_name, const char *commit_arg, const char *tagmsg_arg,
7547 const char *signer_id, int verbosity)
7549 const struct got_error *err = NULL;
7550 struct got_object_id *commit_id = NULL, *tag_id = NULL;
7551 char *label = NULL, *commit_id_str = NULL;
7552 struct got_reference *ref = NULL;
7553 char *refname = NULL, *tagmsg = NULL;
7554 char *tagmsg_path = NULL, *tag_id_str = NULL;
7555 int preserve_tagmsg = 0;
7556 struct got_reflist_head refs;
7561 * Don't let the user create a tag name with a leading '-'.
7562 * While technically a valid reference name, this case is usually
7563 * an unintended typo.
7565 if (tag_name[0] == '-')
7566 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
7568 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
7572 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
7573 GOT_OBJ_TYPE_COMMIT, &refs, repo);
7577 err = got_object_id_str(&commit_id_str, commit_id);
7581 err = get_tag_refname(&refname, tag_name);
7584 if (strncmp("refs/tags/", tag_name, 10) == 0)
7587 err = got_ref_open(&ref, repo, refname, 0);
7589 err = got_error(GOT_ERR_TAG_EXISTS);
7591 } else if (err->code != GOT_ERR_NOT_REF)
7594 if (tagmsg_arg == NULL) {
7595 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
7596 tag_name, got_repo_get_path(repo));
7598 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
7599 tagmsg_path != NULL)
7600 preserve_tagmsg = 1;
7603 /* Editor is done; we can now apply unveil(2) */
7604 err = got_sigs_apply_unveil();
7607 err = apply_unveil(got_repo_get_path(repo), 0, NULL);
7612 err = got_object_tag_create(&tag_id, tag_name, commit_id,
7613 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, signer_id, repo,
7617 preserve_tagmsg = 1;
7621 err = got_ref_alloc(&ref, refname, tag_id);
7624 preserve_tagmsg = 1;
7628 err = got_ref_write(ref, repo);
7631 preserve_tagmsg = 1;
7635 err = got_object_id_str(&tag_id_str, tag_id);
7638 preserve_tagmsg = 1;
7641 printf("Created tag %s\n", tag_id_str);
7643 if (preserve_tagmsg) {
7644 fprintf(stderr, "%s: tag message preserved in %s\n",
7645 getprogname(), tagmsg_path);
7646 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
7647 err = got_error_from_errno2("unlink", tagmsg_path);
7652 free(commit_id_str);
7656 got_ref_list_free(&refs);
7660 static const struct got_error *
7661 cmd_tag(int argc, char *argv[])
7663 const struct got_error *error = NULL;
7664 struct got_repository *repo = NULL;
7665 struct got_worktree *worktree = NULL;
7666 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
7667 char *gitconfig_path = NULL, *tagger = NULL, *keyword_idstr = NULL;
7668 char *allowed_signers = NULL, *revoked_signers = NULL;
7669 const char *signer_id = NULL;
7670 const char *tag_name = NULL, *commit_id_arg = NULL, *tagmsg = NULL;
7671 int ch, do_list = 0, verify_tags = 0, verbosity = 0;
7672 int *pack_fds = NULL;
7675 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
7676 "sendfd unveil", NULL) == -1)
7680 while ((ch = getopt(argc, argv, "c:lm:r:s:Vv")) != -1) {
7683 commit_id_arg = optarg;
7692 repo_path = realpath(optarg, NULL);
7693 if (repo_path == NULL) {
7694 error = got_error_from_errno2("realpath",
7698 got_path_strip_trailing_slashes(repo_path);
7709 else if (verbosity < 3)
7721 if (do_list || verify_tags) {
7722 if (commit_id_arg != NULL)
7724 "-c option can only be used when creating a tag");
7727 option_conflict('l', 'm');
7729 option_conflict('V', 'm');
7733 option_conflict('l', 's');
7735 option_conflict('V', 's');
7739 } else if (argc != 1)
7745 cwd = getcwd(NULL, 0);
7747 error = got_error_from_errno("getcwd");
7751 error = got_repo_pack_fds_open(&pack_fds);
7755 if (repo_path == NULL) {
7756 error = got_worktree_open(&worktree, cwd,
7757 GOT_WORKTREE_GOT_DIR);
7758 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7764 strdup(got_worktree_get_repo_path(worktree));
7765 if (repo_path == NULL)
7766 error = got_error_from_errno("strdup");
7770 repo_path = strdup(cwd);
7771 if (repo_path == NULL) {
7772 error = got_error_from_errno("strdup");
7778 if (do_list || verify_tags) {
7779 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7782 error = get_allowed_signers(&allowed_signers, repo, worktree);
7785 error = get_revoked_signers(&revoked_signers, repo, worktree);
7789 /* Release work tree lock. */
7790 got_worktree_close(worktree);
7795 * Remove "cpath" promise unless needed for signature tmpfile
7799 got_sigs_apply_unveil();
7802 if (pledge("stdio rpath wpath flock proc exec sendfd "
7803 "unveil", NULL) == -1)
7807 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7810 error = list_tags(repo, tag_name, verify_tags, allowed_signers,
7811 revoked_signers, verbosity);
7813 error = get_gitconfig_path(&gitconfig_path);
7816 error = got_repo_open(&repo, repo_path, gitconfig_path,
7821 error = get_author(&tagger, repo, worktree);
7824 if (signer_id == NULL)
7825 signer_id = get_signer_id(repo, worktree);
7829 error = got_sigs_apply_unveil();
7833 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
7838 if (commit_id_arg == NULL) {
7839 struct got_reference *head_ref;
7840 struct got_object_id *commit_id;
7841 error = got_ref_open(&head_ref, repo,
7842 worktree ? got_worktree_get_head_ref_name(worktree)
7846 error = got_ref_resolve(&commit_id, repo, head_ref);
7847 got_ref_close(head_ref);
7850 error = got_object_id_str(&commit_id_str, commit_id);
7855 error = got_keyword_to_idstr(&keyword_idstr,
7856 commit_id_arg, repo, worktree);
7859 commit_id_str = keyword_idstr;
7863 /* Release work tree lock. */
7864 got_worktree_close(worktree);
7868 error = add_tag(repo, tagger, tag_name,
7869 commit_id_str ? commit_id_str : commit_id_arg, tagmsg,
7870 signer_id, verbosity);
7874 const struct got_error *close_err = got_repo_close(repo);
7879 got_worktree_close(worktree);
7881 const struct got_error *pack_err =
7882 got_repo_pack_fds_close(pack_fds);
7888 free(gitconfig_path);
7889 free(commit_id_str);
7891 free(allowed_signers);
7892 free(revoked_signers);
7899 fprintf(stderr, "usage: %s add [-IR] path ...\n", getprogname());
7903 static const struct got_error *
7904 add_progress(void *arg, unsigned char status, const char *path)
7906 while (path[0] == '/')
7908 printf("%c %s\n", status, path);
7912 static const struct got_error *
7913 cmd_add(int argc, char *argv[])
7915 const struct got_error *error = NULL;
7916 struct got_repository *repo = NULL;
7917 struct got_worktree *worktree = NULL;
7919 struct got_pathlist_head paths;
7920 struct got_pathlist_entry *pe;
7921 int ch, can_recurse = 0, no_ignores = 0;
7922 int *pack_fds = NULL;
7927 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7932 while ((ch = getopt(argc, argv, "IR")) != -1) {
7952 cwd = getcwd(NULL, 0);
7954 error = got_error_from_errno("getcwd");
7958 error = got_repo_pack_fds_open(&pack_fds);
7962 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
7964 if (error->code == GOT_ERR_NOT_WORKTREE)
7965 error = wrap_not_worktree_error(error, "add", cwd);
7969 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7974 error = apply_unveil(got_repo_get_path(repo), 1,
7975 got_worktree_get_root_path(worktree));
7979 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7986 TAILQ_FOREACH(pe, &paths, entry) {
7987 if (asprintf(&ondisk_path, "%s/%s",
7988 got_worktree_get_root_path(worktree),
7990 error = got_error_from_errno("asprintf");
7993 if (lstat(ondisk_path, &sb) == -1) {
7994 if (errno == ENOENT) {
7998 error = got_error_from_errno2("lstat",
8004 if (S_ISDIR(sb.st_mode)) {
8005 error = got_error_msg(GOT_ERR_BAD_PATH,
8006 "adding directories requires -R option");
8012 error = got_worktree_schedule_add(worktree, &paths, add_progress,
8013 NULL, repo, no_ignores);
8016 const struct got_error *close_err = got_repo_close(repo);
8021 got_worktree_close(worktree);
8023 const struct got_error *pack_err =
8024 got_repo_pack_fds_close(pack_fds);
8028 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
8036 fprintf(stderr, "usage: %s remove [-fkR] [-s status-codes] path ...\n",
8041 static const struct got_error *
8042 print_remove_status(void *arg, unsigned char status,
8043 unsigned char staged_status, const char *path)
8045 while (path[0] == '/')
8047 if (status == GOT_STATUS_NONEXISTENT)
8049 if (status == staged_status && (status == GOT_STATUS_DELETE))
8050 status = GOT_STATUS_NO_CHANGE;
8051 printf("%c%c %s\n", status, staged_status, path);
8055 static const struct got_error *
8056 cmd_remove(int argc, char *argv[])
8058 const struct got_error *error = NULL;
8059 struct got_worktree *worktree = NULL;
8060 struct got_repository *repo = NULL;
8061 const char *status_codes = NULL;
8063 struct got_pathlist_head paths;
8064 struct got_pathlist_entry *pe;
8065 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0, i;
8066 int ignore_missing_paths = 0;
8067 int *pack_fds = NULL;
8072 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
8077 while ((ch = getopt(argc, argv, "fkRs:")) != -1) {
8080 delete_local_mods = 1;
8081 ignore_missing_paths = 1;
8090 for (i = 0; optarg[i] != '\0'; i++) {
8091 switch (optarg[i]) {
8092 case GOT_STATUS_MODIFY:
8093 delete_local_mods = 1;
8095 case GOT_STATUS_MISSING:
8096 ignore_missing_paths = 1;
8099 errx(1, "invalid status code '%c'",
8103 status_codes = optarg;
8117 cwd = getcwd(NULL, 0);
8119 error = got_error_from_errno("getcwd");
8123 error = got_repo_pack_fds_open(&pack_fds);
8127 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
8129 if (error->code == GOT_ERR_NOT_WORKTREE)
8130 error = wrap_not_worktree_error(error, "remove", cwd);
8134 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8139 error = apply_unveil(got_repo_get_path(repo), 1,
8140 got_worktree_get_root_path(worktree));
8144 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8151 TAILQ_FOREACH(pe, &paths, entry) {
8152 if (asprintf(&ondisk_path, "%s/%s",
8153 got_worktree_get_root_path(worktree),
8155 error = got_error_from_errno("asprintf");
8158 if (lstat(ondisk_path, &sb) == -1) {
8159 if (errno == ENOENT) {
8163 error = got_error_from_errno2("lstat",
8169 if (S_ISDIR(sb.st_mode)) {
8170 error = got_error_msg(GOT_ERR_BAD_PATH,
8171 "removing directories requires -R option");
8177 error = got_worktree_schedule_delete(worktree, &paths,
8178 delete_local_mods, status_codes, print_remove_status, NULL,
8179 repo, keep_on_disk, ignore_missing_paths);
8182 const struct got_error *close_err = got_repo_close(repo);
8187 got_worktree_close(worktree);
8189 const struct got_error *pack_err =
8190 got_repo_pack_fds_close(pack_fds);
8194 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
8202 fprintf(stderr, "usage: %s patch [-nR] [-c commit] [-p strip-count] "
8203 "[patchfile]\n", getprogname());
8207 static const struct got_error *
8208 patch_from_stdin(int *patchfd)
8210 const struct got_error *err = NULL;
8213 sig_t sighup, sigint, sigquit;
8215 *patchfd = got_opentempfd();
8217 return got_error_from_errno("got_opentempfd");
8219 sighup = signal(SIGHUP, SIG_DFL);
8220 sigint = signal(SIGINT, SIG_DFL);
8221 sigquit = signal(SIGQUIT, SIG_DFL);
8224 r = read(0, buf, sizeof(buf));
8226 err = got_error_from_errno("read");
8231 if (write(*patchfd, buf, r) == -1) {
8232 err = got_error_from_errno("write");
8237 signal(SIGHUP, sighup);
8238 signal(SIGINT, sigint);
8239 signal(SIGQUIT, sigquit);
8241 if (err == NULL && lseek(*patchfd, 0, SEEK_SET) == -1)
8242 err = got_error_from_errno("lseek");
8252 struct got_patch_progress_arg {
8258 static const struct got_error *
8259 patch_progress(void *arg, const char *old, const char *new,
8260 unsigned char status, const struct got_error *error, int old_from,
8261 int old_lines, int new_from, int new_lines, int offset,
8262 int ws_mangled, const struct got_error *hunk_err)
8264 const char *path = new == NULL ? old : new;
8265 struct got_patch_progress_arg *a = arg;
8267 while (*path == '/')
8270 if (status != GOT_STATUS_NO_CHANGE &&
8271 status != 0 /* per-hunk progress */) {
8272 printf("%c %s\n", status, path);
8273 a->did_something = 1;
8276 if (hunk_err == NULL) {
8277 if (status == GOT_STATUS_CANNOT_UPDATE)
8279 else if (status == GOT_STATUS_CONFLICT)
8284 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
8286 if (offset != 0 || hunk_err != NULL || ws_mangled) {
8287 printf("@@ -%d,%d +%d,%d @@ ", old_from,
8288 old_lines, new_from, new_lines);
8289 if (hunk_err != NULL)
8290 printf("%s\n", hunk_err->msg);
8291 else if (offset != 0)
8292 printf("applied with offset %d\n", offset);
8294 printf("hunk contains mangled whitespace\n");
8301 print_patch_progress_stats(struct got_patch_progress_arg *ppa)
8303 if (!ppa->did_something)
8306 if (ppa->conflicts > 0)
8307 printf("Files with merge conflicts: %d\n", ppa->conflicts);
8309 if (ppa->rejects > 0) {
8310 printf("Files where patch failed to apply: %d\n",
8315 static const struct got_error *
8316 cmd_patch(int argc, char *argv[])
8318 const struct got_error *error = NULL, *close_error = NULL;
8319 struct got_worktree *worktree = NULL;
8320 struct got_repository *repo = NULL;
8321 struct got_reflist_head refs;
8322 struct got_object_id *commit_id = NULL;
8323 const char *commit_id_str = NULL;
8326 char *cwd = NULL, *keyword_idstr = NULL;
8327 int ch, nop = 0, strip = -1, reverse = 0;
8329 int *pack_fds = NULL;
8330 struct got_patch_progress_arg ppa;
8335 if (pledge("stdio rpath wpath cpath fattr proc exec sendfd flock "
8336 "unveil", NULL) == -1)
8340 while ((ch = getopt(argc, argv, "c:np:R")) != -1) {
8343 commit_id_str = optarg;
8349 strip = strtonum(optarg, 0, INT_MAX, &errstr);
8351 errx(1, "pathname strip count is %s: %s",
8367 error = patch_from_stdin(&patchfd);
8370 } else if (argc == 1) {
8371 patchfd = open(argv[0], O_RDONLY);
8372 if (patchfd == -1) {
8373 error = got_error_from_errno2("open", argv[0]);
8376 if (fstat(patchfd, &sb) == -1) {
8377 error = got_error_from_errno2("fstat", argv[0]);
8380 if (!S_ISREG(sb.st_mode)) {
8381 error = got_error_path(argv[0], GOT_ERR_BAD_FILETYPE);
8387 if ((cwd = getcwd(NULL, 0)) == NULL) {
8388 error = got_error_from_errno("getcwd");
8392 error = got_repo_pack_fds_open(&pack_fds);
8396 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
8400 const char *repo_path = got_worktree_get_repo_path(worktree);
8401 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8405 error = apply_unveil(got_repo_get_path(repo), 0,
8406 got_worktree_get_root_path(worktree));
8410 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
8414 if (commit_id_str != NULL) {
8415 error = got_keyword_to_idstr(&keyword_idstr, commit_id_str,
8420 error = got_repo_match_object_id(&commit_id, NULL,
8421 keyword_idstr != NULL ? keyword_idstr : commit_id_str,
8422 GOT_OBJ_TYPE_COMMIT, &refs, repo);
8427 memset(&ppa, 0, sizeof(ppa));
8428 error = got_patch(patchfd, worktree, repo, nop, strip, reverse,
8429 commit_id, patch_progress, &ppa, check_cancelled, NULL);
8430 print_patch_progress_stats(&ppa);
8432 got_ref_list_free(&refs);
8433 free(keyword_idstr);
8436 close_error = got_repo_close(repo);
8438 error = close_error;
8440 if (worktree != NULL) {
8441 close_error = got_worktree_close(worktree);
8443 error = close_error;
8446 const struct got_error *pack_err =
8447 got_repo_pack_fds_close(pack_fds);
8458 fprintf(stderr, "usage: %s revert [-pR] [-F response-script] path ...\n",
8463 static const struct got_error *
8464 revert_progress(void *arg, unsigned char status, const char *path)
8466 if (status == GOT_STATUS_UNVERSIONED)
8469 while (path[0] == '/')
8471 printf("%c %s\n", status, path);
8475 struct choose_patch_arg {
8476 FILE *patch_script_file;
8480 static const struct got_error *
8481 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
8482 int nchanges, const char *action)
8484 const struct got_error *err;
8486 size_t linesize = 0;
8490 case GOT_STATUS_ADD:
8491 printf("A %s\n%s this addition? [y/n] ", path, action);
8493 case GOT_STATUS_DELETE:
8494 printf("D %s\n%s this deletion? [y/n] ", path, action);
8496 case GOT_STATUS_MODIFY:
8497 if (fseek(patch_file, 0L, SEEK_SET) == -1)
8498 return got_error_from_errno("fseek");
8499 printf(GOT_COMMIT_SEP_STR);
8500 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
8502 if (linelen == -1 && ferror(patch_file)) {
8503 err = got_error_from_errno("getline");
8508 printf(GOT_COMMIT_SEP_STR);
8509 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
8510 path, n, nchanges, action);
8513 return got_error_path(path, GOT_ERR_FILE_STATUS);
8520 static const struct got_error *
8521 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
8522 FILE *patch_file, int n, int nchanges)
8524 const struct got_error *err = NULL;
8526 size_t linesize = 0;
8529 struct choose_patch_arg *a = arg;
8531 *choice = GOT_PATCH_CHOICE_NONE;
8533 if (a->patch_script_file) {
8535 err = show_change(status, path, patch_file, n, nchanges,
8539 linelen = getline(&line, &linesize, a->patch_script_file);
8540 if (linelen == -1) {
8541 if (ferror(a->patch_script_file))
8542 return got_error_from_errno("getline");
8545 nl = strchr(line, '\n');
8548 if (strcmp(line, "y") == 0) {
8549 *choice = GOT_PATCH_CHOICE_YES;
8551 } else if (strcmp(line, "n") == 0) {
8552 *choice = GOT_PATCH_CHOICE_NO;
8554 } else if (strcmp(line, "q") == 0 &&
8555 status == GOT_STATUS_MODIFY) {
8556 *choice = GOT_PATCH_CHOICE_QUIT;
8559 printf("invalid response '%s'\n", line);
8564 while (resp != 'y' && resp != 'n' && resp != 'q') {
8565 err = show_change(status, path, patch_file, n, nchanges,
8572 if (status == GOT_STATUS_MODIFY) {
8573 if (resp != 'y' && resp != 'n' && resp != 'q') {
8574 printf("invalid response '%c'\n", resp);
8577 } else if (resp != 'y' && resp != 'n') {
8578 printf("invalid response '%c'\n", resp);
8584 *choice = GOT_PATCH_CHOICE_YES;
8585 else if (resp == 'n')
8586 *choice = GOT_PATCH_CHOICE_NO;
8587 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
8588 *choice = GOT_PATCH_CHOICE_QUIT;
8593 struct wt_commitable_path_arg {
8594 struct got_pathlist_head *commit_paths;
8599 * Shortcut work tree status callback to determine if the set of paths scanned
8600 * has at least one versioned path that is being modified and, if not NULL, is
8601 * in the arg->commit_paths list. Set arg and return GOT_ERR_FILE_MODIFIED as
8602 * soon as a path is passed with a status that satisfies this criteria.
8604 static const struct got_error *
8605 worktree_has_commitable_path(void *arg, unsigned char status,
8606 unsigned char staged_status, const char *path,
8607 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8608 struct got_object_id *commit_id, int dirfd, const char *de_name)
8610 struct wt_commitable_path_arg *a = arg;
8612 if (status == staged_status && (status == GOT_STATUS_DELETE))
8613 status = GOT_STATUS_NO_CHANGE;
8615 if (!(status == GOT_STATUS_NO_CHANGE ||
8616 status == GOT_STATUS_UNVERSIONED) ||
8617 staged_status != GOT_STATUS_NO_CHANGE) {
8618 if (a->commit_paths != NULL) {
8619 struct got_pathlist_entry *pe;
8621 TAILQ_FOREACH(pe, a->commit_paths, entry) {
8622 if (strncmp(path, pe->path,
8623 pe->path_len) == 0) {
8624 *a->has_changes = 1;
8629 *a->has_changes = 1;
8631 if (*a->has_changes)
8632 return got_error(GOT_ERR_FILE_MODIFIED);
8639 * Check that the changeset of the commit identified by id is
8640 * comprised of at least one modified path that is being committed.
8642 static const struct got_error *
8643 commit_path_changed_in_worktree(struct wt_commitable_path_arg *wcpa,
8644 struct got_object_id *id, struct got_worktree *worktree,
8645 struct got_repository *repo)
8647 const struct got_error *err;
8648 struct got_pathlist_head paths;
8649 struct got_commit_object *commit = NULL, *pcommit = NULL;
8650 struct got_tree_object *tree = NULL, *ptree = NULL;
8651 struct got_object_qid *pid;
8655 err = got_object_open_as_commit(&commit, repo, id);
8659 err = got_object_open_as_tree(&tree, repo,
8660 got_object_commit_get_tree_id(commit));
8664 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
8666 err = got_object_open_as_commit(&pcommit, repo, &pid->id);
8670 err = got_object_open_as_tree(&ptree, repo,
8671 got_object_commit_get_tree_id(pcommit));
8676 err = got_diff_tree(ptree, tree, NULL, NULL, -1, -1, "", "", repo,
8677 got_diff_tree_collect_changed_paths, &paths, 0);
8681 err = got_worktree_status(worktree, &paths, repo, 0,
8682 worktree_has_commitable_path, wcpa, check_cancelled, NULL);
8683 if (err && err->code == GOT_ERR_FILE_MODIFIED) {
8685 * At least one changed path in the referenced commit is
8686 * modified in the work tree, that's all we need to know!
8692 got_pathlist_free(&paths, GOT_PATHLIST_FREE_ALL);
8694 got_object_commit_close(commit);
8696 got_object_commit_close(pcommit);
8698 got_object_tree_close(tree);
8700 got_object_tree_close(ptree);
8705 * Remove any "logmsg" reference comprised entirely of paths that have
8706 * been reverted in this work tree. If any path in the logmsg ref changeset
8707 * remains in a changed state in the worktree, do not remove the reference.
8709 static const struct got_error *
8710 rm_logmsg_ref(struct got_worktree *worktree, struct got_repository *repo)
8712 const struct got_error *err;
8713 struct got_reflist_head refs;
8714 struct got_reflist_entry *re;
8715 struct got_commit_object *commit = NULL;
8716 struct got_object_id *commit_id = NULL;
8717 struct wt_commitable_path_arg wcpa;
8718 char *uuidstr = NULL;
8722 err = got_worktree_get_uuid(&uuidstr, worktree);
8726 err = got_ref_list(&refs, repo, "refs/got/worktree",
8727 got_ref_cmp_by_name, repo);
8731 TAILQ_FOREACH(re, &refs, entry) {
8732 const char *refname;
8733 int has_changes = 0;
8735 refname = got_ref_get_name(re->ref);
8737 if (!strncmp(refname, GOT_WORKTREE_CHERRYPICK_REF_PREFIX,
8738 GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN))
8739 refname += GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN + 1;
8740 else if (!strncmp(refname, GOT_WORKTREE_BACKOUT_REF_PREFIX,
8741 GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN))
8742 refname += GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN + 1;
8746 if (strncmp(refname, uuidstr, GOT_WORKTREE_UUID_STRLEN) == 0)
8747 refname += GOT_WORKTREE_UUID_STRLEN + 1; /* skip '-' */
8751 err = got_repo_match_object_id(&commit_id, NULL, refname,
8752 GOT_OBJ_TYPE_COMMIT, NULL, repo);
8756 err = got_object_open_as_commit(&commit, repo, commit_id);
8760 wcpa.commit_paths = NULL;
8761 wcpa.has_changes = &has_changes;
8763 err = commit_path_changed_in_worktree(&wcpa, commit_id,
8769 err = got_ref_delete(re->ref, repo);
8774 got_object_commit_close(commit);
8783 got_ref_list_free(&refs);
8785 got_object_commit_close(commit);
8789 static const struct got_error *
8790 cmd_revert(int argc, char *argv[])
8792 const struct got_error *error = NULL;
8793 struct got_worktree *worktree = NULL;
8794 struct got_repository *repo = NULL;
8795 char *cwd = NULL, *path = NULL;
8796 struct got_pathlist_head paths;
8797 struct got_pathlist_entry *pe;
8798 int ch, can_recurse = 0, pflag = 0;
8799 FILE *patch_script_file = NULL;
8800 const char *patch_script_path = NULL;
8801 struct choose_patch_arg cpa;
8802 int *pack_fds = NULL;
8807 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8808 "unveil", NULL) == -1)
8812 while ((ch = getopt(argc, argv, "F:pR")) != -1) {
8815 patch_script_path = optarg;
8834 if (patch_script_path && !pflag)
8835 errx(1, "-F option can only be used together with -p option");
8837 cwd = getcwd(NULL, 0);
8839 error = got_error_from_errno("getcwd");
8843 error = got_repo_pack_fds_open(&pack_fds);
8847 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
8849 if (error->code == GOT_ERR_NOT_WORKTREE)
8850 error = wrap_not_worktree_error(error, "revert", cwd);
8854 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8859 if (patch_script_path) {
8860 patch_script_file = fopen(patch_script_path, "re");
8861 if (patch_script_file == NULL) {
8862 error = got_error_from_errno2("fopen",
8869 * XXX "c" perm needed on repo dir to delete merge references.
8871 error = apply_unveil(got_repo_get_path(repo), 0,
8872 got_worktree_get_root_path(worktree));
8876 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8883 TAILQ_FOREACH(pe, &paths, entry) {
8884 if (asprintf(&ondisk_path, "%s/%s",
8885 got_worktree_get_root_path(worktree),
8887 error = got_error_from_errno("asprintf");
8890 if (lstat(ondisk_path, &sb) == -1) {
8891 if (errno == ENOENT) {
8895 error = got_error_from_errno2("lstat",
8901 if (S_ISDIR(sb.st_mode)) {
8902 error = got_error_msg(GOT_ERR_BAD_PATH,
8903 "reverting directories requires -R option");
8909 cpa.patch_script_file = patch_script_file;
8910 cpa.action = "revert";
8911 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
8912 pflag ? choose_patch : NULL, &cpa, repo);
8914 error = rm_logmsg_ref(worktree, repo);
8916 if (patch_script_file && fclose(patch_script_file) == EOF &&
8918 error = got_error_from_errno2("fclose", patch_script_path);
8920 const struct got_error *close_err = got_repo_close(repo);
8925 got_worktree_close(worktree);
8927 const struct got_error *pack_err =
8928 got_repo_pack_fds_close(pack_fds);
8932 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
8941 fprintf(stderr, "usage: %s commit [-CNnS] [-A author] [-F path] "
8942 "[-m message] [path ...]\n", getprogname());
8946 struct collect_commit_logmsg_arg {
8947 const char *cmdline_log;
8948 const char *prepared_log;
8949 const char *merged_log;
8950 int non_interactive;
8952 const char *worktree_path;
8953 const char *branch_name;
8954 const char *repo_path;
8959 static const struct got_error *
8960 read_prepared_logmsg(char **logmsg, const char *path)
8962 const struct got_error *err = NULL;
8968 memset(&sb, 0, sizeof(sb));
8970 f = fopen(path, "re");
8972 return got_error_from_errno2("fopen", path);
8974 if (fstat(fileno(f), &sb) == -1) {
8975 err = got_error_from_errno2("fstat", path);
8978 if (sb.st_size == 0) {
8979 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
8983 *logmsg = malloc(sb.st_size + 1);
8984 if (*logmsg == NULL) {
8985 err = got_error_from_errno("malloc");
8989 r = fread(*logmsg, 1, sb.st_size, f);
8990 if (r != sb.st_size) {
8992 err = got_error_from_errno2("fread", path);
8994 err = got_error(GOT_ERR_IO);
8997 (*logmsg)[sb.st_size] = '\0';
8999 if (fclose(f) == EOF && err == NULL)
9000 err = got_error_from_errno2("fclose", path);
9008 static const struct got_error *
9009 collect_commit_logmsg(struct got_pathlist_head *commitable_paths,
9010 const char *diff_path, char **logmsg, void *arg)
9012 char *initial_content = NULL;
9013 struct got_pathlist_entry *pe;
9014 const struct got_error *err = NULL;
9015 char *template = NULL;
9016 char *prepared_msg = NULL, *merged_msg = NULL;
9017 struct collect_commit_logmsg_arg *a = arg;
9018 int initial_content_len;
9022 /* if a message was specified on the command line, just use it */
9023 if (a->cmdline_log != NULL && *a->cmdline_log != '\0') {
9024 len = strlen(a->cmdline_log) + 1;
9025 *logmsg = malloc(len + 1);
9026 if (*logmsg == NULL)
9027 return got_error_from_errno("malloc");
9028 strlcpy(*logmsg, a->cmdline_log, len);
9030 } else if (a->prepared_log != NULL && a->non_interactive)
9031 return read_prepared_logmsg(logmsg, a->prepared_log);
9033 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
9034 return got_error_from_errno("asprintf");
9036 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template, "");
9040 if (a->prepared_log) {
9041 err = read_prepared_logmsg(&prepared_msg, a->prepared_log);
9044 } else if (a->merged_log) {
9045 err = read_prepared_logmsg(&merged_msg, a->merged_log);
9050 initial_content_len = asprintf(&initial_content,
9051 "%s%s\n# changes to be committed on branch %s:\n",
9052 prepared_msg ? prepared_msg : "",
9053 merged_msg ? merged_msg : "", a->branch_name);
9054 if (initial_content_len == -1) {
9055 err = got_error_from_errno("asprintf");
9059 if (write(fd, initial_content, initial_content_len) == -1) {
9060 err = got_error_from_errno2("write", a->logmsg_path);
9064 TAILQ_FOREACH(pe, commitable_paths, entry) {
9065 struct got_commitable *ct = pe->data;
9066 dprintf(fd, "# %c %s\n",
9067 got_commitable_get_status(ct),
9068 got_commitable_get_path(ct));
9072 dprintf(fd, "# detailed changes can be viewed in %s\n",
9076 if (close(fd) == -1) {
9077 err = got_error_from_errno2("close", a->logmsg_path);
9082 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content,
9083 initial_content_len, a->prepared_log ? 0 : 1);
9085 free(initial_content);
9090 if (fd != -1 && close(fd) == -1 && err == NULL)
9091 err = got_error_from_errno2("close", a->logmsg_path);
9093 /* Editor is done; we can now apply unveil(2) */
9095 err = apply_unveil(a->repo_path, 0, a->worktree_path);
9103 static const struct got_error *
9104 cat_logmsg(FILE *f, struct got_commit_object *commit, const char *idstr,
9105 const char *type, int has_content)
9107 const struct got_error *err = NULL;
9108 char *logmsg = NULL;
9110 err = got_object_commit_get_logmsg(&logmsg, commit);
9114 if (fprintf(f, "%s# log message of %s commit %s:%s",
9115 has_content ? "\n" : "", type, idstr, logmsg) < 0)
9116 err = got_ferror(f, GOT_ERR_IO);
9123 * Lookup "logmsg" references of backed-out and cherrypicked commits
9124 * belonging to the current work tree. If found, and the worktree has
9125 * at least one modified file that was changed in the referenced commit,
9126 * add its log message to a new temporary file at *logmsg_path.
9127 * Add all refs found to matched_refs to be scheduled for removal on
9128 * successful commit.
9130 static const struct got_error *
9131 lookup_logmsg_ref(char **logmsg_path, struct got_pathlist_head *paths,
9132 struct got_reflist_head *matched_refs, struct got_worktree *worktree,
9133 struct got_repository *repo)
9135 const struct got_error *err;
9136 struct got_commit_object *commit = NULL;
9137 struct got_object_id *id = NULL;
9138 struct got_reflist_head refs;
9139 struct got_reflist_entry *re, *re_match;
9141 char *uuidstr = NULL;
9142 int added_logmsg = 0;
9146 *logmsg_path = NULL;
9148 err = got_worktree_get_uuid(&uuidstr, worktree);
9152 err = got_ref_list(&refs, repo, "refs/got/worktree",
9153 got_ref_cmp_by_name, repo);
9157 TAILQ_FOREACH(re, &refs, entry) {
9158 const char *refname, *type;
9159 struct wt_commitable_path_arg wcpa;
9162 refname = got_ref_get_name(re->ref);
9164 if (strncmp(refname, GOT_WORKTREE_CHERRYPICK_REF_PREFIX,
9165 GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN) == 0) {
9166 refname += GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN + 1;
9167 type = "cherrypicked";
9168 } else if (strncmp(refname, GOT_WORKTREE_BACKOUT_REF_PREFIX,
9169 GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN) == 0) {
9170 refname += GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN + 1;
9171 type = "backed-out";
9175 if (strncmp(refname, uuidstr, GOT_WORKTREE_UUID_STRLEN) == 0)
9176 refname += GOT_WORKTREE_UUID_STRLEN + 1; /* skip '-' */
9180 err = got_repo_match_object_id(&id, NULL, refname,
9181 GOT_OBJ_TYPE_COMMIT, NULL, repo);
9185 err = got_object_open_as_commit(&commit, repo, id);
9189 wcpa.commit_paths = paths;
9190 wcpa.has_changes = &add_logmsg;
9192 err = commit_path_changed_in_worktree(&wcpa, id,
9199 err = got_opentemp_named(logmsg_path, &f,
9200 "got-commit-logmsg", "");
9204 err = cat_logmsg(f, commit, refname, type,
9211 err = got_reflist_entry_dup(&re_match, re);
9214 TAILQ_INSERT_HEAD(matched_refs, re_match, entry);
9217 got_object_commit_close(commit);
9226 got_ref_list_free(&refs);
9228 got_object_commit_close(commit);
9229 if (f && fclose(f) == EOF && err == NULL)
9230 err = got_error_from_errno("fclose");
9231 if (!added_logmsg) {
9232 if (*logmsg_path && unlink(*logmsg_path) != 0 && err == NULL)
9233 err = got_error_from_errno2("unlink", *logmsg_path);
9234 *logmsg_path = NULL;
9239 static const struct got_error *
9240 cmd_commit(int argc, char *argv[])
9242 const struct got_error *error = NULL;
9243 struct got_worktree *worktree = NULL;
9244 struct got_repository *repo = NULL;
9245 char *cwd = NULL, *id_str = NULL;
9246 struct got_object_id *id = NULL;
9247 const char *logmsg = NULL;
9248 char *prepared_logmsg = NULL, *merged_logmsg = NULL;
9249 struct collect_commit_logmsg_arg cl_arg;
9250 const char *author = NULL;
9251 char *gitconfig_path = NULL, *editor = NULL, *committer = NULL;
9252 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
9253 int allow_bad_symlinks = 0, non_interactive = 0, merge_in_progress = 0;
9254 int show_diff = 1, commit_conflicts = 0;
9255 struct got_pathlist_head paths;
9256 struct got_reflist_head refs;
9257 struct got_reflist_entry *re;
9258 int *pack_fds = NULL;
9262 cl_arg.logmsg_path = NULL;
9265 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9266 "unveil", NULL) == -1)
9270 while ((ch = getopt(argc, argv, "A:CF:m:NnS")) != -1) {
9274 error = valid_author(author);
9279 commit_conflicts = 1;
9283 option_conflict('F', 'm');
9284 prepared_logmsg = realpath(optarg, NULL);
9285 if (prepared_logmsg == NULL)
9286 return got_error_from_errno2("realpath",
9290 if (prepared_logmsg)
9291 option_conflict('m', 'F');
9295 non_interactive = 1;
9301 allow_bad_symlinks = 1;
9312 cwd = getcwd(NULL, 0);
9314 error = got_error_from_errno("getcwd");
9318 error = got_repo_pack_fds_open(&pack_fds);
9322 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
9324 if (error->code == GOT_ERR_NOT_WORKTREE)
9325 error = wrap_not_worktree_error(error, "commit", cwd);
9329 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
9332 if (rebase_in_progress) {
9333 error = got_error(GOT_ERR_REBASING);
9337 error = got_worktree_histedit_in_progress(&histedit_in_progress,
9342 error = get_gitconfig_path(&gitconfig_path);
9345 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9346 gitconfig_path, pack_fds);
9350 error = got_worktree_merge_in_progress(&merge_in_progress, worktree, repo);
9353 if (merge_in_progress) {
9354 error = got_error(GOT_ERR_MERGE_BUSY);
9358 error = get_author(&committer, repo, worktree);
9366 * unveil(2) traverses exec(2); if an editor is used we have
9367 * to apply unveil after the log message has been written.
9369 if (logmsg == NULL || strlen(logmsg) == 0)
9370 error = get_editor(&editor);
9372 error = apply_unveil(got_repo_get_path(repo), 0,
9373 got_worktree_get_root_path(worktree));
9377 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
9381 if (prepared_logmsg == NULL) {
9382 error = lookup_logmsg_ref(&merged_logmsg,
9383 argc > 0 ? &paths : NULL, &refs, worktree, repo);
9388 cl_arg.editor = editor;
9389 cl_arg.cmdline_log = logmsg;
9390 cl_arg.prepared_log = prepared_logmsg;
9391 cl_arg.merged_log = merged_logmsg;
9392 cl_arg.non_interactive = non_interactive;
9393 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
9394 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
9395 if (!histedit_in_progress) {
9396 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
9397 error = got_error(GOT_ERR_COMMIT_BRANCH);
9400 cl_arg.branch_name += 11;
9402 cl_arg.repo_path = got_repo_get_path(repo);
9403 error = got_worktree_commit(&id, worktree, &paths, author, committer,
9404 allow_bad_symlinks, show_diff, commit_conflicts,
9405 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
9407 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
9408 cl_arg.logmsg_path != NULL)
9409 preserve_logmsg = 1;
9413 error = got_object_id_str(&id_str, id);
9416 printf("Created commit %s\n", id_str);
9418 TAILQ_FOREACH(re, &refs, entry) {
9419 error = got_ref_delete(re->ref, repo);
9425 if (preserve_logmsg) {
9426 fprintf(stderr, "%s: log message preserved in %s\n",
9427 getprogname(), cl_arg.logmsg_path);
9428 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
9430 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
9431 free(cl_arg.logmsg_path);
9432 if (merged_logmsg && unlink(merged_logmsg) == -1 && error == NULL)
9433 error = got_error_from_errno2("unlink", merged_logmsg);
9434 free(merged_logmsg);
9436 const struct got_error *close_err = got_repo_close(repo);
9441 got_worktree_close(worktree);
9443 const struct got_error *pack_err =
9444 got_repo_pack_fds_close(pack_fds);
9448 got_ref_list_free(&refs);
9449 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
9452 free(gitconfig_path);
9455 free(prepared_logmsg);
9462 fprintf(stderr, "usage: %s send [-afqTv] [-b branch] [-d branch] "
9463 "[-r repository-path] [-t tag] [remote-repository]\n",
9469 print_load_info(int print_colored, int print_found, int print_trees,
9470 int ncolored, int nfound, int ntrees)
9472 if (print_colored) {
9473 printf("%d commit%s colored", ncolored,
9474 ncolored == 1 ? "" : "s");
9477 printf("%s%d object%s found",
9478 ncolored > 0 ? "; " : "",
9479 nfound, nfound == 1 ? "" : "s");
9482 printf("; %d tree%s scanned", ntrees,
9483 ntrees == 1 ? "" : "s");
9487 struct got_send_progress_arg {
9488 char last_scaled_packsize[FMT_SCALED_STRSIZE];
9495 int last_nobj_total;
9499 int printed_something;
9501 struct got_pathlist_head *delete_branches;
9504 static const struct got_error *
9505 send_progress(void *arg, int ncolored, int nfound, int ntrees,
9506 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
9507 int nobj_written, off_t bytes_sent, const char *refname,
9508 const char *errmsg, int success)
9510 struct got_send_progress_arg *a = arg;
9511 char scaled_packsize[FMT_SCALED_STRSIZE];
9512 char scaled_sent[FMT_SCALED_STRSIZE];
9513 int p_deltify = 0, p_written = 0, p_sent = 0;
9514 int print_colored = 0, print_found = 0, print_trees = 0;
9515 int print_searching = 0, print_total = 0;
9516 int print_deltify = 0, print_written = 0, print_sent = 0;
9518 if (a->verbosity < 0)
9522 const char *status = success ? "accepted" : "rejected";
9525 struct got_pathlist_entry *pe;
9526 TAILQ_FOREACH(pe, a->delete_branches, entry) {
9527 const char *branchname = pe->path;
9528 if (got_path_cmp(branchname, refname,
9529 strlen(branchname), strlen(refname)) == 0) {
9531 a->sent_something = 1;
9537 if (a->printed_something)
9539 printf("Server has %s %s", status, refname);
9541 printf(": %s", errmsg);
9542 a->printed_something = 1;
9546 if (a->last_ncolored != ncolored) {
9548 a->last_ncolored = ncolored;
9551 if (a->last_nfound != nfound) {
9554 a->last_nfound = nfound;
9557 if (a->last_ntrees != ntrees) {
9561 a->last_ntrees = ntrees;
9564 if ((print_colored || print_found || print_trees) &&
9567 print_load_info(print_colored, print_found, print_trees,
9568 ncolored, nfound, ntrees);
9569 a->printed_something = 1;
9572 } else if (!a->loading_done) {
9574 print_load_info(1, 1, 1, ncolored, nfound, ntrees);
9576 a->loading_done = 1;
9579 if (fmt_scaled(packfile_size, scaled_packsize) == -1)
9580 return got_error_from_errno("fmt_scaled");
9581 if (fmt_scaled(bytes_sent, scaled_sent) == -1)
9582 return got_error_from_errno("fmt_scaled");
9584 if (a->last_ncommits != ncommits) {
9585 print_searching = 1;
9586 a->last_ncommits = ncommits;
9589 if (a->last_nobj_total != nobj_total) {
9590 print_searching = 1;
9592 a->last_nobj_total = nobj_total;
9595 if (packfile_size > 0 && (a->last_scaled_packsize[0] == '\0' ||
9596 strcmp(scaled_packsize, a->last_scaled_packsize)) != 0) {
9597 if (strlcpy(a->last_scaled_packsize, scaled_packsize,
9598 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
9599 return got_error(GOT_ERR_NO_SPACE);
9602 if (nobj_deltify > 0 || nobj_written > 0) {
9603 if (nobj_deltify > 0) {
9604 p_deltify = (nobj_deltify * 100) / nobj_total;
9605 if (p_deltify != a->last_p_deltify) {
9606 a->last_p_deltify = p_deltify;
9607 print_searching = 1;
9612 if (nobj_written > 0) {
9613 p_written = (nobj_written * 100) / nobj_total;
9614 if (p_written != a->last_p_written) {
9615 a->last_p_written = p_written;
9616 print_searching = 1;
9624 if (bytes_sent > 0) {
9625 p_sent = (bytes_sent * 100) / packfile_size;
9626 if (p_sent != a->last_p_sent) {
9627 a->last_p_sent = p_sent;
9628 print_searching = 1;
9634 a->sent_something = 1;
9637 if (print_searching || print_total || print_deltify || print_written ||
9640 if (print_searching)
9641 printf("packing %d reference%s", ncommits,
9642 ncommits == 1 ? "" : "s");
9644 printf("; %d object%s", nobj_total,
9645 nobj_total == 1 ? "" : "s");
9647 printf("; deltify: %d%%", p_deltify);
9649 printf("; uploading pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
9650 scaled_packsize, p_sent);
9651 else if (print_written)
9652 printf("; writing pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
9653 scaled_packsize, p_written);
9654 if (print_searching || print_total || print_deltify ||
9655 print_written || print_sent) {
9656 a->printed_something = 1;
9662 static const struct got_error *
9663 cmd_send(int argc, char *argv[])
9665 const struct got_error *error = NULL;
9666 char *cwd = NULL, *repo_path = NULL;
9667 const char *remote_name;
9668 char *proto = NULL, *host = NULL, *port = NULL;
9669 char *repo_name = NULL, *server_path = NULL;
9670 const struct got_remote_repo *remotes, *remote = NULL;
9671 int nremotes, nbranches = 0, ndelete_branches = 0;
9672 struct got_repository *repo = NULL;
9673 struct got_worktree *worktree = NULL;
9674 const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
9675 struct got_pathlist_head branches;
9676 struct got_pathlist_head tags;
9677 struct got_reflist_head all_branches;
9678 struct got_reflist_head all_tags;
9679 struct got_pathlist_head delete_args;
9680 struct got_pathlist_head delete_branches;
9681 struct got_reflist_entry *re;
9682 struct got_pathlist_entry *pe;
9683 int i, ch, sendfd = -1, sendstatus;
9685 struct got_send_progress_arg spa;
9686 int verbosity = 0, overwrite_refs = 0;
9687 int send_all_branches = 0, send_all_tags = 0;
9688 struct got_reference *ref = NULL;
9689 int *pack_fds = NULL;
9691 TAILQ_INIT(&branches);
9693 TAILQ_INIT(&all_branches);
9694 TAILQ_INIT(&all_tags);
9695 TAILQ_INIT(&delete_args);
9696 TAILQ_INIT(&delete_branches);
9698 while ((ch = getopt(argc, argv, "ab:d:fqr:Tt:v")) != -1) {
9701 send_all_branches = 1;
9704 error = got_pathlist_append(&branches, optarg, NULL);
9710 error = got_pathlist_append(&delete_args, optarg, NULL);
9721 repo_path = realpath(optarg, NULL);
9722 if (repo_path == NULL)
9723 return got_error_from_errno2("realpath",
9725 got_path_strip_trailing_slashes(repo_path);
9731 error = got_pathlist_append(&tags, optarg, NULL);
9738 else if (verbosity < 3)
9749 if (send_all_branches && !TAILQ_EMPTY(&branches))
9750 option_conflict('a', 'b');
9751 if (send_all_tags && !TAILQ_EMPTY(&tags))
9752 option_conflict('T', 't');
9756 remote_name = GOT_SEND_DEFAULT_REMOTE_NAME;
9758 remote_name = argv[0];
9762 cwd = getcwd(NULL, 0);
9764 error = got_error_from_errno("getcwd");
9768 error = got_repo_pack_fds_open(&pack_fds);
9772 if (repo_path == NULL) {
9773 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
9774 if (error && error->code != GOT_ERR_NOT_WORKTREE)
9780 strdup(got_worktree_get_repo_path(worktree));
9781 if (repo_path == NULL)
9782 error = got_error_from_errno("strdup");
9786 repo_path = strdup(cwd);
9787 if (repo_path == NULL) {
9788 error = got_error_from_errno("strdup");
9794 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
9799 worktree_conf = got_worktree_get_gotconfig(worktree);
9800 if (worktree_conf) {
9801 got_gotconfig_get_remotes(&nremotes, &remotes,
9803 for (i = 0; i < nremotes; i++) {
9804 if (strcmp(remotes[i].name, remote_name) == 0) {
9805 remote = &remotes[i];
9811 if (remote == NULL) {
9812 repo_conf = got_repo_get_gotconfig(repo);
9814 got_gotconfig_get_remotes(&nremotes, &remotes,
9816 for (i = 0; i < nremotes; i++) {
9817 if (strcmp(remotes[i].name, remote_name) == 0) {
9818 remote = &remotes[i];
9824 if (remote == NULL) {
9825 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
9826 for (i = 0; i < nremotes; i++) {
9827 if (strcmp(remotes[i].name, remote_name) == 0) {
9828 remote = &remotes[i];
9833 if (remote == NULL) {
9834 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
9838 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
9839 &repo_name, remote->send_url);
9843 if (strcmp(proto, "git") == 0) {
9845 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
9846 "sendfd dns inet unveil", NULL) == -1)
9849 } else if (strcmp(proto, "git+ssh") == 0 ||
9850 strcmp(proto, "ssh") == 0) {
9852 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
9853 "sendfd unveil", NULL) == -1)
9856 } else if (strcmp(proto, "http") == 0 ||
9857 strcmp(proto, "git+http") == 0) {
9858 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
9861 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
9865 error = got_dial_apply_unveil(proto);
9869 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
9873 if (send_all_branches) {
9874 error = got_ref_list(&all_branches, repo, "refs/heads",
9875 got_ref_cmp_by_name, NULL);
9878 TAILQ_FOREACH(re, &all_branches, entry) {
9879 const char *branchname = got_ref_get_name(re->ref);
9880 error = got_pathlist_append(&branches,
9886 } else if (nbranches == 0) {
9887 for (i = 0; i < remote->nsend_branches; i++) {
9888 error = got_pathlist_append(&branches,
9889 remote->send_branches[i], NULL);
9895 if (send_all_tags) {
9896 error = got_ref_list(&all_tags, repo, "refs/tags",
9897 got_ref_cmp_by_name, NULL);
9900 TAILQ_FOREACH(re, &all_tags, entry) {
9901 const char *tagname = got_ref_get_name(re->ref);
9902 error = got_pathlist_append(&tags,
9910 * To prevent accidents only branches in refs/heads/ can be deleted
9911 * with 'got send -d'.
9912 * Deleting anything else requires local repository access or Git.
9914 TAILQ_FOREACH(pe, &delete_args, entry) {
9915 const char *branchname = pe->path;
9917 struct got_pathlist_entry *new;
9918 if (strncmp(branchname, "refs/heads/", 11) == 0) {
9919 s = strdup(branchname);
9921 error = got_error_from_errno("strdup");
9925 if (asprintf(&s, "refs/heads/%s", branchname) == -1) {
9926 error = got_error_from_errno("asprintf");
9930 error = got_pathlist_insert(&new, &delete_branches, s, NULL);
9931 if (error || new == NULL /* duplicate */)
9938 if (nbranches == 0 && ndelete_branches == 0) {
9939 struct got_reference *head_ref;
9941 error = got_ref_open(&head_ref, repo,
9942 got_worktree_get_head_ref_name(worktree), 0);
9944 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
9947 if (got_ref_is_symbolic(head_ref)) {
9948 error = got_ref_resolve_symbolic(&ref, repo, head_ref);
9949 got_ref_close(head_ref);
9954 error = got_pathlist_append(&branches, got_ref_get_name(ref),
9961 if (verbosity >= 0) {
9962 printf("Connecting to \"%s\" %s://%s%s%s%s%s\n",
9963 remote->name, proto, host,
9964 port ? ":" : "", port ? port : "",
9965 *server_path == '/' ? "" : "/", server_path);
9968 error = got_send_connect(&sendpid, &sendfd, proto, host, port,
9969 server_path, verbosity);
9973 memset(&spa, 0, sizeof(spa));
9974 spa.last_scaled_packsize[0] = '\0';
9975 spa.last_p_deltify = -1;
9976 spa.last_p_written = -1;
9977 spa.verbosity = verbosity;
9978 spa.delete_branches = &delete_branches;
9979 error = got_send_pack(remote_name, &branches, &tags, &delete_branches,
9980 verbosity, overwrite_refs, sendfd, repo, send_progress, &spa,
9981 check_cancelled, NULL);
9982 if (spa.printed_something)
9986 if (!spa.sent_something && verbosity >= 0)
9987 printf("Already up-to-date\n");
9990 if (kill(sendpid, SIGTERM) == -1)
9991 error = got_error_from_errno("kill");
9992 if (waitpid(sendpid, &sendstatus, 0) == -1 && error == NULL)
9993 error = got_error_from_errno("waitpid");
9995 if (sendfd != -1 && close(sendfd) == -1 && error == NULL)
9996 error = got_error_from_errno("close");
9998 const struct got_error *close_err = got_repo_close(repo);
10003 got_worktree_close(worktree);
10005 const struct got_error *pack_err =
10006 got_repo_pack_fds_close(pack_fds);
10011 got_ref_close(ref);
10012 got_pathlist_free(&branches, GOT_PATHLIST_FREE_NONE);
10013 got_pathlist_free(&tags, GOT_PATHLIST_FREE_NONE);
10014 got_ref_list_free(&all_branches);
10015 got_ref_list_free(&all_tags);
10016 got_pathlist_free(&delete_args, GOT_PATHLIST_FREE_NONE);
10017 got_pathlist_free(&delete_branches, GOT_PATHLIST_FREE_PATH);
10029 * Print and if delete is set delete all ref_prefix references.
10030 * If wanted_ref is not NULL, only print or delete this reference.
10032 static const struct got_error *
10033 process_logmsg_refs(const char *ref_prefix, size_t prefix_len,
10034 const char *wanted_ref, int delete, struct got_worktree *worktree,
10035 struct got_repository *repo)
10037 const struct got_error *err;
10038 struct got_pathlist_head paths;
10039 struct got_reflist_head refs;
10040 struct got_reflist_entry *re;
10041 struct got_reflist_object_id_map *refs_idmap = NULL;
10042 struct got_commit_object *commit = NULL;
10043 struct got_object_id *id = NULL;
10044 const char *header_prefix;
10045 char *uuidstr = NULL;
10049 TAILQ_INIT(&paths);
10051 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, repo);
10055 err = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
10059 if (worktree != NULL) {
10060 err = got_worktree_get_uuid(&uuidstr, worktree);
10066 if (strncmp(wanted_ref, "refs/heads/", 11) == 0)
10070 if (strcmp(ref_prefix, GOT_WORKTREE_BACKOUT_REF_PREFIX) == 0)
10071 header_prefix = "backout";
10073 header_prefix = "cherrypick";
10075 TAILQ_FOREACH(re, &refs, entry) {
10076 const char *refname, *wt;
10078 refname = got_ref_get_name(re->ref);
10080 err = check_cancelled(NULL);
10084 if (strncmp(refname, ref_prefix, prefix_len) == 0)
10085 refname += prefix_len + 1; /* skip '-' delimiter */
10091 if (worktree == NULL || strncmp(refname, uuidstr,
10092 GOT_WORKTREE_UUID_STRLEN) == 0)
10093 refname += GOT_WORKTREE_UUID_STRLEN + 1; /* skip '-' */
10097 err = got_repo_match_object_id(&id, NULL, refname,
10098 GOT_OBJ_TYPE_COMMIT, NULL, repo);
10102 err = got_object_open_as_commit(&commit, repo, id);
10107 found = strncmp(wanted_ref, refname,
10108 strlen(wanted_ref)) == 0;
10109 if (wanted_ref && !found) {
10110 struct got_reflist_head *ci_refs;
10112 ci_refs = got_reflist_object_id_map_lookup(refs_idmap,
10116 char *refs_str = NULL;
10117 char const *r = NULL;
10119 err = build_refs_str(&refs_str, ci_refs, id,
10126 if (strncmp(r, wanted_ref,
10127 strlen(wanted_ref)) == 0) {
10131 r = strchr(r, ' ');
10139 if (wanted_ref == NULL || found) {
10141 err = got_ref_delete(re->ref, repo);
10144 printf("Deleted: ");
10145 err = print_commit_oneline(commit, id, repo,
10149 * Print paths modified by commit to help
10150 * associate commits with worktree changes.
10152 err = get_changed_paths(&paths, commit,
10157 err = print_commit(commit, id, repo, NULL,
10158 &paths, NULL, 0, 0, refs_idmap, NULL,
10160 got_pathlist_free(&paths,
10161 GOT_PATHLIST_FREE_ALL);
10163 if (worktree == NULL)
10164 printf("work tree: %.*s\n\n",
10165 GOT_WORKTREE_UUID_STRLEN, wt);
10171 got_object_commit_close(commit);
10177 if (wanted_ref != NULL && !found)
10178 err = got_error_fmt(GOT_ERR_NOT_REF, "%s", wanted_ref);
10183 got_ref_list_free(&refs);
10184 got_pathlist_free(&paths, GOT_PATHLIST_FREE_ALL);
10186 got_reflist_object_id_map_free(refs_idmap);
10188 got_object_commit_close(commit);
10193 * Create new temp "logmsg" ref of the backed-out or cherrypicked commit
10194 * identified by id for log messages to prepopulate the editor on commit.
10196 static const struct got_error *
10197 logmsg_ref(struct got_object_id *id, const char *prefix,
10198 struct got_worktree *worktree, struct got_repository *repo)
10200 const struct got_error *err = NULL;
10201 char *idstr, *ref = NULL, *refname = NULL;
10202 int histedit_in_progress;
10203 int rebase_in_progress, merge_in_progress;
10206 * Silenty refuse to create merge reference if any histedit, merge,
10207 * or rebase operation is in progress.
10209 err = got_worktree_histedit_in_progress(&histedit_in_progress,
10213 if (histedit_in_progress)
10216 err = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
10219 if (rebase_in_progress)
10222 err = got_worktree_merge_in_progress(&merge_in_progress, worktree,
10226 if (merge_in_progress)
10229 err = got_object_id_str(&idstr, id);
10233 err = got_worktree_get_logmsg_ref_name(&refname, worktree, prefix);
10237 if (asprintf(&ref, "%s-%s", refname, idstr) == -1) {
10238 err = got_error_from_errno("asprintf");
10242 err = create_ref(ref, got_worktree_get_base_commit_id(worktree),
10252 usage_cherrypick(void)
10254 fprintf(stderr, "usage: %s cherrypick [-lX] [commit-id]\n",
10259 static const struct got_error *
10260 cmd_cherrypick(int argc, char *argv[])
10262 const struct got_error *error = NULL;
10263 struct got_worktree *worktree = NULL;
10264 struct got_repository *repo = NULL;
10265 char *cwd = NULL, *commit_id_str = NULL, *keyword_idstr = NULL;
10266 struct got_object_id *commit_id = NULL;
10267 struct got_commit_object *commit = NULL;
10268 struct got_object_qid *pid;
10269 int ch, list_refs = 0, remove_refs = 0;
10270 struct got_update_progress_arg upa;
10271 int *pack_fds = NULL;
10274 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10275 "unveil", NULL) == -1)
10279 while ((ch = getopt(argc, argv, "lX")) != -1) {
10288 usage_cherrypick();
10296 if (list_refs || remove_refs) {
10297 if (argc != 0 && argc != 1)
10298 usage_cherrypick();
10299 } else if (argc != 1)
10300 usage_cherrypick();
10301 if (list_refs && remove_refs)
10302 option_conflict('l', 'X');
10304 cwd = getcwd(NULL, 0);
10306 error = got_error_from_errno("getcwd");
10310 error = got_repo_pack_fds_open(&pack_fds);
10314 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
10316 if (list_refs || remove_refs) {
10317 if (error->code != GOT_ERR_NOT_WORKTREE)
10320 if (error->code == GOT_ERR_NOT_WORKTREE)
10321 error = wrap_not_worktree_error(error,
10322 "cherrypick", cwd);
10327 error = got_repo_open(&repo,
10328 worktree ? got_worktree_get_repo_path(worktree) : cwd,
10333 error = apply_unveil(got_repo_get_path(repo), 0,
10334 worktree ? got_worktree_get_root_path(worktree) : NULL);
10338 if (list_refs || remove_refs) {
10339 error = process_logmsg_refs(GOT_WORKTREE_CHERRYPICK_REF_PREFIX,
10340 GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN,
10341 argc == 1 ? argv[0] : NULL, remove_refs, worktree, repo);
10345 error = got_keyword_to_idstr(&keyword_idstr, argv[0], repo, worktree);
10349 error = got_repo_match_object_id(&commit_id, NULL,
10350 keyword_idstr != NULL ? keyword_idstr : argv[0],
10351 GOT_OBJ_TYPE_COMMIT, NULL, repo);
10354 error = got_object_id_str(&commit_id_str, commit_id);
10358 error = got_object_open_as_commit(&commit, repo, commit_id);
10361 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
10362 memset(&upa, 0, sizeof(upa));
10363 error = got_worktree_merge_files(worktree, pid ? &pid->id : NULL,
10364 commit_id, repo, update_progress, &upa, check_cancelled,
10369 if (upa.did_something) {
10370 error = logmsg_ref(commit_id,
10371 GOT_WORKTREE_CHERRYPICK_REF_PREFIX, worktree, repo);
10374 printf("Merged commit %s\n", commit_id_str);
10376 print_merge_progress_stats(&upa);
10379 free(keyword_idstr);
10381 got_object_commit_close(commit);
10382 free(commit_id_str);
10384 got_worktree_close(worktree);
10386 const struct got_error *close_err = got_repo_close(repo);
10391 const struct got_error *pack_err =
10392 got_repo_pack_fds_close(pack_fds);
10401 usage_backout(void)
10403 fprintf(stderr, "usage: %s backout [-lX] [commit-id]\n", getprogname());
10407 static const struct got_error *
10408 cmd_backout(int argc, char *argv[])
10410 const struct got_error *error = NULL;
10411 struct got_worktree *worktree = NULL;
10412 struct got_repository *repo = NULL;
10413 char *cwd = NULL, *commit_id_str = NULL, *keyword_idstr = NULL;
10414 struct got_object_id *commit_id = NULL;
10415 struct got_commit_object *commit = NULL;
10416 struct got_object_qid *pid;
10417 int ch, list_refs = 0, remove_refs = 0;
10418 struct got_update_progress_arg upa;
10419 int *pack_fds = NULL;
10422 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10423 "unveil", NULL) == -1)
10427 while ((ch = getopt(argc, argv, "lX")) != -1) {
10444 if (list_refs || remove_refs) {
10445 if (argc != 0 && argc != 1)
10447 } else if (argc != 1)
10449 if (list_refs && remove_refs)
10450 option_conflict('l', 'X');
10452 cwd = getcwd(NULL, 0);
10454 error = got_error_from_errno("getcwd");
10458 error = got_repo_pack_fds_open(&pack_fds);
10462 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
10464 if (list_refs || remove_refs) {
10465 if (error->code != GOT_ERR_NOT_WORKTREE)
10468 if (error->code == GOT_ERR_NOT_WORKTREE)
10469 error = wrap_not_worktree_error(error,
10475 error = got_repo_open(&repo,
10476 worktree ? got_worktree_get_repo_path(worktree) : cwd,
10481 error = apply_unveil(got_repo_get_path(repo), 0,
10482 worktree ? got_worktree_get_root_path(worktree) : NULL);
10486 if (list_refs || remove_refs) {
10487 error = process_logmsg_refs(GOT_WORKTREE_BACKOUT_REF_PREFIX,
10488 GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN,
10489 argc == 1 ? argv[0] : NULL, remove_refs, worktree, repo);
10493 error = got_keyword_to_idstr(&keyword_idstr, argv[0], repo, worktree);
10497 error = got_repo_match_object_id(&commit_id, NULL,
10498 keyword_idstr != NULL ? keyword_idstr : argv[0],
10499 GOT_OBJ_TYPE_COMMIT, NULL, repo);
10502 error = got_object_id_str(&commit_id_str, commit_id);
10506 error = got_object_open_as_commit(&commit, repo, commit_id);
10509 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
10511 error = got_error(GOT_ERR_ROOT_COMMIT);
10515 memset(&upa, 0, sizeof(upa));
10516 error = got_worktree_merge_files(worktree, commit_id, &pid->id,
10517 repo, update_progress, &upa, check_cancelled, NULL);
10521 if (upa.did_something) {
10522 error = logmsg_ref(commit_id, GOT_WORKTREE_BACKOUT_REF_PREFIX,
10526 printf("Backed out commit %s\n", commit_id_str);
10528 print_merge_progress_stats(&upa);
10531 free(keyword_idstr);
10533 got_object_commit_close(commit);
10534 free(commit_id_str);
10536 got_worktree_close(worktree);
10538 const struct got_error *close_err = got_repo_close(repo);
10543 const struct got_error *pack_err =
10544 got_repo_pack_fds_close(pack_fds);
10554 fprintf(stderr, "usage: %s rebase [-aCclX] [branch]\n", getprogname());
10559 trim_logmsg(char *logmsg, int limit)
10564 len = strlen(logmsg);
10567 logmsg[len] = '\0';
10568 nl = strchr(logmsg, '\n');
10573 static const struct got_error *
10574 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
10576 const struct got_error *err;
10577 char *logmsg0 = NULL;
10580 err = got_object_commit_get_logmsg(&logmsg0, commit);
10585 while (isspace((unsigned char)s[0]))
10588 *logmsg = strdup(s);
10589 if (*logmsg == NULL) {
10590 err = got_error_from_errno("strdup");
10594 trim_logmsg(*logmsg, limit);
10600 static const struct got_error *
10601 show_rebase_merge_conflict(struct got_object_id *id,
10602 struct got_repository *repo)
10604 const struct got_error *err;
10605 struct got_commit_object *commit = NULL;
10606 char *id_str = NULL, *logmsg = NULL;
10608 err = got_object_open_as_commit(&commit, repo, id);
10612 err = got_object_id_str(&id_str, id);
10618 err = get_short_logmsg(&logmsg, 42, commit);
10622 printf("%s -> merge conflict: %s\n", id_str, logmsg);
10625 got_object_commit_close(commit);
10630 static const struct got_error *
10631 show_rebase_progress(struct got_commit_object *commit,
10632 struct got_object_id *old_id, struct got_object_id *new_id)
10634 const struct got_error *err;
10635 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
10637 err = got_object_id_str(&old_id_str, old_id);
10642 err = got_object_id_str(&new_id_str, new_id);
10647 old_id_str[12] = '\0';
10649 new_id_str[12] = '\0';
10651 err = get_short_logmsg(&logmsg, 42, commit);
10655 printf("%s -> %s: %s\n", old_id_str,
10656 new_id_str ? new_id_str : "no-op change", logmsg);
10664 static const struct got_error *
10665 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
10666 struct got_reference *branch, struct got_reference *tmp_branch,
10667 struct got_repository *repo, int create_backup)
10669 printf("Switching work tree to %s\n", got_ref_get_name(branch));
10670 return got_worktree_rebase_complete(worktree, fileindex,
10671 tmp_branch, branch, repo, create_backup);
10674 static const struct got_error *
10675 rebase_commit(struct got_pathlist_head *merged_paths,
10676 struct got_worktree *worktree, struct got_fileindex *fileindex,
10677 struct got_reference *tmp_branch, const char *committer,
10678 struct got_object_id *commit_id, int allow_conflict,
10679 struct got_repository *repo)
10681 const struct got_error *error;
10682 struct got_commit_object *commit;
10683 struct got_object_id *new_commit_id;
10685 error = got_object_open_as_commit(&commit, repo, commit_id);
10689 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
10690 worktree, fileindex, tmp_branch, committer, commit, commit_id,
10691 allow_conflict, repo);
10693 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
10695 error = show_rebase_progress(commit, commit_id, NULL);
10697 error = show_rebase_progress(commit, commit_id, new_commit_id);
10698 free(new_commit_id);
10701 got_object_commit_close(commit);
10705 struct check_path_prefix_arg {
10706 const char *path_prefix;
10711 static const struct got_error *
10712 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
10713 struct got_blob_object *blob2, FILE *f1, FILE *f2,
10714 struct got_object_id *id1, struct got_object_id *id2,
10715 const char *path1, const char *path2,
10716 mode_t mode1, mode_t mode2, struct got_repository *repo)
10718 struct check_path_prefix_arg *a = arg;
10720 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
10721 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
10722 return got_error(a->errcode);
10727 static const struct got_error *
10728 check_path_prefix(struct got_object_id *parent_id,
10729 struct got_object_id *commit_id, const char *path_prefix,
10730 int errcode, struct got_repository *repo)
10732 const struct got_error *err;
10733 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
10734 struct got_commit_object *commit = NULL, *parent_commit = NULL;
10735 struct check_path_prefix_arg cpp_arg;
10737 if (got_path_is_root_dir(path_prefix))
10740 err = got_object_open_as_commit(&commit, repo, commit_id);
10744 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
10748 err = got_object_open_as_tree(&tree1, repo,
10749 got_object_commit_get_tree_id(parent_commit));
10753 err = got_object_open_as_tree(&tree2, repo,
10754 got_object_commit_get_tree_id(commit));
10758 cpp_arg.path_prefix = path_prefix;
10759 while (cpp_arg.path_prefix[0] == '/')
10760 cpp_arg.path_prefix++;
10761 cpp_arg.len = strlen(cpp_arg.path_prefix);
10762 cpp_arg.errcode = errcode;
10763 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
10764 check_path_prefix_in_diff, &cpp_arg, 0);
10767 got_object_tree_close(tree1);
10769 got_object_tree_close(tree2);
10771 got_object_commit_close(commit);
10773 got_object_commit_close(parent_commit);
10777 static const struct got_error *
10778 collect_commits(struct got_object_id_queue *commits,
10779 struct got_object_id *initial_commit_id,
10780 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
10781 const char *path_prefix, int path_prefix_errcode,
10782 struct got_repository *repo)
10784 const struct got_error *err = NULL;
10785 struct got_commit_graph *graph = NULL;
10786 struct got_object_id parent_id, commit_id;
10787 struct got_object_qid *qid;
10789 err = got_commit_graph_open(&graph, "/", 1);
10793 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
10794 check_cancelled, NULL);
10798 memcpy(&commit_id, initial_commit_id, sizeof(commit_id));
10799 while (got_object_id_cmp(&commit_id, iter_stop_id) != 0) {
10800 err = got_commit_graph_iter_next(&parent_id, graph, repo,
10801 check_cancelled, NULL);
10803 if (err->code == GOT_ERR_ITER_COMPLETED) {
10804 err = got_error_msg(GOT_ERR_ANCESTRY,
10805 "ran out of commits to rebase before "
10806 "youngest common ancestor commit has "
10807 "been reached?!?");
10811 err = check_path_prefix(&parent_id, &commit_id,
10812 path_prefix, path_prefix_errcode, repo);
10816 err = got_object_qid_alloc(&qid, &commit_id);
10819 STAILQ_INSERT_HEAD(commits, qid, entry);
10821 memcpy(&commit_id, &parent_id, sizeof(commit_id));
10825 got_commit_graph_close(graph);
10829 static const struct got_error *
10830 get_commit_brief_str(char **brief_str, struct got_commit_object *commit)
10832 const struct got_error *err = NULL;
10833 time_t committer_time;
10835 char datebuf[11]; /* YYYY-MM-DD + NUL */
10836 char *author0 = NULL, *author, *smallerthan;
10837 char *logmsg0 = NULL, *logmsg, *newline;
10839 committer_time = got_object_commit_get_committer_time(commit);
10840 if (gmtime_r(&committer_time, &tm) == NULL)
10841 return got_error_from_errno("gmtime_r");
10842 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d", &tm) == 0)
10843 return got_error(GOT_ERR_NO_SPACE);
10845 author0 = strdup(got_object_commit_get_author(commit));
10846 if (author0 == NULL)
10847 return got_error_from_errno("strdup");
10849 smallerthan = strchr(author, '<');
10850 if (smallerthan && smallerthan[1] != '\0')
10851 author = smallerthan + 1;
10852 author[strcspn(author, "@>")] = '\0';
10854 err = got_object_commit_get_logmsg(&logmsg0, commit);
10858 while (*logmsg == '\n')
10860 newline = strchr(logmsg, '\n');
10864 if (asprintf(brief_str, "%s %s %s",
10865 datebuf, author, logmsg) == -1)
10866 err = got_error_from_errno("asprintf");
10873 static const struct got_error *
10874 delete_backup_ref(struct got_reference *ref, struct got_object_id *id,
10875 struct got_repository *repo)
10877 const struct got_error *err;
10880 err = got_object_id_str(&id_str, id);
10884 err = got_ref_delete(ref, repo);
10888 printf("Deleted %s: %s\n", got_ref_get_name(ref), id_str);
10894 static const struct got_error *
10895 print_backup_ref(const char *branch_name, const char *new_id_str,
10896 struct got_object_id *old_commit_id, struct got_commit_object *old_commit,
10897 struct got_reflist_object_id_map *refs_idmap,
10898 struct got_repository *repo)
10900 const struct got_error *err = NULL;
10901 struct got_reflist_head *refs;
10902 char *refs_str = NULL;
10903 struct got_object_id *new_commit_id = NULL;
10904 struct got_commit_object *new_commit = NULL;
10905 char *new_commit_brief_str = NULL;
10906 struct got_object_id *yca_id = NULL;
10907 struct got_commit_object *yca_commit = NULL;
10908 char *yca_id_str = NULL, *yca_brief_str = NULL;
10909 char *custom_refs_str;
10911 if (asprintf(&custom_refs_str, "formerly %s", branch_name) == -1)
10912 return got_error_from_errno("asprintf");
10914 err = print_commit(old_commit, old_commit_id, repo, NULL, NULL, NULL,
10915 0, 0, refs_idmap, custom_refs_str, NULL);
10919 err = got_object_resolve_id_str(&new_commit_id, repo, new_id_str);
10923 refs = got_reflist_object_id_map_lookup(refs_idmap, new_commit_id);
10925 err = build_refs_str(&refs_str, refs, new_commit_id, repo, 0);
10930 err = got_object_open_as_commit(&new_commit, repo, new_commit_id);
10934 err = get_commit_brief_str(&new_commit_brief_str, new_commit);
10938 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
10939 old_commit_id, new_commit_id, 1, repo, check_cancelled, NULL);
10943 printf("has become commit %s%s%s%s\n %s\n", new_id_str,
10944 refs_str ? " (" : "", refs_str ? refs_str : "",
10945 refs_str ? ")" : "", new_commit_brief_str);
10946 if (yca_id && got_object_id_cmp(yca_id, new_commit_id) != 0 &&
10947 got_object_id_cmp(yca_id, old_commit_id) != 0) {
10951 err = got_object_open_as_commit(&yca_commit, repo, yca_id);
10955 err = get_commit_brief_str(&yca_brief_str, yca_commit);
10959 err = got_object_id_str(&yca_id_str, yca_id);
10963 refs = got_reflist_object_id_map_lookup(refs_idmap, yca_id);
10965 err = build_refs_str(&refs_str, refs, yca_id, repo, 0);
10969 printf("history forked at %s%s%s%s\n %s\n",
10971 refs_str ? " (" : "", refs_str ? refs_str : "",
10972 refs_str ? ")" : "", yca_brief_str);
10975 free(custom_refs_str);
10976 free(new_commit_id);
10980 free(yca_brief_str);
10982 got_object_commit_close(new_commit);
10984 got_object_commit_close(yca_commit);
10989 static const struct got_error *
10990 worktree_has_logmsg_ref(const char *caller, struct got_worktree *worktree,
10991 struct got_repository *repo)
10993 const struct got_error *err;
10994 struct got_reflist_head refs;
10995 struct got_reflist_entry *re;
10996 char *uuidstr = NULL;
10997 static char msg[160];
11001 err = got_worktree_get_uuid(&uuidstr, worktree);
11005 err = got_ref_list(&refs, repo, "refs/got/worktree",
11006 got_ref_cmp_by_name, repo);
11010 TAILQ_FOREACH(re, &refs, entry) {
11011 const char *cmd, *refname, *type;
11013 refname = got_ref_get_name(re->ref);
11015 if (strncmp(refname, GOT_WORKTREE_CHERRYPICK_REF_PREFIX,
11016 GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN) == 0) {
11017 refname += GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN + 1;
11018 cmd = "cherrypick";
11019 type = "cherrypicked";
11020 } else if (strncmp(refname, GOT_WORKTREE_BACKOUT_REF_PREFIX,
11021 GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN) == 0) {
11022 refname += GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN + 1;
11024 type = "backed-out";
11028 if (strncmp(refname, uuidstr, GOT_WORKTREE_UUID_STRLEN) != 0)
11031 snprintf(msg, sizeof(msg),
11032 "work tree has references created by %s commits which "
11033 "must be removed with 'got %s -X' before running the %s "
11034 "command", type, cmd, caller);
11035 err = got_error_msg(GOT_ERR_WORKTREE_META, msg);
11041 got_ref_list_free(&refs);
11045 static const struct got_error *
11046 process_backup_refs(const char *backup_ref_prefix,
11047 const char *wanted_branch_name,
11048 int delete, struct got_repository *repo)
11050 const struct got_error *err;
11051 struct got_reflist_head refs, backup_refs;
11052 struct got_reflist_entry *re;
11053 const size_t backup_ref_prefix_len = strlen(backup_ref_prefix);
11054 struct got_object_id *old_commit_id = NULL;
11055 char *branch_name = NULL;
11056 struct got_commit_object *old_commit = NULL;
11057 struct got_reflist_object_id_map *refs_idmap = NULL;
11058 int wanted_branch_found = 0;
11061 TAILQ_INIT(&backup_refs);
11063 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
11067 err = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
11071 if (wanted_branch_name) {
11072 if (strncmp(wanted_branch_name, "refs/heads/", 11) == 0)
11073 wanted_branch_name += 11;
11076 err = got_ref_list(&backup_refs, repo, backup_ref_prefix,
11077 got_ref_cmp_by_commit_timestamp_descending, repo);
11081 TAILQ_FOREACH(re, &backup_refs, entry) {
11082 const char *refname = got_ref_get_name(re->ref);
11085 err = check_cancelled(NULL);
11089 err = got_ref_resolve(&old_commit_id, repo, re->ref);
11093 err = got_object_open_as_commit(&old_commit, repo,
11098 if (strncmp(backup_ref_prefix, refname,
11099 backup_ref_prefix_len) == 0)
11100 refname += backup_ref_prefix_len;
11102 while (refname[0] == '/')
11105 branch_name = strdup(refname);
11106 if (branch_name == NULL) {
11107 err = got_error_from_errno("strdup");
11110 slash = strrchr(branch_name, '/');
11113 refname += strlen(branch_name) + 1;
11116 if (wanted_branch_name == NULL ||
11117 strcmp(wanted_branch_name, branch_name) == 0) {
11118 wanted_branch_found = 1;
11120 err = delete_backup_ref(re->ref,
11121 old_commit_id, repo);
11123 err = print_backup_ref(branch_name, refname,
11124 old_commit_id, old_commit, refs_idmap,
11131 free(old_commit_id);
11132 old_commit_id = NULL;
11134 branch_name = NULL;
11135 got_object_commit_close(old_commit);
11139 if (wanted_branch_name && !wanted_branch_found) {
11140 err = got_error_fmt(GOT_ERR_NOT_REF,
11141 "%s/%s/", backup_ref_prefix, wanted_branch_name);
11145 got_reflist_object_id_map_free(refs_idmap);
11146 got_ref_list_free(&refs);
11147 got_ref_list_free(&backup_refs);
11148 free(old_commit_id);
11151 got_object_commit_close(old_commit);
11155 static const struct got_error *
11156 abort_progress(void *arg, unsigned char status, const char *path)
11159 * Unversioned files should not clutter progress output when
11160 * an operation is aborted.
11162 if (status == GOT_STATUS_UNVERSIONED)
11165 return update_progress(arg, status, path);
11168 static const struct got_error *
11169 cmd_rebase(int argc, char *argv[])
11171 const struct got_error *error = NULL;
11172 struct got_worktree *worktree = NULL;
11173 struct got_repository *repo = NULL;
11174 struct got_fileindex *fileindex = NULL;
11175 char *cwd = NULL, *committer = NULL, *gitconfig_path = NULL;
11176 struct got_reference *branch = NULL;
11177 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
11178 struct got_object_id *commit_id = NULL, *parent_id = NULL;
11179 struct got_object_id *resume_commit_id = NULL;
11180 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
11181 struct got_object_id *head_commit_id = NULL;
11182 struct got_reference *head_ref = NULL;
11183 struct got_commit_object *commit = NULL;
11184 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
11185 int histedit_in_progress = 0, merge_in_progress = 0;
11186 int create_backup = 1, list_backups = 0, delete_backups = 0;
11187 int allow_conflict = 0;
11188 struct got_object_id_queue commits;
11189 struct got_pathlist_head merged_paths;
11190 const struct got_object_id_queue *parent_ids;
11191 struct got_object_qid *qid, *pid;
11192 struct got_update_progress_arg upa;
11193 int *pack_fds = NULL;
11195 STAILQ_INIT(&commits);
11196 TAILQ_INIT(&merged_paths);
11197 memset(&upa, 0, sizeof(upa));
11200 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
11201 "unveil", NULL) == -1)
11205 while ((ch = getopt(argc, argv, "aCclX")) != -1) {
11211 allow_conflict = 1;
11214 continue_rebase = 1;
11220 delete_backups = 1;
11231 if (list_backups) {
11233 option_conflict('l', 'a');
11234 if (allow_conflict)
11235 option_conflict('l', 'C');
11236 if (continue_rebase)
11237 option_conflict('l', 'c');
11238 if (delete_backups)
11239 option_conflict('l', 'X');
11240 if (argc != 0 && argc != 1)
11242 } else if (delete_backups) {
11244 option_conflict('X', 'a');
11245 if (allow_conflict)
11246 option_conflict('X', 'C');
11247 if (continue_rebase)
11248 option_conflict('X', 'c');
11250 option_conflict('l', 'X');
11251 if (argc != 0 && argc != 1)
11253 } else if (allow_conflict) {
11255 option_conflict('C', 'a');
11256 if (!continue_rebase)
11257 errx(1, "-C option requires -c");
11259 if (abort_rebase && continue_rebase)
11261 else if (abort_rebase || continue_rebase) {
11264 } else if (argc != 1)
11268 cwd = getcwd(NULL, 0);
11270 error = got_error_from_errno("getcwd");
11274 error = got_repo_pack_fds_open(&pack_fds);
11278 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
11280 if (list_backups || delete_backups) {
11281 if (error->code != GOT_ERR_NOT_WORKTREE)
11284 if (error->code == GOT_ERR_NOT_WORKTREE)
11285 error = wrap_not_worktree_error(error,
11291 error = get_gitconfig_path(&gitconfig_path);
11294 error = got_repo_open(&repo,
11295 worktree ? got_worktree_get_repo_path(worktree) : cwd,
11296 gitconfig_path, pack_fds);
11300 if (worktree != NULL && !list_backups && !delete_backups) {
11301 error = worktree_has_logmsg_ref("rebase", worktree, repo);
11306 error = get_author(&committer, repo, worktree);
11307 if (error && error->code != GOT_ERR_COMMIT_NO_AUTHOR)
11310 error = apply_unveil(got_repo_get_path(repo), 0,
11311 worktree ? got_worktree_get_root_path(worktree) : NULL);
11315 if (list_backups || delete_backups) {
11316 error = process_backup_refs(
11317 GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
11318 argc == 1 ? argv[0] : NULL, delete_backups, repo);
11319 goto done; /* nothing else to do */
11322 error = got_worktree_histedit_in_progress(&histedit_in_progress,
11326 if (histedit_in_progress) {
11327 error = got_error(GOT_ERR_HISTEDIT_BUSY);
11331 error = got_worktree_merge_in_progress(&merge_in_progress,
11335 if (merge_in_progress) {
11336 error = got_error(GOT_ERR_MERGE_BUSY);
11340 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
11344 if (abort_rebase) {
11345 if (!rebase_in_progress) {
11346 error = got_error(GOT_ERR_NOT_REBASING);
11349 error = got_worktree_rebase_continue(&resume_commit_id,
11350 &new_base_branch, &tmp_branch, &branch, &fileindex,
11354 printf("Switching work tree to %s\n",
11355 got_ref_get_symref_target(new_base_branch));
11356 error = got_worktree_rebase_abort(worktree, fileindex, repo,
11357 new_base_branch, abort_progress, &upa);
11360 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
11361 print_merge_progress_stats(&upa);
11362 goto done; /* nothing else to do */
11365 if (continue_rebase) {
11366 if (!rebase_in_progress) {
11367 error = got_error(GOT_ERR_NOT_REBASING);
11370 error = got_worktree_rebase_continue(&resume_commit_id,
11371 &new_base_branch, &tmp_branch, &branch, &fileindex,
11376 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
11377 committer, resume_commit_id, allow_conflict, repo);
11381 yca_id = got_object_id_dup(resume_commit_id);
11382 if (yca_id == NULL) {
11383 error = got_error_from_errno("got_object_id_dup");
11387 error = got_ref_open(&branch, repo, argv[0], 0);
11390 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
11391 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
11392 "will not rebase a branch which lives outside "
11393 "the \"refs/heads/\" reference namespace");
11398 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
11402 if (!continue_rebase) {
11403 struct got_object_id *base_commit_id;
11405 error = got_ref_open(&head_ref, repo,
11406 got_worktree_get_head_ref_name(worktree), 0);
11409 error = got_ref_resolve(&head_commit_id, repo, head_ref);
11412 base_commit_id = got_worktree_get_base_commit_id(worktree);
11413 if (got_object_id_cmp(base_commit_id, head_commit_id) != 0) {
11414 error = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
11418 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
11419 base_commit_id, branch_head_commit_id, 1, repo,
11420 check_cancelled, NULL);
11422 if (error->code == GOT_ERR_ANCESTRY) {
11423 error = got_error_msg(GOT_ERR_ANCESTRY,
11424 "specified branch shares no common "
11425 "ancestry with work tree's branch");
11430 if (got_object_id_cmp(base_commit_id, yca_id) == 0) {
11431 struct got_pathlist_head paths;
11432 printf("%s is already based on %s\n",
11433 got_ref_get_name(branch),
11434 got_worktree_get_head_ref_name(worktree));
11435 error = switch_head_ref(branch, branch_head_commit_id,
11439 error = got_worktree_set_base_commit_id(worktree, repo,
11440 branch_head_commit_id);
11443 TAILQ_INIT(&paths);
11444 error = got_pathlist_append(&paths, "", NULL);
11447 error = got_worktree_checkout_files(worktree,
11448 &paths, repo, update_progress, &upa,
11449 check_cancelled, NULL);
11450 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
11453 if (upa.did_something) {
11455 error = got_object_id_str(&id_str,
11456 branch_head_commit_id);
11459 printf("Updated to %s: %s\n",
11460 got_worktree_get_head_ref_name(worktree),
11464 printf("Already up-to-date\n");
11465 print_update_progress_stats(&upa);
11470 commit_id = branch_head_commit_id;
11471 error = got_object_open_as_commit(&commit, repo, commit_id);
11475 parent_ids = got_object_commit_get_parent_ids(commit);
11476 pid = STAILQ_FIRST(parent_ids);
11478 error = collect_commits(&commits, commit_id, &pid->id,
11479 yca_id, got_worktree_get_path_prefix(worktree),
11480 GOT_ERR_REBASE_PATH, repo);
11485 got_object_commit_close(commit);
11488 if (!continue_rebase) {
11489 error = got_worktree_rebase_prepare(&new_base_branch,
11490 &tmp_branch, &fileindex, worktree, branch, repo);
11495 if (STAILQ_EMPTY(&commits)) {
11496 if (continue_rebase) {
11497 error = rebase_complete(worktree, fileindex,
11498 branch, tmp_branch, repo, create_backup);
11501 /* Fast-forward the reference of the branch. */
11502 struct got_object_id *new_head_commit_id;
11504 error = got_ref_resolve(&new_head_commit_id, repo,
11508 error = got_object_id_str(&id_str, new_head_commit_id);
11511 printf("Forwarding %s to commit %s\n",
11512 got_ref_get_name(branch), id_str);
11514 error = got_ref_change_ref(branch,
11515 new_head_commit_id);
11518 /* No backup needed since objects did not change. */
11524 STAILQ_FOREACH(qid, &commits, entry) {
11526 commit_id = &qid->id;
11527 parent_id = pid ? &pid->id : yca_id;
11530 memset(&upa, 0, sizeof(upa));
11531 error = got_worktree_rebase_merge_files(&merged_paths,
11532 worktree, fileindex, parent_id, commit_id, repo,
11533 update_progress, &upa, check_cancelled, NULL);
11537 print_merge_progress_stats(&upa);
11538 if (upa.conflicts > 0 || upa.missing > 0 ||
11539 upa.not_deleted > 0 || upa.unversioned > 0) {
11540 if (upa.conflicts > 0) {
11541 error = show_rebase_merge_conflict(&qid->id,
11546 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
11550 error = rebase_commit(&merged_paths, worktree, fileindex,
11551 tmp_branch, committer, commit_id, 0, repo);
11552 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
11557 if (upa.conflicts > 0 || upa.missing > 0 ||
11558 upa.not_deleted > 0 || upa.unversioned > 0) {
11559 error = got_worktree_rebase_postpone(worktree, fileindex);
11562 if (upa.conflicts > 0 && upa.missing == 0 &&
11563 upa.not_deleted == 0 && upa.unversioned == 0) {
11564 error = got_error_msg(GOT_ERR_CONFLICTS,
11565 "conflicts must be resolved before rebasing "
11567 } else if (upa.conflicts > 0) {
11568 error = got_error_msg(GOT_ERR_CONFLICTS,
11569 "conflicts must be resolved before rebasing "
11570 "can continue; changes destined for some "
11571 "files were not yet merged and should be "
11572 "merged manually if required before the "
11573 "rebase operation is continued");
11575 error = got_error_msg(GOT_ERR_CONFLICTS,
11576 "changes destined for some files were not "
11577 "yet merged and should be merged manually "
11578 "if required before the rebase operation "
11582 error = rebase_complete(worktree, fileindex, branch,
11583 tmp_branch, repo, create_backup);
11587 free(gitconfig_path);
11588 got_object_id_queue_free(&commits);
11589 free(branch_head_commit_id);
11590 free(resume_commit_id);
11591 free(head_commit_id);
11594 got_object_commit_close(commit);
11596 got_ref_close(branch);
11597 if (new_base_branch)
11598 got_ref_close(new_base_branch);
11600 got_ref_close(tmp_branch);
11602 got_ref_close(head_ref);
11604 got_worktree_close(worktree);
11606 const struct got_error *close_err = got_repo_close(repo);
11611 const struct got_error *pack_err =
11612 got_repo_pack_fds_close(pack_fds);
11620 usage_histedit(void)
11622 fprintf(stderr, "usage: %s histedit [-aCcdeflmX] [-F histedit-script] "
11623 "[branch]\n", getprogname());
11627 #define GOT_HISTEDIT_PICK 'p'
11628 #define GOT_HISTEDIT_EDIT 'e'
11629 #define GOT_HISTEDIT_FOLD 'f'
11630 #define GOT_HISTEDIT_DROP 'd'
11631 #define GOT_HISTEDIT_MESG 'm'
11633 static const struct got_histedit_cmd {
11634 unsigned char code;
11637 } got_histedit_cmds[] = {
11638 { GOT_HISTEDIT_PICK, "pick", "use commit" },
11639 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
11640 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
11642 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
11643 { GOT_HISTEDIT_MESG, "mesg",
11644 "single-line log message for commit above (open editor if empty)" },
11647 struct got_histedit_list_entry {
11648 TAILQ_ENTRY(got_histedit_list_entry) entry;
11649 struct got_object_id *commit_id;
11650 const struct got_histedit_cmd *cmd;
11653 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
11655 static const struct got_error *
11656 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
11657 FILE *f, struct got_repository *repo)
11659 const struct got_error *err = NULL;
11660 char *logmsg = NULL, *id_str = NULL;
11661 struct got_commit_object *commit = NULL;
11664 err = got_object_open_as_commit(&commit, repo, commit_id);
11668 err = get_short_logmsg(&logmsg, 34, commit);
11672 err = got_object_id_str(&id_str, commit_id);
11676 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
11678 err = got_ferror(f, GOT_ERR_IO);
11681 got_object_commit_close(commit);
11687 static const struct got_error *
11688 histedit_write_commit_list(struct got_object_id_queue *commits,
11689 FILE *f, int edit_logmsg_only, int fold_only, int drop_only,
11690 int edit_only, struct got_repository *repo)
11692 const struct got_error *err = NULL;
11693 struct got_object_qid *qid;
11694 const char *histedit_cmd = NULL;
11696 if (STAILQ_EMPTY(commits))
11697 return got_error(GOT_ERR_EMPTY_HISTEDIT);
11699 STAILQ_FOREACH(qid, commits, entry) {
11700 histedit_cmd = got_histedit_cmds[0].name;
11702 histedit_cmd = "drop";
11703 else if (edit_only)
11704 histedit_cmd = "edit";
11705 else if (fold_only && STAILQ_NEXT(qid, entry) != NULL)
11706 histedit_cmd = "fold";
11707 err = histedit_write_commit(&qid->id, histedit_cmd, f, repo);
11710 if (edit_logmsg_only) {
11711 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
11713 err = got_ferror(f, GOT_ERR_IO);
11722 static const struct got_error *
11723 write_cmd_list(FILE *f, const char *branch_name,
11724 struct got_object_id_queue *commits)
11726 const struct got_error *err = NULL;
11730 struct got_object_qid *qid;
11732 qid = STAILQ_FIRST(commits);
11733 err = got_object_id_str(&id_str, &qid->id);
11738 "# Editing the history of branch '%s' starting at\n"
11740 "# Commits will be processed in order from top to "
11741 "bottom of this file.\n", branch_name, id_str);
11743 err = got_ferror(f, GOT_ERR_IO);
11747 n = fprintf(f, "# Available histedit commands:\n");
11749 err = got_ferror(f, GOT_ERR_IO);
11753 for (i = 0; i < nitems(got_histedit_cmds); i++) {
11754 const struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
11755 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
11758 err = got_ferror(f, GOT_ERR_IO);
11767 static const struct got_error *
11768 histedit_syntax_error(int lineno)
11770 static char msg[42];
11773 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
11775 if (ret < 0 || (size_t)ret >= sizeof(msg))
11776 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
11778 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
11781 static const struct got_error *
11782 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
11783 char *logmsg, struct got_repository *repo)
11785 const struct got_error *err;
11786 struct got_commit_object *folded_commit = NULL;
11787 char *id_str, *folded_logmsg = NULL;
11789 err = got_object_id_str(&id_str, hle->commit_id);
11793 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
11797 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
11800 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
11801 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
11802 folded_logmsg) == -1) {
11803 err = got_error_from_errno("asprintf");
11807 got_object_commit_close(folded_commit);
11809 free(folded_logmsg);
11813 static struct got_histedit_list_entry *
11814 get_folded_commits(struct got_histedit_list_entry *hle)
11816 struct got_histedit_list_entry *prev, *folded = NULL;
11818 prev = TAILQ_PREV(hle, got_histedit_list, entry);
11819 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
11820 prev->cmd->code == GOT_HISTEDIT_DROP)) {
11821 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
11823 prev = TAILQ_PREV(prev, got_histedit_list, entry);
11829 static const struct got_error *
11830 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
11831 struct got_repository *repo)
11833 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
11834 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
11835 const struct got_error *err = NULL;
11836 struct got_commit_object *commit = NULL;
11839 struct got_histedit_list_entry *folded = NULL;
11841 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
11845 folded = get_folded_commits(hle);
11847 while (folded != hle) {
11848 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
11849 folded = TAILQ_NEXT(folded, entry);
11852 err = append_folded_commit_msg(&new_msg, folded,
11858 folded = TAILQ_NEXT(folded, entry);
11862 err = got_object_id_str(&id_str, hle->commit_id);
11865 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
11868 logmsg_len = asprintf(&new_msg,
11869 "%s\n# original log message of commit %s: %s",
11870 logmsg ? logmsg : "", id_str, orig_logmsg);
11871 if (logmsg_len == -1) {
11872 err = got_error_from_errno("asprintf");
11878 err = got_object_id_str(&id_str, hle->commit_id);
11882 err = got_opentemp_named_fd(&logmsg_path, &fd,
11883 GOT_TMPDIR_STR "/got-logmsg", "");
11887 if (write(fd, logmsg, logmsg_len) == -1) {
11888 err = got_error_from_errno2("write", logmsg_path);
11891 if (close(fd) == -1) {
11892 err = got_error_from_errno2("close", logmsg_path);
11897 err = get_editor(&editor);
11901 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg,
11904 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
11907 hle->logmsg = strdup(new_msg);
11908 if (hle->logmsg == NULL)
11909 err = got_error_from_errno("strdup");
11912 if (fd != -1 && close(fd) == -1 && err == NULL)
11913 err = got_error_from_errno2("close", logmsg_path);
11914 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
11915 err = got_error_from_errno2("unlink", logmsg_path);
11921 got_object_commit_close(commit);
11925 static const struct got_error *
11926 histedit_parse_list(struct got_histedit_list *histedit_cmds,
11927 FILE *f, struct got_repository *repo)
11929 const struct got_error *err = NULL;
11930 char *line = NULL, *p, *end;
11931 size_t i, linesize = 0;
11933 int lineno = 0, lastcmd = -1;
11934 const struct got_histedit_cmd *cmd;
11935 struct got_object_id *commit_id = NULL;
11936 struct got_histedit_list_entry *hle = NULL;
11939 linelen = getline(&line, &linesize, f);
11940 if (linelen == -1) {
11941 const struct got_error *getline_err;
11944 getline_err = got_error_from_errno("getline");
11945 err = got_ferror(f, getline_err->code);
11950 while (isspace((unsigned char)p[0]))
11952 if (p[0] == '#' || p[0] == '\0')
11955 for (i = 0; i < nitems(got_histedit_cmds); i++) {
11956 cmd = &got_histedit_cmds[i];
11957 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
11958 isspace((unsigned char)p[strlen(cmd->name)])) {
11959 p += strlen(cmd->name);
11962 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
11967 if (i == nitems(got_histedit_cmds)) {
11968 err = histedit_syntax_error(lineno);
11971 while (isspace((unsigned char)p[0]))
11973 if (cmd->code == GOT_HISTEDIT_MESG) {
11974 if (lastcmd != GOT_HISTEDIT_PICK &&
11975 lastcmd != GOT_HISTEDIT_EDIT) {
11976 err = got_error(GOT_ERR_HISTEDIT_CMD);
11979 if (p[0] == '\0') {
11980 err = histedit_edit_logmsg(hle, repo);
11984 hle->logmsg = strdup(p);
11985 if (hle->logmsg == NULL) {
11986 err = got_error_from_errno("strdup");
11990 lastcmd = cmd->code;
11994 while (end[0] && !isspace((unsigned char)end[0]))
11998 err = got_object_resolve_id_str(&commit_id, repo, p);
12000 /* override error code */
12001 err = histedit_syntax_error(lineno);
12005 hle = malloc(sizeof(*hle));
12007 err = got_error_from_errno("malloc");
12011 hle->commit_id = commit_id;
12012 hle->logmsg = NULL;
12014 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
12015 lastcmd = cmd->code;
12023 static const struct got_error *
12024 histedit_check_script(struct got_histedit_list *histedit_cmds,
12025 struct got_object_id_queue *commits, struct got_repository *repo)
12027 const struct got_error *err = NULL;
12028 struct got_object_qid *qid;
12029 struct got_histedit_list_entry *hle;
12030 static char msg[92];
12033 if (TAILQ_EMPTY(histedit_cmds))
12034 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
12035 "histedit script contains no commands");
12036 if (STAILQ_EMPTY(commits))
12037 return got_error(GOT_ERR_EMPTY_HISTEDIT);
12039 TAILQ_FOREACH(hle, histedit_cmds, entry) {
12040 struct got_histedit_list_entry *hle2;
12041 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
12044 if (got_object_id_cmp(hle->commit_id,
12045 hle2->commit_id) != 0)
12047 err = got_object_id_str(&id_str, hle->commit_id);
12050 snprintf(msg, sizeof(msg), "commit %s is listed "
12051 "more than once in histedit script", id_str);
12053 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
12057 STAILQ_FOREACH(qid, commits, entry) {
12058 TAILQ_FOREACH(hle, histedit_cmds, entry) {
12059 if (got_object_id_cmp(&qid->id, hle->commit_id) == 0)
12063 err = got_object_id_str(&id_str, &qid->id);
12066 snprintf(msg, sizeof(msg),
12067 "commit %s missing from histedit script", id_str);
12069 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
12073 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
12074 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
12075 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
12076 "last commit in histedit script cannot be folded");
12081 static const struct got_error *
12082 histedit_run_editor(struct got_histedit_list *histedit_cmds,
12083 const char *path, struct got_object_id_queue *commits,
12084 struct got_repository *repo)
12086 const struct got_error *err = NULL;
12087 struct stat st, st2;
12088 struct timespec timeout;
12092 err = get_editor(&editor);
12096 if (stat(path, &st) == -1) {
12097 err = got_error_from_errno2("stat", path);
12101 if (spawn_editor(editor, path) == -1) {
12102 err = got_error_from_errno("failed spawning editor");
12106 timeout.tv_sec = 0;
12107 timeout.tv_nsec = 1;
12108 nanosleep(&timeout, NULL);
12110 if (stat(path, &st2) == -1) {
12111 err = got_error_from_errno2("stat", path);
12115 if (st.st_size == st2.st_size &&
12116 timespeccmp(&st.st_mtim, &st2.st_mtim, ==)) {
12117 err = got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
12118 "no changes made to histedit script, aborting");
12122 f = fopen(path, "re");
12124 err = got_error_from_errno("fopen");
12127 err = histedit_parse_list(histedit_cmds, f, repo);
12131 err = histedit_check_script(histedit_cmds, commits, repo);
12133 if (f && fclose(f) == EOF && err == NULL)
12134 err = got_error_from_errno("fclose");
12139 static const struct got_error *
12140 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
12141 struct got_object_id_queue *, const char *, const char *,
12142 struct got_repository *);
12144 static const struct got_error *
12145 histedit_edit_script(struct got_histedit_list *histedit_cmds,
12146 struct got_object_id_queue *commits, const char *branch_name,
12147 int edit_logmsg_only, int fold_only, int drop_only, int edit_only,
12148 struct got_repository *repo)
12150 const struct got_error *err;
12154 err = got_opentemp_named(&path, &f, "got-histedit", "");
12158 err = write_cmd_list(f, branch_name, commits);
12162 err = histedit_write_commit_list(commits, f, edit_logmsg_only,
12163 fold_only, drop_only, edit_only, repo);
12167 if (drop_only || edit_logmsg_only || fold_only || edit_only) {
12169 err = histedit_parse_list(histedit_cmds, f, repo);
12171 if (fclose(f) == EOF) {
12172 err = got_error_from_errno("fclose");
12176 err = histedit_run_editor(histedit_cmds, path, commits, repo);
12178 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
12179 err->code != GOT_ERR_HISTEDIT_CMD)
12181 err = histedit_edit_list_retry(histedit_cmds, err,
12182 commits, path, branch_name, repo);
12186 if (f && fclose(f) == EOF && err == NULL)
12187 err = got_error_from_errno("fclose");
12188 if (path && unlink(path) != 0 && err == NULL)
12189 err = got_error_from_errno2("unlink", path);
12194 static const struct got_error *
12195 histedit_save_list(struct got_histedit_list *histedit_cmds,
12196 struct got_worktree *worktree, struct got_repository *repo)
12198 const struct got_error *err = NULL;
12201 struct got_histedit_list_entry *hle;
12202 struct got_commit_object *commit = NULL;
12204 err = got_worktree_get_histedit_script_path(&path, worktree);
12208 f = fopen(path, "we");
12210 err = got_error_from_errno2("fopen", path);
12213 TAILQ_FOREACH(hle, histedit_cmds, entry) {
12214 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
12220 int n = fprintf(f, "%c %s\n",
12221 GOT_HISTEDIT_MESG, hle->logmsg);
12223 err = got_ferror(f, GOT_ERR_IO);
12229 if (f && fclose(f) == EOF && err == NULL)
12230 err = got_error_from_errno("fclose");
12233 got_object_commit_close(commit);
12238 histedit_free_list(struct got_histedit_list *histedit_cmds)
12240 struct got_histedit_list_entry *hle;
12242 while ((hle = TAILQ_FIRST(histedit_cmds))) {
12243 TAILQ_REMOVE(histedit_cmds, hle, entry);
12248 static const struct got_error *
12249 histedit_load_list(struct got_histedit_list *histedit_cmds,
12250 const char *path, struct got_repository *repo)
12252 const struct got_error *err = NULL;
12255 f = fopen(path, "re");
12257 err = got_error_from_errno2("fopen", path);
12261 err = histedit_parse_list(histedit_cmds, f, repo);
12263 if (f && fclose(f) == EOF && err == NULL)
12264 err = got_error_from_errno("fclose");
12268 static const struct got_error *
12269 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
12270 const struct got_error *edit_err, struct got_object_id_queue *commits,
12271 const char *path, const char *branch_name, struct got_repository *repo)
12273 const struct got_error *err = NULL, *prev_err = edit_err;
12276 while (resp != 'c' && resp != 'r' && resp != 'a') {
12277 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
12278 "or (a)bort: ", getprogname(), prev_err->msg);
12283 histedit_free_list(histedit_cmds);
12284 err = histedit_run_editor(histedit_cmds, path, commits,
12287 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
12288 err->code != GOT_ERR_HISTEDIT_CMD)
12295 } else if (resp == 'r') {
12296 histedit_free_list(histedit_cmds);
12297 err = histedit_edit_script(histedit_cmds,
12298 commits, branch_name, 0, 0, 0, 0, repo);
12300 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
12301 err->code != GOT_ERR_HISTEDIT_CMD)
12308 } else if (resp == 'a') {
12309 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
12312 printf("invalid response '%c'\n", resp);
12318 static const struct got_error *
12319 histedit_complete(struct got_worktree *worktree,
12320 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
12321 struct got_reference *branch, struct got_repository *repo)
12323 printf("Switching work tree to %s\n",
12324 got_ref_get_symref_target(branch));
12325 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
12329 static const struct got_error *
12330 show_histedit_progress(struct got_commit_object *commit,
12331 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
12333 const struct got_error *err;
12334 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
12336 err = got_object_id_str(&old_id_str, hle->commit_id);
12341 err = got_object_id_str(&new_id_str, new_id);
12346 old_id_str[12] = '\0';
12348 new_id_str[12] = '\0';
12351 logmsg = strdup(hle->logmsg);
12352 if (logmsg == NULL) {
12353 err = got_error_from_errno("strdup");
12356 trim_logmsg(logmsg, 42);
12358 err = get_short_logmsg(&logmsg, 42, commit);
12363 switch (hle->cmd->code) {
12364 case GOT_HISTEDIT_PICK:
12365 case GOT_HISTEDIT_EDIT:
12366 printf("%s -> %s: %s\n", old_id_str,
12367 new_id_str ? new_id_str : "no-op change", logmsg);
12369 case GOT_HISTEDIT_DROP:
12370 case GOT_HISTEDIT_FOLD:
12371 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
12383 static const struct got_error *
12384 histedit_commit(struct got_pathlist_head *merged_paths,
12385 struct got_worktree *worktree, struct got_fileindex *fileindex,
12386 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
12387 const char *committer, int allow_conflict, struct got_repository *repo)
12389 const struct got_error *err;
12390 struct got_commit_object *commit;
12391 struct got_object_id *new_commit_id;
12393 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
12394 && hle->logmsg == NULL) {
12395 err = histedit_edit_logmsg(hle, repo);
12400 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
12404 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
12405 worktree, fileindex, tmp_branch, committer, commit, hle->commit_id,
12406 hle->logmsg, allow_conflict, repo);
12408 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
12410 err = show_histedit_progress(commit, hle, NULL);
12412 err = show_histedit_progress(commit, hle, new_commit_id);
12413 free(new_commit_id);
12416 got_object_commit_close(commit);
12420 static const struct got_error *
12421 histedit_skip_commit(struct got_histedit_list_entry *hle,
12422 struct got_worktree *worktree, struct got_repository *repo)
12424 const struct got_error *error;
12425 struct got_commit_object *commit;
12427 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
12432 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
12436 error = show_histedit_progress(commit, hle, NULL);
12437 got_object_commit_close(commit);
12441 static const struct got_error *
12442 check_local_changes(void *arg, unsigned char status,
12443 unsigned char staged_status, const char *path,
12444 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
12445 struct got_object_id *commit_id, int dirfd, const char *de_name)
12447 int *have_local_changes = arg;
12450 case GOT_STATUS_ADD:
12451 case GOT_STATUS_DELETE:
12452 case GOT_STATUS_MODIFY:
12453 case GOT_STATUS_CONFLICT:
12454 *have_local_changes = 1;
12455 return got_error(GOT_ERR_CANCELLED);
12460 switch (staged_status) {
12461 case GOT_STATUS_ADD:
12462 case GOT_STATUS_DELETE:
12463 case GOT_STATUS_MODIFY:
12464 *have_local_changes = 1;
12465 return got_error(GOT_ERR_CANCELLED);
12473 static const struct got_error *
12474 cmd_histedit(int argc, char *argv[])
12476 const struct got_error *error = NULL;
12477 struct got_worktree *worktree = NULL;
12478 struct got_fileindex *fileindex = NULL;
12479 struct got_repository *repo = NULL;
12480 char *cwd = NULL, *committer = NULL, *gitconfig_path = NULL;
12481 struct got_reference *branch = NULL;
12482 struct got_reference *tmp_branch = NULL;
12483 struct got_object_id *resume_commit_id = NULL;
12484 struct got_object_id *base_commit_id = NULL;
12485 struct got_object_id *head_commit_id = NULL;
12486 struct got_commit_object *commit = NULL;
12487 int ch, rebase_in_progress = 0, merge_in_progress = 0;
12488 struct got_update_progress_arg upa;
12489 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
12490 int drop_only = 0, edit_logmsg_only = 0, fold_only = 0, edit_only = 0;
12491 int allow_conflict = 0, list_backups = 0, delete_backups = 0;
12492 const char *edit_script_path = NULL;
12493 struct got_object_id_queue commits;
12494 struct got_pathlist_head merged_paths;
12495 const struct got_object_id_queue *parent_ids;
12496 struct got_object_qid *pid;
12497 struct got_histedit_list histedit_cmds;
12498 struct got_histedit_list_entry *hle;
12499 int *pack_fds = NULL;
12501 STAILQ_INIT(&commits);
12502 TAILQ_INIT(&histedit_cmds);
12503 TAILQ_INIT(&merged_paths);
12504 memset(&upa, 0, sizeof(upa));
12507 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
12508 "unveil", NULL) == -1)
12512 while ((ch = getopt(argc, argv, "aCcdeF:flmX")) != -1) {
12518 allow_conflict = 1;
12530 edit_script_path = optarg;
12539 edit_logmsg_only = 1;
12542 delete_backups = 1;
12553 if (abort_edit && allow_conflict)
12554 option_conflict('a', 'C');
12555 if (abort_edit && continue_edit)
12556 option_conflict('a', 'c');
12557 if (edit_script_path && allow_conflict)
12558 option_conflict('F', 'C');
12559 if (edit_script_path && edit_logmsg_only)
12560 option_conflict('F', 'm');
12561 if (abort_edit && edit_logmsg_only)
12562 option_conflict('a', 'm');
12563 if (edit_logmsg_only && allow_conflict)
12564 option_conflict('m', 'C');
12565 if (continue_edit && edit_logmsg_only)
12566 option_conflict('c', 'm');
12567 if (abort_edit && fold_only)
12568 option_conflict('a', 'f');
12569 if (fold_only && allow_conflict)
12570 option_conflict('f', 'C');
12571 if (continue_edit && fold_only)
12572 option_conflict('c', 'f');
12573 if (fold_only && edit_logmsg_only)
12574 option_conflict('f', 'm');
12575 if (edit_script_path && fold_only)
12576 option_conflict('F', 'f');
12577 if (abort_edit && edit_only)
12578 option_conflict('a', 'e');
12579 if (continue_edit && edit_only)
12580 option_conflict('c', 'e');
12581 if (edit_only && edit_logmsg_only)
12582 option_conflict('e', 'm');
12583 if (edit_script_path && edit_only)
12584 option_conflict('F', 'e');
12585 if (fold_only && edit_only)
12586 option_conflict('f', 'e');
12587 if (drop_only && abort_edit)
12588 option_conflict('d', 'a');
12589 if (drop_only && allow_conflict)
12590 option_conflict('d', 'C');
12591 if (drop_only && continue_edit)
12592 option_conflict('d', 'c');
12593 if (drop_only && edit_logmsg_only)
12594 option_conflict('d', 'm');
12595 if (drop_only && edit_only)
12596 option_conflict('d', 'e');
12597 if (drop_only && edit_script_path)
12598 option_conflict('d', 'F');
12599 if (drop_only && fold_only)
12600 option_conflict('d', 'f');
12601 if (list_backups) {
12603 option_conflict('l', 'a');
12604 if (allow_conflict)
12605 option_conflict('l', 'C');
12607 option_conflict('l', 'c');
12608 if (edit_script_path)
12609 option_conflict('l', 'F');
12610 if (edit_logmsg_only)
12611 option_conflict('l', 'm');
12613 option_conflict('l', 'd');
12615 option_conflict('l', 'f');
12617 option_conflict('l', 'e');
12618 if (delete_backups)
12619 option_conflict('l', 'X');
12620 if (argc != 0 && argc != 1)
12622 } else if (delete_backups) {
12624 option_conflict('X', 'a');
12625 if (allow_conflict)
12626 option_conflict('X', 'C');
12628 option_conflict('X', 'c');
12630 option_conflict('X', 'd');
12631 if (edit_script_path)
12632 option_conflict('X', 'F');
12633 if (edit_logmsg_only)
12634 option_conflict('X', 'm');
12636 option_conflict('X', 'f');
12638 option_conflict('X', 'e');
12640 option_conflict('X', 'l');
12641 if (argc != 0 && argc != 1)
12643 } else if (allow_conflict && !continue_edit)
12644 errx(1, "-C option requires -c");
12645 else if (argc != 0)
12649 * This command cannot apply unveil(2) in all cases because the
12650 * user may choose to run an editor to edit the histedit script
12651 * and to edit individual commit log messages.
12652 * unveil(2) traverses exec(2); if an editor is used we have to
12653 * apply unveil after edit script and log messages have been written.
12654 * XXX TODO: Make use of unveil(2) where possible.
12657 cwd = getcwd(NULL, 0);
12659 error = got_error_from_errno("getcwd");
12663 error = got_repo_pack_fds_open(&pack_fds);
12667 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
12669 if (list_backups || delete_backups) {
12670 if (error->code != GOT_ERR_NOT_WORKTREE)
12673 if (error->code == GOT_ERR_NOT_WORKTREE)
12674 error = wrap_not_worktree_error(error,
12680 if (list_backups || delete_backups) {
12681 error = got_repo_open(&repo,
12682 worktree ? got_worktree_get_repo_path(worktree) : cwd,
12686 error = apply_unveil(got_repo_get_path(repo), 0,
12687 worktree ? got_worktree_get_root_path(worktree) : NULL);
12690 error = process_backup_refs(
12691 GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
12692 argc == 1 ? argv[0] : NULL, delete_backups, repo);
12693 goto done; /* nothing else to do */
12696 error = get_gitconfig_path(&gitconfig_path);
12699 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
12700 gitconfig_path, pack_fds);
12704 if (worktree != NULL && !list_backups && !delete_backups) {
12705 error = worktree_has_logmsg_ref("histedit", worktree, repo);
12710 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
12713 if (rebase_in_progress) {
12714 error = got_error(GOT_ERR_REBASING);
12718 error = got_worktree_merge_in_progress(&merge_in_progress, worktree,
12722 if (merge_in_progress) {
12723 error = got_error(GOT_ERR_MERGE_BUSY);
12727 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
12731 if (edit_in_progress && edit_logmsg_only) {
12732 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
12733 "histedit operation is in progress in this "
12734 "work tree and must be continued or aborted "
12735 "before the -m option can be used");
12738 if (edit_in_progress && drop_only) {
12739 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
12740 "histedit operation is in progress in this "
12741 "work tree and must be continued or aborted "
12742 "before the -d option can be used");
12745 if (edit_in_progress && fold_only) {
12746 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
12747 "histedit operation is in progress in this "
12748 "work tree and must be continued or aborted "
12749 "before the -f option can be used");
12752 if (edit_in_progress && edit_only) {
12753 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
12754 "histedit operation is in progress in this "
12755 "work tree and must be continued or aborted "
12756 "before the -e option can be used");
12760 if (edit_in_progress && abort_edit) {
12761 error = got_worktree_histedit_continue(&resume_commit_id,
12762 &tmp_branch, &branch, &base_commit_id, &fileindex,
12766 printf("Switching work tree to %s\n",
12767 got_ref_get_symref_target(branch));
12768 error = got_worktree_histedit_abort(worktree, fileindex, repo,
12769 branch, base_commit_id, abort_progress, &upa);
12772 printf("Histedit of %s aborted\n",
12773 got_ref_get_symref_target(branch));
12774 print_merge_progress_stats(&upa);
12775 goto done; /* nothing else to do */
12776 } else if (abort_edit) {
12777 error = got_error(GOT_ERR_NOT_HISTEDIT);
12781 error = get_author(&committer, repo, worktree);
12785 if (continue_edit) {
12788 if (!edit_in_progress) {
12789 error = got_error(GOT_ERR_NOT_HISTEDIT);
12793 error = got_worktree_get_histedit_script_path(&path, worktree);
12797 error = histedit_load_list(&histedit_cmds, path, repo);
12802 error = got_worktree_histedit_continue(&resume_commit_id,
12803 &tmp_branch, &branch, &base_commit_id, &fileindex,
12808 error = got_ref_resolve(&head_commit_id, repo, branch);
12812 error = got_object_open_as_commit(&commit, repo,
12816 parent_ids = got_object_commit_get_parent_ids(commit);
12817 pid = STAILQ_FIRST(parent_ids);
12819 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
12822 error = collect_commits(&commits, head_commit_id, &pid->id,
12823 base_commit_id, got_worktree_get_path_prefix(worktree),
12824 GOT_ERR_HISTEDIT_PATH, repo);
12825 got_object_commit_close(commit);
12830 if (edit_in_progress) {
12831 error = got_error(GOT_ERR_HISTEDIT_BUSY);
12835 error = got_ref_open(&branch, repo,
12836 got_worktree_get_head_ref_name(worktree), 0);
12840 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
12841 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
12842 "will not edit commit history of a branch outside "
12843 "the \"refs/heads/\" reference namespace");
12847 error = got_ref_resolve(&head_commit_id, repo, branch);
12848 got_ref_close(branch);
12853 error = got_object_open_as_commit(&commit, repo,
12857 parent_ids = got_object_commit_get_parent_ids(commit);
12858 pid = STAILQ_FIRST(parent_ids);
12860 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
12863 error = collect_commits(&commits, head_commit_id, &pid->id,
12864 got_worktree_get_base_commit_id(worktree),
12865 got_worktree_get_path_prefix(worktree),
12866 GOT_ERR_HISTEDIT_PATH, repo);
12867 got_object_commit_close(commit);
12872 if (STAILQ_EMPTY(&commits)) {
12873 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
12877 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
12878 &base_commit_id, &fileindex, worktree, repo);
12882 if (edit_script_path) {
12883 error = histedit_load_list(&histedit_cmds,
12884 edit_script_path, repo);
12886 got_worktree_histedit_abort(worktree, fileindex,
12887 repo, branch, base_commit_id,
12888 abort_progress, &upa);
12889 print_merge_progress_stats(&upa);
12893 const char *branch_name;
12894 branch_name = got_ref_get_symref_target(branch);
12895 if (strncmp(branch_name, "refs/heads/", 11) == 0)
12897 error = histedit_edit_script(&histedit_cmds, &commits,
12898 branch_name, edit_logmsg_only, fold_only,
12899 drop_only, edit_only, repo);
12901 got_worktree_histedit_abort(worktree, fileindex,
12902 repo, branch, base_commit_id,
12903 abort_progress, &upa);
12904 print_merge_progress_stats(&upa);
12910 error = histedit_save_list(&histedit_cmds, worktree,
12913 got_worktree_histedit_abort(worktree, fileindex,
12914 repo, branch, base_commit_id,
12915 abort_progress, &upa);
12916 print_merge_progress_stats(&upa);
12922 error = histedit_check_script(&histedit_cmds, &commits, repo);
12926 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
12927 if (resume_commit_id) {
12928 if (got_object_id_cmp(hle->commit_id,
12929 resume_commit_id) != 0)
12932 resume_commit_id = NULL;
12933 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
12934 hle->cmd->code == GOT_HISTEDIT_FOLD) {
12935 error = histedit_skip_commit(hle, worktree,
12940 struct got_pathlist_head paths;
12941 int have_changes = 0;
12943 TAILQ_INIT(&paths);
12944 error = got_pathlist_append(&paths, "", NULL);
12947 error = got_worktree_status(worktree, &paths,
12948 repo, 0, check_local_changes, &have_changes,
12949 check_cancelled, NULL);
12950 got_pathlist_free(&paths,
12951 GOT_PATHLIST_FREE_NONE);
12953 if (error->code != GOT_ERR_CANCELLED)
12955 if (sigint_received || sigpipe_received)
12958 if (have_changes) {
12959 error = histedit_commit(NULL, worktree,
12960 fileindex, tmp_branch, hle,
12961 committer, allow_conflict, repo);
12965 error = got_object_open_as_commit(
12966 &commit, repo, hle->commit_id);
12969 error = show_histedit_progress(commit,
12971 got_object_commit_close(commit);
12980 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
12981 error = histedit_skip_commit(hle, worktree, repo);
12987 error = got_object_open_as_commit(&commit, repo,
12991 parent_ids = got_object_commit_get_parent_ids(commit);
12992 pid = STAILQ_FIRST(parent_ids);
12994 error = got_worktree_histedit_merge_files(&merged_paths,
12995 worktree, fileindex, &pid->id, hle->commit_id, repo,
12996 update_progress, &upa, check_cancelled, NULL);
12999 got_object_commit_close(commit);
13002 print_merge_progress_stats(&upa);
13003 if (upa.conflicts > 0 || upa.missing > 0 ||
13004 upa.not_deleted > 0 || upa.unversioned > 0) {
13005 if (upa.conflicts > 0) {
13006 error = show_rebase_merge_conflict(
13007 hle->commit_id, repo);
13011 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
13015 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
13017 error = got_object_id_str(&id_str, hle->commit_id);
13020 printf("Stopping histedit for amending commit %s\n",
13023 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
13024 error = got_worktree_histedit_postpone(worktree,
13029 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
13030 error = histedit_skip_commit(hle, worktree, repo);
13036 error = histedit_commit(&merged_paths, worktree, fileindex,
13037 tmp_branch, hle, committer, allow_conflict, repo);
13038 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
13043 if (upa.conflicts > 0 || upa.missing > 0 ||
13044 upa.not_deleted > 0 || upa.unversioned > 0) {
13045 error = got_worktree_histedit_postpone(worktree, fileindex);
13048 if (upa.conflicts > 0 && upa.missing == 0 &&
13049 upa.not_deleted == 0 && upa.unversioned == 0) {
13050 error = got_error_msg(GOT_ERR_CONFLICTS,
13051 "conflicts must be resolved before histedit "
13053 } else if (upa.conflicts > 0) {
13054 error = got_error_msg(GOT_ERR_CONFLICTS,
13055 "conflicts must be resolved before histedit "
13056 "can continue; changes destined for some "
13057 "files were not yet merged and should be "
13058 "merged manually if required before the "
13059 "histedit operation is continued");
13061 error = got_error_msg(GOT_ERR_CONFLICTS,
13062 "changes destined for some files were not "
13063 "yet merged and should be merged manually "
13064 "if required before the histedit operation "
13068 error = histedit_complete(worktree, fileindex, tmp_branch,
13073 free(gitconfig_path);
13074 got_object_id_queue_free(&commits);
13075 histedit_free_list(&histedit_cmds);
13076 free(head_commit_id);
13077 free(base_commit_id);
13078 free(resume_commit_id);
13080 got_object_commit_close(commit);
13082 got_ref_close(branch);
13084 got_ref_close(tmp_branch);
13086 got_worktree_close(worktree);
13088 const struct got_error *close_err = got_repo_close(repo);
13093 const struct got_error *pack_err =
13094 got_repo_pack_fds_close(pack_fds);
13102 usage_integrate(void)
13104 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
13108 static const struct got_error *
13109 cmd_integrate(int argc, char *argv[])
13111 const struct got_error *error = NULL;
13112 struct got_repository *repo = NULL;
13113 struct got_worktree *worktree = NULL;
13114 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
13115 const char *branch_arg = NULL;
13116 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
13117 struct got_fileindex *fileindex = NULL;
13118 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
13120 struct got_update_progress_arg upa;
13121 int *pack_fds = NULL;
13124 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
13125 "unveil", NULL) == -1)
13129 while ((ch = getopt(argc, argv, "")) != -1) {
13142 branch_arg = argv[0];
13144 cwd = getcwd(NULL, 0);
13146 error = got_error_from_errno("getcwd");
13150 error = got_repo_pack_fds_open(&pack_fds);
13154 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
13156 if (error->code == GOT_ERR_NOT_WORKTREE)
13157 error = wrap_not_worktree_error(error, "integrate",
13162 error = check_rebase_or_histedit_in_progress(worktree);
13166 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
13171 error = apply_unveil(got_repo_get_path(repo), 0,
13172 got_worktree_get_root_path(worktree));
13176 error = check_merge_in_progress(worktree, repo);
13180 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
13181 error = got_error_from_errno("asprintf");
13185 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
13186 &base_branch_ref, worktree, refname, repo);
13190 refname = strdup(got_ref_get_name(branch_ref));
13191 if (refname == NULL) {
13192 error = got_error_from_errno("strdup");
13193 got_worktree_integrate_abort(worktree, fileindex, repo,
13194 branch_ref, base_branch_ref);
13197 base_refname = strdup(got_ref_get_name(base_branch_ref));
13198 if (base_refname == NULL) {
13199 error = got_error_from_errno("strdup");
13200 got_worktree_integrate_abort(worktree, fileindex, repo,
13201 branch_ref, base_branch_ref);
13204 if (strncmp(base_refname, "refs/heads/", 11) != 0) {
13205 error = got_error(GOT_ERR_INTEGRATE_BRANCH);
13206 got_worktree_integrate_abort(worktree, fileindex, repo,
13207 branch_ref, base_branch_ref);
13211 error = got_ref_resolve(&commit_id, repo, branch_ref);
13215 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
13219 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
13220 error = got_error_msg(GOT_ERR_SAME_BRANCH,
13221 "specified branch has already been integrated");
13222 got_worktree_integrate_abort(worktree, fileindex, repo,
13223 branch_ref, base_branch_ref);
13227 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
13229 if (error->code == GOT_ERR_ANCESTRY)
13230 error = got_error(GOT_ERR_REBASE_REQUIRED);
13231 got_worktree_integrate_abort(worktree, fileindex, repo,
13232 branch_ref, base_branch_ref);
13236 memset(&upa, 0, sizeof(upa));
13237 error = got_worktree_integrate_continue(worktree, fileindex, repo,
13238 branch_ref, base_branch_ref, update_progress, &upa,
13239 check_cancelled, NULL);
13243 printf("Integrated %s into %s\n", refname, base_refname);
13244 print_update_progress_stats(&upa);
13247 const struct got_error *close_err = got_repo_close(repo);
13252 got_worktree_close(worktree);
13254 const struct got_error *pack_err =
13255 got_repo_pack_fds_close(pack_fds);
13260 free(base_commit_id);
13263 free(base_refname);
13270 fprintf(stderr, "usage: %s merge [-aCcn] [branch]\n", getprogname());
13274 static const struct got_error *
13275 cmd_merge(int argc, char *argv[])
13277 const struct got_error *error = NULL;
13278 struct got_worktree *worktree = NULL;
13279 struct got_repository *repo = NULL;
13280 struct got_fileindex *fileindex = NULL;
13281 char *cwd = NULL, *id_str = NULL, *author = NULL;
13282 char *gitconfig_path = NULL;
13283 struct got_reference *branch = NULL, *wt_branch = NULL;
13284 struct got_object_id *branch_tip = NULL, *yca_id = NULL;
13285 struct got_object_id *wt_branch_tip = NULL;
13286 int ch, merge_in_progress = 0, abort_merge = 0, continue_merge = 0;
13287 int allow_conflict = 0, prefer_fast_forward = 1, interrupt_merge = 0;
13288 struct got_update_progress_arg upa;
13289 struct got_object_id *merge_commit_id = NULL;
13290 char *branch_name = NULL;
13291 int *pack_fds = NULL;
13293 memset(&upa, 0, sizeof(upa));
13296 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
13297 "unveil", NULL) == -1)
13301 while ((ch = getopt(argc, argv, "aCcMn")) != -1) {
13307 allow_conflict = 1;
13310 continue_merge = 1;
13313 prefer_fast_forward = 0;
13316 interrupt_merge = 1;
13328 if (continue_merge)
13329 option_conflict('a', 'c');
13330 if (!prefer_fast_forward)
13331 option_conflict('a', 'M');
13332 if (interrupt_merge)
13333 option_conflict('a', 'n');
13334 } else if (continue_merge) {
13335 if (!prefer_fast_forward)
13336 option_conflict('c', 'M');
13337 if (interrupt_merge)
13338 option_conflict('c', 'n');
13340 if (allow_conflict) {
13341 if (!continue_merge)
13342 errx(1, "-C option requires -c");
13344 if (abort_merge || continue_merge) {
13347 } else if (argc != 1)
13350 cwd = getcwd(NULL, 0);
13352 error = got_error_from_errno("getcwd");
13356 error = got_repo_pack_fds_open(&pack_fds);
13360 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
13362 if (error->code == GOT_ERR_NOT_WORKTREE)
13363 error = wrap_not_worktree_error(error,
13368 error = get_gitconfig_path(&gitconfig_path);
13371 error = got_repo_open(&repo,
13372 worktree ? got_worktree_get_repo_path(worktree) : cwd,
13373 gitconfig_path, pack_fds);
13377 if (worktree != NULL) {
13378 error = worktree_has_logmsg_ref("merge", worktree, repo);
13383 error = apply_unveil(got_repo_get_path(repo), 0,
13384 worktree ? got_worktree_get_root_path(worktree) : NULL);
13388 error = check_rebase_or_histedit_in_progress(worktree);
13392 error = got_worktree_merge_in_progress(&merge_in_progress, worktree,
13397 if (merge_in_progress && !(abort_merge || continue_merge)) {
13398 error = got_error(GOT_ERR_MERGE_BUSY);
13402 if (!merge_in_progress && (abort_merge || continue_merge)) {
13403 error = got_error(GOT_ERR_NOT_MERGING);
13408 error = got_worktree_merge_continue(&branch_name,
13409 &branch_tip, &fileindex, worktree, repo);
13412 error = got_worktree_merge_abort(worktree, fileindex, repo,
13413 abort_progress, &upa);
13416 printf("Merge of %s aborted\n", branch_name);
13417 goto done; /* nothing else to do */
13420 if (strncmp(got_worktree_get_head_ref_name(worktree),
13421 "refs/heads/", 11) != 0) {
13422 error = got_error_fmt(GOT_ERR_COMMIT_BRANCH,
13423 "work tree's current branch %s is outside the "
13424 "\"refs/heads/\" reference namespace; "
13425 "update -b required",
13426 got_worktree_get_head_ref_name(worktree));
13430 error = get_author(&author, repo, worktree);
13434 error = got_ref_open(&wt_branch, repo,
13435 got_worktree_get_head_ref_name(worktree), 0);
13438 error = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
13442 if (continue_merge) {
13443 struct got_object_id *base_commit_id;
13444 base_commit_id = got_worktree_get_base_commit_id(worktree);
13445 if (got_object_id_cmp(wt_branch_tip, base_commit_id) != 0) {
13446 error = got_error(GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
13449 error = got_worktree_merge_continue(&branch_name,
13450 &branch_tip, &fileindex, worktree, repo);
13454 error = got_ref_open(&branch, repo, argv[0], 0);
13457 branch_name = strdup(got_ref_get_name(branch));
13458 if (branch_name == NULL) {
13459 error = got_error_from_errno("strdup");
13462 error = got_ref_resolve(&branch_tip, repo, branch);
13467 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
13468 wt_branch_tip, branch_tip, 0, repo,
13469 check_cancelled, NULL);
13470 if (error && error->code != GOT_ERR_ANCESTRY)
13473 if (!continue_merge) {
13474 error = check_path_prefix(wt_branch_tip, branch_tip,
13475 got_worktree_get_path_prefix(worktree),
13476 GOT_ERR_MERGE_PATH, repo);
13479 error = got_worktree_merge_prepare(&fileindex, worktree, repo);
13482 if (prefer_fast_forward && yca_id &&
13483 got_object_id_cmp(wt_branch_tip, yca_id) == 0) {
13484 struct got_pathlist_head paths;
13485 if (interrupt_merge) {
13486 error = got_error_fmt(GOT_ERR_BAD_OPTION,
13487 "there are no changes to merge since %s "
13488 "is already based on %s; merge cannot be "
13489 "interrupted for amending; -n",
13490 branch_name, got_ref_get_name(wt_branch));
13493 printf("Forwarding %s to %s\n",
13494 got_ref_get_name(wt_branch), branch_name);
13495 error = got_ref_change_ref(wt_branch, branch_tip);
13498 error = got_ref_write(wt_branch, repo);
13501 error = got_worktree_set_base_commit_id(worktree, repo,
13505 TAILQ_INIT(&paths);
13506 error = got_pathlist_append(&paths, "", NULL);
13509 error = got_worktree_checkout_files(worktree,
13510 &paths, repo, update_progress, &upa,
13511 check_cancelled, NULL);
13512 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
13515 if (upa.did_something) {
13517 error = got_object_id_str(&id_str, branch_tip);
13520 printf("Updated to commit %s\n", id_str);
13523 printf("Already up-to-date\n");
13524 print_update_progress_stats(&upa);
13527 error = got_worktree_merge_write_refs(worktree, branch, repo);
13531 error = got_worktree_merge_branch(worktree, fileindex,
13532 yca_id, branch_tip, repo, update_progress, &upa,
13533 check_cancelled, NULL);
13536 print_merge_progress_stats(&upa);
13537 if (!upa.did_something) {
13538 error = got_worktree_merge_abort(worktree, fileindex,
13539 repo, abort_progress, &upa);
13542 printf("Already up-to-date\n");
13547 if (interrupt_merge) {
13548 error = got_worktree_merge_postpone(worktree, fileindex);
13551 printf("Merge of %s interrupted on request\n", branch_name);
13552 } else if (upa.conflicts > 0 || upa.missing > 0 ||
13553 upa.not_deleted > 0 || upa.unversioned > 0) {
13554 error = got_worktree_merge_postpone(worktree, fileindex);
13557 if (upa.conflicts > 0 && upa.missing == 0 &&
13558 upa.not_deleted == 0 && upa.unversioned == 0) {
13559 error = got_error_msg(GOT_ERR_CONFLICTS,
13560 "conflicts must be resolved before merging "
13562 } else if (upa.conflicts > 0) {
13563 error = got_error_msg(GOT_ERR_CONFLICTS,
13564 "conflicts must be resolved before merging "
13565 "can continue; changes destined for some "
13566 "files were not yet merged and "
13567 "should be merged manually if required before the "
13568 "merge operation is continued");
13570 error = got_error_msg(GOT_ERR_CONFLICTS,
13571 "changes destined for some "
13572 "files were not yet merged and should be "
13573 "merged manually if required before the "
13574 "merge operation is continued");
13578 error = got_worktree_merge_commit(&merge_commit_id, worktree,
13579 fileindex, author, NULL, 1, branch_tip, branch_name,
13580 allow_conflict, repo, continue_merge ? print_status : NULL,
13584 error = got_worktree_merge_complete(worktree, fileindex, repo);
13587 error = got_object_id_str(&id_str, merge_commit_id);
13590 printf("Merged %s into %s: %s\n", branch_name,
13591 got_worktree_get_head_ref_name(worktree),
13596 free(gitconfig_path);
13598 free(merge_commit_id);
13604 got_ref_close(branch);
13606 got_ref_close(wt_branch);
13608 got_worktree_close(worktree);
13610 const struct got_error *close_err = got_repo_close(repo);
13615 const struct got_error *pack_err =
13616 got_repo_pack_fds_close(pack_fds);
13626 fprintf(stderr, "usage: %s stage [-lpS] [-F response-script] "
13627 "[path ...]\n", getprogname());
13631 static const struct got_error *
13632 print_stage(void *arg, unsigned char status, unsigned char staged_status,
13633 const char *path, struct got_object_id *blob_id,
13634 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
13635 int dirfd, const char *de_name)
13637 const struct got_error *err = NULL;
13638 char *id_str = NULL;
13640 if (staged_status != GOT_STATUS_ADD &&
13641 staged_status != GOT_STATUS_MODIFY &&
13642 staged_status != GOT_STATUS_DELETE)
13645 if (staged_status == GOT_STATUS_ADD ||
13646 staged_status == GOT_STATUS_MODIFY)
13647 err = got_object_id_str(&id_str, staged_blob_id);
13649 err = got_object_id_str(&id_str, blob_id);
13653 printf("%s %c %s\n", id_str, staged_status, path);
13658 static const struct got_error *
13659 cmd_stage(int argc, char *argv[])
13661 const struct got_error *error = NULL;
13662 struct got_repository *repo = NULL;
13663 struct got_worktree *worktree = NULL;
13665 struct got_pathlist_head paths;
13666 int ch, list_stage = 0, pflag = 0, allow_bad_symlinks = 0;
13667 FILE *patch_script_file = NULL;
13668 const char *patch_script_path = NULL;
13669 struct choose_patch_arg cpa;
13670 int *pack_fds = NULL;
13672 TAILQ_INIT(&paths);
13675 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
13676 "unveil", NULL) == -1)
13680 while ((ch = getopt(argc, argv, "F:lpS")) != -1) {
13683 patch_script_path = optarg;
13692 allow_bad_symlinks = 1;
13703 if (list_stage && (pflag || patch_script_path))
13704 errx(1, "-l option cannot be used with other options");
13705 if (patch_script_path && !pflag)
13706 errx(1, "-F option can only be used together with -p option");
13708 cwd = getcwd(NULL, 0);
13710 error = got_error_from_errno("getcwd");
13714 error = got_repo_pack_fds_open(&pack_fds);
13718 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
13720 if (error->code == GOT_ERR_NOT_WORKTREE)
13721 error = wrap_not_worktree_error(error, "stage", cwd);
13725 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
13730 if (patch_script_path) {
13731 patch_script_file = fopen(patch_script_path, "re");
13732 if (patch_script_file == NULL) {
13733 error = got_error_from_errno2("fopen",
13734 patch_script_path);
13738 error = apply_unveil(got_repo_get_path(repo), 0,
13739 got_worktree_get_root_path(worktree));
13743 error = check_merge_in_progress(worktree, repo);
13747 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
13752 error = got_worktree_status(worktree, &paths, repo, 0,
13753 print_stage, NULL, check_cancelled, NULL);
13755 cpa.patch_script_file = patch_script_file;
13756 cpa.action = "stage";
13757 error = got_worktree_stage(worktree, &paths,
13758 pflag ? NULL : print_status, NULL,
13759 pflag ? choose_patch : NULL, &cpa,
13760 allow_bad_symlinks, repo);
13763 if (patch_script_file && fclose(patch_script_file) == EOF &&
13765 error = got_error_from_errno2("fclose", patch_script_path);
13767 const struct got_error *close_err = got_repo_close(repo);
13772 got_worktree_close(worktree);
13774 const struct got_error *pack_err =
13775 got_repo_pack_fds_close(pack_fds);
13779 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
13785 usage_unstage(void)
13787 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
13788 "[path ...]\n", getprogname());
13793 static const struct got_error *
13794 cmd_unstage(int argc, char *argv[])
13796 const struct got_error *error = NULL;
13797 struct got_repository *repo = NULL;
13798 struct got_worktree *worktree = NULL;
13800 struct got_pathlist_head paths;
13802 struct got_update_progress_arg upa;
13803 FILE *patch_script_file = NULL;
13804 const char *patch_script_path = NULL;
13805 struct choose_patch_arg cpa;
13806 int *pack_fds = NULL;
13808 TAILQ_INIT(&paths);
13811 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
13812 "unveil", NULL) == -1)
13816 while ((ch = getopt(argc, argv, "F:p")) != -1) {
13819 patch_script_path = optarg;
13833 if (patch_script_path && !pflag)
13834 errx(1, "-F option can only be used together with -p option");
13836 cwd = getcwd(NULL, 0);
13838 error = got_error_from_errno("getcwd");
13842 error = got_repo_pack_fds_open(&pack_fds);
13846 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
13848 if (error->code == GOT_ERR_NOT_WORKTREE)
13849 error = wrap_not_worktree_error(error, "unstage", cwd);
13853 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
13858 if (patch_script_path) {
13859 patch_script_file = fopen(patch_script_path, "re");
13860 if (patch_script_file == NULL) {
13861 error = got_error_from_errno2("fopen",
13862 patch_script_path);
13867 error = apply_unveil(got_repo_get_path(repo), 0,
13868 got_worktree_get_root_path(worktree));
13872 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
13876 cpa.patch_script_file = patch_script_file;
13877 cpa.action = "unstage";
13878 memset(&upa, 0, sizeof(upa));
13879 error = got_worktree_unstage(worktree, &paths, update_progress,
13880 &upa, pflag ? choose_patch : NULL, &cpa, repo);
13882 print_merge_progress_stats(&upa);
13884 if (patch_script_file && fclose(patch_script_file) == EOF &&
13886 error = got_error_from_errno2("fclose", patch_script_path);
13888 const struct got_error *close_err = got_repo_close(repo);
13893 got_worktree_close(worktree);
13895 const struct got_error *pack_err =
13896 got_repo_pack_fds_close(pack_fds);
13900 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
13908 fprintf(stderr, "usage: %s cat [-P] [-c commit] [-r repository-path] "
13909 "arg ...\n", getprogname());
13913 static const struct got_error *
13914 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
13916 const struct got_error *err;
13917 struct got_blob_object *blob;
13920 fd = got_opentempfd();
13922 return got_error_from_errno("got_opentempfd");
13924 err = got_object_open_as_blob(&blob, repo, id, 8192, fd);
13928 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
13930 if (fd != -1 && close(fd) == -1 && err == NULL)
13931 err = got_error_from_errno("close");
13933 got_object_blob_close(blob);
13937 static const struct got_error *
13938 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
13940 const struct got_error *err;
13941 struct got_tree_object *tree;
13944 err = got_object_open_as_tree(&tree, repo, id);
13948 nentries = got_object_tree_get_nentries(tree);
13949 for (i = 0; i < nentries; i++) {
13950 struct got_tree_entry *te;
13952 if (sigint_received || sigpipe_received)
13954 te = got_object_tree_get_entry(tree, i);
13955 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
13958 fprintf(outfile, "%s %.7o %s\n", id_str,
13959 got_tree_entry_get_mode(te),
13960 got_tree_entry_get_name(te));
13964 got_object_tree_close(tree);
13968 static const struct got_error *
13969 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
13971 const struct got_error *err;
13972 struct got_commit_object *commit;
13973 const struct got_object_id_queue *parent_ids;
13974 struct got_object_qid *pid;
13975 char *id_str = NULL;
13976 const char *logmsg = NULL;
13979 err = got_object_open_as_commit(&commit, repo, id);
13983 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
13987 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
13988 parent_ids = got_object_commit_get_parent_ids(commit);
13989 fprintf(outfile, "numparents %d\n",
13990 got_object_commit_get_nparents(commit));
13991 STAILQ_FOREACH(pid, parent_ids, entry) {
13993 err = got_object_id_str(&pid_str, &pid->id);
13996 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
13999 got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
14000 got_object_commit_get_author_gmtoff(commit));
14001 fprintf(outfile, "%s%s %lld %s\n", GOT_COMMIT_LABEL_AUTHOR,
14002 got_object_commit_get_author(commit),
14003 (long long)got_object_commit_get_author_time(commit),
14006 got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
14007 got_object_commit_get_committer_gmtoff(commit));
14008 fprintf(outfile, "%s%s %lld %s\n", GOT_COMMIT_LABEL_COMMITTER,
14009 got_object_commit_get_committer(commit),
14010 (long long)got_object_commit_get_committer_time(commit),
14013 logmsg = got_object_commit_get_logmsg_raw(commit);
14014 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
14015 fprintf(outfile, "%s", logmsg);
14018 got_object_commit_close(commit);
14022 static const struct got_error *
14023 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
14025 const struct got_error *err;
14026 struct got_tag_object *tag;
14027 char *id_str = NULL;
14028 const char *tagmsg = NULL;
14031 err = got_object_open_as_tag(&tag, repo, id);
14035 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
14039 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
14041 switch (got_object_tag_get_object_type(tag)) {
14042 case GOT_OBJ_TYPE_BLOB:
14043 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
14044 GOT_OBJ_LABEL_BLOB);
14046 case GOT_OBJ_TYPE_TREE:
14047 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
14048 GOT_OBJ_LABEL_TREE);
14050 case GOT_OBJ_TYPE_COMMIT:
14051 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
14052 GOT_OBJ_LABEL_COMMIT);
14054 case GOT_OBJ_TYPE_TAG:
14055 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
14056 GOT_OBJ_LABEL_TAG);
14062 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
14063 got_object_tag_get_name(tag));
14065 got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
14066 got_object_tag_get_tagger_gmtoff(tag));
14067 fprintf(outfile, "%s%s %lld %s\n", GOT_TAG_LABEL_TAGGER,
14068 got_object_tag_get_tagger(tag),
14069 (long long)got_object_tag_get_tagger_time(tag),
14072 tagmsg = got_object_tag_get_message(tag);
14073 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
14074 fprintf(outfile, "%s", tagmsg);
14077 got_object_tag_close(tag);
14081 static const struct got_error *
14082 cmd_cat(int argc, char *argv[])
14084 const struct got_error *error;
14085 struct got_repository *repo = NULL;
14086 struct got_worktree *worktree = NULL;
14087 char *cwd = NULL, *repo_path = NULL, *label = NULL;
14088 char *keyword_idstr = NULL;
14089 const char *commit_id_str = NULL;
14090 struct got_object_id *id = NULL, *commit_id = NULL;
14091 struct got_commit_object *commit = NULL;
14092 int ch, obj_type, i, force_path = 0;
14093 struct got_reflist_head refs;
14094 int *pack_fds = NULL;
14099 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
14104 while ((ch = getopt(argc, argv, "c:Pr:")) != -1) {
14107 commit_id_str = optarg;
14113 repo_path = realpath(optarg, NULL);
14114 if (repo_path == NULL)
14115 return got_error_from_errno2("realpath",
14117 got_path_strip_trailing_slashes(repo_path);
14128 cwd = getcwd(NULL, 0);
14130 error = got_error_from_errno("getcwd");
14134 error = got_repo_pack_fds_open(&pack_fds);
14138 if (repo_path == NULL) {
14139 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
14140 if (error && error->code != GOT_ERR_NOT_WORKTREE)
14143 repo_path = strdup(
14144 got_worktree_get_repo_path(worktree));
14145 if (repo_path == NULL) {
14146 error = got_error_from_errno("strdup");
14150 if (commit_id_str == NULL) {
14151 /* Release work tree lock. */
14152 got_worktree_close(worktree);
14158 if (repo_path == NULL) {
14159 repo_path = strdup(cwd);
14160 if (repo_path == NULL)
14161 return got_error_from_errno("strdup");
14164 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
14169 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
14173 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
14177 if (commit_id_str != NULL) {
14178 error = got_keyword_to_idstr(&keyword_idstr, commit_id_str,
14182 if (keyword_idstr != NULL)
14183 commit_id_str = keyword_idstr;
14184 if (worktree != NULL) {
14185 got_worktree_close(worktree);
14189 commit_id_str = GOT_REF_HEAD;
14190 error = got_repo_match_object_id(&commit_id, NULL,
14191 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
14195 error = got_object_open_as_commit(&commit, repo, commit_id);
14199 for (i = 0; i < argc; i++) {
14201 error = got_object_id_by_path(&id, repo, commit,
14206 error = got_repo_match_object_id(&id, &label, argv[i],
14207 GOT_OBJ_TYPE_ANY, NULL /* do not resolve tags */,
14210 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
14211 error->code != GOT_ERR_NOT_REF)
14213 error = got_object_id_by_path(&id, repo,
14220 error = got_object_get_type(&obj_type, repo, id);
14224 switch (obj_type) {
14225 case GOT_OBJ_TYPE_BLOB:
14226 error = cat_blob(id, repo, stdout);
14228 case GOT_OBJ_TYPE_TREE:
14229 error = cat_tree(id, repo, stdout);
14231 case GOT_OBJ_TYPE_COMMIT:
14232 error = cat_commit(id, repo, stdout);
14234 case GOT_OBJ_TYPE_TAG:
14235 error = cat_tag(id, repo, stdout);
14238 error = got_error(GOT_ERR_OBJ_TYPE);
14252 free(keyword_idstr);
14254 got_object_commit_close(commit);
14256 got_worktree_close(worktree);
14258 const struct got_error *close_err = got_repo_close(repo);
14263 const struct got_error *pack_err =
14264 got_repo_pack_fds_close(pack_fds);
14269 got_ref_list_free(&refs);
14276 fprintf(stderr, "usage: %s info [path ...]\n",
14281 static const struct got_error *
14282 print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
14283 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
14284 struct got_object_id *commit_id)
14286 const struct got_error *err = NULL;
14287 char *id_str = NULL;
14289 struct tm mytm, *tm;
14290 struct got_pathlist_head *paths = arg;
14291 struct got_pathlist_entry *pe;
14294 * Clear error indication from any of the path arguments which
14295 * would cause this file index entry to be displayed.
14297 TAILQ_FOREACH(pe, paths, entry) {
14298 if (got_path_cmp(path, pe->path, strlen(path),
14299 pe->path_len) == 0 ||
14300 got_path_is_child(path, pe->path, pe->path_len))
14301 pe->data = NULL; /* no error */
14304 printf(GOT_COMMIT_SEP_STR);
14306 printf("symlink: %s\n", path);
14307 else if (S_ISREG(mode)) {
14308 printf("file: %s\n", path);
14309 printf("mode: %o\n", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
14310 } else if (S_ISDIR(mode))
14311 printf("directory: %s\n", path);
14313 printf("something: %s\n", path);
14315 tm = localtime_r(&mtime, &mytm);
14318 if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) == 0)
14319 return got_error(GOT_ERR_NO_SPACE);
14320 printf("timestamp: %s\n", datebuf);
14323 err = got_object_id_str(&id_str, blob_id);
14326 printf("based on blob: %s\n", id_str);
14330 if (staged_blob_id) {
14331 err = got_object_id_str(&id_str, staged_blob_id);
14334 printf("based on staged blob: %s\n", id_str);
14339 err = got_object_id_str(&id_str, commit_id);
14342 printf("based on commit: %s\n", id_str);
14349 static const struct got_error *
14350 cmd_info(int argc, char *argv[])
14352 const struct got_error *error = NULL;
14353 struct got_worktree *worktree = NULL;
14354 char *cwd = NULL, *id_str = NULL;
14355 struct got_pathlist_head paths;
14356 char *uuidstr = NULL;
14357 int ch, show_files = 0;
14359 TAILQ_INIT(&paths);
14362 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
14367 while ((ch = getopt(argc, argv, "")) != -1) {
14378 cwd = getcwd(NULL, 0);
14380 error = got_error_from_errno("getcwd");
14384 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
14386 if (error->code == GOT_ERR_NOT_WORKTREE)
14387 error = wrap_not_worktree_error(error, "info", cwd);
14392 /* Remove "wpath cpath proc exec sendfd" promises. */
14393 if (pledge("stdio rpath flock unveil", NULL) == -1)
14396 error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
14401 error = get_worktree_paths_from_argv(&paths, argc, argv,
14408 error = got_object_id_str(&id_str,
14409 got_worktree_get_base_commit_id(worktree));
14413 error = got_worktree_get_uuid(&uuidstr, worktree);
14417 printf("work tree: %s\n", got_worktree_get_root_path(worktree));
14418 printf("work tree base commit: %s\n", id_str);
14419 printf("work tree path prefix: %s\n",
14420 got_worktree_get_path_prefix(worktree));
14421 printf("work tree branch reference: %s\n",
14422 got_worktree_get_head_ref_name(worktree));
14423 printf("work tree UUID: %s\n", uuidstr);
14424 printf("repository: %s\n", got_worktree_get_repo_path(worktree));
14427 struct got_pathlist_entry *pe;
14428 TAILQ_FOREACH(pe, &paths, entry) {
14429 if (pe->path_len == 0)
14432 * Assume this path will fail. This will be corrected
14433 * in print_path_info() in case the path does suceeed.
14435 pe->data = (void *)got_error(GOT_ERR_BAD_PATH);
14437 error = got_worktree_path_info(worktree, &paths,
14438 print_path_info, &paths, check_cancelled, NULL);
14441 TAILQ_FOREACH(pe, &paths, entry) {
14442 if (pe->data != NULL) {
14443 const struct got_error *perr;
14446 error = got_error_fmt(perr->code, "%s",
14454 got_worktree_close(worktree);
14455 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);