2 * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Copyright (c) 2020 Ori Bernstein <ori@openbsd.org>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/queue.h>
21 #include <sys/types.h>
42 #include "got_compat.h"
44 #include "got_version.h"
45 #include "got_error.h"
46 #include "got_object.h"
47 #include "got_reference.h"
48 #include "got_repository.h"
50 #include "got_cancel.h"
51 #include "got_worktree.h"
53 #include "got_commit_graph.h"
54 #include "got_fetch.h"
56 #include "got_blame.h"
57 #include "got_privsep.h"
58 #include "got_opentemp.h"
59 #include "got_gotconfig.h"
61 #include "got_patch.h"
66 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
69 static volatile sig_atomic_t sigint_received;
70 static volatile sig_atomic_t sigpipe_received;
73 catch_sigint(int signo)
79 catch_sigpipe(int signo)
87 const struct got_error *(*cmd_main)(int, char *[]);
88 void (*cmd_usage)(void);
89 const char *cmd_alias;
92 __dead static void usage(int, int);
93 __dead static void usage_import(void);
94 __dead static void usage_clone(void);
95 __dead static void usage_fetch(void);
96 __dead static void usage_checkout(void);
97 __dead static void usage_update(void);
98 __dead static void usage_log(void);
99 __dead static void usage_diff(void);
100 __dead static void usage_blame(void);
101 __dead static void usage_tree(void);
102 __dead static void usage_status(void);
103 __dead static void usage_ref(void);
104 __dead static void usage_branch(void);
105 __dead static void usage_tag(void);
106 __dead static void usage_add(void);
107 __dead static void usage_remove(void);
108 __dead static void usage_patch(void);
109 __dead static void usage_revert(void);
110 __dead static void usage_commit(void);
111 __dead static void usage_send(void);
112 __dead static void usage_cherrypick(void);
113 __dead static void usage_backout(void);
114 __dead static void usage_rebase(void);
115 __dead static void usage_histedit(void);
116 __dead static void usage_integrate(void);
117 __dead static void usage_merge(void);
118 __dead static void usage_stage(void);
119 __dead static void usage_unstage(void);
120 __dead static void usage_cat(void);
121 __dead static void usage_info(void);
123 static const struct got_error* cmd_import(int, char *[]);
124 static const struct got_error* cmd_clone(int, char *[]);
125 static const struct got_error* cmd_fetch(int, char *[]);
126 static const struct got_error* cmd_checkout(int, char *[]);
127 static const struct got_error* cmd_update(int, char *[]);
128 static const struct got_error* cmd_log(int, char *[]);
129 static const struct got_error* cmd_diff(int, char *[]);
130 static const struct got_error* cmd_blame(int, char *[]);
131 static const struct got_error* cmd_tree(int, char *[]);
132 static const struct got_error* cmd_status(int, char *[]);
133 static const struct got_error* cmd_ref(int, char *[]);
134 static const struct got_error* cmd_branch(int, char *[]);
135 static const struct got_error* cmd_tag(int, char *[]);
136 static const struct got_error* cmd_add(int, char *[]);
137 static const struct got_error* cmd_remove(int, char *[]);
138 static const struct got_error* cmd_patch(int, char *[]);
139 static const struct got_error* cmd_revert(int, char *[]);
140 static const struct got_error* cmd_commit(int, char *[]);
141 static const struct got_error* cmd_send(int, char *[]);
142 static const struct got_error* cmd_cherrypick(int, char *[]);
143 static const struct got_error* cmd_backout(int, char *[]);
144 static const struct got_error* cmd_rebase(int, char *[]);
145 static const struct got_error* cmd_histedit(int, char *[]);
146 static const struct got_error* cmd_integrate(int, char *[]);
147 static const struct got_error* cmd_merge(int, char *[]);
148 static const struct got_error* cmd_stage(int, char *[]);
149 static const struct got_error* cmd_unstage(int, char *[]);
150 static const struct got_error* cmd_cat(int, char *[]);
151 static const struct got_error* cmd_info(int, char *[]);
153 static const struct got_cmd got_commands[] = {
154 { "import", cmd_import, usage_import, "im" },
155 { "clone", cmd_clone, usage_clone, "cl" },
156 { "fetch", cmd_fetch, usage_fetch, "fe" },
157 { "checkout", cmd_checkout, usage_checkout, "co" },
158 { "update", cmd_update, usage_update, "up" },
159 { "log", cmd_log, usage_log, "" },
160 { "diff", cmd_diff, usage_diff, "di" },
161 { "blame", cmd_blame, usage_blame, "bl" },
162 { "tree", cmd_tree, usage_tree, "tr" },
163 { "status", cmd_status, usage_status, "st" },
164 { "ref", cmd_ref, usage_ref, "" },
165 { "branch", cmd_branch, usage_branch, "br" },
166 { "tag", cmd_tag, usage_tag, "" },
167 { "add", cmd_add, usage_add, "" },
168 { "remove", cmd_remove, usage_remove, "rm" },
169 { "patch", cmd_patch, usage_patch, "pa" },
170 { "revert", cmd_revert, usage_revert, "rv" },
171 { "commit", cmd_commit, usage_commit, "ci" },
172 { "send", cmd_send, usage_send, "se" },
173 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
174 { "backout", cmd_backout, usage_backout, "bo" },
175 { "rebase", cmd_rebase, usage_rebase, "rb" },
176 { "histedit", cmd_histedit, usage_histedit, "he" },
177 { "integrate", cmd_integrate, usage_integrate,"ig" },
178 { "merge", cmd_merge, usage_merge, "mg" },
179 { "stage", cmd_stage, usage_stage, "sg" },
180 { "unstage", cmd_unstage, usage_unstage, "ug" },
181 { "cat", cmd_cat, usage_cat, "" },
182 { "info", cmd_info, usage_info, "" },
186 list_commands(FILE *fp)
190 fprintf(fp, "commands:");
191 for (i = 0; i < nitems(got_commands); i++) {
192 const struct got_cmd *cmd = &got_commands[i];
193 fprintf(fp, " %s", cmd->cmd_name);
199 option_conflict(char a, char b)
201 errx(1, "-%c and -%c options are mutually exclusive", a, b);
205 main(int argc, char *argv[])
207 const struct got_cmd *cmd;
210 int hflag = 0, Vflag = 0;
211 static const struct option longopts[] = {
212 { "version", no_argument, NULL, 'V' },
216 setlocale(LC_CTYPE, "");
218 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
238 got_version_print_str();
243 usage(hflag, hflag ? 0 : 1);
245 signal(SIGINT, catch_sigint);
246 signal(SIGPIPE, catch_sigpipe);
248 for (i = 0; i < nitems(got_commands); i++) {
249 const struct got_error *error;
251 cmd = &got_commands[i];
253 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
254 strcmp(cmd->cmd_alias, argv[0]) != 0)
260 error = cmd->cmd_main(argc, argv);
261 if (error && error->code != GOT_ERR_CANCELLED &&
262 error->code != GOT_ERR_PRIVSEP_EXIT &&
263 !(sigpipe_received &&
264 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
266 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
267 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
274 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
275 list_commands(stderr);
280 usage(int hflag, int status)
282 FILE *fp = (status == 0) ? stdout : stderr;
284 fprintf(fp, "usage: %s [-hV] command [arg ...]\n",
291 static const struct got_error *
292 get_editor(char **abspath)
294 const struct got_error *err = NULL;
299 editor = getenv("VISUAL");
301 editor = getenv("EDITOR");
304 err = got_path_find_prog(abspath, editor);
309 if (*abspath == NULL) {
310 *abspath = strdup("/bin/ed");
311 if (*abspath == NULL)
312 return got_error_from_errno("strdup");
318 static const struct got_error *
319 apply_unveil(const char *repo_path, int repo_read_only,
320 const char *worktree_path)
322 const struct got_error *err;
325 if (unveil("gmon.out", "rwc") != 0)
326 return got_error_from_errno2("unveil", "gmon.out");
328 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
329 return got_error_from_errno2("unveil", repo_path);
331 if (worktree_path && unveil(worktree_path, "rwc") != 0)
332 return got_error_from_errno2("unveil", worktree_path);
334 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
335 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
337 err = got_privsep_unveil_exec_helpers();
341 if (unveil(NULL, NULL) != 0)
342 return got_error_from_errno("unveil");
350 fprintf(stderr, "usage: %s import [-b branch] [-I pattern] [-m message] "
351 "[-r repository-path] directory\n", getprogname());
356 spawn_editor(const char *editor, const char *file)
359 sig_t sighup, sigint, sigquit;
362 sighup = signal(SIGHUP, SIG_IGN);
363 sigint = signal(SIGINT, SIG_IGN);
364 sigquit = signal(SIGQUIT, SIG_IGN);
366 switch (pid = fork()) {
370 execl(editor, editor, file, (char *)NULL);
374 while (waitpid(pid, &st, 0) == -1)
379 (void)signal(SIGHUP, sighup);
380 (void)signal(SIGINT, sigint);
381 (void)signal(SIGQUIT, sigquit);
383 if (!WIFEXITED(st)) {
388 return WEXITSTATUS(st);
391 static const struct got_error *
392 read_logmsg(char **logmsg, size_t *len, FILE *fp, size_t filesize,
395 const struct got_error *err = NULL;
402 if (fseeko(fp, 0L, SEEK_SET) == -1)
403 return got_error_from_errno("fseeko");
405 *logmsg = malloc(filesize + 1);
407 return got_error_from_errno("malloc");
410 while (getline(&line, &linesize, fp) != -1) {
411 if ((strip_comments && line[0] == '#') ||
412 (*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;
448 size_t len, logmsg_len;
449 char *initial_content_stripped = NULL, *buf = NULL, *s;
453 if (stat(logmsg_path, &st) == -1)
454 return got_error_from_errno2("stat", logmsg_path);
456 if (spawn_editor(editor, logmsg_path) == -1)
457 return got_error_from_errno("failed spawning editor");
459 if (stat(logmsg_path, &st2) == -1)
460 return got_error_from_errno("stat");
462 if (require_modification &&
463 st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
464 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
465 "no changes made to commit message, aborting");
468 * Set up a stripped version of the initial content without comments
469 * and blank lines. We need this in order to check if the message
470 * has in fact been edited.
472 initial_content_stripped = malloc(initial_content_len + 1);
473 if (initial_content_stripped == NULL)
474 return got_error_from_errno("malloc");
475 initial_content_stripped[0] = '\0';
477 buf = strdup(initial_content);
479 err = got_error_from_errno("strdup");
484 while ((line = strsep(&s, "\n")) != NULL) {
485 if (len == 0 && line[0] == '\n')
486 continue; /* remove leading empty lines */
487 len = strlcat(initial_content_stripped, line,
488 initial_content_len + 1);
489 if (len >= initial_content_len + 1) {
490 err = got_error(GOT_ERR_NO_SPACE);
494 while (len > 0 && initial_content_stripped[len - 1] == '\n') {
495 initial_content_stripped[len - 1] = '\0';
499 fp = fopen(logmsg_path, "re");
501 err = got_error_from_errno("fopen");
506 * Check whether the log message was modified.
507 * Editing or removal of comments does count as a modifcation to
508 * support reuse of existing log messages during cherrypick/backout.
510 err = read_logmsg(logmsg, &logmsg_len, fp, st2.st_size, 0);
513 if (require_modification &&
514 strcmp(*logmsg, initial_content_stripped) == 0)
515 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
516 "no changes made to commit message, aborting");
518 /* Read log message again, stripping comments this time around. */
520 err = read_logmsg(logmsg, &logmsg_len, fp, st2.st_size, 1);
523 if (logmsg_len == 0) {
524 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
525 "commit message cannot be empty, aborting");
529 free(initial_content_stripped);
531 if (fp && fclose(fp) == EOF && err == NULL)
532 err = got_error_from_errno("fclose");
540 static const struct got_error *
541 collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
542 const char *path_dir, const char *branch_name)
544 char *initial_content = NULL;
545 const struct got_error *err = NULL;
546 int initial_content_len;
549 initial_content_len = asprintf(&initial_content,
550 "\n# %s to be imported to branch %s\n", path_dir,
552 if (initial_content_len == -1)
553 return got_error_from_errno("asprintf");
555 err = got_opentemp_named_fd(logmsg_path, &fd,
556 GOT_TMPDIR_STR "/got-importmsg", "");
560 if (write(fd, initial_content, initial_content_len) == -1) {
561 err = got_error_from_errno2("write", *logmsg_path);
565 err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content,
566 initial_content_len, 1);
568 if (fd != -1 && close(fd) == -1 && err == NULL)
569 err = got_error_from_errno2("close", *logmsg_path);
570 free(initial_content);
578 static const struct got_error *
579 import_progress(void *arg, const char *path)
581 printf("A %s\n", path);
585 static const struct got_error *
586 valid_author(const char *author)
588 const char *email = author;
591 * Git' expects the author (or committer) to be in the form
592 * "name <email>", which are mostly free form (see the
593 * "committer" description in git-fast-import(1)). We're only
594 * doing this to avoid git's object parser breaking on commits
598 while (*author && *author != '\n' && *author != '<' && *author != '>')
600 if (author != email && *author == '<' && *(author - 1) != ' ')
601 return got_error_fmt(GOT_ERR_COMMIT_BAD_AUTHOR, "%s: space "
602 "between author name and email required", email);
603 if (*author++ != '<')
604 return got_error_fmt(GOT_ERR_COMMIT_NO_EMAIL, "%s", email);
605 while (*author && *author != '\n' && *author != '<' && *author != '>')
607 if (strcmp(author, ">") != 0)
608 return got_error_fmt(GOT_ERR_COMMIT_NO_EMAIL, "%s", email);
612 static const struct got_error *
613 get_author(char **author, struct got_repository *repo,
614 struct got_worktree *worktree)
616 const struct got_error *err = NULL;
617 const char *got_author = NULL, *name, *email;
618 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
623 worktree_conf = got_worktree_get_gotconfig(worktree);
624 repo_conf = got_repo_get_gotconfig(repo);
627 * Priority of potential author information sources, from most
628 * significant to least significant:
629 * 1) work tree's .got/got.conf file
630 * 2) repository's got.conf file
631 * 3) repository's git config file
632 * 4) environment variables
633 * 5) global git config files (in user's home directory or /etc)
637 got_author = got_gotconfig_get_author(worktree_conf);
638 if (got_author == NULL)
639 got_author = got_gotconfig_get_author(repo_conf);
640 if (got_author == NULL) {
641 name = got_repo_get_gitconfig_author_name(repo);
642 email = got_repo_get_gitconfig_author_email(repo);
644 if (asprintf(author, "%s <%s>", name, email) == -1)
645 return got_error_from_errno("asprintf");
649 got_author = getenv("GOT_AUTHOR");
650 if (got_author == NULL) {
651 name = got_repo_get_global_gitconfig_author_name(repo);
652 email = got_repo_get_global_gitconfig_author_email(
655 if (asprintf(author, "%s <%s>", name, email)
657 return got_error_from_errno("asprintf");
660 /* TODO: Look up user in password database? */
661 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
665 *author = strdup(got_author);
667 return got_error_from_errno("strdup");
669 err = valid_author(*author);
677 static const struct got_error *
678 get_allowed_signers(char **allowed_signers, struct got_repository *repo,
679 struct got_worktree *worktree)
681 const char *got_allowed_signers = NULL;
682 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
684 *allowed_signers = NULL;
687 worktree_conf = got_worktree_get_gotconfig(worktree);
688 repo_conf = got_repo_get_gotconfig(repo);
691 * Priority of potential author information sources, from most
692 * significant to least significant:
693 * 1) work tree's .got/got.conf file
694 * 2) repository's got.conf file
698 got_allowed_signers = got_gotconfig_get_allowed_signers_file(
700 if (got_allowed_signers == NULL)
701 got_allowed_signers = got_gotconfig_get_allowed_signers_file(
704 if (got_allowed_signers) {
705 *allowed_signers = strdup(got_allowed_signers);
706 if (*allowed_signers == NULL)
707 return got_error_from_errno("strdup");
712 static const struct got_error *
713 get_revoked_signers(char **revoked_signers, struct got_repository *repo,
714 struct got_worktree *worktree)
716 const char *got_revoked_signers = NULL;
717 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
719 *revoked_signers = NULL;
722 worktree_conf = got_worktree_get_gotconfig(worktree);
723 repo_conf = got_repo_get_gotconfig(repo);
726 * Priority of potential author information sources, from most
727 * significant to least significant:
728 * 1) work tree's .got/got.conf file
729 * 2) repository's got.conf file
733 got_revoked_signers = got_gotconfig_get_revoked_signers_file(
735 if (got_revoked_signers == NULL)
736 got_revoked_signers = got_gotconfig_get_revoked_signers_file(
739 if (got_revoked_signers) {
740 *revoked_signers = strdup(got_revoked_signers);
741 if (*revoked_signers == NULL)
742 return got_error_from_errno("strdup");
748 get_signer_id(struct got_repository *repo, struct got_worktree *worktree)
750 const char *got_signer_id = NULL;
751 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
754 worktree_conf = got_worktree_get_gotconfig(worktree);
755 repo_conf = got_repo_get_gotconfig(repo);
758 * Priority of potential author information sources, from most
759 * significant to least significant:
760 * 1) work tree's .got/got.conf file
761 * 2) repository's got.conf file
765 got_signer_id = got_gotconfig_get_signer_id(worktree_conf);
766 if (got_signer_id == NULL)
767 got_signer_id = got_gotconfig_get_signer_id(repo_conf);
769 return got_signer_id;
772 static const struct got_error *
773 get_gitconfig_path(char **gitconfig_path)
775 const char *homedir = getenv("HOME");
777 *gitconfig_path = NULL;
779 if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
780 return got_error_from_errno("asprintf");
786 static const struct got_error *
787 cmd_import(int argc, char *argv[])
789 const struct got_error *error = NULL;
790 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
791 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
792 const char *branch_name = NULL;
793 char *id_str = NULL, *logmsg_path = NULL;
794 char refname[PATH_MAX] = "refs/heads/";
795 struct got_repository *repo = NULL;
796 struct got_reference *branch_ref = NULL, *head_ref = NULL;
797 struct got_object_id *new_commit_id = NULL;
799 struct got_pathlist_head ignores;
800 struct got_pathlist_entry *pe;
801 int preserve_logmsg = 0;
802 int *pack_fds = NULL;
804 TAILQ_INIT(&ignores);
806 while ((ch = getopt(argc, argv, "b:I:m:r:")) != -1) {
809 branch_name = optarg;
812 if (optarg[0] == '\0')
814 error = got_pathlist_insert(&pe, &ignores, optarg,
820 logmsg = strdup(optarg);
821 if (logmsg == NULL) {
822 error = got_error_from_errno("strdup");
827 repo_path = realpath(optarg, NULL);
828 if (repo_path == NULL) {
829 error = got_error_from_errno2("realpath",
844 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
852 if (repo_path == NULL) {
853 repo_path = getcwd(NULL, 0);
854 if (repo_path == NULL)
855 return got_error_from_errno("getcwd");
857 got_path_strip_trailing_slashes(repo_path);
858 error = get_gitconfig_path(&gitconfig_path);
861 error = got_repo_pack_fds_open(&pack_fds);
864 error = got_repo_open(&repo, repo_path, gitconfig_path, pack_fds);
868 error = get_author(&author, repo, NULL);
873 * Don't let the user create a branch name with a leading '-'.
874 * While technically a valid reference name, this case is usually
875 * an unintended typo.
877 if (branch_name && branch_name[0] == '-')
878 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
880 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
881 if (error && error->code != GOT_ERR_NOT_REF)
885 n = strlcat(refname, branch_name, sizeof(refname));
886 else if (head_ref && got_ref_is_symbolic(head_ref))
887 n = strlcpy(refname, got_ref_get_symref_target(head_ref),
890 n = strlcat(refname, "main", sizeof(refname));
891 if (n >= sizeof(refname)) {
892 error = got_error(GOT_ERR_NO_SPACE);
896 error = got_ref_open(&branch_ref, repo, refname, 0);
898 if (error->code != GOT_ERR_NOT_REF)
901 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
902 "import target branch already exists");
906 path_dir = realpath(argv[0], NULL);
907 if (path_dir == NULL) {
908 error = got_error_from_errno2("realpath", argv[0]);
911 got_path_strip_trailing_slashes(path_dir);
914 * unveil(2) traverses exec(2); if an editor is used we have
915 * to apply unveil after the log message has been written.
917 if (logmsg == NULL || strlen(logmsg) == 0) {
918 error = get_editor(&editor);
922 error = collect_import_msg(&logmsg, &logmsg_path, editor,
925 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
932 if (unveil(path_dir, "r") != 0) {
933 error = got_error_from_errno2("unveil", path_dir);
939 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
946 error = got_repo_import(&new_commit_id, path_dir, logmsg,
947 author, &ignores, repo, import_progress, NULL);
954 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
961 error = got_ref_write(branch_ref, repo);
968 error = got_object_id_str(&id_str, new_commit_id);
975 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
977 if (error->code != GOT_ERR_NOT_REF) {
983 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
991 error = got_ref_write(head_ref, repo);
999 printf("Created branch %s with commit %s\n",
1000 got_ref_get_name(branch_ref), id_str);
1003 const struct got_error *pack_err =
1004 got_repo_pack_fds_close(pack_fds);
1008 if (preserve_logmsg) {
1009 fprintf(stderr, "%s: log message preserved in %s\n",
1010 getprogname(), logmsg_path);
1011 } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
1012 error = got_error_from_errno2("unlink", logmsg_path);
1017 free(new_commit_id);
1020 free(gitconfig_path);
1022 got_ref_close(branch_ref);
1024 got_ref_close(head_ref);
1031 fprintf(stderr, "usage: %s clone [-almqv] [-b branch] [-R reference] "
1032 "repository-URL [directory]\n", getprogname());
1036 struct got_fetch_progress_arg {
1037 char last_scaled_size[FMT_SCALED_STRSIZE];
1039 int last_p_resolved;
1042 struct got_repository *repo;
1045 int configs_created;
1047 struct got_pathlist_head *symrefs;
1048 struct got_pathlist_head *wanted_branches;
1049 struct got_pathlist_head *wanted_refs;
1053 const char *remote_repo_path;
1054 const char *git_url;
1055 int fetch_all_branches;
1056 int mirror_references;
1060 /* XXX forward declaration */
1061 static const struct got_error *
1062 create_config_files(const char *proto, const char *host, const char *port,
1063 const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1064 int mirror_references, struct got_pathlist_head *symrefs,
1065 struct got_pathlist_head *wanted_branches,
1066 struct got_pathlist_head *wanted_refs, struct got_repository *repo);
1068 static const struct got_error *
1069 fetch_progress(void *arg, const char *message, off_t packfile_size,
1070 int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
1072 const struct got_error *err = NULL;
1073 struct got_fetch_progress_arg *a = arg;
1074 char scaled_size[FMT_SCALED_STRSIZE];
1075 int p_indexed, p_resolved;
1076 int print_size = 0, print_indexed = 0, print_resolved = 0;
1079 * In order to allow a failed clone to be resumed with 'got fetch'
1080 * we try to create configuration files as soon as possible.
1081 * Once the server has sent information about its default branch
1082 * we have all required information.
1084 if (a->create_configs && !a->configs_created &&
1085 !TAILQ_EMPTY(a->config_info.symrefs)) {
1086 err = create_config_files(a->config_info.proto,
1087 a->config_info.host, a->config_info.port,
1088 a->config_info.remote_repo_path,
1089 a->config_info.git_url,
1090 a->config_info.fetch_all_branches,
1091 a->config_info.mirror_references,
1092 a->config_info.symrefs,
1093 a->config_info.wanted_branches,
1094 a->config_info.wanted_refs, a->repo);
1097 a->configs_created = 1;
1100 if (a->verbosity < 0)
1103 if (message && message[0] != '\0') {
1104 printf("\rserver: %s", message);
1109 if (packfile_size > 0 || nobj_indexed > 0) {
1110 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
1111 (a->last_scaled_size[0] == '\0' ||
1112 strcmp(scaled_size, a->last_scaled_size)) != 0) {
1114 if (strlcpy(a->last_scaled_size, scaled_size,
1115 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
1116 return got_error(GOT_ERR_NO_SPACE);
1118 if (nobj_indexed > 0) {
1119 p_indexed = (nobj_indexed * 100) / nobj_total;
1120 if (p_indexed != a->last_p_indexed) {
1121 a->last_p_indexed = p_indexed;
1126 if (nobj_resolved > 0) {
1127 p_resolved = (nobj_resolved * 100) /
1128 (nobj_total - nobj_loose);
1129 if (p_resolved != a->last_p_resolved) {
1130 a->last_p_resolved = p_resolved;
1138 if (print_size || print_indexed || print_resolved)
1141 printf("%*s fetched", FMT_SCALED_STRSIZE - 2, scaled_size);
1143 printf("; indexing %d%%", p_indexed);
1145 printf("; resolving deltas %d%%", p_resolved);
1146 if (print_size || print_indexed || print_resolved)
1152 static const struct got_error *
1153 create_symref(const char *refname, struct got_reference *target_ref,
1154 int verbosity, struct got_repository *repo)
1156 const struct got_error *err;
1157 struct got_reference *head_symref;
1159 err = got_ref_alloc_symref(&head_symref, refname, target_ref);
1163 err = got_ref_write(head_symref, repo);
1164 if (err == NULL && verbosity > 0) {
1165 printf("Created reference %s: %s\n", GOT_REF_HEAD,
1166 got_ref_get_name(target_ref));
1168 got_ref_close(head_symref);
1172 static const struct got_error *
1173 list_remote_refs(struct got_pathlist_head *symrefs,
1174 struct got_pathlist_head *refs)
1176 const struct got_error *err;
1177 struct got_pathlist_entry *pe;
1179 TAILQ_FOREACH(pe, symrefs, entry) {
1180 const char *refname = pe->path;
1181 const char *targetref = pe->data;
1183 printf("%s: %s\n", refname, targetref);
1186 TAILQ_FOREACH(pe, refs, entry) {
1187 const char *refname = pe->path;
1188 struct got_object_id *id = pe->data;
1191 err = got_object_id_str(&id_str, id);
1194 printf("%s: %s\n", refname, id_str);
1201 static const struct got_error *
1202 create_ref(const char *refname, struct got_object_id *id,
1203 int verbosity, struct got_repository *repo)
1205 const struct got_error *err = NULL;
1206 struct got_reference *ref;
1209 err = got_object_id_str(&id_str, id);
1213 err = got_ref_alloc(&ref, refname, id);
1217 err = got_ref_write(ref, repo);
1220 if (err == NULL && verbosity >= 0)
1221 printf("Created reference %s: %s\n", refname, id_str);
1228 match_wanted_ref(const char *refname, const char *wanted_ref)
1230 if (strncmp(refname, "refs/", 5) != 0)
1235 * Prevent fetching of references that won't make any
1236 * sense outside of the remote repository's context.
1238 if (strncmp(refname, "got/", 4) == 0)
1240 if (strncmp(refname, "remotes/", 8) == 0)
1243 if (strncmp(wanted_ref, "refs/", 5) == 0)
1246 /* Allow prefix match. */
1247 if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
1250 /* Allow exact match. */
1251 return (strcmp(refname, wanted_ref) == 0);
1255 is_wanted_ref(struct got_pathlist_head *wanted_refs, const char *refname)
1257 struct got_pathlist_entry *pe;
1259 TAILQ_FOREACH(pe, wanted_refs, entry) {
1260 if (match_wanted_ref(refname, pe->path))
1267 static const struct got_error *
1268 create_wanted_ref(const char *refname, struct got_object_id *id,
1269 const char *remote_repo_name, int verbosity, struct got_repository *repo)
1271 const struct got_error *err;
1272 char *remote_refname;
1274 if (strncmp("refs/", refname, 5) == 0)
1277 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1278 remote_repo_name, refname) == -1)
1279 return got_error_from_errno("asprintf");
1281 err = create_ref(remote_refname, id, verbosity, repo);
1282 free(remote_refname);
1286 static const struct got_error *
1287 create_gotconfig(const char *proto, const char *host, const char *port,
1288 const char *remote_repo_path, const char *default_branch,
1289 int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1290 struct got_pathlist_head *wanted_refs, int mirror_references,
1291 struct got_repository *repo)
1293 const struct got_error *err = NULL;
1294 char *gotconfig_path = NULL;
1295 char *gotconfig = NULL;
1296 FILE *gotconfig_file = NULL;
1297 const char *branchname = NULL;
1298 char *branches = NULL, *refs = NULL;
1301 if (!fetch_all_branches && !TAILQ_EMPTY(wanted_branches)) {
1302 struct got_pathlist_entry *pe;
1303 TAILQ_FOREACH(pe, wanted_branches, entry) {
1305 branchname = pe->path;
1306 if (strncmp(branchname, "refs/heads/", 11) == 0)
1308 if (asprintf(&s, "%s\"%s\" ",
1309 branches ? branches : "", branchname) == -1) {
1310 err = got_error_from_errno("asprintf");
1316 } else if (!fetch_all_branches && default_branch) {
1317 branchname = default_branch;
1318 if (strncmp(branchname, "refs/heads/", 11) == 0)
1320 if (asprintf(&branches, "\"%s\" ", branchname) == -1) {
1321 err = got_error_from_errno("asprintf");
1325 if (!TAILQ_EMPTY(wanted_refs)) {
1326 struct got_pathlist_entry *pe;
1327 TAILQ_FOREACH(pe, wanted_refs, entry) {
1329 const char *refname = pe->path;
1330 if (strncmp(refname, "refs/", 5) == 0)
1332 if (asprintf(&s, "%s\"%s\" ",
1333 refs ? refs : "", refname) == -1) {
1334 err = got_error_from_errno("asprintf");
1342 /* Create got.conf(5). */
1343 gotconfig_path = got_repo_get_path_gotconfig(repo);
1344 if (gotconfig_path == NULL) {
1345 err = got_error_from_errno("got_repo_get_path_gotconfig");
1348 gotconfig_file = fopen(gotconfig_path, "ae");
1349 if (gotconfig_file == NULL) {
1350 err = got_error_from_errno2("fopen", gotconfig_path);
1353 if (asprintf(&gotconfig,
1358 "\trepository \"%s\"\n"
1364 GOT_FETCH_DEFAULT_REMOTE_NAME, host, proto,
1365 port ? "\tport " : "", port ? port : "", port ? "\n" : "",
1366 remote_repo_path, branches ? "\tbranch { " : "",
1367 branches ? branches : "", branches ? "}\n" : "",
1368 refs ? "\treference { " : "", refs ? refs : "", refs ? "}\n" : "",
1369 mirror_references ? "\tmirror_references yes\n" : "",
1370 fetch_all_branches ? "\tfetch_all_branches yes\n" : "") == -1) {
1371 err = got_error_from_errno("asprintf");
1374 n = fwrite(gotconfig, 1, strlen(gotconfig), gotconfig_file);
1375 if (n != strlen(gotconfig)) {
1376 err = got_ferror(gotconfig_file, GOT_ERR_IO);
1381 if (gotconfig_file && fclose(gotconfig_file) == EOF && err == NULL)
1382 err = got_error_from_errno2("fclose", gotconfig_path);
1383 free(gotconfig_path);
1388 static const struct got_error *
1389 create_gitconfig(const char *git_url, const char *default_branch,
1390 int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1391 struct got_pathlist_head *wanted_refs, int mirror_references,
1392 struct got_repository *repo)
1394 const struct got_error *err = NULL;
1395 char *gitconfig_path = NULL;
1396 char *gitconfig = NULL;
1397 FILE *gitconfig_file = NULL;
1398 char *branches = NULL, *refs = NULL;
1399 const char *branchname;
1402 /* Create a config file Git can understand. */
1403 gitconfig_path = got_repo_get_path_gitconfig(repo);
1404 if (gitconfig_path == NULL) {
1405 err = got_error_from_errno("got_repo_get_path_gitconfig");
1408 gitconfig_file = fopen(gitconfig_path, "ae");
1409 if (gitconfig_file == NULL) {
1410 err = got_error_from_errno2("fopen", gitconfig_path);
1413 if (fetch_all_branches) {
1414 if (mirror_references) {
1415 if (asprintf(&branches,
1416 "\tfetch = refs/heads/*:refs/heads/*\n") == -1) {
1417 err = got_error_from_errno("asprintf");
1420 } else if (asprintf(&branches,
1421 "\tfetch = refs/heads/*:refs/remotes/%s/*\n",
1422 GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1423 err = got_error_from_errno("asprintf");
1426 } else if (!TAILQ_EMPTY(wanted_branches)) {
1427 struct got_pathlist_entry *pe;
1428 TAILQ_FOREACH(pe, wanted_branches, entry) {
1430 branchname = pe->path;
1431 if (strncmp(branchname, "refs/heads/", 11) == 0)
1433 if (mirror_references) {
1435 "%s\tfetch = refs/heads/%s:refs/heads/%s\n",
1436 branches ? branches : "",
1437 branchname, branchname) == -1) {
1438 err = got_error_from_errno("asprintf");
1441 } else if (asprintf(&s,
1442 "%s\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1443 branches ? branches : "",
1444 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1445 branchname) == -1) {
1446 err = got_error_from_errno("asprintf");
1454 * If the server specified a default branch, use just that one.
1455 * Otherwise fall back to fetching all branches on next fetch.
1457 if (default_branch) {
1458 branchname = default_branch;
1459 if (strncmp(branchname, "refs/heads/", 11) == 0)
1462 branchname = "*"; /* fall back to all branches */
1463 if (mirror_references) {
1464 if (asprintf(&branches,
1465 "\tfetch = refs/heads/%s:refs/heads/%s\n",
1466 branchname, branchname) == -1) {
1467 err = got_error_from_errno("asprintf");
1470 } else if (asprintf(&branches,
1471 "\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1472 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1473 branchname) == -1) {
1474 err = got_error_from_errno("asprintf");
1478 if (!TAILQ_EMPTY(wanted_refs)) {
1479 struct got_pathlist_entry *pe;
1480 TAILQ_FOREACH(pe, wanted_refs, entry) {
1482 const char *refname = pe->path;
1483 if (strncmp(refname, "refs/", 5) == 0)
1485 if (mirror_references) {
1487 "%s\tfetch = refs/%s:refs/%s\n",
1488 refs ? refs : "", refname, refname) == -1) {
1489 err = got_error_from_errno("asprintf");
1492 } else if (asprintf(&s,
1493 "%s\tfetch = refs/%s:refs/remotes/%s/%s\n",
1495 refname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1497 err = got_error_from_errno("asprintf");
1505 if (asprintf(&gitconfig,
1510 "\tfetch = refs/tags/*:refs/tags/*\n",
1511 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url, branches ? branches : "",
1512 refs ? refs : "") == -1) {
1513 err = got_error_from_errno("asprintf");
1516 n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1517 if (n != strlen(gitconfig)) {
1518 err = got_ferror(gitconfig_file, GOT_ERR_IO);
1522 if (gitconfig_file && fclose(gitconfig_file) == EOF && err == NULL)
1523 err = got_error_from_errno2("fclose", gitconfig_path);
1524 free(gitconfig_path);
1529 static const struct got_error *
1530 create_config_files(const char *proto, const char *host, const char *port,
1531 const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1532 int mirror_references, struct got_pathlist_head *symrefs,
1533 struct got_pathlist_head *wanted_branches,
1534 struct got_pathlist_head *wanted_refs, struct got_repository *repo)
1536 const struct got_error *err = NULL;
1537 const char *default_branch = NULL;
1538 struct got_pathlist_entry *pe;
1541 * If we asked for a set of wanted branches then use the first
1544 if (!TAILQ_EMPTY(wanted_branches)) {
1545 pe = TAILQ_FIRST(wanted_branches);
1546 default_branch = pe->path;
1548 /* First HEAD ref listed by server is the default branch. */
1549 TAILQ_FOREACH(pe, symrefs, entry) {
1550 const char *refname = pe->path;
1551 const char *target = pe->data;
1553 if (strcmp(refname, GOT_REF_HEAD) != 0)
1556 default_branch = target;
1561 /* Create got.conf(5). */
1562 err = create_gotconfig(proto, host, port, remote_repo_path,
1563 default_branch, fetch_all_branches, wanted_branches,
1564 wanted_refs, mirror_references, repo);
1568 /* Create a config file Git can understand. */
1569 return create_gitconfig(git_url, default_branch, fetch_all_branches,
1570 wanted_branches, wanted_refs, mirror_references, repo);
1573 static const struct got_error *
1574 cmd_clone(int argc, char *argv[])
1576 const struct got_error *error = NULL;
1577 const char *uri, *dirname;
1578 char *proto, *host, *port, *repo_name, *server_path;
1579 char *default_destdir = NULL, *id_str = NULL;
1580 const char *repo_path;
1581 struct got_repository *repo = NULL;
1582 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1583 struct got_pathlist_entry *pe;
1584 struct got_object_id *pack_hash = NULL;
1585 int ch, fetchfd = -1, fetchstatus;
1586 pid_t fetchpid = -1;
1587 struct got_fetch_progress_arg fpa;
1588 char *git_url = NULL;
1589 int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
1590 int list_refs_only = 0;
1591 int *pack_fds = NULL;
1594 TAILQ_INIT(&symrefs);
1595 TAILQ_INIT(&wanted_branches);
1596 TAILQ_INIT(&wanted_refs);
1598 while ((ch = getopt(argc, argv, "ab:lmqR:v")) != -1) {
1601 fetch_all_branches = 1;
1604 error = got_pathlist_append(&wanted_branches,
1613 mirror_references = 1;
1619 error = got_pathlist_append(&wanted_refs,
1627 else if (verbosity < 3)
1638 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1639 option_conflict('a', 'b');
1640 if (list_refs_only) {
1641 if (!TAILQ_EMPTY(&wanted_branches))
1642 option_conflict('l', 'b');
1643 if (fetch_all_branches)
1644 option_conflict('l', 'a');
1645 if (mirror_references)
1646 option_conflict('l', 'm');
1647 if (!TAILQ_EMPTY(&wanted_refs))
1648 option_conflict('l', 'R');
1660 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
1665 if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1666 host, port ? ":" : "", port ? port : "",
1667 server_path[0] != '/' ? "/" : "", server_path) == -1) {
1668 error = got_error_from_errno("asprintf");
1672 if (strcmp(proto, "git") == 0) {
1674 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1675 "sendfd dns inet unveil", NULL) == -1)
1678 } else if (strcmp(proto, "git+ssh") == 0 ||
1679 strcmp(proto, "ssh") == 0) {
1681 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1682 "sendfd unveil", NULL) == -1)
1685 } else if (strcmp(proto, "http") == 0 ||
1686 strcmp(proto, "git+http") == 0) {
1687 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1690 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1693 if (dirname == NULL) {
1694 if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1695 error = got_error_from_errno("asprintf");
1698 repo_path = default_destdir;
1700 repo_path = dirname;
1702 if (!list_refs_only) {
1703 error = got_path_mkdir(repo_path);
1705 (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1706 !(error->code == GOT_ERR_ERRNO && errno == EEXIST)))
1708 if (!got_path_dir_is_empty(repo_path)) {
1709 error = got_error_path(repo_path,
1710 GOT_ERR_DIR_NOT_EMPTY);
1715 error = got_dial_apply_unveil(proto);
1719 error = apply_unveil(repo_path, 0, NULL);
1724 printf("Connecting to %s\n", git_url);
1726 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1727 server_path, verbosity);
1731 if (!list_refs_only) {
1732 error = got_repo_init(repo_path, NULL);
1735 error = got_repo_pack_fds_open(&pack_fds);
1738 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
1743 fpa.last_scaled_size[0] = '\0';
1744 fpa.last_p_indexed = -1;
1745 fpa.last_p_resolved = -1;
1746 fpa.verbosity = verbosity;
1747 fpa.create_configs = 1;
1748 fpa.configs_created = 0;
1750 fpa.config_info.symrefs = &symrefs;
1751 fpa.config_info.wanted_branches = &wanted_branches;
1752 fpa.config_info.wanted_refs = &wanted_refs;
1753 fpa.config_info.proto = proto;
1754 fpa.config_info.host = host;
1755 fpa.config_info.port = port;
1756 fpa.config_info.remote_repo_path = server_path;
1757 fpa.config_info.git_url = git_url;
1758 fpa.config_info.fetch_all_branches = fetch_all_branches;
1759 fpa.config_info.mirror_references = mirror_references;
1760 error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1761 GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1762 fetch_all_branches, &wanted_branches, &wanted_refs,
1763 list_refs_only, verbosity, fetchfd, repo, NULL,
1764 fetch_progress, &fpa);
1768 if (list_refs_only) {
1769 error = list_remote_refs(&symrefs, &refs);
1773 if (pack_hash == NULL) {
1774 error = got_error_fmt(GOT_ERR_FETCH_FAILED, "%s",
1775 "server sent an empty pack file");
1778 error = got_object_id_str(&id_str, pack_hash);
1782 printf("\nFetched %s.pack\n", id_str);
1785 /* Set up references provided with the pack file. */
1786 TAILQ_FOREACH(pe, &refs, entry) {
1787 const char *refname = pe->path;
1788 struct got_object_id *id = pe->data;
1789 char *remote_refname;
1791 if (is_wanted_ref(&wanted_refs, refname) &&
1792 !mirror_references) {
1793 error = create_wanted_ref(refname, id,
1794 GOT_FETCH_DEFAULT_REMOTE_NAME,
1795 verbosity - 1, repo);
1801 error = create_ref(refname, id, verbosity - 1, repo);
1805 if (mirror_references)
1808 if (strncmp("refs/heads/", refname, 11) != 0)
1811 if (asprintf(&remote_refname,
1812 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1813 refname + 11) == -1) {
1814 error = got_error_from_errno("asprintf");
1817 error = create_ref(remote_refname, id, verbosity - 1, repo);
1818 free(remote_refname);
1823 /* Set the HEAD reference if the server provided one. */
1824 TAILQ_FOREACH(pe, &symrefs, entry) {
1825 struct got_reference *target_ref;
1826 const char *refname = pe->path;
1827 const char *target = pe->data;
1828 char *remote_refname = NULL, *remote_target = NULL;
1830 if (strcmp(refname, GOT_REF_HEAD) != 0)
1833 error = got_ref_open(&target_ref, repo, target, 0);
1835 if (error->code == GOT_ERR_NOT_REF) {
1842 error = create_symref(refname, target_ref, verbosity, repo);
1843 got_ref_close(target_ref);
1847 if (mirror_references)
1850 if (strncmp("refs/heads/", target, 11) != 0)
1853 if (asprintf(&remote_refname,
1854 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1856 error = got_error_from_errno("asprintf");
1859 if (asprintf(&remote_target,
1860 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1861 target + 11) == -1) {
1862 error = got_error_from_errno("asprintf");
1863 free(remote_refname);
1866 error = got_ref_open(&target_ref, repo, remote_target, 0);
1868 free(remote_refname);
1869 free(remote_target);
1870 if (error->code == GOT_ERR_NOT_REF) {
1876 error = create_symref(remote_refname, target_ref,
1877 verbosity - 1, repo);
1878 free(remote_refname);
1879 free(remote_target);
1880 got_ref_close(target_ref);
1886 * We failed to set the HEAD reference. If we asked for
1887 * a set of wanted branches use the first of one of those
1888 * which could be fetched instead.
1890 TAILQ_FOREACH(pe, &wanted_branches, entry) {
1891 const char *target = pe->path;
1892 struct got_reference *target_ref;
1894 error = got_ref_open(&target_ref, repo, target, 0);
1896 if (error->code == GOT_ERR_NOT_REF) {
1903 error = create_symref(GOT_REF_HEAD, target_ref,
1905 got_ref_close(target_ref);
1911 if (!fpa.configs_created && pe != NULL) {
1912 error = create_config_files(fpa.config_info.proto,
1913 fpa.config_info.host, fpa.config_info.port,
1914 fpa.config_info.remote_repo_path,
1915 fpa.config_info.git_url,
1916 fpa.config_info.fetch_all_branches,
1917 fpa.config_info.mirror_references,
1918 fpa.config_info.symrefs,
1919 fpa.config_info.wanted_branches,
1920 fpa.config_info.wanted_refs, fpa.repo);
1927 printf("Created %s repository '%s'\n",
1928 mirror_references ? "mirrored" : "cloned", repo_path);
1931 const struct got_error *pack_err =
1932 got_repo_pack_fds_close(pack_fds);
1937 if (kill(fetchpid, SIGTERM) == -1)
1938 error = got_error_from_errno("kill");
1939 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1940 error = got_error_from_errno("waitpid");
1942 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1943 error = got_error_from_errno("close");
1945 const struct got_error *close_err = got_repo_close(repo);
1949 got_pathlist_free(&refs, GOT_PATHLIST_FREE_ALL);
1950 got_pathlist_free(&symrefs, GOT_PATHLIST_FREE_ALL);
1951 got_pathlist_free(&wanted_branches, GOT_PATHLIST_FREE_NONE);
1952 got_pathlist_free(&wanted_refs, GOT_PATHLIST_FREE_NONE);
1959 free(default_destdir);
1964 static const struct got_error *
1965 update_ref(struct got_reference *ref, struct got_object_id *new_id,
1966 int replace_tags, int verbosity, struct got_repository *repo)
1968 const struct got_error *err = NULL;
1969 char *new_id_str = NULL;
1970 struct got_object_id *old_id = NULL;
1972 err = got_object_id_str(&new_id_str, new_id);
1976 if (!replace_tags &&
1977 strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1978 err = got_ref_resolve(&old_id, repo, ref);
1981 if (got_object_id_cmp(old_id, new_id) == 0)
1983 if (verbosity >= 0) {
1984 printf("Rejecting update of existing tag %s: %s\n",
1985 got_ref_get_name(ref), new_id_str);
1990 if (got_ref_is_symbolic(ref)) {
1991 if (verbosity >= 0) {
1992 printf("Replacing reference %s: %s\n",
1993 got_ref_get_name(ref),
1994 got_ref_get_symref_target(ref));
1996 err = got_ref_change_symref_to_ref(ref, new_id);
1999 err = got_ref_write(ref, repo);
2003 err = got_ref_resolve(&old_id, repo, ref);
2006 if (got_object_id_cmp(old_id, new_id) == 0)
2009 err = got_ref_change_ref(ref, new_id);
2012 err = got_ref_write(ref, repo);
2018 printf("Updated %s: %s\n", got_ref_get_name(ref),
2026 static const struct got_error *
2027 update_symref(const char *refname, struct got_reference *target_ref,
2028 int verbosity, struct got_repository *repo)
2030 const struct got_error *err = NULL, *unlock_err;
2031 struct got_reference *symref;
2032 int symref_is_locked = 0;
2034 err = got_ref_open(&symref, repo, refname, 1);
2036 if (err->code != GOT_ERR_NOT_REF)
2038 err = got_ref_alloc_symref(&symref, refname, target_ref);
2042 err = got_ref_write(symref, repo);
2047 printf("Created reference %s: %s\n",
2048 got_ref_get_name(symref),
2049 got_ref_get_symref_target(symref));
2051 symref_is_locked = 1;
2053 if (strcmp(got_ref_get_symref_target(symref),
2054 got_ref_get_name(target_ref)) == 0)
2057 err = got_ref_change_symref(symref,
2058 got_ref_get_name(target_ref));
2062 err = got_ref_write(symref, repo);
2067 printf("Updated %s: %s\n", got_ref_get_name(symref),
2068 got_ref_get_symref_target(symref));
2072 if (symref_is_locked) {
2073 unlock_err = got_ref_unlock(symref);
2074 if (unlock_err && err == NULL)
2077 got_ref_close(symref);
2084 fprintf(stderr, "usage: %s fetch [-adlqtvX] [-b branch] "
2085 "[-R reference] [-r repository-path] [remote-repository]\n",
2090 static const struct got_error *
2091 delete_missing_ref(struct got_reference *ref,
2092 int verbosity, struct got_repository *repo)
2094 const struct got_error *err = NULL;
2095 struct got_object_id *id = NULL;
2096 char *id_str = NULL;
2098 if (got_ref_is_symbolic(ref)) {
2099 err = got_ref_delete(ref, repo);
2102 if (verbosity >= 0) {
2103 printf("Deleted %s: %s\n",
2104 got_ref_get_name(ref),
2105 got_ref_get_symref_target(ref));
2108 err = got_ref_resolve(&id, repo, ref);
2111 err = got_object_id_str(&id_str, id);
2115 err = got_ref_delete(ref, repo);
2118 if (verbosity >= 0) {
2119 printf("Deleted %s: %s\n",
2120 got_ref_get_name(ref), id_str);
2129 static const struct got_error *
2130 delete_missing_refs(struct got_pathlist_head *their_refs,
2131 struct got_pathlist_head *their_symrefs,
2132 const struct got_remote_repo *remote,
2133 int verbosity, struct got_repository *repo)
2135 const struct got_error *err = NULL, *unlock_err;
2136 struct got_reflist_head my_refs;
2137 struct got_reflist_entry *re;
2138 struct got_pathlist_entry *pe;
2139 char *remote_namespace = NULL;
2140 char *local_refname = NULL;
2142 TAILQ_INIT(&my_refs);
2144 if (asprintf(&remote_namespace, "refs/remotes/%s/", remote->name)
2146 return got_error_from_errno("asprintf");
2148 err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
2152 TAILQ_FOREACH(re, &my_refs, entry) {
2153 const char *refname = got_ref_get_name(re->ref);
2154 const char *their_refname;
2156 if (remote->mirror_references) {
2157 their_refname = refname;
2159 if (strncmp(refname, remote_namespace,
2160 strlen(remote_namespace)) == 0) {
2161 if (strcmp(refname + strlen(remote_namespace),
2164 if (asprintf(&local_refname, "refs/heads/%s",
2165 refname + strlen(remote_namespace)) == -1) {
2166 err = got_error_from_errno("asprintf");
2169 } else if (strncmp(refname, "refs/tags/", 10) != 0)
2172 their_refname = local_refname;
2175 TAILQ_FOREACH(pe, their_refs, entry) {
2176 if (strcmp(their_refname, pe->path) == 0)
2182 TAILQ_FOREACH(pe, their_symrefs, entry) {
2183 if (strcmp(their_refname, pe->path) == 0)
2189 err = delete_missing_ref(re->ref, verbosity, repo);
2193 if (local_refname) {
2194 struct got_reference *ref;
2195 err = got_ref_open(&ref, repo, local_refname, 1);
2197 if (err->code != GOT_ERR_NOT_REF)
2199 free(local_refname);
2200 local_refname = NULL;
2203 err = delete_missing_ref(ref, verbosity, repo);
2206 unlock_err = got_ref_unlock(ref);
2208 if (unlock_err && err == NULL) {
2213 free(local_refname);
2214 local_refname = NULL;
2218 got_ref_list_free(&my_refs);
2219 free(remote_namespace);
2220 free(local_refname);
2224 static const struct got_error *
2225 update_wanted_ref(const char *refname, struct got_object_id *id,
2226 const char *remote_repo_name, int verbosity, struct got_repository *repo)
2228 const struct got_error *err, *unlock_err;
2229 char *remote_refname;
2230 struct got_reference *ref;
2232 if (strncmp("refs/", refname, 5) == 0)
2235 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2236 remote_repo_name, refname) == -1)
2237 return got_error_from_errno("asprintf");
2239 err = got_ref_open(&ref, repo, remote_refname, 1);
2241 if (err->code != GOT_ERR_NOT_REF)
2243 err = create_ref(remote_refname, id, verbosity, repo);
2245 err = update_ref(ref, id, 0, verbosity, repo);
2246 unlock_err = got_ref_unlock(ref);
2247 if (unlock_err && err == NULL)
2252 free(remote_refname);
2256 static const struct got_error *
2257 delete_ref(struct got_repository *repo, struct got_reference *ref)
2259 const struct got_error *err = NULL;
2260 struct got_object_id *id = NULL;
2261 char *id_str = NULL;
2264 if (got_ref_is_symbolic(ref)) {
2265 target = got_ref_get_symref_target(ref);
2267 err = got_ref_resolve(&id, repo, ref);
2270 err = got_object_id_str(&id_str, id);
2276 err = got_ref_delete(ref, repo);
2280 printf("Deleted %s: %s\n", got_ref_get_name(ref), target);
2287 static const struct got_error *
2288 delete_refs_for_remote(struct got_repository *repo, const char *remote_name)
2290 const struct got_error *err = NULL;
2291 struct got_reflist_head refs;
2292 struct got_reflist_entry *re;
2297 if (asprintf(&prefix, "refs/remotes/%s", remote_name) == -1) {
2298 err = got_error_from_errno("asprintf");
2301 err = got_ref_list(&refs, repo, prefix, got_ref_cmp_by_name, NULL);
2305 TAILQ_FOREACH(re, &refs, entry)
2306 delete_ref(repo, re->ref);
2308 got_ref_list_free(&refs);
2312 static const struct got_error *
2313 cmd_fetch(int argc, char *argv[])
2315 const struct got_error *error = NULL, *unlock_err;
2316 char *cwd = NULL, *repo_path = NULL;
2317 const char *remote_name;
2318 char *proto = NULL, *host = NULL, *port = NULL;
2319 char *repo_name = NULL, *server_path = NULL;
2320 const struct got_remote_repo *remotes, *remote = NULL;
2322 char *id_str = NULL;
2323 struct got_repository *repo = NULL;
2324 struct got_worktree *worktree = NULL;
2325 const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
2326 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
2327 struct got_pathlist_entry *pe;
2328 struct got_object_id *pack_hash = NULL;
2329 int i, ch, fetchfd = -1, fetchstatus;
2330 pid_t fetchpid = -1;
2331 struct got_fetch_progress_arg fpa;
2332 int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
2333 int delete_refs = 0, replace_tags = 0, delete_remote = 0;
2334 int *pack_fds = NULL, have_bflag = 0;
2335 const char *worktree_branch = NULL;
2338 TAILQ_INIT(&symrefs);
2339 TAILQ_INIT(&wanted_branches);
2340 TAILQ_INIT(&wanted_refs);
2342 while ((ch = getopt(argc, argv, "ab:dlqR:r:tvX")) != -1) {
2345 fetch_all_branches = 1;
2348 error = got_pathlist_append(&wanted_branches,
2364 error = got_pathlist_append(&wanted_refs,
2370 repo_path = realpath(optarg, NULL);
2371 if (repo_path == NULL)
2372 return got_error_from_errno2("realpath",
2374 got_path_strip_trailing_slashes(repo_path);
2382 else if (verbosity < 3)
2396 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
2397 option_conflict('a', 'b');
2398 if (list_refs_only) {
2399 if (!TAILQ_EMPTY(&wanted_branches))
2400 option_conflict('l', 'b');
2401 if (fetch_all_branches)
2402 option_conflict('l', 'a');
2404 option_conflict('l', 'd');
2406 option_conflict('l', 'X');
2408 if (delete_remote) {
2409 if (fetch_all_branches)
2410 option_conflict('X', 'a');
2411 if (!TAILQ_EMPTY(&wanted_branches))
2412 option_conflict('X', 'b');
2414 option_conflict('X', 'd');
2416 option_conflict('X', 't');
2417 if (!TAILQ_EMPTY(&wanted_refs))
2418 option_conflict('X', 'R');
2423 errx(1, "-X option requires a remote name");
2424 remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
2425 } else if (argc == 1)
2426 remote_name = argv[0];
2430 cwd = getcwd(NULL, 0);
2432 error = got_error_from_errno("getcwd");
2436 error = got_repo_pack_fds_open(&pack_fds);
2440 if (repo_path == NULL) {
2441 error = got_worktree_open(&worktree, cwd);
2442 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2448 strdup(got_worktree_get_repo_path(worktree));
2449 if (repo_path == NULL)
2450 error = got_error_from_errno("strdup");
2454 repo_path = strdup(cwd);
2455 if (repo_path == NULL) {
2456 error = got_error_from_errno("strdup");
2462 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
2466 if (delete_remote) {
2467 error = delete_refs_for_remote(repo, remote_name);
2468 goto done; /* nothing else to do */
2472 worktree_conf = got_worktree_get_gotconfig(worktree);
2473 if (worktree_conf) {
2474 got_gotconfig_get_remotes(&nremotes, &remotes,
2476 for (i = 0; i < nremotes; i++) {
2477 if (strcmp(remotes[i].name, remote_name) == 0) {
2478 remote = &remotes[i];
2484 if (remote == NULL) {
2485 repo_conf = got_repo_get_gotconfig(repo);
2487 got_gotconfig_get_remotes(&nremotes, &remotes,
2489 for (i = 0; i < nremotes; i++) {
2490 if (strcmp(remotes[i].name, remote_name) == 0) {
2491 remote = &remotes[i];
2497 if (remote == NULL) {
2498 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
2499 for (i = 0; i < nremotes; i++) {
2500 if (strcmp(remotes[i].name, remote_name) == 0) {
2501 remote = &remotes[i];
2506 if (remote == NULL) {
2507 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
2511 if (TAILQ_EMPTY(&wanted_branches)) {
2512 if (!fetch_all_branches)
2513 fetch_all_branches = remote->fetch_all_branches;
2514 for (i = 0; i < remote->nfetch_branches; i++) {
2515 error = got_pathlist_append(&wanted_branches,
2516 remote->fetch_branches[i], NULL);
2521 if (TAILQ_EMPTY(&wanted_refs)) {
2522 for (i = 0; i < remote->nfetch_refs; i++) {
2523 error = got_pathlist_append(&wanted_refs,
2524 remote->fetch_refs[i], NULL);
2530 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
2531 &repo_name, remote->fetch_url);
2535 if (strcmp(proto, "git") == 0) {
2537 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2538 "sendfd dns inet unveil", NULL) == -1)
2541 } else if (strcmp(proto, "git+ssh") == 0 ||
2542 strcmp(proto, "ssh") == 0) {
2544 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2545 "sendfd unveil", NULL) == -1)
2548 } else if (strcmp(proto, "http") == 0 ||
2549 strcmp(proto, "git+http") == 0) {
2550 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
2553 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
2557 error = got_dial_apply_unveil(proto);
2561 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
2565 if (verbosity >= 0) {
2566 printf("Connecting to \"%s\" %s://%s%s%s%s%s\n",
2567 remote->name, proto, host,
2568 port ? ":" : "", port ? port : "",
2569 *server_path == '/' ? "" : "/", server_path);
2572 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
2573 server_path, verbosity);
2577 if (worktree && !have_bflag) {
2578 const char *refname;
2580 refname = got_worktree_get_head_ref_name(worktree);
2581 if (strncmp(refname, "refs/heads/", 11) == 0)
2582 worktree_branch = refname;
2585 fpa.last_scaled_size[0] = '\0';
2586 fpa.last_p_indexed = -1;
2587 fpa.last_p_resolved = -1;
2588 fpa.verbosity = verbosity;
2590 fpa.create_configs = 0;
2591 fpa.configs_created = 0;
2592 memset(&fpa.config_info, 0, sizeof(fpa.config_info));
2593 error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
2594 remote->mirror_references, fetch_all_branches, &wanted_branches,
2595 &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
2596 worktree_branch, fetch_progress, &fpa);
2600 if (list_refs_only) {
2601 error = list_remote_refs(&symrefs, &refs);
2605 if (pack_hash == NULL) {
2607 printf("Already up-to-date\n");
2608 } else if (verbosity >= 0) {
2609 error = got_object_id_str(&id_str, pack_hash);
2612 printf("\nFetched %s.pack\n", id_str);
2617 /* Update references provided with the pack file. */
2618 TAILQ_FOREACH(pe, &refs, entry) {
2619 const char *refname = pe->path;
2620 struct got_object_id *id = pe->data;
2621 struct got_reference *ref;
2622 char *remote_refname;
2624 if (is_wanted_ref(&wanted_refs, refname) &&
2625 !remote->mirror_references) {
2626 error = update_wanted_ref(refname, id,
2627 remote->name, verbosity, repo);
2633 if (remote->mirror_references ||
2634 strncmp("refs/tags/", refname, 10) == 0) {
2635 error = got_ref_open(&ref, repo, refname, 1);
2637 if (error->code != GOT_ERR_NOT_REF)
2639 error = create_ref(refname, id, verbosity,
2644 error = update_ref(ref, id, replace_tags,
2646 unlock_err = got_ref_unlock(ref);
2647 if (unlock_err && error == NULL)
2653 } else if (strncmp("refs/heads/", refname, 11) == 0) {
2654 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2655 remote_name, refname + 11) == -1) {
2656 error = got_error_from_errno("asprintf");
2660 error = got_ref_open(&ref, repo, remote_refname, 1);
2662 if (error->code != GOT_ERR_NOT_REF)
2664 error = create_ref(remote_refname, id,
2669 error = update_ref(ref, id, replace_tags,
2671 unlock_err = got_ref_unlock(ref);
2672 if (unlock_err && error == NULL)
2679 /* Also create a local branch if none exists yet. */
2680 error = got_ref_open(&ref, repo, refname, 1);
2682 if (error->code != GOT_ERR_NOT_REF)
2684 error = create_ref(refname, id, verbosity,
2689 unlock_err = got_ref_unlock(ref);
2690 if (unlock_err && error == NULL)
2697 error = delete_missing_refs(&refs, &symrefs, remote,
2703 if (!remote->mirror_references) {
2704 /* Update remote HEAD reference if the server provided one. */
2705 TAILQ_FOREACH(pe, &symrefs, entry) {
2706 struct got_reference *target_ref;
2707 const char *refname = pe->path;
2708 const char *target = pe->data;
2709 char *remote_refname = NULL, *remote_target = NULL;
2711 if (strcmp(refname, GOT_REF_HEAD) != 0)
2714 if (strncmp("refs/heads/", target, 11) != 0)
2717 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2718 remote->name, refname) == -1) {
2719 error = got_error_from_errno("asprintf");
2722 if (asprintf(&remote_target, "refs/remotes/%s/%s",
2723 remote->name, target + 11) == -1) {
2724 error = got_error_from_errno("asprintf");
2725 free(remote_refname);
2729 error = got_ref_open(&target_ref, repo, remote_target,
2732 free(remote_refname);
2733 free(remote_target);
2734 if (error->code == GOT_ERR_NOT_REF) {
2740 error = update_symref(remote_refname, target_ref,
2742 free(remote_refname);
2743 free(remote_target);
2744 got_ref_close(target_ref);
2751 if (kill(fetchpid, SIGTERM) == -1)
2752 error = got_error_from_errno("kill");
2753 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
2754 error = got_error_from_errno("waitpid");
2756 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
2757 error = got_error_from_errno("close");
2759 const struct got_error *close_err = got_repo_close(repo);
2764 got_worktree_close(worktree);
2766 const struct got_error *pack_err =
2767 got_repo_pack_fds_close(pack_fds);
2771 got_pathlist_free(&refs, GOT_PATHLIST_FREE_ALL);
2772 got_pathlist_free(&symrefs, GOT_PATHLIST_FREE_ALL);
2773 got_pathlist_free(&wanted_branches, GOT_PATHLIST_FREE_NONE);
2774 got_pathlist_free(&wanted_refs, GOT_PATHLIST_FREE_NONE);
2789 usage_checkout(void)
2791 fprintf(stderr, "usage: %s checkout [-Eq] [-b branch] [-c commit] "
2792 "[-p path-prefix] repository-path [work-tree-path]\n",
2798 show_worktree_base_ref_warning(void)
2800 fprintf(stderr, "%s: warning: could not create a reference "
2801 "to the work tree's base commit; the commit could be "
2802 "garbage-collected by Git or 'gotadmin cleanup'; making the "
2803 "repository writable and running 'got update' will prevent this\n",
2807 struct got_checkout_progress_arg {
2808 const char *worktree_path;
2809 int had_base_commit_ref_error;
2813 static const struct got_error *
2814 checkout_progress(void *arg, unsigned char status, const char *path)
2816 struct got_checkout_progress_arg *a = arg;
2818 /* Base commit bump happens silently. */
2819 if (status == GOT_STATUS_BUMP_BASE)
2822 if (status == GOT_STATUS_BASE_REF_ERR) {
2823 a->had_base_commit_ref_error = 1;
2827 while (path[0] == '/')
2830 if (a->verbosity >= 0)
2831 printf("%c %s/%s\n", status, a->worktree_path, path);
2836 static const struct got_error *
2837 check_cancelled(void *arg)
2839 if (sigint_received || sigpipe_received)
2840 return got_error(GOT_ERR_CANCELLED);
2844 static const struct got_error *
2845 check_linear_ancestry(struct got_object_id *commit_id,
2846 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2847 struct got_repository *repo)
2849 const struct got_error *err = NULL;
2850 struct got_object_id *yca_id;
2852 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2853 commit_id, base_commit_id, 1, repo, check_cancelled, NULL);
2858 return got_error(GOT_ERR_ANCESTRY);
2861 * Require a straight line of history between the target commit
2862 * and the work tree's base commit.
2864 * Non-linear situations such as this require a rebase:
2866 * (commit) D F (base_commit)
2874 * 'got update' only handles linear cases:
2875 * Update forwards in time: A (base/yca) - B - C - D (commit)
2876 * Update backwards in time: D (base) - C - B - A (commit/yca)
2878 if (allow_forwards_in_time_only) {
2879 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2880 return got_error(GOT_ERR_ANCESTRY);
2881 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2882 got_object_id_cmp(base_commit_id, yca_id) != 0)
2883 return got_error(GOT_ERR_ANCESTRY);
2889 static const struct got_error *
2890 check_same_branch(struct got_object_id *commit_id,
2891 struct got_reference *head_ref, struct got_object_id *yca_id,
2892 struct got_repository *repo)
2894 const struct got_error *err = NULL;
2895 struct got_commit_graph *graph = NULL;
2896 struct got_object_id *head_commit_id = NULL;
2897 int is_same_branch = 0;
2899 err = got_ref_resolve(&head_commit_id, repo, head_ref);
2903 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
2907 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
2912 err = got_commit_graph_open(&graph, "/", 1);
2916 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2917 check_cancelled, NULL);
2922 struct got_object_id id;
2924 err = got_commit_graph_iter_next(&id, graph, repo,
2925 check_cancelled, NULL);
2927 if (err->code == GOT_ERR_ITER_COMPLETED)
2932 if (yca_id && got_object_id_cmp(&id, yca_id) == 0)
2934 if (got_object_id_cmp(&id, commit_id) == 0) {
2941 got_commit_graph_close(graph);
2942 free(head_commit_id);
2943 if (!err && !is_same_branch)
2944 err = got_error(GOT_ERR_ANCESTRY);
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;
2993 while ((ch = getopt(argc, argv, "b:c:Ep:q")) != -1) {
2996 branch_name = optarg;
2999 commit_id_str = strdup(optarg);
3000 if (commit_id_str == NULL)
3001 return got_error_from_errno("strdup");
3007 path_prefix = optarg;
3022 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3023 "unveil", NULL) == -1)
3027 char *base, *dotgit;
3029 repo_path = realpath(argv[0], NULL);
3030 if (repo_path == NULL)
3031 return got_error_from_errno2("realpath", argv[0]);
3032 cwd = getcwd(NULL, 0);
3034 error = got_error_from_errno("getcwd");
3041 error = got_path_basename(&base, path);
3044 dotgit = strstr(base, ".git");
3047 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
3048 error = got_error_from_errno("asprintf");
3053 } else if (argc == 2) {
3054 repo_path = realpath(argv[0], NULL);
3055 if (repo_path == NULL) {
3056 error = got_error_from_errno2("realpath", argv[0]);
3059 worktree_path = realpath(argv[1], NULL);
3060 if (worktree_path == NULL) {
3061 if (errno != ENOENT) {
3062 error = got_error_from_errno2("realpath",
3066 worktree_path = strdup(argv[1]);
3067 if (worktree_path == NULL) {
3068 error = got_error_from_errno("strdup");
3075 got_path_strip_trailing_slashes(repo_path);
3076 got_path_strip_trailing_slashes(worktree_path);
3078 error = got_repo_pack_fds_open(&pack_fds);
3082 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3086 /* Pre-create work tree path for unveil(2) */
3087 error = got_path_mkdir(worktree_path);
3089 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
3090 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
3092 if (!allow_nonempty &&
3093 !got_path_dir_is_empty(worktree_path)) {
3094 error = got_error_path(worktree_path,
3095 GOT_ERR_DIR_NOT_EMPTY);
3100 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
3104 error = got_ref_open(&head_ref, repo, branch_name, 0);
3108 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
3109 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
3112 error = got_worktree_open(&worktree, worktree_path);
3116 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
3120 if (!same_path_prefix) {
3121 error = got_error(GOT_ERR_PATH_PREFIX);
3125 if (commit_id_str) {
3126 struct got_reflist_head refs;
3128 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3132 error = got_repo_match_object_id(&commit_id, NULL,
3133 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3134 got_ref_list_free(&refs);
3137 error = check_linear_ancestry(commit_id,
3138 got_worktree_get_base_commit_id(worktree), 0, repo);
3139 if (error != NULL) {
3140 if (error->code == GOT_ERR_ANCESTRY) {
3141 error = checkout_ancestry_error(
3142 head_ref, commit_id_str);
3146 error = check_same_branch(commit_id, head_ref, NULL, repo);
3148 if (error->code == GOT_ERR_ANCESTRY) {
3149 error = checkout_ancestry_error(
3150 head_ref, commit_id_str);
3154 error = got_worktree_set_base_commit_id(worktree, repo,
3158 /* Expand potentially abbreviated commit ID string. */
3159 free(commit_id_str);
3160 error = got_object_id_str(&commit_id_str, commit_id);
3164 commit_id = got_object_id_dup(
3165 got_worktree_get_base_commit_id(worktree));
3166 if (commit_id == NULL) {
3167 error = got_error_from_errno("got_object_id_dup");
3170 error = got_object_id_str(&commit_id_str, commit_id);
3175 error = got_pathlist_append(&paths, "", NULL);
3178 cpa.worktree_path = worktree_path;
3179 cpa.had_base_commit_ref_error = 0;
3180 cpa.verbosity = verbosity;
3181 error = got_worktree_checkout_files(worktree, &paths, repo,
3182 checkout_progress, &cpa, check_cancelled, NULL);
3186 if (got_ref_is_symbolic(head_ref)) {
3187 error = got_ref_resolve_symbolic(&ref, repo, head_ref);
3190 refname = got_ref_get_name(ref);
3192 refname = got_ref_get_name(head_ref);
3193 printf("Checked out %s: %s\n", refname, commit_id_str);
3194 printf("Now shut up and hack\n");
3195 if (cpa.had_base_commit_ref_error)
3196 show_worktree_base_ref_warning();
3199 const struct got_error *pack_err =
3200 got_repo_pack_fds_close(pack_fds);
3205 got_ref_close(head_ref);
3208 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
3209 free(commit_id_str);
3212 free(worktree_path);
3217 struct got_update_progress_arg {
3229 print_update_progress_stats(struct got_update_progress_arg *upa)
3231 if (!upa->did_something)
3234 if (upa->conflicts > 0)
3235 printf("Files with new merge conflicts: %d\n", upa->conflicts);
3236 if (upa->obstructed > 0)
3237 printf("File paths obstructed by a non-regular file: %d\n",
3239 if (upa->not_updated > 0)
3240 printf("Files not updated because of existing merge "
3241 "conflicts: %d\n", upa->not_updated);
3245 * The meaning of some status codes differs between merge-style operations and
3246 * update operations. For example, the ! status code means "file was missing"
3247 * if changes were merged into the work tree, and "missing file was restored"
3248 * if the work tree was updated. This function should be used by any operation
3249 * which merges changes into the work tree without updating the work tree.
3252 print_merge_progress_stats(struct got_update_progress_arg *upa)
3254 if (!upa->did_something)
3257 if (upa->conflicts > 0)
3258 printf("Files with new merge conflicts: %d\n", upa->conflicts);
3259 if (upa->obstructed > 0)
3260 printf("File paths obstructed by a non-regular file: %d\n",
3262 if (upa->missing > 0)
3263 printf("Files which had incoming changes but could not be "
3264 "found in the work tree: %d\n", upa->missing);
3265 if (upa->not_deleted > 0)
3266 printf("Files not deleted due to differences in deleted "
3267 "content: %d\n", upa->not_deleted);
3268 if (upa->unversioned > 0)
3269 printf("Files not merged because an unversioned file was "
3270 "found in the work tree: %d\n", upa->unversioned);
3276 fprintf(stderr, "usage: %s update [-q] [-b branch] [-c commit] "
3277 "[path ...]\n", getprogname());
3281 static const struct got_error *
3282 update_progress(void *arg, unsigned char status, const char *path)
3284 struct got_update_progress_arg *upa = arg;
3286 if (status == GOT_STATUS_EXISTS ||
3287 status == GOT_STATUS_BASE_REF_ERR)
3290 upa->did_something = 1;
3292 /* Base commit bump happens silently. */
3293 if (status == GOT_STATUS_BUMP_BASE)
3296 if (status == GOT_STATUS_CONFLICT)
3298 if (status == GOT_STATUS_OBSTRUCTED)
3300 if (status == GOT_STATUS_CANNOT_UPDATE)
3302 if (status == GOT_STATUS_MISSING)
3304 if (status == GOT_STATUS_CANNOT_DELETE)
3306 if (status == GOT_STATUS_UNVERSIONED)
3309 while (path[0] == '/')
3311 if (upa->verbosity >= 0)
3312 printf("%c %s\n", status, path);
3317 static const struct got_error *
3318 switch_head_ref(struct got_reference *head_ref,
3319 struct got_object_id *commit_id, struct got_worktree *worktree,
3320 struct got_repository *repo)
3322 const struct got_error *err = NULL;
3324 int ref_has_moved = 0;
3326 /* Trivial case: switching between two different references. */
3327 if (strcmp(got_ref_get_name(head_ref),
3328 got_worktree_get_head_ref_name(worktree)) != 0) {
3329 printf("Switching work tree from %s to %s\n",
3330 got_worktree_get_head_ref_name(worktree),
3331 got_ref_get_name(head_ref));
3332 return got_worktree_set_head_ref(worktree, head_ref);
3335 err = check_linear_ancestry(commit_id,
3336 got_worktree_get_base_commit_id(worktree), 0, repo);
3338 if (err->code != GOT_ERR_ANCESTRY)
3345 /* Switching to a rebased branch with the same reference name. */
3346 err = got_object_id_str(&base_id_str,
3347 got_worktree_get_base_commit_id(worktree));
3350 printf("Reference %s now points at a different branch\n",
3351 got_worktree_get_head_ref_name(worktree));
3352 printf("Switching work tree from %s to %s\n", base_id_str,
3353 got_worktree_get_head_ref_name(worktree));
3357 static const struct got_error *
3358 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
3360 const struct got_error *err;
3363 err = got_worktree_rebase_in_progress(&in_progress, worktree);
3367 return got_error(GOT_ERR_REBASING);
3369 err = got_worktree_histedit_in_progress(&in_progress, worktree);
3373 return got_error(GOT_ERR_HISTEDIT_BUSY);
3378 static const struct got_error *
3379 check_merge_in_progress(struct got_worktree *worktree,
3380 struct got_repository *repo)
3382 const struct got_error *err;
3385 err = got_worktree_merge_in_progress(&in_progress, worktree, repo);
3389 return got_error(GOT_ERR_MERGE_BUSY);
3394 static const struct got_error *
3395 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
3396 char *argv[], struct got_worktree *worktree)
3398 const struct got_error *err = NULL;
3400 struct got_pathlist_entry *new;
3406 return got_error_from_errno("strdup");
3407 return got_pathlist_append(paths, path, NULL);
3410 for (i = 0; i < argc; i++) {
3411 err = got_worktree_resolve_path(&path, worktree, argv[i]);
3414 err = got_pathlist_insert(&new, paths, path, NULL);
3415 if (err || new == NULL /* duplicate */) {
3425 static const struct got_error *
3426 wrap_not_worktree_error(const struct got_error *orig_err,
3427 const char *cmdname, const char *path)
3429 const struct got_error *err;
3430 struct got_repository *repo;
3431 static char msg[512];
3432 int *pack_fds = NULL;
3434 err = got_repo_pack_fds_open(&pack_fds);
3438 err = got_repo_open(&repo, path, NULL, pack_fds);
3442 snprintf(msg, sizeof(msg),
3443 "'got %s' needs a work tree in addition to a git repository\n"
3444 "Work trees can be checked out from this Git repository with "
3446 "The got(1) manual page contains more information.", cmdname);
3447 err = got_error_msg(GOT_ERR_NOT_WORKTREE, msg);
3448 got_repo_close(repo);
3450 const struct got_error *pack_err =
3451 got_repo_pack_fds_close(pack_fds);
3458 static const struct got_error *
3459 cmd_update(int argc, char *argv[])
3461 const struct got_error *error = NULL;
3462 struct got_repository *repo = NULL;
3463 struct got_worktree *worktree = NULL;
3464 char *worktree_path = NULL;
3465 struct got_object_id *commit_id = NULL;
3466 char *commit_id_str = NULL;
3467 const char *branch_name = NULL;
3468 struct got_reference *head_ref = NULL;
3469 struct got_pathlist_head paths;
3470 struct got_pathlist_entry *pe;
3471 int ch, verbosity = 0;
3472 struct got_update_progress_arg upa;
3473 int *pack_fds = NULL;
3477 while ((ch = getopt(argc, argv, "b:c:q")) != -1) {
3480 branch_name = optarg;
3483 commit_id_str = strdup(optarg);
3484 if (commit_id_str == NULL)
3485 return got_error_from_errno("strdup");
3500 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3501 "unveil", NULL) == -1)
3504 worktree_path = getcwd(NULL, 0);
3505 if (worktree_path == NULL) {
3506 error = got_error_from_errno("getcwd");
3510 error = got_repo_pack_fds_open(&pack_fds);
3514 error = got_worktree_open(&worktree, worktree_path);
3516 if (error->code == GOT_ERR_NOT_WORKTREE)
3517 error = wrap_not_worktree_error(error, "update",
3522 error = check_rebase_or_histedit_in_progress(worktree);
3526 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3531 error = apply_unveil(got_repo_get_path(repo), 0,
3532 got_worktree_get_root_path(worktree));
3536 error = check_merge_in_progress(worktree, repo);
3540 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3544 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
3545 got_worktree_get_head_ref_name(worktree), 0);
3548 if (commit_id_str == NULL) {
3549 error = got_ref_resolve(&commit_id, repo, head_ref);
3552 error = got_object_id_str(&commit_id_str, commit_id);
3556 struct got_reflist_head refs;
3558 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3562 error = got_repo_match_object_id(&commit_id, NULL,
3563 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3564 got_ref_list_free(&refs);
3565 free(commit_id_str);
3566 commit_id_str = NULL;
3569 error = got_object_id_str(&commit_id_str, commit_id);
3575 struct got_object_id *head_commit_id;
3576 TAILQ_FOREACH(pe, &paths, entry) {
3577 if (pe->path_len == 0)
3579 error = got_error_msg(GOT_ERR_BAD_PATH,
3580 "switching between branches requires that "
3581 "the entire work tree gets updated");
3584 error = got_ref_resolve(&head_commit_id, repo, head_ref);
3587 error = check_linear_ancestry(commit_id, head_commit_id, 0,
3589 free(head_commit_id);
3592 error = check_same_branch(commit_id, head_ref, NULL, repo);
3595 error = switch_head_ref(head_ref, commit_id, worktree, repo);
3599 error = check_linear_ancestry(commit_id,
3600 got_worktree_get_base_commit_id(worktree), 0, repo);
3601 if (error != NULL) {
3602 if (error->code == GOT_ERR_ANCESTRY)
3603 error = got_error(GOT_ERR_BRANCH_MOVED);
3606 error = check_same_branch(commit_id, head_ref, NULL, repo);
3611 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
3613 error = got_worktree_set_base_commit_id(worktree, repo,
3619 memset(&upa, 0, sizeof(upa));
3620 upa.verbosity = verbosity;
3621 error = got_worktree_checkout_files(worktree, &paths, repo,
3622 update_progress, &upa, check_cancelled, NULL);
3626 if (upa.did_something) {
3627 printf("Updated to %s: %s\n",
3628 got_worktree_get_head_ref_name(worktree), commit_id_str);
3630 printf("Already up-to-date\n");
3632 print_update_progress_stats(&upa);
3635 const struct got_error *pack_err =
3636 got_repo_pack_fds_close(pack_fds);
3640 free(worktree_path);
3641 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
3643 free(commit_id_str);
3647 static const struct got_error *
3648 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
3649 const char *path, int diff_context, int ignore_whitespace,
3650 int force_text_diff, struct got_diffstat_cb_arg *dsa,
3651 struct got_repository *repo, FILE *outfile)
3653 const struct got_error *err = NULL;
3654 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
3655 FILE *f1 = NULL, *f2 = NULL;
3656 int fd1 = -1, fd2 = -1;
3658 fd1 = got_opentempfd();
3660 return got_error_from_errno("got_opentempfd");
3661 fd2 = got_opentempfd();
3663 err = got_error_from_errno("got_opentempfd");
3668 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192,
3674 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192, fd2);
3678 f1 = got_opentemp();
3680 err = got_error_from_errno("got_opentemp");
3683 f2 = got_opentemp();
3685 err = got_error_from_errno("got_opentemp");
3689 while (path[0] == '/')
3691 err = got_diff_blob(NULL, NULL, blob1, blob2, f1, f2, path, path,
3692 GOT_DIFF_ALGORITHM_PATIENCE, diff_context, ignore_whitespace,
3693 force_text_diff, dsa, outfile);
3695 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3696 err = got_error_from_errno("close");
3698 got_object_blob_close(blob1);
3699 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3700 err = got_error_from_errno("close");
3701 got_object_blob_close(blob2);
3702 if (f1 && fclose(f1) == EOF && err == NULL)
3703 err = got_error_from_errno("fclose");
3704 if (f2 && fclose(f2) == EOF && err == NULL)
3705 err = got_error_from_errno("fclose");
3709 static const struct got_error *
3710 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
3711 const char *path, int diff_context, int ignore_whitespace,
3712 int force_text_diff, struct got_diffstat_cb_arg *dsa,
3713 struct got_repository *repo, FILE *outfile)
3715 const struct got_error *err = NULL;
3716 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3717 struct got_diff_blob_output_unidiff_arg arg;
3718 FILE *f1 = NULL, *f2 = NULL;
3719 int fd1 = -1, fd2 = -1;
3722 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3725 fd1 = got_opentempfd();
3727 err = got_error_from_errno("got_opentempfd");
3732 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3736 f1 = got_opentemp();
3738 err = got_error_from_errno("got_opentemp");
3742 f2 = got_opentemp();
3744 err = got_error_from_errno("got_opentemp");
3747 fd2 = got_opentempfd();
3749 err = got_error_from_errno("got_opentempfd");
3752 arg.diff_context = diff_context;
3753 arg.ignore_whitespace = ignore_whitespace;
3754 arg.force_text_diff = force_text_diff;
3756 arg.diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
3757 arg.outfile = outfile;
3760 while (path[0] == '/')
3762 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, path, path, repo,
3763 got_diff_blob_output_unidiff, &arg, 1);
3766 got_object_tree_close(tree1);
3768 got_object_tree_close(tree2);
3769 if (f1 && fclose(f1) == EOF && err == NULL)
3770 err = got_error_from_errno("fclose");
3771 if (f2 && fclose(f2) == EOF && err == NULL)
3772 err = got_error_from_errno("fclose");
3773 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3774 err = got_error_from_errno("close");
3775 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3776 err = got_error_from_errno("close");
3780 static const struct got_error *
3781 get_changed_paths(struct got_pathlist_head *paths,
3782 struct got_commit_object *commit, struct got_repository *repo,
3783 struct got_diffstat_cb_arg *dsa)
3785 const struct got_error *err = NULL;
3786 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3787 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3788 struct got_object_qid *qid;
3789 got_diff_blob_cb cb = got_diff_tree_collect_changed_paths;
3790 FILE *f1 = NULL, *f2 = NULL;
3791 int fd1 = -1, fd2 = -1;
3794 cb = got_diff_tree_compute_diffstat;
3796 f1 = got_opentemp();
3798 err = got_error_from_errno("got_opentemp");
3801 f2 = got_opentemp();
3803 err = got_error_from_errno("got_opentemp");
3806 fd1 = got_opentempfd();
3808 err = got_error_from_errno("got_opentempfd");
3811 fd2 = got_opentempfd();
3813 err = got_error_from_errno("got_opentempfd");
3818 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3820 struct got_commit_object *pcommit;
3821 err = got_object_open_as_commit(&pcommit, repo,
3826 tree_id1 = got_object_id_dup(
3827 got_object_commit_get_tree_id(pcommit));
3828 if (tree_id1 == NULL) {
3829 got_object_commit_close(pcommit);
3830 return got_error_from_errno("got_object_id_dup");
3832 got_object_commit_close(pcommit);
3837 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3842 tree_id2 = got_object_commit_get_tree_id(commit);
3843 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3847 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3848 cb, dsa ? (void *)dsa : paths, dsa ? 1 : 0);
3851 got_object_tree_close(tree1);
3853 got_object_tree_close(tree2);
3854 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3855 err = got_error_from_errno("close");
3856 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3857 err = got_error_from_errno("close");
3858 if (f1 && fclose(f1) == EOF && err == NULL)
3859 err = got_error_from_errno("fclose");
3860 if (f2 && fclose(f2) == EOF && err == NULL)
3861 err = got_error_from_errno("fclose");
3866 static const struct got_error *
3867 print_patch(struct got_commit_object *commit, struct got_object_id *id,
3868 const char *path, int diff_context, struct got_diffstat_cb_arg *dsa,
3869 struct got_repository *repo, FILE *outfile)
3871 const struct got_error *err = NULL;
3872 struct got_commit_object *pcommit = NULL;
3873 char *id_str1 = NULL, *id_str2 = NULL;
3874 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
3875 struct got_object_qid *qid;
3877 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3879 err = got_object_open_as_commit(&pcommit, repo,
3883 err = got_object_id_str(&id_str1, &qid->id);
3888 err = got_object_id_str(&id_str2, id);
3892 if (path && path[0] != '\0') {
3894 err = got_object_id_by_path(&obj_id2, repo, commit, path);
3898 err = got_object_id_by_path(&obj_id1, repo,
3901 if (err->code != GOT_ERR_NO_TREE_ENTRY) {
3907 err = got_object_get_type(&obj_type, repo, obj_id2);
3913 "diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3914 fprintf(outfile, "commit - %s\n",
3915 id_str1 ? id_str1 : "/dev/null");
3916 fprintf(outfile, "commit + %s\n", id_str2);
3918 case GOT_OBJ_TYPE_BLOB:
3919 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
3920 0, 0, dsa, repo, outfile);
3922 case GOT_OBJ_TYPE_TREE:
3923 err = diff_trees(obj_id1, obj_id2, path, diff_context,
3924 0, 0, dsa, repo, outfile);
3927 err = got_error(GOT_ERR_OBJ_TYPE);
3933 obj_id2 = got_object_commit_get_tree_id(commit);
3935 obj_id1 = got_object_commit_get_tree_id(pcommit);
3937 "diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3938 fprintf(outfile, "commit - %s\n",
3939 id_str1 ? id_str1 : "/dev/null");
3940 fprintf(outfile, "commit + %s\n", id_str2);
3941 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, 0,
3942 dsa, repo, outfile);
3948 got_object_commit_close(pcommit);
3953 get_datestr(time_t *time, char *datebuf)
3955 struct tm mytm, *tm;
3958 tm = gmtime_r(time, &mytm);
3961 s = asctime_r(tm, datebuf);
3964 p = strchr(s, '\n');
3970 static const struct got_error *
3971 match_commit(int *have_match, struct got_object_id *id,
3972 struct got_commit_object *commit, regex_t *regex)
3974 const struct got_error *err = NULL;
3975 regmatch_t regmatch;
3976 char *id_str = NULL, *logmsg = NULL;
3980 err = got_object_id_str(&id_str, id);
3984 err = got_object_commit_get_logmsg(&logmsg, commit);
3988 if (regexec(regex, got_object_commit_get_author(commit), 1,
3989 ®match, 0) == 0 ||
3990 regexec(regex, got_object_commit_get_committer(commit), 1,
3991 ®match, 0) == 0 ||
3992 regexec(regex, id_str, 1, ®match, 0) == 0 ||
3993 regexec(regex, logmsg, 1, ®match, 0) == 0)
4002 match_changed_paths(int *have_match, struct got_pathlist_head *changed_paths,
4005 regmatch_t regmatch;
4006 struct got_pathlist_entry *pe;
4010 TAILQ_FOREACH(pe, changed_paths, entry) {
4011 if (regexec(regex, pe->path, 1, ®match, 0) == 0) {
4018 static const struct got_error *
4019 match_patch(int *have_match, struct got_commit_object *commit,
4020 struct got_object_id *id, const char *path, int diff_context,
4021 struct got_repository *repo, regex_t *regex, FILE *f)
4023 const struct got_error *err = NULL;
4025 size_t linesize = 0;
4026 regmatch_t regmatch;
4030 err = got_opentemp_truncate(f);
4034 err = print_patch(commit, id, path, diff_context, NULL, repo, f);
4038 if (fseeko(f, 0L, SEEK_SET) == -1) {
4039 err = got_error_from_errno("fseeko");
4043 while (getline(&line, &linesize, f) != -1) {
4044 if (regexec(regex, line, 1, ®match, 0) == 0) {
4054 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
4056 static const struct got_error*
4057 build_refs_str(char **refs_str, struct got_reflist_head *refs,
4058 struct got_object_id *id, struct got_repository *repo,
4061 static const struct got_error *err = NULL;
4062 struct got_reflist_entry *re;
4068 TAILQ_FOREACH(re, refs, entry) {
4069 struct got_tag_object *tag = NULL;
4070 struct got_object_id *ref_id;
4073 name = got_ref_get_name(re->ref);
4074 if (strcmp(name, GOT_REF_HEAD) == 0)
4076 if (strncmp(name, "refs/", 5) == 0)
4078 if (strncmp(name, "got/", 4) == 0)
4080 if (strncmp(name, "heads/", 6) == 0)
4082 if (strncmp(name, "remotes/", 8) == 0) {
4086 s = strstr(name, "/" GOT_REF_HEAD);
4087 if (s != NULL && s[strlen(s)] == '\0')
4090 err = got_ref_resolve(&ref_id, repo, re->ref);
4093 if (strncmp(name, "tags/", 5) == 0) {
4094 err = got_object_open_as_tag(&tag, repo, ref_id);
4096 if (err->code != GOT_ERR_OBJ_TYPE) {
4100 /* Ref points at something other than a tag. */
4105 cmp = got_object_id_cmp(tag ?
4106 got_object_tag_get_object_id(tag) : ref_id, id);
4109 got_object_tag_close(tag);
4113 if (asprintf(refs_str, "%s%s%s", s ? s : "",
4114 s ? ", " : "", name) == -1) {
4115 err = got_error_from_errno("asprintf");
4126 static const struct got_error *
4127 print_commit_oneline(struct got_commit_object *commit, struct got_object_id *id,
4128 struct got_repository *repo, struct got_reflist_object_id_map *refs_idmap)
4130 const struct got_error *err = NULL;
4131 char *ref_str = NULL, *id_str = NULL, *logmsg0 = NULL;
4132 char *comma, *s, *nl;
4133 struct got_reflist_head *refs;
4134 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
4136 time_t committer_time;
4138 refs = got_reflist_object_id_map_lookup(refs_idmap, id);
4140 err = build_refs_str(&ref_str, refs, id, repo, 1);
4144 /* Display the first matching ref only. */
4145 if (ref_str && (comma = strchr(ref_str, ',')) != NULL)
4149 if (ref_str == NULL) {
4150 err = got_object_id_str(&id_str, id);
4155 committer_time = got_object_commit_get_committer_time(commit);
4156 if (gmtime_r(&committer_time, &tm) == NULL) {
4157 err = got_error_from_errno("gmtime_r");
4160 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0) {
4161 err = got_error(GOT_ERR_NO_SPACE);
4165 err = got_object_commit_get_logmsg(&logmsg0, commit);
4170 while (isspace((unsigned char)s[0]))
4173 nl = strchr(s, '\n');
4179 printf("%s%-7s %s\n", datebuf, ref_str, s);
4181 printf("%s%.7s %s\n", datebuf, id_str, s);
4183 if (fflush(stdout) != 0 && err == NULL)
4184 err = got_error_from_errno("fflush");
4192 static const struct got_error *
4193 print_diffstat(struct got_diffstat_cb_arg *dsa, const char *header)
4195 struct got_pathlist_entry *pe;
4198 printf("%s\n", header);
4200 TAILQ_FOREACH(pe, dsa->paths, entry) {
4201 struct got_diff_changed_path *cp = pe->data;
4202 int pad = dsa->max_path_len - pe->path_len + 1;
4204 printf(" %c %s%*c | %*d+ %*d-\n", cp->status, pe->path, pad,
4205 ' ', dsa->add_cols + 1, cp->add, dsa->rm_cols + 1, cp->rm);
4207 printf("\n%d file%s changed, %d insertion%s(+), %d deletion%s(-)\n\n",
4208 dsa->nfiles, dsa->nfiles > 1 ? "s" : "", dsa->ins,
4209 dsa->ins != 1 ? "s" : "", dsa->del, dsa->del != 1 ? "s" : "");
4211 if (fflush(stdout) != 0)
4212 return got_error_from_errno("fflush");
4217 static const struct got_error *
4223 if (fseeko(f, 0L, SEEK_SET) == -1)
4224 return got_error_from_errno("fseek");
4227 r = fread(buf, 1, sizeof(buf), f);
4230 return got_error_from_errno("fread");
4234 if (fwrite(buf, 1, r, stdout) != r)
4235 return got_ferror(stdout, GOT_ERR_IO);
4241 static const struct got_error *
4242 print_commit(struct got_commit_object *commit, struct got_object_id *id,
4243 struct got_repository *repo, const char *path,
4244 struct got_pathlist_head *changed_paths,
4245 struct got_diffstat_cb_arg *diffstat, int show_patch, int diff_context,
4246 struct got_reflist_object_id_map *refs_idmap, const char *custom_refs_str,
4249 const struct got_error *err = NULL;
4251 char *id_str, *datestr, *logmsg0, *logmsg, *line;
4253 time_t committer_time;
4254 const char *author, *committer;
4255 char *refs_str = NULL;
4257 err = got_object_id_str(&id_str, id);
4261 if (custom_refs_str == NULL) {
4262 struct got_reflist_head *refs;
4263 refs = got_reflist_object_id_map_lookup(refs_idmap, id);
4265 err = build_refs_str(&refs_str, refs, id, repo, 0);
4271 printf(GOT_COMMIT_SEP_STR);
4272 if (custom_refs_str)
4273 printf("%s %s (%s)\n", prefix ? prefix : "commit", id_str,
4276 printf("%s %s%s%s%s\n", prefix ? prefix : "commit", id_str,
4277 refs_str ? " (" : "", refs_str ? refs_str : "",
4278 refs_str ? ")" : "");
4283 printf("from: %s\n", got_object_commit_get_author(commit));
4284 author = got_object_commit_get_author(commit);
4285 committer = got_object_commit_get_committer(commit);
4286 if (strcmp(author, committer) != 0)
4287 printf("via: %s\n", committer);
4288 committer_time = got_object_commit_get_committer_time(commit);
4289 datestr = get_datestr(&committer_time, datebuf);
4291 printf("date: %s UTC\n", datestr);
4292 if (got_object_commit_get_nparents(commit) > 1) {
4293 const struct got_object_id_queue *parent_ids;
4294 struct got_object_qid *qid;
4296 parent_ids = got_object_commit_get_parent_ids(commit);
4297 STAILQ_FOREACH(qid, parent_ids, entry) {
4298 err = got_object_id_str(&id_str, &qid->id);
4301 printf("parent %d: %s\n", n++, id_str);
4307 err = got_object_commit_get_logmsg(&logmsg0, commit);
4313 line = strsep(&logmsg, "\n");
4315 printf(" %s\n", line);
4319 if (changed_paths && diffstat == NULL) {
4320 struct got_pathlist_entry *pe;
4322 TAILQ_FOREACH(pe, changed_paths, entry) {
4323 struct got_diff_changed_path *cp = pe->data;
4325 printf(" %c %s\n", cp->status, pe->path);
4333 err = got_error_from_errno("got_opentemp");
4338 err = print_patch(commit, id, path, diff_context, diffstat,
4339 repo, diffstat == NULL ? stdout : f);
4344 err = print_diffstat(diffstat, NULL);
4356 if (fflush(stdout) != 0 && err == NULL)
4357 err = got_error_from_errno("fflush");
4359 if (f && fclose(f) == EOF && err == NULL)
4360 err = got_error_from_errno("fclose");
4366 static const struct got_error *
4367 print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
4368 struct got_repository *repo, const char *path, int show_changed_paths,
4369 int show_diffstat, int show_patch, const char *search_pattern,
4370 int diff_context, int limit, int log_branches, int reverse_display_order,
4371 struct got_reflist_object_id_map *refs_idmap, int one_line,
4374 const struct got_error *err;
4375 struct got_commit_graph *graph;
4378 struct got_object_id_queue reversed_commits;
4379 struct got_object_qid *qid;
4380 struct got_commit_object *commit;
4381 struct got_pathlist_head changed_paths;
4383 STAILQ_INIT(&reversed_commits);
4384 TAILQ_INIT(&changed_paths);
4386 if (search_pattern && regcomp(®ex, search_pattern,
4387 REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
4388 return got_error_msg(GOT_ERR_REGEX, search_pattern);
4390 err = got_commit_graph_open(&graph, path, !log_branches);
4393 err = got_commit_graph_iter_start(graph, root_id, repo,
4394 check_cancelled, NULL);
4398 struct got_object_id id;
4399 struct got_diffstat_cb_arg dsa = { 0, 0, 0, 0, 0, 0,
4400 &changed_paths, 0, 0, GOT_DIFF_ALGORITHM_PATIENCE };
4402 if (sigint_received || sigpipe_received)
4405 err = got_commit_graph_iter_next(&id, graph, repo,
4406 check_cancelled, NULL);
4408 if (err->code == GOT_ERR_ITER_COMPLETED)
4413 err = got_object_open_as_commit(&commit, repo, &id);
4417 if ((show_changed_paths || (show_diffstat && !show_patch))
4418 && !reverse_display_order) {
4419 err = get_changed_paths(&changed_paths, commit, repo,
4420 show_diffstat ? &dsa : NULL);
4425 if (search_pattern) {
4426 err = match_commit(&have_match, &id, commit, ®ex);
4428 got_object_commit_close(commit);
4431 if (have_match == 0 && show_changed_paths)
4432 match_changed_paths(&have_match,
4433 &changed_paths, ®ex);
4434 if (have_match == 0 && show_patch) {
4435 err = match_patch(&have_match, commit, &id,
4436 path, diff_context, repo, ®ex, tmpfile);
4440 if (have_match == 0) {
4441 got_object_commit_close(commit);
4442 got_pathlist_free(&changed_paths,
4443 GOT_PATHLIST_FREE_ALL);
4448 if (reverse_display_order) {
4449 err = got_object_qid_alloc(&qid, &id);
4452 STAILQ_INSERT_HEAD(&reversed_commits, qid, entry);
4453 got_object_commit_close(commit);
4456 err = print_commit_oneline(commit, &id,
4459 err = print_commit(commit, &id, repo, path,
4460 (show_changed_paths || show_diffstat) ?
4461 &changed_paths : NULL,
4462 show_diffstat ? &dsa : NULL, show_patch,
4463 diff_context, refs_idmap, NULL, NULL);
4464 got_object_commit_close(commit);
4468 if ((limit && --limit == 0) ||
4469 (end_id && got_object_id_cmp(&id, end_id) == 0))
4472 got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
4474 if (reverse_display_order) {
4475 STAILQ_FOREACH(qid, &reversed_commits, entry) {
4476 struct got_diffstat_cb_arg dsa = { 0, 0, 0, 0, 0, 0,
4477 &changed_paths, 0, 0, GOT_DIFF_ALGORITHM_PATIENCE };
4479 err = got_object_open_as_commit(&commit, repo,
4483 if (show_changed_paths ||
4484 (show_diffstat && !show_patch)) {
4485 err = get_changed_paths(&changed_paths, commit,
4486 repo, show_diffstat ? &dsa : NULL);
4491 err = print_commit_oneline(commit, &qid->id,
4494 err = print_commit(commit, &qid->id, repo, path,
4495 (show_changed_paths || show_diffstat) ?
4496 &changed_paths : NULL,
4497 show_diffstat ? &dsa : NULL, show_patch,
4498 diff_context, refs_idmap, NULL, NULL);
4499 got_object_commit_close(commit);
4502 got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
4506 while (!STAILQ_EMPTY(&reversed_commits)) {
4507 qid = STAILQ_FIRST(&reversed_commits);
4508 STAILQ_REMOVE_HEAD(&reversed_commits, entry);
4509 got_object_qid_free(qid);
4511 got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
4514 got_commit_graph_close(graph);
4521 fprintf(stderr, "usage: %s log [-bdPpRs] [-C number] [-c commit] "
4522 "[-l N] [-r repository-path] [-S search-pattern] [-x commit] "
4523 "[path]\n", getprogname());
4528 get_default_log_limit(void)
4530 const char *got_default_log_limit;
4534 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
4535 if (got_default_log_limit == NULL)
4537 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
4543 static const struct got_error *
4544 cmd_log(int argc, char *argv[])
4546 const struct got_error *error;
4547 struct got_repository *repo = NULL;
4548 struct got_worktree *worktree = NULL;
4549 struct got_object_id *start_id = NULL, *end_id = NULL;
4550 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
4551 const char *start_commit = NULL, *end_commit = NULL;
4552 const char *search_pattern = NULL;
4553 int diff_context = -1, ch;
4554 int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
4555 int show_diffstat = 0, reverse_display_order = 0, one_line = 0;
4557 struct got_reflist_head refs;
4558 struct got_reflist_object_id_map *refs_idmap = NULL;
4559 FILE *tmpfile = NULL;
4560 int *pack_fds = NULL;
4565 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4571 limit = get_default_log_limit();
4573 while ((ch = getopt(argc, argv, "bC:c:dl:PpRr:S:sx:")) != -1) {
4579 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4582 errx(1, "number of context lines is %s: %s",
4586 start_commit = optarg;
4592 limit = strtonum(optarg, 0, INT_MAX, &errstr);
4594 errx(1, "number of commits is %s: %s",
4598 show_changed_paths = 1;
4604 reverse_display_order = 1;
4607 repo_path = realpath(optarg, NULL);
4608 if (repo_path == NULL)
4609 return got_error_from_errno2("realpath",
4611 got_path_strip_trailing_slashes(repo_path);
4614 search_pattern = optarg;
4620 end_commit = optarg;
4631 if (diff_context == -1)
4633 else if (!show_patch)
4634 errx(1, "-C requires -p");
4636 if (one_line && (show_patch || show_changed_paths || show_diffstat))
4637 errx(1, "cannot use -s with -d, -p or -P");
4639 cwd = getcwd(NULL, 0);
4641 error = got_error_from_errno("getcwd");
4645 error = got_repo_pack_fds_open(&pack_fds);
4649 if (repo_path == NULL) {
4650 error = got_worktree_open(&worktree, cwd);
4651 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4658 error = got_worktree_resolve_path(&path, worktree,
4663 path = strdup(argv[0]);
4665 error = got_error_from_errno("strdup");
4669 } else if (argc != 0)
4672 if (repo_path == NULL) {
4673 repo_path = worktree ?
4674 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
4676 if (repo_path == NULL) {
4677 error = got_error_from_errno("strdup");
4681 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4685 error = apply_unveil(got_repo_get_path(repo), 1,
4686 worktree ? got_worktree_get_root_path(worktree) : NULL);
4690 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4694 error = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
4698 if (start_commit == NULL) {
4699 struct got_reference *head_ref;
4700 struct got_commit_object *commit = NULL;
4701 error = got_ref_open(&head_ref, repo,
4702 worktree ? got_worktree_get_head_ref_name(worktree)
4706 error = got_ref_resolve(&start_id, repo, head_ref);
4707 got_ref_close(head_ref);
4710 error = got_object_open_as_commit(&commit, repo,
4714 got_object_commit_close(commit);
4716 error = got_repo_match_object_id(&start_id, NULL,
4717 start_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4721 if (end_commit != NULL) {
4722 error = got_repo_match_object_id(&end_id, NULL,
4723 end_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4730 * If a path was specified on the command line it was resolved
4731 * to a path in the work tree above. Prepend the work tree's
4732 * path prefix to obtain the corresponding in-repository path.
4736 prefix = got_worktree_get_path_prefix(worktree);
4737 if (asprintf(&in_repo_path, "%s%s%s", prefix,
4738 (path[0] != '\0') ? "/" : "", path) == -1) {
4739 error = got_error_from_errno("asprintf");
4744 error = got_repo_map_path(&in_repo_path, repo,
4750 path = in_repo_path;
4754 /* Release work tree lock. */
4755 got_worktree_close(worktree);
4759 if (search_pattern && show_patch) {
4760 tmpfile = got_opentemp();
4761 if (tmpfile == NULL) {
4762 error = got_error_from_errno("got_opentemp");
4767 error = print_commits(start_id, end_id, repo, path ? path : "",
4768 show_changed_paths, show_diffstat, show_patch, search_pattern,
4769 diff_context, limit, log_branches, reverse_display_order,
4770 refs_idmap, one_line, tmpfile);
4776 got_worktree_close(worktree);
4778 const struct got_error *close_err = got_repo_close(repo);
4783 const struct got_error *pack_err =
4784 got_repo_pack_fds_close(pack_fds);
4789 got_reflist_object_id_map_free(refs_idmap);
4790 if (tmpfile && fclose(tmpfile) == EOF && error == NULL)
4791 error = got_error_from_errno("fclose");
4792 got_ref_list_free(&refs);
4799 fprintf(stderr, "usage: %s diff [-adPsw] [-C number] [-c commit] "
4800 "[-r repository-path] [object1 object2 | path ...]\n",
4805 struct print_diff_arg {
4806 struct got_repository *repo;
4807 struct got_worktree *worktree;
4808 struct got_diffstat_cb_arg *diffstat;
4813 enum got_diff_algorithm diff_algo;
4814 int ignore_whitespace;
4815 int force_text_diff;
4822 * Create a file which contains the target path of a symlink so we can feed
4823 * it as content to the diff engine.
4825 static const struct got_error *
4826 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
4827 const char *abspath)
4829 const struct got_error *err = NULL;
4830 char target_path[PATH_MAX];
4831 ssize_t target_len, outlen;
4836 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
4837 if (target_len == -1)
4838 return got_error_from_errno2("readlinkat", abspath);
4840 target_len = readlink(abspath, target_path, PATH_MAX);
4841 if (target_len == -1)
4842 return got_error_from_errno2("readlink", abspath);
4845 *fd = got_opentempfd();
4847 return got_error_from_errno("got_opentempfd");
4849 outlen = write(*fd, target_path, target_len);
4851 err = got_error_from_errno("got_opentempfd");
4855 if (lseek(*fd, 0, SEEK_SET) == -1) {
4856 err = got_error_from_errno2("lseek", abspath);
4867 static const struct got_error *
4868 print_diff(void *arg, unsigned char status, unsigned char staged_status,
4869 const char *path, struct got_object_id *blob_id,
4870 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4871 int dirfd, const char *de_name)
4873 struct print_diff_arg *a = arg;
4874 const struct got_error *err = NULL;
4875 struct got_blob_object *blob1 = NULL;
4876 int fd = -1, fd1 = -1, fd2 = -1;
4878 char *abspath = NULL, *label1 = NULL;
4883 memset(&sb, 0, sizeof(sb));
4885 if (a->diff_staged) {
4886 if (staged_status != GOT_STATUS_MODIFY &&
4887 staged_status != GOT_STATUS_ADD &&
4888 staged_status != GOT_STATUS_DELETE)
4891 if (staged_status == GOT_STATUS_DELETE)
4893 if (status == GOT_STATUS_NONEXISTENT)
4894 return got_error_set_errno(ENOENT, path);
4895 if (status != GOT_STATUS_MODIFY &&
4896 status != GOT_STATUS_ADD &&
4897 status != GOT_STATUS_DELETE &&
4898 status != GOT_STATUS_CONFLICT)
4902 err = got_opentemp_truncate(a->f1);
4904 return got_error_from_errno("got_opentemp_truncate");
4905 err = got_opentemp_truncate(a->f2);
4907 return got_error_from_errno("got_opentemp_truncate");
4909 if (!a->header_shown) {
4910 if (fprintf(a->outfile, "diff %s%s\n",
4911 a->diff_staged ? "-s " : "",
4912 got_worktree_get_root_path(a->worktree)) < 0) {
4913 err = got_error_from_errno("fprintf");
4916 if (fprintf(a->outfile, "commit - %s\n", a->id_str) < 0) {
4917 err = got_error_from_errno("fprintf");
4920 if (fprintf(a->outfile, "path + %s%s\n",
4921 got_worktree_get_root_path(a->worktree),
4922 a->diff_staged ? " (staged changes)" : "") < 0) {
4923 err = got_error_from_errno("fprintf");
4926 a->header_shown = 1;
4929 if (a->diff_staged) {
4930 const char *label1 = NULL, *label2 = NULL;
4931 switch (staged_status) {
4932 case GOT_STATUS_MODIFY:
4936 case GOT_STATUS_ADD:
4939 case GOT_STATUS_DELETE:
4943 return got_error(GOT_ERR_FILE_STATUS);
4945 fd1 = got_opentempfd();
4947 err = got_error_from_errno("got_opentempfd");
4950 fd2 = got_opentempfd();
4952 err = got_error_from_errno("got_opentempfd");
4955 err = got_diff_objects_as_blobs(NULL, NULL, a->f1, a->f2,
4956 fd1, fd2, blob_id, staged_blob_id, label1, label2,
4957 a->diff_algo, a->diff_context, a->ignore_whitespace,
4958 a->force_text_diff, a->diffstat, a->repo, a->outfile);
4962 fd1 = got_opentempfd();
4964 err = got_error_from_errno("got_opentempfd");
4968 if (staged_status == GOT_STATUS_ADD ||
4969 staged_status == GOT_STATUS_MODIFY) {
4971 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
4975 err = got_object_id_str(&id_str, staged_blob_id);
4978 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
4979 err = got_error_from_errno("asprintf");
4984 } else if (status != GOT_STATUS_ADD) {
4985 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192,
4991 if (status != GOT_STATUS_DELETE) {
4992 if (asprintf(&abspath, "%s/%s",
4993 got_worktree_get_root_path(a->worktree), path) == -1) {
4994 err = got_error_from_errno("asprintf");
4999 fd = openat(dirfd, de_name,
5000 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5002 if (!got_err_open_nofollow_on_symlink()) {
5003 err = got_error_from_errno2("openat",
5007 err = get_symlink_target_file(&fd, dirfd,
5013 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5015 if (!got_err_open_nofollow_on_symlink()) {
5016 err = got_error_from_errno2("open",
5020 err = get_symlink_target_file(&fd, dirfd,
5026 if (fstatat(fd, abspath, &sb, AT_SYMLINK_NOFOLLOW) == -1) {
5027 err = got_error_from_errno2("fstatat", abspath);
5030 f2 = fdopen(fd, "r");
5032 err = got_error_from_errno2("fdopen", abspath);
5040 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5046 err = got_diff_blob_file(blob1, a->f1, size1, label1, f2 ? f2 : a->f2,
5047 f2_exists, &sb, path, GOT_DIFF_ALGORITHM_PATIENCE, a->diff_context,
5048 a->ignore_whitespace, a->force_text_diff, a->diffstat, a->outfile);
5050 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5051 err = got_error_from_errno("close");
5052 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5053 err = got_error_from_errno("close");
5055 got_object_blob_close(blob1);
5056 if (fd != -1 && close(fd) == -1 && err == NULL)
5057 err = got_error_from_errno("close");
5058 if (f2 && fclose(f2) == EOF && err == NULL)
5059 err = got_error_from_errno("fclose");
5064 static const struct got_error *
5065 cmd_diff(int argc, char *argv[])
5067 const struct got_error *error;
5068 struct got_repository *repo = NULL;
5069 struct got_worktree *worktree = NULL;
5070 char *cwd = NULL, *repo_path = NULL;
5071 const char *commit_args[2] = { NULL, NULL };
5072 int ncommit_args = 0;
5073 struct got_object_id *ids[2] = { NULL, NULL };
5074 char *labels[2] = { NULL, NULL };
5075 int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
5076 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch, i;
5077 int force_text_diff = 0, force_path = 0, rflag = 0, show_diffstat = 0;
5079 struct got_reflist_head refs;
5080 struct got_pathlist_head diffstat_paths, paths;
5081 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
5082 int fd1 = -1, fd2 = -1;
5083 int *pack_fds = NULL;
5084 struct got_diffstat_cb_arg dsa;
5086 memset(&dsa, 0, sizeof(dsa));
5090 TAILQ_INIT(&diffstat_paths);
5093 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5098 while ((ch = getopt(argc, argv, "aC:c:dPr:sw")) != -1) {
5101 force_text_diff = 1;
5104 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
5107 errx(1, "number of context lines is %s: %s",
5111 if (ncommit_args >= 2)
5112 errx(1, "too many -c options used");
5113 commit_args[ncommit_args++] = optarg;
5122 repo_path = realpath(optarg, NULL);
5123 if (repo_path == NULL)
5124 return got_error_from_errno2("realpath",
5126 got_path_strip_trailing_slashes(repo_path);
5133 ignore_whitespace = 1;
5144 cwd = getcwd(NULL, 0);
5146 error = got_error_from_errno("getcwd");
5150 error = got_repo_pack_fds_open(&pack_fds);
5154 if (repo_path == NULL) {
5155 error = got_worktree_open(&worktree, cwd);
5156 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5162 strdup(got_worktree_get_repo_path(worktree));
5163 if (repo_path == NULL) {
5164 error = got_error_from_errno("strdup");
5168 repo_path = strdup(cwd);
5169 if (repo_path == NULL) {
5170 error = got_error_from_errno("strdup");
5176 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5181 if (show_diffstat) {
5182 dsa.paths = &diffstat_paths;
5183 dsa.force_text = force_text_diff;
5184 dsa.ignore_ws = ignore_whitespace;
5185 dsa.diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
5188 if (rflag || worktree == NULL || ncommit_args > 0) {
5190 error = got_error_msg(GOT_ERR_NOT_IMPL,
5191 "-P option can only be used when diffing "
5196 error = got_error_msg(GOT_ERR_NOT_IMPL,
5197 "-s option can only be used when diffing "
5203 error = apply_unveil(got_repo_get_path(repo), 1,
5204 worktree ? got_worktree_get_root_path(worktree) : NULL);
5208 if ((!force_path && argc == 2) || ncommit_args > 0) {
5209 int obj_type = (ncommit_args > 0 ?
5210 GOT_OBJ_TYPE_COMMIT : GOT_OBJ_TYPE_ANY);
5211 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5215 for (i = 0; i < (ncommit_args > 0 ? ncommit_args : argc); i++) {
5217 if (ncommit_args > 0)
5218 arg = commit_args[i];
5221 error = got_repo_match_object_id(&ids[i], &labels[i],
5222 arg, obj_type, &refs, repo);
5224 if (error->code != GOT_ERR_NOT_REF &&
5225 error->code != GOT_ERR_NO_OBJ)
5227 if (ncommit_args > 0)
5235 f1 = got_opentemp();
5237 error = got_error_from_errno("got_opentemp");
5241 f2 = got_opentemp();
5243 error = got_error_from_errno("got_opentemp");
5247 outfile = got_opentemp();
5248 if (outfile == NULL) {
5249 error = got_error_from_errno("got_opentemp");
5253 if (ncommit_args == 0 && (ids[0] == NULL || ids[1] == NULL)) {
5254 struct print_diff_arg arg;
5257 if (worktree == NULL) {
5258 if (argc == 2 && ids[0] == NULL) {
5259 error = got_error_path(argv[0], GOT_ERR_NO_OBJ);
5261 } else if (argc == 2 && ids[1] == NULL) {
5262 error = got_error_path(argv[1], GOT_ERR_NO_OBJ);
5264 } else if (argc > 0) {
5265 error = got_error_fmt(GOT_ERR_NOT_WORKTREE,
5266 "%s", "specified paths cannot be resolved");
5269 error = got_error(GOT_ERR_NOT_WORKTREE);
5274 error = get_worktree_paths_from_argv(&paths, argc, argv,
5279 error = got_object_id_str(&id_str,
5280 got_worktree_get_base_commit_id(worktree));
5284 arg.worktree = worktree;
5285 arg.diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
5286 arg.diff_context = diff_context;
5287 arg.id_str = id_str;
5288 arg.header_shown = 0;
5289 arg.diff_staged = diff_staged;
5290 arg.ignore_whitespace = ignore_whitespace;
5291 arg.force_text_diff = force_text_diff;
5292 arg.diffstat = show_diffstat ? &dsa : NULL;
5295 arg.outfile = outfile;
5297 error = got_worktree_status(worktree, &paths, repo, 0,
5298 print_diff, &arg, check_cancelled, NULL);
5303 if (show_diffstat && dsa.nfiles > 0) {
5306 if (asprintf(&header, "diffstat %s%s",
5307 diff_staged ? "-s " : "",
5308 got_worktree_get_root_path(worktree)) == -1) {
5309 error = got_error_from_errno("asprintf");
5313 error = print_diffstat(&dsa, header);
5319 error = printfile(outfile);
5323 if (ncommit_args == 1) {
5324 struct got_commit_object *commit;
5325 error = got_object_open_as_commit(&commit, repo, ids[0]);
5329 labels[1] = labels[0];
5331 if (got_object_commit_get_nparents(commit) > 0) {
5332 const struct got_object_id_queue *pids;
5333 struct got_object_qid *pid;
5334 pids = got_object_commit_get_parent_ids(commit);
5335 pid = STAILQ_FIRST(pids);
5336 ids[0] = got_object_id_dup(&pid->id);
5337 if (ids[0] == NULL) {
5338 error = got_error_from_errno(
5339 "got_object_id_dup");
5340 got_object_commit_close(commit);
5343 error = got_object_id_str(&labels[0], ids[0]);
5345 got_object_commit_close(commit);
5350 labels[0] = strdup("/dev/null");
5351 if (labels[0] == NULL) {
5352 error = got_error_from_errno("strdup");
5353 got_object_commit_close(commit);
5358 got_object_commit_close(commit);
5361 if (ncommit_args == 0 && argc > 2) {
5362 error = got_error_msg(GOT_ERR_BAD_PATH,
5363 "path arguments cannot be used when diffing two objects");
5368 error = got_object_get_type(&type1, repo, ids[0]);
5373 error = got_object_get_type(&type2, repo, ids[1]);
5376 if (type1 != GOT_OBJ_TYPE_ANY && type1 != type2) {
5377 error = got_error(GOT_ERR_OBJ_TYPE);
5380 if (type1 == GOT_OBJ_TYPE_BLOB && argc > 2) {
5381 error = got_error_msg(GOT_ERR_OBJ_TYPE,
5382 "path arguments cannot be used when diffing blobs");
5386 for (i = 0; ncommit_args > 0 && i < argc; i++) {
5388 struct got_pathlist_entry *new;
5392 error = got_worktree_resolve_path(&p, worktree,
5396 prefix = got_worktree_get_path_prefix(worktree);
5397 while (prefix[0] == '/')
5399 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5400 (p[0] != '\0' && prefix[0] != '\0') ? "/" : "",
5402 error = got_error_from_errno("asprintf");
5408 char *mapped_path, *s;
5409 error = got_repo_map_path(&mapped_path, repo, argv[i]);
5415 in_repo_path = strdup(s);
5416 if (in_repo_path == NULL) {
5417 error = got_error_from_errno("asprintf");
5424 error = got_pathlist_insert(&new, &paths, in_repo_path, NULL);
5425 if (error || new == NULL /* duplicate */)
5432 /* Release work tree lock. */
5433 got_worktree_close(worktree);
5437 fd1 = got_opentempfd();
5439 error = got_error_from_errno("got_opentempfd");
5443 fd2 = got_opentempfd();
5445 error = got_error_from_errno("got_opentempfd");
5449 switch (type1 == GOT_OBJ_TYPE_ANY ? type2 : type1) {
5450 case GOT_OBJ_TYPE_BLOB:
5451 error = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5452 fd1, fd2, ids[0], ids[1], NULL, NULL,
5453 GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
5454 ignore_whitespace, force_text_diff,
5455 show_diffstat ? &dsa : NULL, repo, outfile);
5457 case GOT_OBJ_TYPE_TREE:
5458 error = got_diff_objects_as_trees(NULL, NULL, f1, f2, fd1, fd2,
5459 ids[0], ids[1], &paths, "", "",
5460 GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
5461 ignore_whitespace, force_text_diff,
5462 show_diffstat ? &dsa : NULL, repo, outfile);
5464 case GOT_OBJ_TYPE_COMMIT:
5465 fprintf(outfile, "diff %s %s\n", labels[0], labels[1]);
5466 error = got_diff_objects_as_commits(NULL, NULL, f1, f2,
5467 fd1, fd2, ids[0], ids[1], &paths,
5468 GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
5469 ignore_whitespace, force_text_diff,
5470 show_diffstat ? &dsa : NULL, repo, outfile);
5473 error = got_error(GOT_ERR_OBJ_TYPE);
5478 if (show_diffstat && dsa.nfiles > 0) {
5479 char *header = NULL;
5481 if (asprintf(&header, "diffstat %s %s",
5482 labels[0], labels[1]) == -1) {
5483 error = got_error_from_errno("asprintf");
5487 error = print_diffstat(&dsa, header);
5493 error = printfile(outfile);
5501 got_worktree_close(worktree);
5503 const struct got_error *close_err = got_repo_close(repo);
5508 const struct got_error *pack_err =
5509 got_repo_pack_fds_close(pack_fds);
5513 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
5514 got_pathlist_free(&diffstat_paths, GOT_PATHLIST_FREE_ALL);
5515 got_ref_list_free(&refs);
5516 if (outfile && fclose(outfile) == EOF && error == NULL)
5517 error = got_error_from_errno("fclose");
5518 if (f1 && fclose(f1) == EOF && error == NULL)
5519 error = got_error_from_errno("fclose");
5520 if (f2 && fclose(f2) == EOF && error == NULL)
5521 error = got_error_from_errno("fclose");
5522 if (fd1 != -1 && close(fd1) == -1 && error == NULL)
5523 error = got_error_from_errno("close");
5524 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
5525 error = got_error_from_errno("close");
5533 "usage: %s blame [-c commit] [-r repository-path] path\n",
5542 char datebuf[11]; /* YYYY-MM-DD + NUL */
5545 struct blame_cb_args {
5546 struct blame_line *lines;
5550 off_t *line_offsets;
5552 struct got_repository *repo;
5555 static const struct got_error *
5556 blame_cb(void *arg, int nlines, int lineno,
5557 struct got_commit_object *commit, struct got_object_id *id)
5559 const struct got_error *err = NULL;
5560 struct blame_cb_args *a = arg;
5561 struct blame_line *bline;
5563 size_t linesize = 0;
5566 time_t committer_time;
5568 if (nlines != a->nlines ||
5569 (lineno != -1 && lineno < 1) || lineno > a->nlines)
5570 return got_error(GOT_ERR_RANGE);
5572 if (sigint_received)
5573 return got_error(GOT_ERR_ITER_COMPLETED);
5576 return NULL; /* no change in this commit */
5578 /* Annotate this line. */
5579 bline = &a->lines[lineno - 1];
5580 if (bline->annotated)
5582 err = got_object_id_str(&bline->id_str, id);
5586 bline->committer = strdup(got_object_commit_get_committer(commit));
5587 if (bline->committer == NULL) {
5588 err = got_error_from_errno("strdup");
5592 committer_time = got_object_commit_get_committer_time(commit);
5593 if (gmtime_r(&committer_time, &tm) == NULL)
5594 return got_error_from_errno("gmtime_r");
5595 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
5597 err = got_error(GOT_ERR_NO_SPACE);
5600 bline->annotated = 1;
5602 /* Print lines annotated so far. */
5603 bline = &a->lines[a->lineno_cur - 1];
5604 if (!bline->annotated)
5607 offset = a->line_offsets[a->lineno_cur - 1];
5608 if (fseeko(a->f, offset, SEEK_SET) == -1) {
5609 err = got_error_from_errno("fseeko");
5613 while (a->lineno_cur <= a->nlines && bline->annotated) {
5614 char *smallerthan, *at, *nl, *committer;
5617 if (getline(&line, &linesize, a->f) == -1) {
5619 err = got_error_from_errno("getline");
5623 committer = bline->committer;
5624 smallerthan = strchr(committer, '<');
5625 if (smallerthan && smallerthan[1] != '\0')
5626 committer = smallerthan + 1;
5627 at = strchr(committer, '@');
5630 len = strlen(committer);
5632 committer[8] = '\0';
5634 nl = strchr(line, '\n');
5637 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
5638 bline->id_str, bline->datebuf, committer, line);
5641 bline = &a->lines[a->lineno_cur - 1];
5648 static const struct got_error *
5649 cmd_blame(int argc, char *argv[])
5651 const struct got_error *error;
5652 struct got_repository *repo = NULL;
5653 struct got_worktree *worktree = NULL;
5654 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5655 char *link_target = NULL;
5656 struct got_object_id *obj_id = NULL;
5657 struct got_object_id *commit_id = NULL;
5658 struct got_commit_object *commit = NULL;
5659 struct got_blob_object *blob = NULL;
5660 char *commit_id_str = NULL;
5661 struct blame_cb_args bca;
5662 int ch, obj_type, i, fd1 = -1, fd2 = -1, fd3 = -1;
5664 int *pack_fds = NULL;
5665 FILE *f1 = NULL, *f2 = NULL;
5667 fd1 = got_opentempfd();
5669 return got_error_from_errno("got_opentempfd");
5671 memset(&bca, 0, sizeof(bca));
5674 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5679 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5682 commit_id_str = optarg;
5685 repo_path = realpath(optarg, NULL);
5686 if (repo_path == NULL)
5687 return got_error_from_errno2("realpath",
5689 got_path_strip_trailing_slashes(repo_path);
5705 cwd = getcwd(NULL, 0);
5707 error = got_error_from_errno("getcwd");
5711 error = got_repo_pack_fds_open(&pack_fds);
5715 if (repo_path == NULL) {
5716 error = got_worktree_open(&worktree, cwd);
5717 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5723 strdup(got_worktree_get_repo_path(worktree));
5724 if (repo_path == NULL) {
5725 error = got_error_from_errno("strdup");
5730 repo_path = strdup(cwd);
5731 if (repo_path == NULL) {
5732 error = got_error_from_errno("strdup");
5738 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5743 const char *prefix = got_worktree_get_path_prefix(worktree);
5746 error = got_worktree_resolve_path(&p, worktree, path);
5749 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5750 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
5752 error = got_error_from_errno("asprintf");
5757 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5759 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5762 error = got_repo_map_path(&in_repo_path, repo, path);
5767 if (commit_id_str == NULL) {
5768 struct got_reference *head_ref;
5769 error = got_ref_open(&head_ref, repo, worktree ?
5770 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5773 error = got_ref_resolve(&commit_id, repo, head_ref);
5774 got_ref_close(head_ref);
5778 struct got_reflist_head refs;
5780 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5784 error = got_repo_match_object_id(&commit_id, NULL,
5785 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5786 got_ref_list_free(&refs);
5792 /* Release work tree lock. */
5793 got_worktree_close(worktree);
5797 error = got_object_open_as_commit(&commit, repo, commit_id);
5801 error = got_object_resolve_symlinks(&link_target, in_repo_path,
5806 error = got_object_id_by_path(&obj_id, repo, commit,
5807 link_target ? link_target : in_repo_path);
5811 error = got_object_get_type(&obj_type, repo, obj_id);
5815 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5816 error = got_error_path(link_target ? link_target : in_repo_path,
5821 error = got_object_open_as_blob(&blob, repo, obj_id, 8192, fd1);
5824 bca.f = got_opentemp();
5825 if (bca.f == NULL) {
5826 error = got_error_from_errno("got_opentemp");
5829 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
5830 &bca.line_offsets, bca.f, blob);
5831 if (error || bca.nlines == 0)
5834 /* Don't include \n at EOF in the blame line count. */
5835 if (bca.line_offsets[bca.nlines - 1] == filesize)
5838 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
5839 if (bca.lines == NULL) {
5840 error = got_error_from_errno("calloc");
5844 bca.nlines_prec = 0;
5852 fd2 = got_opentempfd();
5854 error = got_error_from_errno("got_opentempfd");
5857 fd3 = got_opentempfd();
5859 error = got_error_from_errno("got_opentempfd");
5862 f1 = got_opentemp();
5864 error = got_error_from_errno("got_opentemp");
5867 f2 = got_opentemp();
5869 error = got_error_from_errno("got_opentemp");
5872 error = got_blame(link_target ? link_target : in_repo_path, commit_id,
5873 repo, GOT_DIFF_ALGORITHM_PATIENCE, blame_cb, &bca,
5874 check_cancelled, NULL, fd2, fd3, f1, f2);
5883 got_object_commit_close(commit);
5885 if (fd1 != -1 && close(fd1) == -1 && error == NULL)
5886 error = got_error_from_errno("close");
5887 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
5888 error = got_error_from_errno("close");
5889 if (fd3 != -1 && close(fd3) == -1 && error == NULL)
5890 error = got_error_from_errno("close");
5891 if (f1 && fclose(f1) == EOF && error == NULL)
5892 error = got_error_from_errno("fclose");
5893 if (f2 && fclose(f2) == EOF && error == NULL)
5894 error = got_error_from_errno("fclose");
5897 got_object_blob_close(blob);
5899 got_worktree_close(worktree);
5901 const struct got_error *close_err = got_repo_close(repo);
5906 const struct got_error *pack_err =
5907 got_repo_pack_fds_close(pack_fds);
5912 for (i = 0; i < bca.nlines; i++) {
5913 struct blame_line *bline = &bca.lines[i];
5914 free(bline->id_str);
5915 free(bline->committer);
5919 free(bca.line_offsets);
5920 if (bca.f && fclose(bca.f) == EOF && error == NULL)
5921 error = got_error_from_errno("fclose");
5928 fprintf(stderr, "usage: %s tree [-iR] [-c commit] [-r repository-path] "
5929 "[path]\n", getprogname());
5933 static const struct got_error *
5934 print_entry(struct got_tree_entry *te, const char *id, const char *path,
5935 const char *root_path, struct got_repository *repo)
5937 const struct got_error *err = NULL;
5938 int is_root_path = (strcmp(path, root_path) == 0);
5939 const char *modestr = "";
5940 mode_t mode = got_tree_entry_get_mode(te);
5941 char *link_target = NULL;
5943 path += strlen(root_path);
5944 while (path[0] == '/')
5947 if (got_object_tree_entry_is_submodule(te))
5949 else if (S_ISLNK(mode)) {
5952 err = got_tree_entry_get_symlink_target(&link_target, te, repo);
5955 for (i = 0; i < strlen(link_target); i++) {
5956 if (!isprint((unsigned char)link_target[i]))
5957 link_target[i] = '?';
5962 else if (S_ISDIR(mode))
5964 else if (mode & S_IXUSR)
5967 printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
5968 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
5969 link_target ? " -> ": "", link_target ? link_target : "");
5975 static const struct got_error *
5976 print_tree(const char *path, struct got_commit_object *commit,
5977 int show_ids, int recurse, const char *root_path,
5978 struct got_repository *repo)
5980 const struct got_error *err = NULL;
5981 struct got_object_id *tree_id = NULL;
5982 struct got_tree_object *tree = NULL;
5985 err = got_object_id_by_path(&tree_id, repo, commit, path);
5989 err = got_object_open_as_tree(&tree, repo, tree_id);
5992 nentries = got_object_tree_get_nentries(tree);
5993 for (i = 0; i < nentries; i++) {
5994 struct got_tree_entry *te;
5997 if (sigint_received || sigpipe_received)
6000 te = got_object_tree_get_entry(tree, i);
6003 err = got_object_id_str(&id_str,
6004 got_tree_entry_get_id(te));
6007 if (asprintf(&id, "%s ", id_str) == -1) {
6008 err = got_error_from_errno("asprintf");
6014 err = print_entry(te, id, path, root_path, repo);
6019 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
6021 if (asprintf(&child_path, "%s%s%s", path,
6022 path[0] == '/' && path[1] == '\0' ? "" : "/",
6023 got_tree_entry_get_name(te)) == -1) {
6024 err = got_error_from_errno("asprintf");
6027 err = print_tree(child_path, commit, show_ids, 1,
6036 got_object_tree_close(tree);
6041 static const struct got_error *
6042 cmd_tree(int argc, char *argv[])
6044 const struct got_error *error;
6045 struct got_repository *repo = NULL;
6046 struct got_worktree *worktree = NULL;
6047 const char *path, *refname = NULL;
6048 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6049 struct got_object_id *commit_id = NULL;
6050 struct got_commit_object *commit = NULL;
6051 char *commit_id_str = NULL;
6052 int show_ids = 0, recurse = 0;
6054 int *pack_fds = NULL;
6057 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6062 while ((ch = getopt(argc, argv, "c:iRr:")) != -1) {
6065 commit_id_str = optarg;
6074 repo_path = realpath(optarg, NULL);
6075 if (repo_path == NULL)
6076 return got_error_from_errno2("realpath",
6078 got_path_strip_trailing_slashes(repo_path);
6096 cwd = getcwd(NULL, 0);
6098 error = got_error_from_errno("getcwd");
6102 error = got_repo_pack_fds_open(&pack_fds);
6106 if (repo_path == NULL) {
6107 error = got_worktree_open(&worktree, cwd);
6108 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6114 strdup(got_worktree_get_repo_path(worktree));
6115 if (repo_path == NULL)
6116 error = got_error_from_errno("strdup");
6120 repo_path = strdup(cwd);
6121 if (repo_path == NULL) {
6122 error = got_error_from_errno("strdup");
6128 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6133 const char *prefix = got_worktree_get_path_prefix(worktree);
6138 error = got_worktree_resolve_path(&p, worktree, path);
6141 if (asprintf(&in_repo_path, "%s%s%s", prefix,
6142 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
6144 error = got_error_from_errno("asprintf");
6149 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6153 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6158 error = got_repo_map_path(&in_repo_path, repo, path);
6163 if (commit_id_str == NULL) {
6164 struct got_reference *head_ref;
6166 refname = got_worktree_get_head_ref_name(worktree);
6168 refname = GOT_REF_HEAD;
6169 error = got_ref_open(&head_ref, repo, refname, 0);
6172 error = got_ref_resolve(&commit_id, repo, head_ref);
6173 got_ref_close(head_ref);
6177 struct got_reflist_head refs;
6179 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
6183 error = got_repo_match_object_id(&commit_id, NULL,
6184 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
6185 got_ref_list_free(&refs);
6191 /* Release work tree lock. */
6192 got_worktree_close(worktree);
6196 error = got_object_open_as_commit(&commit, repo, commit_id);
6200 error = print_tree(in_repo_path, commit, show_ids, recurse,
6201 in_repo_path, repo);
6208 got_object_commit_close(commit);
6210 got_worktree_close(worktree);
6212 const struct got_error *close_err = got_repo_close(repo);
6217 const struct got_error *pack_err =
6218 got_repo_pack_fds_close(pack_fds);
6228 fprintf(stderr, "usage: %s status [-I] [-S status-codes] "
6229 "[-s status-codes] [path ...]\n", getprogname());
6233 struct got_status_arg {
6238 static const struct got_error *
6239 print_status(void *arg, unsigned char status, unsigned char staged_status,
6240 const char *path, struct got_object_id *blob_id,
6241 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6242 int dirfd, const char *de_name)
6244 struct got_status_arg *st = arg;
6246 if (status == staged_status && (status == GOT_STATUS_DELETE))
6247 status = GOT_STATUS_NO_CHANGE;
6248 if (st != NULL && st->status_codes) {
6249 size_t ncodes = strlen(st->status_codes);
6252 for (i = 0; i < ncodes ; i++) {
6254 if (status == st->status_codes[i] ||
6255 staged_status == st->status_codes[i]) {
6260 if (status == st->status_codes[i] ||
6261 staged_status == st->status_codes[i])
6266 if (st->suppress && j == 0)
6273 printf("%c%c %s\n", status, staged_status, path);
6277 static const struct got_error *
6278 cmd_status(int argc, char *argv[])
6280 const struct got_error *error = NULL;
6281 struct got_repository *repo = NULL;
6282 struct got_worktree *worktree = NULL;
6283 struct got_status_arg st;
6285 struct got_pathlist_head paths;
6286 int ch, i, no_ignores = 0;
6287 int *pack_fds = NULL;
6291 memset(&st, 0, sizeof(st));
6292 st.status_codes = NULL;
6295 while ((ch = getopt(argc, argv, "IS:s:")) != -1) {
6301 if (st.status_codes != NULL && st.suppress == 0)
6302 option_conflict('S', 's');
6306 for (i = 0; i < strlen(optarg); i++) {
6307 switch (optarg[i]) {
6308 case GOT_STATUS_MODIFY:
6309 case GOT_STATUS_ADD:
6310 case GOT_STATUS_DELETE:
6311 case GOT_STATUS_CONFLICT:
6312 case GOT_STATUS_MISSING:
6313 case GOT_STATUS_OBSTRUCTED:
6314 case GOT_STATUS_UNVERSIONED:
6315 case GOT_STATUS_MODE_CHANGE:
6316 case GOT_STATUS_NONEXISTENT:
6319 errx(1, "invalid status code '%c'",
6323 if (ch == 's' && st.suppress)
6324 option_conflict('s', 'S');
6325 st.status_codes = optarg;
6337 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6341 cwd = getcwd(NULL, 0);
6343 error = got_error_from_errno("getcwd");
6347 error = got_repo_pack_fds_open(&pack_fds);
6351 error = got_worktree_open(&worktree, cwd);
6353 if (error->code == GOT_ERR_NOT_WORKTREE)
6354 error = wrap_not_worktree_error(error, "status", cwd);
6358 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6363 error = apply_unveil(got_repo_get_path(repo), 1,
6364 got_worktree_get_root_path(worktree));
6368 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6372 error = got_worktree_status(worktree, &paths, repo, no_ignores,
6373 print_status, &st, check_cancelled, NULL);
6376 const struct got_error *pack_err =
6377 got_repo_pack_fds_close(pack_fds);
6382 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
6390 fprintf(stderr, "usage: %s ref [-dlt] [-c object] [-r repository-path] "
6391 "[-s reference] [name]\n", getprogname());
6395 static const struct got_error *
6396 list_refs(struct got_repository *repo, const char *refname, int sort_by_time)
6398 static const struct got_error *err = NULL;
6399 struct got_reflist_head refs;
6400 struct got_reflist_entry *re;
6403 err = got_ref_list(&refs, repo, refname, sort_by_time ?
6404 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6409 TAILQ_FOREACH(re, &refs, entry) {
6411 refstr = got_ref_to_str(re->ref);
6412 if (refstr == NULL) {
6413 err = got_error_from_errno("got_ref_to_str");
6416 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
6420 got_ref_list_free(&refs);
6424 static const struct got_error *
6425 delete_ref_by_name(struct got_repository *repo, const char *refname)
6427 const struct got_error *err;
6428 struct got_reference *ref;
6430 err = got_ref_open(&ref, repo, refname, 0);
6434 err = delete_ref(repo, ref);
6439 static const struct got_error *
6440 add_ref(struct got_repository *repo, const char *refname, const char *target)
6442 const struct got_error *err = NULL;
6443 struct got_object_id *id = NULL;
6444 struct got_reference *ref = NULL;
6445 struct got_reflist_head refs;
6448 * Don't let the user create a reference name with a leading '-'.
6449 * While technically a valid reference name, this case is usually
6450 * an unintended typo.
6452 if (refname[0] == '-')
6453 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
6456 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
6459 err = got_repo_match_object_id(&id, NULL, target, GOT_OBJ_TYPE_ANY,
6461 got_ref_list_free(&refs);
6465 err = got_ref_alloc(&ref, refname, id);
6469 err = got_ref_write(ref, repo);
6477 static const struct got_error *
6478 add_symref(struct got_repository *repo, const char *refname, const char *target)
6480 const struct got_error *err = NULL;
6481 struct got_reference *ref = NULL;
6482 struct got_reference *target_ref = NULL;
6485 * Don't let the user create a reference name with a leading '-'.
6486 * While technically a valid reference name, this case is usually
6487 * an unintended typo.
6489 if (refname[0] == '-')
6490 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
6492 err = got_ref_open(&target_ref, repo, target, 0);
6496 err = got_ref_alloc_symref(&ref, refname, target_ref);
6500 err = got_ref_write(ref, repo);
6503 got_ref_close(target_ref);
6509 static const struct got_error *
6510 cmd_ref(int argc, char *argv[])
6512 const struct got_error *error = NULL;
6513 struct got_repository *repo = NULL;
6514 struct got_worktree *worktree = NULL;
6515 char *cwd = NULL, *repo_path = NULL;
6516 int ch, do_list = 0, do_delete = 0, sort_by_time = 0;
6517 const char *obj_arg = NULL, *symref_target= NULL;
6518 char *refname = NULL;
6519 int *pack_fds = NULL;
6521 while ((ch = getopt(argc, argv, "c:dlr:s:t")) != -1) {
6533 repo_path = realpath(optarg, NULL);
6534 if (repo_path == NULL)
6535 return got_error_from_errno2("realpath",
6537 got_path_strip_trailing_slashes(repo_path);
6540 symref_target = optarg;
6551 if (obj_arg && do_list)
6552 option_conflict('c', 'l');
6553 if (obj_arg && do_delete)
6554 option_conflict('c', 'd');
6555 if (obj_arg && symref_target)
6556 option_conflict('c', 's');
6557 if (symref_target && do_delete)
6558 option_conflict('s', 'd');
6559 if (symref_target && do_list)
6560 option_conflict('s', 'l');
6561 if (do_delete && do_list)
6562 option_conflict('d', 'l');
6563 if (sort_by_time && !do_list)
6564 errx(1, "-t option requires -l option");
6570 if (argc != 0 && argc != 1)
6573 refname = strdup(argv[0]);
6574 if (refname == NULL) {
6575 error = got_error_from_errno("strdup");
6582 refname = strdup(argv[0]);
6583 if (refname == NULL) {
6584 error = got_error_from_errno("strdup");
6590 got_path_strip_trailing_slashes(refname);
6593 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6594 "sendfd unveil", NULL) == -1)
6597 cwd = getcwd(NULL, 0);
6599 error = got_error_from_errno("getcwd");
6603 error = got_repo_pack_fds_open(&pack_fds);
6607 if (repo_path == NULL) {
6608 error = got_worktree_open(&worktree, cwd);
6609 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6615 strdup(got_worktree_get_repo_path(worktree));
6616 if (repo_path == NULL)
6617 error = got_error_from_errno("strdup");
6621 repo_path = strdup(cwd);
6622 if (repo_path == NULL) {
6623 error = got_error_from_errno("strdup");
6629 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6635 /* Remove "cpath" promise. */
6636 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6642 error = apply_unveil(got_repo_get_path(repo), do_list,
6643 worktree ? got_worktree_get_root_path(worktree) : NULL);
6648 error = list_refs(repo, refname, sort_by_time);
6650 error = delete_ref_by_name(repo, refname);
6651 else if (symref_target)
6652 error = add_symref(repo, refname, symref_target);
6654 if (obj_arg == NULL)
6656 error = add_ref(repo, refname, obj_arg);
6661 const struct got_error *close_err = got_repo_close(repo);
6666 got_worktree_close(worktree);
6668 const struct got_error *pack_err =
6669 got_repo_pack_fds_close(pack_fds);
6681 fprintf(stderr, "usage: %s branch [-lnt] [-c commit] [-d name] "
6682 "[-r repository-path] [name]\n", getprogname());
6686 static const struct got_error *
6687 list_branch(struct got_repository *repo, struct got_worktree *worktree,
6688 struct got_reference *ref)
6690 const struct got_error *err = NULL;
6691 const char *refname, *marker = " ";
6694 refname = got_ref_get_name(ref);
6695 if (worktree && strcmp(refname,
6696 got_worktree_get_head_ref_name(worktree)) == 0) {
6697 struct got_object_id *id = NULL;
6699 err = got_ref_resolve(&id, repo, ref);
6702 if (got_object_id_cmp(id,
6703 got_worktree_get_base_commit_id(worktree)) == 0)
6710 if (strncmp(refname, "refs/heads/", 11) == 0)
6712 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
6714 if (strncmp(refname, "refs/remotes/", 13) == 0)
6717 refstr = got_ref_to_str(ref);
6719 return got_error_from_errno("got_ref_to_str");
6721 printf("%s%s: %s\n", marker, refname, refstr);
6726 static const struct got_error *
6727 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
6729 const char *refname;
6731 if (worktree == NULL)
6732 return got_error(GOT_ERR_NOT_WORKTREE);
6734 refname = got_worktree_get_head_ref_name(worktree);
6736 if (strncmp(refname, "refs/heads/", 11) == 0)
6738 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
6741 printf("%s\n", refname);
6746 static const struct got_error *
6747 list_branches(struct got_repository *repo, struct got_worktree *worktree,
6750 static const struct got_error *err = NULL;
6751 struct got_reflist_head refs;
6752 struct got_reflist_entry *re;
6753 struct got_reference *temp_ref = NULL;
6754 int rebase_in_progress, histedit_in_progress;
6759 err = got_worktree_rebase_in_progress(&rebase_in_progress,
6764 err = got_worktree_histedit_in_progress(&histedit_in_progress,
6769 if (rebase_in_progress || histedit_in_progress) {
6770 err = got_ref_open(&temp_ref, repo,
6771 got_worktree_get_head_ref_name(worktree), 0);
6774 list_branch(repo, worktree, temp_ref);
6775 got_ref_close(temp_ref);
6779 err = got_ref_list(&refs, repo, "refs/heads", sort_by_time ?
6780 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6785 TAILQ_FOREACH(re, &refs, entry)
6786 list_branch(repo, worktree, re->ref);
6788 got_ref_list_free(&refs);
6790 err = got_ref_list(&refs, repo, "refs/remotes", sort_by_time ?
6791 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6796 TAILQ_FOREACH(re, &refs, entry)
6797 list_branch(repo, worktree, re->ref);
6799 got_ref_list_free(&refs);
6804 static const struct got_error *
6805 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
6806 const char *branch_name)
6808 const struct got_error *err = NULL;
6809 struct got_reference *ref = NULL;
6810 char *refname, *remote_refname = NULL;
6812 if (strncmp(branch_name, "refs/", 5) == 0)
6814 if (strncmp(branch_name, "heads/", 6) == 0)
6816 else if (strncmp(branch_name, "remotes/", 8) == 0)
6819 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
6820 return got_error_from_errno("asprintf");
6822 if (asprintf(&remote_refname, "refs/remotes/%s",
6823 branch_name) == -1) {
6824 err = got_error_from_errno("asprintf");
6828 err = got_ref_open(&ref, repo, refname, 0);
6830 const struct got_error *err2;
6831 if (err->code != GOT_ERR_NOT_REF)
6834 * Keep 'err' intact such that if neither branch exists
6835 * we report "refs/heads" rather than "refs/remotes" in
6836 * our error message.
6838 err2 = got_ref_open(&ref, repo, remote_refname, 0);
6845 strcmp(got_worktree_get_head_ref_name(worktree),
6846 got_ref_get_name(ref)) == 0) {
6847 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6848 "will not delete this work tree's current branch");
6852 err = delete_ref(repo, ref);
6857 free(remote_refname);
6861 static const struct got_error *
6862 add_branch(struct got_repository *repo, const char *branch_name,
6863 struct got_object_id *base_commit_id)
6865 const struct got_error *err = NULL;
6866 struct got_reference *ref = NULL;
6867 char *refname = NULL;
6870 * Don't let the user create a branch name with a leading '-'.
6871 * While technically a valid reference name, this case is usually
6872 * an unintended typo.
6874 if (branch_name[0] == '-')
6875 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
6877 if (strncmp(branch_name, "refs/heads/", 11) == 0)
6880 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
6881 err = got_error_from_errno("asprintf");
6885 err = got_ref_open(&ref, repo, refname, 0);
6887 err = got_error(GOT_ERR_BRANCH_EXISTS);
6889 } else if (err->code != GOT_ERR_NOT_REF)
6892 err = got_ref_alloc(&ref, refname, base_commit_id);
6896 err = got_ref_write(ref, repo);
6904 static const struct got_error *
6905 cmd_branch(int argc, char *argv[])
6907 const struct got_error *error = NULL;
6908 struct got_repository *repo = NULL;
6909 struct got_worktree *worktree = NULL;
6910 char *cwd = NULL, *repo_path = NULL;
6911 int ch, do_list = 0, do_show = 0, do_update = 1, sort_by_time = 0;
6912 const char *delref = NULL, *commit_id_arg = NULL;
6913 struct got_reference *ref = NULL;
6914 struct got_pathlist_head paths;
6915 struct got_object_id *commit_id = NULL;
6916 char *commit_id_str = NULL;
6917 int *pack_fds = NULL;
6921 while ((ch = getopt(argc, argv, "c:d:lnr:t")) != -1) {
6924 commit_id_arg = optarg;
6936 repo_path = realpath(optarg, NULL);
6937 if (repo_path == NULL)
6938 return got_error_from_errno2("realpath",
6940 got_path_strip_trailing_slashes(repo_path);
6951 if (do_list && delref)
6952 option_conflict('l', 'd');
6953 if (sort_by_time && !do_list)
6954 errx(1, "-t option requires -l option");
6959 if (!do_list && !delref && argc == 0)
6962 if ((do_list || delref || do_show) && commit_id_arg != NULL)
6963 errx(1, "-c option can only be used when creating a branch");
6965 if (do_list || delref) {
6968 } else if (!do_show && argc != 1)
6972 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6973 "sendfd unveil", NULL) == -1)
6976 cwd = getcwd(NULL, 0);
6978 error = got_error_from_errno("getcwd");
6982 error = got_repo_pack_fds_open(&pack_fds);
6986 if (repo_path == NULL) {
6987 error = got_worktree_open(&worktree, cwd);
6988 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6994 strdup(got_worktree_get_repo_path(worktree));
6995 if (repo_path == NULL)
6996 error = got_error_from_errno("strdup");
7000 repo_path = strdup(cwd);
7001 if (repo_path == NULL) {
7002 error = got_error_from_errno("strdup");
7008 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7013 if (do_list || do_show) {
7014 /* Remove "cpath" promise. */
7015 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
7021 error = apply_unveil(got_repo_get_path(repo), do_list,
7022 worktree ? got_worktree_get_root_path(worktree) : NULL);
7027 error = show_current_branch(repo, worktree);
7029 error = list_branches(repo, worktree, sort_by_time);
7031 error = delete_branch(repo, worktree, delref);
7033 struct got_reflist_head refs;
7035 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
7039 if (commit_id_arg == NULL)
7040 commit_id_arg = worktree ?
7041 got_worktree_get_head_ref_name(worktree) :
7043 error = got_repo_match_object_id(&commit_id, NULL,
7044 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &refs, repo);
7045 got_ref_list_free(&refs);
7048 error = add_branch(repo, argv[0], commit_id);
7051 if (worktree && do_update) {
7052 struct got_update_progress_arg upa;
7053 char *branch_refname = NULL;
7055 error = got_object_id_str(&commit_id_str, commit_id);
7058 error = get_worktree_paths_from_argv(&paths, 0, NULL,
7062 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
7064 error = got_error_from_errno("asprintf");
7067 error = got_ref_open(&ref, repo, branch_refname, 0);
7068 free(branch_refname);
7071 error = switch_head_ref(ref, commit_id, worktree,
7075 error = got_worktree_set_base_commit_id(worktree, repo,
7079 memset(&upa, 0, sizeof(upa));
7080 error = got_worktree_checkout_files(worktree, &paths,
7081 repo, update_progress, &upa, check_cancelled,
7085 if (upa.did_something) {
7086 printf("Updated to %s: %s\n",
7087 got_worktree_get_head_ref_name(worktree),
7090 print_update_progress_stats(&upa);
7097 const struct got_error *close_err = got_repo_close(repo);
7102 got_worktree_close(worktree);
7104 const struct got_error *pack_err =
7105 got_repo_pack_fds_close(pack_fds);
7112 free(commit_id_str);
7113 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
7121 fprintf(stderr, "usage: %s tag [-lVv] [-c commit] [-m message] "
7122 "[-r repository-path] [-s signer-id] name\n", getprogname());
7127 static const struct got_error *
7128 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
7130 const struct got_error *err = NULL;
7131 struct got_reflist_entry *re, *se, *new;
7132 struct got_object_id *re_id, *se_id;
7133 struct got_tag_object *re_tag, *se_tag;
7134 time_t re_time, se_time;
7136 STAILQ_FOREACH(re, tags, entry) {
7137 se = STAILQ_FIRST(sorted);
7139 err = got_reflist_entry_dup(&new, re);
7142 STAILQ_INSERT_HEAD(sorted, new, entry);
7145 err = got_ref_resolve(&re_id, repo, re->ref);
7148 err = got_object_open_as_tag(&re_tag, repo, re_id);
7152 re_time = got_object_tag_get_tagger_time(re_tag);
7153 got_object_tag_close(re_tag);
7157 err = got_ref_resolve(&se_id, repo, re->ref);
7160 err = got_object_open_as_tag(&se_tag, repo, se_id);
7164 se_time = got_object_tag_get_tagger_time(se_tag);
7165 got_object_tag_close(se_tag);
7167 if (se_time > re_time) {
7168 err = got_reflist_entry_dup(&new, re);
7171 STAILQ_INSERT_AFTER(sorted, se, new, entry);
7174 se = STAILQ_NEXT(se, entry);
7183 static const struct got_error *
7184 get_tag_refname(char **refname, const char *tag_name)
7186 const struct got_error *err;
7188 if (strncmp("refs/tags/", tag_name, 10) == 0) {
7189 *refname = strdup(tag_name);
7190 if (*refname == NULL)
7191 return got_error_from_errno("strdup");
7192 } else if (asprintf(refname, "refs/tags/%s", tag_name) == -1) {
7193 err = got_error_from_errno("asprintf");
7201 static const struct got_error *
7202 list_tags(struct got_repository *repo, const char *tag_name, int verify_tags,
7203 const char *allowed_signers, const char *revoked_signers, int verbosity)
7205 static const struct got_error *err = NULL;
7206 struct got_reflist_head refs;
7207 struct got_reflist_entry *re;
7208 char *wanted_refname = NULL;
7213 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
7218 struct got_reference *ref;
7219 err = get_tag_refname(&wanted_refname, tag_name);
7222 /* Wanted tag reference should exist. */
7223 err = got_ref_open(&ref, repo, wanted_refname, 0);
7229 TAILQ_FOREACH(re, &refs, entry) {
7230 const char *refname;
7231 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
7233 const char *tagger, *ssh_sig = NULL;
7234 char *sig_msg = NULL;
7236 struct got_object_id *id;
7237 struct got_tag_object *tag;
7238 struct got_commit_object *commit = NULL;
7240 refname = got_ref_get_name(re->ref);
7241 if (strncmp(refname, "refs/tags/", 10) != 0 ||
7242 (wanted_refname && strcmp(refname, wanted_refname) != 0))
7245 refstr = got_ref_to_str(re->ref);
7246 if (refstr == NULL) {
7247 err = got_error_from_errno("got_ref_to_str");
7251 err = got_ref_resolve(&id, repo, re->ref);
7254 err = got_object_open_as_tag(&tag, repo, id);
7256 if (err->code != GOT_ERR_OBJ_TYPE) {
7260 /* "lightweight" tag */
7261 err = got_object_open_as_commit(&commit, repo, id);
7266 tagger = got_object_commit_get_committer(commit);
7268 got_object_commit_get_committer_time(commit);
7269 err = got_object_id_str(&id_str, id);
7275 tagger = got_object_tag_get_tagger(tag);
7276 tagger_time = got_object_tag_get_tagger_time(tag);
7277 err = got_object_id_str(&id_str,
7278 got_object_tag_get_object_id(tag));
7283 if (tag && verify_tags) {
7284 ssh_sig = got_sigs_get_tagmsg_ssh_signature(
7285 got_object_tag_get_message(tag));
7286 if (ssh_sig && allowed_signers == NULL) {
7287 err = got_error_msg(
7288 GOT_ERR_VERIFY_TAG_SIGNATURE,
7289 "SSH signature verification requires "
7290 "setting allowed_signers in "
7296 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
7298 printf("from: %s\n", tagger);
7299 datestr = get_datestr(&tagger_time, datebuf);
7301 printf("date: %s UTC\n", datestr);
7303 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
7305 switch (got_object_tag_get_object_type(tag)) {
7306 case GOT_OBJ_TYPE_BLOB:
7307 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
7310 case GOT_OBJ_TYPE_TREE:
7311 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
7314 case GOT_OBJ_TYPE_COMMIT:
7315 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
7318 case GOT_OBJ_TYPE_TAG:
7319 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
7329 err = got_sigs_verify_tag_ssh(&sig_msg, tag, ssh_sig,
7330 allowed_signers, revoked_signers, verbosity);
7331 if (err && err->code == GOT_ERR_BAD_TAG_SIGNATURE)
7335 printf("signature: %s", sig_msg);
7341 err = got_object_commit_get_logmsg(&tagmsg0, commit);
7344 got_object_commit_close(commit);
7346 tagmsg0 = strdup(got_object_tag_get_message(tag));
7347 got_object_tag_close(tag);
7348 if (tagmsg0 == NULL) {
7349 err = got_error_from_errno("strdup");
7356 line = strsep(&tagmsg, "\n");
7358 printf(" %s\n", line);
7363 got_ref_list_free(&refs);
7364 free(wanted_refname);
7366 if (err == NULL && bad_sigs)
7367 err = got_error(GOT_ERR_BAD_TAG_SIGNATURE);
7371 static const struct got_error *
7372 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
7373 const char *tag_name, const char *repo_path)
7375 const struct got_error *err = NULL;
7376 char *template = NULL, *initial_content = NULL;
7377 char *editor = NULL;
7378 int initial_content_len;
7381 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
7382 err = got_error_from_errno("asprintf");
7386 initial_content_len = asprintf(&initial_content,
7387 "\n# tagging commit %s as %s\n",
7388 commit_id_str, tag_name);
7389 if (initial_content_len == -1) {
7390 err = got_error_from_errno("asprintf");
7394 err = got_opentemp_named_fd(tagmsg_path, &fd, template, "");
7398 if (write(fd, initial_content, initial_content_len) == -1) {
7399 err = got_error_from_errno2("write", *tagmsg_path);
7403 err = get_editor(&editor);
7406 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content,
7407 initial_content_len, 1);
7409 free(initial_content);
7413 if (fd != -1 && close(fd) == -1 && err == NULL)
7414 err = got_error_from_errno2("close", *tagmsg_path);
7423 static const struct got_error *
7424 add_tag(struct got_repository *repo, const char *tagger,
7425 const char *tag_name, const char *commit_arg, const char *tagmsg_arg,
7426 const char *signer_id, int verbosity)
7428 const struct got_error *err = NULL;
7429 struct got_object_id *commit_id = NULL, *tag_id = NULL;
7430 char *label = NULL, *commit_id_str = NULL;
7431 struct got_reference *ref = NULL;
7432 char *refname = NULL, *tagmsg = NULL;
7433 char *tagmsg_path = NULL, *tag_id_str = NULL;
7434 int preserve_tagmsg = 0;
7435 struct got_reflist_head refs;
7440 * Don't let the user create a tag name with a leading '-'.
7441 * While technically a valid reference name, this case is usually
7442 * an unintended typo.
7444 if (tag_name[0] == '-')
7445 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
7447 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
7451 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
7452 GOT_OBJ_TYPE_COMMIT, &refs, repo);
7456 err = got_object_id_str(&commit_id_str, commit_id);
7460 err = get_tag_refname(&refname, tag_name);
7463 if (strncmp("refs/tags/", tag_name, 10) == 0)
7466 err = got_ref_open(&ref, repo, refname, 0);
7468 err = got_error(GOT_ERR_TAG_EXISTS);
7470 } else if (err->code != GOT_ERR_NOT_REF)
7473 if (tagmsg_arg == NULL) {
7474 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
7475 tag_name, got_repo_get_path(repo));
7477 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
7478 tagmsg_path != NULL)
7479 preserve_tagmsg = 1;
7482 /* Editor is done; we can now apply unveil(2) */
7483 err = got_sigs_apply_unveil();
7486 err = apply_unveil(got_repo_get_path(repo), 0, NULL);
7491 err = got_object_tag_create(&tag_id, tag_name, commit_id,
7492 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, signer_id, repo,
7496 preserve_tagmsg = 1;
7500 err = got_ref_alloc(&ref, refname, tag_id);
7503 preserve_tagmsg = 1;
7507 err = got_ref_write(ref, repo);
7510 preserve_tagmsg = 1;
7514 err = got_object_id_str(&tag_id_str, tag_id);
7517 preserve_tagmsg = 1;
7520 printf("Created tag %s\n", tag_id_str);
7522 if (preserve_tagmsg) {
7523 fprintf(stderr, "%s: tag message preserved in %s\n",
7524 getprogname(), tagmsg_path);
7525 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
7526 err = got_error_from_errno2("unlink", tagmsg_path);
7531 free(commit_id_str);
7535 got_ref_list_free(&refs);
7539 static const struct got_error *
7540 cmd_tag(int argc, char *argv[])
7542 const struct got_error *error = NULL;
7543 struct got_repository *repo = NULL;
7544 struct got_worktree *worktree = NULL;
7545 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
7546 char *gitconfig_path = NULL, *tagger = NULL;
7547 char *allowed_signers = NULL, *revoked_signers = NULL;
7548 const char *signer_id = NULL;
7549 const char *tag_name = NULL, *commit_id_arg = NULL, *tagmsg = NULL;
7550 int ch, do_list = 0, verify_tags = 0, verbosity = 0;
7551 int *pack_fds = NULL;
7553 while ((ch = getopt(argc, argv, "c:lm:r:s:Vv")) != -1) {
7556 commit_id_arg = optarg;
7565 repo_path = realpath(optarg, NULL);
7566 if (repo_path == NULL) {
7567 error = got_error_from_errno2("realpath",
7571 got_path_strip_trailing_slashes(repo_path);
7582 else if (verbosity < 3)
7594 if (do_list || verify_tags) {
7595 if (commit_id_arg != NULL)
7597 "-c option can only be used when creating a tag");
7600 option_conflict('l', 'm');
7602 option_conflict('V', 'm');
7606 option_conflict('l', 's');
7608 option_conflict('V', 's');
7612 } else if (argc != 1)
7619 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
7620 "sendfd unveil", NULL) == -1)
7623 cwd = getcwd(NULL, 0);
7625 error = got_error_from_errno("getcwd");
7629 error = got_repo_pack_fds_open(&pack_fds);
7633 if (repo_path == NULL) {
7634 error = got_worktree_open(&worktree, cwd);
7635 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7641 strdup(got_worktree_get_repo_path(worktree));
7642 if (repo_path == NULL)
7643 error = got_error_from_errno("strdup");
7647 repo_path = strdup(cwd);
7648 if (repo_path == NULL) {
7649 error = got_error_from_errno("strdup");
7655 if (do_list || verify_tags) {
7656 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7659 error = get_allowed_signers(&allowed_signers, repo, worktree);
7662 error = get_revoked_signers(&revoked_signers, repo, worktree);
7666 /* Release work tree lock. */
7667 got_worktree_close(worktree);
7672 * Remove "cpath" promise unless needed for signature tmpfile
7676 got_sigs_apply_unveil();
7679 if (pledge("stdio rpath wpath flock proc exec sendfd "
7680 "unveil", NULL) == -1)
7684 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7687 error = list_tags(repo, tag_name, verify_tags, allowed_signers,
7688 revoked_signers, verbosity);
7690 error = get_gitconfig_path(&gitconfig_path);
7693 error = got_repo_open(&repo, repo_path, gitconfig_path,
7698 error = get_author(&tagger, repo, worktree);
7701 if (signer_id == NULL)
7702 signer_id = get_signer_id(repo, worktree);
7706 error = got_sigs_apply_unveil();
7710 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
7715 if (commit_id_arg == NULL) {
7716 struct got_reference *head_ref;
7717 struct got_object_id *commit_id;
7718 error = got_ref_open(&head_ref, repo,
7719 worktree ? got_worktree_get_head_ref_name(worktree)
7723 error = got_ref_resolve(&commit_id, repo, head_ref);
7724 got_ref_close(head_ref);
7727 error = got_object_id_str(&commit_id_str, commit_id);
7734 /* Release work tree lock. */
7735 got_worktree_close(worktree);
7739 error = add_tag(repo, tagger, tag_name,
7740 commit_id_str ? commit_id_str : commit_id_arg, tagmsg,
7741 signer_id, verbosity);
7745 const struct got_error *close_err = got_repo_close(repo);
7750 got_worktree_close(worktree);
7752 const struct got_error *pack_err =
7753 got_repo_pack_fds_close(pack_fds);
7759 free(gitconfig_path);
7760 free(commit_id_str);
7762 free(allowed_signers);
7763 free(revoked_signers);
7770 fprintf(stderr, "usage: %s add [-IR] path ...\n", getprogname());
7774 static const struct got_error *
7775 add_progress(void *arg, unsigned char status, const char *path)
7777 while (path[0] == '/')
7779 printf("%c %s\n", status, path);
7783 static const struct got_error *
7784 cmd_add(int argc, char *argv[])
7786 const struct got_error *error = NULL;
7787 struct got_repository *repo = NULL;
7788 struct got_worktree *worktree = NULL;
7790 struct got_pathlist_head paths;
7791 struct got_pathlist_entry *pe;
7792 int ch, can_recurse = 0, no_ignores = 0;
7793 int *pack_fds = NULL;
7797 while ((ch = getopt(argc, argv, "IR")) != -1) {
7815 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7822 cwd = getcwd(NULL, 0);
7824 error = got_error_from_errno("getcwd");
7828 error = got_repo_pack_fds_open(&pack_fds);
7832 error = got_worktree_open(&worktree, cwd);
7834 if (error->code == GOT_ERR_NOT_WORKTREE)
7835 error = wrap_not_worktree_error(error, "add", cwd);
7839 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7844 error = apply_unveil(got_repo_get_path(repo), 1,
7845 got_worktree_get_root_path(worktree));
7849 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7856 TAILQ_FOREACH(pe, &paths, entry) {
7857 if (asprintf(&ondisk_path, "%s/%s",
7858 got_worktree_get_root_path(worktree),
7860 error = got_error_from_errno("asprintf");
7863 if (lstat(ondisk_path, &sb) == -1) {
7864 if (errno == ENOENT) {
7868 error = got_error_from_errno2("lstat",
7874 if (S_ISDIR(sb.st_mode)) {
7875 error = got_error_msg(GOT_ERR_BAD_PATH,
7876 "adding directories requires -R option");
7882 error = got_worktree_schedule_add(worktree, &paths, add_progress,
7883 NULL, repo, no_ignores);
7886 const struct got_error *close_err = got_repo_close(repo);
7891 got_worktree_close(worktree);
7893 const struct got_error *pack_err =
7894 got_repo_pack_fds_close(pack_fds);
7898 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
7906 fprintf(stderr, "usage: %s remove [-fkR] [-s status-codes] path ...\n",
7911 static const struct got_error *
7912 print_remove_status(void *arg, unsigned char status,
7913 unsigned char staged_status, const char *path)
7915 while (path[0] == '/')
7917 if (status == GOT_STATUS_NONEXISTENT)
7919 if (status == staged_status && (status == GOT_STATUS_DELETE))
7920 status = GOT_STATUS_NO_CHANGE;
7921 printf("%c%c %s\n", status, staged_status, path);
7925 static const struct got_error *
7926 cmd_remove(int argc, char *argv[])
7928 const struct got_error *error = NULL;
7929 struct got_worktree *worktree = NULL;
7930 struct got_repository *repo = NULL;
7931 const char *status_codes = NULL;
7933 struct got_pathlist_head paths;
7934 struct got_pathlist_entry *pe;
7935 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0, i;
7936 int ignore_missing_paths = 0;
7937 int *pack_fds = NULL;
7941 while ((ch = getopt(argc, argv, "fkRs:")) != -1) {
7944 delete_local_mods = 1;
7945 ignore_missing_paths = 1;
7954 for (i = 0; i < strlen(optarg); i++) {
7955 switch (optarg[i]) {
7956 case GOT_STATUS_MODIFY:
7957 delete_local_mods = 1;
7959 case GOT_STATUS_MISSING:
7960 ignore_missing_paths = 1;
7963 errx(1, "invalid status code '%c'",
7967 status_codes = optarg;
7979 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7986 cwd = getcwd(NULL, 0);
7988 error = got_error_from_errno("getcwd");
7992 error = got_repo_pack_fds_open(&pack_fds);
7996 error = got_worktree_open(&worktree, cwd);
7998 if (error->code == GOT_ERR_NOT_WORKTREE)
7999 error = wrap_not_worktree_error(error, "remove", cwd);
8003 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8008 error = apply_unveil(got_repo_get_path(repo), 1,
8009 got_worktree_get_root_path(worktree));
8013 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8020 TAILQ_FOREACH(pe, &paths, entry) {
8021 if (asprintf(&ondisk_path, "%s/%s",
8022 got_worktree_get_root_path(worktree),
8024 error = got_error_from_errno("asprintf");
8027 if (lstat(ondisk_path, &sb) == -1) {
8028 if (errno == ENOENT) {
8032 error = got_error_from_errno2("lstat",
8038 if (S_ISDIR(sb.st_mode)) {
8039 error = got_error_msg(GOT_ERR_BAD_PATH,
8040 "removing directories requires -R option");
8046 error = got_worktree_schedule_delete(worktree, &paths,
8047 delete_local_mods, status_codes, print_remove_status, NULL,
8048 repo, keep_on_disk, ignore_missing_paths);
8051 const struct got_error *close_err = got_repo_close(repo);
8056 got_worktree_close(worktree);
8058 const struct got_error *pack_err =
8059 got_repo_pack_fds_close(pack_fds);
8063 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
8071 fprintf(stderr, "usage: %s patch [-nR] [-c commit] [-p strip-count] "
8072 "[patchfile]\n", getprogname());
8076 static const struct got_error *
8077 patch_from_stdin(int *patchfd)
8079 const struct got_error *err = NULL;
8082 sig_t sighup, sigint, sigquit;
8084 *patchfd = got_opentempfd();
8086 return got_error_from_errno("got_opentempfd");
8088 sighup = signal(SIGHUP, SIG_DFL);
8089 sigint = signal(SIGINT, SIG_DFL);
8090 sigquit = signal(SIGQUIT, SIG_DFL);
8093 r = read(0, buf, sizeof(buf));
8095 err = got_error_from_errno("read");
8100 if (write(*patchfd, buf, r) == -1) {
8101 err = got_error_from_errno("write");
8106 signal(SIGHUP, sighup);
8107 signal(SIGINT, sigint);
8108 signal(SIGQUIT, sigquit);
8110 if (err == NULL && lseek(*patchfd, 0, SEEK_SET) == -1)
8111 err = got_error_from_errno("lseek");
8121 static const struct got_error *
8122 patch_progress(void *arg, const char *old, const char *new,
8123 unsigned char status, const struct got_error *error, int old_from,
8124 int old_lines, int new_from, int new_lines, int offset,
8125 int ws_mangled, const struct got_error *hunk_err)
8127 const char *path = new == NULL ? old : new;
8129 while (*path == '/')
8133 printf("%c %s\n", status, path);
8136 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
8138 if (offset != 0 || hunk_err != NULL || ws_mangled) {
8139 printf("@@ -%d,%d +%d,%d @@ ", old_from,
8140 old_lines, new_from, new_lines);
8141 if (hunk_err != NULL)
8142 printf("%s\n", hunk_err->msg);
8143 else if (offset != 0)
8144 printf("applied with offset %d\n", offset);
8146 printf("hunk contains mangled whitespace\n");
8152 static const struct got_error *
8153 cmd_patch(int argc, char *argv[])
8155 const struct got_error *error = NULL, *close_error = NULL;
8156 struct got_worktree *worktree = NULL;
8157 struct got_repository *repo = NULL;
8158 struct got_reflist_head refs;
8159 struct got_object_id *commit_id = NULL;
8160 const char *commit_id_str = NULL;
8164 int ch, nop = 0, strip = -1, reverse = 0;
8166 int *pack_fds = NULL;
8171 if (pledge("stdio rpath wpath cpath fattr proc exec sendfd flock "
8172 "unveil", NULL) == -1)
8176 while ((ch = getopt(argc, argv, "c:np:R")) != -1) {
8179 commit_id_str = optarg;
8185 strip = strtonum(optarg, 0, INT_MAX, &errstr);
8187 errx(1, "pathname strip count is %s: %s",
8203 error = patch_from_stdin(&patchfd);
8206 } else if (argc == 1) {
8207 patchfd = open(argv[0], O_RDONLY);
8208 if (patchfd == -1) {
8209 error = got_error_from_errno2("open", argv[0]);
8212 if (fstat(patchfd, &sb) == -1) {
8213 error = got_error_from_errno2("fstat", argv[0]);
8216 if (!S_ISREG(sb.st_mode)) {
8217 error = got_error_path(argv[0], GOT_ERR_BAD_FILETYPE);
8223 if ((cwd = getcwd(NULL, 0)) == NULL) {
8224 error = got_error_from_errno("getcwd");
8228 error = got_repo_pack_fds_open(&pack_fds);
8232 error = got_worktree_open(&worktree, cwd);
8236 const char *repo_path = got_worktree_get_repo_path(worktree);
8237 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8241 error = apply_unveil(got_repo_get_path(repo), 0,
8242 got_worktree_get_root_path(worktree));
8246 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
8250 if (commit_id_str != NULL) {
8251 error = got_repo_match_object_id(&commit_id, NULL,
8252 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
8257 error = got_patch(patchfd, worktree, repo, nop, strip, reverse,
8258 commit_id, &patch_progress, NULL, check_cancelled, NULL);
8261 got_ref_list_free(&refs);
8264 close_error = got_repo_close(repo);
8266 error = close_error;
8268 if (worktree != NULL) {
8269 close_error = got_worktree_close(worktree);
8271 error = close_error;
8274 const struct got_error *pack_err =
8275 got_repo_pack_fds_close(pack_fds);
8286 fprintf(stderr, "usage: %s revert [-pR] [-F response-script] path ...\n",
8291 static const struct got_error *
8292 revert_progress(void *arg, unsigned char status, const char *path)
8294 if (status == GOT_STATUS_UNVERSIONED)
8297 while (path[0] == '/')
8299 printf("%c %s\n", status, path);
8303 struct choose_patch_arg {
8304 FILE *patch_script_file;
8308 static const struct got_error *
8309 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
8310 int nchanges, const char *action)
8312 const struct got_error *err;
8314 size_t linesize = 0;
8318 case GOT_STATUS_ADD:
8319 printf("A %s\n%s this addition? [y/n] ", path, action);
8321 case GOT_STATUS_DELETE:
8322 printf("D %s\n%s this deletion? [y/n] ", path, action);
8324 case GOT_STATUS_MODIFY:
8325 if (fseek(patch_file, 0L, SEEK_SET) == -1)
8326 return got_error_from_errno("fseek");
8327 printf(GOT_COMMIT_SEP_STR);
8328 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
8330 if (linelen == -1 && ferror(patch_file)) {
8331 err = got_error_from_errno("getline");
8336 printf(GOT_COMMIT_SEP_STR);
8337 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
8338 path, n, nchanges, action);
8341 return got_error_path(path, GOT_ERR_FILE_STATUS);
8348 static const struct got_error *
8349 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
8350 FILE *patch_file, int n, int nchanges)
8352 const struct got_error *err = NULL;
8354 size_t linesize = 0;
8357 struct choose_patch_arg *a = arg;
8359 *choice = GOT_PATCH_CHOICE_NONE;
8361 if (a->patch_script_file) {
8363 err = show_change(status, path, patch_file, n, nchanges,
8367 linelen = getline(&line, &linesize, a->patch_script_file);
8368 if (linelen == -1) {
8369 if (ferror(a->patch_script_file))
8370 return got_error_from_errno("getline");
8373 nl = strchr(line, '\n');
8376 if (strcmp(line, "y") == 0) {
8377 *choice = GOT_PATCH_CHOICE_YES;
8379 } else if (strcmp(line, "n") == 0) {
8380 *choice = GOT_PATCH_CHOICE_NO;
8382 } else if (strcmp(line, "q") == 0 &&
8383 status == GOT_STATUS_MODIFY) {
8384 *choice = GOT_PATCH_CHOICE_QUIT;
8387 printf("invalid response '%s'\n", line);
8392 while (resp != 'y' && resp != 'n' && resp != 'q') {
8393 err = show_change(status, path, patch_file, n, nchanges,
8400 if (status == GOT_STATUS_MODIFY) {
8401 if (resp != 'y' && resp != 'n' && resp != 'q') {
8402 printf("invalid response '%c'\n", resp);
8405 } else if (resp != 'y' && resp != 'n') {
8406 printf("invalid response '%c'\n", resp);
8412 *choice = GOT_PATCH_CHOICE_YES;
8413 else if (resp == 'n')
8414 *choice = GOT_PATCH_CHOICE_NO;
8415 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
8416 *choice = GOT_PATCH_CHOICE_QUIT;
8421 struct wt_commitable_path_arg {
8422 struct got_pathlist_head *commit_paths;
8427 * Shortcut work tree status callback to determine if the set of paths scanned
8428 * has at least one versioned path that is being modified and, if not NULL, is
8429 * in the arg->commit_paths list. Set arg and return GOT_ERR_FILE_MODIFIED as
8430 * soon as a path is passed with a status that satisfies this criteria.
8432 static const struct got_error *
8433 worktree_has_commitable_path(void *arg, unsigned char status,
8434 unsigned char staged_status, const char *path,
8435 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8436 struct got_object_id *commit_id, int dirfd, const char *de_name)
8438 struct wt_commitable_path_arg *a = arg;
8440 if (status == staged_status && (status == GOT_STATUS_DELETE))
8441 status = GOT_STATUS_NO_CHANGE;
8443 if (!(status == GOT_STATUS_NO_CHANGE ||
8444 status == GOT_STATUS_UNVERSIONED) ||
8445 staged_status != GOT_STATUS_NO_CHANGE) {
8446 if (a->commit_paths != NULL) {
8447 struct got_pathlist_entry *pe;
8449 TAILQ_FOREACH(pe, a->commit_paths, entry) {
8450 if (strncmp(path, pe->path,
8451 pe->path_len) == 0) {
8452 *a->has_changes = 1;
8457 *a->has_changes = 1;
8459 if (*a->has_changes)
8460 return got_error(GOT_ERR_FILE_MODIFIED);
8467 * Check that the changeset of the commit identified by id is
8468 * comprised of at least one modified path that is being committed.
8470 static const struct got_error *
8471 commit_path_changed_in_worktree(struct wt_commitable_path_arg *wcpa,
8472 struct got_object_id *id, struct got_worktree *worktree,
8473 struct got_repository *repo)
8475 const struct got_error *err;
8476 struct got_pathlist_head paths;
8477 struct got_commit_object *commit = NULL, *pcommit = NULL;
8478 struct got_tree_object *tree = NULL, *ptree = NULL;
8479 struct got_object_qid *pid;
8483 err = got_object_open_as_commit(&commit, repo, id);
8487 err = got_object_open_as_tree(&tree, repo,
8488 got_object_commit_get_tree_id(commit));
8492 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
8494 err = got_object_open_as_commit(&pcommit, repo, &pid->id);
8498 err = got_object_open_as_tree(&ptree, repo,
8499 got_object_commit_get_tree_id(pcommit));
8504 err = got_diff_tree(ptree, tree, NULL, NULL, -1, -1, "", "", repo,
8505 got_diff_tree_collect_changed_paths, &paths, 0);
8509 err = got_worktree_status(worktree, &paths, repo, 0,
8510 worktree_has_commitable_path, wcpa, check_cancelled, NULL);
8511 if (err && err->code == GOT_ERR_FILE_MODIFIED) {
8513 * At least one changed path in the referenced commit is
8514 * modified in the work tree, that's all we need to know!
8520 got_pathlist_free(&paths, GOT_PATHLIST_FREE_ALL);
8522 got_object_commit_close(commit);
8524 got_object_commit_close(pcommit);
8526 got_object_tree_close(tree);
8528 got_object_tree_close(ptree);
8533 * Remove any "logmsg" reference comprised entirely of paths that have
8534 * been reverted in this work tree. If any path in the logmsg ref changeset
8535 * remains in a changed state in the worktree, do not remove the reference.
8537 static const struct got_error *
8538 rm_logmsg_ref(struct got_worktree *worktree, struct got_repository *repo)
8540 const struct got_error *err;
8541 struct got_reflist_head refs;
8542 struct got_reflist_entry *re;
8543 struct got_commit_object *commit = NULL;
8544 struct got_object_id *commit_id = NULL;
8545 struct wt_commitable_path_arg wcpa;
8546 char *uuidstr = NULL;
8550 err = got_worktree_get_uuid(&uuidstr, worktree);
8554 err = got_ref_list(&refs, repo, "refs/got/worktree",
8555 got_ref_cmp_by_name, repo);
8559 TAILQ_FOREACH(re, &refs, entry) {
8560 const char *refname;
8561 int has_changes = 0;
8563 refname = got_ref_get_name(re->ref);
8565 if (!strncmp(refname, GOT_WORKTREE_CHERRYPICK_REF_PREFIX,
8566 GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN))
8567 refname += GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN + 1;
8568 else if (!strncmp(refname, GOT_WORKTREE_BACKOUT_REF_PREFIX,
8569 GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN))
8570 refname += GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN + 1;
8574 if (strncmp(refname, uuidstr, GOT_WORKTREE_UUID_STRLEN) == 0)
8575 refname += GOT_WORKTREE_UUID_STRLEN + 1; /* skip '-' */
8579 err = got_repo_match_object_id(&commit_id, NULL, refname,
8580 GOT_OBJ_TYPE_COMMIT, NULL, repo);
8584 err = got_object_open_as_commit(&commit, repo, commit_id);
8588 wcpa.commit_paths = NULL;
8589 wcpa.has_changes = &has_changes;
8591 err = commit_path_changed_in_worktree(&wcpa, commit_id,
8597 err = got_ref_delete(re->ref, repo);
8602 got_object_commit_close(commit);
8611 got_ref_list_free(&refs);
8613 got_object_commit_close(commit);
8617 static const struct got_error *
8618 cmd_revert(int argc, char *argv[])
8620 const struct got_error *error = NULL;
8621 struct got_worktree *worktree = NULL;
8622 struct got_repository *repo = NULL;
8623 char *cwd = NULL, *path = NULL;
8624 struct got_pathlist_head paths;
8625 struct got_pathlist_entry *pe;
8626 int ch, can_recurse = 0, pflag = 0;
8627 FILE *patch_script_file = NULL;
8628 const char *patch_script_path = NULL;
8629 struct choose_patch_arg cpa;
8630 int *pack_fds = NULL;
8634 while ((ch = getopt(argc, argv, "F:pR")) != -1) {
8637 patch_script_path = optarg;
8655 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8656 "unveil", NULL) == -1)
8661 if (patch_script_path && !pflag)
8662 errx(1, "-F option can only be used together with -p option");
8664 cwd = getcwd(NULL, 0);
8666 error = got_error_from_errno("getcwd");
8670 error = got_repo_pack_fds_open(&pack_fds);
8674 error = got_worktree_open(&worktree, cwd);
8676 if (error->code == GOT_ERR_NOT_WORKTREE)
8677 error = wrap_not_worktree_error(error, "revert", cwd);
8681 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8686 if (patch_script_path) {
8687 patch_script_file = fopen(patch_script_path, "re");
8688 if (patch_script_file == NULL) {
8689 error = got_error_from_errno2("fopen",
8696 * XXX "c" perm needed on repo dir to delete merge references.
8698 error = apply_unveil(got_repo_get_path(repo), 0,
8699 got_worktree_get_root_path(worktree));
8703 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8710 TAILQ_FOREACH(pe, &paths, entry) {
8711 if (asprintf(&ondisk_path, "%s/%s",
8712 got_worktree_get_root_path(worktree),
8714 error = got_error_from_errno("asprintf");
8717 if (lstat(ondisk_path, &sb) == -1) {
8718 if (errno == ENOENT) {
8722 error = got_error_from_errno2("lstat",
8728 if (S_ISDIR(sb.st_mode)) {
8729 error = got_error_msg(GOT_ERR_BAD_PATH,
8730 "reverting directories requires -R option");
8736 cpa.patch_script_file = patch_script_file;
8737 cpa.action = "revert";
8738 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
8739 pflag ? choose_patch : NULL, &cpa, repo);
8741 error = rm_logmsg_ref(worktree, repo);
8743 if (patch_script_file && fclose(patch_script_file) == EOF &&
8745 error = got_error_from_errno2("fclose", patch_script_path);
8747 const struct got_error *close_err = got_repo_close(repo);
8752 got_worktree_close(worktree);
8754 const struct got_error *pack_err =
8755 got_repo_pack_fds_close(pack_fds);
8759 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
8768 fprintf(stderr, "usage: %s commit [-NS] [-A author] [-F path] "
8769 "[-m message] [path ...]\n", getprogname());
8773 struct collect_commit_logmsg_arg {
8774 const char *cmdline_log;
8775 const char *prepared_log;
8776 const char *merged_log;
8777 int non_interactive;
8779 const char *worktree_path;
8780 const char *branch_name;
8781 const char *repo_path;
8786 static const struct got_error *
8787 read_prepared_logmsg(char **logmsg, const char *path)
8789 const struct got_error *err = NULL;
8795 memset(&sb, 0, sizeof(sb));
8797 f = fopen(path, "re");
8799 return got_error_from_errno2("fopen", path);
8801 if (fstat(fileno(f), &sb) == -1) {
8802 err = got_error_from_errno2("fstat", path);
8805 if (sb.st_size == 0) {
8806 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
8810 *logmsg = malloc(sb.st_size + 1);
8811 if (*logmsg == NULL) {
8812 err = got_error_from_errno("malloc");
8816 r = fread(*logmsg, 1, sb.st_size, f);
8817 if (r != sb.st_size) {
8819 err = got_error_from_errno2("fread", path);
8821 err = got_error(GOT_ERR_IO);
8824 (*logmsg)[sb.st_size] = '\0';
8826 if (fclose(f) == EOF && err == NULL)
8827 err = got_error_from_errno2("fclose", path);
8835 static const struct got_error *
8836 collect_commit_logmsg(struct got_pathlist_head *commitable_paths,
8837 const char *diff_path, char **logmsg, void *arg)
8839 char *initial_content = NULL;
8840 struct got_pathlist_entry *pe;
8841 const struct got_error *err = NULL;
8842 char *template = NULL;
8843 char *prepared_msg = NULL, *merged_msg = NULL;
8844 struct collect_commit_logmsg_arg *a = arg;
8845 int initial_content_len;
8849 /* if a message was specified on the command line, just use it */
8850 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
8851 len = strlen(a->cmdline_log) + 1;
8852 *logmsg = malloc(len + 1);
8853 if (*logmsg == NULL)
8854 return got_error_from_errno("malloc");
8855 strlcpy(*logmsg, a->cmdline_log, len);
8857 } else if (a->prepared_log != NULL && a->non_interactive)
8858 return read_prepared_logmsg(logmsg, a->prepared_log);
8860 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
8861 return got_error_from_errno("asprintf");
8863 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template, "");
8867 if (a->prepared_log) {
8868 err = read_prepared_logmsg(&prepared_msg, a->prepared_log);
8871 } else if (a->merged_log) {
8872 err = read_prepared_logmsg(&merged_msg, a->merged_log);
8877 initial_content_len = asprintf(&initial_content,
8878 "%s%s\n# changes to be committed on branch %s:\n",
8879 prepared_msg ? prepared_msg : "",
8880 merged_msg ? merged_msg : "", a->branch_name);
8881 if (initial_content_len == -1) {
8882 err = got_error_from_errno("asprintf");
8886 if (write(fd, initial_content, initial_content_len) == -1) {
8887 err = got_error_from_errno2("write", a->logmsg_path);
8891 TAILQ_FOREACH(pe, commitable_paths, entry) {
8892 struct got_commitable *ct = pe->data;
8893 dprintf(fd, "# %c %s\n",
8894 got_commitable_get_status(ct),
8895 got_commitable_get_path(ct));
8899 dprintf(fd, "# detailed changes can be viewed in %s\n",
8903 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content,
8904 initial_content_len, a->prepared_log ? 0 : 1);
8906 free(initial_content);
8911 if (fd != -1 && close(fd) == -1 && err == NULL)
8912 err = got_error_from_errno2("close", a->logmsg_path);
8914 /* Editor is done; we can now apply unveil(2) */
8916 err = apply_unveil(a->repo_path, 0, a->worktree_path);
8924 static const struct got_error *
8925 cat_logmsg(FILE *f, struct got_commit_object *commit, const char *idstr,
8926 const char *type, int has_content)
8928 const struct got_error *err = NULL;
8929 char *logmsg = NULL;
8931 err = got_object_commit_get_logmsg(&logmsg, commit);
8935 if (fprintf(f, "%s# log message of %s commit %s:%s",
8936 has_content ? "\n" : "", type, idstr, logmsg) < 0)
8937 err = got_ferror(f, GOT_ERR_IO);
8944 * Lookup "logmsg" references of backed-out and cherrypicked commits
8945 * belonging to the current work tree. If found, and the worktree has
8946 * at least one modified file that was changed in the referenced commit,
8947 * add its log message to a new temporary file at *logmsg_path.
8948 * Add all refs found to matched_refs to be scheduled for removal on
8949 * successful commit.
8951 static const struct got_error *
8952 lookup_logmsg_ref(char **logmsg_path, struct got_pathlist_head *paths,
8953 struct got_reflist_head *matched_refs, struct got_worktree *worktree,
8954 struct got_repository *repo)
8956 const struct got_error *err;
8957 struct got_commit_object *commit = NULL;
8958 struct got_object_id *id = NULL;
8959 struct got_reflist_head refs;
8960 struct got_reflist_entry *re, *re_match;
8962 char *uuidstr = NULL;
8963 int added_logmsg = 0;
8967 *logmsg_path = NULL;
8969 err = got_worktree_get_uuid(&uuidstr, worktree);
8973 err = got_ref_list(&refs, repo, "refs/got/worktree",
8974 got_ref_cmp_by_name, repo);
8978 TAILQ_FOREACH(re, &refs, entry) {
8979 const char *refname, *type;
8980 struct wt_commitable_path_arg wcpa;
8983 refname = got_ref_get_name(re->ref);
8985 if (strncmp(refname, GOT_WORKTREE_CHERRYPICK_REF_PREFIX,
8986 GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN) == 0) {
8987 refname += GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN + 1;
8988 type = "cherrypicked";
8989 } else if (strncmp(refname, GOT_WORKTREE_BACKOUT_REF_PREFIX,
8990 GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN) == 0) {
8991 refname += GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN + 1;
8992 type = "backed-out";
8996 if (strncmp(refname, uuidstr, GOT_WORKTREE_UUID_STRLEN) == 0)
8997 refname += GOT_WORKTREE_UUID_STRLEN + 1; /* skip '-' */
9001 err = got_repo_match_object_id(&id, NULL, refname,
9002 GOT_OBJ_TYPE_COMMIT, NULL, repo);
9006 err = got_object_open_as_commit(&commit, repo, id);
9010 wcpa.commit_paths = paths;
9011 wcpa.has_changes = &add_logmsg;
9013 err = commit_path_changed_in_worktree(&wcpa, id,
9020 err = got_opentemp_named(logmsg_path, &f,
9021 "got-commit-logmsg", "");
9025 err = cat_logmsg(f, commit, refname, type,
9032 err = got_reflist_entry_dup(&re_match, re);
9035 TAILQ_INSERT_HEAD(matched_refs, re_match, entry);
9038 got_object_commit_close(commit);
9047 got_ref_list_free(&refs);
9049 got_object_commit_close(commit);
9050 if (f && fclose(f) == EOF && err == NULL)
9051 err = got_error_from_errno("fclose");
9052 if (!added_logmsg) {
9053 if (*logmsg_path && unlink(*logmsg_path) != 0 && err == NULL)
9054 err = got_error_from_errno2("unlink", *logmsg_path);
9055 *logmsg_path = NULL;
9060 static const struct got_error *
9061 cmd_commit(int argc, char *argv[])
9063 const struct got_error *error = NULL;
9064 struct got_worktree *worktree = NULL;
9065 struct got_repository *repo = NULL;
9066 char *cwd = NULL, *id_str = NULL;
9067 struct got_object_id *id = NULL;
9068 const char *logmsg = NULL;
9069 char *prepared_logmsg = NULL, *merged_logmsg = NULL;
9070 struct collect_commit_logmsg_arg cl_arg;
9071 const char *author = NULL;
9072 char *gitconfig_path = NULL, *editor = NULL, *committer = NULL;
9073 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
9074 int allow_bad_symlinks = 0, non_interactive = 0, merge_in_progress = 0;
9076 struct got_pathlist_head paths;
9077 struct got_reflist_head refs;
9078 struct got_reflist_entry *re;
9079 int *pack_fds = NULL;
9083 cl_arg.logmsg_path = NULL;
9085 while ((ch = getopt(argc, argv, "A:F:m:NnS")) != -1) {
9089 error = valid_author(author);
9095 option_conflict('F', 'm');
9096 prepared_logmsg = realpath(optarg, NULL);
9097 if (prepared_logmsg == NULL)
9098 return got_error_from_errno2("realpath",
9102 if (prepared_logmsg)
9103 option_conflict('m', 'F');
9107 non_interactive = 1;
9113 allow_bad_symlinks = 1;
9125 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9126 "unveil", NULL) == -1)
9129 cwd = getcwd(NULL, 0);
9131 error = got_error_from_errno("getcwd");
9135 error = got_repo_pack_fds_open(&pack_fds);
9139 error = got_worktree_open(&worktree, cwd);
9141 if (error->code == GOT_ERR_NOT_WORKTREE)
9142 error = wrap_not_worktree_error(error, "commit", cwd);
9146 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
9149 if (rebase_in_progress) {
9150 error = got_error(GOT_ERR_REBASING);
9154 error = got_worktree_histedit_in_progress(&histedit_in_progress,
9159 error = get_gitconfig_path(&gitconfig_path);
9162 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9163 gitconfig_path, pack_fds);
9167 error = got_worktree_merge_in_progress(&merge_in_progress, worktree, repo);
9170 if (merge_in_progress) {
9171 error = got_error(GOT_ERR_MERGE_BUSY);
9175 error = get_author(&committer, repo, worktree);
9179 if (author != NULL && !strcmp(committer, author)) {
9180 error = got_error(GOT_ERR_COMMIT_REDUNDANT_AUTHOR);
9188 * unveil(2) traverses exec(2); if an editor is used we have
9189 * to apply unveil after the log message has been written.
9191 if (logmsg == NULL || strlen(logmsg) == 0)
9192 error = get_editor(&editor);
9194 error = apply_unveil(got_repo_get_path(repo), 0,
9195 got_worktree_get_root_path(worktree));
9199 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
9203 if (prepared_logmsg == NULL) {
9204 error = lookup_logmsg_ref(&merged_logmsg,
9205 argc > 0 ? &paths : NULL, &refs, worktree, repo);
9210 cl_arg.editor = editor;
9211 cl_arg.cmdline_log = logmsg;
9212 cl_arg.prepared_log = prepared_logmsg;
9213 cl_arg.merged_log = merged_logmsg;
9214 cl_arg.non_interactive = non_interactive;
9215 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
9216 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
9217 if (!histedit_in_progress) {
9218 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
9219 error = got_error(GOT_ERR_COMMIT_BRANCH);
9222 cl_arg.branch_name += 11;
9224 cl_arg.repo_path = got_repo_get_path(repo);
9225 error = got_worktree_commit(&id, worktree, &paths, author, committer,
9226 allow_bad_symlinks, show_diff, collect_commit_logmsg, &cl_arg,
9227 print_status, NULL, repo);
9229 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
9230 cl_arg.logmsg_path != NULL)
9231 preserve_logmsg = 1;
9235 error = got_object_id_str(&id_str, id);
9238 printf("Created commit %s\n", id_str);
9240 TAILQ_FOREACH(re, &refs, entry) {
9241 error = got_ref_delete(re->ref, repo);
9247 if (preserve_logmsg) {
9248 fprintf(stderr, "%s: log message preserved in %s\n",
9249 getprogname(), cl_arg.logmsg_path);
9250 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
9252 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
9253 free(cl_arg.logmsg_path);
9254 if (merged_logmsg && unlink(merged_logmsg) == -1 && error == NULL)
9255 error = got_error_from_errno2("unlink", merged_logmsg);
9256 free(merged_logmsg);
9258 const struct got_error *close_err = got_repo_close(repo);
9263 got_worktree_close(worktree);
9265 const struct got_error *pack_err =
9266 got_repo_pack_fds_close(pack_fds);
9270 got_ref_list_free(&refs);
9271 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
9274 free(gitconfig_path);
9277 free(prepared_logmsg);
9284 fprintf(stderr, "usage: %s send [-afqTv] [-b branch] [-d branch] "
9285 "[-r repository-path] [-t tag] [remote-repository]\n",
9291 print_load_info(int print_colored, int print_found, int print_trees,
9292 int ncolored, int nfound, int ntrees)
9294 if (print_colored) {
9295 printf("%d commit%s colored", ncolored,
9296 ncolored == 1 ? "" : "s");
9299 printf("%s%d object%s found",
9300 ncolored > 0 ? "; " : "",
9301 nfound, nfound == 1 ? "" : "s");
9304 printf("; %d tree%s scanned", ntrees,
9305 ntrees == 1 ? "" : "s");
9309 struct got_send_progress_arg {
9310 char last_scaled_packsize[FMT_SCALED_STRSIZE];
9317 int last_nobj_total;
9321 int printed_something;
9323 struct got_pathlist_head *delete_branches;
9326 static const struct got_error *
9327 send_progress(void *arg, int ncolored, int nfound, int ntrees,
9328 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
9329 int nobj_written, off_t bytes_sent, const char *refname,
9330 const char *errmsg, int success)
9332 struct got_send_progress_arg *a = arg;
9333 char scaled_packsize[FMT_SCALED_STRSIZE];
9334 char scaled_sent[FMT_SCALED_STRSIZE];
9335 int p_deltify = 0, p_written = 0, p_sent = 0;
9336 int print_colored = 0, print_found = 0, print_trees = 0;
9337 int print_searching = 0, print_total = 0;
9338 int print_deltify = 0, print_written = 0, print_sent = 0;
9340 if (a->verbosity < 0)
9344 const char *status = success ? "accepted" : "rejected";
9347 struct got_pathlist_entry *pe;
9348 TAILQ_FOREACH(pe, a->delete_branches, entry) {
9349 const char *branchname = pe->path;
9350 if (got_path_cmp(branchname, refname,
9351 strlen(branchname), strlen(refname)) == 0) {
9353 a->sent_something = 1;
9359 if (a->printed_something)
9361 printf("Server has %s %s", status, refname);
9363 printf(": %s", errmsg);
9364 a->printed_something = 1;
9368 if (a->last_ncolored != ncolored) {
9370 a->last_ncolored = ncolored;
9373 if (a->last_nfound != nfound) {
9376 a->last_nfound = nfound;
9379 if (a->last_ntrees != ntrees) {
9383 a->last_ntrees = ntrees;
9386 if ((print_colored || print_found || print_trees) &&
9389 print_load_info(print_colored, print_found, print_trees,
9390 ncolored, nfound, ntrees);
9391 a->printed_something = 1;
9394 } else if (!a->loading_done) {
9396 print_load_info(1, 1, 1, ncolored, nfound, ntrees);
9398 a->loading_done = 1;
9401 if (fmt_scaled(packfile_size, scaled_packsize) == -1)
9402 return got_error_from_errno("fmt_scaled");
9403 if (fmt_scaled(bytes_sent, scaled_sent) == -1)
9404 return got_error_from_errno("fmt_scaled");
9406 if (a->last_ncommits != ncommits) {
9407 print_searching = 1;
9408 a->last_ncommits = ncommits;
9411 if (a->last_nobj_total != nobj_total) {
9412 print_searching = 1;
9414 a->last_nobj_total = nobj_total;
9417 if (packfile_size > 0 && (a->last_scaled_packsize[0] == '\0' ||
9418 strcmp(scaled_packsize, a->last_scaled_packsize)) != 0) {
9419 if (strlcpy(a->last_scaled_packsize, scaled_packsize,
9420 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
9421 return got_error(GOT_ERR_NO_SPACE);
9424 if (nobj_deltify > 0 || nobj_written > 0) {
9425 if (nobj_deltify > 0) {
9426 p_deltify = (nobj_deltify * 100) / nobj_total;
9427 if (p_deltify != a->last_p_deltify) {
9428 a->last_p_deltify = p_deltify;
9429 print_searching = 1;
9434 if (nobj_written > 0) {
9435 p_written = (nobj_written * 100) / nobj_total;
9436 if (p_written != a->last_p_written) {
9437 a->last_p_written = p_written;
9438 print_searching = 1;
9446 if (bytes_sent > 0) {
9447 p_sent = (bytes_sent * 100) / packfile_size;
9448 if (p_sent != a->last_p_sent) {
9449 a->last_p_sent = p_sent;
9450 print_searching = 1;
9456 a->sent_something = 1;
9459 if (print_searching || print_total || print_deltify || print_written ||
9462 if (print_searching)
9463 printf("packing %d reference%s", ncommits,
9464 ncommits == 1 ? "" : "s");
9466 printf("; %d object%s", nobj_total,
9467 nobj_total == 1 ? "" : "s");
9469 printf("; deltify: %d%%", p_deltify);
9471 printf("; uploading pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
9472 scaled_packsize, p_sent);
9473 else if (print_written)
9474 printf("; writing pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
9475 scaled_packsize, p_written);
9476 if (print_searching || print_total || print_deltify ||
9477 print_written || print_sent) {
9478 a->printed_something = 1;
9484 static const struct got_error *
9485 cmd_send(int argc, char *argv[])
9487 const struct got_error *error = NULL;
9488 char *cwd = NULL, *repo_path = NULL;
9489 const char *remote_name;
9490 char *proto = NULL, *host = NULL, *port = NULL;
9491 char *repo_name = NULL, *server_path = NULL;
9492 const struct got_remote_repo *remotes, *remote = NULL;
9493 int nremotes, nbranches = 0, ndelete_branches = 0;
9494 struct got_repository *repo = NULL;
9495 struct got_worktree *worktree = NULL;
9496 const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
9497 struct got_pathlist_head branches;
9498 struct got_pathlist_head tags;
9499 struct got_reflist_head all_branches;
9500 struct got_reflist_head all_tags;
9501 struct got_pathlist_head delete_args;
9502 struct got_pathlist_head delete_branches;
9503 struct got_reflist_entry *re;
9504 struct got_pathlist_entry *pe;
9505 int i, ch, sendfd = -1, sendstatus;
9507 struct got_send_progress_arg spa;
9508 int verbosity = 0, overwrite_refs = 0;
9509 int send_all_branches = 0, send_all_tags = 0;
9510 struct got_reference *ref = NULL;
9511 int *pack_fds = NULL;
9513 TAILQ_INIT(&branches);
9515 TAILQ_INIT(&all_branches);
9516 TAILQ_INIT(&all_tags);
9517 TAILQ_INIT(&delete_args);
9518 TAILQ_INIT(&delete_branches);
9520 while ((ch = getopt(argc, argv, "ab:d:fqr:Tt:v")) != -1) {
9523 send_all_branches = 1;
9526 error = got_pathlist_append(&branches, optarg, NULL);
9532 error = got_pathlist_append(&delete_args, optarg, NULL);
9543 repo_path = realpath(optarg, NULL);
9544 if (repo_path == NULL)
9545 return got_error_from_errno2("realpath",
9547 got_path_strip_trailing_slashes(repo_path);
9553 error = got_pathlist_append(&tags, optarg, NULL);
9560 else if (verbosity < 3)
9571 if (send_all_branches && !TAILQ_EMPTY(&branches))
9572 option_conflict('a', 'b');
9573 if (send_all_tags && !TAILQ_EMPTY(&tags))
9574 option_conflict('T', 't');
9578 remote_name = GOT_SEND_DEFAULT_REMOTE_NAME;
9580 remote_name = argv[0];
9584 cwd = getcwd(NULL, 0);
9586 error = got_error_from_errno("getcwd");
9590 error = got_repo_pack_fds_open(&pack_fds);
9594 if (repo_path == NULL) {
9595 error = got_worktree_open(&worktree, cwd);
9596 if (error && error->code != GOT_ERR_NOT_WORKTREE)
9602 strdup(got_worktree_get_repo_path(worktree));
9603 if (repo_path == NULL)
9604 error = got_error_from_errno("strdup");
9608 repo_path = strdup(cwd);
9609 if (repo_path == NULL) {
9610 error = got_error_from_errno("strdup");
9616 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
9621 worktree_conf = got_worktree_get_gotconfig(worktree);
9622 if (worktree_conf) {
9623 got_gotconfig_get_remotes(&nremotes, &remotes,
9625 for (i = 0; i < nremotes; i++) {
9626 if (strcmp(remotes[i].name, remote_name) == 0) {
9627 remote = &remotes[i];
9633 if (remote == NULL) {
9634 repo_conf = got_repo_get_gotconfig(repo);
9636 got_gotconfig_get_remotes(&nremotes, &remotes,
9638 for (i = 0; i < nremotes; i++) {
9639 if (strcmp(remotes[i].name, remote_name) == 0) {
9640 remote = &remotes[i];
9646 if (remote == NULL) {
9647 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
9648 for (i = 0; i < nremotes; i++) {
9649 if (strcmp(remotes[i].name, remote_name) == 0) {
9650 remote = &remotes[i];
9655 if (remote == NULL) {
9656 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
9660 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
9661 &repo_name, remote->send_url);
9665 if (strcmp(proto, "git") == 0) {
9667 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
9668 "sendfd dns inet unveil", NULL) == -1)
9671 } else if (strcmp(proto, "git+ssh") == 0 ||
9672 strcmp(proto, "ssh") == 0) {
9674 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
9675 "sendfd unveil", NULL) == -1)
9678 } else if (strcmp(proto, "http") == 0 ||
9679 strcmp(proto, "git+http") == 0) {
9680 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
9683 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
9687 error = got_dial_apply_unveil(proto);
9691 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
9695 if (send_all_branches) {
9696 error = got_ref_list(&all_branches, repo, "refs/heads",
9697 got_ref_cmp_by_name, NULL);
9700 TAILQ_FOREACH(re, &all_branches, entry) {
9701 const char *branchname = got_ref_get_name(re->ref);
9702 error = got_pathlist_append(&branches,
9708 } else if (nbranches == 0) {
9709 for (i = 0; i < remote->nsend_branches; i++) {
9710 error = got_pathlist_append(&branches,
9711 remote->send_branches[i], NULL);
9717 if (send_all_tags) {
9718 error = got_ref_list(&all_tags, repo, "refs/tags",
9719 got_ref_cmp_by_name, NULL);
9722 TAILQ_FOREACH(re, &all_tags, entry) {
9723 const char *tagname = got_ref_get_name(re->ref);
9724 error = got_pathlist_append(&tags,
9732 * To prevent accidents only branches in refs/heads/ can be deleted
9733 * with 'got send -d'.
9734 * Deleting anything else requires local repository access or Git.
9736 TAILQ_FOREACH(pe, &delete_args, entry) {
9737 const char *branchname = pe->path;
9739 struct got_pathlist_entry *new;
9740 if (strncmp(branchname, "refs/heads/", 11) == 0) {
9741 s = strdup(branchname);
9743 error = got_error_from_errno("strdup");
9747 if (asprintf(&s, "refs/heads/%s", branchname) == -1) {
9748 error = got_error_from_errno("asprintf");
9752 error = got_pathlist_insert(&new, &delete_branches, s, NULL);
9753 if (error || new == NULL /* duplicate */)
9760 if (nbranches == 0 && ndelete_branches == 0) {
9761 struct got_reference *head_ref;
9763 error = got_ref_open(&head_ref, repo,
9764 got_worktree_get_head_ref_name(worktree), 0);
9766 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
9769 if (got_ref_is_symbolic(head_ref)) {
9770 error = got_ref_resolve_symbolic(&ref, repo, head_ref);
9771 got_ref_close(head_ref);
9776 error = got_pathlist_append(&branches, got_ref_get_name(ref),
9783 if (verbosity >= 0) {
9784 printf("Connecting to \"%s\" %s://%s%s%s%s%s\n",
9785 remote->name, proto, host,
9786 port ? ":" : "", port ? port : "",
9787 *server_path == '/' ? "" : "/", server_path);
9790 error = got_send_connect(&sendpid, &sendfd, proto, host, port,
9791 server_path, verbosity);
9795 memset(&spa, 0, sizeof(spa));
9796 spa.last_scaled_packsize[0] = '\0';
9797 spa.last_p_deltify = -1;
9798 spa.last_p_written = -1;
9799 spa.verbosity = verbosity;
9800 spa.delete_branches = &delete_branches;
9801 error = got_send_pack(remote_name, &branches, &tags, &delete_branches,
9802 verbosity, overwrite_refs, sendfd, repo, send_progress, &spa,
9803 check_cancelled, NULL);
9804 if (spa.printed_something)
9808 if (!spa.sent_something && verbosity >= 0)
9809 printf("Already up-to-date\n");
9812 if (kill(sendpid, SIGTERM) == -1)
9813 error = got_error_from_errno("kill");
9814 if (waitpid(sendpid, &sendstatus, 0) == -1 && error == NULL)
9815 error = got_error_from_errno("waitpid");
9817 if (sendfd != -1 && close(sendfd) == -1 && error == NULL)
9818 error = got_error_from_errno("close");
9820 const struct got_error *close_err = got_repo_close(repo);
9825 got_worktree_close(worktree);
9827 const struct got_error *pack_err =
9828 got_repo_pack_fds_close(pack_fds);
9834 got_pathlist_free(&branches, GOT_PATHLIST_FREE_NONE);
9835 got_pathlist_free(&tags, GOT_PATHLIST_FREE_NONE);
9836 got_ref_list_free(&all_branches);
9837 got_ref_list_free(&all_tags);
9838 got_pathlist_free(&delete_args, GOT_PATHLIST_FREE_NONE);
9839 got_pathlist_free(&delete_branches, GOT_PATHLIST_FREE_PATH);
9851 * Print and if delete is set delete all ref_prefix references.
9852 * If wanted_ref is not NULL, only print or delete this reference.
9854 static const struct got_error *
9855 process_logmsg_refs(const char *ref_prefix, size_t prefix_len,
9856 const char *wanted_ref, int delete, struct got_worktree *worktree,
9857 struct got_repository *repo)
9859 const struct got_error *err;
9860 struct got_pathlist_head paths;
9861 struct got_reflist_head refs;
9862 struct got_reflist_entry *re;
9863 struct got_reflist_object_id_map *refs_idmap = NULL;
9864 struct got_commit_object *commit = NULL;
9865 struct got_object_id *id = NULL;
9866 const char *header_prefix;
9867 char *uuidstr = NULL;
9873 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, repo);
9877 err = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
9881 if (worktree != NULL) {
9882 err = got_worktree_get_uuid(&uuidstr, worktree);
9888 if (strncmp(wanted_ref, "refs/heads/", 11) == 0)
9892 if (strcmp(ref_prefix, GOT_WORKTREE_BACKOUT_REF_PREFIX) == 0)
9893 header_prefix = "backout";
9895 header_prefix = "cherrypick";
9897 TAILQ_FOREACH(re, &refs, entry) {
9898 const char *refname, *wt;
9900 refname = got_ref_get_name(re->ref);
9902 err = check_cancelled(NULL);
9906 if (strncmp(refname, ref_prefix, prefix_len) == 0)
9907 refname += prefix_len + 1; /* skip '-' delimiter */
9913 if (worktree == NULL || strncmp(refname, uuidstr,
9914 GOT_WORKTREE_UUID_STRLEN) == 0)
9915 refname += GOT_WORKTREE_UUID_STRLEN + 1; /* skip '-' */
9919 err = got_repo_match_object_id(&id, NULL, refname,
9920 GOT_OBJ_TYPE_COMMIT, NULL, repo);
9924 err = got_object_open_as_commit(&commit, repo, id);
9929 found = strncmp(wanted_ref, refname,
9930 strlen(wanted_ref)) == 0;
9931 if (wanted_ref && !found) {
9932 struct got_reflist_head *ci_refs;
9934 ci_refs = got_reflist_object_id_map_lookup(refs_idmap,
9938 char *refs_str = NULL;
9939 char const *r = NULL;
9941 err = build_refs_str(&refs_str, ci_refs, id,
9948 if (strncmp(r, wanted_ref,
9949 strlen(wanted_ref)) == 0) {
9961 if (wanted_ref == NULL || found) {
9963 err = got_ref_delete(re->ref, repo);
9966 printf("Deleted: ");
9967 err = print_commit_oneline(commit, id, repo,
9971 * Print paths modified by commit to help
9972 * associate commits with worktree changes.
9974 err = get_changed_paths(&paths, commit,
9979 err = print_commit(commit, id, repo, NULL,
9980 &paths, NULL, 0, 0, refs_idmap, NULL,
9982 got_pathlist_free(&paths,
9983 GOT_PATHLIST_FREE_ALL);
9985 if (worktree == NULL)
9986 printf("work tree: %.*s\n\n",
9987 GOT_WORKTREE_UUID_STRLEN, wt);
9993 got_object_commit_close(commit);
9999 if (wanted_ref != NULL && !found)
10000 err = got_error_fmt(GOT_ERR_NOT_REF, "%s", wanted_ref);
10005 got_ref_list_free(&refs);
10006 got_pathlist_free(&paths, GOT_PATHLIST_FREE_ALL);
10008 got_reflist_object_id_map_free(refs_idmap);
10010 got_object_commit_close(commit);
10015 * Create new temp "logmsg" ref of the backed-out or cherrypicked commit
10016 * identified by id for log messages to prepopulate the editor on commit.
10018 static const struct got_error *
10019 logmsg_ref(struct got_object_id *id, const char *prefix,
10020 struct got_worktree *worktree, struct got_repository *repo)
10022 const struct got_error *err = NULL;
10023 char *idstr, *ref = NULL, *refname = NULL;
10024 int histedit_in_progress;
10025 int rebase_in_progress, merge_in_progress;
10028 * Silenty refuse to create merge reference if any histedit, merge,
10029 * or rebase operation is in progress.
10031 err = got_worktree_histedit_in_progress(&histedit_in_progress,
10035 if (histedit_in_progress)
10038 err = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
10041 if (rebase_in_progress)
10044 err = got_worktree_merge_in_progress(&merge_in_progress, worktree,
10048 if (merge_in_progress)
10051 err = got_object_id_str(&idstr, id);
10055 err = got_worktree_get_logmsg_ref_name(&refname, worktree, prefix);
10059 if (asprintf(&ref, "%s-%s", refname, idstr) == -1) {
10060 err = got_error_from_errno("asprintf");
10064 err = create_ref(ref, got_worktree_get_base_commit_id(worktree),
10074 usage_cherrypick(void)
10076 fprintf(stderr, "usage: %s cherrypick [-lX] [commit-id]\n",
10081 static const struct got_error *
10082 cmd_cherrypick(int argc, char *argv[])
10084 const struct got_error *error = NULL;
10085 struct got_worktree *worktree = NULL;
10086 struct got_repository *repo = NULL;
10087 char *cwd = NULL, *commit_id_str = NULL;
10088 struct got_object_id *commit_id = NULL;
10089 struct got_commit_object *commit = NULL;
10090 struct got_object_qid *pid;
10091 int ch, list_refs = 0, remove_refs = 0;
10092 struct got_update_progress_arg upa;
10093 int *pack_fds = NULL;
10095 while ((ch = getopt(argc, argv, "lX")) != -1) {
10104 usage_cherrypick();
10113 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10114 "unveil", NULL) == -1)
10117 if (list_refs || remove_refs) {
10118 if (argc != 0 && argc != 1)
10119 usage_cherrypick();
10120 } else if (argc != 1)
10121 usage_cherrypick();
10122 if (list_refs && remove_refs)
10123 option_conflict('l', 'X');
10125 cwd = getcwd(NULL, 0);
10127 error = got_error_from_errno("getcwd");
10131 error = got_repo_pack_fds_open(&pack_fds);
10135 error = got_worktree_open(&worktree, cwd);
10137 if (list_refs || remove_refs) {
10138 if (error->code != GOT_ERR_NOT_WORKTREE)
10141 if (error->code == GOT_ERR_NOT_WORKTREE)
10142 error = wrap_not_worktree_error(error,
10143 "cherrypick", cwd);
10148 error = got_repo_open(&repo,
10149 worktree ? got_worktree_get_repo_path(worktree) : cwd,
10154 error = apply_unveil(got_repo_get_path(repo), 0,
10155 worktree ? got_worktree_get_root_path(worktree) : NULL);
10159 if (list_refs || remove_refs) {
10160 error = process_logmsg_refs(GOT_WORKTREE_CHERRYPICK_REF_PREFIX,
10161 GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN,
10162 argc == 1 ? argv[0] : NULL, remove_refs, worktree, repo);
10166 error = got_repo_match_object_id(&commit_id, NULL, argv[0],
10167 GOT_OBJ_TYPE_COMMIT, NULL, repo);
10170 error = got_object_id_str(&commit_id_str, commit_id);
10174 error = got_object_open_as_commit(&commit, repo, commit_id);
10177 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
10178 memset(&upa, 0, sizeof(upa));
10179 error = got_worktree_merge_files(worktree, pid ? &pid->id : NULL,
10180 commit_id, repo, update_progress, &upa, check_cancelled,
10185 if (upa.did_something) {
10186 error = logmsg_ref(commit_id,
10187 GOT_WORKTREE_CHERRYPICK_REF_PREFIX, worktree, repo);
10190 printf("Merged commit %s\n", commit_id_str);
10192 print_merge_progress_stats(&upa);
10196 got_object_commit_close(commit);
10197 free(commit_id_str);
10199 got_worktree_close(worktree);
10201 const struct got_error *close_err = got_repo_close(repo);
10206 const struct got_error *pack_err =
10207 got_repo_pack_fds_close(pack_fds);
10216 usage_backout(void)
10218 fprintf(stderr, "usage: %s backout [-lX] [commit-id]\n", getprogname());
10222 static const struct got_error *
10223 cmd_backout(int argc, char *argv[])
10225 const struct got_error *error = NULL;
10226 struct got_worktree *worktree = NULL;
10227 struct got_repository *repo = NULL;
10228 char *cwd = NULL, *commit_id_str = NULL;
10229 struct got_object_id *commit_id = NULL;
10230 struct got_commit_object *commit = NULL;
10231 struct got_object_qid *pid;
10232 int ch, list_refs = 0, remove_refs = 0;
10233 struct got_update_progress_arg upa;
10234 int *pack_fds = NULL;
10236 while ((ch = getopt(argc, argv, "lX")) != -1) {
10254 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10255 "unveil", NULL) == -1)
10258 if (list_refs || remove_refs) {
10259 if (argc != 0 && argc != 1)
10261 } else if (argc != 1)
10263 if (list_refs && remove_refs)
10264 option_conflict('l', 'X');
10266 cwd = getcwd(NULL, 0);
10268 error = got_error_from_errno("getcwd");
10272 error = got_repo_pack_fds_open(&pack_fds);
10276 error = got_worktree_open(&worktree, cwd);
10278 if (list_refs || remove_refs) {
10279 if (error->code != GOT_ERR_NOT_WORKTREE)
10282 if (error->code == GOT_ERR_NOT_WORKTREE)
10283 error = wrap_not_worktree_error(error,
10289 error = got_repo_open(&repo,
10290 worktree ? got_worktree_get_repo_path(worktree) : cwd,
10295 error = apply_unveil(got_repo_get_path(repo), 0,
10296 worktree ? got_worktree_get_root_path(worktree) : NULL);
10300 if (list_refs || remove_refs) {
10301 error = process_logmsg_refs(GOT_WORKTREE_BACKOUT_REF_PREFIX,
10302 GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN,
10303 argc == 1 ? argv[0] : NULL, remove_refs, worktree, repo);
10307 error = got_repo_match_object_id(&commit_id, NULL, argv[0],
10308 GOT_OBJ_TYPE_COMMIT, NULL, repo);
10311 error = got_object_id_str(&commit_id_str, commit_id);
10315 error = got_object_open_as_commit(&commit, repo, commit_id);
10318 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
10320 error = got_error(GOT_ERR_ROOT_COMMIT);
10324 memset(&upa, 0, sizeof(upa));
10325 error = got_worktree_merge_files(worktree, commit_id, &pid->id,
10326 repo, update_progress, &upa, check_cancelled, NULL);
10330 if (upa.did_something) {
10331 error = logmsg_ref(commit_id, GOT_WORKTREE_BACKOUT_REF_PREFIX,
10335 printf("Backed out commit %s\n", commit_id_str);
10337 print_merge_progress_stats(&upa);
10341 got_object_commit_close(commit);
10342 free(commit_id_str);
10344 got_worktree_close(worktree);
10346 const struct got_error *close_err = got_repo_close(repo);
10351 const struct got_error *pack_err =
10352 got_repo_pack_fds_close(pack_fds);
10362 fprintf(stderr, "usage: %s rebase [-aclX] [branch]\n", getprogname());
10367 trim_logmsg(char *logmsg, int limit)
10372 len = strlen(logmsg);
10375 logmsg[len] = '\0';
10376 nl = strchr(logmsg, '\n');
10381 static const struct got_error *
10382 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
10384 const struct got_error *err;
10385 char *logmsg0 = NULL;
10388 err = got_object_commit_get_logmsg(&logmsg0, commit);
10393 while (isspace((unsigned char)s[0]))
10396 *logmsg = strdup(s);
10397 if (*logmsg == NULL) {
10398 err = got_error_from_errno("strdup");
10402 trim_logmsg(*logmsg, limit);
10408 static const struct got_error *
10409 show_rebase_merge_conflict(struct got_object_id *id,
10410 struct got_repository *repo)
10412 const struct got_error *err;
10413 struct got_commit_object *commit = NULL;
10414 char *id_str = NULL, *logmsg = NULL;
10416 err = got_object_open_as_commit(&commit, repo, id);
10420 err = got_object_id_str(&id_str, id);
10426 err = get_short_logmsg(&logmsg, 42, commit);
10430 printf("%s -> merge conflict: %s\n", id_str, logmsg);
10433 got_object_commit_close(commit);
10438 static const struct got_error *
10439 show_rebase_progress(struct got_commit_object *commit,
10440 struct got_object_id *old_id, struct got_object_id *new_id)
10442 const struct got_error *err;
10443 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
10445 err = got_object_id_str(&old_id_str, old_id);
10450 err = got_object_id_str(&new_id_str, new_id);
10455 old_id_str[12] = '\0';
10457 new_id_str[12] = '\0';
10459 err = get_short_logmsg(&logmsg, 42, commit);
10463 printf("%s -> %s: %s\n", old_id_str,
10464 new_id_str ? new_id_str : "no-op change", logmsg);
10472 static const struct got_error *
10473 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
10474 struct got_reference *branch, struct got_reference *tmp_branch,
10475 struct got_repository *repo, int create_backup)
10477 printf("Switching work tree to %s\n", got_ref_get_name(branch));
10478 return got_worktree_rebase_complete(worktree, fileindex,
10479 tmp_branch, branch, repo, create_backup);
10482 static const struct got_error *
10483 rebase_commit(struct got_pathlist_head *merged_paths,
10484 struct got_worktree *worktree, struct got_fileindex *fileindex,
10485 struct got_reference *tmp_branch, const char *committer,
10486 struct got_object_id *commit_id, struct got_repository *repo)
10488 const struct got_error *error;
10489 struct got_commit_object *commit;
10490 struct got_object_id *new_commit_id;
10492 error = got_object_open_as_commit(&commit, repo, commit_id);
10496 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
10497 worktree, fileindex, tmp_branch, committer, commit, commit_id,
10500 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
10502 error = show_rebase_progress(commit, commit_id, NULL);
10504 error = show_rebase_progress(commit, commit_id, new_commit_id);
10505 free(new_commit_id);
10508 got_object_commit_close(commit);
10512 struct check_path_prefix_arg {
10513 const char *path_prefix;
10518 static const struct got_error *
10519 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
10520 struct got_blob_object *blob2, FILE *f1, FILE *f2,
10521 struct got_object_id *id1, struct got_object_id *id2,
10522 const char *path1, const char *path2,
10523 mode_t mode1, mode_t mode2, struct got_repository *repo)
10525 struct check_path_prefix_arg *a = arg;
10527 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
10528 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
10529 return got_error(a->errcode);
10534 static const struct got_error *
10535 check_path_prefix(struct got_object_id *parent_id,
10536 struct got_object_id *commit_id, const char *path_prefix,
10537 int errcode, struct got_repository *repo)
10539 const struct got_error *err;
10540 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
10541 struct got_commit_object *commit = NULL, *parent_commit = NULL;
10542 struct check_path_prefix_arg cpp_arg;
10544 if (got_path_is_root_dir(path_prefix))
10547 err = got_object_open_as_commit(&commit, repo, commit_id);
10551 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
10555 err = got_object_open_as_tree(&tree1, repo,
10556 got_object_commit_get_tree_id(parent_commit));
10560 err = got_object_open_as_tree(&tree2, repo,
10561 got_object_commit_get_tree_id(commit));
10565 cpp_arg.path_prefix = path_prefix;
10566 while (cpp_arg.path_prefix[0] == '/')
10567 cpp_arg.path_prefix++;
10568 cpp_arg.len = strlen(cpp_arg.path_prefix);
10569 cpp_arg.errcode = errcode;
10570 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
10571 check_path_prefix_in_diff, &cpp_arg, 0);
10574 got_object_tree_close(tree1);
10576 got_object_tree_close(tree2);
10578 got_object_commit_close(commit);
10580 got_object_commit_close(parent_commit);
10584 static const struct got_error *
10585 collect_commits(struct got_object_id_queue *commits,
10586 struct got_object_id *initial_commit_id,
10587 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
10588 const char *path_prefix, int path_prefix_errcode,
10589 struct got_repository *repo)
10591 const struct got_error *err = NULL;
10592 struct got_commit_graph *graph = NULL;
10593 struct got_object_id parent_id, commit_id;
10594 struct got_object_qid *qid;
10596 err = got_commit_graph_open(&graph, "/", 1);
10600 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
10601 check_cancelled, NULL);
10605 memcpy(&commit_id, initial_commit_id, sizeof(commit_id));
10606 while (got_object_id_cmp(&commit_id, iter_stop_id) != 0) {
10607 err = got_commit_graph_iter_next(&parent_id, graph, repo,
10608 check_cancelled, NULL);
10610 if (err->code == GOT_ERR_ITER_COMPLETED) {
10611 err = got_error_msg(GOT_ERR_ANCESTRY,
10612 "ran out of commits to rebase before "
10613 "youngest common ancestor commit has "
10614 "been reached?!?");
10618 err = check_path_prefix(&parent_id, &commit_id,
10619 path_prefix, path_prefix_errcode, repo);
10623 err = got_object_qid_alloc(&qid, &commit_id);
10626 STAILQ_INSERT_HEAD(commits, qid, entry);
10628 memcpy(&commit_id, &parent_id, sizeof(commit_id));
10632 got_commit_graph_close(graph);
10636 static const struct got_error *
10637 get_commit_brief_str(char **brief_str, struct got_commit_object *commit)
10639 const struct got_error *err = NULL;
10640 time_t committer_time;
10642 char datebuf[11]; /* YYYY-MM-DD + NUL */
10643 char *author0 = NULL, *author, *smallerthan;
10644 char *logmsg0 = NULL, *logmsg, *newline;
10646 committer_time = got_object_commit_get_committer_time(commit);
10647 if (gmtime_r(&committer_time, &tm) == NULL)
10648 return got_error_from_errno("gmtime_r");
10649 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d", &tm) == 0)
10650 return got_error(GOT_ERR_NO_SPACE);
10652 author0 = strdup(got_object_commit_get_author(commit));
10653 if (author0 == NULL)
10654 return got_error_from_errno("strdup");
10656 smallerthan = strchr(author, '<');
10657 if (smallerthan && smallerthan[1] != '\0')
10658 author = smallerthan + 1;
10659 author[strcspn(author, "@>")] = '\0';
10661 err = got_object_commit_get_logmsg(&logmsg0, commit);
10665 while (*logmsg == '\n')
10667 newline = strchr(logmsg, '\n');
10671 if (asprintf(brief_str, "%s %s %s",
10672 datebuf, author, logmsg) == -1)
10673 err = got_error_from_errno("asprintf");
10680 static const struct got_error *
10681 delete_backup_ref(struct got_reference *ref, struct got_object_id *id,
10682 struct got_repository *repo)
10684 const struct got_error *err;
10687 err = got_object_id_str(&id_str, id);
10691 err = got_ref_delete(ref, repo);
10695 printf("Deleted %s: %s\n", got_ref_get_name(ref), id_str);
10701 static const struct got_error *
10702 print_backup_ref(const char *branch_name, const char *new_id_str,
10703 struct got_object_id *old_commit_id, struct got_commit_object *old_commit,
10704 struct got_reflist_object_id_map *refs_idmap,
10705 struct got_repository *repo)
10707 const struct got_error *err = NULL;
10708 struct got_reflist_head *refs;
10709 char *refs_str = NULL;
10710 struct got_object_id *new_commit_id = NULL;
10711 struct got_commit_object *new_commit = NULL;
10712 char *new_commit_brief_str = NULL;
10713 struct got_object_id *yca_id = NULL;
10714 struct got_commit_object *yca_commit = NULL;
10715 char *yca_id_str = NULL, *yca_brief_str = NULL;
10716 char *custom_refs_str;
10718 if (asprintf(&custom_refs_str, "formerly %s", branch_name) == -1)
10719 return got_error_from_errno("asprintf");
10721 err = print_commit(old_commit, old_commit_id, repo, NULL, NULL, NULL,
10722 0, 0, refs_idmap, custom_refs_str, NULL);
10726 err = got_object_resolve_id_str(&new_commit_id, repo, new_id_str);
10730 refs = got_reflist_object_id_map_lookup(refs_idmap, new_commit_id);
10732 err = build_refs_str(&refs_str, refs, new_commit_id, repo, 0);
10737 err = got_object_open_as_commit(&new_commit, repo, new_commit_id);
10741 err = get_commit_brief_str(&new_commit_brief_str, new_commit);
10745 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
10746 old_commit_id, new_commit_id, 1, repo, check_cancelled, NULL);
10750 printf("has become commit %s%s%s%s\n %s\n", new_id_str,
10751 refs_str ? " (" : "", refs_str ? refs_str : "",
10752 refs_str ? ")" : "", new_commit_brief_str);
10753 if (yca_id && got_object_id_cmp(yca_id, new_commit_id) != 0 &&
10754 got_object_id_cmp(yca_id, old_commit_id) != 0) {
10758 err = got_object_open_as_commit(&yca_commit, repo, yca_id);
10762 err = get_commit_brief_str(&yca_brief_str, yca_commit);
10766 err = got_object_id_str(&yca_id_str, yca_id);
10770 refs = got_reflist_object_id_map_lookup(refs_idmap, yca_id);
10772 err = build_refs_str(&refs_str, refs, yca_id, repo, 0);
10776 printf("history forked at %s%s%s%s\n %s\n",
10778 refs_str ? " (" : "", refs_str ? refs_str : "",
10779 refs_str ? ")" : "", yca_brief_str);
10782 free(custom_refs_str);
10783 free(new_commit_id);
10787 free(yca_brief_str);
10789 got_object_commit_close(new_commit);
10791 got_object_commit_close(yca_commit);
10796 static const struct got_error *
10797 worktree_has_logmsg_ref(const char *caller, struct got_worktree *worktree,
10798 struct got_repository *repo)
10800 const struct got_error *err;
10801 struct got_reflist_head refs;
10802 struct got_reflist_entry *re;
10803 char *uuidstr = NULL;
10804 static char msg[160];
10808 err = got_worktree_get_uuid(&uuidstr, worktree);
10812 err = got_ref_list(&refs, repo, "refs/got/worktree",
10813 got_ref_cmp_by_name, repo);
10817 TAILQ_FOREACH(re, &refs, entry) {
10818 const char *cmd, *refname, *type;
10820 refname = got_ref_get_name(re->ref);
10822 if (strncmp(refname, GOT_WORKTREE_CHERRYPICK_REF_PREFIX,
10823 GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN) == 0) {
10824 refname += GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN + 1;
10825 cmd = "cherrypick";
10826 type = "cherrypicked";
10827 } else if (strncmp(refname, GOT_WORKTREE_BACKOUT_REF_PREFIX,
10828 GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN) == 0) {
10829 refname += GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN + 1;
10831 type = "backed-out";
10835 if (strncmp(refname, uuidstr, GOT_WORKTREE_UUID_STRLEN) != 0)
10838 snprintf(msg, sizeof(msg),
10839 "work tree has references created by %s commits which "
10840 "must be removed with 'got %s -X' before running the %s "
10841 "command", type, cmd, caller);
10842 err = got_error_msg(GOT_ERR_WORKTREE_META, msg);
10848 got_ref_list_free(&refs);
10852 static const struct got_error *
10853 process_backup_refs(const char *backup_ref_prefix,
10854 const char *wanted_branch_name,
10855 int delete, struct got_repository *repo)
10857 const struct got_error *err;
10858 struct got_reflist_head refs, backup_refs;
10859 struct got_reflist_entry *re;
10860 const size_t backup_ref_prefix_len = strlen(backup_ref_prefix);
10861 struct got_object_id *old_commit_id = NULL;
10862 char *branch_name = NULL;
10863 struct got_commit_object *old_commit = NULL;
10864 struct got_reflist_object_id_map *refs_idmap = NULL;
10865 int wanted_branch_found = 0;
10868 TAILQ_INIT(&backup_refs);
10870 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
10874 err = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
10878 if (wanted_branch_name) {
10879 if (strncmp(wanted_branch_name, "refs/heads/", 11) == 0)
10880 wanted_branch_name += 11;
10883 err = got_ref_list(&backup_refs, repo, backup_ref_prefix,
10884 got_ref_cmp_by_commit_timestamp_descending, repo);
10888 TAILQ_FOREACH(re, &backup_refs, entry) {
10889 const char *refname = got_ref_get_name(re->ref);
10892 err = check_cancelled(NULL);
10896 err = got_ref_resolve(&old_commit_id, repo, re->ref);
10900 err = got_object_open_as_commit(&old_commit, repo,
10905 if (strncmp(backup_ref_prefix, refname,
10906 backup_ref_prefix_len) == 0)
10907 refname += backup_ref_prefix_len;
10909 while (refname[0] == '/')
10912 branch_name = strdup(refname);
10913 if (branch_name == NULL) {
10914 err = got_error_from_errno("strdup");
10917 slash = strrchr(branch_name, '/');
10920 refname += strlen(branch_name) + 1;
10923 if (wanted_branch_name == NULL ||
10924 strcmp(wanted_branch_name, branch_name) == 0) {
10925 wanted_branch_found = 1;
10927 err = delete_backup_ref(re->ref,
10928 old_commit_id, repo);
10930 err = print_backup_ref(branch_name, refname,
10931 old_commit_id, old_commit, refs_idmap,
10938 free(old_commit_id);
10939 old_commit_id = NULL;
10941 branch_name = NULL;
10942 got_object_commit_close(old_commit);
10946 if (wanted_branch_name && !wanted_branch_found) {
10947 err = got_error_fmt(GOT_ERR_NOT_REF,
10948 "%s/%s/", backup_ref_prefix, wanted_branch_name);
10952 got_reflist_object_id_map_free(refs_idmap);
10953 got_ref_list_free(&refs);
10954 got_ref_list_free(&backup_refs);
10955 free(old_commit_id);
10958 got_object_commit_close(old_commit);
10962 static const struct got_error *
10963 abort_progress(void *arg, unsigned char status, const char *path)
10966 * Unversioned files should not clutter progress output when
10967 * an operation is aborted.
10969 if (status == GOT_STATUS_UNVERSIONED)
10972 return update_progress(arg, status, path);
10975 static const struct got_error *
10976 cmd_rebase(int argc, char *argv[])
10978 const struct got_error *error = NULL;
10979 struct got_worktree *worktree = NULL;
10980 struct got_repository *repo = NULL;
10981 struct got_fileindex *fileindex = NULL;
10982 char *cwd = NULL, *committer = NULL, *gitconfig_path = NULL;
10983 struct got_reference *branch = NULL;
10984 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
10985 struct got_object_id *commit_id = NULL, *parent_id = NULL;
10986 struct got_object_id *resume_commit_id = NULL;
10987 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
10988 struct got_object_id *head_commit_id = NULL;
10989 struct got_reference *head_ref = NULL;
10990 struct got_commit_object *commit = NULL;
10991 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
10992 int histedit_in_progress = 0, merge_in_progress = 0;
10993 int create_backup = 1, list_backups = 0, delete_backups = 0;
10994 struct got_object_id_queue commits;
10995 struct got_pathlist_head merged_paths;
10996 const struct got_object_id_queue *parent_ids;
10997 struct got_object_qid *qid, *pid;
10998 struct got_update_progress_arg upa;
10999 int *pack_fds = NULL;
11001 STAILQ_INIT(&commits);
11002 TAILQ_INIT(&merged_paths);
11003 memset(&upa, 0, sizeof(upa));
11005 while ((ch = getopt(argc, argv, "aclX")) != -1) {
11011 continue_rebase = 1;
11017 delete_backups = 1;
11029 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
11030 "unveil", NULL) == -1)
11033 if (list_backups) {
11035 option_conflict('l', 'a');
11036 if (continue_rebase)
11037 option_conflict('l', 'c');
11038 if (delete_backups)
11039 option_conflict('l', 'X');
11040 if (argc != 0 && argc != 1)
11042 } else if (delete_backups) {
11044 option_conflict('X', 'a');
11045 if (continue_rebase)
11046 option_conflict('X', 'c');
11048 option_conflict('l', 'X');
11049 if (argc != 0 && argc != 1)
11052 if (abort_rebase && continue_rebase)
11054 else if (abort_rebase || continue_rebase) {
11057 } else if (argc != 1)
11061 cwd = getcwd(NULL, 0);
11063 error = got_error_from_errno("getcwd");
11067 error = got_repo_pack_fds_open(&pack_fds);
11071 error = got_worktree_open(&worktree, cwd);
11073 if (list_backups || delete_backups) {
11074 if (error->code != GOT_ERR_NOT_WORKTREE)
11077 if (error->code == GOT_ERR_NOT_WORKTREE)
11078 error = wrap_not_worktree_error(error,
11084 error = get_gitconfig_path(&gitconfig_path);
11087 error = got_repo_open(&repo,
11088 worktree ? got_worktree_get_repo_path(worktree) : cwd,
11089 gitconfig_path, pack_fds);
11093 if (worktree != NULL && !list_backups && !delete_backups) {
11094 error = worktree_has_logmsg_ref("rebase", worktree, repo);
11099 error = get_author(&committer, repo, worktree);
11100 if (error && error->code != GOT_ERR_COMMIT_NO_AUTHOR)
11103 error = apply_unveil(got_repo_get_path(repo), 0,
11104 worktree ? got_worktree_get_root_path(worktree) : NULL);
11108 if (list_backups || delete_backups) {
11109 error = process_backup_refs(
11110 GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
11111 argc == 1 ? argv[0] : NULL, delete_backups, repo);
11112 goto done; /* nothing else to do */
11115 error = got_worktree_histedit_in_progress(&histedit_in_progress,
11119 if (histedit_in_progress) {
11120 error = got_error(GOT_ERR_HISTEDIT_BUSY);
11124 error = got_worktree_merge_in_progress(&merge_in_progress,
11128 if (merge_in_progress) {
11129 error = got_error(GOT_ERR_MERGE_BUSY);
11133 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
11137 if (abort_rebase) {
11138 if (!rebase_in_progress) {
11139 error = got_error(GOT_ERR_NOT_REBASING);
11142 error = got_worktree_rebase_continue(&resume_commit_id,
11143 &new_base_branch, &tmp_branch, &branch, &fileindex,
11147 printf("Switching work tree to %s\n",
11148 got_ref_get_symref_target(new_base_branch));
11149 error = got_worktree_rebase_abort(worktree, fileindex, repo,
11150 new_base_branch, abort_progress, &upa);
11153 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
11154 print_merge_progress_stats(&upa);
11155 goto done; /* nothing else to do */
11158 if (continue_rebase) {
11159 if (!rebase_in_progress) {
11160 error = got_error(GOT_ERR_NOT_REBASING);
11163 error = got_worktree_rebase_continue(&resume_commit_id,
11164 &new_base_branch, &tmp_branch, &branch, &fileindex,
11169 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
11170 committer, resume_commit_id, repo);
11174 yca_id = got_object_id_dup(resume_commit_id);
11175 if (yca_id == NULL) {
11176 error = got_error_from_errno("got_object_id_dup");
11180 error = got_ref_open(&branch, repo, argv[0], 0);
11183 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
11184 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
11185 "will not rebase a branch which lives outside "
11186 "the \"refs/heads/\" reference namespace");
11191 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
11195 if (!continue_rebase) {
11196 struct got_object_id *base_commit_id;
11198 error = got_ref_open(&head_ref, repo,
11199 got_worktree_get_head_ref_name(worktree), 0);
11202 error = got_ref_resolve(&head_commit_id, repo, head_ref);
11205 base_commit_id = got_worktree_get_base_commit_id(worktree);
11206 if (got_object_id_cmp(base_commit_id, head_commit_id) != 0) {
11207 error = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
11211 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
11212 base_commit_id, branch_head_commit_id, 1, repo,
11213 check_cancelled, NULL);
11215 if (error->code == GOT_ERR_ANCESTRY) {
11216 error = got_error_msg(GOT_ERR_ANCESTRY,
11217 "specified branch shares no common "
11218 "ancestry with work tree's branch");
11223 error = check_same_branch(base_commit_id, branch, yca_id, repo);
11225 if (error->code != GOT_ERR_ANCESTRY)
11229 struct got_pathlist_head paths;
11230 printf("%s is already based on %s\n",
11231 got_ref_get_name(branch),
11232 got_worktree_get_head_ref_name(worktree));
11233 error = switch_head_ref(branch, branch_head_commit_id,
11237 error = got_worktree_set_base_commit_id(worktree, repo,
11238 branch_head_commit_id);
11241 TAILQ_INIT(&paths);
11242 error = got_pathlist_append(&paths, "", NULL);
11245 error = got_worktree_checkout_files(worktree,
11246 &paths, repo, update_progress, &upa,
11247 check_cancelled, NULL);
11248 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
11251 if (upa.did_something) {
11253 error = got_object_id_str(&id_str,
11254 branch_head_commit_id);
11257 printf("Updated to %s: %s\n",
11258 got_worktree_get_head_ref_name(worktree),
11262 printf("Already up-to-date\n");
11263 print_update_progress_stats(&upa);
11268 commit_id = branch_head_commit_id;
11269 error = got_object_open_as_commit(&commit, repo, commit_id);
11273 parent_ids = got_object_commit_get_parent_ids(commit);
11274 pid = STAILQ_FIRST(parent_ids);
11276 error = collect_commits(&commits, commit_id, &pid->id,
11277 yca_id, got_worktree_get_path_prefix(worktree),
11278 GOT_ERR_REBASE_PATH, repo);
11283 got_object_commit_close(commit);
11286 if (!continue_rebase) {
11287 error = got_worktree_rebase_prepare(&new_base_branch,
11288 &tmp_branch, &fileindex, worktree, branch, repo);
11293 if (STAILQ_EMPTY(&commits)) {
11294 if (continue_rebase) {
11295 error = rebase_complete(worktree, fileindex,
11296 branch, tmp_branch, repo, create_backup);
11299 /* Fast-forward the reference of the branch. */
11300 struct got_object_id *new_head_commit_id;
11302 error = got_ref_resolve(&new_head_commit_id, repo,
11306 error = got_object_id_str(&id_str, new_head_commit_id);
11309 printf("Forwarding %s to commit %s\n",
11310 got_ref_get_name(branch), id_str);
11312 error = got_ref_change_ref(branch,
11313 new_head_commit_id);
11316 /* No backup needed since objects did not change. */
11322 STAILQ_FOREACH(qid, &commits, entry) {
11324 commit_id = &qid->id;
11325 parent_id = pid ? &pid->id : yca_id;
11328 memset(&upa, 0, sizeof(upa));
11329 error = got_worktree_rebase_merge_files(&merged_paths,
11330 worktree, fileindex, parent_id, commit_id, repo,
11331 update_progress, &upa, check_cancelled, NULL);
11335 print_merge_progress_stats(&upa);
11336 if (upa.conflicts > 0 || upa.missing > 0 ||
11337 upa.not_deleted > 0 || upa.unversioned > 0) {
11338 if (upa.conflicts > 0) {
11339 error = show_rebase_merge_conflict(&qid->id,
11344 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
11348 error = rebase_commit(&merged_paths, worktree, fileindex,
11349 tmp_branch, committer, commit_id, repo);
11350 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
11355 if (upa.conflicts > 0 || upa.missing > 0 ||
11356 upa.not_deleted > 0 || upa.unversioned > 0) {
11357 error = got_worktree_rebase_postpone(worktree, fileindex);
11360 if (upa.conflicts > 0 && upa.missing == 0 &&
11361 upa.not_deleted == 0 && upa.unversioned == 0) {
11362 error = got_error_msg(GOT_ERR_CONFLICTS,
11363 "conflicts must be resolved before rebasing "
11365 } else if (upa.conflicts > 0) {
11366 error = got_error_msg(GOT_ERR_CONFLICTS,
11367 "conflicts must be resolved before rebasing "
11368 "can continue; changes destined for some "
11369 "files were not yet merged and should be "
11370 "merged manually if required before the "
11371 "rebase operation is continued");
11373 error = got_error_msg(GOT_ERR_CONFLICTS,
11374 "changes destined for some files were not "
11375 "yet merged and should be merged manually "
11376 "if required before the rebase operation "
11380 error = rebase_complete(worktree, fileindex, branch,
11381 tmp_branch, repo, create_backup);
11385 free(gitconfig_path);
11386 got_object_id_queue_free(&commits);
11387 free(branch_head_commit_id);
11388 free(resume_commit_id);
11389 free(head_commit_id);
11392 got_object_commit_close(commit);
11394 got_ref_close(branch);
11395 if (new_base_branch)
11396 got_ref_close(new_base_branch);
11398 got_ref_close(tmp_branch);
11400 got_ref_close(head_ref);
11402 got_worktree_close(worktree);
11404 const struct got_error *close_err = got_repo_close(repo);
11409 const struct got_error *pack_err =
11410 got_repo_pack_fds_close(pack_fds);
11418 usage_histedit(void)
11420 fprintf(stderr, "usage: %s histedit [-acdeflmX] [-F histedit-script] "
11421 "[branch]\n", getprogname());
11425 #define GOT_HISTEDIT_PICK 'p'
11426 #define GOT_HISTEDIT_EDIT 'e'
11427 #define GOT_HISTEDIT_FOLD 'f'
11428 #define GOT_HISTEDIT_DROP 'd'
11429 #define GOT_HISTEDIT_MESG 'm'
11431 static const struct got_histedit_cmd {
11432 unsigned char code;
11435 } got_histedit_cmds[] = {
11436 { GOT_HISTEDIT_PICK, "pick", "use commit" },
11437 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
11438 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
11440 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
11441 { GOT_HISTEDIT_MESG, "mesg",
11442 "single-line log message for commit above (open editor if empty)" },
11445 struct got_histedit_list_entry {
11446 TAILQ_ENTRY(got_histedit_list_entry) entry;
11447 struct got_object_id *commit_id;
11448 const struct got_histedit_cmd *cmd;
11451 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
11453 static const struct got_error *
11454 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
11455 FILE *f, struct got_repository *repo)
11457 const struct got_error *err = NULL;
11458 char *logmsg = NULL, *id_str = NULL;
11459 struct got_commit_object *commit = NULL;
11462 err = got_object_open_as_commit(&commit, repo, commit_id);
11466 err = get_short_logmsg(&logmsg, 34, commit);
11470 err = got_object_id_str(&id_str, commit_id);
11474 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
11476 err = got_ferror(f, GOT_ERR_IO);
11479 got_object_commit_close(commit);
11485 static const struct got_error *
11486 histedit_write_commit_list(struct got_object_id_queue *commits,
11487 FILE *f, int edit_logmsg_only, int fold_only, int drop_only,
11488 int edit_only, struct got_repository *repo)
11490 const struct got_error *err = NULL;
11491 struct got_object_qid *qid;
11492 const char *histedit_cmd = NULL;
11494 if (STAILQ_EMPTY(commits))
11495 return got_error(GOT_ERR_EMPTY_HISTEDIT);
11497 STAILQ_FOREACH(qid, commits, entry) {
11498 histedit_cmd = got_histedit_cmds[0].name;
11500 histedit_cmd = "drop";
11501 else if (edit_only)
11502 histedit_cmd = "edit";
11503 else if (fold_only && STAILQ_NEXT(qid, entry) != NULL)
11504 histedit_cmd = "fold";
11505 err = histedit_write_commit(&qid->id, histedit_cmd, f, repo);
11508 if (edit_logmsg_only) {
11509 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
11511 err = got_ferror(f, GOT_ERR_IO);
11520 static const struct got_error *
11521 write_cmd_list(FILE *f, const char *branch_name,
11522 struct got_object_id_queue *commits)
11524 const struct got_error *err = NULL;
11528 struct got_object_qid *qid;
11530 qid = STAILQ_FIRST(commits);
11531 err = got_object_id_str(&id_str, &qid->id);
11536 "# Editing the history of branch '%s' starting at\n"
11538 "# Commits will be processed in order from top to "
11539 "bottom of this file.\n", branch_name, id_str);
11541 err = got_ferror(f, GOT_ERR_IO);
11545 n = fprintf(f, "# Available histedit commands:\n");
11547 err = got_ferror(f, GOT_ERR_IO);
11551 for (i = 0; i < nitems(got_histedit_cmds); i++) {
11552 const struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
11553 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
11556 err = got_ferror(f, GOT_ERR_IO);
11565 static const struct got_error *
11566 histedit_syntax_error(int lineno)
11568 static char msg[42];
11571 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
11573 if (ret < 0 || (size_t)ret >= sizeof(msg))
11574 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
11576 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
11579 static const struct got_error *
11580 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
11581 char *logmsg, struct got_repository *repo)
11583 const struct got_error *err;
11584 struct got_commit_object *folded_commit = NULL;
11585 char *id_str, *folded_logmsg = NULL;
11587 err = got_object_id_str(&id_str, hle->commit_id);
11591 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
11595 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
11598 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
11599 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
11600 folded_logmsg) == -1) {
11601 err = got_error_from_errno("asprintf");
11605 got_object_commit_close(folded_commit);
11607 free(folded_logmsg);
11611 static struct got_histedit_list_entry *
11612 get_folded_commits(struct got_histedit_list_entry *hle)
11614 struct got_histedit_list_entry *prev, *folded = NULL;
11616 prev = TAILQ_PREV(hle, got_histedit_list, entry);
11617 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
11618 prev->cmd->code == GOT_HISTEDIT_DROP)) {
11619 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
11621 prev = TAILQ_PREV(prev, got_histedit_list, entry);
11627 static const struct got_error *
11628 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
11629 struct got_repository *repo)
11631 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
11632 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
11633 const struct got_error *err = NULL;
11634 struct got_commit_object *commit = NULL;
11637 struct got_histedit_list_entry *folded = NULL;
11639 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
11643 folded = get_folded_commits(hle);
11645 while (folded != hle) {
11646 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
11647 folded = TAILQ_NEXT(folded, entry);
11650 err = append_folded_commit_msg(&new_msg, folded,
11656 folded = TAILQ_NEXT(folded, entry);
11660 err = got_object_id_str(&id_str, hle->commit_id);
11663 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
11666 logmsg_len = asprintf(&new_msg,
11667 "%s\n# original log message of commit %s: %s",
11668 logmsg ? logmsg : "", id_str, orig_logmsg);
11669 if (logmsg_len == -1) {
11670 err = got_error_from_errno("asprintf");
11676 err = got_object_id_str(&id_str, hle->commit_id);
11680 err = got_opentemp_named_fd(&logmsg_path, &fd,
11681 GOT_TMPDIR_STR "/got-logmsg", "");
11685 write(fd, logmsg, logmsg_len);
11688 err = get_editor(&editor);
11692 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg,
11695 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
11698 hle->logmsg = strdup(new_msg);
11699 if (hle->logmsg == NULL)
11700 err = got_error_from_errno("strdup");
11703 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
11704 err = got_error_from_errno2("unlink", logmsg_path);
11710 got_object_commit_close(commit);
11714 static const struct got_error *
11715 histedit_parse_list(struct got_histedit_list *histedit_cmds,
11716 FILE *f, struct got_repository *repo)
11718 const struct got_error *err = NULL;
11719 char *line = NULL, *p, *end;
11722 int lineno = 0, lastcmd = -1;
11723 const struct got_histedit_cmd *cmd;
11724 struct got_object_id *commit_id = NULL;
11725 struct got_histedit_list_entry *hle = NULL;
11728 len = getline(&line, &size, f);
11730 const struct got_error *getline_err;
11733 getline_err = got_error_from_errno("getline");
11734 err = got_ferror(f, getline_err->code);
11739 while (isspace((unsigned char)p[0]))
11741 if (p[0] == '#' || p[0] == '\0') {
11747 for (i = 0; i < nitems(got_histedit_cmds); i++) {
11748 cmd = &got_histedit_cmds[i];
11749 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
11750 isspace((unsigned char)p[strlen(cmd->name)])) {
11751 p += strlen(cmd->name);
11754 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
11759 if (i == nitems(got_histedit_cmds)) {
11760 err = histedit_syntax_error(lineno);
11763 while (isspace((unsigned char)p[0]))
11765 if (cmd->code == GOT_HISTEDIT_MESG) {
11766 if (lastcmd != GOT_HISTEDIT_PICK &&
11767 lastcmd != GOT_HISTEDIT_EDIT) {
11768 err = got_error(GOT_ERR_HISTEDIT_CMD);
11771 if (p[0] == '\0') {
11772 err = histedit_edit_logmsg(hle, repo);
11776 hle->logmsg = strdup(p);
11777 if (hle->logmsg == NULL) {
11778 err = got_error_from_errno("strdup");
11784 lastcmd = cmd->code;
11788 while (end[0] && !isspace((unsigned char)end[0]))
11792 err = got_object_resolve_id_str(&commit_id, repo, p);
11794 /* override error code */
11795 err = histedit_syntax_error(lineno);
11799 hle = malloc(sizeof(*hle));
11801 err = got_error_from_errno("malloc");
11805 hle->commit_id = commit_id;
11806 hle->logmsg = NULL;
11810 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
11811 lastcmd = cmd->code;
11819 static const struct got_error *
11820 histedit_check_script(struct got_histedit_list *histedit_cmds,
11821 struct got_object_id_queue *commits, struct got_repository *repo)
11823 const struct got_error *err = NULL;
11824 struct got_object_qid *qid;
11825 struct got_histedit_list_entry *hle;
11826 static char msg[92];
11829 if (TAILQ_EMPTY(histedit_cmds))
11830 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
11831 "histedit script contains no commands");
11832 if (STAILQ_EMPTY(commits))
11833 return got_error(GOT_ERR_EMPTY_HISTEDIT);
11835 TAILQ_FOREACH(hle, histedit_cmds, entry) {
11836 struct got_histedit_list_entry *hle2;
11837 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
11840 if (got_object_id_cmp(hle->commit_id,
11841 hle2->commit_id) != 0)
11843 err = got_object_id_str(&id_str, hle->commit_id);
11846 snprintf(msg, sizeof(msg), "commit %s is listed "
11847 "more than once in histedit script", id_str);
11849 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
11853 STAILQ_FOREACH(qid, commits, entry) {
11854 TAILQ_FOREACH(hle, histedit_cmds, entry) {
11855 if (got_object_id_cmp(&qid->id, hle->commit_id) == 0)
11859 err = got_object_id_str(&id_str, &qid->id);
11862 snprintf(msg, sizeof(msg),
11863 "commit %s missing from histedit script", id_str);
11865 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
11869 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
11870 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
11871 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
11872 "last commit in histedit script cannot be folded");
11877 static const struct got_error *
11878 histedit_run_editor(struct got_histedit_list *histedit_cmds,
11879 const char *path, struct got_object_id_queue *commits,
11880 struct got_repository *repo)
11882 const struct got_error *err = NULL;
11886 err = get_editor(&editor);
11890 if (spawn_editor(editor, path) == -1) {
11891 err = got_error_from_errno("failed spawning editor");
11895 f = fopen(path, "re");
11897 err = got_error_from_errno("fopen");
11900 err = histedit_parse_list(histedit_cmds, f, repo);
11904 err = histedit_check_script(histedit_cmds, commits, repo);
11906 if (f && fclose(f) == EOF && err == NULL)
11907 err = got_error_from_errno("fclose");
11912 static const struct got_error *
11913 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
11914 struct got_object_id_queue *, const char *, const char *,
11915 struct got_repository *);
11917 static const struct got_error *
11918 histedit_edit_script(struct got_histedit_list *histedit_cmds,
11919 struct got_object_id_queue *commits, const char *branch_name,
11920 int edit_logmsg_only, int fold_only, int drop_only, int edit_only,
11921 struct got_repository *repo)
11923 const struct got_error *err;
11927 err = got_opentemp_named(&path, &f, "got-histedit", "");
11931 err = write_cmd_list(f, branch_name, commits);
11935 err = histedit_write_commit_list(commits, f, edit_logmsg_only,
11936 fold_only, drop_only, edit_only, repo);
11940 if (drop_only || edit_logmsg_only || fold_only || edit_only) {
11942 err = histedit_parse_list(histedit_cmds, f, repo);
11944 if (fclose(f) == EOF) {
11945 err = got_error_from_errno("fclose");
11949 err = histedit_run_editor(histedit_cmds, path, commits, repo);
11951 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
11952 err->code != GOT_ERR_HISTEDIT_CMD)
11954 err = histedit_edit_list_retry(histedit_cmds, err,
11955 commits, path, branch_name, repo);
11959 if (f && fclose(f) == EOF && err == NULL)
11960 err = got_error_from_errno("fclose");
11961 if (path && unlink(path) != 0 && err == NULL)
11962 err = got_error_from_errno2("unlink", path);
11967 static const struct got_error *
11968 histedit_save_list(struct got_histedit_list *histedit_cmds,
11969 struct got_worktree *worktree, struct got_repository *repo)
11971 const struct got_error *err = NULL;
11974 struct got_histedit_list_entry *hle;
11975 struct got_commit_object *commit = NULL;
11977 err = got_worktree_get_histedit_script_path(&path, worktree);
11981 f = fopen(path, "we");
11983 err = got_error_from_errno2("fopen", path);
11986 TAILQ_FOREACH(hle, histedit_cmds, entry) {
11987 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
11993 int n = fprintf(f, "%c %s\n",
11994 GOT_HISTEDIT_MESG, hle->logmsg);
11996 err = got_ferror(f, GOT_ERR_IO);
12002 if (f && fclose(f) == EOF && err == NULL)
12003 err = got_error_from_errno("fclose");
12006 got_object_commit_close(commit);
12011 histedit_free_list(struct got_histedit_list *histedit_cmds)
12013 struct got_histedit_list_entry *hle;
12015 while ((hle = TAILQ_FIRST(histedit_cmds))) {
12016 TAILQ_REMOVE(histedit_cmds, hle, entry);
12021 static const struct got_error *
12022 histedit_load_list(struct got_histedit_list *histedit_cmds,
12023 const char *path, struct got_repository *repo)
12025 const struct got_error *err = NULL;
12028 f = fopen(path, "re");
12030 err = got_error_from_errno2("fopen", path);
12034 err = histedit_parse_list(histedit_cmds, f, repo);
12036 if (f && fclose(f) == EOF && err == NULL)
12037 err = got_error_from_errno("fclose");
12041 static const struct got_error *
12042 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
12043 const struct got_error *edit_err, struct got_object_id_queue *commits,
12044 const char *path, const char *branch_name, struct got_repository *repo)
12046 const struct got_error *err = NULL, *prev_err = edit_err;
12049 while (resp != 'c' && resp != 'r' && resp != 'a') {
12050 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
12051 "or (a)bort: ", getprogname(), prev_err->msg);
12056 histedit_free_list(histedit_cmds);
12057 err = histedit_run_editor(histedit_cmds, path, commits,
12060 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
12061 err->code != GOT_ERR_HISTEDIT_CMD)
12068 } else if (resp == 'r') {
12069 histedit_free_list(histedit_cmds);
12070 err = histedit_edit_script(histedit_cmds,
12071 commits, branch_name, 0, 0, 0, 0, repo);
12073 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
12074 err->code != GOT_ERR_HISTEDIT_CMD)
12081 } else if (resp == 'a') {
12082 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
12085 printf("invalid response '%c'\n", resp);
12091 static const struct got_error *
12092 histedit_complete(struct got_worktree *worktree,
12093 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
12094 struct got_reference *branch, struct got_repository *repo)
12096 printf("Switching work tree to %s\n",
12097 got_ref_get_symref_target(branch));
12098 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
12102 static const struct got_error *
12103 show_histedit_progress(struct got_commit_object *commit,
12104 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
12106 const struct got_error *err;
12107 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
12109 err = got_object_id_str(&old_id_str, hle->commit_id);
12114 err = got_object_id_str(&new_id_str, new_id);
12119 old_id_str[12] = '\0';
12121 new_id_str[12] = '\0';
12124 logmsg = strdup(hle->logmsg);
12125 if (logmsg == NULL) {
12126 err = got_error_from_errno("strdup");
12129 trim_logmsg(logmsg, 42);
12131 err = get_short_logmsg(&logmsg, 42, commit);
12136 switch (hle->cmd->code) {
12137 case GOT_HISTEDIT_PICK:
12138 case GOT_HISTEDIT_EDIT:
12139 printf("%s -> %s: %s\n", old_id_str,
12140 new_id_str ? new_id_str : "no-op change", logmsg);
12142 case GOT_HISTEDIT_DROP:
12143 case GOT_HISTEDIT_FOLD:
12144 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
12156 static const struct got_error *
12157 histedit_commit(struct got_pathlist_head *merged_paths,
12158 struct got_worktree *worktree, struct got_fileindex *fileindex,
12159 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
12160 const char *committer, struct got_repository *repo)
12162 const struct got_error *err;
12163 struct got_commit_object *commit;
12164 struct got_object_id *new_commit_id;
12166 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
12167 && hle->logmsg == NULL) {
12168 err = histedit_edit_logmsg(hle, repo);
12173 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
12177 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
12178 worktree, fileindex, tmp_branch, committer, commit, hle->commit_id,
12179 hle->logmsg, repo);
12181 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
12183 err = show_histedit_progress(commit, hle, NULL);
12185 err = show_histedit_progress(commit, hle, new_commit_id);
12186 free(new_commit_id);
12189 got_object_commit_close(commit);
12193 static const struct got_error *
12194 histedit_skip_commit(struct got_histedit_list_entry *hle,
12195 struct got_worktree *worktree, struct got_repository *repo)
12197 const struct got_error *error;
12198 struct got_commit_object *commit;
12200 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
12205 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
12209 error = show_histedit_progress(commit, hle, NULL);
12210 got_object_commit_close(commit);
12214 static const struct got_error *
12215 check_local_changes(void *arg, unsigned char status,
12216 unsigned char staged_status, const char *path,
12217 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
12218 struct got_object_id *commit_id, int dirfd, const char *de_name)
12220 int *have_local_changes = arg;
12223 case GOT_STATUS_ADD:
12224 case GOT_STATUS_DELETE:
12225 case GOT_STATUS_MODIFY:
12226 case GOT_STATUS_CONFLICT:
12227 *have_local_changes = 1;
12228 return got_error(GOT_ERR_CANCELLED);
12233 switch (staged_status) {
12234 case GOT_STATUS_ADD:
12235 case GOT_STATUS_DELETE:
12236 case GOT_STATUS_MODIFY:
12237 *have_local_changes = 1;
12238 return got_error(GOT_ERR_CANCELLED);
12246 static const struct got_error *
12247 cmd_histedit(int argc, char *argv[])
12249 const struct got_error *error = NULL;
12250 struct got_worktree *worktree = NULL;
12251 struct got_fileindex *fileindex = NULL;
12252 struct got_repository *repo = NULL;
12253 char *cwd = NULL, *committer = NULL, *gitconfig_path = NULL;
12254 struct got_reference *branch = NULL;
12255 struct got_reference *tmp_branch = NULL;
12256 struct got_object_id *resume_commit_id = NULL;
12257 struct got_object_id *base_commit_id = NULL;
12258 struct got_object_id *head_commit_id = NULL;
12259 struct got_commit_object *commit = NULL;
12260 int ch, rebase_in_progress = 0, merge_in_progress = 0;
12261 struct got_update_progress_arg upa;
12262 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
12263 int drop_only = 0, edit_logmsg_only = 0, fold_only = 0, edit_only = 0;
12264 int list_backups = 0, delete_backups = 0;
12265 const char *edit_script_path = NULL;
12266 struct got_object_id_queue commits;
12267 struct got_pathlist_head merged_paths;
12268 const struct got_object_id_queue *parent_ids;
12269 struct got_object_qid *pid;
12270 struct got_histedit_list histedit_cmds;
12271 struct got_histedit_list_entry *hle;
12272 int *pack_fds = NULL;
12274 STAILQ_INIT(&commits);
12275 TAILQ_INIT(&histedit_cmds);
12276 TAILQ_INIT(&merged_paths);
12277 memset(&upa, 0, sizeof(upa));
12279 while ((ch = getopt(argc, argv, "acdeF:flmX")) != -1) {
12294 edit_script_path = optarg;
12303 edit_logmsg_only = 1;
12306 delete_backups = 1;
12318 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
12319 "unveil", NULL) == -1)
12322 if (abort_edit && continue_edit)
12323 option_conflict('a', 'c');
12324 if (edit_script_path && edit_logmsg_only)
12325 option_conflict('F', 'm');
12326 if (abort_edit && edit_logmsg_only)
12327 option_conflict('a', 'm');
12328 if (continue_edit && edit_logmsg_only)
12329 option_conflict('c', 'm');
12330 if (abort_edit && fold_only)
12331 option_conflict('a', 'f');
12332 if (continue_edit && fold_only)
12333 option_conflict('c', 'f');
12334 if (fold_only && edit_logmsg_only)
12335 option_conflict('f', 'm');
12336 if (edit_script_path && fold_only)
12337 option_conflict('F', 'f');
12338 if (abort_edit && edit_only)
12339 option_conflict('a', 'e');
12340 if (continue_edit && edit_only)
12341 option_conflict('c', 'e');
12342 if (edit_only && edit_logmsg_only)
12343 option_conflict('e', 'm');
12344 if (edit_script_path && edit_only)
12345 option_conflict('F', 'e');
12346 if (fold_only && edit_only)
12347 option_conflict('f', 'e');
12348 if (drop_only && abort_edit)
12349 option_conflict('d', 'a');
12350 if (drop_only && continue_edit)
12351 option_conflict('d', 'c');
12352 if (drop_only && edit_logmsg_only)
12353 option_conflict('d', 'm');
12354 if (drop_only && edit_only)
12355 option_conflict('d', 'e');
12356 if (drop_only && edit_script_path)
12357 option_conflict('d', 'F');
12358 if (drop_only && fold_only)
12359 option_conflict('d', 'f');
12360 if (list_backups) {
12362 option_conflict('l', 'a');
12364 option_conflict('l', 'c');
12365 if (edit_script_path)
12366 option_conflict('l', 'F');
12367 if (edit_logmsg_only)
12368 option_conflict('l', 'm');
12370 option_conflict('l', 'd');
12372 option_conflict('l', 'f');
12374 option_conflict('l', 'e');
12375 if (delete_backups)
12376 option_conflict('l', 'X');
12377 if (argc != 0 && argc != 1)
12379 } else if (delete_backups) {
12381 option_conflict('X', 'a');
12383 option_conflict('X', 'c');
12385 option_conflict('X', 'd');
12386 if (edit_script_path)
12387 option_conflict('X', 'F');
12388 if (edit_logmsg_only)
12389 option_conflict('X', 'm');
12391 option_conflict('X', 'f');
12393 option_conflict('X', 'e');
12395 option_conflict('X', 'l');
12396 if (argc != 0 && argc != 1)
12398 } else if (argc != 0)
12402 * This command cannot apply unveil(2) in all cases because the
12403 * user may choose to run an editor to edit the histedit script
12404 * and to edit individual commit log messages.
12405 * unveil(2) traverses exec(2); if an editor is used we have to
12406 * apply unveil after edit script and log messages have been written.
12407 * XXX TODO: Make use of unveil(2) where possible.
12410 cwd = getcwd(NULL, 0);
12412 error = got_error_from_errno("getcwd");
12416 error = got_repo_pack_fds_open(&pack_fds);
12420 error = got_worktree_open(&worktree, cwd);
12422 if (list_backups || delete_backups) {
12423 if (error->code != GOT_ERR_NOT_WORKTREE)
12426 if (error->code == GOT_ERR_NOT_WORKTREE)
12427 error = wrap_not_worktree_error(error,
12433 if (list_backups || delete_backups) {
12434 error = got_repo_open(&repo,
12435 worktree ? got_worktree_get_repo_path(worktree) : cwd,
12439 error = apply_unveil(got_repo_get_path(repo), 0,
12440 worktree ? got_worktree_get_root_path(worktree) : NULL);
12443 error = process_backup_refs(
12444 GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
12445 argc == 1 ? argv[0] : NULL, delete_backups, repo);
12446 goto done; /* nothing else to do */
12449 error = get_gitconfig_path(&gitconfig_path);
12452 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
12453 gitconfig_path, pack_fds);
12457 if (worktree != NULL && !list_backups && !delete_backups) {
12458 error = worktree_has_logmsg_ref("histedit", worktree, repo);
12463 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
12466 if (rebase_in_progress) {
12467 error = got_error(GOT_ERR_REBASING);
12471 error = got_worktree_merge_in_progress(&merge_in_progress, worktree,
12475 if (merge_in_progress) {
12476 error = got_error(GOT_ERR_MERGE_BUSY);
12480 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
12484 if (edit_in_progress && edit_logmsg_only) {
12485 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
12486 "histedit operation is in progress in this "
12487 "work tree and must be continued or aborted "
12488 "before the -m option can be used");
12491 if (edit_in_progress && drop_only) {
12492 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
12493 "histedit operation is in progress in this "
12494 "work tree and must be continued or aborted "
12495 "before the -d option can be used");
12498 if (edit_in_progress && fold_only) {
12499 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
12500 "histedit operation is in progress in this "
12501 "work tree and must be continued or aborted "
12502 "before the -f option can be used");
12505 if (edit_in_progress && edit_only) {
12506 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
12507 "histedit operation is in progress in this "
12508 "work tree and must be continued or aborted "
12509 "before the -e option can be used");
12513 if (edit_in_progress && abort_edit) {
12514 error = got_worktree_histedit_continue(&resume_commit_id,
12515 &tmp_branch, &branch, &base_commit_id, &fileindex,
12519 printf("Switching work tree to %s\n",
12520 got_ref_get_symref_target(branch));
12521 error = got_worktree_histedit_abort(worktree, fileindex, repo,
12522 branch, base_commit_id, abort_progress, &upa);
12525 printf("Histedit of %s aborted\n",
12526 got_ref_get_symref_target(branch));
12527 print_merge_progress_stats(&upa);
12528 goto done; /* nothing else to do */
12529 } else if (abort_edit) {
12530 error = got_error(GOT_ERR_NOT_HISTEDIT);
12534 error = get_author(&committer, repo, worktree);
12538 if (continue_edit) {
12541 if (!edit_in_progress) {
12542 error = got_error(GOT_ERR_NOT_HISTEDIT);
12546 error = got_worktree_get_histedit_script_path(&path, worktree);
12550 error = histedit_load_list(&histedit_cmds, path, repo);
12555 error = got_worktree_histedit_continue(&resume_commit_id,
12556 &tmp_branch, &branch, &base_commit_id, &fileindex,
12561 error = got_ref_resolve(&head_commit_id, repo, branch);
12565 error = got_object_open_as_commit(&commit, repo,
12569 parent_ids = got_object_commit_get_parent_ids(commit);
12570 pid = STAILQ_FIRST(parent_ids);
12572 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
12575 error = collect_commits(&commits, head_commit_id, &pid->id,
12576 base_commit_id, got_worktree_get_path_prefix(worktree),
12577 GOT_ERR_HISTEDIT_PATH, repo);
12578 got_object_commit_close(commit);
12583 if (edit_in_progress) {
12584 error = got_error(GOT_ERR_HISTEDIT_BUSY);
12588 error = got_ref_open(&branch, repo,
12589 got_worktree_get_head_ref_name(worktree), 0);
12593 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
12594 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
12595 "will not edit commit history of a branch outside "
12596 "the \"refs/heads/\" reference namespace");
12600 error = got_ref_resolve(&head_commit_id, repo, branch);
12601 got_ref_close(branch);
12606 error = got_object_open_as_commit(&commit, repo,
12610 parent_ids = got_object_commit_get_parent_ids(commit);
12611 pid = STAILQ_FIRST(parent_ids);
12613 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
12616 error = collect_commits(&commits, head_commit_id, &pid->id,
12617 got_worktree_get_base_commit_id(worktree),
12618 got_worktree_get_path_prefix(worktree),
12619 GOT_ERR_HISTEDIT_PATH, repo);
12620 got_object_commit_close(commit);
12625 if (STAILQ_EMPTY(&commits)) {
12626 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
12630 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
12631 &base_commit_id, &fileindex, worktree, repo);
12635 if (edit_script_path) {
12636 error = histedit_load_list(&histedit_cmds,
12637 edit_script_path, repo);
12639 got_worktree_histedit_abort(worktree, fileindex,
12640 repo, branch, base_commit_id,
12641 abort_progress, &upa);
12642 print_merge_progress_stats(&upa);
12646 const char *branch_name;
12647 branch_name = got_ref_get_symref_target(branch);
12648 if (strncmp(branch_name, "refs/heads/", 11) == 0)
12650 error = histedit_edit_script(&histedit_cmds, &commits,
12651 branch_name, edit_logmsg_only, fold_only,
12652 drop_only, edit_only, repo);
12654 got_worktree_histedit_abort(worktree, fileindex,
12655 repo, branch, base_commit_id,
12656 abort_progress, &upa);
12657 print_merge_progress_stats(&upa);
12663 error = histedit_save_list(&histedit_cmds, worktree,
12666 got_worktree_histedit_abort(worktree, fileindex,
12667 repo, branch, base_commit_id,
12668 abort_progress, &upa);
12669 print_merge_progress_stats(&upa);
12675 error = histedit_check_script(&histedit_cmds, &commits, repo);
12679 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
12680 if (resume_commit_id) {
12681 if (got_object_id_cmp(hle->commit_id,
12682 resume_commit_id) != 0)
12685 resume_commit_id = NULL;
12686 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
12687 hle->cmd->code == GOT_HISTEDIT_FOLD) {
12688 error = histedit_skip_commit(hle, worktree,
12693 struct got_pathlist_head paths;
12694 int have_changes = 0;
12696 TAILQ_INIT(&paths);
12697 error = got_pathlist_append(&paths, "", NULL);
12700 error = got_worktree_status(worktree, &paths,
12701 repo, 0, check_local_changes, &have_changes,
12702 check_cancelled, NULL);
12703 got_pathlist_free(&paths,
12704 GOT_PATHLIST_FREE_NONE);
12706 if (error->code != GOT_ERR_CANCELLED)
12708 if (sigint_received || sigpipe_received)
12711 if (have_changes) {
12712 error = histedit_commit(NULL, worktree,
12713 fileindex, tmp_branch, hle,
12718 error = got_object_open_as_commit(
12719 &commit, repo, hle->commit_id);
12722 error = show_histedit_progress(commit,
12724 got_object_commit_close(commit);
12733 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
12734 error = histedit_skip_commit(hle, worktree, repo);
12740 error = got_object_open_as_commit(&commit, repo,
12744 parent_ids = got_object_commit_get_parent_ids(commit);
12745 pid = STAILQ_FIRST(parent_ids);
12747 error = got_worktree_histedit_merge_files(&merged_paths,
12748 worktree, fileindex, &pid->id, hle->commit_id, repo,
12749 update_progress, &upa, check_cancelled, NULL);
12752 got_object_commit_close(commit);
12755 print_merge_progress_stats(&upa);
12756 if (upa.conflicts > 0 || upa.missing > 0 ||
12757 upa.not_deleted > 0 || upa.unversioned > 0) {
12758 if (upa.conflicts > 0) {
12759 error = show_rebase_merge_conflict(
12760 hle->commit_id, repo);
12764 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
12768 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
12770 error = got_object_id_str(&id_str, hle->commit_id);
12773 printf("Stopping histedit for amending commit %s\n",
12776 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
12777 error = got_worktree_histedit_postpone(worktree,
12782 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
12783 error = histedit_skip_commit(hle, worktree, repo);
12789 error = histedit_commit(&merged_paths, worktree, fileindex,
12790 tmp_branch, hle, committer, repo);
12791 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
12796 if (upa.conflicts > 0 || upa.missing > 0 ||
12797 upa.not_deleted > 0 || upa.unversioned > 0) {
12798 error = got_worktree_histedit_postpone(worktree, fileindex);
12801 if (upa.conflicts > 0 && upa.missing == 0 &&
12802 upa.not_deleted == 0 && upa.unversioned == 0) {
12803 error = got_error_msg(GOT_ERR_CONFLICTS,
12804 "conflicts must be resolved before histedit "
12806 } else if (upa.conflicts > 0) {
12807 error = got_error_msg(GOT_ERR_CONFLICTS,
12808 "conflicts must be resolved before histedit "
12809 "can continue; changes destined for some "
12810 "files were not yet merged and should be "
12811 "merged manually if required before the "
12812 "histedit operation is continued");
12814 error = got_error_msg(GOT_ERR_CONFLICTS,
12815 "changes destined for some files were not "
12816 "yet merged and should be merged manually "
12817 "if required before the histedit operation "
12821 error = histedit_complete(worktree, fileindex, tmp_branch,
12826 free(gitconfig_path);
12827 got_object_id_queue_free(&commits);
12828 histedit_free_list(&histedit_cmds);
12829 free(head_commit_id);
12830 free(base_commit_id);
12831 free(resume_commit_id);
12833 got_object_commit_close(commit);
12835 got_ref_close(branch);
12837 got_ref_close(tmp_branch);
12839 got_worktree_close(worktree);
12841 const struct got_error *close_err = got_repo_close(repo);
12846 const struct got_error *pack_err =
12847 got_repo_pack_fds_close(pack_fds);
12855 usage_integrate(void)
12857 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
12861 static const struct got_error *
12862 cmd_integrate(int argc, char *argv[])
12864 const struct got_error *error = NULL;
12865 struct got_repository *repo = NULL;
12866 struct got_worktree *worktree = NULL;
12867 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
12868 const char *branch_arg = NULL;
12869 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
12870 struct got_fileindex *fileindex = NULL;
12871 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
12873 struct got_update_progress_arg upa;
12874 int *pack_fds = NULL;
12876 while ((ch = getopt(argc, argv, "")) != -1) {
12889 branch_arg = argv[0];
12891 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
12892 "unveil", NULL) == -1)
12895 cwd = getcwd(NULL, 0);
12897 error = got_error_from_errno("getcwd");
12901 error = got_repo_pack_fds_open(&pack_fds);
12905 error = got_worktree_open(&worktree, cwd);
12907 if (error->code == GOT_ERR_NOT_WORKTREE)
12908 error = wrap_not_worktree_error(error, "integrate",
12913 error = check_rebase_or_histedit_in_progress(worktree);
12917 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
12922 error = apply_unveil(got_repo_get_path(repo), 0,
12923 got_worktree_get_root_path(worktree));
12927 error = check_merge_in_progress(worktree, repo);
12931 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
12932 error = got_error_from_errno("asprintf");
12936 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
12937 &base_branch_ref, worktree, refname, repo);
12941 refname = strdup(got_ref_get_name(branch_ref));
12942 if (refname == NULL) {
12943 error = got_error_from_errno("strdup");
12944 got_worktree_integrate_abort(worktree, fileindex, repo,
12945 branch_ref, base_branch_ref);
12948 base_refname = strdup(got_ref_get_name(base_branch_ref));
12949 if (base_refname == NULL) {
12950 error = got_error_from_errno("strdup");
12951 got_worktree_integrate_abort(worktree, fileindex, repo,
12952 branch_ref, base_branch_ref);
12955 if (strncmp(base_refname, "refs/heads/", 11) != 0) {
12956 error = got_error(GOT_ERR_INTEGRATE_BRANCH);
12957 got_worktree_integrate_abort(worktree, fileindex, repo,
12958 branch_ref, base_branch_ref);
12962 error = got_ref_resolve(&commit_id, repo, branch_ref);
12966 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
12970 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
12971 error = got_error_msg(GOT_ERR_SAME_BRANCH,
12972 "specified branch has already been integrated");
12973 got_worktree_integrate_abort(worktree, fileindex, repo,
12974 branch_ref, base_branch_ref);
12978 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
12980 if (error->code == GOT_ERR_ANCESTRY)
12981 error = got_error(GOT_ERR_REBASE_REQUIRED);
12982 got_worktree_integrate_abort(worktree, fileindex, repo,
12983 branch_ref, base_branch_ref);
12987 memset(&upa, 0, sizeof(upa));
12988 error = got_worktree_integrate_continue(worktree, fileindex, repo,
12989 branch_ref, base_branch_ref, update_progress, &upa,
12990 check_cancelled, NULL);
12994 printf("Integrated %s into %s\n", refname, base_refname);
12995 print_update_progress_stats(&upa);
12998 const struct got_error *close_err = got_repo_close(repo);
13003 got_worktree_close(worktree);
13005 const struct got_error *pack_err =
13006 got_repo_pack_fds_close(pack_fds);
13011 free(base_commit_id);
13014 free(base_refname);
13021 fprintf(stderr, "usage: %s merge [-acn] [branch]\n", getprogname());
13025 static const struct got_error *
13026 cmd_merge(int argc, char *argv[])
13028 const struct got_error *error = NULL;
13029 struct got_worktree *worktree = NULL;
13030 struct got_repository *repo = NULL;
13031 struct got_fileindex *fileindex = NULL;
13032 char *cwd = NULL, *id_str = NULL, *author = NULL;
13033 struct got_reference *branch = NULL, *wt_branch = NULL;
13034 struct got_object_id *branch_tip = NULL, *yca_id = NULL;
13035 struct got_object_id *wt_branch_tip = NULL;
13036 int ch, merge_in_progress = 0, abort_merge = 0, continue_merge = 0;
13037 int interrupt_merge = 0;
13038 struct got_update_progress_arg upa;
13039 struct got_object_id *merge_commit_id = NULL;
13040 char *branch_name = NULL;
13041 int *pack_fds = NULL;
13043 memset(&upa, 0, sizeof(upa));
13045 while ((ch = getopt(argc, argv, "acn")) != -1) {
13051 continue_merge = 1;
13054 interrupt_merge = 1;
13066 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
13067 "unveil", NULL) == -1)
13071 if (abort_merge && continue_merge)
13072 option_conflict('a', 'c');
13073 if (abort_merge || continue_merge) {
13076 } else if (argc != 1)
13079 cwd = getcwd(NULL, 0);
13081 error = got_error_from_errno("getcwd");
13085 error = got_repo_pack_fds_open(&pack_fds);
13089 error = got_worktree_open(&worktree, cwd);
13091 if (error->code == GOT_ERR_NOT_WORKTREE)
13092 error = wrap_not_worktree_error(error,
13097 error = got_repo_open(&repo,
13098 worktree ? got_worktree_get_repo_path(worktree) : cwd, NULL,
13103 if (worktree != NULL) {
13104 error = worktree_has_logmsg_ref("merge", worktree, repo);
13109 error = apply_unveil(got_repo_get_path(repo), 0,
13110 worktree ? got_worktree_get_root_path(worktree) : NULL);
13114 error = check_rebase_or_histedit_in_progress(worktree);
13118 error = got_worktree_merge_in_progress(&merge_in_progress, worktree,
13124 if (!merge_in_progress) {
13125 error = got_error(GOT_ERR_NOT_MERGING);
13128 error = got_worktree_merge_continue(&branch_name,
13129 &branch_tip, &fileindex, worktree, repo);
13132 error = got_worktree_merge_abort(worktree, fileindex, repo,
13133 abort_progress, &upa);
13136 printf("Merge of %s aborted\n", branch_name);
13137 goto done; /* nothing else to do */
13140 error = get_author(&author, repo, worktree);
13144 if (continue_merge) {
13145 if (!merge_in_progress) {
13146 error = got_error(GOT_ERR_NOT_MERGING);
13149 error = got_worktree_merge_continue(&branch_name,
13150 &branch_tip, &fileindex, worktree, repo);
13154 error = got_ref_open(&branch, repo, argv[0], 0);
13157 branch_name = strdup(got_ref_get_name(branch));
13158 if (branch_name == NULL) {
13159 error = got_error_from_errno("strdup");
13162 error = got_ref_resolve(&branch_tip, repo, branch);
13167 error = got_ref_open(&wt_branch, repo,
13168 got_worktree_get_head_ref_name(worktree), 0);
13171 error = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
13174 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
13175 wt_branch_tip, branch_tip, 0, repo,
13176 check_cancelled, NULL);
13177 if (error && error->code != GOT_ERR_ANCESTRY)
13180 if (!continue_merge) {
13181 error = check_path_prefix(wt_branch_tip, branch_tip,
13182 got_worktree_get_path_prefix(worktree),
13183 GOT_ERR_MERGE_PATH, repo);
13187 error = check_same_branch(wt_branch_tip, branch,
13190 if (error->code != GOT_ERR_ANCESTRY)
13194 static char msg[512];
13195 snprintf(msg, sizeof(msg),
13196 "cannot create a merge commit because "
13197 "%s is based on %s; %s can be integrated "
13198 "with 'got integrate' instead", branch_name,
13199 got_worktree_get_head_ref_name(worktree),
13201 error = got_error_msg(GOT_ERR_SAME_BRANCH, msg);
13205 error = got_worktree_merge_prepare(&fileindex, worktree,
13210 error = got_worktree_merge_branch(worktree, fileindex,
13211 yca_id, branch_tip, repo, update_progress, &upa,
13212 check_cancelled, NULL);
13215 print_merge_progress_stats(&upa);
13216 if (!upa.did_something) {
13217 error = got_worktree_merge_abort(worktree, fileindex,
13218 repo, abort_progress, &upa);
13221 printf("Already up-to-date\n");
13226 if (interrupt_merge) {
13227 error = got_worktree_merge_postpone(worktree, fileindex);
13230 printf("Merge of %s interrupted on request\n", branch_name);
13231 } else if (upa.conflicts > 0 || upa.missing > 0 ||
13232 upa.not_deleted > 0 || upa.unversioned > 0) {
13233 error = got_worktree_merge_postpone(worktree, fileindex);
13236 if (upa.conflicts > 0 && upa.missing == 0 &&
13237 upa.not_deleted == 0 && upa.unversioned == 0) {
13238 error = got_error_msg(GOT_ERR_CONFLICTS,
13239 "conflicts must be resolved before merging "
13241 } else if (upa.conflicts > 0) {
13242 error = got_error_msg(GOT_ERR_CONFLICTS,
13243 "conflicts must be resolved before merging "
13244 "can continue; changes destined for some "
13245 "files were not yet merged and "
13246 "should be merged manually if required before the "
13247 "merge operation is continued");
13249 error = got_error_msg(GOT_ERR_CONFLICTS,
13250 "changes destined for some "
13251 "files were not yet merged and should be "
13252 "merged manually if required before the "
13253 "merge operation is continued");
13257 error = got_worktree_merge_commit(&merge_commit_id, worktree,
13258 fileindex, author, NULL, 1, branch_tip, branch_name,
13259 repo, continue_merge ? print_status : NULL, NULL);
13262 error = got_worktree_merge_complete(worktree, fileindex, repo);
13265 error = got_object_id_str(&id_str, merge_commit_id);
13268 printf("Merged %s into %s: %s\n", branch_name,
13269 got_worktree_get_head_ref_name(worktree),
13275 free(merge_commit_id);
13281 got_ref_close(branch);
13283 got_ref_close(wt_branch);
13285 got_worktree_close(worktree);
13287 const struct got_error *close_err = got_repo_close(repo);
13292 const struct got_error *pack_err =
13293 got_repo_pack_fds_close(pack_fds);
13303 fprintf(stderr, "usage: %s stage [-lpS] [-F response-script] "
13304 "[path ...]\n", getprogname());
13308 static const struct got_error *
13309 print_stage(void *arg, unsigned char status, unsigned char staged_status,
13310 const char *path, struct got_object_id *blob_id,
13311 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
13312 int dirfd, const char *de_name)
13314 const struct got_error *err = NULL;
13315 char *id_str = NULL;
13317 if (staged_status != GOT_STATUS_ADD &&
13318 staged_status != GOT_STATUS_MODIFY &&
13319 staged_status != GOT_STATUS_DELETE)
13322 if (staged_status == GOT_STATUS_ADD ||
13323 staged_status == GOT_STATUS_MODIFY)
13324 err = got_object_id_str(&id_str, staged_blob_id);
13326 err = got_object_id_str(&id_str, blob_id);
13330 printf("%s %c %s\n", id_str, staged_status, path);
13335 static const struct got_error *
13336 cmd_stage(int argc, char *argv[])
13338 const struct got_error *error = NULL;
13339 struct got_repository *repo = NULL;
13340 struct got_worktree *worktree = NULL;
13342 struct got_pathlist_head paths;
13343 int ch, list_stage = 0, pflag = 0, allow_bad_symlinks = 0;
13344 FILE *patch_script_file = NULL;
13345 const char *patch_script_path = NULL;
13346 struct choose_patch_arg cpa;
13347 int *pack_fds = NULL;
13349 TAILQ_INIT(&paths);
13351 while ((ch = getopt(argc, argv, "F:lpS")) != -1) {
13354 patch_script_path = optarg;
13363 allow_bad_symlinks = 1;
13375 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
13376 "unveil", NULL) == -1)
13379 if (list_stage && (pflag || patch_script_path))
13380 errx(1, "-l option cannot be used with other options");
13381 if (patch_script_path && !pflag)
13382 errx(1, "-F option can only be used together with -p option");
13384 cwd = getcwd(NULL, 0);
13386 error = got_error_from_errno("getcwd");
13390 error = got_repo_pack_fds_open(&pack_fds);
13394 error = got_worktree_open(&worktree, cwd);
13396 if (error->code == GOT_ERR_NOT_WORKTREE)
13397 error = wrap_not_worktree_error(error, "stage", cwd);
13401 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
13406 if (patch_script_path) {
13407 patch_script_file = fopen(patch_script_path, "re");
13408 if (patch_script_file == NULL) {
13409 error = got_error_from_errno2("fopen",
13410 patch_script_path);
13414 error = apply_unveil(got_repo_get_path(repo), 0,
13415 got_worktree_get_root_path(worktree));
13419 error = check_merge_in_progress(worktree, repo);
13423 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
13428 error = got_worktree_status(worktree, &paths, repo, 0,
13429 print_stage, NULL, check_cancelled, NULL);
13431 cpa.patch_script_file = patch_script_file;
13432 cpa.action = "stage";
13433 error = got_worktree_stage(worktree, &paths,
13434 pflag ? NULL : print_status, NULL,
13435 pflag ? choose_patch : NULL, &cpa,
13436 allow_bad_symlinks, repo);
13439 if (patch_script_file && fclose(patch_script_file) == EOF &&
13441 error = got_error_from_errno2("fclose", patch_script_path);
13443 const struct got_error *close_err = got_repo_close(repo);
13448 got_worktree_close(worktree);
13450 const struct got_error *pack_err =
13451 got_repo_pack_fds_close(pack_fds);
13455 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
13461 usage_unstage(void)
13463 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
13464 "[path ...]\n", getprogname());
13469 static const struct got_error *
13470 cmd_unstage(int argc, char *argv[])
13472 const struct got_error *error = NULL;
13473 struct got_repository *repo = NULL;
13474 struct got_worktree *worktree = NULL;
13476 struct got_pathlist_head paths;
13478 struct got_update_progress_arg upa;
13479 FILE *patch_script_file = NULL;
13480 const char *patch_script_path = NULL;
13481 struct choose_patch_arg cpa;
13482 int *pack_fds = NULL;
13484 TAILQ_INIT(&paths);
13486 while ((ch = getopt(argc, argv, "F:p")) != -1) {
13489 patch_script_path = optarg;
13504 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
13505 "unveil", NULL) == -1)
13508 if (patch_script_path && !pflag)
13509 errx(1, "-F option can only be used together with -p option");
13511 cwd = getcwd(NULL, 0);
13513 error = got_error_from_errno("getcwd");
13517 error = got_repo_pack_fds_open(&pack_fds);
13521 error = got_worktree_open(&worktree, cwd);
13523 if (error->code == GOT_ERR_NOT_WORKTREE)
13524 error = wrap_not_worktree_error(error, "unstage", cwd);
13528 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
13533 if (patch_script_path) {
13534 patch_script_file = fopen(patch_script_path, "re");
13535 if (patch_script_file == NULL) {
13536 error = got_error_from_errno2("fopen",
13537 patch_script_path);
13542 error = apply_unveil(got_repo_get_path(repo), 0,
13543 got_worktree_get_root_path(worktree));
13547 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
13551 cpa.patch_script_file = patch_script_file;
13552 cpa.action = "unstage";
13553 memset(&upa, 0, sizeof(upa));
13554 error = got_worktree_unstage(worktree, &paths, update_progress,
13555 &upa, pflag ? choose_patch : NULL, &cpa, repo);
13557 print_merge_progress_stats(&upa);
13559 if (patch_script_file && fclose(patch_script_file) == EOF &&
13561 error = got_error_from_errno2("fclose", patch_script_path);
13563 const struct got_error *close_err = got_repo_close(repo);
13568 got_worktree_close(worktree);
13570 const struct got_error *pack_err =
13571 got_repo_pack_fds_close(pack_fds);
13575 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
13583 fprintf(stderr, "usage: %s cat [-P] [-c commit] [-r repository-path] "
13584 "arg ...\n", getprogname());
13588 static const struct got_error *
13589 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
13591 const struct got_error *err;
13592 struct got_blob_object *blob;
13595 fd = got_opentempfd();
13597 return got_error_from_errno("got_opentempfd");
13599 err = got_object_open_as_blob(&blob, repo, id, 8192, fd);
13603 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
13605 if (fd != -1 && close(fd) == -1 && err == NULL)
13606 err = got_error_from_errno("close");
13608 got_object_blob_close(blob);
13612 static const struct got_error *
13613 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
13615 const struct got_error *err;
13616 struct got_tree_object *tree;
13619 err = got_object_open_as_tree(&tree, repo, id);
13623 nentries = got_object_tree_get_nentries(tree);
13624 for (i = 0; i < nentries; i++) {
13625 struct got_tree_entry *te;
13627 if (sigint_received || sigpipe_received)
13629 te = got_object_tree_get_entry(tree, i);
13630 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
13633 fprintf(outfile, "%s %.7o %s\n", id_str,
13634 got_tree_entry_get_mode(te),
13635 got_tree_entry_get_name(te));
13639 got_object_tree_close(tree);
13643 static const struct got_error *
13644 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
13646 const struct got_error *err;
13647 struct got_commit_object *commit;
13648 const struct got_object_id_queue *parent_ids;
13649 struct got_object_qid *pid;
13650 char *id_str = NULL;
13651 const char *logmsg = NULL;
13654 err = got_object_open_as_commit(&commit, repo, id);
13658 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
13662 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
13663 parent_ids = got_object_commit_get_parent_ids(commit);
13664 fprintf(outfile, "numparents %d\n",
13665 got_object_commit_get_nparents(commit));
13666 STAILQ_FOREACH(pid, parent_ids, entry) {
13668 err = got_object_id_str(&pid_str, &pid->id);
13671 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
13674 got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
13675 got_object_commit_get_author_gmtoff(commit));
13676 fprintf(outfile, "%s%s %lld %s\n", GOT_COMMIT_LABEL_AUTHOR,
13677 got_object_commit_get_author(commit),
13678 (long long)got_object_commit_get_author_time(commit),
13681 got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
13682 got_object_commit_get_committer_gmtoff(commit));
13683 fprintf(outfile, "%s%s %lld %s\n", GOT_COMMIT_LABEL_COMMITTER,
13684 got_object_commit_get_committer(commit),
13685 (long long)got_object_commit_get_committer_time(commit),
13688 logmsg = got_object_commit_get_logmsg_raw(commit);
13689 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
13690 fprintf(outfile, "%s", logmsg);
13693 got_object_commit_close(commit);
13697 static const struct got_error *
13698 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
13700 const struct got_error *err;
13701 struct got_tag_object *tag;
13702 char *id_str = NULL;
13703 const char *tagmsg = NULL;
13706 err = got_object_open_as_tag(&tag, repo, id);
13710 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
13714 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
13716 switch (got_object_tag_get_object_type(tag)) {
13717 case GOT_OBJ_TYPE_BLOB:
13718 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
13719 GOT_OBJ_LABEL_BLOB);
13721 case GOT_OBJ_TYPE_TREE:
13722 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
13723 GOT_OBJ_LABEL_TREE);
13725 case GOT_OBJ_TYPE_COMMIT:
13726 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
13727 GOT_OBJ_LABEL_COMMIT);
13729 case GOT_OBJ_TYPE_TAG:
13730 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
13731 GOT_OBJ_LABEL_TAG);
13737 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
13738 got_object_tag_get_name(tag));
13740 got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
13741 got_object_tag_get_tagger_gmtoff(tag));
13742 fprintf(outfile, "%s%s %lld %s\n", GOT_TAG_LABEL_TAGGER,
13743 got_object_tag_get_tagger(tag),
13744 (long long)got_object_tag_get_tagger_time(tag),
13747 tagmsg = got_object_tag_get_message(tag);
13748 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
13749 fprintf(outfile, "%s", tagmsg);
13752 got_object_tag_close(tag);
13756 static const struct got_error *
13757 cmd_cat(int argc, char *argv[])
13759 const struct got_error *error;
13760 struct got_repository *repo = NULL;
13761 struct got_worktree *worktree = NULL;
13762 char *cwd = NULL, *repo_path = NULL, *label = NULL;
13763 const char *commit_id_str = NULL;
13764 struct got_object_id *id = NULL, *commit_id = NULL;
13765 struct got_commit_object *commit = NULL;
13766 int ch, obj_type, i, force_path = 0;
13767 struct got_reflist_head refs;
13768 int *pack_fds = NULL;
13773 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
13778 while ((ch = getopt(argc, argv, "c:Pr:")) != -1) {
13781 commit_id_str = optarg;
13787 repo_path = realpath(optarg, NULL);
13788 if (repo_path == NULL)
13789 return got_error_from_errno2("realpath",
13791 got_path_strip_trailing_slashes(repo_path);
13802 cwd = getcwd(NULL, 0);
13804 error = got_error_from_errno("getcwd");
13808 error = got_repo_pack_fds_open(&pack_fds);
13812 if (repo_path == NULL) {
13813 error = got_worktree_open(&worktree, cwd);
13814 if (error && error->code != GOT_ERR_NOT_WORKTREE)
13817 repo_path = strdup(
13818 got_worktree_get_repo_path(worktree));
13819 if (repo_path == NULL) {
13820 error = got_error_from_errno("strdup");
13824 /* Release work tree lock. */
13825 got_worktree_close(worktree);
13830 if (repo_path == NULL) {
13831 repo_path = strdup(cwd);
13832 if (repo_path == NULL)
13833 return got_error_from_errno("strdup");
13836 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
13841 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
13845 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
13849 if (commit_id_str == NULL)
13850 commit_id_str = GOT_REF_HEAD;
13851 error = got_repo_match_object_id(&commit_id, NULL,
13852 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
13856 error = got_object_open_as_commit(&commit, repo, commit_id);
13860 for (i = 0; i < argc; i++) {
13862 error = got_object_id_by_path(&id, repo, commit,
13867 error = got_repo_match_object_id(&id, &label, argv[i],
13868 GOT_OBJ_TYPE_ANY, NULL /* do not resolve tags */,
13871 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
13872 error->code != GOT_ERR_NOT_REF)
13874 error = got_object_id_by_path(&id, repo,
13881 error = got_object_get_type(&obj_type, repo, id);
13885 switch (obj_type) {
13886 case GOT_OBJ_TYPE_BLOB:
13887 error = cat_blob(id, repo, stdout);
13889 case GOT_OBJ_TYPE_TREE:
13890 error = cat_tree(id, repo, stdout);
13892 case GOT_OBJ_TYPE_COMMIT:
13893 error = cat_commit(id, repo, stdout);
13895 case GOT_OBJ_TYPE_TAG:
13896 error = cat_tag(id, repo, stdout);
13899 error = got_error(GOT_ERR_OBJ_TYPE);
13914 got_object_commit_close(commit);
13916 got_worktree_close(worktree);
13918 const struct got_error *close_err = got_repo_close(repo);
13923 const struct got_error *pack_err =
13924 got_repo_pack_fds_close(pack_fds);
13929 got_ref_list_free(&refs);
13936 fprintf(stderr, "usage: %s info [path ...]\n",
13941 static const struct got_error *
13942 print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
13943 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
13944 struct got_object_id *commit_id)
13946 const struct got_error *err = NULL;
13947 char *id_str = NULL;
13949 struct tm mytm, *tm;
13950 struct got_pathlist_head *paths = arg;
13951 struct got_pathlist_entry *pe;
13954 * Clear error indication from any of the path arguments which
13955 * would cause this file index entry to be displayed.
13957 TAILQ_FOREACH(pe, paths, entry) {
13958 if (got_path_cmp(path, pe->path, strlen(path),
13959 pe->path_len) == 0 ||
13960 got_path_is_child(path, pe->path, pe->path_len))
13961 pe->data = NULL; /* no error */
13964 printf(GOT_COMMIT_SEP_STR);
13966 printf("symlink: %s\n", path);
13967 else if (S_ISREG(mode)) {
13968 printf("file: %s\n", path);
13969 printf("mode: %o\n", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
13970 } else if (S_ISDIR(mode))
13971 printf("directory: %s\n", path);
13973 printf("something: %s\n", path);
13975 tm = localtime_r(&mtime, &mytm);
13978 if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) == 0)
13979 return got_error(GOT_ERR_NO_SPACE);
13980 printf("timestamp: %s\n", datebuf);
13983 err = got_object_id_str(&id_str, blob_id);
13986 printf("based on blob: %s\n", id_str);
13990 if (staged_blob_id) {
13991 err = got_object_id_str(&id_str, staged_blob_id);
13994 printf("based on staged blob: %s\n", id_str);
13999 err = got_object_id_str(&id_str, commit_id);
14002 printf("based on commit: %s\n", id_str);
14009 static const struct got_error *
14010 cmd_info(int argc, char *argv[])
14012 const struct got_error *error = NULL;
14013 struct got_worktree *worktree = NULL;
14014 char *cwd = NULL, *id_str = NULL;
14015 struct got_pathlist_head paths;
14016 char *uuidstr = NULL;
14017 int ch, show_files = 0;
14019 TAILQ_INIT(&paths);
14021 while ((ch = getopt(argc, argv, "")) != -1) {
14033 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
14037 cwd = getcwd(NULL, 0);
14039 error = got_error_from_errno("getcwd");
14043 error = got_worktree_open(&worktree, cwd);
14045 if (error->code == GOT_ERR_NOT_WORKTREE)
14046 error = wrap_not_worktree_error(error, "info", cwd);
14051 /* Remove "wpath cpath proc exec sendfd" promises. */
14052 if (pledge("stdio rpath flock unveil", NULL) == -1)
14055 error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
14060 error = get_worktree_paths_from_argv(&paths, argc, argv,
14067 error = got_object_id_str(&id_str,
14068 got_worktree_get_base_commit_id(worktree));
14072 error = got_worktree_get_uuid(&uuidstr, worktree);
14076 printf("work tree: %s\n", got_worktree_get_root_path(worktree));
14077 printf("work tree base commit: %s\n", id_str);
14078 printf("work tree path prefix: %s\n",
14079 got_worktree_get_path_prefix(worktree));
14080 printf("work tree branch reference: %s\n",
14081 got_worktree_get_head_ref_name(worktree));
14082 printf("work tree UUID: %s\n", uuidstr);
14083 printf("repository: %s\n", got_worktree_get_repo_path(worktree));
14086 struct got_pathlist_entry *pe;
14087 TAILQ_FOREACH(pe, &paths, entry) {
14088 if (pe->path_len == 0)
14091 * Assume this path will fail. This will be corrected
14092 * in print_path_info() in case the path does suceeed.
14094 pe->data = (void *)got_error(GOT_ERR_BAD_PATH);
14096 error = got_worktree_path_info(worktree, &paths,
14097 print_path_info, &paths, check_cancelled, NULL);
14100 TAILQ_FOREACH(pe, &paths, entry) {
14101 if (pe->data != NULL) {
14102 const struct got_error *perr;
14105 error = got_error_fmt(perr->code, "%s",
14113 got_worktree_close(worktree);
14114 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);