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);
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, repo);
3111 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
3114 error = got_worktree_open(&worktree, worktree_path);
3118 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
3122 if (!same_path_prefix) {
3123 error = got_error(GOT_ERR_PATH_PREFIX);
3127 if (commit_id_str) {
3128 struct got_reflist_head refs;
3130 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3135 error = got_keyword_to_idstr(&keyword_idstr, commit_id_str,
3139 if (keyword_idstr != NULL) {
3140 free(commit_id_str);
3141 commit_id_str = keyword_idstr;
3144 error = got_repo_match_object_id(&commit_id, NULL,
3145 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3146 got_ref_list_free(&refs);
3149 error = check_linear_ancestry(commit_id,
3150 got_worktree_get_base_commit_id(worktree), 0, repo);
3151 if (error != NULL) {
3152 if (error->code == GOT_ERR_ANCESTRY) {
3153 error = checkout_ancestry_error(
3154 head_ref, commit_id_str);
3158 error = check_same_branch(commit_id, head_ref, repo);
3160 if (error->code == GOT_ERR_ANCESTRY) {
3161 error = checkout_ancestry_error(
3162 head_ref, commit_id_str);
3166 error = got_worktree_set_base_commit_id(worktree, repo,
3170 /* Expand potentially abbreviated commit ID string. */
3171 free(commit_id_str);
3172 error = got_object_id_str(&commit_id_str, commit_id);
3176 commit_id = got_object_id_dup(
3177 got_worktree_get_base_commit_id(worktree));
3178 if (commit_id == NULL) {
3179 error = got_error_from_errno("got_object_id_dup");
3182 error = got_object_id_str(&commit_id_str, commit_id);
3187 error = got_pathlist_append(&paths, "", NULL);
3190 cpa.worktree_path = worktree_path;
3191 cpa.had_base_commit_ref_error = 0;
3192 cpa.verbosity = verbosity;
3193 error = got_worktree_checkout_files(worktree, &paths, repo,
3194 checkout_progress, &cpa, check_cancelled, NULL);
3198 if (got_ref_is_symbolic(head_ref)) {
3199 error = got_ref_resolve_symbolic(&ref, repo, head_ref);
3202 refname = got_ref_get_name(ref);
3204 refname = got_ref_get_name(head_ref);
3205 printf("Checked out %s: %s\n", refname, commit_id_str);
3206 printf("Now shut up and hack\n");
3207 if (cpa.had_base_commit_ref_error)
3208 show_worktree_base_ref_warning();
3211 const struct got_error *pack_err =
3212 got_repo_pack_fds_close(pack_fds);
3217 got_ref_close(head_ref);
3221 const struct got_error *close_err = got_repo_close(repo);
3225 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
3226 free(commit_id_str);
3229 free(worktree_path);
3234 struct got_update_progress_arg {
3246 print_update_progress_stats(struct got_update_progress_arg *upa)
3248 if (!upa->did_something)
3251 if (upa->conflicts > 0)
3252 printf("Files with new merge conflicts: %d\n", upa->conflicts);
3253 if (upa->obstructed > 0)
3254 printf("File paths obstructed by a non-regular file: %d\n",
3256 if (upa->not_updated > 0)
3257 printf("Files not updated because of existing merge "
3258 "conflicts: %d\n", upa->not_updated);
3262 * The meaning of some status codes differs between merge-style operations and
3263 * update operations. For example, the ! status code means "file was missing"
3264 * if changes were merged into the work tree, and "missing file was restored"
3265 * if the work tree was updated. This function should be used by any operation
3266 * which merges changes into the work tree without updating the work tree.
3269 print_merge_progress_stats(struct got_update_progress_arg *upa)
3271 if (!upa->did_something)
3274 if (upa->conflicts > 0)
3275 printf("Files with new merge conflicts: %d\n", upa->conflicts);
3276 if (upa->obstructed > 0)
3277 printf("File paths obstructed by a non-regular file: %d\n",
3279 if (upa->missing > 0)
3280 printf("Files which had incoming changes but could not be "
3281 "found in the work tree: %d\n", upa->missing);
3282 if (upa->not_deleted > 0)
3283 printf("Files not deleted due to differences in deleted "
3284 "content: %d\n", upa->not_deleted);
3285 if (upa->unversioned > 0)
3286 printf("Files not merged because an unversioned file was "
3287 "found in the work tree: %d\n", upa->unversioned);
3293 fprintf(stderr, "usage: %s update [-q] [-b branch] [-c commit] "
3294 "[path ...]\n", getprogname());
3298 static const struct got_error *
3299 update_progress(void *arg, unsigned char status, const char *path)
3301 struct got_update_progress_arg *upa = arg;
3303 if (status == GOT_STATUS_EXISTS ||
3304 status == GOT_STATUS_BASE_REF_ERR)
3307 upa->did_something = 1;
3309 /* Base commit bump happens silently. */
3310 if (status == GOT_STATUS_BUMP_BASE)
3313 if (status == GOT_STATUS_CONFLICT)
3315 if (status == GOT_STATUS_OBSTRUCTED)
3317 if (status == GOT_STATUS_CANNOT_UPDATE)
3319 if (status == GOT_STATUS_MISSING)
3321 if (status == GOT_STATUS_CANNOT_DELETE)
3323 if (status == GOT_STATUS_UNVERSIONED)
3326 while (path[0] == '/')
3328 if (upa->verbosity >= 0)
3329 printf("%c %s\n", status, path);
3334 static const struct got_error *
3335 switch_head_ref(struct got_reference *head_ref,
3336 struct got_object_id *commit_id, struct got_worktree *worktree,
3337 struct got_repository *repo)
3339 const struct got_error *err = NULL;
3341 int ref_has_moved = 0;
3343 /* Trivial case: switching between two different references. */
3344 if (strcmp(got_ref_get_name(head_ref),
3345 got_worktree_get_head_ref_name(worktree)) != 0) {
3346 printf("Switching work tree from %s to %s\n",
3347 got_worktree_get_head_ref_name(worktree),
3348 got_ref_get_name(head_ref));
3349 return got_worktree_set_head_ref(worktree, head_ref);
3352 err = check_linear_ancestry(commit_id,
3353 got_worktree_get_base_commit_id(worktree), 0, repo);
3355 if (err->code != GOT_ERR_ANCESTRY)
3362 /* Switching to a rebased branch with the same reference name. */
3363 err = got_object_id_str(&base_id_str,
3364 got_worktree_get_base_commit_id(worktree));
3367 printf("Reference %s now points at a different branch\n",
3368 got_worktree_get_head_ref_name(worktree));
3369 printf("Switching work tree from %s to %s\n", base_id_str,
3370 got_worktree_get_head_ref_name(worktree));
3374 static const struct got_error *
3375 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
3377 const struct got_error *err;
3380 err = got_worktree_rebase_in_progress(&in_progress, worktree);
3384 return got_error(GOT_ERR_REBASING);
3386 err = got_worktree_histedit_in_progress(&in_progress, worktree);
3390 return got_error(GOT_ERR_HISTEDIT_BUSY);
3395 static const struct got_error *
3396 check_merge_in_progress(struct got_worktree *worktree,
3397 struct got_repository *repo)
3399 const struct got_error *err;
3402 err = got_worktree_merge_in_progress(&in_progress, worktree, repo);
3406 return got_error(GOT_ERR_MERGE_BUSY);
3411 static const struct got_error *
3412 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
3413 char *argv[], struct got_worktree *worktree)
3415 const struct got_error *err = NULL;
3417 struct got_pathlist_entry *new;
3423 return got_error_from_errno("strdup");
3424 return got_pathlist_append(paths, path, NULL);
3427 for (i = 0; i < argc; i++) {
3428 err = got_worktree_resolve_path(&path, worktree, argv[i]);
3431 err = got_pathlist_insert(&new, paths, path, NULL);
3432 if (err || new == NULL /* duplicate */) {
3442 static const struct got_error *
3443 wrap_not_worktree_error(const struct got_error *orig_err,
3444 const char *cmdname, const char *path)
3446 const struct got_error *err;
3447 struct got_repository *repo;
3448 static char msg[512];
3449 int *pack_fds = NULL;
3451 err = got_repo_pack_fds_open(&pack_fds);
3455 err = got_repo_open(&repo, path, NULL, pack_fds);
3459 snprintf(msg, sizeof(msg),
3460 "'got %s' needs a work tree in addition to a git repository\n"
3461 "Work trees can be checked out from this Git repository with "
3463 "The got(1) manual page contains more information.", cmdname);
3464 err = got_error_msg(GOT_ERR_NOT_WORKTREE, msg);
3466 const struct got_error *close_err = got_repo_close(repo);
3471 const struct got_error *pack_err =
3472 got_repo_pack_fds_close(pack_fds);
3479 static const struct got_error *
3480 cmd_update(int argc, char *argv[])
3482 const struct got_error *error = NULL;
3483 struct got_repository *repo = NULL;
3484 struct got_worktree *worktree = NULL;
3485 char *worktree_path = NULL;
3486 struct got_object_id *commit_id = NULL;
3487 char *commit_id_str = NULL;
3488 const char *branch_name = NULL;
3489 struct got_reference *head_ref = NULL;
3490 struct got_pathlist_head paths;
3491 struct got_pathlist_entry *pe;
3492 int ch, verbosity = 0;
3493 struct got_update_progress_arg upa;
3494 int *pack_fds = NULL;
3499 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3500 "unveil", NULL) == -1)
3504 while ((ch = getopt(argc, argv, "b:c:q")) != -1) {
3507 branch_name = optarg;
3510 commit_id_str = strdup(optarg);
3511 if (commit_id_str == NULL)
3512 return got_error_from_errno("strdup");
3526 worktree_path = getcwd(NULL, 0);
3527 if (worktree_path == NULL) {
3528 error = got_error_from_errno("getcwd");
3532 error = got_repo_pack_fds_open(&pack_fds);
3536 error = got_worktree_open(&worktree, worktree_path);
3538 if (error->code == GOT_ERR_NOT_WORKTREE)
3539 error = wrap_not_worktree_error(error, "update",
3544 error = check_rebase_or_histedit_in_progress(worktree);
3548 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3553 error = apply_unveil(got_repo_get_path(repo), 0,
3554 got_worktree_get_root_path(worktree));
3558 error = check_merge_in_progress(worktree, repo);
3562 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3566 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
3567 got_worktree_get_head_ref_name(worktree), 0);
3570 if (commit_id_str == NULL) {
3571 error = got_ref_resolve(&commit_id, repo, head_ref);
3574 error = got_object_id_str(&commit_id_str, commit_id);
3578 struct got_reflist_head refs;
3579 char *keyword_idstr = NULL;
3583 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3588 error = got_keyword_to_idstr(&keyword_idstr, commit_id_str,
3592 if (keyword_idstr != NULL) {
3593 free(commit_id_str);
3594 commit_id_str = keyword_idstr;
3597 error = got_repo_match_object_id(&commit_id, NULL,
3598 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3599 got_ref_list_free(&refs);
3600 free(commit_id_str);
3601 commit_id_str = NULL;
3604 error = got_object_id_str(&commit_id_str, commit_id);
3610 struct got_object_id *head_commit_id;
3611 TAILQ_FOREACH(pe, &paths, entry) {
3612 if (pe->path_len == 0)
3614 error = got_error_msg(GOT_ERR_BAD_PATH,
3615 "switching between branches requires that "
3616 "the entire work tree gets updated");
3619 error = got_ref_resolve(&head_commit_id, repo, head_ref);
3622 error = check_linear_ancestry(commit_id, head_commit_id, 0,
3624 free(head_commit_id);
3627 error = check_same_branch(commit_id, head_ref, repo);
3630 error = switch_head_ref(head_ref, commit_id, worktree, repo);
3634 error = check_linear_ancestry(commit_id,
3635 got_worktree_get_base_commit_id(worktree), 0, repo);
3636 if (error != NULL) {
3637 if (error->code == GOT_ERR_ANCESTRY)
3638 error = got_error(GOT_ERR_BRANCH_MOVED);
3641 error = check_same_branch(commit_id, head_ref, repo);
3646 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
3648 error = got_worktree_set_base_commit_id(worktree, repo,
3654 memset(&upa, 0, sizeof(upa));
3655 upa.verbosity = verbosity;
3656 error = got_worktree_checkout_files(worktree, &paths, repo,
3657 update_progress, &upa, check_cancelled, NULL);
3661 if (upa.did_something) {
3662 printf("Updated to %s: %s\n",
3663 got_worktree_get_head_ref_name(worktree), commit_id_str);
3665 printf("Already up-to-date\n");
3667 print_update_progress_stats(&upa);
3670 const struct got_error *pack_err =
3671 got_repo_pack_fds_close(pack_fds);
3676 const struct got_error *close_err = got_repo_close(repo);
3680 free(worktree_path);
3681 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
3683 free(commit_id_str);
3687 static const struct got_error *
3688 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
3689 const char *path, int diff_context, int ignore_whitespace,
3690 int force_text_diff, struct got_diffstat_cb_arg *dsa,
3691 struct got_repository *repo, FILE *outfile)
3693 const struct got_error *err = NULL;
3694 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
3695 FILE *f1 = NULL, *f2 = NULL;
3696 int fd1 = -1, fd2 = -1;
3698 fd1 = got_opentempfd();
3700 return got_error_from_errno("got_opentempfd");
3701 fd2 = got_opentempfd();
3703 err = got_error_from_errno("got_opentempfd");
3708 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192,
3714 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192, fd2);
3718 f1 = got_opentemp();
3720 err = got_error_from_errno("got_opentemp");
3723 f2 = got_opentemp();
3725 err = got_error_from_errno("got_opentemp");
3729 while (path[0] == '/')
3731 err = got_diff_blob(NULL, NULL, blob1, blob2, f1, f2, path, path,
3732 GOT_DIFF_ALGORITHM_PATIENCE, diff_context, ignore_whitespace,
3733 force_text_diff, dsa, outfile);
3735 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3736 err = got_error_from_errno("close");
3738 got_object_blob_close(blob1);
3739 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3740 err = got_error_from_errno("close");
3742 got_object_blob_close(blob2);
3743 if (f1 && fclose(f1) == EOF && err == NULL)
3744 err = got_error_from_errno("fclose");
3745 if (f2 && fclose(f2) == EOF && err == NULL)
3746 err = got_error_from_errno("fclose");
3750 static const struct got_error *
3751 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
3752 const char *path, int diff_context, int ignore_whitespace,
3753 int force_text_diff, struct got_diffstat_cb_arg *dsa,
3754 struct got_repository *repo, FILE *outfile)
3756 const struct got_error *err = NULL;
3757 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3758 struct got_diff_blob_output_unidiff_arg arg;
3759 FILE *f1 = NULL, *f2 = NULL;
3760 int fd1 = -1, fd2 = -1;
3763 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3766 fd1 = got_opentempfd();
3768 err = got_error_from_errno("got_opentempfd");
3773 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3777 f1 = got_opentemp();
3779 err = got_error_from_errno("got_opentemp");
3783 f2 = got_opentemp();
3785 err = got_error_from_errno("got_opentemp");
3788 fd2 = got_opentempfd();
3790 err = got_error_from_errno("got_opentempfd");
3793 arg.diff_context = diff_context;
3794 arg.ignore_whitespace = ignore_whitespace;
3795 arg.force_text_diff = force_text_diff;
3797 arg.diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
3798 arg.outfile = outfile;
3801 while (path[0] == '/')
3803 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, path, path, repo,
3804 got_diff_blob_output_unidiff, &arg, 1);
3807 got_object_tree_close(tree1);
3809 got_object_tree_close(tree2);
3810 if (f1 && fclose(f1) == EOF && err == NULL)
3811 err = got_error_from_errno("fclose");
3812 if (f2 && fclose(f2) == EOF && err == NULL)
3813 err = got_error_from_errno("fclose");
3814 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3815 err = got_error_from_errno("close");
3816 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3817 err = got_error_from_errno("close");
3821 static const struct got_error *
3822 get_changed_paths(struct got_pathlist_head *paths,
3823 struct got_commit_object *commit, struct got_repository *repo,
3824 struct got_diffstat_cb_arg *dsa)
3826 const struct got_error *err = NULL;
3827 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3828 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3829 struct got_object_qid *qid;
3830 got_diff_blob_cb cb = got_diff_tree_collect_changed_paths;
3831 FILE *f1 = NULL, *f2 = NULL;
3832 int fd1 = -1, fd2 = -1;
3835 cb = got_diff_tree_compute_diffstat;
3837 f1 = got_opentemp();
3839 err = got_error_from_errno("got_opentemp");
3842 f2 = got_opentemp();
3844 err = got_error_from_errno("got_opentemp");
3847 fd1 = got_opentempfd();
3849 err = got_error_from_errno("got_opentempfd");
3852 fd2 = got_opentempfd();
3854 err = got_error_from_errno("got_opentempfd");
3859 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3861 struct got_commit_object *pcommit;
3862 err = got_object_open_as_commit(&pcommit, repo,
3867 tree_id1 = got_object_id_dup(
3868 got_object_commit_get_tree_id(pcommit));
3869 if (tree_id1 == NULL) {
3870 got_object_commit_close(pcommit);
3871 return got_error_from_errno("got_object_id_dup");
3873 got_object_commit_close(pcommit);
3878 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3883 tree_id2 = got_object_commit_get_tree_id(commit);
3884 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3888 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3889 cb, dsa ? (void *)dsa : paths, dsa ? 1 : 0);
3892 got_object_tree_close(tree1);
3894 got_object_tree_close(tree2);
3895 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3896 err = got_error_from_errno("close");
3897 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3898 err = got_error_from_errno("close");
3899 if (f1 && fclose(f1) == EOF && err == NULL)
3900 err = got_error_from_errno("fclose");
3901 if (f2 && fclose(f2) == EOF && err == NULL)
3902 err = got_error_from_errno("fclose");
3907 static const struct got_error *
3908 print_patch(struct got_commit_object *commit, struct got_object_id *id,
3909 const char *path, int diff_context, struct got_diffstat_cb_arg *dsa,
3910 struct got_repository *repo, FILE *outfile)
3912 const struct got_error *err = NULL;
3913 struct got_commit_object *pcommit = NULL;
3914 char *id_str1 = NULL, *id_str2 = NULL;
3915 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
3916 struct got_object_qid *qid;
3918 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3920 err = got_object_open_as_commit(&pcommit, repo,
3924 err = got_object_id_str(&id_str1, &qid->id);
3929 err = got_object_id_str(&id_str2, id);
3933 if (path && path[0] != '\0') {
3935 err = got_object_id_by_path(&obj_id2, repo, commit, path);
3939 err = got_object_id_by_path(&obj_id1, repo,
3942 if (err->code != GOT_ERR_NO_TREE_ENTRY) {
3948 err = got_object_get_type(&obj_type, repo, obj_id2);
3954 "diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3955 fprintf(outfile, "commit - %s\n",
3956 id_str1 ? id_str1 : "/dev/null");
3957 fprintf(outfile, "commit + %s\n", id_str2);
3959 case GOT_OBJ_TYPE_BLOB:
3960 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
3961 0, 0, dsa, repo, outfile);
3963 case GOT_OBJ_TYPE_TREE:
3964 err = diff_trees(obj_id1, obj_id2, path, diff_context,
3965 0, 0, dsa, repo, outfile);
3968 err = got_error(GOT_ERR_OBJ_TYPE);
3974 obj_id2 = got_object_commit_get_tree_id(commit);
3976 obj_id1 = got_object_commit_get_tree_id(pcommit);
3978 "diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3979 fprintf(outfile, "commit - %s\n",
3980 id_str1 ? id_str1 : "/dev/null");
3981 fprintf(outfile, "commit + %s\n", id_str2);
3982 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, 0,
3983 dsa, repo, outfile);
3989 got_object_commit_close(pcommit);
3994 get_datestr(time_t *time, char *datebuf)
3996 struct tm mytm, *tm;
3999 tm = gmtime_r(time, &mytm);
4002 s = asctime_r(tm, datebuf);
4005 p = strchr(s, '\n');
4011 static const struct got_error *
4012 match_commit(int *have_match, struct got_object_id *id,
4013 struct got_commit_object *commit, regex_t *regex)
4015 const struct got_error *err = NULL;
4016 regmatch_t regmatch;
4017 char *id_str = NULL, *logmsg = NULL;
4021 err = got_object_id_str(&id_str, id);
4025 err = got_object_commit_get_logmsg(&logmsg, commit);
4029 if (regexec(regex, got_object_commit_get_author(commit), 1,
4030 ®match, 0) == 0 ||
4031 regexec(regex, got_object_commit_get_committer(commit), 1,
4032 ®match, 0) == 0 ||
4033 regexec(regex, id_str, 1, ®match, 0) == 0 ||
4034 regexec(regex, logmsg, 1, ®match, 0) == 0)
4043 match_changed_paths(int *have_match, struct got_pathlist_head *changed_paths,
4046 regmatch_t regmatch;
4047 struct got_pathlist_entry *pe;
4051 TAILQ_FOREACH(pe, changed_paths, entry) {
4052 if (regexec(regex, pe->path, 1, ®match, 0) == 0) {
4059 static const struct got_error *
4060 match_patch(int *have_match, struct got_commit_object *commit,
4061 struct got_object_id *id, const char *path, int diff_context,
4062 struct got_repository *repo, regex_t *regex, FILE *f)
4064 const struct got_error *err = NULL;
4066 size_t linesize = 0;
4067 regmatch_t regmatch;
4071 err = got_opentemp_truncate(f);
4075 err = print_patch(commit, id, path, diff_context, NULL, repo, f);
4079 if (fseeko(f, 0L, SEEK_SET) == -1) {
4080 err = got_error_from_errno("fseeko");
4084 while (getline(&line, &linesize, f) != -1) {
4085 if (regexec(regex, line, 1, ®match, 0) == 0) {
4095 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
4097 static const struct got_error*
4098 build_refs_str(char **refs_str, struct got_reflist_head *refs,
4099 struct got_object_id *id, struct got_repository *repo,
4102 static const struct got_error *err = NULL;
4103 struct got_reflist_entry *re;
4109 TAILQ_FOREACH(re, refs, entry) {
4110 struct got_tag_object *tag = NULL;
4111 struct got_object_id *ref_id;
4114 name = got_ref_get_name(re->ref);
4115 if (strcmp(name, GOT_REF_HEAD) == 0)
4117 if (strncmp(name, "refs/", 5) == 0)
4119 if (strncmp(name, "got/", 4) == 0)
4121 if (strncmp(name, "heads/", 6) == 0)
4123 if (strncmp(name, "remotes/", 8) == 0) {
4127 s = strstr(name, "/" GOT_REF_HEAD);
4128 if (s != NULL && strcmp(s, "/" GOT_REF_HEAD) == 0)
4131 err = got_ref_resolve(&ref_id, repo, re->ref);
4134 if (strncmp(name, "tags/", 5) == 0) {
4135 err = got_object_open_as_tag(&tag, repo, ref_id);
4137 if (err->code != GOT_ERR_OBJ_TYPE) {
4141 /* Ref points at something other than a tag. */
4146 cmp = got_object_id_cmp(tag ?
4147 got_object_tag_get_object_id(tag) : ref_id, id);
4150 got_object_tag_close(tag);
4154 if (asprintf(refs_str, "%s%s%s", s ? s : "",
4155 s ? ", " : "", name) == -1) {
4156 err = got_error_from_errno("asprintf");
4167 static const struct got_error *
4168 print_commit_oneline(struct got_commit_object *commit, struct got_object_id *id,
4169 struct got_repository *repo, struct got_reflist_object_id_map *refs_idmap)
4171 const struct got_error *err = NULL;
4172 char *ref_str = NULL, *id_str = NULL, *logmsg0 = NULL;
4173 char *comma, *s, *nl;
4174 struct got_reflist_head *refs;
4175 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
4177 time_t committer_time;
4179 refs = got_reflist_object_id_map_lookup(refs_idmap, id);
4181 err = build_refs_str(&ref_str, refs, id, repo, 1);
4185 /* Display the first matching ref only. */
4186 if (ref_str && (comma = strchr(ref_str, ',')) != NULL)
4190 if (ref_str == NULL) {
4191 err = got_object_id_str(&id_str, id);
4196 committer_time = got_object_commit_get_committer_time(commit);
4197 if (gmtime_r(&committer_time, &tm) == NULL) {
4198 err = got_error_from_errno("gmtime_r");
4201 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0) {
4202 err = got_error(GOT_ERR_NO_SPACE);
4206 err = got_object_commit_get_logmsg(&logmsg0, commit);
4211 while (isspace((unsigned char)s[0]))
4214 nl = strchr(s, '\n');
4220 printf("%s%-7s %s\n", datebuf, ref_str, s);
4222 printf("%s%.7s %s\n", datebuf, id_str, s);
4224 if (fflush(stdout) != 0 && err == NULL)
4225 err = got_error_from_errno("fflush");
4233 static const struct got_error *
4234 print_diffstat(struct got_diffstat_cb_arg *dsa, const char *header)
4236 struct got_pathlist_entry *pe;
4239 printf("%s\n", header);
4241 TAILQ_FOREACH(pe, dsa->paths, entry) {
4242 struct got_diff_changed_path *cp = pe->data;
4243 int pad = dsa->max_path_len - pe->path_len + 1;
4245 printf(" %c %s%*c | %*d+ %*d-\n", cp->status, pe->path, pad,
4246 ' ', dsa->add_cols + 1, cp->add, dsa->rm_cols + 1, cp->rm);
4248 printf("\n%d file%s changed, %d insertion%s(+), %d deletion%s(-)\n\n",
4249 dsa->nfiles, dsa->nfiles > 1 ? "s" : "", dsa->ins,
4250 dsa->ins != 1 ? "s" : "", dsa->del, dsa->del != 1 ? "s" : "");
4252 if (fflush(stdout) != 0)
4253 return got_error_from_errno("fflush");
4258 static const struct got_error *
4264 if (fseeko(f, 0L, SEEK_SET) == -1)
4265 return got_error_from_errno("fseek");
4268 r = fread(buf, 1, sizeof(buf), f);
4271 return got_error_from_errno("fread");
4275 if (fwrite(buf, 1, r, stdout) != r)
4276 return got_ferror(stdout, GOT_ERR_IO);
4282 static const struct got_error *
4283 print_commit(struct got_commit_object *commit, struct got_object_id *id,
4284 struct got_repository *repo, const char *path,
4285 struct got_pathlist_head *changed_paths,
4286 struct got_diffstat_cb_arg *diffstat, int show_patch, int diff_context,
4287 struct got_reflist_object_id_map *refs_idmap, const char *custom_refs_str,
4290 const struct got_error *err = NULL;
4292 char *id_str, *datestr, *logmsg0, *logmsg, *line;
4294 time_t committer_time;
4295 const char *author, *committer;
4296 char *refs_str = NULL;
4298 err = got_object_id_str(&id_str, id);
4302 if (custom_refs_str == NULL) {
4303 struct got_reflist_head *refs;
4304 refs = got_reflist_object_id_map_lookup(refs_idmap, id);
4306 err = build_refs_str(&refs_str, refs, id, repo, 0);
4312 printf(GOT_COMMIT_SEP_STR);
4313 if (custom_refs_str)
4314 printf("%s %s (%s)\n", prefix ? prefix : "commit", id_str,
4317 printf("%s %s%s%s%s\n", prefix ? prefix : "commit", id_str,
4318 refs_str ? " (" : "", refs_str ? refs_str : "",
4319 refs_str ? ")" : "");
4324 printf("from: %s\n", got_object_commit_get_author(commit));
4325 author = got_object_commit_get_author(commit);
4326 committer = got_object_commit_get_committer(commit);
4327 if (strcmp(author, committer) != 0)
4328 printf("via: %s\n", committer);
4329 committer_time = got_object_commit_get_committer_time(commit);
4330 datestr = get_datestr(&committer_time, datebuf);
4332 printf("date: %s UTC\n", datestr);
4333 if (got_object_commit_get_nparents(commit) > 1) {
4334 const struct got_object_id_queue *parent_ids;
4335 struct got_object_qid *qid;
4337 parent_ids = got_object_commit_get_parent_ids(commit);
4338 STAILQ_FOREACH(qid, parent_ids, entry) {
4339 err = got_object_id_str(&id_str, &qid->id);
4342 printf("parent %d: %s\n", n++, id_str);
4348 err = got_object_commit_get_logmsg(&logmsg0, commit);
4354 line = strsep(&logmsg, "\n");
4356 printf(" %s\n", line);
4360 if (changed_paths && diffstat == NULL) {
4361 struct got_pathlist_entry *pe;
4363 TAILQ_FOREACH(pe, changed_paths, entry) {
4364 struct got_diff_changed_path *cp = pe->data;
4366 printf(" %c %s\n", cp->status, pe->path);
4374 err = got_error_from_errno("got_opentemp");
4379 err = print_patch(commit, id, path, diff_context, diffstat,
4380 repo, diffstat == NULL ? stdout : f);
4385 err = print_diffstat(diffstat, NULL);
4397 if (fflush(stdout) != 0 && err == NULL)
4398 err = got_error_from_errno("fflush");
4400 if (f && fclose(f) == EOF && err == NULL)
4401 err = got_error_from_errno("fclose");
4407 static const struct got_error *
4408 print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
4409 struct got_repository *repo, const char *path, int show_changed_paths,
4410 int show_diffstat, int show_patch, const char *search_pattern,
4411 int diff_context, int limit, int log_branches, int reverse_display_order,
4412 struct got_reflist_object_id_map *refs_idmap, int one_line,
4415 const struct got_error *err;
4416 struct got_commit_graph *graph;
4419 struct got_object_id_queue reversed_commits;
4420 struct got_object_qid *qid;
4421 struct got_commit_object *commit;
4422 struct got_pathlist_head changed_paths;
4424 STAILQ_INIT(&reversed_commits);
4425 TAILQ_INIT(&changed_paths);
4427 if (search_pattern && regcomp(®ex, search_pattern,
4428 REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
4429 return got_error_msg(GOT_ERR_REGEX, search_pattern);
4431 err = got_commit_graph_open(&graph, path, !log_branches);
4434 err = got_commit_graph_iter_start(graph, root_id, repo,
4435 check_cancelled, NULL);
4439 struct got_object_id id;
4440 struct got_diffstat_cb_arg dsa = { 0, 0, 0, 0, 0, 0,
4441 &changed_paths, 0, 0, GOT_DIFF_ALGORITHM_PATIENCE };
4443 if (sigint_received || sigpipe_received)
4446 err = got_commit_graph_iter_next(&id, graph, repo,
4447 check_cancelled, NULL);
4449 if (err->code == GOT_ERR_ITER_COMPLETED)
4454 err = got_object_open_as_commit(&commit, repo, &id);
4458 if ((show_changed_paths || (show_diffstat && !show_patch))
4459 && !reverse_display_order) {
4460 err = get_changed_paths(&changed_paths, commit, repo,
4461 show_diffstat ? &dsa : NULL);
4466 if (search_pattern) {
4467 err = match_commit(&have_match, &id, commit, ®ex);
4469 got_object_commit_close(commit);
4472 if (have_match == 0 && show_changed_paths)
4473 match_changed_paths(&have_match,
4474 &changed_paths, ®ex);
4475 if (have_match == 0 && show_patch) {
4476 err = match_patch(&have_match, commit, &id,
4477 path, diff_context, repo, ®ex, tmpfile);
4481 if (have_match == 0) {
4482 got_object_commit_close(commit);
4483 got_pathlist_free(&changed_paths,
4484 GOT_PATHLIST_FREE_ALL);
4489 if (reverse_display_order) {
4490 err = got_object_qid_alloc(&qid, &id);
4493 STAILQ_INSERT_HEAD(&reversed_commits, qid, entry);
4494 got_object_commit_close(commit);
4497 err = print_commit_oneline(commit, &id,
4500 err = print_commit(commit, &id, repo, path,
4501 (show_changed_paths || show_diffstat) ?
4502 &changed_paths : NULL,
4503 show_diffstat ? &dsa : NULL, show_patch,
4504 diff_context, refs_idmap, NULL, NULL);
4505 got_object_commit_close(commit);
4509 if ((limit && --limit == 0) ||
4510 (end_id && got_object_id_cmp(&id, end_id) == 0))
4513 got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
4515 if (reverse_display_order) {
4516 STAILQ_FOREACH(qid, &reversed_commits, entry) {
4517 struct got_diffstat_cb_arg dsa = { 0, 0, 0, 0, 0, 0,
4518 &changed_paths, 0, 0, GOT_DIFF_ALGORITHM_PATIENCE };
4520 err = got_object_open_as_commit(&commit, repo,
4524 if (show_changed_paths ||
4525 (show_diffstat && !show_patch)) {
4526 err = get_changed_paths(&changed_paths, commit,
4527 repo, show_diffstat ? &dsa : NULL);
4532 err = print_commit_oneline(commit, &qid->id,
4535 err = print_commit(commit, &qid->id, repo, path,
4536 (show_changed_paths || show_diffstat) ?
4537 &changed_paths : NULL,
4538 show_diffstat ? &dsa : NULL, show_patch,
4539 diff_context, refs_idmap, NULL, NULL);
4540 got_object_commit_close(commit);
4543 got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
4547 while (!STAILQ_EMPTY(&reversed_commits)) {
4548 qid = STAILQ_FIRST(&reversed_commits);
4549 STAILQ_REMOVE_HEAD(&reversed_commits, entry);
4550 got_object_qid_free(qid);
4552 got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
4555 got_commit_graph_close(graph);
4562 fprintf(stderr, "usage: %s log [-bdPpRs] [-C number] [-c commit] "
4563 "[-l N] [-r repository-path] [-S search-pattern] [-x commit] "
4564 "[path]\n", getprogname());
4569 get_default_log_limit(void)
4571 const char *got_default_log_limit;
4575 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
4576 if (got_default_log_limit == NULL)
4578 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
4584 static const struct got_error *
4585 cmd_log(int argc, char *argv[])
4587 const struct got_error *error;
4588 struct got_repository *repo = NULL;
4589 struct got_worktree *worktree = NULL;
4590 struct got_object_id *start_id = NULL, *end_id = NULL;
4591 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
4592 const char *start_commit = NULL, *end_commit = NULL;
4593 const char *search_pattern = NULL;
4594 int diff_context = -1, ch;
4595 int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
4596 int show_diffstat = 0, reverse_display_order = 0, one_line = 0;
4598 struct got_reflist_head refs;
4599 struct got_reflist_object_id_map *refs_idmap = NULL;
4600 FILE *tmpfile = NULL;
4601 int *pack_fds = NULL;
4606 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4612 limit = get_default_log_limit();
4614 while ((ch = getopt(argc, argv, "bC:c:dl:PpRr:S:sx:")) != -1) {
4620 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4623 errx(1, "number of context lines is %s: %s",
4627 start_commit = optarg;
4633 limit = strtonum(optarg, 0, INT_MAX, &errstr);
4635 errx(1, "number of commits is %s: %s",
4639 show_changed_paths = 1;
4645 reverse_display_order = 1;
4648 repo_path = realpath(optarg, NULL);
4649 if (repo_path == NULL)
4650 return got_error_from_errno2("realpath",
4652 got_path_strip_trailing_slashes(repo_path);
4655 search_pattern = optarg;
4661 end_commit = optarg;
4672 if (diff_context == -1)
4674 else if (!show_patch)
4675 errx(1, "-C requires -p");
4677 if (one_line && (show_patch || show_changed_paths || show_diffstat))
4678 errx(1, "cannot use -s with -d, -p or -P");
4680 cwd = getcwd(NULL, 0);
4682 error = got_error_from_errno("getcwd");
4686 error = got_repo_pack_fds_open(&pack_fds);
4690 if (repo_path == NULL) {
4691 error = got_worktree_open(&worktree, cwd);
4692 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4699 error = got_worktree_resolve_path(&path, worktree,
4704 path = strdup(argv[0]);
4706 error = got_error_from_errno("strdup");
4710 } else if (argc != 0)
4713 if (repo_path == NULL) {
4714 repo_path = worktree ?
4715 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
4717 if (repo_path == NULL) {
4718 error = got_error_from_errno("strdup");
4722 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4726 error = apply_unveil(got_repo_get_path(repo), 1,
4727 worktree ? got_worktree_get_root_path(worktree) : NULL);
4731 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4735 error = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
4739 if (start_commit == NULL) {
4740 struct got_reference *head_ref;
4741 struct got_commit_object *commit = NULL;
4742 error = got_ref_open(&head_ref, repo,
4743 worktree ? got_worktree_get_head_ref_name(worktree)
4747 error = got_ref_resolve(&start_id, repo, head_ref);
4748 got_ref_close(head_ref);
4751 error = got_object_open_as_commit(&commit, repo,
4755 got_object_commit_close(commit);
4757 char *keyword_idstr = NULL;
4759 error = got_keyword_to_idstr(&keyword_idstr, start_commit,
4763 if (keyword_idstr != NULL)
4764 start_commit = keyword_idstr;
4766 error = got_repo_match_object_id(&start_id, NULL,
4767 start_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4768 free(keyword_idstr);
4772 if (end_commit != NULL) {
4773 error = got_repo_match_object_id(&end_id, NULL,
4774 end_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4781 * If a path was specified on the command line it was resolved
4782 * to a path in the work tree above. Prepend the work tree's
4783 * path prefix to obtain the corresponding in-repository path.
4787 prefix = got_worktree_get_path_prefix(worktree);
4788 if (asprintf(&in_repo_path, "%s%s%s", prefix,
4789 (path[0] != '\0') ? "/" : "", path) == -1) {
4790 error = got_error_from_errno("asprintf");
4795 error = got_repo_map_path(&in_repo_path, repo,
4801 path = in_repo_path;
4805 /* Release work tree lock. */
4806 got_worktree_close(worktree);
4810 if (search_pattern && show_patch) {
4811 tmpfile = got_opentemp();
4812 if (tmpfile == NULL) {
4813 error = got_error_from_errno("got_opentemp");
4818 error = print_commits(start_id, end_id, repo, path ? path : "",
4819 show_changed_paths, show_diffstat, show_patch, search_pattern,
4820 diff_context, limit, log_branches, reverse_display_order,
4821 refs_idmap, one_line, tmpfile);
4829 got_worktree_close(worktree);
4831 const struct got_error *close_err = got_repo_close(repo);
4836 const struct got_error *pack_err =
4837 got_repo_pack_fds_close(pack_fds);
4842 got_reflist_object_id_map_free(refs_idmap);
4843 if (tmpfile && fclose(tmpfile) == EOF && error == NULL)
4844 error = got_error_from_errno("fclose");
4845 got_ref_list_free(&refs);
4852 fprintf(stderr, "usage: %s diff [-adPsw] [-C number] [-c commit] "
4853 "[-r repository-path] [object1 object2 | path ...]\n",
4858 struct print_diff_arg {
4859 struct got_repository *repo;
4860 struct got_worktree *worktree;
4861 struct got_diffstat_cb_arg *diffstat;
4866 enum got_diff_algorithm diff_algo;
4867 int ignore_whitespace;
4868 int force_text_diff;
4875 * Create a file which contains the target path of a symlink so we can feed
4876 * it as content to the diff engine.
4878 static const struct got_error *
4879 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
4880 const char *abspath)
4882 const struct got_error *err = NULL;
4883 char target_path[PATH_MAX];
4884 ssize_t target_len, outlen;
4889 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
4890 if (target_len == -1)
4891 return got_error_from_errno2("readlinkat", abspath);
4893 target_len = readlink(abspath, target_path, PATH_MAX);
4894 if (target_len == -1)
4895 return got_error_from_errno2("readlink", abspath);
4898 *fd = got_opentempfd();
4900 return got_error_from_errno("got_opentempfd");
4902 outlen = write(*fd, target_path, target_len);
4904 err = got_error_from_errno("got_opentempfd");
4908 if (lseek(*fd, 0, SEEK_SET) == -1) {
4909 err = got_error_from_errno2("lseek", abspath);
4920 static const struct got_error *
4921 print_diff(void *arg, unsigned char status, unsigned char staged_status,
4922 const char *path, struct got_object_id *blob_id,
4923 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4924 int dirfd, const char *de_name)
4926 struct print_diff_arg *a = arg;
4927 const struct got_error *err = NULL;
4928 struct got_blob_object *blob1 = NULL;
4929 int fd = -1, fd1 = -1, fd2 = -1;
4931 char *abspath = NULL, *label1 = NULL;
4936 memset(&sb, 0, sizeof(sb));
4938 if (a->diff_staged) {
4939 if (staged_status != GOT_STATUS_MODIFY &&
4940 staged_status != GOT_STATUS_ADD &&
4941 staged_status != GOT_STATUS_DELETE)
4944 if (staged_status == GOT_STATUS_DELETE)
4946 if (status == GOT_STATUS_NONEXISTENT)
4947 return got_error_set_errno(ENOENT, path);
4948 if (status != GOT_STATUS_MODIFY &&
4949 status != GOT_STATUS_ADD &&
4950 status != GOT_STATUS_DELETE &&
4951 status != GOT_STATUS_CONFLICT)
4955 err = got_opentemp_truncate(a->f1);
4957 return got_error_from_errno("got_opentemp_truncate");
4958 err = got_opentemp_truncate(a->f2);
4960 return got_error_from_errno("got_opentemp_truncate");
4962 if (!a->header_shown) {
4963 if (fprintf(a->outfile, "diff %s%s\n",
4964 a->diff_staged ? "-s " : "",
4965 got_worktree_get_root_path(a->worktree)) < 0) {
4966 err = got_error_from_errno("fprintf");
4969 if (fprintf(a->outfile, "commit - %s\n", a->id_str) < 0) {
4970 err = got_error_from_errno("fprintf");
4973 if (fprintf(a->outfile, "path + %s%s\n",
4974 got_worktree_get_root_path(a->worktree),
4975 a->diff_staged ? " (staged changes)" : "") < 0) {
4976 err = got_error_from_errno("fprintf");
4979 a->header_shown = 1;
4982 if (a->diff_staged) {
4983 const char *label1 = NULL, *label2 = NULL;
4984 switch (staged_status) {
4985 case GOT_STATUS_MODIFY:
4989 case GOT_STATUS_ADD:
4992 case GOT_STATUS_DELETE:
4996 return got_error(GOT_ERR_FILE_STATUS);
4998 fd1 = got_opentempfd();
5000 err = got_error_from_errno("got_opentempfd");
5003 fd2 = got_opentempfd();
5005 err = got_error_from_errno("got_opentempfd");
5008 err = got_diff_objects_as_blobs(NULL, NULL, a->f1, a->f2,
5009 fd1, fd2, blob_id, staged_blob_id, label1, label2,
5010 a->diff_algo, a->diff_context, a->ignore_whitespace,
5011 a->force_text_diff, a->diffstat, a->repo, a->outfile);
5015 fd1 = got_opentempfd();
5017 err = got_error_from_errno("got_opentempfd");
5021 if (staged_status == GOT_STATUS_ADD ||
5022 staged_status == GOT_STATUS_MODIFY) {
5024 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
5028 err = got_object_id_str(&id_str, staged_blob_id);
5031 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
5032 err = got_error_from_errno("asprintf");
5037 } else if (status != GOT_STATUS_ADD) {
5038 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192,
5044 if (status != GOT_STATUS_DELETE) {
5045 if (asprintf(&abspath, "%s/%s",
5046 got_worktree_get_root_path(a->worktree), path) == -1) {
5047 err = got_error_from_errno("asprintf");
5052 fd = openat(dirfd, de_name,
5053 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5055 if (!got_err_open_nofollow_on_symlink()) {
5056 err = got_error_from_errno2("openat",
5060 err = get_symlink_target_file(&fd, dirfd,
5066 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5068 if (!got_err_open_nofollow_on_symlink()) {
5069 err = got_error_from_errno2("open",
5073 err = get_symlink_target_file(&fd, dirfd,
5079 if (fstatat(fd, abspath, &sb, AT_SYMLINK_NOFOLLOW) == -1) {
5080 err = got_error_from_errno2("fstatat", abspath);
5083 f2 = fdopen(fd, "r");
5085 err = got_error_from_errno2("fdopen", abspath);
5093 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5099 err = got_diff_blob_file(blob1, a->f1, size1, label1, f2 ? f2 : a->f2,
5100 f2_exists, &sb, path, GOT_DIFF_ALGORITHM_PATIENCE, a->diff_context,
5101 a->ignore_whitespace, a->force_text_diff, a->diffstat, a->outfile);
5103 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5104 err = got_error_from_errno("close");
5105 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5106 err = got_error_from_errno("close");
5108 got_object_blob_close(blob1);
5109 if (fd != -1 && close(fd) == -1 && err == NULL)
5110 err = got_error_from_errno("close");
5111 if (f2 && fclose(f2) == EOF && err == NULL)
5112 err = got_error_from_errno("fclose");
5117 static const struct got_error *
5118 cmd_diff(int argc, char *argv[])
5120 const struct got_error *error;
5121 struct got_repository *repo = NULL;
5122 struct got_worktree *worktree = NULL;
5123 char *cwd = NULL, *repo_path = NULL;
5124 const char *commit_args[2] = { NULL, NULL };
5125 int ncommit_args = 0;
5126 struct got_object_id *ids[2] = { NULL, NULL };
5127 char *labels[2] = { NULL, NULL };
5128 int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
5129 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch, i;
5130 int force_text_diff = 0, force_path = 0, rflag = 0, show_diffstat = 0;
5132 struct got_reflist_head refs;
5133 struct got_pathlist_head diffstat_paths, paths;
5134 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
5135 int fd1 = -1, fd2 = -1;
5136 int *pack_fds = NULL;
5137 struct got_diffstat_cb_arg dsa;
5139 memset(&dsa, 0, sizeof(dsa));
5143 TAILQ_INIT(&diffstat_paths);
5146 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5151 while ((ch = getopt(argc, argv, "aC:c:dPr:sw")) != -1) {
5154 force_text_diff = 1;
5157 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
5160 errx(1, "number of context lines is %s: %s",
5164 if (ncommit_args >= 2)
5165 errx(1, "too many -c options used");
5166 commit_args[ncommit_args++] = optarg;
5175 repo_path = realpath(optarg, NULL);
5176 if (repo_path == NULL)
5177 return got_error_from_errno2("realpath",
5179 got_path_strip_trailing_slashes(repo_path);
5186 ignore_whitespace = 1;
5197 cwd = getcwd(NULL, 0);
5199 error = got_error_from_errno("getcwd");
5203 error = got_repo_pack_fds_open(&pack_fds);
5207 if (repo_path == NULL) {
5208 error = got_worktree_open(&worktree, cwd);
5209 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5215 strdup(got_worktree_get_repo_path(worktree));
5216 if (repo_path == NULL) {
5217 error = got_error_from_errno("strdup");
5221 repo_path = strdup(cwd);
5222 if (repo_path == NULL) {
5223 error = got_error_from_errno("strdup");
5229 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5234 if (show_diffstat) {
5235 dsa.paths = &diffstat_paths;
5236 dsa.force_text = force_text_diff;
5237 dsa.ignore_ws = ignore_whitespace;
5238 dsa.diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
5241 if (rflag || worktree == NULL || ncommit_args > 0) {
5243 error = got_error_msg(GOT_ERR_NOT_IMPL,
5244 "-P option can only be used when diffing "
5249 error = got_error_msg(GOT_ERR_NOT_IMPL,
5250 "-s option can only be used when diffing "
5256 error = apply_unveil(got_repo_get_path(repo), 1,
5257 worktree ? got_worktree_get_root_path(worktree) : NULL);
5261 if ((!force_path && argc == 2) || ncommit_args > 0) {
5262 int obj_type = (ncommit_args > 0 ?
5263 GOT_OBJ_TYPE_COMMIT : GOT_OBJ_TYPE_ANY);
5264 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5268 for (i = 0; i < (ncommit_args > 0 ? ncommit_args : argc); i++) {
5270 char *keyword_idstr = NULL;
5272 if (ncommit_args > 0)
5273 arg = commit_args[i];
5277 error = got_keyword_to_idstr(&keyword_idstr, arg,
5281 if (keyword_idstr != NULL)
5282 arg = keyword_idstr;
5284 error = got_repo_match_object_id(&ids[i], &labels[i],
5285 arg, obj_type, &refs, repo);
5286 free(keyword_idstr);
5288 if (error->code != GOT_ERR_NOT_REF &&
5289 error->code != GOT_ERR_NO_OBJ)
5291 if (ncommit_args > 0)
5299 f1 = got_opentemp();
5301 error = got_error_from_errno("got_opentemp");
5305 f2 = got_opentemp();
5307 error = got_error_from_errno("got_opentemp");
5311 outfile = got_opentemp();
5312 if (outfile == NULL) {
5313 error = got_error_from_errno("got_opentemp");
5317 if (ncommit_args == 0 && (ids[0] == NULL || ids[1] == NULL)) {
5318 struct print_diff_arg arg;
5321 if (worktree == NULL) {
5322 if (argc == 2 && ids[0] == NULL) {
5323 error = got_error_path(argv[0], GOT_ERR_NO_OBJ);
5325 } else if (argc == 2 && ids[1] == NULL) {
5326 error = got_error_path(argv[1], GOT_ERR_NO_OBJ);
5328 } else if (argc > 0) {
5329 error = got_error_fmt(GOT_ERR_NOT_WORKTREE,
5330 "%s", "specified paths cannot be resolved");
5333 error = got_error(GOT_ERR_NOT_WORKTREE);
5338 error = get_worktree_paths_from_argv(&paths, argc, argv,
5343 error = got_object_id_str(&id_str,
5344 got_worktree_get_base_commit_id(worktree));
5348 arg.worktree = worktree;
5349 arg.diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
5350 arg.diff_context = diff_context;
5351 arg.id_str = id_str;
5352 arg.header_shown = 0;
5353 arg.diff_staged = diff_staged;
5354 arg.ignore_whitespace = ignore_whitespace;
5355 arg.force_text_diff = force_text_diff;
5356 arg.diffstat = show_diffstat ? &dsa : NULL;
5359 arg.outfile = outfile;
5361 error = got_worktree_status(worktree, &paths, repo, 0,
5362 print_diff, &arg, check_cancelled, NULL);
5367 if (show_diffstat && dsa.nfiles > 0) {
5370 if (asprintf(&header, "diffstat %s%s",
5371 diff_staged ? "-s " : "",
5372 got_worktree_get_root_path(worktree)) == -1) {
5373 error = got_error_from_errno("asprintf");
5377 error = print_diffstat(&dsa, header);
5383 error = printfile(outfile);
5387 if (ncommit_args == 1) {
5388 struct got_commit_object *commit;
5389 error = got_object_open_as_commit(&commit, repo, ids[0]);
5393 labels[1] = labels[0];
5395 if (got_object_commit_get_nparents(commit) > 0) {
5396 const struct got_object_id_queue *pids;
5397 struct got_object_qid *pid;
5398 pids = got_object_commit_get_parent_ids(commit);
5399 pid = STAILQ_FIRST(pids);
5400 ids[0] = got_object_id_dup(&pid->id);
5401 if (ids[0] == NULL) {
5402 error = got_error_from_errno(
5403 "got_object_id_dup");
5404 got_object_commit_close(commit);
5407 error = got_object_id_str(&labels[0], ids[0]);
5409 got_object_commit_close(commit);
5414 labels[0] = strdup("/dev/null");
5415 if (labels[0] == NULL) {
5416 error = got_error_from_errno("strdup");
5417 got_object_commit_close(commit);
5422 got_object_commit_close(commit);
5425 if (ncommit_args == 0 && argc > 2) {
5426 error = got_error_msg(GOT_ERR_BAD_PATH,
5427 "path arguments cannot be used when diffing two objects");
5432 error = got_object_get_type(&type1, repo, ids[0]);
5437 error = got_object_get_type(&type2, repo, ids[1]);
5440 if (type1 != GOT_OBJ_TYPE_ANY && type1 != type2) {
5441 error = got_error(GOT_ERR_OBJ_TYPE);
5444 if (type1 == GOT_OBJ_TYPE_BLOB && argc > 2) {
5445 error = got_error_msg(GOT_ERR_OBJ_TYPE,
5446 "path arguments cannot be used when diffing blobs");
5450 for (i = 0; ncommit_args > 0 && i < argc; i++) {
5452 struct got_pathlist_entry *new;
5456 error = got_worktree_resolve_path(&p, worktree,
5460 prefix = got_worktree_get_path_prefix(worktree);
5461 while (prefix[0] == '/')
5463 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5464 (p[0] != '\0' && prefix[0] != '\0') ? "/" : "",
5466 error = got_error_from_errno("asprintf");
5472 char *mapped_path, *s;
5473 error = got_repo_map_path(&mapped_path, repo, argv[i]);
5479 in_repo_path = strdup(s);
5480 if (in_repo_path == NULL) {
5481 error = got_error_from_errno("asprintf");
5488 error = got_pathlist_insert(&new, &paths, in_repo_path, NULL);
5489 if (error || new == NULL /* duplicate */)
5496 /* Release work tree lock. */
5497 got_worktree_close(worktree);
5501 fd1 = got_opentempfd();
5503 error = got_error_from_errno("got_opentempfd");
5507 fd2 = got_opentempfd();
5509 error = got_error_from_errno("got_opentempfd");
5513 switch (type1 == GOT_OBJ_TYPE_ANY ? type2 : type1) {
5514 case GOT_OBJ_TYPE_BLOB:
5515 error = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5516 fd1, fd2, ids[0], ids[1], NULL, NULL,
5517 GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
5518 ignore_whitespace, force_text_diff,
5519 show_diffstat ? &dsa : NULL, repo, outfile);
5521 case GOT_OBJ_TYPE_TREE:
5522 error = got_diff_objects_as_trees(NULL, NULL, f1, f2, fd1, fd2,
5523 ids[0], ids[1], &paths, "", "",
5524 GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
5525 ignore_whitespace, force_text_diff,
5526 show_diffstat ? &dsa : NULL, repo, outfile);
5528 case GOT_OBJ_TYPE_COMMIT:
5529 fprintf(outfile, "diff %s %s\n", labels[0], labels[1]);
5530 error = got_diff_objects_as_commits(NULL, NULL, f1, f2,
5531 fd1, fd2, ids[0], ids[1], &paths,
5532 GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
5533 ignore_whitespace, force_text_diff,
5534 show_diffstat ? &dsa : NULL, repo, outfile);
5537 error = got_error(GOT_ERR_OBJ_TYPE);
5542 if (show_diffstat && dsa.nfiles > 0) {
5543 char *header = NULL;
5545 if (asprintf(&header, "diffstat %s %s",
5546 labels[0], labels[1]) == -1) {
5547 error = got_error_from_errno("asprintf");
5551 error = print_diffstat(&dsa, header);
5557 error = printfile(outfile);
5565 got_worktree_close(worktree);
5567 const struct got_error *close_err = got_repo_close(repo);
5572 const struct got_error *pack_err =
5573 got_repo_pack_fds_close(pack_fds);
5577 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
5578 got_pathlist_free(&diffstat_paths, GOT_PATHLIST_FREE_ALL);
5579 got_ref_list_free(&refs);
5580 if (outfile && fclose(outfile) == EOF && error == NULL)
5581 error = got_error_from_errno("fclose");
5582 if (f1 && fclose(f1) == EOF && error == NULL)
5583 error = got_error_from_errno("fclose");
5584 if (f2 && fclose(f2) == EOF && error == NULL)
5585 error = got_error_from_errno("fclose");
5586 if (fd1 != -1 && close(fd1) == -1 && error == NULL)
5587 error = got_error_from_errno("close");
5588 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
5589 error = got_error_from_errno("close");
5597 "usage: %s blame [-c commit] [-r repository-path] path\n",
5606 char datebuf[11]; /* YYYY-MM-DD + NUL */
5609 struct blame_cb_args {
5610 struct blame_line *lines;
5614 off_t *line_offsets;
5616 struct got_repository *repo;
5619 static const struct got_error *
5620 blame_cb(void *arg, int nlines, int lineno,
5621 struct got_commit_object *commit, struct got_object_id *id)
5623 const struct got_error *err = NULL;
5624 struct blame_cb_args *a = arg;
5625 struct blame_line *bline;
5627 size_t linesize = 0;
5630 time_t committer_time;
5632 if (nlines != a->nlines ||
5633 (lineno != -1 && lineno < 1) || lineno > a->nlines)
5634 return got_error(GOT_ERR_RANGE);
5636 if (sigint_received)
5637 return got_error(GOT_ERR_ITER_COMPLETED);
5640 return NULL; /* no change in this commit */
5642 /* Annotate this line. */
5643 bline = &a->lines[lineno - 1];
5644 if (bline->annotated)
5646 err = got_object_id_str(&bline->id_str, id);
5650 bline->committer = strdup(got_object_commit_get_committer(commit));
5651 if (bline->committer == NULL) {
5652 err = got_error_from_errno("strdup");
5656 committer_time = got_object_commit_get_committer_time(commit);
5657 if (gmtime_r(&committer_time, &tm) == NULL)
5658 return got_error_from_errno("gmtime_r");
5659 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
5661 err = got_error(GOT_ERR_NO_SPACE);
5664 bline->annotated = 1;
5666 /* Print lines annotated so far. */
5667 bline = &a->lines[a->lineno_cur - 1];
5668 if (!bline->annotated)
5671 offset = a->line_offsets[a->lineno_cur - 1];
5672 if (fseeko(a->f, offset, SEEK_SET) == -1) {
5673 err = got_error_from_errno("fseeko");
5677 while (a->lineno_cur <= a->nlines && bline->annotated) {
5678 char *smallerthan, *at, *nl, *committer;
5681 if (getline(&line, &linesize, a->f) == -1) {
5683 err = got_error_from_errno("getline");
5687 committer = bline->committer;
5688 smallerthan = strchr(committer, '<');
5689 if (smallerthan && smallerthan[1] != '\0')
5690 committer = smallerthan + 1;
5691 at = strchr(committer, '@');
5694 len = strlen(committer);
5696 committer[8] = '\0';
5698 nl = strchr(line, '\n');
5701 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
5702 bline->id_str, bline->datebuf, committer, line);
5705 bline = &a->lines[a->lineno_cur - 1];
5712 static const struct got_error *
5713 cmd_blame(int argc, char *argv[])
5715 const struct got_error *error;
5716 struct got_repository *repo = NULL;
5717 struct got_worktree *worktree = NULL;
5718 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5719 char *link_target = NULL;
5720 struct got_object_id *obj_id = NULL;
5721 struct got_object_id *commit_id = NULL;
5722 struct got_commit_object *commit = NULL;
5723 struct got_blob_object *blob = NULL;
5724 char *commit_id_str = NULL;
5725 struct blame_cb_args bca;
5726 int ch, obj_type, i, fd1 = -1, fd2 = -1, fd3 = -1;
5728 int *pack_fds = NULL;
5729 FILE *f1 = NULL, *f2 = NULL;
5731 fd1 = got_opentempfd();
5733 return got_error_from_errno("got_opentempfd");
5735 memset(&bca, 0, sizeof(bca));
5738 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5743 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5746 commit_id_str = optarg;
5749 repo_path = realpath(optarg, NULL);
5750 if (repo_path == NULL)
5751 return got_error_from_errno2("realpath",
5753 got_path_strip_trailing_slashes(repo_path);
5769 cwd = getcwd(NULL, 0);
5771 error = got_error_from_errno("getcwd");
5775 error = got_repo_pack_fds_open(&pack_fds);
5779 if (repo_path == NULL) {
5780 error = got_worktree_open(&worktree, cwd);
5781 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5787 strdup(got_worktree_get_repo_path(worktree));
5788 if (repo_path == NULL) {
5789 error = got_error_from_errno("strdup");
5794 repo_path = strdup(cwd);
5795 if (repo_path == NULL) {
5796 error = got_error_from_errno("strdup");
5802 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5807 const char *prefix = got_worktree_get_path_prefix(worktree);
5810 error = got_worktree_resolve_path(&p, worktree, path);
5813 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5814 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
5816 error = got_error_from_errno("asprintf");
5821 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5823 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5826 error = got_repo_map_path(&in_repo_path, repo, path);
5831 if (commit_id_str == NULL) {
5832 struct got_reference *head_ref;
5833 error = got_ref_open(&head_ref, repo, worktree ?
5834 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5837 error = got_ref_resolve(&commit_id, repo, head_ref);
5838 got_ref_close(head_ref);
5842 struct got_reflist_head refs;
5844 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5848 error = got_repo_match_object_id(&commit_id, NULL,
5849 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5850 got_ref_list_free(&refs);
5856 /* Release work tree lock. */
5857 got_worktree_close(worktree);
5861 error = got_object_open_as_commit(&commit, repo, commit_id);
5865 error = got_object_resolve_symlinks(&link_target, in_repo_path,
5870 error = got_object_id_by_path(&obj_id, repo, commit,
5871 link_target ? link_target : in_repo_path);
5875 error = got_object_get_type(&obj_type, repo, obj_id);
5879 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5880 error = got_error_path(link_target ? link_target : in_repo_path,
5885 error = got_object_open_as_blob(&blob, repo, obj_id, 8192, fd1);
5888 bca.f = got_opentemp();
5889 if (bca.f == NULL) {
5890 error = got_error_from_errno("got_opentemp");
5893 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
5894 &bca.line_offsets, bca.f, blob);
5895 if (error || bca.nlines == 0)
5898 /* Don't include \n at EOF in the blame line count. */
5899 if (bca.line_offsets[bca.nlines - 1] == filesize)
5902 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
5903 if (bca.lines == NULL) {
5904 error = got_error_from_errno("calloc");
5908 bca.nlines_prec = 0;
5916 fd2 = got_opentempfd();
5918 error = got_error_from_errno("got_opentempfd");
5921 fd3 = got_opentempfd();
5923 error = got_error_from_errno("got_opentempfd");
5926 f1 = got_opentemp();
5928 error = got_error_from_errno("got_opentemp");
5931 f2 = got_opentemp();
5933 error = got_error_from_errno("got_opentemp");
5936 error = got_blame(link_target ? link_target : in_repo_path, commit_id,
5937 repo, GOT_DIFF_ALGORITHM_PATIENCE, blame_cb, &bca,
5938 check_cancelled, NULL, fd2, fd3, f1, f2);
5947 got_object_commit_close(commit);
5949 if (fd1 != -1 && close(fd1) == -1 && error == NULL)
5950 error = got_error_from_errno("close");
5951 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
5952 error = got_error_from_errno("close");
5953 if (fd3 != -1 && close(fd3) == -1 && error == NULL)
5954 error = got_error_from_errno("close");
5955 if (f1 && fclose(f1) == EOF && error == NULL)
5956 error = got_error_from_errno("fclose");
5957 if (f2 && fclose(f2) == EOF && error == NULL)
5958 error = got_error_from_errno("fclose");
5961 got_object_blob_close(blob);
5963 got_worktree_close(worktree);
5965 const struct got_error *close_err = got_repo_close(repo);
5970 const struct got_error *pack_err =
5971 got_repo_pack_fds_close(pack_fds);
5976 for (i = 0; i < bca.nlines; i++) {
5977 struct blame_line *bline = &bca.lines[i];
5978 free(bline->id_str);
5979 free(bline->committer);
5983 free(bca.line_offsets);
5984 if (bca.f && fclose(bca.f) == EOF && error == NULL)
5985 error = got_error_from_errno("fclose");
5992 fprintf(stderr, "usage: %s tree [-iR] [-c commit] [-r repository-path] "
5993 "[path]\n", getprogname());
5997 static const struct got_error *
5998 print_entry(struct got_tree_entry *te, const char *id, const char *path,
5999 const char *root_path, struct got_repository *repo)
6001 const struct got_error *err = NULL;
6002 int is_root_path = (strcmp(path, root_path) == 0);
6003 const char *modestr = "";
6004 mode_t mode = got_tree_entry_get_mode(te);
6005 char *link_target = NULL;
6007 path += strlen(root_path);
6008 while (path[0] == '/')
6011 if (got_object_tree_entry_is_submodule(te))
6013 else if (S_ISLNK(mode)) {
6016 err = got_tree_entry_get_symlink_target(&link_target, te, repo);
6019 for (i = 0; link_target[i] != '\0'; i++) {
6020 if (!isprint((unsigned char)link_target[i]))
6021 link_target[i] = '?';
6026 else if (S_ISDIR(mode))
6028 else if (mode & S_IXUSR)
6031 printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
6032 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
6033 link_target ? " -> ": "", link_target ? link_target : "");
6039 static const struct got_error *
6040 print_tree(const char *path, struct got_commit_object *commit,
6041 int show_ids, int recurse, const char *root_path,
6042 struct got_repository *repo)
6044 const struct got_error *err = NULL;
6045 struct got_object_id *tree_id = NULL;
6046 struct got_tree_object *tree = NULL;
6049 err = got_object_id_by_path(&tree_id, repo, commit, path);
6053 err = got_object_open_as_tree(&tree, repo, tree_id);
6056 nentries = got_object_tree_get_nentries(tree);
6057 for (i = 0; i < nentries; i++) {
6058 struct got_tree_entry *te;
6061 if (sigint_received || sigpipe_received)
6064 te = got_object_tree_get_entry(tree, i);
6067 err = got_object_id_str(&id_str,
6068 got_tree_entry_get_id(te));
6071 if (asprintf(&id, "%s ", id_str) == -1) {
6072 err = got_error_from_errno("asprintf");
6078 err = print_entry(te, id, path, root_path, repo);
6083 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
6085 if (asprintf(&child_path, "%s%s%s", path,
6086 path[0] == '/' && path[1] == '\0' ? "" : "/",
6087 got_tree_entry_get_name(te)) == -1) {
6088 err = got_error_from_errno("asprintf");
6091 err = print_tree(child_path, commit, show_ids, 1,
6100 got_object_tree_close(tree);
6105 static const struct got_error *
6106 cmd_tree(int argc, char *argv[])
6108 const struct got_error *error;
6109 struct got_repository *repo = NULL;
6110 struct got_worktree *worktree = NULL;
6111 const char *path, *refname = NULL;
6112 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6113 struct got_object_id *commit_id = NULL;
6114 struct got_commit_object *commit = NULL;
6115 char *commit_id_str = NULL;
6116 int show_ids = 0, recurse = 0;
6118 int *pack_fds = NULL;
6121 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6126 while ((ch = getopt(argc, argv, "c:iRr:")) != -1) {
6129 commit_id_str = optarg;
6138 repo_path = realpath(optarg, NULL);
6139 if (repo_path == NULL)
6140 return got_error_from_errno2("realpath",
6142 got_path_strip_trailing_slashes(repo_path);
6160 cwd = getcwd(NULL, 0);
6162 error = got_error_from_errno("getcwd");
6166 error = got_repo_pack_fds_open(&pack_fds);
6170 if (repo_path == NULL) {
6171 error = got_worktree_open(&worktree, cwd);
6172 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6178 strdup(got_worktree_get_repo_path(worktree));
6179 if (repo_path == NULL)
6180 error = got_error_from_errno("strdup");
6184 repo_path = strdup(cwd);
6185 if (repo_path == NULL) {
6186 error = got_error_from_errno("strdup");
6192 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6197 const char *prefix = got_worktree_get_path_prefix(worktree);
6200 if (path == NULL || got_path_is_root_dir(path))
6202 error = got_worktree_resolve_path(&p, worktree, path);
6205 if (asprintf(&in_repo_path, "%s%s%s", prefix,
6206 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
6208 error = got_error_from_errno("asprintf");
6213 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6217 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6222 error = got_repo_map_path(&in_repo_path, repo, path);
6227 if (commit_id_str == NULL) {
6228 struct got_reference *head_ref;
6230 refname = got_worktree_get_head_ref_name(worktree);
6232 refname = GOT_REF_HEAD;
6233 error = got_ref_open(&head_ref, repo, refname, 0);
6236 error = got_ref_resolve(&commit_id, repo, head_ref);
6237 got_ref_close(head_ref);
6241 struct got_reflist_head refs;
6243 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
6247 error = got_repo_match_object_id(&commit_id, NULL,
6248 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
6249 got_ref_list_free(&refs);
6255 /* Release work tree lock. */
6256 got_worktree_close(worktree);
6260 error = got_object_open_as_commit(&commit, repo, commit_id);
6264 error = print_tree(in_repo_path, commit, show_ids, recurse,
6265 in_repo_path, repo);
6272 got_object_commit_close(commit);
6274 got_worktree_close(worktree);
6276 const struct got_error *close_err = got_repo_close(repo);
6281 const struct got_error *pack_err =
6282 got_repo_pack_fds_close(pack_fds);
6292 fprintf(stderr, "usage: %s status [-I] [-S status-codes] "
6293 "[-s status-codes] [path ...]\n", getprogname());
6297 struct got_status_arg {
6302 static const struct got_error *
6303 print_status(void *arg, unsigned char status, unsigned char staged_status,
6304 const char *path, struct got_object_id *blob_id,
6305 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6306 int dirfd, const char *de_name)
6308 struct got_status_arg *st = arg;
6310 if (status == staged_status && (status == GOT_STATUS_DELETE))
6311 status = GOT_STATUS_NO_CHANGE;
6312 if (st != NULL && st->status_codes) {
6313 size_t ncodes = strlen(st->status_codes);
6316 for (i = 0; i < ncodes ; i++) {
6318 if (status == st->status_codes[i] ||
6319 staged_status == st->status_codes[i]) {
6324 if (status == st->status_codes[i] ||
6325 staged_status == st->status_codes[i])
6330 if (st->suppress && j == 0)
6337 printf("%c%c %s\n", status, staged_status, path);
6341 static const struct got_error *
6342 cmd_status(int argc, char *argv[])
6344 const struct got_error *error = NULL;
6345 struct got_repository *repo = NULL;
6346 struct got_worktree *worktree = NULL;
6347 struct got_status_arg st;
6349 struct got_pathlist_head paths;
6350 int ch, i, no_ignores = 0;
6351 int *pack_fds = NULL;
6355 memset(&st, 0, sizeof(st));
6356 st.status_codes = NULL;
6360 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6365 while ((ch = getopt(argc, argv, "IS:s:")) != -1) {
6371 if (st.status_codes != NULL && st.suppress == 0)
6372 option_conflict('S', 's');
6376 for (i = 0; optarg[i] != '\0'; i++) {
6377 switch (optarg[i]) {
6378 case GOT_STATUS_MODIFY:
6379 case GOT_STATUS_ADD:
6380 case GOT_STATUS_DELETE:
6381 case GOT_STATUS_CONFLICT:
6382 case GOT_STATUS_MISSING:
6383 case GOT_STATUS_OBSTRUCTED:
6384 case GOT_STATUS_UNVERSIONED:
6385 case GOT_STATUS_MODE_CHANGE:
6386 case GOT_STATUS_NONEXISTENT:
6389 errx(1, "invalid status code '%c'",
6393 if (ch == 's' && st.suppress)
6394 option_conflict('s', 'S');
6395 st.status_codes = optarg;
6406 cwd = getcwd(NULL, 0);
6408 error = got_error_from_errno("getcwd");
6412 error = got_repo_pack_fds_open(&pack_fds);
6416 error = got_worktree_open(&worktree, cwd);
6418 if (error->code == GOT_ERR_NOT_WORKTREE)
6419 error = wrap_not_worktree_error(error, "status", cwd);
6423 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6428 error = apply_unveil(got_repo_get_path(repo), 1,
6429 got_worktree_get_root_path(worktree));
6433 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6437 error = got_worktree_status(worktree, &paths, repo, no_ignores,
6438 print_status, &st, check_cancelled, NULL);
6441 const struct got_error *pack_err =
6442 got_repo_pack_fds_close(pack_fds);
6447 const struct got_error *close_err = got_repo_close(repo);
6452 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
6460 fprintf(stderr, "usage: %s ref [-dlt] [-c object] [-r repository-path] "
6461 "[-s reference] [name]\n", getprogname());
6465 static const struct got_error *
6466 list_refs(struct got_repository *repo, const char *refname, int sort_by_time)
6468 static const struct got_error *err = NULL;
6469 struct got_reflist_head refs;
6470 struct got_reflist_entry *re;
6473 err = got_ref_list(&refs, repo, refname, sort_by_time ?
6474 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6479 TAILQ_FOREACH(re, &refs, entry) {
6481 refstr = got_ref_to_str(re->ref);
6482 if (refstr == NULL) {
6483 err = got_error_from_errno("got_ref_to_str");
6486 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
6490 got_ref_list_free(&refs);
6494 static const struct got_error *
6495 delete_ref_by_name(struct got_repository *repo, const char *refname)
6497 const struct got_error *err;
6498 struct got_reference *ref;
6500 err = got_ref_open(&ref, repo, refname, 0);
6504 err = delete_ref(repo, ref);
6509 static const struct got_error *
6510 add_ref(struct got_repository *repo, const char *refname, const char *target)
6512 const struct got_error *err = NULL;
6513 struct got_object_id *id = NULL;
6514 struct got_reference *ref = NULL;
6515 struct got_reflist_head refs;
6518 * Don't let the user create a reference name with a leading '-'.
6519 * While technically a valid reference name, this case is usually
6520 * an unintended typo.
6522 if (refname[0] == '-')
6523 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
6526 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
6529 err = got_repo_match_object_id(&id, NULL, target, GOT_OBJ_TYPE_ANY,
6531 got_ref_list_free(&refs);
6535 err = got_ref_alloc(&ref, refname, id);
6539 err = got_ref_write(ref, repo);
6547 static const struct got_error *
6548 add_symref(struct got_repository *repo, const char *refname, const char *target)
6550 const struct got_error *err = NULL;
6551 struct got_reference *ref = NULL;
6552 struct got_reference *target_ref = NULL;
6555 * Don't let the user create a reference name with a leading '-'.
6556 * While technically a valid reference name, this case is usually
6557 * an unintended typo.
6559 if (refname[0] == '-')
6560 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
6562 err = got_ref_open(&target_ref, repo, target, 0);
6566 err = got_ref_alloc_symref(&ref, refname, target_ref);
6570 err = got_ref_write(ref, repo);
6573 got_ref_close(target_ref);
6579 static const struct got_error *
6580 cmd_ref(int argc, char *argv[])
6582 const struct got_error *error = NULL;
6583 struct got_repository *repo = NULL;
6584 struct got_worktree *worktree = NULL;
6585 char *cwd = NULL, *repo_path = NULL;
6586 int ch, do_list = 0, do_delete = 0, sort_by_time = 0;
6587 const char *obj_arg = NULL, *symref_target= NULL;
6588 char *refname = NULL;
6589 int *pack_fds = NULL;
6592 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6593 "sendfd unveil", NULL) == -1)
6597 while ((ch = getopt(argc, argv, "c:dlr:s:t")) != -1) {
6609 repo_path = realpath(optarg, NULL);
6610 if (repo_path == NULL)
6611 return got_error_from_errno2("realpath",
6613 got_path_strip_trailing_slashes(repo_path);
6616 symref_target = optarg;
6627 if (obj_arg && do_list)
6628 option_conflict('c', 'l');
6629 if (obj_arg && do_delete)
6630 option_conflict('c', 'd');
6631 if (obj_arg && symref_target)
6632 option_conflict('c', 's');
6633 if (symref_target && do_delete)
6634 option_conflict('s', 'd');
6635 if (symref_target && do_list)
6636 option_conflict('s', 'l');
6637 if (do_delete && do_list)
6638 option_conflict('d', 'l');
6639 if (sort_by_time && !do_list)
6640 errx(1, "-t option requires -l option");
6646 if (argc != 0 && argc != 1)
6649 refname = strdup(argv[0]);
6650 if (refname == NULL) {
6651 error = got_error_from_errno("strdup");
6658 refname = strdup(argv[0]);
6659 if (refname == NULL) {
6660 error = got_error_from_errno("strdup");
6666 got_path_strip_trailing_slashes(refname);
6668 cwd = getcwd(NULL, 0);
6670 error = got_error_from_errno("getcwd");
6674 error = got_repo_pack_fds_open(&pack_fds);
6678 if (repo_path == NULL) {
6679 error = got_worktree_open(&worktree, cwd);
6680 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6686 strdup(got_worktree_get_repo_path(worktree));
6687 if (repo_path == NULL)
6688 error = got_error_from_errno("strdup");
6692 repo_path = strdup(cwd);
6693 if (repo_path == NULL) {
6694 error = got_error_from_errno("strdup");
6700 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6706 /* Remove "cpath" promise. */
6707 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6713 error = apply_unveil(got_repo_get_path(repo), do_list,
6714 worktree ? got_worktree_get_root_path(worktree) : NULL);
6719 error = list_refs(repo, refname, sort_by_time);
6721 error = delete_ref_by_name(repo, refname);
6722 else if (symref_target)
6723 error = add_symref(repo, refname, symref_target);
6725 if (obj_arg == NULL)
6727 error = add_ref(repo, refname, obj_arg);
6732 const struct got_error *close_err = got_repo_close(repo);
6737 got_worktree_close(worktree);
6739 const struct got_error *pack_err =
6740 got_repo_pack_fds_close(pack_fds);
6752 fprintf(stderr, "usage: %s branch [-lnt] [-c commit] [-d name] "
6753 "[-r repository-path] [name]\n", getprogname());
6757 static const struct got_error *
6758 list_branch(struct got_repository *repo, struct got_worktree *worktree,
6759 struct got_reference *ref)
6761 const struct got_error *err = NULL;
6762 const char *refname, *marker = " ";
6765 refname = got_ref_get_name(ref);
6766 if (worktree && strcmp(refname,
6767 got_worktree_get_head_ref_name(worktree)) == 0) {
6768 struct got_object_id *id = NULL;
6770 err = got_ref_resolve(&id, repo, ref);
6773 if (got_object_id_cmp(id,
6774 got_worktree_get_base_commit_id(worktree)) == 0)
6781 if (strncmp(refname, "refs/heads/", 11) == 0)
6783 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
6785 if (strncmp(refname, "refs/remotes/", 13) == 0)
6788 refstr = got_ref_to_str(ref);
6790 return got_error_from_errno("got_ref_to_str");
6792 printf("%s%s: %s\n", marker, refname, refstr);
6797 static const struct got_error *
6798 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
6800 const char *refname;
6802 if (worktree == NULL)
6803 return got_error(GOT_ERR_NOT_WORKTREE);
6805 refname = got_worktree_get_head_ref_name(worktree);
6807 if (strncmp(refname, "refs/heads/", 11) == 0)
6809 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
6812 printf("%s\n", refname);
6817 static const struct got_error *
6818 list_branches(struct got_repository *repo, struct got_worktree *worktree,
6821 static const struct got_error *err = NULL;
6822 struct got_reflist_head refs;
6823 struct got_reflist_entry *re;
6824 struct got_reference *temp_ref = NULL;
6825 int rebase_in_progress, histedit_in_progress;
6830 err = got_worktree_rebase_in_progress(&rebase_in_progress,
6835 err = got_worktree_histedit_in_progress(&histedit_in_progress,
6840 if (rebase_in_progress || histedit_in_progress) {
6841 err = got_ref_open(&temp_ref, repo,
6842 got_worktree_get_head_ref_name(worktree), 0);
6845 list_branch(repo, worktree, temp_ref);
6846 got_ref_close(temp_ref);
6850 err = got_ref_list(&refs, repo, "refs/heads", sort_by_time ?
6851 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6856 TAILQ_FOREACH(re, &refs, entry)
6857 list_branch(repo, worktree, re->ref);
6859 got_ref_list_free(&refs);
6861 err = got_ref_list(&refs, repo, "refs/remotes", sort_by_time ?
6862 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6867 TAILQ_FOREACH(re, &refs, entry)
6868 list_branch(repo, worktree, re->ref);
6870 got_ref_list_free(&refs);
6875 static const struct got_error *
6876 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
6877 const char *branch_name)
6879 const struct got_error *err = NULL;
6880 struct got_reference *ref = NULL;
6881 char *refname, *remote_refname = NULL;
6883 if (strncmp(branch_name, "refs/", 5) == 0)
6885 if (strncmp(branch_name, "heads/", 6) == 0)
6887 else if (strncmp(branch_name, "remotes/", 8) == 0)
6890 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
6891 return got_error_from_errno("asprintf");
6893 if (asprintf(&remote_refname, "refs/remotes/%s",
6894 branch_name) == -1) {
6895 err = got_error_from_errno("asprintf");
6899 err = got_ref_open(&ref, repo, refname, 0);
6901 const struct got_error *err2;
6902 if (err->code != GOT_ERR_NOT_REF)
6905 * Keep 'err' intact such that if neither branch exists
6906 * we report "refs/heads" rather than "refs/remotes" in
6907 * our error message.
6909 err2 = got_ref_open(&ref, repo, remote_refname, 0);
6916 strcmp(got_worktree_get_head_ref_name(worktree),
6917 got_ref_get_name(ref)) == 0) {
6918 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6919 "will not delete this work tree's current branch");
6923 err = delete_ref(repo, ref);
6928 free(remote_refname);
6932 static const struct got_error *
6933 add_branch(struct got_repository *repo, const char *branch_name,
6934 struct got_object_id *base_commit_id)
6936 const struct got_error *err = NULL;
6937 struct got_reference *ref = NULL;
6938 char *refname = NULL;
6941 * Don't let the user create a branch name with a leading '-'.
6942 * While technically a valid reference name, this case is usually
6943 * an unintended typo.
6945 if (branch_name[0] == '-')
6946 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
6948 if (strncmp(branch_name, "refs/heads/", 11) == 0)
6951 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
6952 err = got_error_from_errno("asprintf");
6956 err = got_ref_open(&ref, repo, refname, 0);
6958 err = got_error(GOT_ERR_BRANCH_EXISTS);
6960 } else if (err->code != GOT_ERR_NOT_REF)
6963 err = got_ref_alloc(&ref, refname, base_commit_id);
6967 err = got_ref_write(ref, repo);
6975 static const struct got_error *
6976 cmd_branch(int argc, char *argv[])
6978 const struct got_error *error = NULL;
6979 struct got_repository *repo = NULL;
6980 struct got_worktree *worktree = NULL;
6981 char *cwd = NULL, *repo_path = NULL;
6982 int ch, do_list = 0, do_show = 0, do_update = 1, sort_by_time = 0;
6983 const char *delref = NULL, *commit_id_arg = NULL;
6984 struct got_reference *ref = NULL;
6985 struct got_pathlist_head paths;
6986 struct got_object_id *commit_id = NULL;
6987 char *commit_id_str = NULL, *keyword_idstr = NULL;;
6988 int *pack_fds = NULL;
6993 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6994 "sendfd unveil", NULL) == -1)
6998 while ((ch = getopt(argc, argv, "c:d:lnr:t")) != -1) {
7001 commit_id_arg = optarg;
7013 repo_path = realpath(optarg, NULL);
7014 if (repo_path == NULL)
7015 return got_error_from_errno2("realpath",
7017 got_path_strip_trailing_slashes(repo_path);
7028 if (do_list && delref)
7029 option_conflict('l', 'd');
7030 if (sort_by_time && !do_list)
7031 errx(1, "-t option requires -l option");
7036 if (!do_list && !delref && argc == 0)
7039 if ((do_list || delref || do_show) && commit_id_arg != NULL)
7040 errx(1, "-c option can only be used when creating a branch");
7042 if (do_list || delref) {
7045 } else if (!do_show && argc != 1)
7048 cwd = getcwd(NULL, 0);
7050 error = got_error_from_errno("getcwd");
7054 error = got_repo_pack_fds_open(&pack_fds);
7058 if (repo_path == NULL) {
7059 error = got_worktree_open(&worktree, cwd);
7060 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7066 strdup(got_worktree_get_repo_path(worktree));
7067 if (repo_path == NULL)
7068 error = got_error_from_errno("strdup");
7072 repo_path = strdup(cwd);
7073 if (repo_path == NULL) {
7074 error = got_error_from_errno("strdup");
7080 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7085 if (do_list || do_show) {
7086 /* Remove "cpath" promise. */
7087 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
7093 error = apply_unveil(got_repo_get_path(repo), do_list,
7094 worktree ? got_worktree_get_root_path(worktree) : NULL);
7099 error = show_current_branch(repo, worktree);
7101 error = list_branches(repo, worktree, sort_by_time);
7103 error = delete_branch(repo, worktree, delref);
7105 struct got_reflist_head refs;
7107 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
7111 if (commit_id_arg == NULL)
7112 commit_id_arg = worktree ?
7113 got_worktree_get_head_ref_name(worktree) :
7116 error = got_keyword_to_idstr(&keyword_idstr,
7117 commit_id_arg, repo, worktree);
7120 if (keyword_idstr != NULL)
7121 commit_id_arg = keyword_idstr;
7123 error = got_repo_match_object_id(&commit_id, NULL,
7124 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &refs, repo);
7125 got_ref_list_free(&refs);
7128 error = add_branch(repo, argv[0], commit_id);
7131 if (worktree && do_update) {
7132 struct got_update_progress_arg upa;
7133 char *branch_refname = NULL;
7135 error = got_object_id_str(&commit_id_str, commit_id);
7138 error = get_worktree_paths_from_argv(&paths, 0, NULL,
7142 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
7144 error = got_error_from_errno("asprintf");
7147 error = got_ref_open(&ref, repo, branch_refname, 0);
7148 free(branch_refname);
7151 error = switch_head_ref(ref, commit_id, worktree,
7155 error = got_worktree_set_base_commit_id(worktree, repo,
7159 memset(&upa, 0, sizeof(upa));
7160 error = got_worktree_checkout_files(worktree, &paths,
7161 repo, update_progress, &upa, check_cancelled,
7165 if (upa.did_something) {
7166 printf("Updated to %s: %s\n",
7167 got_worktree_get_head_ref_name(worktree),
7170 print_update_progress_stats(&upa);
7174 free(keyword_idstr);
7178 const struct got_error *close_err = got_repo_close(repo);
7183 got_worktree_close(worktree);
7185 const struct got_error *pack_err =
7186 got_repo_pack_fds_close(pack_fds);
7193 free(commit_id_str);
7194 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
7202 fprintf(stderr, "usage: %s tag [-lVv] [-c commit] [-m message] "
7203 "[-r repository-path] [-s signer-id] name\n", getprogname());
7208 static const struct got_error *
7209 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
7211 const struct got_error *err = NULL;
7212 struct got_reflist_entry *re, *se, *new;
7213 struct got_object_id *re_id, *se_id;
7214 struct got_tag_object *re_tag, *se_tag;
7215 time_t re_time, se_time;
7217 STAILQ_FOREACH(re, tags, entry) {
7218 se = STAILQ_FIRST(sorted);
7220 err = got_reflist_entry_dup(&new, re);
7223 STAILQ_INSERT_HEAD(sorted, new, entry);
7226 err = got_ref_resolve(&re_id, repo, re->ref);
7229 err = got_object_open_as_tag(&re_tag, repo, re_id);
7233 re_time = got_object_tag_get_tagger_time(re_tag);
7234 got_object_tag_close(re_tag);
7238 err = got_ref_resolve(&se_id, repo, re->ref);
7241 err = got_object_open_as_tag(&se_tag, repo, se_id);
7245 se_time = got_object_tag_get_tagger_time(se_tag);
7246 got_object_tag_close(se_tag);
7248 if (se_time > re_time) {
7249 err = got_reflist_entry_dup(&new, re);
7252 STAILQ_INSERT_AFTER(sorted, se, new, entry);
7255 se = STAILQ_NEXT(se, entry);
7264 static const struct got_error *
7265 get_tag_refname(char **refname, const char *tag_name)
7267 const struct got_error *err;
7269 if (strncmp("refs/tags/", tag_name, 10) == 0) {
7270 *refname = strdup(tag_name);
7271 if (*refname == NULL)
7272 return got_error_from_errno("strdup");
7273 } else if (asprintf(refname, "refs/tags/%s", tag_name) == -1) {
7274 err = got_error_from_errno("asprintf");
7282 static const struct got_error *
7283 list_tags(struct got_repository *repo, const char *tag_name, int verify_tags,
7284 const char *allowed_signers, const char *revoked_signers, int verbosity)
7286 static const struct got_error *err = NULL;
7287 struct got_reflist_head refs;
7288 struct got_reflist_entry *re;
7289 char *wanted_refname = NULL;
7294 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
7299 struct got_reference *ref;
7300 err = get_tag_refname(&wanted_refname, tag_name);
7303 /* Wanted tag reference should exist. */
7304 err = got_ref_open(&ref, repo, wanted_refname, 0);
7310 TAILQ_FOREACH(re, &refs, entry) {
7311 const char *refname;
7312 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
7314 const char *tagger, *ssh_sig = NULL;
7315 char *sig_msg = NULL;
7317 struct got_object_id *id;
7318 struct got_tag_object *tag;
7319 struct got_commit_object *commit = NULL;
7321 refname = got_ref_get_name(re->ref);
7322 if (strncmp(refname, "refs/tags/", 10) != 0 ||
7323 (wanted_refname && strcmp(refname, wanted_refname) != 0))
7326 refstr = got_ref_to_str(re->ref);
7327 if (refstr == NULL) {
7328 err = got_error_from_errno("got_ref_to_str");
7332 err = got_ref_resolve(&id, repo, re->ref);
7335 err = got_object_open_as_tag(&tag, repo, id);
7337 if (err->code != GOT_ERR_OBJ_TYPE) {
7341 /* "lightweight" tag */
7342 err = got_object_open_as_commit(&commit, repo, id);
7347 tagger = got_object_commit_get_committer(commit);
7349 got_object_commit_get_committer_time(commit);
7350 err = got_object_id_str(&id_str, id);
7356 tagger = got_object_tag_get_tagger(tag);
7357 tagger_time = got_object_tag_get_tagger_time(tag);
7358 err = got_object_id_str(&id_str,
7359 got_object_tag_get_object_id(tag));
7364 if (tag && verify_tags) {
7365 ssh_sig = got_sigs_get_tagmsg_ssh_signature(
7366 got_object_tag_get_message(tag));
7367 if (ssh_sig && allowed_signers == NULL) {
7368 err = got_error_msg(
7369 GOT_ERR_VERIFY_TAG_SIGNATURE,
7370 "SSH signature verification requires "
7371 "setting allowed_signers in "
7377 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
7379 printf("from: %s\n", tagger);
7380 datestr = get_datestr(&tagger_time, datebuf);
7382 printf("date: %s UTC\n", datestr);
7384 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
7386 switch (got_object_tag_get_object_type(tag)) {
7387 case GOT_OBJ_TYPE_BLOB:
7388 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
7391 case GOT_OBJ_TYPE_TREE:
7392 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
7395 case GOT_OBJ_TYPE_COMMIT:
7396 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
7399 case GOT_OBJ_TYPE_TAG:
7400 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
7410 err = got_sigs_verify_tag_ssh(&sig_msg, tag, ssh_sig,
7411 allowed_signers, revoked_signers, verbosity);
7412 if (err && err->code == GOT_ERR_BAD_TAG_SIGNATURE)
7416 printf("signature: %s", sig_msg);
7422 err = got_object_commit_get_logmsg(&tagmsg0, commit);
7425 got_object_commit_close(commit);
7427 tagmsg0 = strdup(got_object_tag_get_message(tag));
7428 got_object_tag_close(tag);
7429 if (tagmsg0 == NULL) {
7430 err = got_error_from_errno("strdup");
7437 line = strsep(&tagmsg, "\n");
7439 printf(" %s\n", line);
7444 got_ref_list_free(&refs);
7445 free(wanted_refname);
7447 if (err == NULL && bad_sigs)
7448 err = got_error(GOT_ERR_BAD_TAG_SIGNATURE);
7452 static const struct got_error *
7453 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
7454 const char *tag_name, const char *repo_path)
7456 const struct got_error *err = NULL;
7457 char *template = NULL, *initial_content = NULL;
7458 char *editor = NULL;
7459 int initial_content_len;
7462 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
7463 err = got_error_from_errno("asprintf");
7467 initial_content_len = asprintf(&initial_content,
7468 "\n# tagging commit %s as %s\n",
7469 commit_id_str, tag_name);
7470 if (initial_content_len == -1) {
7471 err = got_error_from_errno("asprintf");
7475 err = got_opentemp_named_fd(tagmsg_path, &fd, template, "");
7479 if (write(fd, initial_content, initial_content_len) == -1) {
7480 err = got_error_from_errno2("write", *tagmsg_path);
7483 if (close(fd) == -1) {
7484 err = got_error_from_errno2("close", *tagmsg_path);
7489 err = get_editor(&editor);
7492 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content,
7493 initial_content_len, 1);
7495 free(initial_content);
7499 if (fd != -1 && close(fd) == -1 && err == NULL)
7500 err = got_error_from_errno2("close", *tagmsg_path);
7509 static const struct got_error *
7510 add_tag(struct got_repository *repo, const char *tagger,
7511 const char *tag_name, const char *commit_arg, const char *tagmsg_arg,
7512 const char *signer_id, int verbosity)
7514 const struct got_error *err = NULL;
7515 struct got_object_id *commit_id = NULL, *tag_id = NULL;
7516 char *label = NULL, *commit_id_str = NULL;
7517 struct got_reference *ref = NULL;
7518 char *refname = NULL, *tagmsg = NULL;
7519 char *tagmsg_path = NULL, *tag_id_str = NULL;
7520 int preserve_tagmsg = 0;
7521 struct got_reflist_head refs;
7526 * Don't let the user create a tag name with a leading '-'.
7527 * While technically a valid reference name, this case is usually
7528 * an unintended typo.
7530 if (tag_name[0] == '-')
7531 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
7533 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
7537 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
7538 GOT_OBJ_TYPE_COMMIT, &refs, repo);
7542 err = got_object_id_str(&commit_id_str, commit_id);
7546 err = get_tag_refname(&refname, tag_name);
7549 if (strncmp("refs/tags/", tag_name, 10) == 0)
7552 err = got_ref_open(&ref, repo, refname, 0);
7554 err = got_error(GOT_ERR_TAG_EXISTS);
7556 } else if (err->code != GOT_ERR_NOT_REF)
7559 if (tagmsg_arg == NULL) {
7560 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
7561 tag_name, got_repo_get_path(repo));
7563 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
7564 tagmsg_path != NULL)
7565 preserve_tagmsg = 1;
7568 /* Editor is done; we can now apply unveil(2) */
7569 err = got_sigs_apply_unveil();
7572 err = apply_unveil(got_repo_get_path(repo), 0, NULL);
7577 err = got_object_tag_create(&tag_id, tag_name, commit_id,
7578 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, signer_id, repo,
7582 preserve_tagmsg = 1;
7586 err = got_ref_alloc(&ref, refname, tag_id);
7589 preserve_tagmsg = 1;
7593 err = got_ref_write(ref, repo);
7596 preserve_tagmsg = 1;
7600 err = got_object_id_str(&tag_id_str, tag_id);
7603 preserve_tagmsg = 1;
7606 printf("Created tag %s\n", tag_id_str);
7608 if (preserve_tagmsg) {
7609 fprintf(stderr, "%s: tag message preserved in %s\n",
7610 getprogname(), tagmsg_path);
7611 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
7612 err = got_error_from_errno2("unlink", tagmsg_path);
7617 free(commit_id_str);
7621 got_ref_list_free(&refs);
7625 static const struct got_error *
7626 cmd_tag(int argc, char *argv[])
7628 const struct got_error *error = NULL;
7629 struct got_repository *repo = NULL;
7630 struct got_worktree *worktree = NULL;
7631 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
7632 char *gitconfig_path = NULL, *tagger = NULL;
7633 char *allowed_signers = NULL, *revoked_signers = NULL;
7634 const char *signer_id = NULL;
7635 const char *tag_name = NULL, *commit_id_arg = NULL, *tagmsg = NULL;
7636 int ch, do_list = 0, verify_tags = 0, verbosity = 0;
7637 int *pack_fds = NULL;
7640 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
7641 "sendfd unveil", NULL) == -1)
7645 while ((ch = getopt(argc, argv, "c:lm:r:s:Vv")) != -1) {
7648 commit_id_arg = optarg;
7657 repo_path = realpath(optarg, NULL);
7658 if (repo_path == NULL) {
7659 error = got_error_from_errno2("realpath",
7663 got_path_strip_trailing_slashes(repo_path);
7674 else if (verbosity < 3)
7686 if (do_list || verify_tags) {
7687 if (commit_id_arg != NULL)
7689 "-c option can only be used when creating a tag");
7692 option_conflict('l', 'm');
7694 option_conflict('V', 'm');
7698 option_conflict('l', 's');
7700 option_conflict('V', 's');
7704 } else if (argc != 1)
7710 cwd = getcwd(NULL, 0);
7712 error = got_error_from_errno("getcwd");
7716 error = got_repo_pack_fds_open(&pack_fds);
7720 if (repo_path == NULL) {
7721 error = got_worktree_open(&worktree, cwd);
7722 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7728 strdup(got_worktree_get_repo_path(worktree));
7729 if (repo_path == NULL)
7730 error = got_error_from_errno("strdup");
7734 repo_path = strdup(cwd);
7735 if (repo_path == NULL) {
7736 error = got_error_from_errno("strdup");
7742 if (do_list || verify_tags) {
7743 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7746 error = get_allowed_signers(&allowed_signers, repo, worktree);
7749 error = get_revoked_signers(&revoked_signers, repo, worktree);
7753 /* Release work tree lock. */
7754 got_worktree_close(worktree);
7759 * Remove "cpath" promise unless needed for signature tmpfile
7763 got_sigs_apply_unveil();
7766 if (pledge("stdio rpath wpath flock proc exec sendfd "
7767 "unveil", NULL) == -1)
7771 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7774 error = list_tags(repo, tag_name, verify_tags, allowed_signers,
7775 revoked_signers, verbosity);
7777 error = get_gitconfig_path(&gitconfig_path);
7780 error = got_repo_open(&repo, repo_path, gitconfig_path,
7785 error = get_author(&tagger, repo, worktree);
7788 if (signer_id == NULL)
7789 signer_id = get_signer_id(repo, worktree);
7793 error = got_sigs_apply_unveil();
7797 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
7802 if (commit_id_arg == NULL) {
7803 struct got_reference *head_ref;
7804 struct got_object_id *commit_id;
7805 error = got_ref_open(&head_ref, repo,
7806 worktree ? got_worktree_get_head_ref_name(worktree)
7810 error = got_ref_resolve(&commit_id, repo, head_ref);
7811 got_ref_close(head_ref);
7814 error = got_object_id_str(&commit_id_str, commit_id);
7821 /* Release work tree lock. */
7822 got_worktree_close(worktree);
7826 error = add_tag(repo, tagger, tag_name,
7827 commit_id_str ? commit_id_str : commit_id_arg, tagmsg,
7828 signer_id, verbosity);
7832 const struct got_error *close_err = got_repo_close(repo);
7837 got_worktree_close(worktree);
7839 const struct got_error *pack_err =
7840 got_repo_pack_fds_close(pack_fds);
7846 free(gitconfig_path);
7847 free(commit_id_str);
7849 free(allowed_signers);
7850 free(revoked_signers);
7857 fprintf(stderr, "usage: %s add [-IR] path ...\n", getprogname());
7861 static const struct got_error *
7862 add_progress(void *arg, unsigned char status, const char *path)
7864 while (path[0] == '/')
7866 printf("%c %s\n", status, path);
7870 static const struct got_error *
7871 cmd_add(int argc, char *argv[])
7873 const struct got_error *error = NULL;
7874 struct got_repository *repo = NULL;
7875 struct got_worktree *worktree = NULL;
7877 struct got_pathlist_head paths;
7878 struct got_pathlist_entry *pe;
7879 int ch, can_recurse = 0, no_ignores = 0;
7880 int *pack_fds = NULL;
7885 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7890 while ((ch = getopt(argc, argv, "IR")) != -1) {
7910 cwd = getcwd(NULL, 0);
7912 error = got_error_from_errno("getcwd");
7916 error = got_repo_pack_fds_open(&pack_fds);
7920 error = got_worktree_open(&worktree, cwd);
7922 if (error->code == GOT_ERR_NOT_WORKTREE)
7923 error = wrap_not_worktree_error(error, "add", cwd);
7927 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7932 error = apply_unveil(got_repo_get_path(repo), 1,
7933 got_worktree_get_root_path(worktree));
7937 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7944 TAILQ_FOREACH(pe, &paths, entry) {
7945 if (asprintf(&ondisk_path, "%s/%s",
7946 got_worktree_get_root_path(worktree),
7948 error = got_error_from_errno("asprintf");
7951 if (lstat(ondisk_path, &sb) == -1) {
7952 if (errno == ENOENT) {
7956 error = got_error_from_errno2("lstat",
7962 if (S_ISDIR(sb.st_mode)) {
7963 error = got_error_msg(GOT_ERR_BAD_PATH,
7964 "adding directories requires -R option");
7970 error = got_worktree_schedule_add(worktree, &paths, add_progress,
7971 NULL, repo, no_ignores);
7974 const struct got_error *close_err = got_repo_close(repo);
7979 got_worktree_close(worktree);
7981 const struct got_error *pack_err =
7982 got_repo_pack_fds_close(pack_fds);
7986 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
7994 fprintf(stderr, "usage: %s remove [-fkR] [-s status-codes] path ...\n",
7999 static const struct got_error *
8000 print_remove_status(void *arg, unsigned char status,
8001 unsigned char staged_status, const char *path)
8003 while (path[0] == '/')
8005 if (status == GOT_STATUS_NONEXISTENT)
8007 if (status == staged_status && (status == GOT_STATUS_DELETE))
8008 status = GOT_STATUS_NO_CHANGE;
8009 printf("%c%c %s\n", status, staged_status, path);
8013 static const struct got_error *
8014 cmd_remove(int argc, char *argv[])
8016 const struct got_error *error = NULL;
8017 struct got_worktree *worktree = NULL;
8018 struct got_repository *repo = NULL;
8019 const char *status_codes = NULL;
8021 struct got_pathlist_head paths;
8022 struct got_pathlist_entry *pe;
8023 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0, i;
8024 int ignore_missing_paths = 0;
8025 int *pack_fds = NULL;
8030 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
8035 while ((ch = getopt(argc, argv, "fkRs:")) != -1) {
8038 delete_local_mods = 1;
8039 ignore_missing_paths = 1;
8048 for (i = 0; optarg[i] != '\0'; i++) {
8049 switch (optarg[i]) {
8050 case GOT_STATUS_MODIFY:
8051 delete_local_mods = 1;
8053 case GOT_STATUS_MISSING:
8054 ignore_missing_paths = 1;
8057 errx(1, "invalid status code '%c'",
8061 status_codes = optarg;
8075 cwd = getcwd(NULL, 0);
8077 error = got_error_from_errno("getcwd");
8081 error = got_repo_pack_fds_open(&pack_fds);
8085 error = got_worktree_open(&worktree, cwd);
8087 if (error->code == GOT_ERR_NOT_WORKTREE)
8088 error = wrap_not_worktree_error(error, "remove", cwd);
8092 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8097 error = apply_unveil(got_repo_get_path(repo), 1,
8098 got_worktree_get_root_path(worktree));
8102 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8109 TAILQ_FOREACH(pe, &paths, entry) {
8110 if (asprintf(&ondisk_path, "%s/%s",
8111 got_worktree_get_root_path(worktree),
8113 error = got_error_from_errno("asprintf");
8116 if (lstat(ondisk_path, &sb) == -1) {
8117 if (errno == ENOENT) {
8121 error = got_error_from_errno2("lstat",
8127 if (S_ISDIR(sb.st_mode)) {
8128 error = got_error_msg(GOT_ERR_BAD_PATH,
8129 "removing directories requires -R option");
8135 error = got_worktree_schedule_delete(worktree, &paths,
8136 delete_local_mods, status_codes, print_remove_status, NULL,
8137 repo, keep_on_disk, ignore_missing_paths);
8140 const struct got_error *close_err = got_repo_close(repo);
8145 got_worktree_close(worktree);
8147 const struct got_error *pack_err =
8148 got_repo_pack_fds_close(pack_fds);
8152 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
8160 fprintf(stderr, "usage: %s patch [-nR] [-c commit] [-p strip-count] "
8161 "[patchfile]\n", getprogname());
8165 static const struct got_error *
8166 patch_from_stdin(int *patchfd)
8168 const struct got_error *err = NULL;
8171 sig_t sighup, sigint, sigquit;
8173 *patchfd = got_opentempfd();
8175 return got_error_from_errno("got_opentempfd");
8177 sighup = signal(SIGHUP, SIG_DFL);
8178 sigint = signal(SIGINT, SIG_DFL);
8179 sigquit = signal(SIGQUIT, SIG_DFL);
8182 r = read(0, buf, sizeof(buf));
8184 err = got_error_from_errno("read");
8189 if (write(*patchfd, buf, r) == -1) {
8190 err = got_error_from_errno("write");
8195 signal(SIGHUP, sighup);
8196 signal(SIGINT, sigint);
8197 signal(SIGQUIT, sigquit);
8199 if (err == NULL && lseek(*patchfd, 0, SEEK_SET) == -1)
8200 err = got_error_from_errno("lseek");
8210 struct got_patch_progress_arg {
8216 static const struct got_error *
8217 patch_progress(void *arg, const char *old, const char *new,
8218 unsigned char status, const struct got_error *error, int old_from,
8219 int old_lines, int new_from, int new_lines, int offset,
8220 int ws_mangled, const struct got_error *hunk_err)
8222 const char *path = new == NULL ? old : new;
8223 struct got_patch_progress_arg *a = arg;
8225 while (*path == '/')
8228 if (status != GOT_STATUS_NO_CHANGE &&
8229 status != 0 /* per-hunk progress */) {
8230 printf("%c %s\n", status, path);
8231 a->did_something = 1;
8234 if (hunk_err == NULL) {
8235 if (status == GOT_STATUS_CANNOT_UPDATE)
8237 else if (status == GOT_STATUS_CONFLICT)
8242 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
8244 if (offset != 0 || hunk_err != NULL || ws_mangled) {
8245 printf("@@ -%d,%d +%d,%d @@ ", old_from,
8246 old_lines, new_from, new_lines);
8247 if (hunk_err != NULL)
8248 printf("%s\n", hunk_err->msg);
8249 else if (offset != 0)
8250 printf("applied with offset %d\n", offset);
8252 printf("hunk contains mangled whitespace\n");
8259 print_patch_progress_stats(struct got_patch_progress_arg *ppa)
8261 if (!ppa->did_something)
8264 if (ppa->conflicts > 0)
8265 printf("Files with merge conflicts: %d\n", ppa->conflicts);
8267 if (ppa->rejects > 0) {
8268 printf("Files where patch failed to apply: %d\n",
8273 static const struct got_error *
8274 cmd_patch(int argc, char *argv[])
8276 const struct got_error *error = NULL, *close_error = NULL;
8277 struct got_worktree *worktree = NULL;
8278 struct got_repository *repo = NULL;
8279 struct got_reflist_head refs;
8280 struct got_object_id *commit_id = NULL;
8281 const char *commit_id_str = NULL;
8284 char *cwd = NULL, *keyword_idstr = NULL;
8285 int ch, nop = 0, strip = -1, reverse = 0;
8287 int *pack_fds = NULL;
8288 struct got_patch_progress_arg ppa;
8293 if (pledge("stdio rpath wpath cpath fattr proc exec sendfd flock "
8294 "unveil", NULL) == -1)
8298 while ((ch = getopt(argc, argv, "c:np:R")) != -1) {
8301 commit_id_str = optarg;
8307 strip = strtonum(optarg, 0, INT_MAX, &errstr);
8309 errx(1, "pathname strip count is %s: %s",
8325 error = patch_from_stdin(&patchfd);
8328 } else if (argc == 1) {
8329 patchfd = open(argv[0], O_RDONLY);
8330 if (patchfd == -1) {
8331 error = got_error_from_errno2("open", argv[0]);
8334 if (fstat(patchfd, &sb) == -1) {
8335 error = got_error_from_errno2("fstat", argv[0]);
8338 if (!S_ISREG(sb.st_mode)) {
8339 error = got_error_path(argv[0], GOT_ERR_BAD_FILETYPE);
8345 if ((cwd = getcwd(NULL, 0)) == NULL) {
8346 error = got_error_from_errno("getcwd");
8350 error = got_repo_pack_fds_open(&pack_fds);
8354 error = got_worktree_open(&worktree, cwd);
8358 const char *repo_path = got_worktree_get_repo_path(worktree);
8359 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8363 error = apply_unveil(got_repo_get_path(repo), 0,
8364 got_worktree_get_root_path(worktree));
8368 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
8372 if (commit_id_str != NULL) {
8373 error = got_keyword_to_idstr(&keyword_idstr, commit_id_str,
8378 error = got_repo_match_object_id(&commit_id, NULL,
8379 keyword_idstr != NULL ? keyword_idstr : commit_id_str,
8380 GOT_OBJ_TYPE_COMMIT, &refs, repo);
8385 memset(&ppa, 0, sizeof(ppa));
8386 error = got_patch(patchfd, worktree, repo, nop, strip, reverse,
8387 commit_id, patch_progress, &ppa, check_cancelled, NULL);
8388 print_patch_progress_stats(&ppa);
8390 got_ref_list_free(&refs);
8391 free(keyword_idstr);
8394 close_error = got_repo_close(repo);
8396 error = close_error;
8398 if (worktree != NULL) {
8399 close_error = got_worktree_close(worktree);
8401 error = close_error;
8404 const struct got_error *pack_err =
8405 got_repo_pack_fds_close(pack_fds);
8416 fprintf(stderr, "usage: %s revert [-pR] [-F response-script] path ...\n",
8421 static const struct got_error *
8422 revert_progress(void *arg, unsigned char status, const char *path)
8424 if (status == GOT_STATUS_UNVERSIONED)
8427 while (path[0] == '/')
8429 printf("%c %s\n", status, path);
8433 struct choose_patch_arg {
8434 FILE *patch_script_file;
8438 static const struct got_error *
8439 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
8440 int nchanges, const char *action)
8442 const struct got_error *err;
8444 size_t linesize = 0;
8448 case GOT_STATUS_ADD:
8449 printf("A %s\n%s this addition? [y/n] ", path, action);
8451 case GOT_STATUS_DELETE:
8452 printf("D %s\n%s this deletion? [y/n] ", path, action);
8454 case GOT_STATUS_MODIFY:
8455 if (fseek(patch_file, 0L, SEEK_SET) == -1)
8456 return got_error_from_errno("fseek");
8457 printf(GOT_COMMIT_SEP_STR);
8458 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
8460 if (linelen == -1 && ferror(patch_file)) {
8461 err = got_error_from_errno("getline");
8466 printf(GOT_COMMIT_SEP_STR);
8467 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
8468 path, n, nchanges, action);
8471 return got_error_path(path, GOT_ERR_FILE_STATUS);
8478 static const struct got_error *
8479 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
8480 FILE *patch_file, int n, int nchanges)
8482 const struct got_error *err = NULL;
8484 size_t linesize = 0;
8487 struct choose_patch_arg *a = arg;
8489 *choice = GOT_PATCH_CHOICE_NONE;
8491 if (a->patch_script_file) {
8493 err = show_change(status, path, patch_file, n, nchanges,
8497 linelen = getline(&line, &linesize, a->patch_script_file);
8498 if (linelen == -1) {
8499 if (ferror(a->patch_script_file))
8500 return got_error_from_errno("getline");
8503 nl = strchr(line, '\n');
8506 if (strcmp(line, "y") == 0) {
8507 *choice = GOT_PATCH_CHOICE_YES;
8509 } else if (strcmp(line, "n") == 0) {
8510 *choice = GOT_PATCH_CHOICE_NO;
8512 } else if (strcmp(line, "q") == 0 &&
8513 status == GOT_STATUS_MODIFY) {
8514 *choice = GOT_PATCH_CHOICE_QUIT;
8517 printf("invalid response '%s'\n", line);
8522 while (resp != 'y' && resp != 'n' && resp != 'q') {
8523 err = show_change(status, path, patch_file, n, nchanges,
8530 if (status == GOT_STATUS_MODIFY) {
8531 if (resp != 'y' && resp != 'n' && resp != 'q') {
8532 printf("invalid response '%c'\n", resp);
8535 } else if (resp != 'y' && resp != 'n') {
8536 printf("invalid response '%c'\n", resp);
8542 *choice = GOT_PATCH_CHOICE_YES;
8543 else if (resp == 'n')
8544 *choice = GOT_PATCH_CHOICE_NO;
8545 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
8546 *choice = GOT_PATCH_CHOICE_QUIT;
8551 struct wt_commitable_path_arg {
8552 struct got_pathlist_head *commit_paths;
8557 * Shortcut work tree status callback to determine if the set of paths scanned
8558 * has at least one versioned path that is being modified and, if not NULL, is
8559 * in the arg->commit_paths list. Set arg and return GOT_ERR_FILE_MODIFIED as
8560 * soon as a path is passed with a status that satisfies this criteria.
8562 static const struct got_error *
8563 worktree_has_commitable_path(void *arg, unsigned char status,
8564 unsigned char staged_status, const char *path,
8565 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8566 struct got_object_id *commit_id, int dirfd, const char *de_name)
8568 struct wt_commitable_path_arg *a = arg;
8570 if (status == staged_status && (status == GOT_STATUS_DELETE))
8571 status = GOT_STATUS_NO_CHANGE;
8573 if (!(status == GOT_STATUS_NO_CHANGE ||
8574 status == GOT_STATUS_UNVERSIONED) ||
8575 staged_status != GOT_STATUS_NO_CHANGE) {
8576 if (a->commit_paths != NULL) {
8577 struct got_pathlist_entry *pe;
8579 TAILQ_FOREACH(pe, a->commit_paths, entry) {
8580 if (strncmp(path, pe->path,
8581 pe->path_len) == 0) {
8582 *a->has_changes = 1;
8587 *a->has_changes = 1;
8589 if (*a->has_changes)
8590 return got_error(GOT_ERR_FILE_MODIFIED);
8597 * Check that the changeset of the commit identified by id is
8598 * comprised of at least one modified path that is being committed.
8600 static const struct got_error *
8601 commit_path_changed_in_worktree(struct wt_commitable_path_arg *wcpa,
8602 struct got_object_id *id, struct got_worktree *worktree,
8603 struct got_repository *repo)
8605 const struct got_error *err;
8606 struct got_pathlist_head paths;
8607 struct got_commit_object *commit = NULL, *pcommit = NULL;
8608 struct got_tree_object *tree = NULL, *ptree = NULL;
8609 struct got_object_qid *pid;
8613 err = got_object_open_as_commit(&commit, repo, id);
8617 err = got_object_open_as_tree(&tree, repo,
8618 got_object_commit_get_tree_id(commit));
8622 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
8624 err = got_object_open_as_commit(&pcommit, repo, &pid->id);
8628 err = got_object_open_as_tree(&ptree, repo,
8629 got_object_commit_get_tree_id(pcommit));
8634 err = got_diff_tree(ptree, tree, NULL, NULL, -1, -1, "", "", repo,
8635 got_diff_tree_collect_changed_paths, &paths, 0);
8639 err = got_worktree_status(worktree, &paths, repo, 0,
8640 worktree_has_commitable_path, wcpa, check_cancelled, NULL);
8641 if (err && err->code == GOT_ERR_FILE_MODIFIED) {
8643 * At least one changed path in the referenced commit is
8644 * modified in the work tree, that's all we need to know!
8650 got_pathlist_free(&paths, GOT_PATHLIST_FREE_ALL);
8652 got_object_commit_close(commit);
8654 got_object_commit_close(pcommit);
8656 got_object_tree_close(tree);
8658 got_object_tree_close(ptree);
8663 * Remove any "logmsg" reference comprised entirely of paths that have
8664 * been reverted in this work tree. If any path in the logmsg ref changeset
8665 * remains in a changed state in the worktree, do not remove the reference.
8667 static const struct got_error *
8668 rm_logmsg_ref(struct got_worktree *worktree, struct got_repository *repo)
8670 const struct got_error *err;
8671 struct got_reflist_head refs;
8672 struct got_reflist_entry *re;
8673 struct got_commit_object *commit = NULL;
8674 struct got_object_id *commit_id = NULL;
8675 struct wt_commitable_path_arg wcpa;
8676 char *uuidstr = NULL;
8680 err = got_worktree_get_uuid(&uuidstr, worktree);
8684 err = got_ref_list(&refs, repo, "refs/got/worktree",
8685 got_ref_cmp_by_name, repo);
8689 TAILQ_FOREACH(re, &refs, entry) {
8690 const char *refname;
8691 int has_changes = 0;
8693 refname = got_ref_get_name(re->ref);
8695 if (!strncmp(refname, GOT_WORKTREE_CHERRYPICK_REF_PREFIX,
8696 GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN))
8697 refname += GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN + 1;
8698 else if (!strncmp(refname, GOT_WORKTREE_BACKOUT_REF_PREFIX,
8699 GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN))
8700 refname += GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN + 1;
8704 if (strncmp(refname, uuidstr, GOT_WORKTREE_UUID_STRLEN) == 0)
8705 refname += GOT_WORKTREE_UUID_STRLEN + 1; /* skip '-' */
8709 err = got_repo_match_object_id(&commit_id, NULL, refname,
8710 GOT_OBJ_TYPE_COMMIT, NULL, repo);
8714 err = got_object_open_as_commit(&commit, repo, commit_id);
8718 wcpa.commit_paths = NULL;
8719 wcpa.has_changes = &has_changes;
8721 err = commit_path_changed_in_worktree(&wcpa, commit_id,
8727 err = got_ref_delete(re->ref, repo);
8732 got_object_commit_close(commit);
8741 got_ref_list_free(&refs);
8743 got_object_commit_close(commit);
8747 static const struct got_error *
8748 cmd_revert(int argc, char *argv[])
8750 const struct got_error *error = NULL;
8751 struct got_worktree *worktree = NULL;
8752 struct got_repository *repo = NULL;
8753 char *cwd = NULL, *path = NULL;
8754 struct got_pathlist_head paths;
8755 struct got_pathlist_entry *pe;
8756 int ch, can_recurse = 0, pflag = 0;
8757 FILE *patch_script_file = NULL;
8758 const char *patch_script_path = NULL;
8759 struct choose_patch_arg cpa;
8760 int *pack_fds = NULL;
8765 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8766 "unveil", NULL) == -1)
8770 while ((ch = getopt(argc, argv, "F:pR")) != -1) {
8773 patch_script_path = optarg;
8792 if (patch_script_path && !pflag)
8793 errx(1, "-F option can only be used together with -p option");
8795 cwd = getcwd(NULL, 0);
8797 error = got_error_from_errno("getcwd");
8801 error = got_repo_pack_fds_open(&pack_fds);
8805 error = got_worktree_open(&worktree, cwd);
8807 if (error->code == GOT_ERR_NOT_WORKTREE)
8808 error = wrap_not_worktree_error(error, "revert", cwd);
8812 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8817 if (patch_script_path) {
8818 patch_script_file = fopen(patch_script_path, "re");
8819 if (patch_script_file == NULL) {
8820 error = got_error_from_errno2("fopen",
8827 * XXX "c" perm needed on repo dir to delete merge references.
8829 error = apply_unveil(got_repo_get_path(repo), 0,
8830 got_worktree_get_root_path(worktree));
8834 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8841 TAILQ_FOREACH(pe, &paths, entry) {
8842 if (asprintf(&ondisk_path, "%s/%s",
8843 got_worktree_get_root_path(worktree),
8845 error = got_error_from_errno("asprintf");
8848 if (lstat(ondisk_path, &sb) == -1) {
8849 if (errno == ENOENT) {
8853 error = got_error_from_errno2("lstat",
8859 if (S_ISDIR(sb.st_mode)) {
8860 error = got_error_msg(GOT_ERR_BAD_PATH,
8861 "reverting directories requires -R option");
8867 cpa.patch_script_file = patch_script_file;
8868 cpa.action = "revert";
8869 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
8870 pflag ? choose_patch : NULL, &cpa, repo);
8872 error = rm_logmsg_ref(worktree, repo);
8874 if (patch_script_file && fclose(patch_script_file) == EOF &&
8876 error = got_error_from_errno2("fclose", patch_script_path);
8878 const struct got_error *close_err = got_repo_close(repo);
8883 got_worktree_close(worktree);
8885 const struct got_error *pack_err =
8886 got_repo_pack_fds_close(pack_fds);
8890 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
8899 fprintf(stderr, "usage: %s commit [-CNnS] [-A author] [-F path] "
8900 "[-m message] [path ...]\n", getprogname());
8904 struct collect_commit_logmsg_arg {
8905 const char *cmdline_log;
8906 const char *prepared_log;
8907 const char *merged_log;
8908 int non_interactive;
8910 const char *worktree_path;
8911 const char *branch_name;
8912 const char *repo_path;
8917 static const struct got_error *
8918 read_prepared_logmsg(char **logmsg, const char *path)
8920 const struct got_error *err = NULL;
8926 memset(&sb, 0, sizeof(sb));
8928 f = fopen(path, "re");
8930 return got_error_from_errno2("fopen", path);
8932 if (fstat(fileno(f), &sb) == -1) {
8933 err = got_error_from_errno2("fstat", path);
8936 if (sb.st_size == 0) {
8937 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
8941 *logmsg = malloc(sb.st_size + 1);
8942 if (*logmsg == NULL) {
8943 err = got_error_from_errno("malloc");
8947 r = fread(*logmsg, 1, sb.st_size, f);
8948 if (r != sb.st_size) {
8950 err = got_error_from_errno2("fread", path);
8952 err = got_error(GOT_ERR_IO);
8955 (*logmsg)[sb.st_size] = '\0';
8957 if (fclose(f) == EOF && err == NULL)
8958 err = got_error_from_errno2("fclose", path);
8966 static const struct got_error *
8967 collect_commit_logmsg(struct got_pathlist_head *commitable_paths,
8968 const char *diff_path, char **logmsg, void *arg)
8970 char *initial_content = NULL;
8971 struct got_pathlist_entry *pe;
8972 const struct got_error *err = NULL;
8973 char *template = NULL;
8974 char *prepared_msg = NULL, *merged_msg = NULL;
8975 struct collect_commit_logmsg_arg *a = arg;
8976 int initial_content_len;
8980 /* if a message was specified on the command line, just use it */
8981 if (a->cmdline_log != NULL && *a->cmdline_log != '\0') {
8982 len = strlen(a->cmdline_log) + 1;
8983 *logmsg = malloc(len + 1);
8984 if (*logmsg == NULL)
8985 return got_error_from_errno("malloc");
8986 strlcpy(*logmsg, a->cmdline_log, len);
8988 } else if (a->prepared_log != NULL && a->non_interactive)
8989 return read_prepared_logmsg(logmsg, a->prepared_log);
8991 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
8992 return got_error_from_errno("asprintf");
8994 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template, "");
8998 if (a->prepared_log) {
8999 err = read_prepared_logmsg(&prepared_msg, a->prepared_log);
9002 } else if (a->merged_log) {
9003 err = read_prepared_logmsg(&merged_msg, a->merged_log);
9008 initial_content_len = asprintf(&initial_content,
9009 "%s%s\n# changes to be committed on branch %s:\n",
9010 prepared_msg ? prepared_msg : "",
9011 merged_msg ? merged_msg : "", a->branch_name);
9012 if (initial_content_len == -1) {
9013 err = got_error_from_errno("asprintf");
9017 if (write(fd, initial_content, initial_content_len) == -1) {
9018 err = got_error_from_errno2("write", a->logmsg_path);
9022 TAILQ_FOREACH(pe, commitable_paths, entry) {
9023 struct got_commitable *ct = pe->data;
9024 dprintf(fd, "# %c %s\n",
9025 got_commitable_get_status(ct),
9026 got_commitable_get_path(ct));
9030 dprintf(fd, "# detailed changes can be viewed in %s\n",
9034 if (close(fd) == -1) {
9035 err = got_error_from_errno2("close", a->logmsg_path);
9040 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content,
9041 initial_content_len, a->prepared_log ? 0 : 1);
9043 free(initial_content);
9048 if (fd != -1 && close(fd) == -1 && err == NULL)
9049 err = got_error_from_errno2("close", a->logmsg_path);
9051 /* Editor is done; we can now apply unveil(2) */
9053 err = apply_unveil(a->repo_path, 0, a->worktree_path);
9061 static const struct got_error *
9062 cat_logmsg(FILE *f, struct got_commit_object *commit, const char *idstr,
9063 const char *type, int has_content)
9065 const struct got_error *err = NULL;
9066 char *logmsg = NULL;
9068 err = got_object_commit_get_logmsg(&logmsg, commit);
9072 if (fprintf(f, "%s# log message of %s commit %s:%s",
9073 has_content ? "\n" : "", type, idstr, logmsg) < 0)
9074 err = got_ferror(f, GOT_ERR_IO);
9081 * Lookup "logmsg" references of backed-out and cherrypicked commits
9082 * belonging to the current work tree. If found, and the worktree has
9083 * at least one modified file that was changed in the referenced commit,
9084 * add its log message to a new temporary file at *logmsg_path.
9085 * Add all refs found to matched_refs to be scheduled for removal on
9086 * successful commit.
9088 static const struct got_error *
9089 lookup_logmsg_ref(char **logmsg_path, struct got_pathlist_head *paths,
9090 struct got_reflist_head *matched_refs, struct got_worktree *worktree,
9091 struct got_repository *repo)
9093 const struct got_error *err;
9094 struct got_commit_object *commit = NULL;
9095 struct got_object_id *id = NULL;
9096 struct got_reflist_head refs;
9097 struct got_reflist_entry *re, *re_match;
9099 char *uuidstr = NULL;
9100 int added_logmsg = 0;
9104 *logmsg_path = NULL;
9106 err = got_worktree_get_uuid(&uuidstr, worktree);
9110 err = got_ref_list(&refs, repo, "refs/got/worktree",
9111 got_ref_cmp_by_name, repo);
9115 TAILQ_FOREACH(re, &refs, entry) {
9116 const char *refname, *type;
9117 struct wt_commitable_path_arg wcpa;
9120 refname = got_ref_get_name(re->ref);
9122 if (strncmp(refname, GOT_WORKTREE_CHERRYPICK_REF_PREFIX,
9123 GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN) == 0) {
9124 refname += GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN + 1;
9125 type = "cherrypicked";
9126 } else if (strncmp(refname, GOT_WORKTREE_BACKOUT_REF_PREFIX,
9127 GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN) == 0) {
9128 refname += GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN + 1;
9129 type = "backed-out";
9133 if (strncmp(refname, uuidstr, GOT_WORKTREE_UUID_STRLEN) == 0)
9134 refname += GOT_WORKTREE_UUID_STRLEN + 1; /* skip '-' */
9138 err = got_repo_match_object_id(&id, NULL, refname,
9139 GOT_OBJ_TYPE_COMMIT, NULL, repo);
9143 err = got_object_open_as_commit(&commit, repo, id);
9147 wcpa.commit_paths = paths;
9148 wcpa.has_changes = &add_logmsg;
9150 err = commit_path_changed_in_worktree(&wcpa, id,
9157 err = got_opentemp_named(logmsg_path, &f,
9158 "got-commit-logmsg", "");
9162 err = cat_logmsg(f, commit, refname, type,
9169 err = got_reflist_entry_dup(&re_match, re);
9172 TAILQ_INSERT_HEAD(matched_refs, re_match, entry);
9175 got_object_commit_close(commit);
9184 got_ref_list_free(&refs);
9186 got_object_commit_close(commit);
9187 if (f && fclose(f) == EOF && err == NULL)
9188 err = got_error_from_errno("fclose");
9189 if (!added_logmsg) {
9190 if (*logmsg_path && unlink(*logmsg_path) != 0 && err == NULL)
9191 err = got_error_from_errno2("unlink", *logmsg_path);
9192 *logmsg_path = NULL;
9197 static const struct got_error *
9198 cmd_commit(int argc, char *argv[])
9200 const struct got_error *error = NULL;
9201 struct got_worktree *worktree = NULL;
9202 struct got_repository *repo = NULL;
9203 char *cwd = NULL, *id_str = NULL;
9204 struct got_object_id *id = NULL;
9205 const char *logmsg = NULL;
9206 char *prepared_logmsg = NULL, *merged_logmsg = NULL;
9207 struct collect_commit_logmsg_arg cl_arg;
9208 const char *author = NULL;
9209 char *gitconfig_path = NULL, *editor = NULL, *committer = NULL;
9210 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
9211 int allow_bad_symlinks = 0, non_interactive = 0, merge_in_progress = 0;
9212 int show_diff = 1, commit_conflicts = 0;
9213 struct got_pathlist_head paths;
9214 struct got_reflist_head refs;
9215 struct got_reflist_entry *re;
9216 int *pack_fds = NULL;
9220 cl_arg.logmsg_path = NULL;
9223 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9224 "unveil", NULL) == -1)
9228 while ((ch = getopt(argc, argv, "A:CF:m:NnS")) != -1) {
9232 error = valid_author(author);
9237 commit_conflicts = 1;
9241 option_conflict('F', 'm');
9242 prepared_logmsg = realpath(optarg, NULL);
9243 if (prepared_logmsg == NULL)
9244 return got_error_from_errno2("realpath",
9248 if (prepared_logmsg)
9249 option_conflict('m', 'F');
9253 non_interactive = 1;
9259 allow_bad_symlinks = 1;
9270 cwd = getcwd(NULL, 0);
9272 error = got_error_from_errno("getcwd");
9276 error = got_repo_pack_fds_open(&pack_fds);
9280 error = got_worktree_open(&worktree, cwd);
9282 if (error->code == GOT_ERR_NOT_WORKTREE)
9283 error = wrap_not_worktree_error(error, "commit", cwd);
9287 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
9290 if (rebase_in_progress) {
9291 error = got_error(GOT_ERR_REBASING);
9295 error = got_worktree_histedit_in_progress(&histedit_in_progress,
9300 error = get_gitconfig_path(&gitconfig_path);
9303 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9304 gitconfig_path, pack_fds);
9308 error = got_worktree_merge_in_progress(&merge_in_progress, worktree, repo);
9311 if (merge_in_progress) {
9312 error = got_error(GOT_ERR_MERGE_BUSY);
9316 error = get_author(&committer, repo, worktree);
9324 * unveil(2) traverses exec(2); if an editor is used we have
9325 * to apply unveil after the log message has been written.
9327 if (logmsg == NULL || strlen(logmsg) == 0)
9328 error = get_editor(&editor);
9330 error = apply_unveil(got_repo_get_path(repo), 0,
9331 got_worktree_get_root_path(worktree));
9335 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
9339 if (prepared_logmsg == NULL) {
9340 error = lookup_logmsg_ref(&merged_logmsg,
9341 argc > 0 ? &paths : NULL, &refs, worktree, repo);
9346 cl_arg.editor = editor;
9347 cl_arg.cmdline_log = logmsg;
9348 cl_arg.prepared_log = prepared_logmsg;
9349 cl_arg.merged_log = merged_logmsg;
9350 cl_arg.non_interactive = non_interactive;
9351 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
9352 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
9353 if (!histedit_in_progress) {
9354 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
9355 error = got_error(GOT_ERR_COMMIT_BRANCH);
9358 cl_arg.branch_name += 11;
9360 cl_arg.repo_path = got_repo_get_path(repo);
9361 error = got_worktree_commit(&id, worktree, &paths, author, committer,
9362 allow_bad_symlinks, show_diff, commit_conflicts,
9363 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
9365 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
9366 cl_arg.logmsg_path != NULL)
9367 preserve_logmsg = 1;
9371 error = got_object_id_str(&id_str, id);
9374 printf("Created commit %s\n", id_str);
9376 TAILQ_FOREACH(re, &refs, entry) {
9377 error = got_ref_delete(re->ref, repo);
9383 if (preserve_logmsg) {
9384 fprintf(stderr, "%s: log message preserved in %s\n",
9385 getprogname(), cl_arg.logmsg_path);
9386 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
9388 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
9389 free(cl_arg.logmsg_path);
9390 if (merged_logmsg && unlink(merged_logmsg) == -1 && error == NULL)
9391 error = got_error_from_errno2("unlink", merged_logmsg);
9392 free(merged_logmsg);
9394 const struct got_error *close_err = got_repo_close(repo);
9399 got_worktree_close(worktree);
9401 const struct got_error *pack_err =
9402 got_repo_pack_fds_close(pack_fds);
9406 got_ref_list_free(&refs);
9407 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
9410 free(gitconfig_path);
9413 free(prepared_logmsg);
9420 fprintf(stderr, "usage: %s send [-afqTv] [-b branch] [-d branch] "
9421 "[-r repository-path] [-t tag] [remote-repository]\n",
9427 print_load_info(int print_colored, int print_found, int print_trees,
9428 int ncolored, int nfound, int ntrees)
9430 if (print_colored) {
9431 printf("%d commit%s colored", ncolored,
9432 ncolored == 1 ? "" : "s");
9435 printf("%s%d object%s found",
9436 ncolored > 0 ? "; " : "",
9437 nfound, nfound == 1 ? "" : "s");
9440 printf("; %d tree%s scanned", ntrees,
9441 ntrees == 1 ? "" : "s");
9445 struct got_send_progress_arg {
9446 char last_scaled_packsize[FMT_SCALED_STRSIZE];
9453 int last_nobj_total;
9457 int printed_something;
9459 struct got_pathlist_head *delete_branches;
9462 static const struct got_error *
9463 send_progress(void *arg, int ncolored, int nfound, int ntrees,
9464 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
9465 int nobj_written, off_t bytes_sent, const char *refname,
9466 const char *errmsg, int success)
9468 struct got_send_progress_arg *a = arg;
9469 char scaled_packsize[FMT_SCALED_STRSIZE];
9470 char scaled_sent[FMT_SCALED_STRSIZE];
9471 int p_deltify = 0, p_written = 0, p_sent = 0;
9472 int print_colored = 0, print_found = 0, print_trees = 0;
9473 int print_searching = 0, print_total = 0;
9474 int print_deltify = 0, print_written = 0, print_sent = 0;
9476 if (a->verbosity < 0)
9480 const char *status = success ? "accepted" : "rejected";
9483 struct got_pathlist_entry *pe;
9484 TAILQ_FOREACH(pe, a->delete_branches, entry) {
9485 const char *branchname = pe->path;
9486 if (got_path_cmp(branchname, refname,
9487 strlen(branchname), strlen(refname)) == 0) {
9489 a->sent_something = 1;
9495 if (a->printed_something)
9497 printf("Server has %s %s", status, refname);
9499 printf(": %s", errmsg);
9500 a->printed_something = 1;
9504 if (a->last_ncolored != ncolored) {
9506 a->last_ncolored = ncolored;
9509 if (a->last_nfound != nfound) {
9512 a->last_nfound = nfound;
9515 if (a->last_ntrees != ntrees) {
9519 a->last_ntrees = ntrees;
9522 if ((print_colored || print_found || print_trees) &&
9525 print_load_info(print_colored, print_found, print_trees,
9526 ncolored, nfound, ntrees);
9527 a->printed_something = 1;
9530 } else if (!a->loading_done) {
9532 print_load_info(1, 1, 1, ncolored, nfound, ntrees);
9534 a->loading_done = 1;
9537 if (fmt_scaled(packfile_size, scaled_packsize) == -1)
9538 return got_error_from_errno("fmt_scaled");
9539 if (fmt_scaled(bytes_sent, scaled_sent) == -1)
9540 return got_error_from_errno("fmt_scaled");
9542 if (a->last_ncommits != ncommits) {
9543 print_searching = 1;
9544 a->last_ncommits = ncommits;
9547 if (a->last_nobj_total != nobj_total) {
9548 print_searching = 1;
9550 a->last_nobj_total = nobj_total;
9553 if (packfile_size > 0 && (a->last_scaled_packsize[0] == '\0' ||
9554 strcmp(scaled_packsize, a->last_scaled_packsize)) != 0) {
9555 if (strlcpy(a->last_scaled_packsize, scaled_packsize,
9556 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
9557 return got_error(GOT_ERR_NO_SPACE);
9560 if (nobj_deltify > 0 || nobj_written > 0) {
9561 if (nobj_deltify > 0) {
9562 p_deltify = (nobj_deltify * 100) / nobj_total;
9563 if (p_deltify != a->last_p_deltify) {
9564 a->last_p_deltify = p_deltify;
9565 print_searching = 1;
9570 if (nobj_written > 0) {
9571 p_written = (nobj_written * 100) / nobj_total;
9572 if (p_written != a->last_p_written) {
9573 a->last_p_written = p_written;
9574 print_searching = 1;
9582 if (bytes_sent > 0) {
9583 p_sent = (bytes_sent * 100) / packfile_size;
9584 if (p_sent != a->last_p_sent) {
9585 a->last_p_sent = p_sent;
9586 print_searching = 1;
9592 a->sent_something = 1;
9595 if (print_searching || print_total || print_deltify || print_written ||
9598 if (print_searching)
9599 printf("packing %d reference%s", ncommits,
9600 ncommits == 1 ? "" : "s");
9602 printf("; %d object%s", nobj_total,
9603 nobj_total == 1 ? "" : "s");
9605 printf("; deltify: %d%%", p_deltify);
9607 printf("; uploading pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
9608 scaled_packsize, p_sent);
9609 else if (print_written)
9610 printf("; writing pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
9611 scaled_packsize, p_written);
9612 if (print_searching || print_total || print_deltify ||
9613 print_written || print_sent) {
9614 a->printed_something = 1;
9620 static const struct got_error *
9621 cmd_send(int argc, char *argv[])
9623 const struct got_error *error = NULL;
9624 char *cwd = NULL, *repo_path = NULL;
9625 const char *remote_name;
9626 char *proto = NULL, *host = NULL, *port = NULL;
9627 char *repo_name = NULL, *server_path = NULL;
9628 const struct got_remote_repo *remotes, *remote = NULL;
9629 int nremotes, nbranches = 0, ndelete_branches = 0;
9630 struct got_repository *repo = NULL;
9631 struct got_worktree *worktree = NULL;
9632 const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
9633 struct got_pathlist_head branches;
9634 struct got_pathlist_head tags;
9635 struct got_reflist_head all_branches;
9636 struct got_reflist_head all_tags;
9637 struct got_pathlist_head delete_args;
9638 struct got_pathlist_head delete_branches;
9639 struct got_reflist_entry *re;
9640 struct got_pathlist_entry *pe;
9641 int i, ch, sendfd = -1, sendstatus;
9643 struct got_send_progress_arg spa;
9644 int verbosity = 0, overwrite_refs = 0;
9645 int send_all_branches = 0, send_all_tags = 0;
9646 struct got_reference *ref = NULL;
9647 int *pack_fds = NULL;
9649 TAILQ_INIT(&branches);
9651 TAILQ_INIT(&all_branches);
9652 TAILQ_INIT(&all_tags);
9653 TAILQ_INIT(&delete_args);
9654 TAILQ_INIT(&delete_branches);
9656 while ((ch = getopt(argc, argv, "ab:d:fqr:Tt:v")) != -1) {
9659 send_all_branches = 1;
9662 error = got_pathlist_append(&branches, optarg, NULL);
9668 error = got_pathlist_append(&delete_args, optarg, NULL);
9679 repo_path = realpath(optarg, NULL);
9680 if (repo_path == NULL)
9681 return got_error_from_errno2("realpath",
9683 got_path_strip_trailing_slashes(repo_path);
9689 error = got_pathlist_append(&tags, optarg, NULL);
9696 else if (verbosity < 3)
9707 if (send_all_branches && !TAILQ_EMPTY(&branches))
9708 option_conflict('a', 'b');
9709 if (send_all_tags && !TAILQ_EMPTY(&tags))
9710 option_conflict('T', 't');
9714 remote_name = GOT_SEND_DEFAULT_REMOTE_NAME;
9716 remote_name = argv[0];
9720 cwd = getcwd(NULL, 0);
9722 error = got_error_from_errno("getcwd");
9726 error = got_repo_pack_fds_open(&pack_fds);
9730 if (repo_path == NULL) {
9731 error = got_worktree_open(&worktree, cwd);
9732 if (error && error->code != GOT_ERR_NOT_WORKTREE)
9738 strdup(got_worktree_get_repo_path(worktree));
9739 if (repo_path == NULL)
9740 error = got_error_from_errno("strdup");
9744 repo_path = strdup(cwd);
9745 if (repo_path == NULL) {
9746 error = got_error_from_errno("strdup");
9752 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
9757 worktree_conf = got_worktree_get_gotconfig(worktree);
9758 if (worktree_conf) {
9759 got_gotconfig_get_remotes(&nremotes, &remotes,
9761 for (i = 0; i < nremotes; i++) {
9762 if (strcmp(remotes[i].name, remote_name) == 0) {
9763 remote = &remotes[i];
9769 if (remote == NULL) {
9770 repo_conf = got_repo_get_gotconfig(repo);
9772 got_gotconfig_get_remotes(&nremotes, &remotes,
9774 for (i = 0; i < nremotes; i++) {
9775 if (strcmp(remotes[i].name, remote_name) == 0) {
9776 remote = &remotes[i];
9782 if (remote == NULL) {
9783 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
9784 for (i = 0; i < nremotes; i++) {
9785 if (strcmp(remotes[i].name, remote_name) == 0) {
9786 remote = &remotes[i];
9791 if (remote == NULL) {
9792 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
9796 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
9797 &repo_name, remote->send_url);
9801 if (strcmp(proto, "git") == 0) {
9803 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
9804 "sendfd dns inet unveil", NULL) == -1)
9807 } else if (strcmp(proto, "git+ssh") == 0 ||
9808 strcmp(proto, "ssh") == 0) {
9810 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
9811 "sendfd unveil", NULL) == -1)
9814 } else if (strcmp(proto, "http") == 0 ||
9815 strcmp(proto, "git+http") == 0) {
9816 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
9819 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
9823 error = got_dial_apply_unveil(proto);
9827 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
9831 if (send_all_branches) {
9832 error = got_ref_list(&all_branches, repo, "refs/heads",
9833 got_ref_cmp_by_name, NULL);
9836 TAILQ_FOREACH(re, &all_branches, entry) {
9837 const char *branchname = got_ref_get_name(re->ref);
9838 error = got_pathlist_append(&branches,
9844 } else if (nbranches == 0) {
9845 for (i = 0; i < remote->nsend_branches; i++) {
9846 error = got_pathlist_append(&branches,
9847 remote->send_branches[i], NULL);
9853 if (send_all_tags) {
9854 error = got_ref_list(&all_tags, repo, "refs/tags",
9855 got_ref_cmp_by_name, NULL);
9858 TAILQ_FOREACH(re, &all_tags, entry) {
9859 const char *tagname = got_ref_get_name(re->ref);
9860 error = got_pathlist_append(&tags,
9868 * To prevent accidents only branches in refs/heads/ can be deleted
9869 * with 'got send -d'.
9870 * Deleting anything else requires local repository access or Git.
9872 TAILQ_FOREACH(pe, &delete_args, entry) {
9873 const char *branchname = pe->path;
9875 struct got_pathlist_entry *new;
9876 if (strncmp(branchname, "refs/heads/", 11) == 0) {
9877 s = strdup(branchname);
9879 error = got_error_from_errno("strdup");
9883 if (asprintf(&s, "refs/heads/%s", branchname) == -1) {
9884 error = got_error_from_errno("asprintf");
9888 error = got_pathlist_insert(&new, &delete_branches, s, NULL);
9889 if (error || new == NULL /* duplicate */)
9896 if (nbranches == 0 && ndelete_branches == 0) {
9897 struct got_reference *head_ref;
9899 error = got_ref_open(&head_ref, repo,
9900 got_worktree_get_head_ref_name(worktree), 0);
9902 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
9905 if (got_ref_is_symbolic(head_ref)) {
9906 error = got_ref_resolve_symbolic(&ref, repo, head_ref);
9907 got_ref_close(head_ref);
9912 error = got_pathlist_append(&branches, got_ref_get_name(ref),
9919 if (verbosity >= 0) {
9920 printf("Connecting to \"%s\" %s://%s%s%s%s%s\n",
9921 remote->name, proto, host,
9922 port ? ":" : "", port ? port : "",
9923 *server_path == '/' ? "" : "/", server_path);
9926 error = got_send_connect(&sendpid, &sendfd, proto, host, port,
9927 server_path, verbosity);
9931 memset(&spa, 0, sizeof(spa));
9932 spa.last_scaled_packsize[0] = '\0';
9933 spa.last_p_deltify = -1;
9934 spa.last_p_written = -1;
9935 spa.verbosity = verbosity;
9936 spa.delete_branches = &delete_branches;
9937 error = got_send_pack(remote_name, &branches, &tags, &delete_branches,
9938 verbosity, overwrite_refs, sendfd, repo, send_progress, &spa,
9939 check_cancelled, NULL);
9940 if (spa.printed_something)
9944 if (!spa.sent_something && verbosity >= 0)
9945 printf("Already up-to-date\n");
9948 if (kill(sendpid, SIGTERM) == -1)
9949 error = got_error_from_errno("kill");
9950 if (waitpid(sendpid, &sendstatus, 0) == -1 && error == NULL)
9951 error = got_error_from_errno("waitpid");
9953 if (sendfd != -1 && close(sendfd) == -1 && error == NULL)
9954 error = got_error_from_errno("close");
9956 const struct got_error *close_err = got_repo_close(repo);
9961 got_worktree_close(worktree);
9963 const struct got_error *pack_err =
9964 got_repo_pack_fds_close(pack_fds);
9970 got_pathlist_free(&branches, GOT_PATHLIST_FREE_NONE);
9971 got_pathlist_free(&tags, GOT_PATHLIST_FREE_NONE);
9972 got_ref_list_free(&all_branches);
9973 got_ref_list_free(&all_tags);
9974 got_pathlist_free(&delete_args, GOT_PATHLIST_FREE_NONE);
9975 got_pathlist_free(&delete_branches, GOT_PATHLIST_FREE_PATH);
9987 * Print and if delete is set delete all ref_prefix references.
9988 * If wanted_ref is not NULL, only print or delete this reference.
9990 static const struct got_error *
9991 process_logmsg_refs(const char *ref_prefix, size_t prefix_len,
9992 const char *wanted_ref, int delete, struct got_worktree *worktree,
9993 struct got_repository *repo)
9995 const struct got_error *err;
9996 struct got_pathlist_head paths;
9997 struct got_reflist_head refs;
9998 struct got_reflist_entry *re;
9999 struct got_reflist_object_id_map *refs_idmap = NULL;
10000 struct got_commit_object *commit = NULL;
10001 struct got_object_id *id = NULL;
10002 const char *header_prefix;
10003 char *uuidstr = NULL;
10007 TAILQ_INIT(&paths);
10009 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, repo);
10013 err = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
10017 if (worktree != NULL) {
10018 err = got_worktree_get_uuid(&uuidstr, worktree);
10024 if (strncmp(wanted_ref, "refs/heads/", 11) == 0)
10028 if (strcmp(ref_prefix, GOT_WORKTREE_BACKOUT_REF_PREFIX) == 0)
10029 header_prefix = "backout";
10031 header_prefix = "cherrypick";
10033 TAILQ_FOREACH(re, &refs, entry) {
10034 const char *refname, *wt;
10036 refname = got_ref_get_name(re->ref);
10038 err = check_cancelled(NULL);
10042 if (strncmp(refname, ref_prefix, prefix_len) == 0)
10043 refname += prefix_len + 1; /* skip '-' delimiter */
10049 if (worktree == NULL || strncmp(refname, uuidstr,
10050 GOT_WORKTREE_UUID_STRLEN) == 0)
10051 refname += GOT_WORKTREE_UUID_STRLEN + 1; /* skip '-' */
10055 err = got_repo_match_object_id(&id, NULL, refname,
10056 GOT_OBJ_TYPE_COMMIT, NULL, repo);
10060 err = got_object_open_as_commit(&commit, repo, id);
10065 found = strncmp(wanted_ref, refname,
10066 strlen(wanted_ref)) == 0;
10067 if (wanted_ref && !found) {
10068 struct got_reflist_head *ci_refs;
10070 ci_refs = got_reflist_object_id_map_lookup(refs_idmap,
10074 char *refs_str = NULL;
10075 char const *r = NULL;
10077 err = build_refs_str(&refs_str, ci_refs, id,
10084 if (strncmp(r, wanted_ref,
10085 strlen(wanted_ref)) == 0) {
10089 r = strchr(r, ' ');
10097 if (wanted_ref == NULL || found) {
10099 err = got_ref_delete(re->ref, repo);
10102 printf("Deleted: ");
10103 err = print_commit_oneline(commit, id, repo,
10107 * Print paths modified by commit to help
10108 * associate commits with worktree changes.
10110 err = get_changed_paths(&paths, commit,
10115 err = print_commit(commit, id, repo, NULL,
10116 &paths, NULL, 0, 0, refs_idmap, NULL,
10118 got_pathlist_free(&paths,
10119 GOT_PATHLIST_FREE_ALL);
10121 if (worktree == NULL)
10122 printf("work tree: %.*s\n\n",
10123 GOT_WORKTREE_UUID_STRLEN, wt);
10129 got_object_commit_close(commit);
10135 if (wanted_ref != NULL && !found)
10136 err = got_error_fmt(GOT_ERR_NOT_REF, "%s", wanted_ref);
10141 got_ref_list_free(&refs);
10142 got_pathlist_free(&paths, GOT_PATHLIST_FREE_ALL);
10144 got_reflist_object_id_map_free(refs_idmap);
10146 got_object_commit_close(commit);
10151 * Create new temp "logmsg" ref of the backed-out or cherrypicked commit
10152 * identified by id for log messages to prepopulate the editor on commit.
10154 static const struct got_error *
10155 logmsg_ref(struct got_object_id *id, const char *prefix,
10156 struct got_worktree *worktree, struct got_repository *repo)
10158 const struct got_error *err = NULL;
10159 char *idstr, *ref = NULL, *refname = NULL;
10160 int histedit_in_progress;
10161 int rebase_in_progress, merge_in_progress;
10164 * Silenty refuse to create merge reference if any histedit, merge,
10165 * or rebase operation is in progress.
10167 err = got_worktree_histedit_in_progress(&histedit_in_progress,
10171 if (histedit_in_progress)
10174 err = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
10177 if (rebase_in_progress)
10180 err = got_worktree_merge_in_progress(&merge_in_progress, worktree,
10184 if (merge_in_progress)
10187 err = got_object_id_str(&idstr, id);
10191 err = got_worktree_get_logmsg_ref_name(&refname, worktree, prefix);
10195 if (asprintf(&ref, "%s-%s", refname, idstr) == -1) {
10196 err = got_error_from_errno("asprintf");
10200 err = create_ref(ref, got_worktree_get_base_commit_id(worktree),
10210 usage_cherrypick(void)
10212 fprintf(stderr, "usage: %s cherrypick [-lX] [commit-id]\n",
10217 static const struct got_error *
10218 cmd_cherrypick(int argc, char *argv[])
10220 const struct got_error *error = NULL;
10221 struct got_worktree *worktree = NULL;
10222 struct got_repository *repo = NULL;
10223 char *cwd = NULL, *commit_id_str = NULL, *keyword_idstr = NULL;
10224 struct got_object_id *commit_id = NULL;
10225 struct got_commit_object *commit = NULL;
10226 struct got_object_qid *pid;
10227 int ch, list_refs = 0, remove_refs = 0;
10228 struct got_update_progress_arg upa;
10229 int *pack_fds = NULL;
10232 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10233 "unveil", NULL) == -1)
10237 while ((ch = getopt(argc, argv, "lX")) != -1) {
10246 usage_cherrypick();
10254 if (list_refs || remove_refs) {
10255 if (argc != 0 && argc != 1)
10256 usage_cherrypick();
10257 } else if (argc != 1)
10258 usage_cherrypick();
10259 if (list_refs && remove_refs)
10260 option_conflict('l', 'X');
10262 cwd = getcwd(NULL, 0);
10264 error = got_error_from_errno("getcwd");
10268 error = got_repo_pack_fds_open(&pack_fds);
10272 error = got_worktree_open(&worktree, cwd);
10274 if (list_refs || remove_refs) {
10275 if (error->code != GOT_ERR_NOT_WORKTREE)
10278 if (error->code == GOT_ERR_NOT_WORKTREE)
10279 error = wrap_not_worktree_error(error,
10280 "cherrypick", cwd);
10285 error = got_repo_open(&repo,
10286 worktree ? got_worktree_get_repo_path(worktree) : cwd,
10291 error = apply_unveil(got_repo_get_path(repo), 0,
10292 worktree ? got_worktree_get_root_path(worktree) : NULL);
10296 if (list_refs || remove_refs) {
10297 error = process_logmsg_refs(GOT_WORKTREE_CHERRYPICK_REF_PREFIX,
10298 GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN,
10299 argc == 1 ? argv[0] : NULL, remove_refs, worktree, repo);
10303 error = got_keyword_to_idstr(&keyword_idstr, argv[0], repo, worktree);
10307 error = got_repo_match_object_id(&commit_id, NULL,
10308 keyword_idstr != NULL ? keyword_idstr : argv[0],
10309 GOT_OBJ_TYPE_COMMIT, NULL, repo);
10312 error = got_object_id_str(&commit_id_str, commit_id);
10316 error = got_object_open_as_commit(&commit, repo, commit_id);
10319 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
10320 memset(&upa, 0, sizeof(upa));
10321 error = got_worktree_merge_files(worktree, pid ? &pid->id : NULL,
10322 commit_id, repo, update_progress, &upa, check_cancelled,
10327 if (upa.did_something) {
10328 error = logmsg_ref(commit_id,
10329 GOT_WORKTREE_CHERRYPICK_REF_PREFIX, worktree, repo);
10332 printf("Merged commit %s\n", commit_id_str);
10334 print_merge_progress_stats(&upa);
10337 free(keyword_idstr);
10339 got_object_commit_close(commit);
10340 free(commit_id_str);
10342 got_worktree_close(worktree);
10344 const struct got_error *close_err = got_repo_close(repo);
10349 const struct got_error *pack_err =
10350 got_repo_pack_fds_close(pack_fds);
10359 usage_backout(void)
10361 fprintf(stderr, "usage: %s backout [-lX] [commit-id]\n", getprogname());
10365 static const struct got_error *
10366 cmd_backout(int argc, char *argv[])
10368 const struct got_error *error = NULL;
10369 struct got_worktree *worktree = NULL;
10370 struct got_repository *repo = NULL;
10371 char *cwd = NULL, *commit_id_str = NULL, *keyword_idstr = NULL;
10372 struct got_object_id *commit_id = NULL;
10373 struct got_commit_object *commit = NULL;
10374 struct got_object_qid *pid;
10375 int ch, list_refs = 0, remove_refs = 0;
10376 struct got_update_progress_arg upa;
10377 int *pack_fds = NULL;
10380 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10381 "unveil", NULL) == -1)
10385 while ((ch = getopt(argc, argv, "lX")) != -1) {
10402 if (list_refs || remove_refs) {
10403 if (argc != 0 && argc != 1)
10405 } else if (argc != 1)
10407 if (list_refs && remove_refs)
10408 option_conflict('l', 'X');
10410 cwd = getcwd(NULL, 0);
10412 error = got_error_from_errno("getcwd");
10416 error = got_repo_pack_fds_open(&pack_fds);
10420 error = got_worktree_open(&worktree, cwd);
10422 if (list_refs || remove_refs) {
10423 if (error->code != GOT_ERR_NOT_WORKTREE)
10426 if (error->code == GOT_ERR_NOT_WORKTREE)
10427 error = wrap_not_worktree_error(error,
10433 error = got_repo_open(&repo,
10434 worktree ? got_worktree_get_repo_path(worktree) : cwd,
10439 error = apply_unveil(got_repo_get_path(repo), 0,
10440 worktree ? got_worktree_get_root_path(worktree) : NULL);
10444 if (list_refs || remove_refs) {
10445 error = process_logmsg_refs(GOT_WORKTREE_BACKOUT_REF_PREFIX,
10446 GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN,
10447 argc == 1 ? argv[0] : NULL, remove_refs, worktree, repo);
10451 error = got_keyword_to_idstr(&keyword_idstr, argv[0], repo, worktree);
10455 error = got_repo_match_object_id(&commit_id, NULL,
10456 keyword_idstr != NULL ? keyword_idstr : argv[0],
10457 GOT_OBJ_TYPE_COMMIT, NULL, repo);
10460 error = got_object_id_str(&commit_id_str, commit_id);
10464 error = got_object_open_as_commit(&commit, repo, commit_id);
10467 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
10469 error = got_error(GOT_ERR_ROOT_COMMIT);
10473 memset(&upa, 0, sizeof(upa));
10474 error = got_worktree_merge_files(worktree, commit_id, &pid->id,
10475 repo, update_progress, &upa, check_cancelled, NULL);
10479 if (upa.did_something) {
10480 error = logmsg_ref(commit_id, GOT_WORKTREE_BACKOUT_REF_PREFIX,
10484 printf("Backed out commit %s\n", commit_id_str);
10486 print_merge_progress_stats(&upa);
10489 free(keyword_idstr);
10491 got_object_commit_close(commit);
10492 free(commit_id_str);
10494 got_worktree_close(worktree);
10496 const struct got_error *close_err = got_repo_close(repo);
10501 const struct got_error *pack_err =
10502 got_repo_pack_fds_close(pack_fds);
10512 fprintf(stderr, "usage: %s rebase [-aCclX] [branch]\n", getprogname());
10517 trim_logmsg(char *logmsg, int limit)
10522 len = strlen(logmsg);
10525 logmsg[len] = '\0';
10526 nl = strchr(logmsg, '\n');
10531 static const struct got_error *
10532 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
10534 const struct got_error *err;
10535 char *logmsg0 = NULL;
10538 err = got_object_commit_get_logmsg(&logmsg0, commit);
10543 while (isspace((unsigned char)s[0]))
10546 *logmsg = strdup(s);
10547 if (*logmsg == NULL) {
10548 err = got_error_from_errno("strdup");
10552 trim_logmsg(*logmsg, limit);
10558 static const struct got_error *
10559 show_rebase_merge_conflict(struct got_object_id *id,
10560 struct got_repository *repo)
10562 const struct got_error *err;
10563 struct got_commit_object *commit = NULL;
10564 char *id_str = NULL, *logmsg = NULL;
10566 err = got_object_open_as_commit(&commit, repo, id);
10570 err = got_object_id_str(&id_str, id);
10576 err = get_short_logmsg(&logmsg, 42, commit);
10580 printf("%s -> merge conflict: %s\n", id_str, logmsg);
10583 got_object_commit_close(commit);
10588 static const struct got_error *
10589 show_rebase_progress(struct got_commit_object *commit,
10590 struct got_object_id *old_id, struct got_object_id *new_id)
10592 const struct got_error *err;
10593 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
10595 err = got_object_id_str(&old_id_str, old_id);
10600 err = got_object_id_str(&new_id_str, new_id);
10605 old_id_str[12] = '\0';
10607 new_id_str[12] = '\0';
10609 err = get_short_logmsg(&logmsg, 42, commit);
10613 printf("%s -> %s: %s\n", old_id_str,
10614 new_id_str ? new_id_str : "no-op change", logmsg);
10622 static const struct got_error *
10623 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
10624 struct got_reference *branch, struct got_reference *tmp_branch,
10625 struct got_repository *repo, int create_backup)
10627 printf("Switching work tree to %s\n", got_ref_get_name(branch));
10628 return got_worktree_rebase_complete(worktree, fileindex,
10629 tmp_branch, branch, repo, create_backup);
10632 static const struct got_error *
10633 rebase_commit(struct got_pathlist_head *merged_paths,
10634 struct got_worktree *worktree, struct got_fileindex *fileindex,
10635 struct got_reference *tmp_branch, const char *committer,
10636 struct got_object_id *commit_id, int allow_conflict,
10637 struct got_repository *repo)
10639 const struct got_error *error;
10640 struct got_commit_object *commit;
10641 struct got_object_id *new_commit_id;
10643 error = got_object_open_as_commit(&commit, repo, commit_id);
10647 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
10648 worktree, fileindex, tmp_branch, committer, commit, commit_id,
10649 allow_conflict, repo);
10651 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
10653 error = show_rebase_progress(commit, commit_id, NULL);
10655 error = show_rebase_progress(commit, commit_id, new_commit_id);
10656 free(new_commit_id);
10659 got_object_commit_close(commit);
10663 struct check_path_prefix_arg {
10664 const char *path_prefix;
10669 static const struct got_error *
10670 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
10671 struct got_blob_object *blob2, FILE *f1, FILE *f2,
10672 struct got_object_id *id1, struct got_object_id *id2,
10673 const char *path1, const char *path2,
10674 mode_t mode1, mode_t mode2, struct got_repository *repo)
10676 struct check_path_prefix_arg *a = arg;
10678 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
10679 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
10680 return got_error(a->errcode);
10685 static const struct got_error *
10686 check_path_prefix(struct got_object_id *parent_id,
10687 struct got_object_id *commit_id, const char *path_prefix,
10688 int errcode, struct got_repository *repo)
10690 const struct got_error *err;
10691 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
10692 struct got_commit_object *commit = NULL, *parent_commit = NULL;
10693 struct check_path_prefix_arg cpp_arg;
10695 if (got_path_is_root_dir(path_prefix))
10698 err = got_object_open_as_commit(&commit, repo, commit_id);
10702 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
10706 err = got_object_open_as_tree(&tree1, repo,
10707 got_object_commit_get_tree_id(parent_commit));
10711 err = got_object_open_as_tree(&tree2, repo,
10712 got_object_commit_get_tree_id(commit));
10716 cpp_arg.path_prefix = path_prefix;
10717 while (cpp_arg.path_prefix[0] == '/')
10718 cpp_arg.path_prefix++;
10719 cpp_arg.len = strlen(cpp_arg.path_prefix);
10720 cpp_arg.errcode = errcode;
10721 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
10722 check_path_prefix_in_diff, &cpp_arg, 0);
10725 got_object_tree_close(tree1);
10727 got_object_tree_close(tree2);
10729 got_object_commit_close(commit);
10731 got_object_commit_close(parent_commit);
10735 static const struct got_error *
10736 collect_commits(struct got_object_id_queue *commits,
10737 struct got_object_id *initial_commit_id,
10738 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
10739 const char *path_prefix, int path_prefix_errcode,
10740 struct got_repository *repo)
10742 const struct got_error *err = NULL;
10743 struct got_commit_graph *graph = NULL;
10744 struct got_object_id parent_id, commit_id;
10745 struct got_object_qid *qid;
10747 err = got_commit_graph_open(&graph, "/", 1);
10751 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
10752 check_cancelled, NULL);
10756 memcpy(&commit_id, initial_commit_id, sizeof(commit_id));
10757 while (got_object_id_cmp(&commit_id, iter_stop_id) != 0) {
10758 err = got_commit_graph_iter_next(&parent_id, graph, repo,
10759 check_cancelled, NULL);
10761 if (err->code == GOT_ERR_ITER_COMPLETED) {
10762 err = got_error_msg(GOT_ERR_ANCESTRY,
10763 "ran out of commits to rebase before "
10764 "youngest common ancestor commit has "
10765 "been reached?!?");
10769 err = check_path_prefix(&parent_id, &commit_id,
10770 path_prefix, path_prefix_errcode, repo);
10774 err = got_object_qid_alloc(&qid, &commit_id);
10777 STAILQ_INSERT_HEAD(commits, qid, entry);
10779 memcpy(&commit_id, &parent_id, sizeof(commit_id));
10783 got_commit_graph_close(graph);
10787 static const struct got_error *
10788 get_commit_brief_str(char **brief_str, struct got_commit_object *commit)
10790 const struct got_error *err = NULL;
10791 time_t committer_time;
10793 char datebuf[11]; /* YYYY-MM-DD + NUL */
10794 char *author0 = NULL, *author, *smallerthan;
10795 char *logmsg0 = NULL, *logmsg, *newline;
10797 committer_time = got_object_commit_get_committer_time(commit);
10798 if (gmtime_r(&committer_time, &tm) == NULL)
10799 return got_error_from_errno("gmtime_r");
10800 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d", &tm) == 0)
10801 return got_error(GOT_ERR_NO_SPACE);
10803 author0 = strdup(got_object_commit_get_author(commit));
10804 if (author0 == NULL)
10805 return got_error_from_errno("strdup");
10807 smallerthan = strchr(author, '<');
10808 if (smallerthan && smallerthan[1] != '\0')
10809 author = smallerthan + 1;
10810 author[strcspn(author, "@>")] = '\0';
10812 err = got_object_commit_get_logmsg(&logmsg0, commit);
10816 while (*logmsg == '\n')
10818 newline = strchr(logmsg, '\n');
10822 if (asprintf(brief_str, "%s %s %s",
10823 datebuf, author, logmsg) == -1)
10824 err = got_error_from_errno("asprintf");
10831 static const struct got_error *
10832 delete_backup_ref(struct got_reference *ref, struct got_object_id *id,
10833 struct got_repository *repo)
10835 const struct got_error *err;
10838 err = got_object_id_str(&id_str, id);
10842 err = got_ref_delete(ref, repo);
10846 printf("Deleted %s: %s\n", got_ref_get_name(ref), id_str);
10852 static const struct got_error *
10853 print_backup_ref(const char *branch_name, const char *new_id_str,
10854 struct got_object_id *old_commit_id, struct got_commit_object *old_commit,
10855 struct got_reflist_object_id_map *refs_idmap,
10856 struct got_repository *repo)
10858 const struct got_error *err = NULL;
10859 struct got_reflist_head *refs;
10860 char *refs_str = NULL;
10861 struct got_object_id *new_commit_id = NULL;
10862 struct got_commit_object *new_commit = NULL;
10863 char *new_commit_brief_str = NULL;
10864 struct got_object_id *yca_id = NULL;
10865 struct got_commit_object *yca_commit = NULL;
10866 char *yca_id_str = NULL, *yca_brief_str = NULL;
10867 char *custom_refs_str;
10869 if (asprintf(&custom_refs_str, "formerly %s", branch_name) == -1)
10870 return got_error_from_errno("asprintf");
10872 err = print_commit(old_commit, old_commit_id, repo, NULL, NULL, NULL,
10873 0, 0, refs_idmap, custom_refs_str, NULL);
10877 err = got_object_resolve_id_str(&new_commit_id, repo, new_id_str);
10881 refs = got_reflist_object_id_map_lookup(refs_idmap, new_commit_id);
10883 err = build_refs_str(&refs_str, refs, new_commit_id, repo, 0);
10888 err = got_object_open_as_commit(&new_commit, repo, new_commit_id);
10892 err = get_commit_brief_str(&new_commit_brief_str, new_commit);
10896 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
10897 old_commit_id, new_commit_id, 1, repo, check_cancelled, NULL);
10901 printf("has become commit %s%s%s%s\n %s\n", new_id_str,
10902 refs_str ? " (" : "", refs_str ? refs_str : "",
10903 refs_str ? ")" : "", new_commit_brief_str);
10904 if (yca_id && got_object_id_cmp(yca_id, new_commit_id) != 0 &&
10905 got_object_id_cmp(yca_id, old_commit_id) != 0) {
10909 err = got_object_open_as_commit(&yca_commit, repo, yca_id);
10913 err = get_commit_brief_str(&yca_brief_str, yca_commit);
10917 err = got_object_id_str(&yca_id_str, yca_id);
10921 refs = got_reflist_object_id_map_lookup(refs_idmap, yca_id);
10923 err = build_refs_str(&refs_str, refs, yca_id, repo, 0);
10927 printf("history forked at %s%s%s%s\n %s\n",
10929 refs_str ? " (" : "", refs_str ? refs_str : "",
10930 refs_str ? ")" : "", yca_brief_str);
10933 free(custom_refs_str);
10934 free(new_commit_id);
10938 free(yca_brief_str);
10940 got_object_commit_close(new_commit);
10942 got_object_commit_close(yca_commit);
10947 static const struct got_error *
10948 worktree_has_logmsg_ref(const char *caller, struct got_worktree *worktree,
10949 struct got_repository *repo)
10951 const struct got_error *err;
10952 struct got_reflist_head refs;
10953 struct got_reflist_entry *re;
10954 char *uuidstr = NULL;
10955 static char msg[160];
10959 err = got_worktree_get_uuid(&uuidstr, worktree);
10963 err = got_ref_list(&refs, repo, "refs/got/worktree",
10964 got_ref_cmp_by_name, repo);
10968 TAILQ_FOREACH(re, &refs, entry) {
10969 const char *cmd, *refname, *type;
10971 refname = got_ref_get_name(re->ref);
10973 if (strncmp(refname, GOT_WORKTREE_CHERRYPICK_REF_PREFIX,
10974 GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN) == 0) {
10975 refname += GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN + 1;
10976 cmd = "cherrypick";
10977 type = "cherrypicked";
10978 } else if (strncmp(refname, GOT_WORKTREE_BACKOUT_REF_PREFIX,
10979 GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN) == 0) {
10980 refname += GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN + 1;
10982 type = "backed-out";
10986 if (strncmp(refname, uuidstr, GOT_WORKTREE_UUID_STRLEN) != 0)
10989 snprintf(msg, sizeof(msg),
10990 "work tree has references created by %s commits which "
10991 "must be removed with 'got %s -X' before running the %s "
10992 "command", type, cmd, caller);
10993 err = got_error_msg(GOT_ERR_WORKTREE_META, msg);
10999 got_ref_list_free(&refs);
11003 static const struct got_error *
11004 process_backup_refs(const char *backup_ref_prefix,
11005 const char *wanted_branch_name,
11006 int delete, struct got_repository *repo)
11008 const struct got_error *err;
11009 struct got_reflist_head refs, backup_refs;
11010 struct got_reflist_entry *re;
11011 const size_t backup_ref_prefix_len = strlen(backup_ref_prefix);
11012 struct got_object_id *old_commit_id = NULL;
11013 char *branch_name = NULL;
11014 struct got_commit_object *old_commit = NULL;
11015 struct got_reflist_object_id_map *refs_idmap = NULL;
11016 int wanted_branch_found = 0;
11019 TAILQ_INIT(&backup_refs);
11021 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
11025 err = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
11029 if (wanted_branch_name) {
11030 if (strncmp(wanted_branch_name, "refs/heads/", 11) == 0)
11031 wanted_branch_name += 11;
11034 err = got_ref_list(&backup_refs, repo, backup_ref_prefix,
11035 got_ref_cmp_by_commit_timestamp_descending, repo);
11039 TAILQ_FOREACH(re, &backup_refs, entry) {
11040 const char *refname = got_ref_get_name(re->ref);
11043 err = check_cancelled(NULL);
11047 err = got_ref_resolve(&old_commit_id, repo, re->ref);
11051 err = got_object_open_as_commit(&old_commit, repo,
11056 if (strncmp(backup_ref_prefix, refname,
11057 backup_ref_prefix_len) == 0)
11058 refname += backup_ref_prefix_len;
11060 while (refname[0] == '/')
11063 branch_name = strdup(refname);
11064 if (branch_name == NULL) {
11065 err = got_error_from_errno("strdup");
11068 slash = strrchr(branch_name, '/');
11071 refname += strlen(branch_name) + 1;
11074 if (wanted_branch_name == NULL ||
11075 strcmp(wanted_branch_name, branch_name) == 0) {
11076 wanted_branch_found = 1;
11078 err = delete_backup_ref(re->ref,
11079 old_commit_id, repo);
11081 err = print_backup_ref(branch_name, refname,
11082 old_commit_id, old_commit, refs_idmap,
11089 free(old_commit_id);
11090 old_commit_id = NULL;
11092 branch_name = NULL;
11093 got_object_commit_close(old_commit);
11097 if (wanted_branch_name && !wanted_branch_found) {
11098 err = got_error_fmt(GOT_ERR_NOT_REF,
11099 "%s/%s/", backup_ref_prefix, wanted_branch_name);
11103 got_reflist_object_id_map_free(refs_idmap);
11104 got_ref_list_free(&refs);
11105 got_ref_list_free(&backup_refs);
11106 free(old_commit_id);
11109 got_object_commit_close(old_commit);
11113 static const struct got_error *
11114 abort_progress(void *arg, unsigned char status, const char *path)
11117 * Unversioned files should not clutter progress output when
11118 * an operation is aborted.
11120 if (status == GOT_STATUS_UNVERSIONED)
11123 return update_progress(arg, status, path);
11126 static const struct got_error *
11127 cmd_rebase(int argc, char *argv[])
11129 const struct got_error *error = NULL;
11130 struct got_worktree *worktree = NULL;
11131 struct got_repository *repo = NULL;
11132 struct got_fileindex *fileindex = NULL;
11133 char *cwd = NULL, *committer = NULL, *gitconfig_path = NULL;
11134 struct got_reference *branch = NULL;
11135 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
11136 struct got_object_id *commit_id = NULL, *parent_id = NULL;
11137 struct got_object_id *resume_commit_id = NULL;
11138 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
11139 struct got_object_id *head_commit_id = NULL;
11140 struct got_reference *head_ref = NULL;
11141 struct got_commit_object *commit = NULL;
11142 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
11143 int histedit_in_progress = 0, merge_in_progress = 0;
11144 int create_backup = 1, list_backups = 0, delete_backups = 0;
11145 int allow_conflict = 0;
11146 struct got_object_id_queue commits;
11147 struct got_pathlist_head merged_paths;
11148 const struct got_object_id_queue *parent_ids;
11149 struct got_object_qid *qid, *pid;
11150 struct got_update_progress_arg upa;
11151 int *pack_fds = NULL;
11153 STAILQ_INIT(&commits);
11154 TAILQ_INIT(&merged_paths);
11155 memset(&upa, 0, sizeof(upa));
11158 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
11159 "unveil", NULL) == -1)
11163 while ((ch = getopt(argc, argv, "aCclX")) != -1) {
11169 allow_conflict = 1;
11172 continue_rebase = 1;
11178 delete_backups = 1;
11189 if (list_backups) {
11191 option_conflict('l', 'a');
11192 if (allow_conflict)
11193 option_conflict('l', 'C');
11194 if (continue_rebase)
11195 option_conflict('l', 'c');
11196 if (delete_backups)
11197 option_conflict('l', 'X');
11198 if (argc != 0 && argc != 1)
11200 } else if (delete_backups) {
11202 option_conflict('X', 'a');
11203 if (allow_conflict)
11204 option_conflict('X', 'C');
11205 if (continue_rebase)
11206 option_conflict('X', 'c');
11208 option_conflict('l', 'X');
11209 if (argc != 0 && argc != 1)
11211 } else if (allow_conflict) {
11213 option_conflict('C', 'a');
11214 if (!continue_rebase)
11215 errx(1, "-C option requires -c");
11217 if (abort_rebase && continue_rebase)
11219 else if (abort_rebase || continue_rebase) {
11222 } else if (argc != 1)
11226 cwd = getcwd(NULL, 0);
11228 error = got_error_from_errno("getcwd");
11232 error = got_repo_pack_fds_open(&pack_fds);
11236 error = got_worktree_open(&worktree, cwd);
11238 if (list_backups || delete_backups) {
11239 if (error->code != GOT_ERR_NOT_WORKTREE)
11242 if (error->code == GOT_ERR_NOT_WORKTREE)
11243 error = wrap_not_worktree_error(error,
11249 error = get_gitconfig_path(&gitconfig_path);
11252 error = got_repo_open(&repo,
11253 worktree ? got_worktree_get_repo_path(worktree) : cwd,
11254 gitconfig_path, pack_fds);
11258 if (worktree != NULL && !list_backups && !delete_backups) {
11259 error = worktree_has_logmsg_ref("rebase", worktree, repo);
11264 error = get_author(&committer, repo, worktree);
11265 if (error && error->code != GOT_ERR_COMMIT_NO_AUTHOR)
11268 error = apply_unveil(got_repo_get_path(repo), 0,
11269 worktree ? got_worktree_get_root_path(worktree) : NULL);
11273 if (list_backups || delete_backups) {
11274 error = process_backup_refs(
11275 GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
11276 argc == 1 ? argv[0] : NULL, delete_backups, repo);
11277 goto done; /* nothing else to do */
11280 error = got_worktree_histedit_in_progress(&histedit_in_progress,
11284 if (histedit_in_progress) {
11285 error = got_error(GOT_ERR_HISTEDIT_BUSY);
11289 error = got_worktree_merge_in_progress(&merge_in_progress,
11293 if (merge_in_progress) {
11294 error = got_error(GOT_ERR_MERGE_BUSY);
11298 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
11302 if (abort_rebase) {
11303 if (!rebase_in_progress) {
11304 error = got_error(GOT_ERR_NOT_REBASING);
11307 error = got_worktree_rebase_continue(&resume_commit_id,
11308 &new_base_branch, &tmp_branch, &branch, &fileindex,
11312 printf("Switching work tree to %s\n",
11313 got_ref_get_symref_target(new_base_branch));
11314 error = got_worktree_rebase_abort(worktree, fileindex, repo,
11315 new_base_branch, abort_progress, &upa);
11318 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
11319 print_merge_progress_stats(&upa);
11320 goto done; /* nothing else to do */
11323 if (continue_rebase) {
11324 if (!rebase_in_progress) {
11325 error = got_error(GOT_ERR_NOT_REBASING);
11328 error = got_worktree_rebase_continue(&resume_commit_id,
11329 &new_base_branch, &tmp_branch, &branch, &fileindex,
11334 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
11335 committer, resume_commit_id, allow_conflict, repo);
11339 yca_id = got_object_id_dup(resume_commit_id);
11340 if (yca_id == NULL) {
11341 error = got_error_from_errno("got_object_id_dup");
11345 error = got_ref_open(&branch, repo, argv[0], 0);
11348 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
11349 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
11350 "will not rebase a branch which lives outside "
11351 "the \"refs/heads/\" reference namespace");
11356 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
11360 if (!continue_rebase) {
11361 struct got_object_id *base_commit_id;
11363 error = got_ref_open(&head_ref, repo,
11364 got_worktree_get_head_ref_name(worktree), 0);
11367 error = got_ref_resolve(&head_commit_id, repo, head_ref);
11370 base_commit_id = got_worktree_get_base_commit_id(worktree);
11371 if (got_object_id_cmp(base_commit_id, head_commit_id) != 0) {
11372 error = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
11376 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
11377 base_commit_id, branch_head_commit_id, 1, repo,
11378 check_cancelled, NULL);
11380 if (error->code == GOT_ERR_ANCESTRY) {
11381 error = got_error_msg(GOT_ERR_ANCESTRY,
11382 "specified branch shares no common "
11383 "ancestry with work tree's branch");
11388 if (got_object_id_cmp(base_commit_id, yca_id) == 0) {
11389 struct got_pathlist_head paths;
11390 printf("%s is already based on %s\n",
11391 got_ref_get_name(branch),
11392 got_worktree_get_head_ref_name(worktree));
11393 error = switch_head_ref(branch, branch_head_commit_id,
11397 error = got_worktree_set_base_commit_id(worktree, repo,
11398 branch_head_commit_id);
11401 TAILQ_INIT(&paths);
11402 error = got_pathlist_append(&paths, "", NULL);
11405 error = got_worktree_checkout_files(worktree,
11406 &paths, repo, update_progress, &upa,
11407 check_cancelled, NULL);
11408 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
11411 if (upa.did_something) {
11413 error = got_object_id_str(&id_str,
11414 branch_head_commit_id);
11417 printf("Updated to %s: %s\n",
11418 got_worktree_get_head_ref_name(worktree),
11422 printf("Already up-to-date\n");
11423 print_update_progress_stats(&upa);
11428 commit_id = branch_head_commit_id;
11429 error = got_object_open_as_commit(&commit, repo, commit_id);
11433 parent_ids = got_object_commit_get_parent_ids(commit);
11434 pid = STAILQ_FIRST(parent_ids);
11436 error = collect_commits(&commits, commit_id, &pid->id,
11437 yca_id, got_worktree_get_path_prefix(worktree),
11438 GOT_ERR_REBASE_PATH, repo);
11443 got_object_commit_close(commit);
11446 if (!continue_rebase) {
11447 error = got_worktree_rebase_prepare(&new_base_branch,
11448 &tmp_branch, &fileindex, worktree, branch, repo);
11453 if (STAILQ_EMPTY(&commits)) {
11454 if (continue_rebase) {
11455 error = rebase_complete(worktree, fileindex,
11456 branch, tmp_branch, repo, create_backup);
11459 /* Fast-forward the reference of the branch. */
11460 struct got_object_id *new_head_commit_id;
11462 error = got_ref_resolve(&new_head_commit_id, repo,
11466 error = got_object_id_str(&id_str, new_head_commit_id);
11469 printf("Forwarding %s to commit %s\n",
11470 got_ref_get_name(branch), id_str);
11472 error = got_ref_change_ref(branch,
11473 new_head_commit_id);
11476 /* No backup needed since objects did not change. */
11482 STAILQ_FOREACH(qid, &commits, entry) {
11484 commit_id = &qid->id;
11485 parent_id = pid ? &pid->id : yca_id;
11488 memset(&upa, 0, sizeof(upa));
11489 error = got_worktree_rebase_merge_files(&merged_paths,
11490 worktree, fileindex, parent_id, commit_id, repo,
11491 update_progress, &upa, check_cancelled, NULL);
11495 print_merge_progress_stats(&upa);
11496 if (upa.conflicts > 0 || upa.missing > 0 ||
11497 upa.not_deleted > 0 || upa.unversioned > 0) {
11498 if (upa.conflicts > 0) {
11499 error = show_rebase_merge_conflict(&qid->id,
11504 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
11508 error = rebase_commit(&merged_paths, worktree, fileindex,
11509 tmp_branch, committer, commit_id, 0, repo);
11510 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
11515 if (upa.conflicts > 0 || upa.missing > 0 ||
11516 upa.not_deleted > 0 || upa.unversioned > 0) {
11517 error = got_worktree_rebase_postpone(worktree, fileindex);
11520 if (upa.conflicts > 0 && upa.missing == 0 &&
11521 upa.not_deleted == 0 && upa.unversioned == 0) {
11522 error = got_error_msg(GOT_ERR_CONFLICTS,
11523 "conflicts must be resolved before rebasing "
11525 } else if (upa.conflicts > 0) {
11526 error = got_error_msg(GOT_ERR_CONFLICTS,
11527 "conflicts must be resolved before rebasing "
11528 "can continue; changes destined for some "
11529 "files were not yet merged and should be "
11530 "merged manually if required before the "
11531 "rebase operation is continued");
11533 error = got_error_msg(GOT_ERR_CONFLICTS,
11534 "changes destined for some files were not "
11535 "yet merged and should be merged manually "
11536 "if required before the rebase operation "
11540 error = rebase_complete(worktree, fileindex, branch,
11541 tmp_branch, repo, create_backup);
11545 free(gitconfig_path);
11546 got_object_id_queue_free(&commits);
11547 free(branch_head_commit_id);
11548 free(resume_commit_id);
11549 free(head_commit_id);
11552 got_object_commit_close(commit);
11554 got_ref_close(branch);
11555 if (new_base_branch)
11556 got_ref_close(new_base_branch);
11558 got_ref_close(tmp_branch);
11560 got_ref_close(head_ref);
11562 got_worktree_close(worktree);
11564 const struct got_error *close_err = got_repo_close(repo);
11569 const struct got_error *pack_err =
11570 got_repo_pack_fds_close(pack_fds);
11578 usage_histedit(void)
11580 fprintf(stderr, "usage: %s histedit [-aCcdeflmX] [-F histedit-script] "
11581 "[branch]\n", getprogname());
11585 #define GOT_HISTEDIT_PICK 'p'
11586 #define GOT_HISTEDIT_EDIT 'e'
11587 #define GOT_HISTEDIT_FOLD 'f'
11588 #define GOT_HISTEDIT_DROP 'd'
11589 #define GOT_HISTEDIT_MESG 'm'
11591 static const struct got_histedit_cmd {
11592 unsigned char code;
11595 } got_histedit_cmds[] = {
11596 { GOT_HISTEDIT_PICK, "pick", "use commit" },
11597 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
11598 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
11600 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
11601 { GOT_HISTEDIT_MESG, "mesg",
11602 "single-line log message for commit above (open editor if empty)" },
11605 struct got_histedit_list_entry {
11606 TAILQ_ENTRY(got_histedit_list_entry) entry;
11607 struct got_object_id *commit_id;
11608 const struct got_histedit_cmd *cmd;
11611 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
11613 static const struct got_error *
11614 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
11615 FILE *f, struct got_repository *repo)
11617 const struct got_error *err = NULL;
11618 char *logmsg = NULL, *id_str = NULL;
11619 struct got_commit_object *commit = NULL;
11622 err = got_object_open_as_commit(&commit, repo, commit_id);
11626 err = get_short_logmsg(&logmsg, 34, commit);
11630 err = got_object_id_str(&id_str, commit_id);
11634 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
11636 err = got_ferror(f, GOT_ERR_IO);
11639 got_object_commit_close(commit);
11645 static const struct got_error *
11646 histedit_write_commit_list(struct got_object_id_queue *commits,
11647 FILE *f, int edit_logmsg_only, int fold_only, int drop_only,
11648 int edit_only, struct got_repository *repo)
11650 const struct got_error *err = NULL;
11651 struct got_object_qid *qid;
11652 const char *histedit_cmd = NULL;
11654 if (STAILQ_EMPTY(commits))
11655 return got_error(GOT_ERR_EMPTY_HISTEDIT);
11657 STAILQ_FOREACH(qid, commits, entry) {
11658 histedit_cmd = got_histedit_cmds[0].name;
11660 histedit_cmd = "drop";
11661 else if (edit_only)
11662 histedit_cmd = "edit";
11663 else if (fold_only && STAILQ_NEXT(qid, entry) != NULL)
11664 histedit_cmd = "fold";
11665 err = histedit_write_commit(&qid->id, histedit_cmd, f, repo);
11668 if (edit_logmsg_only) {
11669 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
11671 err = got_ferror(f, GOT_ERR_IO);
11680 static const struct got_error *
11681 write_cmd_list(FILE *f, const char *branch_name,
11682 struct got_object_id_queue *commits)
11684 const struct got_error *err = NULL;
11688 struct got_object_qid *qid;
11690 qid = STAILQ_FIRST(commits);
11691 err = got_object_id_str(&id_str, &qid->id);
11696 "# Editing the history of branch '%s' starting at\n"
11698 "# Commits will be processed in order from top to "
11699 "bottom of this file.\n", branch_name, id_str);
11701 err = got_ferror(f, GOT_ERR_IO);
11705 n = fprintf(f, "# Available histedit commands:\n");
11707 err = got_ferror(f, GOT_ERR_IO);
11711 for (i = 0; i < nitems(got_histedit_cmds); i++) {
11712 const struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
11713 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
11716 err = got_ferror(f, GOT_ERR_IO);
11725 static const struct got_error *
11726 histedit_syntax_error(int lineno)
11728 static char msg[42];
11731 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
11733 if (ret < 0 || (size_t)ret >= sizeof(msg))
11734 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
11736 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
11739 static const struct got_error *
11740 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
11741 char *logmsg, struct got_repository *repo)
11743 const struct got_error *err;
11744 struct got_commit_object *folded_commit = NULL;
11745 char *id_str, *folded_logmsg = NULL;
11747 err = got_object_id_str(&id_str, hle->commit_id);
11751 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
11755 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
11758 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
11759 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
11760 folded_logmsg) == -1) {
11761 err = got_error_from_errno("asprintf");
11765 got_object_commit_close(folded_commit);
11767 free(folded_logmsg);
11771 static struct got_histedit_list_entry *
11772 get_folded_commits(struct got_histedit_list_entry *hle)
11774 struct got_histedit_list_entry *prev, *folded = NULL;
11776 prev = TAILQ_PREV(hle, got_histedit_list, entry);
11777 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
11778 prev->cmd->code == GOT_HISTEDIT_DROP)) {
11779 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
11781 prev = TAILQ_PREV(prev, got_histedit_list, entry);
11787 static const struct got_error *
11788 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
11789 struct got_repository *repo)
11791 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
11792 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
11793 const struct got_error *err = NULL;
11794 struct got_commit_object *commit = NULL;
11797 struct got_histedit_list_entry *folded = NULL;
11799 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
11803 folded = get_folded_commits(hle);
11805 while (folded != hle) {
11806 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
11807 folded = TAILQ_NEXT(folded, entry);
11810 err = append_folded_commit_msg(&new_msg, folded,
11816 folded = TAILQ_NEXT(folded, entry);
11820 err = got_object_id_str(&id_str, hle->commit_id);
11823 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
11826 logmsg_len = asprintf(&new_msg,
11827 "%s\n# original log message of commit %s: %s",
11828 logmsg ? logmsg : "", id_str, orig_logmsg);
11829 if (logmsg_len == -1) {
11830 err = got_error_from_errno("asprintf");
11836 err = got_object_id_str(&id_str, hle->commit_id);
11840 err = got_opentemp_named_fd(&logmsg_path, &fd,
11841 GOT_TMPDIR_STR "/got-logmsg", "");
11845 if (write(fd, logmsg, logmsg_len) == -1) {
11846 err = got_error_from_errno2("write", logmsg_path);
11849 if (close(fd) == -1) {
11850 err = got_error_from_errno2("close", logmsg_path);
11855 err = get_editor(&editor);
11859 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg,
11862 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
11865 hle->logmsg = strdup(new_msg);
11866 if (hle->logmsg == NULL)
11867 err = got_error_from_errno("strdup");
11870 if (fd != -1 && close(fd) == -1 && err == NULL)
11871 err = got_error_from_errno2("close", logmsg_path);
11872 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
11873 err = got_error_from_errno2("unlink", logmsg_path);
11879 got_object_commit_close(commit);
11883 static const struct got_error *
11884 histedit_parse_list(struct got_histedit_list *histedit_cmds,
11885 FILE *f, struct got_repository *repo)
11887 const struct got_error *err = NULL;
11888 char *line = NULL, *p, *end;
11889 size_t i, linesize = 0;
11891 int lineno = 0, lastcmd = -1;
11892 const struct got_histedit_cmd *cmd;
11893 struct got_object_id *commit_id = NULL;
11894 struct got_histedit_list_entry *hle = NULL;
11897 linelen = getline(&line, &linesize, f);
11898 if (linelen == -1) {
11899 const struct got_error *getline_err;
11902 getline_err = got_error_from_errno("getline");
11903 err = got_ferror(f, getline_err->code);
11908 while (isspace((unsigned char)p[0]))
11910 if (p[0] == '#' || p[0] == '\0')
11913 for (i = 0; i < nitems(got_histedit_cmds); i++) {
11914 cmd = &got_histedit_cmds[i];
11915 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
11916 isspace((unsigned char)p[strlen(cmd->name)])) {
11917 p += strlen(cmd->name);
11920 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
11925 if (i == nitems(got_histedit_cmds)) {
11926 err = histedit_syntax_error(lineno);
11929 while (isspace((unsigned char)p[0]))
11931 if (cmd->code == GOT_HISTEDIT_MESG) {
11932 if (lastcmd != GOT_HISTEDIT_PICK &&
11933 lastcmd != GOT_HISTEDIT_EDIT) {
11934 err = got_error(GOT_ERR_HISTEDIT_CMD);
11937 if (p[0] == '\0') {
11938 err = histedit_edit_logmsg(hle, repo);
11942 hle->logmsg = strdup(p);
11943 if (hle->logmsg == NULL) {
11944 err = got_error_from_errno("strdup");
11948 lastcmd = cmd->code;
11952 while (end[0] && !isspace((unsigned char)end[0]))
11956 err = got_object_resolve_id_str(&commit_id, repo, p);
11958 /* override error code */
11959 err = histedit_syntax_error(lineno);
11963 hle = malloc(sizeof(*hle));
11965 err = got_error_from_errno("malloc");
11969 hle->commit_id = commit_id;
11970 hle->logmsg = NULL;
11972 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
11973 lastcmd = cmd->code;
11981 static const struct got_error *
11982 histedit_check_script(struct got_histedit_list *histedit_cmds,
11983 struct got_object_id_queue *commits, struct got_repository *repo)
11985 const struct got_error *err = NULL;
11986 struct got_object_qid *qid;
11987 struct got_histedit_list_entry *hle;
11988 static char msg[92];
11991 if (TAILQ_EMPTY(histedit_cmds))
11992 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
11993 "histedit script contains no commands");
11994 if (STAILQ_EMPTY(commits))
11995 return got_error(GOT_ERR_EMPTY_HISTEDIT);
11997 TAILQ_FOREACH(hle, histedit_cmds, entry) {
11998 struct got_histedit_list_entry *hle2;
11999 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
12002 if (got_object_id_cmp(hle->commit_id,
12003 hle2->commit_id) != 0)
12005 err = got_object_id_str(&id_str, hle->commit_id);
12008 snprintf(msg, sizeof(msg), "commit %s is listed "
12009 "more than once in histedit script", id_str);
12011 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
12015 STAILQ_FOREACH(qid, commits, entry) {
12016 TAILQ_FOREACH(hle, histedit_cmds, entry) {
12017 if (got_object_id_cmp(&qid->id, hle->commit_id) == 0)
12021 err = got_object_id_str(&id_str, &qid->id);
12024 snprintf(msg, sizeof(msg),
12025 "commit %s missing from histedit script", id_str);
12027 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
12031 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
12032 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
12033 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
12034 "last commit in histedit script cannot be folded");
12039 static const struct got_error *
12040 histedit_run_editor(struct got_histedit_list *histedit_cmds,
12041 const char *path, struct got_object_id_queue *commits,
12042 struct got_repository *repo)
12044 const struct got_error *err = NULL;
12045 struct stat st, st2;
12046 struct timespec timeout;
12050 err = get_editor(&editor);
12054 if (stat(path, &st) == -1) {
12055 err = got_error_from_errno2("stat", path);
12059 if (spawn_editor(editor, path) == -1) {
12060 err = got_error_from_errno("failed spawning editor");
12064 timeout.tv_sec = 0;
12065 timeout.tv_nsec = 1;
12066 nanosleep(&timeout, NULL);
12068 if (stat(path, &st2) == -1) {
12069 err = got_error_from_errno2("stat", path);
12073 if (st.st_size == st2.st_size &&
12074 timespeccmp(&st.st_mtim, &st2.st_mtim, ==)) {
12075 err = got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
12076 "no changes made to histedit script, aborting");
12080 f = fopen(path, "re");
12082 err = got_error_from_errno("fopen");
12085 err = histedit_parse_list(histedit_cmds, f, repo);
12089 err = histedit_check_script(histedit_cmds, commits, repo);
12091 if (f && fclose(f) == EOF && err == NULL)
12092 err = got_error_from_errno("fclose");
12097 static const struct got_error *
12098 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
12099 struct got_object_id_queue *, const char *, const char *,
12100 struct got_repository *);
12102 static const struct got_error *
12103 histedit_edit_script(struct got_histedit_list *histedit_cmds,
12104 struct got_object_id_queue *commits, const char *branch_name,
12105 int edit_logmsg_only, int fold_only, int drop_only, int edit_only,
12106 struct got_repository *repo)
12108 const struct got_error *err;
12112 err = got_opentemp_named(&path, &f, "got-histedit", "");
12116 err = write_cmd_list(f, branch_name, commits);
12120 err = histedit_write_commit_list(commits, f, edit_logmsg_only,
12121 fold_only, drop_only, edit_only, repo);
12125 if (drop_only || edit_logmsg_only || fold_only || edit_only) {
12127 err = histedit_parse_list(histedit_cmds, f, repo);
12129 if (fclose(f) == EOF) {
12130 err = got_error_from_errno("fclose");
12134 err = histedit_run_editor(histedit_cmds, path, commits, repo);
12136 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
12137 err->code != GOT_ERR_HISTEDIT_CMD)
12139 err = histedit_edit_list_retry(histedit_cmds, err,
12140 commits, path, branch_name, repo);
12144 if (f && fclose(f) == EOF && err == NULL)
12145 err = got_error_from_errno("fclose");
12146 if (path && unlink(path) != 0 && err == NULL)
12147 err = got_error_from_errno2("unlink", path);
12152 static const struct got_error *
12153 histedit_save_list(struct got_histedit_list *histedit_cmds,
12154 struct got_worktree *worktree, struct got_repository *repo)
12156 const struct got_error *err = NULL;
12159 struct got_histedit_list_entry *hle;
12160 struct got_commit_object *commit = NULL;
12162 err = got_worktree_get_histedit_script_path(&path, worktree);
12166 f = fopen(path, "we");
12168 err = got_error_from_errno2("fopen", path);
12171 TAILQ_FOREACH(hle, histedit_cmds, entry) {
12172 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
12178 int n = fprintf(f, "%c %s\n",
12179 GOT_HISTEDIT_MESG, hle->logmsg);
12181 err = got_ferror(f, GOT_ERR_IO);
12187 if (f && fclose(f) == EOF && err == NULL)
12188 err = got_error_from_errno("fclose");
12191 got_object_commit_close(commit);
12196 histedit_free_list(struct got_histedit_list *histedit_cmds)
12198 struct got_histedit_list_entry *hle;
12200 while ((hle = TAILQ_FIRST(histedit_cmds))) {
12201 TAILQ_REMOVE(histedit_cmds, hle, entry);
12206 static const struct got_error *
12207 histedit_load_list(struct got_histedit_list *histedit_cmds,
12208 const char *path, struct got_repository *repo)
12210 const struct got_error *err = NULL;
12213 f = fopen(path, "re");
12215 err = got_error_from_errno2("fopen", path);
12219 err = histedit_parse_list(histedit_cmds, f, repo);
12221 if (f && fclose(f) == EOF && err == NULL)
12222 err = got_error_from_errno("fclose");
12226 static const struct got_error *
12227 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
12228 const struct got_error *edit_err, struct got_object_id_queue *commits,
12229 const char *path, const char *branch_name, struct got_repository *repo)
12231 const struct got_error *err = NULL, *prev_err = edit_err;
12234 while (resp != 'c' && resp != 'r' && resp != 'a') {
12235 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
12236 "or (a)bort: ", getprogname(), prev_err->msg);
12241 histedit_free_list(histedit_cmds);
12242 err = histedit_run_editor(histedit_cmds, path, commits,
12245 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
12246 err->code != GOT_ERR_HISTEDIT_CMD)
12253 } else if (resp == 'r') {
12254 histedit_free_list(histedit_cmds);
12255 err = histedit_edit_script(histedit_cmds,
12256 commits, branch_name, 0, 0, 0, 0, repo);
12258 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
12259 err->code != GOT_ERR_HISTEDIT_CMD)
12266 } else if (resp == 'a') {
12267 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
12270 printf("invalid response '%c'\n", resp);
12276 static const struct got_error *
12277 histedit_complete(struct got_worktree *worktree,
12278 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
12279 struct got_reference *branch, struct got_repository *repo)
12281 printf("Switching work tree to %s\n",
12282 got_ref_get_symref_target(branch));
12283 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
12287 static const struct got_error *
12288 show_histedit_progress(struct got_commit_object *commit,
12289 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
12291 const struct got_error *err;
12292 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
12294 err = got_object_id_str(&old_id_str, hle->commit_id);
12299 err = got_object_id_str(&new_id_str, new_id);
12304 old_id_str[12] = '\0';
12306 new_id_str[12] = '\0';
12309 logmsg = strdup(hle->logmsg);
12310 if (logmsg == NULL) {
12311 err = got_error_from_errno("strdup");
12314 trim_logmsg(logmsg, 42);
12316 err = get_short_logmsg(&logmsg, 42, commit);
12321 switch (hle->cmd->code) {
12322 case GOT_HISTEDIT_PICK:
12323 case GOT_HISTEDIT_EDIT:
12324 printf("%s -> %s: %s\n", old_id_str,
12325 new_id_str ? new_id_str : "no-op change", logmsg);
12327 case GOT_HISTEDIT_DROP:
12328 case GOT_HISTEDIT_FOLD:
12329 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
12341 static const struct got_error *
12342 histedit_commit(struct got_pathlist_head *merged_paths,
12343 struct got_worktree *worktree, struct got_fileindex *fileindex,
12344 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
12345 const char *committer, int allow_conflict, struct got_repository *repo)
12347 const struct got_error *err;
12348 struct got_commit_object *commit;
12349 struct got_object_id *new_commit_id;
12351 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
12352 && hle->logmsg == NULL) {
12353 err = histedit_edit_logmsg(hle, repo);
12358 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
12362 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
12363 worktree, fileindex, tmp_branch, committer, commit, hle->commit_id,
12364 hle->logmsg, allow_conflict, repo);
12366 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
12368 err = show_histedit_progress(commit, hle, NULL);
12370 err = show_histedit_progress(commit, hle, new_commit_id);
12371 free(new_commit_id);
12374 got_object_commit_close(commit);
12378 static const struct got_error *
12379 histedit_skip_commit(struct got_histedit_list_entry *hle,
12380 struct got_worktree *worktree, struct got_repository *repo)
12382 const struct got_error *error;
12383 struct got_commit_object *commit;
12385 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
12390 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
12394 error = show_histedit_progress(commit, hle, NULL);
12395 got_object_commit_close(commit);
12399 static const struct got_error *
12400 check_local_changes(void *arg, unsigned char status,
12401 unsigned char staged_status, const char *path,
12402 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
12403 struct got_object_id *commit_id, int dirfd, const char *de_name)
12405 int *have_local_changes = arg;
12408 case GOT_STATUS_ADD:
12409 case GOT_STATUS_DELETE:
12410 case GOT_STATUS_MODIFY:
12411 case GOT_STATUS_CONFLICT:
12412 *have_local_changes = 1;
12413 return got_error(GOT_ERR_CANCELLED);
12418 switch (staged_status) {
12419 case GOT_STATUS_ADD:
12420 case GOT_STATUS_DELETE:
12421 case GOT_STATUS_MODIFY:
12422 *have_local_changes = 1;
12423 return got_error(GOT_ERR_CANCELLED);
12431 static const struct got_error *
12432 cmd_histedit(int argc, char *argv[])
12434 const struct got_error *error = NULL;
12435 struct got_worktree *worktree = NULL;
12436 struct got_fileindex *fileindex = NULL;
12437 struct got_repository *repo = NULL;
12438 char *cwd = NULL, *committer = NULL, *gitconfig_path = NULL;
12439 struct got_reference *branch = NULL;
12440 struct got_reference *tmp_branch = NULL;
12441 struct got_object_id *resume_commit_id = NULL;
12442 struct got_object_id *base_commit_id = NULL;
12443 struct got_object_id *head_commit_id = NULL;
12444 struct got_commit_object *commit = NULL;
12445 int ch, rebase_in_progress = 0, merge_in_progress = 0;
12446 struct got_update_progress_arg upa;
12447 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
12448 int drop_only = 0, edit_logmsg_only = 0, fold_only = 0, edit_only = 0;
12449 int allow_conflict = 0, list_backups = 0, delete_backups = 0;
12450 const char *edit_script_path = NULL;
12451 struct got_object_id_queue commits;
12452 struct got_pathlist_head merged_paths;
12453 const struct got_object_id_queue *parent_ids;
12454 struct got_object_qid *pid;
12455 struct got_histedit_list histedit_cmds;
12456 struct got_histedit_list_entry *hle;
12457 int *pack_fds = NULL;
12459 STAILQ_INIT(&commits);
12460 TAILQ_INIT(&histedit_cmds);
12461 TAILQ_INIT(&merged_paths);
12462 memset(&upa, 0, sizeof(upa));
12465 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
12466 "unveil", NULL) == -1)
12470 while ((ch = getopt(argc, argv, "aCcdeF:flmX")) != -1) {
12476 allow_conflict = 1;
12488 edit_script_path = optarg;
12497 edit_logmsg_only = 1;
12500 delete_backups = 1;
12511 if (abort_edit && allow_conflict)
12512 option_conflict('a', 'C');
12513 if (abort_edit && continue_edit)
12514 option_conflict('a', 'c');
12515 if (edit_script_path && allow_conflict)
12516 option_conflict('F', 'C');
12517 if (edit_script_path && edit_logmsg_only)
12518 option_conflict('F', 'm');
12519 if (abort_edit && edit_logmsg_only)
12520 option_conflict('a', 'm');
12521 if (edit_logmsg_only && allow_conflict)
12522 option_conflict('m', 'C');
12523 if (continue_edit && edit_logmsg_only)
12524 option_conflict('c', 'm');
12525 if (abort_edit && fold_only)
12526 option_conflict('a', 'f');
12527 if (fold_only && allow_conflict)
12528 option_conflict('f', 'C');
12529 if (continue_edit && fold_only)
12530 option_conflict('c', 'f');
12531 if (fold_only && edit_logmsg_only)
12532 option_conflict('f', 'm');
12533 if (edit_script_path && fold_only)
12534 option_conflict('F', 'f');
12535 if (abort_edit && edit_only)
12536 option_conflict('a', 'e');
12537 if (continue_edit && edit_only)
12538 option_conflict('c', 'e');
12539 if (edit_only && edit_logmsg_only)
12540 option_conflict('e', 'm');
12541 if (edit_script_path && edit_only)
12542 option_conflict('F', 'e');
12543 if (fold_only && edit_only)
12544 option_conflict('f', 'e');
12545 if (drop_only && abort_edit)
12546 option_conflict('d', 'a');
12547 if (drop_only && allow_conflict)
12548 option_conflict('d', 'C');
12549 if (drop_only && continue_edit)
12550 option_conflict('d', 'c');
12551 if (drop_only && edit_logmsg_only)
12552 option_conflict('d', 'm');
12553 if (drop_only && edit_only)
12554 option_conflict('d', 'e');
12555 if (drop_only && edit_script_path)
12556 option_conflict('d', 'F');
12557 if (drop_only && fold_only)
12558 option_conflict('d', 'f');
12559 if (list_backups) {
12561 option_conflict('l', 'a');
12562 if (allow_conflict)
12563 option_conflict('l', 'C');
12565 option_conflict('l', 'c');
12566 if (edit_script_path)
12567 option_conflict('l', 'F');
12568 if (edit_logmsg_only)
12569 option_conflict('l', 'm');
12571 option_conflict('l', 'd');
12573 option_conflict('l', 'f');
12575 option_conflict('l', 'e');
12576 if (delete_backups)
12577 option_conflict('l', 'X');
12578 if (argc != 0 && argc != 1)
12580 } else if (delete_backups) {
12582 option_conflict('X', 'a');
12583 if (allow_conflict)
12584 option_conflict('X', 'C');
12586 option_conflict('X', 'c');
12588 option_conflict('X', 'd');
12589 if (edit_script_path)
12590 option_conflict('X', 'F');
12591 if (edit_logmsg_only)
12592 option_conflict('X', 'm');
12594 option_conflict('X', 'f');
12596 option_conflict('X', 'e');
12598 option_conflict('X', 'l');
12599 if (argc != 0 && argc != 1)
12601 } else if (allow_conflict && !continue_edit)
12602 errx(1, "-C option requires -c");
12603 else if (argc != 0)
12607 * This command cannot apply unveil(2) in all cases because the
12608 * user may choose to run an editor to edit the histedit script
12609 * and to edit individual commit log messages.
12610 * unveil(2) traverses exec(2); if an editor is used we have to
12611 * apply unveil after edit script and log messages have been written.
12612 * XXX TODO: Make use of unveil(2) where possible.
12615 cwd = getcwd(NULL, 0);
12617 error = got_error_from_errno("getcwd");
12621 error = got_repo_pack_fds_open(&pack_fds);
12625 error = got_worktree_open(&worktree, cwd);
12627 if (list_backups || delete_backups) {
12628 if (error->code != GOT_ERR_NOT_WORKTREE)
12631 if (error->code == GOT_ERR_NOT_WORKTREE)
12632 error = wrap_not_worktree_error(error,
12638 if (list_backups || delete_backups) {
12639 error = got_repo_open(&repo,
12640 worktree ? got_worktree_get_repo_path(worktree) : cwd,
12644 error = apply_unveil(got_repo_get_path(repo), 0,
12645 worktree ? got_worktree_get_root_path(worktree) : NULL);
12648 error = process_backup_refs(
12649 GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
12650 argc == 1 ? argv[0] : NULL, delete_backups, repo);
12651 goto done; /* nothing else to do */
12654 error = get_gitconfig_path(&gitconfig_path);
12657 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
12658 gitconfig_path, pack_fds);
12662 if (worktree != NULL && !list_backups && !delete_backups) {
12663 error = worktree_has_logmsg_ref("histedit", worktree, repo);
12668 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
12671 if (rebase_in_progress) {
12672 error = got_error(GOT_ERR_REBASING);
12676 error = got_worktree_merge_in_progress(&merge_in_progress, worktree,
12680 if (merge_in_progress) {
12681 error = got_error(GOT_ERR_MERGE_BUSY);
12685 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
12689 if (edit_in_progress && edit_logmsg_only) {
12690 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
12691 "histedit operation is in progress in this "
12692 "work tree and must be continued or aborted "
12693 "before the -m option can be used");
12696 if (edit_in_progress && drop_only) {
12697 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
12698 "histedit operation is in progress in this "
12699 "work tree and must be continued or aborted "
12700 "before the -d option can be used");
12703 if (edit_in_progress && fold_only) {
12704 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
12705 "histedit operation is in progress in this "
12706 "work tree and must be continued or aborted "
12707 "before the -f option can be used");
12710 if (edit_in_progress && edit_only) {
12711 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
12712 "histedit operation is in progress in this "
12713 "work tree and must be continued or aborted "
12714 "before the -e option can be used");
12718 if (edit_in_progress && abort_edit) {
12719 error = got_worktree_histedit_continue(&resume_commit_id,
12720 &tmp_branch, &branch, &base_commit_id, &fileindex,
12724 printf("Switching work tree to %s\n",
12725 got_ref_get_symref_target(branch));
12726 error = got_worktree_histedit_abort(worktree, fileindex, repo,
12727 branch, base_commit_id, abort_progress, &upa);
12730 printf("Histedit of %s aborted\n",
12731 got_ref_get_symref_target(branch));
12732 print_merge_progress_stats(&upa);
12733 goto done; /* nothing else to do */
12734 } else if (abort_edit) {
12735 error = got_error(GOT_ERR_NOT_HISTEDIT);
12739 error = get_author(&committer, repo, worktree);
12743 if (continue_edit) {
12746 if (!edit_in_progress) {
12747 error = got_error(GOT_ERR_NOT_HISTEDIT);
12751 error = got_worktree_get_histedit_script_path(&path, worktree);
12755 error = histedit_load_list(&histedit_cmds, path, repo);
12760 error = got_worktree_histedit_continue(&resume_commit_id,
12761 &tmp_branch, &branch, &base_commit_id, &fileindex,
12766 error = got_ref_resolve(&head_commit_id, repo, branch);
12770 error = got_object_open_as_commit(&commit, repo,
12774 parent_ids = got_object_commit_get_parent_ids(commit);
12775 pid = STAILQ_FIRST(parent_ids);
12777 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
12780 error = collect_commits(&commits, head_commit_id, &pid->id,
12781 base_commit_id, got_worktree_get_path_prefix(worktree),
12782 GOT_ERR_HISTEDIT_PATH, repo);
12783 got_object_commit_close(commit);
12788 if (edit_in_progress) {
12789 error = got_error(GOT_ERR_HISTEDIT_BUSY);
12793 error = got_ref_open(&branch, repo,
12794 got_worktree_get_head_ref_name(worktree), 0);
12798 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
12799 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
12800 "will not edit commit history of a branch outside "
12801 "the \"refs/heads/\" reference namespace");
12805 error = got_ref_resolve(&head_commit_id, repo, branch);
12806 got_ref_close(branch);
12811 error = got_object_open_as_commit(&commit, repo,
12815 parent_ids = got_object_commit_get_parent_ids(commit);
12816 pid = STAILQ_FIRST(parent_ids);
12818 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
12821 error = collect_commits(&commits, head_commit_id, &pid->id,
12822 got_worktree_get_base_commit_id(worktree),
12823 got_worktree_get_path_prefix(worktree),
12824 GOT_ERR_HISTEDIT_PATH, repo);
12825 got_object_commit_close(commit);
12830 if (STAILQ_EMPTY(&commits)) {
12831 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
12835 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
12836 &base_commit_id, &fileindex, worktree, repo);
12840 if (edit_script_path) {
12841 error = histedit_load_list(&histedit_cmds,
12842 edit_script_path, repo);
12844 got_worktree_histedit_abort(worktree, fileindex,
12845 repo, branch, base_commit_id,
12846 abort_progress, &upa);
12847 print_merge_progress_stats(&upa);
12851 const char *branch_name;
12852 branch_name = got_ref_get_symref_target(branch);
12853 if (strncmp(branch_name, "refs/heads/", 11) == 0)
12855 error = histedit_edit_script(&histedit_cmds, &commits,
12856 branch_name, edit_logmsg_only, fold_only,
12857 drop_only, edit_only, repo);
12859 got_worktree_histedit_abort(worktree, fileindex,
12860 repo, branch, base_commit_id,
12861 abort_progress, &upa);
12862 print_merge_progress_stats(&upa);
12868 error = histedit_save_list(&histedit_cmds, worktree,
12871 got_worktree_histedit_abort(worktree, fileindex,
12872 repo, branch, base_commit_id,
12873 abort_progress, &upa);
12874 print_merge_progress_stats(&upa);
12880 error = histedit_check_script(&histedit_cmds, &commits, repo);
12884 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
12885 if (resume_commit_id) {
12886 if (got_object_id_cmp(hle->commit_id,
12887 resume_commit_id) != 0)
12890 resume_commit_id = NULL;
12891 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
12892 hle->cmd->code == GOT_HISTEDIT_FOLD) {
12893 error = histedit_skip_commit(hle, worktree,
12898 struct got_pathlist_head paths;
12899 int have_changes = 0;
12901 TAILQ_INIT(&paths);
12902 error = got_pathlist_append(&paths, "", NULL);
12905 error = got_worktree_status(worktree, &paths,
12906 repo, 0, check_local_changes, &have_changes,
12907 check_cancelled, NULL);
12908 got_pathlist_free(&paths,
12909 GOT_PATHLIST_FREE_NONE);
12911 if (error->code != GOT_ERR_CANCELLED)
12913 if (sigint_received || sigpipe_received)
12916 if (have_changes) {
12917 error = histedit_commit(NULL, worktree,
12918 fileindex, tmp_branch, hle,
12919 committer, allow_conflict, repo);
12923 error = got_object_open_as_commit(
12924 &commit, repo, hle->commit_id);
12927 error = show_histedit_progress(commit,
12929 got_object_commit_close(commit);
12938 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
12939 error = histedit_skip_commit(hle, worktree, repo);
12945 error = got_object_open_as_commit(&commit, repo,
12949 parent_ids = got_object_commit_get_parent_ids(commit);
12950 pid = STAILQ_FIRST(parent_ids);
12952 error = got_worktree_histedit_merge_files(&merged_paths,
12953 worktree, fileindex, &pid->id, hle->commit_id, repo,
12954 update_progress, &upa, check_cancelled, NULL);
12957 got_object_commit_close(commit);
12960 print_merge_progress_stats(&upa);
12961 if (upa.conflicts > 0 || upa.missing > 0 ||
12962 upa.not_deleted > 0 || upa.unversioned > 0) {
12963 if (upa.conflicts > 0) {
12964 error = show_rebase_merge_conflict(
12965 hle->commit_id, repo);
12969 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
12973 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
12975 error = got_object_id_str(&id_str, hle->commit_id);
12978 printf("Stopping histedit for amending commit %s\n",
12981 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
12982 error = got_worktree_histedit_postpone(worktree,
12987 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
12988 error = histedit_skip_commit(hle, worktree, repo);
12994 error = histedit_commit(&merged_paths, worktree, fileindex,
12995 tmp_branch, hle, committer, allow_conflict, repo);
12996 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
13001 if (upa.conflicts > 0 || upa.missing > 0 ||
13002 upa.not_deleted > 0 || upa.unversioned > 0) {
13003 error = got_worktree_histedit_postpone(worktree, fileindex);
13006 if (upa.conflicts > 0 && upa.missing == 0 &&
13007 upa.not_deleted == 0 && upa.unversioned == 0) {
13008 error = got_error_msg(GOT_ERR_CONFLICTS,
13009 "conflicts must be resolved before histedit "
13011 } else if (upa.conflicts > 0) {
13012 error = got_error_msg(GOT_ERR_CONFLICTS,
13013 "conflicts must be resolved before histedit "
13014 "can continue; changes destined for some "
13015 "files were not yet merged and should be "
13016 "merged manually if required before the "
13017 "histedit operation is continued");
13019 error = got_error_msg(GOT_ERR_CONFLICTS,
13020 "changes destined for some files were not "
13021 "yet merged and should be merged manually "
13022 "if required before the histedit operation "
13026 error = histedit_complete(worktree, fileindex, tmp_branch,
13031 free(gitconfig_path);
13032 got_object_id_queue_free(&commits);
13033 histedit_free_list(&histedit_cmds);
13034 free(head_commit_id);
13035 free(base_commit_id);
13036 free(resume_commit_id);
13038 got_object_commit_close(commit);
13040 got_ref_close(branch);
13042 got_ref_close(tmp_branch);
13044 got_worktree_close(worktree);
13046 const struct got_error *close_err = got_repo_close(repo);
13051 const struct got_error *pack_err =
13052 got_repo_pack_fds_close(pack_fds);
13060 usage_integrate(void)
13062 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
13066 static const struct got_error *
13067 cmd_integrate(int argc, char *argv[])
13069 const struct got_error *error = NULL;
13070 struct got_repository *repo = NULL;
13071 struct got_worktree *worktree = NULL;
13072 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
13073 const char *branch_arg = NULL;
13074 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
13075 struct got_fileindex *fileindex = NULL;
13076 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
13078 struct got_update_progress_arg upa;
13079 int *pack_fds = NULL;
13082 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
13083 "unveil", NULL) == -1)
13087 while ((ch = getopt(argc, argv, "")) != -1) {
13100 branch_arg = argv[0];
13102 cwd = getcwd(NULL, 0);
13104 error = got_error_from_errno("getcwd");
13108 error = got_repo_pack_fds_open(&pack_fds);
13112 error = got_worktree_open(&worktree, cwd);
13114 if (error->code == GOT_ERR_NOT_WORKTREE)
13115 error = wrap_not_worktree_error(error, "integrate",
13120 error = check_rebase_or_histedit_in_progress(worktree);
13124 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
13129 error = apply_unveil(got_repo_get_path(repo), 0,
13130 got_worktree_get_root_path(worktree));
13134 error = check_merge_in_progress(worktree, repo);
13138 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
13139 error = got_error_from_errno("asprintf");
13143 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
13144 &base_branch_ref, worktree, refname, repo);
13148 refname = strdup(got_ref_get_name(branch_ref));
13149 if (refname == NULL) {
13150 error = got_error_from_errno("strdup");
13151 got_worktree_integrate_abort(worktree, fileindex, repo,
13152 branch_ref, base_branch_ref);
13155 base_refname = strdup(got_ref_get_name(base_branch_ref));
13156 if (base_refname == NULL) {
13157 error = got_error_from_errno("strdup");
13158 got_worktree_integrate_abort(worktree, fileindex, repo,
13159 branch_ref, base_branch_ref);
13162 if (strncmp(base_refname, "refs/heads/", 11) != 0) {
13163 error = got_error(GOT_ERR_INTEGRATE_BRANCH);
13164 got_worktree_integrate_abort(worktree, fileindex, repo,
13165 branch_ref, base_branch_ref);
13169 error = got_ref_resolve(&commit_id, repo, branch_ref);
13173 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
13177 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
13178 error = got_error_msg(GOT_ERR_SAME_BRANCH,
13179 "specified branch has already been integrated");
13180 got_worktree_integrate_abort(worktree, fileindex, repo,
13181 branch_ref, base_branch_ref);
13185 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
13187 if (error->code == GOT_ERR_ANCESTRY)
13188 error = got_error(GOT_ERR_REBASE_REQUIRED);
13189 got_worktree_integrate_abort(worktree, fileindex, repo,
13190 branch_ref, base_branch_ref);
13194 memset(&upa, 0, sizeof(upa));
13195 error = got_worktree_integrate_continue(worktree, fileindex, repo,
13196 branch_ref, base_branch_ref, update_progress, &upa,
13197 check_cancelled, NULL);
13201 printf("Integrated %s into %s\n", refname, base_refname);
13202 print_update_progress_stats(&upa);
13205 const struct got_error *close_err = got_repo_close(repo);
13210 got_worktree_close(worktree);
13212 const struct got_error *pack_err =
13213 got_repo_pack_fds_close(pack_fds);
13218 free(base_commit_id);
13221 free(base_refname);
13228 fprintf(stderr, "usage: %s merge [-aCcn] [branch]\n", getprogname());
13232 static const struct got_error *
13233 cmd_merge(int argc, char *argv[])
13235 const struct got_error *error = NULL;
13236 struct got_worktree *worktree = NULL;
13237 struct got_repository *repo = NULL;
13238 struct got_fileindex *fileindex = NULL;
13239 char *cwd = NULL, *id_str = NULL, *author = NULL;
13240 char *gitconfig_path = NULL;
13241 struct got_reference *branch = NULL, *wt_branch = NULL;
13242 struct got_object_id *branch_tip = NULL, *yca_id = NULL;
13243 struct got_object_id *wt_branch_tip = NULL;
13244 int ch, merge_in_progress = 0, abort_merge = 0, continue_merge = 0;
13245 int allow_conflict = 0, prefer_fast_forward = 1, interrupt_merge = 0;
13246 struct got_update_progress_arg upa;
13247 struct got_object_id *merge_commit_id = NULL;
13248 char *branch_name = NULL;
13249 int *pack_fds = NULL;
13251 memset(&upa, 0, sizeof(upa));
13254 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
13255 "unveil", NULL) == -1)
13259 while ((ch = getopt(argc, argv, "aCcMn")) != -1) {
13265 allow_conflict = 1;
13268 continue_merge = 1;
13271 prefer_fast_forward = 0;
13274 interrupt_merge = 1;
13286 if (continue_merge)
13287 option_conflict('a', 'c');
13288 if (!prefer_fast_forward)
13289 option_conflict('a', 'M');
13290 if (interrupt_merge)
13291 option_conflict('a', 'n');
13292 } else if (continue_merge) {
13293 if (!prefer_fast_forward)
13294 option_conflict('c', 'M');
13295 if (interrupt_merge)
13296 option_conflict('c', 'n');
13298 if (allow_conflict) {
13299 if (!continue_merge)
13300 errx(1, "-C option requires -c");
13302 if (abort_merge || continue_merge) {
13305 } else if (argc != 1)
13308 cwd = getcwd(NULL, 0);
13310 error = got_error_from_errno("getcwd");
13314 error = got_repo_pack_fds_open(&pack_fds);
13318 error = got_worktree_open(&worktree, cwd);
13320 if (error->code == GOT_ERR_NOT_WORKTREE)
13321 error = wrap_not_worktree_error(error,
13326 error = get_gitconfig_path(&gitconfig_path);
13329 error = got_repo_open(&repo,
13330 worktree ? got_worktree_get_repo_path(worktree) : cwd,
13331 gitconfig_path, pack_fds);
13335 if (worktree != NULL) {
13336 error = worktree_has_logmsg_ref("merge", worktree, repo);
13341 error = apply_unveil(got_repo_get_path(repo), 0,
13342 worktree ? got_worktree_get_root_path(worktree) : NULL);
13346 error = check_rebase_or_histedit_in_progress(worktree);
13350 error = got_worktree_merge_in_progress(&merge_in_progress, worktree,
13355 if (merge_in_progress && !(abort_merge || continue_merge)) {
13356 error = got_error(GOT_ERR_MERGE_BUSY);
13360 if (!merge_in_progress && (abort_merge || continue_merge)) {
13361 error = got_error(GOT_ERR_NOT_MERGING);
13366 error = got_worktree_merge_continue(&branch_name,
13367 &branch_tip, &fileindex, worktree, repo);
13370 error = got_worktree_merge_abort(worktree, fileindex, repo,
13371 abort_progress, &upa);
13374 printf("Merge of %s aborted\n", branch_name);
13375 goto done; /* nothing else to do */
13378 if (strncmp(got_worktree_get_head_ref_name(worktree),
13379 "refs/heads/", 11) != 0) {
13380 error = got_error_fmt(GOT_ERR_COMMIT_BRANCH,
13381 "work tree's current branch %s is outside the "
13382 "\"refs/heads/\" reference namespace; "
13383 "update -b required",
13384 got_worktree_get_head_ref_name(worktree));
13388 error = get_author(&author, repo, worktree);
13392 error = got_ref_open(&wt_branch, repo,
13393 got_worktree_get_head_ref_name(worktree), 0);
13396 error = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
13400 if (continue_merge) {
13401 struct got_object_id *base_commit_id;
13402 base_commit_id = got_worktree_get_base_commit_id(worktree);
13403 if (got_object_id_cmp(wt_branch_tip, base_commit_id) != 0) {
13404 error = got_error(GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
13407 error = got_worktree_merge_continue(&branch_name,
13408 &branch_tip, &fileindex, worktree, repo);
13412 error = got_ref_open(&branch, repo, argv[0], 0);
13415 branch_name = strdup(got_ref_get_name(branch));
13416 if (branch_name == NULL) {
13417 error = got_error_from_errno("strdup");
13420 error = got_ref_resolve(&branch_tip, repo, branch);
13425 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
13426 wt_branch_tip, branch_tip, 0, repo,
13427 check_cancelled, NULL);
13428 if (error && error->code != GOT_ERR_ANCESTRY)
13431 if (!continue_merge) {
13432 error = check_path_prefix(wt_branch_tip, branch_tip,
13433 got_worktree_get_path_prefix(worktree),
13434 GOT_ERR_MERGE_PATH, repo);
13437 error = got_worktree_merge_prepare(&fileindex, worktree, repo);
13440 if (prefer_fast_forward && yca_id &&
13441 got_object_id_cmp(wt_branch_tip, yca_id) == 0) {
13442 struct got_pathlist_head paths;
13443 if (interrupt_merge) {
13444 error = got_error_fmt(GOT_ERR_BAD_OPTION,
13445 "there are no changes to merge since %s "
13446 "is already based on %s; merge cannot be "
13447 "interrupted for amending; -n",
13448 branch_name, got_ref_get_name(wt_branch));
13451 printf("Forwarding %s to %s\n",
13452 got_ref_get_name(wt_branch), branch_name);
13453 error = got_ref_change_ref(wt_branch, branch_tip);
13456 error = got_ref_write(wt_branch, repo);
13459 error = got_worktree_set_base_commit_id(worktree, repo,
13463 TAILQ_INIT(&paths);
13464 error = got_pathlist_append(&paths, "", NULL);
13467 error = got_worktree_checkout_files(worktree,
13468 &paths, repo, update_progress, &upa,
13469 check_cancelled, NULL);
13470 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
13473 if (upa.did_something) {
13475 error = got_object_id_str(&id_str, branch_tip);
13478 printf("Updated to commit %s\n", id_str);
13481 printf("Already up-to-date\n");
13482 print_update_progress_stats(&upa);
13485 error = got_worktree_merge_write_refs(worktree, branch, repo);
13489 error = got_worktree_merge_branch(worktree, fileindex,
13490 yca_id, branch_tip, repo, update_progress, &upa,
13491 check_cancelled, NULL);
13494 print_merge_progress_stats(&upa);
13495 if (!upa.did_something) {
13496 error = got_worktree_merge_abort(worktree, fileindex,
13497 repo, abort_progress, &upa);
13500 printf("Already up-to-date\n");
13505 if (interrupt_merge) {
13506 error = got_worktree_merge_postpone(worktree, fileindex);
13509 printf("Merge of %s interrupted on request\n", branch_name);
13510 } else if (upa.conflicts > 0 || upa.missing > 0 ||
13511 upa.not_deleted > 0 || upa.unversioned > 0) {
13512 error = got_worktree_merge_postpone(worktree, fileindex);
13515 if (upa.conflicts > 0 && upa.missing == 0 &&
13516 upa.not_deleted == 0 && upa.unversioned == 0) {
13517 error = got_error_msg(GOT_ERR_CONFLICTS,
13518 "conflicts must be resolved before merging "
13520 } else if (upa.conflicts > 0) {
13521 error = got_error_msg(GOT_ERR_CONFLICTS,
13522 "conflicts must be resolved before merging "
13523 "can continue; changes destined for some "
13524 "files were not yet merged and "
13525 "should be merged manually if required before the "
13526 "merge operation is continued");
13528 error = got_error_msg(GOT_ERR_CONFLICTS,
13529 "changes destined for some "
13530 "files were not yet merged and should be "
13531 "merged manually if required before the "
13532 "merge operation is continued");
13536 error = got_worktree_merge_commit(&merge_commit_id, worktree,
13537 fileindex, author, NULL, 1, branch_tip, branch_name,
13538 allow_conflict, repo, continue_merge ? print_status : NULL,
13542 error = got_worktree_merge_complete(worktree, fileindex, repo);
13545 error = got_object_id_str(&id_str, merge_commit_id);
13548 printf("Merged %s into %s: %s\n", branch_name,
13549 got_worktree_get_head_ref_name(worktree),
13554 free(gitconfig_path);
13556 free(merge_commit_id);
13562 got_ref_close(branch);
13564 got_ref_close(wt_branch);
13566 got_worktree_close(worktree);
13568 const struct got_error *close_err = got_repo_close(repo);
13573 const struct got_error *pack_err =
13574 got_repo_pack_fds_close(pack_fds);
13584 fprintf(stderr, "usage: %s stage [-lpS] [-F response-script] "
13585 "[path ...]\n", getprogname());
13589 static const struct got_error *
13590 print_stage(void *arg, unsigned char status, unsigned char staged_status,
13591 const char *path, struct got_object_id *blob_id,
13592 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
13593 int dirfd, const char *de_name)
13595 const struct got_error *err = NULL;
13596 char *id_str = NULL;
13598 if (staged_status != GOT_STATUS_ADD &&
13599 staged_status != GOT_STATUS_MODIFY &&
13600 staged_status != GOT_STATUS_DELETE)
13603 if (staged_status == GOT_STATUS_ADD ||
13604 staged_status == GOT_STATUS_MODIFY)
13605 err = got_object_id_str(&id_str, staged_blob_id);
13607 err = got_object_id_str(&id_str, blob_id);
13611 printf("%s %c %s\n", id_str, staged_status, path);
13616 static const struct got_error *
13617 cmd_stage(int argc, char *argv[])
13619 const struct got_error *error = NULL;
13620 struct got_repository *repo = NULL;
13621 struct got_worktree *worktree = NULL;
13623 struct got_pathlist_head paths;
13624 int ch, list_stage = 0, pflag = 0, allow_bad_symlinks = 0;
13625 FILE *patch_script_file = NULL;
13626 const char *patch_script_path = NULL;
13627 struct choose_patch_arg cpa;
13628 int *pack_fds = NULL;
13630 TAILQ_INIT(&paths);
13633 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
13634 "unveil", NULL) == -1)
13638 while ((ch = getopt(argc, argv, "F:lpS")) != -1) {
13641 patch_script_path = optarg;
13650 allow_bad_symlinks = 1;
13661 if (list_stage && (pflag || patch_script_path))
13662 errx(1, "-l option cannot be used with other options");
13663 if (patch_script_path && !pflag)
13664 errx(1, "-F option can only be used together with -p option");
13666 cwd = getcwd(NULL, 0);
13668 error = got_error_from_errno("getcwd");
13672 error = got_repo_pack_fds_open(&pack_fds);
13676 error = got_worktree_open(&worktree, cwd);
13678 if (error->code == GOT_ERR_NOT_WORKTREE)
13679 error = wrap_not_worktree_error(error, "stage", cwd);
13683 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
13688 if (patch_script_path) {
13689 patch_script_file = fopen(patch_script_path, "re");
13690 if (patch_script_file == NULL) {
13691 error = got_error_from_errno2("fopen",
13692 patch_script_path);
13696 error = apply_unveil(got_repo_get_path(repo), 0,
13697 got_worktree_get_root_path(worktree));
13701 error = check_merge_in_progress(worktree, repo);
13705 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
13710 error = got_worktree_status(worktree, &paths, repo, 0,
13711 print_stage, NULL, check_cancelled, NULL);
13713 cpa.patch_script_file = patch_script_file;
13714 cpa.action = "stage";
13715 error = got_worktree_stage(worktree, &paths,
13716 pflag ? NULL : print_status, NULL,
13717 pflag ? choose_patch : NULL, &cpa,
13718 allow_bad_symlinks, repo);
13721 if (patch_script_file && fclose(patch_script_file) == EOF &&
13723 error = got_error_from_errno2("fclose", patch_script_path);
13725 const struct got_error *close_err = got_repo_close(repo);
13730 got_worktree_close(worktree);
13732 const struct got_error *pack_err =
13733 got_repo_pack_fds_close(pack_fds);
13737 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
13743 usage_unstage(void)
13745 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
13746 "[path ...]\n", getprogname());
13751 static const struct got_error *
13752 cmd_unstage(int argc, char *argv[])
13754 const struct got_error *error = NULL;
13755 struct got_repository *repo = NULL;
13756 struct got_worktree *worktree = NULL;
13758 struct got_pathlist_head paths;
13760 struct got_update_progress_arg upa;
13761 FILE *patch_script_file = NULL;
13762 const char *patch_script_path = NULL;
13763 struct choose_patch_arg cpa;
13764 int *pack_fds = NULL;
13766 TAILQ_INIT(&paths);
13769 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
13770 "unveil", NULL) == -1)
13774 while ((ch = getopt(argc, argv, "F:p")) != -1) {
13777 patch_script_path = optarg;
13791 if (patch_script_path && !pflag)
13792 errx(1, "-F option can only be used together with -p option");
13794 cwd = getcwd(NULL, 0);
13796 error = got_error_from_errno("getcwd");
13800 error = got_repo_pack_fds_open(&pack_fds);
13804 error = got_worktree_open(&worktree, cwd);
13806 if (error->code == GOT_ERR_NOT_WORKTREE)
13807 error = wrap_not_worktree_error(error, "unstage", cwd);
13811 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
13816 if (patch_script_path) {
13817 patch_script_file = fopen(patch_script_path, "re");
13818 if (patch_script_file == NULL) {
13819 error = got_error_from_errno2("fopen",
13820 patch_script_path);
13825 error = apply_unveil(got_repo_get_path(repo), 0,
13826 got_worktree_get_root_path(worktree));
13830 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
13834 cpa.patch_script_file = patch_script_file;
13835 cpa.action = "unstage";
13836 memset(&upa, 0, sizeof(upa));
13837 error = got_worktree_unstage(worktree, &paths, update_progress,
13838 &upa, pflag ? choose_patch : NULL, &cpa, repo);
13840 print_merge_progress_stats(&upa);
13842 if (patch_script_file && fclose(patch_script_file) == EOF &&
13844 error = got_error_from_errno2("fclose", patch_script_path);
13846 const struct got_error *close_err = got_repo_close(repo);
13851 got_worktree_close(worktree);
13853 const struct got_error *pack_err =
13854 got_repo_pack_fds_close(pack_fds);
13858 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
13866 fprintf(stderr, "usage: %s cat [-P] [-c commit] [-r repository-path] "
13867 "arg ...\n", getprogname());
13871 static const struct got_error *
13872 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
13874 const struct got_error *err;
13875 struct got_blob_object *blob;
13878 fd = got_opentempfd();
13880 return got_error_from_errno("got_opentempfd");
13882 err = got_object_open_as_blob(&blob, repo, id, 8192, fd);
13886 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
13888 if (fd != -1 && close(fd) == -1 && err == NULL)
13889 err = got_error_from_errno("close");
13891 got_object_blob_close(blob);
13895 static const struct got_error *
13896 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
13898 const struct got_error *err;
13899 struct got_tree_object *tree;
13902 err = got_object_open_as_tree(&tree, repo, id);
13906 nentries = got_object_tree_get_nentries(tree);
13907 for (i = 0; i < nentries; i++) {
13908 struct got_tree_entry *te;
13910 if (sigint_received || sigpipe_received)
13912 te = got_object_tree_get_entry(tree, i);
13913 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
13916 fprintf(outfile, "%s %.7o %s\n", id_str,
13917 got_tree_entry_get_mode(te),
13918 got_tree_entry_get_name(te));
13922 got_object_tree_close(tree);
13926 static const struct got_error *
13927 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
13929 const struct got_error *err;
13930 struct got_commit_object *commit;
13931 const struct got_object_id_queue *parent_ids;
13932 struct got_object_qid *pid;
13933 char *id_str = NULL;
13934 const char *logmsg = NULL;
13937 err = got_object_open_as_commit(&commit, repo, id);
13941 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
13945 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
13946 parent_ids = got_object_commit_get_parent_ids(commit);
13947 fprintf(outfile, "numparents %d\n",
13948 got_object_commit_get_nparents(commit));
13949 STAILQ_FOREACH(pid, parent_ids, entry) {
13951 err = got_object_id_str(&pid_str, &pid->id);
13954 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
13957 got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
13958 got_object_commit_get_author_gmtoff(commit));
13959 fprintf(outfile, "%s%s %lld %s\n", GOT_COMMIT_LABEL_AUTHOR,
13960 got_object_commit_get_author(commit),
13961 (long long)got_object_commit_get_author_time(commit),
13964 got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
13965 got_object_commit_get_committer_gmtoff(commit));
13966 fprintf(outfile, "%s%s %lld %s\n", GOT_COMMIT_LABEL_COMMITTER,
13967 got_object_commit_get_committer(commit),
13968 (long long)got_object_commit_get_committer_time(commit),
13971 logmsg = got_object_commit_get_logmsg_raw(commit);
13972 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
13973 fprintf(outfile, "%s", logmsg);
13976 got_object_commit_close(commit);
13980 static const struct got_error *
13981 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
13983 const struct got_error *err;
13984 struct got_tag_object *tag;
13985 char *id_str = NULL;
13986 const char *tagmsg = NULL;
13989 err = got_object_open_as_tag(&tag, repo, id);
13993 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
13997 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
13999 switch (got_object_tag_get_object_type(tag)) {
14000 case GOT_OBJ_TYPE_BLOB:
14001 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
14002 GOT_OBJ_LABEL_BLOB);
14004 case GOT_OBJ_TYPE_TREE:
14005 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
14006 GOT_OBJ_LABEL_TREE);
14008 case GOT_OBJ_TYPE_COMMIT:
14009 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
14010 GOT_OBJ_LABEL_COMMIT);
14012 case GOT_OBJ_TYPE_TAG:
14013 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
14014 GOT_OBJ_LABEL_TAG);
14020 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
14021 got_object_tag_get_name(tag));
14023 got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
14024 got_object_tag_get_tagger_gmtoff(tag));
14025 fprintf(outfile, "%s%s %lld %s\n", GOT_TAG_LABEL_TAGGER,
14026 got_object_tag_get_tagger(tag),
14027 (long long)got_object_tag_get_tagger_time(tag),
14030 tagmsg = got_object_tag_get_message(tag);
14031 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
14032 fprintf(outfile, "%s", tagmsg);
14035 got_object_tag_close(tag);
14039 static const struct got_error *
14040 cmd_cat(int argc, char *argv[])
14042 const struct got_error *error;
14043 struct got_repository *repo = NULL;
14044 struct got_worktree *worktree = NULL;
14045 char *cwd = NULL, *repo_path = NULL, *label = NULL;
14046 const char *commit_id_str = NULL;
14047 struct got_object_id *id = NULL, *commit_id = NULL;
14048 struct got_commit_object *commit = NULL;
14049 int ch, obj_type, i, force_path = 0;
14050 struct got_reflist_head refs;
14051 int *pack_fds = NULL;
14056 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
14061 while ((ch = getopt(argc, argv, "c:Pr:")) != -1) {
14064 commit_id_str = optarg;
14070 repo_path = realpath(optarg, NULL);
14071 if (repo_path == NULL)
14072 return got_error_from_errno2("realpath",
14074 got_path_strip_trailing_slashes(repo_path);
14085 cwd = getcwd(NULL, 0);
14087 error = got_error_from_errno("getcwd");
14091 error = got_repo_pack_fds_open(&pack_fds);
14095 if (repo_path == NULL) {
14096 error = got_worktree_open(&worktree, cwd);
14097 if (error && error->code != GOT_ERR_NOT_WORKTREE)
14100 repo_path = strdup(
14101 got_worktree_get_repo_path(worktree));
14102 if (repo_path == NULL) {
14103 error = got_error_from_errno("strdup");
14107 /* Release work tree lock. */
14108 got_worktree_close(worktree);
14113 if (repo_path == NULL) {
14114 repo_path = strdup(cwd);
14115 if (repo_path == NULL)
14116 return got_error_from_errno("strdup");
14119 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
14124 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
14128 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
14132 if (commit_id_str == NULL)
14133 commit_id_str = GOT_REF_HEAD;
14134 error = got_repo_match_object_id(&commit_id, NULL,
14135 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
14139 error = got_object_open_as_commit(&commit, repo, commit_id);
14143 for (i = 0; i < argc; i++) {
14145 error = got_object_id_by_path(&id, repo, commit,
14150 error = got_repo_match_object_id(&id, &label, argv[i],
14151 GOT_OBJ_TYPE_ANY, NULL /* do not resolve tags */,
14154 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
14155 error->code != GOT_ERR_NOT_REF)
14157 error = got_object_id_by_path(&id, repo,
14164 error = got_object_get_type(&obj_type, repo, id);
14168 switch (obj_type) {
14169 case GOT_OBJ_TYPE_BLOB:
14170 error = cat_blob(id, repo, stdout);
14172 case GOT_OBJ_TYPE_TREE:
14173 error = cat_tree(id, repo, stdout);
14175 case GOT_OBJ_TYPE_COMMIT:
14176 error = cat_commit(id, repo, stdout);
14178 case GOT_OBJ_TYPE_TAG:
14179 error = cat_tag(id, repo, stdout);
14182 error = got_error(GOT_ERR_OBJ_TYPE);
14197 got_object_commit_close(commit);
14199 got_worktree_close(worktree);
14201 const struct got_error *close_err = got_repo_close(repo);
14206 const struct got_error *pack_err =
14207 got_repo_pack_fds_close(pack_fds);
14212 got_ref_list_free(&refs);
14219 fprintf(stderr, "usage: %s info [path ...]\n",
14224 static const struct got_error *
14225 print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
14226 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
14227 struct got_object_id *commit_id)
14229 const struct got_error *err = NULL;
14230 char *id_str = NULL;
14232 struct tm mytm, *tm;
14233 struct got_pathlist_head *paths = arg;
14234 struct got_pathlist_entry *pe;
14237 * Clear error indication from any of the path arguments which
14238 * would cause this file index entry to be displayed.
14240 TAILQ_FOREACH(pe, paths, entry) {
14241 if (got_path_cmp(path, pe->path, strlen(path),
14242 pe->path_len) == 0 ||
14243 got_path_is_child(path, pe->path, pe->path_len))
14244 pe->data = NULL; /* no error */
14247 printf(GOT_COMMIT_SEP_STR);
14249 printf("symlink: %s\n", path);
14250 else if (S_ISREG(mode)) {
14251 printf("file: %s\n", path);
14252 printf("mode: %o\n", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
14253 } else if (S_ISDIR(mode))
14254 printf("directory: %s\n", path);
14256 printf("something: %s\n", path);
14258 tm = localtime_r(&mtime, &mytm);
14261 if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) == 0)
14262 return got_error(GOT_ERR_NO_SPACE);
14263 printf("timestamp: %s\n", datebuf);
14266 err = got_object_id_str(&id_str, blob_id);
14269 printf("based on blob: %s\n", id_str);
14273 if (staged_blob_id) {
14274 err = got_object_id_str(&id_str, staged_blob_id);
14277 printf("based on staged blob: %s\n", id_str);
14282 err = got_object_id_str(&id_str, commit_id);
14285 printf("based on commit: %s\n", id_str);
14292 static const struct got_error *
14293 cmd_info(int argc, char *argv[])
14295 const struct got_error *error = NULL;
14296 struct got_worktree *worktree = NULL;
14297 char *cwd = NULL, *id_str = NULL;
14298 struct got_pathlist_head paths;
14299 char *uuidstr = NULL;
14300 int ch, show_files = 0;
14302 TAILQ_INIT(&paths);
14305 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
14310 while ((ch = getopt(argc, argv, "")) != -1) {
14321 cwd = getcwd(NULL, 0);
14323 error = got_error_from_errno("getcwd");
14327 error = got_worktree_open(&worktree, cwd);
14329 if (error->code == GOT_ERR_NOT_WORKTREE)
14330 error = wrap_not_worktree_error(error, "info", cwd);
14335 /* Remove "wpath cpath proc exec sendfd" promises. */
14336 if (pledge("stdio rpath flock unveil", NULL) == -1)
14339 error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
14344 error = get_worktree_paths_from_argv(&paths, argc, argv,
14351 error = got_object_id_str(&id_str,
14352 got_worktree_get_base_commit_id(worktree));
14356 error = got_worktree_get_uuid(&uuidstr, worktree);
14360 printf("work tree: %s\n", got_worktree_get_root_path(worktree));
14361 printf("work tree base commit: %s\n", id_str);
14362 printf("work tree path prefix: %s\n",
14363 got_worktree_get_path_prefix(worktree));
14364 printf("work tree branch reference: %s\n",
14365 got_worktree_get_head_ref_name(worktree));
14366 printf("work tree UUID: %s\n", uuidstr);
14367 printf("repository: %s\n", got_worktree_get_repo_path(worktree));
14370 struct got_pathlist_entry *pe;
14371 TAILQ_FOREACH(pe, &paths, entry) {
14372 if (pe->path_len == 0)
14375 * Assume this path will fail. This will be corrected
14376 * in print_path_info() in case the path does suceeed.
14378 pe->data = (void *)got_error(GOT_ERR_BAD_PATH);
14380 error = got_worktree_path_info(worktree, &paths,
14381 print_path_info, &paths, check_cancelled, NULL);
14384 TAILQ_FOREACH(pe, &paths, entry) {
14385 if (pe->data != NULL) {
14386 const struct got_error *perr;
14389 error = got_error_fmt(perr->code, "%s",
14397 got_worktree_close(worktree);
14398 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);