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"
67 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
70 static volatile sig_atomic_t sigint_received;
71 static volatile sig_atomic_t sigpipe_received;
74 catch_sigint(int signo)
80 catch_sigpipe(int signo)
88 const struct got_error *(*cmd_main)(int, char *[]);
89 void (*cmd_usage)(void);
90 const char *cmd_alias;
93 __dead static void usage(int, int);
94 __dead static void usage_import(void);
95 __dead static void usage_clone(void);
96 __dead static void usage_fetch(void);
97 __dead static void usage_checkout(void);
98 __dead static void usage_update(void);
99 __dead static void usage_log(void);
100 __dead static void usage_diff(void);
101 __dead static void usage_blame(void);
102 __dead static void usage_tree(void);
103 __dead static void usage_status(void);
104 __dead static void usage_ref(void);
105 __dead static void usage_branch(void);
106 __dead static void usage_tag(void);
107 __dead static void usage_add(void);
108 __dead static void usage_remove(void);
109 __dead static void usage_patch(void);
110 __dead static void usage_revert(void);
111 __dead static void usage_commit(void);
112 __dead static void usage_send(void);
113 __dead static void usage_cherrypick(void);
114 __dead static void usage_backout(void);
115 __dead static void usage_rebase(void);
116 __dead static void usage_histedit(void);
117 __dead static void usage_integrate(void);
118 __dead static void usage_merge(void);
119 __dead static void usage_stage(void);
120 __dead static void usage_unstage(void);
121 __dead static void usage_cat(void);
122 __dead static void usage_info(void);
124 static const struct got_error* cmd_import(int, char *[]);
125 static const struct got_error* cmd_clone(int, char *[]);
126 static const struct got_error* cmd_fetch(int, char *[]);
127 static const struct got_error* cmd_checkout(int, char *[]);
128 static const struct got_error* cmd_update(int, char *[]);
129 static const struct got_error* cmd_log(int, char *[]);
130 static const struct got_error* cmd_diff(int, char *[]);
131 static const struct got_error* cmd_blame(int, char *[]);
132 static const struct got_error* cmd_tree(int, char *[]);
133 static const struct got_error* cmd_status(int, char *[]);
134 static const struct got_error* cmd_ref(int, char *[]);
135 static const struct got_error* cmd_branch(int, char *[]);
136 static const struct got_error* cmd_tag(int, char *[]);
137 static const struct got_error* cmd_add(int, char *[]);
138 static const struct got_error* cmd_remove(int, char *[]);
139 static const struct got_error* cmd_patch(int, char *[]);
140 static const struct got_error* cmd_revert(int, char *[]);
141 static const struct got_error* cmd_commit(int, char *[]);
142 static const struct got_error* cmd_send(int, char *[]);
143 static const struct got_error* cmd_cherrypick(int, char *[]);
144 static const struct got_error* cmd_backout(int, char *[]);
145 static const struct got_error* cmd_rebase(int, char *[]);
146 static const struct got_error* cmd_histedit(int, char *[]);
147 static const struct got_error* cmd_integrate(int, char *[]);
148 static const struct got_error* cmd_merge(int, char *[]);
149 static const struct got_error* cmd_stage(int, char *[]);
150 static const struct got_error* cmd_unstage(int, char *[]);
151 static const struct got_error* cmd_cat(int, char *[]);
152 static const struct got_error* cmd_info(int, char *[]);
154 static const struct got_cmd got_commands[] = {
155 { "import", cmd_import, usage_import, "im" },
156 { "clone", cmd_clone, usage_clone, "cl" },
157 { "fetch", cmd_fetch, usage_fetch, "fe" },
158 { "checkout", cmd_checkout, usage_checkout, "co" },
159 { "update", cmd_update, usage_update, "up" },
160 { "log", cmd_log, usage_log, "" },
161 { "diff", cmd_diff, usage_diff, "di" },
162 { "blame", cmd_blame, usage_blame, "bl" },
163 { "tree", cmd_tree, usage_tree, "tr" },
164 { "status", cmd_status, usage_status, "st" },
165 { "ref", cmd_ref, usage_ref, "" },
166 { "branch", cmd_branch, usage_branch, "br" },
167 { "tag", cmd_tag, usage_tag, "" },
168 { "add", cmd_add, usage_add, "" },
169 { "remove", cmd_remove, usage_remove, "rm" },
170 { "patch", cmd_patch, usage_patch, "pa" },
171 { "revert", cmd_revert, usage_revert, "rv" },
172 { "commit", cmd_commit, usage_commit, "ci" },
173 { "send", cmd_send, usage_send, "se" },
174 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
175 { "backout", cmd_backout, usage_backout, "bo" },
176 { "rebase", cmd_rebase, usage_rebase, "rb" },
177 { "histedit", cmd_histedit, usage_histedit, "he" },
178 { "integrate", cmd_integrate, usage_integrate,"ig" },
179 { "merge", cmd_merge, usage_merge, "mg" },
180 { "stage", cmd_stage, usage_stage, "sg" },
181 { "unstage", cmd_unstage, usage_unstage, "ug" },
182 { "cat", cmd_cat, usage_cat, "" },
183 { "info", cmd_info, usage_info, "" },
187 list_commands(FILE *fp)
191 fprintf(fp, "commands:");
192 for (i = 0; i < nitems(got_commands); i++) {
193 const struct got_cmd *cmd = &got_commands[i];
194 fprintf(fp, " %s", cmd->cmd_name);
200 option_conflict(char a, char b)
202 errx(1, "-%c and -%c options are mutually exclusive", a, b);
206 main(int argc, char *argv[])
208 const struct got_cmd *cmd;
211 int hflag = 0, Vflag = 0;
212 static const struct option longopts[] = {
213 { "version", no_argument, NULL, 'V' },
217 setlocale(LC_CTYPE, "");
219 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
239 got_version_print_str();
244 usage(hflag, hflag ? 0 : 1);
246 signal(SIGINT, catch_sigint);
247 signal(SIGPIPE, catch_sigpipe);
249 for (i = 0; i < nitems(got_commands); i++) {
250 const struct got_error *error;
252 cmd = &got_commands[i];
254 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
255 strcmp(cmd->cmd_alias, argv[0]) != 0)
261 error = cmd->cmd_main(argc, argv);
262 if (error && error->code != GOT_ERR_CANCELLED &&
263 error->code != GOT_ERR_PRIVSEP_EXIT &&
264 !(sigpipe_received &&
265 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
267 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
269 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
276 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
277 list_commands(stderr);
282 usage(int hflag, int status)
284 FILE *fp = (status == 0) ? stdout : stderr;
286 fprintf(fp, "usage: %s [-hV] command [arg ...]\n",
293 static const struct got_error *
294 get_editor(char **abspath)
296 const struct got_error *err = NULL;
301 editor = getenv("VISUAL");
303 editor = getenv("EDITOR");
306 err = got_path_find_prog(abspath, editor);
311 if (*abspath == NULL) {
312 *abspath = strdup("/usr/bin/vi");
313 if (*abspath == NULL)
314 return got_error_from_errno("strdup");
320 static const struct got_error *
321 apply_unveil(const char *repo_path, int repo_read_only,
322 const char *worktree_path)
324 const struct got_error *err;
327 if (unveil("gmon.out", "rwc") != 0)
328 return got_error_from_errno2("unveil", "gmon.out");
330 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
331 return got_error_from_errno2("unveil", repo_path);
333 if (worktree_path && unveil(worktree_path, "rwc") != 0)
334 return got_error_from_errno2("unveil", worktree_path);
336 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
337 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
339 err = got_privsep_unveil_exec_helpers();
343 if (unveil(NULL, NULL) != 0)
344 return got_error_from_errno("unveil");
352 fprintf(stderr, "usage: %s import [-b branch] [-I pattern] [-m message] "
353 "[-r repository-path] directory\n", getprogname());
358 spawn_editor(const char *editor, const char *file)
361 sig_t sighup, sigint, sigquit;
364 sighup = signal(SIGHUP, SIG_IGN);
365 sigint = signal(SIGINT, SIG_IGN);
366 sigquit = signal(SIGQUIT, SIG_IGN);
368 switch (pid = fork()) {
372 execl(editor, editor, file, (char *)NULL);
376 while (waitpid(pid, &st, 0) == -1)
381 (void)signal(SIGHUP, sighup);
382 (void)signal(SIGINT, sigint);
383 (void)signal(SIGQUIT, sigquit);
385 if (!WIFEXITED(st)) {
390 return WEXITSTATUS(st);
393 static const struct got_error *
394 read_logmsg(char **logmsg, size_t *len, FILE *fp, size_t filesize)
396 const struct got_error *err = NULL;
403 if (fseeko(fp, 0L, SEEK_SET) == -1)
404 return got_error_from_errno("fseeko");
406 *logmsg = malloc(filesize + 1);
408 return got_error_from_errno("malloc");
411 while (getline(&line, &linesize, fp) != -1) {
412 if (line[0] == '#' || (*len == 0 && line[0] == '\n'))
413 continue; /* remove comments and leading empty lines */
414 *len = strlcat(*logmsg, line, filesize + 1);
415 if (*len >= filesize + 1) {
416 err = got_error(GOT_ERR_NO_SPACE);
421 err = got_ferror(fp, GOT_ERR_IO);
425 while (*len > 0 && (*logmsg)[*len - 1] == '\n') {
426 (*logmsg)[*len - 1] = '\0';
439 static const struct got_error *
440 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
441 const char *initial_content, size_t initial_content_len,
442 int require_modification)
444 const struct got_error *err = NULL;
451 if (stat(logmsg_path, &st) == -1)
452 return got_error_from_errno2("stat", logmsg_path);
454 if (spawn_editor(editor, logmsg_path) == -1)
455 return got_error_from_errno("failed spawning editor");
457 if (require_modification) {
458 struct timespec timeout;
462 nanosleep(&timeout, NULL);
465 if (stat(logmsg_path, &st2) == -1)
466 return got_error_from_errno2("stat", logmsg_path);
468 if (require_modification && st.st_size == st2.st_size &&
469 timespeccmp(&st.st_mtim, &st2.st_mtim, ==))
470 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
471 "no changes made to commit message, aborting");
473 fp = fopen(logmsg_path, "re");
475 err = got_error_from_errno("fopen");
479 /* strip comments and leading/trailing newlines */
480 err = read_logmsg(logmsg, &logmsg_len, fp, st2.st_size);
483 if (logmsg_len == 0) {
484 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
485 "commit message cannot be empty, aborting");
489 if (fp && fclose(fp) == EOF && err == NULL)
490 err = got_error_from_errno("fclose");
498 static const struct got_error *
499 collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
500 const char *path_dir, const char *branch_name)
502 char *initial_content = NULL;
503 const struct got_error *err = NULL;
504 int initial_content_len;
507 initial_content_len = asprintf(&initial_content,
508 "\n# %s to be imported to branch %s\n", path_dir,
510 if (initial_content_len == -1)
511 return got_error_from_errno("asprintf");
513 err = got_opentemp_named_fd(logmsg_path, &fd,
514 GOT_TMPDIR_STR "/got-importmsg", "");
518 if (write(fd, initial_content, initial_content_len) == -1) {
519 err = got_error_from_errno2("write", *logmsg_path);
522 if (close(fd) == -1) {
523 err = got_error_from_errno2("close", *logmsg_path);
528 err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content,
529 initial_content_len, 1);
531 if (fd != -1 && close(fd) == -1 && err == NULL)
532 err = got_error_from_errno2("close", *logmsg_path);
533 free(initial_content);
541 static const struct got_error *
542 import_progress(void *arg, const char *path)
544 printf("A %s\n", path);
548 static const struct got_error *
549 valid_author(const char *author)
551 const char *email = author;
554 * Git' expects the author (or committer) to be in the form
555 * "name <email>", which are mostly free form (see the
556 * "committer" description in git-fast-import(1)). We're only
557 * doing this to avoid git's object parser breaking on commits
561 while (*author && *author != '\n' && *author != '<' && *author != '>')
563 if (author != email && *author == '<' && *(author - 1) != ' ')
564 return got_error_fmt(GOT_ERR_COMMIT_BAD_AUTHOR, "%s: space "
565 "between author name and email required", email);
566 if (*author++ != '<')
567 return got_error_fmt(GOT_ERR_COMMIT_NO_EMAIL, "%s", email);
568 while (*author && *author != '\n' && *author != '<' && *author != '>')
570 if (strcmp(author, ">") != 0)
571 return got_error_fmt(GOT_ERR_COMMIT_NO_EMAIL, "%s", email);
575 static const struct got_error *
576 get_author(char **author, struct got_repository *repo,
577 struct got_worktree *worktree)
579 const struct got_error *err = NULL;
580 const char *got_author = NULL, *name, *email;
581 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
586 worktree_conf = got_worktree_get_gotconfig(worktree);
587 repo_conf = got_repo_get_gotconfig(repo);
590 * Priority of potential author information sources, from most
591 * significant to least significant:
592 * 1) work tree's .got/got.conf file
593 * 2) repository's got.conf file
594 * 3) repository's git config file
595 * 4) environment variables
596 * 5) global git config files (in user's home directory or /etc)
600 got_author = got_gotconfig_get_author(worktree_conf);
601 if (got_author == NULL)
602 got_author = got_gotconfig_get_author(repo_conf);
603 if (got_author == NULL) {
604 name = got_repo_get_gitconfig_author_name(repo);
605 email = got_repo_get_gitconfig_author_email(repo);
607 if (asprintf(author, "%s <%s>", name, email) == -1)
608 return got_error_from_errno("asprintf");
612 got_author = getenv("GOT_AUTHOR");
613 if (got_author == NULL) {
614 name = got_repo_get_global_gitconfig_author_name(repo);
615 email = got_repo_get_global_gitconfig_author_email(
618 if (asprintf(author, "%s <%s>", name, email)
620 return got_error_from_errno("asprintf");
623 /* TODO: Look up user in password database? */
624 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
628 *author = strdup(got_author);
630 return got_error_from_errno("strdup");
632 err = valid_author(*author);
640 static const struct got_error *
641 get_allowed_signers(char **allowed_signers, struct got_repository *repo,
642 struct got_worktree *worktree)
644 const char *got_allowed_signers = NULL;
645 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
647 *allowed_signers = NULL;
650 worktree_conf = got_worktree_get_gotconfig(worktree);
651 repo_conf = got_repo_get_gotconfig(repo);
654 * Priority of potential author information sources, from most
655 * significant to least significant:
656 * 1) work tree's .got/got.conf file
657 * 2) repository's got.conf file
661 got_allowed_signers = got_gotconfig_get_allowed_signers_file(
663 if (got_allowed_signers == NULL)
664 got_allowed_signers = got_gotconfig_get_allowed_signers_file(
667 if (got_allowed_signers) {
668 *allowed_signers = strdup(got_allowed_signers);
669 if (*allowed_signers == NULL)
670 return got_error_from_errno("strdup");
675 static const struct got_error *
676 get_revoked_signers(char **revoked_signers, struct got_repository *repo,
677 struct got_worktree *worktree)
679 const char *got_revoked_signers = NULL;
680 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
682 *revoked_signers = NULL;
685 worktree_conf = got_worktree_get_gotconfig(worktree);
686 repo_conf = got_repo_get_gotconfig(repo);
689 * Priority of potential author information sources, from most
690 * significant to least significant:
691 * 1) work tree's .got/got.conf file
692 * 2) repository's got.conf file
696 got_revoked_signers = got_gotconfig_get_revoked_signers_file(
698 if (got_revoked_signers == NULL)
699 got_revoked_signers = got_gotconfig_get_revoked_signers_file(
702 if (got_revoked_signers) {
703 *revoked_signers = strdup(got_revoked_signers);
704 if (*revoked_signers == NULL)
705 return got_error_from_errno("strdup");
711 get_signer_id(struct got_repository *repo, struct got_worktree *worktree)
713 const char *got_signer_id = NULL;
714 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
717 worktree_conf = got_worktree_get_gotconfig(worktree);
718 repo_conf = got_repo_get_gotconfig(repo);
721 * Priority of potential author information sources, from most
722 * significant to least significant:
723 * 1) work tree's .got/got.conf file
724 * 2) repository's got.conf file
728 got_signer_id = got_gotconfig_get_signer_id(worktree_conf);
729 if (got_signer_id == NULL)
730 got_signer_id = got_gotconfig_get_signer_id(repo_conf);
732 return got_signer_id;
735 static const struct got_error *
736 get_gitconfig_path(char **gitconfig_path)
738 const char *homedir = getenv("HOME");
740 *gitconfig_path = NULL;
742 if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
743 return got_error_from_errno("asprintf");
749 static const struct got_error *
750 cmd_import(int argc, char *argv[])
752 const struct got_error *error = NULL;
753 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
754 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
755 const char *branch_name = NULL;
756 char *id_str = NULL, *logmsg_path = NULL;
757 char refname[PATH_MAX] = "refs/heads/";
758 struct got_repository *repo = NULL;
759 struct got_reference *branch_ref = NULL, *head_ref = NULL;
760 struct got_object_id *new_commit_id = NULL;
762 struct got_pathlist_head ignores;
763 struct got_pathlist_entry *pe;
764 int preserve_logmsg = 0;
765 int *pack_fds = NULL;
767 TAILQ_INIT(&ignores);
770 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
776 while ((ch = getopt(argc, argv, "b:I:m:r:")) != -1) {
779 branch_name = optarg;
782 if (optarg[0] == '\0')
784 error = got_pathlist_insert(&pe, &ignores, optarg,
790 logmsg = strdup(optarg);
791 if (logmsg == NULL) {
792 error = got_error_from_errno("strdup");
797 repo_path = realpath(optarg, NULL);
798 if (repo_path == NULL) {
799 error = got_error_from_errno2("realpath",
816 if (repo_path == NULL) {
817 repo_path = getcwd(NULL, 0);
818 if (repo_path == NULL)
819 return got_error_from_errno("getcwd");
821 got_path_strip_trailing_slashes(repo_path);
822 error = get_gitconfig_path(&gitconfig_path);
825 error = got_repo_pack_fds_open(&pack_fds);
828 error = got_repo_open(&repo, repo_path, gitconfig_path, pack_fds);
832 error = get_author(&author, repo, NULL);
837 * Don't let the user create a branch name with a leading '-'.
838 * While technically a valid reference name, this case is usually
839 * an unintended typo.
841 if (branch_name && branch_name[0] == '-')
842 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
844 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
845 if (error && error->code != GOT_ERR_NOT_REF)
849 n = strlcat(refname, branch_name, sizeof(refname));
850 else if (head_ref && got_ref_is_symbolic(head_ref))
851 n = strlcpy(refname, got_ref_get_symref_target(head_ref),
854 n = strlcat(refname, "main", sizeof(refname));
855 if (n >= sizeof(refname)) {
856 error = got_error(GOT_ERR_NO_SPACE);
860 error = got_ref_open(&branch_ref, repo, refname, 0);
862 if (error->code != GOT_ERR_NOT_REF)
865 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
866 "import target branch already exists");
870 path_dir = realpath(argv[0], NULL);
871 if (path_dir == NULL) {
872 error = got_error_from_errno2("realpath", argv[0]);
875 got_path_strip_trailing_slashes(path_dir);
878 * unveil(2) traverses exec(2); if an editor is used we have
879 * to apply unveil after the log message has been written.
881 if (logmsg == NULL || *logmsg == '\0') {
882 error = get_editor(&editor);
886 error = collect_import_msg(&logmsg, &logmsg_path, editor,
889 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
896 if (unveil(path_dir, "r") != 0) {
897 error = got_error_from_errno2("unveil", path_dir);
903 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
910 error = got_repo_import(&new_commit_id, path_dir, logmsg,
911 author, &ignores, repo, import_progress, NULL);
918 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
925 error = got_ref_write(branch_ref, repo);
932 error = got_object_id_str(&id_str, new_commit_id);
939 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
941 if (error->code != GOT_ERR_NOT_REF) {
947 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
955 error = got_ref_write(head_ref, repo);
963 printf("Created branch %s with commit %s\n",
964 got_ref_get_name(branch_ref), id_str);
967 const struct got_error *pack_err =
968 got_repo_pack_fds_close(pack_fds);
973 const struct got_error *close_err = got_repo_close(repo);
977 if (preserve_logmsg) {
978 fprintf(stderr, "%s: log message preserved in %s\n",
979 getprogname(), logmsg_path);
980 } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
981 error = got_error_from_errno2("unlink", logmsg_path);
989 free(gitconfig_path);
991 got_ref_close(branch_ref);
993 got_ref_close(head_ref);
1000 fprintf(stderr, "usage: %s clone [-almqv] [-b branch] [-R reference] "
1001 "repository-URL [directory]\n", getprogname());
1005 struct got_fetch_progress_arg {
1006 char last_scaled_size[FMT_SCALED_STRSIZE];
1008 int last_p_resolved;
1011 struct got_repository *repo;
1014 int configs_created;
1016 struct got_pathlist_head *symrefs;
1017 struct got_pathlist_head *wanted_branches;
1018 struct got_pathlist_head *wanted_refs;
1022 const char *remote_repo_path;
1023 const char *git_url;
1024 int fetch_all_branches;
1025 int mirror_references;
1029 /* XXX forward declaration */
1030 static const struct got_error *
1031 create_config_files(const char *proto, const char *host, const char *port,
1032 const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1033 int mirror_references, struct got_pathlist_head *symrefs,
1034 struct got_pathlist_head *wanted_branches,
1035 struct got_pathlist_head *wanted_refs, struct got_repository *repo);
1037 static const struct got_error *
1038 fetch_progress(void *arg, const char *message, off_t packfile_size,
1039 int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
1041 const struct got_error *err = NULL;
1042 struct got_fetch_progress_arg *a = arg;
1043 char scaled_size[FMT_SCALED_STRSIZE];
1044 int p_indexed, p_resolved;
1045 int print_size = 0, print_indexed = 0, print_resolved = 0;
1048 * In order to allow a failed clone to be resumed with 'got fetch'
1049 * we try to create configuration files as soon as possible.
1050 * Once the server has sent information about its default branch
1051 * we have all required information.
1053 if (a->create_configs && !a->configs_created &&
1054 !TAILQ_EMPTY(a->config_info.symrefs)) {
1055 err = create_config_files(a->config_info.proto,
1056 a->config_info.host, a->config_info.port,
1057 a->config_info.remote_repo_path,
1058 a->config_info.git_url,
1059 a->config_info.fetch_all_branches,
1060 a->config_info.mirror_references,
1061 a->config_info.symrefs,
1062 a->config_info.wanted_branches,
1063 a->config_info.wanted_refs, a->repo);
1066 a->configs_created = 1;
1069 if (a->verbosity < 0)
1072 if (message && message[0] != '\0') {
1073 printf("\rserver: %s", message);
1078 if (packfile_size > 0 || nobj_indexed > 0) {
1079 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
1080 (a->last_scaled_size[0] == '\0' ||
1081 strcmp(scaled_size, a->last_scaled_size)) != 0) {
1083 if (strlcpy(a->last_scaled_size, scaled_size,
1084 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
1085 return got_error(GOT_ERR_NO_SPACE);
1087 if (nobj_indexed > 0) {
1088 p_indexed = (nobj_indexed * 100) / nobj_total;
1089 if (p_indexed != a->last_p_indexed) {
1090 a->last_p_indexed = p_indexed;
1095 if (nobj_resolved > 0) {
1096 p_resolved = (nobj_resolved * 100) /
1097 (nobj_total - nobj_loose);
1098 if (p_resolved != a->last_p_resolved) {
1099 a->last_p_resolved = p_resolved;
1107 if (print_size || print_indexed || print_resolved)
1110 printf("%*s fetched", FMT_SCALED_STRSIZE - 2, scaled_size);
1112 printf("; indexing %d%%", p_indexed);
1114 printf("; resolving deltas %d%%", p_resolved);
1115 if (print_size || print_indexed || print_resolved)
1121 static const struct got_error *
1122 create_symref(const char *refname, struct got_reference *target_ref,
1123 int verbosity, struct got_repository *repo)
1125 const struct got_error *err;
1126 struct got_reference *head_symref;
1128 err = got_ref_alloc_symref(&head_symref, refname, target_ref);
1132 err = got_ref_write(head_symref, repo);
1133 if (err == NULL && verbosity > 0) {
1134 printf("Created reference %s: %s\n", GOT_REF_HEAD,
1135 got_ref_get_name(target_ref));
1137 got_ref_close(head_symref);
1141 static const struct got_error *
1142 list_remote_refs(struct got_pathlist_head *symrefs,
1143 struct got_pathlist_head *refs)
1145 const struct got_error *err;
1146 struct got_pathlist_entry *pe;
1148 TAILQ_FOREACH(pe, symrefs, entry) {
1149 const char *refname = pe->path;
1150 const char *targetref = pe->data;
1152 printf("%s: %s\n", refname, targetref);
1155 TAILQ_FOREACH(pe, refs, entry) {
1156 const char *refname = pe->path;
1157 struct got_object_id *id = pe->data;
1160 err = got_object_id_str(&id_str, id);
1163 printf("%s: %s\n", refname, id_str);
1170 static const struct got_error *
1171 create_ref(const char *refname, struct got_object_id *id,
1172 int verbosity, struct got_repository *repo)
1174 const struct got_error *err = NULL;
1175 struct got_reference *ref;
1178 err = got_object_id_str(&id_str, id);
1182 err = got_ref_alloc(&ref, refname, id);
1186 err = got_ref_write(ref, repo);
1189 if (err == NULL && verbosity >= 0)
1190 printf("Created reference %s: %s\n", refname, id_str);
1197 match_wanted_ref(const char *refname, const char *wanted_ref)
1199 if (strncmp(refname, "refs/", 5) != 0)
1204 * Prevent fetching of references that won't make any
1205 * sense outside of the remote repository's context.
1207 if (strncmp(refname, "got/", 4) == 0)
1209 if (strncmp(refname, "remotes/", 8) == 0)
1212 if (strncmp(wanted_ref, "refs/", 5) == 0)
1215 /* Allow prefix match. */
1216 if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
1219 /* Allow exact match. */
1220 return (strcmp(refname, wanted_ref) == 0);
1224 is_wanted_ref(struct got_pathlist_head *wanted_refs, const char *refname)
1226 struct got_pathlist_entry *pe;
1228 TAILQ_FOREACH(pe, wanted_refs, entry) {
1229 if (match_wanted_ref(refname, pe->path))
1236 static const struct got_error *
1237 create_wanted_ref(const char *refname, struct got_object_id *id,
1238 const char *remote_repo_name, int verbosity, struct got_repository *repo)
1240 const struct got_error *err;
1241 char *remote_refname;
1243 if (strncmp("refs/", refname, 5) == 0)
1246 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1247 remote_repo_name, refname) == -1)
1248 return got_error_from_errno("asprintf");
1250 err = create_ref(remote_refname, id, verbosity, repo);
1251 free(remote_refname);
1255 static const struct got_error *
1256 create_gotconfig(const char *proto, const char *host, const char *port,
1257 const char *remote_repo_path, const char *default_branch,
1258 int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1259 struct got_pathlist_head *wanted_refs, int mirror_references,
1260 struct got_repository *repo)
1262 const struct got_error *err = NULL;
1263 char *gotconfig_path = NULL;
1264 char *gotconfig = NULL;
1265 FILE *gotconfig_file = NULL;
1266 const char *branchname = NULL;
1267 char *branches = NULL, *refs = NULL;
1270 if (!fetch_all_branches && !TAILQ_EMPTY(wanted_branches)) {
1271 struct got_pathlist_entry *pe;
1272 TAILQ_FOREACH(pe, wanted_branches, entry) {
1274 branchname = pe->path;
1275 if (strncmp(branchname, "refs/heads/", 11) == 0)
1277 if (asprintf(&s, "%s\"%s\" ",
1278 branches ? branches : "", branchname) == -1) {
1279 err = got_error_from_errno("asprintf");
1285 } else if (!fetch_all_branches && default_branch) {
1286 branchname = default_branch;
1287 if (strncmp(branchname, "refs/heads/", 11) == 0)
1289 if (asprintf(&branches, "\"%s\" ", branchname) == -1) {
1290 err = got_error_from_errno("asprintf");
1294 if (!TAILQ_EMPTY(wanted_refs)) {
1295 struct got_pathlist_entry *pe;
1296 TAILQ_FOREACH(pe, wanted_refs, entry) {
1298 const char *refname = pe->path;
1299 if (strncmp(refname, "refs/", 5) == 0)
1301 if (asprintf(&s, "%s\"%s\" ",
1302 refs ? refs : "", refname) == -1) {
1303 err = got_error_from_errno("asprintf");
1311 /* Create got.conf(5). */
1312 gotconfig_path = got_repo_get_path_gotconfig(repo);
1313 if (gotconfig_path == NULL) {
1314 err = got_error_from_errno("got_repo_get_path_gotconfig");
1317 gotconfig_file = fopen(gotconfig_path, "ae");
1318 if (gotconfig_file == NULL) {
1319 err = got_error_from_errno2("fopen", gotconfig_path);
1322 if (asprintf(&gotconfig,
1327 "\trepository \"%s\"\n"
1333 GOT_FETCH_DEFAULT_REMOTE_NAME, host, proto,
1334 port ? "\tport " : "", port ? port : "", port ? "\n" : "",
1335 remote_repo_path, branches ? "\tbranch { " : "",
1336 branches ? branches : "", branches ? "}\n" : "",
1337 refs ? "\treference { " : "", refs ? refs : "", refs ? "}\n" : "",
1338 mirror_references ? "\tmirror_references yes\n" : "",
1339 fetch_all_branches ? "\tfetch_all_branches yes\n" : "") == -1) {
1340 err = got_error_from_errno("asprintf");
1343 n = fwrite(gotconfig, 1, strlen(gotconfig), gotconfig_file);
1344 if (n != strlen(gotconfig)) {
1345 err = got_ferror(gotconfig_file, GOT_ERR_IO);
1350 if (gotconfig_file && fclose(gotconfig_file) == EOF && err == NULL)
1351 err = got_error_from_errno2("fclose", gotconfig_path);
1352 free(gotconfig_path);
1357 static const struct got_error *
1358 create_gitconfig(const char *git_url, const char *default_branch,
1359 int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1360 struct got_pathlist_head *wanted_refs, int mirror_references,
1361 struct got_repository *repo)
1363 const struct got_error *err = NULL;
1364 char *gitconfig_path = NULL;
1365 char *gitconfig = NULL;
1366 FILE *gitconfig_file = NULL;
1367 char *branches = NULL, *refs = NULL;
1368 const char *branchname;
1371 /* Create a config file Git can understand. */
1372 gitconfig_path = got_repo_get_path_gitconfig(repo);
1373 if (gitconfig_path == NULL) {
1374 err = got_error_from_errno("got_repo_get_path_gitconfig");
1377 gitconfig_file = fopen(gitconfig_path, "ae");
1378 if (gitconfig_file == NULL) {
1379 err = got_error_from_errno2("fopen", gitconfig_path);
1382 if (fetch_all_branches) {
1383 if (mirror_references) {
1384 if (asprintf(&branches,
1385 "\tfetch = refs/heads/*:refs/heads/*\n") == -1) {
1386 err = got_error_from_errno("asprintf");
1389 } else if (asprintf(&branches,
1390 "\tfetch = refs/heads/*:refs/remotes/%s/*\n",
1391 GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1392 err = got_error_from_errno("asprintf");
1395 } else if (!TAILQ_EMPTY(wanted_branches)) {
1396 struct got_pathlist_entry *pe;
1397 TAILQ_FOREACH(pe, wanted_branches, entry) {
1399 branchname = pe->path;
1400 if (strncmp(branchname, "refs/heads/", 11) == 0)
1402 if (mirror_references) {
1404 "%s\tfetch = refs/heads/%s:refs/heads/%s\n",
1405 branches ? branches : "",
1406 branchname, branchname) == -1) {
1407 err = got_error_from_errno("asprintf");
1410 } else if (asprintf(&s,
1411 "%s\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1412 branches ? branches : "",
1413 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1414 branchname) == -1) {
1415 err = got_error_from_errno("asprintf");
1423 * If the server specified a default branch, use just that one.
1424 * Otherwise fall back to fetching all branches on next fetch.
1426 if (default_branch) {
1427 branchname = default_branch;
1428 if (strncmp(branchname, "refs/heads/", 11) == 0)
1431 branchname = "*"; /* fall back to all branches */
1432 if (mirror_references) {
1433 if (asprintf(&branches,
1434 "\tfetch = refs/heads/%s:refs/heads/%s\n",
1435 branchname, branchname) == -1) {
1436 err = got_error_from_errno("asprintf");
1439 } else if (asprintf(&branches,
1440 "\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1441 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1442 branchname) == -1) {
1443 err = got_error_from_errno("asprintf");
1447 if (!TAILQ_EMPTY(wanted_refs)) {
1448 struct got_pathlist_entry *pe;
1449 TAILQ_FOREACH(pe, wanted_refs, entry) {
1451 const char *refname = pe->path;
1452 if (strncmp(refname, "refs/", 5) == 0)
1454 if (mirror_references) {
1456 "%s\tfetch = refs/%s:refs/%s\n",
1457 refs ? refs : "", refname, refname) == -1) {
1458 err = got_error_from_errno("asprintf");
1461 } else if (asprintf(&s,
1462 "%s\tfetch = refs/%s:refs/remotes/%s/%s\n",
1464 refname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1466 err = got_error_from_errno("asprintf");
1474 if (asprintf(&gitconfig,
1479 "\tfetch = refs/tags/*:refs/tags/*\n",
1480 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url, branches ? branches : "",
1481 refs ? refs : "") == -1) {
1482 err = got_error_from_errno("asprintf");
1485 n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1486 if (n != strlen(gitconfig)) {
1487 err = got_ferror(gitconfig_file, GOT_ERR_IO);
1491 if (gitconfig_file && fclose(gitconfig_file) == EOF && err == NULL)
1492 err = got_error_from_errno2("fclose", gitconfig_path);
1493 free(gitconfig_path);
1498 static const struct got_error *
1499 create_config_files(const char *proto, const char *host, const char *port,
1500 const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1501 int mirror_references, struct got_pathlist_head *symrefs,
1502 struct got_pathlist_head *wanted_branches,
1503 struct got_pathlist_head *wanted_refs, struct got_repository *repo)
1505 const struct got_error *err = NULL;
1506 const char *default_branch = NULL;
1507 struct got_pathlist_entry *pe;
1510 * If we asked for a set of wanted branches then use the first
1513 if (!TAILQ_EMPTY(wanted_branches)) {
1514 pe = TAILQ_FIRST(wanted_branches);
1515 default_branch = pe->path;
1517 /* First HEAD ref listed by server is the default branch. */
1518 TAILQ_FOREACH(pe, symrefs, entry) {
1519 const char *refname = pe->path;
1520 const char *target = pe->data;
1522 if (strcmp(refname, GOT_REF_HEAD) != 0)
1525 default_branch = target;
1530 /* Create got.conf(5). */
1531 err = create_gotconfig(proto, host, port, remote_repo_path,
1532 default_branch, fetch_all_branches, wanted_branches,
1533 wanted_refs, mirror_references, repo);
1537 /* Create a config file Git can understand. */
1538 return create_gitconfig(git_url, default_branch, fetch_all_branches,
1539 wanted_branches, wanted_refs, mirror_references, repo);
1542 static const struct got_error *
1543 cmd_clone(int argc, char *argv[])
1545 const struct got_error *error = NULL;
1546 const char *uri, *dirname;
1547 char *proto, *host, *port, *repo_name, *server_path;
1548 char *default_destdir = NULL, *id_str = NULL;
1549 const char *repo_path;
1550 struct got_repository *repo = NULL;
1551 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1552 struct got_pathlist_entry *pe;
1553 struct got_object_id *pack_hash = NULL;
1554 int ch, fetchfd = -1, fetchstatus;
1555 pid_t fetchpid = -1;
1556 struct got_fetch_progress_arg fpa;
1557 char *git_url = NULL;
1558 int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
1559 int bflag = 0, list_refs_only = 0;
1560 int *pack_fds = NULL;
1563 TAILQ_INIT(&symrefs);
1564 TAILQ_INIT(&wanted_branches);
1565 TAILQ_INIT(&wanted_refs);
1567 while ((ch = getopt(argc, argv, "ab:lmqR:v")) != -1) {
1570 fetch_all_branches = 1;
1573 error = got_pathlist_append(&wanted_branches,
1583 mirror_references = 1;
1589 error = got_pathlist_append(&wanted_refs,
1597 else if (verbosity < 3)
1608 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1609 option_conflict('a', 'b');
1610 if (list_refs_only) {
1611 if (!TAILQ_EMPTY(&wanted_branches))
1612 option_conflict('l', 'b');
1613 if (fetch_all_branches)
1614 option_conflict('l', 'a');
1615 if (mirror_references)
1616 option_conflict('l', 'm');
1617 if (!TAILQ_EMPTY(&wanted_refs))
1618 option_conflict('l', 'R');
1630 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
1635 if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1636 host, port ? ":" : "", port ? port : "",
1637 server_path[0] != '/' ? "/" : "", server_path) == -1) {
1638 error = got_error_from_errno("asprintf");
1642 if (strcmp(proto, "git") == 0) {
1644 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1645 "sendfd dns inet unveil", NULL) == -1)
1648 } else if (strcmp(proto, "git+ssh") == 0 ||
1649 strcmp(proto, "ssh") == 0) {
1651 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1652 "sendfd unveil", NULL) == -1)
1655 } else if (strcmp(proto, "http") == 0 ||
1656 strcmp(proto, "git+http") == 0) {
1657 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1660 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1663 if (dirname == NULL) {
1664 if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1665 error = got_error_from_errno("asprintf");
1668 repo_path = default_destdir;
1670 repo_path = dirname;
1672 if (!list_refs_only) {
1673 error = got_path_mkdir(repo_path);
1675 (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1676 !(error->code == GOT_ERR_ERRNO && errno == EEXIST)))
1678 if (!got_path_dir_is_empty(repo_path)) {
1679 error = got_error_path(repo_path,
1680 GOT_ERR_DIR_NOT_EMPTY);
1685 error = got_dial_apply_unveil(proto);
1689 error = apply_unveil(repo_path, 0, NULL);
1694 printf("Connecting to %s\n", git_url);
1696 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1697 server_path, verbosity);
1701 if (!list_refs_only) {
1702 error = got_repo_init(repo_path, NULL);
1705 error = got_repo_pack_fds_open(&pack_fds);
1708 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
1713 fpa.last_scaled_size[0] = '\0';
1714 fpa.last_p_indexed = -1;
1715 fpa.last_p_resolved = -1;
1716 fpa.verbosity = verbosity;
1717 fpa.create_configs = 1;
1718 fpa.configs_created = 0;
1720 fpa.config_info.symrefs = &symrefs;
1721 fpa.config_info.wanted_branches = &wanted_branches;
1722 fpa.config_info.wanted_refs = &wanted_refs;
1723 fpa.config_info.proto = proto;
1724 fpa.config_info.host = host;
1725 fpa.config_info.port = port;
1726 fpa.config_info.remote_repo_path = server_path;
1727 fpa.config_info.git_url = git_url;
1728 fpa.config_info.fetch_all_branches = fetch_all_branches;
1729 fpa.config_info.mirror_references = mirror_references;
1730 error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1731 GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1732 fetch_all_branches, &wanted_branches, &wanted_refs,
1733 list_refs_only, verbosity, fetchfd, repo, NULL, NULL, bflag,
1734 fetch_progress, &fpa);
1738 if (list_refs_only) {
1739 error = list_remote_refs(&symrefs, &refs);
1743 if (pack_hash == NULL) {
1744 error = got_error_fmt(GOT_ERR_FETCH_FAILED, "%s",
1745 "server sent an empty pack file");
1748 error = got_object_id_str(&id_str, pack_hash);
1752 printf("\nFetched %s.pack\n", id_str);
1755 /* Set up references provided with the pack file. */
1756 TAILQ_FOREACH(pe, &refs, entry) {
1757 const char *refname = pe->path;
1758 struct got_object_id *id = pe->data;
1759 char *remote_refname;
1761 if (is_wanted_ref(&wanted_refs, refname) &&
1762 !mirror_references) {
1763 error = create_wanted_ref(refname, id,
1764 GOT_FETCH_DEFAULT_REMOTE_NAME,
1765 verbosity - 1, repo);
1771 error = create_ref(refname, id, verbosity - 1, repo);
1775 if (mirror_references)
1778 if (strncmp("refs/heads/", refname, 11) != 0)
1781 if (asprintf(&remote_refname,
1782 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1783 refname + 11) == -1) {
1784 error = got_error_from_errno("asprintf");
1787 error = create_ref(remote_refname, id, verbosity - 1, repo);
1788 free(remote_refname);
1793 /* Set the HEAD reference if the server provided one. */
1794 TAILQ_FOREACH(pe, &symrefs, entry) {
1795 struct got_reference *target_ref;
1796 const char *refname = pe->path;
1797 const char *target = pe->data;
1798 char *remote_refname = NULL, *remote_target = NULL;
1800 if (strcmp(refname, GOT_REF_HEAD) != 0)
1803 error = got_ref_open(&target_ref, repo, target, 0);
1805 if (error->code == GOT_ERR_NOT_REF) {
1812 error = create_symref(refname, target_ref, verbosity, repo);
1813 got_ref_close(target_ref);
1817 if (mirror_references)
1820 if (strncmp("refs/heads/", target, 11) != 0)
1823 if (asprintf(&remote_refname,
1824 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1826 error = got_error_from_errno("asprintf");
1829 if (asprintf(&remote_target,
1830 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1831 target + 11) == -1) {
1832 error = got_error_from_errno("asprintf");
1833 free(remote_refname);
1836 error = got_ref_open(&target_ref, repo, remote_target, 0);
1838 free(remote_refname);
1839 free(remote_target);
1840 if (error->code == GOT_ERR_NOT_REF) {
1846 error = create_symref(remote_refname, target_ref,
1847 verbosity - 1, repo);
1848 free(remote_refname);
1849 free(remote_target);
1850 got_ref_close(target_ref);
1856 * We failed to set the HEAD reference. If we asked for
1857 * a set of wanted branches use the first of one of those
1858 * which could be fetched instead.
1860 TAILQ_FOREACH(pe, &wanted_branches, entry) {
1861 const char *target = pe->path;
1862 struct got_reference *target_ref;
1864 error = got_ref_open(&target_ref, repo, target, 0);
1866 if (error->code == GOT_ERR_NOT_REF) {
1873 error = create_symref(GOT_REF_HEAD, target_ref,
1875 got_ref_close(target_ref);
1881 if (!fpa.configs_created && pe != NULL) {
1882 error = create_config_files(fpa.config_info.proto,
1883 fpa.config_info.host, fpa.config_info.port,
1884 fpa.config_info.remote_repo_path,
1885 fpa.config_info.git_url,
1886 fpa.config_info.fetch_all_branches,
1887 fpa.config_info.mirror_references,
1888 fpa.config_info.symrefs,
1889 fpa.config_info.wanted_branches,
1890 fpa.config_info.wanted_refs, fpa.repo);
1897 printf("Created %s repository '%s'\n",
1898 mirror_references ? "mirrored" : "cloned", repo_path);
1901 const struct got_error *pack_err =
1902 got_repo_pack_fds_close(pack_fds);
1907 if (kill(fetchpid, SIGTERM) == -1)
1908 error = got_error_from_errno("kill");
1909 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1910 error = got_error_from_errno("waitpid");
1912 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1913 error = got_error_from_errno("close");
1915 const struct got_error *close_err = got_repo_close(repo);
1919 got_pathlist_free(&refs, GOT_PATHLIST_FREE_ALL);
1920 got_pathlist_free(&symrefs, GOT_PATHLIST_FREE_ALL);
1921 got_pathlist_free(&wanted_branches, GOT_PATHLIST_FREE_NONE);
1922 got_pathlist_free(&wanted_refs, GOT_PATHLIST_FREE_NONE);
1929 free(default_destdir);
1934 static const struct got_error *
1935 update_ref(struct got_reference *ref, struct got_object_id *new_id,
1936 int replace_tags, int verbosity, struct got_repository *repo)
1938 const struct got_error *err = NULL;
1939 char *new_id_str = NULL;
1940 struct got_object_id *old_id = NULL;
1942 err = got_object_id_str(&new_id_str, new_id);
1946 if (!replace_tags &&
1947 strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1948 err = got_ref_resolve(&old_id, repo, ref);
1951 if (got_object_id_cmp(old_id, new_id) == 0)
1953 if (verbosity >= 0) {
1954 printf("Rejecting update of existing tag %s: %s\n",
1955 got_ref_get_name(ref), new_id_str);
1960 if (got_ref_is_symbolic(ref)) {
1961 if (verbosity >= 0) {
1962 printf("Replacing reference %s: %s\n",
1963 got_ref_get_name(ref),
1964 got_ref_get_symref_target(ref));
1966 err = got_ref_change_symref_to_ref(ref, new_id);
1969 err = got_ref_write(ref, repo);
1973 err = got_ref_resolve(&old_id, repo, ref);
1976 if (got_object_id_cmp(old_id, new_id) == 0)
1979 err = got_ref_change_ref(ref, new_id);
1982 err = got_ref_write(ref, repo);
1988 printf("Updated %s: %s\n", got_ref_get_name(ref),
1996 static const struct got_error *
1997 update_symref(const char *refname, struct got_reference *target_ref,
1998 int verbosity, struct got_repository *repo)
2000 const struct got_error *err = NULL, *unlock_err;
2001 struct got_reference *symref;
2002 int symref_is_locked = 0;
2004 err = got_ref_open(&symref, repo, refname, 1);
2006 if (err->code != GOT_ERR_NOT_REF)
2008 err = got_ref_alloc_symref(&symref, refname, target_ref);
2012 err = got_ref_write(symref, repo);
2017 printf("Created reference %s: %s\n",
2018 got_ref_get_name(symref),
2019 got_ref_get_symref_target(symref));
2021 symref_is_locked = 1;
2023 if (strcmp(got_ref_get_symref_target(symref),
2024 got_ref_get_name(target_ref)) == 0)
2027 err = got_ref_change_symref(symref,
2028 got_ref_get_name(target_ref));
2032 err = got_ref_write(symref, repo);
2037 printf("Updated %s: %s\n", got_ref_get_name(symref),
2038 got_ref_get_symref_target(symref));
2042 if (symref_is_locked) {
2043 unlock_err = got_ref_unlock(symref);
2044 if (unlock_err && err == NULL)
2047 got_ref_close(symref);
2054 fprintf(stderr, "usage: %s fetch [-adlqtvX] [-b branch] "
2055 "[-R reference] [-r repository-path] [remote-repository]\n",
2060 static const struct got_error *
2061 delete_missing_ref(struct got_reference *ref,
2062 int verbosity, struct got_repository *repo)
2064 const struct got_error *err = NULL;
2065 struct got_object_id *id = NULL;
2066 char *id_str = NULL;
2068 if (got_ref_is_symbolic(ref)) {
2069 err = got_ref_delete(ref, repo);
2072 if (verbosity >= 0) {
2073 printf("Deleted %s: %s\n",
2074 got_ref_get_name(ref),
2075 got_ref_get_symref_target(ref));
2078 err = got_ref_resolve(&id, repo, ref);
2081 err = got_object_id_str(&id_str, id);
2085 err = got_ref_delete(ref, repo);
2088 if (verbosity >= 0) {
2089 printf("Deleted %s: %s\n",
2090 got_ref_get_name(ref), id_str);
2099 static const struct got_error *
2100 delete_missing_refs(struct got_pathlist_head *their_refs,
2101 struct got_pathlist_head *their_symrefs,
2102 const struct got_remote_repo *remote,
2103 int verbosity, struct got_repository *repo)
2105 const struct got_error *err = NULL, *unlock_err;
2106 struct got_reflist_head my_refs;
2107 struct got_reflist_entry *re;
2108 struct got_pathlist_entry *pe;
2109 char *remote_namespace = NULL;
2110 char *local_refname = NULL;
2112 TAILQ_INIT(&my_refs);
2114 if (asprintf(&remote_namespace, "refs/remotes/%s/", remote->name)
2116 return got_error_from_errno("asprintf");
2118 err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
2122 TAILQ_FOREACH(re, &my_refs, entry) {
2123 const char *refname = got_ref_get_name(re->ref);
2124 const char *their_refname;
2126 if (remote->mirror_references) {
2127 their_refname = refname;
2129 if (strncmp(refname, remote_namespace,
2130 strlen(remote_namespace)) == 0) {
2131 if (strcmp(refname + strlen(remote_namespace),
2134 if (asprintf(&local_refname, "refs/heads/%s",
2135 refname + strlen(remote_namespace)) == -1) {
2136 err = got_error_from_errno("asprintf");
2139 } else if (strncmp(refname, "refs/tags/", 10) != 0)
2142 their_refname = local_refname;
2145 TAILQ_FOREACH(pe, their_refs, entry) {
2146 if (strcmp(their_refname, pe->path) == 0)
2152 TAILQ_FOREACH(pe, their_symrefs, entry) {
2153 if (strcmp(their_refname, pe->path) == 0)
2159 err = delete_missing_ref(re->ref, verbosity, repo);
2163 if (local_refname) {
2164 struct got_reference *ref;
2165 err = got_ref_open(&ref, repo, local_refname, 1);
2167 if (err->code != GOT_ERR_NOT_REF)
2169 free(local_refname);
2170 local_refname = NULL;
2173 err = delete_missing_ref(ref, verbosity, repo);
2176 unlock_err = got_ref_unlock(ref);
2178 if (unlock_err && err == NULL) {
2183 free(local_refname);
2184 local_refname = NULL;
2188 got_ref_list_free(&my_refs);
2189 free(remote_namespace);
2190 free(local_refname);
2194 static const struct got_error *
2195 update_wanted_ref(const char *refname, struct got_object_id *id,
2196 const char *remote_repo_name, int verbosity, struct got_repository *repo)
2198 const struct got_error *err, *unlock_err;
2199 char *remote_refname;
2200 struct got_reference *ref;
2202 if (strncmp("refs/", refname, 5) == 0)
2205 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2206 remote_repo_name, refname) == -1)
2207 return got_error_from_errno("asprintf");
2209 err = got_ref_open(&ref, repo, remote_refname, 1);
2211 if (err->code != GOT_ERR_NOT_REF)
2213 err = create_ref(remote_refname, id, verbosity, repo);
2215 err = update_ref(ref, id, 0, verbosity, repo);
2216 unlock_err = got_ref_unlock(ref);
2217 if (unlock_err && err == NULL)
2222 free(remote_refname);
2226 static const struct got_error *
2227 delete_ref(struct got_repository *repo, struct got_reference *ref)
2229 const struct got_error *err = NULL;
2230 struct got_object_id *id = NULL;
2231 char *id_str = NULL;
2234 if (got_ref_is_symbolic(ref)) {
2235 target = got_ref_get_symref_target(ref);
2237 err = got_ref_resolve(&id, repo, ref);
2240 err = got_object_id_str(&id_str, id);
2246 err = got_ref_delete(ref, repo);
2250 printf("Deleted %s: %s\n", got_ref_get_name(ref), target);
2257 static const struct got_error *
2258 delete_refs_for_remote(struct got_repository *repo, const char *remote_name)
2260 const struct got_error *err = NULL;
2261 struct got_reflist_head refs;
2262 struct got_reflist_entry *re;
2267 if (asprintf(&prefix, "refs/remotes/%s", remote_name) == -1) {
2268 err = got_error_from_errno("asprintf");
2271 err = got_ref_list(&refs, repo, prefix, got_ref_cmp_by_name, NULL);
2275 TAILQ_FOREACH(re, &refs, entry)
2276 delete_ref(repo, re->ref);
2278 got_ref_list_free(&refs);
2282 static const struct got_error *
2283 cmd_fetch(int argc, char *argv[])
2285 const struct got_error *error = NULL, *unlock_err;
2286 char *cwd = NULL, *repo_path = NULL;
2287 const char *remote_name;
2288 char *proto = NULL, *host = NULL, *port = NULL;
2289 char *repo_name = NULL, *server_path = NULL;
2290 const struct got_remote_repo *remotes, *remote = NULL;
2292 char *id_str = NULL;
2293 struct got_repository *repo = NULL;
2294 struct got_worktree *worktree = NULL;
2295 const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
2296 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
2297 struct got_pathlist_entry *pe;
2298 struct got_reflist_head remote_refs;
2299 struct got_reflist_entry *re;
2300 struct got_object_id *pack_hash = NULL;
2301 int i, ch, fetchfd = -1, fetchstatus;
2302 pid_t fetchpid = -1;
2303 struct got_fetch_progress_arg fpa;
2304 int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
2305 int delete_refs = 0, replace_tags = 0, delete_remote = 0;
2306 int *pack_fds = NULL, have_bflag = 0;
2307 const char *remote_head = NULL, *worktree_branch = NULL;
2310 TAILQ_INIT(&symrefs);
2311 TAILQ_INIT(&remote_refs);
2312 TAILQ_INIT(&wanted_branches);
2313 TAILQ_INIT(&wanted_refs);
2315 while ((ch = getopt(argc, argv, "ab:dlqR:r:tvX")) != -1) {
2318 fetch_all_branches = 1;
2321 error = got_pathlist_append(&wanted_branches,
2337 error = got_pathlist_append(&wanted_refs,
2343 repo_path = realpath(optarg, NULL);
2344 if (repo_path == NULL)
2345 return got_error_from_errno2("realpath",
2347 got_path_strip_trailing_slashes(repo_path);
2355 else if (verbosity < 3)
2369 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
2370 option_conflict('a', 'b');
2371 if (list_refs_only) {
2372 if (!TAILQ_EMPTY(&wanted_branches))
2373 option_conflict('l', 'b');
2374 if (fetch_all_branches)
2375 option_conflict('l', 'a');
2377 option_conflict('l', 'd');
2379 option_conflict('l', 'X');
2381 if (delete_remote) {
2382 if (fetch_all_branches)
2383 option_conflict('X', 'a');
2384 if (!TAILQ_EMPTY(&wanted_branches))
2385 option_conflict('X', 'b');
2387 option_conflict('X', 'd');
2389 option_conflict('X', 't');
2390 if (!TAILQ_EMPTY(&wanted_refs))
2391 option_conflict('X', 'R');
2396 errx(1, "-X option requires a remote name");
2397 remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
2398 } else if (argc == 1)
2399 remote_name = argv[0];
2403 cwd = getcwd(NULL, 0);
2405 error = got_error_from_errno("getcwd");
2409 error = got_repo_pack_fds_open(&pack_fds);
2413 if (repo_path == NULL) {
2414 error = got_worktree_open(&worktree, cwd);
2415 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2421 strdup(got_worktree_get_repo_path(worktree));
2422 if (repo_path == NULL)
2423 error = got_error_from_errno("strdup");
2427 repo_path = strdup(cwd);
2428 if (repo_path == NULL) {
2429 error = got_error_from_errno("strdup");
2435 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
2439 if (delete_remote) {
2440 error = delete_refs_for_remote(repo, remote_name);
2441 goto done; /* nothing else to do */
2445 worktree_conf = got_worktree_get_gotconfig(worktree);
2446 if (worktree_conf) {
2447 got_gotconfig_get_remotes(&nremotes, &remotes,
2449 for (i = 0; i < nremotes; i++) {
2450 if (strcmp(remotes[i].name, remote_name) == 0) {
2451 remote = &remotes[i];
2457 if (remote == NULL) {
2458 repo_conf = got_repo_get_gotconfig(repo);
2460 got_gotconfig_get_remotes(&nremotes, &remotes,
2462 for (i = 0; i < nremotes; i++) {
2463 if (strcmp(remotes[i].name, remote_name) == 0) {
2464 remote = &remotes[i];
2470 if (remote == NULL) {
2471 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
2472 for (i = 0; i < nremotes; i++) {
2473 if (strcmp(remotes[i].name, remote_name) == 0) {
2474 remote = &remotes[i];
2479 if (remote == NULL) {
2480 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
2484 if (TAILQ_EMPTY(&wanted_branches)) {
2485 if (!fetch_all_branches)
2486 fetch_all_branches = remote->fetch_all_branches;
2487 for (i = 0; i < remote->nfetch_branches; i++) {
2488 error = got_pathlist_append(&wanted_branches,
2489 remote->fetch_branches[i], NULL);
2494 if (TAILQ_EMPTY(&wanted_refs)) {
2495 for (i = 0; i < remote->nfetch_refs; i++) {
2496 error = got_pathlist_append(&wanted_refs,
2497 remote->fetch_refs[i], NULL);
2503 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
2504 &repo_name, remote->fetch_url);
2508 if (strcmp(proto, "git") == 0) {
2510 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2511 "sendfd dns inet unveil", NULL) == -1)
2514 } else if (strcmp(proto, "git+ssh") == 0 ||
2515 strcmp(proto, "ssh") == 0) {
2517 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2518 "sendfd unveil", NULL) == -1)
2521 } else if (strcmp(proto, "http") == 0 ||
2522 strcmp(proto, "git+http") == 0) {
2523 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
2526 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
2530 error = got_dial_apply_unveil(proto);
2534 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
2538 if (verbosity >= 0) {
2539 printf("Connecting to \"%s\" %s://%s%s%s%s%s\n",
2540 remote->name, proto, host,
2541 port ? ":" : "", port ? port : "",
2542 *server_path == '/' ? "" : "/", server_path);
2545 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
2546 server_path, verbosity);
2552 * If set, get this remote's HEAD ref target so
2553 * if it has changed on the server we can fetch it.
2555 error = got_ref_list(&remote_refs, repo, "refs/remotes",
2556 got_ref_cmp_by_name, repo);
2560 TAILQ_FOREACH(re, &remote_refs, entry) {
2561 const char *remote_refname, *remote_target;
2562 size_t remote_name_len;
2564 if (!got_ref_is_symbolic(re->ref))
2567 remote_name_len = strlen(remote->name);
2568 remote_refname = got_ref_get_name(re->ref);
2570 /* we only want refs/remotes/$remote->name/HEAD */
2571 if (strncmp(remote_refname + 13, remote->name,
2572 remote_name_len) != 0)
2575 if (strcmp(remote_refname + remote_name_len + 14,
2580 * Take the name itself because we already
2581 * only match with refs/heads/ in fetch_pack().
2583 remote_target = got_ref_get_symref_target(re->ref);
2584 remote_head = remote_target + remote_name_len + 14;
2589 const char *refname;
2591 refname = got_worktree_get_head_ref_name(worktree);
2592 if (strncmp(refname, "refs/heads/", 11) == 0)
2593 worktree_branch = refname;
2597 fpa.last_scaled_size[0] = '\0';
2598 fpa.last_p_indexed = -1;
2599 fpa.last_p_resolved = -1;
2600 fpa.verbosity = verbosity;
2602 fpa.create_configs = 0;
2603 fpa.configs_created = 0;
2604 memset(&fpa.config_info, 0, sizeof(fpa.config_info));
2606 error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
2607 remote->mirror_references, fetch_all_branches, &wanted_branches,
2608 &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
2609 worktree_branch, remote_head, have_bflag, fetch_progress, &fpa);
2613 if (list_refs_only) {
2614 error = list_remote_refs(&symrefs, &refs);
2618 if (pack_hash == NULL) {
2620 printf("Already up-to-date\n");
2621 } else if (verbosity >= 0) {
2622 error = got_object_id_str(&id_str, pack_hash);
2625 printf("\nFetched %s.pack\n", id_str);
2630 /* Update references provided with the pack file. */
2631 TAILQ_FOREACH(pe, &refs, entry) {
2632 const char *refname = pe->path;
2633 struct got_object_id *id = pe->data;
2634 struct got_reference *ref;
2635 char *remote_refname;
2637 if (is_wanted_ref(&wanted_refs, refname) &&
2638 !remote->mirror_references) {
2639 error = update_wanted_ref(refname, id,
2640 remote->name, verbosity, repo);
2646 if (remote->mirror_references ||
2647 strncmp("refs/tags/", refname, 10) == 0) {
2648 error = got_ref_open(&ref, repo, refname, 1);
2650 if (error->code != GOT_ERR_NOT_REF)
2652 error = create_ref(refname, id, verbosity,
2657 error = update_ref(ref, id, replace_tags,
2659 unlock_err = got_ref_unlock(ref);
2660 if (unlock_err && error == NULL)
2666 } else if (strncmp("refs/heads/", refname, 11) == 0) {
2667 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2668 remote_name, refname + 11) == -1) {
2669 error = got_error_from_errno("asprintf");
2673 error = got_ref_open(&ref, repo, remote_refname, 1);
2675 if (error->code != GOT_ERR_NOT_REF)
2677 error = create_ref(remote_refname, id,
2682 error = update_ref(ref, id, replace_tags,
2684 unlock_err = got_ref_unlock(ref);
2685 if (unlock_err && error == NULL)
2692 /* Also create a local branch if none exists yet. */
2693 error = got_ref_open(&ref, repo, refname, 1);
2695 if (error->code != GOT_ERR_NOT_REF)
2697 error = create_ref(refname, id, verbosity,
2702 unlock_err = got_ref_unlock(ref);
2703 if (unlock_err && error == NULL)
2710 error = delete_missing_refs(&refs, &symrefs, remote,
2716 if (!remote->mirror_references) {
2717 /* Update remote HEAD reference if the server provided one. */
2718 TAILQ_FOREACH(pe, &symrefs, entry) {
2719 struct got_reference *target_ref;
2720 const char *refname = pe->path;
2721 const char *target = pe->data;
2722 char *remote_refname = NULL, *remote_target = NULL;
2724 if (strcmp(refname, GOT_REF_HEAD) != 0)
2727 if (strncmp("refs/heads/", target, 11) != 0)
2730 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2731 remote->name, refname) == -1) {
2732 error = got_error_from_errno("asprintf");
2735 if (asprintf(&remote_target, "refs/remotes/%s/%s",
2736 remote->name, target + 11) == -1) {
2737 error = got_error_from_errno("asprintf");
2738 free(remote_refname);
2742 error = got_ref_open(&target_ref, repo, remote_target,
2745 free(remote_refname);
2746 free(remote_target);
2747 if (error->code == GOT_ERR_NOT_REF) {
2753 error = update_symref(remote_refname, target_ref,
2755 free(remote_refname);
2756 free(remote_target);
2757 got_ref_close(target_ref);
2764 if (kill(fetchpid, SIGTERM) == -1)
2765 error = got_error_from_errno("kill");
2766 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
2767 error = got_error_from_errno("waitpid");
2769 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
2770 error = got_error_from_errno("close");
2772 const struct got_error *close_err = got_repo_close(repo);
2777 got_worktree_close(worktree);
2779 const struct got_error *pack_err =
2780 got_repo_pack_fds_close(pack_fds);
2784 got_pathlist_free(&refs, GOT_PATHLIST_FREE_ALL);
2785 got_pathlist_free(&symrefs, GOT_PATHLIST_FREE_ALL);
2786 got_pathlist_free(&wanted_branches, GOT_PATHLIST_FREE_NONE);
2787 got_pathlist_free(&wanted_refs, GOT_PATHLIST_FREE_NONE);
2788 got_ref_list_free(&remote_refs);
2803 usage_checkout(void)
2805 fprintf(stderr, "usage: %s checkout [-Eq] [-b branch] [-c commit] "
2806 "[-p path-prefix] repository-path [work-tree-path]\n",
2812 show_worktree_base_ref_warning(void)
2814 fprintf(stderr, "%s: warning: could not create a reference "
2815 "to the work tree's base commit; the commit could be "
2816 "garbage-collected by Git or 'gotadmin cleanup'; making the "
2817 "repository writable and running 'got update' will prevent this\n",
2821 struct got_checkout_progress_arg {
2822 const char *worktree_path;
2823 int had_base_commit_ref_error;
2827 static const struct got_error *
2828 checkout_progress(void *arg, unsigned char status, const char *path)
2830 struct got_checkout_progress_arg *a = arg;
2832 /* Base commit bump happens silently. */
2833 if (status == GOT_STATUS_BUMP_BASE)
2836 if (status == GOT_STATUS_BASE_REF_ERR) {
2837 a->had_base_commit_ref_error = 1;
2841 while (path[0] == '/')
2844 if (a->verbosity >= 0)
2845 printf("%c %s/%s\n", status, a->worktree_path, path);
2850 static const struct got_error *
2851 check_cancelled(void *arg)
2853 if (sigint_received || sigpipe_received)
2854 return got_error(GOT_ERR_CANCELLED);
2858 static const struct got_error *
2859 check_linear_ancestry(struct got_object_id *commit_id,
2860 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2861 struct got_repository *repo)
2863 const struct got_error *err = NULL;
2864 struct got_object_id *yca_id;
2866 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2867 commit_id, base_commit_id, 1, repo, check_cancelled, NULL);
2872 return got_error(GOT_ERR_ANCESTRY);
2875 * Require a straight line of history between the target commit
2876 * and the work tree's base commit.
2878 * Non-linear situations such as this require a rebase:
2880 * (commit) D F (base_commit)
2888 * 'got update' only handles linear cases:
2889 * Update forwards in time: A (base/yca) - B - C - D (commit)
2890 * Update backwards in time: D (base) - C - B - A (commit/yca)
2892 if (allow_forwards_in_time_only) {
2893 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2894 return got_error(GOT_ERR_ANCESTRY);
2895 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2896 got_object_id_cmp(base_commit_id, yca_id) != 0)
2897 return got_error(GOT_ERR_ANCESTRY);
2903 static const struct got_error *
2904 check_same_branch(struct got_object_id *commit_id,
2905 struct got_reference *head_ref, struct got_repository *repo)
2907 const struct got_error *err = NULL;
2908 struct got_commit_graph *graph = NULL;
2909 struct got_object_id *head_commit_id = NULL;
2911 err = got_ref_resolve(&head_commit_id, repo, head_ref);
2915 if (got_object_id_cmp(head_commit_id, commit_id) == 0)
2918 err = got_commit_graph_open(&graph, "/", 1);
2922 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2923 check_cancelled, NULL);
2928 struct got_object_id id;
2930 err = got_commit_graph_iter_next(&id, graph, repo,
2931 check_cancelled, NULL);
2933 if (err->code == GOT_ERR_ITER_COMPLETED)
2934 err = got_error(GOT_ERR_ANCESTRY);
2938 if (got_object_id_cmp(&id, commit_id) == 0)
2943 got_commit_graph_close(graph);
2944 free(head_commit_id);
2948 static const struct got_error *
2949 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2951 static char msg[512];
2952 const char *branch_name;
2954 if (got_ref_is_symbolic(ref))
2955 branch_name = got_ref_get_symref_target(ref);
2957 branch_name = got_ref_get_name(ref);
2959 if (strncmp("refs/heads/", branch_name, 11) == 0)
2962 snprintf(msg, sizeof(msg),
2963 "target commit is not contained in branch '%s'; "
2964 "the branch to use must be specified with -b; "
2965 "if necessary a new branch can be created for "
2966 "this commit with 'got branch -c %s BRANCH_NAME'",
2967 branch_name, commit_id_str);
2969 return got_error_msg(GOT_ERR_ANCESTRY, msg);
2972 static const struct got_error *
2973 cmd_checkout(int argc, char *argv[])
2975 const struct got_error *error = NULL;
2976 struct got_repository *repo = NULL;
2977 struct got_reference *head_ref = NULL, *ref = NULL;
2978 struct got_worktree *worktree = NULL;
2979 char *repo_path = NULL;
2980 char *worktree_path = NULL;
2981 const char *path_prefix = "";
2982 const char *branch_name = GOT_REF_HEAD, *refname = NULL;
2983 char *commit_id_str = NULL;
2984 struct got_object_id *commit_id = NULL;
2986 int ch, same_path_prefix, allow_nonempty = 0, verbosity = 0;
2987 struct got_pathlist_head paths;
2988 struct got_checkout_progress_arg cpa;
2989 int *pack_fds = NULL;
2994 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2995 "unveil", NULL) == -1)
2999 while ((ch = getopt(argc, argv, "b:c:Ep:q")) != -1) {
3002 branch_name = optarg;
3005 commit_id_str = strdup(optarg);
3006 if (commit_id_str == NULL)
3007 return got_error_from_errno("strdup");
3013 path_prefix = optarg;
3028 char *base, *dotgit;
3030 repo_path = realpath(argv[0], NULL);
3031 if (repo_path == NULL)
3032 return got_error_from_errno2("realpath", argv[0]);
3033 cwd = getcwd(NULL, 0);
3035 error = got_error_from_errno("getcwd");
3042 error = got_path_basename(&base, path);
3045 dotgit = strstr(base, ".git");
3048 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
3049 error = got_error_from_errno("asprintf");
3054 } else if (argc == 2) {
3055 repo_path = realpath(argv[0], NULL);
3056 if (repo_path == NULL) {
3057 error = got_error_from_errno2("realpath", argv[0]);
3060 worktree_path = realpath(argv[1], NULL);
3061 if (worktree_path == NULL) {
3062 if (errno != ENOENT) {
3063 error = got_error_from_errno2("realpath",
3067 worktree_path = strdup(argv[1]);
3068 if (worktree_path == NULL) {
3069 error = got_error_from_errno("strdup");
3076 got_path_strip_trailing_slashes(repo_path);
3077 got_path_strip_trailing_slashes(worktree_path);
3079 error = got_repo_pack_fds_open(&pack_fds);
3083 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3087 /* Pre-create work tree path for unveil(2) */
3088 error = got_path_mkdir(worktree_path);
3090 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
3091 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
3093 if (!allow_nonempty &&
3094 !got_path_dir_is_empty(worktree_path)) {
3095 error = got_error_path(worktree_path,
3096 GOT_ERR_DIR_NOT_EMPTY);
3101 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
3105 error = got_ref_open(&head_ref, repo, branch_name, 0);
3109 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
3110 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
3113 error = got_worktree_open(&worktree, worktree_path);
3117 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
3121 if (!same_path_prefix) {
3122 error = got_error(GOT_ERR_PATH_PREFIX);
3126 if (commit_id_str) {
3127 struct got_reflist_head refs;
3129 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3133 error = got_repo_match_object_id(&commit_id, NULL,
3134 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3135 got_ref_list_free(&refs);
3138 error = check_linear_ancestry(commit_id,
3139 got_worktree_get_base_commit_id(worktree), 0, repo);
3140 if (error != NULL) {
3141 if (error->code == GOT_ERR_ANCESTRY) {
3142 error = checkout_ancestry_error(
3143 head_ref, commit_id_str);
3147 error = check_same_branch(commit_id, head_ref, repo);
3149 if (error->code == GOT_ERR_ANCESTRY) {
3150 error = checkout_ancestry_error(
3151 head_ref, commit_id_str);
3155 error = got_worktree_set_base_commit_id(worktree, repo,
3159 /* Expand potentially abbreviated commit ID string. */
3160 free(commit_id_str);
3161 error = got_object_id_str(&commit_id_str, commit_id);
3165 commit_id = got_object_id_dup(
3166 got_worktree_get_base_commit_id(worktree));
3167 if (commit_id == NULL) {
3168 error = got_error_from_errno("got_object_id_dup");
3171 error = got_object_id_str(&commit_id_str, commit_id);
3176 error = got_pathlist_append(&paths, "", NULL);
3179 cpa.worktree_path = worktree_path;
3180 cpa.had_base_commit_ref_error = 0;
3181 cpa.verbosity = verbosity;
3182 error = got_worktree_checkout_files(worktree, &paths, repo,
3183 checkout_progress, &cpa, check_cancelled, NULL);
3187 if (got_ref_is_symbolic(head_ref)) {
3188 error = got_ref_resolve_symbolic(&ref, repo, head_ref);
3191 refname = got_ref_get_name(ref);
3193 refname = got_ref_get_name(head_ref);
3194 printf("Checked out %s: %s\n", refname, commit_id_str);
3195 printf("Now shut up and hack\n");
3196 if (cpa.had_base_commit_ref_error)
3197 show_worktree_base_ref_warning();
3200 const struct got_error *pack_err =
3201 got_repo_pack_fds_close(pack_fds);
3206 got_ref_close(head_ref);
3210 const struct got_error *close_err = got_repo_close(repo);
3214 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
3215 free(commit_id_str);
3218 free(worktree_path);
3223 struct got_update_progress_arg {
3235 print_update_progress_stats(struct got_update_progress_arg *upa)
3237 if (!upa->did_something)
3240 if (upa->conflicts > 0)
3241 printf("Files with new merge conflicts: %d\n", upa->conflicts);
3242 if (upa->obstructed > 0)
3243 printf("File paths obstructed by a non-regular file: %d\n",
3245 if (upa->not_updated > 0)
3246 printf("Files not updated because of existing merge "
3247 "conflicts: %d\n", upa->not_updated);
3251 * The meaning of some status codes differs between merge-style operations and
3252 * update operations. For example, the ! status code means "file was missing"
3253 * if changes were merged into the work tree, and "missing file was restored"
3254 * if the work tree was updated. This function should be used by any operation
3255 * which merges changes into the work tree without updating the work tree.
3258 print_merge_progress_stats(struct got_update_progress_arg *upa)
3260 if (!upa->did_something)
3263 if (upa->conflicts > 0)
3264 printf("Files with new merge conflicts: %d\n", upa->conflicts);
3265 if (upa->obstructed > 0)
3266 printf("File paths obstructed by a non-regular file: %d\n",
3268 if (upa->missing > 0)
3269 printf("Files which had incoming changes but could not be "
3270 "found in the work tree: %d\n", upa->missing);
3271 if (upa->not_deleted > 0)
3272 printf("Files not deleted due to differences in deleted "
3273 "content: %d\n", upa->not_deleted);
3274 if (upa->unversioned > 0)
3275 printf("Files not merged because an unversioned file was "
3276 "found in the work tree: %d\n", upa->unversioned);
3282 fprintf(stderr, "usage: %s update [-q] [-b branch] [-c commit] "
3283 "[path ...]\n", getprogname());
3287 static const struct got_error *
3288 update_progress(void *arg, unsigned char status, const char *path)
3290 struct got_update_progress_arg *upa = arg;
3292 if (status == GOT_STATUS_EXISTS ||
3293 status == GOT_STATUS_BASE_REF_ERR)
3296 upa->did_something = 1;
3298 /* Base commit bump happens silently. */
3299 if (status == GOT_STATUS_BUMP_BASE)
3302 if (status == GOT_STATUS_CONFLICT)
3304 if (status == GOT_STATUS_OBSTRUCTED)
3306 if (status == GOT_STATUS_CANNOT_UPDATE)
3308 if (status == GOT_STATUS_MISSING)
3310 if (status == GOT_STATUS_CANNOT_DELETE)
3312 if (status == GOT_STATUS_UNVERSIONED)
3315 while (path[0] == '/')
3317 if (upa->verbosity >= 0)
3318 printf("%c %s\n", status, path);
3323 static const struct got_error *
3324 switch_head_ref(struct got_reference *head_ref,
3325 struct got_object_id *commit_id, struct got_worktree *worktree,
3326 struct got_repository *repo)
3328 const struct got_error *err = NULL;
3330 int ref_has_moved = 0;
3332 /* Trivial case: switching between two different references. */
3333 if (strcmp(got_ref_get_name(head_ref),
3334 got_worktree_get_head_ref_name(worktree)) != 0) {
3335 printf("Switching work tree from %s to %s\n",
3336 got_worktree_get_head_ref_name(worktree),
3337 got_ref_get_name(head_ref));
3338 return got_worktree_set_head_ref(worktree, head_ref);
3341 err = check_linear_ancestry(commit_id,
3342 got_worktree_get_base_commit_id(worktree), 0, repo);
3344 if (err->code != GOT_ERR_ANCESTRY)
3351 /* Switching to a rebased branch with the same reference name. */
3352 err = got_object_id_str(&base_id_str,
3353 got_worktree_get_base_commit_id(worktree));
3356 printf("Reference %s now points at a different branch\n",
3357 got_worktree_get_head_ref_name(worktree));
3358 printf("Switching work tree from %s to %s\n", base_id_str,
3359 got_worktree_get_head_ref_name(worktree));
3363 static const struct got_error *
3364 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
3366 const struct got_error *err;
3369 err = got_worktree_rebase_in_progress(&in_progress, worktree);
3373 return got_error(GOT_ERR_REBASING);
3375 err = got_worktree_histedit_in_progress(&in_progress, worktree);
3379 return got_error(GOT_ERR_HISTEDIT_BUSY);
3384 static const struct got_error *
3385 check_merge_in_progress(struct got_worktree *worktree,
3386 struct got_repository *repo)
3388 const struct got_error *err;
3391 err = got_worktree_merge_in_progress(&in_progress, worktree, repo);
3395 return got_error(GOT_ERR_MERGE_BUSY);
3400 static const struct got_error *
3401 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
3402 char *argv[], struct got_worktree *worktree)
3404 const struct got_error *err = NULL;
3406 struct got_pathlist_entry *new;
3412 return got_error_from_errno("strdup");
3413 return got_pathlist_append(paths, path, NULL);
3416 for (i = 0; i < argc; i++) {
3417 err = got_worktree_resolve_path(&path, worktree, argv[i]);
3420 err = got_pathlist_insert(&new, paths, path, NULL);
3421 if (err || new == NULL /* duplicate */) {
3431 static const struct got_error *
3432 wrap_not_worktree_error(const struct got_error *orig_err,
3433 const char *cmdname, const char *path)
3435 const struct got_error *err;
3436 struct got_repository *repo;
3437 static char msg[512];
3438 int *pack_fds = NULL;
3440 err = got_repo_pack_fds_open(&pack_fds);
3444 err = got_repo_open(&repo, path, NULL, pack_fds);
3448 snprintf(msg, sizeof(msg),
3449 "'got %s' needs a work tree in addition to a git repository\n"
3450 "Work trees can be checked out from this Git repository with "
3452 "The got(1) manual page contains more information.", cmdname);
3453 err = got_error_msg(GOT_ERR_NOT_WORKTREE, msg);
3455 const struct got_error *close_err = got_repo_close(repo);
3460 const struct got_error *pack_err =
3461 got_repo_pack_fds_close(pack_fds);
3468 static const struct got_error *
3469 cmd_update(int argc, char *argv[])
3471 const struct got_error *error = NULL;
3472 struct got_repository *repo = NULL;
3473 struct got_worktree *worktree = NULL;
3474 char *worktree_path = NULL;
3475 struct got_object_id *commit_id = NULL;
3476 char *commit_id_str = NULL;
3477 const char *branch_name = NULL;
3478 struct got_reference *head_ref = NULL;
3479 struct got_pathlist_head paths;
3480 struct got_pathlist_entry *pe;
3481 int ch, verbosity = 0;
3482 struct got_update_progress_arg upa;
3483 int *pack_fds = NULL;
3488 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3489 "unveil", NULL) == -1)
3493 while ((ch = getopt(argc, argv, "b:c:q")) != -1) {
3496 branch_name = optarg;
3499 commit_id_str = strdup(optarg);
3500 if (commit_id_str == NULL)
3501 return got_error_from_errno("strdup");
3515 worktree_path = getcwd(NULL, 0);
3516 if (worktree_path == NULL) {
3517 error = got_error_from_errno("getcwd");
3521 error = got_repo_pack_fds_open(&pack_fds);
3525 error = got_worktree_open(&worktree, worktree_path);
3527 if (error->code == GOT_ERR_NOT_WORKTREE)
3528 error = wrap_not_worktree_error(error, "update",
3533 error = check_rebase_or_histedit_in_progress(worktree);
3537 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3542 error = apply_unveil(got_repo_get_path(repo), 0,
3543 got_worktree_get_root_path(worktree));
3547 error = check_merge_in_progress(worktree, repo);
3551 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3555 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
3556 got_worktree_get_head_ref_name(worktree), 0);
3559 if (commit_id_str == NULL) {
3560 error = got_ref_resolve(&commit_id, repo, head_ref);
3563 error = got_object_id_str(&commit_id_str, commit_id);
3567 struct got_reflist_head refs;
3569 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3573 error = got_repo_match_object_id(&commit_id, NULL,
3574 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3575 got_ref_list_free(&refs);
3576 free(commit_id_str);
3577 commit_id_str = NULL;
3580 error = got_object_id_str(&commit_id_str, commit_id);
3586 struct got_object_id *head_commit_id;
3587 TAILQ_FOREACH(pe, &paths, entry) {
3588 if (pe->path_len == 0)
3590 error = got_error_msg(GOT_ERR_BAD_PATH,
3591 "switching between branches requires that "
3592 "the entire work tree gets updated");
3595 error = got_ref_resolve(&head_commit_id, repo, head_ref);
3598 error = check_linear_ancestry(commit_id, head_commit_id, 0,
3600 free(head_commit_id);
3603 error = check_same_branch(commit_id, head_ref, repo);
3606 error = switch_head_ref(head_ref, commit_id, worktree, repo);
3610 error = check_linear_ancestry(commit_id,
3611 got_worktree_get_base_commit_id(worktree), 0, repo);
3612 if (error != NULL) {
3613 if (error->code == GOT_ERR_ANCESTRY)
3614 error = got_error(GOT_ERR_BRANCH_MOVED);
3617 error = check_same_branch(commit_id, head_ref, repo);
3622 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
3624 error = got_worktree_set_base_commit_id(worktree, repo,
3630 memset(&upa, 0, sizeof(upa));
3631 upa.verbosity = verbosity;
3632 error = got_worktree_checkout_files(worktree, &paths, repo,
3633 update_progress, &upa, check_cancelled, NULL);
3637 if (upa.did_something) {
3638 printf("Updated to %s: %s\n",
3639 got_worktree_get_head_ref_name(worktree), commit_id_str);
3641 printf("Already up-to-date\n");
3643 print_update_progress_stats(&upa);
3646 const struct got_error *pack_err =
3647 got_repo_pack_fds_close(pack_fds);
3652 const struct got_error *close_err = got_repo_close(repo);
3656 free(worktree_path);
3657 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
3659 free(commit_id_str);
3663 static const struct got_error *
3664 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
3665 const char *path, int diff_context, int ignore_whitespace,
3666 int force_text_diff, struct got_diffstat_cb_arg *dsa,
3667 struct got_repository *repo, FILE *outfile)
3669 const struct got_error *err = NULL;
3670 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
3671 FILE *f1 = NULL, *f2 = NULL;
3672 int fd1 = -1, fd2 = -1;
3674 fd1 = got_opentempfd();
3676 return got_error_from_errno("got_opentempfd");
3677 fd2 = got_opentempfd();
3679 err = got_error_from_errno("got_opentempfd");
3684 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192,
3690 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192, fd2);
3694 f1 = got_opentemp();
3696 err = got_error_from_errno("got_opentemp");
3699 f2 = got_opentemp();
3701 err = got_error_from_errno("got_opentemp");
3705 while (path[0] == '/')
3707 err = got_diff_blob(NULL, NULL, blob1, blob2, f1, f2, path, path,
3708 GOT_DIFF_ALGORITHM_PATIENCE, diff_context, ignore_whitespace,
3709 force_text_diff, dsa, outfile);
3711 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3712 err = got_error_from_errno("close");
3714 got_object_blob_close(blob1);
3715 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3716 err = got_error_from_errno("close");
3718 got_object_blob_close(blob2);
3719 if (f1 && fclose(f1) == EOF && err == NULL)
3720 err = got_error_from_errno("fclose");
3721 if (f2 && fclose(f2) == EOF && err == NULL)
3722 err = got_error_from_errno("fclose");
3726 static const struct got_error *
3727 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
3728 const char *path, int diff_context, int ignore_whitespace,
3729 int force_text_diff, struct got_diffstat_cb_arg *dsa,
3730 struct got_repository *repo, FILE *outfile)
3732 const struct got_error *err = NULL;
3733 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3734 struct got_diff_blob_output_unidiff_arg arg;
3735 FILE *f1 = NULL, *f2 = NULL;
3736 int fd1 = -1, fd2 = -1;
3739 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3742 fd1 = got_opentempfd();
3744 err = got_error_from_errno("got_opentempfd");
3749 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3753 f1 = got_opentemp();
3755 err = got_error_from_errno("got_opentemp");
3759 f2 = got_opentemp();
3761 err = got_error_from_errno("got_opentemp");
3764 fd2 = got_opentempfd();
3766 err = got_error_from_errno("got_opentempfd");
3769 arg.diff_context = diff_context;
3770 arg.ignore_whitespace = ignore_whitespace;
3771 arg.force_text_diff = force_text_diff;
3773 arg.diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
3774 arg.outfile = outfile;
3777 while (path[0] == '/')
3779 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, path, path, repo,
3780 got_diff_blob_output_unidiff, &arg, 1);
3783 got_object_tree_close(tree1);
3785 got_object_tree_close(tree2);
3786 if (f1 && fclose(f1) == EOF && err == NULL)
3787 err = got_error_from_errno("fclose");
3788 if (f2 && fclose(f2) == EOF && err == NULL)
3789 err = got_error_from_errno("fclose");
3790 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3791 err = got_error_from_errno("close");
3792 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3793 err = got_error_from_errno("close");
3797 static const struct got_error *
3798 get_changed_paths(struct got_pathlist_head *paths,
3799 struct got_commit_object *commit, struct got_repository *repo,
3800 struct got_diffstat_cb_arg *dsa)
3802 const struct got_error *err = NULL;
3803 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3804 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3805 struct got_object_qid *qid;
3806 got_diff_blob_cb cb = got_diff_tree_collect_changed_paths;
3807 FILE *f1 = NULL, *f2 = NULL;
3808 int fd1 = -1, fd2 = -1;
3811 cb = got_diff_tree_compute_diffstat;
3813 f1 = got_opentemp();
3815 err = got_error_from_errno("got_opentemp");
3818 f2 = got_opentemp();
3820 err = got_error_from_errno("got_opentemp");
3823 fd1 = got_opentempfd();
3825 err = got_error_from_errno("got_opentempfd");
3828 fd2 = got_opentempfd();
3830 err = got_error_from_errno("got_opentempfd");
3835 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3837 struct got_commit_object *pcommit;
3838 err = got_object_open_as_commit(&pcommit, repo,
3843 tree_id1 = got_object_id_dup(
3844 got_object_commit_get_tree_id(pcommit));
3845 if (tree_id1 == NULL) {
3846 got_object_commit_close(pcommit);
3847 return got_error_from_errno("got_object_id_dup");
3849 got_object_commit_close(pcommit);
3854 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3859 tree_id2 = got_object_commit_get_tree_id(commit);
3860 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3864 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3865 cb, dsa ? (void *)dsa : paths, dsa ? 1 : 0);
3868 got_object_tree_close(tree1);
3870 got_object_tree_close(tree2);
3871 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3872 err = got_error_from_errno("close");
3873 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3874 err = got_error_from_errno("close");
3875 if (f1 && fclose(f1) == EOF && err == NULL)
3876 err = got_error_from_errno("fclose");
3877 if (f2 && fclose(f2) == EOF && err == NULL)
3878 err = got_error_from_errno("fclose");
3883 static const struct got_error *
3884 print_patch(struct got_commit_object *commit, struct got_object_id *id,
3885 const char *path, int diff_context, struct got_diffstat_cb_arg *dsa,
3886 struct got_repository *repo, FILE *outfile)
3888 const struct got_error *err = NULL;
3889 struct got_commit_object *pcommit = NULL;
3890 char *id_str1 = NULL, *id_str2 = NULL;
3891 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
3892 struct got_object_qid *qid;
3894 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3896 err = got_object_open_as_commit(&pcommit, repo,
3900 err = got_object_id_str(&id_str1, &qid->id);
3905 err = got_object_id_str(&id_str2, id);
3909 if (path && path[0] != '\0') {
3911 err = got_object_id_by_path(&obj_id2, repo, commit, path);
3915 err = got_object_id_by_path(&obj_id1, repo,
3918 if (err->code != GOT_ERR_NO_TREE_ENTRY) {
3924 err = got_object_get_type(&obj_type, repo, obj_id2);
3930 "diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3931 fprintf(outfile, "commit - %s\n",
3932 id_str1 ? id_str1 : "/dev/null");
3933 fprintf(outfile, "commit + %s\n", id_str2);
3935 case GOT_OBJ_TYPE_BLOB:
3936 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
3937 0, 0, dsa, repo, outfile);
3939 case GOT_OBJ_TYPE_TREE:
3940 err = diff_trees(obj_id1, obj_id2, path, diff_context,
3941 0, 0, dsa, repo, outfile);
3944 err = got_error(GOT_ERR_OBJ_TYPE);
3950 obj_id2 = got_object_commit_get_tree_id(commit);
3952 obj_id1 = got_object_commit_get_tree_id(pcommit);
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);
3958 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, 0,
3959 dsa, repo, outfile);
3965 got_object_commit_close(pcommit);
3970 get_datestr(time_t *time, char *datebuf)
3972 struct tm mytm, *tm;
3975 tm = gmtime_r(time, &mytm);
3978 s = asctime_r(tm, datebuf);
3981 p = strchr(s, '\n');
3987 static const struct got_error *
3988 match_commit(int *have_match, struct got_object_id *id,
3989 struct got_commit_object *commit, regex_t *regex)
3991 const struct got_error *err = NULL;
3992 regmatch_t regmatch;
3993 char *id_str = NULL, *logmsg = NULL;
3997 err = got_object_id_str(&id_str, id);
4001 err = got_object_commit_get_logmsg(&logmsg, commit);
4005 if (regexec(regex, got_object_commit_get_author(commit), 1,
4006 ®match, 0) == 0 ||
4007 regexec(regex, got_object_commit_get_committer(commit), 1,
4008 ®match, 0) == 0 ||
4009 regexec(regex, id_str, 1, ®match, 0) == 0 ||
4010 regexec(regex, logmsg, 1, ®match, 0) == 0)
4019 match_changed_paths(int *have_match, struct got_pathlist_head *changed_paths,
4022 regmatch_t regmatch;
4023 struct got_pathlist_entry *pe;
4027 TAILQ_FOREACH(pe, changed_paths, entry) {
4028 if (regexec(regex, pe->path, 1, ®match, 0) == 0) {
4035 static const struct got_error *
4036 match_patch(int *have_match, struct got_commit_object *commit,
4037 struct got_object_id *id, const char *path, int diff_context,
4038 struct got_repository *repo, regex_t *regex, FILE *f)
4040 const struct got_error *err = NULL;
4042 size_t linesize = 0;
4043 regmatch_t regmatch;
4047 err = got_opentemp_truncate(f);
4051 err = print_patch(commit, id, path, diff_context, NULL, repo, f);
4055 if (fseeko(f, 0L, SEEK_SET) == -1) {
4056 err = got_error_from_errno("fseeko");
4060 while (getline(&line, &linesize, f) != -1) {
4061 if (regexec(regex, line, 1, ®match, 0) == 0) {
4071 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
4073 static const struct got_error*
4074 build_refs_str(char **refs_str, struct got_reflist_head *refs,
4075 struct got_object_id *id, struct got_repository *repo,
4078 static const struct got_error *err = NULL;
4079 struct got_reflist_entry *re;
4085 TAILQ_FOREACH(re, refs, entry) {
4086 struct got_tag_object *tag = NULL;
4087 struct got_object_id *ref_id;
4090 name = got_ref_get_name(re->ref);
4091 if (strcmp(name, GOT_REF_HEAD) == 0)
4093 if (strncmp(name, "refs/", 5) == 0)
4095 if (strncmp(name, "got/", 4) == 0)
4097 if (strncmp(name, "heads/", 6) == 0)
4099 if (strncmp(name, "remotes/", 8) == 0) {
4103 s = strstr(name, "/" GOT_REF_HEAD);
4104 if (s != NULL && strcmp(s, "/" GOT_REF_HEAD) == 0)
4107 err = got_ref_resolve(&ref_id, repo, re->ref);
4110 if (strncmp(name, "tags/", 5) == 0) {
4111 err = got_object_open_as_tag(&tag, repo, ref_id);
4113 if (err->code != GOT_ERR_OBJ_TYPE) {
4117 /* Ref points at something other than a tag. */
4122 cmp = got_object_id_cmp(tag ?
4123 got_object_tag_get_object_id(tag) : ref_id, id);
4126 got_object_tag_close(tag);
4130 if (asprintf(refs_str, "%s%s%s", s ? s : "",
4131 s ? ", " : "", name) == -1) {
4132 err = got_error_from_errno("asprintf");
4143 static const struct got_error *
4144 print_commit_oneline(struct got_commit_object *commit, struct got_object_id *id,
4145 struct got_repository *repo, struct got_reflist_object_id_map *refs_idmap)
4147 const struct got_error *err = NULL;
4148 char *ref_str = NULL, *id_str = NULL, *logmsg0 = NULL;
4149 char *comma, *s, *nl;
4150 struct got_reflist_head *refs;
4151 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
4153 time_t committer_time;
4155 refs = got_reflist_object_id_map_lookup(refs_idmap, id);
4157 err = build_refs_str(&ref_str, refs, id, repo, 1);
4161 /* Display the first matching ref only. */
4162 if (ref_str && (comma = strchr(ref_str, ',')) != NULL)
4166 if (ref_str == NULL) {
4167 err = got_object_id_str(&id_str, id);
4172 committer_time = got_object_commit_get_committer_time(commit);
4173 if (gmtime_r(&committer_time, &tm) == NULL) {
4174 err = got_error_from_errno("gmtime_r");
4177 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0) {
4178 err = got_error(GOT_ERR_NO_SPACE);
4182 err = got_object_commit_get_logmsg(&logmsg0, commit);
4187 while (isspace((unsigned char)s[0]))
4190 nl = strchr(s, '\n');
4196 printf("%s%-7s %s\n", datebuf, ref_str, s);
4198 printf("%s%.7s %s\n", datebuf, id_str, s);
4200 if (fflush(stdout) != 0 && err == NULL)
4201 err = got_error_from_errno("fflush");
4209 static const struct got_error *
4210 print_diffstat(struct got_diffstat_cb_arg *dsa, const char *header)
4212 struct got_pathlist_entry *pe;
4215 printf("%s\n", header);
4217 TAILQ_FOREACH(pe, dsa->paths, entry) {
4218 struct got_diff_changed_path *cp = pe->data;
4219 int pad = dsa->max_path_len - pe->path_len + 1;
4221 printf(" %c %s%*c | %*d+ %*d-\n", cp->status, pe->path, pad,
4222 ' ', dsa->add_cols + 1, cp->add, dsa->rm_cols + 1, cp->rm);
4224 printf("\n%d file%s changed, %d insertion%s(+), %d deletion%s(-)\n\n",
4225 dsa->nfiles, dsa->nfiles > 1 ? "s" : "", dsa->ins,
4226 dsa->ins != 1 ? "s" : "", dsa->del, dsa->del != 1 ? "s" : "");
4228 if (fflush(stdout) != 0)
4229 return got_error_from_errno("fflush");
4234 static const struct got_error *
4240 if (fseeko(f, 0L, SEEK_SET) == -1)
4241 return got_error_from_errno("fseek");
4244 r = fread(buf, 1, sizeof(buf), f);
4247 return got_error_from_errno("fread");
4251 if (fwrite(buf, 1, r, stdout) != r)
4252 return got_ferror(stdout, GOT_ERR_IO);
4258 static const struct got_error *
4259 print_commit(struct got_commit_object *commit, struct got_object_id *id,
4260 struct got_repository *repo, const char *path,
4261 struct got_pathlist_head *changed_paths,
4262 struct got_diffstat_cb_arg *diffstat, int show_patch, int diff_context,
4263 struct got_reflist_object_id_map *refs_idmap, const char *custom_refs_str,
4266 const struct got_error *err = NULL;
4268 char *id_str, *datestr, *logmsg0, *logmsg, *line;
4270 time_t committer_time;
4271 const char *author, *committer;
4272 char *refs_str = NULL;
4274 err = got_object_id_str(&id_str, id);
4278 if (custom_refs_str == NULL) {
4279 struct got_reflist_head *refs;
4280 refs = got_reflist_object_id_map_lookup(refs_idmap, id);
4282 err = build_refs_str(&refs_str, refs, id, repo, 0);
4288 printf(GOT_COMMIT_SEP_STR);
4289 if (custom_refs_str)
4290 printf("%s %s (%s)\n", prefix ? prefix : "commit", id_str,
4293 printf("%s %s%s%s%s\n", prefix ? prefix : "commit", id_str,
4294 refs_str ? " (" : "", refs_str ? refs_str : "",
4295 refs_str ? ")" : "");
4300 printf("from: %s\n", got_object_commit_get_author(commit));
4301 author = got_object_commit_get_author(commit);
4302 committer = got_object_commit_get_committer(commit);
4303 if (strcmp(author, committer) != 0)
4304 printf("via: %s\n", committer);
4305 committer_time = got_object_commit_get_committer_time(commit);
4306 datestr = get_datestr(&committer_time, datebuf);
4308 printf("date: %s UTC\n", datestr);
4309 if (got_object_commit_get_nparents(commit) > 1) {
4310 const struct got_object_id_queue *parent_ids;
4311 struct got_object_qid *qid;
4313 parent_ids = got_object_commit_get_parent_ids(commit);
4314 STAILQ_FOREACH(qid, parent_ids, entry) {
4315 err = got_object_id_str(&id_str, &qid->id);
4318 printf("parent %d: %s\n", n++, id_str);
4324 err = got_object_commit_get_logmsg(&logmsg0, commit);
4330 line = strsep(&logmsg, "\n");
4332 printf(" %s\n", line);
4336 if (changed_paths && diffstat == NULL) {
4337 struct got_pathlist_entry *pe;
4339 TAILQ_FOREACH(pe, changed_paths, entry) {
4340 struct got_diff_changed_path *cp = pe->data;
4342 printf(" %c %s\n", cp->status, pe->path);
4350 err = got_error_from_errno("got_opentemp");
4355 err = print_patch(commit, id, path, diff_context, diffstat,
4356 repo, diffstat == NULL ? stdout : f);
4361 err = print_diffstat(diffstat, NULL);
4373 if (fflush(stdout) != 0 && err == NULL)
4374 err = got_error_from_errno("fflush");
4376 if (f && fclose(f) == EOF && err == NULL)
4377 err = got_error_from_errno("fclose");
4383 static const struct got_error *
4384 print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
4385 struct got_repository *repo, const char *path, int show_changed_paths,
4386 int show_diffstat, int show_patch, const char *search_pattern,
4387 int diff_context, int limit, int log_branches, int reverse_display_order,
4388 struct got_reflist_object_id_map *refs_idmap, int one_line,
4391 const struct got_error *err;
4392 struct got_commit_graph *graph;
4395 struct got_object_id_queue reversed_commits;
4396 struct got_object_qid *qid;
4397 struct got_commit_object *commit;
4398 struct got_pathlist_head changed_paths;
4400 STAILQ_INIT(&reversed_commits);
4401 TAILQ_INIT(&changed_paths);
4403 if (search_pattern && regcomp(®ex, search_pattern,
4404 REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
4405 return got_error_msg(GOT_ERR_REGEX, search_pattern);
4407 err = got_commit_graph_open(&graph, path, !log_branches);
4410 err = got_commit_graph_iter_start(graph, root_id, repo,
4411 check_cancelled, NULL);
4415 struct got_object_id id;
4416 struct got_diffstat_cb_arg dsa = { 0, 0, 0, 0, 0, 0,
4417 &changed_paths, 0, 0, GOT_DIFF_ALGORITHM_PATIENCE };
4419 if (sigint_received || sigpipe_received)
4422 err = got_commit_graph_iter_next(&id, graph, repo,
4423 check_cancelled, NULL);
4425 if (err->code == GOT_ERR_ITER_COMPLETED)
4430 err = got_object_open_as_commit(&commit, repo, &id);
4434 if ((show_changed_paths || (show_diffstat && !show_patch))
4435 && !reverse_display_order) {
4436 err = get_changed_paths(&changed_paths, commit, repo,
4437 show_diffstat ? &dsa : NULL);
4442 if (search_pattern) {
4443 err = match_commit(&have_match, &id, commit, ®ex);
4445 got_object_commit_close(commit);
4448 if (have_match == 0 && show_changed_paths)
4449 match_changed_paths(&have_match,
4450 &changed_paths, ®ex);
4451 if (have_match == 0 && show_patch) {
4452 err = match_patch(&have_match, commit, &id,
4453 path, diff_context, repo, ®ex, tmpfile);
4457 if (have_match == 0) {
4458 got_object_commit_close(commit);
4459 got_pathlist_free(&changed_paths,
4460 GOT_PATHLIST_FREE_ALL);
4465 if (reverse_display_order) {
4466 err = got_object_qid_alloc(&qid, &id);
4469 STAILQ_INSERT_HEAD(&reversed_commits, qid, entry);
4470 got_object_commit_close(commit);
4473 err = print_commit_oneline(commit, &id,
4476 err = print_commit(commit, &id, repo, path,
4477 (show_changed_paths || show_diffstat) ?
4478 &changed_paths : NULL,
4479 show_diffstat ? &dsa : NULL, show_patch,
4480 diff_context, refs_idmap, NULL, NULL);
4481 got_object_commit_close(commit);
4485 if ((limit && --limit == 0) ||
4486 (end_id && got_object_id_cmp(&id, end_id) == 0))
4489 got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
4491 if (reverse_display_order) {
4492 STAILQ_FOREACH(qid, &reversed_commits, entry) {
4493 struct got_diffstat_cb_arg dsa = { 0, 0, 0, 0, 0, 0,
4494 &changed_paths, 0, 0, GOT_DIFF_ALGORITHM_PATIENCE };
4496 err = got_object_open_as_commit(&commit, repo,
4500 if (show_changed_paths ||
4501 (show_diffstat && !show_patch)) {
4502 err = get_changed_paths(&changed_paths, commit,
4503 repo, show_diffstat ? &dsa : NULL);
4508 err = print_commit_oneline(commit, &qid->id,
4511 err = print_commit(commit, &qid->id, repo, path,
4512 (show_changed_paths || show_diffstat) ?
4513 &changed_paths : NULL,
4514 show_diffstat ? &dsa : NULL, show_patch,
4515 diff_context, refs_idmap, NULL, NULL);
4516 got_object_commit_close(commit);
4519 got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
4523 while (!STAILQ_EMPTY(&reversed_commits)) {
4524 qid = STAILQ_FIRST(&reversed_commits);
4525 STAILQ_REMOVE_HEAD(&reversed_commits, entry);
4526 got_object_qid_free(qid);
4528 got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
4531 got_commit_graph_close(graph);
4538 fprintf(stderr, "usage: %s log [-bdPpRs] [-C number] [-c commit] "
4539 "[-l N] [-r repository-path] [-S search-pattern] [-x commit] "
4540 "[path]\n", getprogname());
4545 get_default_log_limit(void)
4547 const char *got_default_log_limit;
4551 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
4552 if (got_default_log_limit == NULL)
4554 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
4560 static const struct got_error *
4561 cmd_log(int argc, char *argv[])
4563 const struct got_error *error;
4564 struct got_repository *repo = NULL;
4565 struct got_worktree *worktree = NULL;
4566 struct got_object_id *start_id = NULL, *end_id = NULL;
4567 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
4568 const char *start_commit = NULL, *end_commit = NULL;
4569 const char *search_pattern = NULL;
4570 int diff_context = -1, ch;
4571 int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
4572 int show_diffstat = 0, reverse_display_order = 0, one_line = 0;
4574 struct got_reflist_head refs;
4575 struct got_reflist_object_id_map *refs_idmap = NULL;
4576 FILE *tmpfile = NULL;
4577 int *pack_fds = NULL;
4582 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4588 limit = get_default_log_limit();
4590 while ((ch = getopt(argc, argv, "bC:c:dl:PpRr:S:sx:")) != -1) {
4596 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4599 errx(1, "number of context lines is %s: %s",
4603 start_commit = optarg;
4609 limit = strtonum(optarg, 0, INT_MAX, &errstr);
4611 errx(1, "number of commits is %s: %s",
4615 show_changed_paths = 1;
4621 reverse_display_order = 1;
4624 repo_path = realpath(optarg, NULL);
4625 if (repo_path == NULL)
4626 return got_error_from_errno2("realpath",
4628 got_path_strip_trailing_slashes(repo_path);
4631 search_pattern = optarg;
4637 end_commit = optarg;
4648 if (diff_context == -1)
4650 else if (!show_patch)
4651 errx(1, "-C requires -p");
4653 if (one_line && (show_patch || show_changed_paths || show_diffstat))
4654 errx(1, "cannot use -s with -d, -p or -P");
4656 cwd = getcwd(NULL, 0);
4658 error = got_error_from_errno("getcwd");
4662 error = got_repo_pack_fds_open(&pack_fds);
4666 if (repo_path == NULL) {
4667 error = got_worktree_open(&worktree, cwd);
4668 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4675 error = got_worktree_resolve_path(&path, worktree,
4680 path = strdup(argv[0]);
4682 error = got_error_from_errno("strdup");
4686 } else if (argc != 0)
4689 if (repo_path == NULL) {
4690 repo_path = worktree ?
4691 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
4693 if (repo_path == NULL) {
4694 error = got_error_from_errno("strdup");
4698 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4702 error = apply_unveil(got_repo_get_path(repo), 1,
4703 worktree ? got_worktree_get_root_path(worktree) : NULL);
4707 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4711 error = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
4715 if (start_commit == NULL) {
4716 struct got_reference *head_ref;
4717 struct got_commit_object *commit = NULL;
4718 error = got_ref_open(&head_ref, repo,
4719 worktree ? got_worktree_get_head_ref_name(worktree)
4723 error = got_ref_resolve(&start_id, repo, head_ref);
4724 got_ref_close(head_ref);
4727 error = got_object_open_as_commit(&commit, repo,
4731 got_object_commit_close(commit);
4733 error = got_repo_match_object_id(&start_id, NULL,
4734 start_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4738 if (end_commit != NULL) {
4739 error = got_repo_match_object_id(&end_id, NULL,
4740 end_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4747 * If a path was specified on the command line it was resolved
4748 * to a path in the work tree above. Prepend the work tree's
4749 * path prefix to obtain the corresponding in-repository path.
4753 prefix = got_worktree_get_path_prefix(worktree);
4754 if (asprintf(&in_repo_path, "%s%s%s", prefix,
4755 (path[0] != '\0') ? "/" : "", path) == -1) {
4756 error = got_error_from_errno("asprintf");
4761 error = got_repo_map_path(&in_repo_path, repo,
4767 path = in_repo_path;
4771 /* Release work tree lock. */
4772 got_worktree_close(worktree);
4776 if (search_pattern && show_patch) {
4777 tmpfile = got_opentemp();
4778 if (tmpfile == NULL) {
4779 error = got_error_from_errno("got_opentemp");
4784 error = print_commits(start_id, end_id, repo, path ? path : "",
4785 show_changed_paths, show_diffstat, show_patch, search_pattern,
4786 diff_context, limit, log_branches, reverse_display_order,
4787 refs_idmap, one_line, tmpfile);
4795 got_worktree_close(worktree);
4797 const struct got_error *close_err = got_repo_close(repo);
4802 const struct got_error *pack_err =
4803 got_repo_pack_fds_close(pack_fds);
4808 got_reflist_object_id_map_free(refs_idmap);
4809 if (tmpfile && fclose(tmpfile) == EOF && error == NULL)
4810 error = got_error_from_errno("fclose");
4811 got_ref_list_free(&refs);
4818 fprintf(stderr, "usage: %s diff [-adPsw] [-C number] [-c commit] "
4819 "[-r repository-path] [object1 object2 | path ...]\n",
4824 struct print_diff_arg {
4825 struct got_repository *repo;
4826 struct got_worktree *worktree;
4827 struct got_diffstat_cb_arg *diffstat;
4832 enum got_diff_algorithm diff_algo;
4833 int ignore_whitespace;
4834 int force_text_diff;
4841 * Create a file which contains the target path of a symlink so we can feed
4842 * it as content to the diff engine.
4844 static const struct got_error *
4845 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
4846 const char *abspath)
4848 const struct got_error *err = NULL;
4849 char target_path[PATH_MAX];
4850 ssize_t target_len, outlen;
4855 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
4856 if (target_len == -1)
4857 return got_error_from_errno2("readlinkat", abspath);
4859 target_len = readlink(abspath, target_path, PATH_MAX);
4860 if (target_len == -1)
4861 return got_error_from_errno2("readlink", abspath);
4864 *fd = got_opentempfd();
4866 return got_error_from_errno("got_opentempfd");
4868 outlen = write(*fd, target_path, target_len);
4870 err = got_error_from_errno("got_opentempfd");
4874 if (lseek(*fd, 0, SEEK_SET) == -1) {
4875 err = got_error_from_errno2("lseek", abspath);
4886 static const struct got_error *
4887 print_diff(void *arg, unsigned char status, unsigned char staged_status,
4888 const char *path, struct got_object_id *blob_id,
4889 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4890 int dirfd, const char *de_name)
4892 struct print_diff_arg *a = arg;
4893 const struct got_error *err = NULL;
4894 struct got_blob_object *blob1 = NULL;
4895 int fd = -1, fd1 = -1, fd2 = -1;
4897 char *abspath = NULL, *label1 = NULL;
4902 memset(&sb, 0, sizeof(sb));
4904 if (a->diff_staged) {
4905 if (staged_status != GOT_STATUS_MODIFY &&
4906 staged_status != GOT_STATUS_ADD &&
4907 staged_status != GOT_STATUS_DELETE)
4910 if (staged_status == GOT_STATUS_DELETE)
4912 if (status == GOT_STATUS_NONEXISTENT)
4913 return got_error_set_errno(ENOENT, path);
4914 if (status != GOT_STATUS_MODIFY &&
4915 status != GOT_STATUS_ADD &&
4916 status != GOT_STATUS_DELETE &&
4917 status != GOT_STATUS_CONFLICT)
4921 err = got_opentemp_truncate(a->f1);
4923 return got_error_from_errno("got_opentemp_truncate");
4924 err = got_opentemp_truncate(a->f2);
4926 return got_error_from_errno("got_opentemp_truncate");
4928 if (!a->header_shown) {
4929 if (fprintf(a->outfile, "diff %s%s\n",
4930 a->diff_staged ? "-s " : "",
4931 got_worktree_get_root_path(a->worktree)) < 0) {
4932 err = got_error_from_errno("fprintf");
4935 if (fprintf(a->outfile, "commit - %s\n", a->id_str) < 0) {
4936 err = got_error_from_errno("fprintf");
4939 if (fprintf(a->outfile, "path + %s%s\n",
4940 got_worktree_get_root_path(a->worktree),
4941 a->diff_staged ? " (staged changes)" : "") < 0) {
4942 err = got_error_from_errno("fprintf");
4945 a->header_shown = 1;
4948 if (a->diff_staged) {
4949 const char *label1 = NULL, *label2 = NULL;
4950 switch (staged_status) {
4951 case GOT_STATUS_MODIFY:
4955 case GOT_STATUS_ADD:
4958 case GOT_STATUS_DELETE:
4962 return got_error(GOT_ERR_FILE_STATUS);
4964 fd1 = got_opentempfd();
4966 err = got_error_from_errno("got_opentempfd");
4969 fd2 = got_opentempfd();
4971 err = got_error_from_errno("got_opentempfd");
4974 err = got_diff_objects_as_blobs(NULL, NULL, a->f1, a->f2,
4975 fd1, fd2, blob_id, staged_blob_id, label1, label2,
4976 a->diff_algo, a->diff_context, a->ignore_whitespace,
4977 a->force_text_diff, a->diffstat, a->repo, a->outfile);
4981 fd1 = got_opentempfd();
4983 err = got_error_from_errno("got_opentempfd");
4987 if (staged_status == GOT_STATUS_ADD ||
4988 staged_status == GOT_STATUS_MODIFY) {
4990 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
4994 err = got_object_id_str(&id_str, staged_blob_id);
4997 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
4998 err = got_error_from_errno("asprintf");
5003 } else if (status != GOT_STATUS_ADD) {
5004 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192,
5010 if (status != GOT_STATUS_DELETE) {
5011 if (asprintf(&abspath, "%s/%s",
5012 got_worktree_get_root_path(a->worktree), path) == -1) {
5013 err = got_error_from_errno("asprintf");
5018 fd = openat(dirfd, de_name,
5019 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5021 if (!got_err_open_nofollow_on_symlink()) {
5022 err = got_error_from_errno2("openat",
5026 err = get_symlink_target_file(&fd, dirfd,
5032 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5034 if (!got_err_open_nofollow_on_symlink()) {
5035 err = got_error_from_errno2("open",
5039 err = get_symlink_target_file(&fd, dirfd,
5045 if (fstatat(fd, abspath, &sb, AT_SYMLINK_NOFOLLOW) == -1) {
5046 err = got_error_from_errno2("fstatat", abspath);
5049 f2 = fdopen(fd, "r");
5051 err = got_error_from_errno2("fdopen", abspath);
5059 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5065 err = got_diff_blob_file(blob1, a->f1, size1, label1, f2 ? f2 : a->f2,
5066 f2_exists, &sb, path, GOT_DIFF_ALGORITHM_PATIENCE, a->diff_context,
5067 a->ignore_whitespace, a->force_text_diff, a->diffstat, a->outfile);
5069 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5070 err = got_error_from_errno("close");
5071 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5072 err = got_error_from_errno("close");
5074 got_object_blob_close(blob1);
5075 if (fd != -1 && close(fd) == -1 && err == NULL)
5076 err = got_error_from_errno("close");
5077 if (f2 && fclose(f2) == EOF && err == NULL)
5078 err = got_error_from_errno("fclose");
5083 static const struct got_error *
5084 cmd_diff(int argc, char *argv[])
5086 const struct got_error *error;
5087 struct got_repository *repo = NULL;
5088 struct got_worktree *worktree = NULL;
5089 char *cwd = NULL, *repo_path = NULL;
5090 const char *commit_args[2] = { NULL, NULL };
5091 int ncommit_args = 0;
5092 struct got_object_id *ids[2] = { NULL, NULL };
5093 char *labels[2] = { NULL, NULL };
5094 int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
5095 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch, i;
5096 int force_text_diff = 0, force_path = 0, rflag = 0, show_diffstat = 0;
5098 struct got_reflist_head refs;
5099 struct got_pathlist_head diffstat_paths, paths;
5100 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
5101 int fd1 = -1, fd2 = -1;
5102 int *pack_fds = NULL;
5103 struct got_diffstat_cb_arg dsa;
5105 memset(&dsa, 0, sizeof(dsa));
5109 TAILQ_INIT(&diffstat_paths);
5112 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5117 while ((ch = getopt(argc, argv, "aC:c:dPr:sw")) != -1) {
5120 force_text_diff = 1;
5123 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
5126 errx(1, "number of context lines is %s: %s",
5130 if (ncommit_args >= 2)
5131 errx(1, "too many -c options used");
5132 commit_args[ncommit_args++] = optarg;
5141 repo_path = realpath(optarg, NULL);
5142 if (repo_path == NULL)
5143 return got_error_from_errno2("realpath",
5145 got_path_strip_trailing_slashes(repo_path);
5152 ignore_whitespace = 1;
5163 cwd = getcwd(NULL, 0);
5165 error = got_error_from_errno("getcwd");
5169 error = got_repo_pack_fds_open(&pack_fds);
5173 if (repo_path == NULL) {
5174 error = got_worktree_open(&worktree, cwd);
5175 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5181 strdup(got_worktree_get_repo_path(worktree));
5182 if (repo_path == NULL) {
5183 error = got_error_from_errno("strdup");
5187 repo_path = strdup(cwd);
5188 if (repo_path == NULL) {
5189 error = got_error_from_errno("strdup");
5195 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5200 if (show_diffstat) {
5201 dsa.paths = &diffstat_paths;
5202 dsa.force_text = force_text_diff;
5203 dsa.ignore_ws = ignore_whitespace;
5204 dsa.diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
5207 if (rflag || worktree == NULL || ncommit_args > 0) {
5209 error = got_error_msg(GOT_ERR_NOT_IMPL,
5210 "-P option can only be used when diffing "
5215 error = got_error_msg(GOT_ERR_NOT_IMPL,
5216 "-s option can only be used when diffing "
5222 error = apply_unveil(got_repo_get_path(repo), 1,
5223 worktree ? got_worktree_get_root_path(worktree) : NULL);
5227 if ((!force_path && argc == 2) || ncommit_args > 0) {
5228 int obj_type = (ncommit_args > 0 ?
5229 GOT_OBJ_TYPE_COMMIT : GOT_OBJ_TYPE_ANY);
5230 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5234 for (i = 0; i < (ncommit_args > 0 ? ncommit_args : argc); i++) {
5236 if (ncommit_args > 0)
5237 arg = commit_args[i];
5240 error = got_repo_match_object_id(&ids[i], &labels[i],
5241 arg, obj_type, &refs, repo);
5243 if (error->code != GOT_ERR_NOT_REF &&
5244 error->code != GOT_ERR_NO_OBJ)
5246 if (ncommit_args > 0)
5254 f1 = got_opentemp();
5256 error = got_error_from_errno("got_opentemp");
5260 f2 = got_opentemp();
5262 error = got_error_from_errno("got_opentemp");
5266 outfile = got_opentemp();
5267 if (outfile == NULL) {
5268 error = got_error_from_errno("got_opentemp");
5272 if (ncommit_args == 0 && (ids[0] == NULL || ids[1] == NULL)) {
5273 struct print_diff_arg arg;
5276 if (worktree == NULL) {
5277 if (argc == 2 && ids[0] == NULL) {
5278 error = got_error_path(argv[0], GOT_ERR_NO_OBJ);
5280 } else if (argc == 2 && ids[1] == NULL) {
5281 error = got_error_path(argv[1], GOT_ERR_NO_OBJ);
5283 } else if (argc > 0) {
5284 error = got_error_fmt(GOT_ERR_NOT_WORKTREE,
5285 "%s", "specified paths cannot be resolved");
5288 error = got_error(GOT_ERR_NOT_WORKTREE);
5293 error = get_worktree_paths_from_argv(&paths, argc, argv,
5298 error = got_object_id_str(&id_str,
5299 got_worktree_get_base_commit_id(worktree));
5303 arg.worktree = worktree;
5304 arg.diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
5305 arg.diff_context = diff_context;
5306 arg.id_str = id_str;
5307 arg.header_shown = 0;
5308 arg.diff_staged = diff_staged;
5309 arg.ignore_whitespace = ignore_whitespace;
5310 arg.force_text_diff = force_text_diff;
5311 arg.diffstat = show_diffstat ? &dsa : NULL;
5314 arg.outfile = outfile;
5316 error = got_worktree_status(worktree, &paths, repo, 0,
5317 print_diff, &arg, check_cancelled, NULL);
5322 if (show_diffstat && dsa.nfiles > 0) {
5325 if (asprintf(&header, "diffstat %s%s",
5326 diff_staged ? "-s " : "",
5327 got_worktree_get_root_path(worktree)) == -1) {
5328 error = got_error_from_errno("asprintf");
5332 error = print_diffstat(&dsa, header);
5338 error = printfile(outfile);
5342 if (ncommit_args == 1) {
5343 struct got_commit_object *commit;
5344 error = got_object_open_as_commit(&commit, repo, ids[0]);
5348 labels[1] = labels[0];
5350 if (got_object_commit_get_nparents(commit) > 0) {
5351 const struct got_object_id_queue *pids;
5352 struct got_object_qid *pid;
5353 pids = got_object_commit_get_parent_ids(commit);
5354 pid = STAILQ_FIRST(pids);
5355 ids[0] = got_object_id_dup(&pid->id);
5356 if (ids[0] == NULL) {
5357 error = got_error_from_errno(
5358 "got_object_id_dup");
5359 got_object_commit_close(commit);
5362 error = got_object_id_str(&labels[0], ids[0]);
5364 got_object_commit_close(commit);
5369 labels[0] = strdup("/dev/null");
5370 if (labels[0] == NULL) {
5371 error = got_error_from_errno("strdup");
5372 got_object_commit_close(commit);
5377 got_object_commit_close(commit);
5380 if (ncommit_args == 0 && argc > 2) {
5381 error = got_error_msg(GOT_ERR_BAD_PATH,
5382 "path arguments cannot be used when diffing two objects");
5387 error = got_object_get_type(&type1, repo, ids[0]);
5392 error = got_object_get_type(&type2, repo, ids[1]);
5395 if (type1 != GOT_OBJ_TYPE_ANY && type1 != type2) {
5396 error = got_error(GOT_ERR_OBJ_TYPE);
5399 if (type1 == GOT_OBJ_TYPE_BLOB && argc > 2) {
5400 error = got_error_msg(GOT_ERR_OBJ_TYPE,
5401 "path arguments cannot be used when diffing blobs");
5405 for (i = 0; ncommit_args > 0 && i < argc; i++) {
5407 struct got_pathlist_entry *new;
5411 error = got_worktree_resolve_path(&p, worktree,
5415 prefix = got_worktree_get_path_prefix(worktree);
5416 while (prefix[0] == '/')
5418 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5419 (p[0] != '\0' && prefix[0] != '\0') ? "/" : "",
5421 error = got_error_from_errno("asprintf");
5427 char *mapped_path, *s;
5428 error = got_repo_map_path(&mapped_path, repo, argv[i]);
5434 in_repo_path = strdup(s);
5435 if (in_repo_path == NULL) {
5436 error = got_error_from_errno("asprintf");
5443 error = got_pathlist_insert(&new, &paths, in_repo_path, NULL);
5444 if (error || new == NULL /* duplicate */)
5451 /* Release work tree lock. */
5452 got_worktree_close(worktree);
5456 fd1 = got_opentempfd();
5458 error = got_error_from_errno("got_opentempfd");
5462 fd2 = got_opentempfd();
5464 error = got_error_from_errno("got_opentempfd");
5468 switch (type1 == GOT_OBJ_TYPE_ANY ? type2 : type1) {
5469 case GOT_OBJ_TYPE_BLOB:
5470 error = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5471 fd1, fd2, ids[0], ids[1], NULL, NULL,
5472 GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
5473 ignore_whitespace, force_text_diff,
5474 show_diffstat ? &dsa : NULL, repo, outfile);
5476 case GOT_OBJ_TYPE_TREE:
5477 error = got_diff_objects_as_trees(NULL, NULL, f1, f2, fd1, fd2,
5478 ids[0], ids[1], &paths, "", "",
5479 GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
5480 ignore_whitespace, force_text_diff,
5481 show_diffstat ? &dsa : NULL, repo, outfile);
5483 case GOT_OBJ_TYPE_COMMIT:
5484 fprintf(outfile, "diff %s %s\n", labels[0], labels[1]);
5485 error = got_diff_objects_as_commits(NULL, NULL, f1, f2,
5486 fd1, fd2, ids[0], ids[1], &paths,
5487 GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
5488 ignore_whitespace, force_text_diff,
5489 show_diffstat ? &dsa : NULL, repo, outfile);
5492 error = got_error(GOT_ERR_OBJ_TYPE);
5497 if (show_diffstat && dsa.nfiles > 0) {
5498 char *header = NULL;
5500 if (asprintf(&header, "diffstat %s %s",
5501 labels[0], labels[1]) == -1) {
5502 error = got_error_from_errno("asprintf");
5506 error = print_diffstat(&dsa, header);
5512 error = printfile(outfile);
5520 got_worktree_close(worktree);
5522 const struct got_error *close_err = got_repo_close(repo);
5527 const struct got_error *pack_err =
5528 got_repo_pack_fds_close(pack_fds);
5532 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
5533 got_pathlist_free(&diffstat_paths, GOT_PATHLIST_FREE_ALL);
5534 got_ref_list_free(&refs);
5535 if (outfile && fclose(outfile) == EOF && error == NULL)
5536 error = got_error_from_errno("fclose");
5537 if (f1 && fclose(f1) == EOF && error == NULL)
5538 error = got_error_from_errno("fclose");
5539 if (f2 && fclose(f2) == EOF && error == NULL)
5540 error = got_error_from_errno("fclose");
5541 if (fd1 != -1 && close(fd1) == -1 && error == NULL)
5542 error = got_error_from_errno("close");
5543 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
5544 error = got_error_from_errno("close");
5552 "usage: %s blame [-c commit] [-r repository-path] path\n",
5561 char datebuf[11]; /* YYYY-MM-DD + NUL */
5564 struct blame_cb_args {
5565 struct blame_line *lines;
5569 off_t *line_offsets;
5571 struct got_repository *repo;
5574 static const struct got_error *
5575 blame_cb(void *arg, int nlines, int lineno,
5576 struct got_commit_object *commit, struct got_object_id *id)
5578 const struct got_error *err = NULL;
5579 struct blame_cb_args *a = arg;
5580 struct blame_line *bline;
5582 size_t linesize = 0;
5585 time_t committer_time;
5587 if (nlines != a->nlines ||
5588 (lineno != -1 && lineno < 1) || lineno > a->nlines)
5589 return got_error(GOT_ERR_RANGE);
5591 if (sigint_received)
5592 return got_error(GOT_ERR_ITER_COMPLETED);
5595 return NULL; /* no change in this commit */
5597 /* Annotate this line. */
5598 bline = &a->lines[lineno - 1];
5599 if (bline->annotated)
5601 err = got_object_id_str(&bline->id_str, id);
5605 bline->committer = strdup(got_object_commit_get_committer(commit));
5606 if (bline->committer == NULL) {
5607 err = got_error_from_errno("strdup");
5611 committer_time = got_object_commit_get_committer_time(commit);
5612 if (gmtime_r(&committer_time, &tm) == NULL)
5613 return got_error_from_errno("gmtime_r");
5614 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
5616 err = got_error(GOT_ERR_NO_SPACE);
5619 bline->annotated = 1;
5621 /* Print lines annotated so far. */
5622 bline = &a->lines[a->lineno_cur - 1];
5623 if (!bline->annotated)
5626 offset = a->line_offsets[a->lineno_cur - 1];
5627 if (fseeko(a->f, offset, SEEK_SET) == -1) {
5628 err = got_error_from_errno("fseeko");
5632 while (a->lineno_cur <= a->nlines && bline->annotated) {
5633 char *smallerthan, *at, *nl, *committer;
5636 if (getline(&line, &linesize, a->f) == -1) {
5638 err = got_error_from_errno("getline");
5642 committer = bline->committer;
5643 smallerthan = strchr(committer, '<');
5644 if (smallerthan && smallerthan[1] != '\0')
5645 committer = smallerthan + 1;
5646 at = strchr(committer, '@');
5649 len = strlen(committer);
5651 committer[8] = '\0';
5653 nl = strchr(line, '\n');
5656 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
5657 bline->id_str, bline->datebuf, committer, line);
5660 bline = &a->lines[a->lineno_cur - 1];
5667 static const struct got_error *
5668 cmd_blame(int argc, char *argv[])
5670 const struct got_error *error;
5671 struct got_repository *repo = NULL;
5672 struct got_worktree *worktree = NULL;
5673 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5674 char *link_target = NULL;
5675 struct got_object_id *obj_id = NULL;
5676 struct got_object_id *commit_id = NULL;
5677 struct got_commit_object *commit = NULL;
5678 struct got_blob_object *blob = NULL;
5679 char *commit_id_str = NULL;
5680 struct blame_cb_args bca;
5681 int ch, obj_type, i, fd1 = -1, fd2 = -1, fd3 = -1;
5683 int *pack_fds = NULL;
5684 FILE *f1 = NULL, *f2 = NULL;
5686 fd1 = got_opentempfd();
5688 return got_error_from_errno("got_opentempfd");
5690 memset(&bca, 0, sizeof(bca));
5693 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5698 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5701 commit_id_str = optarg;
5704 repo_path = realpath(optarg, NULL);
5705 if (repo_path == NULL)
5706 return got_error_from_errno2("realpath",
5708 got_path_strip_trailing_slashes(repo_path);
5724 cwd = getcwd(NULL, 0);
5726 error = got_error_from_errno("getcwd");
5730 error = got_repo_pack_fds_open(&pack_fds);
5734 if (repo_path == NULL) {
5735 error = got_worktree_open(&worktree, cwd);
5736 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5742 strdup(got_worktree_get_repo_path(worktree));
5743 if (repo_path == NULL) {
5744 error = got_error_from_errno("strdup");
5749 repo_path = strdup(cwd);
5750 if (repo_path == NULL) {
5751 error = got_error_from_errno("strdup");
5757 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5762 const char *prefix = got_worktree_get_path_prefix(worktree);
5765 error = got_worktree_resolve_path(&p, worktree, path);
5768 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5769 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
5771 error = got_error_from_errno("asprintf");
5776 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5778 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5781 error = got_repo_map_path(&in_repo_path, repo, path);
5786 if (commit_id_str == NULL) {
5787 struct got_reference *head_ref;
5788 error = got_ref_open(&head_ref, repo, worktree ?
5789 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5792 error = got_ref_resolve(&commit_id, repo, head_ref);
5793 got_ref_close(head_ref);
5797 struct got_reflist_head refs;
5799 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5803 error = got_repo_match_object_id(&commit_id, NULL,
5804 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5805 got_ref_list_free(&refs);
5811 /* Release work tree lock. */
5812 got_worktree_close(worktree);
5816 error = got_object_open_as_commit(&commit, repo, commit_id);
5820 error = got_object_resolve_symlinks(&link_target, in_repo_path,
5825 error = got_object_id_by_path(&obj_id, repo, commit,
5826 link_target ? link_target : in_repo_path);
5830 error = got_object_get_type(&obj_type, repo, obj_id);
5834 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5835 error = got_error_path(link_target ? link_target : in_repo_path,
5840 error = got_object_open_as_blob(&blob, repo, obj_id, 8192, fd1);
5843 bca.f = got_opentemp();
5844 if (bca.f == NULL) {
5845 error = got_error_from_errno("got_opentemp");
5848 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
5849 &bca.line_offsets, bca.f, blob);
5850 if (error || bca.nlines == 0)
5853 /* Don't include \n at EOF in the blame line count. */
5854 if (bca.line_offsets[bca.nlines - 1] == filesize)
5857 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
5858 if (bca.lines == NULL) {
5859 error = got_error_from_errno("calloc");
5863 bca.nlines_prec = 0;
5871 fd2 = got_opentempfd();
5873 error = got_error_from_errno("got_opentempfd");
5876 fd3 = got_opentempfd();
5878 error = got_error_from_errno("got_opentempfd");
5881 f1 = got_opentemp();
5883 error = got_error_from_errno("got_opentemp");
5886 f2 = got_opentemp();
5888 error = got_error_from_errno("got_opentemp");
5891 error = got_blame(link_target ? link_target : in_repo_path, commit_id,
5892 repo, GOT_DIFF_ALGORITHM_PATIENCE, blame_cb, &bca,
5893 check_cancelled, NULL, fd2, fd3, f1, f2);
5902 got_object_commit_close(commit);
5904 if (fd1 != -1 && close(fd1) == -1 && error == NULL)
5905 error = got_error_from_errno("close");
5906 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
5907 error = got_error_from_errno("close");
5908 if (fd3 != -1 && close(fd3) == -1 && error == NULL)
5909 error = got_error_from_errno("close");
5910 if (f1 && fclose(f1) == EOF && error == NULL)
5911 error = got_error_from_errno("fclose");
5912 if (f2 && fclose(f2) == EOF && error == NULL)
5913 error = got_error_from_errno("fclose");
5916 got_object_blob_close(blob);
5918 got_worktree_close(worktree);
5920 const struct got_error *close_err = got_repo_close(repo);
5925 const struct got_error *pack_err =
5926 got_repo_pack_fds_close(pack_fds);
5931 for (i = 0; i < bca.nlines; i++) {
5932 struct blame_line *bline = &bca.lines[i];
5933 free(bline->id_str);
5934 free(bline->committer);
5938 free(bca.line_offsets);
5939 if (bca.f && fclose(bca.f) == EOF && error == NULL)
5940 error = got_error_from_errno("fclose");
5947 fprintf(stderr, "usage: %s tree [-iR] [-c commit] [-r repository-path] "
5948 "[path]\n", getprogname());
5952 static const struct got_error *
5953 print_entry(struct got_tree_entry *te, const char *id, const char *path,
5954 const char *root_path, struct got_repository *repo)
5956 const struct got_error *err = NULL;
5957 int is_root_path = (strcmp(path, root_path) == 0);
5958 const char *modestr = "";
5959 mode_t mode = got_tree_entry_get_mode(te);
5960 char *link_target = NULL;
5962 path += strlen(root_path);
5963 while (path[0] == '/')
5966 if (got_object_tree_entry_is_submodule(te))
5968 else if (S_ISLNK(mode)) {
5971 err = got_tree_entry_get_symlink_target(&link_target, te, repo);
5974 for (i = 0; link_target[i] != '\0'; i++) {
5975 if (!isprint((unsigned char)link_target[i]))
5976 link_target[i] = '?';
5981 else if (S_ISDIR(mode))
5983 else if (mode & S_IXUSR)
5986 printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
5987 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
5988 link_target ? " -> ": "", link_target ? link_target : "");
5994 static const struct got_error *
5995 print_tree(const char *path, struct got_commit_object *commit,
5996 int show_ids, int recurse, const char *root_path,
5997 struct got_repository *repo)
5999 const struct got_error *err = NULL;
6000 struct got_object_id *tree_id = NULL;
6001 struct got_tree_object *tree = NULL;
6004 err = got_object_id_by_path(&tree_id, repo, commit, path);
6008 err = got_object_open_as_tree(&tree, repo, tree_id);
6011 nentries = got_object_tree_get_nentries(tree);
6012 for (i = 0; i < nentries; i++) {
6013 struct got_tree_entry *te;
6016 if (sigint_received || sigpipe_received)
6019 te = got_object_tree_get_entry(tree, i);
6022 err = got_object_id_str(&id_str,
6023 got_tree_entry_get_id(te));
6026 if (asprintf(&id, "%s ", id_str) == -1) {
6027 err = got_error_from_errno("asprintf");
6033 err = print_entry(te, id, path, root_path, repo);
6038 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
6040 if (asprintf(&child_path, "%s%s%s", path,
6041 path[0] == '/' && path[1] == '\0' ? "" : "/",
6042 got_tree_entry_get_name(te)) == -1) {
6043 err = got_error_from_errno("asprintf");
6046 err = print_tree(child_path, commit, show_ids, 1,
6055 got_object_tree_close(tree);
6060 static const struct got_error *
6061 cmd_tree(int argc, char *argv[])
6063 const struct got_error *error;
6064 struct got_repository *repo = NULL;
6065 struct got_worktree *worktree = NULL;
6066 const char *path, *refname = NULL;
6067 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6068 struct got_object_id *commit_id = NULL;
6069 struct got_commit_object *commit = NULL;
6070 char *commit_id_str = NULL;
6071 int show_ids = 0, recurse = 0;
6073 int *pack_fds = NULL;
6076 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6081 while ((ch = getopt(argc, argv, "c:iRr:")) != -1) {
6084 commit_id_str = optarg;
6093 repo_path = realpath(optarg, NULL);
6094 if (repo_path == NULL)
6095 return got_error_from_errno2("realpath",
6097 got_path_strip_trailing_slashes(repo_path);
6115 cwd = getcwd(NULL, 0);
6117 error = got_error_from_errno("getcwd");
6121 error = got_repo_pack_fds_open(&pack_fds);
6125 if (repo_path == NULL) {
6126 error = got_worktree_open(&worktree, cwd);
6127 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6133 strdup(got_worktree_get_repo_path(worktree));
6134 if (repo_path == NULL)
6135 error = got_error_from_errno("strdup");
6139 repo_path = strdup(cwd);
6140 if (repo_path == NULL) {
6141 error = got_error_from_errno("strdup");
6147 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6152 const char *prefix = got_worktree_get_path_prefix(worktree);
6155 if (path == NULL || got_path_is_root_dir(path))
6157 error = got_worktree_resolve_path(&p, worktree, path);
6160 if (asprintf(&in_repo_path, "%s%s%s", prefix,
6161 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
6163 error = got_error_from_errno("asprintf");
6168 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6172 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6177 error = got_repo_map_path(&in_repo_path, repo, path);
6182 if (commit_id_str == NULL) {
6183 struct got_reference *head_ref;
6185 refname = got_worktree_get_head_ref_name(worktree);
6187 refname = GOT_REF_HEAD;
6188 error = got_ref_open(&head_ref, repo, refname, 0);
6191 error = got_ref_resolve(&commit_id, repo, head_ref);
6192 got_ref_close(head_ref);
6196 struct got_reflist_head refs;
6198 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
6202 error = got_repo_match_object_id(&commit_id, NULL,
6203 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
6204 got_ref_list_free(&refs);
6210 /* Release work tree lock. */
6211 got_worktree_close(worktree);
6215 error = got_object_open_as_commit(&commit, repo, commit_id);
6219 error = print_tree(in_repo_path, commit, show_ids, recurse,
6220 in_repo_path, repo);
6227 got_object_commit_close(commit);
6229 got_worktree_close(worktree);
6231 const struct got_error *close_err = got_repo_close(repo);
6236 const struct got_error *pack_err =
6237 got_repo_pack_fds_close(pack_fds);
6247 fprintf(stderr, "usage: %s status [-I] [-S status-codes] "
6248 "[-s status-codes] [path ...]\n", getprogname());
6252 struct got_status_arg {
6257 static const struct got_error *
6258 print_status(void *arg, unsigned char status, unsigned char staged_status,
6259 const char *path, struct got_object_id *blob_id,
6260 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6261 int dirfd, const char *de_name)
6263 struct got_status_arg *st = arg;
6265 if (status == staged_status && (status == GOT_STATUS_DELETE))
6266 status = GOT_STATUS_NO_CHANGE;
6267 if (st != NULL && st->status_codes) {
6268 size_t ncodes = strlen(st->status_codes);
6271 for (i = 0; i < ncodes ; i++) {
6273 if (status == st->status_codes[i] ||
6274 staged_status == st->status_codes[i]) {
6279 if (status == st->status_codes[i] ||
6280 staged_status == st->status_codes[i])
6285 if (st->suppress && j == 0)
6292 printf("%c%c %s\n", status, staged_status, path);
6296 static const struct got_error *
6297 cmd_status(int argc, char *argv[])
6299 const struct got_error *error = NULL;
6300 struct got_repository *repo = NULL;
6301 struct got_worktree *worktree = NULL;
6302 struct got_status_arg st;
6304 struct got_pathlist_head paths;
6305 int ch, i, no_ignores = 0;
6306 int *pack_fds = NULL;
6310 memset(&st, 0, sizeof(st));
6311 st.status_codes = NULL;
6315 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6320 while ((ch = getopt(argc, argv, "IS:s:")) != -1) {
6326 if (st.status_codes != NULL && st.suppress == 0)
6327 option_conflict('S', 's');
6331 for (i = 0; optarg[i] != '\0'; i++) {
6332 switch (optarg[i]) {
6333 case GOT_STATUS_MODIFY:
6334 case GOT_STATUS_ADD:
6335 case GOT_STATUS_DELETE:
6336 case GOT_STATUS_CONFLICT:
6337 case GOT_STATUS_MISSING:
6338 case GOT_STATUS_OBSTRUCTED:
6339 case GOT_STATUS_UNVERSIONED:
6340 case GOT_STATUS_MODE_CHANGE:
6341 case GOT_STATUS_NONEXISTENT:
6344 errx(1, "invalid status code '%c'",
6348 if (ch == 's' && st.suppress)
6349 option_conflict('s', 'S');
6350 st.status_codes = optarg;
6361 cwd = getcwd(NULL, 0);
6363 error = got_error_from_errno("getcwd");
6367 error = got_repo_pack_fds_open(&pack_fds);
6371 error = got_worktree_open(&worktree, cwd);
6373 if (error->code == GOT_ERR_NOT_WORKTREE)
6374 error = wrap_not_worktree_error(error, "status", cwd);
6378 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6383 error = apply_unveil(got_repo_get_path(repo), 1,
6384 got_worktree_get_root_path(worktree));
6388 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6392 error = got_worktree_status(worktree, &paths, repo, no_ignores,
6393 print_status, &st, check_cancelled, NULL);
6396 const struct got_error *pack_err =
6397 got_repo_pack_fds_close(pack_fds);
6402 const struct got_error *close_err = got_repo_close(repo);
6407 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
6415 fprintf(stderr, "usage: %s ref [-dlt] [-c object] [-r repository-path] "
6416 "[-s reference] [name]\n", getprogname());
6420 static const struct got_error *
6421 list_refs(struct got_repository *repo, const char *refname, int sort_by_time)
6423 static const struct got_error *err = NULL;
6424 struct got_reflist_head refs;
6425 struct got_reflist_entry *re;
6428 err = got_ref_list(&refs, repo, refname, sort_by_time ?
6429 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6434 TAILQ_FOREACH(re, &refs, entry) {
6436 refstr = got_ref_to_str(re->ref);
6437 if (refstr == NULL) {
6438 err = got_error_from_errno("got_ref_to_str");
6441 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
6445 got_ref_list_free(&refs);
6449 static const struct got_error *
6450 delete_ref_by_name(struct got_repository *repo, const char *refname)
6452 const struct got_error *err;
6453 struct got_reference *ref;
6455 err = got_ref_open(&ref, repo, refname, 0);
6459 err = delete_ref(repo, ref);
6464 static const struct got_error *
6465 add_ref(struct got_repository *repo, const char *refname, const char *target)
6467 const struct got_error *err = NULL;
6468 struct got_object_id *id = NULL;
6469 struct got_reference *ref = NULL;
6470 struct got_reflist_head refs;
6473 * Don't let the user create a reference name with a leading '-'.
6474 * While technically a valid reference name, this case is usually
6475 * an unintended typo.
6477 if (refname[0] == '-')
6478 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
6481 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
6484 err = got_repo_match_object_id(&id, NULL, target, GOT_OBJ_TYPE_ANY,
6486 got_ref_list_free(&refs);
6490 err = got_ref_alloc(&ref, refname, id);
6494 err = got_ref_write(ref, repo);
6502 static const struct got_error *
6503 add_symref(struct got_repository *repo, const char *refname, const char *target)
6505 const struct got_error *err = NULL;
6506 struct got_reference *ref = NULL;
6507 struct got_reference *target_ref = NULL;
6510 * Don't let the user create a reference name with a leading '-'.
6511 * While technically a valid reference name, this case is usually
6512 * an unintended typo.
6514 if (refname[0] == '-')
6515 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
6517 err = got_ref_open(&target_ref, repo, target, 0);
6521 err = got_ref_alloc_symref(&ref, refname, target_ref);
6525 err = got_ref_write(ref, repo);
6528 got_ref_close(target_ref);
6534 static const struct got_error *
6535 cmd_ref(int argc, char *argv[])
6537 const struct got_error *error = NULL;
6538 struct got_repository *repo = NULL;
6539 struct got_worktree *worktree = NULL;
6540 char *cwd = NULL, *repo_path = NULL;
6541 int ch, do_list = 0, do_delete = 0, sort_by_time = 0;
6542 const char *obj_arg = NULL, *symref_target= NULL;
6543 char *refname = NULL;
6544 int *pack_fds = NULL;
6547 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6548 "sendfd unveil", NULL) == -1)
6552 while ((ch = getopt(argc, argv, "c:dlr:s:t")) != -1) {
6564 repo_path = realpath(optarg, NULL);
6565 if (repo_path == NULL)
6566 return got_error_from_errno2("realpath",
6568 got_path_strip_trailing_slashes(repo_path);
6571 symref_target = optarg;
6582 if (obj_arg && do_list)
6583 option_conflict('c', 'l');
6584 if (obj_arg && do_delete)
6585 option_conflict('c', 'd');
6586 if (obj_arg && symref_target)
6587 option_conflict('c', 's');
6588 if (symref_target && do_delete)
6589 option_conflict('s', 'd');
6590 if (symref_target && do_list)
6591 option_conflict('s', 'l');
6592 if (do_delete && do_list)
6593 option_conflict('d', 'l');
6594 if (sort_by_time && !do_list)
6595 errx(1, "-t option requires -l option");
6601 if (argc != 0 && argc != 1)
6604 refname = strdup(argv[0]);
6605 if (refname == NULL) {
6606 error = got_error_from_errno("strdup");
6613 refname = strdup(argv[0]);
6614 if (refname == NULL) {
6615 error = got_error_from_errno("strdup");
6621 got_path_strip_trailing_slashes(refname);
6623 cwd = getcwd(NULL, 0);
6625 error = got_error_from_errno("getcwd");
6629 error = got_repo_pack_fds_open(&pack_fds);
6633 if (repo_path == NULL) {
6634 error = got_worktree_open(&worktree, cwd);
6635 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6641 strdup(got_worktree_get_repo_path(worktree));
6642 if (repo_path == NULL)
6643 error = got_error_from_errno("strdup");
6647 repo_path = strdup(cwd);
6648 if (repo_path == NULL) {
6649 error = got_error_from_errno("strdup");
6655 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6661 /* Remove "cpath" promise. */
6662 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6668 error = apply_unveil(got_repo_get_path(repo), do_list,
6669 worktree ? got_worktree_get_root_path(worktree) : NULL);
6674 error = list_refs(repo, refname, sort_by_time);
6676 error = delete_ref_by_name(repo, refname);
6677 else if (symref_target)
6678 error = add_symref(repo, refname, symref_target);
6680 if (obj_arg == NULL)
6682 error = add_ref(repo, refname, obj_arg);
6687 const struct got_error *close_err = got_repo_close(repo);
6692 got_worktree_close(worktree);
6694 const struct got_error *pack_err =
6695 got_repo_pack_fds_close(pack_fds);
6707 fprintf(stderr, "usage: %s branch [-lnt] [-c commit] [-d name] "
6708 "[-r repository-path] [name]\n", getprogname());
6712 static const struct got_error *
6713 list_branch(struct got_repository *repo, struct got_worktree *worktree,
6714 struct got_reference *ref)
6716 const struct got_error *err = NULL;
6717 const char *refname, *marker = " ";
6720 refname = got_ref_get_name(ref);
6721 if (worktree && strcmp(refname,
6722 got_worktree_get_head_ref_name(worktree)) == 0) {
6723 struct got_object_id *id = NULL;
6725 err = got_ref_resolve(&id, repo, ref);
6728 if (got_object_id_cmp(id,
6729 got_worktree_get_base_commit_id(worktree)) == 0)
6736 if (strncmp(refname, "refs/heads/", 11) == 0)
6738 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
6740 if (strncmp(refname, "refs/remotes/", 13) == 0)
6743 refstr = got_ref_to_str(ref);
6745 return got_error_from_errno("got_ref_to_str");
6747 printf("%s%s: %s\n", marker, refname, refstr);
6752 static const struct got_error *
6753 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
6755 const char *refname;
6757 if (worktree == NULL)
6758 return got_error(GOT_ERR_NOT_WORKTREE);
6760 refname = got_worktree_get_head_ref_name(worktree);
6762 if (strncmp(refname, "refs/heads/", 11) == 0)
6764 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
6767 printf("%s\n", refname);
6772 static const struct got_error *
6773 list_branches(struct got_repository *repo, struct got_worktree *worktree,
6776 static const struct got_error *err = NULL;
6777 struct got_reflist_head refs;
6778 struct got_reflist_entry *re;
6779 struct got_reference *temp_ref = NULL;
6780 int rebase_in_progress, histedit_in_progress;
6785 err = got_worktree_rebase_in_progress(&rebase_in_progress,
6790 err = got_worktree_histedit_in_progress(&histedit_in_progress,
6795 if (rebase_in_progress || histedit_in_progress) {
6796 err = got_ref_open(&temp_ref, repo,
6797 got_worktree_get_head_ref_name(worktree), 0);
6800 list_branch(repo, worktree, temp_ref);
6801 got_ref_close(temp_ref);
6805 err = got_ref_list(&refs, repo, "refs/heads", sort_by_time ?
6806 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6811 TAILQ_FOREACH(re, &refs, entry)
6812 list_branch(repo, worktree, re->ref);
6814 got_ref_list_free(&refs);
6816 err = got_ref_list(&refs, repo, "refs/remotes", sort_by_time ?
6817 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6822 TAILQ_FOREACH(re, &refs, entry)
6823 list_branch(repo, worktree, re->ref);
6825 got_ref_list_free(&refs);
6830 static const struct got_error *
6831 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
6832 const char *branch_name)
6834 const struct got_error *err = NULL;
6835 struct got_reference *ref = NULL;
6836 char *refname, *remote_refname = NULL;
6838 if (strncmp(branch_name, "refs/", 5) == 0)
6840 if (strncmp(branch_name, "heads/", 6) == 0)
6842 else if (strncmp(branch_name, "remotes/", 8) == 0)
6845 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
6846 return got_error_from_errno("asprintf");
6848 if (asprintf(&remote_refname, "refs/remotes/%s",
6849 branch_name) == -1) {
6850 err = got_error_from_errno("asprintf");
6854 err = got_ref_open(&ref, repo, refname, 0);
6856 const struct got_error *err2;
6857 if (err->code != GOT_ERR_NOT_REF)
6860 * Keep 'err' intact such that if neither branch exists
6861 * we report "refs/heads" rather than "refs/remotes" in
6862 * our error message.
6864 err2 = got_ref_open(&ref, repo, remote_refname, 0);
6871 strcmp(got_worktree_get_head_ref_name(worktree),
6872 got_ref_get_name(ref)) == 0) {
6873 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6874 "will not delete this work tree's current branch");
6878 err = delete_ref(repo, ref);
6883 free(remote_refname);
6887 static const struct got_error *
6888 add_branch(struct got_repository *repo, const char *branch_name,
6889 struct got_object_id *base_commit_id)
6891 const struct got_error *err = NULL;
6892 struct got_reference *ref = NULL;
6893 char *refname = NULL;
6896 * Don't let the user create a branch name with a leading '-'.
6897 * While technically a valid reference name, this case is usually
6898 * an unintended typo.
6900 if (branch_name[0] == '-')
6901 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
6903 if (strncmp(branch_name, "refs/heads/", 11) == 0)
6906 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
6907 err = got_error_from_errno("asprintf");
6911 err = got_ref_open(&ref, repo, refname, 0);
6913 err = got_error(GOT_ERR_BRANCH_EXISTS);
6915 } else if (err->code != GOT_ERR_NOT_REF)
6918 err = got_ref_alloc(&ref, refname, base_commit_id);
6922 err = got_ref_write(ref, repo);
6930 static const struct got_error *
6931 cmd_branch(int argc, char *argv[])
6933 const struct got_error *error = NULL;
6934 struct got_repository *repo = NULL;
6935 struct got_worktree *worktree = NULL;
6936 char *cwd = NULL, *repo_path = NULL;
6937 int ch, do_list = 0, do_show = 0, do_update = 1, sort_by_time = 0;
6938 const char *delref = NULL, *commit_id_arg = NULL;
6939 struct got_reference *ref = NULL;
6940 struct got_pathlist_head paths;
6941 struct got_object_id *commit_id = NULL;
6942 char *commit_id_str = NULL;
6943 int *pack_fds = NULL;
6948 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6949 "sendfd unveil", NULL) == -1)
6953 while ((ch = getopt(argc, argv, "c:d:lnr:t")) != -1) {
6956 commit_id_arg = optarg;
6968 repo_path = realpath(optarg, NULL);
6969 if (repo_path == NULL)
6970 return got_error_from_errno2("realpath",
6972 got_path_strip_trailing_slashes(repo_path);
6983 if (do_list && delref)
6984 option_conflict('l', 'd');
6985 if (sort_by_time && !do_list)
6986 errx(1, "-t option requires -l option");
6991 if (!do_list && !delref && argc == 0)
6994 if ((do_list || delref || do_show) && commit_id_arg != NULL)
6995 errx(1, "-c option can only be used when creating a branch");
6997 if (do_list || delref) {
7000 } else if (!do_show && argc != 1)
7003 cwd = getcwd(NULL, 0);
7005 error = got_error_from_errno("getcwd");
7009 error = got_repo_pack_fds_open(&pack_fds);
7013 if (repo_path == NULL) {
7014 error = got_worktree_open(&worktree, cwd);
7015 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7021 strdup(got_worktree_get_repo_path(worktree));
7022 if (repo_path == NULL)
7023 error = got_error_from_errno("strdup");
7027 repo_path = strdup(cwd);
7028 if (repo_path == NULL) {
7029 error = got_error_from_errno("strdup");
7035 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7040 if (do_list || do_show) {
7041 /* Remove "cpath" promise. */
7042 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
7048 error = apply_unveil(got_repo_get_path(repo), do_list,
7049 worktree ? got_worktree_get_root_path(worktree) : NULL);
7054 error = show_current_branch(repo, worktree);
7056 error = list_branches(repo, worktree, sort_by_time);
7058 error = delete_branch(repo, worktree, delref);
7060 struct got_reflist_head refs;
7062 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
7066 if (commit_id_arg == NULL)
7067 commit_id_arg = worktree ?
7068 got_worktree_get_head_ref_name(worktree) :
7070 error = got_repo_match_object_id(&commit_id, NULL,
7071 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &refs, repo);
7072 got_ref_list_free(&refs);
7075 error = add_branch(repo, argv[0], commit_id);
7078 if (worktree && do_update) {
7079 struct got_update_progress_arg upa;
7080 char *branch_refname = NULL;
7082 error = got_object_id_str(&commit_id_str, commit_id);
7085 error = get_worktree_paths_from_argv(&paths, 0, NULL,
7089 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
7091 error = got_error_from_errno("asprintf");
7094 error = got_ref_open(&ref, repo, branch_refname, 0);
7095 free(branch_refname);
7098 error = switch_head_ref(ref, commit_id, worktree,
7102 error = got_worktree_set_base_commit_id(worktree, repo,
7106 memset(&upa, 0, sizeof(upa));
7107 error = got_worktree_checkout_files(worktree, &paths,
7108 repo, update_progress, &upa, check_cancelled,
7112 if (upa.did_something) {
7113 printf("Updated to %s: %s\n",
7114 got_worktree_get_head_ref_name(worktree),
7117 print_update_progress_stats(&upa);
7124 const struct got_error *close_err = got_repo_close(repo);
7129 got_worktree_close(worktree);
7131 const struct got_error *pack_err =
7132 got_repo_pack_fds_close(pack_fds);
7139 free(commit_id_str);
7140 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
7148 fprintf(stderr, "usage: %s tag [-lVv] [-c commit] [-m message] "
7149 "[-r repository-path] [-s signer-id] name\n", getprogname());
7154 static const struct got_error *
7155 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
7157 const struct got_error *err = NULL;
7158 struct got_reflist_entry *re, *se, *new;
7159 struct got_object_id *re_id, *se_id;
7160 struct got_tag_object *re_tag, *se_tag;
7161 time_t re_time, se_time;
7163 STAILQ_FOREACH(re, tags, entry) {
7164 se = STAILQ_FIRST(sorted);
7166 err = got_reflist_entry_dup(&new, re);
7169 STAILQ_INSERT_HEAD(sorted, new, entry);
7172 err = got_ref_resolve(&re_id, repo, re->ref);
7175 err = got_object_open_as_tag(&re_tag, repo, re_id);
7179 re_time = got_object_tag_get_tagger_time(re_tag);
7180 got_object_tag_close(re_tag);
7184 err = got_ref_resolve(&se_id, repo, re->ref);
7187 err = got_object_open_as_tag(&se_tag, repo, se_id);
7191 se_time = got_object_tag_get_tagger_time(se_tag);
7192 got_object_tag_close(se_tag);
7194 if (se_time > re_time) {
7195 err = got_reflist_entry_dup(&new, re);
7198 STAILQ_INSERT_AFTER(sorted, se, new, entry);
7201 se = STAILQ_NEXT(se, entry);
7210 static const struct got_error *
7211 get_tag_refname(char **refname, const char *tag_name)
7213 const struct got_error *err;
7215 if (strncmp("refs/tags/", tag_name, 10) == 0) {
7216 *refname = strdup(tag_name);
7217 if (*refname == NULL)
7218 return got_error_from_errno("strdup");
7219 } else if (asprintf(refname, "refs/tags/%s", tag_name) == -1) {
7220 err = got_error_from_errno("asprintf");
7228 static const struct got_error *
7229 list_tags(struct got_repository *repo, const char *tag_name, int verify_tags,
7230 const char *allowed_signers, const char *revoked_signers, int verbosity)
7232 static const struct got_error *err = NULL;
7233 struct got_reflist_head refs;
7234 struct got_reflist_entry *re;
7235 char *wanted_refname = NULL;
7240 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
7245 struct got_reference *ref;
7246 err = get_tag_refname(&wanted_refname, tag_name);
7249 /* Wanted tag reference should exist. */
7250 err = got_ref_open(&ref, repo, wanted_refname, 0);
7256 TAILQ_FOREACH(re, &refs, entry) {
7257 const char *refname;
7258 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
7260 const char *tagger, *ssh_sig = NULL;
7261 char *sig_msg = NULL;
7263 struct got_object_id *id;
7264 struct got_tag_object *tag;
7265 struct got_commit_object *commit = NULL;
7267 refname = got_ref_get_name(re->ref);
7268 if (strncmp(refname, "refs/tags/", 10) != 0 ||
7269 (wanted_refname && strcmp(refname, wanted_refname) != 0))
7272 refstr = got_ref_to_str(re->ref);
7273 if (refstr == NULL) {
7274 err = got_error_from_errno("got_ref_to_str");
7278 err = got_ref_resolve(&id, repo, re->ref);
7281 err = got_object_open_as_tag(&tag, repo, id);
7283 if (err->code != GOT_ERR_OBJ_TYPE) {
7287 /* "lightweight" tag */
7288 err = got_object_open_as_commit(&commit, repo, id);
7293 tagger = got_object_commit_get_committer(commit);
7295 got_object_commit_get_committer_time(commit);
7296 err = got_object_id_str(&id_str, id);
7302 tagger = got_object_tag_get_tagger(tag);
7303 tagger_time = got_object_tag_get_tagger_time(tag);
7304 err = got_object_id_str(&id_str,
7305 got_object_tag_get_object_id(tag));
7310 if (tag && verify_tags) {
7311 ssh_sig = got_sigs_get_tagmsg_ssh_signature(
7312 got_object_tag_get_message(tag));
7313 if (ssh_sig && allowed_signers == NULL) {
7314 err = got_error_msg(
7315 GOT_ERR_VERIFY_TAG_SIGNATURE,
7316 "SSH signature verification requires "
7317 "setting allowed_signers in "
7323 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
7325 printf("from: %s\n", tagger);
7326 datestr = get_datestr(&tagger_time, datebuf);
7328 printf("date: %s UTC\n", datestr);
7330 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
7332 switch (got_object_tag_get_object_type(tag)) {
7333 case GOT_OBJ_TYPE_BLOB:
7334 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
7337 case GOT_OBJ_TYPE_TREE:
7338 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
7341 case GOT_OBJ_TYPE_COMMIT:
7342 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
7345 case GOT_OBJ_TYPE_TAG:
7346 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
7356 err = got_sigs_verify_tag_ssh(&sig_msg, tag, ssh_sig,
7357 allowed_signers, revoked_signers, verbosity);
7358 if (err && err->code == GOT_ERR_BAD_TAG_SIGNATURE)
7362 printf("signature: %s", sig_msg);
7368 err = got_object_commit_get_logmsg(&tagmsg0, commit);
7371 got_object_commit_close(commit);
7373 tagmsg0 = strdup(got_object_tag_get_message(tag));
7374 got_object_tag_close(tag);
7375 if (tagmsg0 == NULL) {
7376 err = got_error_from_errno("strdup");
7383 line = strsep(&tagmsg, "\n");
7385 printf(" %s\n", line);
7390 got_ref_list_free(&refs);
7391 free(wanted_refname);
7393 if (err == NULL && bad_sigs)
7394 err = got_error(GOT_ERR_BAD_TAG_SIGNATURE);
7398 static const struct got_error *
7399 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
7400 const char *tag_name, const char *repo_path)
7402 const struct got_error *err = NULL;
7403 char *template = NULL, *initial_content = NULL;
7404 char *editor = NULL;
7405 int initial_content_len;
7408 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
7409 err = got_error_from_errno("asprintf");
7413 initial_content_len = asprintf(&initial_content,
7414 "\n# tagging commit %s as %s\n",
7415 commit_id_str, tag_name);
7416 if (initial_content_len == -1) {
7417 err = got_error_from_errno("asprintf");
7421 err = got_opentemp_named_fd(tagmsg_path, &fd, template, "");
7425 if (write(fd, initial_content, initial_content_len) == -1) {
7426 err = got_error_from_errno2("write", *tagmsg_path);
7429 if (close(fd) == -1) {
7430 err = got_error_from_errno2("close", *tagmsg_path);
7435 err = get_editor(&editor);
7438 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content,
7439 initial_content_len, 1);
7441 free(initial_content);
7445 if (fd != -1 && close(fd) == -1 && err == NULL)
7446 err = got_error_from_errno2("close", *tagmsg_path);
7455 static const struct got_error *
7456 add_tag(struct got_repository *repo, const char *tagger,
7457 const char *tag_name, const char *commit_arg, const char *tagmsg_arg,
7458 const char *signer_id, int verbosity)
7460 const struct got_error *err = NULL;
7461 struct got_object_id *commit_id = NULL, *tag_id = NULL;
7462 char *label = NULL, *commit_id_str = NULL;
7463 struct got_reference *ref = NULL;
7464 char *refname = NULL, *tagmsg = NULL;
7465 char *tagmsg_path = NULL, *tag_id_str = NULL;
7466 int preserve_tagmsg = 0;
7467 struct got_reflist_head refs;
7472 * Don't let the user create a tag name with a leading '-'.
7473 * While technically a valid reference name, this case is usually
7474 * an unintended typo.
7476 if (tag_name[0] == '-')
7477 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
7479 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
7483 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
7484 GOT_OBJ_TYPE_COMMIT, &refs, repo);
7488 err = got_object_id_str(&commit_id_str, commit_id);
7492 err = get_tag_refname(&refname, tag_name);
7495 if (strncmp("refs/tags/", tag_name, 10) == 0)
7498 err = got_ref_open(&ref, repo, refname, 0);
7500 err = got_error(GOT_ERR_TAG_EXISTS);
7502 } else if (err->code != GOT_ERR_NOT_REF)
7505 if (tagmsg_arg == NULL) {
7506 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
7507 tag_name, got_repo_get_path(repo));
7509 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
7510 tagmsg_path != NULL)
7511 preserve_tagmsg = 1;
7514 /* Editor is done; we can now apply unveil(2) */
7515 err = got_sigs_apply_unveil();
7518 err = apply_unveil(got_repo_get_path(repo), 0, NULL);
7523 err = got_object_tag_create(&tag_id, tag_name, commit_id,
7524 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, signer_id, repo,
7528 preserve_tagmsg = 1;
7532 err = got_ref_alloc(&ref, refname, tag_id);
7535 preserve_tagmsg = 1;
7539 err = got_ref_write(ref, repo);
7542 preserve_tagmsg = 1;
7546 err = got_object_id_str(&tag_id_str, tag_id);
7549 preserve_tagmsg = 1;
7552 printf("Created tag %s\n", tag_id_str);
7554 if (preserve_tagmsg) {
7555 fprintf(stderr, "%s: tag message preserved in %s\n",
7556 getprogname(), tagmsg_path);
7557 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
7558 err = got_error_from_errno2("unlink", tagmsg_path);
7563 free(commit_id_str);
7567 got_ref_list_free(&refs);
7571 static const struct got_error *
7572 cmd_tag(int argc, char *argv[])
7574 const struct got_error *error = NULL;
7575 struct got_repository *repo = NULL;
7576 struct got_worktree *worktree = NULL;
7577 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
7578 char *gitconfig_path = NULL, *tagger = NULL;
7579 char *allowed_signers = NULL, *revoked_signers = NULL;
7580 const char *signer_id = NULL;
7581 const char *tag_name = NULL, *commit_id_arg = NULL, *tagmsg = NULL;
7582 int ch, do_list = 0, verify_tags = 0, verbosity = 0;
7583 int *pack_fds = NULL;
7586 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
7587 "sendfd unveil", NULL) == -1)
7591 while ((ch = getopt(argc, argv, "c:lm:r:s:Vv")) != -1) {
7594 commit_id_arg = optarg;
7603 repo_path = realpath(optarg, NULL);
7604 if (repo_path == NULL) {
7605 error = got_error_from_errno2("realpath",
7609 got_path_strip_trailing_slashes(repo_path);
7620 else if (verbosity < 3)
7632 if (do_list || verify_tags) {
7633 if (commit_id_arg != NULL)
7635 "-c option can only be used when creating a tag");
7638 option_conflict('l', 'm');
7640 option_conflict('V', 'm');
7644 option_conflict('l', 's');
7646 option_conflict('V', 's');
7650 } else if (argc != 1)
7656 cwd = getcwd(NULL, 0);
7658 error = got_error_from_errno("getcwd");
7662 error = got_repo_pack_fds_open(&pack_fds);
7666 if (repo_path == NULL) {
7667 error = got_worktree_open(&worktree, cwd);
7668 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7674 strdup(got_worktree_get_repo_path(worktree));
7675 if (repo_path == NULL)
7676 error = got_error_from_errno("strdup");
7680 repo_path = strdup(cwd);
7681 if (repo_path == NULL) {
7682 error = got_error_from_errno("strdup");
7688 if (do_list || verify_tags) {
7689 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7692 error = get_allowed_signers(&allowed_signers, repo, worktree);
7695 error = get_revoked_signers(&revoked_signers, repo, worktree);
7699 /* Release work tree lock. */
7700 got_worktree_close(worktree);
7705 * Remove "cpath" promise unless needed for signature tmpfile
7709 got_sigs_apply_unveil();
7712 if (pledge("stdio rpath wpath flock proc exec sendfd "
7713 "unveil", NULL) == -1)
7717 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7720 error = list_tags(repo, tag_name, verify_tags, allowed_signers,
7721 revoked_signers, verbosity);
7723 error = get_gitconfig_path(&gitconfig_path);
7726 error = got_repo_open(&repo, repo_path, gitconfig_path,
7731 error = get_author(&tagger, repo, worktree);
7734 if (signer_id == NULL)
7735 signer_id = get_signer_id(repo, worktree);
7739 error = got_sigs_apply_unveil();
7743 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
7748 if (commit_id_arg == NULL) {
7749 struct got_reference *head_ref;
7750 struct got_object_id *commit_id;
7751 error = got_ref_open(&head_ref, repo,
7752 worktree ? got_worktree_get_head_ref_name(worktree)
7756 error = got_ref_resolve(&commit_id, repo, head_ref);
7757 got_ref_close(head_ref);
7760 error = got_object_id_str(&commit_id_str, commit_id);
7767 /* Release work tree lock. */
7768 got_worktree_close(worktree);
7772 error = add_tag(repo, tagger, tag_name,
7773 commit_id_str ? commit_id_str : commit_id_arg, tagmsg,
7774 signer_id, verbosity);
7778 const struct got_error *close_err = got_repo_close(repo);
7783 got_worktree_close(worktree);
7785 const struct got_error *pack_err =
7786 got_repo_pack_fds_close(pack_fds);
7792 free(gitconfig_path);
7793 free(commit_id_str);
7795 free(allowed_signers);
7796 free(revoked_signers);
7803 fprintf(stderr, "usage: %s add [-IR] path ...\n", getprogname());
7807 static const struct got_error *
7808 add_progress(void *arg, unsigned char status, const char *path)
7810 while (path[0] == '/')
7812 printf("%c %s\n", status, path);
7816 static const struct got_error *
7817 cmd_add(int argc, char *argv[])
7819 const struct got_error *error = NULL;
7820 struct got_repository *repo = NULL;
7821 struct got_worktree *worktree = NULL;
7823 struct got_pathlist_head paths;
7824 struct got_pathlist_entry *pe;
7825 int ch, can_recurse = 0, no_ignores = 0;
7826 int *pack_fds = NULL;
7831 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7836 while ((ch = getopt(argc, argv, "IR")) != -1) {
7856 cwd = getcwd(NULL, 0);
7858 error = got_error_from_errno("getcwd");
7862 error = got_repo_pack_fds_open(&pack_fds);
7866 error = got_worktree_open(&worktree, cwd);
7868 if (error->code == GOT_ERR_NOT_WORKTREE)
7869 error = wrap_not_worktree_error(error, "add", cwd);
7873 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7878 error = apply_unveil(got_repo_get_path(repo), 1,
7879 got_worktree_get_root_path(worktree));
7883 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7890 TAILQ_FOREACH(pe, &paths, entry) {
7891 if (asprintf(&ondisk_path, "%s/%s",
7892 got_worktree_get_root_path(worktree),
7894 error = got_error_from_errno("asprintf");
7897 if (lstat(ondisk_path, &sb) == -1) {
7898 if (errno == ENOENT) {
7902 error = got_error_from_errno2("lstat",
7908 if (S_ISDIR(sb.st_mode)) {
7909 error = got_error_msg(GOT_ERR_BAD_PATH,
7910 "adding directories requires -R option");
7916 error = got_worktree_schedule_add(worktree, &paths, add_progress,
7917 NULL, repo, no_ignores);
7920 const struct got_error *close_err = got_repo_close(repo);
7925 got_worktree_close(worktree);
7927 const struct got_error *pack_err =
7928 got_repo_pack_fds_close(pack_fds);
7932 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
7940 fprintf(stderr, "usage: %s remove [-fkR] [-s status-codes] path ...\n",
7945 static const struct got_error *
7946 print_remove_status(void *arg, unsigned char status,
7947 unsigned char staged_status, const char *path)
7949 while (path[0] == '/')
7951 if (status == GOT_STATUS_NONEXISTENT)
7953 if (status == staged_status && (status == GOT_STATUS_DELETE))
7954 status = GOT_STATUS_NO_CHANGE;
7955 printf("%c%c %s\n", status, staged_status, path);
7959 static const struct got_error *
7960 cmd_remove(int argc, char *argv[])
7962 const struct got_error *error = NULL;
7963 struct got_worktree *worktree = NULL;
7964 struct got_repository *repo = NULL;
7965 const char *status_codes = NULL;
7967 struct got_pathlist_head paths;
7968 struct got_pathlist_entry *pe;
7969 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0, i;
7970 int ignore_missing_paths = 0;
7971 int *pack_fds = NULL;
7976 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7981 while ((ch = getopt(argc, argv, "fkRs:")) != -1) {
7984 delete_local_mods = 1;
7985 ignore_missing_paths = 1;
7994 for (i = 0; optarg[i] != '\0'; i++) {
7995 switch (optarg[i]) {
7996 case GOT_STATUS_MODIFY:
7997 delete_local_mods = 1;
7999 case GOT_STATUS_MISSING:
8000 ignore_missing_paths = 1;
8003 errx(1, "invalid status code '%c'",
8007 status_codes = optarg;
8021 cwd = getcwd(NULL, 0);
8023 error = got_error_from_errno("getcwd");
8027 error = got_repo_pack_fds_open(&pack_fds);
8031 error = got_worktree_open(&worktree, cwd);
8033 if (error->code == GOT_ERR_NOT_WORKTREE)
8034 error = wrap_not_worktree_error(error, "remove", cwd);
8038 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8043 error = apply_unveil(got_repo_get_path(repo), 1,
8044 got_worktree_get_root_path(worktree));
8048 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8055 TAILQ_FOREACH(pe, &paths, entry) {
8056 if (asprintf(&ondisk_path, "%s/%s",
8057 got_worktree_get_root_path(worktree),
8059 error = got_error_from_errno("asprintf");
8062 if (lstat(ondisk_path, &sb) == -1) {
8063 if (errno == ENOENT) {
8067 error = got_error_from_errno2("lstat",
8073 if (S_ISDIR(sb.st_mode)) {
8074 error = got_error_msg(GOT_ERR_BAD_PATH,
8075 "removing directories requires -R option");
8081 error = got_worktree_schedule_delete(worktree, &paths,
8082 delete_local_mods, status_codes, print_remove_status, NULL,
8083 repo, keep_on_disk, ignore_missing_paths);
8086 const struct got_error *close_err = got_repo_close(repo);
8091 got_worktree_close(worktree);
8093 const struct got_error *pack_err =
8094 got_repo_pack_fds_close(pack_fds);
8098 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
8106 fprintf(stderr, "usage: %s patch [-nR] [-c commit] [-p strip-count] "
8107 "[patchfile]\n", getprogname());
8111 static const struct got_error *
8112 patch_from_stdin(int *patchfd)
8114 const struct got_error *err = NULL;
8117 sig_t sighup, sigint, sigquit;
8119 *patchfd = got_opentempfd();
8121 return got_error_from_errno("got_opentempfd");
8123 sighup = signal(SIGHUP, SIG_DFL);
8124 sigint = signal(SIGINT, SIG_DFL);
8125 sigquit = signal(SIGQUIT, SIG_DFL);
8128 r = read(0, buf, sizeof(buf));
8130 err = got_error_from_errno("read");
8135 if (write(*patchfd, buf, r) == -1) {
8136 err = got_error_from_errno("write");
8141 signal(SIGHUP, sighup);
8142 signal(SIGINT, sigint);
8143 signal(SIGQUIT, sigquit);
8145 if (err == NULL && lseek(*patchfd, 0, SEEK_SET) == -1)
8146 err = got_error_from_errno("lseek");
8156 struct got_patch_progress_arg {
8162 static const struct got_error *
8163 patch_progress(void *arg, const char *old, const char *new,
8164 unsigned char status, const struct got_error *error, int old_from,
8165 int old_lines, int new_from, int new_lines, int offset,
8166 int ws_mangled, const struct got_error *hunk_err)
8168 const char *path = new == NULL ? old : new;
8169 struct got_patch_progress_arg *a = arg;
8171 while (*path == '/')
8174 if (status != GOT_STATUS_NO_CHANGE &&
8175 status != 0 /* per-hunk progress */) {
8176 printf("%c %s\n", status, path);
8177 a->did_something = 1;
8180 if (hunk_err == NULL) {
8181 if (status == GOT_STATUS_CANNOT_UPDATE)
8183 else if (status == GOT_STATUS_CONFLICT)
8188 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
8190 if (offset != 0 || hunk_err != NULL || ws_mangled) {
8191 printf("@@ -%d,%d +%d,%d @@ ", old_from,
8192 old_lines, new_from, new_lines);
8193 if (hunk_err != NULL)
8194 printf("%s\n", hunk_err->msg);
8195 else if (offset != 0)
8196 printf("applied with offset %d\n", offset);
8198 printf("hunk contains mangled whitespace\n");
8205 print_patch_progress_stats(struct got_patch_progress_arg *ppa)
8207 if (!ppa->did_something)
8210 if (ppa->conflicts > 0)
8211 printf("Files with merge conflicts: %d\n", ppa->conflicts);
8213 if (ppa->rejects > 0) {
8214 printf("Files where patch failed to apply: %d\n",
8219 static const struct got_error *
8220 cmd_patch(int argc, char *argv[])
8222 const struct got_error *error = NULL, *close_error = NULL;
8223 struct got_worktree *worktree = NULL;
8224 struct got_repository *repo = NULL;
8225 struct got_reflist_head refs;
8226 struct got_object_id *commit_id = NULL;
8227 const char *commit_id_str = NULL;
8231 int ch, nop = 0, strip = -1, reverse = 0;
8233 int *pack_fds = NULL;
8234 struct got_patch_progress_arg ppa;
8239 if (pledge("stdio rpath wpath cpath fattr proc exec sendfd flock "
8240 "unveil", NULL) == -1)
8244 while ((ch = getopt(argc, argv, "c:np:R")) != -1) {
8247 commit_id_str = optarg;
8253 strip = strtonum(optarg, 0, INT_MAX, &errstr);
8255 errx(1, "pathname strip count is %s: %s",
8271 error = patch_from_stdin(&patchfd);
8274 } else if (argc == 1) {
8275 patchfd = open(argv[0], O_RDONLY);
8276 if (patchfd == -1) {
8277 error = got_error_from_errno2("open", argv[0]);
8280 if (fstat(patchfd, &sb) == -1) {
8281 error = got_error_from_errno2("fstat", argv[0]);
8284 if (!S_ISREG(sb.st_mode)) {
8285 error = got_error_path(argv[0], GOT_ERR_BAD_FILETYPE);
8291 if ((cwd = getcwd(NULL, 0)) == NULL) {
8292 error = got_error_from_errno("getcwd");
8296 error = got_repo_pack_fds_open(&pack_fds);
8300 error = got_worktree_open(&worktree, cwd);
8304 const char *repo_path = got_worktree_get_repo_path(worktree);
8305 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8309 error = apply_unveil(got_repo_get_path(repo), 0,
8310 got_worktree_get_root_path(worktree));
8314 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
8318 if (commit_id_str != NULL) {
8319 error = got_repo_match_object_id(&commit_id, NULL,
8320 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
8325 memset(&ppa, 0, sizeof(ppa));
8326 error = got_patch(patchfd, worktree, repo, nop, strip, reverse,
8327 commit_id, patch_progress, &ppa, check_cancelled, NULL);
8328 print_patch_progress_stats(&ppa);
8330 got_ref_list_free(&refs);
8333 close_error = got_repo_close(repo);
8335 error = close_error;
8337 if (worktree != NULL) {
8338 close_error = got_worktree_close(worktree);
8340 error = close_error;
8343 const struct got_error *pack_err =
8344 got_repo_pack_fds_close(pack_fds);
8355 fprintf(stderr, "usage: %s revert [-pR] [-F response-script] path ...\n",
8360 static const struct got_error *
8361 revert_progress(void *arg, unsigned char status, const char *path)
8363 if (status == GOT_STATUS_UNVERSIONED)
8366 while (path[0] == '/')
8368 printf("%c %s\n", status, path);
8372 struct choose_patch_arg {
8373 FILE *patch_script_file;
8377 static const struct got_error *
8378 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
8379 int nchanges, const char *action)
8381 const struct got_error *err;
8383 size_t linesize = 0;
8387 case GOT_STATUS_ADD:
8388 printf("A %s\n%s this addition? [y/n] ", path, action);
8390 case GOT_STATUS_DELETE:
8391 printf("D %s\n%s this deletion? [y/n] ", path, action);
8393 case GOT_STATUS_MODIFY:
8394 if (fseek(patch_file, 0L, SEEK_SET) == -1)
8395 return got_error_from_errno("fseek");
8396 printf(GOT_COMMIT_SEP_STR);
8397 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
8399 if (linelen == -1 && ferror(patch_file)) {
8400 err = got_error_from_errno("getline");
8405 printf(GOT_COMMIT_SEP_STR);
8406 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
8407 path, n, nchanges, action);
8410 return got_error_path(path, GOT_ERR_FILE_STATUS);
8417 static const struct got_error *
8418 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
8419 FILE *patch_file, int n, int nchanges)
8421 const struct got_error *err = NULL;
8423 size_t linesize = 0;
8426 struct choose_patch_arg *a = arg;
8428 *choice = GOT_PATCH_CHOICE_NONE;
8430 if (a->patch_script_file) {
8432 err = show_change(status, path, patch_file, n, nchanges,
8436 linelen = getline(&line, &linesize, a->patch_script_file);
8437 if (linelen == -1) {
8438 if (ferror(a->patch_script_file))
8439 return got_error_from_errno("getline");
8442 nl = strchr(line, '\n');
8445 if (strcmp(line, "y") == 0) {
8446 *choice = GOT_PATCH_CHOICE_YES;
8448 } else if (strcmp(line, "n") == 0) {
8449 *choice = GOT_PATCH_CHOICE_NO;
8451 } else if (strcmp(line, "q") == 0 &&
8452 status == GOT_STATUS_MODIFY) {
8453 *choice = GOT_PATCH_CHOICE_QUIT;
8456 printf("invalid response '%s'\n", line);
8461 while (resp != 'y' && resp != 'n' && resp != 'q') {
8462 err = show_change(status, path, patch_file, n, nchanges,
8469 if (status == GOT_STATUS_MODIFY) {
8470 if (resp != 'y' && resp != 'n' && resp != 'q') {
8471 printf("invalid response '%c'\n", resp);
8474 } else if (resp != 'y' && resp != 'n') {
8475 printf("invalid response '%c'\n", resp);
8481 *choice = GOT_PATCH_CHOICE_YES;
8482 else if (resp == 'n')
8483 *choice = GOT_PATCH_CHOICE_NO;
8484 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
8485 *choice = GOT_PATCH_CHOICE_QUIT;
8490 struct wt_commitable_path_arg {
8491 struct got_pathlist_head *commit_paths;
8496 * Shortcut work tree status callback to determine if the set of paths scanned
8497 * has at least one versioned path that is being modified and, if not NULL, is
8498 * in the arg->commit_paths list. Set arg and return GOT_ERR_FILE_MODIFIED as
8499 * soon as a path is passed with a status that satisfies this criteria.
8501 static const struct got_error *
8502 worktree_has_commitable_path(void *arg, unsigned char status,
8503 unsigned char staged_status, const char *path,
8504 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8505 struct got_object_id *commit_id, int dirfd, const char *de_name)
8507 struct wt_commitable_path_arg *a = arg;
8509 if (status == staged_status && (status == GOT_STATUS_DELETE))
8510 status = GOT_STATUS_NO_CHANGE;
8512 if (!(status == GOT_STATUS_NO_CHANGE ||
8513 status == GOT_STATUS_UNVERSIONED) ||
8514 staged_status != GOT_STATUS_NO_CHANGE) {
8515 if (a->commit_paths != NULL) {
8516 struct got_pathlist_entry *pe;
8518 TAILQ_FOREACH(pe, a->commit_paths, entry) {
8519 if (strncmp(path, pe->path,
8520 pe->path_len) == 0) {
8521 *a->has_changes = 1;
8526 *a->has_changes = 1;
8528 if (*a->has_changes)
8529 return got_error(GOT_ERR_FILE_MODIFIED);
8536 * Check that the changeset of the commit identified by id is
8537 * comprised of at least one modified path that is being committed.
8539 static const struct got_error *
8540 commit_path_changed_in_worktree(struct wt_commitable_path_arg *wcpa,
8541 struct got_object_id *id, struct got_worktree *worktree,
8542 struct got_repository *repo)
8544 const struct got_error *err;
8545 struct got_pathlist_head paths;
8546 struct got_commit_object *commit = NULL, *pcommit = NULL;
8547 struct got_tree_object *tree = NULL, *ptree = NULL;
8548 struct got_object_qid *pid;
8552 err = got_object_open_as_commit(&commit, repo, id);
8556 err = got_object_open_as_tree(&tree, repo,
8557 got_object_commit_get_tree_id(commit));
8561 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
8563 err = got_object_open_as_commit(&pcommit, repo, &pid->id);
8567 err = got_object_open_as_tree(&ptree, repo,
8568 got_object_commit_get_tree_id(pcommit));
8573 err = got_diff_tree(ptree, tree, NULL, NULL, -1, -1, "", "", repo,
8574 got_diff_tree_collect_changed_paths, &paths, 0);
8578 err = got_worktree_status(worktree, &paths, repo, 0,
8579 worktree_has_commitable_path, wcpa, check_cancelled, NULL);
8580 if (err && err->code == GOT_ERR_FILE_MODIFIED) {
8582 * At least one changed path in the referenced commit is
8583 * modified in the work tree, that's all we need to know!
8589 got_pathlist_free(&paths, GOT_PATHLIST_FREE_ALL);
8591 got_object_commit_close(commit);
8593 got_object_commit_close(pcommit);
8595 got_object_tree_close(tree);
8597 got_object_tree_close(ptree);
8602 * Remove any "logmsg" reference comprised entirely of paths that have
8603 * been reverted in this work tree. If any path in the logmsg ref changeset
8604 * remains in a changed state in the worktree, do not remove the reference.
8606 static const struct got_error *
8607 rm_logmsg_ref(struct got_worktree *worktree, struct got_repository *repo)
8609 const struct got_error *err;
8610 struct got_reflist_head refs;
8611 struct got_reflist_entry *re;
8612 struct got_commit_object *commit = NULL;
8613 struct got_object_id *commit_id = NULL;
8614 struct wt_commitable_path_arg wcpa;
8615 char *uuidstr = NULL;
8619 err = got_worktree_get_uuid(&uuidstr, worktree);
8623 err = got_ref_list(&refs, repo, "refs/got/worktree",
8624 got_ref_cmp_by_name, repo);
8628 TAILQ_FOREACH(re, &refs, entry) {
8629 const char *refname;
8630 int has_changes = 0;
8632 refname = got_ref_get_name(re->ref);
8634 if (!strncmp(refname, GOT_WORKTREE_CHERRYPICK_REF_PREFIX,
8635 GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN))
8636 refname += GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN + 1;
8637 else if (!strncmp(refname, GOT_WORKTREE_BACKOUT_REF_PREFIX,
8638 GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN))
8639 refname += GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN + 1;
8643 if (strncmp(refname, uuidstr, GOT_WORKTREE_UUID_STRLEN) == 0)
8644 refname += GOT_WORKTREE_UUID_STRLEN + 1; /* skip '-' */
8648 err = got_repo_match_object_id(&commit_id, NULL, refname,
8649 GOT_OBJ_TYPE_COMMIT, NULL, repo);
8653 err = got_object_open_as_commit(&commit, repo, commit_id);
8657 wcpa.commit_paths = NULL;
8658 wcpa.has_changes = &has_changes;
8660 err = commit_path_changed_in_worktree(&wcpa, commit_id,
8666 err = got_ref_delete(re->ref, repo);
8671 got_object_commit_close(commit);
8680 got_ref_list_free(&refs);
8682 got_object_commit_close(commit);
8686 static const struct got_error *
8687 cmd_revert(int argc, char *argv[])
8689 const struct got_error *error = NULL;
8690 struct got_worktree *worktree = NULL;
8691 struct got_repository *repo = NULL;
8692 char *cwd = NULL, *path = NULL;
8693 struct got_pathlist_head paths;
8694 struct got_pathlist_entry *pe;
8695 int ch, can_recurse = 0, pflag = 0;
8696 FILE *patch_script_file = NULL;
8697 const char *patch_script_path = NULL;
8698 struct choose_patch_arg cpa;
8699 int *pack_fds = NULL;
8704 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8705 "unveil", NULL) == -1)
8709 while ((ch = getopt(argc, argv, "F:pR")) != -1) {
8712 patch_script_path = optarg;
8731 if (patch_script_path && !pflag)
8732 errx(1, "-F option can only be used together with -p option");
8734 cwd = getcwd(NULL, 0);
8736 error = got_error_from_errno("getcwd");
8740 error = got_repo_pack_fds_open(&pack_fds);
8744 error = got_worktree_open(&worktree, cwd);
8746 if (error->code == GOT_ERR_NOT_WORKTREE)
8747 error = wrap_not_worktree_error(error, "revert", cwd);
8751 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8756 if (patch_script_path) {
8757 patch_script_file = fopen(patch_script_path, "re");
8758 if (patch_script_file == NULL) {
8759 error = got_error_from_errno2("fopen",
8766 * XXX "c" perm needed on repo dir to delete merge references.
8768 error = apply_unveil(got_repo_get_path(repo), 0,
8769 got_worktree_get_root_path(worktree));
8773 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8780 TAILQ_FOREACH(pe, &paths, entry) {
8781 if (asprintf(&ondisk_path, "%s/%s",
8782 got_worktree_get_root_path(worktree),
8784 error = got_error_from_errno("asprintf");
8787 if (lstat(ondisk_path, &sb) == -1) {
8788 if (errno == ENOENT) {
8792 error = got_error_from_errno2("lstat",
8798 if (S_ISDIR(sb.st_mode)) {
8799 error = got_error_msg(GOT_ERR_BAD_PATH,
8800 "reverting directories requires -R option");
8806 cpa.patch_script_file = patch_script_file;
8807 cpa.action = "revert";
8808 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
8809 pflag ? choose_patch : NULL, &cpa, repo);
8811 error = rm_logmsg_ref(worktree, repo);
8813 if (patch_script_file && fclose(patch_script_file) == EOF &&
8815 error = got_error_from_errno2("fclose", patch_script_path);
8817 const struct got_error *close_err = got_repo_close(repo);
8822 got_worktree_close(worktree);
8824 const struct got_error *pack_err =
8825 got_repo_pack_fds_close(pack_fds);
8829 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
8838 fprintf(stderr, "usage: %s commit [-CNnS] [-A author] [-F path] "
8839 "[-m message] [path ...]\n", getprogname());
8843 struct collect_commit_logmsg_arg {
8844 const char *cmdline_log;
8845 const char *prepared_log;
8846 const char *merged_log;
8847 int non_interactive;
8849 const char *worktree_path;
8850 const char *branch_name;
8851 const char *repo_path;
8856 static const struct got_error *
8857 read_prepared_logmsg(char **logmsg, const char *path)
8859 const struct got_error *err = NULL;
8865 memset(&sb, 0, sizeof(sb));
8867 f = fopen(path, "re");
8869 return got_error_from_errno2("fopen", path);
8871 if (fstat(fileno(f), &sb) == -1) {
8872 err = got_error_from_errno2("fstat", path);
8875 if (sb.st_size == 0) {
8876 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
8880 *logmsg = malloc(sb.st_size + 1);
8881 if (*logmsg == NULL) {
8882 err = got_error_from_errno("malloc");
8886 r = fread(*logmsg, 1, sb.st_size, f);
8887 if (r != sb.st_size) {
8889 err = got_error_from_errno2("fread", path);
8891 err = got_error(GOT_ERR_IO);
8894 (*logmsg)[sb.st_size] = '\0';
8896 if (fclose(f) == EOF && err == NULL)
8897 err = got_error_from_errno2("fclose", path);
8905 static const struct got_error *
8906 collect_commit_logmsg(struct got_pathlist_head *commitable_paths,
8907 const char *diff_path, char **logmsg, void *arg)
8909 char *initial_content = NULL;
8910 struct got_pathlist_entry *pe;
8911 const struct got_error *err = NULL;
8912 char *template = NULL;
8913 char *prepared_msg = NULL, *merged_msg = NULL;
8914 struct collect_commit_logmsg_arg *a = arg;
8915 int initial_content_len;
8919 /* if a message was specified on the command line, just use it */
8920 if (a->cmdline_log != NULL && *a->cmdline_log != '\0') {
8921 len = strlen(a->cmdline_log) + 1;
8922 *logmsg = malloc(len + 1);
8923 if (*logmsg == NULL)
8924 return got_error_from_errno("malloc");
8925 strlcpy(*logmsg, a->cmdline_log, len);
8927 } else if (a->prepared_log != NULL && a->non_interactive)
8928 return read_prepared_logmsg(logmsg, a->prepared_log);
8930 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
8931 return got_error_from_errno("asprintf");
8933 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template, "");
8937 if (a->prepared_log) {
8938 err = read_prepared_logmsg(&prepared_msg, a->prepared_log);
8941 } else if (a->merged_log) {
8942 err = read_prepared_logmsg(&merged_msg, a->merged_log);
8947 initial_content_len = asprintf(&initial_content,
8948 "%s%s\n# changes to be committed on branch %s:\n",
8949 prepared_msg ? prepared_msg : "",
8950 merged_msg ? merged_msg : "", a->branch_name);
8951 if (initial_content_len == -1) {
8952 err = got_error_from_errno("asprintf");
8956 if (write(fd, initial_content, initial_content_len) == -1) {
8957 err = got_error_from_errno2("write", a->logmsg_path);
8961 TAILQ_FOREACH(pe, commitable_paths, entry) {
8962 struct got_commitable *ct = pe->data;
8963 dprintf(fd, "# %c %s\n",
8964 got_commitable_get_status(ct),
8965 got_commitable_get_path(ct));
8969 dprintf(fd, "# detailed changes can be viewed in %s\n",
8973 if (close(fd) == -1) {
8974 err = got_error_from_errno2("close", a->logmsg_path);
8979 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content,
8980 initial_content_len, a->prepared_log ? 0 : 1);
8982 free(initial_content);
8987 if (fd != -1 && close(fd) == -1 && err == NULL)
8988 err = got_error_from_errno2("close", a->logmsg_path);
8990 /* Editor is done; we can now apply unveil(2) */
8992 err = apply_unveil(a->repo_path, 0, a->worktree_path);
9000 static const struct got_error *
9001 cat_logmsg(FILE *f, struct got_commit_object *commit, const char *idstr,
9002 const char *type, int has_content)
9004 const struct got_error *err = NULL;
9005 char *logmsg = NULL;
9007 err = got_object_commit_get_logmsg(&logmsg, commit);
9011 if (fprintf(f, "%s# log message of %s commit %s:%s",
9012 has_content ? "\n" : "", type, idstr, logmsg) < 0)
9013 err = got_ferror(f, GOT_ERR_IO);
9020 * Lookup "logmsg" references of backed-out and cherrypicked commits
9021 * belonging to the current work tree. If found, and the worktree has
9022 * at least one modified file that was changed in the referenced commit,
9023 * add its log message to a new temporary file at *logmsg_path.
9024 * Add all refs found to matched_refs to be scheduled for removal on
9025 * successful commit.
9027 static const struct got_error *
9028 lookup_logmsg_ref(char **logmsg_path, struct got_pathlist_head *paths,
9029 struct got_reflist_head *matched_refs, struct got_worktree *worktree,
9030 struct got_repository *repo)
9032 const struct got_error *err;
9033 struct got_commit_object *commit = NULL;
9034 struct got_object_id *id = NULL;
9035 struct got_reflist_head refs;
9036 struct got_reflist_entry *re, *re_match;
9038 char *uuidstr = NULL;
9039 int added_logmsg = 0;
9043 *logmsg_path = NULL;
9045 err = got_worktree_get_uuid(&uuidstr, worktree);
9049 err = got_ref_list(&refs, repo, "refs/got/worktree",
9050 got_ref_cmp_by_name, repo);
9054 TAILQ_FOREACH(re, &refs, entry) {
9055 const char *refname, *type;
9056 struct wt_commitable_path_arg wcpa;
9059 refname = got_ref_get_name(re->ref);
9061 if (strncmp(refname, GOT_WORKTREE_CHERRYPICK_REF_PREFIX,
9062 GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN) == 0) {
9063 refname += GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN + 1;
9064 type = "cherrypicked";
9065 } else if (strncmp(refname, GOT_WORKTREE_BACKOUT_REF_PREFIX,
9066 GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN) == 0) {
9067 refname += GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN + 1;
9068 type = "backed-out";
9072 if (strncmp(refname, uuidstr, GOT_WORKTREE_UUID_STRLEN) == 0)
9073 refname += GOT_WORKTREE_UUID_STRLEN + 1; /* skip '-' */
9077 err = got_repo_match_object_id(&id, NULL, refname,
9078 GOT_OBJ_TYPE_COMMIT, NULL, repo);
9082 err = got_object_open_as_commit(&commit, repo, id);
9086 wcpa.commit_paths = paths;
9087 wcpa.has_changes = &add_logmsg;
9089 err = commit_path_changed_in_worktree(&wcpa, id,
9096 err = got_opentemp_named(logmsg_path, &f,
9097 "got-commit-logmsg", "");
9101 err = cat_logmsg(f, commit, refname, type,
9108 err = got_reflist_entry_dup(&re_match, re);
9111 TAILQ_INSERT_HEAD(matched_refs, re_match, entry);
9114 got_object_commit_close(commit);
9123 got_ref_list_free(&refs);
9125 got_object_commit_close(commit);
9126 if (f && fclose(f) == EOF && err == NULL)
9127 err = got_error_from_errno("fclose");
9128 if (!added_logmsg) {
9129 if (*logmsg_path && unlink(*logmsg_path) != 0 && err == NULL)
9130 err = got_error_from_errno2("unlink", *logmsg_path);
9131 *logmsg_path = NULL;
9136 static const struct got_error *
9137 cmd_commit(int argc, char *argv[])
9139 const struct got_error *error = NULL;
9140 struct got_worktree *worktree = NULL;
9141 struct got_repository *repo = NULL;
9142 char *cwd = NULL, *id_str = NULL;
9143 struct got_object_id *id = NULL;
9144 const char *logmsg = NULL;
9145 char *prepared_logmsg = NULL, *merged_logmsg = NULL;
9146 struct collect_commit_logmsg_arg cl_arg;
9147 const char *author = NULL;
9148 char *gitconfig_path = NULL, *editor = NULL, *committer = NULL;
9149 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
9150 int allow_bad_symlinks = 0, non_interactive = 0, merge_in_progress = 0;
9151 int show_diff = 1, commit_conflicts = 0;
9152 struct got_pathlist_head paths;
9153 struct got_reflist_head refs;
9154 struct got_reflist_entry *re;
9155 int *pack_fds = NULL;
9159 cl_arg.logmsg_path = NULL;
9162 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9163 "unveil", NULL) == -1)
9167 while ((ch = getopt(argc, argv, "A:CF:m:NnS")) != -1) {
9171 error = valid_author(author);
9176 commit_conflicts = 1;
9180 option_conflict('F', 'm');
9181 prepared_logmsg = realpath(optarg, NULL);
9182 if (prepared_logmsg == NULL)
9183 return got_error_from_errno2("realpath",
9187 if (prepared_logmsg)
9188 option_conflict('m', 'F');
9192 non_interactive = 1;
9198 allow_bad_symlinks = 1;
9209 cwd = getcwd(NULL, 0);
9211 error = got_error_from_errno("getcwd");
9215 error = got_repo_pack_fds_open(&pack_fds);
9219 error = got_worktree_open(&worktree, cwd);
9221 if (error->code == GOT_ERR_NOT_WORKTREE)
9222 error = wrap_not_worktree_error(error, "commit", cwd);
9226 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
9229 if (rebase_in_progress) {
9230 error = got_error(GOT_ERR_REBASING);
9234 error = got_worktree_histedit_in_progress(&histedit_in_progress,
9239 error = get_gitconfig_path(&gitconfig_path);
9242 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9243 gitconfig_path, pack_fds);
9247 error = got_worktree_merge_in_progress(&merge_in_progress, worktree, repo);
9250 if (merge_in_progress) {
9251 error = got_error(GOT_ERR_MERGE_BUSY);
9255 error = get_author(&committer, repo, worktree);
9263 * unveil(2) traverses exec(2); if an editor is used we have
9264 * to apply unveil after the log message has been written.
9266 if (logmsg == NULL || strlen(logmsg) == 0)
9267 error = get_editor(&editor);
9269 error = apply_unveil(got_repo_get_path(repo), 0,
9270 got_worktree_get_root_path(worktree));
9274 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
9278 if (prepared_logmsg == NULL) {
9279 error = lookup_logmsg_ref(&merged_logmsg,
9280 argc > 0 ? &paths : NULL, &refs, worktree, repo);
9285 cl_arg.editor = editor;
9286 cl_arg.cmdline_log = logmsg;
9287 cl_arg.prepared_log = prepared_logmsg;
9288 cl_arg.merged_log = merged_logmsg;
9289 cl_arg.non_interactive = non_interactive;
9290 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
9291 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
9292 if (!histedit_in_progress) {
9293 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
9294 error = got_error(GOT_ERR_COMMIT_BRANCH);
9297 cl_arg.branch_name += 11;
9299 cl_arg.repo_path = got_repo_get_path(repo);
9300 error = got_worktree_commit(&id, worktree, &paths, author, committer,
9301 allow_bad_symlinks, show_diff, commit_conflicts,
9302 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
9304 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
9305 cl_arg.logmsg_path != NULL)
9306 preserve_logmsg = 1;
9310 error = got_object_id_str(&id_str, id);
9313 printf("Created commit %s\n", id_str);
9315 TAILQ_FOREACH(re, &refs, entry) {
9316 error = got_ref_delete(re->ref, repo);
9322 if (preserve_logmsg) {
9323 fprintf(stderr, "%s: log message preserved in %s\n",
9324 getprogname(), cl_arg.logmsg_path);
9325 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
9327 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
9328 free(cl_arg.logmsg_path);
9329 if (merged_logmsg && unlink(merged_logmsg) == -1 && error == NULL)
9330 error = got_error_from_errno2("unlink", merged_logmsg);
9331 free(merged_logmsg);
9333 const struct got_error *close_err = got_repo_close(repo);
9338 got_worktree_close(worktree);
9340 const struct got_error *pack_err =
9341 got_repo_pack_fds_close(pack_fds);
9345 got_ref_list_free(&refs);
9346 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
9349 free(gitconfig_path);
9352 free(prepared_logmsg);
9359 fprintf(stderr, "usage: %s send [-afqTv] [-b branch] [-d branch] "
9360 "[-r repository-path] [-t tag] [remote-repository]\n",
9366 print_load_info(int print_colored, int print_found, int print_trees,
9367 int ncolored, int nfound, int ntrees)
9369 if (print_colored) {
9370 printf("%d commit%s colored", ncolored,
9371 ncolored == 1 ? "" : "s");
9374 printf("%s%d object%s found",
9375 ncolored > 0 ? "; " : "",
9376 nfound, nfound == 1 ? "" : "s");
9379 printf("; %d tree%s scanned", ntrees,
9380 ntrees == 1 ? "" : "s");
9384 struct got_send_progress_arg {
9385 char last_scaled_packsize[FMT_SCALED_STRSIZE];
9392 int last_nobj_total;
9396 int printed_something;
9398 struct got_pathlist_head *delete_branches;
9401 static const struct got_error *
9402 send_progress(void *arg, int ncolored, int nfound, int ntrees,
9403 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
9404 int nobj_written, off_t bytes_sent, const char *refname,
9405 const char *errmsg, int success)
9407 struct got_send_progress_arg *a = arg;
9408 char scaled_packsize[FMT_SCALED_STRSIZE];
9409 char scaled_sent[FMT_SCALED_STRSIZE];
9410 int p_deltify = 0, p_written = 0, p_sent = 0;
9411 int print_colored = 0, print_found = 0, print_trees = 0;
9412 int print_searching = 0, print_total = 0;
9413 int print_deltify = 0, print_written = 0, print_sent = 0;
9415 if (a->verbosity < 0)
9419 const char *status = success ? "accepted" : "rejected";
9422 struct got_pathlist_entry *pe;
9423 TAILQ_FOREACH(pe, a->delete_branches, entry) {
9424 const char *branchname = pe->path;
9425 if (got_path_cmp(branchname, refname,
9426 strlen(branchname), strlen(refname)) == 0) {
9428 a->sent_something = 1;
9434 if (a->printed_something)
9436 printf("Server has %s %s", status, refname);
9438 printf(": %s", errmsg);
9439 a->printed_something = 1;
9443 if (a->last_ncolored != ncolored) {
9445 a->last_ncolored = ncolored;
9448 if (a->last_nfound != nfound) {
9451 a->last_nfound = nfound;
9454 if (a->last_ntrees != ntrees) {
9458 a->last_ntrees = ntrees;
9461 if ((print_colored || print_found || print_trees) &&
9464 print_load_info(print_colored, print_found, print_trees,
9465 ncolored, nfound, ntrees);
9466 a->printed_something = 1;
9469 } else if (!a->loading_done) {
9471 print_load_info(1, 1, 1, ncolored, nfound, ntrees);
9473 a->loading_done = 1;
9476 if (fmt_scaled(packfile_size, scaled_packsize) == -1)
9477 return got_error_from_errno("fmt_scaled");
9478 if (fmt_scaled(bytes_sent, scaled_sent) == -1)
9479 return got_error_from_errno("fmt_scaled");
9481 if (a->last_ncommits != ncommits) {
9482 print_searching = 1;
9483 a->last_ncommits = ncommits;
9486 if (a->last_nobj_total != nobj_total) {
9487 print_searching = 1;
9489 a->last_nobj_total = nobj_total;
9492 if (packfile_size > 0 && (a->last_scaled_packsize[0] == '\0' ||
9493 strcmp(scaled_packsize, a->last_scaled_packsize)) != 0) {
9494 if (strlcpy(a->last_scaled_packsize, scaled_packsize,
9495 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
9496 return got_error(GOT_ERR_NO_SPACE);
9499 if (nobj_deltify > 0 || nobj_written > 0) {
9500 if (nobj_deltify > 0) {
9501 p_deltify = (nobj_deltify * 100) / nobj_total;
9502 if (p_deltify != a->last_p_deltify) {
9503 a->last_p_deltify = p_deltify;
9504 print_searching = 1;
9509 if (nobj_written > 0) {
9510 p_written = (nobj_written * 100) / nobj_total;
9511 if (p_written != a->last_p_written) {
9512 a->last_p_written = p_written;
9513 print_searching = 1;
9521 if (bytes_sent > 0) {
9522 p_sent = (bytes_sent * 100) / packfile_size;
9523 if (p_sent != a->last_p_sent) {
9524 a->last_p_sent = p_sent;
9525 print_searching = 1;
9531 a->sent_something = 1;
9534 if (print_searching || print_total || print_deltify || print_written ||
9537 if (print_searching)
9538 printf("packing %d reference%s", ncommits,
9539 ncommits == 1 ? "" : "s");
9541 printf("; %d object%s", nobj_total,
9542 nobj_total == 1 ? "" : "s");
9544 printf("; deltify: %d%%", p_deltify);
9546 printf("; uploading pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
9547 scaled_packsize, p_sent);
9548 else if (print_written)
9549 printf("; writing pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
9550 scaled_packsize, p_written);
9551 if (print_searching || print_total || print_deltify ||
9552 print_written || print_sent) {
9553 a->printed_something = 1;
9559 static const struct got_error *
9560 cmd_send(int argc, char *argv[])
9562 const struct got_error *error = NULL;
9563 char *cwd = NULL, *repo_path = NULL;
9564 const char *remote_name;
9565 char *proto = NULL, *host = NULL, *port = NULL;
9566 char *repo_name = NULL, *server_path = NULL;
9567 const struct got_remote_repo *remotes, *remote = NULL;
9568 int nremotes, nbranches = 0, ndelete_branches = 0;
9569 struct got_repository *repo = NULL;
9570 struct got_worktree *worktree = NULL;
9571 const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
9572 struct got_pathlist_head branches;
9573 struct got_pathlist_head tags;
9574 struct got_reflist_head all_branches;
9575 struct got_reflist_head all_tags;
9576 struct got_pathlist_head delete_args;
9577 struct got_pathlist_head delete_branches;
9578 struct got_reflist_entry *re;
9579 struct got_pathlist_entry *pe;
9580 int i, ch, sendfd = -1, sendstatus;
9582 struct got_send_progress_arg spa;
9583 int verbosity = 0, overwrite_refs = 0;
9584 int send_all_branches = 0, send_all_tags = 0;
9585 struct got_reference *ref = NULL;
9586 int *pack_fds = NULL;
9588 TAILQ_INIT(&branches);
9590 TAILQ_INIT(&all_branches);
9591 TAILQ_INIT(&all_tags);
9592 TAILQ_INIT(&delete_args);
9593 TAILQ_INIT(&delete_branches);
9595 while ((ch = getopt(argc, argv, "ab:d:fqr:Tt:v")) != -1) {
9598 send_all_branches = 1;
9601 error = got_pathlist_append(&branches, optarg, NULL);
9607 error = got_pathlist_append(&delete_args, optarg, NULL);
9618 repo_path = realpath(optarg, NULL);
9619 if (repo_path == NULL)
9620 return got_error_from_errno2("realpath",
9622 got_path_strip_trailing_slashes(repo_path);
9628 error = got_pathlist_append(&tags, optarg, NULL);
9635 else if (verbosity < 3)
9646 if (send_all_branches && !TAILQ_EMPTY(&branches))
9647 option_conflict('a', 'b');
9648 if (send_all_tags && !TAILQ_EMPTY(&tags))
9649 option_conflict('T', 't');
9653 remote_name = GOT_SEND_DEFAULT_REMOTE_NAME;
9655 remote_name = argv[0];
9659 cwd = getcwd(NULL, 0);
9661 error = got_error_from_errno("getcwd");
9665 error = got_repo_pack_fds_open(&pack_fds);
9669 if (repo_path == NULL) {
9670 error = got_worktree_open(&worktree, cwd);
9671 if (error && error->code != GOT_ERR_NOT_WORKTREE)
9677 strdup(got_worktree_get_repo_path(worktree));
9678 if (repo_path == NULL)
9679 error = got_error_from_errno("strdup");
9683 repo_path = strdup(cwd);
9684 if (repo_path == NULL) {
9685 error = got_error_from_errno("strdup");
9691 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
9696 worktree_conf = got_worktree_get_gotconfig(worktree);
9697 if (worktree_conf) {
9698 got_gotconfig_get_remotes(&nremotes, &remotes,
9700 for (i = 0; i < nremotes; i++) {
9701 if (strcmp(remotes[i].name, remote_name) == 0) {
9702 remote = &remotes[i];
9708 if (remote == NULL) {
9709 repo_conf = got_repo_get_gotconfig(repo);
9711 got_gotconfig_get_remotes(&nremotes, &remotes,
9713 for (i = 0; i < nremotes; i++) {
9714 if (strcmp(remotes[i].name, remote_name) == 0) {
9715 remote = &remotes[i];
9721 if (remote == NULL) {
9722 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
9723 for (i = 0; i < nremotes; i++) {
9724 if (strcmp(remotes[i].name, remote_name) == 0) {
9725 remote = &remotes[i];
9730 if (remote == NULL) {
9731 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
9735 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
9736 &repo_name, remote->send_url);
9740 if (strcmp(proto, "git") == 0) {
9742 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
9743 "sendfd dns inet unveil", NULL) == -1)
9746 } else if (strcmp(proto, "git+ssh") == 0 ||
9747 strcmp(proto, "ssh") == 0) {
9749 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
9750 "sendfd unveil", NULL) == -1)
9753 } else if (strcmp(proto, "http") == 0 ||
9754 strcmp(proto, "git+http") == 0) {
9755 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
9758 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
9762 error = got_dial_apply_unveil(proto);
9766 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
9770 if (send_all_branches) {
9771 error = got_ref_list(&all_branches, repo, "refs/heads",
9772 got_ref_cmp_by_name, NULL);
9775 TAILQ_FOREACH(re, &all_branches, entry) {
9776 const char *branchname = got_ref_get_name(re->ref);
9777 error = got_pathlist_append(&branches,
9783 } else if (nbranches == 0) {
9784 for (i = 0; i < remote->nsend_branches; i++) {
9785 error = got_pathlist_append(&branches,
9786 remote->send_branches[i], NULL);
9792 if (send_all_tags) {
9793 error = got_ref_list(&all_tags, repo, "refs/tags",
9794 got_ref_cmp_by_name, NULL);
9797 TAILQ_FOREACH(re, &all_tags, entry) {
9798 const char *tagname = got_ref_get_name(re->ref);
9799 error = got_pathlist_append(&tags,
9807 * To prevent accidents only branches in refs/heads/ can be deleted
9808 * with 'got send -d'.
9809 * Deleting anything else requires local repository access or Git.
9811 TAILQ_FOREACH(pe, &delete_args, entry) {
9812 const char *branchname = pe->path;
9814 struct got_pathlist_entry *new;
9815 if (strncmp(branchname, "refs/heads/", 11) == 0) {
9816 s = strdup(branchname);
9818 error = got_error_from_errno("strdup");
9822 if (asprintf(&s, "refs/heads/%s", branchname) == -1) {
9823 error = got_error_from_errno("asprintf");
9827 error = got_pathlist_insert(&new, &delete_branches, s, NULL);
9828 if (error || new == NULL /* duplicate */)
9835 if (nbranches == 0 && ndelete_branches == 0) {
9836 struct got_reference *head_ref;
9838 error = got_ref_open(&head_ref, repo,
9839 got_worktree_get_head_ref_name(worktree), 0);
9841 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
9844 if (got_ref_is_symbolic(head_ref)) {
9845 error = got_ref_resolve_symbolic(&ref, repo, head_ref);
9846 got_ref_close(head_ref);
9851 error = got_pathlist_append(&branches, got_ref_get_name(ref),
9858 if (verbosity >= 0) {
9859 printf("Connecting to \"%s\" %s://%s%s%s%s%s\n",
9860 remote->name, proto, host,
9861 port ? ":" : "", port ? port : "",
9862 *server_path == '/' ? "" : "/", server_path);
9865 error = got_send_connect(&sendpid, &sendfd, proto, host, port,
9866 server_path, verbosity);
9870 memset(&spa, 0, sizeof(spa));
9871 spa.last_scaled_packsize[0] = '\0';
9872 spa.last_p_deltify = -1;
9873 spa.last_p_written = -1;
9874 spa.verbosity = verbosity;
9875 spa.delete_branches = &delete_branches;
9876 error = got_send_pack(remote_name, &branches, &tags, &delete_branches,
9877 verbosity, overwrite_refs, sendfd, repo, send_progress, &spa,
9878 check_cancelled, NULL);
9879 if (spa.printed_something)
9883 if (!spa.sent_something && verbosity >= 0)
9884 printf("Already up-to-date\n");
9887 if (kill(sendpid, SIGTERM) == -1)
9888 error = got_error_from_errno("kill");
9889 if (waitpid(sendpid, &sendstatus, 0) == -1 && error == NULL)
9890 error = got_error_from_errno("waitpid");
9892 if (sendfd != -1 && close(sendfd) == -1 && error == NULL)
9893 error = got_error_from_errno("close");
9895 const struct got_error *close_err = got_repo_close(repo);
9900 got_worktree_close(worktree);
9902 const struct got_error *pack_err =
9903 got_repo_pack_fds_close(pack_fds);
9909 got_pathlist_free(&branches, GOT_PATHLIST_FREE_NONE);
9910 got_pathlist_free(&tags, GOT_PATHLIST_FREE_NONE);
9911 got_ref_list_free(&all_branches);
9912 got_ref_list_free(&all_tags);
9913 got_pathlist_free(&delete_args, GOT_PATHLIST_FREE_NONE);
9914 got_pathlist_free(&delete_branches, GOT_PATHLIST_FREE_PATH);
9926 * Print and if delete is set delete all ref_prefix references.
9927 * If wanted_ref is not NULL, only print or delete this reference.
9929 static const struct got_error *
9930 process_logmsg_refs(const char *ref_prefix, size_t prefix_len,
9931 const char *wanted_ref, int delete, struct got_worktree *worktree,
9932 struct got_repository *repo)
9934 const struct got_error *err;
9935 struct got_pathlist_head paths;
9936 struct got_reflist_head refs;
9937 struct got_reflist_entry *re;
9938 struct got_reflist_object_id_map *refs_idmap = NULL;
9939 struct got_commit_object *commit = NULL;
9940 struct got_object_id *id = NULL;
9941 const char *header_prefix;
9942 char *uuidstr = NULL;
9948 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, repo);
9952 err = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
9956 if (worktree != NULL) {
9957 err = got_worktree_get_uuid(&uuidstr, worktree);
9963 if (strncmp(wanted_ref, "refs/heads/", 11) == 0)
9967 if (strcmp(ref_prefix, GOT_WORKTREE_BACKOUT_REF_PREFIX) == 0)
9968 header_prefix = "backout";
9970 header_prefix = "cherrypick";
9972 TAILQ_FOREACH(re, &refs, entry) {
9973 const char *refname, *wt;
9975 refname = got_ref_get_name(re->ref);
9977 err = check_cancelled(NULL);
9981 if (strncmp(refname, ref_prefix, prefix_len) == 0)
9982 refname += prefix_len + 1; /* skip '-' delimiter */
9988 if (worktree == NULL || strncmp(refname, uuidstr,
9989 GOT_WORKTREE_UUID_STRLEN) == 0)
9990 refname += GOT_WORKTREE_UUID_STRLEN + 1; /* skip '-' */
9994 err = got_repo_match_object_id(&id, NULL, refname,
9995 GOT_OBJ_TYPE_COMMIT, NULL, repo);
9999 err = got_object_open_as_commit(&commit, repo, id);
10004 found = strncmp(wanted_ref, refname,
10005 strlen(wanted_ref)) == 0;
10006 if (wanted_ref && !found) {
10007 struct got_reflist_head *ci_refs;
10009 ci_refs = got_reflist_object_id_map_lookup(refs_idmap,
10013 char *refs_str = NULL;
10014 char const *r = NULL;
10016 err = build_refs_str(&refs_str, ci_refs, id,
10023 if (strncmp(r, wanted_ref,
10024 strlen(wanted_ref)) == 0) {
10028 r = strchr(r, ' ');
10036 if (wanted_ref == NULL || found) {
10038 err = got_ref_delete(re->ref, repo);
10041 printf("Deleted: ");
10042 err = print_commit_oneline(commit, id, repo,
10046 * Print paths modified by commit to help
10047 * associate commits with worktree changes.
10049 err = get_changed_paths(&paths, commit,
10054 err = print_commit(commit, id, repo, NULL,
10055 &paths, NULL, 0, 0, refs_idmap, NULL,
10057 got_pathlist_free(&paths,
10058 GOT_PATHLIST_FREE_ALL);
10060 if (worktree == NULL)
10061 printf("work tree: %.*s\n\n",
10062 GOT_WORKTREE_UUID_STRLEN, wt);
10068 got_object_commit_close(commit);
10074 if (wanted_ref != NULL && !found)
10075 err = got_error_fmt(GOT_ERR_NOT_REF, "%s", wanted_ref);
10080 got_ref_list_free(&refs);
10081 got_pathlist_free(&paths, GOT_PATHLIST_FREE_ALL);
10083 got_reflist_object_id_map_free(refs_idmap);
10085 got_object_commit_close(commit);
10090 * Create new temp "logmsg" ref of the backed-out or cherrypicked commit
10091 * identified by id for log messages to prepopulate the editor on commit.
10093 static const struct got_error *
10094 logmsg_ref(struct got_object_id *id, const char *prefix,
10095 struct got_worktree *worktree, struct got_repository *repo)
10097 const struct got_error *err = NULL;
10098 char *idstr, *ref = NULL, *refname = NULL;
10099 int histedit_in_progress;
10100 int rebase_in_progress, merge_in_progress;
10103 * Silenty refuse to create merge reference if any histedit, merge,
10104 * or rebase operation is in progress.
10106 err = got_worktree_histedit_in_progress(&histedit_in_progress,
10110 if (histedit_in_progress)
10113 err = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
10116 if (rebase_in_progress)
10119 err = got_worktree_merge_in_progress(&merge_in_progress, worktree,
10123 if (merge_in_progress)
10126 err = got_object_id_str(&idstr, id);
10130 err = got_worktree_get_logmsg_ref_name(&refname, worktree, prefix);
10134 if (asprintf(&ref, "%s-%s", refname, idstr) == -1) {
10135 err = got_error_from_errno("asprintf");
10139 err = create_ref(ref, got_worktree_get_base_commit_id(worktree),
10149 usage_cherrypick(void)
10151 fprintf(stderr, "usage: %s cherrypick [-lX] [commit-id]\n",
10156 static const struct got_error *
10157 cmd_cherrypick(int argc, char *argv[])
10159 const struct got_error *error = NULL;
10160 struct got_worktree *worktree = NULL;
10161 struct got_repository *repo = NULL;
10162 char *cwd = NULL, *commit_id_str = NULL;
10163 struct got_object_id *commit_id = NULL;
10164 struct got_commit_object *commit = NULL;
10165 struct got_object_qid *pid;
10166 int ch, list_refs = 0, remove_refs = 0;
10167 struct got_update_progress_arg upa;
10168 int *pack_fds = NULL;
10171 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10172 "unveil", NULL) == -1)
10176 while ((ch = getopt(argc, argv, "lX")) != -1) {
10185 usage_cherrypick();
10193 if (list_refs || remove_refs) {
10194 if (argc != 0 && argc != 1)
10195 usage_cherrypick();
10196 } else if (argc != 1)
10197 usage_cherrypick();
10198 if (list_refs && remove_refs)
10199 option_conflict('l', 'X');
10201 cwd = getcwd(NULL, 0);
10203 error = got_error_from_errno("getcwd");
10207 error = got_repo_pack_fds_open(&pack_fds);
10211 error = got_worktree_open(&worktree, cwd);
10213 if (list_refs || remove_refs) {
10214 if (error->code != GOT_ERR_NOT_WORKTREE)
10217 if (error->code == GOT_ERR_NOT_WORKTREE)
10218 error = wrap_not_worktree_error(error,
10219 "cherrypick", cwd);
10224 error = got_repo_open(&repo,
10225 worktree ? got_worktree_get_repo_path(worktree) : cwd,
10230 error = apply_unveil(got_repo_get_path(repo), 0,
10231 worktree ? got_worktree_get_root_path(worktree) : NULL);
10235 if (list_refs || remove_refs) {
10236 error = process_logmsg_refs(GOT_WORKTREE_CHERRYPICK_REF_PREFIX,
10237 GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN,
10238 argc == 1 ? argv[0] : NULL, remove_refs, worktree, repo);
10242 error = got_repo_match_object_id(&commit_id, NULL, argv[0],
10243 GOT_OBJ_TYPE_COMMIT, NULL, repo);
10246 error = got_object_id_str(&commit_id_str, commit_id);
10250 error = got_object_open_as_commit(&commit, repo, commit_id);
10253 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
10254 memset(&upa, 0, sizeof(upa));
10255 error = got_worktree_merge_files(worktree, pid ? &pid->id : NULL,
10256 commit_id, repo, update_progress, &upa, check_cancelled,
10261 if (upa.did_something) {
10262 error = logmsg_ref(commit_id,
10263 GOT_WORKTREE_CHERRYPICK_REF_PREFIX, worktree, repo);
10266 printf("Merged commit %s\n", commit_id_str);
10268 print_merge_progress_stats(&upa);
10272 got_object_commit_close(commit);
10273 free(commit_id_str);
10275 got_worktree_close(worktree);
10277 const struct got_error *close_err = got_repo_close(repo);
10282 const struct got_error *pack_err =
10283 got_repo_pack_fds_close(pack_fds);
10292 usage_backout(void)
10294 fprintf(stderr, "usage: %s backout [-lX] [commit-id]\n", getprogname());
10298 static const struct got_error *
10299 cmd_backout(int argc, char *argv[])
10301 const struct got_error *error = NULL;
10302 struct got_worktree *worktree = NULL;
10303 struct got_repository *repo = NULL;
10304 char *cwd = NULL, *commit_id_str = NULL;
10305 struct got_object_id *commit_id = NULL;
10306 struct got_commit_object *commit = NULL;
10307 struct got_object_qid *pid;
10308 int ch, list_refs = 0, remove_refs = 0;
10309 struct got_update_progress_arg upa;
10310 int *pack_fds = NULL;
10313 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10314 "unveil", NULL) == -1)
10318 while ((ch = getopt(argc, argv, "lX")) != -1) {
10335 if (list_refs || remove_refs) {
10336 if (argc != 0 && argc != 1)
10338 } else if (argc != 1)
10340 if (list_refs && remove_refs)
10341 option_conflict('l', 'X');
10343 cwd = getcwd(NULL, 0);
10345 error = got_error_from_errno("getcwd");
10349 error = got_repo_pack_fds_open(&pack_fds);
10353 error = got_worktree_open(&worktree, cwd);
10355 if (list_refs || remove_refs) {
10356 if (error->code != GOT_ERR_NOT_WORKTREE)
10359 if (error->code == GOT_ERR_NOT_WORKTREE)
10360 error = wrap_not_worktree_error(error,
10366 error = got_repo_open(&repo,
10367 worktree ? got_worktree_get_repo_path(worktree) : cwd,
10372 error = apply_unveil(got_repo_get_path(repo), 0,
10373 worktree ? got_worktree_get_root_path(worktree) : NULL);
10377 if (list_refs || remove_refs) {
10378 error = process_logmsg_refs(GOT_WORKTREE_BACKOUT_REF_PREFIX,
10379 GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN,
10380 argc == 1 ? argv[0] : NULL, remove_refs, worktree, repo);
10384 error = got_repo_match_object_id(&commit_id, NULL, argv[0],
10385 GOT_OBJ_TYPE_COMMIT, NULL, repo);
10388 error = got_object_id_str(&commit_id_str, commit_id);
10392 error = got_object_open_as_commit(&commit, repo, commit_id);
10395 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
10397 error = got_error(GOT_ERR_ROOT_COMMIT);
10401 memset(&upa, 0, sizeof(upa));
10402 error = got_worktree_merge_files(worktree, commit_id, &pid->id,
10403 repo, update_progress, &upa, check_cancelled, NULL);
10407 if (upa.did_something) {
10408 error = logmsg_ref(commit_id, GOT_WORKTREE_BACKOUT_REF_PREFIX,
10412 printf("Backed out commit %s\n", commit_id_str);
10414 print_merge_progress_stats(&upa);
10418 got_object_commit_close(commit);
10419 free(commit_id_str);
10421 got_worktree_close(worktree);
10423 const struct got_error *close_err = got_repo_close(repo);
10428 const struct got_error *pack_err =
10429 got_repo_pack_fds_close(pack_fds);
10439 fprintf(stderr, "usage: %s rebase [-aCclX] [branch]\n", getprogname());
10444 trim_logmsg(char *logmsg, int limit)
10449 len = strlen(logmsg);
10452 logmsg[len] = '\0';
10453 nl = strchr(logmsg, '\n');
10458 static const struct got_error *
10459 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
10461 const struct got_error *err;
10462 char *logmsg0 = NULL;
10465 err = got_object_commit_get_logmsg(&logmsg0, commit);
10470 while (isspace((unsigned char)s[0]))
10473 *logmsg = strdup(s);
10474 if (*logmsg == NULL) {
10475 err = got_error_from_errno("strdup");
10479 trim_logmsg(*logmsg, limit);
10485 static const struct got_error *
10486 show_rebase_merge_conflict(struct got_object_id *id,
10487 struct got_repository *repo)
10489 const struct got_error *err;
10490 struct got_commit_object *commit = NULL;
10491 char *id_str = NULL, *logmsg = NULL;
10493 err = got_object_open_as_commit(&commit, repo, id);
10497 err = got_object_id_str(&id_str, id);
10503 err = get_short_logmsg(&logmsg, 42, commit);
10507 printf("%s -> merge conflict: %s\n", id_str, logmsg);
10510 got_object_commit_close(commit);
10515 static const struct got_error *
10516 show_rebase_progress(struct got_commit_object *commit,
10517 struct got_object_id *old_id, struct got_object_id *new_id)
10519 const struct got_error *err;
10520 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
10522 err = got_object_id_str(&old_id_str, old_id);
10527 err = got_object_id_str(&new_id_str, new_id);
10532 old_id_str[12] = '\0';
10534 new_id_str[12] = '\0';
10536 err = get_short_logmsg(&logmsg, 42, commit);
10540 printf("%s -> %s: %s\n", old_id_str,
10541 new_id_str ? new_id_str : "no-op change", logmsg);
10549 static const struct got_error *
10550 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
10551 struct got_reference *branch, struct got_reference *tmp_branch,
10552 struct got_repository *repo, int create_backup)
10554 printf("Switching work tree to %s\n", got_ref_get_name(branch));
10555 return got_worktree_rebase_complete(worktree, fileindex,
10556 tmp_branch, branch, repo, create_backup);
10559 static const struct got_error *
10560 rebase_commit(struct got_pathlist_head *merged_paths,
10561 struct got_worktree *worktree, struct got_fileindex *fileindex,
10562 struct got_reference *tmp_branch, const char *committer,
10563 struct got_object_id *commit_id, int allow_conflict,
10564 struct got_repository *repo)
10566 const struct got_error *error;
10567 struct got_commit_object *commit;
10568 struct got_object_id *new_commit_id;
10570 error = got_object_open_as_commit(&commit, repo, commit_id);
10574 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
10575 worktree, fileindex, tmp_branch, committer, commit, commit_id,
10576 allow_conflict, repo);
10578 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
10580 error = show_rebase_progress(commit, commit_id, NULL);
10582 error = show_rebase_progress(commit, commit_id, new_commit_id);
10583 free(new_commit_id);
10586 got_object_commit_close(commit);
10590 struct check_path_prefix_arg {
10591 const char *path_prefix;
10596 static const struct got_error *
10597 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
10598 struct got_blob_object *blob2, FILE *f1, FILE *f2,
10599 struct got_object_id *id1, struct got_object_id *id2,
10600 const char *path1, const char *path2,
10601 mode_t mode1, mode_t mode2, struct got_repository *repo)
10603 struct check_path_prefix_arg *a = arg;
10605 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
10606 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
10607 return got_error(a->errcode);
10612 static const struct got_error *
10613 check_path_prefix(struct got_object_id *parent_id,
10614 struct got_object_id *commit_id, const char *path_prefix,
10615 int errcode, struct got_repository *repo)
10617 const struct got_error *err;
10618 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
10619 struct got_commit_object *commit = NULL, *parent_commit = NULL;
10620 struct check_path_prefix_arg cpp_arg;
10622 if (got_path_is_root_dir(path_prefix))
10625 err = got_object_open_as_commit(&commit, repo, commit_id);
10629 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
10633 err = got_object_open_as_tree(&tree1, repo,
10634 got_object_commit_get_tree_id(parent_commit));
10638 err = got_object_open_as_tree(&tree2, repo,
10639 got_object_commit_get_tree_id(commit));
10643 cpp_arg.path_prefix = path_prefix;
10644 while (cpp_arg.path_prefix[0] == '/')
10645 cpp_arg.path_prefix++;
10646 cpp_arg.len = strlen(cpp_arg.path_prefix);
10647 cpp_arg.errcode = errcode;
10648 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
10649 check_path_prefix_in_diff, &cpp_arg, 0);
10652 got_object_tree_close(tree1);
10654 got_object_tree_close(tree2);
10656 got_object_commit_close(commit);
10658 got_object_commit_close(parent_commit);
10662 static const struct got_error *
10663 collect_commits(struct got_object_id_queue *commits,
10664 struct got_object_id *initial_commit_id,
10665 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
10666 const char *path_prefix, int path_prefix_errcode,
10667 struct got_repository *repo)
10669 const struct got_error *err = NULL;
10670 struct got_commit_graph *graph = NULL;
10671 struct got_object_id parent_id, commit_id;
10672 struct got_object_qid *qid;
10674 err = got_commit_graph_open(&graph, "/", 1);
10678 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
10679 check_cancelled, NULL);
10683 memcpy(&commit_id, initial_commit_id, sizeof(commit_id));
10684 while (got_object_id_cmp(&commit_id, iter_stop_id) != 0) {
10685 err = got_commit_graph_iter_next(&parent_id, graph, repo,
10686 check_cancelled, NULL);
10688 if (err->code == GOT_ERR_ITER_COMPLETED) {
10689 err = got_error_msg(GOT_ERR_ANCESTRY,
10690 "ran out of commits to rebase before "
10691 "youngest common ancestor commit has "
10692 "been reached?!?");
10696 err = check_path_prefix(&parent_id, &commit_id,
10697 path_prefix, path_prefix_errcode, repo);
10701 err = got_object_qid_alloc(&qid, &commit_id);
10704 STAILQ_INSERT_HEAD(commits, qid, entry);
10706 memcpy(&commit_id, &parent_id, sizeof(commit_id));
10710 got_commit_graph_close(graph);
10714 static const struct got_error *
10715 get_commit_brief_str(char **brief_str, struct got_commit_object *commit)
10717 const struct got_error *err = NULL;
10718 time_t committer_time;
10720 char datebuf[11]; /* YYYY-MM-DD + NUL */
10721 char *author0 = NULL, *author, *smallerthan;
10722 char *logmsg0 = NULL, *logmsg, *newline;
10724 committer_time = got_object_commit_get_committer_time(commit);
10725 if (gmtime_r(&committer_time, &tm) == NULL)
10726 return got_error_from_errno("gmtime_r");
10727 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d", &tm) == 0)
10728 return got_error(GOT_ERR_NO_SPACE);
10730 author0 = strdup(got_object_commit_get_author(commit));
10731 if (author0 == NULL)
10732 return got_error_from_errno("strdup");
10734 smallerthan = strchr(author, '<');
10735 if (smallerthan && smallerthan[1] != '\0')
10736 author = smallerthan + 1;
10737 author[strcspn(author, "@>")] = '\0';
10739 err = got_object_commit_get_logmsg(&logmsg0, commit);
10743 while (*logmsg == '\n')
10745 newline = strchr(logmsg, '\n');
10749 if (asprintf(brief_str, "%s %s %s",
10750 datebuf, author, logmsg) == -1)
10751 err = got_error_from_errno("asprintf");
10758 static const struct got_error *
10759 delete_backup_ref(struct got_reference *ref, struct got_object_id *id,
10760 struct got_repository *repo)
10762 const struct got_error *err;
10765 err = got_object_id_str(&id_str, id);
10769 err = got_ref_delete(ref, repo);
10773 printf("Deleted %s: %s\n", got_ref_get_name(ref), id_str);
10779 static const struct got_error *
10780 print_backup_ref(const char *branch_name, const char *new_id_str,
10781 struct got_object_id *old_commit_id, struct got_commit_object *old_commit,
10782 struct got_reflist_object_id_map *refs_idmap,
10783 struct got_repository *repo)
10785 const struct got_error *err = NULL;
10786 struct got_reflist_head *refs;
10787 char *refs_str = NULL;
10788 struct got_object_id *new_commit_id = NULL;
10789 struct got_commit_object *new_commit = NULL;
10790 char *new_commit_brief_str = NULL;
10791 struct got_object_id *yca_id = NULL;
10792 struct got_commit_object *yca_commit = NULL;
10793 char *yca_id_str = NULL, *yca_brief_str = NULL;
10794 char *custom_refs_str;
10796 if (asprintf(&custom_refs_str, "formerly %s", branch_name) == -1)
10797 return got_error_from_errno("asprintf");
10799 err = print_commit(old_commit, old_commit_id, repo, NULL, NULL, NULL,
10800 0, 0, refs_idmap, custom_refs_str, NULL);
10804 err = got_object_resolve_id_str(&new_commit_id, repo, new_id_str);
10808 refs = got_reflist_object_id_map_lookup(refs_idmap, new_commit_id);
10810 err = build_refs_str(&refs_str, refs, new_commit_id, repo, 0);
10815 err = got_object_open_as_commit(&new_commit, repo, new_commit_id);
10819 err = get_commit_brief_str(&new_commit_brief_str, new_commit);
10823 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
10824 old_commit_id, new_commit_id, 1, repo, check_cancelled, NULL);
10828 printf("has become commit %s%s%s%s\n %s\n", new_id_str,
10829 refs_str ? " (" : "", refs_str ? refs_str : "",
10830 refs_str ? ")" : "", new_commit_brief_str);
10831 if (yca_id && got_object_id_cmp(yca_id, new_commit_id) != 0 &&
10832 got_object_id_cmp(yca_id, old_commit_id) != 0) {
10836 err = got_object_open_as_commit(&yca_commit, repo, yca_id);
10840 err = get_commit_brief_str(&yca_brief_str, yca_commit);
10844 err = got_object_id_str(&yca_id_str, yca_id);
10848 refs = got_reflist_object_id_map_lookup(refs_idmap, yca_id);
10850 err = build_refs_str(&refs_str, refs, yca_id, repo, 0);
10854 printf("history forked at %s%s%s%s\n %s\n",
10856 refs_str ? " (" : "", refs_str ? refs_str : "",
10857 refs_str ? ")" : "", yca_brief_str);
10860 free(custom_refs_str);
10861 free(new_commit_id);
10865 free(yca_brief_str);
10867 got_object_commit_close(new_commit);
10869 got_object_commit_close(yca_commit);
10874 static const struct got_error *
10875 worktree_has_logmsg_ref(const char *caller, struct got_worktree *worktree,
10876 struct got_repository *repo)
10878 const struct got_error *err;
10879 struct got_reflist_head refs;
10880 struct got_reflist_entry *re;
10881 char *uuidstr = NULL;
10882 static char msg[160];
10886 err = got_worktree_get_uuid(&uuidstr, worktree);
10890 err = got_ref_list(&refs, repo, "refs/got/worktree",
10891 got_ref_cmp_by_name, repo);
10895 TAILQ_FOREACH(re, &refs, entry) {
10896 const char *cmd, *refname, *type;
10898 refname = got_ref_get_name(re->ref);
10900 if (strncmp(refname, GOT_WORKTREE_CHERRYPICK_REF_PREFIX,
10901 GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN) == 0) {
10902 refname += GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN + 1;
10903 cmd = "cherrypick";
10904 type = "cherrypicked";
10905 } else if (strncmp(refname, GOT_WORKTREE_BACKOUT_REF_PREFIX,
10906 GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN) == 0) {
10907 refname += GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN + 1;
10909 type = "backed-out";
10913 if (strncmp(refname, uuidstr, GOT_WORKTREE_UUID_STRLEN) != 0)
10916 snprintf(msg, sizeof(msg),
10917 "work tree has references created by %s commits which "
10918 "must be removed with 'got %s -X' before running the %s "
10919 "command", type, cmd, caller);
10920 err = got_error_msg(GOT_ERR_WORKTREE_META, msg);
10926 got_ref_list_free(&refs);
10930 static const struct got_error *
10931 process_backup_refs(const char *backup_ref_prefix,
10932 const char *wanted_branch_name,
10933 int delete, struct got_repository *repo)
10935 const struct got_error *err;
10936 struct got_reflist_head refs, backup_refs;
10937 struct got_reflist_entry *re;
10938 const size_t backup_ref_prefix_len = strlen(backup_ref_prefix);
10939 struct got_object_id *old_commit_id = NULL;
10940 char *branch_name = NULL;
10941 struct got_commit_object *old_commit = NULL;
10942 struct got_reflist_object_id_map *refs_idmap = NULL;
10943 int wanted_branch_found = 0;
10946 TAILQ_INIT(&backup_refs);
10948 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
10952 err = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
10956 if (wanted_branch_name) {
10957 if (strncmp(wanted_branch_name, "refs/heads/", 11) == 0)
10958 wanted_branch_name += 11;
10961 err = got_ref_list(&backup_refs, repo, backup_ref_prefix,
10962 got_ref_cmp_by_commit_timestamp_descending, repo);
10966 TAILQ_FOREACH(re, &backup_refs, entry) {
10967 const char *refname = got_ref_get_name(re->ref);
10970 err = check_cancelled(NULL);
10974 err = got_ref_resolve(&old_commit_id, repo, re->ref);
10978 err = got_object_open_as_commit(&old_commit, repo,
10983 if (strncmp(backup_ref_prefix, refname,
10984 backup_ref_prefix_len) == 0)
10985 refname += backup_ref_prefix_len;
10987 while (refname[0] == '/')
10990 branch_name = strdup(refname);
10991 if (branch_name == NULL) {
10992 err = got_error_from_errno("strdup");
10995 slash = strrchr(branch_name, '/');
10998 refname += strlen(branch_name) + 1;
11001 if (wanted_branch_name == NULL ||
11002 strcmp(wanted_branch_name, branch_name) == 0) {
11003 wanted_branch_found = 1;
11005 err = delete_backup_ref(re->ref,
11006 old_commit_id, repo);
11008 err = print_backup_ref(branch_name, refname,
11009 old_commit_id, old_commit, refs_idmap,
11016 free(old_commit_id);
11017 old_commit_id = NULL;
11019 branch_name = NULL;
11020 got_object_commit_close(old_commit);
11024 if (wanted_branch_name && !wanted_branch_found) {
11025 err = got_error_fmt(GOT_ERR_NOT_REF,
11026 "%s/%s/", backup_ref_prefix, wanted_branch_name);
11030 got_reflist_object_id_map_free(refs_idmap);
11031 got_ref_list_free(&refs);
11032 got_ref_list_free(&backup_refs);
11033 free(old_commit_id);
11036 got_object_commit_close(old_commit);
11040 static const struct got_error *
11041 abort_progress(void *arg, unsigned char status, const char *path)
11044 * Unversioned files should not clutter progress output when
11045 * an operation is aborted.
11047 if (status == GOT_STATUS_UNVERSIONED)
11050 return update_progress(arg, status, path);
11053 static const struct got_error *
11054 cmd_rebase(int argc, char *argv[])
11056 const struct got_error *error = NULL;
11057 struct got_worktree *worktree = NULL;
11058 struct got_repository *repo = NULL;
11059 struct got_fileindex *fileindex = NULL;
11060 char *cwd = NULL, *committer = NULL, *gitconfig_path = NULL;
11061 struct got_reference *branch = NULL;
11062 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
11063 struct got_object_id *commit_id = NULL, *parent_id = NULL;
11064 struct got_object_id *resume_commit_id = NULL;
11065 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
11066 struct got_object_id *head_commit_id = NULL;
11067 struct got_reference *head_ref = NULL;
11068 struct got_commit_object *commit = NULL;
11069 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
11070 int histedit_in_progress = 0, merge_in_progress = 0;
11071 int create_backup = 1, list_backups = 0, delete_backups = 0;
11072 int allow_conflict = 0;
11073 struct got_object_id_queue commits;
11074 struct got_pathlist_head merged_paths;
11075 const struct got_object_id_queue *parent_ids;
11076 struct got_object_qid *qid, *pid;
11077 struct got_update_progress_arg upa;
11078 int *pack_fds = NULL;
11080 STAILQ_INIT(&commits);
11081 TAILQ_INIT(&merged_paths);
11082 memset(&upa, 0, sizeof(upa));
11085 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
11086 "unveil", NULL) == -1)
11090 while ((ch = getopt(argc, argv, "aCclX")) != -1) {
11096 allow_conflict = 1;
11099 continue_rebase = 1;
11105 delete_backups = 1;
11116 if (list_backups) {
11118 option_conflict('l', 'a');
11119 if (allow_conflict)
11120 option_conflict('l', 'C');
11121 if (continue_rebase)
11122 option_conflict('l', 'c');
11123 if (delete_backups)
11124 option_conflict('l', 'X');
11125 if (argc != 0 && argc != 1)
11127 } else if (delete_backups) {
11129 option_conflict('X', 'a');
11130 if (allow_conflict)
11131 option_conflict('X', 'C');
11132 if (continue_rebase)
11133 option_conflict('X', 'c');
11135 option_conflict('l', 'X');
11136 if (argc != 0 && argc != 1)
11138 } else if (allow_conflict) {
11140 option_conflict('C', 'a');
11141 if (!continue_rebase)
11142 errx(1, "-C option requires -c");
11144 if (abort_rebase && continue_rebase)
11146 else if (abort_rebase || continue_rebase) {
11149 } else if (argc != 1)
11153 cwd = getcwd(NULL, 0);
11155 error = got_error_from_errno("getcwd");
11159 error = got_repo_pack_fds_open(&pack_fds);
11163 error = got_worktree_open(&worktree, cwd);
11165 if (list_backups || delete_backups) {
11166 if (error->code != GOT_ERR_NOT_WORKTREE)
11169 if (error->code == GOT_ERR_NOT_WORKTREE)
11170 error = wrap_not_worktree_error(error,
11176 error = get_gitconfig_path(&gitconfig_path);
11179 error = got_repo_open(&repo,
11180 worktree ? got_worktree_get_repo_path(worktree) : cwd,
11181 gitconfig_path, pack_fds);
11185 if (worktree != NULL && !list_backups && !delete_backups) {
11186 error = worktree_has_logmsg_ref("rebase", worktree, repo);
11191 error = get_author(&committer, repo, worktree);
11192 if (error && error->code != GOT_ERR_COMMIT_NO_AUTHOR)
11195 error = apply_unveil(got_repo_get_path(repo), 0,
11196 worktree ? got_worktree_get_root_path(worktree) : NULL);
11200 if (list_backups || delete_backups) {
11201 error = process_backup_refs(
11202 GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
11203 argc == 1 ? argv[0] : NULL, delete_backups, repo);
11204 goto done; /* nothing else to do */
11207 error = got_worktree_histedit_in_progress(&histedit_in_progress,
11211 if (histedit_in_progress) {
11212 error = got_error(GOT_ERR_HISTEDIT_BUSY);
11216 error = got_worktree_merge_in_progress(&merge_in_progress,
11220 if (merge_in_progress) {
11221 error = got_error(GOT_ERR_MERGE_BUSY);
11225 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
11229 if (abort_rebase) {
11230 if (!rebase_in_progress) {
11231 error = got_error(GOT_ERR_NOT_REBASING);
11234 error = got_worktree_rebase_continue(&resume_commit_id,
11235 &new_base_branch, &tmp_branch, &branch, &fileindex,
11239 printf("Switching work tree to %s\n",
11240 got_ref_get_symref_target(new_base_branch));
11241 error = got_worktree_rebase_abort(worktree, fileindex, repo,
11242 new_base_branch, abort_progress, &upa);
11245 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
11246 print_merge_progress_stats(&upa);
11247 goto done; /* nothing else to do */
11250 if (continue_rebase) {
11251 if (!rebase_in_progress) {
11252 error = got_error(GOT_ERR_NOT_REBASING);
11255 error = got_worktree_rebase_continue(&resume_commit_id,
11256 &new_base_branch, &tmp_branch, &branch, &fileindex,
11261 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
11262 committer, resume_commit_id, allow_conflict, repo);
11266 yca_id = got_object_id_dup(resume_commit_id);
11267 if (yca_id == NULL) {
11268 error = got_error_from_errno("got_object_id_dup");
11272 error = got_ref_open(&branch, repo, argv[0], 0);
11275 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
11276 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
11277 "will not rebase a branch which lives outside "
11278 "the \"refs/heads/\" reference namespace");
11283 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
11287 if (!continue_rebase) {
11288 struct got_object_id *base_commit_id;
11290 error = got_ref_open(&head_ref, repo,
11291 got_worktree_get_head_ref_name(worktree), 0);
11294 error = got_ref_resolve(&head_commit_id, repo, head_ref);
11297 base_commit_id = got_worktree_get_base_commit_id(worktree);
11298 if (got_object_id_cmp(base_commit_id, head_commit_id) != 0) {
11299 error = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
11303 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
11304 base_commit_id, branch_head_commit_id, 1, repo,
11305 check_cancelled, NULL);
11307 if (error->code == GOT_ERR_ANCESTRY) {
11308 error = got_error_msg(GOT_ERR_ANCESTRY,
11309 "specified branch shares no common "
11310 "ancestry with work tree's branch");
11315 if (got_object_id_cmp(base_commit_id, yca_id) == 0) {
11316 struct got_pathlist_head paths;
11317 printf("%s is already based on %s\n",
11318 got_ref_get_name(branch),
11319 got_worktree_get_head_ref_name(worktree));
11320 error = switch_head_ref(branch, branch_head_commit_id,
11324 error = got_worktree_set_base_commit_id(worktree, repo,
11325 branch_head_commit_id);
11328 TAILQ_INIT(&paths);
11329 error = got_pathlist_append(&paths, "", NULL);
11332 error = got_worktree_checkout_files(worktree,
11333 &paths, repo, update_progress, &upa,
11334 check_cancelled, NULL);
11335 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
11338 if (upa.did_something) {
11340 error = got_object_id_str(&id_str,
11341 branch_head_commit_id);
11344 printf("Updated to %s: %s\n",
11345 got_worktree_get_head_ref_name(worktree),
11349 printf("Already up-to-date\n");
11350 print_update_progress_stats(&upa);
11355 commit_id = branch_head_commit_id;
11356 error = got_object_open_as_commit(&commit, repo, commit_id);
11360 parent_ids = got_object_commit_get_parent_ids(commit);
11361 pid = STAILQ_FIRST(parent_ids);
11363 error = collect_commits(&commits, commit_id, &pid->id,
11364 yca_id, got_worktree_get_path_prefix(worktree),
11365 GOT_ERR_REBASE_PATH, repo);
11370 got_object_commit_close(commit);
11373 if (!continue_rebase) {
11374 error = got_worktree_rebase_prepare(&new_base_branch,
11375 &tmp_branch, &fileindex, worktree, branch, repo);
11380 if (STAILQ_EMPTY(&commits)) {
11381 if (continue_rebase) {
11382 error = rebase_complete(worktree, fileindex,
11383 branch, tmp_branch, repo, create_backup);
11386 /* Fast-forward the reference of the branch. */
11387 struct got_object_id *new_head_commit_id;
11389 error = got_ref_resolve(&new_head_commit_id, repo,
11393 error = got_object_id_str(&id_str, new_head_commit_id);
11396 printf("Forwarding %s to commit %s\n",
11397 got_ref_get_name(branch), id_str);
11399 error = got_ref_change_ref(branch,
11400 new_head_commit_id);
11403 /* No backup needed since objects did not change. */
11409 STAILQ_FOREACH(qid, &commits, entry) {
11411 commit_id = &qid->id;
11412 parent_id = pid ? &pid->id : yca_id;
11415 memset(&upa, 0, sizeof(upa));
11416 error = got_worktree_rebase_merge_files(&merged_paths,
11417 worktree, fileindex, parent_id, commit_id, repo,
11418 update_progress, &upa, check_cancelled, NULL);
11422 print_merge_progress_stats(&upa);
11423 if (upa.conflicts > 0 || upa.missing > 0 ||
11424 upa.not_deleted > 0 || upa.unversioned > 0) {
11425 if (upa.conflicts > 0) {
11426 error = show_rebase_merge_conflict(&qid->id,
11431 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
11435 error = rebase_commit(&merged_paths, worktree, fileindex,
11436 tmp_branch, committer, commit_id, 0, repo);
11437 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
11442 if (upa.conflicts > 0 || upa.missing > 0 ||
11443 upa.not_deleted > 0 || upa.unversioned > 0) {
11444 error = got_worktree_rebase_postpone(worktree, fileindex);
11447 if (upa.conflicts > 0 && upa.missing == 0 &&
11448 upa.not_deleted == 0 && upa.unversioned == 0) {
11449 error = got_error_msg(GOT_ERR_CONFLICTS,
11450 "conflicts must be resolved before rebasing "
11452 } else if (upa.conflicts > 0) {
11453 error = got_error_msg(GOT_ERR_CONFLICTS,
11454 "conflicts must be resolved before rebasing "
11455 "can continue; changes destined for some "
11456 "files were not yet merged and should be "
11457 "merged manually if required before the "
11458 "rebase operation is continued");
11460 error = got_error_msg(GOT_ERR_CONFLICTS,
11461 "changes destined for some files were not "
11462 "yet merged and should be merged manually "
11463 "if required before the rebase operation "
11467 error = rebase_complete(worktree, fileindex, branch,
11468 tmp_branch, repo, create_backup);
11472 free(gitconfig_path);
11473 got_object_id_queue_free(&commits);
11474 free(branch_head_commit_id);
11475 free(resume_commit_id);
11476 free(head_commit_id);
11479 got_object_commit_close(commit);
11481 got_ref_close(branch);
11482 if (new_base_branch)
11483 got_ref_close(new_base_branch);
11485 got_ref_close(tmp_branch);
11487 got_ref_close(head_ref);
11489 got_worktree_close(worktree);
11491 const struct got_error *close_err = got_repo_close(repo);
11496 const struct got_error *pack_err =
11497 got_repo_pack_fds_close(pack_fds);
11505 usage_histedit(void)
11507 fprintf(stderr, "usage: %s histedit [-aCcdeflmX] [-F histedit-script] "
11508 "[branch]\n", getprogname());
11512 #define GOT_HISTEDIT_PICK 'p'
11513 #define GOT_HISTEDIT_EDIT 'e'
11514 #define GOT_HISTEDIT_FOLD 'f'
11515 #define GOT_HISTEDIT_DROP 'd'
11516 #define GOT_HISTEDIT_MESG 'm'
11518 static const struct got_histedit_cmd {
11519 unsigned char code;
11522 } got_histedit_cmds[] = {
11523 { GOT_HISTEDIT_PICK, "pick", "use commit" },
11524 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
11525 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
11527 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
11528 { GOT_HISTEDIT_MESG, "mesg",
11529 "single-line log message for commit above (open editor if empty)" },
11532 struct got_histedit_list_entry {
11533 TAILQ_ENTRY(got_histedit_list_entry) entry;
11534 struct got_object_id *commit_id;
11535 const struct got_histedit_cmd *cmd;
11538 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
11540 static const struct got_error *
11541 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
11542 FILE *f, struct got_repository *repo)
11544 const struct got_error *err = NULL;
11545 char *logmsg = NULL, *id_str = NULL;
11546 struct got_commit_object *commit = NULL;
11549 err = got_object_open_as_commit(&commit, repo, commit_id);
11553 err = get_short_logmsg(&logmsg, 34, commit);
11557 err = got_object_id_str(&id_str, commit_id);
11561 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
11563 err = got_ferror(f, GOT_ERR_IO);
11566 got_object_commit_close(commit);
11572 static const struct got_error *
11573 histedit_write_commit_list(struct got_object_id_queue *commits,
11574 FILE *f, int edit_logmsg_only, int fold_only, int drop_only,
11575 int edit_only, struct got_repository *repo)
11577 const struct got_error *err = NULL;
11578 struct got_object_qid *qid;
11579 const char *histedit_cmd = NULL;
11581 if (STAILQ_EMPTY(commits))
11582 return got_error(GOT_ERR_EMPTY_HISTEDIT);
11584 STAILQ_FOREACH(qid, commits, entry) {
11585 histedit_cmd = got_histedit_cmds[0].name;
11587 histedit_cmd = "drop";
11588 else if (edit_only)
11589 histedit_cmd = "edit";
11590 else if (fold_only && STAILQ_NEXT(qid, entry) != NULL)
11591 histedit_cmd = "fold";
11592 err = histedit_write_commit(&qid->id, histedit_cmd, f, repo);
11595 if (edit_logmsg_only) {
11596 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
11598 err = got_ferror(f, GOT_ERR_IO);
11607 static const struct got_error *
11608 write_cmd_list(FILE *f, const char *branch_name,
11609 struct got_object_id_queue *commits)
11611 const struct got_error *err = NULL;
11615 struct got_object_qid *qid;
11617 qid = STAILQ_FIRST(commits);
11618 err = got_object_id_str(&id_str, &qid->id);
11623 "# Editing the history of branch '%s' starting at\n"
11625 "# Commits will be processed in order from top to "
11626 "bottom of this file.\n", branch_name, id_str);
11628 err = got_ferror(f, GOT_ERR_IO);
11632 n = fprintf(f, "# Available histedit commands:\n");
11634 err = got_ferror(f, GOT_ERR_IO);
11638 for (i = 0; i < nitems(got_histedit_cmds); i++) {
11639 const struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
11640 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
11643 err = got_ferror(f, GOT_ERR_IO);
11652 static const struct got_error *
11653 histedit_syntax_error(int lineno)
11655 static char msg[42];
11658 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
11660 if (ret < 0 || (size_t)ret >= sizeof(msg))
11661 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
11663 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
11666 static const struct got_error *
11667 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
11668 char *logmsg, struct got_repository *repo)
11670 const struct got_error *err;
11671 struct got_commit_object *folded_commit = NULL;
11672 char *id_str, *folded_logmsg = NULL;
11674 err = got_object_id_str(&id_str, hle->commit_id);
11678 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
11682 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
11685 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
11686 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
11687 folded_logmsg) == -1) {
11688 err = got_error_from_errno("asprintf");
11692 got_object_commit_close(folded_commit);
11694 free(folded_logmsg);
11698 static struct got_histedit_list_entry *
11699 get_folded_commits(struct got_histedit_list_entry *hle)
11701 struct got_histedit_list_entry *prev, *folded = NULL;
11703 prev = TAILQ_PREV(hle, got_histedit_list, entry);
11704 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
11705 prev->cmd->code == GOT_HISTEDIT_DROP)) {
11706 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
11708 prev = TAILQ_PREV(prev, got_histedit_list, entry);
11714 static const struct got_error *
11715 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
11716 struct got_repository *repo)
11718 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
11719 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
11720 const struct got_error *err = NULL;
11721 struct got_commit_object *commit = NULL;
11724 struct got_histedit_list_entry *folded = NULL;
11726 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
11730 folded = get_folded_commits(hle);
11732 while (folded != hle) {
11733 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
11734 folded = TAILQ_NEXT(folded, entry);
11737 err = append_folded_commit_msg(&new_msg, folded,
11743 folded = TAILQ_NEXT(folded, entry);
11747 err = got_object_id_str(&id_str, hle->commit_id);
11750 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
11753 logmsg_len = asprintf(&new_msg,
11754 "%s\n# original log message of commit %s: %s",
11755 logmsg ? logmsg : "", id_str, orig_logmsg);
11756 if (logmsg_len == -1) {
11757 err = got_error_from_errno("asprintf");
11763 err = got_object_id_str(&id_str, hle->commit_id);
11767 err = got_opentemp_named_fd(&logmsg_path, &fd,
11768 GOT_TMPDIR_STR "/got-logmsg", "");
11772 if (write(fd, logmsg, logmsg_len) == -1) {
11773 err = got_error_from_errno2("write", logmsg_path);
11776 if (close(fd) == -1) {
11777 err = got_error_from_errno2("close", logmsg_path);
11782 err = get_editor(&editor);
11786 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg,
11789 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
11792 hle->logmsg = strdup(new_msg);
11793 if (hle->logmsg == NULL)
11794 err = got_error_from_errno("strdup");
11797 if (fd != -1 && close(fd) == -1 && err == NULL)
11798 err = got_error_from_errno2("close", logmsg_path);
11799 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
11800 err = got_error_from_errno2("unlink", logmsg_path);
11806 got_object_commit_close(commit);
11810 static const struct got_error *
11811 histedit_parse_list(struct got_histedit_list *histedit_cmds,
11812 FILE *f, struct got_repository *repo)
11814 const struct got_error *err = NULL;
11815 char *line = NULL, *p, *end;
11816 size_t i, linesize = 0;
11818 int lineno = 0, lastcmd = -1;
11819 const struct got_histedit_cmd *cmd;
11820 struct got_object_id *commit_id = NULL;
11821 struct got_histedit_list_entry *hle = NULL;
11824 linelen = getline(&line, &linesize, f);
11825 if (linelen == -1) {
11826 const struct got_error *getline_err;
11829 getline_err = got_error_from_errno("getline");
11830 err = got_ferror(f, getline_err->code);
11835 while (isspace((unsigned char)p[0]))
11837 if (p[0] == '#' || p[0] == '\0')
11840 for (i = 0; i < nitems(got_histedit_cmds); i++) {
11841 cmd = &got_histedit_cmds[i];
11842 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
11843 isspace((unsigned char)p[strlen(cmd->name)])) {
11844 p += strlen(cmd->name);
11847 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
11852 if (i == nitems(got_histedit_cmds)) {
11853 err = histedit_syntax_error(lineno);
11856 while (isspace((unsigned char)p[0]))
11858 if (cmd->code == GOT_HISTEDIT_MESG) {
11859 if (lastcmd != GOT_HISTEDIT_PICK &&
11860 lastcmd != GOT_HISTEDIT_EDIT) {
11861 err = got_error(GOT_ERR_HISTEDIT_CMD);
11864 if (p[0] == '\0') {
11865 err = histedit_edit_logmsg(hle, repo);
11869 hle->logmsg = strdup(p);
11870 if (hle->logmsg == NULL) {
11871 err = got_error_from_errno("strdup");
11875 lastcmd = cmd->code;
11879 while (end[0] && !isspace((unsigned char)end[0]))
11883 err = got_object_resolve_id_str(&commit_id, repo, p);
11885 /* override error code */
11886 err = histedit_syntax_error(lineno);
11890 hle = malloc(sizeof(*hle));
11892 err = got_error_from_errno("malloc");
11896 hle->commit_id = commit_id;
11897 hle->logmsg = NULL;
11899 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
11900 lastcmd = cmd->code;
11908 static const struct got_error *
11909 histedit_check_script(struct got_histedit_list *histedit_cmds,
11910 struct got_object_id_queue *commits, struct got_repository *repo)
11912 const struct got_error *err = NULL;
11913 struct got_object_qid *qid;
11914 struct got_histedit_list_entry *hle;
11915 static char msg[92];
11918 if (TAILQ_EMPTY(histedit_cmds))
11919 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
11920 "histedit script contains no commands");
11921 if (STAILQ_EMPTY(commits))
11922 return got_error(GOT_ERR_EMPTY_HISTEDIT);
11924 TAILQ_FOREACH(hle, histedit_cmds, entry) {
11925 struct got_histedit_list_entry *hle2;
11926 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
11929 if (got_object_id_cmp(hle->commit_id,
11930 hle2->commit_id) != 0)
11932 err = got_object_id_str(&id_str, hle->commit_id);
11935 snprintf(msg, sizeof(msg), "commit %s is listed "
11936 "more than once in histedit script", id_str);
11938 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
11942 STAILQ_FOREACH(qid, commits, entry) {
11943 TAILQ_FOREACH(hle, histedit_cmds, entry) {
11944 if (got_object_id_cmp(&qid->id, hle->commit_id) == 0)
11948 err = got_object_id_str(&id_str, &qid->id);
11951 snprintf(msg, sizeof(msg),
11952 "commit %s missing from histedit script", id_str);
11954 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
11958 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
11959 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
11960 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
11961 "last commit in histedit script cannot be folded");
11966 static const struct got_error *
11967 histedit_run_editor(struct got_histedit_list *histedit_cmds,
11968 const char *path, struct got_object_id_queue *commits,
11969 struct got_repository *repo)
11971 const struct got_error *err = NULL;
11975 err = get_editor(&editor);
11979 if (spawn_editor(editor, path) == -1) {
11980 err = got_error_from_errno("failed spawning editor");
11984 f = fopen(path, "re");
11986 err = got_error_from_errno("fopen");
11989 err = histedit_parse_list(histedit_cmds, f, repo);
11993 err = histedit_check_script(histedit_cmds, commits, repo);
11995 if (f && fclose(f) == EOF && err == NULL)
11996 err = got_error_from_errno("fclose");
12001 static const struct got_error *
12002 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
12003 struct got_object_id_queue *, const char *, const char *,
12004 struct got_repository *);
12006 static const struct got_error *
12007 histedit_edit_script(struct got_histedit_list *histedit_cmds,
12008 struct got_object_id_queue *commits, const char *branch_name,
12009 int edit_logmsg_only, int fold_only, int drop_only, int edit_only,
12010 struct got_repository *repo)
12012 const struct got_error *err;
12016 err = got_opentemp_named(&path, &f, "got-histedit", "");
12020 err = write_cmd_list(f, branch_name, commits);
12024 err = histedit_write_commit_list(commits, f, edit_logmsg_only,
12025 fold_only, drop_only, edit_only, repo);
12029 if (drop_only || edit_logmsg_only || fold_only || edit_only) {
12031 err = histedit_parse_list(histedit_cmds, f, repo);
12033 if (fclose(f) == EOF) {
12034 err = got_error_from_errno("fclose");
12038 err = histedit_run_editor(histedit_cmds, path, commits, repo);
12040 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
12041 err->code != GOT_ERR_HISTEDIT_CMD)
12043 err = histedit_edit_list_retry(histedit_cmds, err,
12044 commits, path, branch_name, repo);
12048 if (f && fclose(f) == EOF && err == NULL)
12049 err = got_error_from_errno("fclose");
12050 if (path && unlink(path) != 0 && err == NULL)
12051 err = got_error_from_errno2("unlink", path);
12056 static const struct got_error *
12057 histedit_save_list(struct got_histedit_list *histedit_cmds,
12058 struct got_worktree *worktree, struct got_repository *repo)
12060 const struct got_error *err = NULL;
12063 struct got_histedit_list_entry *hle;
12064 struct got_commit_object *commit = NULL;
12066 err = got_worktree_get_histedit_script_path(&path, worktree);
12070 f = fopen(path, "we");
12072 err = got_error_from_errno2("fopen", path);
12075 TAILQ_FOREACH(hle, histedit_cmds, entry) {
12076 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
12082 int n = fprintf(f, "%c %s\n",
12083 GOT_HISTEDIT_MESG, hle->logmsg);
12085 err = got_ferror(f, GOT_ERR_IO);
12091 if (f && fclose(f) == EOF && err == NULL)
12092 err = got_error_from_errno("fclose");
12095 got_object_commit_close(commit);
12100 histedit_free_list(struct got_histedit_list *histedit_cmds)
12102 struct got_histedit_list_entry *hle;
12104 while ((hle = TAILQ_FIRST(histedit_cmds))) {
12105 TAILQ_REMOVE(histedit_cmds, hle, entry);
12110 static const struct got_error *
12111 histedit_load_list(struct got_histedit_list *histedit_cmds,
12112 const char *path, struct got_repository *repo)
12114 const struct got_error *err = NULL;
12117 f = fopen(path, "re");
12119 err = got_error_from_errno2("fopen", path);
12123 err = histedit_parse_list(histedit_cmds, f, repo);
12125 if (f && fclose(f) == EOF && err == NULL)
12126 err = got_error_from_errno("fclose");
12130 static const struct got_error *
12131 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
12132 const struct got_error *edit_err, struct got_object_id_queue *commits,
12133 const char *path, const char *branch_name, struct got_repository *repo)
12135 const struct got_error *err = NULL, *prev_err = edit_err;
12138 while (resp != 'c' && resp != 'r' && resp != 'a') {
12139 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
12140 "or (a)bort: ", getprogname(), prev_err->msg);
12145 histedit_free_list(histedit_cmds);
12146 err = histedit_run_editor(histedit_cmds, path, commits,
12149 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
12150 err->code != GOT_ERR_HISTEDIT_CMD)
12157 } else if (resp == 'r') {
12158 histedit_free_list(histedit_cmds);
12159 err = histedit_edit_script(histedit_cmds,
12160 commits, branch_name, 0, 0, 0, 0, repo);
12162 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
12163 err->code != GOT_ERR_HISTEDIT_CMD)
12170 } else if (resp == 'a') {
12171 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
12174 printf("invalid response '%c'\n", resp);
12180 static const struct got_error *
12181 histedit_complete(struct got_worktree *worktree,
12182 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
12183 struct got_reference *branch, struct got_repository *repo)
12185 printf("Switching work tree to %s\n",
12186 got_ref_get_symref_target(branch));
12187 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
12191 static const struct got_error *
12192 show_histedit_progress(struct got_commit_object *commit,
12193 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
12195 const struct got_error *err;
12196 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
12198 err = got_object_id_str(&old_id_str, hle->commit_id);
12203 err = got_object_id_str(&new_id_str, new_id);
12208 old_id_str[12] = '\0';
12210 new_id_str[12] = '\0';
12213 logmsg = strdup(hle->logmsg);
12214 if (logmsg == NULL) {
12215 err = got_error_from_errno("strdup");
12218 trim_logmsg(logmsg, 42);
12220 err = get_short_logmsg(&logmsg, 42, commit);
12225 switch (hle->cmd->code) {
12226 case GOT_HISTEDIT_PICK:
12227 case GOT_HISTEDIT_EDIT:
12228 printf("%s -> %s: %s\n", old_id_str,
12229 new_id_str ? new_id_str : "no-op change", logmsg);
12231 case GOT_HISTEDIT_DROP:
12232 case GOT_HISTEDIT_FOLD:
12233 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
12245 static const struct got_error *
12246 histedit_commit(struct got_pathlist_head *merged_paths,
12247 struct got_worktree *worktree, struct got_fileindex *fileindex,
12248 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
12249 const char *committer, int allow_conflict, struct got_repository *repo)
12251 const struct got_error *err;
12252 struct got_commit_object *commit;
12253 struct got_object_id *new_commit_id;
12255 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
12256 && hle->logmsg == NULL) {
12257 err = histedit_edit_logmsg(hle, repo);
12262 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
12266 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
12267 worktree, fileindex, tmp_branch, committer, commit, hle->commit_id,
12268 hle->logmsg, allow_conflict, repo);
12270 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
12272 err = show_histedit_progress(commit, hle, NULL);
12274 err = show_histedit_progress(commit, hle, new_commit_id);
12275 free(new_commit_id);
12278 got_object_commit_close(commit);
12282 static const struct got_error *
12283 histedit_skip_commit(struct got_histedit_list_entry *hle,
12284 struct got_worktree *worktree, struct got_repository *repo)
12286 const struct got_error *error;
12287 struct got_commit_object *commit;
12289 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
12294 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
12298 error = show_histedit_progress(commit, hle, NULL);
12299 got_object_commit_close(commit);
12303 static const struct got_error *
12304 check_local_changes(void *arg, unsigned char status,
12305 unsigned char staged_status, const char *path,
12306 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
12307 struct got_object_id *commit_id, int dirfd, const char *de_name)
12309 int *have_local_changes = arg;
12312 case GOT_STATUS_ADD:
12313 case GOT_STATUS_DELETE:
12314 case GOT_STATUS_MODIFY:
12315 case GOT_STATUS_CONFLICT:
12316 *have_local_changes = 1;
12317 return got_error(GOT_ERR_CANCELLED);
12322 switch (staged_status) {
12323 case GOT_STATUS_ADD:
12324 case GOT_STATUS_DELETE:
12325 case GOT_STATUS_MODIFY:
12326 *have_local_changes = 1;
12327 return got_error(GOT_ERR_CANCELLED);
12335 static const struct got_error *
12336 cmd_histedit(int argc, char *argv[])
12338 const struct got_error *error = NULL;
12339 struct got_worktree *worktree = NULL;
12340 struct got_fileindex *fileindex = NULL;
12341 struct got_repository *repo = NULL;
12342 char *cwd = NULL, *committer = NULL, *gitconfig_path = NULL;
12343 struct got_reference *branch = NULL;
12344 struct got_reference *tmp_branch = NULL;
12345 struct got_object_id *resume_commit_id = NULL;
12346 struct got_object_id *base_commit_id = NULL;
12347 struct got_object_id *head_commit_id = NULL;
12348 struct got_commit_object *commit = NULL;
12349 int ch, rebase_in_progress = 0, merge_in_progress = 0;
12350 struct got_update_progress_arg upa;
12351 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
12352 int drop_only = 0, edit_logmsg_only = 0, fold_only = 0, edit_only = 0;
12353 int allow_conflict = 0, list_backups = 0, delete_backups = 0;
12354 const char *edit_script_path = NULL;
12355 struct got_object_id_queue commits;
12356 struct got_pathlist_head merged_paths;
12357 const struct got_object_id_queue *parent_ids;
12358 struct got_object_qid *pid;
12359 struct got_histedit_list histedit_cmds;
12360 struct got_histedit_list_entry *hle;
12361 int *pack_fds = NULL;
12363 STAILQ_INIT(&commits);
12364 TAILQ_INIT(&histedit_cmds);
12365 TAILQ_INIT(&merged_paths);
12366 memset(&upa, 0, sizeof(upa));
12369 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
12370 "unveil", NULL) == -1)
12374 while ((ch = getopt(argc, argv, "aCcdeF:flmX")) != -1) {
12380 allow_conflict = 1;
12392 edit_script_path = optarg;
12401 edit_logmsg_only = 1;
12404 delete_backups = 1;
12415 if (abort_edit && allow_conflict)
12416 option_conflict('a', 'C');
12417 if (abort_edit && continue_edit)
12418 option_conflict('a', 'c');
12419 if (edit_script_path && allow_conflict)
12420 option_conflict('F', 'C');
12421 if (edit_script_path && edit_logmsg_only)
12422 option_conflict('F', 'm');
12423 if (abort_edit && edit_logmsg_only)
12424 option_conflict('a', 'm');
12425 if (edit_logmsg_only && allow_conflict)
12426 option_conflict('m', 'C');
12427 if (continue_edit && edit_logmsg_only)
12428 option_conflict('c', 'm');
12429 if (abort_edit && fold_only)
12430 option_conflict('a', 'f');
12431 if (fold_only && allow_conflict)
12432 option_conflict('f', 'C');
12433 if (continue_edit && fold_only)
12434 option_conflict('c', 'f');
12435 if (fold_only && edit_logmsg_only)
12436 option_conflict('f', 'm');
12437 if (edit_script_path && fold_only)
12438 option_conflict('F', 'f');
12439 if (abort_edit && edit_only)
12440 option_conflict('a', 'e');
12441 if (continue_edit && edit_only)
12442 option_conflict('c', 'e');
12443 if (edit_only && edit_logmsg_only)
12444 option_conflict('e', 'm');
12445 if (edit_script_path && edit_only)
12446 option_conflict('F', 'e');
12447 if (fold_only && edit_only)
12448 option_conflict('f', 'e');
12449 if (drop_only && abort_edit)
12450 option_conflict('d', 'a');
12451 if (drop_only && allow_conflict)
12452 option_conflict('d', 'C');
12453 if (drop_only && continue_edit)
12454 option_conflict('d', 'c');
12455 if (drop_only && edit_logmsg_only)
12456 option_conflict('d', 'm');
12457 if (drop_only && edit_only)
12458 option_conflict('d', 'e');
12459 if (drop_only && edit_script_path)
12460 option_conflict('d', 'F');
12461 if (drop_only && fold_only)
12462 option_conflict('d', 'f');
12463 if (list_backups) {
12465 option_conflict('l', 'a');
12466 if (allow_conflict)
12467 option_conflict('l', 'C');
12469 option_conflict('l', 'c');
12470 if (edit_script_path)
12471 option_conflict('l', 'F');
12472 if (edit_logmsg_only)
12473 option_conflict('l', 'm');
12475 option_conflict('l', 'd');
12477 option_conflict('l', 'f');
12479 option_conflict('l', 'e');
12480 if (delete_backups)
12481 option_conflict('l', 'X');
12482 if (argc != 0 && argc != 1)
12484 } else if (delete_backups) {
12486 option_conflict('X', 'a');
12487 if (allow_conflict)
12488 option_conflict('X', 'C');
12490 option_conflict('X', 'c');
12492 option_conflict('X', 'd');
12493 if (edit_script_path)
12494 option_conflict('X', 'F');
12495 if (edit_logmsg_only)
12496 option_conflict('X', 'm');
12498 option_conflict('X', 'f');
12500 option_conflict('X', 'e');
12502 option_conflict('X', 'l');
12503 if (argc != 0 && argc != 1)
12505 } else if (allow_conflict && !continue_edit)
12506 errx(1, "-C option requires -c");
12507 else if (argc != 0)
12511 * This command cannot apply unveil(2) in all cases because the
12512 * user may choose to run an editor to edit the histedit script
12513 * and to edit individual commit log messages.
12514 * unveil(2) traverses exec(2); if an editor is used we have to
12515 * apply unveil after edit script and log messages have been written.
12516 * XXX TODO: Make use of unveil(2) where possible.
12519 cwd = getcwd(NULL, 0);
12521 error = got_error_from_errno("getcwd");
12525 error = got_repo_pack_fds_open(&pack_fds);
12529 error = got_worktree_open(&worktree, cwd);
12531 if (list_backups || delete_backups) {
12532 if (error->code != GOT_ERR_NOT_WORKTREE)
12535 if (error->code == GOT_ERR_NOT_WORKTREE)
12536 error = wrap_not_worktree_error(error,
12542 if (list_backups || delete_backups) {
12543 error = got_repo_open(&repo,
12544 worktree ? got_worktree_get_repo_path(worktree) : cwd,
12548 error = apply_unveil(got_repo_get_path(repo), 0,
12549 worktree ? got_worktree_get_root_path(worktree) : NULL);
12552 error = process_backup_refs(
12553 GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
12554 argc == 1 ? argv[0] : NULL, delete_backups, repo);
12555 goto done; /* nothing else to do */
12558 error = get_gitconfig_path(&gitconfig_path);
12561 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
12562 gitconfig_path, pack_fds);
12566 if (worktree != NULL && !list_backups && !delete_backups) {
12567 error = worktree_has_logmsg_ref("histedit", worktree, repo);
12572 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
12575 if (rebase_in_progress) {
12576 error = got_error(GOT_ERR_REBASING);
12580 error = got_worktree_merge_in_progress(&merge_in_progress, worktree,
12584 if (merge_in_progress) {
12585 error = got_error(GOT_ERR_MERGE_BUSY);
12589 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
12593 if (edit_in_progress && edit_logmsg_only) {
12594 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
12595 "histedit operation is in progress in this "
12596 "work tree and must be continued or aborted "
12597 "before the -m option can be used");
12600 if (edit_in_progress && drop_only) {
12601 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
12602 "histedit operation is in progress in this "
12603 "work tree and must be continued or aborted "
12604 "before the -d option can be used");
12607 if (edit_in_progress && fold_only) {
12608 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
12609 "histedit operation is in progress in this "
12610 "work tree and must be continued or aborted "
12611 "before the -f option can be used");
12614 if (edit_in_progress && edit_only) {
12615 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
12616 "histedit operation is in progress in this "
12617 "work tree and must be continued or aborted "
12618 "before the -e option can be used");
12622 if (edit_in_progress && abort_edit) {
12623 error = got_worktree_histedit_continue(&resume_commit_id,
12624 &tmp_branch, &branch, &base_commit_id, &fileindex,
12628 printf("Switching work tree to %s\n",
12629 got_ref_get_symref_target(branch));
12630 error = got_worktree_histedit_abort(worktree, fileindex, repo,
12631 branch, base_commit_id, abort_progress, &upa);
12634 printf("Histedit of %s aborted\n",
12635 got_ref_get_symref_target(branch));
12636 print_merge_progress_stats(&upa);
12637 goto done; /* nothing else to do */
12638 } else if (abort_edit) {
12639 error = got_error(GOT_ERR_NOT_HISTEDIT);
12643 error = get_author(&committer, repo, worktree);
12647 if (continue_edit) {
12650 if (!edit_in_progress) {
12651 error = got_error(GOT_ERR_NOT_HISTEDIT);
12655 error = got_worktree_get_histedit_script_path(&path, worktree);
12659 error = histedit_load_list(&histedit_cmds, path, repo);
12664 error = got_worktree_histedit_continue(&resume_commit_id,
12665 &tmp_branch, &branch, &base_commit_id, &fileindex,
12670 error = got_ref_resolve(&head_commit_id, repo, branch);
12674 error = got_object_open_as_commit(&commit, repo,
12678 parent_ids = got_object_commit_get_parent_ids(commit);
12679 pid = STAILQ_FIRST(parent_ids);
12681 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
12684 error = collect_commits(&commits, head_commit_id, &pid->id,
12685 base_commit_id, got_worktree_get_path_prefix(worktree),
12686 GOT_ERR_HISTEDIT_PATH, repo);
12687 got_object_commit_close(commit);
12692 if (edit_in_progress) {
12693 error = got_error(GOT_ERR_HISTEDIT_BUSY);
12697 error = got_ref_open(&branch, repo,
12698 got_worktree_get_head_ref_name(worktree), 0);
12702 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
12703 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
12704 "will not edit commit history of a branch outside "
12705 "the \"refs/heads/\" reference namespace");
12709 error = got_ref_resolve(&head_commit_id, repo, branch);
12710 got_ref_close(branch);
12715 error = got_object_open_as_commit(&commit, repo,
12719 parent_ids = got_object_commit_get_parent_ids(commit);
12720 pid = STAILQ_FIRST(parent_ids);
12722 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
12725 error = collect_commits(&commits, head_commit_id, &pid->id,
12726 got_worktree_get_base_commit_id(worktree),
12727 got_worktree_get_path_prefix(worktree),
12728 GOT_ERR_HISTEDIT_PATH, repo);
12729 got_object_commit_close(commit);
12734 if (STAILQ_EMPTY(&commits)) {
12735 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
12739 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
12740 &base_commit_id, &fileindex, worktree, repo);
12744 if (edit_script_path) {
12745 error = histedit_load_list(&histedit_cmds,
12746 edit_script_path, repo);
12748 got_worktree_histedit_abort(worktree, fileindex,
12749 repo, branch, base_commit_id,
12750 abort_progress, &upa);
12751 print_merge_progress_stats(&upa);
12755 const char *branch_name;
12756 branch_name = got_ref_get_symref_target(branch);
12757 if (strncmp(branch_name, "refs/heads/", 11) == 0)
12759 error = histedit_edit_script(&histedit_cmds, &commits,
12760 branch_name, edit_logmsg_only, fold_only,
12761 drop_only, edit_only, repo);
12763 got_worktree_histedit_abort(worktree, fileindex,
12764 repo, branch, base_commit_id,
12765 abort_progress, &upa);
12766 print_merge_progress_stats(&upa);
12772 error = histedit_save_list(&histedit_cmds, worktree,
12775 got_worktree_histedit_abort(worktree, fileindex,
12776 repo, branch, base_commit_id,
12777 abort_progress, &upa);
12778 print_merge_progress_stats(&upa);
12784 error = histedit_check_script(&histedit_cmds, &commits, repo);
12788 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
12789 if (resume_commit_id) {
12790 if (got_object_id_cmp(hle->commit_id,
12791 resume_commit_id) != 0)
12794 resume_commit_id = NULL;
12795 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
12796 hle->cmd->code == GOT_HISTEDIT_FOLD) {
12797 error = histedit_skip_commit(hle, worktree,
12802 struct got_pathlist_head paths;
12803 int have_changes = 0;
12805 TAILQ_INIT(&paths);
12806 error = got_pathlist_append(&paths, "", NULL);
12809 error = got_worktree_status(worktree, &paths,
12810 repo, 0, check_local_changes, &have_changes,
12811 check_cancelled, NULL);
12812 got_pathlist_free(&paths,
12813 GOT_PATHLIST_FREE_NONE);
12815 if (error->code != GOT_ERR_CANCELLED)
12817 if (sigint_received || sigpipe_received)
12820 if (have_changes) {
12821 error = histedit_commit(NULL, worktree,
12822 fileindex, tmp_branch, hle,
12823 committer, allow_conflict, repo);
12827 error = got_object_open_as_commit(
12828 &commit, repo, hle->commit_id);
12831 error = show_histedit_progress(commit,
12833 got_object_commit_close(commit);
12842 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
12843 error = histedit_skip_commit(hle, worktree, repo);
12849 error = got_object_open_as_commit(&commit, repo,
12853 parent_ids = got_object_commit_get_parent_ids(commit);
12854 pid = STAILQ_FIRST(parent_ids);
12856 error = got_worktree_histedit_merge_files(&merged_paths,
12857 worktree, fileindex, &pid->id, hle->commit_id, repo,
12858 update_progress, &upa, check_cancelled, NULL);
12861 got_object_commit_close(commit);
12864 print_merge_progress_stats(&upa);
12865 if (upa.conflicts > 0 || upa.missing > 0 ||
12866 upa.not_deleted > 0 || upa.unversioned > 0) {
12867 if (upa.conflicts > 0) {
12868 error = show_rebase_merge_conflict(
12869 hle->commit_id, repo);
12873 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
12877 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
12879 error = got_object_id_str(&id_str, hle->commit_id);
12882 printf("Stopping histedit for amending commit %s\n",
12885 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
12886 error = got_worktree_histedit_postpone(worktree,
12891 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
12892 error = histedit_skip_commit(hle, worktree, repo);
12898 error = histedit_commit(&merged_paths, worktree, fileindex,
12899 tmp_branch, hle, committer, allow_conflict, repo);
12900 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
12905 if (upa.conflicts > 0 || upa.missing > 0 ||
12906 upa.not_deleted > 0 || upa.unversioned > 0) {
12907 error = got_worktree_histedit_postpone(worktree, fileindex);
12910 if (upa.conflicts > 0 && upa.missing == 0 &&
12911 upa.not_deleted == 0 && upa.unversioned == 0) {
12912 error = got_error_msg(GOT_ERR_CONFLICTS,
12913 "conflicts must be resolved before histedit "
12915 } else if (upa.conflicts > 0) {
12916 error = got_error_msg(GOT_ERR_CONFLICTS,
12917 "conflicts must be resolved before histedit "
12918 "can continue; changes destined for some "
12919 "files were not yet merged and should be "
12920 "merged manually if required before the "
12921 "histedit operation is continued");
12923 error = got_error_msg(GOT_ERR_CONFLICTS,
12924 "changes destined for some files were not "
12925 "yet merged and should be merged manually "
12926 "if required before the histedit operation "
12930 error = histedit_complete(worktree, fileindex, tmp_branch,
12935 free(gitconfig_path);
12936 got_object_id_queue_free(&commits);
12937 histedit_free_list(&histedit_cmds);
12938 free(head_commit_id);
12939 free(base_commit_id);
12940 free(resume_commit_id);
12942 got_object_commit_close(commit);
12944 got_ref_close(branch);
12946 got_ref_close(tmp_branch);
12948 got_worktree_close(worktree);
12950 const struct got_error *close_err = got_repo_close(repo);
12955 const struct got_error *pack_err =
12956 got_repo_pack_fds_close(pack_fds);
12964 usage_integrate(void)
12966 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
12970 static const struct got_error *
12971 cmd_integrate(int argc, char *argv[])
12973 const struct got_error *error = NULL;
12974 struct got_repository *repo = NULL;
12975 struct got_worktree *worktree = NULL;
12976 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
12977 const char *branch_arg = NULL;
12978 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
12979 struct got_fileindex *fileindex = NULL;
12980 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
12982 struct got_update_progress_arg upa;
12983 int *pack_fds = NULL;
12986 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
12987 "unveil", NULL) == -1)
12991 while ((ch = getopt(argc, argv, "")) != -1) {
13004 branch_arg = argv[0];
13006 cwd = getcwd(NULL, 0);
13008 error = got_error_from_errno("getcwd");
13012 error = got_repo_pack_fds_open(&pack_fds);
13016 error = got_worktree_open(&worktree, cwd);
13018 if (error->code == GOT_ERR_NOT_WORKTREE)
13019 error = wrap_not_worktree_error(error, "integrate",
13024 error = check_rebase_or_histedit_in_progress(worktree);
13028 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
13033 error = apply_unveil(got_repo_get_path(repo), 0,
13034 got_worktree_get_root_path(worktree));
13038 error = check_merge_in_progress(worktree, repo);
13042 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
13043 error = got_error_from_errno("asprintf");
13047 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
13048 &base_branch_ref, worktree, refname, repo);
13052 refname = strdup(got_ref_get_name(branch_ref));
13053 if (refname == NULL) {
13054 error = got_error_from_errno("strdup");
13055 got_worktree_integrate_abort(worktree, fileindex, repo,
13056 branch_ref, base_branch_ref);
13059 base_refname = strdup(got_ref_get_name(base_branch_ref));
13060 if (base_refname == NULL) {
13061 error = got_error_from_errno("strdup");
13062 got_worktree_integrate_abort(worktree, fileindex, repo,
13063 branch_ref, base_branch_ref);
13066 if (strncmp(base_refname, "refs/heads/", 11) != 0) {
13067 error = got_error(GOT_ERR_INTEGRATE_BRANCH);
13068 got_worktree_integrate_abort(worktree, fileindex, repo,
13069 branch_ref, base_branch_ref);
13073 error = got_ref_resolve(&commit_id, repo, branch_ref);
13077 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
13081 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
13082 error = got_error_msg(GOT_ERR_SAME_BRANCH,
13083 "specified branch has already been integrated");
13084 got_worktree_integrate_abort(worktree, fileindex, repo,
13085 branch_ref, base_branch_ref);
13089 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
13091 if (error->code == GOT_ERR_ANCESTRY)
13092 error = got_error(GOT_ERR_REBASE_REQUIRED);
13093 got_worktree_integrate_abort(worktree, fileindex, repo,
13094 branch_ref, base_branch_ref);
13098 memset(&upa, 0, sizeof(upa));
13099 error = got_worktree_integrate_continue(worktree, fileindex, repo,
13100 branch_ref, base_branch_ref, update_progress, &upa,
13101 check_cancelled, NULL);
13105 printf("Integrated %s into %s\n", refname, base_refname);
13106 print_update_progress_stats(&upa);
13109 const struct got_error *close_err = got_repo_close(repo);
13114 got_worktree_close(worktree);
13116 const struct got_error *pack_err =
13117 got_repo_pack_fds_close(pack_fds);
13122 free(base_commit_id);
13125 free(base_refname);
13132 fprintf(stderr, "usage: %s merge [-aCcn] [branch]\n", getprogname());
13136 static const struct got_error *
13137 cmd_merge(int argc, char *argv[])
13139 const struct got_error *error = NULL;
13140 struct got_worktree *worktree = NULL;
13141 struct got_repository *repo = NULL;
13142 struct got_fileindex *fileindex = NULL;
13143 char *cwd = NULL, *id_str = NULL, *author = NULL;
13144 char *gitconfig_path = NULL;
13145 struct got_reference *branch = NULL, *wt_branch = NULL;
13146 struct got_object_id *branch_tip = NULL, *yca_id = NULL;
13147 struct got_object_id *wt_branch_tip = NULL;
13148 int ch, merge_in_progress = 0, abort_merge = 0, continue_merge = 0;
13149 int allow_conflict = 0, prefer_fast_forward = 1, interrupt_merge = 0;
13150 struct got_update_progress_arg upa;
13151 struct got_object_id *merge_commit_id = NULL;
13152 char *branch_name = NULL;
13153 int *pack_fds = NULL;
13155 memset(&upa, 0, sizeof(upa));
13158 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
13159 "unveil", NULL) == -1)
13163 while ((ch = getopt(argc, argv, "aCcMn")) != -1) {
13169 allow_conflict = 1;
13172 continue_merge = 1;
13175 prefer_fast_forward = 0;
13178 interrupt_merge = 1;
13190 if (continue_merge)
13191 option_conflict('a', 'c');
13192 if (!prefer_fast_forward)
13193 option_conflict('a', 'M');
13194 if (interrupt_merge)
13195 option_conflict('a', 'n');
13196 } else if (continue_merge) {
13197 if (!prefer_fast_forward)
13198 option_conflict('c', 'M');
13199 if (interrupt_merge)
13200 option_conflict('c', 'n');
13202 if (allow_conflict) {
13203 if (!continue_merge)
13204 errx(1, "-C option requires -c");
13206 if (abort_merge || continue_merge) {
13209 } else if (argc != 1)
13212 cwd = getcwd(NULL, 0);
13214 error = got_error_from_errno("getcwd");
13218 error = got_repo_pack_fds_open(&pack_fds);
13222 error = got_worktree_open(&worktree, cwd);
13224 if (error->code == GOT_ERR_NOT_WORKTREE)
13225 error = wrap_not_worktree_error(error,
13230 error = get_gitconfig_path(&gitconfig_path);
13233 error = got_repo_open(&repo,
13234 worktree ? got_worktree_get_repo_path(worktree) : cwd,
13235 gitconfig_path, pack_fds);
13239 if (worktree != NULL) {
13240 error = worktree_has_logmsg_ref("merge", worktree, repo);
13245 error = apply_unveil(got_repo_get_path(repo), 0,
13246 worktree ? got_worktree_get_root_path(worktree) : NULL);
13250 error = check_rebase_or_histedit_in_progress(worktree);
13254 error = got_worktree_merge_in_progress(&merge_in_progress, worktree,
13259 if (merge_in_progress && !(abort_merge || continue_merge)) {
13260 error = got_error(GOT_ERR_MERGE_BUSY);
13264 if (!merge_in_progress && (abort_merge || continue_merge)) {
13265 error = got_error(GOT_ERR_NOT_MERGING);
13270 error = got_worktree_merge_continue(&branch_name,
13271 &branch_tip, &fileindex, worktree, repo);
13274 error = got_worktree_merge_abort(worktree, fileindex, repo,
13275 abort_progress, &upa);
13278 printf("Merge of %s aborted\n", branch_name);
13279 goto done; /* nothing else to do */
13282 if (strncmp(got_worktree_get_head_ref_name(worktree),
13283 "refs/heads/", 11) != 0) {
13284 error = got_error_fmt(GOT_ERR_COMMIT_BRANCH,
13285 "work tree's current branch %s is outside the "
13286 "\"refs/heads/\" reference namespace; "
13287 "update -b required",
13288 got_worktree_get_head_ref_name(worktree));
13292 error = get_author(&author, repo, worktree);
13296 error = got_ref_open(&wt_branch, repo,
13297 got_worktree_get_head_ref_name(worktree), 0);
13300 error = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
13304 if (continue_merge) {
13305 struct got_object_id *base_commit_id;
13306 base_commit_id = got_worktree_get_base_commit_id(worktree);
13307 if (got_object_id_cmp(wt_branch_tip, base_commit_id) != 0) {
13308 error = got_error(GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
13311 error = got_worktree_merge_continue(&branch_name,
13312 &branch_tip, &fileindex, worktree, repo);
13316 error = got_ref_open(&branch, repo, argv[0], 0);
13319 branch_name = strdup(got_ref_get_name(branch));
13320 if (branch_name == NULL) {
13321 error = got_error_from_errno("strdup");
13324 error = got_ref_resolve(&branch_tip, repo, branch);
13329 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
13330 wt_branch_tip, branch_tip, 0, repo,
13331 check_cancelled, NULL);
13332 if (error && error->code != GOT_ERR_ANCESTRY)
13335 if (!continue_merge) {
13336 error = check_path_prefix(wt_branch_tip, branch_tip,
13337 got_worktree_get_path_prefix(worktree),
13338 GOT_ERR_MERGE_PATH, repo);
13341 error = got_worktree_merge_prepare(&fileindex, worktree, repo);
13344 if (prefer_fast_forward && yca_id &&
13345 got_object_id_cmp(wt_branch_tip, yca_id) == 0) {
13346 struct got_pathlist_head paths;
13347 if (interrupt_merge) {
13348 error = got_error_fmt(GOT_ERR_BAD_OPTION,
13349 "there are no changes to merge since %s "
13350 "is already based on %s; merge cannot be "
13351 "interrupted for amending; -n",
13352 branch_name, got_ref_get_name(wt_branch));
13355 printf("Forwarding %s to %s\n",
13356 got_ref_get_name(wt_branch), branch_name);
13357 error = got_ref_change_ref(wt_branch, branch_tip);
13360 error = got_ref_write(wt_branch, repo);
13363 error = got_worktree_set_base_commit_id(worktree, repo,
13367 TAILQ_INIT(&paths);
13368 error = got_pathlist_append(&paths, "", NULL);
13371 error = got_worktree_checkout_files(worktree,
13372 &paths, repo, update_progress, &upa,
13373 check_cancelled, NULL);
13374 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
13377 if (upa.did_something) {
13379 error = got_object_id_str(&id_str, branch_tip);
13382 printf("Updated to commit %s\n", id_str);
13385 printf("Already up-to-date\n");
13386 print_update_progress_stats(&upa);
13389 error = got_worktree_merge_write_refs(worktree, branch, repo);
13393 error = got_worktree_merge_branch(worktree, fileindex,
13394 yca_id, branch_tip, repo, update_progress, &upa,
13395 check_cancelled, NULL);
13398 print_merge_progress_stats(&upa);
13399 if (!upa.did_something) {
13400 error = got_worktree_merge_abort(worktree, fileindex,
13401 repo, abort_progress, &upa);
13404 printf("Already up-to-date\n");
13409 if (interrupt_merge) {
13410 error = got_worktree_merge_postpone(worktree, fileindex);
13413 printf("Merge of %s interrupted on request\n", branch_name);
13414 } else if (upa.conflicts > 0 || upa.missing > 0 ||
13415 upa.not_deleted > 0 || upa.unversioned > 0) {
13416 error = got_worktree_merge_postpone(worktree, fileindex);
13419 if (upa.conflicts > 0 && upa.missing == 0 &&
13420 upa.not_deleted == 0 && upa.unversioned == 0) {
13421 error = got_error_msg(GOT_ERR_CONFLICTS,
13422 "conflicts must be resolved before merging "
13424 } else if (upa.conflicts > 0) {
13425 error = got_error_msg(GOT_ERR_CONFLICTS,
13426 "conflicts must be resolved before merging "
13427 "can continue; changes destined for some "
13428 "files were not yet merged and "
13429 "should be merged manually if required before the "
13430 "merge operation is continued");
13432 error = got_error_msg(GOT_ERR_CONFLICTS,
13433 "changes destined for some "
13434 "files were not yet merged and should be "
13435 "merged manually if required before the "
13436 "merge operation is continued");
13440 error = got_worktree_merge_commit(&merge_commit_id, worktree,
13441 fileindex, author, NULL, 1, branch_tip, branch_name,
13442 allow_conflict, repo, continue_merge ? print_status : NULL,
13446 error = got_worktree_merge_complete(worktree, fileindex, repo);
13449 error = got_object_id_str(&id_str, merge_commit_id);
13452 printf("Merged %s into %s: %s\n", branch_name,
13453 got_worktree_get_head_ref_name(worktree),
13458 free(gitconfig_path);
13460 free(merge_commit_id);
13466 got_ref_close(branch);
13468 got_ref_close(wt_branch);
13470 got_worktree_close(worktree);
13472 const struct got_error *close_err = got_repo_close(repo);
13477 const struct got_error *pack_err =
13478 got_repo_pack_fds_close(pack_fds);
13488 fprintf(stderr, "usage: %s stage [-lpS] [-F response-script] "
13489 "[path ...]\n", getprogname());
13493 static const struct got_error *
13494 print_stage(void *arg, unsigned char status, unsigned char staged_status,
13495 const char *path, struct got_object_id *blob_id,
13496 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
13497 int dirfd, const char *de_name)
13499 const struct got_error *err = NULL;
13500 char *id_str = NULL;
13502 if (staged_status != GOT_STATUS_ADD &&
13503 staged_status != GOT_STATUS_MODIFY &&
13504 staged_status != GOT_STATUS_DELETE)
13507 if (staged_status == GOT_STATUS_ADD ||
13508 staged_status == GOT_STATUS_MODIFY)
13509 err = got_object_id_str(&id_str, staged_blob_id);
13511 err = got_object_id_str(&id_str, blob_id);
13515 printf("%s %c %s\n", id_str, staged_status, path);
13520 static const struct got_error *
13521 cmd_stage(int argc, char *argv[])
13523 const struct got_error *error = NULL;
13524 struct got_repository *repo = NULL;
13525 struct got_worktree *worktree = NULL;
13527 struct got_pathlist_head paths;
13528 int ch, list_stage = 0, pflag = 0, allow_bad_symlinks = 0;
13529 FILE *patch_script_file = NULL;
13530 const char *patch_script_path = NULL;
13531 struct choose_patch_arg cpa;
13532 int *pack_fds = NULL;
13534 TAILQ_INIT(&paths);
13537 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
13538 "unveil", NULL) == -1)
13542 while ((ch = getopt(argc, argv, "F:lpS")) != -1) {
13545 patch_script_path = optarg;
13554 allow_bad_symlinks = 1;
13565 if (list_stage && (pflag || patch_script_path))
13566 errx(1, "-l option cannot be used with other options");
13567 if (patch_script_path && !pflag)
13568 errx(1, "-F option can only be used together with -p option");
13570 cwd = getcwd(NULL, 0);
13572 error = got_error_from_errno("getcwd");
13576 error = got_repo_pack_fds_open(&pack_fds);
13580 error = got_worktree_open(&worktree, cwd);
13582 if (error->code == GOT_ERR_NOT_WORKTREE)
13583 error = wrap_not_worktree_error(error, "stage", cwd);
13587 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
13592 if (patch_script_path) {
13593 patch_script_file = fopen(patch_script_path, "re");
13594 if (patch_script_file == NULL) {
13595 error = got_error_from_errno2("fopen",
13596 patch_script_path);
13600 error = apply_unveil(got_repo_get_path(repo), 0,
13601 got_worktree_get_root_path(worktree));
13605 error = check_merge_in_progress(worktree, repo);
13609 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
13614 error = got_worktree_status(worktree, &paths, repo, 0,
13615 print_stage, NULL, check_cancelled, NULL);
13617 cpa.patch_script_file = patch_script_file;
13618 cpa.action = "stage";
13619 error = got_worktree_stage(worktree, &paths,
13620 pflag ? NULL : print_status, NULL,
13621 pflag ? choose_patch : NULL, &cpa,
13622 allow_bad_symlinks, repo);
13625 if (patch_script_file && fclose(patch_script_file) == EOF &&
13627 error = got_error_from_errno2("fclose", patch_script_path);
13629 const struct got_error *close_err = got_repo_close(repo);
13634 got_worktree_close(worktree);
13636 const struct got_error *pack_err =
13637 got_repo_pack_fds_close(pack_fds);
13641 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
13647 usage_unstage(void)
13649 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
13650 "[path ...]\n", getprogname());
13655 static const struct got_error *
13656 cmd_unstage(int argc, char *argv[])
13658 const struct got_error *error = NULL;
13659 struct got_repository *repo = NULL;
13660 struct got_worktree *worktree = NULL;
13662 struct got_pathlist_head paths;
13664 struct got_update_progress_arg upa;
13665 FILE *patch_script_file = NULL;
13666 const char *patch_script_path = NULL;
13667 struct choose_patch_arg cpa;
13668 int *pack_fds = NULL;
13670 TAILQ_INIT(&paths);
13673 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
13674 "unveil", NULL) == -1)
13678 while ((ch = getopt(argc, argv, "F:p")) != -1) {
13681 patch_script_path = optarg;
13695 if (patch_script_path && !pflag)
13696 errx(1, "-F option can only be used together with -p option");
13698 cwd = getcwd(NULL, 0);
13700 error = got_error_from_errno("getcwd");
13704 error = got_repo_pack_fds_open(&pack_fds);
13708 error = got_worktree_open(&worktree, cwd);
13710 if (error->code == GOT_ERR_NOT_WORKTREE)
13711 error = wrap_not_worktree_error(error, "unstage", cwd);
13715 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
13720 if (patch_script_path) {
13721 patch_script_file = fopen(patch_script_path, "re");
13722 if (patch_script_file == NULL) {
13723 error = got_error_from_errno2("fopen",
13724 patch_script_path);
13729 error = apply_unveil(got_repo_get_path(repo), 0,
13730 got_worktree_get_root_path(worktree));
13734 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
13738 cpa.patch_script_file = patch_script_file;
13739 cpa.action = "unstage";
13740 memset(&upa, 0, sizeof(upa));
13741 error = got_worktree_unstage(worktree, &paths, update_progress,
13742 &upa, pflag ? choose_patch : NULL, &cpa, repo);
13744 print_merge_progress_stats(&upa);
13746 if (patch_script_file && fclose(patch_script_file) == EOF &&
13748 error = got_error_from_errno2("fclose", patch_script_path);
13750 const struct got_error *close_err = got_repo_close(repo);
13755 got_worktree_close(worktree);
13757 const struct got_error *pack_err =
13758 got_repo_pack_fds_close(pack_fds);
13762 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
13770 fprintf(stderr, "usage: %s cat [-P] [-c commit] [-r repository-path] "
13771 "arg ...\n", getprogname());
13775 static const struct got_error *
13776 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
13778 const struct got_error *err;
13779 struct got_blob_object *blob;
13782 fd = got_opentempfd();
13784 return got_error_from_errno("got_opentempfd");
13786 err = got_object_open_as_blob(&blob, repo, id, 8192, fd);
13790 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
13792 if (fd != -1 && close(fd) == -1 && err == NULL)
13793 err = got_error_from_errno("close");
13795 got_object_blob_close(blob);
13799 static const struct got_error *
13800 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
13802 const struct got_error *err;
13803 struct got_tree_object *tree;
13806 err = got_object_open_as_tree(&tree, repo, id);
13810 nentries = got_object_tree_get_nentries(tree);
13811 for (i = 0; i < nentries; i++) {
13812 struct got_tree_entry *te;
13814 if (sigint_received || sigpipe_received)
13816 te = got_object_tree_get_entry(tree, i);
13817 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
13820 fprintf(outfile, "%s %.7o %s\n", id_str,
13821 got_tree_entry_get_mode(te),
13822 got_tree_entry_get_name(te));
13826 got_object_tree_close(tree);
13830 static const struct got_error *
13831 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
13833 const struct got_error *err;
13834 struct got_commit_object *commit;
13835 const struct got_object_id_queue *parent_ids;
13836 struct got_object_qid *pid;
13837 char *id_str = NULL;
13838 const char *logmsg = NULL;
13841 err = got_object_open_as_commit(&commit, repo, id);
13845 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
13849 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
13850 parent_ids = got_object_commit_get_parent_ids(commit);
13851 fprintf(outfile, "numparents %d\n",
13852 got_object_commit_get_nparents(commit));
13853 STAILQ_FOREACH(pid, parent_ids, entry) {
13855 err = got_object_id_str(&pid_str, &pid->id);
13858 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
13861 got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
13862 got_object_commit_get_author_gmtoff(commit));
13863 fprintf(outfile, "%s%s %lld %s\n", GOT_COMMIT_LABEL_AUTHOR,
13864 got_object_commit_get_author(commit),
13865 (long long)got_object_commit_get_author_time(commit),
13868 got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
13869 got_object_commit_get_committer_gmtoff(commit));
13870 fprintf(outfile, "%s%s %lld %s\n", GOT_COMMIT_LABEL_COMMITTER,
13871 got_object_commit_get_committer(commit),
13872 (long long)got_object_commit_get_committer_time(commit),
13875 logmsg = got_object_commit_get_logmsg_raw(commit);
13876 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
13877 fprintf(outfile, "%s", logmsg);
13880 got_object_commit_close(commit);
13884 static const struct got_error *
13885 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
13887 const struct got_error *err;
13888 struct got_tag_object *tag;
13889 char *id_str = NULL;
13890 const char *tagmsg = NULL;
13893 err = got_object_open_as_tag(&tag, repo, id);
13897 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
13901 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
13903 switch (got_object_tag_get_object_type(tag)) {
13904 case GOT_OBJ_TYPE_BLOB:
13905 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
13906 GOT_OBJ_LABEL_BLOB);
13908 case GOT_OBJ_TYPE_TREE:
13909 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
13910 GOT_OBJ_LABEL_TREE);
13912 case GOT_OBJ_TYPE_COMMIT:
13913 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
13914 GOT_OBJ_LABEL_COMMIT);
13916 case GOT_OBJ_TYPE_TAG:
13917 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
13918 GOT_OBJ_LABEL_TAG);
13924 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
13925 got_object_tag_get_name(tag));
13927 got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
13928 got_object_tag_get_tagger_gmtoff(tag));
13929 fprintf(outfile, "%s%s %lld %s\n", GOT_TAG_LABEL_TAGGER,
13930 got_object_tag_get_tagger(tag),
13931 (long long)got_object_tag_get_tagger_time(tag),
13934 tagmsg = got_object_tag_get_message(tag);
13935 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
13936 fprintf(outfile, "%s", tagmsg);
13939 got_object_tag_close(tag);
13943 static const struct got_error *
13944 cmd_cat(int argc, char *argv[])
13946 const struct got_error *error;
13947 struct got_repository *repo = NULL;
13948 struct got_worktree *worktree = NULL;
13949 char *cwd = NULL, *repo_path = NULL, *label = NULL;
13950 const char *commit_id_str = NULL;
13951 struct got_object_id *id = NULL, *commit_id = NULL;
13952 struct got_commit_object *commit = NULL;
13953 int ch, obj_type, i, force_path = 0;
13954 struct got_reflist_head refs;
13955 int *pack_fds = NULL;
13960 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
13965 while ((ch = getopt(argc, argv, "c:Pr:")) != -1) {
13968 commit_id_str = optarg;
13974 repo_path = realpath(optarg, NULL);
13975 if (repo_path == NULL)
13976 return got_error_from_errno2("realpath",
13978 got_path_strip_trailing_slashes(repo_path);
13989 cwd = getcwd(NULL, 0);
13991 error = got_error_from_errno("getcwd");
13995 error = got_repo_pack_fds_open(&pack_fds);
13999 if (repo_path == NULL) {
14000 error = got_worktree_open(&worktree, cwd);
14001 if (error && error->code != GOT_ERR_NOT_WORKTREE)
14004 repo_path = strdup(
14005 got_worktree_get_repo_path(worktree));
14006 if (repo_path == NULL) {
14007 error = got_error_from_errno("strdup");
14011 /* Release work tree lock. */
14012 got_worktree_close(worktree);
14017 if (repo_path == NULL) {
14018 repo_path = strdup(cwd);
14019 if (repo_path == NULL)
14020 return got_error_from_errno("strdup");
14023 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
14028 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
14032 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
14036 if (commit_id_str == NULL)
14037 commit_id_str = GOT_REF_HEAD;
14038 error = got_repo_match_object_id(&commit_id, NULL,
14039 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
14043 error = got_object_open_as_commit(&commit, repo, commit_id);
14047 for (i = 0; i < argc; i++) {
14049 error = got_object_id_by_path(&id, repo, commit,
14054 error = got_repo_match_object_id(&id, &label, argv[i],
14055 GOT_OBJ_TYPE_ANY, NULL /* do not resolve tags */,
14058 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
14059 error->code != GOT_ERR_NOT_REF)
14061 error = got_object_id_by_path(&id, repo,
14068 error = got_object_get_type(&obj_type, repo, id);
14072 switch (obj_type) {
14073 case GOT_OBJ_TYPE_BLOB:
14074 error = cat_blob(id, repo, stdout);
14076 case GOT_OBJ_TYPE_TREE:
14077 error = cat_tree(id, repo, stdout);
14079 case GOT_OBJ_TYPE_COMMIT:
14080 error = cat_commit(id, repo, stdout);
14082 case GOT_OBJ_TYPE_TAG:
14083 error = cat_tag(id, repo, stdout);
14086 error = got_error(GOT_ERR_OBJ_TYPE);
14101 got_object_commit_close(commit);
14103 got_worktree_close(worktree);
14105 const struct got_error *close_err = got_repo_close(repo);
14110 const struct got_error *pack_err =
14111 got_repo_pack_fds_close(pack_fds);
14116 got_ref_list_free(&refs);
14123 fprintf(stderr, "usage: %s info [path ...]\n",
14128 static const struct got_error *
14129 print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
14130 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
14131 struct got_object_id *commit_id)
14133 const struct got_error *err = NULL;
14134 char *id_str = NULL;
14136 struct tm mytm, *tm;
14137 struct got_pathlist_head *paths = arg;
14138 struct got_pathlist_entry *pe;
14141 * Clear error indication from any of the path arguments which
14142 * would cause this file index entry to be displayed.
14144 TAILQ_FOREACH(pe, paths, entry) {
14145 if (got_path_cmp(path, pe->path, strlen(path),
14146 pe->path_len) == 0 ||
14147 got_path_is_child(path, pe->path, pe->path_len))
14148 pe->data = NULL; /* no error */
14151 printf(GOT_COMMIT_SEP_STR);
14153 printf("symlink: %s\n", path);
14154 else if (S_ISREG(mode)) {
14155 printf("file: %s\n", path);
14156 printf("mode: %o\n", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
14157 } else if (S_ISDIR(mode))
14158 printf("directory: %s\n", path);
14160 printf("something: %s\n", path);
14162 tm = localtime_r(&mtime, &mytm);
14165 if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) == 0)
14166 return got_error(GOT_ERR_NO_SPACE);
14167 printf("timestamp: %s\n", datebuf);
14170 err = got_object_id_str(&id_str, blob_id);
14173 printf("based on blob: %s\n", id_str);
14177 if (staged_blob_id) {
14178 err = got_object_id_str(&id_str, staged_blob_id);
14181 printf("based on staged blob: %s\n", id_str);
14186 err = got_object_id_str(&id_str, commit_id);
14189 printf("based on commit: %s\n", id_str);
14196 static const struct got_error *
14197 cmd_info(int argc, char *argv[])
14199 const struct got_error *error = NULL;
14200 struct got_worktree *worktree = NULL;
14201 char *cwd = NULL, *id_str = NULL;
14202 struct got_pathlist_head paths;
14203 char *uuidstr = NULL;
14204 int ch, show_files = 0;
14206 TAILQ_INIT(&paths);
14209 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
14214 while ((ch = getopt(argc, argv, "")) != -1) {
14225 cwd = getcwd(NULL, 0);
14227 error = got_error_from_errno("getcwd");
14231 error = got_worktree_open(&worktree, cwd);
14233 if (error->code == GOT_ERR_NOT_WORKTREE)
14234 error = wrap_not_worktree_error(error, "info", cwd);
14239 /* Remove "wpath cpath proc exec sendfd" promises. */
14240 if (pledge("stdio rpath flock unveil", NULL) == -1)
14243 error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
14248 error = get_worktree_paths_from_argv(&paths, argc, argv,
14255 error = got_object_id_str(&id_str,
14256 got_worktree_get_base_commit_id(worktree));
14260 error = got_worktree_get_uuid(&uuidstr, worktree);
14264 printf("work tree: %s\n", got_worktree_get_root_path(worktree));
14265 printf("work tree base commit: %s\n", id_str);
14266 printf("work tree path prefix: %s\n",
14267 got_worktree_get_path_prefix(worktree));
14268 printf("work tree branch reference: %s\n",
14269 got_worktree_get_head_ref_name(worktree));
14270 printf("work tree UUID: %s\n", uuidstr);
14271 printf("repository: %s\n", got_worktree_get_repo_path(worktree));
14274 struct got_pathlist_entry *pe;
14275 TAILQ_FOREACH(pe, &paths, entry) {
14276 if (pe->path_len == 0)
14279 * Assume this path will fail. This will be corrected
14280 * in print_path_info() in case the path does suceeed.
14282 pe->data = (void *)got_error(GOT_ERR_BAD_PATH);
14284 error = got_worktree_path_info(worktree, &paths,
14285 print_path_info, &paths, check_cancelled, NULL);
14288 TAILQ_FOREACH(pe, &paths, entry) {
14289 if (pe->data != NULL) {
14290 const struct got_error *perr;
14293 error = got_error_fmt(perr->code, "%s",
14301 got_worktree_close(worktree);
14302 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);