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 free(worktree_path);
3684 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
3686 free(commit_id_str);
3690 static const struct got_error *
3691 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
3692 const char *path, int diff_context, int ignore_whitespace,
3693 int force_text_diff, struct got_diffstat_cb_arg *dsa,
3694 struct got_repository *repo, FILE *outfile)
3696 const struct got_error *err = NULL;
3697 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
3698 FILE *f1 = NULL, *f2 = NULL;
3699 int fd1 = -1, fd2 = -1;
3701 fd1 = got_opentempfd();
3703 return got_error_from_errno("got_opentempfd");
3704 fd2 = got_opentempfd();
3706 err = got_error_from_errno("got_opentempfd");
3711 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192,
3717 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192, fd2);
3721 f1 = got_opentemp();
3723 err = got_error_from_errno("got_opentemp");
3726 f2 = got_opentemp();
3728 err = got_error_from_errno("got_opentemp");
3732 while (path[0] == '/')
3734 err = got_diff_blob(NULL, NULL, blob1, blob2, f1, f2, path, path,
3735 GOT_DIFF_ALGORITHM_PATIENCE, diff_context, ignore_whitespace,
3736 force_text_diff, dsa, outfile);
3738 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3739 err = got_error_from_errno("close");
3741 got_object_blob_close(blob1);
3742 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3743 err = got_error_from_errno("close");
3745 got_object_blob_close(blob2);
3746 if (f1 && fclose(f1) == EOF && err == NULL)
3747 err = got_error_from_errno("fclose");
3748 if (f2 && fclose(f2) == EOF && err == NULL)
3749 err = got_error_from_errno("fclose");
3753 static const struct got_error *
3754 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
3755 const char *path, int diff_context, int ignore_whitespace,
3756 int force_text_diff, struct got_diffstat_cb_arg *dsa,
3757 struct got_repository *repo, FILE *outfile)
3759 const struct got_error *err = NULL;
3760 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3761 struct got_diff_blob_output_unidiff_arg arg;
3762 FILE *f1 = NULL, *f2 = NULL;
3763 int fd1 = -1, fd2 = -1;
3766 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3769 fd1 = got_opentempfd();
3771 err = got_error_from_errno("got_opentempfd");
3776 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3780 f1 = got_opentemp();
3782 err = got_error_from_errno("got_opentemp");
3786 f2 = got_opentemp();
3788 err = got_error_from_errno("got_opentemp");
3791 fd2 = got_opentempfd();
3793 err = got_error_from_errno("got_opentempfd");
3796 arg.diff_context = diff_context;
3797 arg.ignore_whitespace = ignore_whitespace;
3798 arg.force_text_diff = force_text_diff;
3800 arg.diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
3801 arg.outfile = outfile;
3804 while (path[0] == '/')
3806 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, path, path, repo,
3807 got_diff_blob_output_unidiff, &arg, 1);
3810 got_object_tree_close(tree1);
3812 got_object_tree_close(tree2);
3813 if (f1 && fclose(f1) == EOF && err == NULL)
3814 err = got_error_from_errno("fclose");
3815 if (f2 && fclose(f2) == EOF && err == NULL)
3816 err = got_error_from_errno("fclose");
3817 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3818 err = got_error_from_errno("close");
3819 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3820 err = got_error_from_errno("close");
3824 static const struct got_error *
3825 get_changed_paths(struct got_pathlist_head *paths,
3826 struct got_commit_object *commit, struct got_repository *repo,
3827 struct got_diffstat_cb_arg *dsa)
3829 const struct got_error *err = NULL;
3830 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3831 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3832 struct got_object_qid *qid;
3833 got_diff_blob_cb cb = got_diff_tree_collect_changed_paths;
3834 FILE *f1 = NULL, *f2 = NULL;
3835 int fd1 = -1, fd2 = -1;
3838 cb = got_diff_tree_compute_diffstat;
3840 f1 = got_opentemp();
3842 err = got_error_from_errno("got_opentemp");
3845 f2 = got_opentemp();
3847 err = got_error_from_errno("got_opentemp");
3850 fd1 = got_opentempfd();
3852 err = got_error_from_errno("got_opentempfd");
3855 fd2 = got_opentempfd();
3857 err = got_error_from_errno("got_opentempfd");
3862 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3864 struct got_commit_object *pcommit;
3865 err = got_object_open_as_commit(&pcommit, repo,
3870 tree_id1 = got_object_id_dup(
3871 got_object_commit_get_tree_id(pcommit));
3872 if (tree_id1 == NULL) {
3873 got_object_commit_close(pcommit);
3874 return got_error_from_errno("got_object_id_dup");
3876 got_object_commit_close(pcommit);
3881 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3886 tree_id2 = got_object_commit_get_tree_id(commit);
3887 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3891 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3892 cb, dsa ? (void *)dsa : paths, dsa ? 1 : 0);
3895 got_object_tree_close(tree1);
3897 got_object_tree_close(tree2);
3898 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3899 err = got_error_from_errno("close");
3900 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3901 err = got_error_from_errno("close");
3902 if (f1 && fclose(f1) == EOF && err == NULL)
3903 err = got_error_from_errno("fclose");
3904 if (f2 && fclose(f2) == EOF && err == NULL)
3905 err = got_error_from_errno("fclose");
3910 static const struct got_error *
3911 print_patch(struct got_commit_object *commit, struct got_object_id *id,
3912 const char *path, int diff_context, struct got_diffstat_cb_arg *dsa,
3913 struct got_repository *repo, FILE *outfile)
3915 const struct got_error *err = NULL;
3916 struct got_commit_object *pcommit = NULL;
3917 char *id_str1 = NULL, *id_str2 = NULL;
3918 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
3919 struct got_object_qid *qid;
3921 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3923 err = got_object_open_as_commit(&pcommit, repo,
3927 err = got_object_id_str(&id_str1, &qid->id);
3932 err = got_object_id_str(&id_str2, id);
3936 if (path && path[0] != '\0') {
3938 err = got_object_id_by_path(&obj_id2, repo, commit, path);
3942 err = got_object_id_by_path(&obj_id1, repo,
3945 if (err->code != GOT_ERR_NO_TREE_ENTRY) {
3951 err = got_object_get_type(&obj_type, repo, obj_id2);
3957 "diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3958 fprintf(outfile, "commit - %s\n",
3959 id_str1 ? id_str1 : "/dev/null");
3960 fprintf(outfile, "commit + %s\n", id_str2);
3962 case GOT_OBJ_TYPE_BLOB:
3963 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
3964 0, 0, dsa, repo, outfile);
3966 case GOT_OBJ_TYPE_TREE:
3967 err = diff_trees(obj_id1, obj_id2, path, diff_context,
3968 0, 0, dsa, repo, outfile);
3971 err = got_error(GOT_ERR_OBJ_TYPE);
3977 obj_id2 = got_object_commit_get_tree_id(commit);
3979 obj_id1 = got_object_commit_get_tree_id(pcommit);
3981 "diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3982 fprintf(outfile, "commit - %s\n",
3983 id_str1 ? id_str1 : "/dev/null");
3984 fprintf(outfile, "commit + %s\n", id_str2);
3985 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, 0,
3986 dsa, repo, outfile);
3992 got_object_commit_close(pcommit);
3997 get_datestr(time_t *time, char *datebuf)
3999 struct tm mytm, *tm;
4002 tm = gmtime_r(time, &mytm);
4005 s = asctime_r(tm, datebuf);
4008 p = strchr(s, '\n');
4014 static const struct got_error *
4015 match_commit(int *have_match, struct got_object_id *id,
4016 struct got_commit_object *commit, regex_t *regex)
4018 const struct got_error *err = NULL;
4019 regmatch_t regmatch;
4020 char *id_str = NULL, *logmsg = NULL;
4024 err = got_object_id_str(&id_str, id);
4028 err = got_object_commit_get_logmsg(&logmsg, commit);
4032 if (regexec(regex, got_object_commit_get_author(commit), 1,
4033 ®match, 0) == 0 ||
4034 regexec(regex, got_object_commit_get_committer(commit), 1,
4035 ®match, 0) == 0 ||
4036 regexec(regex, id_str, 1, ®match, 0) == 0 ||
4037 regexec(regex, logmsg, 1, ®match, 0) == 0)
4046 match_changed_paths(int *have_match, struct got_pathlist_head *changed_paths,
4049 regmatch_t regmatch;
4050 struct got_pathlist_entry *pe;
4054 TAILQ_FOREACH(pe, changed_paths, entry) {
4055 if (regexec(regex, pe->path, 1, ®match, 0) == 0) {
4062 static const struct got_error *
4063 match_patch(int *have_match, struct got_commit_object *commit,
4064 struct got_object_id *id, const char *path, int diff_context,
4065 struct got_repository *repo, regex_t *regex, FILE *f)
4067 const struct got_error *err = NULL;
4069 size_t linesize = 0;
4070 regmatch_t regmatch;
4074 err = got_opentemp_truncate(f);
4078 err = print_patch(commit, id, path, diff_context, NULL, repo, f);
4082 if (fseeko(f, 0L, SEEK_SET) == -1) {
4083 err = got_error_from_errno("fseeko");
4087 while (getline(&line, &linesize, f) != -1) {
4088 if (regexec(regex, line, 1, ®match, 0) == 0) {
4098 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
4100 static const struct got_error*
4101 build_refs_str(char **refs_str, struct got_reflist_head *refs,
4102 struct got_object_id *id, struct got_repository *repo,
4105 static const struct got_error *err = NULL;
4106 struct got_reflist_entry *re;
4112 TAILQ_FOREACH(re, refs, entry) {
4113 struct got_tag_object *tag = NULL;
4114 struct got_object_id *ref_id;
4117 name = got_ref_get_name(re->ref);
4118 if (strcmp(name, GOT_REF_HEAD) == 0)
4120 if (strncmp(name, "refs/", 5) == 0)
4122 if (strncmp(name, "got/", 4) == 0)
4124 if (strncmp(name, "heads/", 6) == 0)
4126 if (strncmp(name, "remotes/", 8) == 0) {
4130 s = strstr(name, "/" GOT_REF_HEAD);
4131 if (s != NULL && strcmp(s, "/" GOT_REF_HEAD) == 0)
4134 err = got_ref_resolve(&ref_id, repo, re->ref);
4137 if (strncmp(name, "tags/", 5) == 0) {
4138 err = got_object_open_as_tag(&tag, repo, ref_id);
4140 if (err->code != GOT_ERR_OBJ_TYPE) {
4144 /* Ref points at something other than a tag. */
4149 cmp = got_object_id_cmp(tag ?
4150 got_object_tag_get_object_id(tag) : ref_id, id);
4153 got_object_tag_close(tag);
4157 if (asprintf(refs_str, "%s%s%s", s ? s : "",
4158 s ? ", " : "", name) == -1) {
4159 err = got_error_from_errno("asprintf");
4170 static const struct got_error *
4171 print_commit_oneline(struct got_commit_object *commit, struct got_object_id *id,
4172 struct got_repository *repo, struct got_reflist_object_id_map *refs_idmap)
4174 const struct got_error *err = NULL;
4175 char *ref_str = NULL, *id_str = NULL, *logmsg0 = NULL;
4176 char *comma, *s, *nl;
4177 struct got_reflist_head *refs;
4178 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
4180 time_t committer_time;
4182 refs = got_reflist_object_id_map_lookup(refs_idmap, id);
4184 err = build_refs_str(&ref_str, refs, id, repo, 1);
4188 /* Display the first matching ref only. */
4189 if (ref_str && (comma = strchr(ref_str, ',')) != NULL)
4193 if (ref_str == NULL) {
4194 err = got_object_id_str(&id_str, id);
4199 committer_time = got_object_commit_get_committer_time(commit);
4200 if (gmtime_r(&committer_time, &tm) == NULL) {
4201 err = got_error_from_errno("gmtime_r");
4204 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0) {
4205 err = got_error(GOT_ERR_NO_SPACE);
4209 err = got_object_commit_get_logmsg(&logmsg0, commit);
4214 while (isspace((unsigned char)s[0]))
4217 nl = strchr(s, '\n');
4223 printf("%s%-7s %s\n", datebuf, ref_str, s);
4225 printf("%s%.7s %s\n", datebuf, id_str, s);
4227 if (fflush(stdout) != 0 && err == NULL)
4228 err = got_error_from_errno("fflush");
4236 static const struct got_error *
4237 print_diffstat(struct got_diffstat_cb_arg *dsa, const char *header)
4239 struct got_pathlist_entry *pe;
4242 printf("%s\n", header);
4244 TAILQ_FOREACH(pe, dsa->paths, entry) {
4245 struct got_diff_changed_path *cp = pe->data;
4246 int pad = dsa->max_path_len - pe->path_len + 1;
4248 printf(" %c %s%*c | %*d+ %*d-\n", cp->status, pe->path, pad,
4249 ' ', dsa->add_cols + 1, cp->add, dsa->rm_cols + 1, cp->rm);
4251 printf("\n%d file%s changed, %d insertion%s(+), %d deletion%s(-)\n\n",
4252 dsa->nfiles, dsa->nfiles > 1 ? "s" : "", dsa->ins,
4253 dsa->ins != 1 ? "s" : "", dsa->del, dsa->del != 1 ? "s" : "");
4255 if (fflush(stdout) != 0)
4256 return got_error_from_errno("fflush");
4261 static const struct got_error *
4267 if (fseeko(f, 0L, SEEK_SET) == -1)
4268 return got_error_from_errno("fseek");
4271 r = fread(buf, 1, sizeof(buf), f);
4274 return got_error_from_errno("fread");
4278 if (fwrite(buf, 1, r, stdout) != r)
4279 return got_ferror(stdout, GOT_ERR_IO);
4285 static const struct got_error *
4286 print_commit(struct got_commit_object *commit, struct got_object_id *id,
4287 struct got_repository *repo, const char *path,
4288 struct got_pathlist_head *changed_paths,
4289 struct got_diffstat_cb_arg *diffstat, int show_patch, int diff_context,
4290 struct got_reflist_object_id_map *refs_idmap, const char *custom_refs_str,
4293 const struct got_error *err = NULL;
4295 char *id_str, *datestr, *logmsg0, *logmsg, *line;
4297 time_t committer_time;
4298 const char *author, *committer;
4299 char *refs_str = NULL;
4301 err = got_object_id_str(&id_str, id);
4305 if (custom_refs_str == NULL) {
4306 struct got_reflist_head *refs;
4307 refs = got_reflist_object_id_map_lookup(refs_idmap, id);
4309 err = build_refs_str(&refs_str, refs, id, repo, 0);
4315 printf(GOT_COMMIT_SEP_STR);
4316 if (custom_refs_str)
4317 printf("%s %s (%s)\n", prefix ? prefix : "commit", id_str,
4320 printf("%s %s%s%s%s\n", prefix ? prefix : "commit", id_str,
4321 refs_str ? " (" : "", refs_str ? refs_str : "",
4322 refs_str ? ")" : "");
4327 printf("from: %s\n", got_object_commit_get_author(commit));
4328 author = got_object_commit_get_author(commit);
4329 committer = got_object_commit_get_committer(commit);
4330 if (strcmp(author, committer) != 0)
4331 printf("via: %s\n", committer);
4332 committer_time = got_object_commit_get_committer_time(commit);
4333 datestr = get_datestr(&committer_time, datebuf);
4335 printf("date: %s UTC\n", datestr);
4336 if (got_object_commit_get_nparents(commit) > 1) {
4337 const struct got_object_id_queue *parent_ids;
4338 struct got_object_qid *qid;
4340 parent_ids = got_object_commit_get_parent_ids(commit);
4341 STAILQ_FOREACH(qid, parent_ids, entry) {
4342 err = got_object_id_str(&id_str, &qid->id);
4345 printf("parent %d: %s\n", n++, id_str);
4351 err = got_object_commit_get_logmsg(&logmsg0, commit);
4357 line = strsep(&logmsg, "\n");
4359 printf(" %s\n", line);
4363 if (changed_paths && diffstat == NULL) {
4364 struct got_pathlist_entry *pe;
4366 TAILQ_FOREACH(pe, changed_paths, entry) {
4367 struct got_diff_changed_path *cp = pe->data;
4369 printf(" %c %s\n", cp->status, pe->path);
4377 err = got_error_from_errno("got_opentemp");
4382 err = print_patch(commit, id, path, diff_context, diffstat,
4383 repo, diffstat == NULL ? stdout : f);
4388 err = print_diffstat(diffstat, NULL);
4400 if (fflush(stdout) != 0 && err == NULL)
4401 err = got_error_from_errno("fflush");
4403 if (f && fclose(f) == EOF && err == NULL)
4404 err = got_error_from_errno("fclose");
4410 static const struct got_error *
4411 print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
4412 struct got_repository *repo, const char *path, int show_changed_paths,
4413 int show_diffstat, int show_patch, const char *search_pattern,
4414 int diff_context, int limit, int log_branches, int reverse_display_order,
4415 struct got_reflist_object_id_map *refs_idmap, int one_line,
4418 const struct got_error *err;
4419 struct got_commit_graph *graph;
4422 struct got_object_id_queue reversed_commits;
4423 struct got_object_qid *qid;
4424 struct got_commit_object *commit;
4425 struct got_pathlist_head changed_paths;
4427 STAILQ_INIT(&reversed_commits);
4428 TAILQ_INIT(&changed_paths);
4430 if (search_pattern && regcomp(®ex, search_pattern,
4431 REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
4432 return got_error_msg(GOT_ERR_REGEX, search_pattern);
4434 err = got_commit_graph_open(&graph, path, !log_branches);
4437 err = got_commit_graph_iter_start(graph, root_id, repo,
4438 check_cancelled, NULL);
4442 struct got_object_id id;
4443 struct got_diffstat_cb_arg dsa = { 0, 0, 0, 0, 0, 0,
4444 &changed_paths, 0, 0, GOT_DIFF_ALGORITHM_PATIENCE };
4446 if (sigint_received || sigpipe_received)
4449 err = got_commit_graph_iter_next(&id, graph, repo,
4450 check_cancelled, NULL);
4452 if (err->code == GOT_ERR_ITER_COMPLETED)
4457 err = got_object_open_as_commit(&commit, repo, &id);
4461 if ((show_changed_paths || (show_diffstat && !show_patch))
4462 && !reverse_display_order) {
4463 err = get_changed_paths(&changed_paths, commit, repo,
4464 show_diffstat ? &dsa : NULL);
4469 if (search_pattern) {
4470 err = match_commit(&have_match, &id, commit, ®ex);
4472 got_object_commit_close(commit);
4475 if (have_match == 0 && show_changed_paths)
4476 match_changed_paths(&have_match,
4477 &changed_paths, ®ex);
4478 if (have_match == 0 && show_patch) {
4479 err = match_patch(&have_match, commit, &id,
4480 path, diff_context, repo, ®ex, tmpfile);
4484 if (have_match == 0) {
4485 got_object_commit_close(commit);
4486 got_pathlist_free(&changed_paths,
4487 GOT_PATHLIST_FREE_ALL);
4492 if (reverse_display_order) {
4493 err = got_object_qid_alloc(&qid, &id);
4496 STAILQ_INSERT_HEAD(&reversed_commits, qid, entry);
4497 got_object_commit_close(commit);
4500 err = print_commit_oneline(commit, &id,
4503 err = print_commit(commit, &id, repo, path,
4504 (show_changed_paths || show_diffstat) ?
4505 &changed_paths : NULL,
4506 show_diffstat ? &dsa : NULL, show_patch,
4507 diff_context, refs_idmap, NULL, NULL);
4508 got_object_commit_close(commit);
4512 if ((limit && --limit == 0) ||
4513 (end_id && got_object_id_cmp(&id, end_id) == 0))
4516 got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
4518 if (reverse_display_order) {
4519 STAILQ_FOREACH(qid, &reversed_commits, entry) {
4520 struct got_diffstat_cb_arg dsa = { 0, 0, 0, 0, 0, 0,
4521 &changed_paths, 0, 0, GOT_DIFF_ALGORITHM_PATIENCE };
4523 err = got_object_open_as_commit(&commit, repo,
4527 if (show_changed_paths ||
4528 (show_diffstat && !show_patch)) {
4529 err = get_changed_paths(&changed_paths, commit,
4530 repo, show_diffstat ? &dsa : NULL);
4535 err = print_commit_oneline(commit, &qid->id,
4538 err = print_commit(commit, &qid->id, repo, path,
4539 (show_changed_paths || show_diffstat) ?
4540 &changed_paths : NULL,
4541 show_diffstat ? &dsa : NULL, show_patch,
4542 diff_context, refs_idmap, NULL, NULL);
4543 got_object_commit_close(commit);
4546 got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
4550 while (!STAILQ_EMPTY(&reversed_commits)) {
4551 qid = STAILQ_FIRST(&reversed_commits);
4552 STAILQ_REMOVE_HEAD(&reversed_commits, entry);
4553 got_object_qid_free(qid);
4555 got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
4558 got_commit_graph_close(graph);
4565 fprintf(stderr, "usage: %s log [-bdPpRs] [-C number] [-c commit] "
4566 "[-l N] [-r repository-path] [-S search-pattern] [-x commit] "
4567 "[path]\n", getprogname());
4572 get_default_log_limit(void)
4574 const char *got_default_log_limit;
4578 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
4579 if (got_default_log_limit == NULL)
4581 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
4587 static const struct got_error *
4588 cmd_log(int argc, char *argv[])
4590 const struct got_error *error;
4591 struct got_repository *repo = NULL;
4592 struct got_worktree *worktree = NULL;
4593 struct got_object_id *start_id = NULL, *end_id = NULL;
4594 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
4595 const char *start_commit = NULL, *end_commit = NULL;
4596 const char *search_pattern = NULL;
4597 int diff_context = -1, ch;
4598 int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
4599 int show_diffstat = 0, reverse_display_order = 0, one_line = 0;
4601 struct got_reflist_head refs;
4602 struct got_reflist_object_id_map *refs_idmap = NULL;
4603 FILE *tmpfile = NULL;
4604 int *pack_fds = NULL;
4609 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4615 limit = get_default_log_limit();
4617 while ((ch = getopt(argc, argv, "bC:c:dl:PpRr:S:sx:")) != -1) {
4623 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4626 errx(1, "number of context lines is %s: %s",
4630 start_commit = optarg;
4636 limit = strtonum(optarg, 0, INT_MAX, &errstr);
4638 errx(1, "number of commits is %s: %s",
4642 show_changed_paths = 1;
4648 reverse_display_order = 1;
4651 repo_path = realpath(optarg, NULL);
4652 if (repo_path == NULL)
4653 return got_error_from_errno2("realpath",
4655 got_path_strip_trailing_slashes(repo_path);
4658 search_pattern = optarg;
4664 end_commit = optarg;
4675 if (diff_context == -1)
4677 else if (!show_patch)
4678 errx(1, "-C requires -p");
4680 if (one_line && (show_patch || show_changed_paths || show_diffstat))
4681 errx(1, "cannot use -s with -d, -p or -P");
4683 cwd = getcwd(NULL, 0);
4685 error = got_error_from_errno("getcwd");
4689 error = got_repo_pack_fds_open(&pack_fds);
4693 if (repo_path == NULL) {
4694 error = got_worktree_open(&worktree, cwd,
4695 GOT_WORKTREE_GOT_DIR);
4696 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4703 error = got_worktree_resolve_path(&path, worktree,
4708 path = strdup(argv[0]);
4710 error = got_error_from_errno("strdup");
4714 } else if (argc != 0)
4717 if (repo_path == NULL) {
4718 repo_path = worktree ?
4719 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
4721 if (repo_path == NULL) {
4722 error = got_error_from_errno("strdup");
4726 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4730 error = apply_unveil(got_repo_get_path(repo), 1,
4731 worktree ? got_worktree_get_root_path(worktree) : NULL);
4735 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4739 error = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
4743 if (start_commit == NULL) {
4744 struct got_reference *head_ref;
4745 struct got_commit_object *commit = NULL;
4746 error = got_ref_open(&head_ref, repo,
4747 worktree ? got_worktree_get_head_ref_name(worktree)
4751 error = got_ref_resolve(&start_id, repo, head_ref);
4752 got_ref_close(head_ref);
4755 error = got_object_open_as_commit(&commit, repo,
4759 got_object_commit_close(commit);
4761 char *keyword_idstr = NULL;
4763 error = got_keyword_to_idstr(&keyword_idstr, start_commit,
4767 if (keyword_idstr != NULL)
4768 start_commit = keyword_idstr;
4770 error = got_repo_match_object_id(&start_id, NULL,
4771 start_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4772 free(keyword_idstr);
4776 if (end_commit != NULL) {
4777 error = got_repo_match_object_id(&end_id, NULL,
4778 end_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4785 * If a path was specified on the command line it was resolved
4786 * to a path in the work tree above. Prepend the work tree's
4787 * path prefix to obtain the corresponding in-repository path.
4791 prefix = got_worktree_get_path_prefix(worktree);
4792 if (asprintf(&in_repo_path, "%s%s%s", prefix,
4793 (path[0] != '\0') ? "/" : "", path) == -1) {
4794 error = got_error_from_errno("asprintf");
4799 error = got_repo_map_path(&in_repo_path, repo,
4805 path = in_repo_path;
4809 /* Release work tree lock. */
4810 got_worktree_close(worktree);
4814 if (search_pattern && show_patch) {
4815 tmpfile = got_opentemp();
4816 if (tmpfile == NULL) {
4817 error = got_error_from_errno("got_opentemp");
4822 error = print_commits(start_id, end_id, repo, path ? path : "",
4823 show_changed_paths, show_diffstat, show_patch, search_pattern,
4824 diff_context, limit, log_branches, reverse_display_order,
4825 refs_idmap, one_line, tmpfile);
4833 got_worktree_close(worktree);
4835 const struct got_error *close_err = got_repo_close(repo);
4840 const struct got_error *pack_err =
4841 got_repo_pack_fds_close(pack_fds);
4846 got_reflist_object_id_map_free(refs_idmap);
4847 if (tmpfile && fclose(tmpfile) == EOF && error == NULL)
4848 error = got_error_from_errno("fclose");
4849 got_ref_list_free(&refs);
4856 fprintf(stderr, "usage: %s diff [-adPsw] [-C number] [-c commit] "
4857 "[-r repository-path] [object1 object2 | path ...]\n",
4862 struct print_diff_arg {
4863 struct got_repository *repo;
4864 struct got_worktree *worktree;
4865 struct got_diffstat_cb_arg *diffstat;
4870 enum got_diff_algorithm diff_algo;
4871 int ignore_whitespace;
4872 int force_text_diff;
4879 * Create a file which contains the target path of a symlink so we can feed
4880 * it as content to the diff engine.
4882 static const struct got_error *
4883 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
4884 const char *abspath)
4886 const struct got_error *err = NULL;
4887 char target_path[PATH_MAX];
4888 ssize_t target_len, outlen;
4893 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
4894 if (target_len == -1)
4895 return got_error_from_errno2("readlinkat", abspath);
4897 target_len = readlink(abspath, target_path, PATH_MAX);
4898 if (target_len == -1)
4899 return got_error_from_errno2("readlink", abspath);
4902 *fd = got_opentempfd();
4904 return got_error_from_errno("got_opentempfd");
4906 outlen = write(*fd, target_path, target_len);
4908 err = got_error_from_errno("got_opentempfd");
4912 if (lseek(*fd, 0, SEEK_SET) == -1) {
4913 err = got_error_from_errno2("lseek", abspath);
4924 static const struct got_error *
4925 print_diff(void *arg, unsigned char status, unsigned char staged_status,
4926 const char *path, struct got_object_id *blob_id,
4927 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4928 int dirfd, const char *de_name)
4930 struct print_diff_arg *a = arg;
4931 const struct got_error *err = NULL;
4932 struct got_blob_object *blob1 = NULL;
4933 int fd = -1, fd1 = -1, fd2 = -1;
4935 char *abspath = NULL, *label1 = NULL;
4940 memset(&sb, 0, sizeof(sb));
4942 if (a->diff_staged) {
4943 if (staged_status != GOT_STATUS_MODIFY &&
4944 staged_status != GOT_STATUS_ADD &&
4945 staged_status != GOT_STATUS_DELETE)
4948 if (staged_status == GOT_STATUS_DELETE)
4950 if (status == GOT_STATUS_NONEXISTENT)
4951 return got_error_set_errno(ENOENT, path);
4952 if (status != GOT_STATUS_MODIFY &&
4953 status != GOT_STATUS_ADD &&
4954 status != GOT_STATUS_DELETE &&
4955 status != GOT_STATUS_CONFLICT)
4959 err = got_opentemp_truncate(a->f1);
4961 return got_error_from_errno("got_opentemp_truncate");
4962 err = got_opentemp_truncate(a->f2);
4964 return got_error_from_errno("got_opentemp_truncate");
4966 if (!a->header_shown) {
4967 if (fprintf(a->outfile, "diff %s%s\n",
4968 a->diff_staged ? "-s " : "",
4969 got_worktree_get_root_path(a->worktree)) < 0) {
4970 err = got_error_from_errno("fprintf");
4973 if (fprintf(a->outfile, "commit - %s\n", a->id_str) < 0) {
4974 err = got_error_from_errno("fprintf");
4977 if (fprintf(a->outfile, "path + %s%s\n",
4978 got_worktree_get_root_path(a->worktree),
4979 a->diff_staged ? " (staged changes)" : "") < 0) {
4980 err = got_error_from_errno("fprintf");
4983 a->header_shown = 1;
4986 if (a->diff_staged) {
4987 const char *label1 = NULL, *label2 = NULL;
4988 switch (staged_status) {
4989 case GOT_STATUS_MODIFY:
4993 case GOT_STATUS_ADD:
4996 case GOT_STATUS_DELETE:
5000 return got_error(GOT_ERR_FILE_STATUS);
5002 fd1 = got_opentempfd();
5004 err = got_error_from_errno("got_opentempfd");
5007 fd2 = got_opentempfd();
5009 err = got_error_from_errno("got_opentempfd");
5012 err = got_diff_objects_as_blobs(NULL, NULL, a->f1, a->f2,
5013 fd1, fd2, blob_id, staged_blob_id, label1, label2,
5014 a->diff_algo, a->diff_context, a->ignore_whitespace,
5015 a->force_text_diff, a->diffstat, a->repo, a->outfile);
5019 fd1 = got_opentempfd();
5021 err = got_error_from_errno("got_opentempfd");
5025 if (staged_status == GOT_STATUS_ADD ||
5026 staged_status == GOT_STATUS_MODIFY) {
5028 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
5032 err = got_object_id_str(&id_str, staged_blob_id);
5035 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
5036 err = got_error_from_errno("asprintf");
5041 } else if (status != GOT_STATUS_ADD) {
5042 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192,
5048 if (status != GOT_STATUS_DELETE) {
5049 if (asprintf(&abspath, "%s/%s",
5050 got_worktree_get_root_path(a->worktree), path) == -1) {
5051 err = got_error_from_errno("asprintf");
5056 fd = openat(dirfd, de_name,
5057 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5059 if (!got_err_open_nofollow_on_symlink()) {
5060 err = got_error_from_errno2("openat",
5064 err = get_symlink_target_file(&fd, dirfd,
5070 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5072 if (!got_err_open_nofollow_on_symlink()) {
5073 err = got_error_from_errno2("open",
5077 err = get_symlink_target_file(&fd, dirfd,
5083 if (fstatat(fd, abspath, &sb, AT_SYMLINK_NOFOLLOW) == -1) {
5084 err = got_error_from_errno2("fstatat", abspath);
5087 f2 = fdopen(fd, "r");
5089 err = got_error_from_errno2("fdopen", abspath);
5097 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5103 err = got_diff_blob_file(blob1, a->f1, size1, label1, f2 ? f2 : a->f2,
5104 f2_exists, &sb, path, GOT_DIFF_ALGORITHM_PATIENCE, a->diff_context,
5105 a->ignore_whitespace, a->force_text_diff, a->diffstat, a->outfile);
5107 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5108 err = got_error_from_errno("close");
5109 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5110 err = got_error_from_errno("close");
5112 got_object_blob_close(blob1);
5113 if (fd != -1 && close(fd) == -1 && err == NULL)
5114 err = got_error_from_errno("close");
5115 if (f2 && fclose(f2) == EOF && err == NULL)
5116 err = got_error_from_errno("fclose");
5121 static const struct got_error *
5122 cmd_diff(int argc, char *argv[])
5124 const struct got_error *error;
5125 struct got_repository *repo = NULL;
5126 struct got_worktree *worktree = NULL;
5127 char *cwd = NULL, *repo_path = NULL;
5128 const char *commit_args[2] = { NULL, NULL };
5129 int ncommit_args = 0;
5130 struct got_object_id *ids[2] = { NULL, NULL };
5131 char *labels[2] = { NULL, NULL };
5132 int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
5133 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch, i;
5134 int force_text_diff = 0, force_path = 0, rflag = 0, show_diffstat = 0;
5136 struct got_reflist_head refs;
5137 struct got_pathlist_head diffstat_paths, paths;
5138 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
5139 int fd1 = -1, fd2 = -1;
5140 int *pack_fds = NULL;
5141 struct got_diffstat_cb_arg dsa;
5143 memset(&dsa, 0, sizeof(dsa));
5147 TAILQ_INIT(&diffstat_paths);
5150 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5155 while ((ch = getopt(argc, argv, "aC:c:dPr:sw")) != -1) {
5158 force_text_diff = 1;
5161 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
5164 errx(1, "number of context lines is %s: %s",
5168 if (ncommit_args >= 2)
5169 errx(1, "too many -c options used");
5170 commit_args[ncommit_args++] = optarg;
5179 repo_path = realpath(optarg, NULL);
5180 if (repo_path == NULL)
5181 return got_error_from_errno2("realpath",
5183 got_path_strip_trailing_slashes(repo_path);
5190 ignore_whitespace = 1;
5201 cwd = getcwd(NULL, 0);
5203 error = got_error_from_errno("getcwd");
5207 error = got_repo_pack_fds_open(&pack_fds);
5211 if (repo_path == NULL) {
5212 error = got_worktree_open(&worktree, cwd,
5213 GOT_WORKTREE_GOT_DIR);
5214 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5220 strdup(got_worktree_get_repo_path(worktree));
5221 if (repo_path == NULL) {
5222 error = got_error_from_errno("strdup");
5226 repo_path = strdup(cwd);
5227 if (repo_path == NULL) {
5228 error = got_error_from_errno("strdup");
5234 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5239 if (show_diffstat) {
5240 dsa.paths = &diffstat_paths;
5241 dsa.force_text = force_text_diff;
5242 dsa.ignore_ws = ignore_whitespace;
5243 dsa.diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
5246 if (rflag || worktree == NULL || ncommit_args > 0) {
5248 error = got_error_msg(GOT_ERR_NOT_IMPL,
5249 "-P option can only be used when diffing "
5254 error = got_error_msg(GOT_ERR_NOT_IMPL,
5255 "-s option can only be used when diffing "
5261 error = apply_unveil(got_repo_get_path(repo), 1,
5262 worktree ? got_worktree_get_root_path(worktree) : NULL);
5266 if ((!force_path && argc == 2) || ncommit_args > 0) {
5267 int obj_type = (ncommit_args > 0 ?
5268 GOT_OBJ_TYPE_COMMIT : GOT_OBJ_TYPE_ANY);
5269 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5273 for (i = 0; i < (ncommit_args > 0 ? ncommit_args : argc); i++) {
5275 char *keyword_idstr = NULL;
5277 if (ncommit_args > 0)
5278 arg = commit_args[i];
5282 error = got_keyword_to_idstr(&keyword_idstr, arg,
5286 if (keyword_idstr != NULL)
5287 arg = keyword_idstr;
5289 error = got_repo_match_object_id(&ids[i], &labels[i],
5290 arg, obj_type, &refs, repo);
5291 free(keyword_idstr);
5293 if (error->code != GOT_ERR_NOT_REF &&
5294 error->code != GOT_ERR_NO_OBJ)
5296 if (ncommit_args > 0)
5304 f1 = got_opentemp();
5306 error = got_error_from_errno("got_opentemp");
5310 f2 = got_opentemp();
5312 error = got_error_from_errno("got_opentemp");
5316 outfile = got_opentemp();
5317 if (outfile == NULL) {
5318 error = got_error_from_errno("got_opentemp");
5322 if (ncommit_args == 0 && (ids[0] == NULL || ids[1] == NULL)) {
5323 struct print_diff_arg arg;
5326 if (worktree == NULL) {
5327 if (argc == 2 && ids[0] == NULL) {
5328 error = got_error_path(argv[0], GOT_ERR_NO_OBJ);
5330 } else if (argc == 2 && ids[1] == NULL) {
5331 error = got_error_path(argv[1], GOT_ERR_NO_OBJ);
5333 } else if (argc > 0) {
5334 error = got_error_fmt(GOT_ERR_NOT_WORKTREE,
5335 "%s", "specified paths cannot be resolved");
5338 error = got_error(GOT_ERR_NOT_WORKTREE);
5343 error = get_worktree_paths_from_argv(&paths, argc, argv,
5348 error = got_object_id_str(&id_str,
5349 got_worktree_get_base_commit_id(worktree));
5353 arg.worktree = worktree;
5354 arg.diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
5355 arg.diff_context = diff_context;
5356 arg.id_str = id_str;
5357 arg.header_shown = 0;
5358 arg.diff_staged = diff_staged;
5359 arg.ignore_whitespace = ignore_whitespace;
5360 arg.force_text_diff = force_text_diff;
5361 arg.diffstat = show_diffstat ? &dsa : NULL;
5364 arg.outfile = outfile;
5366 error = got_worktree_status(worktree, &paths, repo, 0,
5367 print_diff, &arg, check_cancelled, NULL);
5372 if (show_diffstat && dsa.nfiles > 0) {
5375 if (asprintf(&header, "diffstat %s%s",
5376 diff_staged ? "-s " : "",
5377 got_worktree_get_root_path(worktree)) == -1) {
5378 error = got_error_from_errno("asprintf");
5382 error = print_diffstat(&dsa, header);
5388 error = printfile(outfile);
5392 if (ncommit_args == 1) {
5393 struct got_commit_object *commit;
5394 error = got_object_open_as_commit(&commit, repo, ids[0]);
5398 labels[1] = labels[0];
5400 if (got_object_commit_get_nparents(commit) > 0) {
5401 const struct got_object_id_queue *pids;
5402 struct got_object_qid *pid;
5403 pids = got_object_commit_get_parent_ids(commit);
5404 pid = STAILQ_FIRST(pids);
5405 ids[0] = got_object_id_dup(&pid->id);
5406 if (ids[0] == NULL) {
5407 error = got_error_from_errno(
5408 "got_object_id_dup");
5409 got_object_commit_close(commit);
5412 error = got_object_id_str(&labels[0], ids[0]);
5414 got_object_commit_close(commit);
5419 labels[0] = strdup("/dev/null");
5420 if (labels[0] == NULL) {
5421 error = got_error_from_errno("strdup");
5422 got_object_commit_close(commit);
5427 got_object_commit_close(commit);
5430 if (ncommit_args == 0 && argc > 2) {
5431 error = got_error_msg(GOT_ERR_BAD_PATH,
5432 "path arguments cannot be used when diffing two objects");
5437 error = got_object_get_type(&type1, repo, ids[0]);
5442 error = got_object_get_type(&type2, repo, ids[1]);
5445 if (type1 != GOT_OBJ_TYPE_ANY && type1 != type2) {
5446 error = got_error(GOT_ERR_OBJ_TYPE);
5449 if (type1 == GOT_OBJ_TYPE_BLOB && argc > 2) {
5450 error = got_error_msg(GOT_ERR_OBJ_TYPE,
5451 "path arguments cannot be used when diffing blobs");
5455 for (i = 0; ncommit_args > 0 && i < argc; i++) {
5457 struct got_pathlist_entry *new;
5461 error = got_worktree_resolve_path(&p, worktree,
5465 prefix = got_worktree_get_path_prefix(worktree);
5466 while (prefix[0] == '/')
5468 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5469 (p[0] != '\0' && prefix[0] != '\0') ? "/" : "",
5471 error = got_error_from_errno("asprintf");
5477 char *mapped_path, *s;
5478 error = got_repo_map_path(&mapped_path, repo, argv[i]);
5484 in_repo_path = strdup(s);
5485 if (in_repo_path == NULL) {
5486 error = got_error_from_errno("asprintf");
5493 error = got_pathlist_insert(&new, &paths, in_repo_path, NULL);
5494 if (error || new == NULL /* duplicate */)
5501 /* Release work tree lock. */
5502 got_worktree_close(worktree);
5506 fd1 = got_opentempfd();
5508 error = got_error_from_errno("got_opentempfd");
5512 fd2 = got_opentempfd();
5514 error = got_error_from_errno("got_opentempfd");
5518 switch (type1 == GOT_OBJ_TYPE_ANY ? type2 : type1) {
5519 case GOT_OBJ_TYPE_BLOB:
5520 error = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5521 fd1, fd2, ids[0], ids[1], NULL, NULL,
5522 GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
5523 ignore_whitespace, force_text_diff,
5524 show_diffstat ? &dsa : NULL, repo, outfile);
5526 case GOT_OBJ_TYPE_TREE:
5527 error = got_diff_objects_as_trees(NULL, NULL, f1, f2, fd1, fd2,
5528 ids[0], ids[1], &paths, "", "",
5529 GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
5530 ignore_whitespace, force_text_diff,
5531 show_diffstat ? &dsa : NULL, repo, outfile);
5533 case GOT_OBJ_TYPE_COMMIT:
5534 fprintf(outfile, "diff %s %s\n", labels[0], labels[1]);
5535 error = got_diff_objects_as_commits(NULL, NULL, f1, f2,
5536 fd1, fd2, ids[0], ids[1], &paths,
5537 GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
5538 ignore_whitespace, force_text_diff,
5539 show_diffstat ? &dsa : NULL, repo, outfile);
5542 error = got_error(GOT_ERR_OBJ_TYPE);
5547 if (show_diffstat && dsa.nfiles > 0) {
5548 char *header = NULL;
5550 if (asprintf(&header, "diffstat %s %s",
5551 labels[0], labels[1]) == -1) {
5552 error = got_error_from_errno("asprintf");
5556 error = print_diffstat(&dsa, header);
5562 error = printfile(outfile);
5570 got_worktree_close(worktree);
5572 const struct got_error *close_err = got_repo_close(repo);
5577 const struct got_error *pack_err =
5578 got_repo_pack_fds_close(pack_fds);
5582 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
5583 got_pathlist_free(&diffstat_paths, GOT_PATHLIST_FREE_ALL);
5584 got_ref_list_free(&refs);
5585 if (outfile && fclose(outfile) == EOF && error == NULL)
5586 error = got_error_from_errno("fclose");
5587 if (f1 && fclose(f1) == EOF && error == NULL)
5588 error = got_error_from_errno("fclose");
5589 if (f2 && fclose(f2) == EOF && error == NULL)
5590 error = got_error_from_errno("fclose");
5591 if (fd1 != -1 && close(fd1) == -1 && error == NULL)
5592 error = got_error_from_errno("close");
5593 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
5594 error = got_error_from_errno("close");
5602 "usage: %s blame [-c commit] [-r repository-path] path\n",
5611 char datebuf[11]; /* YYYY-MM-DD + NUL */
5614 struct blame_cb_args {
5615 struct blame_line *lines;
5619 off_t *line_offsets;
5621 struct got_repository *repo;
5624 static const struct got_error *
5625 blame_cb(void *arg, int nlines, int lineno,
5626 struct got_commit_object *commit, struct got_object_id *id)
5628 const struct got_error *err = NULL;
5629 struct blame_cb_args *a = arg;
5630 struct blame_line *bline;
5632 size_t linesize = 0;
5635 time_t committer_time;
5637 if (nlines != a->nlines ||
5638 (lineno != -1 && lineno < 1) || lineno > a->nlines)
5639 return got_error(GOT_ERR_RANGE);
5641 if (sigint_received)
5642 return got_error(GOT_ERR_ITER_COMPLETED);
5645 return NULL; /* no change in this commit */
5647 /* Annotate this line. */
5648 bline = &a->lines[lineno - 1];
5649 if (bline->annotated)
5651 err = got_object_id_str(&bline->id_str, id);
5655 bline->committer = strdup(got_object_commit_get_committer(commit));
5656 if (bline->committer == NULL) {
5657 err = got_error_from_errno("strdup");
5661 committer_time = got_object_commit_get_committer_time(commit);
5662 if (gmtime_r(&committer_time, &tm) == NULL)
5663 return got_error_from_errno("gmtime_r");
5664 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
5666 err = got_error(GOT_ERR_NO_SPACE);
5669 bline->annotated = 1;
5671 /* Print lines annotated so far. */
5672 bline = &a->lines[a->lineno_cur - 1];
5673 if (!bline->annotated)
5676 offset = a->line_offsets[a->lineno_cur - 1];
5677 if (fseeko(a->f, offset, SEEK_SET) == -1) {
5678 err = got_error_from_errno("fseeko");
5682 while (a->lineno_cur <= a->nlines && bline->annotated) {
5683 char *smallerthan, *at, *nl, *committer;
5686 if (getline(&line, &linesize, a->f) == -1) {
5688 err = got_error_from_errno("getline");
5692 committer = bline->committer;
5693 smallerthan = strchr(committer, '<');
5694 if (smallerthan && smallerthan[1] != '\0')
5695 committer = smallerthan + 1;
5696 at = strchr(committer, '@');
5699 len = strlen(committer);
5701 committer[8] = '\0';
5703 nl = strchr(line, '\n');
5706 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
5707 bline->id_str, bline->datebuf, committer, line);
5710 bline = &a->lines[a->lineno_cur - 1];
5717 static const struct got_error *
5718 cmd_blame(int argc, char *argv[])
5720 const struct got_error *error;
5721 struct got_repository *repo = NULL;
5722 struct got_worktree *worktree = NULL;
5723 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5724 char *link_target = NULL;
5725 struct got_object_id *obj_id = NULL;
5726 struct got_object_id *commit_id = NULL;
5727 struct got_commit_object *commit = NULL;
5728 struct got_blob_object *blob = NULL;
5729 char *commit_id_str = NULL, *keyword_idstr = NULL;
5730 struct blame_cb_args bca;
5731 int ch, obj_type, i, fd1 = -1, fd2 = -1, fd3 = -1;
5733 int *pack_fds = NULL;
5734 FILE *f1 = NULL, *f2 = NULL;
5736 fd1 = got_opentempfd();
5738 return got_error_from_errno("got_opentempfd");
5740 memset(&bca, 0, sizeof(bca));
5743 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5748 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5751 commit_id_str = optarg;
5754 repo_path = realpath(optarg, NULL);
5755 if (repo_path == NULL)
5756 return got_error_from_errno2("realpath",
5758 got_path_strip_trailing_slashes(repo_path);
5774 cwd = getcwd(NULL, 0);
5776 error = got_error_from_errno("getcwd");
5780 error = got_repo_pack_fds_open(&pack_fds);
5784 if (repo_path == NULL) {
5785 error = got_worktree_open(&worktree, cwd,
5786 GOT_WORKTREE_GOT_DIR);
5787 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5793 strdup(got_worktree_get_repo_path(worktree));
5794 if (repo_path == NULL) {
5795 error = got_error_from_errno("strdup");
5800 repo_path = strdup(cwd);
5801 if (repo_path == NULL) {
5802 error = got_error_from_errno("strdup");
5808 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5813 const char *prefix = got_worktree_get_path_prefix(worktree);
5816 error = got_worktree_resolve_path(&p, worktree, path);
5819 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5820 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
5822 error = got_error_from_errno("asprintf");
5827 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5829 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5832 error = got_repo_map_path(&in_repo_path, repo, path);
5837 if (commit_id_str == NULL) {
5838 struct got_reference *head_ref;
5839 error = got_ref_open(&head_ref, repo, worktree ?
5840 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5843 error = got_ref_resolve(&commit_id, repo, head_ref);
5844 got_ref_close(head_ref);
5848 struct got_reflist_head refs;
5851 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5856 error = got_keyword_to_idstr(&keyword_idstr, commit_id_str,
5860 if (keyword_idstr != NULL)
5861 commit_id_str = keyword_idstr;
5863 error = got_repo_match_object_id(&commit_id, NULL,
5864 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5865 got_ref_list_free(&refs);
5871 /* Release work tree lock. */
5872 got_worktree_close(worktree);
5876 error = got_object_open_as_commit(&commit, repo, commit_id);
5880 error = got_object_resolve_symlinks(&link_target, in_repo_path,
5885 error = got_object_id_by_path(&obj_id, repo, commit,
5886 link_target ? link_target : in_repo_path);
5890 error = got_object_get_type(&obj_type, repo, obj_id);
5894 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5895 error = got_error_path(link_target ? link_target : in_repo_path,
5900 error = got_object_open_as_blob(&blob, repo, obj_id, 8192, fd1);
5903 bca.f = got_opentemp();
5904 if (bca.f == NULL) {
5905 error = got_error_from_errno("got_opentemp");
5908 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
5909 &bca.line_offsets, bca.f, blob);
5910 if (error || bca.nlines == 0)
5913 /* Don't include \n at EOF in the blame line count. */
5914 if (bca.line_offsets[bca.nlines - 1] == filesize)
5917 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
5918 if (bca.lines == NULL) {
5919 error = got_error_from_errno("calloc");
5923 bca.nlines_prec = 0;
5931 fd2 = got_opentempfd();
5933 error = got_error_from_errno("got_opentempfd");
5936 fd3 = got_opentempfd();
5938 error = got_error_from_errno("got_opentempfd");
5941 f1 = got_opentemp();
5943 error = got_error_from_errno("got_opentemp");
5946 f2 = got_opentemp();
5948 error = got_error_from_errno("got_opentemp");
5951 error = got_blame(link_target ? link_target : in_repo_path, commit_id,
5952 repo, GOT_DIFF_ALGORITHM_PATIENCE, blame_cb, &bca,
5953 check_cancelled, NULL, fd2, fd3, f1, f2);
5955 free(keyword_idstr);
5963 got_object_commit_close(commit);
5965 if (fd1 != -1 && close(fd1) == -1 && error == NULL)
5966 error = got_error_from_errno("close");
5967 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
5968 error = got_error_from_errno("close");
5969 if (fd3 != -1 && close(fd3) == -1 && error == NULL)
5970 error = got_error_from_errno("close");
5971 if (f1 && fclose(f1) == EOF && error == NULL)
5972 error = got_error_from_errno("fclose");
5973 if (f2 && fclose(f2) == EOF && error == NULL)
5974 error = got_error_from_errno("fclose");
5977 got_object_blob_close(blob);
5979 got_worktree_close(worktree);
5981 const struct got_error *close_err = got_repo_close(repo);
5986 const struct got_error *pack_err =
5987 got_repo_pack_fds_close(pack_fds);
5992 for (i = 0; i < bca.nlines; i++) {
5993 struct blame_line *bline = &bca.lines[i];
5994 free(bline->id_str);
5995 free(bline->committer);
5999 free(bca.line_offsets);
6000 if (bca.f && fclose(bca.f) == EOF && error == NULL)
6001 error = got_error_from_errno("fclose");
6008 fprintf(stderr, "usage: %s tree [-iR] [-c commit] [-r repository-path] "
6009 "[path]\n", getprogname());
6013 static const struct got_error *
6014 print_entry(struct got_tree_entry *te, const char *id, const char *path,
6015 const char *root_path, struct got_repository *repo)
6017 const struct got_error *err = NULL;
6018 int is_root_path = (strcmp(path, root_path) == 0);
6019 const char *modestr = "";
6020 mode_t mode = got_tree_entry_get_mode(te);
6021 char *link_target = NULL;
6023 path += strlen(root_path);
6024 while (path[0] == '/')
6027 if (got_object_tree_entry_is_submodule(te))
6029 else if (S_ISLNK(mode)) {
6032 err = got_tree_entry_get_symlink_target(&link_target, te, repo);
6035 for (i = 0; link_target[i] != '\0'; i++) {
6036 if (!isprint((unsigned char)link_target[i]))
6037 link_target[i] = '?';
6042 else if (S_ISDIR(mode))
6044 else if (mode & S_IXUSR)
6047 printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
6048 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
6049 link_target ? " -> ": "", link_target ? link_target : "");
6055 static const struct got_error *
6056 print_tree(const char *path, struct got_commit_object *commit,
6057 int show_ids, int recurse, const char *root_path,
6058 struct got_repository *repo)
6060 const struct got_error *err = NULL;
6061 struct got_object_id *tree_id = NULL;
6062 struct got_tree_object *tree = NULL;
6065 err = got_object_id_by_path(&tree_id, repo, commit, path);
6069 err = got_object_open_as_tree(&tree, repo, tree_id);
6072 nentries = got_object_tree_get_nentries(tree);
6073 for (i = 0; i < nentries; i++) {
6074 struct got_tree_entry *te;
6077 if (sigint_received || sigpipe_received)
6080 te = got_object_tree_get_entry(tree, i);
6083 err = got_object_id_str(&id_str,
6084 got_tree_entry_get_id(te));
6087 if (asprintf(&id, "%s ", id_str) == -1) {
6088 err = got_error_from_errno("asprintf");
6094 err = print_entry(te, id, path, root_path, repo);
6099 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
6101 if (asprintf(&child_path, "%s%s%s", path,
6102 path[0] == '/' && path[1] == '\0' ? "" : "/",
6103 got_tree_entry_get_name(te)) == -1) {
6104 err = got_error_from_errno("asprintf");
6107 err = print_tree(child_path, commit, show_ids, 1,
6116 got_object_tree_close(tree);
6121 static const struct got_error *
6122 cmd_tree(int argc, char *argv[])
6124 const struct got_error *error;
6125 struct got_repository *repo = NULL;
6126 struct got_worktree *worktree = NULL;
6127 const char *path, *refname = NULL;
6128 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6129 struct got_object_id *commit_id = NULL;
6130 struct got_commit_object *commit = NULL;
6131 char *commit_id_str = NULL, *keyword_idstr = NULL;
6132 int show_ids = 0, recurse = 0;
6134 int *pack_fds = NULL;
6137 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6142 while ((ch = getopt(argc, argv, "c:iRr:")) != -1) {
6145 commit_id_str = optarg;
6154 repo_path = realpath(optarg, NULL);
6155 if (repo_path == NULL)
6156 return got_error_from_errno2("realpath",
6158 got_path_strip_trailing_slashes(repo_path);
6176 cwd = getcwd(NULL, 0);
6178 error = got_error_from_errno("getcwd");
6182 error = got_repo_pack_fds_open(&pack_fds);
6186 if (repo_path == NULL) {
6187 error = got_worktree_open(&worktree, cwd,
6188 GOT_WORKTREE_GOT_DIR);
6189 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6195 strdup(got_worktree_get_repo_path(worktree));
6196 if (repo_path == NULL)
6197 error = got_error_from_errno("strdup");
6201 repo_path = strdup(cwd);
6202 if (repo_path == NULL) {
6203 error = got_error_from_errno("strdup");
6209 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6214 const char *prefix = got_worktree_get_path_prefix(worktree);
6217 if (path == NULL || got_path_is_root_dir(path))
6219 error = got_worktree_resolve_path(&p, worktree, path);
6222 if (asprintf(&in_repo_path, "%s%s%s", prefix,
6223 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
6225 error = got_error_from_errno("asprintf");
6230 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6234 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6239 error = got_repo_map_path(&in_repo_path, repo, path);
6244 if (commit_id_str == NULL) {
6245 struct got_reference *head_ref;
6247 refname = got_worktree_get_head_ref_name(worktree);
6249 refname = GOT_REF_HEAD;
6250 error = got_ref_open(&head_ref, repo, refname, 0);
6253 error = got_ref_resolve(&commit_id, repo, head_ref);
6254 got_ref_close(head_ref);
6258 struct got_reflist_head refs;
6261 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
6266 error = got_keyword_to_idstr(&keyword_idstr, commit_id_str,
6270 if (keyword_idstr != NULL)
6271 commit_id_str = keyword_idstr;
6273 error = got_repo_match_object_id(&commit_id, NULL,
6274 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
6275 got_ref_list_free(&refs);
6281 /* Release work tree lock. */
6282 got_worktree_close(worktree);
6286 error = got_object_open_as_commit(&commit, repo, commit_id);
6290 error = print_tree(in_repo_path, commit, show_ids, recurse,
6291 in_repo_path, repo);
6293 free(keyword_idstr);
6299 got_object_commit_close(commit);
6301 got_worktree_close(worktree);
6303 const struct got_error *close_err = got_repo_close(repo);
6308 const struct got_error *pack_err =
6309 got_repo_pack_fds_close(pack_fds);
6319 fprintf(stderr, "usage: %s status [-I] [-S status-codes] "
6320 "[-s status-codes] [path ...]\n", getprogname());
6324 struct got_status_arg {
6329 static const struct got_error *
6330 print_status(void *arg, unsigned char status, unsigned char staged_status,
6331 const char *path, struct got_object_id *blob_id,
6332 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6333 int dirfd, const char *de_name)
6335 struct got_status_arg *st = arg;
6337 if (status == staged_status && (status == GOT_STATUS_DELETE))
6338 status = GOT_STATUS_NO_CHANGE;
6339 if (st != NULL && st->status_codes) {
6340 size_t ncodes = strlen(st->status_codes);
6343 for (i = 0; i < ncodes ; i++) {
6345 if (status == st->status_codes[i] ||
6346 staged_status == st->status_codes[i]) {
6351 if (status == st->status_codes[i] ||
6352 staged_status == st->status_codes[i])
6357 if (st->suppress && j == 0)
6364 printf("%c%c %s\n", status, staged_status, path);
6368 static const struct got_error *
6369 cmd_status(int argc, char *argv[])
6371 const struct got_error *error = NULL;
6372 struct got_repository *repo = NULL;
6373 struct got_worktree *worktree = NULL;
6374 struct got_status_arg st;
6376 struct got_pathlist_head paths;
6377 int ch, i, no_ignores = 0;
6378 int *pack_fds = NULL;
6382 memset(&st, 0, sizeof(st));
6383 st.status_codes = NULL;
6387 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6392 while ((ch = getopt(argc, argv, "IS:s:")) != -1) {
6398 if (st.status_codes != NULL && st.suppress == 0)
6399 option_conflict('S', 's');
6403 for (i = 0; optarg[i] != '\0'; i++) {
6404 switch (optarg[i]) {
6405 case GOT_STATUS_MODIFY:
6406 case GOT_STATUS_ADD:
6407 case GOT_STATUS_DELETE:
6408 case GOT_STATUS_CONFLICT:
6409 case GOT_STATUS_MISSING:
6410 case GOT_STATUS_OBSTRUCTED:
6411 case GOT_STATUS_UNVERSIONED:
6412 case GOT_STATUS_MODE_CHANGE:
6413 case GOT_STATUS_NONEXISTENT:
6416 errx(1, "invalid status code '%c'",
6420 if (ch == 's' && st.suppress)
6421 option_conflict('s', 'S');
6422 st.status_codes = optarg;
6433 cwd = getcwd(NULL, 0);
6435 error = got_error_from_errno("getcwd");
6439 error = got_repo_pack_fds_open(&pack_fds);
6443 error = got_worktree_open(&worktree, cwd,
6444 GOT_WORKTREE_GOT_DIR);
6446 if (error->code == GOT_ERR_NOT_WORKTREE)
6447 error = wrap_not_worktree_error(error, "status", cwd);
6451 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6456 error = apply_unveil(got_repo_get_path(repo), 1,
6457 got_worktree_get_root_path(worktree));
6461 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6465 error = got_worktree_status(worktree, &paths, repo, no_ignores,
6466 print_status, &st, check_cancelled, NULL);
6469 const struct got_error *pack_err =
6470 got_repo_pack_fds_close(pack_fds);
6475 const struct got_error *close_err = got_repo_close(repo);
6480 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
6488 fprintf(stderr, "usage: %s ref [-dlt] [-c object] [-r repository-path] "
6489 "[-s reference] [name]\n", getprogname());
6493 static const struct got_error *
6494 list_refs(struct got_repository *repo, const char *refname, int sort_by_time)
6496 static const struct got_error *err = NULL;
6497 struct got_reflist_head refs;
6498 struct got_reflist_entry *re;
6501 err = got_ref_list(&refs, repo, refname, sort_by_time ?
6502 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6507 TAILQ_FOREACH(re, &refs, entry) {
6509 refstr = got_ref_to_str(re->ref);
6510 if (refstr == NULL) {
6511 err = got_error_from_errno("got_ref_to_str");
6514 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
6518 got_ref_list_free(&refs);
6522 static const struct got_error *
6523 delete_ref_by_name(struct got_repository *repo, const char *refname)
6525 const struct got_error *err;
6526 struct got_reference *ref;
6528 err = got_ref_open(&ref, repo, refname, 0);
6532 err = delete_ref(repo, ref);
6537 static const struct got_error *
6538 add_ref(struct got_repository *repo, const char *refname, const char *target)
6540 const struct got_error *err = NULL;
6541 struct got_object_id *id = NULL;
6542 struct got_reference *ref = NULL;
6543 struct got_reflist_head refs;
6546 * Don't let the user create a reference name with a leading '-'.
6547 * While technically a valid reference name, this case is usually
6548 * an unintended typo.
6550 if (refname[0] == '-')
6551 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
6554 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
6557 err = got_repo_match_object_id(&id, NULL, target, GOT_OBJ_TYPE_ANY,
6559 got_ref_list_free(&refs);
6563 err = got_ref_alloc(&ref, refname, id);
6567 err = got_ref_write(ref, repo);
6575 static const struct got_error *
6576 add_symref(struct got_repository *repo, const char *refname, const char *target)
6578 const struct got_error *err = NULL;
6579 struct got_reference *ref = NULL;
6580 struct got_reference *target_ref = NULL;
6583 * Don't let the user create a reference name with a leading '-'.
6584 * While technically a valid reference name, this case is usually
6585 * an unintended typo.
6587 if (refname[0] == '-')
6588 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
6590 err = got_ref_open(&target_ref, repo, target, 0);
6594 err = got_ref_alloc_symref(&ref, refname, target_ref);
6598 err = got_ref_write(ref, repo);
6601 got_ref_close(target_ref);
6607 static const struct got_error *
6608 cmd_ref(int argc, char *argv[])
6610 const struct got_error *error = NULL;
6611 struct got_repository *repo = NULL;
6612 struct got_worktree *worktree = NULL;
6613 char *cwd = NULL, *repo_path = NULL;
6614 int ch, do_list = 0, do_delete = 0, sort_by_time = 0;
6615 const char *obj_arg = NULL, *symref_target= NULL;
6616 char *refname = NULL, *keyword_idstr = NULL;
6617 int *pack_fds = NULL;
6620 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6621 "sendfd unveil", NULL) == -1)
6625 while ((ch = getopt(argc, argv, "c:dlr:s:t")) != -1) {
6637 repo_path = realpath(optarg, NULL);
6638 if (repo_path == NULL)
6639 return got_error_from_errno2("realpath",
6641 got_path_strip_trailing_slashes(repo_path);
6644 symref_target = optarg;
6655 if (obj_arg && do_list)
6656 option_conflict('c', 'l');
6657 if (obj_arg && do_delete)
6658 option_conflict('c', 'd');
6659 if (obj_arg && symref_target)
6660 option_conflict('c', 's');
6661 if (symref_target && do_delete)
6662 option_conflict('s', 'd');
6663 if (symref_target && do_list)
6664 option_conflict('s', 'l');
6665 if (do_delete && do_list)
6666 option_conflict('d', 'l');
6667 if (sort_by_time && !do_list)
6668 errx(1, "-t option requires -l option");
6674 if (argc != 0 && argc != 1)
6677 refname = strdup(argv[0]);
6678 if (refname == NULL) {
6679 error = got_error_from_errno("strdup");
6686 refname = strdup(argv[0]);
6687 if (refname == NULL) {
6688 error = got_error_from_errno("strdup");
6694 got_path_strip_trailing_slashes(refname);
6696 cwd = getcwd(NULL, 0);
6698 error = got_error_from_errno("getcwd");
6702 error = got_repo_pack_fds_open(&pack_fds);
6706 if (repo_path == NULL) {
6707 error = got_worktree_open(&worktree, cwd,
6708 GOT_WORKTREE_GOT_DIR);
6709 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6715 strdup(got_worktree_get_repo_path(worktree));
6716 if (repo_path == NULL)
6717 error = got_error_from_errno("strdup");
6721 repo_path = strdup(cwd);
6722 if (repo_path == NULL) {
6723 error = got_error_from_errno("strdup");
6729 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6735 /* Remove "cpath" promise. */
6736 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6742 error = apply_unveil(got_repo_get_path(repo), do_list,
6743 worktree ? got_worktree_get_root_path(worktree) : NULL);
6748 error = list_refs(repo, refname, sort_by_time);
6750 error = delete_ref_by_name(repo, refname);
6751 else if (symref_target)
6752 error = add_symref(repo, refname, symref_target);
6754 if (obj_arg == NULL)
6757 error = got_keyword_to_idstr(&keyword_idstr, obj_arg,
6761 if (keyword_idstr != NULL)
6762 obj_arg = keyword_idstr;
6764 error = add_ref(repo, refname, obj_arg);
6769 const struct got_error *close_err = got_repo_close(repo);
6774 got_worktree_close(worktree);
6776 const struct got_error *pack_err =
6777 got_repo_pack_fds_close(pack_fds);
6783 free(keyword_idstr);
6790 fprintf(stderr, "usage: %s branch [-lnt] [-c commit] [-d name] "
6791 "[-r repository-path] [name]\n", getprogname());
6795 static const struct got_error *
6796 list_branch(struct got_repository *repo, struct got_worktree *worktree,
6797 struct got_reference *ref)
6799 const struct got_error *err = NULL;
6800 const char *refname;
6804 refname = got_ref_get_name(ref);
6805 if (worktree && strcmp(refname,
6806 got_worktree_get_head_ref_name(worktree)) == 0) {
6807 err = got_worktree_get_state(&marker, repo, worktree);
6812 if (strncmp(refname, "refs/heads/", 11) == 0)
6814 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
6816 if (strncmp(refname, "refs/remotes/", 13) == 0)
6819 refstr = got_ref_to_str(ref);
6821 return got_error_from_errno("got_ref_to_str");
6823 printf("%c %s: %s\n", marker, refname, refstr);
6828 static const struct got_error *
6829 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
6831 const char *refname;
6833 if (worktree == NULL)
6834 return got_error(GOT_ERR_NOT_WORKTREE);
6836 refname = got_worktree_get_head_ref_name(worktree);
6838 if (strncmp(refname, "refs/heads/", 11) == 0)
6840 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
6843 printf("%s\n", refname);
6848 static const struct got_error *
6849 list_branches(struct got_repository *repo, struct got_worktree *worktree,
6852 static const struct got_error *err = NULL;
6853 struct got_reflist_head refs;
6854 struct got_reflist_entry *re;
6855 struct got_reference *temp_ref = NULL;
6856 int rebase_in_progress, histedit_in_progress;
6861 err = got_worktree_rebase_in_progress(&rebase_in_progress,
6866 err = got_worktree_histedit_in_progress(&histedit_in_progress,
6871 if (rebase_in_progress || histedit_in_progress) {
6872 err = got_ref_open(&temp_ref, repo,
6873 got_worktree_get_head_ref_name(worktree), 0);
6876 list_branch(repo, worktree, temp_ref);
6877 got_ref_close(temp_ref);
6881 err = got_ref_list(&refs, repo, "refs/heads", sort_by_time ?
6882 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6887 TAILQ_FOREACH(re, &refs, entry)
6888 list_branch(repo, worktree, re->ref);
6890 got_ref_list_free(&refs);
6892 err = got_ref_list(&refs, repo, "refs/remotes", sort_by_time ?
6893 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6898 TAILQ_FOREACH(re, &refs, entry)
6899 list_branch(repo, worktree, re->ref);
6901 got_ref_list_free(&refs);
6906 static const struct got_error *
6907 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
6908 const char *branch_name)
6910 const struct got_error *err = NULL;
6911 struct got_reference *ref = NULL;
6912 char *refname, *remote_refname = NULL;
6914 if (strncmp(branch_name, "refs/", 5) == 0)
6916 if (strncmp(branch_name, "heads/", 6) == 0)
6918 else if (strncmp(branch_name, "remotes/", 8) == 0)
6921 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
6922 return got_error_from_errno("asprintf");
6924 if (asprintf(&remote_refname, "refs/remotes/%s",
6925 branch_name) == -1) {
6926 err = got_error_from_errno("asprintf");
6930 err = got_ref_open(&ref, repo, refname, 0);
6932 const struct got_error *err2;
6933 if (err->code != GOT_ERR_NOT_REF)
6936 * Keep 'err' intact such that if neither branch exists
6937 * we report "refs/heads" rather than "refs/remotes" in
6938 * our error message.
6940 err2 = got_ref_open(&ref, repo, remote_refname, 0);
6947 strcmp(got_worktree_get_head_ref_name(worktree),
6948 got_ref_get_name(ref)) == 0) {
6949 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6950 "will not delete this work tree's current branch");
6954 err = delete_ref(repo, ref);
6959 free(remote_refname);
6963 static const struct got_error *
6964 add_branch(struct got_repository *repo, const char *branch_name,
6965 struct got_object_id *base_commit_id)
6967 const struct got_error *err = NULL;
6968 struct got_reference *ref = NULL;
6969 char *refname = NULL;
6972 * Don't let the user create a branch name with a leading '-'.
6973 * While technically a valid reference name, this case is usually
6974 * an unintended typo.
6976 if (branch_name[0] == '-')
6977 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
6979 if (strncmp(branch_name, "refs/heads/", 11) == 0)
6982 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
6983 err = got_error_from_errno("asprintf");
6987 err = got_ref_open(&ref, repo, refname, 0);
6989 err = got_error(GOT_ERR_BRANCH_EXISTS);
6991 } else if (err->code != GOT_ERR_NOT_REF)
6994 err = got_ref_alloc(&ref, refname, base_commit_id);
6998 err = got_ref_write(ref, repo);
7006 static const struct got_error *
7007 cmd_branch(int argc, char *argv[])
7009 const struct got_error *error = NULL;
7010 struct got_repository *repo = NULL;
7011 struct got_worktree *worktree = NULL;
7012 char *cwd = NULL, *repo_path = NULL;
7013 int ch, do_list = 0, do_show = 0, do_update = 1, sort_by_time = 0;
7014 const char *delref = NULL, *commit_id_arg = NULL;
7015 struct got_reference *ref = NULL;
7016 struct got_pathlist_head paths;
7017 struct got_object_id *commit_id = NULL;
7018 char *commit_id_str = NULL, *keyword_idstr = NULL;;
7019 int *pack_fds = NULL;
7024 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
7025 "sendfd unveil", NULL) == -1)
7029 while ((ch = getopt(argc, argv, "c:d:lnr:t")) != -1) {
7032 commit_id_arg = optarg;
7044 repo_path = realpath(optarg, NULL);
7045 if (repo_path == NULL)
7046 return got_error_from_errno2("realpath",
7048 got_path_strip_trailing_slashes(repo_path);
7059 if (do_list && delref)
7060 option_conflict('l', 'd');
7061 if (sort_by_time && !do_list)
7062 errx(1, "-t option requires -l option");
7067 if (!do_list && !delref && argc == 0)
7070 if ((do_list || delref || do_show) && commit_id_arg != NULL)
7071 errx(1, "-c option can only be used when creating a branch");
7073 if (do_list || delref) {
7076 } else if (!do_show && argc != 1)
7079 cwd = getcwd(NULL, 0);
7081 error = got_error_from_errno("getcwd");
7085 error = got_repo_pack_fds_open(&pack_fds);
7089 if (repo_path == NULL) {
7090 error = got_worktree_open(&worktree, cwd,
7091 GOT_WORKTREE_GOT_DIR);
7092 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7098 strdup(got_worktree_get_repo_path(worktree));
7099 if (repo_path == NULL)
7100 error = got_error_from_errno("strdup");
7104 repo_path = strdup(cwd);
7105 if (repo_path == NULL) {
7106 error = got_error_from_errno("strdup");
7112 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7117 if (do_list || do_show) {
7118 /* Remove "cpath" promise. */
7119 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
7125 error = apply_unveil(got_repo_get_path(repo), do_list,
7126 worktree ? got_worktree_get_root_path(worktree) : NULL);
7131 error = show_current_branch(repo, worktree);
7133 error = list_branches(repo, worktree, sort_by_time);
7135 error = delete_branch(repo, worktree, delref);
7137 struct got_reflist_head refs;
7139 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
7143 if (commit_id_arg == NULL)
7144 commit_id_arg = worktree ?
7145 got_worktree_get_head_ref_name(worktree) :
7148 error = got_keyword_to_idstr(&keyword_idstr,
7149 commit_id_arg, repo, worktree);
7152 if (keyword_idstr != NULL)
7153 commit_id_arg = keyword_idstr;
7155 error = got_repo_match_object_id(&commit_id, NULL,
7156 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &refs, repo);
7157 got_ref_list_free(&refs);
7160 error = add_branch(repo, argv[0], commit_id);
7163 if (worktree && do_update) {
7164 struct got_update_progress_arg upa;
7165 char *branch_refname = NULL;
7167 error = got_object_id_str(&commit_id_str, commit_id);
7170 error = get_worktree_paths_from_argv(&paths, 0, NULL,
7174 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
7176 error = got_error_from_errno("asprintf");
7179 error = got_ref_open(&ref, repo, branch_refname, 0);
7180 free(branch_refname);
7183 error = switch_head_ref(ref, commit_id, worktree,
7187 error = got_worktree_set_base_commit_id(worktree, repo,
7191 memset(&upa, 0, sizeof(upa));
7192 error = got_worktree_checkout_files(worktree, &paths,
7193 repo, update_progress, &upa, check_cancelled,
7197 if (upa.did_something) {
7198 printf("Updated to %s: %s\n",
7199 got_worktree_get_head_ref_name(worktree),
7202 print_update_progress_stats(&upa);
7206 free(keyword_idstr);
7210 const struct got_error *close_err = got_repo_close(repo);
7215 got_worktree_close(worktree);
7217 const struct got_error *pack_err =
7218 got_repo_pack_fds_close(pack_fds);
7225 free(commit_id_str);
7226 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
7234 fprintf(stderr, "usage: %s tag [-lVv] [-c commit] [-m message] "
7235 "[-r repository-path] [-s signer-id] name\n", getprogname());
7240 static const struct got_error *
7241 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
7243 const struct got_error *err = NULL;
7244 struct got_reflist_entry *re, *se, *new;
7245 struct got_object_id *re_id, *se_id;
7246 struct got_tag_object *re_tag, *se_tag;
7247 time_t re_time, se_time;
7249 STAILQ_FOREACH(re, tags, entry) {
7250 se = STAILQ_FIRST(sorted);
7252 err = got_reflist_entry_dup(&new, re);
7255 STAILQ_INSERT_HEAD(sorted, new, entry);
7258 err = got_ref_resolve(&re_id, repo, re->ref);
7261 err = got_object_open_as_tag(&re_tag, repo, re_id);
7265 re_time = got_object_tag_get_tagger_time(re_tag);
7266 got_object_tag_close(re_tag);
7270 err = got_ref_resolve(&se_id, repo, re->ref);
7273 err = got_object_open_as_tag(&se_tag, repo, se_id);
7277 se_time = got_object_tag_get_tagger_time(se_tag);
7278 got_object_tag_close(se_tag);
7280 if (se_time > re_time) {
7281 err = got_reflist_entry_dup(&new, re);
7284 STAILQ_INSERT_AFTER(sorted, se, new, entry);
7287 se = STAILQ_NEXT(se, entry);
7296 static const struct got_error *
7297 get_tag_refname(char **refname, const char *tag_name)
7299 const struct got_error *err;
7301 if (strncmp("refs/tags/", tag_name, 10) == 0) {
7302 *refname = strdup(tag_name);
7303 if (*refname == NULL)
7304 return got_error_from_errno("strdup");
7305 } else if (asprintf(refname, "refs/tags/%s", tag_name) == -1) {
7306 err = got_error_from_errno("asprintf");
7314 static const struct got_error *
7315 list_tags(struct got_repository *repo, const char *tag_name, int verify_tags,
7316 const char *allowed_signers, const char *revoked_signers, int verbosity)
7318 static const struct got_error *err = NULL;
7319 struct got_reflist_head refs;
7320 struct got_reflist_entry *re;
7321 char *wanted_refname = NULL;
7326 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
7331 struct got_reference *ref;
7332 err = get_tag_refname(&wanted_refname, tag_name);
7335 /* Wanted tag reference should exist. */
7336 err = got_ref_open(&ref, repo, wanted_refname, 0);
7342 TAILQ_FOREACH(re, &refs, entry) {
7343 const char *refname;
7344 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
7346 const char *tagger, *ssh_sig = NULL;
7347 char *sig_msg = NULL;
7349 struct got_object_id *id;
7350 struct got_tag_object *tag;
7351 struct got_commit_object *commit = NULL;
7353 refname = got_ref_get_name(re->ref);
7354 if (strncmp(refname, "refs/tags/", 10) != 0 ||
7355 (wanted_refname && strcmp(refname, wanted_refname) != 0))
7358 refstr = got_ref_to_str(re->ref);
7359 if (refstr == NULL) {
7360 err = got_error_from_errno("got_ref_to_str");
7364 err = got_ref_resolve(&id, repo, re->ref);
7367 err = got_object_open_as_tag(&tag, repo, id);
7369 if (err->code != GOT_ERR_OBJ_TYPE) {
7373 /* "lightweight" tag */
7374 err = got_object_open_as_commit(&commit, repo, id);
7379 tagger = got_object_commit_get_committer(commit);
7381 got_object_commit_get_committer_time(commit);
7382 err = got_object_id_str(&id_str, id);
7388 tagger = got_object_tag_get_tagger(tag);
7389 tagger_time = got_object_tag_get_tagger_time(tag);
7390 err = got_object_id_str(&id_str,
7391 got_object_tag_get_object_id(tag));
7396 if (tag && verify_tags) {
7397 ssh_sig = got_sigs_get_tagmsg_ssh_signature(
7398 got_object_tag_get_message(tag));
7399 if (ssh_sig && allowed_signers == NULL) {
7400 err = got_error_msg(
7401 GOT_ERR_VERIFY_TAG_SIGNATURE,
7402 "SSH signature verification requires "
7403 "setting allowed_signers in "
7409 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
7411 printf("from: %s\n", tagger);
7412 datestr = get_datestr(&tagger_time, datebuf);
7414 printf("date: %s UTC\n", datestr);
7416 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
7418 switch (got_object_tag_get_object_type(tag)) {
7419 case GOT_OBJ_TYPE_BLOB:
7420 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
7423 case GOT_OBJ_TYPE_TREE:
7424 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
7427 case GOT_OBJ_TYPE_COMMIT:
7428 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
7431 case GOT_OBJ_TYPE_TAG:
7432 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
7442 err = got_sigs_verify_tag_ssh(&sig_msg, tag, ssh_sig,
7443 allowed_signers, revoked_signers, verbosity);
7444 if (err && err->code == GOT_ERR_BAD_TAG_SIGNATURE)
7448 printf("signature: %s", sig_msg);
7454 err = got_object_commit_get_logmsg(&tagmsg0, commit);
7457 got_object_commit_close(commit);
7459 tagmsg0 = strdup(got_object_tag_get_message(tag));
7460 got_object_tag_close(tag);
7461 if (tagmsg0 == NULL) {
7462 err = got_error_from_errno("strdup");
7469 line = strsep(&tagmsg, "\n");
7471 printf(" %s\n", line);
7476 got_ref_list_free(&refs);
7477 free(wanted_refname);
7479 if (err == NULL && bad_sigs)
7480 err = got_error(GOT_ERR_BAD_TAG_SIGNATURE);
7484 static const struct got_error *
7485 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
7486 const char *tag_name, const char *repo_path)
7488 const struct got_error *err = NULL;
7489 char *template = NULL, *initial_content = NULL;
7490 char *editor = NULL;
7491 int initial_content_len;
7494 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
7495 err = got_error_from_errno("asprintf");
7499 initial_content_len = asprintf(&initial_content,
7500 "\n# tagging commit %s as %s\n",
7501 commit_id_str, tag_name);
7502 if (initial_content_len == -1) {
7503 err = got_error_from_errno("asprintf");
7507 err = got_opentemp_named_fd(tagmsg_path, &fd, template, "");
7511 if (write(fd, initial_content, initial_content_len) == -1) {
7512 err = got_error_from_errno2("write", *tagmsg_path);
7515 if (close(fd) == -1) {
7516 err = got_error_from_errno2("close", *tagmsg_path);
7521 err = get_editor(&editor);
7524 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content,
7525 initial_content_len, 1);
7527 free(initial_content);
7531 if (fd != -1 && close(fd) == -1 && err == NULL)
7532 err = got_error_from_errno2("close", *tagmsg_path);
7541 static const struct got_error *
7542 add_tag(struct got_repository *repo, const char *tagger,
7543 const char *tag_name, const char *commit_arg, const char *tagmsg_arg,
7544 const char *signer_id, int verbosity)
7546 const struct got_error *err = NULL;
7547 struct got_object_id *commit_id = NULL, *tag_id = NULL;
7548 char *label = NULL, *commit_id_str = NULL;
7549 struct got_reference *ref = NULL;
7550 char *refname = NULL, *tagmsg = NULL;
7551 char *tagmsg_path = NULL, *tag_id_str = NULL;
7552 int preserve_tagmsg = 0;
7553 struct got_reflist_head refs;
7558 * Don't let the user create a tag name with a leading '-'.
7559 * While technically a valid reference name, this case is usually
7560 * an unintended typo.
7562 if (tag_name[0] == '-')
7563 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
7565 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
7569 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
7570 GOT_OBJ_TYPE_COMMIT, &refs, repo);
7574 err = got_object_id_str(&commit_id_str, commit_id);
7578 err = get_tag_refname(&refname, tag_name);
7581 if (strncmp("refs/tags/", tag_name, 10) == 0)
7584 err = got_ref_open(&ref, repo, refname, 0);
7586 err = got_error(GOT_ERR_TAG_EXISTS);
7588 } else if (err->code != GOT_ERR_NOT_REF)
7591 if (tagmsg_arg == NULL) {
7592 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
7593 tag_name, got_repo_get_path(repo));
7595 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
7596 tagmsg_path != NULL)
7597 preserve_tagmsg = 1;
7600 /* Editor is done; we can now apply unveil(2) */
7601 err = got_sigs_apply_unveil();
7604 err = apply_unveil(got_repo_get_path(repo), 0, NULL);
7609 err = got_object_tag_create(&tag_id, tag_name, commit_id,
7610 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, signer_id, repo,
7614 preserve_tagmsg = 1;
7618 err = got_ref_alloc(&ref, refname, tag_id);
7621 preserve_tagmsg = 1;
7625 err = got_ref_write(ref, repo);
7628 preserve_tagmsg = 1;
7632 err = got_object_id_str(&tag_id_str, tag_id);
7635 preserve_tagmsg = 1;
7638 printf("Created tag %s\n", tag_id_str);
7640 if (preserve_tagmsg) {
7641 fprintf(stderr, "%s: tag message preserved in %s\n",
7642 getprogname(), tagmsg_path);
7643 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
7644 err = got_error_from_errno2("unlink", tagmsg_path);
7649 free(commit_id_str);
7653 got_ref_list_free(&refs);
7657 static const struct got_error *
7658 cmd_tag(int argc, char *argv[])
7660 const struct got_error *error = NULL;
7661 struct got_repository *repo = NULL;
7662 struct got_worktree *worktree = NULL;
7663 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
7664 char *gitconfig_path = NULL, *tagger = NULL, *keyword_idstr = NULL;
7665 char *allowed_signers = NULL, *revoked_signers = NULL;
7666 const char *signer_id = NULL;
7667 const char *tag_name = NULL, *commit_id_arg = NULL, *tagmsg = NULL;
7668 int ch, do_list = 0, verify_tags = 0, verbosity = 0;
7669 int *pack_fds = NULL;
7672 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
7673 "sendfd unveil", NULL) == -1)
7677 while ((ch = getopt(argc, argv, "c:lm:r:s:Vv")) != -1) {
7680 commit_id_arg = optarg;
7689 repo_path = realpath(optarg, NULL);
7690 if (repo_path == NULL) {
7691 error = got_error_from_errno2("realpath",
7695 got_path_strip_trailing_slashes(repo_path);
7706 else if (verbosity < 3)
7718 if (do_list || verify_tags) {
7719 if (commit_id_arg != NULL)
7721 "-c option can only be used when creating a tag");
7724 option_conflict('l', 'm');
7726 option_conflict('V', 'm');
7730 option_conflict('l', 's');
7732 option_conflict('V', 's');
7736 } else if (argc != 1)
7742 cwd = getcwd(NULL, 0);
7744 error = got_error_from_errno("getcwd");
7748 error = got_repo_pack_fds_open(&pack_fds);
7752 if (repo_path == NULL) {
7753 error = got_worktree_open(&worktree, cwd,
7754 GOT_WORKTREE_GOT_DIR);
7755 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7761 strdup(got_worktree_get_repo_path(worktree));
7762 if (repo_path == NULL)
7763 error = got_error_from_errno("strdup");
7767 repo_path = strdup(cwd);
7768 if (repo_path == NULL) {
7769 error = got_error_from_errno("strdup");
7775 if (do_list || verify_tags) {
7776 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7779 error = get_allowed_signers(&allowed_signers, repo, worktree);
7782 error = get_revoked_signers(&revoked_signers, repo, worktree);
7786 /* Release work tree lock. */
7787 got_worktree_close(worktree);
7792 * Remove "cpath" promise unless needed for signature tmpfile
7796 got_sigs_apply_unveil();
7799 if (pledge("stdio rpath wpath flock proc exec sendfd "
7800 "unveil", NULL) == -1)
7804 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7807 error = list_tags(repo, tag_name, verify_tags, allowed_signers,
7808 revoked_signers, verbosity);
7810 error = get_gitconfig_path(&gitconfig_path);
7813 error = got_repo_open(&repo, repo_path, gitconfig_path,
7818 error = get_author(&tagger, repo, worktree);
7821 if (signer_id == NULL)
7822 signer_id = get_signer_id(repo, worktree);
7826 error = got_sigs_apply_unveil();
7830 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
7835 if (commit_id_arg == NULL) {
7836 struct got_reference *head_ref;
7837 struct got_object_id *commit_id;
7838 error = got_ref_open(&head_ref, repo,
7839 worktree ? got_worktree_get_head_ref_name(worktree)
7843 error = got_ref_resolve(&commit_id, repo, head_ref);
7844 got_ref_close(head_ref);
7847 error = got_object_id_str(&commit_id_str, commit_id);
7852 error = got_keyword_to_idstr(&keyword_idstr,
7853 commit_id_arg, repo, worktree);
7856 commit_id_str = keyword_idstr;
7860 /* Release work tree lock. */
7861 got_worktree_close(worktree);
7865 error = add_tag(repo, tagger, tag_name,
7866 commit_id_str ? commit_id_str : commit_id_arg, tagmsg,
7867 signer_id, verbosity);
7871 const struct got_error *close_err = got_repo_close(repo);
7876 got_worktree_close(worktree);
7878 const struct got_error *pack_err =
7879 got_repo_pack_fds_close(pack_fds);
7885 free(gitconfig_path);
7886 free(commit_id_str);
7888 free(allowed_signers);
7889 free(revoked_signers);
7896 fprintf(stderr, "usage: %s add [-IR] path ...\n", getprogname());
7900 static const struct got_error *
7901 add_progress(void *arg, unsigned char status, const char *path)
7903 while (path[0] == '/')
7905 printf("%c %s\n", status, path);
7909 static const struct got_error *
7910 cmd_add(int argc, char *argv[])
7912 const struct got_error *error = NULL;
7913 struct got_repository *repo = NULL;
7914 struct got_worktree *worktree = NULL;
7916 struct got_pathlist_head paths;
7917 struct got_pathlist_entry *pe;
7918 int ch, can_recurse = 0, no_ignores = 0;
7919 int *pack_fds = NULL;
7924 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7929 while ((ch = getopt(argc, argv, "IR")) != -1) {
7949 cwd = getcwd(NULL, 0);
7951 error = got_error_from_errno("getcwd");
7955 error = got_repo_pack_fds_open(&pack_fds);
7959 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
7961 if (error->code == GOT_ERR_NOT_WORKTREE)
7962 error = wrap_not_worktree_error(error, "add", cwd);
7966 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7971 error = apply_unveil(got_repo_get_path(repo), 1,
7972 got_worktree_get_root_path(worktree));
7976 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7983 TAILQ_FOREACH(pe, &paths, entry) {
7984 if (asprintf(&ondisk_path, "%s/%s",
7985 got_worktree_get_root_path(worktree),
7987 error = got_error_from_errno("asprintf");
7990 if (lstat(ondisk_path, &sb) == -1) {
7991 if (errno == ENOENT) {
7995 error = got_error_from_errno2("lstat",
8001 if (S_ISDIR(sb.st_mode)) {
8002 error = got_error_msg(GOT_ERR_BAD_PATH,
8003 "adding directories requires -R option");
8009 error = got_worktree_schedule_add(worktree, &paths, add_progress,
8010 NULL, repo, no_ignores);
8013 const struct got_error *close_err = got_repo_close(repo);
8018 got_worktree_close(worktree);
8020 const struct got_error *pack_err =
8021 got_repo_pack_fds_close(pack_fds);
8025 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
8033 fprintf(stderr, "usage: %s remove [-fkR] [-s status-codes] path ...\n",
8038 static const struct got_error *
8039 print_remove_status(void *arg, unsigned char status,
8040 unsigned char staged_status, const char *path)
8042 while (path[0] == '/')
8044 if (status == GOT_STATUS_NONEXISTENT)
8046 if (status == staged_status && (status == GOT_STATUS_DELETE))
8047 status = GOT_STATUS_NO_CHANGE;
8048 printf("%c%c %s\n", status, staged_status, path);
8052 static const struct got_error *
8053 cmd_remove(int argc, char *argv[])
8055 const struct got_error *error = NULL;
8056 struct got_worktree *worktree = NULL;
8057 struct got_repository *repo = NULL;
8058 const char *status_codes = NULL;
8060 struct got_pathlist_head paths;
8061 struct got_pathlist_entry *pe;
8062 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0, i;
8063 int ignore_missing_paths = 0;
8064 int *pack_fds = NULL;
8069 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
8074 while ((ch = getopt(argc, argv, "fkRs:")) != -1) {
8077 delete_local_mods = 1;
8078 ignore_missing_paths = 1;
8087 for (i = 0; optarg[i] != '\0'; i++) {
8088 switch (optarg[i]) {
8089 case GOT_STATUS_MODIFY:
8090 delete_local_mods = 1;
8092 case GOT_STATUS_MISSING:
8093 ignore_missing_paths = 1;
8096 errx(1, "invalid status code '%c'",
8100 status_codes = optarg;
8114 cwd = getcwd(NULL, 0);
8116 error = got_error_from_errno("getcwd");
8120 error = got_repo_pack_fds_open(&pack_fds);
8124 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
8126 if (error->code == GOT_ERR_NOT_WORKTREE)
8127 error = wrap_not_worktree_error(error, "remove", cwd);
8131 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8136 error = apply_unveil(got_repo_get_path(repo), 1,
8137 got_worktree_get_root_path(worktree));
8141 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8148 TAILQ_FOREACH(pe, &paths, entry) {
8149 if (asprintf(&ondisk_path, "%s/%s",
8150 got_worktree_get_root_path(worktree),
8152 error = got_error_from_errno("asprintf");
8155 if (lstat(ondisk_path, &sb) == -1) {
8156 if (errno == ENOENT) {
8160 error = got_error_from_errno2("lstat",
8166 if (S_ISDIR(sb.st_mode)) {
8167 error = got_error_msg(GOT_ERR_BAD_PATH,
8168 "removing directories requires -R option");
8174 error = got_worktree_schedule_delete(worktree, &paths,
8175 delete_local_mods, status_codes, print_remove_status, NULL,
8176 repo, keep_on_disk, ignore_missing_paths);
8179 const struct got_error *close_err = got_repo_close(repo);
8184 got_worktree_close(worktree);
8186 const struct got_error *pack_err =
8187 got_repo_pack_fds_close(pack_fds);
8191 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
8199 fprintf(stderr, "usage: %s patch [-nR] [-c commit] [-p strip-count] "
8200 "[patchfile]\n", getprogname());
8204 static const struct got_error *
8205 patch_from_stdin(int *patchfd)
8207 const struct got_error *err = NULL;
8210 sig_t sighup, sigint, sigquit;
8212 *patchfd = got_opentempfd();
8214 return got_error_from_errno("got_opentempfd");
8216 sighup = signal(SIGHUP, SIG_DFL);
8217 sigint = signal(SIGINT, SIG_DFL);
8218 sigquit = signal(SIGQUIT, SIG_DFL);
8221 r = read(0, buf, sizeof(buf));
8223 err = got_error_from_errno("read");
8228 if (write(*patchfd, buf, r) == -1) {
8229 err = got_error_from_errno("write");
8234 signal(SIGHUP, sighup);
8235 signal(SIGINT, sigint);
8236 signal(SIGQUIT, sigquit);
8238 if (err == NULL && lseek(*patchfd, 0, SEEK_SET) == -1)
8239 err = got_error_from_errno("lseek");
8249 struct got_patch_progress_arg {
8255 static const struct got_error *
8256 patch_progress(void *arg, const char *old, const char *new,
8257 unsigned char status, const struct got_error *error, int old_from,
8258 int old_lines, int new_from, int new_lines, int offset,
8259 int ws_mangled, const struct got_error *hunk_err)
8261 const char *path = new == NULL ? old : new;
8262 struct got_patch_progress_arg *a = arg;
8264 while (*path == '/')
8267 if (status != GOT_STATUS_NO_CHANGE &&
8268 status != 0 /* per-hunk progress */) {
8269 printf("%c %s\n", status, path);
8270 a->did_something = 1;
8273 if (hunk_err == NULL) {
8274 if (status == GOT_STATUS_CANNOT_UPDATE)
8276 else if (status == GOT_STATUS_CONFLICT)
8281 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
8283 if (offset != 0 || hunk_err != NULL || ws_mangled) {
8284 printf("@@ -%d,%d +%d,%d @@ ", old_from,
8285 old_lines, new_from, new_lines);
8286 if (hunk_err != NULL)
8287 printf("%s\n", hunk_err->msg);
8288 else if (offset != 0)
8289 printf("applied with offset %d\n", offset);
8291 printf("hunk contains mangled whitespace\n");
8298 print_patch_progress_stats(struct got_patch_progress_arg *ppa)
8300 if (!ppa->did_something)
8303 if (ppa->conflicts > 0)
8304 printf("Files with merge conflicts: %d\n", ppa->conflicts);
8306 if (ppa->rejects > 0) {
8307 printf("Files where patch failed to apply: %d\n",
8312 static const struct got_error *
8313 cmd_patch(int argc, char *argv[])
8315 const struct got_error *error = NULL, *close_error = NULL;
8316 struct got_worktree *worktree = NULL;
8317 struct got_repository *repo = NULL;
8318 struct got_reflist_head refs;
8319 struct got_object_id *commit_id = NULL;
8320 const char *commit_id_str = NULL;
8323 char *cwd = NULL, *keyword_idstr = NULL;
8324 int ch, nop = 0, strip = -1, reverse = 0;
8326 int *pack_fds = NULL;
8327 struct got_patch_progress_arg ppa;
8332 if (pledge("stdio rpath wpath cpath fattr proc exec sendfd flock "
8333 "unveil", NULL) == -1)
8337 while ((ch = getopt(argc, argv, "c:np:R")) != -1) {
8340 commit_id_str = optarg;
8346 strip = strtonum(optarg, 0, INT_MAX, &errstr);
8348 errx(1, "pathname strip count is %s: %s",
8364 error = patch_from_stdin(&patchfd);
8367 } else if (argc == 1) {
8368 patchfd = open(argv[0], O_RDONLY);
8369 if (patchfd == -1) {
8370 error = got_error_from_errno2("open", argv[0]);
8373 if (fstat(patchfd, &sb) == -1) {
8374 error = got_error_from_errno2("fstat", argv[0]);
8377 if (!S_ISREG(sb.st_mode)) {
8378 error = got_error_path(argv[0], GOT_ERR_BAD_FILETYPE);
8384 if ((cwd = getcwd(NULL, 0)) == NULL) {
8385 error = got_error_from_errno("getcwd");
8389 error = got_repo_pack_fds_open(&pack_fds);
8393 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
8397 const char *repo_path = got_worktree_get_repo_path(worktree);
8398 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8402 error = apply_unveil(got_repo_get_path(repo), 0,
8403 got_worktree_get_root_path(worktree));
8407 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
8411 if (commit_id_str != NULL) {
8412 error = got_keyword_to_idstr(&keyword_idstr, commit_id_str,
8417 error = got_repo_match_object_id(&commit_id, NULL,
8418 keyword_idstr != NULL ? keyword_idstr : commit_id_str,
8419 GOT_OBJ_TYPE_COMMIT, &refs, repo);
8424 memset(&ppa, 0, sizeof(ppa));
8425 error = got_patch(patchfd, worktree, repo, nop, strip, reverse,
8426 commit_id, patch_progress, &ppa, check_cancelled, NULL);
8427 print_patch_progress_stats(&ppa);
8429 got_ref_list_free(&refs);
8430 free(keyword_idstr);
8433 close_error = got_repo_close(repo);
8435 error = close_error;
8437 if (worktree != NULL) {
8438 close_error = got_worktree_close(worktree);
8440 error = close_error;
8443 const struct got_error *pack_err =
8444 got_repo_pack_fds_close(pack_fds);
8455 fprintf(stderr, "usage: %s revert [-pR] [-F response-script] path ...\n",
8460 static const struct got_error *
8461 revert_progress(void *arg, unsigned char status, const char *path)
8463 if (status == GOT_STATUS_UNVERSIONED)
8466 while (path[0] == '/')
8468 printf("%c %s\n", status, path);
8472 struct choose_patch_arg {
8473 FILE *patch_script_file;
8477 static const struct got_error *
8478 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
8479 int nchanges, const char *action)
8481 const struct got_error *err;
8483 size_t linesize = 0;
8487 case GOT_STATUS_ADD:
8488 printf("A %s\n%s this addition? [y/n] ", path, action);
8490 case GOT_STATUS_DELETE:
8491 printf("D %s\n%s this deletion? [y/n] ", path, action);
8493 case GOT_STATUS_MODIFY:
8494 if (fseek(patch_file, 0L, SEEK_SET) == -1)
8495 return got_error_from_errno("fseek");
8496 printf(GOT_COMMIT_SEP_STR);
8497 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
8499 if (linelen == -1 && ferror(patch_file)) {
8500 err = got_error_from_errno("getline");
8505 printf(GOT_COMMIT_SEP_STR);
8506 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
8507 path, n, nchanges, action);
8510 return got_error_path(path, GOT_ERR_FILE_STATUS);
8517 static const struct got_error *
8518 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
8519 FILE *patch_file, int n, int nchanges)
8521 const struct got_error *err = NULL;
8523 size_t linesize = 0;
8526 struct choose_patch_arg *a = arg;
8528 *choice = GOT_PATCH_CHOICE_NONE;
8530 if (a->patch_script_file) {
8532 err = show_change(status, path, patch_file, n, nchanges,
8536 linelen = getline(&line, &linesize, a->patch_script_file);
8537 if (linelen == -1) {
8538 if (ferror(a->patch_script_file))
8539 return got_error_from_errno("getline");
8542 nl = strchr(line, '\n');
8545 if (strcmp(line, "y") == 0) {
8546 *choice = GOT_PATCH_CHOICE_YES;
8548 } else if (strcmp(line, "n") == 0) {
8549 *choice = GOT_PATCH_CHOICE_NO;
8551 } else if (strcmp(line, "q") == 0 &&
8552 status == GOT_STATUS_MODIFY) {
8553 *choice = GOT_PATCH_CHOICE_QUIT;
8556 printf("invalid response '%s'\n", line);
8561 while (resp != 'y' && resp != 'n' && resp != 'q') {
8562 err = show_change(status, path, patch_file, n, nchanges,
8569 if (status == GOT_STATUS_MODIFY) {
8570 if (resp != 'y' && resp != 'n' && resp != 'q') {
8571 printf("invalid response '%c'\n", resp);
8574 } else if (resp != 'y' && resp != 'n') {
8575 printf("invalid response '%c'\n", resp);
8581 *choice = GOT_PATCH_CHOICE_YES;
8582 else if (resp == 'n')
8583 *choice = GOT_PATCH_CHOICE_NO;
8584 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
8585 *choice = GOT_PATCH_CHOICE_QUIT;
8590 struct wt_commitable_path_arg {
8591 struct got_pathlist_head *commit_paths;
8596 * Shortcut work tree status callback to determine if the set of paths scanned
8597 * has at least one versioned path that is being modified and, if not NULL, is
8598 * in the arg->commit_paths list. Set arg and return GOT_ERR_FILE_MODIFIED as
8599 * soon as a path is passed with a status that satisfies this criteria.
8601 static const struct got_error *
8602 worktree_has_commitable_path(void *arg, unsigned char status,
8603 unsigned char staged_status, const char *path,
8604 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8605 struct got_object_id *commit_id, int dirfd, const char *de_name)
8607 struct wt_commitable_path_arg *a = arg;
8609 if (status == staged_status && (status == GOT_STATUS_DELETE))
8610 status = GOT_STATUS_NO_CHANGE;
8612 if (!(status == GOT_STATUS_NO_CHANGE ||
8613 status == GOT_STATUS_UNVERSIONED) ||
8614 staged_status != GOT_STATUS_NO_CHANGE) {
8615 if (a->commit_paths != NULL) {
8616 struct got_pathlist_entry *pe;
8618 TAILQ_FOREACH(pe, a->commit_paths, entry) {
8619 if (strncmp(path, pe->path,
8620 pe->path_len) == 0) {
8621 *a->has_changes = 1;
8626 *a->has_changes = 1;
8628 if (*a->has_changes)
8629 return got_error(GOT_ERR_FILE_MODIFIED);
8636 * Check that the changeset of the commit identified by id is
8637 * comprised of at least one modified path that is being committed.
8639 static const struct got_error *
8640 commit_path_changed_in_worktree(struct wt_commitable_path_arg *wcpa,
8641 struct got_object_id *id, struct got_worktree *worktree,
8642 struct got_repository *repo)
8644 const struct got_error *err;
8645 struct got_pathlist_head paths;
8646 struct got_commit_object *commit = NULL, *pcommit = NULL;
8647 struct got_tree_object *tree = NULL, *ptree = NULL;
8648 struct got_object_qid *pid;
8652 err = got_object_open_as_commit(&commit, repo, id);
8656 err = got_object_open_as_tree(&tree, repo,
8657 got_object_commit_get_tree_id(commit));
8661 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
8663 err = got_object_open_as_commit(&pcommit, repo, &pid->id);
8667 err = got_object_open_as_tree(&ptree, repo,
8668 got_object_commit_get_tree_id(pcommit));
8673 err = got_diff_tree(ptree, tree, NULL, NULL, -1, -1, "", "", repo,
8674 got_diff_tree_collect_changed_paths, &paths, 0);
8678 err = got_worktree_status(worktree, &paths, repo, 0,
8679 worktree_has_commitable_path, wcpa, check_cancelled, NULL);
8680 if (err && err->code == GOT_ERR_FILE_MODIFIED) {
8682 * At least one changed path in the referenced commit is
8683 * modified in the work tree, that's all we need to know!
8689 got_pathlist_free(&paths, GOT_PATHLIST_FREE_ALL);
8691 got_object_commit_close(commit);
8693 got_object_commit_close(pcommit);
8695 got_object_tree_close(tree);
8697 got_object_tree_close(ptree);
8702 * Remove any "logmsg" reference comprised entirely of paths that have
8703 * been reverted in this work tree. If any path in the logmsg ref changeset
8704 * remains in a changed state in the worktree, do not remove the reference.
8706 static const struct got_error *
8707 rm_logmsg_ref(struct got_worktree *worktree, struct got_repository *repo)
8709 const struct got_error *err;
8710 struct got_reflist_head refs;
8711 struct got_reflist_entry *re;
8712 struct got_commit_object *commit = NULL;
8713 struct got_object_id *commit_id = NULL;
8714 struct wt_commitable_path_arg wcpa;
8715 char *uuidstr = NULL;
8719 err = got_worktree_get_uuid(&uuidstr, worktree);
8723 err = got_ref_list(&refs, repo, "refs/got/worktree",
8724 got_ref_cmp_by_name, repo);
8728 TAILQ_FOREACH(re, &refs, entry) {
8729 const char *refname;
8730 int has_changes = 0;
8732 refname = got_ref_get_name(re->ref);
8734 if (!strncmp(refname, GOT_WORKTREE_CHERRYPICK_REF_PREFIX,
8735 GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN))
8736 refname += GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN + 1;
8737 else if (!strncmp(refname, GOT_WORKTREE_BACKOUT_REF_PREFIX,
8738 GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN))
8739 refname += GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN + 1;
8743 if (strncmp(refname, uuidstr, GOT_WORKTREE_UUID_STRLEN) == 0)
8744 refname += GOT_WORKTREE_UUID_STRLEN + 1; /* skip '-' */
8748 err = got_repo_match_object_id(&commit_id, NULL, refname,
8749 GOT_OBJ_TYPE_COMMIT, NULL, repo);
8753 err = got_object_open_as_commit(&commit, repo, commit_id);
8757 wcpa.commit_paths = NULL;
8758 wcpa.has_changes = &has_changes;
8760 err = commit_path_changed_in_worktree(&wcpa, commit_id,
8766 err = got_ref_delete(re->ref, repo);
8771 got_object_commit_close(commit);
8780 got_ref_list_free(&refs);
8782 got_object_commit_close(commit);
8786 static const struct got_error *
8787 cmd_revert(int argc, char *argv[])
8789 const struct got_error *error = NULL;
8790 struct got_worktree *worktree = NULL;
8791 struct got_repository *repo = NULL;
8792 char *cwd = NULL, *path = NULL;
8793 struct got_pathlist_head paths;
8794 struct got_pathlist_entry *pe;
8795 int ch, can_recurse = 0, pflag = 0;
8796 FILE *patch_script_file = NULL;
8797 const char *patch_script_path = NULL;
8798 struct choose_patch_arg cpa;
8799 int *pack_fds = NULL;
8804 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8805 "unveil", NULL) == -1)
8809 while ((ch = getopt(argc, argv, "F:pR")) != -1) {
8812 patch_script_path = optarg;
8831 if (patch_script_path && !pflag)
8832 errx(1, "-F option can only be used together with -p option");
8834 cwd = getcwd(NULL, 0);
8836 error = got_error_from_errno("getcwd");
8840 error = got_repo_pack_fds_open(&pack_fds);
8844 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
8846 if (error->code == GOT_ERR_NOT_WORKTREE)
8847 error = wrap_not_worktree_error(error, "revert", cwd);
8851 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8856 if (patch_script_path) {
8857 patch_script_file = fopen(patch_script_path, "re");
8858 if (patch_script_file == NULL) {
8859 error = got_error_from_errno2("fopen",
8866 * XXX "c" perm needed on repo dir to delete merge references.
8868 error = apply_unveil(got_repo_get_path(repo), 0,
8869 got_worktree_get_root_path(worktree));
8873 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8880 TAILQ_FOREACH(pe, &paths, entry) {
8881 if (asprintf(&ondisk_path, "%s/%s",
8882 got_worktree_get_root_path(worktree),
8884 error = got_error_from_errno("asprintf");
8887 if (lstat(ondisk_path, &sb) == -1) {
8888 if (errno == ENOENT) {
8892 error = got_error_from_errno2("lstat",
8898 if (S_ISDIR(sb.st_mode)) {
8899 error = got_error_msg(GOT_ERR_BAD_PATH,
8900 "reverting directories requires -R option");
8906 cpa.patch_script_file = patch_script_file;
8907 cpa.action = "revert";
8908 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
8909 pflag ? choose_patch : NULL, &cpa, repo);
8911 error = rm_logmsg_ref(worktree, repo);
8913 if (patch_script_file && fclose(patch_script_file) == EOF &&
8915 error = got_error_from_errno2("fclose", patch_script_path);
8917 const struct got_error *close_err = got_repo_close(repo);
8922 got_worktree_close(worktree);
8924 const struct got_error *pack_err =
8925 got_repo_pack_fds_close(pack_fds);
8929 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
8938 fprintf(stderr, "usage: %s commit [-CNnS] [-A author] [-F path] "
8939 "[-m message] [path ...]\n", getprogname());
8943 struct collect_commit_logmsg_arg {
8944 const char *cmdline_log;
8945 const char *prepared_log;
8946 const char *merged_log;
8947 int non_interactive;
8949 const char *worktree_path;
8950 const char *branch_name;
8951 const char *repo_path;
8956 static const struct got_error *
8957 read_prepared_logmsg(char **logmsg, const char *path)
8959 const struct got_error *err = NULL;
8965 memset(&sb, 0, sizeof(sb));
8967 f = fopen(path, "re");
8969 return got_error_from_errno2("fopen", path);
8971 if (fstat(fileno(f), &sb) == -1) {
8972 err = got_error_from_errno2("fstat", path);
8975 if (sb.st_size == 0) {
8976 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
8980 *logmsg = malloc(sb.st_size + 1);
8981 if (*logmsg == NULL) {
8982 err = got_error_from_errno("malloc");
8986 r = fread(*logmsg, 1, sb.st_size, f);
8987 if (r != sb.st_size) {
8989 err = got_error_from_errno2("fread", path);
8991 err = got_error(GOT_ERR_IO);
8994 (*logmsg)[sb.st_size] = '\0';
8996 if (fclose(f) == EOF && err == NULL)
8997 err = got_error_from_errno2("fclose", path);
9005 static const struct got_error *
9006 collect_commit_logmsg(struct got_pathlist_head *commitable_paths,
9007 const char *diff_path, char **logmsg, void *arg)
9009 char *initial_content = NULL;
9010 struct got_pathlist_entry *pe;
9011 const struct got_error *err = NULL;
9012 char *template = NULL;
9013 char *prepared_msg = NULL, *merged_msg = NULL;
9014 struct collect_commit_logmsg_arg *a = arg;
9015 int initial_content_len;
9019 /* if a message was specified on the command line, just use it */
9020 if (a->cmdline_log != NULL && *a->cmdline_log != '\0') {
9021 len = strlen(a->cmdline_log) + 1;
9022 *logmsg = malloc(len + 1);
9023 if (*logmsg == NULL)
9024 return got_error_from_errno("malloc");
9025 strlcpy(*logmsg, a->cmdline_log, len);
9027 } else if (a->prepared_log != NULL && a->non_interactive)
9028 return read_prepared_logmsg(logmsg, a->prepared_log);
9030 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
9031 return got_error_from_errno("asprintf");
9033 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template, "");
9037 if (a->prepared_log) {
9038 err = read_prepared_logmsg(&prepared_msg, a->prepared_log);
9041 } else if (a->merged_log) {
9042 err = read_prepared_logmsg(&merged_msg, a->merged_log);
9047 initial_content_len = asprintf(&initial_content,
9048 "%s%s\n# changes to be committed on branch %s:\n",
9049 prepared_msg ? prepared_msg : "",
9050 merged_msg ? merged_msg : "", a->branch_name);
9051 if (initial_content_len == -1) {
9052 err = got_error_from_errno("asprintf");
9056 if (write(fd, initial_content, initial_content_len) == -1) {
9057 err = got_error_from_errno2("write", a->logmsg_path);
9061 TAILQ_FOREACH(pe, commitable_paths, entry) {
9062 struct got_commitable *ct = pe->data;
9063 dprintf(fd, "# %c %s\n",
9064 got_commitable_get_status(ct),
9065 got_commitable_get_path(ct));
9069 dprintf(fd, "# detailed changes can be viewed in %s\n",
9073 if (close(fd) == -1) {
9074 err = got_error_from_errno2("close", a->logmsg_path);
9079 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content,
9080 initial_content_len, a->prepared_log ? 0 : 1);
9082 free(initial_content);
9087 if (fd != -1 && close(fd) == -1 && err == NULL)
9088 err = got_error_from_errno2("close", a->logmsg_path);
9090 /* Editor is done; we can now apply unveil(2) */
9092 err = apply_unveil(a->repo_path, 0, a->worktree_path);
9100 static const struct got_error *
9101 cat_logmsg(FILE *f, struct got_commit_object *commit, const char *idstr,
9102 const char *type, int has_content)
9104 const struct got_error *err = NULL;
9105 char *logmsg = NULL;
9107 err = got_object_commit_get_logmsg(&logmsg, commit);
9111 if (fprintf(f, "%s# log message of %s commit %s:%s",
9112 has_content ? "\n" : "", type, idstr, logmsg) < 0)
9113 err = got_ferror(f, GOT_ERR_IO);
9120 * Lookup "logmsg" references of backed-out and cherrypicked commits
9121 * belonging to the current work tree. If found, and the worktree has
9122 * at least one modified file that was changed in the referenced commit,
9123 * add its log message to a new temporary file at *logmsg_path.
9124 * Add all refs found to matched_refs to be scheduled for removal on
9125 * successful commit.
9127 static const struct got_error *
9128 lookup_logmsg_ref(char **logmsg_path, struct got_pathlist_head *paths,
9129 struct got_reflist_head *matched_refs, struct got_worktree *worktree,
9130 struct got_repository *repo)
9132 const struct got_error *err;
9133 struct got_commit_object *commit = NULL;
9134 struct got_object_id *id = NULL;
9135 struct got_reflist_head refs;
9136 struct got_reflist_entry *re, *re_match;
9138 char *uuidstr = NULL;
9139 int added_logmsg = 0;
9143 *logmsg_path = NULL;
9145 err = got_worktree_get_uuid(&uuidstr, worktree);
9149 err = got_ref_list(&refs, repo, "refs/got/worktree",
9150 got_ref_cmp_by_name, repo);
9154 TAILQ_FOREACH(re, &refs, entry) {
9155 const char *refname, *type;
9156 struct wt_commitable_path_arg wcpa;
9159 refname = got_ref_get_name(re->ref);
9161 if (strncmp(refname, GOT_WORKTREE_CHERRYPICK_REF_PREFIX,
9162 GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN) == 0) {
9163 refname += GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN + 1;
9164 type = "cherrypicked";
9165 } else if (strncmp(refname, GOT_WORKTREE_BACKOUT_REF_PREFIX,
9166 GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN) == 0) {
9167 refname += GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN + 1;
9168 type = "backed-out";
9172 if (strncmp(refname, uuidstr, GOT_WORKTREE_UUID_STRLEN) == 0)
9173 refname += GOT_WORKTREE_UUID_STRLEN + 1; /* skip '-' */
9177 err = got_repo_match_object_id(&id, NULL, refname,
9178 GOT_OBJ_TYPE_COMMIT, NULL, repo);
9182 err = got_object_open_as_commit(&commit, repo, id);
9186 wcpa.commit_paths = paths;
9187 wcpa.has_changes = &add_logmsg;
9189 err = commit_path_changed_in_worktree(&wcpa, id,
9196 err = got_opentemp_named(logmsg_path, &f,
9197 "got-commit-logmsg", "");
9201 err = cat_logmsg(f, commit, refname, type,
9208 err = got_reflist_entry_dup(&re_match, re);
9211 TAILQ_INSERT_HEAD(matched_refs, re_match, entry);
9214 got_object_commit_close(commit);
9223 got_ref_list_free(&refs);
9225 got_object_commit_close(commit);
9226 if (f && fclose(f) == EOF && err == NULL)
9227 err = got_error_from_errno("fclose");
9228 if (!added_logmsg) {
9229 if (*logmsg_path && unlink(*logmsg_path) != 0 && err == NULL)
9230 err = got_error_from_errno2("unlink", *logmsg_path);
9231 *logmsg_path = NULL;
9236 static const struct got_error *
9237 cmd_commit(int argc, char *argv[])
9239 const struct got_error *error = NULL;
9240 struct got_worktree *worktree = NULL;
9241 struct got_repository *repo = NULL;
9242 char *cwd = NULL, *id_str = NULL;
9243 struct got_object_id *id = NULL;
9244 const char *logmsg = NULL;
9245 char *prepared_logmsg = NULL, *merged_logmsg = NULL;
9246 struct collect_commit_logmsg_arg cl_arg;
9247 const char *author = NULL;
9248 char *gitconfig_path = NULL, *editor = NULL, *committer = NULL;
9249 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
9250 int allow_bad_symlinks = 0, non_interactive = 0, merge_in_progress = 0;
9251 int show_diff = 1, commit_conflicts = 0;
9252 struct got_pathlist_head paths;
9253 struct got_reflist_head refs;
9254 struct got_reflist_entry *re;
9255 int *pack_fds = NULL;
9259 cl_arg.logmsg_path = NULL;
9262 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9263 "unveil", NULL) == -1)
9267 while ((ch = getopt(argc, argv, "A:CF:m:NnS")) != -1) {
9271 error = valid_author(author);
9276 commit_conflicts = 1;
9280 option_conflict('F', 'm');
9281 prepared_logmsg = realpath(optarg, NULL);
9282 if (prepared_logmsg == NULL)
9283 return got_error_from_errno2("realpath",
9287 if (prepared_logmsg)
9288 option_conflict('m', 'F');
9292 non_interactive = 1;
9298 allow_bad_symlinks = 1;
9309 cwd = getcwd(NULL, 0);
9311 error = got_error_from_errno("getcwd");
9315 error = got_repo_pack_fds_open(&pack_fds);
9319 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
9321 if (error->code == GOT_ERR_NOT_WORKTREE)
9322 error = wrap_not_worktree_error(error, "commit", cwd);
9326 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
9329 if (rebase_in_progress) {
9330 error = got_error(GOT_ERR_REBASING);
9334 error = got_worktree_histedit_in_progress(&histedit_in_progress,
9339 error = get_gitconfig_path(&gitconfig_path);
9342 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9343 gitconfig_path, pack_fds);
9347 error = got_worktree_merge_in_progress(&merge_in_progress, worktree, repo);
9350 if (merge_in_progress) {
9351 error = got_error(GOT_ERR_MERGE_BUSY);
9355 error = get_author(&committer, repo, worktree);
9363 * unveil(2) traverses exec(2); if an editor is used we have
9364 * to apply unveil after the log message has been written.
9366 if (logmsg == NULL || strlen(logmsg) == 0)
9367 error = get_editor(&editor);
9369 error = apply_unveil(got_repo_get_path(repo), 0,
9370 got_worktree_get_root_path(worktree));
9374 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
9378 if (prepared_logmsg == NULL) {
9379 error = lookup_logmsg_ref(&merged_logmsg,
9380 argc > 0 ? &paths : NULL, &refs, worktree, repo);
9385 cl_arg.editor = editor;
9386 cl_arg.cmdline_log = logmsg;
9387 cl_arg.prepared_log = prepared_logmsg;
9388 cl_arg.merged_log = merged_logmsg;
9389 cl_arg.non_interactive = non_interactive;
9390 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
9391 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
9392 if (!histedit_in_progress) {
9393 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
9394 error = got_error(GOT_ERR_COMMIT_BRANCH);
9397 cl_arg.branch_name += 11;
9399 cl_arg.repo_path = got_repo_get_path(repo);
9400 error = got_worktree_commit(&id, worktree, &paths, author, committer,
9401 allow_bad_symlinks, show_diff, commit_conflicts,
9402 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
9404 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
9405 cl_arg.logmsg_path != NULL)
9406 preserve_logmsg = 1;
9410 error = got_object_id_str(&id_str, id);
9413 printf("Created commit %s\n", id_str);
9415 TAILQ_FOREACH(re, &refs, entry) {
9416 error = got_ref_delete(re->ref, repo);
9422 if (preserve_logmsg) {
9423 fprintf(stderr, "%s: log message preserved in %s\n",
9424 getprogname(), cl_arg.logmsg_path);
9425 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
9427 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
9428 free(cl_arg.logmsg_path);
9429 if (merged_logmsg && unlink(merged_logmsg) == -1 && error == NULL)
9430 error = got_error_from_errno2("unlink", merged_logmsg);
9431 free(merged_logmsg);
9433 const struct got_error *close_err = got_repo_close(repo);
9438 got_worktree_close(worktree);
9440 const struct got_error *pack_err =
9441 got_repo_pack_fds_close(pack_fds);
9445 got_ref_list_free(&refs);
9446 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
9449 free(gitconfig_path);
9452 free(prepared_logmsg);
9459 fprintf(stderr, "usage: %s send [-afqTv] [-b branch] [-d branch] "
9460 "[-r repository-path] [-t tag] [remote-repository]\n",
9466 print_load_info(int print_colored, int print_found, int print_trees,
9467 int ncolored, int nfound, int ntrees)
9469 if (print_colored) {
9470 printf("%d commit%s colored", ncolored,
9471 ncolored == 1 ? "" : "s");
9474 printf("%s%d object%s found",
9475 ncolored > 0 ? "; " : "",
9476 nfound, nfound == 1 ? "" : "s");
9479 printf("; %d tree%s scanned", ntrees,
9480 ntrees == 1 ? "" : "s");
9484 struct got_send_progress_arg {
9485 char last_scaled_packsize[FMT_SCALED_STRSIZE];
9492 int last_nobj_total;
9496 int printed_something;
9498 struct got_pathlist_head *delete_branches;
9501 static const struct got_error *
9502 send_progress(void *arg, int ncolored, int nfound, int ntrees,
9503 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
9504 int nobj_written, off_t bytes_sent, const char *refname,
9505 const char *errmsg, int success)
9507 struct got_send_progress_arg *a = arg;
9508 char scaled_packsize[FMT_SCALED_STRSIZE];
9509 char scaled_sent[FMT_SCALED_STRSIZE];
9510 int p_deltify = 0, p_written = 0, p_sent = 0;
9511 int print_colored = 0, print_found = 0, print_trees = 0;
9512 int print_searching = 0, print_total = 0;
9513 int print_deltify = 0, print_written = 0, print_sent = 0;
9515 if (a->verbosity < 0)
9519 const char *status = success ? "accepted" : "rejected";
9522 struct got_pathlist_entry *pe;
9523 TAILQ_FOREACH(pe, a->delete_branches, entry) {
9524 const char *branchname = pe->path;
9525 if (got_path_cmp(branchname, refname,
9526 strlen(branchname), strlen(refname)) == 0) {
9528 a->sent_something = 1;
9534 if (a->printed_something)
9536 printf("Server has %s %s", status, refname);
9538 printf(": %s", errmsg);
9539 a->printed_something = 1;
9543 if (a->last_ncolored != ncolored) {
9545 a->last_ncolored = ncolored;
9548 if (a->last_nfound != nfound) {
9551 a->last_nfound = nfound;
9554 if (a->last_ntrees != ntrees) {
9558 a->last_ntrees = ntrees;
9561 if ((print_colored || print_found || print_trees) &&
9564 print_load_info(print_colored, print_found, print_trees,
9565 ncolored, nfound, ntrees);
9566 a->printed_something = 1;
9569 } else if (!a->loading_done) {
9571 print_load_info(1, 1, 1, ncolored, nfound, ntrees);
9573 a->loading_done = 1;
9576 if (fmt_scaled(packfile_size, scaled_packsize) == -1)
9577 return got_error_from_errno("fmt_scaled");
9578 if (fmt_scaled(bytes_sent, scaled_sent) == -1)
9579 return got_error_from_errno("fmt_scaled");
9581 if (a->last_ncommits != ncommits) {
9582 print_searching = 1;
9583 a->last_ncommits = ncommits;
9586 if (a->last_nobj_total != nobj_total) {
9587 print_searching = 1;
9589 a->last_nobj_total = nobj_total;
9592 if (packfile_size > 0 && (a->last_scaled_packsize[0] == '\0' ||
9593 strcmp(scaled_packsize, a->last_scaled_packsize)) != 0) {
9594 if (strlcpy(a->last_scaled_packsize, scaled_packsize,
9595 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
9596 return got_error(GOT_ERR_NO_SPACE);
9599 if (nobj_deltify > 0 || nobj_written > 0) {
9600 if (nobj_deltify > 0) {
9601 p_deltify = (nobj_deltify * 100) / nobj_total;
9602 if (p_deltify != a->last_p_deltify) {
9603 a->last_p_deltify = p_deltify;
9604 print_searching = 1;
9609 if (nobj_written > 0) {
9610 p_written = (nobj_written * 100) / nobj_total;
9611 if (p_written != a->last_p_written) {
9612 a->last_p_written = p_written;
9613 print_searching = 1;
9621 if (bytes_sent > 0) {
9622 p_sent = (bytes_sent * 100) / packfile_size;
9623 if (p_sent != a->last_p_sent) {
9624 a->last_p_sent = p_sent;
9625 print_searching = 1;
9631 a->sent_something = 1;
9634 if (print_searching || print_total || print_deltify || print_written ||
9637 if (print_searching)
9638 printf("packing %d reference%s", ncommits,
9639 ncommits == 1 ? "" : "s");
9641 printf("; %d object%s", nobj_total,
9642 nobj_total == 1 ? "" : "s");
9644 printf("; deltify: %d%%", p_deltify);
9646 printf("; uploading pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
9647 scaled_packsize, p_sent);
9648 else if (print_written)
9649 printf("; writing pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
9650 scaled_packsize, p_written);
9651 if (print_searching || print_total || print_deltify ||
9652 print_written || print_sent) {
9653 a->printed_something = 1;
9659 static const struct got_error *
9660 cmd_send(int argc, char *argv[])
9662 const struct got_error *error = NULL;
9663 char *cwd = NULL, *repo_path = NULL;
9664 const char *remote_name;
9665 char *proto = NULL, *host = NULL, *port = NULL;
9666 char *repo_name = NULL, *server_path = NULL;
9667 const struct got_remote_repo *remotes, *remote = NULL;
9668 int nremotes, nbranches = 0, ndelete_branches = 0;
9669 struct got_repository *repo = NULL;
9670 struct got_worktree *worktree = NULL;
9671 const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
9672 struct got_pathlist_head branches;
9673 struct got_pathlist_head tags;
9674 struct got_reflist_head all_branches;
9675 struct got_reflist_head all_tags;
9676 struct got_pathlist_head delete_args;
9677 struct got_pathlist_head delete_branches;
9678 struct got_reflist_entry *re;
9679 struct got_pathlist_entry *pe;
9680 int i, ch, sendfd = -1, sendstatus;
9682 struct got_send_progress_arg spa;
9683 int verbosity = 0, overwrite_refs = 0;
9684 int send_all_branches = 0, send_all_tags = 0;
9685 struct got_reference *ref = NULL;
9686 int *pack_fds = NULL;
9688 TAILQ_INIT(&branches);
9690 TAILQ_INIT(&all_branches);
9691 TAILQ_INIT(&all_tags);
9692 TAILQ_INIT(&delete_args);
9693 TAILQ_INIT(&delete_branches);
9695 while ((ch = getopt(argc, argv, "ab:d:fqr:Tt:v")) != -1) {
9698 send_all_branches = 1;
9701 error = got_pathlist_append(&branches, optarg, NULL);
9707 error = got_pathlist_append(&delete_args, optarg, NULL);
9718 repo_path = realpath(optarg, NULL);
9719 if (repo_path == NULL)
9720 return got_error_from_errno2("realpath",
9722 got_path_strip_trailing_slashes(repo_path);
9728 error = got_pathlist_append(&tags, optarg, NULL);
9735 else if (verbosity < 3)
9746 if (send_all_branches && !TAILQ_EMPTY(&branches))
9747 option_conflict('a', 'b');
9748 if (send_all_tags && !TAILQ_EMPTY(&tags))
9749 option_conflict('T', 't');
9753 remote_name = GOT_SEND_DEFAULT_REMOTE_NAME;
9755 remote_name = argv[0];
9759 cwd = getcwd(NULL, 0);
9761 error = got_error_from_errno("getcwd");
9765 error = got_repo_pack_fds_open(&pack_fds);
9769 if (repo_path == NULL) {
9770 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
9771 if (error && error->code != GOT_ERR_NOT_WORKTREE)
9777 strdup(got_worktree_get_repo_path(worktree));
9778 if (repo_path == NULL)
9779 error = got_error_from_errno("strdup");
9783 repo_path = strdup(cwd);
9784 if (repo_path == NULL) {
9785 error = got_error_from_errno("strdup");
9791 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
9796 worktree_conf = got_worktree_get_gotconfig(worktree);
9797 if (worktree_conf) {
9798 got_gotconfig_get_remotes(&nremotes, &remotes,
9800 for (i = 0; i < nremotes; i++) {
9801 if (strcmp(remotes[i].name, remote_name) == 0) {
9802 remote = &remotes[i];
9808 if (remote == NULL) {
9809 repo_conf = got_repo_get_gotconfig(repo);
9811 got_gotconfig_get_remotes(&nremotes, &remotes,
9813 for (i = 0; i < nremotes; i++) {
9814 if (strcmp(remotes[i].name, remote_name) == 0) {
9815 remote = &remotes[i];
9821 if (remote == NULL) {
9822 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
9823 for (i = 0; i < nremotes; i++) {
9824 if (strcmp(remotes[i].name, remote_name) == 0) {
9825 remote = &remotes[i];
9830 if (remote == NULL) {
9831 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
9835 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
9836 &repo_name, remote->send_url);
9840 if (strcmp(proto, "git") == 0) {
9842 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
9843 "sendfd dns inet unveil", NULL) == -1)
9846 } else if (strcmp(proto, "git+ssh") == 0 ||
9847 strcmp(proto, "ssh") == 0) {
9849 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
9850 "sendfd unveil", NULL) == -1)
9853 } else if (strcmp(proto, "http") == 0 ||
9854 strcmp(proto, "git+http") == 0) {
9855 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
9858 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
9862 error = got_dial_apply_unveil(proto);
9866 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
9870 if (send_all_branches) {
9871 error = got_ref_list(&all_branches, repo, "refs/heads",
9872 got_ref_cmp_by_name, NULL);
9875 TAILQ_FOREACH(re, &all_branches, entry) {
9876 const char *branchname = got_ref_get_name(re->ref);
9877 error = got_pathlist_append(&branches,
9883 } else if (nbranches == 0) {
9884 for (i = 0; i < remote->nsend_branches; i++) {
9885 error = got_pathlist_append(&branches,
9886 remote->send_branches[i], NULL);
9892 if (send_all_tags) {
9893 error = got_ref_list(&all_tags, repo, "refs/tags",
9894 got_ref_cmp_by_name, NULL);
9897 TAILQ_FOREACH(re, &all_tags, entry) {
9898 const char *tagname = got_ref_get_name(re->ref);
9899 error = got_pathlist_append(&tags,
9907 * To prevent accidents only branches in refs/heads/ can be deleted
9908 * with 'got send -d'.
9909 * Deleting anything else requires local repository access or Git.
9911 TAILQ_FOREACH(pe, &delete_args, entry) {
9912 const char *branchname = pe->path;
9914 struct got_pathlist_entry *new;
9915 if (strncmp(branchname, "refs/heads/", 11) == 0) {
9916 s = strdup(branchname);
9918 error = got_error_from_errno("strdup");
9922 if (asprintf(&s, "refs/heads/%s", branchname) == -1) {
9923 error = got_error_from_errno("asprintf");
9927 error = got_pathlist_insert(&new, &delete_branches, s, NULL);
9928 if (error || new == NULL /* duplicate */)
9935 if (nbranches == 0 && ndelete_branches == 0) {
9936 struct got_reference *head_ref;
9938 error = got_ref_open(&head_ref, repo,
9939 got_worktree_get_head_ref_name(worktree), 0);
9941 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
9944 if (got_ref_is_symbolic(head_ref)) {
9945 error = got_ref_resolve_symbolic(&ref, repo, head_ref);
9946 got_ref_close(head_ref);
9951 error = got_pathlist_append(&branches, got_ref_get_name(ref),
9958 if (verbosity >= 0) {
9959 printf("Connecting to \"%s\" %s://%s%s%s%s%s\n",
9960 remote->name, proto, host,
9961 port ? ":" : "", port ? port : "",
9962 *server_path == '/' ? "" : "/", server_path);
9965 error = got_send_connect(&sendpid, &sendfd, proto, host, port,
9966 server_path, verbosity);
9970 memset(&spa, 0, sizeof(spa));
9971 spa.last_scaled_packsize[0] = '\0';
9972 spa.last_p_deltify = -1;
9973 spa.last_p_written = -1;
9974 spa.verbosity = verbosity;
9975 spa.delete_branches = &delete_branches;
9976 error = got_send_pack(remote_name, &branches, &tags, &delete_branches,
9977 verbosity, overwrite_refs, sendfd, repo, send_progress, &spa,
9978 check_cancelled, NULL);
9979 if (spa.printed_something)
9983 if (!spa.sent_something && verbosity >= 0)
9984 printf("Already up-to-date\n");
9987 if (kill(sendpid, SIGTERM) == -1)
9988 error = got_error_from_errno("kill");
9989 if (waitpid(sendpid, &sendstatus, 0) == -1 && error == NULL)
9990 error = got_error_from_errno("waitpid");
9992 if (sendfd != -1 && close(sendfd) == -1 && error == NULL)
9993 error = got_error_from_errno("close");
9995 const struct got_error *close_err = got_repo_close(repo);
10000 got_worktree_close(worktree);
10002 const struct got_error *pack_err =
10003 got_repo_pack_fds_close(pack_fds);
10008 got_ref_close(ref);
10009 got_pathlist_free(&branches, GOT_PATHLIST_FREE_NONE);
10010 got_pathlist_free(&tags, GOT_PATHLIST_FREE_NONE);
10011 got_ref_list_free(&all_branches);
10012 got_ref_list_free(&all_tags);
10013 got_pathlist_free(&delete_args, GOT_PATHLIST_FREE_NONE);
10014 got_pathlist_free(&delete_branches, GOT_PATHLIST_FREE_PATH);
10026 * Print and if delete is set delete all ref_prefix references.
10027 * If wanted_ref is not NULL, only print or delete this reference.
10029 static const struct got_error *
10030 process_logmsg_refs(const char *ref_prefix, size_t prefix_len,
10031 const char *wanted_ref, int delete, struct got_worktree *worktree,
10032 struct got_repository *repo)
10034 const struct got_error *err;
10035 struct got_pathlist_head paths;
10036 struct got_reflist_head refs;
10037 struct got_reflist_entry *re;
10038 struct got_reflist_object_id_map *refs_idmap = NULL;
10039 struct got_commit_object *commit = NULL;
10040 struct got_object_id *id = NULL;
10041 const char *header_prefix;
10042 char *uuidstr = NULL;
10046 TAILQ_INIT(&paths);
10048 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, repo);
10052 err = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
10056 if (worktree != NULL) {
10057 err = got_worktree_get_uuid(&uuidstr, worktree);
10063 if (strncmp(wanted_ref, "refs/heads/", 11) == 0)
10067 if (strcmp(ref_prefix, GOT_WORKTREE_BACKOUT_REF_PREFIX) == 0)
10068 header_prefix = "backout";
10070 header_prefix = "cherrypick";
10072 TAILQ_FOREACH(re, &refs, entry) {
10073 const char *refname, *wt;
10075 refname = got_ref_get_name(re->ref);
10077 err = check_cancelled(NULL);
10081 if (strncmp(refname, ref_prefix, prefix_len) == 0)
10082 refname += prefix_len + 1; /* skip '-' delimiter */
10088 if (worktree == NULL || strncmp(refname, uuidstr,
10089 GOT_WORKTREE_UUID_STRLEN) == 0)
10090 refname += GOT_WORKTREE_UUID_STRLEN + 1; /* skip '-' */
10094 err = got_repo_match_object_id(&id, NULL, refname,
10095 GOT_OBJ_TYPE_COMMIT, NULL, repo);
10099 err = got_object_open_as_commit(&commit, repo, id);
10104 found = strncmp(wanted_ref, refname,
10105 strlen(wanted_ref)) == 0;
10106 if (wanted_ref && !found) {
10107 struct got_reflist_head *ci_refs;
10109 ci_refs = got_reflist_object_id_map_lookup(refs_idmap,
10113 char *refs_str = NULL;
10114 char const *r = NULL;
10116 err = build_refs_str(&refs_str, ci_refs, id,
10123 if (strncmp(r, wanted_ref,
10124 strlen(wanted_ref)) == 0) {
10128 r = strchr(r, ' ');
10136 if (wanted_ref == NULL || found) {
10138 err = got_ref_delete(re->ref, repo);
10141 printf("Deleted: ");
10142 err = print_commit_oneline(commit, id, repo,
10146 * Print paths modified by commit to help
10147 * associate commits with worktree changes.
10149 err = get_changed_paths(&paths, commit,
10154 err = print_commit(commit, id, repo, NULL,
10155 &paths, NULL, 0, 0, refs_idmap, NULL,
10157 got_pathlist_free(&paths,
10158 GOT_PATHLIST_FREE_ALL);
10160 if (worktree == NULL)
10161 printf("work tree: %.*s\n\n",
10162 GOT_WORKTREE_UUID_STRLEN, wt);
10168 got_object_commit_close(commit);
10174 if (wanted_ref != NULL && !found)
10175 err = got_error_fmt(GOT_ERR_NOT_REF, "%s", wanted_ref);
10180 got_ref_list_free(&refs);
10181 got_pathlist_free(&paths, GOT_PATHLIST_FREE_ALL);
10183 got_reflist_object_id_map_free(refs_idmap);
10185 got_object_commit_close(commit);
10190 * Create new temp "logmsg" ref of the backed-out or cherrypicked commit
10191 * identified by id for log messages to prepopulate the editor on commit.
10193 static const struct got_error *
10194 logmsg_ref(struct got_object_id *id, const char *prefix,
10195 struct got_worktree *worktree, struct got_repository *repo)
10197 const struct got_error *err = NULL;
10198 char *idstr, *ref = NULL, *refname = NULL;
10199 int histedit_in_progress;
10200 int rebase_in_progress, merge_in_progress;
10203 * Silenty refuse to create merge reference if any histedit, merge,
10204 * or rebase operation is in progress.
10206 err = got_worktree_histedit_in_progress(&histedit_in_progress,
10210 if (histedit_in_progress)
10213 err = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
10216 if (rebase_in_progress)
10219 err = got_worktree_merge_in_progress(&merge_in_progress, worktree,
10223 if (merge_in_progress)
10226 err = got_object_id_str(&idstr, id);
10230 err = got_worktree_get_logmsg_ref_name(&refname, worktree, prefix);
10234 if (asprintf(&ref, "%s-%s", refname, idstr) == -1) {
10235 err = got_error_from_errno("asprintf");
10239 err = create_ref(ref, got_worktree_get_base_commit_id(worktree),
10249 usage_cherrypick(void)
10251 fprintf(stderr, "usage: %s cherrypick [-lX] [commit-id]\n",
10256 static const struct got_error *
10257 cmd_cherrypick(int argc, char *argv[])
10259 const struct got_error *error = NULL;
10260 struct got_worktree *worktree = NULL;
10261 struct got_repository *repo = NULL;
10262 char *cwd = NULL, *commit_id_str = NULL, *keyword_idstr = NULL;
10263 struct got_object_id *commit_id = NULL;
10264 struct got_commit_object *commit = NULL;
10265 struct got_object_qid *pid;
10266 int ch, list_refs = 0, remove_refs = 0;
10267 struct got_update_progress_arg upa;
10268 int *pack_fds = NULL;
10271 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10272 "unveil", NULL) == -1)
10276 while ((ch = getopt(argc, argv, "lX")) != -1) {
10285 usage_cherrypick();
10293 if (list_refs || remove_refs) {
10294 if (argc != 0 && argc != 1)
10295 usage_cherrypick();
10296 } else if (argc != 1)
10297 usage_cherrypick();
10298 if (list_refs && remove_refs)
10299 option_conflict('l', 'X');
10301 cwd = getcwd(NULL, 0);
10303 error = got_error_from_errno("getcwd");
10307 error = got_repo_pack_fds_open(&pack_fds);
10311 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
10313 if (list_refs || remove_refs) {
10314 if (error->code != GOT_ERR_NOT_WORKTREE)
10317 if (error->code == GOT_ERR_NOT_WORKTREE)
10318 error = wrap_not_worktree_error(error,
10319 "cherrypick", cwd);
10324 error = got_repo_open(&repo,
10325 worktree ? got_worktree_get_repo_path(worktree) : cwd,
10330 error = apply_unveil(got_repo_get_path(repo), 0,
10331 worktree ? got_worktree_get_root_path(worktree) : NULL);
10335 if (list_refs || remove_refs) {
10336 error = process_logmsg_refs(GOT_WORKTREE_CHERRYPICK_REF_PREFIX,
10337 GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN,
10338 argc == 1 ? argv[0] : NULL, remove_refs, worktree, repo);
10342 error = got_keyword_to_idstr(&keyword_idstr, argv[0], repo, worktree);
10346 error = got_repo_match_object_id(&commit_id, NULL,
10347 keyword_idstr != NULL ? keyword_idstr : argv[0],
10348 GOT_OBJ_TYPE_COMMIT, NULL, repo);
10351 error = got_object_id_str(&commit_id_str, commit_id);
10355 error = got_object_open_as_commit(&commit, repo, commit_id);
10358 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
10359 memset(&upa, 0, sizeof(upa));
10360 error = got_worktree_merge_files(worktree, pid ? &pid->id : NULL,
10361 commit_id, repo, update_progress, &upa, check_cancelled,
10366 if (upa.did_something) {
10367 error = logmsg_ref(commit_id,
10368 GOT_WORKTREE_CHERRYPICK_REF_PREFIX, worktree, repo);
10371 printf("Merged commit %s\n", commit_id_str);
10373 print_merge_progress_stats(&upa);
10376 free(keyword_idstr);
10378 got_object_commit_close(commit);
10379 free(commit_id_str);
10381 got_worktree_close(worktree);
10383 const struct got_error *close_err = got_repo_close(repo);
10388 const struct got_error *pack_err =
10389 got_repo_pack_fds_close(pack_fds);
10398 usage_backout(void)
10400 fprintf(stderr, "usage: %s backout [-lX] [commit-id]\n", getprogname());
10404 static const struct got_error *
10405 cmd_backout(int argc, char *argv[])
10407 const struct got_error *error = NULL;
10408 struct got_worktree *worktree = NULL;
10409 struct got_repository *repo = NULL;
10410 char *cwd = NULL, *commit_id_str = NULL, *keyword_idstr = NULL;
10411 struct got_object_id *commit_id = NULL;
10412 struct got_commit_object *commit = NULL;
10413 struct got_object_qid *pid;
10414 int ch, list_refs = 0, remove_refs = 0;
10415 struct got_update_progress_arg upa;
10416 int *pack_fds = NULL;
10419 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10420 "unveil", NULL) == -1)
10424 while ((ch = getopt(argc, argv, "lX")) != -1) {
10441 if (list_refs || remove_refs) {
10442 if (argc != 0 && argc != 1)
10444 } else if (argc != 1)
10446 if (list_refs && remove_refs)
10447 option_conflict('l', 'X');
10449 cwd = getcwd(NULL, 0);
10451 error = got_error_from_errno("getcwd");
10455 error = got_repo_pack_fds_open(&pack_fds);
10459 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
10461 if (list_refs || remove_refs) {
10462 if (error->code != GOT_ERR_NOT_WORKTREE)
10465 if (error->code == GOT_ERR_NOT_WORKTREE)
10466 error = wrap_not_worktree_error(error,
10472 error = got_repo_open(&repo,
10473 worktree ? got_worktree_get_repo_path(worktree) : cwd,
10478 error = apply_unveil(got_repo_get_path(repo), 0,
10479 worktree ? got_worktree_get_root_path(worktree) : NULL);
10483 if (list_refs || remove_refs) {
10484 error = process_logmsg_refs(GOT_WORKTREE_BACKOUT_REF_PREFIX,
10485 GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN,
10486 argc == 1 ? argv[0] : NULL, remove_refs, worktree, repo);
10490 error = got_keyword_to_idstr(&keyword_idstr, argv[0], repo, worktree);
10494 error = got_repo_match_object_id(&commit_id, NULL,
10495 keyword_idstr != NULL ? keyword_idstr : argv[0],
10496 GOT_OBJ_TYPE_COMMIT, NULL, repo);
10499 error = got_object_id_str(&commit_id_str, commit_id);
10503 error = got_object_open_as_commit(&commit, repo, commit_id);
10506 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
10508 error = got_error(GOT_ERR_ROOT_COMMIT);
10512 memset(&upa, 0, sizeof(upa));
10513 error = got_worktree_merge_files(worktree, commit_id, &pid->id,
10514 repo, update_progress, &upa, check_cancelled, NULL);
10518 if (upa.did_something) {
10519 error = logmsg_ref(commit_id, GOT_WORKTREE_BACKOUT_REF_PREFIX,
10523 printf("Backed out commit %s\n", commit_id_str);
10525 print_merge_progress_stats(&upa);
10528 free(keyword_idstr);
10530 got_object_commit_close(commit);
10531 free(commit_id_str);
10533 got_worktree_close(worktree);
10535 const struct got_error *close_err = got_repo_close(repo);
10540 const struct got_error *pack_err =
10541 got_repo_pack_fds_close(pack_fds);
10551 fprintf(stderr, "usage: %s rebase [-aCclX] [branch]\n", getprogname());
10556 trim_logmsg(char *logmsg, int limit)
10561 len = strlen(logmsg);
10564 logmsg[len] = '\0';
10565 nl = strchr(logmsg, '\n');
10570 static const struct got_error *
10571 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
10573 const struct got_error *err;
10574 char *logmsg0 = NULL;
10577 err = got_object_commit_get_logmsg(&logmsg0, commit);
10582 while (isspace((unsigned char)s[0]))
10585 *logmsg = strdup(s);
10586 if (*logmsg == NULL) {
10587 err = got_error_from_errno("strdup");
10591 trim_logmsg(*logmsg, limit);
10597 static const struct got_error *
10598 show_rebase_merge_conflict(struct got_object_id *id,
10599 struct got_repository *repo)
10601 const struct got_error *err;
10602 struct got_commit_object *commit = NULL;
10603 char *id_str = NULL, *logmsg = NULL;
10605 err = got_object_open_as_commit(&commit, repo, id);
10609 err = got_object_id_str(&id_str, id);
10615 err = get_short_logmsg(&logmsg, 42, commit);
10619 printf("%s -> merge conflict: %s\n", id_str, logmsg);
10622 got_object_commit_close(commit);
10627 static const struct got_error *
10628 show_rebase_progress(struct got_commit_object *commit,
10629 struct got_object_id *old_id, struct got_object_id *new_id)
10631 const struct got_error *err;
10632 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
10634 err = got_object_id_str(&old_id_str, old_id);
10639 err = got_object_id_str(&new_id_str, new_id);
10644 old_id_str[12] = '\0';
10646 new_id_str[12] = '\0';
10648 err = get_short_logmsg(&logmsg, 42, commit);
10652 printf("%s -> %s: %s\n", old_id_str,
10653 new_id_str ? new_id_str : "no-op change", logmsg);
10661 static const struct got_error *
10662 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
10663 struct got_reference *branch, struct got_reference *tmp_branch,
10664 struct got_repository *repo, int create_backup)
10666 printf("Switching work tree to %s\n", got_ref_get_name(branch));
10667 return got_worktree_rebase_complete(worktree, fileindex,
10668 tmp_branch, branch, repo, create_backup);
10671 static const struct got_error *
10672 rebase_commit(struct got_pathlist_head *merged_paths,
10673 struct got_worktree *worktree, struct got_fileindex *fileindex,
10674 struct got_reference *tmp_branch, const char *committer,
10675 struct got_object_id *commit_id, int allow_conflict,
10676 struct got_repository *repo)
10678 const struct got_error *error;
10679 struct got_commit_object *commit;
10680 struct got_object_id *new_commit_id;
10682 error = got_object_open_as_commit(&commit, repo, commit_id);
10686 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
10687 worktree, fileindex, tmp_branch, committer, commit, commit_id,
10688 allow_conflict, repo);
10690 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
10692 error = show_rebase_progress(commit, commit_id, NULL);
10694 error = show_rebase_progress(commit, commit_id, new_commit_id);
10695 free(new_commit_id);
10698 got_object_commit_close(commit);
10702 struct check_path_prefix_arg {
10703 const char *path_prefix;
10708 static const struct got_error *
10709 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
10710 struct got_blob_object *blob2, FILE *f1, FILE *f2,
10711 struct got_object_id *id1, struct got_object_id *id2,
10712 const char *path1, const char *path2,
10713 mode_t mode1, mode_t mode2, struct got_repository *repo)
10715 struct check_path_prefix_arg *a = arg;
10717 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
10718 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
10719 return got_error(a->errcode);
10724 static const struct got_error *
10725 check_path_prefix(struct got_object_id *parent_id,
10726 struct got_object_id *commit_id, const char *path_prefix,
10727 int errcode, struct got_repository *repo)
10729 const struct got_error *err;
10730 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
10731 struct got_commit_object *commit = NULL, *parent_commit = NULL;
10732 struct check_path_prefix_arg cpp_arg;
10734 if (got_path_is_root_dir(path_prefix))
10737 err = got_object_open_as_commit(&commit, repo, commit_id);
10741 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
10745 err = got_object_open_as_tree(&tree1, repo,
10746 got_object_commit_get_tree_id(parent_commit));
10750 err = got_object_open_as_tree(&tree2, repo,
10751 got_object_commit_get_tree_id(commit));
10755 cpp_arg.path_prefix = path_prefix;
10756 while (cpp_arg.path_prefix[0] == '/')
10757 cpp_arg.path_prefix++;
10758 cpp_arg.len = strlen(cpp_arg.path_prefix);
10759 cpp_arg.errcode = errcode;
10760 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
10761 check_path_prefix_in_diff, &cpp_arg, 0);
10764 got_object_tree_close(tree1);
10766 got_object_tree_close(tree2);
10768 got_object_commit_close(commit);
10770 got_object_commit_close(parent_commit);
10774 static const struct got_error *
10775 collect_commits(struct got_object_id_queue *commits,
10776 struct got_object_id *initial_commit_id,
10777 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
10778 const char *path_prefix, int path_prefix_errcode,
10779 struct got_repository *repo)
10781 const struct got_error *err = NULL;
10782 struct got_commit_graph *graph = NULL;
10783 struct got_object_id parent_id, commit_id;
10784 struct got_object_qid *qid;
10786 err = got_commit_graph_open(&graph, "/", 1);
10790 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
10791 check_cancelled, NULL);
10795 memcpy(&commit_id, initial_commit_id, sizeof(commit_id));
10796 while (got_object_id_cmp(&commit_id, iter_stop_id) != 0) {
10797 err = got_commit_graph_iter_next(&parent_id, graph, repo,
10798 check_cancelled, NULL);
10800 if (err->code == GOT_ERR_ITER_COMPLETED) {
10801 err = got_error_msg(GOT_ERR_ANCESTRY,
10802 "ran out of commits to rebase before "
10803 "youngest common ancestor commit has "
10804 "been reached?!?");
10808 err = check_path_prefix(&parent_id, &commit_id,
10809 path_prefix, path_prefix_errcode, repo);
10813 err = got_object_qid_alloc(&qid, &commit_id);
10816 STAILQ_INSERT_HEAD(commits, qid, entry);
10818 memcpy(&commit_id, &parent_id, sizeof(commit_id));
10822 got_commit_graph_close(graph);
10826 static const struct got_error *
10827 get_commit_brief_str(char **brief_str, struct got_commit_object *commit)
10829 const struct got_error *err = NULL;
10830 time_t committer_time;
10832 char datebuf[11]; /* YYYY-MM-DD + NUL */
10833 char *author0 = NULL, *author, *smallerthan;
10834 char *logmsg0 = NULL, *logmsg, *newline;
10836 committer_time = got_object_commit_get_committer_time(commit);
10837 if (gmtime_r(&committer_time, &tm) == NULL)
10838 return got_error_from_errno("gmtime_r");
10839 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d", &tm) == 0)
10840 return got_error(GOT_ERR_NO_SPACE);
10842 author0 = strdup(got_object_commit_get_author(commit));
10843 if (author0 == NULL)
10844 return got_error_from_errno("strdup");
10846 smallerthan = strchr(author, '<');
10847 if (smallerthan && smallerthan[1] != '\0')
10848 author = smallerthan + 1;
10849 author[strcspn(author, "@>")] = '\0';
10851 err = got_object_commit_get_logmsg(&logmsg0, commit);
10855 while (*logmsg == '\n')
10857 newline = strchr(logmsg, '\n');
10861 if (asprintf(brief_str, "%s %s %s",
10862 datebuf, author, logmsg) == -1)
10863 err = got_error_from_errno("asprintf");
10870 static const struct got_error *
10871 delete_backup_ref(struct got_reference *ref, struct got_object_id *id,
10872 struct got_repository *repo)
10874 const struct got_error *err;
10877 err = got_object_id_str(&id_str, id);
10881 err = got_ref_delete(ref, repo);
10885 printf("Deleted %s: %s\n", got_ref_get_name(ref), id_str);
10891 static const struct got_error *
10892 print_backup_ref(const char *branch_name, const char *new_id_str,
10893 struct got_object_id *old_commit_id, struct got_commit_object *old_commit,
10894 struct got_reflist_object_id_map *refs_idmap,
10895 struct got_repository *repo)
10897 const struct got_error *err = NULL;
10898 struct got_reflist_head *refs;
10899 char *refs_str = NULL;
10900 struct got_object_id *new_commit_id = NULL;
10901 struct got_commit_object *new_commit = NULL;
10902 char *new_commit_brief_str = NULL;
10903 struct got_object_id *yca_id = NULL;
10904 struct got_commit_object *yca_commit = NULL;
10905 char *yca_id_str = NULL, *yca_brief_str = NULL;
10906 char *custom_refs_str;
10908 if (asprintf(&custom_refs_str, "formerly %s", branch_name) == -1)
10909 return got_error_from_errno("asprintf");
10911 err = print_commit(old_commit, old_commit_id, repo, NULL, NULL, NULL,
10912 0, 0, refs_idmap, custom_refs_str, NULL);
10916 err = got_object_resolve_id_str(&new_commit_id, repo, new_id_str);
10920 refs = got_reflist_object_id_map_lookup(refs_idmap, new_commit_id);
10922 err = build_refs_str(&refs_str, refs, new_commit_id, repo, 0);
10927 err = got_object_open_as_commit(&new_commit, repo, new_commit_id);
10931 err = get_commit_brief_str(&new_commit_brief_str, new_commit);
10935 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
10936 old_commit_id, new_commit_id, 1, repo, check_cancelled, NULL);
10940 printf("has become commit %s%s%s%s\n %s\n", new_id_str,
10941 refs_str ? " (" : "", refs_str ? refs_str : "",
10942 refs_str ? ")" : "", new_commit_brief_str);
10943 if (yca_id && got_object_id_cmp(yca_id, new_commit_id) != 0 &&
10944 got_object_id_cmp(yca_id, old_commit_id) != 0) {
10948 err = got_object_open_as_commit(&yca_commit, repo, yca_id);
10952 err = get_commit_brief_str(&yca_brief_str, yca_commit);
10956 err = got_object_id_str(&yca_id_str, yca_id);
10960 refs = got_reflist_object_id_map_lookup(refs_idmap, yca_id);
10962 err = build_refs_str(&refs_str, refs, yca_id, repo, 0);
10966 printf("history forked at %s%s%s%s\n %s\n",
10968 refs_str ? " (" : "", refs_str ? refs_str : "",
10969 refs_str ? ")" : "", yca_brief_str);
10972 free(custom_refs_str);
10973 free(new_commit_id);
10977 free(yca_brief_str);
10979 got_object_commit_close(new_commit);
10981 got_object_commit_close(yca_commit);
10986 static const struct got_error *
10987 worktree_has_logmsg_ref(const char *caller, struct got_worktree *worktree,
10988 struct got_repository *repo)
10990 const struct got_error *err;
10991 struct got_reflist_head refs;
10992 struct got_reflist_entry *re;
10993 char *uuidstr = NULL;
10994 static char msg[160];
10998 err = got_worktree_get_uuid(&uuidstr, worktree);
11002 err = got_ref_list(&refs, repo, "refs/got/worktree",
11003 got_ref_cmp_by_name, repo);
11007 TAILQ_FOREACH(re, &refs, entry) {
11008 const char *cmd, *refname, *type;
11010 refname = got_ref_get_name(re->ref);
11012 if (strncmp(refname, GOT_WORKTREE_CHERRYPICK_REF_PREFIX,
11013 GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN) == 0) {
11014 refname += GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN + 1;
11015 cmd = "cherrypick";
11016 type = "cherrypicked";
11017 } else if (strncmp(refname, GOT_WORKTREE_BACKOUT_REF_PREFIX,
11018 GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN) == 0) {
11019 refname += GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN + 1;
11021 type = "backed-out";
11025 if (strncmp(refname, uuidstr, GOT_WORKTREE_UUID_STRLEN) != 0)
11028 snprintf(msg, sizeof(msg),
11029 "work tree has references created by %s commits which "
11030 "must be removed with 'got %s -X' before running the %s "
11031 "command", type, cmd, caller);
11032 err = got_error_msg(GOT_ERR_WORKTREE_META, msg);
11038 got_ref_list_free(&refs);
11042 static const struct got_error *
11043 process_backup_refs(const char *backup_ref_prefix,
11044 const char *wanted_branch_name,
11045 int delete, struct got_repository *repo)
11047 const struct got_error *err;
11048 struct got_reflist_head refs, backup_refs;
11049 struct got_reflist_entry *re;
11050 const size_t backup_ref_prefix_len = strlen(backup_ref_prefix);
11051 struct got_object_id *old_commit_id = NULL;
11052 char *branch_name = NULL;
11053 struct got_commit_object *old_commit = NULL;
11054 struct got_reflist_object_id_map *refs_idmap = NULL;
11055 int wanted_branch_found = 0;
11058 TAILQ_INIT(&backup_refs);
11060 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
11064 err = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
11068 if (wanted_branch_name) {
11069 if (strncmp(wanted_branch_name, "refs/heads/", 11) == 0)
11070 wanted_branch_name += 11;
11073 err = got_ref_list(&backup_refs, repo, backup_ref_prefix,
11074 got_ref_cmp_by_commit_timestamp_descending, repo);
11078 TAILQ_FOREACH(re, &backup_refs, entry) {
11079 const char *refname = got_ref_get_name(re->ref);
11082 err = check_cancelled(NULL);
11086 err = got_ref_resolve(&old_commit_id, repo, re->ref);
11090 err = got_object_open_as_commit(&old_commit, repo,
11095 if (strncmp(backup_ref_prefix, refname,
11096 backup_ref_prefix_len) == 0)
11097 refname += backup_ref_prefix_len;
11099 while (refname[0] == '/')
11102 branch_name = strdup(refname);
11103 if (branch_name == NULL) {
11104 err = got_error_from_errno("strdup");
11107 slash = strrchr(branch_name, '/');
11110 refname += strlen(branch_name) + 1;
11113 if (wanted_branch_name == NULL ||
11114 strcmp(wanted_branch_name, branch_name) == 0) {
11115 wanted_branch_found = 1;
11117 err = delete_backup_ref(re->ref,
11118 old_commit_id, repo);
11120 err = print_backup_ref(branch_name, refname,
11121 old_commit_id, old_commit, refs_idmap,
11128 free(old_commit_id);
11129 old_commit_id = NULL;
11131 branch_name = NULL;
11132 got_object_commit_close(old_commit);
11136 if (wanted_branch_name && !wanted_branch_found) {
11137 err = got_error_fmt(GOT_ERR_NOT_REF,
11138 "%s/%s/", backup_ref_prefix, wanted_branch_name);
11142 got_reflist_object_id_map_free(refs_idmap);
11143 got_ref_list_free(&refs);
11144 got_ref_list_free(&backup_refs);
11145 free(old_commit_id);
11148 got_object_commit_close(old_commit);
11152 static const struct got_error *
11153 abort_progress(void *arg, unsigned char status, const char *path)
11156 * Unversioned files should not clutter progress output when
11157 * an operation is aborted.
11159 if (status == GOT_STATUS_UNVERSIONED)
11162 return update_progress(arg, status, path);
11165 static const struct got_error *
11166 cmd_rebase(int argc, char *argv[])
11168 const struct got_error *error = NULL;
11169 struct got_worktree *worktree = NULL;
11170 struct got_repository *repo = NULL;
11171 struct got_fileindex *fileindex = NULL;
11172 char *cwd = NULL, *committer = NULL, *gitconfig_path = NULL;
11173 struct got_reference *branch = NULL;
11174 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
11175 struct got_object_id *commit_id = NULL, *parent_id = NULL;
11176 struct got_object_id *resume_commit_id = NULL;
11177 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
11178 struct got_object_id *head_commit_id = NULL;
11179 struct got_reference *head_ref = NULL;
11180 struct got_commit_object *commit = NULL;
11181 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
11182 int histedit_in_progress = 0, merge_in_progress = 0;
11183 int create_backup = 1, list_backups = 0, delete_backups = 0;
11184 int allow_conflict = 0;
11185 struct got_object_id_queue commits;
11186 struct got_pathlist_head merged_paths;
11187 const struct got_object_id_queue *parent_ids;
11188 struct got_object_qid *qid, *pid;
11189 struct got_update_progress_arg upa;
11190 int *pack_fds = NULL;
11192 STAILQ_INIT(&commits);
11193 TAILQ_INIT(&merged_paths);
11194 memset(&upa, 0, sizeof(upa));
11197 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
11198 "unveil", NULL) == -1)
11202 while ((ch = getopt(argc, argv, "aCclX")) != -1) {
11208 allow_conflict = 1;
11211 continue_rebase = 1;
11217 delete_backups = 1;
11228 if (list_backups) {
11230 option_conflict('l', 'a');
11231 if (allow_conflict)
11232 option_conflict('l', 'C');
11233 if (continue_rebase)
11234 option_conflict('l', 'c');
11235 if (delete_backups)
11236 option_conflict('l', 'X');
11237 if (argc != 0 && argc != 1)
11239 } else if (delete_backups) {
11241 option_conflict('X', 'a');
11242 if (allow_conflict)
11243 option_conflict('X', 'C');
11244 if (continue_rebase)
11245 option_conflict('X', 'c');
11247 option_conflict('l', 'X');
11248 if (argc != 0 && argc != 1)
11250 } else if (allow_conflict) {
11252 option_conflict('C', 'a');
11253 if (!continue_rebase)
11254 errx(1, "-C option requires -c");
11256 if (abort_rebase && continue_rebase)
11258 else if (abort_rebase || continue_rebase) {
11261 } else if (argc != 1)
11265 cwd = getcwd(NULL, 0);
11267 error = got_error_from_errno("getcwd");
11271 error = got_repo_pack_fds_open(&pack_fds);
11275 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
11277 if (list_backups || delete_backups) {
11278 if (error->code != GOT_ERR_NOT_WORKTREE)
11281 if (error->code == GOT_ERR_NOT_WORKTREE)
11282 error = wrap_not_worktree_error(error,
11288 error = get_gitconfig_path(&gitconfig_path);
11291 error = got_repo_open(&repo,
11292 worktree ? got_worktree_get_repo_path(worktree) : cwd,
11293 gitconfig_path, pack_fds);
11297 if (worktree != NULL && !list_backups && !delete_backups) {
11298 error = worktree_has_logmsg_ref("rebase", worktree, repo);
11303 error = get_author(&committer, repo, worktree);
11304 if (error && error->code != GOT_ERR_COMMIT_NO_AUTHOR)
11307 error = apply_unveil(got_repo_get_path(repo), 0,
11308 worktree ? got_worktree_get_root_path(worktree) : NULL);
11312 if (list_backups || delete_backups) {
11313 error = process_backup_refs(
11314 GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
11315 argc == 1 ? argv[0] : NULL, delete_backups, repo);
11316 goto done; /* nothing else to do */
11319 error = got_worktree_histedit_in_progress(&histedit_in_progress,
11323 if (histedit_in_progress) {
11324 error = got_error(GOT_ERR_HISTEDIT_BUSY);
11328 error = got_worktree_merge_in_progress(&merge_in_progress,
11332 if (merge_in_progress) {
11333 error = got_error(GOT_ERR_MERGE_BUSY);
11337 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
11341 if (abort_rebase) {
11342 if (!rebase_in_progress) {
11343 error = got_error(GOT_ERR_NOT_REBASING);
11346 error = got_worktree_rebase_continue(&resume_commit_id,
11347 &new_base_branch, &tmp_branch, &branch, &fileindex,
11351 printf("Switching work tree to %s\n",
11352 got_ref_get_symref_target(new_base_branch));
11353 error = got_worktree_rebase_abort(worktree, fileindex, repo,
11354 new_base_branch, abort_progress, &upa);
11357 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
11358 print_merge_progress_stats(&upa);
11359 goto done; /* nothing else to do */
11362 if (continue_rebase) {
11363 if (!rebase_in_progress) {
11364 error = got_error(GOT_ERR_NOT_REBASING);
11367 error = got_worktree_rebase_continue(&resume_commit_id,
11368 &new_base_branch, &tmp_branch, &branch, &fileindex,
11373 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
11374 committer, resume_commit_id, allow_conflict, repo);
11378 yca_id = got_object_id_dup(resume_commit_id);
11379 if (yca_id == NULL) {
11380 error = got_error_from_errno("got_object_id_dup");
11384 error = got_ref_open(&branch, repo, argv[0], 0);
11387 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
11388 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
11389 "will not rebase a branch which lives outside "
11390 "the \"refs/heads/\" reference namespace");
11395 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
11399 if (!continue_rebase) {
11400 struct got_object_id *base_commit_id;
11402 error = got_ref_open(&head_ref, repo,
11403 got_worktree_get_head_ref_name(worktree), 0);
11406 error = got_ref_resolve(&head_commit_id, repo, head_ref);
11409 base_commit_id = got_worktree_get_base_commit_id(worktree);
11410 if (got_object_id_cmp(base_commit_id, head_commit_id) != 0) {
11411 error = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
11415 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
11416 base_commit_id, branch_head_commit_id, 1, repo,
11417 check_cancelled, NULL);
11419 if (error->code == GOT_ERR_ANCESTRY) {
11420 error = got_error_msg(GOT_ERR_ANCESTRY,
11421 "specified branch shares no common "
11422 "ancestry with work tree's branch");
11427 if (got_object_id_cmp(base_commit_id, yca_id) == 0) {
11428 struct got_pathlist_head paths;
11429 printf("%s is already based on %s\n",
11430 got_ref_get_name(branch),
11431 got_worktree_get_head_ref_name(worktree));
11432 error = switch_head_ref(branch, branch_head_commit_id,
11436 error = got_worktree_set_base_commit_id(worktree, repo,
11437 branch_head_commit_id);
11440 TAILQ_INIT(&paths);
11441 error = got_pathlist_append(&paths, "", NULL);
11444 error = got_worktree_checkout_files(worktree,
11445 &paths, repo, update_progress, &upa,
11446 check_cancelled, NULL);
11447 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
11450 if (upa.did_something) {
11452 error = got_object_id_str(&id_str,
11453 branch_head_commit_id);
11456 printf("Updated to %s: %s\n",
11457 got_worktree_get_head_ref_name(worktree),
11461 printf("Already up-to-date\n");
11462 print_update_progress_stats(&upa);
11467 commit_id = branch_head_commit_id;
11468 error = got_object_open_as_commit(&commit, repo, commit_id);
11472 parent_ids = got_object_commit_get_parent_ids(commit);
11473 pid = STAILQ_FIRST(parent_ids);
11475 error = collect_commits(&commits, commit_id, &pid->id,
11476 yca_id, got_worktree_get_path_prefix(worktree),
11477 GOT_ERR_REBASE_PATH, repo);
11482 got_object_commit_close(commit);
11485 if (!continue_rebase) {
11486 error = got_worktree_rebase_prepare(&new_base_branch,
11487 &tmp_branch, &fileindex, worktree, branch, repo);
11492 if (STAILQ_EMPTY(&commits)) {
11493 if (continue_rebase) {
11494 error = rebase_complete(worktree, fileindex,
11495 branch, tmp_branch, repo, create_backup);
11498 /* Fast-forward the reference of the branch. */
11499 struct got_object_id *new_head_commit_id;
11501 error = got_ref_resolve(&new_head_commit_id, repo,
11505 error = got_object_id_str(&id_str, new_head_commit_id);
11508 printf("Forwarding %s to commit %s\n",
11509 got_ref_get_name(branch), id_str);
11511 error = got_ref_change_ref(branch,
11512 new_head_commit_id);
11515 /* No backup needed since objects did not change. */
11521 STAILQ_FOREACH(qid, &commits, entry) {
11523 commit_id = &qid->id;
11524 parent_id = pid ? &pid->id : yca_id;
11527 memset(&upa, 0, sizeof(upa));
11528 error = got_worktree_rebase_merge_files(&merged_paths,
11529 worktree, fileindex, parent_id, commit_id, repo,
11530 update_progress, &upa, check_cancelled, NULL);
11534 print_merge_progress_stats(&upa);
11535 if (upa.conflicts > 0 || upa.missing > 0 ||
11536 upa.not_deleted > 0 || upa.unversioned > 0) {
11537 if (upa.conflicts > 0) {
11538 error = show_rebase_merge_conflict(&qid->id,
11543 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
11547 error = rebase_commit(&merged_paths, worktree, fileindex,
11548 tmp_branch, committer, commit_id, 0, repo);
11549 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
11554 if (upa.conflicts > 0 || upa.missing > 0 ||
11555 upa.not_deleted > 0 || upa.unversioned > 0) {
11556 error = got_worktree_rebase_postpone(worktree, fileindex);
11559 if (upa.conflicts > 0 && upa.missing == 0 &&
11560 upa.not_deleted == 0 && upa.unversioned == 0) {
11561 error = got_error_msg(GOT_ERR_CONFLICTS,
11562 "conflicts must be resolved before rebasing "
11564 } else if (upa.conflicts > 0) {
11565 error = got_error_msg(GOT_ERR_CONFLICTS,
11566 "conflicts must be resolved before rebasing "
11567 "can continue; changes destined for some "
11568 "files were not yet merged and should be "
11569 "merged manually if required before the "
11570 "rebase operation is continued");
11572 error = got_error_msg(GOT_ERR_CONFLICTS,
11573 "changes destined for some files were not "
11574 "yet merged and should be merged manually "
11575 "if required before the rebase operation "
11579 error = rebase_complete(worktree, fileindex, branch,
11580 tmp_branch, repo, create_backup);
11584 free(gitconfig_path);
11585 got_object_id_queue_free(&commits);
11586 free(branch_head_commit_id);
11587 free(resume_commit_id);
11588 free(head_commit_id);
11591 got_object_commit_close(commit);
11593 got_ref_close(branch);
11594 if (new_base_branch)
11595 got_ref_close(new_base_branch);
11597 got_ref_close(tmp_branch);
11599 got_ref_close(head_ref);
11601 got_worktree_close(worktree);
11603 const struct got_error *close_err = got_repo_close(repo);
11608 const struct got_error *pack_err =
11609 got_repo_pack_fds_close(pack_fds);
11617 usage_histedit(void)
11619 fprintf(stderr, "usage: %s histedit [-aCcdeflmX] [-F histedit-script] "
11620 "[branch]\n", getprogname());
11624 #define GOT_HISTEDIT_PICK 'p'
11625 #define GOT_HISTEDIT_EDIT 'e'
11626 #define GOT_HISTEDIT_FOLD 'f'
11627 #define GOT_HISTEDIT_DROP 'd'
11628 #define GOT_HISTEDIT_MESG 'm'
11630 static const struct got_histedit_cmd {
11631 unsigned char code;
11634 } got_histedit_cmds[] = {
11635 { GOT_HISTEDIT_PICK, "pick", "use commit" },
11636 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
11637 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
11639 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
11640 { GOT_HISTEDIT_MESG, "mesg",
11641 "single-line log message for commit above (open editor if empty)" },
11644 struct got_histedit_list_entry {
11645 TAILQ_ENTRY(got_histedit_list_entry) entry;
11646 struct got_object_id *commit_id;
11647 const struct got_histedit_cmd *cmd;
11650 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
11652 static const struct got_error *
11653 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
11654 FILE *f, struct got_repository *repo)
11656 const struct got_error *err = NULL;
11657 char *logmsg = NULL, *id_str = NULL;
11658 struct got_commit_object *commit = NULL;
11661 err = got_object_open_as_commit(&commit, repo, commit_id);
11665 err = get_short_logmsg(&logmsg, 34, commit);
11669 err = got_object_id_str(&id_str, commit_id);
11673 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
11675 err = got_ferror(f, GOT_ERR_IO);
11678 got_object_commit_close(commit);
11684 static const struct got_error *
11685 histedit_write_commit_list(struct got_object_id_queue *commits,
11686 FILE *f, int edit_logmsg_only, int fold_only, int drop_only,
11687 int edit_only, struct got_repository *repo)
11689 const struct got_error *err = NULL;
11690 struct got_object_qid *qid;
11691 const char *histedit_cmd = NULL;
11693 if (STAILQ_EMPTY(commits))
11694 return got_error(GOT_ERR_EMPTY_HISTEDIT);
11696 STAILQ_FOREACH(qid, commits, entry) {
11697 histedit_cmd = got_histedit_cmds[0].name;
11699 histedit_cmd = "drop";
11700 else if (edit_only)
11701 histedit_cmd = "edit";
11702 else if (fold_only && STAILQ_NEXT(qid, entry) != NULL)
11703 histedit_cmd = "fold";
11704 err = histedit_write_commit(&qid->id, histedit_cmd, f, repo);
11707 if (edit_logmsg_only) {
11708 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
11710 err = got_ferror(f, GOT_ERR_IO);
11719 static const struct got_error *
11720 write_cmd_list(FILE *f, const char *branch_name,
11721 struct got_object_id_queue *commits)
11723 const struct got_error *err = NULL;
11727 struct got_object_qid *qid;
11729 qid = STAILQ_FIRST(commits);
11730 err = got_object_id_str(&id_str, &qid->id);
11735 "# Editing the history of branch '%s' starting at\n"
11737 "# Commits will be processed in order from top to "
11738 "bottom of this file.\n", branch_name, id_str);
11740 err = got_ferror(f, GOT_ERR_IO);
11744 n = fprintf(f, "# Available histedit commands:\n");
11746 err = got_ferror(f, GOT_ERR_IO);
11750 for (i = 0; i < nitems(got_histedit_cmds); i++) {
11751 const struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
11752 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
11755 err = got_ferror(f, GOT_ERR_IO);
11764 static const struct got_error *
11765 histedit_syntax_error(int lineno)
11767 static char msg[42];
11770 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
11772 if (ret < 0 || (size_t)ret >= sizeof(msg))
11773 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
11775 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
11778 static const struct got_error *
11779 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
11780 char *logmsg, struct got_repository *repo)
11782 const struct got_error *err;
11783 struct got_commit_object *folded_commit = NULL;
11784 char *id_str, *folded_logmsg = NULL;
11786 err = got_object_id_str(&id_str, hle->commit_id);
11790 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
11794 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
11797 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
11798 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
11799 folded_logmsg) == -1) {
11800 err = got_error_from_errno("asprintf");
11804 got_object_commit_close(folded_commit);
11806 free(folded_logmsg);
11810 static struct got_histedit_list_entry *
11811 get_folded_commits(struct got_histedit_list_entry *hle)
11813 struct got_histedit_list_entry *prev, *folded = NULL;
11815 prev = TAILQ_PREV(hle, got_histedit_list, entry);
11816 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
11817 prev->cmd->code == GOT_HISTEDIT_DROP)) {
11818 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
11820 prev = TAILQ_PREV(prev, got_histedit_list, entry);
11826 static const struct got_error *
11827 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
11828 struct got_repository *repo)
11830 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
11831 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
11832 const struct got_error *err = NULL;
11833 struct got_commit_object *commit = NULL;
11836 struct got_histedit_list_entry *folded = NULL;
11838 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
11842 folded = get_folded_commits(hle);
11844 while (folded != hle) {
11845 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
11846 folded = TAILQ_NEXT(folded, entry);
11849 err = append_folded_commit_msg(&new_msg, folded,
11855 folded = TAILQ_NEXT(folded, entry);
11859 err = got_object_id_str(&id_str, hle->commit_id);
11862 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
11865 logmsg_len = asprintf(&new_msg,
11866 "%s\n# original log message of commit %s: %s",
11867 logmsg ? logmsg : "", id_str, orig_logmsg);
11868 if (logmsg_len == -1) {
11869 err = got_error_from_errno("asprintf");
11875 err = got_object_id_str(&id_str, hle->commit_id);
11879 err = got_opentemp_named_fd(&logmsg_path, &fd,
11880 GOT_TMPDIR_STR "/got-logmsg", "");
11884 if (write(fd, logmsg, logmsg_len) == -1) {
11885 err = got_error_from_errno2("write", logmsg_path);
11888 if (close(fd) == -1) {
11889 err = got_error_from_errno2("close", logmsg_path);
11894 err = get_editor(&editor);
11898 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg,
11901 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
11904 hle->logmsg = strdup(new_msg);
11905 if (hle->logmsg == NULL)
11906 err = got_error_from_errno("strdup");
11909 if (fd != -1 && close(fd) == -1 && err == NULL)
11910 err = got_error_from_errno2("close", logmsg_path);
11911 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
11912 err = got_error_from_errno2("unlink", logmsg_path);
11918 got_object_commit_close(commit);
11922 static const struct got_error *
11923 histedit_parse_list(struct got_histedit_list *histedit_cmds,
11924 FILE *f, struct got_repository *repo)
11926 const struct got_error *err = NULL;
11927 char *line = NULL, *p, *end;
11928 size_t i, linesize = 0;
11930 int lineno = 0, lastcmd = -1;
11931 const struct got_histedit_cmd *cmd;
11932 struct got_object_id *commit_id = NULL;
11933 struct got_histedit_list_entry *hle = NULL;
11936 linelen = getline(&line, &linesize, f);
11937 if (linelen == -1) {
11938 const struct got_error *getline_err;
11941 getline_err = got_error_from_errno("getline");
11942 err = got_ferror(f, getline_err->code);
11947 while (isspace((unsigned char)p[0]))
11949 if (p[0] == '#' || p[0] == '\0')
11952 for (i = 0; i < nitems(got_histedit_cmds); i++) {
11953 cmd = &got_histedit_cmds[i];
11954 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
11955 isspace((unsigned char)p[strlen(cmd->name)])) {
11956 p += strlen(cmd->name);
11959 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
11964 if (i == nitems(got_histedit_cmds)) {
11965 err = histedit_syntax_error(lineno);
11968 while (isspace((unsigned char)p[0]))
11970 if (cmd->code == GOT_HISTEDIT_MESG) {
11971 if (lastcmd != GOT_HISTEDIT_PICK &&
11972 lastcmd != GOT_HISTEDIT_EDIT) {
11973 err = got_error(GOT_ERR_HISTEDIT_CMD);
11976 if (p[0] == '\0') {
11977 err = histedit_edit_logmsg(hle, repo);
11981 hle->logmsg = strdup(p);
11982 if (hle->logmsg == NULL) {
11983 err = got_error_from_errno("strdup");
11987 lastcmd = cmd->code;
11991 while (end[0] && !isspace((unsigned char)end[0]))
11995 err = got_object_resolve_id_str(&commit_id, repo, p);
11997 /* override error code */
11998 err = histedit_syntax_error(lineno);
12002 hle = malloc(sizeof(*hle));
12004 err = got_error_from_errno("malloc");
12008 hle->commit_id = commit_id;
12009 hle->logmsg = NULL;
12011 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
12012 lastcmd = cmd->code;
12020 static const struct got_error *
12021 histedit_check_script(struct got_histedit_list *histedit_cmds,
12022 struct got_object_id_queue *commits, struct got_repository *repo)
12024 const struct got_error *err = NULL;
12025 struct got_object_qid *qid;
12026 struct got_histedit_list_entry *hle;
12027 static char msg[92];
12030 if (TAILQ_EMPTY(histedit_cmds))
12031 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
12032 "histedit script contains no commands");
12033 if (STAILQ_EMPTY(commits))
12034 return got_error(GOT_ERR_EMPTY_HISTEDIT);
12036 TAILQ_FOREACH(hle, histedit_cmds, entry) {
12037 struct got_histedit_list_entry *hle2;
12038 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
12041 if (got_object_id_cmp(hle->commit_id,
12042 hle2->commit_id) != 0)
12044 err = got_object_id_str(&id_str, hle->commit_id);
12047 snprintf(msg, sizeof(msg), "commit %s is listed "
12048 "more than once in histedit script", id_str);
12050 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
12054 STAILQ_FOREACH(qid, commits, entry) {
12055 TAILQ_FOREACH(hle, histedit_cmds, entry) {
12056 if (got_object_id_cmp(&qid->id, hle->commit_id) == 0)
12060 err = got_object_id_str(&id_str, &qid->id);
12063 snprintf(msg, sizeof(msg),
12064 "commit %s missing from histedit script", id_str);
12066 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
12070 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
12071 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
12072 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
12073 "last commit in histedit script cannot be folded");
12078 static const struct got_error *
12079 histedit_run_editor(struct got_histedit_list *histedit_cmds,
12080 const char *path, struct got_object_id_queue *commits,
12081 struct got_repository *repo)
12083 const struct got_error *err = NULL;
12084 struct stat st, st2;
12085 struct timespec timeout;
12089 err = get_editor(&editor);
12093 if (stat(path, &st) == -1) {
12094 err = got_error_from_errno2("stat", path);
12098 if (spawn_editor(editor, path) == -1) {
12099 err = got_error_from_errno("failed spawning editor");
12103 timeout.tv_sec = 0;
12104 timeout.tv_nsec = 1;
12105 nanosleep(&timeout, NULL);
12107 if (stat(path, &st2) == -1) {
12108 err = got_error_from_errno2("stat", path);
12112 if (st.st_size == st2.st_size &&
12113 timespeccmp(&st.st_mtim, &st2.st_mtim, ==)) {
12114 err = got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
12115 "no changes made to histedit script, aborting");
12119 f = fopen(path, "re");
12121 err = got_error_from_errno("fopen");
12124 err = histedit_parse_list(histedit_cmds, f, repo);
12128 err = histedit_check_script(histedit_cmds, commits, repo);
12130 if (f && fclose(f) == EOF && err == NULL)
12131 err = got_error_from_errno("fclose");
12136 static const struct got_error *
12137 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
12138 struct got_object_id_queue *, const char *, const char *,
12139 struct got_repository *);
12141 static const struct got_error *
12142 histedit_edit_script(struct got_histedit_list *histedit_cmds,
12143 struct got_object_id_queue *commits, const char *branch_name,
12144 int edit_logmsg_only, int fold_only, int drop_only, int edit_only,
12145 struct got_repository *repo)
12147 const struct got_error *err;
12151 err = got_opentemp_named(&path, &f, "got-histedit", "");
12155 err = write_cmd_list(f, branch_name, commits);
12159 err = histedit_write_commit_list(commits, f, edit_logmsg_only,
12160 fold_only, drop_only, edit_only, repo);
12164 if (drop_only || edit_logmsg_only || fold_only || edit_only) {
12166 err = histedit_parse_list(histedit_cmds, f, repo);
12168 if (fclose(f) == EOF) {
12169 err = got_error_from_errno("fclose");
12173 err = histedit_run_editor(histedit_cmds, path, commits, repo);
12175 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
12176 err->code != GOT_ERR_HISTEDIT_CMD)
12178 err = histedit_edit_list_retry(histedit_cmds, err,
12179 commits, path, branch_name, repo);
12183 if (f && fclose(f) == EOF && err == NULL)
12184 err = got_error_from_errno("fclose");
12185 if (path && unlink(path) != 0 && err == NULL)
12186 err = got_error_from_errno2("unlink", path);
12191 static const struct got_error *
12192 histedit_save_list(struct got_histedit_list *histedit_cmds,
12193 struct got_worktree *worktree, struct got_repository *repo)
12195 const struct got_error *err = NULL;
12198 struct got_histedit_list_entry *hle;
12199 struct got_commit_object *commit = NULL;
12201 err = got_worktree_get_histedit_script_path(&path, worktree);
12205 f = fopen(path, "we");
12207 err = got_error_from_errno2("fopen", path);
12210 TAILQ_FOREACH(hle, histedit_cmds, entry) {
12211 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
12217 int n = fprintf(f, "%c %s\n",
12218 GOT_HISTEDIT_MESG, hle->logmsg);
12220 err = got_ferror(f, GOT_ERR_IO);
12226 if (f && fclose(f) == EOF && err == NULL)
12227 err = got_error_from_errno("fclose");
12230 got_object_commit_close(commit);
12235 histedit_free_list(struct got_histedit_list *histedit_cmds)
12237 struct got_histedit_list_entry *hle;
12239 while ((hle = TAILQ_FIRST(histedit_cmds))) {
12240 TAILQ_REMOVE(histedit_cmds, hle, entry);
12245 static const struct got_error *
12246 histedit_load_list(struct got_histedit_list *histedit_cmds,
12247 const char *path, struct got_repository *repo)
12249 const struct got_error *err = NULL;
12252 f = fopen(path, "re");
12254 err = got_error_from_errno2("fopen", path);
12258 err = histedit_parse_list(histedit_cmds, f, repo);
12260 if (f && fclose(f) == EOF && err == NULL)
12261 err = got_error_from_errno("fclose");
12265 static const struct got_error *
12266 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
12267 const struct got_error *edit_err, struct got_object_id_queue *commits,
12268 const char *path, const char *branch_name, struct got_repository *repo)
12270 const struct got_error *err = NULL, *prev_err = edit_err;
12273 while (resp != 'c' && resp != 'r' && resp != 'a') {
12274 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
12275 "or (a)bort: ", getprogname(), prev_err->msg);
12280 histedit_free_list(histedit_cmds);
12281 err = histedit_run_editor(histedit_cmds, path, commits,
12284 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
12285 err->code != GOT_ERR_HISTEDIT_CMD)
12292 } else if (resp == 'r') {
12293 histedit_free_list(histedit_cmds);
12294 err = histedit_edit_script(histedit_cmds,
12295 commits, branch_name, 0, 0, 0, 0, repo);
12297 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
12298 err->code != GOT_ERR_HISTEDIT_CMD)
12305 } else if (resp == 'a') {
12306 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
12309 printf("invalid response '%c'\n", resp);
12315 static const struct got_error *
12316 histedit_complete(struct got_worktree *worktree,
12317 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
12318 struct got_reference *branch, struct got_repository *repo)
12320 printf("Switching work tree to %s\n",
12321 got_ref_get_symref_target(branch));
12322 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
12326 static const struct got_error *
12327 show_histedit_progress(struct got_commit_object *commit,
12328 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
12330 const struct got_error *err;
12331 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
12333 err = got_object_id_str(&old_id_str, hle->commit_id);
12338 err = got_object_id_str(&new_id_str, new_id);
12343 old_id_str[12] = '\0';
12345 new_id_str[12] = '\0';
12348 logmsg = strdup(hle->logmsg);
12349 if (logmsg == NULL) {
12350 err = got_error_from_errno("strdup");
12353 trim_logmsg(logmsg, 42);
12355 err = get_short_logmsg(&logmsg, 42, commit);
12360 switch (hle->cmd->code) {
12361 case GOT_HISTEDIT_PICK:
12362 case GOT_HISTEDIT_EDIT:
12363 printf("%s -> %s: %s\n", old_id_str,
12364 new_id_str ? new_id_str : "no-op change", logmsg);
12366 case GOT_HISTEDIT_DROP:
12367 case GOT_HISTEDIT_FOLD:
12368 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
12380 static const struct got_error *
12381 histedit_commit(struct got_pathlist_head *merged_paths,
12382 struct got_worktree *worktree, struct got_fileindex *fileindex,
12383 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
12384 const char *committer, int allow_conflict, struct got_repository *repo)
12386 const struct got_error *err;
12387 struct got_commit_object *commit;
12388 struct got_object_id *new_commit_id;
12390 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
12391 && hle->logmsg == NULL) {
12392 err = histedit_edit_logmsg(hle, repo);
12397 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
12401 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
12402 worktree, fileindex, tmp_branch, committer, commit, hle->commit_id,
12403 hle->logmsg, allow_conflict, repo);
12405 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
12407 err = show_histedit_progress(commit, hle, NULL);
12409 err = show_histedit_progress(commit, hle, new_commit_id);
12410 free(new_commit_id);
12413 got_object_commit_close(commit);
12417 static const struct got_error *
12418 histedit_skip_commit(struct got_histedit_list_entry *hle,
12419 struct got_worktree *worktree, struct got_repository *repo)
12421 const struct got_error *error;
12422 struct got_commit_object *commit;
12424 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
12429 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
12433 error = show_histedit_progress(commit, hle, NULL);
12434 got_object_commit_close(commit);
12438 static const struct got_error *
12439 check_local_changes(void *arg, unsigned char status,
12440 unsigned char staged_status, const char *path,
12441 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
12442 struct got_object_id *commit_id, int dirfd, const char *de_name)
12444 int *have_local_changes = arg;
12447 case GOT_STATUS_ADD:
12448 case GOT_STATUS_DELETE:
12449 case GOT_STATUS_MODIFY:
12450 case GOT_STATUS_CONFLICT:
12451 *have_local_changes = 1;
12452 return got_error(GOT_ERR_CANCELLED);
12457 switch (staged_status) {
12458 case GOT_STATUS_ADD:
12459 case GOT_STATUS_DELETE:
12460 case GOT_STATUS_MODIFY:
12461 *have_local_changes = 1;
12462 return got_error(GOT_ERR_CANCELLED);
12470 static const struct got_error *
12471 cmd_histedit(int argc, char *argv[])
12473 const struct got_error *error = NULL;
12474 struct got_worktree *worktree = NULL;
12475 struct got_fileindex *fileindex = NULL;
12476 struct got_repository *repo = NULL;
12477 char *cwd = NULL, *committer = NULL, *gitconfig_path = NULL;
12478 struct got_reference *branch = NULL;
12479 struct got_reference *tmp_branch = NULL;
12480 struct got_object_id *resume_commit_id = NULL;
12481 struct got_object_id *base_commit_id = NULL;
12482 struct got_object_id *head_commit_id = NULL;
12483 struct got_commit_object *commit = NULL;
12484 int ch, rebase_in_progress = 0, merge_in_progress = 0;
12485 struct got_update_progress_arg upa;
12486 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
12487 int drop_only = 0, edit_logmsg_only = 0, fold_only = 0, edit_only = 0;
12488 int allow_conflict = 0, list_backups = 0, delete_backups = 0;
12489 const char *edit_script_path = NULL;
12490 struct got_object_id_queue commits;
12491 struct got_pathlist_head merged_paths;
12492 const struct got_object_id_queue *parent_ids;
12493 struct got_object_qid *pid;
12494 struct got_histedit_list histedit_cmds;
12495 struct got_histedit_list_entry *hle;
12496 int *pack_fds = NULL;
12498 STAILQ_INIT(&commits);
12499 TAILQ_INIT(&histedit_cmds);
12500 TAILQ_INIT(&merged_paths);
12501 memset(&upa, 0, sizeof(upa));
12504 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
12505 "unveil", NULL) == -1)
12509 while ((ch = getopt(argc, argv, "aCcdeF:flmX")) != -1) {
12515 allow_conflict = 1;
12527 edit_script_path = optarg;
12536 edit_logmsg_only = 1;
12539 delete_backups = 1;
12550 if (abort_edit && allow_conflict)
12551 option_conflict('a', 'C');
12552 if (abort_edit && continue_edit)
12553 option_conflict('a', 'c');
12554 if (edit_script_path && allow_conflict)
12555 option_conflict('F', 'C');
12556 if (edit_script_path && edit_logmsg_only)
12557 option_conflict('F', 'm');
12558 if (abort_edit && edit_logmsg_only)
12559 option_conflict('a', 'm');
12560 if (edit_logmsg_only && allow_conflict)
12561 option_conflict('m', 'C');
12562 if (continue_edit && edit_logmsg_only)
12563 option_conflict('c', 'm');
12564 if (abort_edit && fold_only)
12565 option_conflict('a', 'f');
12566 if (fold_only && allow_conflict)
12567 option_conflict('f', 'C');
12568 if (continue_edit && fold_only)
12569 option_conflict('c', 'f');
12570 if (fold_only && edit_logmsg_only)
12571 option_conflict('f', 'm');
12572 if (edit_script_path && fold_only)
12573 option_conflict('F', 'f');
12574 if (abort_edit && edit_only)
12575 option_conflict('a', 'e');
12576 if (continue_edit && edit_only)
12577 option_conflict('c', 'e');
12578 if (edit_only && edit_logmsg_only)
12579 option_conflict('e', 'm');
12580 if (edit_script_path && edit_only)
12581 option_conflict('F', 'e');
12582 if (fold_only && edit_only)
12583 option_conflict('f', 'e');
12584 if (drop_only && abort_edit)
12585 option_conflict('d', 'a');
12586 if (drop_only && allow_conflict)
12587 option_conflict('d', 'C');
12588 if (drop_only && continue_edit)
12589 option_conflict('d', 'c');
12590 if (drop_only && edit_logmsg_only)
12591 option_conflict('d', 'm');
12592 if (drop_only && edit_only)
12593 option_conflict('d', 'e');
12594 if (drop_only && edit_script_path)
12595 option_conflict('d', 'F');
12596 if (drop_only && fold_only)
12597 option_conflict('d', 'f');
12598 if (list_backups) {
12600 option_conflict('l', 'a');
12601 if (allow_conflict)
12602 option_conflict('l', 'C');
12604 option_conflict('l', 'c');
12605 if (edit_script_path)
12606 option_conflict('l', 'F');
12607 if (edit_logmsg_only)
12608 option_conflict('l', 'm');
12610 option_conflict('l', 'd');
12612 option_conflict('l', 'f');
12614 option_conflict('l', 'e');
12615 if (delete_backups)
12616 option_conflict('l', 'X');
12617 if (argc != 0 && argc != 1)
12619 } else if (delete_backups) {
12621 option_conflict('X', 'a');
12622 if (allow_conflict)
12623 option_conflict('X', 'C');
12625 option_conflict('X', 'c');
12627 option_conflict('X', 'd');
12628 if (edit_script_path)
12629 option_conflict('X', 'F');
12630 if (edit_logmsg_only)
12631 option_conflict('X', 'm');
12633 option_conflict('X', 'f');
12635 option_conflict('X', 'e');
12637 option_conflict('X', 'l');
12638 if (argc != 0 && argc != 1)
12640 } else if (allow_conflict && !continue_edit)
12641 errx(1, "-C option requires -c");
12642 else if (argc != 0)
12646 * This command cannot apply unveil(2) in all cases because the
12647 * user may choose to run an editor to edit the histedit script
12648 * and to edit individual commit log messages.
12649 * unveil(2) traverses exec(2); if an editor is used we have to
12650 * apply unveil after edit script and log messages have been written.
12651 * XXX TODO: Make use of unveil(2) where possible.
12654 cwd = getcwd(NULL, 0);
12656 error = got_error_from_errno("getcwd");
12660 error = got_repo_pack_fds_open(&pack_fds);
12664 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
12666 if (list_backups || delete_backups) {
12667 if (error->code != GOT_ERR_NOT_WORKTREE)
12670 if (error->code == GOT_ERR_NOT_WORKTREE)
12671 error = wrap_not_worktree_error(error,
12677 if (list_backups || delete_backups) {
12678 error = got_repo_open(&repo,
12679 worktree ? got_worktree_get_repo_path(worktree) : cwd,
12683 error = apply_unveil(got_repo_get_path(repo), 0,
12684 worktree ? got_worktree_get_root_path(worktree) : NULL);
12687 error = process_backup_refs(
12688 GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
12689 argc == 1 ? argv[0] : NULL, delete_backups, repo);
12690 goto done; /* nothing else to do */
12693 error = get_gitconfig_path(&gitconfig_path);
12696 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
12697 gitconfig_path, pack_fds);
12701 if (worktree != NULL && !list_backups && !delete_backups) {
12702 error = worktree_has_logmsg_ref("histedit", worktree, repo);
12707 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
12710 if (rebase_in_progress) {
12711 error = got_error(GOT_ERR_REBASING);
12715 error = got_worktree_merge_in_progress(&merge_in_progress, worktree,
12719 if (merge_in_progress) {
12720 error = got_error(GOT_ERR_MERGE_BUSY);
12724 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
12728 if (edit_in_progress && edit_logmsg_only) {
12729 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
12730 "histedit operation is in progress in this "
12731 "work tree and must be continued or aborted "
12732 "before the -m option can be used");
12735 if (edit_in_progress && drop_only) {
12736 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
12737 "histedit operation is in progress in this "
12738 "work tree and must be continued or aborted "
12739 "before the -d option can be used");
12742 if (edit_in_progress && fold_only) {
12743 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
12744 "histedit operation is in progress in this "
12745 "work tree and must be continued or aborted "
12746 "before the -f option can be used");
12749 if (edit_in_progress && edit_only) {
12750 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
12751 "histedit operation is in progress in this "
12752 "work tree and must be continued or aborted "
12753 "before the -e option can be used");
12757 if (edit_in_progress && abort_edit) {
12758 error = got_worktree_histedit_continue(&resume_commit_id,
12759 &tmp_branch, &branch, &base_commit_id, &fileindex,
12763 printf("Switching work tree to %s\n",
12764 got_ref_get_symref_target(branch));
12765 error = got_worktree_histedit_abort(worktree, fileindex, repo,
12766 branch, base_commit_id, abort_progress, &upa);
12769 printf("Histedit of %s aborted\n",
12770 got_ref_get_symref_target(branch));
12771 print_merge_progress_stats(&upa);
12772 goto done; /* nothing else to do */
12773 } else if (abort_edit) {
12774 error = got_error(GOT_ERR_NOT_HISTEDIT);
12778 error = get_author(&committer, repo, worktree);
12782 if (continue_edit) {
12785 if (!edit_in_progress) {
12786 error = got_error(GOT_ERR_NOT_HISTEDIT);
12790 error = got_worktree_get_histedit_script_path(&path, worktree);
12794 error = histedit_load_list(&histedit_cmds, path, repo);
12799 error = got_worktree_histedit_continue(&resume_commit_id,
12800 &tmp_branch, &branch, &base_commit_id, &fileindex,
12805 error = got_ref_resolve(&head_commit_id, repo, branch);
12809 error = got_object_open_as_commit(&commit, repo,
12813 parent_ids = got_object_commit_get_parent_ids(commit);
12814 pid = STAILQ_FIRST(parent_ids);
12816 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
12819 error = collect_commits(&commits, head_commit_id, &pid->id,
12820 base_commit_id, got_worktree_get_path_prefix(worktree),
12821 GOT_ERR_HISTEDIT_PATH, repo);
12822 got_object_commit_close(commit);
12827 if (edit_in_progress) {
12828 error = got_error(GOT_ERR_HISTEDIT_BUSY);
12832 error = got_ref_open(&branch, repo,
12833 got_worktree_get_head_ref_name(worktree), 0);
12837 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
12838 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
12839 "will not edit commit history of a branch outside "
12840 "the \"refs/heads/\" reference namespace");
12844 error = got_ref_resolve(&head_commit_id, repo, branch);
12845 got_ref_close(branch);
12850 error = got_object_open_as_commit(&commit, repo,
12854 parent_ids = got_object_commit_get_parent_ids(commit);
12855 pid = STAILQ_FIRST(parent_ids);
12857 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
12860 error = collect_commits(&commits, head_commit_id, &pid->id,
12861 got_worktree_get_base_commit_id(worktree),
12862 got_worktree_get_path_prefix(worktree),
12863 GOT_ERR_HISTEDIT_PATH, repo);
12864 got_object_commit_close(commit);
12869 if (STAILQ_EMPTY(&commits)) {
12870 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
12874 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
12875 &base_commit_id, &fileindex, worktree, repo);
12879 if (edit_script_path) {
12880 error = histedit_load_list(&histedit_cmds,
12881 edit_script_path, repo);
12883 got_worktree_histedit_abort(worktree, fileindex,
12884 repo, branch, base_commit_id,
12885 abort_progress, &upa);
12886 print_merge_progress_stats(&upa);
12890 const char *branch_name;
12891 branch_name = got_ref_get_symref_target(branch);
12892 if (strncmp(branch_name, "refs/heads/", 11) == 0)
12894 error = histedit_edit_script(&histedit_cmds, &commits,
12895 branch_name, edit_logmsg_only, fold_only,
12896 drop_only, edit_only, repo);
12898 got_worktree_histedit_abort(worktree, fileindex,
12899 repo, branch, base_commit_id,
12900 abort_progress, &upa);
12901 print_merge_progress_stats(&upa);
12907 error = histedit_save_list(&histedit_cmds, worktree,
12910 got_worktree_histedit_abort(worktree, fileindex,
12911 repo, branch, base_commit_id,
12912 abort_progress, &upa);
12913 print_merge_progress_stats(&upa);
12919 error = histedit_check_script(&histedit_cmds, &commits, repo);
12923 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
12924 if (resume_commit_id) {
12925 if (got_object_id_cmp(hle->commit_id,
12926 resume_commit_id) != 0)
12929 resume_commit_id = NULL;
12930 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
12931 hle->cmd->code == GOT_HISTEDIT_FOLD) {
12932 error = histedit_skip_commit(hle, worktree,
12937 struct got_pathlist_head paths;
12938 int have_changes = 0;
12940 TAILQ_INIT(&paths);
12941 error = got_pathlist_append(&paths, "", NULL);
12944 error = got_worktree_status(worktree, &paths,
12945 repo, 0, check_local_changes, &have_changes,
12946 check_cancelled, NULL);
12947 got_pathlist_free(&paths,
12948 GOT_PATHLIST_FREE_NONE);
12950 if (error->code != GOT_ERR_CANCELLED)
12952 if (sigint_received || sigpipe_received)
12955 if (have_changes) {
12956 error = histedit_commit(NULL, worktree,
12957 fileindex, tmp_branch, hle,
12958 committer, allow_conflict, repo);
12962 error = got_object_open_as_commit(
12963 &commit, repo, hle->commit_id);
12966 error = show_histedit_progress(commit,
12968 got_object_commit_close(commit);
12977 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
12978 error = histedit_skip_commit(hle, worktree, repo);
12984 error = got_object_open_as_commit(&commit, repo,
12988 parent_ids = got_object_commit_get_parent_ids(commit);
12989 pid = STAILQ_FIRST(parent_ids);
12991 error = got_worktree_histedit_merge_files(&merged_paths,
12992 worktree, fileindex, &pid->id, hle->commit_id, repo,
12993 update_progress, &upa, check_cancelled, NULL);
12996 got_object_commit_close(commit);
12999 print_merge_progress_stats(&upa);
13000 if (upa.conflicts > 0 || upa.missing > 0 ||
13001 upa.not_deleted > 0 || upa.unversioned > 0) {
13002 if (upa.conflicts > 0) {
13003 error = show_rebase_merge_conflict(
13004 hle->commit_id, repo);
13008 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
13012 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
13014 error = got_object_id_str(&id_str, hle->commit_id);
13017 printf("Stopping histedit for amending commit %s\n",
13020 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
13021 error = got_worktree_histedit_postpone(worktree,
13026 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
13027 error = histedit_skip_commit(hle, worktree, repo);
13033 error = histedit_commit(&merged_paths, worktree, fileindex,
13034 tmp_branch, hle, committer, allow_conflict, repo);
13035 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
13040 if (upa.conflicts > 0 || upa.missing > 0 ||
13041 upa.not_deleted > 0 || upa.unversioned > 0) {
13042 error = got_worktree_histedit_postpone(worktree, fileindex);
13045 if (upa.conflicts > 0 && upa.missing == 0 &&
13046 upa.not_deleted == 0 && upa.unversioned == 0) {
13047 error = got_error_msg(GOT_ERR_CONFLICTS,
13048 "conflicts must be resolved before histedit "
13050 } else if (upa.conflicts > 0) {
13051 error = got_error_msg(GOT_ERR_CONFLICTS,
13052 "conflicts must be resolved before histedit "
13053 "can continue; changes destined for some "
13054 "files were not yet merged and should be "
13055 "merged manually if required before the "
13056 "histedit operation is continued");
13058 error = got_error_msg(GOT_ERR_CONFLICTS,
13059 "changes destined for some files were not "
13060 "yet merged and should be merged manually "
13061 "if required before the histedit operation "
13065 error = histedit_complete(worktree, fileindex, tmp_branch,
13070 free(gitconfig_path);
13071 got_object_id_queue_free(&commits);
13072 histedit_free_list(&histedit_cmds);
13073 free(head_commit_id);
13074 free(base_commit_id);
13075 free(resume_commit_id);
13077 got_object_commit_close(commit);
13079 got_ref_close(branch);
13081 got_ref_close(tmp_branch);
13083 got_worktree_close(worktree);
13085 const struct got_error *close_err = got_repo_close(repo);
13090 const struct got_error *pack_err =
13091 got_repo_pack_fds_close(pack_fds);
13099 usage_integrate(void)
13101 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
13105 static const struct got_error *
13106 cmd_integrate(int argc, char *argv[])
13108 const struct got_error *error = NULL;
13109 struct got_repository *repo = NULL;
13110 struct got_worktree *worktree = NULL;
13111 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
13112 const char *branch_arg = NULL;
13113 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
13114 struct got_fileindex *fileindex = NULL;
13115 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
13117 struct got_update_progress_arg upa;
13118 int *pack_fds = NULL;
13121 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
13122 "unveil", NULL) == -1)
13126 while ((ch = getopt(argc, argv, "")) != -1) {
13139 branch_arg = argv[0];
13141 cwd = getcwd(NULL, 0);
13143 error = got_error_from_errno("getcwd");
13147 error = got_repo_pack_fds_open(&pack_fds);
13151 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
13153 if (error->code == GOT_ERR_NOT_WORKTREE)
13154 error = wrap_not_worktree_error(error, "integrate",
13159 error = check_rebase_or_histedit_in_progress(worktree);
13163 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
13168 error = apply_unveil(got_repo_get_path(repo), 0,
13169 got_worktree_get_root_path(worktree));
13173 error = check_merge_in_progress(worktree, repo);
13177 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
13178 error = got_error_from_errno("asprintf");
13182 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
13183 &base_branch_ref, worktree, refname, repo);
13187 refname = strdup(got_ref_get_name(branch_ref));
13188 if (refname == NULL) {
13189 error = got_error_from_errno("strdup");
13190 got_worktree_integrate_abort(worktree, fileindex, repo,
13191 branch_ref, base_branch_ref);
13194 base_refname = strdup(got_ref_get_name(base_branch_ref));
13195 if (base_refname == NULL) {
13196 error = got_error_from_errno("strdup");
13197 got_worktree_integrate_abort(worktree, fileindex, repo,
13198 branch_ref, base_branch_ref);
13201 if (strncmp(base_refname, "refs/heads/", 11) != 0) {
13202 error = got_error(GOT_ERR_INTEGRATE_BRANCH);
13203 got_worktree_integrate_abort(worktree, fileindex, repo,
13204 branch_ref, base_branch_ref);
13208 error = got_ref_resolve(&commit_id, repo, branch_ref);
13212 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
13216 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
13217 error = got_error_msg(GOT_ERR_SAME_BRANCH,
13218 "specified branch has already been integrated");
13219 got_worktree_integrate_abort(worktree, fileindex, repo,
13220 branch_ref, base_branch_ref);
13224 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
13226 if (error->code == GOT_ERR_ANCESTRY)
13227 error = got_error(GOT_ERR_REBASE_REQUIRED);
13228 got_worktree_integrate_abort(worktree, fileindex, repo,
13229 branch_ref, base_branch_ref);
13233 memset(&upa, 0, sizeof(upa));
13234 error = got_worktree_integrate_continue(worktree, fileindex, repo,
13235 branch_ref, base_branch_ref, update_progress, &upa,
13236 check_cancelled, NULL);
13240 printf("Integrated %s into %s\n", refname, base_refname);
13241 print_update_progress_stats(&upa);
13244 const struct got_error *close_err = got_repo_close(repo);
13249 got_worktree_close(worktree);
13251 const struct got_error *pack_err =
13252 got_repo_pack_fds_close(pack_fds);
13257 free(base_commit_id);
13260 free(base_refname);
13267 fprintf(stderr, "usage: %s merge [-aCcn] [branch]\n", getprogname());
13271 static const struct got_error *
13272 cmd_merge(int argc, char *argv[])
13274 const struct got_error *error = NULL;
13275 struct got_worktree *worktree = NULL;
13276 struct got_repository *repo = NULL;
13277 struct got_fileindex *fileindex = NULL;
13278 char *cwd = NULL, *id_str = NULL, *author = NULL;
13279 char *gitconfig_path = NULL;
13280 struct got_reference *branch = NULL, *wt_branch = NULL;
13281 struct got_object_id *branch_tip = NULL, *yca_id = NULL;
13282 struct got_object_id *wt_branch_tip = NULL;
13283 int ch, merge_in_progress = 0, abort_merge = 0, continue_merge = 0;
13284 int allow_conflict = 0, prefer_fast_forward = 1, interrupt_merge = 0;
13285 struct got_update_progress_arg upa;
13286 struct got_object_id *merge_commit_id = NULL;
13287 char *branch_name = NULL;
13288 int *pack_fds = NULL;
13290 memset(&upa, 0, sizeof(upa));
13293 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
13294 "unveil", NULL) == -1)
13298 while ((ch = getopt(argc, argv, "aCcMn")) != -1) {
13304 allow_conflict = 1;
13307 continue_merge = 1;
13310 prefer_fast_forward = 0;
13313 interrupt_merge = 1;
13325 if (continue_merge)
13326 option_conflict('a', 'c');
13327 if (!prefer_fast_forward)
13328 option_conflict('a', 'M');
13329 if (interrupt_merge)
13330 option_conflict('a', 'n');
13331 } else if (continue_merge) {
13332 if (!prefer_fast_forward)
13333 option_conflict('c', 'M');
13334 if (interrupt_merge)
13335 option_conflict('c', 'n');
13337 if (allow_conflict) {
13338 if (!continue_merge)
13339 errx(1, "-C option requires -c");
13341 if (abort_merge || continue_merge) {
13344 } else if (argc != 1)
13347 cwd = getcwd(NULL, 0);
13349 error = got_error_from_errno("getcwd");
13353 error = got_repo_pack_fds_open(&pack_fds);
13357 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
13359 if (error->code == GOT_ERR_NOT_WORKTREE)
13360 error = wrap_not_worktree_error(error,
13365 error = get_gitconfig_path(&gitconfig_path);
13368 error = got_repo_open(&repo,
13369 worktree ? got_worktree_get_repo_path(worktree) : cwd,
13370 gitconfig_path, pack_fds);
13374 if (worktree != NULL) {
13375 error = worktree_has_logmsg_ref("merge", worktree, repo);
13380 error = apply_unveil(got_repo_get_path(repo), 0,
13381 worktree ? got_worktree_get_root_path(worktree) : NULL);
13385 error = check_rebase_or_histedit_in_progress(worktree);
13389 error = got_worktree_merge_in_progress(&merge_in_progress, worktree,
13394 if (merge_in_progress && !(abort_merge || continue_merge)) {
13395 error = got_error(GOT_ERR_MERGE_BUSY);
13399 if (!merge_in_progress && (abort_merge || continue_merge)) {
13400 error = got_error(GOT_ERR_NOT_MERGING);
13405 error = got_worktree_merge_continue(&branch_name,
13406 &branch_tip, &fileindex, worktree, repo);
13409 error = got_worktree_merge_abort(worktree, fileindex, repo,
13410 abort_progress, &upa);
13413 printf("Merge of %s aborted\n", branch_name);
13414 goto done; /* nothing else to do */
13417 if (strncmp(got_worktree_get_head_ref_name(worktree),
13418 "refs/heads/", 11) != 0) {
13419 error = got_error_fmt(GOT_ERR_COMMIT_BRANCH,
13420 "work tree's current branch %s is outside the "
13421 "\"refs/heads/\" reference namespace; "
13422 "update -b required",
13423 got_worktree_get_head_ref_name(worktree));
13427 error = get_author(&author, repo, worktree);
13431 error = got_ref_open(&wt_branch, repo,
13432 got_worktree_get_head_ref_name(worktree), 0);
13435 error = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
13439 if (continue_merge) {
13440 struct got_object_id *base_commit_id;
13441 base_commit_id = got_worktree_get_base_commit_id(worktree);
13442 if (got_object_id_cmp(wt_branch_tip, base_commit_id) != 0) {
13443 error = got_error(GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
13446 error = got_worktree_merge_continue(&branch_name,
13447 &branch_tip, &fileindex, worktree, repo);
13451 error = got_ref_open(&branch, repo, argv[0], 0);
13454 branch_name = strdup(got_ref_get_name(branch));
13455 if (branch_name == NULL) {
13456 error = got_error_from_errno("strdup");
13459 error = got_ref_resolve(&branch_tip, repo, branch);
13464 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
13465 wt_branch_tip, branch_tip, 0, repo,
13466 check_cancelled, NULL);
13467 if (error && error->code != GOT_ERR_ANCESTRY)
13470 if (!continue_merge) {
13471 error = check_path_prefix(wt_branch_tip, branch_tip,
13472 got_worktree_get_path_prefix(worktree),
13473 GOT_ERR_MERGE_PATH, repo);
13476 error = got_worktree_merge_prepare(&fileindex, worktree, repo);
13479 if (prefer_fast_forward && yca_id &&
13480 got_object_id_cmp(wt_branch_tip, yca_id) == 0) {
13481 struct got_pathlist_head paths;
13482 if (interrupt_merge) {
13483 error = got_error_fmt(GOT_ERR_BAD_OPTION,
13484 "there are no changes to merge since %s "
13485 "is already based on %s; merge cannot be "
13486 "interrupted for amending; -n",
13487 branch_name, got_ref_get_name(wt_branch));
13490 printf("Forwarding %s to %s\n",
13491 got_ref_get_name(wt_branch), branch_name);
13492 error = got_ref_change_ref(wt_branch, branch_tip);
13495 error = got_ref_write(wt_branch, repo);
13498 error = got_worktree_set_base_commit_id(worktree, repo,
13502 TAILQ_INIT(&paths);
13503 error = got_pathlist_append(&paths, "", NULL);
13506 error = got_worktree_checkout_files(worktree,
13507 &paths, repo, update_progress, &upa,
13508 check_cancelled, NULL);
13509 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
13512 if (upa.did_something) {
13514 error = got_object_id_str(&id_str, branch_tip);
13517 printf("Updated to commit %s\n", id_str);
13520 printf("Already up-to-date\n");
13521 print_update_progress_stats(&upa);
13524 error = got_worktree_merge_write_refs(worktree, branch, repo);
13528 error = got_worktree_merge_branch(worktree, fileindex,
13529 yca_id, branch_tip, repo, update_progress, &upa,
13530 check_cancelled, NULL);
13533 print_merge_progress_stats(&upa);
13534 if (!upa.did_something) {
13535 error = got_worktree_merge_abort(worktree, fileindex,
13536 repo, abort_progress, &upa);
13539 printf("Already up-to-date\n");
13544 if (interrupt_merge) {
13545 error = got_worktree_merge_postpone(worktree, fileindex);
13548 printf("Merge of %s interrupted on request\n", branch_name);
13549 } else if (upa.conflicts > 0 || upa.missing > 0 ||
13550 upa.not_deleted > 0 || upa.unversioned > 0) {
13551 error = got_worktree_merge_postpone(worktree, fileindex);
13554 if (upa.conflicts > 0 && upa.missing == 0 &&
13555 upa.not_deleted == 0 && upa.unversioned == 0) {
13556 error = got_error_msg(GOT_ERR_CONFLICTS,
13557 "conflicts must be resolved before merging "
13559 } else if (upa.conflicts > 0) {
13560 error = got_error_msg(GOT_ERR_CONFLICTS,
13561 "conflicts must be resolved before merging "
13562 "can continue; changes destined for some "
13563 "files were not yet merged and "
13564 "should be merged manually if required before the "
13565 "merge operation is continued");
13567 error = got_error_msg(GOT_ERR_CONFLICTS,
13568 "changes destined for some "
13569 "files were not yet merged and should be "
13570 "merged manually if required before the "
13571 "merge operation is continued");
13575 error = got_worktree_merge_commit(&merge_commit_id, worktree,
13576 fileindex, author, NULL, 1, branch_tip, branch_name,
13577 allow_conflict, repo, continue_merge ? print_status : NULL,
13581 error = got_worktree_merge_complete(worktree, fileindex, repo);
13584 error = got_object_id_str(&id_str, merge_commit_id);
13587 printf("Merged %s into %s: %s\n", branch_name,
13588 got_worktree_get_head_ref_name(worktree),
13593 free(gitconfig_path);
13595 free(merge_commit_id);
13601 got_ref_close(branch);
13603 got_ref_close(wt_branch);
13605 got_worktree_close(worktree);
13607 const struct got_error *close_err = got_repo_close(repo);
13612 const struct got_error *pack_err =
13613 got_repo_pack_fds_close(pack_fds);
13623 fprintf(stderr, "usage: %s stage [-lpS] [-F response-script] "
13624 "[path ...]\n", getprogname());
13628 static const struct got_error *
13629 print_stage(void *arg, unsigned char status, unsigned char staged_status,
13630 const char *path, struct got_object_id *blob_id,
13631 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
13632 int dirfd, const char *de_name)
13634 const struct got_error *err = NULL;
13635 char *id_str = NULL;
13637 if (staged_status != GOT_STATUS_ADD &&
13638 staged_status != GOT_STATUS_MODIFY &&
13639 staged_status != GOT_STATUS_DELETE)
13642 if (staged_status == GOT_STATUS_ADD ||
13643 staged_status == GOT_STATUS_MODIFY)
13644 err = got_object_id_str(&id_str, staged_blob_id);
13646 err = got_object_id_str(&id_str, blob_id);
13650 printf("%s %c %s\n", id_str, staged_status, path);
13655 static const struct got_error *
13656 cmd_stage(int argc, char *argv[])
13658 const struct got_error *error = NULL;
13659 struct got_repository *repo = NULL;
13660 struct got_worktree *worktree = NULL;
13662 struct got_pathlist_head paths;
13663 int ch, list_stage = 0, pflag = 0, allow_bad_symlinks = 0;
13664 FILE *patch_script_file = NULL;
13665 const char *patch_script_path = NULL;
13666 struct choose_patch_arg cpa;
13667 int *pack_fds = NULL;
13669 TAILQ_INIT(&paths);
13672 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
13673 "unveil", NULL) == -1)
13677 while ((ch = getopt(argc, argv, "F:lpS")) != -1) {
13680 patch_script_path = optarg;
13689 allow_bad_symlinks = 1;
13700 if (list_stage && (pflag || patch_script_path))
13701 errx(1, "-l option cannot be used with other options");
13702 if (patch_script_path && !pflag)
13703 errx(1, "-F option can only be used together with -p option");
13705 cwd = getcwd(NULL, 0);
13707 error = got_error_from_errno("getcwd");
13711 error = got_repo_pack_fds_open(&pack_fds);
13715 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
13717 if (error->code == GOT_ERR_NOT_WORKTREE)
13718 error = wrap_not_worktree_error(error, "stage", cwd);
13722 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
13727 if (patch_script_path) {
13728 patch_script_file = fopen(patch_script_path, "re");
13729 if (patch_script_file == NULL) {
13730 error = got_error_from_errno2("fopen",
13731 patch_script_path);
13735 error = apply_unveil(got_repo_get_path(repo), 0,
13736 got_worktree_get_root_path(worktree));
13740 error = check_merge_in_progress(worktree, repo);
13744 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
13749 error = got_worktree_status(worktree, &paths, repo, 0,
13750 print_stage, NULL, check_cancelled, NULL);
13752 cpa.patch_script_file = patch_script_file;
13753 cpa.action = "stage";
13754 error = got_worktree_stage(worktree, &paths,
13755 pflag ? NULL : print_status, NULL,
13756 pflag ? choose_patch : NULL, &cpa,
13757 allow_bad_symlinks, repo);
13760 if (patch_script_file && fclose(patch_script_file) == EOF &&
13762 error = got_error_from_errno2("fclose", patch_script_path);
13764 const struct got_error *close_err = got_repo_close(repo);
13769 got_worktree_close(worktree);
13771 const struct got_error *pack_err =
13772 got_repo_pack_fds_close(pack_fds);
13776 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
13782 usage_unstage(void)
13784 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
13785 "[path ...]\n", getprogname());
13790 static const struct got_error *
13791 cmd_unstage(int argc, char *argv[])
13793 const struct got_error *error = NULL;
13794 struct got_repository *repo = NULL;
13795 struct got_worktree *worktree = NULL;
13797 struct got_pathlist_head paths;
13799 struct got_update_progress_arg upa;
13800 FILE *patch_script_file = NULL;
13801 const char *patch_script_path = NULL;
13802 struct choose_patch_arg cpa;
13803 int *pack_fds = NULL;
13805 TAILQ_INIT(&paths);
13808 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
13809 "unveil", NULL) == -1)
13813 while ((ch = getopt(argc, argv, "F:p")) != -1) {
13816 patch_script_path = optarg;
13830 if (patch_script_path && !pflag)
13831 errx(1, "-F option can only be used together with -p option");
13833 cwd = getcwd(NULL, 0);
13835 error = got_error_from_errno("getcwd");
13839 error = got_repo_pack_fds_open(&pack_fds);
13843 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
13845 if (error->code == GOT_ERR_NOT_WORKTREE)
13846 error = wrap_not_worktree_error(error, "unstage", cwd);
13850 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
13855 if (patch_script_path) {
13856 patch_script_file = fopen(patch_script_path, "re");
13857 if (patch_script_file == NULL) {
13858 error = got_error_from_errno2("fopen",
13859 patch_script_path);
13864 error = apply_unveil(got_repo_get_path(repo), 0,
13865 got_worktree_get_root_path(worktree));
13869 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
13873 cpa.patch_script_file = patch_script_file;
13874 cpa.action = "unstage";
13875 memset(&upa, 0, sizeof(upa));
13876 error = got_worktree_unstage(worktree, &paths, update_progress,
13877 &upa, pflag ? choose_patch : NULL, &cpa, repo);
13879 print_merge_progress_stats(&upa);
13881 if (patch_script_file && fclose(patch_script_file) == EOF &&
13883 error = got_error_from_errno2("fclose", patch_script_path);
13885 const struct got_error *close_err = got_repo_close(repo);
13890 got_worktree_close(worktree);
13892 const struct got_error *pack_err =
13893 got_repo_pack_fds_close(pack_fds);
13897 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
13905 fprintf(stderr, "usage: %s cat [-P] [-c commit] [-r repository-path] "
13906 "arg ...\n", getprogname());
13910 static const struct got_error *
13911 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
13913 const struct got_error *err;
13914 struct got_blob_object *blob;
13917 fd = got_opentempfd();
13919 return got_error_from_errno("got_opentempfd");
13921 err = got_object_open_as_blob(&blob, repo, id, 8192, fd);
13925 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
13927 if (fd != -1 && close(fd) == -1 && err == NULL)
13928 err = got_error_from_errno("close");
13930 got_object_blob_close(blob);
13934 static const struct got_error *
13935 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
13937 const struct got_error *err;
13938 struct got_tree_object *tree;
13941 err = got_object_open_as_tree(&tree, repo, id);
13945 nentries = got_object_tree_get_nentries(tree);
13946 for (i = 0; i < nentries; i++) {
13947 struct got_tree_entry *te;
13949 if (sigint_received || sigpipe_received)
13951 te = got_object_tree_get_entry(tree, i);
13952 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
13955 fprintf(outfile, "%s %.7o %s\n", id_str,
13956 got_tree_entry_get_mode(te),
13957 got_tree_entry_get_name(te));
13961 got_object_tree_close(tree);
13965 static const struct got_error *
13966 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
13968 const struct got_error *err;
13969 struct got_commit_object *commit;
13970 const struct got_object_id_queue *parent_ids;
13971 struct got_object_qid *pid;
13972 char *id_str = NULL;
13973 const char *logmsg = NULL;
13976 err = got_object_open_as_commit(&commit, repo, id);
13980 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
13984 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
13985 parent_ids = got_object_commit_get_parent_ids(commit);
13986 fprintf(outfile, "numparents %d\n",
13987 got_object_commit_get_nparents(commit));
13988 STAILQ_FOREACH(pid, parent_ids, entry) {
13990 err = got_object_id_str(&pid_str, &pid->id);
13993 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
13996 got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
13997 got_object_commit_get_author_gmtoff(commit));
13998 fprintf(outfile, "%s%s %lld %s\n", GOT_COMMIT_LABEL_AUTHOR,
13999 got_object_commit_get_author(commit),
14000 (long long)got_object_commit_get_author_time(commit),
14003 got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
14004 got_object_commit_get_committer_gmtoff(commit));
14005 fprintf(outfile, "%s%s %lld %s\n", GOT_COMMIT_LABEL_COMMITTER,
14006 got_object_commit_get_committer(commit),
14007 (long long)got_object_commit_get_committer_time(commit),
14010 logmsg = got_object_commit_get_logmsg_raw(commit);
14011 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
14012 fprintf(outfile, "%s", logmsg);
14015 got_object_commit_close(commit);
14019 static const struct got_error *
14020 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
14022 const struct got_error *err;
14023 struct got_tag_object *tag;
14024 char *id_str = NULL;
14025 const char *tagmsg = NULL;
14028 err = got_object_open_as_tag(&tag, repo, id);
14032 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
14036 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
14038 switch (got_object_tag_get_object_type(tag)) {
14039 case GOT_OBJ_TYPE_BLOB:
14040 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
14041 GOT_OBJ_LABEL_BLOB);
14043 case GOT_OBJ_TYPE_TREE:
14044 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
14045 GOT_OBJ_LABEL_TREE);
14047 case GOT_OBJ_TYPE_COMMIT:
14048 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
14049 GOT_OBJ_LABEL_COMMIT);
14051 case GOT_OBJ_TYPE_TAG:
14052 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
14053 GOT_OBJ_LABEL_TAG);
14059 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
14060 got_object_tag_get_name(tag));
14062 got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
14063 got_object_tag_get_tagger_gmtoff(tag));
14064 fprintf(outfile, "%s%s %lld %s\n", GOT_TAG_LABEL_TAGGER,
14065 got_object_tag_get_tagger(tag),
14066 (long long)got_object_tag_get_tagger_time(tag),
14069 tagmsg = got_object_tag_get_message(tag);
14070 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
14071 fprintf(outfile, "%s", tagmsg);
14074 got_object_tag_close(tag);
14078 static const struct got_error *
14079 cmd_cat(int argc, char *argv[])
14081 const struct got_error *error;
14082 struct got_repository *repo = NULL;
14083 struct got_worktree *worktree = NULL;
14084 char *cwd = NULL, *repo_path = NULL, *label = NULL;
14085 char *keyword_idstr = NULL;
14086 const char *commit_id_str = NULL;
14087 struct got_object_id *id = NULL, *commit_id = NULL;
14088 struct got_commit_object *commit = NULL;
14089 int ch, obj_type, i, force_path = 0;
14090 struct got_reflist_head refs;
14091 int *pack_fds = NULL;
14096 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
14101 while ((ch = getopt(argc, argv, "c:Pr:")) != -1) {
14104 commit_id_str = optarg;
14110 repo_path = realpath(optarg, NULL);
14111 if (repo_path == NULL)
14112 return got_error_from_errno2("realpath",
14114 got_path_strip_trailing_slashes(repo_path);
14125 cwd = getcwd(NULL, 0);
14127 error = got_error_from_errno("getcwd");
14131 error = got_repo_pack_fds_open(&pack_fds);
14135 if (repo_path == NULL) {
14136 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
14137 if (error && error->code != GOT_ERR_NOT_WORKTREE)
14140 repo_path = strdup(
14141 got_worktree_get_repo_path(worktree));
14142 if (repo_path == NULL) {
14143 error = got_error_from_errno("strdup");
14147 if (commit_id_str == NULL) {
14148 /* Release work tree lock. */
14149 got_worktree_close(worktree);
14155 if (repo_path == NULL) {
14156 repo_path = strdup(cwd);
14157 if (repo_path == NULL)
14158 return got_error_from_errno("strdup");
14161 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
14166 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
14170 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
14174 if (commit_id_str != NULL) {
14175 error = got_keyword_to_idstr(&keyword_idstr, commit_id_str,
14179 if (keyword_idstr != NULL)
14180 commit_id_str = keyword_idstr;
14181 if (worktree != NULL) {
14182 got_worktree_close(worktree);
14186 commit_id_str = GOT_REF_HEAD;
14187 error = got_repo_match_object_id(&commit_id, NULL,
14188 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
14192 error = got_object_open_as_commit(&commit, repo, commit_id);
14196 for (i = 0; i < argc; i++) {
14198 error = got_object_id_by_path(&id, repo, commit,
14203 error = got_repo_match_object_id(&id, &label, argv[i],
14204 GOT_OBJ_TYPE_ANY, NULL /* do not resolve tags */,
14207 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
14208 error->code != GOT_ERR_NOT_REF)
14210 error = got_object_id_by_path(&id, repo,
14217 error = got_object_get_type(&obj_type, repo, id);
14221 switch (obj_type) {
14222 case GOT_OBJ_TYPE_BLOB:
14223 error = cat_blob(id, repo, stdout);
14225 case GOT_OBJ_TYPE_TREE:
14226 error = cat_tree(id, repo, stdout);
14228 case GOT_OBJ_TYPE_COMMIT:
14229 error = cat_commit(id, repo, stdout);
14231 case GOT_OBJ_TYPE_TAG:
14232 error = cat_tag(id, repo, stdout);
14235 error = got_error(GOT_ERR_OBJ_TYPE);
14249 free(keyword_idstr);
14251 got_object_commit_close(commit);
14253 got_worktree_close(worktree);
14255 const struct got_error *close_err = got_repo_close(repo);
14260 const struct got_error *pack_err =
14261 got_repo_pack_fds_close(pack_fds);
14266 got_ref_list_free(&refs);
14273 fprintf(stderr, "usage: %s info [path ...]\n",
14278 static const struct got_error *
14279 print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
14280 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
14281 struct got_object_id *commit_id)
14283 const struct got_error *err = NULL;
14284 char *id_str = NULL;
14286 struct tm mytm, *tm;
14287 struct got_pathlist_head *paths = arg;
14288 struct got_pathlist_entry *pe;
14291 * Clear error indication from any of the path arguments which
14292 * would cause this file index entry to be displayed.
14294 TAILQ_FOREACH(pe, paths, entry) {
14295 if (got_path_cmp(path, pe->path, strlen(path),
14296 pe->path_len) == 0 ||
14297 got_path_is_child(path, pe->path, pe->path_len))
14298 pe->data = NULL; /* no error */
14301 printf(GOT_COMMIT_SEP_STR);
14303 printf("symlink: %s\n", path);
14304 else if (S_ISREG(mode)) {
14305 printf("file: %s\n", path);
14306 printf("mode: %o\n", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
14307 } else if (S_ISDIR(mode))
14308 printf("directory: %s\n", path);
14310 printf("something: %s\n", path);
14312 tm = localtime_r(&mtime, &mytm);
14315 if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) == 0)
14316 return got_error(GOT_ERR_NO_SPACE);
14317 printf("timestamp: %s\n", datebuf);
14320 err = got_object_id_str(&id_str, blob_id);
14323 printf("based on blob: %s\n", id_str);
14327 if (staged_blob_id) {
14328 err = got_object_id_str(&id_str, staged_blob_id);
14331 printf("based on staged blob: %s\n", id_str);
14336 err = got_object_id_str(&id_str, commit_id);
14339 printf("based on commit: %s\n", id_str);
14346 static const struct got_error *
14347 cmd_info(int argc, char *argv[])
14349 const struct got_error *error = NULL;
14350 struct got_worktree *worktree = NULL;
14351 char *cwd = NULL, *id_str = NULL;
14352 struct got_pathlist_head paths;
14353 char *uuidstr = NULL;
14354 int ch, show_files = 0;
14356 TAILQ_INIT(&paths);
14359 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
14364 while ((ch = getopt(argc, argv, "")) != -1) {
14375 cwd = getcwd(NULL, 0);
14377 error = got_error_from_errno("getcwd");
14381 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
14383 if (error->code == GOT_ERR_NOT_WORKTREE)
14384 error = wrap_not_worktree_error(error, "info", cwd);
14389 /* Remove "wpath cpath proc exec sendfd" promises. */
14390 if (pledge("stdio rpath flock unveil", NULL) == -1)
14393 error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
14398 error = get_worktree_paths_from_argv(&paths, argc, argv,
14405 error = got_object_id_str(&id_str,
14406 got_worktree_get_base_commit_id(worktree));
14410 error = got_worktree_get_uuid(&uuidstr, worktree);
14414 printf("work tree: %s\n", got_worktree_get_root_path(worktree));
14415 printf("work tree base commit: %s\n", id_str);
14416 printf("work tree path prefix: %s\n",
14417 got_worktree_get_path_prefix(worktree));
14418 printf("work tree branch reference: %s\n",
14419 got_worktree_get_head_ref_name(worktree));
14420 printf("work tree UUID: %s\n", uuidstr);
14421 printf("repository: %s\n", got_worktree_get_repo_path(worktree));
14424 struct got_pathlist_entry *pe;
14425 TAILQ_FOREACH(pe, &paths, entry) {
14426 if (pe->path_len == 0)
14429 * Assume this path will fail. This will be corrected
14430 * in print_path_info() in case the path does suceeed.
14432 pe->data = (void *)got_error(GOT_ERR_BAD_PATH);
14434 error = got_worktree_path_info(worktree, &paths,
14435 print_path_info, &paths, check_cancelled, NULL);
14438 TAILQ_FOREACH(pe, &paths, entry) {
14439 if (pe->data != NULL) {
14440 const struct got_error *perr;
14443 error = got_error_fmt(perr->code, "%s",
14451 got_worktree_close(worktree);
14452 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);