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>
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 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
393 const char *initial_content, size_t initial_content_len,
394 int require_modification)
396 const struct got_error *err = NULL;
401 size_t len, logmsg_len;
402 char *initial_content_stripped = NULL, *buf = NULL, *s;
406 if (stat(logmsg_path, &st) == -1)
407 return got_error_from_errno2("stat", logmsg_path);
409 if (spawn_editor(editor, logmsg_path) == -1)
410 return got_error_from_errno("failed spawning editor");
412 if (stat(logmsg_path, &st2) == -1)
413 return got_error_from_errno("stat");
415 if (require_modification &&
416 st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
417 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
418 "no changes made to commit message, aborting");
421 * Set up a stripped version of the initial content without comments
422 * and blank lines. We need this in order to check if the message
423 * has in fact been edited.
425 initial_content_stripped = malloc(initial_content_len + 1);
426 if (initial_content_stripped == NULL)
427 return got_error_from_errno("malloc");
428 initial_content_stripped[0] = '\0';
430 buf = strdup(initial_content);
432 err = got_error_from_errno("strdup");
437 while ((line = strsep(&s, "\n")) != NULL) {
438 if ((line[0] == '#' || (len == 0 && line[0] == '\n')))
439 continue; /* remove comments and leading empty lines */
440 len = strlcat(initial_content_stripped, line,
441 initial_content_len + 1);
442 if (len >= initial_content_len + 1) {
443 err = got_error(GOT_ERR_NO_SPACE);
447 while (len > 0 && initial_content_stripped[len - 1] == '\n') {
448 initial_content_stripped[len - 1] = '\0';
452 logmsg_len = st2.st_size;
453 *logmsg = malloc(logmsg_len + 1);
455 return got_error_from_errno("malloc");
458 fp = fopen(logmsg_path, "re");
460 err = got_error_from_errno("fopen");
465 while (getline(&line, &linesize, fp) != -1) {
466 if ((line[0] == '#' || (len == 0 && line[0] == '\n')))
467 continue; /* remove comments and leading empty lines */
468 len = strlcat(*logmsg, line, logmsg_len + 1);
469 if (len >= logmsg_len + 1) {
470 err = got_error(GOT_ERR_NO_SPACE);
476 err = got_ferror(fp, GOT_ERR_IO);
479 while (len > 0 && (*logmsg)[len - 1] == '\n') {
480 (*logmsg)[len - 1] = '\0';
485 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
486 "commit message cannot be empty, aborting");
489 if (require_modification &&
490 strcmp(*logmsg, initial_content_stripped) == 0)
491 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
492 "no changes made to commit message, aborting");
494 free(initial_content_stripped);
496 if (fp && fclose(fp) == EOF && err == NULL)
497 err = got_error_from_errno("fclose");
505 static const struct got_error *
506 collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
507 const char *path_dir, const char *branch_name)
509 char *initial_content = NULL;
510 const struct got_error *err = NULL;
511 int initial_content_len;
514 initial_content_len = asprintf(&initial_content,
515 "\n# %s to be imported to branch %s\n", path_dir,
517 if (initial_content_len == -1)
518 return got_error_from_errno("asprintf");
520 err = got_opentemp_named_fd(logmsg_path, &fd,
521 GOT_TMPDIR_STR "/got-importmsg", "");
525 if (write(fd, initial_content, initial_content_len) == -1) {
526 err = got_error_from_errno2("write", *logmsg_path);
530 err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content,
531 initial_content_len, 1);
533 if (fd != -1 && close(fd) == -1 && err == NULL)
534 err = got_error_from_errno2("close", *logmsg_path);
535 free(initial_content);
543 static const struct got_error *
544 import_progress(void *arg, const char *path)
546 printf("A %s\n", path);
550 static const struct got_error *
551 valid_author(const char *author)
553 const char *email = author;
556 * Git' expects the author (or committer) to be in the form
557 * "name <email>", which are mostly free form (see the
558 * "committer" description in git-fast-import(1)). We're only
559 * doing this to avoid git's object parser breaking on commits
563 while (*author && *author != '\n' && *author != '<' && *author != '>')
565 if (author != email && *author == '<' && *(author - 1) != ' ')
566 return got_error_fmt(GOT_ERR_COMMIT_BAD_AUTHOR, "%s: space "
567 "between author name and email required", email);
568 if (*author++ != '<')
569 return got_error_fmt(GOT_ERR_COMMIT_NO_EMAIL, "%s", email);
570 while (*author && *author != '\n' && *author != '<' && *author != '>')
572 if (strcmp(author, ">") != 0)
573 return got_error_fmt(GOT_ERR_COMMIT_NO_EMAIL, "%s", email);
577 static const struct got_error *
578 get_author(char **author, struct got_repository *repo,
579 struct got_worktree *worktree)
581 const struct got_error *err = NULL;
582 const char *got_author = NULL, *name, *email;
583 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
588 worktree_conf = got_worktree_get_gotconfig(worktree);
589 repo_conf = got_repo_get_gotconfig(repo);
592 * Priority of potential author information sources, from most
593 * significant to least significant:
594 * 1) work tree's .got/got.conf file
595 * 2) repository's got.conf file
596 * 3) repository's git config file
597 * 4) environment variables
598 * 5) global git config files (in user's home directory or /etc)
602 got_author = got_gotconfig_get_author(worktree_conf);
603 if (got_author == NULL)
604 got_author = got_gotconfig_get_author(repo_conf);
605 if (got_author == NULL) {
606 name = got_repo_get_gitconfig_author_name(repo);
607 email = got_repo_get_gitconfig_author_email(repo);
609 if (asprintf(author, "%s <%s>", name, email) == -1)
610 return got_error_from_errno("asprintf");
614 got_author = getenv("GOT_AUTHOR");
615 if (got_author == NULL) {
616 name = got_repo_get_global_gitconfig_author_name(repo);
617 email = got_repo_get_global_gitconfig_author_email(
620 if (asprintf(author, "%s <%s>", name, email)
622 return got_error_from_errno("asprintf");
625 /* TODO: Look up user in password database? */
626 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
630 *author = strdup(got_author);
632 return got_error_from_errno("strdup");
634 err = valid_author(*author);
642 static const struct got_error *
643 get_allowed_signers(char **allowed_signers, struct got_repository *repo,
644 struct got_worktree *worktree)
646 const char *got_allowed_signers = NULL;
647 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
649 *allowed_signers = NULL;
652 worktree_conf = got_worktree_get_gotconfig(worktree);
653 repo_conf = got_repo_get_gotconfig(repo);
656 * Priority of potential author information sources, from most
657 * significant to least significant:
658 * 1) work tree's .got/got.conf file
659 * 2) repository's got.conf file
663 got_allowed_signers = got_gotconfig_get_allowed_signers_file(
665 if (got_allowed_signers == NULL)
666 got_allowed_signers = got_gotconfig_get_allowed_signers_file(
669 if (got_allowed_signers) {
670 *allowed_signers = strdup(got_allowed_signers);
671 if (*allowed_signers == NULL)
672 return got_error_from_errno("strdup");
677 static const struct got_error *
678 get_revoked_signers(char **revoked_signers, struct got_repository *repo,
679 struct got_worktree *worktree)
681 const char *got_revoked_signers = NULL;
682 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
684 *revoked_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_revoked_signers = got_gotconfig_get_revoked_signers_file(
700 if (got_revoked_signers == NULL)
701 got_revoked_signers = got_gotconfig_get_revoked_signers_file(
704 if (got_revoked_signers) {
705 *revoked_signers = strdup(got_revoked_signers);
706 if (*revoked_signers == NULL)
707 return got_error_from_errno("strdup");
713 get_signer_id(struct got_repository *repo, struct got_worktree *worktree)
715 const char *got_signer_id = NULL;
716 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
719 worktree_conf = got_worktree_get_gotconfig(worktree);
720 repo_conf = got_repo_get_gotconfig(repo);
723 * Priority of potential author information sources, from most
724 * significant to least significant:
725 * 1) work tree's .got/got.conf file
726 * 2) repository's got.conf file
730 got_signer_id = got_gotconfig_get_signer_id(worktree_conf);
731 if (got_signer_id == NULL)
732 got_signer_id = got_gotconfig_get_signer_id(repo_conf);
734 return got_signer_id;
737 static const struct got_error *
738 get_gitconfig_path(char **gitconfig_path)
740 const char *homedir = getenv("HOME");
742 *gitconfig_path = NULL;
744 if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
745 return got_error_from_errno("asprintf");
751 static const struct got_error *
752 cmd_import(int argc, char *argv[])
754 const struct got_error *error = NULL;
755 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
756 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
757 const char *branch_name = NULL;
758 char *id_str = NULL, *logmsg_path = NULL;
759 char refname[PATH_MAX] = "refs/heads/";
760 struct got_repository *repo = NULL;
761 struct got_reference *branch_ref = NULL, *head_ref = NULL;
762 struct got_object_id *new_commit_id = NULL;
764 struct got_pathlist_head ignores;
765 struct got_pathlist_entry *pe;
766 int preserve_logmsg = 0;
767 int *pack_fds = NULL;
769 TAILQ_INIT(&ignores);
771 while ((ch = getopt(argc, argv, "b:I:m:r:")) != -1) {
774 branch_name = optarg;
777 if (optarg[0] == '\0')
779 error = got_pathlist_insert(&pe, &ignores, optarg,
785 logmsg = strdup(optarg);
786 if (logmsg == NULL) {
787 error = got_error_from_errno("strdup");
792 repo_path = realpath(optarg, NULL);
793 if (repo_path == NULL) {
794 error = got_error_from_errno2("realpath",
809 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
817 if (repo_path == NULL) {
818 repo_path = getcwd(NULL, 0);
819 if (repo_path == NULL)
820 return got_error_from_errno("getcwd");
822 got_path_strip_trailing_slashes(repo_path);
823 error = get_gitconfig_path(&gitconfig_path);
826 error = got_repo_pack_fds_open(&pack_fds);
829 error = got_repo_open(&repo, repo_path, gitconfig_path, pack_fds);
833 error = get_author(&author, repo, NULL);
838 * Don't let the user create a branch name with a leading '-'.
839 * While technically a valid reference name, this case is usually
840 * an unintended typo.
842 if (branch_name && branch_name[0] == '-')
843 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
845 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
846 if (error && error->code != GOT_ERR_NOT_REF)
850 n = strlcat(refname, branch_name, sizeof(refname));
851 else if (head_ref && got_ref_is_symbolic(head_ref))
852 n = strlcpy(refname, got_ref_get_symref_target(head_ref),
855 n = strlcat(refname, "main", sizeof(refname));
856 if (n >= sizeof(refname)) {
857 error = got_error(GOT_ERR_NO_SPACE);
861 error = got_ref_open(&branch_ref, repo, refname, 0);
863 if (error->code != GOT_ERR_NOT_REF)
866 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
867 "import target branch already exists");
871 path_dir = realpath(argv[0], NULL);
872 if (path_dir == NULL) {
873 error = got_error_from_errno2("realpath", argv[0]);
876 got_path_strip_trailing_slashes(path_dir);
879 * unveil(2) traverses exec(2); if an editor is used we have
880 * to apply unveil after the log message has been written.
882 if (logmsg == NULL || strlen(logmsg) == 0) {
883 error = get_editor(&editor);
887 error = collect_import_msg(&logmsg, &logmsg_path, editor,
890 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
897 if (unveil(path_dir, "r") != 0) {
898 error = got_error_from_errno2("unveil", path_dir);
904 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
911 error = got_repo_import(&new_commit_id, path_dir, logmsg,
912 author, &ignores, repo, import_progress, NULL);
919 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
926 error = got_ref_write(branch_ref, repo);
933 error = got_object_id_str(&id_str, new_commit_id);
940 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
942 if (error->code != GOT_ERR_NOT_REF) {
948 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
956 error = got_ref_write(head_ref, repo);
964 printf("Created branch %s with commit %s\n",
965 got_ref_get_name(branch_ref), id_str);
968 const struct got_error *pack_err =
969 got_repo_pack_fds_close(pack_fds);
973 if (preserve_logmsg) {
974 fprintf(stderr, "%s: log message preserved in %s\n",
975 getprogname(), logmsg_path);
976 } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
977 error = got_error_from_errno2("unlink", logmsg_path);
985 free(gitconfig_path);
987 got_ref_close(branch_ref);
989 got_ref_close(head_ref);
996 fprintf(stderr, "usage: %s clone [-almqv] [-b branch] [-R reference] "
997 "repository-URL [directory]\n", getprogname());
1001 struct got_fetch_progress_arg {
1002 char last_scaled_size[FMT_SCALED_STRSIZE];
1004 int last_p_resolved;
1007 struct got_repository *repo;
1010 int configs_created;
1012 struct got_pathlist_head *symrefs;
1013 struct got_pathlist_head *wanted_branches;
1014 struct got_pathlist_head *wanted_refs;
1018 const char *remote_repo_path;
1019 const char *git_url;
1020 int fetch_all_branches;
1021 int mirror_references;
1025 /* XXX forward declaration */
1026 static const struct got_error *
1027 create_config_files(const char *proto, const char *host, const char *port,
1028 const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1029 int mirror_references, struct got_pathlist_head *symrefs,
1030 struct got_pathlist_head *wanted_branches,
1031 struct got_pathlist_head *wanted_refs, struct got_repository *repo);
1033 static const struct got_error *
1034 fetch_progress(void *arg, const char *message, off_t packfile_size,
1035 int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
1037 const struct got_error *err = NULL;
1038 struct got_fetch_progress_arg *a = arg;
1039 char scaled_size[FMT_SCALED_STRSIZE];
1040 int p_indexed, p_resolved;
1041 int print_size = 0, print_indexed = 0, print_resolved = 0;
1044 * In order to allow a failed clone to be resumed with 'got fetch'
1045 * we try to create configuration files as soon as possible.
1046 * Once the server has sent information about its default branch
1047 * we have all required information.
1049 if (a->create_configs && !a->configs_created &&
1050 !TAILQ_EMPTY(a->config_info.symrefs)) {
1051 err = create_config_files(a->config_info.proto,
1052 a->config_info.host, a->config_info.port,
1053 a->config_info.remote_repo_path,
1054 a->config_info.git_url,
1055 a->config_info.fetch_all_branches,
1056 a->config_info.mirror_references,
1057 a->config_info.symrefs,
1058 a->config_info.wanted_branches,
1059 a->config_info.wanted_refs, a->repo);
1062 a->configs_created = 1;
1065 if (a->verbosity < 0)
1068 if (message && message[0] != '\0') {
1069 printf("\rserver: %s", message);
1074 if (packfile_size > 0 || nobj_indexed > 0) {
1075 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
1076 (a->last_scaled_size[0] == '\0' ||
1077 strcmp(scaled_size, a->last_scaled_size)) != 0) {
1079 if (strlcpy(a->last_scaled_size, scaled_size,
1080 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
1081 return got_error(GOT_ERR_NO_SPACE);
1083 if (nobj_indexed > 0) {
1084 p_indexed = (nobj_indexed * 100) / nobj_total;
1085 if (p_indexed != a->last_p_indexed) {
1086 a->last_p_indexed = p_indexed;
1091 if (nobj_resolved > 0) {
1092 p_resolved = (nobj_resolved * 100) /
1093 (nobj_total - nobj_loose);
1094 if (p_resolved != a->last_p_resolved) {
1095 a->last_p_resolved = p_resolved;
1103 if (print_size || print_indexed || print_resolved)
1106 printf("%*s fetched", FMT_SCALED_STRSIZE - 2, scaled_size);
1108 printf("; indexing %d%%", p_indexed);
1110 printf("; resolving deltas %d%%", p_resolved);
1111 if (print_size || print_indexed || print_resolved)
1117 static const struct got_error *
1118 create_symref(const char *refname, struct got_reference *target_ref,
1119 int verbosity, struct got_repository *repo)
1121 const struct got_error *err;
1122 struct got_reference *head_symref;
1124 err = got_ref_alloc_symref(&head_symref, refname, target_ref);
1128 err = got_ref_write(head_symref, repo);
1129 if (err == NULL && verbosity > 0) {
1130 printf("Created reference %s: %s\n", GOT_REF_HEAD,
1131 got_ref_get_name(target_ref));
1133 got_ref_close(head_symref);
1137 static const struct got_error *
1138 list_remote_refs(struct got_pathlist_head *symrefs,
1139 struct got_pathlist_head *refs)
1141 const struct got_error *err;
1142 struct got_pathlist_entry *pe;
1144 TAILQ_FOREACH(pe, symrefs, entry) {
1145 const char *refname = pe->path;
1146 const char *targetref = pe->data;
1148 printf("%s: %s\n", refname, targetref);
1151 TAILQ_FOREACH(pe, refs, entry) {
1152 const char *refname = pe->path;
1153 struct got_object_id *id = pe->data;
1156 err = got_object_id_str(&id_str, id);
1159 printf("%s: %s\n", refname, id_str);
1166 static const struct got_error *
1167 create_ref(const char *refname, struct got_object_id *id,
1168 int verbosity, struct got_repository *repo)
1170 const struct got_error *err = NULL;
1171 struct got_reference *ref;
1174 err = got_object_id_str(&id_str, id);
1178 err = got_ref_alloc(&ref, refname, id);
1182 err = got_ref_write(ref, repo);
1185 if (err == NULL && verbosity >= 0)
1186 printf("Created reference %s: %s\n", refname, id_str);
1193 match_wanted_ref(const char *refname, const char *wanted_ref)
1195 if (strncmp(refname, "refs/", 5) != 0)
1200 * Prevent fetching of references that won't make any
1201 * sense outside of the remote repository's context.
1203 if (strncmp(refname, "got/", 4) == 0)
1205 if (strncmp(refname, "remotes/", 8) == 0)
1208 if (strncmp(wanted_ref, "refs/", 5) == 0)
1211 /* Allow prefix match. */
1212 if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
1215 /* Allow exact match. */
1216 return (strcmp(refname, wanted_ref) == 0);
1220 is_wanted_ref(struct got_pathlist_head *wanted_refs, const char *refname)
1222 struct got_pathlist_entry *pe;
1224 TAILQ_FOREACH(pe, wanted_refs, entry) {
1225 if (match_wanted_ref(refname, pe->path))
1232 static const struct got_error *
1233 create_wanted_ref(const char *refname, struct got_object_id *id,
1234 const char *remote_repo_name, int verbosity, struct got_repository *repo)
1236 const struct got_error *err;
1237 char *remote_refname;
1239 if (strncmp("refs/", refname, 5) == 0)
1242 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1243 remote_repo_name, refname) == -1)
1244 return got_error_from_errno("asprintf");
1246 err = create_ref(remote_refname, id, verbosity, repo);
1247 free(remote_refname);
1251 static const struct got_error *
1252 create_gotconfig(const char *proto, const char *host, const char *port,
1253 const char *remote_repo_path, const char *default_branch,
1254 int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1255 struct got_pathlist_head *wanted_refs, int mirror_references,
1256 struct got_repository *repo)
1258 const struct got_error *err = NULL;
1259 char *gotconfig_path = NULL;
1260 char *gotconfig = NULL;
1261 FILE *gotconfig_file = NULL;
1262 const char *branchname = NULL;
1263 char *branches = NULL, *refs = NULL;
1266 if (!fetch_all_branches && !TAILQ_EMPTY(wanted_branches)) {
1267 struct got_pathlist_entry *pe;
1268 TAILQ_FOREACH(pe, wanted_branches, entry) {
1270 branchname = pe->path;
1271 if (strncmp(branchname, "refs/heads/", 11) == 0)
1273 if (asprintf(&s, "%s\"%s\" ",
1274 branches ? branches : "", branchname) == -1) {
1275 err = got_error_from_errno("asprintf");
1281 } else if (!fetch_all_branches && default_branch) {
1282 branchname = default_branch;
1283 if (strncmp(branchname, "refs/heads/", 11) == 0)
1285 if (asprintf(&branches, "\"%s\" ", branchname) == -1) {
1286 err = got_error_from_errno("asprintf");
1290 if (!TAILQ_EMPTY(wanted_refs)) {
1291 struct got_pathlist_entry *pe;
1292 TAILQ_FOREACH(pe, wanted_refs, entry) {
1294 const char *refname = pe->path;
1295 if (strncmp(refname, "refs/", 5) == 0)
1297 if (asprintf(&s, "%s\"%s\" ",
1298 refs ? refs : "", refname) == -1) {
1299 err = got_error_from_errno("asprintf");
1307 /* Create got.conf(5). */
1308 gotconfig_path = got_repo_get_path_gotconfig(repo);
1309 if (gotconfig_path == NULL) {
1310 err = got_error_from_errno("got_repo_get_path_gotconfig");
1313 gotconfig_file = fopen(gotconfig_path, "ae");
1314 if (gotconfig_file == NULL) {
1315 err = got_error_from_errno2("fopen", gotconfig_path);
1318 if (asprintf(&gotconfig,
1323 "\trepository \"%s\"\n"
1329 GOT_FETCH_DEFAULT_REMOTE_NAME, host, proto,
1330 port ? "\tport " : "", port ? port : "", port ? "\n" : "",
1331 remote_repo_path, branches ? "\tbranch { " : "",
1332 branches ? branches : "", branches ? "}\n" : "",
1333 refs ? "\treference { " : "", refs ? refs : "", refs ? "}\n" : "",
1334 mirror_references ? "\tmirror_references yes\n" : "",
1335 fetch_all_branches ? "\tfetch_all_branches yes\n" : "") == -1) {
1336 err = got_error_from_errno("asprintf");
1339 n = fwrite(gotconfig, 1, strlen(gotconfig), gotconfig_file);
1340 if (n != strlen(gotconfig)) {
1341 err = got_ferror(gotconfig_file, GOT_ERR_IO);
1346 if (gotconfig_file && fclose(gotconfig_file) == EOF && err == NULL)
1347 err = got_error_from_errno2("fclose", gotconfig_path);
1348 free(gotconfig_path);
1353 static const struct got_error *
1354 create_gitconfig(const char *git_url, const char *default_branch,
1355 int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1356 struct got_pathlist_head *wanted_refs, int mirror_references,
1357 struct got_repository *repo)
1359 const struct got_error *err = NULL;
1360 char *gitconfig_path = NULL;
1361 char *gitconfig = NULL;
1362 FILE *gitconfig_file = NULL;
1363 char *branches = NULL, *refs = NULL;
1364 const char *branchname;
1367 /* Create a config file Git can understand. */
1368 gitconfig_path = got_repo_get_path_gitconfig(repo);
1369 if (gitconfig_path == NULL) {
1370 err = got_error_from_errno("got_repo_get_path_gitconfig");
1373 gitconfig_file = fopen(gitconfig_path, "ae");
1374 if (gitconfig_file == NULL) {
1375 err = got_error_from_errno2("fopen", gitconfig_path);
1378 if (fetch_all_branches) {
1379 if (mirror_references) {
1380 if (asprintf(&branches,
1381 "\tfetch = refs/heads/*:refs/heads/*\n") == -1) {
1382 err = got_error_from_errno("asprintf");
1385 } else if (asprintf(&branches,
1386 "\tfetch = refs/heads/*:refs/remotes/%s/*\n",
1387 GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1388 err = got_error_from_errno("asprintf");
1391 } else if (!TAILQ_EMPTY(wanted_branches)) {
1392 struct got_pathlist_entry *pe;
1393 TAILQ_FOREACH(pe, wanted_branches, entry) {
1395 branchname = pe->path;
1396 if (strncmp(branchname, "refs/heads/", 11) == 0)
1398 if (mirror_references) {
1400 "%s\tfetch = refs/heads/%s:refs/heads/%s\n",
1401 branches ? branches : "",
1402 branchname, branchname) == -1) {
1403 err = got_error_from_errno("asprintf");
1406 } else if (asprintf(&s,
1407 "%s\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1408 branches ? branches : "",
1409 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1410 branchname) == -1) {
1411 err = got_error_from_errno("asprintf");
1419 * If the server specified a default branch, use just that one.
1420 * Otherwise fall back to fetching all branches on next fetch.
1422 if (default_branch) {
1423 branchname = default_branch;
1424 if (strncmp(branchname, "refs/heads/", 11) == 0)
1427 branchname = "*"; /* fall back to all branches */
1428 if (mirror_references) {
1429 if (asprintf(&branches,
1430 "\tfetch = refs/heads/%s:refs/heads/%s\n",
1431 branchname, branchname) == -1) {
1432 err = got_error_from_errno("asprintf");
1435 } else if (asprintf(&branches,
1436 "\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1437 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1438 branchname) == -1) {
1439 err = got_error_from_errno("asprintf");
1443 if (!TAILQ_EMPTY(wanted_refs)) {
1444 struct got_pathlist_entry *pe;
1445 TAILQ_FOREACH(pe, wanted_refs, entry) {
1447 const char *refname = pe->path;
1448 if (strncmp(refname, "refs/", 5) == 0)
1450 if (mirror_references) {
1452 "%s\tfetch = refs/%s:refs/%s\n",
1453 refs ? refs : "", refname, refname) == -1) {
1454 err = got_error_from_errno("asprintf");
1457 } else if (asprintf(&s,
1458 "%s\tfetch = refs/%s:refs/remotes/%s/%s\n",
1460 refname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1462 err = got_error_from_errno("asprintf");
1470 if (asprintf(&gitconfig,
1475 "\tfetch = refs/tags/*:refs/tags/*\n",
1476 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url, branches ? branches : "",
1477 refs ? refs : "") == -1) {
1478 err = got_error_from_errno("asprintf");
1481 n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1482 if (n != strlen(gitconfig)) {
1483 err = got_ferror(gitconfig_file, GOT_ERR_IO);
1487 if (gitconfig_file && fclose(gitconfig_file) == EOF && err == NULL)
1488 err = got_error_from_errno2("fclose", gitconfig_path);
1489 free(gitconfig_path);
1494 static const struct got_error *
1495 create_config_files(const char *proto, const char *host, const char *port,
1496 const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1497 int mirror_references, struct got_pathlist_head *symrefs,
1498 struct got_pathlist_head *wanted_branches,
1499 struct got_pathlist_head *wanted_refs, struct got_repository *repo)
1501 const struct got_error *err = NULL;
1502 const char *default_branch = NULL;
1503 struct got_pathlist_entry *pe;
1506 * If we asked for a set of wanted branches then use the first
1509 if (!TAILQ_EMPTY(wanted_branches)) {
1510 pe = TAILQ_FIRST(wanted_branches);
1511 default_branch = pe->path;
1513 /* First HEAD ref listed by server is the default branch. */
1514 TAILQ_FOREACH(pe, symrefs, entry) {
1515 const char *refname = pe->path;
1516 const char *target = pe->data;
1518 if (strcmp(refname, GOT_REF_HEAD) != 0)
1521 default_branch = target;
1526 /* Create got.conf(5). */
1527 err = create_gotconfig(proto, host, port, remote_repo_path,
1528 default_branch, fetch_all_branches, wanted_branches,
1529 wanted_refs, mirror_references, repo);
1533 /* Create a config file Git can understand. */
1534 return create_gitconfig(git_url, default_branch, fetch_all_branches,
1535 wanted_branches, wanted_refs, mirror_references, repo);
1538 static const struct got_error *
1539 cmd_clone(int argc, char *argv[])
1541 const struct got_error *error = NULL;
1542 const char *uri, *dirname;
1543 char *proto, *host, *port, *repo_name, *server_path;
1544 char *default_destdir = NULL, *id_str = NULL;
1545 const char *repo_path;
1546 struct got_repository *repo = NULL;
1547 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1548 struct got_pathlist_entry *pe;
1549 struct got_object_id *pack_hash = NULL;
1550 int ch, fetchfd = -1, fetchstatus;
1551 pid_t fetchpid = -1;
1552 struct got_fetch_progress_arg fpa;
1553 char *git_url = NULL;
1554 int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
1555 int list_refs_only = 0;
1556 int *pack_fds = NULL;
1559 TAILQ_INIT(&symrefs);
1560 TAILQ_INIT(&wanted_branches);
1561 TAILQ_INIT(&wanted_refs);
1563 while ((ch = getopt(argc, argv, "ab:lmqR:v")) != -1) {
1566 fetch_all_branches = 1;
1569 error = got_pathlist_append(&wanted_branches,
1578 mirror_references = 1;
1584 error = got_pathlist_append(&wanted_refs,
1592 else if (verbosity < 3)
1603 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1604 option_conflict('a', 'b');
1605 if (list_refs_only) {
1606 if (!TAILQ_EMPTY(&wanted_branches))
1607 option_conflict('l', 'b');
1608 if (fetch_all_branches)
1609 option_conflict('l', 'a');
1610 if (mirror_references)
1611 option_conflict('l', 'm');
1612 if (!TAILQ_EMPTY(&wanted_refs))
1613 option_conflict('l', 'R');
1625 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
1630 if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1631 host, port ? ":" : "", port ? port : "",
1632 server_path[0] != '/' ? "/" : "", server_path) == -1) {
1633 error = got_error_from_errno("asprintf");
1637 if (strcmp(proto, "git") == 0) {
1639 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1640 "sendfd dns inet unveil", NULL) == -1)
1643 } else if (strcmp(proto, "git+ssh") == 0 ||
1644 strcmp(proto, "ssh") == 0) {
1646 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1647 "sendfd unveil", NULL) == -1)
1650 } else if (strcmp(proto, "http") == 0 ||
1651 strcmp(proto, "git+http") == 0) {
1652 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1655 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1658 if (dirname == NULL) {
1659 if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1660 error = got_error_from_errno("asprintf");
1663 repo_path = default_destdir;
1665 repo_path = dirname;
1667 if (!list_refs_only) {
1668 error = got_path_mkdir(repo_path);
1670 (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1671 !(error->code == GOT_ERR_ERRNO && errno == EEXIST)))
1673 if (!got_path_dir_is_empty(repo_path)) {
1674 error = got_error_path(repo_path,
1675 GOT_ERR_DIR_NOT_EMPTY);
1680 error = got_dial_apply_unveil(proto);
1684 error = apply_unveil(repo_path, 0, NULL);
1689 printf("Connecting to %s\n", git_url);
1691 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1692 server_path, verbosity);
1696 if (!list_refs_only) {
1697 error = got_repo_init(repo_path, NULL);
1700 error = got_repo_pack_fds_open(&pack_fds);
1703 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
1708 fpa.last_scaled_size[0] = '\0';
1709 fpa.last_p_indexed = -1;
1710 fpa.last_p_resolved = -1;
1711 fpa.verbosity = verbosity;
1712 fpa.create_configs = 1;
1713 fpa.configs_created = 0;
1715 fpa.config_info.symrefs = &symrefs;
1716 fpa.config_info.wanted_branches = &wanted_branches;
1717 fpa.config_info.wanted_refs = &wanted_refs;
1718 fpa.config_info.proto = proto;
1719 fpa.config_info.host = host;
1720 fpa.config_info.port = port;
1721 fpa.config_info.remote_repo_path = server_path;
1722 fpa.config_info.git_url = git_url;
1723 fpa.config_info.fetch_all_branches = fetch_all_branches;
1724 fpa.config_info.mirror_references = mirror_references;
1725 error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1726 GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1727 fetch_all_branches, &wanted_branches, &wanted_refs,
1728 list_refs_only, verbosity, fetchfd, repo,
1729 fetch_progress, &fpa);
1733 if (list_refs_only) {
1734 error = list_remote_refs(&symrefs, &refs);
1738 if (pack_hash == NULL) {
1739 error = got_error_fmt(GOT_ERR_FETCH_FAILED, "%s",
1740 "server sent an empty pack file");
1743 error = got_object_id_str(&id_str, pack_hash);
1747 printf("\nFetched %s.pack\n", id_str);
1750 /* Set up references provided with the pack file. */
1751 TAILQ_FOREACH(pe, &refs, entry) {
1752 const char *refname = pe->path;
1753 struct got_object_id *id = pe->data;
1754 char *remote_refname;
1756 if (is_wanted_ref(&wanted_refs, refname) &&
1757 !mirror_references) {
1758 error = create_wanted_ref(refname, id,
1759 GOT_FETCH_DEFAULT_REMOTE_NAME,
1760 verbosity - 1, repo);
1766 error = create_ref(refname, id, verbosity - 1, repo);
1770 if (mirror_references)
1773 if (strncmp("refs/heads/", refname, 11) != 0)
1776 if (asprintf(&remote_refname,
1777 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1778 refname + 11) == -1) {
1779 error = got_error_from_errno("asprintf");
1782 error = create_ref(remote_refname, id, verbosity - 1, repo);
1783 free(remote_refname);
1788 /* Set the HEAD reference if the server provided one. */
1789 TAILQ_FOREACH(pe, &symrefs, entry) {
1790 struct got_reference *target_ref;
1791 const char *refname = pe->path;
1792 const char *target = pe->data;
1793 char *remote_refname = NULL, *remote_target = NULL;
1795 if (strcmp(refname, GOT_REF_HEAD) != 0)
1798 error = got_ref_open(&target_ref, repo, target, 0);
1800 if (error->code == GOT_ERR_NOT_REF) {
1807 error = create_symref(refname, target_ref, verbosity, repo);
1808 got_ref_close(target_ref);
1812 if (mirror_references)
1815 if (strncmp("refs/heads/", target, 11) != 0)
1818 if (asprintf(&remote_refname,
1819 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1821 error = got_error_from_errno("asprintf");
1824 if (asprintf(&remote_target,
1825 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1826 target + 11) == -1) {
1827 error = got_error_from_errno("asprintf");
1828 free(remote_refname);
1831 error = got_ref_open(&target_ref, repo, remote_target, 0);
1833 free(remote_refname);
1834 free(remote_target);
1835 if (error->code == GOT_ERR_NOT_REF) {
1841 error = create_symref(remote_refname, target_ref,
1842 verbosity - 1, repo);
1843 free(remote_refname);
1844 free(remote_target);
1845 got_ref_close(target_ref);
1851 * We failed to set the HEAD reference. If we asked for
1852 * a set of wanted branches use the first of one of those
1853 * which could be fetched instead.
1855 TAILQ_FOREACH(pe, &wanted_branches, entry) {
1856 const char *target = pe->path;
1857 struct got_reference *target_ref;
1859 error = got_ref_open(&target_ref, repo, target, 0);
1861 if (error->code == GOT_ERR_NOT_REF) {
1868 error = create_symref(GOT_REF_HEAD, target_ref,
1870 got_ref_close(target_ref);
1876 if (!fpa.configs_created && pe != NULL) {
1877 error = create_config_files(fpa.config_info.proto,
1878 fpa.config_info.host, fpa.config_info.port,
1879 fpa.config_info.remote_repo_path,
1880 fpa.config_info.git_url,
1881 fpa.config_info.fetch_all_branches,
1882 fpa.config_info.mirror_references,
1883 fpa.config_info.symrefs,
1884 fpa.config_info.wanted_branches,
1885 fpa.config_info.wanted_refs, fpa.repo);
1892 printf("Created %s repository '%s'\n",
1893 mirror_references ? "mirrored" : "cloned", repo_path);
1896 const struct got_error *pack_err =
1897 got_repo_pack_fds_close(pack_fds);
1902 if (kill(fetchpid, SIGTERM) == -1)
1903 error = got_error_from_errno("kill");
1904 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1905 error = got_error_from_errno("waitpid");
1907 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1908 error = got_error_from_errno("close");
1910 const struct got_error *close_err = got_repo_close(repo);
1914 got_pathlist_free(&refs, GOT_PATHLIST_FREE_ALL);
1915 got_pathlist_free(&symrefs, GOT_PATHLIST_FREE_ALL);
1916 got_pathlist_free(&wanted_branches, GOT_PATHLIST_FREE_NONE);
1917 got_pathlist_free(&wanted_refs, GOT_PATHLIST_FREE_NONE);
1924 free(default_destdir);
1929 static const struct got_error *
1930 update_ref(struct got_reference *ref, struct got_object_id *new_id,
1931 int replace_tags, int verbosity, struct got_repository *repo)
1933 const struct got_error *err = NULL;
1934 char *new_id_str = NULL;
1935 struct got_object_id *old_id = NULL;
1937 err = got_object_id_str(&new_id_str, new_id);
1941 if (!replace_tags &&
1942 strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1943 err = got_ref_resolve(&old_id, repo, ref);
1946 if (got_object_id_cmp(old_id, new_id) == 0)
1948 if (verbosity >= 0) {
1949 printf("Rejecting update of existing tag %s: %s\n",
1950 got_ref_get_name(ref), new_id_str);
1955 if (got_ref_is_symbolic(ref)) {
1956 if (verbosity >= 0) {
1957 printf("Replacing reference %s: %s\n",
1958 got_ref_get_name(ref),
1959 got_ref_get_symref_target(ref));
1961 err = got_ref_change_symref_to_ref(ref, new_id);
1964 err = got_ref_write(ref, repo);
1968 err = got_ref_resolve(&old_id, repo, ref);
1971 if (got_object_id_cmp(old_id, new_id) == 0)
1974 err = got_ref_change_ref(ref, new_id);
1977 err = got_ref_write(ref, repo);
1983 printf("Updated %s: %s\n", got_ref_get_name(ref),
1991 static const struct got_error *
1992 update_symref(const char *refname, struct got_reference *target_ref,
1993 int verbosity, struct got_repository *repo)
1995 const struct got_error *err = NULL, *unlock_err;
1996 struct got_reference *symref;
1997 int symref_is_locked = 0;
1999 err = got_ref_open(&symref, repo, refname, 1);
2001 if (err->code != GOT_ERR_NOT_REF)
2003 err = got_ref_alloc_symref(&symref, refname, target_ref);
2007 err = got_ref_write(symref, repo);
2012 printf("Created reference %s: %s\n",
2013 got_ref_get_name(symref),
2014 got_ref_get_symref_target(symref));
2016 symref_is_locked = 1;
2018 if (strcmp(got_ref_get_symref_target(symref),
2019 got_ref_get_name(target_ref)) == 0)
2022 err = got_ref_change_symref(symref,
2023 got_ref_get_name(target_ref));
2027 err = got_ref_write(symref, repo);
2032 printf("Updated %s: %s\n", got_ref_get_name(symref),
2033 got_ref_get_symref_target(symref));
2037 if (symref_is_locked) {
2038 unlock_err = got_ref_unlock(symref);
2039 if (unlock_err && err == NULL)
2042 got_ref_close(symref);
2049 fprintf(stderr, "usage: %s fetch [-adlqtvX] [-b branch] "
2050 "[-R reference] [-r repository-path] [remote-repository]\n",
2055 static const struct got_error *
2056 delete_missing_ref(struct got_reference *ref,
2057 int verbosity, struct got_repository *repo)
2059 const struct got_error *err = NULL;
2060 struct got_object_id *id = NULL;
2061 char *id_str = NULL;
2063 if (got_ref_is_symbolic(ref)) {
2064 err = got_ref_delete(ref, repo);
2067 if (verbosity >= 0) {
2068 printf("Deleted %s: %s\n",
2069 got_ref_get_name(ref),
2070 got_ref_get_symref_target(ref));
2073 err = got_ref_resolve(&id, repo, ref);
2076 err = got_object_id_str(&id_str, id);
2080 err = got_ref_delete(ref, repo);
2083 if (verbosity >= 0) {
2084 printf("Deleted %s: %s\n",
2085 got_ref_get_name(ref), id_str);
2094 static const struct got_error *
2095 delete_missing_refs(struct got_pathlist_head *their_refs,
2096 struct got_pathlist_head *their_symrefs,
2097 const struct got_remote_repo *remote,
2098 int verbosity, struct got_repository *repo)
2100 const struct got_error *err = NULL, *unlock_err;
2101 struct got_reflist_head my_refs;
2102 struct got_reflist_entry *re;
2103 struct got_pathlist_entry *pe;
2104 char *remote_namespace = NULL;
2105 char *local_refname = NULL;
2107 TAILQ_INIT(&my_refs);
2109 if (asprintf(&remote_namespace, "refs/remotes/%s/", remote->name)
2111 return got_error_from_errno("asprintf");
2113 err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
2117 TAILQ_FOREACH(re, &my_refs, entry) {
2118 const char *refname = got_ref_get_name(re->ref);
2119 const char *their_refname;
2121 if (remote->mirror_references) {
2122 their_refname = refname;
2124 if (strncmp(refname, remote_namespace,
2125 strlen(remote_namespace)) == 0) {
2126 if (strcmp(refname + strlen(remote_namespace),
2129 if (asprintf(&local_refname, "refs/heads/%s",
2130 refname + strlen(remote_namespace)) == -1) {
2131 err = got_error_from_errno("asprintf");
2134 } else if (strncmp(refname, "refs/tags/", 10) != 0)
2137 their_refname = local_refname;
2140 TAILQ_FOREACH(pe, their_refs, entry) {
2141 if (strcmp(their_refname, pe->path) == 0)
2147 TAILQ_FOREACH(pe, their_symrefs, entry) {
2148 if (strcmp(their_refname, pe->path) == 0)
2154 err = delete_missing_ref(re->ref, verbosity, repo);
2158 if (local_refname) {
2159 struct got_reference *ref;
2160 err = got_ref_open(&ref, repo, local_refname, 1);
2162 if (err->code != GOT_ERR_NOT_REF)
2164 free(local_refname);
2165 local_refname = NULL;
2168 err = delete_missing_ref(ref, verbosity, repo);
2171 unlock_err = got_ref_unlock(ref);
2173 if (unlock_err && err == NULL) {
2178 free(local_refname);
2179 local_refname = NULL;
2183 got_ref_list_free(&my_refs);
2184 free(remote_namespace);
2185 free(local_refname);
2189 static const struct got_error *
2190 update_wanted_ref(const char *refname, struct got_object_id *id,
2191 const char *remote_repo_name, int verbosity, struct got_repository *repo)
2193 const struct got_error *err, *unlock_err;
2194 char *remote_refname;
2195 struct got_reference *ref;
2197 if (strncmp("refs/", refname, 5) == 0)
2200 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2201 remote_repo_name, refname) == -1)
2202 return got_error_from_errno("asprintf");
2204 err = got_ref_open(&ref, repo, remote_refname, 1);
2206 if (err->code != GOT_ERR_NOT_REF)
2208 err = create_ref(remote_refname, id, verbosity, repo);
2210 err = update_ref(ref, id, 0, verbosity, repo);
2211 unlock_err = got_ref_unlock(ref);
2212 if (unlock_err && err == NULL)
2217 free(remote_refname);
2221 static const struct got_error *
2222 delete_ref(struct got_repository *repo, struct got_reference *ref)
2224 const struct got_error *err = NULL;
2225 struct got_object_id *id = NULL;
2226 char *id_str = NULL;
2229 if (got_ref_is_symbolic(ref)) {
2230 target = got_ref_get_symref_target(ref);
2232 err = got_ref_resolve(&id, repo, ref);
2235 err = got_object_id_str(&id_str, id);
2241 err = got_ref_delete(ref, repo);
2245 printf("Deleted %s: %s\n", got_ref_get_name(ref), target);
2252 static const struct got_error *
2253 delete_refs_for_remote(struct got_repository *repo, const char *remote_name)
2255 const struct got_error *err = NULL;
2256 struct got_reflist_head refs;
2257 struct got_reflist_entry *re;
2262 if (asprintf(&prefix, "refs/remotes/%s", remote_name) == -1) {
2263 err = got_error_from_errno("asprintf");
2266 err = got_ref_list(&refs, repo, prefix, got_ref_cmp_by_name, NULL);
2270 TAILQ_FOREACH(re, &refs, entry)
2271 delete_ref(repo, re->ref);
2273 got_ref_list_free(&refs);
2277 static const struct got_error *
2278 cmd_fetch(int argc, char *argv[])
2280 const struct got_error *error = NULL, *unlock_err;
2281 char *cwd = NULL, *repo_path = NULL;
2282 const char *remote_name;
2283 char *proto = NULL, *host = NULL, *port = NULL;
2284 char *repo_name = NULL, *server_path = NULL;
2285 const struct got_remote_repo *remotes, *remote = NULL;
2287 char *id_str = NULL;
2288 struct got_repository *repo = NULL;
2289 struct got_worktree *worktree = NULL;
2290 const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
2291 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
2292 struct got_pathlist_entry *pe;
2293 struct got_object_id *pack_hash = NULL;
2294 int i, ch, fetchfd = -1, fetchstatus;
2295 pid_t fetchpid = -1;
2296 struct got_fetch_progress_arg fpa;
2297 int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
2298 int delete_refs = 0, replace_tags = 0, delete_remote = 0;
2299 int *pack_fds = NULL;
2302 TAILQ_INIT(&symrefs);
2303 TAILQ_INIT(&wanted_branches);
2304 TAILQ_INIT(&wanted_refs);
2306 while ((ch = getopt(argc, argv, "ab:dlqR:r:tvX")) != -1) {
2309 fetch_all_branches = 1;
2312 error = got_pathlist_append(&wanted_branches,
2327 error = got_pathlist_append(&wanted_refs,
2333 repo_path = realpath(optarg, NULL);
2334 if (repo_path == NULL)
2335 return got_error_from_errno2("realpath",
2337 got_path_strip_trailing_slashes(repo_path);
2345 else if (verbosity < 3)
2359 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
2360 option_conflict('a', 'b');
2361 if (list_refs_only) {
2362 if (!TAILQ_EMPTY(&wanted_branches))
2363 option_conflict('l', 'b');
2364 if (fetch_all_branches)
2365 option_conflict('l', 'a');
2367 option_conflict('l', 'd');
2369 option_conflict('l', 'X');
2371 if (delete_remote) {
2372 if (fetch_all_branches)
2373 option_conflict('X', 'a');
2374 if (!TAILQ_EMPTY(&wanted_branches))
2375 option_conflict('X', 'b');
2377 option_conflict('X', 'd');
2379 option_conflict('X', 't');
2380 if (!TAILQ_EMPTY(&wanted_refs))
2381 option_conflict('X', 'R');
2386 errx(1, "-X option requires a remote name");
2387 remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
2388 } else if (argc == 1)
2389 remote_name = argv[0];
2393 cwd = getcwd(NULL, 0);
2395 error = got_error_from_errno("getcwd");
2399 error = got_repo_pack_fds_open(&pack_fds);
2403 if (repo_path == NULL) {
2404 error = got_worktree_open(&worktree, cwd);
2405 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2411 strdup(got_worktree_get_repo_path(worktree));
2412 if (repo_path == NULL)
2413 error = got_error_from_errno("strdup");
2417 repo_path = strdup(cwd);
2418 if (repo_path == NULL) {
2419 error = got_error_from_errno("strdup");
2425 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
2429 if (delete_remote) {
2430 error = delete_refs_for_remote(repo, remote_name);
2431 goto done; /* nothing else to do */
2435 worktree_conf = got_worktree_get_gotconfig(worktree);
2436 if (worktree_conf) {
2437 got_gotconfig_get_remotes(&nremotes, &remotes,
2439 for (i = 0; i < nremotes; i++) {
2440 if (strcmp(remotes[i].name, remote_name) == 0) {
2441 remote = &remotes[i];
2447 if (remote == NULL) {
2448 repo_conf = got_repo_get_gotconfig(repo);
2450 got_gotconfig_get_remotes(&nremotes, &remotes,
2452 for (i = 0; i < nremotes; i++) {
2453 if (strcmp(remotes[i].name, remote_name) == 0) {
2454 remote = &remotes[i];
2460 if (remote == NULL) {
2461 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
2462 for (i = 0; i < nremotes; i++) {
2463 if (strcmp(remotes[i].name, remote_name) == 0) {
2464 remote = &remotes[i];
2469 if (remote == NULL) {
2470 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
2474 if (TAILQ_EMPTY(&wanted_branches)) {
2475 if (!fetch_all_branches)
2476 fetch_all_branches = remote->fetch_all_branches;
2477 for (i = 0; i < remote->nfetch_branches; i++) {
2478 error = got_pathlist_append(&wanted_branches,
2479 remote->fetch_branches[i], NULL);
2484 if (TAILQ_EMPTY(&wanted_refs)) {
2485 for (i = 0; i < remote->nfetch_refs; i++) {
2486 error = got_pathlist_append(&wanted_refs,
2487 remote->fetch_refs[i], NULL);
2493 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
2494 &repo_name, remote->fetch_url);
2498 if (strcmp(proto, "git") == 0) {
2500 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2501 "sendfd dns inet unveil", NULL) == -1)
2504 } else if (strcmp(proto, "git+ssh") == 0 ||
2505 strcmp(proto, "ssh") == 0) {
2507 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2508 "sendfd unveil", NULL) == -1)
2511 } else if (strcmp(proto, "http") == 0 ||
2512 strcmp(proto, "git+http") == 0) {
2513 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
2516 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
2520 error = got_dial_apply_unveil(proto);
2524 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
2528 if (verbosity >= 0) {
2529 printf("Connecting to \"%s\" %s://%s%s%s%s%s\n",
2530 remote->name, proto, host,
2531 port ? ":" : "", port ? port : "",
2532 *server_path == '/' ? "" : "/", server_path);
2535 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
2536 server_path, verbosity);
2540 fpa.last_scaled_size[0] = '\0';
2541 fpa.last_p_indexed = -1;
2542 fpa.last_p_resolved = -1;
2543 fpa.verbosity = verbosity;
2545 fpa.create_configs = 0;
2546 fpa.configs_created = 0;
2547 memset(&fpa.config_info, 0, sizeof(fpa.config_info));
2548 error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
2549 remote->mirror_references, fetch_all_branches, &wanted_branches,
2550 &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
2551 fetch_progress, &fpa);
2555 if (list_refs_only) {
2556 error = list_remote_refs(&symrefs, &refs);
2560 if (pack_hash == NULL) {
2562 printf("Already up-to-date\n");
2563 } else if (verbosity >= 0) {
2564 error = got_object_id_str(&id_str, pack_hash);
2567 printf("\nFetched %s.pack\n", id_str);
2572 /* Update references provided with the pack file. */
2573 TAILQ_FOREACH(pe, &refs, entry) {
2574 const char *refname = pe->path;
2575 struct got_object_id *id = pe->data;
2576 struct got_reference *ref;
2577 char *remote_refname;
2579 if (is_wanted_ref(&wanted_refs, refname) &&
2580 !remote->mirror_references) {
2581 error = update_wanted_ref(refname, id,
2582 remote->name, verbosity, repo);
2588 if (remote->mirror_references ||
2589 strncmp("refs/tags/", refname, 10) == 0) {
2590 error = got_ref_open(&ref, repo, refname, 1);
2592 if (error->code != GOT_ERR_NOT_REF)
2594 error = create_ref(refname, id, verbosity,
2599 error = update_ref(ref, id, replace_tags,
2601 unlock_err = got_ref_unlock(ref);
2602 if (unlock_err && error == NULL)
2608 } else if (strncmp("refs/heads/", refname, 11) == 0) {
2609 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2610 remote_name, refname + 11) == -1) {
2611 error = got_error_from_errno("asprintf");
2615 error = got_ref_open(&ref, repo, remote_refname, 1);
2617 if (error->code != GOT_ERR_NOT_REF)
2619 error = create_ref(remote_refname, id,
2624 error = update_ref(ref, id, replace_tags,
2626 unlock_err = got_ref_unlock(ref);
2627 if (unlock_err && error == NULL)
2634 /* Also create a local branch if none exists yet. */
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 unlock_err = got_ref_unlock(ref);
2645 if (unlock_err && error == NULL)
2652 error = delete_missing_refs(&refs, &symrefs, remote,
2658 if (!remote->mirror_references) {
2659 /* Update remote HEAD reference if the server provided one. */
2660 TAILQ_FOREACH(pe, &symrefs, entry) {
2661 struct got_reference *target_ref;
2662 const char *refname = pe->path;
2663 const char *target = pe->data;
2664 char *remote_refname = NULL, *remote_target = NULL;
2666 if (strcmp(refname, GOT_REF_HEAD) != 0)
2669 if (strncmp("refs/heads/", target, 11) != 0)
2672 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2673 remote->name, refname) == -1) {
2674 error = got_error_from_errno("asprintf");
2677 if (asprintf(&remote_target, "refs/remotes/%s/%s",
2678 remote->name, target + 11) == -1) {
2679 error = got_error_from_errno("asprintf");
2680 free(remote_refname);
2684 error = got_ref_open(&target_ref, repo, remote_target,
2687 free(remote_refname);
2688 free(remote_target);
2689 if (error->code == GOT_ERR_NOT_REF) {
2695 error = update_symref(remote_refname, target_ref,
2697 free(remote_refname);
2698 free(remote_target);
2699 got_ref_close(target_ref);
2706 if (kill(fetchpid, SIGTERM) == -1)
2707 error = got_error_from_errno("kill");
2708 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
2709 error = got_error_from_errno("waitpid");
2711 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
2712 error = got_error_from_errno("close");
2714 const struct got_error *close_err = got_repo_close(repo);
2719 got_worktree_close(worktree);
2721 const struct got_error *pack_err =
2722 got_repo_pack_fds_close(pack_fds);
2726 got_pathlist_free(&refs, GOT_PATHLIST_FREE_ALL);
2727 got_pathlist_free(&symrefs, GOT_PATHLIST_FREE_ALL);
2728 got_pathlist_free(&wanted_branches, GOT_PATHLIST_FREE_NONE);
2729 got_pathlist_free(&wanted_refs, GOT_PATHLIST_FREE_NONE);
2744 usage_checkout(void)
2746 fprintf(stderr, "usage: %s checkout [-Eq] [-b branch] [-c commit] "
2747 "[-p path-prefix] repository-path [work-tree-path]\n",
2753 show_worktree_base_ref_warning(void)
2755 fprintf(stderr, "%s: warning: could not create a reference "
2756 "to the work tree's base commit; the commit could be "
2757 "garbage-collected by Git or 'gotadmin cleanup'; making the "
2758 "repository writable and running 'got update' will prevent this\n",
2762 struct got_checkout_progress_arg {
2763 const char *worktree_path;
2764 int had_base_commit_ref_error;
2768 static const struct got_error *
2769 checkout_progress(void *arg, unsigned char status, const char *path)
2771 struct got_checkout_progress_arg *a = arg;
2773 /* Base commit bump happens silently. */
2774 if (status == GOT_STATUS_BUMP_BASE)
2777 if (status == GOT_STATUS_BASE_REF_ERR) {
2778 a->had_base_commit_ref_error = 1;
2782 while (path[0] == '/')
2785 if (a->verbosity >= 0)
2786 printf("%c %s/%s\n", status, a->worktree_path, path);
2791 static const struct got_error *
2792 check_cancelled(void *arg)
2794 if (sigint_received || sigpipe_received)
2795 return got_error(GOT_ERR_CANCELLED);
2799 static const struct got_error *
2800 check_linear_ancestry(struct got_object_id *commit_id,
2801 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2802 struct got_repository *repo)
2804 const struct got_error *err = NULL;
2805 struct got_object_id *yca_id;
2807 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2808 commit_id, base_commit_id, 1, repo, check_cancelled, NULL);
2813 return got_error(GOT_ERR_ANCESTRY);
2816 * Require a straight line of history between the target commit
2817 * and the work tree's base commit.
2819 * Non-linear situations such as this require a rebase:
2821 * (commit) D F (base_commit)
2829 * 'got update' only handles linear cases:
2830 * Update forwards in time: A (base/yca) - B - C - D (commit)
2831 * Update backwards in time: D (base) - C - B - A (commit/yca)
2833 if (allow_forwards_in_time_only) {
2834 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2835 return got_error(GOT_ERR_ANCESTRY);
2836 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2837 got_object_id_cmp(base_commit_id, yca_id) != 0)
2838 return got_error(GOT_ERR_ANCESTRY);
2844 static const struct got_error *
2845 check_same_branch(struct got_object_id *commit_id,
2846 struct got_reference *head_ref, struct got_object_id *yca_id,
2847 struct got_repository *repo)
2849 const struct got_error *err = NULL;
2850 struct got_commit_graph *graph = NULL;
2851 struct got_object_id *head_commit_id = NULL;
2852 int is_same_branch = 0;
2854 err = got_ref_resolve(&head_commit_id, repo, head_ref);
2858 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
2862 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
2867 err = got_commit_graph_open(&graph, "/", 1);
2871 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2872 check_cancelled, NULL);
2877 struct got_object_id id;
2879 err = got_commit_graph_iter_next(&id, graph, repo,
2880 check_cancelled, NULL);
2882 if (err->code == GOT_ERR_ITER_COMPLETED)
2887 if (yca_id && got_object_id_cmp(&id, yca_id) == 0)
2889 if (got_object_id_cmp(&id, commit_id) == 0) {
2896 got_commit_graph_close(graph);
2897 free(head_commit_id);
2898 if (!err && !is_same_branch)
2899 err = got_error(GOT_ERR_ANCESTRY);
2903 static const struct got_error *
2904 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2906 static char msg[512];
2907 const char *branch_name;
2909 if (got_ref_is_symbolic(ref))
2910 branch_name = got_ref_get_symref_target(ref);
2912 branch_name = got_ref_get_name(ref);
2914 if (strncmp("refs/heads/", branch_name, 11) == 0)
2917 snprintf(msg, sizeof(msg),
2918 "target commit is not contained in branch '%s'; "
2919 "the branch to use must be specified with -b; "
2920 "if necessary a new branch can be created for "
2921 "this commit with 'got branch -c %s BRANCH_NAME'",
2922 branch_name, commit_id_str);
2924 return got_error_msg(GOT_ERR_ANCESTRY, msg);
2927 static const struct got_error *
2928 cmd_checkout(int argc, char *argv[])
2930 const struct got_error *error = NULL;
2931 struct got_repository *repo = NULL;
2932 struct got_reference *head_ref = NULL, *ref = NULL;
2933 struct got_worktree *worktree = NULL;
2934 char *repo_path = NULL;
2935 char *worktree_path = NULL;
2936 const char *path_prefix = "";
2937 const char *branch_name = GOT_REF_HEAD, *refname = NULL;
2938 char *commit_id_str = NULL;
2939 struct got_object_id *commit_id = NULL;
2941 int ch, same_path_prefix, allow_nonempty = 0, verbosity = 0;
2942 struct got_pathlist_head paths;
2943 struct got_checkout_progress_arg cpa;
2944 int *pack_fds = NULL;
2948 while ((ch = getopt(argc, argv, "b:c:Ep:q")) != -1) {
2951 branch_name = optarg;
2954 commit_id_str = strdup(optarg);
2955 if (commit_id_str == NULL)
2956 return got_error_from_errno("strdup");
2962 path_prefix = optarg;
2977 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2978 "unveil", NULL) == -1)
2982 char *base, *dotgit;
2984 repo_path = realpath(argv[0], NULL);
2985 if (repo_path == NULL)
2986 return got_error_from_errno2("realpath", argv[0]);
2987 cwd = getcwd(NULL, 0);
2989 error = got_error_from_errno("getcwd");
2996 error = got_path_basename(&base, path);
2999 dotgit = strstr(base, ".git");
3002 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
3003 error = got_error_from_errno("asprintf");
3008 } else if (argc == 2) {
3009 repo_path = realpath(argv[0], NULL);
3010 if (repo_path == NULL) {
3011 error = got_error_from_errno2("realpath", argv[0]);
3014 worktree_path = realpath(argv[1], NULL);
3015 if (worktree_path == NULL) {
3016 if (errno != ENOENT) {
3017 error = got_error_from_errno2("realpath",
3021 worktree_path = strdup(argv[1]);
3022 if (worktree_path == NULL) {
3023 error = got_error_from_errno("strdup");
3030 got_path_strip_trailing_slashes(repo_path);
3031 got_path_strip_trailing_slashes(worktree_path);
3033 error = got_repo_pack_fds_open(&pack_fds);
3037 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3041 /* Pre-create work tree path for unveil(2) */
3042 error = got_path_mkdir(worktree_path);
3044 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
3045 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
3047 if (!allow_nonempty &&
3048 !got_path_dir_is_empty(worktree_path)) {
3049 error = got_error_path(worktree_path,
3050 GOT_ERR_DIR_NOT_EMPTY);
3055 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
3059 error = got_ref_open(&head_ref, repo, branch_name, 0);
3063 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
3064 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
3067 error = got_worktree_open(&worktree, worktree_path);
3071 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
3075 if (!same_path_prefix) {
3076 error = got_error(GOT_ERR_PATH_PREFIX);
3080 if (commit_id_str) {
3081 struct got_reflist_head refs;
3083 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3087 error = got_repo_match_object_id(&commit_id, NULL,
3088 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3089 got_ref_list_free(&refs);
3092 error = check_linear_ancestry(commit_id,
3093 got_worktree_get_base_commit_id(worktree), 0, repo);
3094 if (error != NULL) {
3095 if (error->code == GOT_ERR_ANCESTRY) {
3096 error = checkout_ancestry_error(
3097 head_ref, commit_id_str);
3101 error = check_same_branch(commit_id, head_ref, NULL, repo);
3103 if (error->code == GOT_ERR_ANCESTRY) {
3104 error = checkout_ancestry_error(
3105 head_ref, commit_id_str);
3109 error = got_worktree_set_base_commit_id(worktree, repo,
3113 /* Expand potentially abbreviated commit ID string. */
3114 free(commit_id_str);
3115 error = got_object_id_str(&commit_id_str, commit_id);
3119 commit_id = got_object_id_dup(
3120 got_worktree_get_base_commit_id(worktree));
3121 if (commit_id == NULL) {
3122 error = got_error_from_errno("got_object_id_dup");
3125 error = got_object_id_str(&commit_id_str, commit_id);
3130 error = got_pathlist_append(&paths, "", NULL);
3133 cpa.worktree_path = worktree_path;
3134 cpa.had_base_commit_ref_error = 0;
3135 cpa.verbosity = verbosity;
3136 error = got_worktree_checkout_files(worktree, &paths, repo,
3137 checkout_progress, &cpa, check_cancelled, NULL);
3141 if (got_ref_is_symbolic(head_ref)) {
3142 error = got_ref_resolve_symbolic(&ref, repo, head_ref);
3145 refname = got_ref_get_name(ref);
3147 refname = got_ref_get_name(head_ref);
3148 printf("Checked out %s: %s\n", refname, commit_id_str);
3149 printf("Now shut up and hack\n");
3150 if (cpa.had_base_commit_ref_error)
3151 show_worktree_base_ref_warning();
3154 const struct got_error *pack_err =
3155 got_repo_pack_fds_close(pack_fds);
3160 got_ref_close(head_ref);
3163 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
3164 free(commit_id_str);
3167 free(worktree_path);
3172 struct got_update_progress_arg {
3184 print_update_progress_stats(struct got_update_progress_arg *upa)
3186 if (!upa->did_something)
3189 if (upa->conflicts > 0)
3190 printf("Files with new merge conflicts: %d\n", upa->conflicts);
3191 if (upa->obstructed > 0)
3192 printf("File paths obstructed by a non-regular file: %d\n",
3194 if (upa->not_updated > 0)
3195 printf("Files not updated because of existing merge "
3196 "conflicts: %d\n", upa->not_updated);
3200 * The meaning of some status codes differs between merge-style operations and
3201 * update operations. For example, the ! status code means "file was missing"
3202 * if changes were merged into the work tree, and "missing file was restored"
3203 * if the work tree was updated. This function should be used by any operation
3204 * which merges changes into the work tree without updating the work tree.
3207 print_merge_progress_stats(struct got_update_progress_arg *upa)
3209 if (!upa->did_something)
3212 if (upa->conflicts > 0)
3213 printf("Files with new merge conflicts: %d\n", upa->conflicts);
3214 if (upa->obstructed > 0)
3215 printf("File paths obstructed by a non-regular file: %d\n",
3217 if (upa->missing > 0)
3218 printf("Files which had incoming changes but could not be "
3219 "found in the work tree: %d\n", upa->missing);
3220 if (upa->not_deleted > 0)
3221 printf("Files not deleted due to differences in deleted "
3222 "content: %d\n", upa->not_deleted);
3223 if (upa->unversioned > 0)
3224 printf("Files not merged because an unversioned file was "
3225 "found in the work tree: %d\n", upa->unversioned);
3231 fprintf(stderr, "usage: %s update [-q] [-b branch] [-c commit] "
3232 "[path ...]\n", getprogname());
3236 static const struct got_error *
3237 update_progress(void *arg, unsigned char status, const char *path)
3239 struct got_update_progress_arg *upa = arg;
3241 if (status == GOT_STATUS_EXISTS ||
3242 status == GOT_STATUS_BASE_REF_ERR)
3245 upa->did_something = 1;
3247 /* Base commit bump happens silently. */
3248 if (status == GOT_STATUS_BUMP_BASE)
3251 if (status == GOT_STATUS_CONFLICT)
3253 if (status == GOT_STATUS_OBSTRUCTED)
3255 if (status == GOT_STATUS_CANNOT_UPDATE)
3257 if (status == GOT_STATUS_MISSING)
3259 if (status == GOT_STATUS_CANNOT_DELETE)
3261 if (status == GOT_STATUS_UNVERSIONED)
3264 while (path[0] == '/')
3266 if (upa->verbosity >= 0)
3267 printf("%c %s\n", status, path);
3272 static const struct got_error *
3273 switch_head_ref(struct got_reference *head_ref,
3274 struct got_object_id *commit_id, struct got_worktree *worktree,
3275 struct got_repository *repo)
3277 const struct got_error *err = NULL;
3279 int ref_has_moved = 0;
3281 /* Trivial case: switching between two different references. */
3282 if (strcmp(got_ref_get_name(head_ref),
3283 got_worktree_get_head_ref_name(worktree)) != 0) {
3284 printf("Switching work tree from %s to %s\n",
3285 got_worktree_get_head_ref_name(worktree),
3286 got_ref_get_name(head_ref));
3287 return got_worktree_set_head_ref(worktree, head_ref);
3290 err = check_linear_ancestry(commit_id,
3291 got_worktree_get_base_commit_id(worktree), 0, repo);
3293 if (err->code != GOT_ERR_ANCESTRY)
3300 /* Switching to a rebased branch with the same reference name. */
3301 err = got_object_id_str(&base_id_str,
3302 got_worktree_get_base_commit_id(worktree));
3305 printf("Reference %s now points at a different branch\n",
3306 got_worktree_get_head_ref_name(worktree));
3307 printf("Switching work tree from %s to %s\n", base_id_str,
3308 got_worktree_get_head_ref_name(worktree));
3312 static const struct got_error *
3313 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
3315 const struct got_error *err;
3318 err = got_worktree_rebase_in_progress(&in_progress, worktree);
3322 return got_error(GOT_ERR_REBASING);
3324 err = got_worktree_histedit_in_progress(&in_progress, worktree);
3328 return got_error(GOT_ERR_HISTEDIT_BUSY);
3333 static const struct got_error *
3334 check_merge_in_progress(struct got_worktree *worktree,
3335 struct got_repository *repo)
3337 const struct got_error *err;
3340 err = got_worktree_merge_in_progress(&in_progress, worktree, repo);
3344 return got_error(GOT_ERR_MERGE_BUSY);
3349 static const struct got_error *
3350 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
3351 char *argv[], struct got_worktree *worktree)
3353 const struct got_error *err = NULL;
3355 struct got_pathlist_entry *new;
3361 return got_error_from_errno("strdup");
3362 return got_pathlist_append(paths, path, NULL);
3365 for (i = 0; i < argc; i++) {
3366 err = got_worktree_resolve_path(&path, worktree, argv[i]);
3369 err = got_pathlist_insert(&new, paths, path, NULL);
3370 if (err || new == NULL /* duplicate */) {
3380 static const struct got_error *
3381 wrap_not_worktree_error(const struct got_error *orig_err,
3382 const char *cmdname, const char *path)
3384 const struct got_error *err;
3385 struct got_repository *repo;
3386 static char msg[512];
3387 int *pack_fds = NULL;
3389 err = got_repo_pack_fds_open(&pack_fds);
3393 err = got_repo_open(&repo, path, NULL, pack_fds);
3397 snprintf(msg, sizeof(msg),
3398 "'got %s' needs a work tree in addition to a git repository\n"
3399 "Work trees can be checked out from this Git repository with "
3401 "The got(1) manual page contains more information.", cmdname);
3402 err = got_error_msg(GOT_ERR_NOT_WORKTREE, msg);
3403 got_repo_close(repo);
3405 const struct got_error *pack_err =
3406 got_repo_pack_fds_close(pack_fds);
3413 static const struct got_error *
3414 cmd_update(int argc, char *argv[])
3416 const struct got_error *error = NULL;
3417 struct got_repository *repo = NULL;
3418 struct got_worktree *worktree = NULL;
3419 char *worktree_path = NULL;
3420 struct got_object_id *commit_id = NULL;
3421 char *commit_id_str = NULL;
3422 const char *branch_name = NULL;
3423 struct got_reference *head_ref = NULL;
3424 struct got_pathlist_head paths;
3425 struct got_pathlist_entry *pe;
3426 int ch, verbosity = 0;
3427 struct got_update_progress_arg upa;
3428 int *pack_fds = NULL;
3432 while ((ch = getopt(argc, argv, "b:c:q")) != -1) {
3435 branch_name = optarg;
3438 commit_id_str = strdup(optarg);
3439 if (commit_id_str == NULL)
3440 return got_error_from_errno("strdup");
3455 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3456 "unveil", NULL) == -1)
3459 worktree_path = getcwd(NULL, 0);
3460 if (worktree_path == NULL) {
3461 error = got_error_from_errno("getcwd");
3465 error = got_repo_pack_fds_open(&pack_fds);
3469 error = got_worktree_open(&worktree, worktree_path);
3471 if (error->code == GOT_ERR_NOT_WORKTREE)
3472 error = wrap_not_worktree_error(error, "update",
3477 error = check_rebase_or_histedit_in_progress(worktree);
3481 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3486 error = apply_unveil(got_repo_get_path(repo), 0,
3487 got_worktree_get_root_path(worktree));
3491 error = check_merge_in_progress(worktree, repo);
3495 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3499 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
3500 got_worktree_get_head_ref_name(worktree), 0);
3503 if (commit_id_str == NULL) {
3504 error = got_ref_resolve(&commit_id, repo, head_ref);
3507 error = got_object_id_str(&commit_id_str, commit_id);
3511 struct got_reflist_head refs;
3513 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3517 error = got_repo_match_object_id(&commit_id, NULL,
3518 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3519 got_ref_list_free(&refs);
3520 free(commit_id_str);
3521 commit_id_str = NULL;
3524 error = got_object_id_str(&commit_id_str, commit_id);
3530 struct got_object_id *head_commit_id;
3531 TAILQ_FOREACH(pe, &paths, entry) {
3532 if (pe->path_len == 0)
3534 error = got_error_msg(GOT_ERR_BAD_PATH,
3535 "switching between branches requires that "
3536 "the entire work tree gets updated");
3539 error = got_ref_resolve(&head_commit_id, repo, head_ref);
3542 error = check_linear_ancestry(commit_id, head_commit_id, 0,
3544 free(head_commit_id);
3547 error = check_same_branch(commit_id, head_ref, NULL, repo);
3550 error = switch_head_ref(head_ref, commit_id, worktree, repo);
3554 error = check_linear_ancestry(commit_id,
3555 got_worktree_get_base_commit_id(worktree), 0, repo);
3556 if (error != NULL) {
3557 if (error->code == GOT_ERR_ANCESTRY)
3558 error = got_error(GOT_ERR_BRANCH_MOVED);
3561 error = check_same_branch(commit_id, head_ref, NULL, repo);
3566 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
3568 error = got_worktree_set_base_commit_id(worktree, repo,
3574 memset(&upa, 0, sizeof(upa));
3575 upa.verbosity = verbosity;
3576 error = got_worktree_checkout_files(worktree, &paths, repo,
3577 update_progress, &upa, check_cancelled, NULL);
3581 if (upa.did_something) {
3582 printf("Updated to %s: %s\n",
3583 got_worktree_get_head_ref_name(worktree), commit_id_str);
3585 printf("Already up-to-date\n");
3587 print_update_progress_stats(&upa);
3590 const struct got_error *pack_err =
3591 got_repo_pack_fds_close(pack_fds);
3595 free(worktree_path);
3596 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
3598 free(commit_id_str);
3602 static const struct got_error *
3603 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
3604 const char *path, int diff_context, int ignore_whitespace,
3605 int force_text_diff, struct got_diffstat_cb_arg *dsa,
3606 struct got_repository *repo, FILE *outfile)
3608 const struct got_error *err = NULL;
3609 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
3610 FILE *f1 = NULL, *f2 = NULL;
3611 int fd1 = -1, fd2 = -1;
3613 fd1 = got_opentempfd();
3615 return got_error_from_errno("got_opentempfd");
3616 fd2 = got_opentempfd();
3618 err = got_error_from_errno("got_opentempfd");
3623 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192,
3629 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192, fd2);
3633 f1 = got_opentemp();
3635 err = got_error_from_errno("got_opentemp");
3638 f2 = got_opentemp();
3640 err = got_error_from_errno("got_opentemp");
3644 while (path[0] == '/')
3646 err = got_diff_blob(NULL, NULL, blob1, blob2, f1, f2, path, path,
3647 GOT_DIFF_ALGORITHM_PATIENCE, diff_context, ignore_whitespace,
3648 force_text_diff, dsa, outfile);
3650 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3651 err = got_error_from_errno("close");
3653 got_object_blob_close(blob1);
3654 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3655 err = got_error_from_errno("close");
3656 got_object_blob_close(blob2);
3657 if (f1 && fclose(f1) == EOF && err == NULL)
3658 err = got_error_from_errno("fclose");
3659 if (f2 && fclose(f2) == EOF && err == NULL)
3660 err = got_error_from_errno("fclose");
3664 static const struct got_error *
3665 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
3666 const char *path, int diff_context, int ignore_whitespace,
3667 int force_text_diff, struct got_diffstat_cb_arg *dsa,
3668 struct got_repository *repo, FILE *outfile)
3670 const struct got_error *err = NULL;
3671 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3672 struct got_diff_blob_output_unidiff_arg arg;
3673 FILE *f1 = NULL, *f2 = NULL;
3674 int fd1 = -1, fd2 = -1;
3677 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3680 fd1 = got_opentempfd();
3682 err = got_error_from_errno("got_opentempfd");
3687 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3691 f1 = got_opentemp();
3693 err = got_error_from_errno("got_opentemp");
3697 f2 = got_opentemp();
3699 err = got_error_from_errno("got_opentemp");
3702 fd2 = got_opentempfd();
3704 err = got_error_from_errno("got_opentempfd");
3707 arg.diff_context = diff_context;
3708 arg.ignore_whitespace = ignore_whitespace;
3709 arg.force_text_diff = force_text_diff;
3711 arg.diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
3712 arg.outfile = outfile;
3715 while (path[0] == '/')
3717 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, path, path, repo,
3718 got_diff_blob_output_unidiff, &arg, 1);
3721 got_object_tree_close(tree1);
3723 got_object_tree_close(tree2);
3724 if (f1 && fclose(f1) == EOF && err == NULL)
3725 err = got_error_from_errno("fclose");
3726 if (f2 && fclose(f2) == EOF && err == NULL)
3727 err = got_error_from_errno("fclose");
3728 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3729 err = got_error_from_errno("close");
3730 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3731 err = got_error_from_errno("close");
3735 static const struct got_error *
3736 get_changed_paths(struct got_pathlist_head *paths,
3737 struct got_commit_object *commit, struct got_repository *repo,
3738 struct got_diffstat_cb_arg *dsa)
3740 const struct got_error *err = NULL;
3741 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3742 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3743 struct got_object_qid *qid;
3744 got_diff_blob_cb cb = got_diff_tree_collect_changed_paths;
3745 FILE *f1 = NULL, *f2 = NULL;
3746 int fd1 = -1, fd2 = -1;
3749 cb = got_diff_tree_compute_diffstat;
3751 f1 = got_opentemp();
3753 err = got_error_from_errno("got_opentemp");
3756 f2 = got_opentemp();
3758 err = got_error_from_errno("got_opentemp");
3761 fd1 = got_opentempfd();
3763 err = got_error_from_errno("got_opentempfd");
3766 fd2 = got_opentempfd();
3768 err = got_error_from_errno("got_opentempfd");
3773 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3775 struct got_commit_object *pcommit;
3776 err = got_object_open_as_commit(&pcommit, repo,
3781 tree_id1 = got_object_id_dup(
3782 got_object_commit_get_tree_id(pcommit));
3783 if (tree_id1 == NULL) {
3784 got_object_commit_close(pcommit);
3785 return got_error_from_errno("got_object_id_dup");
3787 got_object_commit_close(pcommit);
3792 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3797 tree_id2 = got_object_commit_get_tree_id(commit);
3798 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3802 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3803 cb, dsa ? (void *)dsa : paths, dsa ? 1 : 0);
3806 got_object_tree_close(tree1);
3808 got_object_tree_close(tree2);
3809 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3810 err = got_error_from_errno("close");
3811 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3812 err = got_error_from_errno("close");
3813 if (f1 && fclose(f1) == EOF && err == NULL)
3814 err = got_error_from_errno("fclose");
3815 if (f2 && fclose(f2) == EOF && err == NULL)
3816 err = got_error_from_errno("fclose");
3821 static const struct got_error *
3822 print_patch(struct got_commit_object *commit, struct got_object_id *id,
3823 const char *path, int diff_context, struct got_diffstat_cb_arg *dsa,
3824 struct got_repository *repo, FILE *outfile)
3826 const struct got_error *err = NULL;
3827 struct got_commit_object *pcommit = NULL;
3828 char *id_str1 = NULL, *id_str2 = NULL;
3829 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
3830 struct got_object_qid *qid;
3832 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3834 err = got_object_open_as_commit(&pcommit, repo,
3838 err = got_object_id_str(&id_str1, &qid->id);
3843 err = got_object_id_str(&id_str2, id);
3847 if (path && path[0] != '\0') {
3849 err = got_object_id_by_path(&obj_id2, repo, commit, path);
3853 err = got_object_id_by_path(&obj_id1, repo,
3856 if (err->code != GOT_ERR_NO_TREE_ENTRY) {
3862 err = got_object_get_type(&obj_type, repo, obj_id2);
3868 "diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3869 fprintf(outfile, "commit - %s\n",
3870 id_str1 ? id_str1 : "/dev/null");
3871 fprintf(outfile, "commit + %s\n", id_str2);
3873 case GOT_OBJ_TYPE_BLOB:
3874 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
3875 0, 0, dsa, repo, outfile);
3877 case GOT_OBJ_TYPE_TREE:
3878 err = diff_trees(obj_id1, obj_id2, path, diff_context,
3879 0, 0, dsa, repo, outfile);
3882 err = got_error(GOT_ERR_OBJ_TYPE);
3888 obj_id2 = got_object_commit_get_tree_id(commit);
3890 obj_id1 = got_object_commit_get_tree_id(pcommit);
3892 "diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3893 fprintf(outfile, "commit - %s\n",
3894 id_str1 ? id_str1 : "/dev/null");
3895 fprintf(outfile, "commit + %s\n", id_str2);
3896 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, 0,
3897 dsa, repo, outfile);
3903 got_object_commit_close(pcommit);
3908 get_datestr(time_t *time, char *datebuf)
3910 struct tm mytm, *tm;
3913 tm = gmtime_r(time, &mytm);
3916 s = asctime_r(tm, datebuf);
3919 p = strchr(s, '\n');
3925 static const struct got_error *
3926 match_commit(int *have_match, struct got_object_id *id,
3927 struct got_commit_object *commit, regex_t *regex)
3929 const struct got_error *err = NULL;
3930 regmatch_t regmatch;
3931 char *id_str = NULL, *logmsg = NULL;
3935 err = got_object_id_str(&id_str, id);
3939 err = got_object_commit_get_logmsg(&logmsg, commit);
3943 if (regexec(regex, got_object_commit_get_author(commit), 1,
3944 ®match, 0) == 0 ||
3945 regexec(regex, got_object_commit_get_committer(commit), 1,
3946 ®match, 0) == 0 ||
3947 regexec(regex, id_str, 1, ®match, 0) == 0 ||
3948 regexec(regex, logmsg, 1, ®match, 0) == 0)
3957 match_changed_paths(int *have_match, struct got_pathlist_head *changed_paths,
3960 regmatch_t regmatch;
3961 struct got_pathlist_entry *pe;
3965 TAILQ_FOREACH(pe, changed_paths, entry) {
3966 if (regexec(regex, pe->path, 1, ®match, 0) == 0) {
3973 static const struct got_error *
3974 match_patch(int *have_match, struct got_commit_object *commit,
3975 struct got_object_id *id, const char *path, int diff_context,
3976 struct got_repository *repo, regex_t *regex, FILE *f)
3978 const struct got_error *err = NULL;
3980 size_t linesize = 0;
3981 regmatch_t regmatch;
3985 err = got_opentemp_truncate(f);
3989 err = print_patch(commit, id, path, diff_context, NULL, repo, f);
3993 if (fseeko(f, 0L, SEEK_SET) == -1) {
3994 err = got_error_from_errno("fseeko");
3998 while (getline(&line, &linesize, f) != -1) {
3999 if (regexec(regex, line, 1, ®match, 0) == 0) {
4009 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
4011 static const struct got_error*
4012 build_refs_str(char **refs_str, struct got_reflist_head *refs,
4013 struct got_object_id *id, struct got_repository *repo,
4016 static const struct got_error *err = NULL;
4017 struct got_reflist_entry *re;
4023 TAILQ_FOREACH(re, refs, entry) {
4024 struct got_tag_object *tag = NULL;
4025 struct got_object_id *ref_id;
4028 name = got_ref_get_name(re->ref);
4029 if (strcmp(name, GOT_REF_HEAD) == 0)
4031 if (strncmp(name, "refs/", 5) == 0)
4033 if (strncmp(name, "got/", 4) == 0)
4035 if (strncmp(name, "heads/", 6) == 0)
4037 if (strncmp(name, "remotes/", 8) == 0) {
4041 s = strstr(name, "/" GOT_REF_HEAD);
4042 if (s != NULL && s[strlen(s)] == '\0')
4045 err = got_ref_resolve(&ref_id, repo, re->ref);
4048 if (strncmp(name, "tags/", 5) == 0) {
4049 err = got_object_open_as_tag(&tag, repo, ref_id);
4051 if (err->code != GOT_ERR_OBJ_TYPE) {
4055 /* Ref points at something other than a tag. */
4060 cmp = got_object_id_cmp(tag ?
4061 got_object_tag_get_object_id(tag) : ref_id, id);
4064 got_object_tag_close(tag);
4068 if (asprintf(refs_str, "%s%s%s", s ? s : "",
4069 s ? ", " : "", name) == -1) {
4070 err = got_error_from_errno("asprintf");
4081 static const struct got_error *
4082 print_commit_oneline(struct got_commit_object *commit, struct got_object_id *id,
4083 struct got_repository *repo, struct got_reflist_object_id_map *refs_idmap)
4085 const struct got_error *err = NULL;
4086 char *ref_str = NULL, *id_str = NULL, *logmsg0 = NULL;
4087 char *comma, *s, *nl;
4088 struct got_reflist_head *refs;
4089 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
4091 time_t committer_time;
4093 refs = got_reflist_object_id_map_lookup(refs_idmap, id);
4095 err = build_refs_str(&ref_str, refs, id, repo, 1);
4099 /* Display the first matching ref only. */
4100 if (ref_str && (comma = strchr(ref_str, ',')) != NULL)
4104 if (ref_str == NULL) {
4105 err = got_object_id_str(&id_str, id);
4110 committer_time = got_object_commit_get_committer_time(commit);
4111 if (gmtime_r(&committer_time, &tm) == NULL) {
4112 err = got_error_from_errno("gmtime_r");
4115 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0) {
4116 err = got_error(GOT_ERR_NO_SPACE);
4120 err = got_object_commit_get_logmsg(&logmsg0, commit);
4125 while (isspace((unsigned char)s[0]))
4128 nl = strchr(s, '\n');
4134 printf("%s%-7s %s\n", datebuf, ref_str, s);
4136 printf("%s%.7s %s\n", datebuf, id_str, s);
4138 if (fflush(stdout) != 0 && err == NULL)
4139 err = got_error_from_errno("fflush");
4147 static const struct got_error *
4148 print_diffstat(struct got_diffstat_cb_arg *dsa, const char *header)
4150 struct got_pathlist_entry *pe;
4153 printf("%s\n", header);
4155 TAILQ_FOREACH(pe, dsa->paths, entry) {
4156 struct got_diff_changed_path *cp = pe->data;
4157 int pad = dsa->max_path_len - pe->path_len + 1;
4159 printf(" %c %s%*c | %*d+ %*d-\n", cp->status, pe->path, pad,
4160 ' ', dsa->add_cols + 1, cp->add, dsa->rm_cols + 1, cp->rm);
4162 printf("\n%d file%s changed, %d insertion%s(+), %d deletion%s(-)\n\n",
4163 dsa->nfiles, dsa->nfiles > 1 ? "s" : "", dsa->ins,
4164 dsa->ins != 1 ? "s" : "", dsa->del, dsa->del != 1 ? "s" : "");
4166 if (fflush(stdout) != 0)
4167 return got_error_from_errno("fflush");
4172 static const struct got_error *
4178 if (fseeko(f, 0L, SEEK_SET) == -1)
4179 return got_error_from_errno("fseek");
4182 r = fread(buf, 1, sizeof(buf), f);
4185 return got_error_from_errno("fread");
4189 if (fwrite(buf, 1, r, stdout) != r)
4190 return got_ferror(stdout, GOT_ERR_IO);
4196 static const struct got_error *
4197 print_commit(struct got_commit_object *commit, struct got_object_id *id,
4198 struct got_repository *repo, const char *path,
4199 struct got_pathlist_head *changed_paths,
4200 struct got_diffstat_cb_arg *diffstat, int show_patch, int diff_context,
4201 struct got_reflist_object_id_map *refs_idmap, const char *custom_refs_str)
4203 const struct got_error *err = NULL;
4205 char *id_str, *datestr, *logmsg0, *logmsg, *line;
4207 time_t committer_time;
4208 const char *author, *committer;
4209 char *refs_str = NULL;
4211 err = got_object_id_str(&id_str, id);
4215 if (custom_refs_str == NULL) {
4216 struct got_reflist_head *refs;
4217 refs = got_reflist_object_id_map_lookup(refs_idmap, id);
4219 err = build_refs_str(&refs_str, refs, id, repo, 0);
4225 printf(GOT_COMMIT_SEP_STR);
4226 if (custom_refs_str)
4227 printf("commit %s (%s)\n", id_str, custom_refs_str);
4229 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4230 refs_str ? refs_str : "", refs_str ? ")" : "");
4235 printf("from: %s\n", got_object_commit_get_author(commit));
4236 author = got_object_commit_get_author(commit);
4237 committer = got_object_commit_get_committer(commit);
4238 if (strcmp(author, committer) != 0)
4239 printf("via: %s\n", committer);
4240 committer_time = got_object_commit_get_committer_time(commit);
4241 datestr = get_datestr(&committer_time, datebuf);
4243 printf("date: %s UTC\n", datestr);
4244 if (got_object_commit_get_nparents(commit) > 1) {
4245 const struct got_object_id_queue *parent_ids;
4246 struct got_object_qid *qid;
4248 parent_ids = got_object_commit_get_parent_ids(commit);
4249 STAILQ_FOREACH(qid, parent_ids, entry) {
4250 err = got_object_id_str(&id_str, &qid->id);
4253 printf("parent %d: %s\n", n++, id_str);
4259 err = got_object_commit_get_logmsg(&logmsg0, commit);
4265 line = strsep(&logmsg, "\n");
4267 printf(" %s\n", line);
4271 if (changed_paths && diffstat == NULL) {
4272 struct got_pathlist_entry *pe;
4274 TAILQ_FOREACH(pe, changed_paths, entry) {
4275 struct got_diff_changed_path *cp = pe->data;
4277 printf(" %c %s\n", cp->status, pe->path);
4285 err = got_error_from_errno("got_opentemp");
4290 err = print_patch(commit, id, path, diff_context, diffstat,
4291 repo, diffstat == NULL ? stdout : f);
4296 err = print_diffstat(diffstat, NULL);
4308 if (fflush(stdout) != 0 && err == NULL)
4309 err = got_error_from_errno("fflush");
4311 if (f && fclose(f) == EOF && err == NULL)
4312 err = got_error_from_errno("fclose");
4318 static const struct got_error *
4319 print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
4320 struct got_repository *repo, const char *path, int show_changed_paths,
4321 int show_diffstat, int show_patch, const char *search_pattern,
4322 int diff_context, int limit, int log_branches, int reverse_display_order,
4323 struct got_reflist_object_id_map *refs_idmap, int one_line,
4326 const struct got_error *err;
4327 struct got_commit_graph *graph;
4330 struct got_object_id_queue reversed_commits;
4331 struct got_object_qid *qid;
4332 struct got_commit_object *commit;
4333 struct got_pathlist_head changed_paths;
4335 STAILQ_INIT(&reversed_commits);
4336 TAILQ_INIT(&changed_paths);
4338 if (search_pattern && regcomp(®ex, search_pattern,
4339 REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
4340 return got_error_msg(GOT_ERR_REGEX, search_pattern);
4342 err = got_commit_graph_open(&graph, path, !log_branches);
4345 err = got_commit_graph_iter_start(graph, root_id, repo,
4346 check_cancelled, NULL);
4350 struct got_object_id id;
4351 struct got_diffstat_cb_arg dsa = { 0, 0, 0, 0, 0, 0,
4352 &changed_paths, 0, 0, GOT_DIFF_ALGORITHM_PATIENCE };
4354 if (sigint_received || sigpipe_received)
4357 err = got_commit_graph_iter_next(&id, graph, repo,
4358 check_cancelled, NULL);
4360 if (err->code == GOT_ERR_ITER_COMPLETED)
4365 err = got_object_open_as_commit(&commit, repo, &id);
4369 if ((show_changed_paths || (show_diffstat && !show_patch))
4370 && !reverse_display_order) {
4371 err = get_changed_paths(&changed_paths, commit, repo,
4372 show_diffstat ? &dsa : NULL);
4377 if (search_pattern) {
4378 err = match_commit(&have_match, &id, commit, ®ex);
4380 got_object_commit_close(commit);
4383 if (have_match == 0 && show_changed_paths)
4384 match_changed_paths(&have_match,
4385 &changed_paths, ®ex);
4386 if (have_match == 0 && show_patch) {
4387 err = match_patch(&have_match, commit, &id,
4388 path, diff_context, repo, ®ex, tmpfile);
4392 if (have_match == 0) {
4393 got_object_commit_close(commit);
4394 got_pathlist_free(&changed_paths,
4395 GOT_PATHLIST_FREE_ALL);
4400 if (reverse_display_order) {
4401 err = got_object_qid_alloc(&qid, &id);
4404 STAILQ_INSERT_HEAD(&reversed_commits, qid, entry);
4405 got_object_commit_close(commit);
4408 err = print_commit_oneline(commit, &id,
4411 err = print_commit(commit, &id, repo, path,
4412 (show_changed_paths || show_diffstat) ?
4413 &changed_paths : NULL,
4414 show_diffstat ? &dsa : NULL, show_patch,
4415 diff_context, refs_idmap, NULL);
4416 got_object_commit_close(commit);
4420 if ((limit && --limit == 0) ||
4421 (end_id && got_object_id_cmp(&id, end_id) == 0))
4424 got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
4426 if (reverse_display_order) {
4427 STAILQ_FOREACH(qid, &reversed_commits, entry) {
4428 struct got_diffstat_cb_arg dsa = { 0, 0, 0, 0, 0, 0,
4429 &changed_paths, 0, 0, GOT_DIFF_ALGORITHM_PATIENCE };
4431 err = got_object_open_as_commit(&commit, repo,
4435 if (show_changed_paths ||
4436 (show_diffstat && !show_patch)) {
4437 err = get_changed_paths(&changed_paths, commit,
4438 repo, show_diffstat ? &dsa : NULL);
4443 err = print_commit_oneline(commit, &qid->id,
4446 err = print_commit(commit, &qid->id, repo, path,
4447 (show_changed_paths || show_diffstat) ?
4448 &changed_paths : NULL,
4449 show_diffstat ? &dsa : NULL, show_patch,
4450 diff_context, refs_idmap, NULL);
4451 got_object_commit_close(commit);
4454 got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
4458 while (!STAILQ_EMPTY(&reversed_commits)) {
4459 qid = STAILQ_FIRST(&reversed_commits);
4460 STAILQ_REMOVE_HEAD(&reversed_commits, entry);
4461 got_object_qid_free(qid);
4463 got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
4466 got_commit_graph_close(graph);
4473 fprintf(stderr, "usage: %s log [-bdPpRs] [-C number] [-c commit] "
4474 "[-l N] [-r repository-path] [-S search-pattern] [-x commit] "
4475 "[path]\n", getprogname());
4480 get_default_log_limit(void)
4482 const char *got_default_log_limit;
4486 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
4487 if (got_default_log_limit == NULL)
4489 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
4495 static const struct got_error *
4496 cmd_log(int argc, char *argv[])
4498 const struct got_error *error;
4499 struct got_repository *repo = NULL;
4500 struct got_worktree *worktree = NULL;
4501 struct got_object_id *start_id = NULL, *end_id = NULL;
4502 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
4503 const char *start_commit = NULL, *end_commit = NULL;
4504 const char *search_pattern = NULL;
4505 int diff_context = -1, ch;
4506 int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
4507 int show_diffstat = 0, reverse_display_order = 0, one_line = 0;
4509 struct got_reflist_head refs;
4510 struct got_reflist_object_id_map *refs_idmap = NULL;
4511 FILE *tmpfile = NULL;
4512 int *pack_fds = NULL;
4517 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4523 limit = get_default_log_limit();
4525 while ((ch = getopt(argc, argv, "bC:c:dl:PpRr:S:sx:")) != -1) {
4531 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4534 errx(1, "number of context lines is %s: %s",
4538 start_commit = optarg;
4544 limit = strtonum(optarg, 0, INT_MAX, &errstr);
4546 errx(1, "number of commits is %s: %s",
4550 show_changed_paths = 1;
4556 reverse_display_order = 1;
4559 repo_path = realpath(optarg, NULL);
4560 if (repo_path == NULL)
4561 return got_error_from_errno2("realpath",
4563 got_path_strip_trailing_slashes(repo_path);
4566 search_pattern = optarg;
4572 end_commit = optarg;
4583 if (diff_context == -1)
4585 else if (!show_patch)
4586 errx(1, "-C requires -p");
4588 if (one_line && (show_patch || show_changed_paths || show_diffstat))
4589 errx(1, "cannot use -s with -d, -p or -P");
4591 cwd = getcwd(NULL, 0);
4593 error = got_error_from_errno("getcwd");
4597 error = got_repo_pack_fds_open(&pack_fds);
4601 if (repo_path == NULL) {
4602 error = got_worktree_open(&worktree, cwd);
4603 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4610 error = got_worktree_resolve_path(&path, worktree,
4615 path = strdup(argv[0]);
4617 error = got_error_from_errno("strdup");
4621 } else if (argc != 0)
4624 if (repo_path == NULL) {
4625 repo_path = worktree ?
4626 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
4628 if (repo_path == NULL) {
4629 error = got_error_from_errno("strdup");
4633 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4637 error = apply_unveil(got_repo_get_path(repo), 1,
4638 worktree ? got_worktree_get_root_path(worktree) : NULL);
4642 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4646 error = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
4650 if (start_commit == NULL) {
4651 struct got_reference *head_ref;
4652 struct got_commit_object *commit = NULL;
4653 error = got_ref_open(&head_ref, repo,
4654 worktree ? got_worktree_get_head_ref_name(worktree)
4658 error = got_ref_resolve(&start_id, repo, head_ref);
4659 got_ref_close(head_ref);
4662 error = got_object_open_as_commit(&commit, repo,
4666 got_object_commit_close(commit);
4668 error = got_repo_match_object_id(&start_id, NULL,
4669 start_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4673 if (end_commit != NULL) {
4674 error = got_repo_match_object_id(&end_id, NULL,
4675 end_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4682 * If a path was specified on the command line it was resolved
4683 * to a path in the work tree above. Prepend the work tree's
4684 * path prefix to obtain the corresponding in-repository path.
4688 prefix = got_worktree_get_path_prefix(worktree);
4689 if (asprintf(&in_repo_path, "%s%s%s", prefix,
4690 (path[0] != '\0') ? "/" : "", path) == -1) {
4691 error = got_error_from_errno("asprintf");
4696 error = got_repo_map_path(&in_repo_path, repo,
4702 path = in_repo_path;
4706 /* Release work tree lock. */
4707 got_worktree_close(worktree);
4711 if (search_pattern && show_patch) {
4712 tmpfile = got_opentemp();
4713 if (tmpfile == NULL) {
4714 error = got_error_from_errno("got_opentemp");
4719 error = print_commits(start_id, end_id, repo, path ? path : "",
4720 show_changed_paths, show_diffstat, show_patch, search_pattern,
4721 diff_context, limit, log_branches, reverse_display_order,
4722 refs_idmap, one_line, tmpfile);
4728 got_worktree_close(worktree);
4730 const struct got_error *close_err = got_repo_close(repo);
4735 const struct got_error *pack_err =
4736 got_repo_pack_fds_close(pack_fds);
4741 got_reflist_object_id_map_free(refs_idmap);
4742 if (tmpfile && fclose(tmpfile) == EOF && error == NULL)
4743 error = got_error_from_errno("fclose");
4744 got_ref_list_free(&refs);
4751 fprintf(stderr, "usage: %s diff [-adPsw] [-C number] [-c commit] "
4752 "[-r repository-path] [object1 object2 | path ...]\n",
4757 struct print_diff_arg {
4758 struct got_repository *repo;
4759 struct got_worktree *worktree;
4760 struct got_diffstat_cb_arg *diffstat;
4765 enum got_diff_algorithm diff_algo;
4766 int ignore_whitespace;
4767 int force_text_diff;
4774 * Create a file which contains the target path of a symlink so we can feed
4775 * it as content to the diff engine.
4777 static const struct got_error *
4778 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
4779 const char *abspath)
4781 const struct got_error *err = NULL;
4782 char target_path[PATH_MAX];
4783 ssize_t target_len, outlen;
4788 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
4789 if (target_len == -1)
4790 return got_error_from_errno2("readlinkat", abspath);
4792 target_len = readlink(abspath, target_path, PATH_MAX);
4793 if (target_len == -1)
4794 return got_error_from_errno2("readlink", abspath);
4797 *fd = got_opentempfd();
4799 return got_error_from_errno("got_opentempfd");
4801 outlen = write(*fd, target_path, target_len);
4803 err = got_error_from_errno("got_opentempfd");
4807 if (lseek(*fd, 0, SEEK_SET) == -1) {
4808 err = got_error_from_errno2("lseek", abspath);
4819 static const struct got_error *
4820 print_diff(void *arg, unsigned char status, unsigned char staged_status,
4821 const char *path, struct got_object_id *blob_id,
4822 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4823 int dirfd, const char *de_name)
4825 struct print_diff_arg *a = arg;
4826 const struct got_error *err = NULL;
4827 struct got_blob_object *blob1 = NULL;
4828 int fd = -1, fd1 = -1, fd2 = -1;
4830 char *abspath = NULL, *label1 = NULL;
4835 memset(&sb, 0, sizeof(sb));
4837 if (a->diff_staged) {
4838 if (staged_status != GOT_STATUS_MODIFY &&
4839 staged_status != GOT_STATUS_ADD &&
4840 staged_status != GOT_STATUS_DELETE)
4843 if (staged_status == GOT_STATUS_DELETE)
4845 if (status == GOT_STATUS_NONEXISTENT)
4846 return got_error_set_errno(ENOENT, path);
4847 if (status != GOT_STATUS_MODIFY &&
4848 status != GOT_STATUS_ADD &&
4849 status != GOT_STATUS_DELETE &&
4850 status != GOT_STATUS_CONFLICT)
4854 err = got_opentemp_truncate(a->f1);
4856 return got_error_from_errno("got_opentemp_truncate");
4857 err = got_opentemp_truncate(a->f2);
4859 return got_error_from_errno("got_opentemp_truncate");
4861 if (!a->header_shown) {
4862 if (fprintf(a->outfile, "diff %s%s\n",
4863 a->diff_staged ? "-s " : "",
4864 got_worktree_get_root_path(a->worktree)) < 0) {
4865 err = got_error_from_errno("fprintf");
4868 if (fprintf(a->outfile, "commit - %s\n", a->id_str) < 0) {
4869 err = got_error_from_errno("fprintf");
4872 if (fprintf(a->outfile, "path + %s%s\n",
4873 got_worktree_get_root_path(a->worktree),
4874 a->diff_staged ? " (staged changes)" : "") < 0) {
4875 err = got_error_from_errno("fprintf");
4878 a->header_shown = 1;
4881 if (a->diff_staged) {
4882 const char *label1 = NULL, *label2 = NULL;
4883 switch (staged_status) {
4884 case GOT_STATUS_MODIFY:
4888 case GOT_STATUS_ADD:
4891 case GOT_STATUS_DELETE:
4895 return got_error(GOT_ERR_FILE_STATUS);
4897 fd1 = got_opentempfd();
4899 err = got_error_from_errno("got_opentempfd");
4902 fd2 = got_opentempfd();
4904 err = got_error_from_errno("got_opentempfd");
4907 err = got_diff_objects_as_blobs(NULL, NULL, a->f1, a->f2,
4908 fd1, fd2, blob_id, staged_blob_id, label1, label2,
4909 a->diff_algo, a->diff_context, a->ignore_whitespace,
4910 a->force_text_diff, a->diffstat, a->repo, a->outfile);
4914 fd1 = got_opentempfd();
4916 err = got_error_from_errno("got_opentempfd");
4920 if (staged_status == GOT_STATUS_ADD ||
4921 staged_status == GOT_STATUS_MODIFY) {
4923 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
4927 err = got_object_id_str(&id_str, staged_blob_id);
4930 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
4931 err = got_error_from_errno("asprintf");
4936 } else if (status != GOT_STATUS_ADD) {
4937 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192,
4943 if (status != GOT_STATUS_DELETE) {
4944 if (asprintf(&abspath, "%s/%s",
4945 got_worktree_get_root_path(a->worktree), path) == -1) {
4946 err = got_error_from_errno("asprintf");
4951 fd = openat(dirfd, de_name,
4952 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4954 if (!got_err_open_nofollow_on_symlink()) {
4955 err = got_error_from_errno2("openat",
4959 err = get_symlink_target_file(&fd, dirfd,
4965 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4967 if (!got_err_open_nofollow_on_symlink()) {
4968 err = got_error_from_errno2("open",
4972 err = get_symlink_target_file(&fd, dirfd,
4978 if (fstatat(fd, abspath, &sb, AT_SYMLINK_NOFOLLOW) == -1) {
4979 err = got_error_from_errno2("fstatat", abspath);
4982 f2 = fdopen(fd, "r");
4984 err = got_error_from_errno2("fdopen", abspath);
4992 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
4998 err = got_diff_blob_file(blob1, a->f1, size1, label1, f2 ? f2 : a->f2,
4999 f2_exists, &sb, path, GOT_DIFF_ALGORITHM_PATIENCE, a->diff_context,
5000 a->ignore_whitespace, a->force_text_diff, a->diffstat, a->outfile);
5002 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5003 err = got_error_from_errno("close");
5004 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5005 err = got_error_from_errno("close");
5007 got_object_blob_close(blob1);
5008 if (fd != -1 && close(fd) == -1 && err == NULL)
5009 err = got_error_from_errno("close");
5010 if (f2 && fclose(f2) == EOF && err == NULL)
5011 err = got_error_from_errno("fclose");
5016 static const struct got_error *
5017 cmd_diff(int argc, char *argv[])
5019 const struct got_error *error;
5020 struct got_repository *repo = NULL;
5021 struct got_worktree *worktree = NULL;
5022 char *cwd = NULL, *repo_path = NULL;
5023 const char *commit_args[2] = { NULL, NULL };
5024 int ncommit_args = 0;
5025 struct got_object_id *ids[2] = { NULL, NULL };
5026 char *labels[2] = { NULL, NULL };
5027 int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
5028 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch, i;
5029 int force_text_diff = 0, force_path = 0, rflag = 0, show_diffstat = 0;
5031 struct got_reflist_head refs;
5032 struct got_pathlist_head diffstat_paths, paths;
5033 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
5034 int fd1 = -1, fd2 = -1;
5035 int *pack_fds = NULL;
5036 struct got_diffstat_cb_arg dsa;
5038 memset(&dsa, 0, sizeof(dsa));
5042 TAILQ_INIT(&diffstat_paths);
5045 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5050 while ((ch = getopt(argc, argv, "aC:c:dPr:sw")) != -1) {
5053 force_text_diff = 1;
5056 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
5059 errx(1, "number of context lines is %s: %s",
5063 if (ncommit_args >= 2)
5064 errx(1, "too many -c options used");
5065 commit_args[ncommit_args++] = optarg;
5074 repo_path = realpath(optarg, NULL);
5075 if (repo_path == NULL)
5076 return got_error_from_errno2("realpath",
5078 got_path_strip_trailing_slashes(repo_path);
5085 ignore_whitespace = 1;
5096 cwd = getcwd(NULL, 0);
5098 error = got_error_from_errno("getcwd");
5102 error = got_repo_pack_fds_open(&pack_fds);
5106 if (repo_path == NULL) {
5107 error = got_worktree_open(&worktree, cwd);
5108 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5114 strdup(got_worktree_get_repo_path(worktree));
5115 if (repo_path == NULL) {
5116 error = got_error_from_errno("strdup");
5120 repo_path = strdup(cwd);
5121 if (repo_path == NULL) {
5122 error = got_error_from_errno("strdup");
5128 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5133 if (show_diffstat) {
5134 dsa.paths = &diffstat_paths;
5135 dsa.force_text = force_text_diff;
5136 dsa.ignore_ws = ignore_whitespace;
5137 dsa.diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
5140 if (rflag || worktree == NULL || ncommit_args > 0) {
5142 error = got_error_msg(GOT_ERR_NOT_IMPL,
5143 "-P option can only be used when diffing "
5148 error = got_error_msg(GOT_ERR_NOT_IMPL,
5149 "-s option can only be used when diffing "
5155 error = apply_unveil(got_repo_get_path(repo), 1,
5156 worktree ? got_worktree_get_root_path(worktree) : NULL);
5160 if ((!force_path && argc == 2) || ncommit_args > 0) {
5161 int obj_type = (ncommit_args > 0 ?
5162 GOT_OBJ_TYPE_COMMIT : GOT_OBJ_TYPE_ANY);
5163 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5167 for (i = 0; i < (ncommit_args > 0 ? ncommit_args : argc); i++) {
5169 if (ncommit_args > 0)
5170 arg = commit_args[i];
5173 error = got_repo_match_object_id(&ids[i], &labels[i],
5174 arg, obj_type, &refs, repo);
5176 if (error->code != GOT_ERR_NOT_REF &&
5177 error->code != GOT_ERR_NO_OBJ)
5179 if (ncommit_args > 0)
5187 f1 = got_opentemp();
5189 error = got_error_from_errno("got_opentemp");
5193 f2 = got_opentemp();
5195 error = got_error_from_errno("got_opentemp");
5199 outfile = got_opentemp();
5200 if (outfile == NULL) {
5201 error = got_error_from_errno("got_opentemp");
5205 if (ncommit_args == 0 && (ids[0] == NULL || ids[1] == NULL)) {
5206 struct print_diff_arg arg;
5209 if (worktree == NULL) {
5210 if (argc == 2 && ids[0] == NULL) {
5211 error = got_error_path(argv[0], GOT_ERR_NO_OBJ);
5213 } else if (argc == 2 && ids[1] == NULL) {
5214 error = got_error_path(argv[1], GOT_ERR_NO_OBJ);
5216 } else if (argc > 0) {
5217 error = got_error_fmt(GOT_ERR_NOT_WORKTREE,
5218 "%s", "specified paths cannot be resolved");
5221 error = got_error(GOT_ERR_NOT_WORKTREE);
5226 error = get_worktree_paths_from_argv(&paths, argc, argv,
5231 error = got_object_id_str(&id_str,
5232 got_worktree_get_base_commit_id(worktree));
5236 arg.worktree = worktree;
5237 arg.diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
5238 arg.diff_context = diff_context;
5239 arg.id_str = id_str;
5240 arg.header_shown = 0;
5241 arg.diff_staged = diff_staged;
5242 arg.ignore_whitespace = ignore_whitespace;
5243 arg.force_text_diff = force_text_diff;
5244 arg.diffstat = show_diffstat ? &dsa : NULL;
5247 arg.outfile = outfile;
5249 error = got_worktree_status(worktree, &paths, repo, 0,
5250 print_diff, &arg, check_cancelled, NULL);
5255 if (show_diffstat && dsa.nfiles > 0) {
5258 if (asprintf(&header, "diffstat %s%s",
5259 diff_staged ? "-s " : "",
5260 got_worktree_get_root_path(worktree)) == -1) {
5261 error = got_error_from_errno("asprintf");
5265 error = print_diffstat(&dsa, header);
5271 error = printfile(outfile);
5275 if (ncommit_args == 1) {
5276 struct got_commit_object *commit;
5277 error = got_object_open_as_commit(&commit, repo, ids[0]);
5281 labels[1] = labels[0];
5283 if (got_object_commit_get_nparents(commit) > 0) {
5284 const struct got_object_id_queue *pids;
5285 struct got_object_qid *pid;
5286 pids = got_object_commit_get_parent_ids(commit);
5287 pid = STAILQ_FIRST(pids);
5288 ids[0] = got_object_id_dup(&pid->id);
5289 if (ids[0] == NULL) {
5290 error = got_error_from_errno(
5291 "got_object_id_dup");
5292 got_object_commit_close(commit);
5295 error = got_object_id_str(&labels[0], ids[0]);
5297 got_object_commit_close(commit);
5302 labels[0] = strdup("/dev/null");
5303 if (labels[0] == NULL) {
5304 error = got_error_from_errno("strdup");
5305 got_object_commit_close(commit);
5310 got_object_commit_close(commit);
5313 if (ncommit_args == 0 && argc > 2) {
5314 error = got_error_msg(GOT_ERR_BAD_PATH,
5315 "path arguments cannot be used when diffing two objects");
5320 error = got_object_get_type(&type1, repo, ids[0]);
5325 error = got_object_get_type(&type2, repo, ids[1]);
5328 if (type1 != GOT_OBJ_TYPE_ANY && type1 != type2) {
5329 error = got_error(GOT_ERR_OBJ_TYPE);
5332 if (type1 == GOT_OBJ_TYPE_BLOB && argc > 2) {
5333 error = got_error_msg(GOT_ERR_OBJ_TYPE,
5334 "path arguments cannot be used when diffing blobs");
5338 for (i = 0; ncommit_args > 0 && i < argc; i++) {
5340 struct got_pathlist_entry *new;
5344 error = got_worktree_resolve_path(&p, worktree,
5348 prefix = got_worktree_get_path_prefix(worktree);
5349 while (prefix[0] == '/')
5351 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5352 (p[0] != '\0' && prefix[0] != '\0') ? "/" : "",
5354 error = got_error_from_errno("asprintf");
5360 char *mapped_path, *s;
5361 error = got_repo_map_path(&mapped_path, repo, argv[i]);
5367 in_repo_path = strdup(s);
5368 if (in_repo_path == NULL) {
5369 error = got_error_from_errno("asprintf");
5376 error = got_pathlist_insert(&new, &paths, in_repo_path, NULL);
5377 if (error || new == NULL /* duplicate */)
5384 /* Release work tree lock. */
5385 got_worktree_close(worktree);
5389 fd1 = got_opentempfd();
5391 error = got_error_from_errno("got_opentempfd");
5395 fd2 = got_opentempfd();
5397 error = got_error_from_errno("got_opentempfd");
5401 switch (type1 == GOT_OBJ_TYPE_ANY ? type2 : type1) {
5402 case GOT_OBJ_TYPE_BLOB:
5403 error = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5404 fd1, fd2, ids[0], ids[1], NULL, NULL,
5405 GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
5406 ignore_whitespace, force_text_diff,
5407 show_diffstat ? &dsa : NULL, repo, outfile);
5409 case GOT_OBJ_TYPE_TREE:
5410 error = got_diff_objects_as_trees(NULL, NULL, f1, f2, fd1, fd2,
5411 ids[0], ids[1], &paths, "", "",
5412 GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
5413 ignore_whitespace, force_text_diff,
5414 show_diffstat ? &dsa : NULL, repo, outfile);
5416 case GOT_OBJ_TYPE_COMMIT:
5417 fprintf(outfile, "diff %s %s\n", labels[0], labels[1]);
5418 error = got_diff_objects_as_commits(NULL, NULL, f1, f2,
5419 fd1, fd2, ids[0], ids[1], &paths,
5420 GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
5421 ignore_whitespace, force_text_diff,
5422 show_diffstat ? &dsa : NULL, repo, outfile);
5425 error = got_error(GOT_ERR_OBJ_TYPE);
5430 if (show_diffstat && dsa.nfiles > 0) {
5431 char *header = NULL;
5433 if (asprintf(&header, "diffstat %s %s",
5434 labels[0], labels[1]) == -1) {
5435 error = got_error_from_errno("asprintf");
5439 error = print_diffstat(&dsa, header);
5445 error = printfile(outfile);
5453 got_worktree_close(worktree);
5455 const struct got_error *close_err = got_repo_close(repo);
5460 const struct got_error *pack_err =
5461 got_repo_pack_fds_close(pack_fds);
5465 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
5466 got_pathlist_free(&diffstat_paths, GOT_PATHLIST_FREE_ALL);
5467 got_ref_list_free(&refs);
5468 if (outfile && fclose(outfile) == EOF && error == NULL)
5469 error = got_error_from_errno("fclose");
5470 if (f1 && fclose(f1) == EOF && error == NULL)
5471 error = got_error_from_errno("fclose");
5472 if (f2 && fclose(f2) == EOF && error == NULL)
5473 error = got_error_from_errno("fclose");
5474 if (fd1 != -1 && close(fd1) == -1 && error == NULL)
5475 error = got_error_from_errno("close");
5476 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
5477 error = got_error_from_errno("close");
5485 "usage: %s blame [-c commit] [-r repository-path] path\n",
5494 char datebuf[11]; /* YYYY-MM-DD + NUL */
5497 struct blame_cb_args {
5498 struct blame_line *lines;
5502 off_t *line_offsets;
5504 struct got_repository *repo;
5507 static const struct got_error *
5508 blame_cb(void *arg, int nlines, int lineno,
5509 struct got_commit_object *commit, struct got_object_id *id)
5511 const struct got_error *err = NULL;
5512 struct blame_cb_args *a = arg;
5513 struct blame_line *bline;
5515 size_t linesize = 0;
5518 time_t committer_time;
5520 if (nlines != a->nlines ||
5521 (lineno != -1 && lineno < 1) || lineno > a->nlines)
5522 return got_error(GOT_ERR_RANGE);
5524 if (sigint_received)
5525 return got_error(GOT_ERR_ITER_COMPLETED);
5528 return NULL; /* no change in this commit */
5530 /* Annotate this line. */
5531 bline = &a->lines[lineno - 1];
5532 if (bline->annotated)
5534 err = got_object_id_str(&bline->id_str, id);
5538 bline->committer = strdup(got_object_commit_get_committer(commit));
5539 if (bline->committer == NULL) {
5540 err = got_error_from_errno("strdup");
5544 committer_time = got_object_commit_get_committer_time(commit);
5545 if (gmtime_r(&committer_time, &tm) == NULL)
5546 return got_error_from_errno("gmtime_r");
5547 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
5549 err = got_error(GOT_ERR_NO_SPACE);
5552 bline->annotated = 1;
5554 /* Print lines annotated so far. */
5555 bline = &a->lines[a->lineno_cur - 1];
5556 if (!bline->annotated)
5559 offset = a->line_offsets[a->lineno_cur - 1];
5560 if (fseeko(a->f, offset, SEEK_SET) == -1) {
5561 err = got_error_from_errno("fseeko");
5565 while (a->lineno_cur <= a->nlines && bline->annotated) {
5566 char *smallerthan, *at, *nl, *committer;
5569 if (getline(&line, &linesize, a->f) == -1) {
5571 err = got_error_from_errno("getline");
5575 committer = bline->committer;
5576 smallerthan = strchr(committer, '<');
5577 if (smallerthan && smallerthan[1] != '\0')
5578 committer = smallerthan + 1;
5579 at = strchr(committer, '@');
5582 len = strlen(committer);
5584 committer[8] = '\0';
5586 nl = strchr(line, '\n');
5589 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
5590 bline->id_str, bline->datebuf, committer, line);
5593 bline = &a->lines[a->lineno_cur - 1];
5600 static const struct got_error *
5601 cmd_blame(int argc, char *argv[])
5603 const struct got_error *error;
5604 struct got_repository *repo = NULL;
5605 struct got_worktree *worktree = NULL;
5606 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5607 char *link_target = NULL;
5608 struct got_object_id *obj_id = NULL;
5609 struct got_object_id *commit_id = NULL;
5610 struct got_commit_object *commit = NULL;
5611 struct got_blob_object *blob = NULL;
5612 char *commit_id_str = NULL;
5613 struct blame_cb_args bca;
5614 int ch, obj_type, i, fd1 = -1, fd2 = -1, fd3 = -1;
5616 int *pack_fds = NULL;
5617 FILE *f1 = NULL, *f2 = NULL;
5619 fd1 = got_opentempfd();
5621 return got_error_from_errno("got_opentempfd");
5623 memset(&bca, 0, sizeof(bca));
5626 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5631 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5634 commit_id_str = optarg;
5637 repo_path = realpath(optarg, NULL);
5638 if (repo_path == NULL)
5639 return got_error_from_errno2("realpath",
5641 got_path_strip_trailing_slashes(repo_path);
5657 cwd = getcwd(NULL, 0);
5659 error = got_error_from_errno("getcwd");
5663 error = got_repo_pack_fds_open(&pack_fds);
5667 if (repo_path == NULL) {
5668 error = got_worktree_open(&worktree, cwd);
5669 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5675 strdup(got_worktree_get_repo_path(worktree));
5676 if (repo_path == NULL) {
5677 error = got_error_from_errno("strdup");
5682 repo_path = strdup(cwd);
5683 if (repo_path == NULL) {
5684 error = got_error_from_errno("strdup");
5690 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5695 const char *prefix = got_worktree_get_path_prefix(worktree);
5698 error = got_worktree_resolve_path(&p, worktree, path);
5701 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5702 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
5704 error = got_error_from_errno("asprintf");
5709 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5711 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5714 error = got_repo_map_path(&in_repo_path, repo, path);
5719 if (commit_id_str == NULL) {
5720 struct got_reference *head_ref;
5721 error = got_ref_open(&head_ref, repo, worktree ?
5722 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5725 error = got_ref_resolve(&commit_id, repo, head_ref);
5726 got_ref_close(head_ref);
5730 struct got_reflist_head refs;
5732 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5736 error = got_repo_match_object_id(&commit_id, NULL,
5737 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5738 got_ref_list_free(&refs);
5744 /* Release work tree lock. */
5745 got_worktree_close(worktree);
5749 error = got_object_open_as_commit(&commit, repo, commit_id);
5753 error = got_object_resolve_symlinks(&link_target, in_repo_path,
5758 error = got_object_id_by_path(&obj_id, repo, commit,
5759 link_target ? link_target : in_repo_path);
5763 error = got_object_get_type(&obj_type, repo, obj_id);
5767 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5768 error = got_error_path(link_target ? link_target : in_repo_path,
5773 error = got_object_open_as_blob(&blob, repo, obj_id, 8192, fd1);
5776 bca.f = got_opentemp();
5777 if (bca.f == NULL) {
5778 error = got_error_from_errno("got_opentemp");
5781 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
5782 &bca.line_offsets, bca.f, blob);
5783 if (error || bca.nlines == 0)
5786 /* Don't include \n at EOF in the blame line count. */
5787 if (bca.line_offsets[bca.nlines - 1] == filesize)
5790 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
5791 if (bca.lines == NULL) {
5792 error = got_error_from_errno("calloc");
5796 bca.nlines_prec = 0;
5804 fd2 = got_opentempfd();
5806 error = got_error_from_errno("got_opentempfd");
5809 fd3 = got_opentempfd();
5811 error = got_error_from_errno("got_opentempfd");
5814 f1 = got_opentemp();
5816 error = got_error_from_errno("got_opentemp");
5819 f2 = got_opentemp();
5821 error = got_error_from_errno("got_opentemp");
5824 error = got_blame(link_target ? link_target : in_repo_path, commit_id,
5825 repo, GOT_DIFF_ALGORITHM_PATIENCE, blame_cb, &bca,
5826 check_cancelled, NULL, fd2, fd3, f1, f2);
5835 got_object_commit_close(commit);
5837 if (fd1 != -1 && close(fd1) == -1 && error == NULL)
5838 error = got_error_from_errno("close");
5839 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
5840 error = got_error_from_errno("close");
5841 if (fd3 != -1 && close(fd3) == -1 && error == NULL)
5842 error = got_error_from_errno("close");
5843 if (f1 && fclose(f1) == EOF && error == NULL)
5844 error = got_error_from_errno("fclose");
5845 if (f2 && fclose(f2) == EOF && error == NULL)
5846 error = got_error_from_errno("fclose");
5849 got_object_blob_close(blob);
5851 got_worktree_close(worktree);
5853 const struct got_error *close_err = got_repo_close(repo);
5858 const struct got_error *pack_err =
5859 got_repo_pack_fds_close(pack_fds);
5864 for (i = 0; i < bca.nlines; i++) {
5865 struct blame_line *bline = &bca.lines[i];
5866 free(bline->id_str);
5867 free(bline->committer);
5871 free(bca.line_offsets);
5872 if (bca.f && fclose(bca.f) == EOF && error == NULL)
5873 error = got_error_from_errno("fclose");
5880 fprintf(stderr, "usage: %s tree [-iR] [-c commit] [-r repository-path] "
5881 "[path]\n", getprogname());
5885 static const struct got_error *
5886 print_entry(struct got_tree_entry *te, const char *id, const char *path,
5887 const char *root_path, struct got_repository *repo)
5889 const struct got_error *err = NULL;
5890 int is_root_path = (strcmp(path, root_path) == 0);
5891 const char *modestr = "";
5892 mode_t mode = got_tree_entry_get_mode(te);
5893 char *link_target = NULL;
5895 path += strlen(root_path);
5896 while (path[0] == '/')
5899 if (got_object_tree_entry_is_submodule(te))
5901 else if (S_ISLNK(mode)) {
5904 err = got_tree_entry_get_symlink_target(&link_target, te, repo);
5907 for (i = 0; i < strlen(link_target); i++) {
5908 if (!isprint((unsigned char)link_target[i]))
5909 link_target[i] = '?';
5914 else if (S_ISDIR(mode))
5916 else if (mode & S_IXUSR)
5919 printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
5920 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
5921 link_target ? " -> ": "", link_target ? link_target : "");
5927 static const struct got_error *
5928 print_tree(const char *path, struct got_commit_object *commit,
5929 int show_ids, int recurse, const char *root_path,
5930 struct got_repository *repo)
5932 const struct got_error *err = NULL;
5933 struct got_object_id *tree_id = NULL;
5934 struct got_tree_object *tree = NULL;
5937 err = got_object_id_by_path(&tree_id, repo, commit, path);
5941 err = got_object_open_as_tree(&tree, repo, tree_id);
5944 nentries = got_object_tree_get_nentries(tree);
5945 for (i = 0; i < nentries; i++) {
5946 struct got_tree_entry *te;
5949 if (sigint_received || sigpipe_received)
5952 te = got_object_tree_get_entry(tree, i);
5955 err = got_object_id_str(&id_str,
5956 got_tree_entry_get_id(te));
5959 if (asprintf(&id, "%s ", id_str) == -1) {
5960 err = got_error_from_errno("asprintf");
5966 err = print_entry(te, id, path, root_path, repo);
5971 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
5973 if (asprintf(&child_path, "%s%s%s", path,
5974 path[0] == '/' && path[1] == '\0' ? "" : "/",
5975 got_tree_entry_get_name(te)) == -1) {
5976 err = got_error_from_errno("asprintf");
5979 err = print_tree(child_path, commit, show_ids, 1,
5988 got_object_tree_close(tree);
5993 static const struct got_error *
5994 cmd_tree(int argc, char *argv[])
5996 const struct got_error *error;
5997 struct got_repository *repo = NULL;
5998 struct got_worktree *worktree = NULL;
5999 const char *path, *refname = NULL;
6000 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6001 struct got_object_id *commit_id = NULL;
6002 struct got_commit_object *commit = NULL;
6003 char *commit_id_str = NULL;
6004 int show_ids = 0, recurse = 0;
6006 int *pack_fds = NULL;
6009 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6014 while ((ch = getopt(argc, argv, "c:iRr:")) != -1) {
6017 commit_id_str = optarg;
6026 repo_path = realpath(optarg, NULL);
6027 if (repo_path == NULL)
6028 return got_error_from_errno2("realpath",
6030 got_path_strip_trailing_slashes(repo_path);
6048 cwd = getcwd(NULL, 0);
6050 error = got_error_from_errno("getcwd");
6054 error = got_repo_pack_fds_open(&pack_fds);
6058 if (repo_path == NULL) {
6059 error = got_worktree_open(&worktree, cwd);
6060 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6066 strdup(got_worktree_get_repo_path(worktree));
6067 if (repo_path == NULL)
6068 error = got_error_from_errno("strdup");
6072 repo_path = strdup(cwd);
6073 if (repo_path == NULL) {
6074 error = got_error_from_errno("strdup");
6080 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6085 const char *prefix = got_worktree_get_path_prefix(worktree);
6090 error = got_worktree_resolve_path(&p, worktree, path);
6093 if (asprintf(&in_repo_path, "%s%s%s", prefix,
6094 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
6096 error = got_error_from_errno("asprintf");
6101 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6105 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6110 error = got_repo_map_path(&in_repo_path, repo, path);
6115 if (commit_id_str == NULL) {
6116 struct got_reference *head_ref;
6118 refname = got_worktree_get_head_ref_name(worktree);
6120 refname = GOT_REF_HEAD;
6121 error = got_ref_open(&head_ref, repo, refname, 0);
6124 error = got_ref_resolve(&commit_id, repo, head_ref);
6125 got_ref_close(head_ref);
6129 struct got_reflist_head refs;
6131 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
6135 error = got_repo_match_object_id(&commit_id, NULL,
6136 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
6137 got_ref_list_free(&refs);
6143 /* Release work tree lock. */
6144 got_worktree_close(worktree);
6148 error = got_object_open_as_commit(&commit, repo, commit_id);
6152 error = print_tree(in_repo_path, commit, show_ids, recurse,
6153 in_repo_path, repo);
6160 got_object_commit_close(commit);
6162 got_worktree_close(worktree);
6164 const struct got_error *close_err = got_repo_close(repo);
6169 const struct got_error *pack_err =
6170 got_repo_pack_fds_close(pack_fds);
6180 fprintf(stderr, "usage: %s status [-I] [-S status-codes] "
6181 "[-s status-codes] [path ...]\n", getprogname());
6185 struct got_status_arg {
6190 static const struct got_error *
6191 print_status(void *arg, unsigned char status, unsigned char staged_status,
6192 const char *path, struct got_object_id *blob_id,
6193 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6194 int dirfd, const char *de_name)
6196 struct got_status_arg *st = arg;
6198 if (status == staged_status && (status == GOT_STATUS_DELETE))
6199 status = GOT_STATUS_NO_CHANGE;
6200 if (st != NULL && st->status_codes) {
6201 size_t ncodes = strlen(st->status_codes);
6204 for (i = 0; i < ncodes ; i++) {
6206 if (status == st->status_codes[i] ||
6207 staged_status == st->status_codes[i]) {
6212 if (status == st->status_codes[i] ||
6213 staged_status == st->status_codes[i])
6218 if (st->suppress && j == 0)
6225 printf("%c%c %s\n", status, staged_status, path);
6229 static const struct got_error *
6230 cmd_status(int argc, char *argv[])
6232 const struct got_error *error = NULL;
6233 struct got_repository *repo = NULL;
6234 struct got_worktree *worktree = NULL;
6235 struct got_status_arg st;
6237 struct got_pathlist_head paths;
6238 int ch, i, no_ignores = 0;
6239 int *pack_fds = NULL;
6243 memset(&st, 0, sizeof(st));
6244 st.status_codes = NULL;
6247 while ((ch = getopt(argc, argv, "IS:s:")) != -1) {
6253 if (st.status_codes != NULL && st.suppress == 0)
6254 option_conflict('S', 's');
6258 for (i = 0; i < strlen(optarg); i++) {
6259 switch (optarg[i]) {
6260 case GOT_STATUS_MODIFY:
6261 case GOT_STATUS_ADD:
6262 case GOT_STATUS_DELETE:
6263 case GOT_STATUS_CONFLICT:
6264 case GOT_STATUS_MISSING:
6265 case GOT_STATUS_OBSTRUCTED:
6266 case GOT_STATUS_UNVERSIONED:
6267 case GOT_STATUS_MODE_CHANGE:
6268 case GOT_STATUS_NONEXISTENT:
6271 errx(1, "invalid status code '%c'",
6275 if (ch == 's' && st.suppress)
6276 option_conflict('s', 'S');
6277 st.status_codes = optarg;
6289 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6293 cwd = getcwd(NULL, 0);
6295 error = got_error_from_errno("getcwd");
6299 error = got_repo_pack_fds_open(&pack_fds);
6303 error = got_worktree_open(&worktree, cwd);
6305 if (error->code == GOT_ERR_NOT_WORKTREE)
6306 error = wrap_not_worktree_error(error, "status", cwd);
6310 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6315 error = apply_unveil(got_repo_get_path(repo), 1,
6316 got_worktree_get_root_path(worktree));
6320 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6324 error = got_worktree_status(worktree, &paths, repo, no_ignores,
6325 print_status, &st, check_cancelled, NULL);
6328 const struct got_error *pack_err =
6329 got_repo_pack_fds_close(pack_fds);
6334 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
6342 fprintf(stderr, "usage: %s ref [-dlt] [-c object] [-r repository-path] "
6343 "[-s reference] [name]\n", getprogname());
6347 static const struct got_error *
6348 list_refs(struct got_repository *repo, const char *refname, int sort_by_time)
6350 static const struct got_error *err = NULL;
6351 struct got_reflist_head refs;
6352 struct got_reflist_entry *re;
6355 err = got_ref_list(&refs, repo, refname, sort_by_time ?
6356 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6361 TAILQ_FOREACH(re, &refs, entry) {
6363 refstr = got_ref_to_str(re->ref);
6364 if (refstr == NULL) {
6365 err = got_error_from_errno("got_ref_to_str");
6368 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
6372 got_ref_list_free(&refs);
6376 static const struct got_error *
6377 delete_ref_by_name(struct got_repository *repo, const char *refname)
6379 const struct got_error *err;
6380 struct got_reference *ref;
6382 err = got_ref_open(&ref, repo, refname, 0);
6386 err = delete_ref(repo, ref);
6391 static const struct got_error *
6392 add_ref(struct got_repository *repo, const char *refname, const char *target)
6394 const struct got_error *err = NULL;
6395 struct got_object_id *id = NULL;
6396 struct got_reference *ref = NULL;
6397 struct got_reflist_head refs;
6400 * Don't let the user create a reference name with a leading '-'.
6401 * While technically a valid reference name, this case is usually
6402 * an unintended typo.
6404 if (refname[0] == '-')
6405 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
6408 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
6411 err = got_repo_match_object_id(&id, NULL, target, GOT_OBJ_TYPE_ANY,
6413 got_ref_list_free(&refs);
6417 err = got_ref_alloc(&ref, refname, id);
6421 err = got_ref_write(ref, repo);
6429 static const struct got_error *
6430 add_symref(struct got_repository *repo, const char *refname, const char *target)
6432 const struct got_error *err = NULL;
6433 struct got_reference *ref = NULL;
6434 struct got_reference *target_ref = NULL;
6437 * Don't let the user create a reference name with a leading '-'.
6438 * While technically a valid reference name, this case is usually
6439 * an unintended typo.
6441 if (refname[0] == '-')
6442 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
6444 err = got_ref_open(&target_ref, repo, target, 0);
6448 err = got_ref_alloc_symref(&ref, refname, target_ref);
6452 err = got_ref_write(ref, repo);
6455 got_ref_close(target_ref);
6461 static const struct got_error *
6462 cmd_ref(int argc, char *argv[])
6464 const struct got_error *error = NULL;
6465 struct got_repository *repo = NULL;
6466 struct got_worktree *worktree = NULL;
6467 char *cwd = NULL, *repo_path = NULL;
6468 int ch, do_list = 0, do_delete = 0, sort_by_time = 0;
6469 const char *obj_arg = NULL, *symref_target= NULL;
6470 char *refname = NULL;
6471 int *pack_fds = NULL;
6473 while ((ch = getopt(argc, argv, "c:dlr:s:t")) != -1) {
6485 repo_path = realpath(optarg, NULL);
6486 if (repo_path == NULL)
6487 return got_error_from_errno2("realpath",
6489 got_path_strip_trailing_slashes(repo_path);
6492 symref_target = optarg;
6503 if (obj_arg && do_list)
6504 option_conflict('c', 'l');
6505 if (obj_arg && do_delete)
6506 option_conflict('c', 'd');
6507 if (obj_arg && symref_target)
6508 option_conflict('c', 's');
6509 if (symref_target && do_delete)
6510 option_conflict('s', 'd');
6511 if (symref_target && do_list)
6512 option_conflict('s', 'l');
6513 if (do_delete && do_list)
6514 option_conflict('d', 'l');
6515 if (sort_by_time && !do_list)
6516 errx(1, "-t option requires -l option");
6522 if (argc != 0 && argc != 1)
6525 refname = strdup(argv[0]);
6526 if (refname == NULL) {
6527 error = got_error_from_errno("strdup");
6534 refname = strdup(argv[0]);
6535 if (refname == NULL) {
6536 error = got_error_from_errno("strdup");
6542 got_path_strip_trailing_slashes(refname);
6545 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6546 "sendfd unveil", NULL) == -1)
6549 cwd = getcwd(NULL, 0);
6551 error = got_error_from_errno("getcwd");
6555 error = got_repo_pack_fds_open(&pack_fds);
6559 if (repo_path == NULL) {
6560 error = got_worktree_open(&worktree, cwd);
6561 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6567 strdup(got_worktree_get_repo_path(worktree));
6568 if (repo_path == NULL)
6569 error = got_error_from_errno("strdup");
6573 repo_path = strdup(cwd);
6574 if (repo_path == NULL) {
6575 error = got_error_from_errno("strdup");
6581 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6587 /* Remove "cpath" promise. */
6588 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6594 error = apply_unveil(got_repo_get_path(repo), do_list,
6595 worktree ? got_worktree_get_root_path(worktree) : NULL);
6600 error = list_refs(repo, refname, sort_by_time);
6602 error = delete_ref_by_name(repo, refname);
6603 else if (symref_target)
6604 error = add_symref(repo, refname, symref_target);
6606 if (obj_arg == NULL)
6608 error = add_ref(repo, refname, obj_arg);
6613 const struct got_error *close_err = got_repo_close(repo);
6618 got_worktree_close(worktree);
6620 const struct got_error *pack_err =
6621 got_repo_pack_fds_close(pack_fds);
6633 fprintf(stderr, "usage: %s branch [-lnt] [-c commit] [-d name] "
6634 "[-r repository-path] [name]\n", getprogname());
6638 static const struct got_error *
6639 list_branch(struct got_repository *repo, struct got_worktree *worktree,
6640 struct got_reference *ref)
6642 const struct got_error *err = NULL;
6643 const char *refname, *marker = " ";
6646 refname = got_ref_get_name(ref);
6647 if (worktree && strcmp(refname,
6648 got_worktree_get_head_ref_name(worktree)) == 0) {
6649 struct got_object_id *id = NULL;
6651 err = got_ref_resolve(&id, repo, ref);
6654 if (got_object_id_cmp(id,
6655 got_worktree_get_base_commit_id(worktree)) == 0)
6662 if (strncmp(refname, "refs/heads/", 11) == 0)
6664 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
6666 if (strncmp(refname, "refs/remotes/", 13) == 0)
6669 refstr = got_ref_to_str(ref);
6671 return got_error_from_errno("got_ref_to_str");
6673 printf("%s%s: %s\n", marker, refname, refstr);
6678 static const struct got_error *
6679 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
6681 const char *refname;
6683 if (worktree == NULL)
6684 return got_error(GOT_ERR_NOT_WORKTREE);
6686 refname = got_worktree_get_head_ref_name(worktree);
6688 if (strncmp(refname, "refs/heads/", 11) == 0)
6690 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
6693 printf("%s\n", refname);
6698 static const struct got_error *
6699 list_branches(struct got_repository *repo, struct got_worktree *worktree,
6702 static const struct got_error *err = NULL;
6703 struct got_reflist_head refs;
6704 struct got_reflist_entry *re;
6705 struct got_reference *temp_ref = NULL;
6706 int rebase_in_progress, histedit_in_progress;
6711 err = got_worktree_rebase_in_progress(&rebase_in_progress,
6716 err = got_worktree_histedit_in_progress(&histedit_in_progress,
6721 if (rebase_in_progress || histedit_in_progress) {
6722 err = got_ref_open(&temp_ref, repo,
6723 got_worktree_get_head_ref_name(worktree), 0);
6726 list_branch(repo, worktree, temp_ref);
6727 got_ref_close(temp_ref);
6731 err = got_ref_list(&refs, repo, "refs/heads", sort_by_time ?
6732 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6737 TAILQ_FOREACH(re, &refs, entry)
6738 list_branch(repo, worktree, re->ref);
6740 got_ref_list_free(&refs);
6742 err = got_ref_list(&refs, repo, "refs/remotes", sort_by_time ?
6743 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6748 TAILQ_FOREACH(re, &refs, entry)
6749 list_branch(repo, worktree, re->ref);
6751 got_ref_list_free(&refs);
6756 static const struct got_error *
6757 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
6758 const char *branch_name)
6760 const struct got_error *err = NULL;
6761 struct got_reference *ref = NULL;
6762 char *refname, *remote_refname = NULL;
6764 if (strncmp(branch_name, "refs/", 5) == 0)
6766 if (strncmp(branch_name, "heads/", 6) == 0)
6768 else if (strncmp(branch_name, "remotes/", 8) == 0)
6771 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
6772 return got_error_from_errno("asprintf");
6774 if (asprintf(&remote_refname, "refs/remotes/%s",
6775 branch_name) == -1) {
6776 err = got_error_from_errno("asprintf");
6780 err = got_ref_open(&ref, repo, refname, 0);
6782 const struct got_error *err2;
6783 if (err->code != GOT_ERR_NOT_REF)
6786 * Keep 'err' intact such that if neither branch exists
6787 * we report "refs/heads" rather than "refs/remotes" in
6788 * our error message.
6790 err2 = got_ref_open(&ref, repo, remote_refname, 0);
6797 strcmp(got_worktree_get_head_ref_name(worktree),
6798 got_ref_get_name(ref)) == 0) {
6799 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6800 "will not delete this work tree's current branch");
6804 err = delete_ref(repo, ref);
6809 free(remote_refname);
6813 static const struct got_error *
6814 add_branch(struct got_repository *repo, const char *branch_name,
6815 struct got_object_id *base_commit_id)
6817 const struct got_error *err = NULL;
6818 struct got_reference *ref = NULL;
6819 char *refname = NULL;
6822 * Don't let the user create a branch name with a leading '-'.
6823 * While technically a valid reference name, this case is usually
6824 * an unintended typo.
6826 if (branch_name[0] == '-')
6827 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
6829 if (strncmp(branch_name, "refs/heads/", 11) == 0)
6832 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
6833 err = got_error_from_errno("asprintf");
6837 err = got_ref_open(&ref, repo, refname, 0);
6839 err = got_error(GOT_ERR_BRANCH_EXISTS);
6841 } else if (err->code != GOT_ERR_NOT_REF)
6844 err = got_ref_alloc(&ref, refname, base_commit_id);
6848 err = got_ref_write(ref, repo);
6856 static const struct got_error *
6857 cmd_branch(int argc, char *argv[])
6859 const struct got_error *error = NULL;
6860 struct got_repository *repo = NULL;
6861 struct got_worktree *worktree = NULL;
6862 char *cwd = NULL, *repo_path = NULL;
6863 int ch, do_list = 0, do_show = 0, do_update = 1, sort_by_time = 0;
6864 const char *delref = NULL, *commit_id_arg = NULL;
6865 struct got_reference *ref = NULL;
6866 struct got_pathlist_head paths;
6867 struct got_object_id *commit_id = NULL;
6868 char *commit_id_str = NULL;
6869 int *pack_fds = NULL;
6873 while ((ch = getopt(argc, argv, "c:d:lnr:t")) != -1) {
6876 commit_id_arg = optarg;
6888 repo_path = realpath(optarg, NULL);
6889 if (repo_path == NULL)
6890 return got_error_from_errno2("realpath",
6892 got_path_strip_trailing_slashes(repo_path);
6903 if (do_list && delref)
6904 option_conflict('l', 'd');
6905 if (sort_by_time && !do_list)
6906 errx(1, "-t option requires -l option");
6911 if (!do_list && !delref && argc == 0)
6914 if ((do_list || delref || do_show) && commit_id_arg != NULL)
6915 errx(1, "-c option can only be used when creating a branch");
6917 if (do_list || delref) {
6920 } else if (!do_show && argc != 1)
6924 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6925 "sendfd unveil", NULL) == -1)
6928 cwd = getcwd(NULL, 0);
6930 error = got_error_from_errno("getcwd");
6934 error = got_repo_pack_fds_open(&pack_fds);
6938 if (repo_path == NULL) {
6939 error = got_worktree_open(&worktree, cwd);
6940 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6946 strdup(got_worktree_get_repo_path(worktree));
6947 if (repo_path == NULL)
6948 error = got_error_from_errno("strdup");
6952 repo_path = strdup(cwd);
6953 if (repo_path == NULL) {
6954 error = got_error_from_errno("strdup");
6960 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6965 if (do_list || do_show) {
6966 /* Remove "cpath" promise. */
6967 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6973 error = apply_unveil(got_repo_get_path(repo), do_list,
6974 worktree ? got_worktree_get_root_path(worktree) : NULL);
6979 error = show_current_branch(repo, worktree);
6981 error = list_branches(repo, worktree, sort_by_time);
6983 error = delete_branch(repo, worktree, delref);
6985 struct got_reflist_head refs;
6987 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
6991 if (commit_id_arg == NULL)
6992 commit_id_arg = worktree ?
6993 got_worktree_get_head_ref_name(worktree) :
6995 error = got_repo_match_object_id(&commit_id, NULL,
6996 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &refs, repo);
6997 got_ref_list_free(&refs);
7000 error = add_branch(repo, argv[0], commit_id);
7003 if (worktree && do_update) {
7004 struct got_update_progress_arg upa;
7005 char *branch_refname = NULL;
7007 error = got_object_id_str(&commit_id_str, commit_id);
7010 error = get_worktree_paths_from_argv(&paths, 0, NULL,
7014 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
7016 error = got_error_from_errno("asprintf");
7019 error = got_ref_open(&ref, repo, branch_refname, 0);
7020 free(branch_refname);
7023 error = switch_head_ref(ref, commit_id, worktree,
7027 error = got_worktree_set_base_commit_id(worktree, repo,
7031 memset(&upa, 0, sizeof(upa));
7032 error = got_worktree_checkout_files(worktree, &paths,
7033 repo, update_progress, &upa, check_cancelled,
7037 if (upa.did_something) {
7038 printf("Updated to %s: %s\n",
7039 got_worktree_get_head_ref_name(worktree),
7042 print_update_progress_stats(&upa);
7049 const struct got_error *close_err = got_repo_close(repo);
7054 got_worktree_close(worktree);
7056 const struct got_error *pack_err =
7057 got_repo_pack_fds_close(pack_fds);
7064 free(commit_id_str);
7065 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
7073 fprintf(stderr, "usage: %s tag [-lVv] [-c commit] [-m message] "
7074 "[-r repository-path] [-s signer-id] name\n", getprogname());
7079 static const struct got_error *
7080 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
7082 const struct got_error *err = NULL;
7083 struct got_reflist_entry *re, *se, *new;
7084 struct got_object_id *re_id, *se_id;
7085 struct got_tag_object *re_tag, *se_tag;
7086 time_t re_time, se_time;
7088 STAILQ_FOREACH(re, tags, entry) {
7089 se = STAILQ_FIRST(sorted);
7091 err = got_reflist_entry_dup(&new, re);
7094 STAILQ_INSERT_HEAD(sorted, new, entry);
7097 err = got_ref_resolve(&re_id, repo, re->ref);
7100 err = got_object_open_as_tag(&re_tag, repo, re_id);
7104 re_time = got_object_tag_get_tagger_time(re_tag);
7105 got_object_tag_close(re_tag);
7109 err = got_ref_resolve(&se_id, repo, re->ref);
7112 err = got_object_open_as_tag(&se_tag, repo, se_id);
7116 se_time = got_object_tag_get_tagger_time(se_tag);
7117 got_object_tag_close(se_tag);
7119 if (se_time > re_time) {
7120 err = got_reflist_entry_dup(&new, re);
7123 STAILQ_INSERT_AFTER(sorted, se, new, entry);
7126 se = STAILQ_NEXT(se, entry);
7135 static const struct got_error *
7136 get_tag_refname(char **refname, const char *tag_name)
7138 const struct got_error *err;
7140 if (strncmp("refs/tags/", tag_name, 10) == 0) {
7141 *refname = strdup(tag_name);
7142 if (*refname == NULL)
7143 return got_error_from_errno("strdup");
7144 } else if (asprintf(refname, "refs/tags/%s", tag_name) == -1) {
7145 err = got_error_from_errno("asprintf");
7153 static const struct got_error *
7154 list_tags(struct got_repository *repo, const char *tag_name, int verify_tags,
7155 const char *allowed_signers, const char *revoked_signers, int verbosity)
7157 static const struct got_error *err = NULL;
7158 struct got_reflist_head refs;
7159 struct got_reflist_entry *re;
7160 char *wanted_refname = NULL;
7165 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
7170 struct got_reference *ref;
7171 err = get_tag_refname(&wanted_refname, tag_name);
7174 /* Wanted tag reference should exist. */
7175 err = got_ref_open(&ref, repo, wanted_refname, 0);
7181 TAILQ_FOREACH(re, &refs, entry) {
7182 const char *refname;
7183 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
7185 const char *tagger, *ssh_sig = NULL;
7186 char *sig_msg = NULL;
7188 struct got_object_id *id;
7189 struct got_tag_object *tag;
7190 struct got_commit_object *commit = NULL;
7192 refname = got_ref_get_name(re->ref);
7193 if (strncmp(refname, "refs/tags/", 10) != 0 ||
7194 (wanted_refname && strcmp(refname, wanted_refname) != 0))
7197 refstr = got_ref_to_str(re->ref);
7198 if (refstr == NULL) {
7199 err = got_error_from_errno("got_ref_to_str");
7203 err = got_ref_resolve(&id, repo, re->ref);
7206 err = got_object_open_as_tag(&tag, repo, id);
7208 if (err->code != GOT_ERR_OBJ_TYPE) {
7212 /* "lightweight" tag */
7213 err = got_object_open_as_commit(&commit, repo, id);
7218 tagger = got_object_commit_get_committer(commit);
7220 got_object_commit_get_committer_time(commit);
7221 err = got_object_id_str(&id_str, id);
7227 tagger = got_object_tag_get_tagger(tag);
7228 tagger_time = got_object_tag_get_tagger_time(tag);
7229 err = got_object_id_str(&id_str,
7230 got_object_tag_get_object_id(tag));
7235 if (tag && verify_tags) {
7236 ssh_sig = got_sigs_get_tagmsg_ssh_signature(
7237 got_object_tag_get_message(tag));
7238 if (ssh_sig && allowed_signers == NULL) {
7239 err = got_error_msg(
7240 GOT_ERR_VERIFY_TAG_SIGNATURE,
7241 "SSH signature verification requires "
7242 "setting allowed_signers in "
7248 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
7250 printf("from: %s\n", tagger);
7251 datestr = get_datestr(&tagger_time, datebuf);
7253 printf("date: %s UTC\n", datestr);
7255 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
7257 switch (got_object_tag_get_object_type(tag)) {
7258 case GOT_OBJ_TYPE_BLOB:
7259 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
7262 case GOT_OBJ_TYPE_TREE:
7263 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
7266 case GOT_OBJ_TYPE_COMMIT:
7267 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
7270 case GOT_OBJ_TYPE_TAG:
7271 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
7281 err = got_sigs_verify_tag_ssh(&sig_msg, tag, ssh_sig,
7282 allowed_signers, revoked_signers, verbosity);
7283 if (err && err->code == GOT_ERR_BAD_TAG_SIGNATURE)
7287 printf("signature: %s", sig_msg);
7293 err = got_object_commit_get_logmsg(&tagmsg0, commit);
7296 got_object_commit_close(commit);
7298 tagmsg0 = strdup(got_object_tag_get_message(tag));
7299 got_object_tag_close(tag);
7300 if (tagmsg0 == NULL) {
7301 err = got_error_from_errno("strdup");
7308 line = strsep(&tagmsg, "\n");
7310 printf(" %s\n", line);
7315 got_ref_list_free(&refs);
7316 free(wanted_refname);
7318 if (err == NULL && bad_sigs)
7319 err = got_error(GOT_ERR_BAD_TAG_SIGNATURE);
7323 static const struct got_error *
7324 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
7325 const char *tag_name, const char *repo_path)
7327 const struct got_error *err = NULL;
7328 char *template = NULL, *initial_content = NULL;
7329 char *editor = NULL;
7330 int initial_content_len;
7333 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
7334 err = got_error_from_errno("asprintf");
7338 initial_content_len = asprintf(&initial_content,
7339 "\n# tagging commit %s as %s\n",
7340 commit_id_str, tag_name);
7341 if (initial_content_len == -1) {
7342 err = got_error_from_errno("asprintf");
7346 err = got_opentemp_named_fd(tagmsg_path, &fd, template, "");
7350 if (write(fd, initial_content, initial_content_len) == -1) {
7351 err = got_error_from_errno2("write", *tagmsg_path);
7355 err = get_editor(&editor);
7358 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content,
7359 initial_content_len, 1);
7361 free(initial_content);
7365 if (fd != -1 && close(fd) == -1 && err == NULL)
7366 err = got_error_from_errno2("close", *tagmsg_path);
7375 static const struct got_error *
7376 add_tag(struct got_repository *repo, const char *tagger,
7377 const char *tag_name, const char *commit_arg, const char *tagmsg_arg,
7378 const char *signer_id, int verbosity)
7380 const struct got_error *err = NULL;
7381 struct got_object_id *commit_id = NULL, *tag_id = NULL;
7382 char *label = NULL, *commit_id_str = NULL;
7383 struct got_reference *ref = NULL;
7384 char *refname = NULL, *tagmsg = NULL;
7385 char *tagmsg_path = NULL, *tag_id_str = NULL;
7386 int preserve_tagmsg = 0;
7387 struct got_reflist_head refs;
7392 * Don't let the user create a tag name with a leading '-'.
7393 * While technically a valid reference name, this case is usually
7394 * an unintended typo.
7396 if (tag_name[0] == '-')
7397 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
7399 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
7403 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
7404 GOT_OBJ_TYPE_COMMIT, &refs, repo);
7408 err = got_object_id_str(&commit_id_str, commit_id);
7412 err = get_tag_refname(&refname, tag_name);
7415 if (strncmp("refs/tags/", tag_name, 10) == 0)
7418 err = got_ref_open(&ref, repo, refname, 0);
7420 err = got_error(GOT_ERR_TAG_EXISTS);
7422 } else if (err->code != GOT_ERR_NOT_REF)
7425 if (tagmsg_arg == NULL) {
7426 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
7427 tag_name, got_repo_get_path(repo));
7429 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
7430 tagmsg_path != NULL)
7431 preserve_tagmsg = 1;
7434 /* Editor is done; we can now apply unveil(2) */
7435 err = got_sigs_apply_unveil();
7438 err = apply_unveil(got_repo_get_path(repo), 0, NULL);
7443 err = got_object_tag_create(&tag_id, tag_name, commit_id,
7444 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, signer_id, repo,
7448 preserve_tagmsg = 1;
7452 err = got_ref_alloc(&ref, refname, tag_id);
7455 preserve_tagmsg = 1;
7459 err = got_ref_write(ref, repo);
7462 preserve_tagmsg = 1;
7466 err = got_object_id_str(&tag_id_str, tag_id);
7469 preserve_tagmsg = 1;
7472 printf("Created tag %s\n", tag_id_str);
7474 if (preserve_tagmsg) {
7475 fprintf(stderr, "%s: tag message preserved in %s\n",
7476 getprogname(), tagmsg_path);
7477 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
7478 err = got_error_from_errno2("unlink", tagmsg_path);
7483 free(commit_id_str);
7487 got_ref_list_free(&refs);
7491 static const struct got_error *
7492 cmd_tag(int argc, char *argv[])
7494 const struct got_error *error = NULL;
7495 struct got_repository *repo = NULL;
7496 struct got_worktree *worktree = NULL;
7497 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
7498 char *gitconfig_path = NULL, *tagger = NULL;
7499 char *allowed_signers = NULL, *revoked_signers = NULL;
7500 const char *signer_id = NULL;
7501 const char *tag_name = NULL, *commit_id_arg = NULL, *tagmsg = NULL;
7502 int ch, do_list = 0, verify_tags = 0, verbosity = 0;
7503 int *pack_fds = NULL;
7505 while ((ch = getopt(argc, argv, "c:lm:r:s:Vv")) != -1) {
7508 commit_id_arg = optarg;
7517 repo_path = realpath(optarg, NULL);
7518 if (repo_path == NULL) {
7519 error = got_error_from_errno2("realpath",
7523 got_path_strip_trailing_slashes(repo_path);
7534 else if (verbosity < 3)
7546 if (do_list || verify_tags) {
7547 if (commit_id_arg != NULL)
7549 "-c option can only be used when creating a tag");
7552 option_conflict('l', 'm');
7554 option_conflict('V', 'm');
7558 option_conflict('l', 's');
7560 option_conflict('V', 's');
7564 } else if (argc != 1)
7571 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
7572 "sendfd unveil", NULL) == -1)
7575 cwd = getcwd(NULL, 0);
7577 error = got_error_from_errno("getcwd");
7581 error = got_repo_pack_fds_open(&pack_fds);
7585 if (repo_path == NULL) {
7586 error = got_worktree_open(&worktree, cwd);
7587 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7593 strdup(got_worktree_get_repo_path(worktree));
7594 if (repo_path == NULL)
7595 error = got_error_from_errno("strdup");
7599 repo_path = strdup(cwd);
7600 if (repo_path == NULL) {
7601 error = got_error_from_errno("strdup");
7607 if (do_list || verify_tags) {
7608 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7611 error = get_allowed_signers(&allowed_signers, repo, worktree);
7614 error = get_revoked_signers(&revoked_signers, repo, worktree);
7618 /* Release work tree lock. */
7619 got_worktree_close(worktree);
7624 * Remove "cpath" promise unless needed for signature tmpfile
7628 got_sigs_apply_unveil();
7631 if (pledge("stdio rpath wpath flock proc exec sendfd "
7632 "unveil", NULL) == -1)
7636 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7639 error = list_tags(repo, tag_name, verify_tags, allowed_signers,
7640 revoked_signers, verbosity);
7642 error = get_gitconfig_path(&gitconfig_path);
7645 error = got_repo_open(&repo, repo_path, gitconfig_path,
7650 error = get_author(&tagger, repo, worktree);
7653 if (signer_id == NULL)
7654 signer_id = get_signer_id(repo, worktree);
7658 error = got_sigs_apply_unveil();
7662 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
7667 if (commit_id_arg == NULL) {
7668 struct got_reference *head_ref;
7669 struct got_object_id *commit_id;
7670 error = got_ref_open(&head_ref, repo,
7671 worktree ? got_worktree_get_head_ref_name(worktree)
7675 error = got_ref_resolve(&commit_id, repo, head_ref);
7676 got_ref_close(head_ref);
7679 error = got_object_id_str(&commit_id_str, commit_id);
7686 /* Release work tree lock. */
7687 got_worktree_close(worktree);
7691 error = add_tag(repo, tagger, tag_name,
7692 commit_id_str ? commit_id_str : commit_id_arg, tagmsg,
7693 signer_id, verbosity);
7697 const struct got_error *close_err = got_repo_close(repo);
7702 got_worktree_close(worktree);
7704 const struct got_error *pack_err =
7705 got_repo_pack_fds_close(pack_fds);
7711 free(gitconfig_path);
7712 free(commit_id_str);
7714 free(allowed_signers);
7715 free(revoked_signers);
7722 fprintf(stderr, "usage: %s add [-IR] path ...\n", getprogname());
7726 static const struct got_error *
7727 add_progress(void *arg, unsigned char status, const char *path)
7729 while (path[0] == '/')
7731 printf("%c %s\n", status, path);
7735 static const struct got_error *
7736 cmd_add(int argc, char *argv[])
7738 const struct got_error *error = NULL;
7739 struct got_repository *repo = NULL;
7740 struct got_worktree *worktree = NULL;
7742 struct got_pathlist_head paths;
7743 struct got_pathlist_entry *pe;
7744 int ch, can_recurse = 0, no_ignores = 0;
7745 int *pack_fds = NULL;
7749 while ((ch = getopt(argc, argv, "IR")) != -1) {
7767 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7774 cwd = getcwd(NULL, 0);
7776 error = got_error_from_errno("getcwd");
7780 error = got_repo_pack_fds_open(&pack_fds);
7784 error = got_worktree_open(&worktree, cwd);
7786 if (error->code == GOT_ERR_NOT_WORKTREE)
7787 error = wrap_not_worktree_error(error, "add", cwd);
7791 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7796 error = apply_unveil(got_repo_get_path(repo), 1,
7797 got_worktree_get_root_path(worktree));
7801 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7808 TAILQ_FOREACH(pe, &paths, entry) {
7809 if (asprintf(&ondisk_path, "%s/%s",
7810 got_worktree_get_root_path(worktree),
7812 error = got_error_from_errno("asprintf");
7815 if (lstat(ondisk_path, &sb) == -1) {
7816 if (errno == ENOENT) {
7820 error = got_error_from_errno2("lstat",
7826 if (S_ISDIR(sb.st_mode)) {
7827 error = got_error_msg(GOT_ERR_BAD_PATH,
7828 "adding directories requires -R option");
7834 error = got_worktree_schedule_add(worktree, &paths, add_progress,
7835 NULL, repo, no_ignores);
7838 const struct got_error *close_err = got_repo_close(repo);
7843 got_worktree_close(worktree);
7845 const struct got_error *pack_err =
7846 got_repo_pack_fds_close(pack_fds);
7850 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
7858 fprintf(stderr, "usage: %s remove [-fkR] [-s status-codes] path ...\n",
7863 static const struct got_error *
7864 print_remove_status(void *arg, unsigned char status,
7865 unsigned char staged_status, const char *path)
7867 while (path[0] == '/')
7869 if (status == GOT_STATUS_NONEXISTENT)
7871 if (status == staged_status && (status == GOT_STATUS_DELETE))
7872 status = GOT_STATUS_NO_CHANGE;
7873 printf("%c%c %s\n", status, staged_status, path);
7877 static const struct got_error *
7878 cmd_remove(int argc, char *argv[])
7880 const struct got_error *error = NULL;
7881 struct got_worktree *worktree = NULL;
7882 struct got_repository *repo = NULL;
7883 const char *status_codes = NULL;
7885 struct got_pathlist_head paths;
7886 struct got_pathlist_entry *pe;
7887 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0, i;
7888 int ignore_missing_paths = 0;
7889 int *pack_fds = NULL;
7893 while ((ch = getopt(argc, argv, "fkRs:")) != -1) {
7896 delete_local_mods = 1;
7897 ignore_missing_paths = 1;
7906 for (i = 0; i < strlen(optarg); i++) {
7907 switch (optarg[i]) {
7908 case GOT_STATUS_MODIFY:
7909 delete_local_mods = 1;
7911 case GOT_STATUS_MISSING:
7912 ignore_missing_paths = 1;
7915 errx(1, "invalid status code '%c'",
7919 status_codes = optarg;
7931 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7938 cwd = getcwd(NULL, 0);
7940 error = got_error_from_errno("getcwd");
7944 error = got_repo_pack_fds_open(&pack_fds);
7948 error = got_worktree_open(&worktree, cwd);
7950 if (error->code == GOT_ERR_NOT_WORKTREE)
7951 error = wrap_not_worktree_error(error, "remove", cwd);
7955 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7960 error = apply_unveil(got_repo_get_path(repo), 1,
7961 got_worktree_get_root_path(worktree));
7965 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7972 TAILQ_FOREACH(pe, &paths, entry) {
7973 if (asprintf(&ondisk_path, "%s/%s",
7974 got_worktree_get_root_path(worktree),
7976 error = got_error_from_errno("asprintf");
7979 if (lstat(ondisk_path, &sb) == -1) {
7980 if (errno == ENOENT) {
7984 error = got_error_from_errno2("lstat",
7990 if (S_ISDIR(sb.st_mode)) {
7991 error = got_error_msg(GOT_ERR_BAD_PATH,
7992 "removing directories requires -R option");
7998 error = got_worktree_schedule_delete(worktree, &paths,
7999 delete_local_mods, status_codes, print_remove_status, NULL,
8000 repo, keep_on_disk, ignore_missing_paths);
8003 const struct got_error *close_err = got_repo_close(repo);
8008 got_worktree_close(worktree);
8010 const struct got_error *pack_err =
8011 got_repo_pack_fds_close(pack_fds);
8015 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
8023 fprintf(stderr, "usage: %s patch [-nR] [-c commit] [-p strip-count] "
8024 "[patchfile]\n", getprogname());
8028 static const struct got_error *
8029 patch_from_stdin(int *patchfd)
8031 const struct got_error *err = NULL;
8034 sig_t sighup, sigint, sigquit;
8036 *patchfd = got_opentempfd();
8038 return got_error_from_errno("got_opentempfd");
8040 sighup = signal(SIGHUP, SIG_DFL);
8041 sigint = signal(SIGINT, SIG_DFL);
8042 sigquit = signal(SIGQUIT, SIG_DFL);
8045 r = read(0, buf, sizeof(buf));
8047 err = got_error_from_errno("read");
8052 if (write(*patchfd, buf, r) == -1) {
8053 err = got_error_from_errno("write");
8058 signal(SIGHUP, sighup);
8059 signal(SIGINT, sigint);
8060 signal(SIGQUIT, sigquit);
8062 if (err == NULL && lseek(*patchfd, 0, SEEK_SET) == -1)
8063 err = got_error_from_errno("lseek");
8073 static const struct got_error *
8074 patch_progress(void *arg, const char *old, const char *new,
8075 unsigned char status, const struct got_error *error, int old_from,
8076 int old_lines, int new_from, int new_lines, int offset,
8077 int ws_mangled, const struct got_error *hunk_err)
8079 const char *path = new == NULL ? old : new;
8081 while (*path == '/')
8085 printf("%c %s\n", status, path);
8088 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
8090 if (offset != 0 || hunk_err != NULL || ws_mangled) {
8091 printf("@@ -%d,%d +%d,%d @@ ", old_from,
8092 old_lines, new_from, new_lines);
8093 if (hunk_err != NULL)
8094 printf("%s\n", hunk_err->msg);
8095 else if (offset != 0)
8096 printf("applied with offset %d\n", offset);
8098 printf("hunk contains mangled whitespace\n");
8104 static const struct got_error *
8105 cmd_patch(int argc, char *argv[])
8107 const struct got_error *error = NULL, *close_error = NULL;
8108 struct got_worktree *worktree = NULL;
8109 struct got_repository *repo = NULL;
8110 struct got_reflist_head refs;
8111 struct got_object_id *commit_id = NULL;
8112 const char *commit_id_str = NULL;
8116 int ch, nop = 0, strip = -1, reverse = 0;
8118 int *pack_fds = NULL;
8123 if (pledge("stdio rpath wpath cpath fattr proc exec sendfd flock "
8124 "unveil", NULL) == -1)
8128 while ((ch = getopt(argc, argv, "c:np:R")) != -1) {
8131 commit_id_str = optarg;
8137 strip = strtonum(optarg, 0, INT_MAX, &errstr);
8139 errx(1, "pathname strip count is %s: %s",
8155 error = patch_from_stdin(&patchfd);
8158 } else if (argc == 1) {
8159 patchfd = open(argv[0], O_RDONLY);
8160 if (patchfd == -1) {
8161 error = got_error_from_errno2("open", argv[0]);
8164 if (fstat(patchfd, &sb) == -1) {
8165 error = got_error_from_errno2("fstat", argv[0]);
8168 if (!S_ISREG(sb.st_mode)) {
8169 error = got_error_path(argv[0], GOT_ERR_BAD_FILETYPE);
8175 if ((cwd = getcwd(NULL, 0)) == NULL) {
8176 error = got_error_from_errno("getcwd");
8180 error = got_repo_pack_fds_open(&pack_fds);
8184 error = got_worktree_open(&worktree, cwd);
8188 const char *repo_path = got_worktree_get_repo_path(worktree);
8189 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8193 error = apply_unveil(got_repo_get_path(repo), 0,
8194 got_worktree_get_root_path(worktree));
8198 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
8202 if (commit_id_str != NULL) {
8203 error = got_repo_match_object_id(&commit_id, NULL,
8204 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
8209 error = got_patch(patchfd, worktree, repo, nop, strip, reverse,
8210 commit_id, &patch_progress, NULL, check_cancelled, NULL);
8213 got_ref_list_free(&refs);
8216 close_error = got_repo_close(repo);
8218 error = close_error;
8220 if (worktree != NULL) {
8221 close_error = got_worktree_close(worktree);
8223 error = close_error;
8226 const struct got_error *pack_err =
8227 got_repo_pack_fds_close(pack_fds);
8238 fprintf(stderr, "usage: %s revert [-pR] [-F response-script] path ...\n",
8243 static const struct got_error *
8244 revert_progress(void *arg, unsigned char status, const char *path)
8246 if (status == GOT_STATUS_UNVERSIONED)
8249 while (path[0] == '/')
8251 printf("%c %s\n", status, path);
8255 struct choose_patch_arg {
8256 FILE *patch_script_file;
8260 static const struct got_error *
8261 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
8262 int nchanges, const char *action)
8264 const struct got_error *err;
8266 size_t linesize = 0;
8270 case GOT_STATUS_ADD:
8271 printf("A %s\n%s this addition? [y/n] ", path, action);
8273 case GOT_STATUS_DELETE:
8274 printf("D %s\n%s this deletion? [y/n] ", path, action);
8276 case GOT_STATUS_MODIFY:
8277 if (fseek(patch_file, 0L, SEEK_SET) == -1)
8278 return got_error_from_errno("fseek");
8279 printf(GOT_COMMIT_SEP_STR);
8280 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
8282 if (linelen == -1 && ferror(patch_file)) {
8283 err = got_error_from_errno("getline");
8288 printf(GOT_COMMIT_SEP_STR);
8289 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
8290 path, n, nchanges, action);
8293 return got_error_path(path, GOT_ERR_FILE_STATUS);
8300 static const struct got_error *
8301 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
8302 FILE *patch_file, int n, int nchanges)
8304 const struct got_error *err = NULL;
8306 size_t linesize = 0;
8309 struct choose_patch_arg *a = arg;
8311 *choice = GOT_PATCH_CHOICE_NONE;
8313 if (a->patch_script_file) {
8315 err = show_change(status, path, patch_file, n, nchanges,
8319 linelen = getline(&line, &linesize, a->patch_script_file);
8320 if (linelen == -1) {
8321 if (ferror(a->patch_script_file))
8322 return got_error_from_errno("getline");
8325 nl = strchr(line, '\n');
8328 if (strcmp(line, "y") == 0) {
8329 *choice = GOT_PATCH_CHOICE_YES;
8331 } else if (strcmp(line, "n") == 0) {
8332 *choice = GOT_PATCH_CHOICE_NO;
8334 } else if (strcmp(line, "q") == 0 &&
8335 status == GOT_STATUS_MODIFY) {
8336 *choice = GOT_PATCH_CHOICE_QUIT;
8339 printf("invalid response '%s'\n", line);
8344 while (resp != 'y' && resp != 'n' && resp != 'q') {
8345 err = show_change(status, path, patch_file, n, nchanges,
8352 if (status == GOT_STATUS_MODIFY) {
8353 if (resp != 'y' && resp != 'n' && resp != 'q') {
8354 printf("invalid response '%c'\n", resp);
8357 } else if (resp != 'y' && resp != 'n') {
8358 printf("invalid response '%c'\n", resp);
8364 *choice = GOT_PATCH_CHOICE_YES;
8365 else if (resp == 'n')
8366 *choice = GOT_PATCH_CHOICE_NO;
8367 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
8368 *choice = GOT_PATCH_CHOICE_QUIT;
8373 static const struct got_error *
8374 cmd_revert(int argc, char *argv[])
8376 const struct got_error *error = NULL;
8377 struct got_worktree *worktree = NULL;
8378 struct got_repository *repo = NULL;
8379 char *cwd = NULL, *path = NULL;
8380 struct got_pathlist_head paths;
8381 struct got_pathlist_entry *pe;
8382 int ch, can_recurse = 0, pflag = 0;
8383 FILE *patch_script_file = NULL;
8384 const char *patch_script_path = NULL;
8385 struct choose_patch_arg cpa;
8386 int *pack_fds = NULL;
8390 while ((ch = getopt(argc, argv, "F:pR")) != -1) {
8393 patch_script_path = optarg;
8411 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8412 "unveil", NULL) == -1)
8417 if (patch_script_path && !pflag)
8418 errx(1, "-F option can only be used together with -p option");
8420 cwd = getcwd(NULL, 0);
8422 error = got_error_from_errno("getcwd");
8426 error = got_repo_pack_fds_open(&pack_fds);
8430 error = got_worktree_open(&worktree, cwd);
8432 if (error->code == GOT_ERR_NOT_WORKTREE)
8433 error = wrap_not_worktree_error(error, "revert", cwd);
8437 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8442 if (patch_script_path) {
8443 patch_script_file = fopen(patch_script_path, "re");
8444 if (patch_script_file == NULL) {
8445 error = got_error_from_errno2("fopen",
8450 error = apply_unveil(got_repo_get_path(repo), 1,
8451 got_worktree_get_root_path(worktree));
8455 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8462 TAILQ_FOREACH(pe, &paths, entry) {
8463 if (asprintf(&ondisk_path, "%s/%s",
8464 got_worktree_get_root_path(worktree),
8466 error = got_error_from_errno("asprintf");
8469 if (lstat(ondisk_path, &sb) == -1) {
8470 if (errno == ENOENT) {
8474 error = got_error_from_errno2("lstat",
8480 if (S_ISDIR(sb.st_mode)) {
8481 error = got_error_msg(GOT_ERR_BAD_PATH,
8482 "reverting directories requires -R option");
8488 cpa.patch_script_file = patch_script_file;
8489 cpa.action = "revert";
8490 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
8491 pflag ? choose_patch : NULL, &cpa, repo);
8493 if (patch_script_file && fclose(patch_script_file) == EOF &&
8495 error = got_error_from_errno2("fclose", patch_script_path);
8497 const struct got_error *close_err = got_repo_close(repo);
8502 got_worktree_close(worktree);
8504 const struct got_error *pack_err =
8505 got_repo_pack_fds_close(pack_fds);
8509 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
8518 fprintf(stderr, "usage: %s commit [-NS] [-A author] [-F path] "
8519 "[-m message] [path ...]\n", getprogname());
8523 struct collect_commit_logmsg_arg {
8524 const char *cmdline_log;
8525 const char *prepared_log;
8526 int non_interactive;
8528 const char *worktree_path;
8529 const char *branch_name;
8530 const char *repo_path;
8535 static const struct got_error *
8536 read_prepared_logmsg(char **logmsg, const char *path)
8538 const struct got_error *err = NULL;
8544 memset(&sb, 0, sizeof(sb));
8546 f = fopen(path, "re");
8548 return got_error_from_errno2("fopen", path);
8550 if (fstat(fileno(f), &sb) == -1) {
8551 err = got_error_from_errno2("fstat", path);
8554 if (sb.st_size == 0) {
8555 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
8559 *logmsg = malloc(sb.st_size + 1);
8560 if (*logmsg == NULL) {
8561 err = got_error_from_errno("malloc");
8565 r = fread(*logmsg, 1, sb.st_size, f);
8566 if (r != sb.st_size) {
8568 err = got_error_from_errno2("fread", path);
8570 err = got_error(GOT_ERR_IO);
8573 (*logmsg)[sb.st_size] = '\0';
8575 if (fclose(f) == EOF && err == NULL)
8576 err = got_error_from_errno2("fclose", path);
8584 static const struct got_error *
8585 collect_commit_logmsg(struct got_pathlist_head *commitable_paths,
8586 const char *diff_path, char **logmsg, void *arg)
8588 char *initial_content = NULL;
8589 struct got_pathlist_entry *pe;
8590 const struct got_error *err = NULL;
8591 char *template = NULL;
8592 struct collect_commit_logmsg_arg *a = arg;
8593 int initial_content_len;
8597 /* if a message was specified on the command line, just use it */
8598 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
8599 len = strlen(a->cmdline_log) + 1;
8600 *logmsg = malloc(len + 1);
8601 if (*logmsg == NULL)
8602 return got_error_from_errno("malloc");
8603 strlcpy(*logmsg, a->cmdline_log, len);
8605 } else if (a->prepared_log != NULL && a->non_interactive)
8606 return read_prepared_logmsg(logmsg, a->prepared_log);
8608 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
8609 return got_error_from_errno("asprintf");
8611 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template, "");
8615 if (a->prepared_log) {
8617 err = read_prepared_logmsg(&msg, a->prepared_log);
8620 if (write(fd, msg, strlen(msg)) == -1) {
8621 err = got_error_from_errno2("write", a->logmsg_path);
8628 initial_content_len = asprintf(&initial_content,
8629 "\n# changes to be committed on branch %s:\n",
8631 if (initial_content_len == -1) {
8632 err = got_error_from_errno("asprintf");
8636 if (write(fd, initial_content, initial_content_len) == -1) {
8637 err = got_error_from_errno2("write", a->logmsg_path);
8641 TAILQ_FOREACH(pe, commitable_paths, entry) {
8642 struct got_commitable *ct = pe->data;
8643 dprintf(fd, "# %c %s\n",
8644 got_commitable_get_status(ct),
8645 got_commitable_get_path(ct));
8649 dprintf(fd, "# detailed changes can be viewed in %s\n",
8653 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content,
8654 initial_content_len, a->prepared_log ? 0 : 1);
8656 free(initial_content);
8659 if (fd != -1 && close(fd) == -1 && err == NULL)
8660 err = got_error_from_errno2("close", a->logmsg_path);
8662 /* Editor is done; we can now apply unveil(2) */
8664 err = apply_unveil(a->repo_path, 0, a->worktree_path);
8672 static const struct got_error *
8673 cmd_commit(int argc, char *argv[])
8675 const struct got_error *error = NULL;
8676 struct got_worktree *worktree = NULL;
8677 struct got_repository *repo = NULL;
8678 char *cwd = NULL, *id_str = NULL;
8679 struct got_object_id *id = NULL;
8680 const char *logmsg = NULL;
8681 char *prepared_logmsg = NULL;
8682 struct collect_commit_logmsg_arg cl_arg;
8683 const char *author = NULL;
8684 char *gitconfig_path = NULL, *editor = NULL, *committer = NULL;
8685 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
8686 int allow_bad_symlinks = 0, non_interactive = 0, merge_in_progress = 0;
8688 struct got_pathlist_head paths;
8689 int *pack_fds = NULL;
8692 cl_arg.logmsg_path = NULL;
8694 while ((ch = getopt(argc, argv, "A:F:m:NnS")) != -1) {
8698 error = valid_author(author);
8704 option_conflict('F', 'm');
8705 prepared_logmsg = realpath(optarg, NULL);
8706 if (prepared_logmsg == NULL)
8707 return got_error_from_errno2("realpath",
8711 if (prepared_logmsg)
8712 option_conflict('m', 'F');
8716 non_interactive = 1;
8722 allow_bad_symlinks = 1;
8734 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8735 "unveil", NULL) == -1)
8738 cwd = getcwd(NULL, 0);
8740 error = got_error_from_errno("getcwd");
8744 error = got_repo_pack_fds_open(&pack_fds);
8748 error = got_worktree_open(&worktree, cwd);
8750 if (error->code == GOT_ERR_NOT_WORKTREE)
8751 error = wrap_not_worktree_error(error, "commit", cwd);
8755 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
8758 if (rebase_in_progress) {
8759 error = got_error(GOT_ERR_REBASING);
8763 error = got_worktree_histedit_in_progress(&histedit_in_progress,
8768 error = get_gitconfig_path(&gitconfig_path);
8771 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8772 gitconfig_path, pack_fds);
8776 error = got_worktree_merge_in_progress(&merge_in_progress, worktree, repo);
8779 if (merge_in_progress) {
8780 error = got_error(GOT_ERR_MERGE_BUSY);
8784 error = get_author(&committer, repo, worktree);
8788 if (author != NULL && !strcmp(committer, author)) {
8789 error = got_error(GOT_ERR_COMMIT_REDUNDANT_AUTHOR);
8797 * unveil(2) traverses exec(2); if an editor is used we have
8798 * to apply unveil after the log message has been written.
8800 if (logmsg == NULL || strlen(logmsg) == 0)
8801 error = get_editor(&editor);
8803 error = apply_unveil(got_repo_get_path(repo), 0,
8804 got_worktree_get_root_path(worktree));
8808 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8812 cl_arg.editor = editor;
8813 cl_arg.cmdline_log = logmsg;
8814 cl_arg.prepared_log = prepared_logmsg;
8815 cl_arg.non_interactive = non_interactive;
8816 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
8817 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
8818 if (!histedit_in_progress) {
8819 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
8820 error = got_error(GOT_ERR_COMMIT_BRANCH);
8823 cl_arg.branch_name += 11;
8825 cl_arg.repo_path = got_repo_get_path(repo);
8826 error = got_worktree_commit(&id, worktree, &paths, author, committer,
8827 allow_bad_symlinks, show_diff, collect_commit_logmsg, &cl_arg,
8828 print_status, NULL, repo);
8830 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
8831 cl_arg.logmsg_path != NULL)
8832 preserve_logmsg = 1;
8836 error = got_object_id_str(&id_str, id);
8839 printf("Created commit %s\n", id_str);
8841 if (preserve_logmsg) {
8842 fprintf(stderr, "%s: log message preserved in %s\n",
8843 getprogname(), cl_arg.logmsg_path);
8844 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
8846 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
8847 free(cl_arg.logmsg_path);
8849 const struct got_error *close_err = got_repo_close(repo);
8854 got_worktree_close(worktree);
8856 const struct got_error *pack_err =
8857 got_repo_pack_fds_close(pack_fds);
8861 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
8864 free(gitconfig_path);
8867 free(prepared_logmsg);
8874 fprintf(stderr, "usage: %s send [-afqTv] [-b branch] [-d branch] "
8875 "[-r repository-path] [-t tag] [remote-repository]\n",
8881 print_load_info(int print_colored, int print_found, int print_trees,
8882 int ncolored, int nfound, int ntrees)
8884 if (print_colored) {
8885 printf("%d commit%s colored", ncolored,
8886 ncolored == 1 ? "" : "s");
8889 printf("%s%d object%s found",
8890 ncolored > 0 ? "; " : "",
8891 nfound, nfound == 1 ? "" : "s");
8894 printf("; %d tree%s scanned", ntrees,
8895 ntrees == 1 ? "" : "s");
8899 struct got_send_progress_arg {
8900 char last_scaled_packsize[FMT_SCALED_STRSIZE];
8907 int last_nobj_total;
8911 int printed_something;
8913 struct got_pathlist_head *delete_branches;
8916 static const struct got_error *
8917 send_progress(void *arg, int ncolored, int nfound, int ntrees,
8918 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
8919 int nobj_written, off_t bytes_sent, const char *refname,
8920 const char *errmsg, int success)
8922 struct got_send_progress_arg *a = arg;
8923 char scaled_packsize[FMT_SCALED_STRSIZE];
8924 char scaled_sent[FMT_SCALED_STRSIZE];
8925 int p_deltify = 0, p_written = 0, p_sent = 0;
8926 int print_colored = 0, print_found = 0, print_trees = 0;
8927 int print_searching = 0, print_total = 0;
8928 int print_deltify = 0, print_written = 0, print_sent = 0;
8930 if (a->verbosity < 0)
8934 const char *status = success ? "accepted" : "rejected";
8937 struct got_pathlist_entry *pe;
8938 TAILQ_FOREACH(pe, a->delete_branches, entry) {
8939 const char *branchname = pe->path;
8940 if (got_path_cmp(branchname, refname,
8941 strlen(branchname), strlen(refname)) == 0) {
8943 a->sent_something = 1;
8949 if (a->printed_something)
8951 printf("Server has %s %s", status, refname);
8953 printf(": %s", errmsg);
8954 a->printed_something = 1;
8958 if (a->last_ncolored != ncolored) {
8960 a->last_ncolored = ncolored;
8963 if (a->last_nfound != nfound) {
8966 a->last_nfound = nfound;
8969 if (a->last_ntrees != ntrees) {
8973 a->last_ntrees = ntrees;
8976 if ((print_colored || print_found || print_trees) &&
8979 print_load_info(print_colored, print_found, print_trees,
8980 ncolored, nfound, ntrees);
8981 a->printed_something = 1;
8984 } else if (!a->loading_done) {
8986 print_load_info(1, 1, 1, ncolored, nfound, ntrees);
8988 a->loading_done = 1;
8991 if (fmt_scaled(packfile_size, scaled_packsize) == -1)
8992 return got_error_from_errno("fmt_scaled");
8993 if (fmt_scaled(bytes_sent, scaled_sent) == -1)
8994 return got_error_from_errno("fmt_scaled");
8996 if (a->last_ncommits != ncommits) {
8997 print_searching = 1;
8998 a->last_ncommits = ncommits;
9001 if (a->last_nobj_total != nobj_total) {
9002 print_searching = 1;
9004 a->last_nobj_total = nobj_total;
9007 if (packfile_size > 0 && (a->last_scaled_packsize[0] == '\0' ||
9008 strcmp(scaled_packsize, a->last_scaled_packsize)) != 0) {
9009 if (strlcpy(a->last_scaled_packsize, scaled_packsize,
9010 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
9011 return got_error(GOT_ERR_NO_SPACE);
9014 if (nobj_deltify > 0 || nobj_written > 0) {
9015 if (nobj_deltify > 0) {
9016 p_deltify = (nobj_deltify * 100) / nobj_total;
9017 if (p_deltify != a->last_p_deltify) {
9018 a->last_p_deltify = p_deltify;
9019 print_searching = 1;
9024 if (nobj_written > 0) {
9025 p_written = (nobj_written * 100) / nobj_total;
9026 if (p_written != a->last_p_written) {
9027 a->last_p_written = p_written;
9028 print_searching = 1;
9036 if (bytes_sent > 0) {
9037 p_sent = (bytes_sent * 100) / packfile_size;
9038 if (p_sent != a->last_p_sent) {
9039 a->last_p_sent = p_sent;
9040 print_searching = 1;
9046 a->sent_something = 1;
9049 if (print_searching || print_total || print_deltify || print_written ||
9052 if (print_searching)
9053 printf("packing %d reference%s", ncommits,
9054 ncommits == 1 ? "" : "s");
9056 printf("; %d object%s", nobj_total,
9057 nobj_total == 1 ? "" : "s");
9059 printf("; deltify: %d%%", p_deltify);
9061 printf("; uploading pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
9062 scaled_packsize, p_sent);
9063 else if (print_written)
9064 printf("; writing pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
9065 scaled_packsize, p_written);
9066 if (print_searching || print_total || print_deltify ||
9067 print_written || print_sent) {
9068 a->printed_something = 1;
9074 static const struct got_error *
9075 cmd_send(int argc, char *argv[])
9077 const struct got_error *error = NULL;
9078 char *cwd = NULL, *repo_path = NULL;
9079 const char *remote_name;
9080 char *proto = NULL, *host = NULL, *port = NULL;
9081 char *repo_name = NULL, *server_path = NULL;
9082 const struct got_remote_repo *remotes, *remote = NULL;
9083 int nremotes, nbranches = 0, ndelete_branches = 0;
9084 struct got_repository *repo = NULL;
9085 struct got_worktree *worktree = NULL;
9086 const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
9087 struct got_pathlist_head branches;
9088 struct got_pathlist_head tags;
9089 struct got_reflist_head all_branches;
9090 struct got_reflist_head all_tags;
9091 struct got_pathlist_head delete_args;
9092 struct got_pathlist_head delete_branches;
9093 struct got_reflist_entry *re;
9094 struct got_pathlist_entry *pe;
9095 int i, ch, sendfd = -1, sendstatus;
9097 struct got_send_progress_arg spa;
9098 int verbosity = 0, overwrite_refs = 0;
9099 int send_all_branches = 0, send_all_tags = 0;
9100 struct got_reference *ref = NULL;
9101 int *pack_fds = NULL;
9103 TAILQ_INIT(&branches);
9105 TAILQ_INIT(&all_branches);
9106 TAILQ_INIT(&all_tags);
9107 TAILQ_INIT(&delete_args);
9108 TAILQ_INIT(&delete_branches);
9110 while ((ch = getopt(argc, argv, "ab:d:fqr:Tt:v")) != -1) {
9113 send_all_branches = 1;
9116 error = got_pathlist_append(&branches, optarg, NULL);
9122 error = got_pathlist_append(&delete_args, optarg, NULL);
9133 repo_path = realpath(optarg, NULL);
9134 if (repo_path == NULL)
9135 return got_error_from_errno2("realpath",
9137 got_path_strip_trailing_slashes(repo_path);
9143 error = got_pathlist_append(&tags, optarg, NULL);
9150 else if (verbosity < 3)
9161 if (send_all_branches && !TAILQ_EMPTY(&branches))
9162 option_conflict('a', 'b');
9163 if (send_all_tags && !TAILQ_EMPTY(&tags))
9164 option_conflict('T', 't');
9168 remote_name = GOT_SEND_DEFAULT_REMOTE_NAME;
9170 remote_name = argv[0];
9174 cwd = getcwd(NULL, 0);
9176 error = got_error_from_errno("getcwd");
9180 error = got_repo_pack_fds_open(&pack_fds);
9184 if (repo_path == NULL) {
9185 error = got_worktree_open(&worktree, cwd);
9186 if (error && error->code != GOT_ERR_NOT_WORKTREE)
9192 strdup(got_worktree_get_repo_path(worktree));
9193 if (repo_path == NULL)
9194 error = got_error_from_errno("strdup");
9198 repo_path = strdup(cwd);
9199 if (repo_path == NULL) {
9200 error = got_error_from_errno("strdup");
9206 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
9211 worktree_conf = got_worktree_get_gotconfig(worktree);
9212 if (worktree_conf) {
9213 got_gotconfig_get_remotes(&nremotes, &remotes,
9215 for (i = 0; i < nremotes; i++) {
9216 if (strcmp(remotes[i].name, remote_name) == 0) {
9217 remote = &remotes[i];
9223 if (remote == NULL) {
9224 repo_conf = got_repo_get_gotconfig(repo);
9226 got_gotconfig_get_remotes(&nremotes, &remotes,
9228 for (i = 0; i < nremotes; i++) {
9229 if (strcmp(remotes[i].name, remote_name) == 0) {
9230 remote = &remotes[i];
9236 if (remote == NULL) {
9237 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
9238 for (i = 0; i < nremotes; i++) {
9239 if (strcmp(remotes[i].name, remote_name) == 0) {
9240 remote = &remotes[i];
9245 if (remote == NULL) {
9246 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
9250 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
9251 &repo_name, remote->send_url);
9255 if (strcmp(proto, "git") == 0) {
9257 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
9258 "sendfd dns inet unveil", NULL) == -1)
9261 } else if (strcmp(proto, "git+ssh") == 0 ||
9262 strcmp(proto, "ssh") == 0) {
9264 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
9265 "sendfd unveil", NULL) == -1)
9268 } else if (strcmp(proto, "http") == 0 ||
9269 strcmp(proto, "git+http") == 0) {
9270 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
9273 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
9277 error = got_dial_apply_unveil(proto);
9281 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
9285 if (send_all_branches) {
9286 error = got_ref_list(&all_branches, repo, "refs/heads",
9287 got_ref_cmp_by_name, NULL);
9290 TAILQ_FOREACH(re, &all_branches, entry) {
9291 const char *branchname = got_ref_get_name(re->ref);
9292 error = got_pathlist_append(&branches,
9298 } else if (nbranches == 0) {
9299 for (i = 0; i < remote->nsend_branches; i++) {
9300 error = got_pathlist_append(&branches,
9301 remote->send_branches[i], NULL);
9307 if (send_all_tags) {
9308 error = got_ref_list(&all_tags, repo, "refs/tags",
9309 got_ref_cmp_by_name, NULL);
9312 TAILQ_FOREACH(re, &all_tags, entry) {
9313 const char *tagname = got_ref_get_name(re->ref);
9314 error = got_pathlist_append(&tags,
9322 * To prevent accidents only branches in refs/heads/ can be deleted
9323 * with 'got send -d'.
9324 * Deleting anything else requires local repository access or Git.
9326 TAILQ_FOREACH(pe, &delete_args, entry) {
9327 const char *branchname = pe->path;
9329 struct got_pathlist_entry *new;
9330 if (strncmp(branchname, "refs/heads/", 11) == 0) {
9331 s = strdup(branchname);
9333 error = got_error_from_errno("strdup");
9337 if (asprintf(&s, "refs/heads/%s", branchname) == -1) {
9338 error = got_error_from_errno("asprintf");
9342 error = got_pathlist_insert(&new, &delete_branches, s, NULL);
9343 if (error || new == NULL /* duplicate */)
9350 if (nbranches == 0 && ndelete_branches == 0) {
9351 struct got_reference *head_ref;
9353 error = got_ref_open(&head_ref, repo,
9354 got_worktree_get_head_ref_name(worktree), 0);
9356 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
9359 if (got_ref_is_symbolic(head_ref)) {
9360 error = got_ref_resolve_symbolic(&ref, repo, head_ref);
9361 got_ref_close(head_ref);
9366 error = got_pathlist_append(&branches, got_ref_get_name(ref),
9373 if (verbosity >= 0) {
9374 printf("Connecting to \"%s\" %s://%s%s%s%s%s\n",
9375 remote->name, proto, host,
9376 port ? ":" : "", port ? port : "",
9377 *server_path == '/' ? "" : "/", server_path);
9380 error = got_send_connect(&sendpid, &sendfd, proto, host, port,
9381 server_path, verbosity);
9385 memset(&spa, 0, sizeof(spa));
9386 spa.last_scaled_packsize[0] = '\0';
9387 spa.last_p_deltify = -1;
9388 spa.last_p_written = -1;
9389 spa.verbosity = verbosity;
9390 spa.delete_branches = &delete_branches;
9391 error = got_send_pack(remote_name, &branches, &tags, &delete_branches,
9392 verbosity, overwrite_refs, sendfd, repo, send_progress, &spa,
9393 check_cancelled, NULL);
9394 if (spa.printed_something)
9398 if (!spa.sent_something && verbosity >= 0)
9399 printf("Already up-to-date\n");
9402 if (kill(sendpid, SIGTERM) == -1)
9403 error = got_error_from_errno("kill");
9404 if (waitpid(sendpid, &sendstatus, 0) == -1 && error == NULL)
9405 error = got_error_from_errno("waitpid");
9407 if (sendfd != -1 && close(sendfd) == -1 && error == NULL)
9408 error = got_error_from_errno("close");
9410 const struct got_error *close_err = got_repo_close(repo);
9415 got_worktree_close(worktree);
9417 const struct got_error *pack_err =
9418 got_repo_pack_fds_close(pack_fds);
9424 got_pathlist_free(&branches, GOT_PATHLIST_FREE_NONE);
9425 got_pathlist_free(&tags, GOT_PATHLIST_FREE_NONE);
9426 got_ref_list_free(&all_branches);
9427 got_ref_list_free(&all_tags);
9428 got_pathlist_free(&delete_args, GOT_PATHLIST_FREE_NONE);
9429 got_pathlist_free(&delete_branches, GOT_PATHLIST_FREE_PATH);
9441 usage_cherrypick(void)
9443 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
9447 static const struct got_error *
9448 cmd_cherrypick(int argc, char *argv[])
9450 const struct got_error *error = NULL;
9451 struct got_worktree *worktree = NULL;
9452 struct got_repository *repo = NULL;
9453 char *cwd = NULL, *commit_id_str = NULL;
9454 struct got_object_id *commit_id = NULL;
9455 struct got_commit_object *commit = NULL;
9456 struct got_object_qid *pid;
9458 struct got_update_progress_arg upa;
9459 int *pack_fds = NULL;
9461 while ((ch = getopt(argc, argv, "")) != -1) {
9473 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9474 "unveil", NULL) == -1)
9480 cwd = getcwd(NULL, 0);
9482 error = got_error_from_errno("getcwd");
9486 error = got_repo_pack_fds_open(&pack_fds);
9490 error = got_worktree_open(&worktree, cwd);
9492 if (error->code == GOT_ERR_NOT_WORKTREE)
9493 error = wrap_not_worktree_error(error, "cherrypick",
9498 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9503 error = apply_unveil(got_repo_get_path(repo), 0,
9504 got_worktree_get_root_path(worktree));
9508 error = got_repo_match_object_id(&commit_id, NULL, argv[0],
9509 GOT_OBJ_TYPE_COMMIT, NULL, repo);
9512 error = got_object_id_str(&commit_id_str, commit_id);
9516 error = got_object_open_as_commit(&commit, repo, commit_id);
9519 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
9520 memset(&upa, 0, sizeof(upa));
9521 error = got_worktree_merge_files(worktree, pid ? &pid->id : NULL,
9522 commit_id, repo, update_progress, &upa, check_cancelled,
9527 if (upa.did_something)
9528 printf("Merged commit %s\n", commit_id_str);
9529 print_merge_progress_stats(&upa);
9532 got_object_commit_close(commit);
9533 free(commit_id_str);
9535 got_worktree_close(worktree);
9537 const struct got_error *close_err = got_repo_close(repo);
9542 const struct got_error *pack_err =
9543 got_repo_pack_fds_close(pack_fds);
9554 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
9558 static const struct got_error *
9559 cmd_backout(int argc, char *argv[])
9561 const struct got_error *error = NULL;
9562 struct got_worktree *worktree = NULL;
9563 struct got_repository *repo = NULL;
9564 char *cwd = NULL, *commit_id_str = NULL;
9565 struct got_object_id *commit_id = NULL;
9566 struct got_commit_object *commit = NULL;
9567 struct got_object_qid *pid;
9569 struct got_update_progress_arg upa;
9570 int *pack_fds = NULL;
9572 while ((ch = getopt(argc, argv, "")) != -1) {
9584 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9585 "unveil", NULL) == -1)
9591 cwd = getcwd(NULL, 0);
9593 error = got_error_from_errno("getcwd");
9597 error = got_repo_pack_fds_open(&pack_fds);
9601 error = got_worktree_open(&worktree, cwd);
9603 if (error->code == GOT_ERR_NOT_WORKTREE)
9604 error = wrap_not_worktree_error(error, "backout", cwd);
9608 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9613 error = apply_unveil(got_repo_get_path(repo), 0,
9614 got_worktree_get_root_path(worktree));
9618 error = got_repo_match_object_id(&commit_id, NULL, argv[0],
9619 GOT_OBJ_TYPE_COMMIT, NULL, repo);
9622 error = got_object_id_str(&commit_id_str, commit_id);
9626 error = got_object_open_as_commit(&commit, repo, commit_id);
9629 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
9631 error = got_error(GOT_ERR_ROOT_COMMIT);
9635 memset(&upa, 0, sizeof(upa));
9636 error = got_worktree_merge_files(worktree, commit_id, &pid->id,
9637 repo, update_progress, &upa, check_cancelled, NULL);
9641 if (upa.did_something)
9642 printf("Backed out commit %s\n", commit_id_str);
9643 print_merge_progress_stats(&upa);
9646 got_object_commit_close(commit);
9647 free(commit_id_str);
9649 got_worktree_close(worktree);
9651 const struct got_error *close_err = got_repo_close(repo);
9656 const struct got_error *pack_err =
9657 got_repo_pack_fds_close(pack_fds);
9667 fprintf(stderr, "usage: %s rebase [-aclX] [branch]\n", getprogname());
9672 trim_logmsg(char *logmsg, int limit)
9677 len = strlen(logmsg);
9681 nl = strchr(logmsg, '\n');
9686 static const struct got_error *
9687 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
9689 const struct got_error *err;
9690 char *logmsg0 = NULL;
9693 err = got_object_commit_get_logmsg(&logmsg0, commit);
9698 while (isspace((unsigned char)s[0]))
9701 *logmsg = strdup(s);
9702 if (*logmsg == NULL) {
9703 err = got_error_from_errno("strdup");
9707 trim_logmsg(*logmsg, limit);
9713 static const struct got_error *
9714 show_rebase_merge_conflict(struct got_object_id *id,
9715 struct got_repository *repo)
9717 const struct got_error *err;
9718 struct got_commit_object *commit = NULL;
9719 char *id_str = NULL, *logmsg = NULL;
9721 err = got_object_open_as_commit(&commit, repo, id);
9725 err = got_object_id_str(&id_str, id);
9731 err = get_short_logmsg(&logmsg, 42, commit);
9735 printf("%s -> merge conflict: %s\n", id_str, logmsg);
9738 got_object_commit_close(commit);
9743 static const struct got_error *
9744 show_rebase_progress(struct got_commit_object *commit,
9745 struct got_object_id *old_id, struct got_object_id *new_id)
9747 const struct got_error *err;
9748 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
9750 err = got_object_id_str(&old_id_str, old_id);
9755 err = got_object_id_str(&new_id_str, new_id);
9760 old_id_str[12] = '\0';
9762 new_id_str[12] = '\0';
9764 err = get_short_logmsg(&logmsg, 42, commit);
9768 printf("%s -> %s: %s\n", old_id_str,
9769 new_id_str ? new_id_str : "no-op change", logmsg);
9777 static const struct got_error *
9778 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
9779 struct got_reference *branch, struct got_reference *new_base_branch,
9780 struct got_reference *tmp_branch, struct got_repository *repo,
9783 printf("Switching work tree to %s\n", got_ref_get_name(branch));
9784 return got_worktree_rebase_complete(worktree, fileindex,
9785 new_base_branch, tmp_branch, branch, repo, create_backup);
9788 static const struct got_error *
9789 rebase_commit(struct got_pathlist_head *merged_paths,
9790 struct got_worktree *worktree, struct got_fileindex *fileindex,
9791 struct got_reference *tmp_branch, const char *committer,
9792 struct got_object_id *commit_id, struct got_repository *repo)
9794 const struct got_error *error;
9795 struct got_commit_object *commit;
9796 struct got_object_id *new_commit_id;
9798 error = got_object_open_as_commit(&commit, repo, commit_id);
9802 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
9803 worktree, fileindex, tmp_branch, committer, commit, commit_id,
9806 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
9808 error = show_rebase_progress(commit, commit_id, NULL);
9810 error = show_rebase_progress(commit, commit_id, new_commit_id);
9811 free(new_commit_id);
9814 got_object_commit_close(commit);
9818 struct check_path_prefix_arg {
9819 const char *path_prefix;
9824 static const struct got_error *
9825 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
9826 struct got_blob_object *blob2, FILE *f1, FILE *f2,
9827 struct got_object_id *id1, struct got_object_id *id2,
9828 const char *path1, const char *path2,
9829 mode_t mode1, mode_t mode2, struct got_repository *repo)
9831 struct check_path_prefix_arg *a = arg;
9833 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
9834 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
9835 return got_error(a->errcode);
9840 static const struct got_error *
9841 check_path_prefix(struct got_object_id *parent_id,
9842 struct got_object_id *commit_id, const char *path_prefix,
9843 int errcode, struct got_repository *repo)
9845 const struct got_error *err;
9846 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
9847 struct got_commit_object *commit = NULL, *parent_commit = NULL;
9848 struct check_path_prefix_arg cpp_arg;
9850 if (got_path_is_root_dir(path_prefix))
9853 err = got_object_open_as_commit(&commit, repo, commit_id);
9857 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
9861 err = got_object_open_as_tree(&tree1, repo,
9862 got_object_commit_get_tree_id(parent_commit));
9866 err = got_object_open_as_tree(&tree2, repo,
9867 got_object_commit_get_tree_id(commit));
9871 cpp_arg.path_prefix = path_prefix;
9872 while (cpp_arg.path_prefix[0] == '/')
9873 cpp_arg.path_prefix++;
9874 cpp_arg.len = strlen(cpp_arg.path_prefix);
9875 cpp_arg.errcode = errcode;
9876 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
9877 check_path_prefix_in_diff, &cpp_arg, 0);
9880 got_object_tree_close(tree1);
9882 got_object_tree_close(tree2);
9884 got_object_commit_close(commit);
9886 got_object_commit_close(parent_commit);
9890 static const struct got_error *
9891 collect_commits(struct got_object_id_queue *commits,
9892 struct got_object_id *initial_commit_id,
9893 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
9894 const char *path_prefix, int path_prefix_errcode,
9895 struct got_repository *repo)
9897 const struct got_error *err = NULL;
9898 struct got_commit_graph *graph = NULL;
9899 struct got_object_id parent_id, commit_id;
9900 struct got_object_qid *qid;
9902 err = got_commit_graph_open(&graph, "/", 1);
9906 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
9907 check_cancelled, NULL);
9911 memcpy(&commit_id, initial_commit_id, sizeof(commit_id));
9912 while (got_object_id_cmp(&commit_id, iter_stop_id) != 0) {
9913 err = got_commit_graph_iter_next(&parent_id, graph, repo,
9914 check_cancelled, NULL);
9916 if (err->code == GOT_ERR_ITER_COMPLETED) {
9917 err = got_error_msg(GOT_ERR_ANCESTRY,
9918 "ran out of commits to rebase before "
9919 "youngest common ancestor commit has "
9924 err = check_path_prefix(&parent_id, &commit_id,
9925 path_prefix, path_prefix_errcode, repo);
9929 err = got_object_qid_alloc(&qid, &commit_id);
9932 STAILQ_INSERT_HEAD(commits, qid, entry);
9934 memcpy(&commit_id, &parent_id, sizeof(commit_id));
9938 got_commit_graph_close(graph);
9942 static const struct got_error *
9943 get_commit_brief_str(char **brief_str, struct got_commit_object *commit)
9945 const struct got_error *err = NULL;
9946 time_t committer_time;
9948 char datebuf[11]; /* YYYY-MM-DD + NUL */
9949 char *author0 = NULL, *author, *smallerthan;
9950 char *logmsg0 = NULL, *logmsg, *newline;
9952 committer_time = got_object_commit_get_committer_time(commit);
9953 if (gmtime_r(&committer_time, &tm) == NULL)
9954 return got_error_from_errno("gmtime_r");
9955 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d", &tm) == 0)
9956 return got_error(GOT_ERR_NO_SPACE);
9958 author0 = strdup(got_object_commit_get_author(commit));
9959 if (author0 == NULL)
9960 return got_error_from_errno("strdup");
9962 smallerthan = strchr(author, '<');
9963 if (smallerthan && smallerthan[1] != '\0')
9964 author = smallerthan + 1;
9965 author[strcspn(author, "@>")] = '\0';
9967 err = got_object_commit_get_logmsg(&logmsg0, commit);
9971 while (*logmsg == '\n')
9973 newline = strchr(logmsg, '\n');
9977 if (asprintf(brief_str, "%s %s %s",
9978 datebuf, author, logmsg) == -1)
9979 err = got_error_from_errno("asprintf");
9986 static const struct got_error *
9987 delete_backup_ref(struct got_reference *ref, struct got_object_id *id,
9988 struct got_repository *repo)
9990 const struct got_error *err;
9993 err = got_object_id_str(&id_str, id);
9997 err = got_ref_delete(ref, repo);
10001 printf("Deleted %s: %s\n", got_ref_get_name(ref), id_str);
10007 static const struct got_error *
10008 print_backup_ref(const char *branch_name, const char *new_id_str,
10009 struct got_object_id *old_commit_id, struct got_commit_object *old_commit,
10010 struct got_reflist_object_id_map *refs_idmap,
10011 struct got_repository *repo)
10013 const struct got_error *err = NULL;
10014 struct got_reflist_head *refs;
10015 char *refs_str = NULL;
10016 struct got_object_id *new_commit_id = NULL;
10017 struct got_commit_object *new_commit = NULL;
10018 char *new_commit_brief_str = NULL;
10019 struct got_object_id *yca_id = NULL;
10020 struct got_commit_object *yca_commit = NULL;
10021 char *yca_id_str = NULL, *yca_brief_str = NULL;
10022 char *custom_refs_str;
10024 if (asprintf(&custom_refs_str, "formerly %s", branch_name) == -1)
10025 return got_error_from_errno("asprintf");
10027 err = print_commit(old_commit, old_commit_id, repo, NULL, NULL, NULL,
10028 0, 0, refs_idmap, custom_refs_str);
10032 err = got_object_resolve_id_str(&new_commit_id, repo, new_id_str);
10036 refs = got_reflist_object_id_map_lookup(refs_idmap, new_commit_id);
10038 err = build_refs_str(&refs_str, refs, new_commit_id, repo, 0);
10043 err = got_object_open_as_commit(&new_commit, repo, new_commit_id);
10047 err = get_commit_brief_str(&new_commit_brief_str, new_commit);
10051 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
10052 old_commit_id, new_commit_id, 1, repo, check_cancelled, NULL);
10056 printf("has become commit %s%s%s%s\n %s\n", new_id_str,
10057 refs_str ? " (" : "", refs_str ? refs_str : "",
10058 refs_str ? ")" : "", new_commit_brief_str);
10059 if (yca_id && got_object_id_cmp(yca_id, new_commit_id) != 0 &&
10060 got_object_id_cmp(yca_id, old_commit_id) != 0) {
10064 err = got_object_open_as_commit(&yca_commit, repo, yca_id);
10068 err = get_commit_brief_str(&yca_brief_str, yca_commit);
10072 err = got_object_id_str(&yca_id_str, yca_id);
10076 refs = got_reflist_object_id_map_lookup(refs_idmap, yca_id);
10078 err = build_refs_str(&refs_str, refs, yca_id, repo, 0);
10082 printf("history forked at %s%s%s%s\n %s\n",
10084 refs_str ? " (" : "", refs_str ? refs_str : "",
10085 refs_str ? ")" : "", yca_brief_str);
10088 free(custom_refs_str);
10089 free(new_commit_id);
10093 free(yca_brief_str);
10095 got_object_commit_close(new_commit);
10097 got_object_commit_close(yca_commit);
10102 static const struct got_error *
10103 process_backup_refs(const char *backup_ref_prefix,
10104 const char *wanted_branch_name,
10105 int delete, struct got_repository *repo)
10107 const struct got_error *err;
10108 struct got_reflist_head refs, backup_refs;
10109 struct got_reflist_entry *re;
10110 const size_t backup_ref_prefix_len = strlen(backup_ref_prefix);
10111 struct got_object_id *old_commit_id = NULL;
10112 char *branch_name = NULL;
10113 struct got_commit_object *old_commit = NULL;
10114 struct got_reflist_object_id_map *refs_idmap = NULL;
10115 int wanted_branch_found = 0;
10118 TAILQ_INIT(&backup_refs);
10120 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
10124 err = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
10128 if (wanted_branch_name) {
10129 if (strncmp(wanted_branch_name, "refs/heads/", 11) == 0)
10130 wanted_branch_name += 11;
10133 err = got_ref_list(&backup_refs, repo, backup_ref_prefix,
10134 got_ref_cmp_by_commit_timestamp_descending, repo);
10138 TAILQ_FOREACH(re, &backup_refs, entry) {
10139 const char *refname = got_ref_get_name(re->ref);
10142 err = check_cancelled(NULL);
10146 err = got_ref_resolve(&old_commit_id, repo, re->ref);
10150 err = got_object_open_as_commit(&old_commit, repo,
10155 if (strncmp(backup_ref_prefix, refname,
10156 backup_ref_prefix_len) == 0)
10157 refname += backup_ref_prefix_len;
10159 while (refname[0] == '/')
10162 branch_name = strdup(refname);
10163 if (branch_name == NULL) {
10164 err = got_error_from_errno("strdup");
10167 slash = strrchr(branch_name, '/');
10170 refname += strlen(branch_name) + 1;
10173 if (wanted_branch_name == NULL ||
10174 strcmp(wanted_branch_name, branch_name) == 0) {
10175 wanted_branch_found = 1;
10177 err = delete_backup_ref(re->ref,
10178 old_commit_id, repo);
10180 err = print_backup_ref(branch_name, refname,
10181 old_commit_id, old_commit, refs_idmap,
10188 free(old_commit_id);
10189 old_commit_id = NULL;
10191 branch_name = NULL;
10192 got_object_commit_close(old_commit);
10196 if (wanted_branch_name && !wanted_branch_found) {
10197 err = got_error_fmt(GOT_ERR_NOT_REF,
10198 "%s/%s/", backup_ref_prefix, wanted_branch_name);
10202 got_reflist_object_id_map_free(refs_idmap);
10203 got_ref_list_free(&refs);
10204 got_ref_list_free(&backup_refs);
10205 free(old_commit_id);
10208 got_object_commit_close(old_commit);
10212 static const struct got_error *
10213 abort_progress(void *arg, unsigned char status, const char *path)
10216 * Unversioned files should not clutter progress output when
10217 * an operation is aborted.
10219 if (status == GOT_STATUS_UNVERSIONED)
10222 return update_progress(arg, status, path);
10225 static const struct got_error *
10226 cmd_rebase(int argc, char *argv[])
10228 const struct got_error *error = NULL;
10229 struct got_worktree *worktree = NULL;
10230 struct got_repository *repo = NULL;
10231 struct got_fileindex *fileindex = NULL;
10232 char *cwd = NULL, *committer = NULL, *gitconfig_path = NULL;
10233 struct got_reference *branch = NULL;
10234 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
10235 struct got_object_id *commit_id = NULL, *parent_id = NULL;
10236 struct got_object_id *resume_commit_id = NULL;
10237 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
10238 struct got_object_id *head_commit_id = NULL;
10239 struct got_reference *head_ref = NULL;
10240 struct got_commit_object *commit = NULL;
10241 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
10242 int histedit_in_progress = 0, merge_in_progress = 0;
10243 int create_backup = 1, list_backups = 0, delete_backups = 0;
10244 struct got_object_id_queue commits;
10245 struct got_pathlist_head merged_paths;
10246 const struct got_object_id_queue *parent_ids;
10247 struct got_object_qid *qid, *pid;
10248 struct got_update_progress_arg upa;
10249 int *pack_fds = NULL;
10251 STAILQ_INIT(&commits);
10252 TAILQ_INIT(&merged_paths);
10253 memset(&upa, 0, sizeof(upa));
10255 while ((ch = getopt(argc, argv, "aclX")) != -1) {
10261 continue_rebase = 1;
10267 delete_backups = 1;
10279 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10280 "unveil", NULL) == -1)
10283 if (list_backups) {
10285 option_conflict('l', 'a');
10286 if (continue_rebase)
10287 option_conflict('l', 'c');
10288 if (delete_backups)
10289 option_conflict('l', 'X');
10290 if (argc != 0 && argc != 1)
10292 } else if (delete_backups) {
10294 option_conflict('X', 'a');
10295 if (continue_rebase)
10296 option_conflict('X', 'c');
10298 option_conflict('l', 'X');
10299 if (argc != 0 && argc != 1)
10302 if (abort_rebase && continue_rebase)
10304 else if (abort_rebase || continue_rebase) {
10307 } else if (argc != 1)
10311 cwd = getcwd(NULL, 0);
10313 error = got_error_from_errno("getcwd");
10317 error = got_repo_pack_fds_open(&pack_fds);
10321 error = got_worktree_open(&worktree, cwd);
10323 if (list_backups || delete_backups) {
10324 if (error->code != GOT_ERR_NOT_WORKTREE)
10327 if (error->code == GOT_ERR_NOT_WORKTREE)
10328 error = wrap_not_worktree_error(error,
10334 error = get_gitconfig_path(&gitconfig_path);
10337 error = got_repo_open(&repo,
10338 worktree ? got_worktree_get_repo_path(worktree) : cwd,
10339 gitconfig_path, pack_fds);
10343 error = get_author(&committer, repo, worktree);
10344 if (error && error->code != GOT_ERR_COMMIT_NO_AUTHOR)
10347 error = apply_unveil(got_repo_get_path(repo), 0,
10348 worktree ? got_worktree_get_root_path(worktree) : NULL);
10352 if (list_backups || delete_backups) {
10353 error = process_backup_refs(
10354 GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
10355 argc == 1 ? argv[0] : NULL, delete_backups, repo);
10356 goto done; /* nothing else to do */
10359 error = got_worktree_histedit_in_progress(&histedit_in_progress,
10363 if (histedit_in_progress) {
10364 error = got_error(GOT_ERR_HISTEDIT_BUSY);
10368 error = got_worktree_merge_in_progress(&merge_in_progress,
10372 if (merge_in_progress) {
10373 error = got_error(GOT_ERR_MERGE_BUSY);
10377 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
10381 if (abort_rebase) {
10382 if (!rebase_in_progress) {
10383 error = got_error(GOT_ERR_NOT_REBASING);
10386 error = got_worktree_rebase_continue(&resume_commit_id,
10387 &new_base_branch, &tmp_branch, &branch, &fileindex,
10391 printf("Switching work tree to %s\n",
10392 got_ref_get_symref_target(new_base_branch));
10393 error = got_worktree_rebase_abort(worktree, fileindex, repo,
10394 new_base_branch, abort_progress, &upa);
10397 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
10398 print_merge_progress_stats(&upa);
10399 goto done; /* nothing else to do */
10402 if (continue_rebase) {
10403 if (!rebase_in_progress) {
10404 error = got_error(GOT_ERR_NOT_REBASING);
10407 error = got_worktree_rebase_continue(&resume_commit_id,
10408 &new_base_branch, &tmp_branch, &branch, &fileindex,
10413 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
10414 committer, resume_commit_id, repo);
10418 yca_id = got_object_id_dup(resume_commit_id);
10419 if (yca_id == NULL) {
10420 error = got_error_from_errno("got_object_id_dup");
10424 error = got_ref_open(&branch, repo, argv[0], 0);
10427 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
10428 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
10429 "will not rebase a branch which lives outside "
10430 "the \"refs/heads/\" reference namespace");
10435 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
10439 if (!continue_rebase) {
10440 struct got_object_id *base_commit_id;
10442 error = got_ref_open(&head_ref, repo,
10443 got_worktree_get_head_ref_name(worktree), 0);
10446 error = got_ref_resolve(&head_commit_id, repo, head_ref);
10449 base_commit_id = got_worktree_get_base_commit_id(worktree);
10450 if (got_object_id_cmp(base_commit_id, head_commit_id) != 0) {
10451 error = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
10455 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
10456 base_commit_id, branch_head_commit_id, 1, repo,
10457 check_cancelled, NULL);
10460 if (yca_id == NULL) {
10461 error = got_error_msg(GOT_ERR_ANCESTRY,
10462 "specified branch shares no common ancestry "
10463 "with work tree's branch");
10467 error = check_same_branch(base_commit_id, branch, yca_id, repo);
10469 if (error->code != GOT_ERR_ANCESTRY)
10473 struct got_pathlist_head paths;
10474 printf("%s is already based on %s\n",
10475 got_ref_get_name(branch),
10476 got_worktree_get_head_ref_name(worktree));
10477 error = switch_head_ref(branch, branch_head_commit_id,
10481 error = got_worktree_set_base_commit_id(worktree, repo,
10482 branch_head_commit_id);
10485 TAILQ_INIT(&paths);
10486 error = got_pathlist_append(&paths, "", NULL);
10489 error = got_worktree_checkout_files(worktree,
10490 &paths, repo, update_progress, &upa,
10491 check_cancelled, NULL);
10492 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
10495 if (upa.did_something) {
10497 error = got_object_id_str(&id_str,
10498 branch_head_commit_id);
10501 printf("Updated to %s: %s\n",
10502 got_worktree_get_head_ref_name(worktree),
10506 printf("Already up-to-date\n");
10507 print_update_progress_stats(&upa);
10512 commit_id = branch_head_commit_id;
10513 error = got_object_open_as_commit(&commit, repo, commit_id);
10517 parent_ids = got_object_commit_get_parent_ids(commit);
10518 pid = STAILQ_FIRST(parent_ids);
10520 error = got_error(GOT_ERR_EMPTY_REBASE);
10523 error = collect_commits(&commits, commit_id, &pid->id,
10524 yca_id, got_worktree_get_path_prefix(worktree),
10525 GOT_ERR_REBASE_PATH, repo);
10526 got_object_commit_close(commit);
10531 if (!continue_rebase) {
10532 error = got_worktree_rebase_prepare(&new_base_branch,
10533 &tmp_branch, &fileindex, worktree, branch, repo);
10538 if (STAILQ_EMPTY(&commits)) {
10539 if (continue_rebase) {
10540 error = rebase_complete(worktree, fileindex,
10541 branch, new_base_branch, tmp_branch, repo,
10545 /* Fast-forward the reference of the branch. */
10546 struct got_object_id *new_head_commit_id;
10548 error = got_ref_resolve(&new_head_commit_id, repo,
10552 error = got_object_id_str(&id_str, new_head_commit_id);
10555 printf("Forwarding %s to commit %s\n",
10556 got_ref_get_name(branch), id_str);
10558 error = got_ref_change_ref(branch,
10559 new_head_commit_id);
10562 /* No backup needed since objects did not change. */
10568 STAILQ_FOREACH(qid, &commits, entry) {
10570 commit_id = &qid->id;
10571 parent_id = pid ? &pid->id : yca_id;
10574 memset(&upa, 0, sizeof(upa));
10575 error = got_worktree_rebase_merge_files(&merged_paths,
10576 worktree, fileindex, parent_id, commit_id, repo,
10577 update_progress, &upa, check_cancelled, NULL);
10581 print_merge_progress_stats(&upa);
10582 if (upa.conflicts > 0 || upa.missing > 0 ||
10583 upa.not_deleted > 0 || upa.unversioned > 0) {
10584 if (upa.conflicts > 0) {
10585 error = show_rebase_merge_conflict(&qid->id,
10590 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
10594 error = rebase_commit(&merged_paths, worktree, fileindex,
10595 tmp_branch, committer, commit_id, repo);
10596 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
10601 if (upa.conflicts > 0 || upa.missing > 0 ||
10602 upa.not_deleted > 0 || upa.unversioned > 0) {
10603 error = got_worktree_rebase_postpone(worktree, fileindex);
10606 if (upa.conflicts > 0 && upa.missing == 0 &&
10607 upa.not_deleted == 0 && upa.unversioned == 0) {
10608 error = got_error_msg(GOT_ERR_CONFLICTS,
10609 "conflicts must be resolved before rebasing "
10611 } else if (upa.conflicts > 0) {
10612 error = got_error_msg(GOT_ERR_CONFLICTS,
10613 "conflicts must be resolved before rebasing "
10614 "can continue; changes destined for some "
10615 "files were not yet merged and should be "
10616 "merged manually if required before the "
10617 "rebase operation is continued");
10619 error = got_error_msg(GOT_ERR_CONFLICTS,
10620 "changes destined for some files were not "
10621 "yet merged and should be merged manually "
10622 "if required before the rebase operation "
10626 error = rebase_complete(worktree, fileindex, branch,
10627 new_base_branch, tmp_branch, repo, create_backup);
10631 free(gitconfig_path);
10632 got_object_id_queue_free(&commits);
10633 free(branch_head_commit_id);
10634 free(resume_commit_id);
10635 free(head_commit_id);
10638 got_object_commit_close(commit);
10640 got_ref_close(branch);
10641 if (new_base_branch)
10642 got_ref_close(new_base_branch);
10644 got_ref_close(tmp_branch);
10646 got_ref_close(head_ref);
10648 got_worktree_close(worktree);
10650 const struct got_error *close_err = got_repo_close(repo);
10655 const struct got_error *pack_err =
10656 got_repo_pack_fds_close(pack_fds);
10664 usage_histedit(void)
10666 fprintf(stderr, "usage: %s histedit [-aceflmX] [-F histedit-script] "
10667 "[branch]\n", getprogname());
10671 #define GOT_HISTEDIT_PICK 'p'
10672 #define GOT_HISTEDIT_EDIT 'e'
10673 #define GOT_HISTEDIT_FOLD 'f'
10674 #define GOT_HISTEDIT_DROP 'd'
10675 #define GOT_HISTEDIT_MESG 'm'
10677 static const struct got_histedit_cmd {
10678 unsigned char code;
10681 } got_histedit_cmds[] = {
10682 { GOT_HISTEDIT_PICK, "pick", "use commit" },
10683 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
10684 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
10686 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
10687 { GOT_HISTEDIT_MESG, "mesg",
10688 "single-line log message for commit above (open editor if empty)" },
10691 struct got_histedit_list_entry {
10692 TAILQ_ENTRY(got_histedit_list_entry) entry;
10693 struct got_object_id *commit_id;
10694 const struct got_histedit_cmd *cmd;
10697 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
10699 static const struct got_error *
10700 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
10701 FILE *f, struct got_repository *repo)
10703 const struct got_error *err = NULL;
10704 char *logmsg = NULL, *id_str = NULL;
10705 struct got_commit_object *commit = NULL;
10708 err = got_object_open_as_commit(&commit, repo, commit_id);
10712 err = get_short_logmsg(&logmsg, 34, commit);
10716 err = got_object_id_str(&id_str, commit_id);
10720 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
10722 err = got_ferror(f, GOT_ERR_IO);
10725 got_object_commit_close(commit);
10731 static const struct got_error *
10732 histedit_write_commit_list(struct got_object_id_queue *commits,
10733 FILE *f, int edit_logmsg_only, int fold_only, int edit_only,
10734 struct got_repository *repo)
10736 const struct got_error *err = NULL;
10737 struct got_object_qid *qid;
10738 const char *histedit_cmd = NULL;
10740 if (STAILQ_EMPTY(commits))
10741 return got_error(GOT_ERR_EMPTY_HISTEDIT);
10743 STAILQ_FOREACH(qid, commits, entry) {
10744 histedit_cmd = got_histedit_cmds[0].name;
10746 histedit_cmd = "edit";
10747 else if (fold_only && STAILQ_NEXT(qid, entry) != NULL)
10748 histedit_cmd = "fold";
10749 err = histedit_write_commit(&qid->id, histedit_cmd, f, repo);
10752 if (edit_logmsg_only) {
10753 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
10755 err = got_ferror(f, GOT_ERR_IO);
10764 static const struct got_error *
10765 write_cmd_list(FILE *f, const char *branch_name,
10766 struct got_object_id_queue *commits)
10768 const struct got_error *err = NULL;
10772 struct got_object_qid *qid;
10774 qid = STAILQ_FIRST(commits);
10775 err = got_object_id_str(&id_str, &qid->id);
10780 "# Editing the history of branch '%s' starting at\n"
10782 "# Commits will be processed in order from top to "
10783 "bottom of this file.\n", branch_name, id_str);
10785 err = got_ferror(f, GOT_ERR_IO);
10789 n = fprintf(f, "# Available histedit commands:\n");
10791 err = got_ferror(f, GOT_ERR_IO);
10795 for (i = 0; i < nitems(got_histedit_cmds); i++) {
10796 const struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
10797 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
10800 err = got_ferror(f, GOT_ERR_IO);
10809 static const struct got_error *
10810 histedit_syntax_error(int lineno)
10812 static char msg[42];
10815 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
10817 if (ret < 0 || (size_t)ret >= sizeof(msg))
10818 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
10820 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
10823 static const struct got_error *
10824 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
10825 char *logmsg, struct got_repository *repo)
10827 const struct got_error *err;
10828 struct got_commit_object *folded_commit = NULL;
10829 char *id_str, *folded_logmsg = NULL;
10831 err = got_object_id_str(&id_str, hle->commit_id);
10835 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
10839 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
10842 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
10843 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
10844 folded_logmsg) == -1) {
10845 err = got_error_from_errno("asprintf");
10849 got_object_commit_close(folded_commit);
10851 free(folded_logmsg);
10855 static struct got_histedit_list_entry *
10856 get_folded_commits(struct got_histedit_list_entry *hle)
10858 struct got_histedit_list_entry *prev, *folded = NULL;
10860 prev = TAILQ_PREV(hle, got_histedit_list, entry);
10861 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
10862 prev->cmd->code == GOT_HISTEDIT_DROP)) {
10863 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
10865 prev = TAILQ_PREV(prev, got_histedit_list, entry);
10871 static const struct got_error *
10872 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
10873 struct got_repository *repo)
10875 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
10876 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
10877 const struct got_error *err = NULL;
10878 struct got_commit_object *commit = NULL;
10881 struct got_histedit_list_entry *folded = NULL;
10883 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
10887 folded = get_folded_commits(hle);
10889 while (folded != hle) {
10890 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
10891 folded = TAILQ_NEXT(folded, entry);
10894 err = append_folded_commit_msg(&new_msg, folded,
10900 folded = TAILQ_NEXT(folded, entry);
10904 err = got_object_id_str(&id_str, hle->commit_id);
10907 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
10910 logmsg_len = asprintf(&new_msg,
10911 "%s\n# original log message of commit %s: %s",
10912 logmsg ? logmsg : "", id_str, orig_logmsg);
10913 if (logmsg_len == -1) {
10914 err = got_error_from_errno("asprintf");
10920 err = got_object_id_str(&id_str, hle->commit_id);
10924 err = got_opentemp_named_fd(&logmsg_path, &fd,
10925 GOT_TMPDIR_STR "/got-logmsg", "");
10929 write(fd, logmsg, logmsg_len);
10932 err = get_editor(&editor);
10936 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg,
10939 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
10942 hle->logmsg = strdup(new_msg);
10943 if (hle->logmsg == NULL)
10944 err = got_error_from_errno("strdup");
10947 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
10948 err = got_error_from_errno2("unlink", logmsg_path);
10954 got_object_commit_close(commit);
10958 static const struct got_error *
10959 histedit_parse_list(struct got_histedit_list *histedit_cmds,
10960 FILE *f, struct got_repository *repo)
10962 const struct got_error *err = NULL;
10963 char *line = NULL, *p, *end;
10966 int lineno = 0, lastcmd = -1;
10967 const struct got_histedit_cmd *cmd;
10968 struct got_object_id *commit_id = NULL;
10969 struct got_histedit_list_entry *hle = NULL;
10972 len = getline(&line, &size, f);
10974 const struct got_error *getline_err;
10977 getline_err = got_error_from_errno("getline");
10978 err = got_ferror(f, getline_err->code);
10983 while (isspace((unsigned char)p[0]))
10985 if (p[0] == '#' || p[0] == '\0') {
10991 for (i = 0; i < nitems(got_histedit_cmds); i++) {
10992 cmd = &got_histedit_cmds[i];
10993 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
10994 isspace((unsigned char)p[strlen(cmd->name)])) {
10995 p += strlen(cmd->name);
10998 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
11003 if (i == nitems(got_histedit_cmds)) {
11004 err = histedit_syntax_error(lineno);
11007 while (isspace((unsigned char)p[0]))
11009 if (cmd->code == GOT_HISTEDIT_MESG) {
11010 if (lastcmd != GOT_HISTEDIT_PICK &&
11011 lastcmd != GOT_HISTEDIT_EDIT) {
11012 err = got_error(GOT_ERR_HISTEDIT_CMD);
11015 if (p[0] == '\0') {
11016 err = histedit_edit_logmsg(hle, repo);
11020 hle->logmsg = strdup(p);
11021 if (hle->logmsg == NULL) {
11022 err = got_error_from_errno("strdup");
11028 lastcmd = cmd->code;
11032 while (end[0] && !isspace((unsigned char)end[0]))
11036 err = got_object_resolve_id_str(&commit_id, repo, p);
11038 /* override error code */
11039 err = histedit_syntax_error(lineno);
11043 hle = malloc(sizeof(*hle));
11045 err = got_error_from_errno("malloc");
11049 hle->commit_id = commit_id;
11050 hle->logmsg = NULL;
11054 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
11055 lastcmd = cmd->code;
11063 static const struct got_error *
11064 histedit_check_script(struct got_histedit_list *histedit_cmds,
11065 struct got_object_id_queue *commits, struct got_repository *repo)
11067 const struct got_error *err = NULL;
11068 struct got_object_qid *qid;
11069 struct got_histedit_list_entry *hle;
11070 static char msg[92];
11073 if (TAILQ_EMPTY(histedit_cmds))
11074 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
11075 "histedit script contains no commands");
11076 if (STAILQ_EMPTY(commits))
11077 return got_error(GOT_ERR_EMPTY_HISTEDIT);
11079 TAILQ_FOREACH(hle, histedit_cmds, entry) {
11080 struct got_histedit_list_entry *hle2;
11081 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
11084 if (got_object_id_cmp(hle->commit_id,
11085 hle2->commit_id) != 0)
11087 err = got_object_id_str(&id_str, hle->commit_id);
11090 snprintf(msg, sizeof(msg), "commit %s is listed "
11091 "more than once in histedit script", id_str);
11093 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
11097 STAILQ_FOREACH(qid, commits, entry) {
11098 TAILQ_FOREACH(hle, histedit_cmds, entry) {
11099 if (got_object_id_cmp(&qid->id, hle->commit_id) == 0)
11103 err = got_object_id_str(&id_str, &qid->id);
11106 snprintf(msg, sizeof(msg),
11107 "commit %s missing from histedit script", id_str);
11109 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
11113 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
11114 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
11115 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
11116 "last commit in histedit script cannot be folded");
11121 static const struct got_error *
11122 histedit_run_editor(struct got_histedit_list *histedit_cmds,
11123 const char *path, struct got_object_id_queue *commits,
11124 struct got_repository *repo)
11126 const struct got_error *err = NULL;
11130 err = get_editor(&editor);
11134 if (spawn_editor(editor, path) == -1) {
11135 err = got_error_from_errno("failed spawning editor");
11139 f = fopen(path, "re");
11141 err = got_error_from_errno("fopen");
11144 err = histedit_parse_list(histedit_cmds, f, repo);
11148 err = histedit_check_script(histedit_cmds, commits, repo);
11150 if (f && fclose(f) == EOF && err == NULL)
11151 err = got_error_from_errno("fclose");
11156 static const struct got_error *
11157 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
11158 struct got_object_id_queue *, const char *, const char *,
11159 struct got_repository *);
11161 static const struct got_error *
11162 histedit_edit_script(struct got_histedit_list *histedit_cmds,
11163 struct got_object_id_queue *commits, const char *branch_name,
11164 int edit_logmsg_only, int fold_only, int edit_only,
11165 struct got_repository *repo)
11167 const struct got_error *err;
11171 err = got_opentemp_named(&path, &f, "got-histedit", "");
11175 err = write_cmd_list(f, branch_name, commits);
11179 err = histedit_write_commit_list(commits, f, edit_logmsg_only,
11180 fold_only, edit_only, repo);
11184 if (edit_logmsg_only || fold_only || edit_only) {
11186 err = histedit_parse_list(histedit_cmds, f, repo);
11188 if (fclose(f) == EOF) {
11189 err = got_error_from_errno("fclose");
11193 err = histedit_run_editor(histedit_cmds, path, commits, repo);
11195 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
11196 err->code != GOT_ERR_HISTEDIT_CMD)
11198 err = histedit_edit_list_retry(histedit_cmds, err,
11199 commits, path, branch_name, repo);
11203 if (f && fclose(f) == EOF && err == NULL)
11204 err = got_error_from_errno("fclose");
11205 if (path && unlink(path) != 0 && err == NULL)
11206 err = got_error_from_errno2("unlink", path);
11211 static const struct got_error *
11212 histedit_save_list(struct got_histedit_list *histedit_cmds,
11213 struct got_worktree *worktree, struct got_repository *repo)
11215 const struct got_error *err = NULL;
11218 struct got_histedit_list_entry *hle;
11219 struct got_commit_object *commit = NULL;
11221 err = got_worktree_get_histedit_script_path(&path, worktree);
11225 f = fopen(path, "we");
11227 err = got_error_from_errno2("fopen", path);
11230 TAILQ_FOREACH(hle, histedit_cmds, entry) {
11231 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
11237 int n = fprintf(f, "%c %s\n",
11238 GOT_HISTEDIT_MESG, hle->logmsg);
11240 err = got_ferror(f, GOT_ERR_IO);
11246 if (f && fclose(f) == EOF && err == NULL)
11247 err = got_error_from_errno("fclose");
11250 got_object_commit_close(commit);
11255 histedit_free_list(struct got_histedit_list *histedit_cmds)
11257 struct got_histedit_list_entry *hle;
11259 while ((hle = TAILQ_FIRST(histedit_cmds))) {
11260 TAILQ_REMOVE(histedit_cmds, hle, entry);
11265 static const struct got_error *
11266 histedit_load_list(struct got_histedit_list *histedit_cmds,
11267 const char *path, struct got_repository *repo)
11269 const struct got_error *err = NULL;
11272 f = fopen(path, "re");
11274 err = got_error_from_errno2("fopen", path);
11278 err = histedit_parse_list(histedit_cmds, f, repo);
11280 if (f && fclose(f) == EOF && err == NULL)
11281 err = got_error_from_errno("fclose");
11285 static const struct got_error *
11286 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
11287 const struct got_error *edit_err, struct got_object_id_queue *commits,
11288 const char *path, const char *branch_name, struct got_repository *repo)
11290 const struct got_error *err = NULL, *prev_err = edit_err;
11293 while (resp != 'c' && resp != 'r' && resp != 'a') {
11294 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
11295 "or (a)bort: ", getprogname(), prev_err->msg);
11300 histedit_free_list(histedit_cmds);
11301 err = histedit_run_editor(histedit_cmds, path, commits,
11304 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
11305 err->code != GOT_ERR_HISTEDIT_CMD)
11312 } else if (resp == 'r') {
11313 histedit_free_list(histedit_cmds);
11314 err = histedit_edit_script(histedit_cmds,
11315 commits, branch_name, 0, 0, 0, repo);
11317 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
11318 err->code != GOT_ERR_HISTEDIT_CMD)
11325 } else if (resp == 'a') {
11326 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
11329 printf("invalid response '%c'\n", resp);
11335 static const struct got_error *
11336 histedit_complete(struct got_worktree *worktree,
11337 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
11338 struct got_reference *branch, struct got_repository *repo)
11340 printf("Switching work tree to %s\n",
11341 got_ref_get_symref_target(branch));
11342 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
11346 static const struct got_error *
11347 show_histedit_progress(struct got_commit_object *commit,
11348 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
11350 const struct got_error *err;
11351 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
11353 err = got_object_id_str(&old_id_str, hle->commit_id);
11358 err = got_object_id_str(&new_id_str, new_id);
11363 old_id_str[12] = '\0';
11365 new_id_str[12] = '\0';
11368 logmsg = strdup(hle->logmsg);
11369 if (logmsg == NULL) {
11370 err = got_error_from_errno("strdup");
11373 trim_logmsg(logmsg, 42);
11375 err = get_short_logmsg(&logmsg, 42, commit);
11380 switch (hle->cmd->code) {
11381 case GOT_HISTEDIT_PICK:
11382 case GOT_HISTEDIT_EDIT:
11383 printf("%s -> %s: %s\n", old_id_str,
11384 new_id_str ? new_id_str : "no-op change", logmsg);
11386 case GOT_HISTEDIT_DROP:
11387 case GOT_HISTEDIT_FOLD:
11388 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
11400 static const struct got_error *
11401 histedit_commit(struct got_pathlist_head *merged_paths,
11402 struct got_worktree *worktree, struct got_fileindex *fileindex,
11403 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
11404 const char *committer, struct got_repository *repo)
11406 const struct got_error *err;
11407 struct got_commit_object *commit;
11408 struct got_object_id *new_commit_id;
11410 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
11411 && hle->logmsg == NULL) {
11412 err = histedit_edit_logmsg(hle, repo);
11417 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
11421 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
11422 worktree, fileindex, tmp_branch, committer, commit, hle->commit_id,
11423 hle->logmsg, repo);
11425 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
11427 err = show_histedit_progress(commit, hle, NULL);
11429 err = show_histedit_progress(commit, hle, new_commit_id);
11430 free(new_commit_id);
11433 got_object_commit_close(commit);
11437 static const struct got_error *
11438 histedit_skip_commit(struct got_histedit_list_entry *hle,
11439 struct got_worktree *worktree, struct got_repository *repo)
11441 const struct got_error *error;
11442 struct got_commit_object *commit;
11444 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
11449 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
11453 error = show_histedit_progress(commit, hle, NULL);
11454 got_object_commit_close(commit);
11458 static const struct got_error *
11459 check_local_changes(void *arg, unsigned char status,
11460 unsigned char staged_status, const char *path,
11461 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
11462 struct got_object_id *commit_id, int dirfd, const char *de_name)
11464 int *have_local_changes = arg;
11467 case GOT_STATUS_ADD:
11468 case GOT_STATUS_DELETE:
11469 case GOT_STATUS_MODIFY:
11470 case GOT_STATUS_CONFLICT:
11471 *have_local_changes = 1;
11472 return got_error(GOT_ERR_CANCELLED);
11477 switch (staged_status) {
11478 case GOT_STATUS_ADD:
11479 case GOT_STATUS_DELETE:
11480 case GOT_STATUS_MODIFY:
11481 *have_local_changes = 1;
11482 return got_error(GOT_ERR_CANCELLED);
11490 static const struct got_error *
11491 cmd_histedit(int argc, char *argv[])
11493 const struct got_error *error = NULL;
11494 struct got_worktree *worktree = NULL;
11495 struct got_fileindex *fileindex = NULL;
11496 struct got_repository *repo = NULL;
11497 char *cwd = NULL, *committer = NULL, *gitconfig_path = NULL;
11498 struct got_reference *branch = NULL;
11499 struct got_reference *tmp_branch = NULL;
11500 struct got_object_id *resume_commit_id = NULL;
11501 struct got_object_id *base_commit_id = NULL;
11502 struct got_object_id *head_commit_id = NULL;
11503 struct got_commit_object *commit = NULL;
11504 int ch, rebase_in_progress = 0, merge_in_progress = 0;
11505 struct got_update_progress_arg upa;
11506 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
11507 int edit_logmsg_only = 0, fold_only = 0, edit_only = 0;
11508 int list_backups = 0, delete_backups = 0;
11509 const char *edit_script_path = NULL;
11510 struct got_object_id_queue commits;
11511 struct got_pathlist_head merged_paths;
11512 const struct got_object_id_queue *parent_ids;
11513 struct got_object_qid *pid;
11514 struct got_histedit_list histedit_cmds;
11515 struct got_histedit_list_entry *hle;
11516 int *pack_fds = NULL;
11518 STAILQ_INIT(&commits);
11519 TAILQ_INIT(&histedit_cmds);
11520 TAILQ_INIT(&merged_paths);
11521 memset(&upa, 0, sizeof(upa));
11523 while ((ch = getopt(argc, argv, "aceF:flmX")) != -1) {
11535 edit_script_path = optarg;
11544 edit_logmsg_only = 1;
11547 delete_backups = 1;
11559 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
11560 "unveil", NULL) == -1)
11563 if (abort_edit && continue_edit)
11564 option_conflict('a', 'c');
11565 if (edit_script_path && edit_logmsg_only)
11566 option_conflict('F', 'm');
11567 if (abort_edit && edit_logmsg_only)
11568 option_conflict('a', 'm');
11569 if (continue_edit && edit_logmsg_only)
11570 option_conflict('c', 'm');
11571 if (abort_edit && fold_only)
11572 option_conflict('a', 'f');
11573 if (continue_edit && fold_only)
11574 option_conflict('c', 'f');
11575 if (fold_only && edit_logmsg_only)
11576 option_conflict('f', 'm');
11577 if (edit_script_path && fold_only)
11578 option_conflict('F', 'f');
11579 if (abort_edit && edit_only)
11580 option_conflict('a', 'e');
11581 if (continue_edit && edit_only)
11582 option_conflict('c', 'e');
11583 if (edit_only && edit_logmsg_only)
11584 option_conflict('e', 'm');
11585 if (edit_script_path && edit_only)
11586 option_conflict('F', 'e');
11587 if (list_backups) {
11589 option_conflict('l', 'a');
11591 option_conflict('l', 'c');
11592 if (edit_script_path)
11593 option_conflict('l', 'F');
11594 if (edit_logmsg_only)
11595 option_conflict('l', 'm');
11597 option_conflict('l', 'f');
11599 option_conflict('l', 'e');
11600 if (delete_backups)
11601 option_conflict('l', 'X');
11602 if (argc != 0 && argc != 1)
11604 } else if (delete_backups) {
11606 option_conflict('X', 'a');
11608 option_conflict('X', 'c');
11609 if (edit_script_path)
11610 option_conflict('X', 'F');
11611 if (edit_logmsg_only)
11612 option_conflict('X', 'm');
11614 option_conflict('X', 'f');
11616 option_conflict('X', 'e');
11618 option_conflict('X', 'l');
11619 if (argc != 0 && argc != 1)
11621 } else if (argc != 0)
11625 * This command cannot apply unveil(2) in all cases because the
11626 * user may choose to run an editor to edit the histedit script
11627 * and to edit individual commit log messages.
11628 * unveil(2) traverses exec(2); if an editor is used we have to
11629 * apply unveil after edit script and log messages have been written.
11630 * XXX TODO: Make use of unveil(2) where possible.
11633 cwd = getcwd(NULL, 0);
11635 error = got_error_from_errno("getcwd");
11639 error = got_repo_pack_fds_open(&pack_fds);
11643 error = got_worktree_open(&worktree, cwd);
11645 if (list_backups || delete_backups) {
11646 if (error->code != GOT_ERR_NOT_WORKTREE)
11649 if (error->code == GOT_ERR_NOT_WORKTREE)
11650 error = wrap_not_worktree_error(error,
11656 if (list_backups || delete_backups) {
11657 error = got_repo_open(&repo,
11658 worktree ? got_worktree_get_repo_path(worktree) : cwd,
11662 error = apply_unveil(got_repo_get_path(repo), 0,
11663 worktree ? got_worktree_get_root_path(worktree) : NULL);
11666 error = process_backup_refs(
11667 GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
11668 argc == 1 ? argv[0] : NULL, delete_backups, repo);
11669 goto done; /* nothing else to do */
11672 error = get_gitconfig_path(&gitconfig_path);
11675 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
11676 gitconfig_path, pack_fds);
11680 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
11683 if (rebase_in_progress) {
11684 error = got_error(GOT_ERR_REBASING);
11688 error = got_worktree_merge_in_progress(&merge_in_progress, worktree,
11692 if (merge_in_progress) {
11693 error = got_error(GOT_ERR_MERGE_BUSY);
11697 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
11701 if (edit_in_progress && edit_logmsg_only) {
11702 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
11703 "histedit operation is in progress in this "
11704 "work tree and must be continued or aborted "
11705 "before the -m option can be used");
11708 if (edit_in_progress && fold_only) {
11709 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
11710 "histedit operation is in progress in this "
11711 "work tree and must be continued or aborted "
11712 "before the -f option can be used");
11715 if (edit_in_progress && edit_only) {
11716 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
11717 "histedit operation is in progress in this "
11718 "work tree and must be continued or aborted "
11719 "before the -e option can be used");
11723 if (edit_in_progress && abort_edit) {
11724 error = got_worktree_histedit_continue(&resume_commit_id,
11725 &tmp_branch, &branch, &base_commit_id, &fileindex,
11729 printf("Switching work tree to %s\n",
11730 got_ref_get_symref_target(branch));
11731 error = got_worktree_histedit_abort(worktree, fileindex, repo,
11732 branch, base_commit_id, abort_progress, &upa);
11735 printf("Histedit of %s aborted\n",
11736 got_ref_get_symref_target(branch));
11737 print_merge_progress_stats(&upa);
11738 goto done; /* nothing else to do */
11739 } else if (abort_edit) {
11740 error = got_error(GOT_ERR_NOT_HISTEDIT);
11744 error = get_author(&committer, repo, worktree);
11748 if (continue_edit) {
11751 if (!edit_in_progress) {
11752 error = got_error(GOT_ERR_NOT_HISTEDIT);
11756 error = got_worktree_get_histedit_script_path(&path, worktree);
11760 error = histedit_load_list(&histedit_cmds, path, repo);
11765 error = got_worktree_histedit_continue(&resume_commit_id,
11766 &tmp_branch, &branch, &base_commit_id, &fileindex,
11771 error = got_ref_resolve(&head_commit_id, repo, branch);
11775 error = got_object_open_as_commit(&commit, repo,
11779 parent_ids = got_object_commit_get_parent_ids(commit);
11780 pid = STAILQ_FIRST(parent_ids);
11782 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
11785 error = collect_commits(&commits, head_commit_id, &pid->id,
11786 base_commit_id, got_worktree_get_path_prefix(worktree),
11787 GOT_ERR_HISTEDIT_PATH, repo);
11788 got_object_commit_close(commit);
11793 if (edit_in_progress) {
11794 error = got_error(GOT_ERR_HISTEDIT_BUSY);
11798 error = got_ref_open(&branch, repo,
11799 got_worktree_get_head_ref_name(worktree), 0);
11803 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
11804 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
11805 "will not edit commit history of a branch outside "
11806 "the \"refs/heads/\" reference namespace");
11810 error = got_ref_resolve(&head_commit_id, repo, branch);
11811 got_ref_close(branch);
11816 error = got_object_open_as_commit(&commit, repo,
11820 parent_ids = got_object_commit_get_parent_ids(commit);
11821 pid = STAILQ_FIRST(parent_ids);
11823 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
11826 error = collect_commits(&commits, head_commit_id, &pid->id,
11827 got_worktree_get_base_commit_id(worktree),
11828 got_worktree_get_path_prefix(worktree),
11829 GOT_ERR_HISTEDIT_PATH, repo);
11830 got_object_commit_close(commit);
11835 if (STAILQ_EMPTY(&commits)) {
11836 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
11840 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
11841 &base_commit_id, &fileindex, worktree, repo);
11845 if (edit_script_path) {
11846 error = histedit_load_list(&histedit_cmds,
11847 edit_script_path, repo);
11849 got_worktree_histedit_abort(worktree, fileindex,
11850 repo, branch, base_commit_id,
11851 abort_progress, &upa);
11852 print_merge_progress_stats(&upa);
11856 const char *branch_name;
11857 branch_name = got_ref_get_symref_target(branch);
11858 if (strncmp(branch_name, "refs/heads/", 11) == 0)
11860 error = histedit_edit_script(&histedit_cmds, &commits,
11861 branch_name, edit_logmsg_only, fold_only,
11864 got_worktree_histedit_abort(worktree, fileindex,
11865 repo, branch, base_commit_id,
11866 abort_progress, &upa);
11867 print_merge_progress_stats(&upa);
11873 error = histedit_save_list(&histedit_cmds, worktree,
11876 got_worktree_histedit_abort(worktree, fileindex,
11877 repo, branch, base_commit_id,
11878 abort_progress, &upa);
11879 print_merge_progress_stats(&upa);
11885 error = histedit_check_script(&histedit_cmds, &commits, repo);
11889 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
11890 if (resume_commit_id) {
11891 if (got_object_id_cmp(hle->commit_id,
11892 resume_commit_id) != 0)
11895 resume_commit_id = NULL;
11896 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
11897 hle->cmd->code == GOT_HISTEDIT_FOLD) {
11898 error = histedit_skip_commit(hle, worktree,
11903 struct got_pathlist_head paths;
11904 int have_changes = 0;
11906 TAILQ_INIT(&paths);
11907 error = got_pathlist_append(&paths, "", NULL);
11910 error = got_worktree_status(worktree, &paths,
11911 repo, 0, check_local_changes, &have_changes,
11912 check_cancelled, NULL);
11913 got_pathlist_free(&paths,
11914 GOT_PATHLIST_FREE_NONE);
11916 if (error->code != GOT_ERR_CANCELLED)
11918 if (sigint_received || sigpipe_received)
11921 if (have_changes) {
11922 error = histedit_commit(NULL, worktree,
11923 fileindex, tmp_branch, hle,
11928 error = got_object_open_as_commit(
11929 &commit, repo, hle->commit_id);
11932 error = show_histedit_progress(commit,
11934 got_object_commit_close(commit);
11943 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
11944 error = histedit_skip_commit(hle, worktree, repo);
11950 error = got_object_open_as_commit(&commit, repo,
11954 parent_ids = got_object_commit_get_parent_ids(commit);
11955 pid = STAILQ_FIRST(parent_ids);
11957 error = got_worktree_histedit_merge_files(&merged_paths,
11958 worktree, fileindex, &pid->id, hle->commit_id, repo,
11959 update_progress, &upa, check_cancelled, NULL);
11962 got_object_commit_close(commit);
11965 print_merge_progress_stats(&upa);
11966 if (upa.conflicts > 0 || upa.missing > 0 ||
11967 upa.not_deleted > 0 || upa.unversioned > 0) {
11968 if (upa.conflicts > 0) {
11969 error = show_rebase_merge_conflict(
11970 hle->commit_id, repo);
11974 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
11978 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
11980 error = got_object_id_str(&id_str, hle->commit_id);
11983 printf("Stopping histedit for amending commit %s\n",
11986 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
11987 error = got_worktree_histedit_postpone(worktree,
11992 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
11993 error = histedit_skip_commit(hle, worktree, repo);
11999 error = histedit_commit(&merged_paths, worktree, fileindex,
12000 tmp_branch, hle, committer, repo);
12001 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
12006 if (upa.conflicts > 0 || upa.missing > 0 ||
12007 upa.not_deleted > 0 || upa.unversioned > 0) {
12008 error = got_worktree_histedit_postpone(worktree, fileindex);
12011 if (upa.conflicts > 0 && upa.missing == 0 &&
12012 upa.not_deleted == 0 && upa.unversioned == 0) {
12013 error = got_error_msg(GOT_ERR_CONFLICTS,
12014 "conflicts must be resolved before histedit "
12016 } else if (upa.conflicts > 0) {
12017 error = got_error_msg(GOT_ERR_CONFLICTS,
12018 "conflicts must be resolved before histedit "
12019 "can continue; changes destined for some "
12020 "files were not yet merged and should be "
12021 "merged manually if required before the "
12022 "histedit operation is continued");
12024 error = got_error_msg(GOT_ERR_CONFLICTS,
12025 "changes destined for some files were not "
12026 "yet merged and should be merged manually "
12027 "if required before the histedit operation "
12031 error = histedit_complete(worktree, fileindex, tmp_branch,
12036 free(gitconfig_path);
12037 got_object_id_queue_free(&commits);
12038 histedit_free_list(&histedit_cmds);
12039 free(head_commit_id);
12040 free(base_commit_id);
12041 free(resume_commit_id);
12043 got_object_commit_close(commit);
12045 got_ref_close(branch);
12047 got_ref_close(tmp_branch);
12049 got_worktree_close(worktree);
12051 const struct got_error *close_err = got_repo_close(repo);
12056 const struct got_error *pack_err =
12057 got_repo_pack_fds_close(pack_fds);
12065 usage_integrate(void)
12067 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
12071 static const struct got_error *
12072 cmd_integrate(int argc, char *argv[])
12074 const struct got_error *error = NULL;
12075 struct got_repository *repo = NULL;
12076 struct got_worktree *worktree = NULL;
12077 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
12078 const char *branch_arg = NULL;
12079 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
12080 struct got_fileindex *fileindex = NULL;
12081 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
12083 struct got_update_progress_arg upa;
12084 int *pack_fds = NULL;
12086 while ((ch = getopt(argc, argv, "")) != -1) {
12099 branch_arg = argv[0];
12101 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
12102 "unveil", NULL) == -1)
12105 cwd = getcwd(NULL, 0);
12107 error = got_error_from_errno("getcwd");
12111 error = got_repo_pack_fds_open(&pack_fds);
12115 error = got_worktree_open(&worktree, cwd);
12117 if (error->code == GOT_ERR_NOT_WORKTREE)
12118 error = wrap_not_worktree_error(error, "integrate",
12123 error = check_rebase_or_histedit_in_progress(worktree);
12127 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
12132 error = apply_unveil(got_repo_get_path(repo), 0,
12133 got_worktree_get_root_path(worktree));
12137 error = check_merge_in_progress(worktree, repo);
12141 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
12142 error = got_error_from_errno("asprintf");
12146 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
12147 &base_branch_ref, worktree, refname, repo);
12151 refname = strdup(got_ref_get_name(branch_ref));
12152 if (refname == NULL) {
12153 error = got_error_from_errno("strdup");
12154 got_worktree_integrate_abort(worktree, fileindex, repo,
12155 branch_ref, base_branch_ref);
12158 base_refname = strdup(got_ref_get_name(base_branch_ref));
12159 if (base_refname == NULL) {
12160 error = got_error_from_errno("strdup");
12161 got_worktree_integrate_abort(worktree, fileindex, repo,
12162 branch_ref, base_branch_ref);
12165 if (strncmp(base_refname, "refs/heads/", 11) != 0) {
12166 error = got_error(GOT_ERR_INTEGRATE_BRANCH);
12167 got_worktree_integrate_abort(worktree, fileindex, repo,
12168 branch_ref, base_branch_ref);
12172 error = got_ref_resolve(&commit_id, repo, branch_ref);
12176 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
12180 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
12181 error = got_error_msg(GOT_ERR_SAME_BRANCH,
12182 "specified branch has already been integrated");
12183 got_worktree_integrate_abort(worktree, fileindex, repo,
12184 branch_ref, base_branch_ref);
12188 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
12190 if (error->code == GOT_ERR_ANCESTRY)
12191 error = got_error(GOT_ERR_REBASE_REQUIRED);
12192 got_worktree_integrate_abort(worktree, fileindex, repo,
12193 branch_ref, base_branch_ref);
12197 memset(&upa, 0, sizeof(upa));
12198 error = got_worktree_integrate_continue(worktree, fileindex, repo,
12199 branch_ref, base_branch_ref, update_progress, &upa,
12200 check_cancelled, NULL);
12204 printf("Integrated %s into %s\n", refname, base_refname);
12205 print_update_progress_stats(&upa);
12208 const struct got_error *close_err = got_repo_close(repo);
12213 got_worktree_close(worktree);
12215 const struct got_error *pack_err =
12216 got_repo_pack_fds_close(pack_fds);
12221 free(base_commit_id);
12224 free(base_refname);
12231 fprintf(stderr, "usage: %s merge [-acn] [branch]\n", getprogname());
12235 static const struct got_error *
12236 cmd_merge(int argc, char *argv[])
12238 const struct got_error *error = NULL;
12239 struct got_worktree *worktree = NULL;
12240 struct got_repository *repo = NULL;
12241 struct got_fileindex *fileindex = NULL;
12242 char *cwd = NULL, *id_str = NULL, *author = NULL;
12243 struct got_reference *branch = NULL, *wt_branch = NULL;
12244 struct got_object_id *branch_tip = NULL, *yca_id = NULL;
12245 struct got_object_id *wt_branch_tip = NULL;
12246 int ch, merge_in_progress = 0, abort_merge = 0, continue_merge = 0;
12247 int interrupt_merge = 0;
12248 struct got_update_progress_arg upa;
12249 struct got_object_id *merge_commit_id = NULL;
12250 char *branch_name = NULL;
12251 int *pack_fds = NULL;
12253 memset(&upa, 0, sizeof(upa));
12255 while ((ch = getopt(argc, argv, "acn")) != -1) {
12261 continue_merge = 1;
12264 interrupt_merge = 1;
12276 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
12277 "unveil", NULL) == -1)
12281 if (abort_merge && continue_merge)
12282 option_conflict('a', 'c');
12283 if (abort_merge || continue_merge) {
12286 } else if (argc != 1)
12289 cwd = getcwd(NULL, 0);
12291 error = got_error_from_errno("getcwd");
12295 error = got_repo_pack_fds_open(&pack_fds);
12299 error = got_worktree_open(&worktree, cwd);
12301 if (error->code == GOT_ERR_NOT_WORKTREE)
12302 error = wrap_not_worktree_error(error,
12307 error = got_repo_open(&repo,
12308 worktree ? got_worktree_get_repo_path(worktree) : cwd, NULL,
12313 error = apply_unveil(got_repo_get_path(repo), 0,
12314 worktree ? got_worktree_get_root_path(worktree) : NULL);
12318 error = check_rebase_or_histedit_in_progress(worktree);
12322 error = got_worktree_merge_in_progress(&merge_in_progress, worktree,
12328 if (!merge_in_progress) {
12329 error = got_error(GOT_ERR_NOT_MERGING);
12332 error = got_worktree_merge_continue(&branch_name,
12333 &branch_tip, &fileindex, worktree, repo);
12336 error = got_worktree_merge_abort(worktree, fileindex, repo,
12337 abort_progress, &upa);
12340 printf("Merge of %s aborted\n", branch_name);
12341 goto done; /* nothing else to do */
12344 error = get_author(&author, repo, worktree);
12348 if (continue_merge) {
12349 if (!merge_in_progress) {
12350 error = got_error(GOT_ERR_NOT_MERGING);
12353 error = got_worktree_merge_continue(&branch_name,
12354 &branch_tip, &fileindex, worktree, repo);
12358 error = got_ref_open(&branch, repo, argv[0], 0);
12361 branch_name = strdup(got_ref_get_name(branch));
12362 if (branch_name == NULL) {
12363 error = got_error_from_errno("strdup");
12366 error = got_ref_resolve(&branch_tip, repo, branch);
12371 error = got_ref_open(&wt_branch, repo,
12372 got_worktree_get_head_ref_name(worktree), 0);
12375 error = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
12378 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
12379 wt_branch_tip, branch_tip, 0, repo,
12380 check_cancelled, NULL);
12381 if (error && error->code != GOT_ERR_ANCESTRY)
12384 if (!continue_merge) {
12385 error = check_path_prefix(wt_branch_tip, branch_tip,
12386 got_worktree_get_path_prefix(worktree),
12387 GOT_ERR_MERGE_PATH, repo);
12391 error = check_same_branch(wt_branch_tip, branch,
12394 if (error->code != GOT_ERR_ANCESTRY)
12398 static char msg[512];
12399 snprintf(msg, sizeof(msg),
12400 "cannot create a merge commit because "
12401 "%s is based on %s; %s can be integrated "
12402 "with 'got integrate' instead", branch_name,
12403 got_worktree_get_head_ref_name(worktree),
12405 error = got_error_msg(GOT_ERR_SAME_BRANCH, msg);
12409 error = got_worktree_merge_prepare(&fileindex, worktree,
12414 error = got_worktree_merge_branch(worktree, fileindex,
12415 yca_id, branch_tip, repo, update_progress, &upa,
12416 check_cancelled, NULL);
12419 print_merge_progress_stats(&upa);
12420 if (!upa.did_something) {
12421 error = got_worktree_merge_abort(worktree, fileindex,
12422 repo, abort_progress, &upa);
12425 printf("Already up-to-date\n");
12430 if (interrupt_merge) {
12431 error = got_worktree_merge_postpone(worktree, fileindex);
12434 printf("Merge of %s interrupted on request\n", branch_name);
12435 } else if (upa.conflicts > 0 || upa.missing > 0 ||
12436 upa.not_deleted > 0 || upa.unversioned > 0) {
12437 error = got_worktree_merge_postpone(worktree, fileindex);
12440 if (upa.conflicts > 0 && upa.missing == 0 &&
12441 upa.not_deleted == 0 && upa.unversioned == 0) {
12442 error = got_error_msg(GOT_ERR_CONFLICTS,
12443 "conflicts must be resolved before merging "
12445 } else if (upa.conflicts > 0) {
12446 error = got_error_msg(GOT_ERR_CONFLICTS,
12447 "conflicts must be resolved before merging "
12448 "can continue; changes destined for some "
12449 "files were not yet merged and "
12450 "should be merged manually if required before the "
12451 "merge operation is continued");
12453 error = got_error_msg(GOT_ERR_CONFLICTS,
12454 "changes destined for some "
12455 "files were not yet merged and should be "
12456 "merged manually if required before the "
12457 "merge operation is continued");
12461 error = got_worktree_merge_commit(&merge_commit_id, worktree,
12462 fileindex, author, NULL, 1, branch_tip, branch_name,
12463 repo, continue_merge ? print_status : NULL, NULL);
12466 error = got_worktree_merge_complete(worktree, fileindex, repo);
12469 error = got_object_id_str(&id_str, merge_commit_id);
12472 printf("Merged %s into %s: %s\n", branch_name,
12473 got_worktree_get_head_ref_name(worktree),
12479 free(merge_commit_id);
12485 got_ref_close(branch);
12487 got_ref_close(wt_branch);
12489 got_worktree_close(worktree);
12491 const struct got_error *close_err = got_repo_close(repo);
12496 const struct got_error *pack_err =
12497 got_repo_pack_fds_close(pack_fds);
12507 fprintf(stderr, "usage: %s stage [-lpS] [-F response-script] "
12508 "[path ...]\n", getprogname());
12512 static const struct got_error *
12513 print_stage(void *arg, unsigned char status, unsigned char staged_status,
12514 const char *path, struct got_object_id *blob_id,
12515 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
12516 int dirfd, const char *de_name)
12518 const struct got_error *err = NULL;
12519 char *id_str = NULL;
12521 if (staged_status != GOT_STATUS_ADD &&
12522 staged_status != GOT_STATUS_MODIFY &&
12523 staged_status != GOT_STATUS_DELETE)
12526 if (staged_status == GOT_STATUS_ADD ||
12527 staged_status == GOT_STATUS_MODIFY)
12528 err = got_object_id_str(&id_str, staged_blob_id);
12530 err = got_object_id_str(&id_str, blob_id);
12534 printf("%s %c %s\n", id_str, staged_status, path);
12539 static const struct got_error *
12540 cmd_stage(int argc, char *argv[])
12542 const struct got_error *error = NULL;
12543 struct got_repository *repo = NULL;
12544 struct got_worktree *worktree = NULL;
12546 struct got_pathlist_head paths;
12547 int ch, list_stage = 0, pflag = 0, allow_bad_symlinks = 0;
12548 FILE *patch_script_file = NULL;
12549 const char *patch_script_path = NULL;
12550 struct choose_patch_arg cpa;
12551 int *pack_fds = NULL;
12553 TAILQ_INIT(&paths);
12555 while ((ch = getopt(argc, argv, "F:lpS")) != -1) {
12558 patch_script_path = optarg;
12567 allow_bad_symlinks = 1;
12579 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
12580 "unveil", NULL) == -1)
12583 if (list_stage && (pflag || patch_script_path))
12584 errx(1, "-l option cannot be used with other options");
12585 if (patch_script_path && !pflag)
12586 errx(1, "-F option can only be used together with -p option");
12588 cwd = getcwd(NULL, 0);
12590 error = got_error_from_errno("getcwd");
12594 error = got_repo_pack_fds_open(&pack_fds);
12598 error = got_worktree_open(&worktree, cwd);
12600 if (error->code == GOT_ERR_NOT_WORKTREE)
12601 error = wrap_not_worktree_error(error, "stage", cwd);
12605 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
12610 if (patch_script_path) {
12611 patch_script_file = fopen(patch_script_path, "re");
12612 if (patch_script_file == NULL) {
12613 error = got_error_from_errno2("fopen",
12614 patch_script_path);
12618 error = apply_unveil(got_repo_get_path(repo), 0,
12619 got_worktree_get_root_path(worktree));
12623 error = check_merge_in_progress(worktree, repo);
12627 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
12632 error = got_worktree_status(worktree, &paths, repo, 0,
12633 print_stage, NULL, check_cancelled, NULL);
12635 cpa.patch_script_file = patch_script_file;
12636 cpa.action = "stage";
12637 error = got_worktree_stage(worktree, &paths,
12638 pflag ? NULL : print_status, NULL,
12639 pflag ? choose_patch : NULL, &cpa,
12640 allow_bad_symlinks, repo);
12643 if (patch_script_file && fclose(patch_script_file) == EOF &&
12645 error = got_error_from_errno2("fclose", patch_script_path);
12647 const struct got_error *close_err = got_repo_close(repo);
12652 got_worktree_close(worktree);
12654 const struct got_error *pack_err =
12655 got_repo_pack_fds_close(pack_fds);
12659 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
12665 usage_unstage(void)
12667 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
12668 "[path ...]\n", getprogname());
12673 static const struct got_error *
12674 cmd_unstage(int argc, char *argv[])
12676 const struct got_error *error = NULL;
12677 struct got_repository *repo = NULL;
12678 struct got_worktree *worktree = NULL;
12680 struct got_pathlist_head paths;
12682 struct got_update_progress_arg upa;
12683 FILE *patch_script_file = NULL;
12684 const char *patch_script_path = NULL;
12685 struct choose_patch_arg cpa;
12686 int *pack_fds = NULL;
12688 TAILQ_INIT(&paths);
12690 while ((ch = getopt(argc, argv, "F:p")) != -1) {
12693 patch_script_path = optarg;
12708 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
12709 "unveil", NULL) == -1)
12712 if (patch_script_path && !pflag)
12713 errx(1, "-F option can only be used together with -p option");
12715 cwd = getcwd(NULL, 0);
12717 error = got_error_from_errno("getcwd");
12721 error = got_repo_pack_fds_open(&pack_fds);
12725 error = got_worktree_open(&worktree, cwd);
12727 if (error->code == GOT_ERR_NOT_WORKTREE)
12728 error = wrap_not_worktree_error(error, "unstage", cwd);
12732 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
12737 if (patch_script_path) {
12738 patch_script_file = fopen(patch_script_path, "re");
12739 if (patch_script_file == NULL) {
12740 error = got_error_from_errno2("fopen",
12741 patch_script_path);
12746 error = apply_unveil(got_repo_get_path(repo), 0,
12747 got_worktree_get_root_path(worktree));
12751 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
12755 cpa.patch_script_file = patch_script_file;
12756 cpa.action = "unstage";
12757 memset(&upa, 0, sizeof(upa));
12758 error = got_worktree_unstage(worktree, &paths, update_progress,
12759 &upa, pflag ? choose_patch : NULL, &cpa, repo);
12761 print_merge_progress_stats(&upa);
12763 if (patch_script_file && fclose(patch_script_file) == EOF &&
12765 error = got_error_from_errno2("fclose", patch_script_path);
12767 const struct got_error *close_err = got_repo_close(repo);
12772 got_worktree_close(worktree);
12774 const struct got_error *pack_err =
12775 got_repo_pack_fds_close(pack_fds);
12779 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
12787 fprintf(stderr, "usage: %s cat [-P] [-c commit] [-r repository-path] "
12788 "arg ...\n", getprogname());
12792 static const struct got_error *
12793 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
12795 const struct got_error *err;
12796 struct got_blob_object *blob;
12799 fd = got_opentempfd();
12801 return got_error_from_errno("got_opentempfd");
12803 err = got_object_open_as_blob(&blob, repo, id, 8192, fd);
12807 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
12809 if (fd != -1 && close(fd) == -1 && err == NULL)
12810 err = got_error_from_errno("close");
12812 got_object_blob_close(blob);
12816 static const struct got_error *
12817 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
12819 const struct got_error *err;
12820 struct got_tree_object *tree;
12823 err = got_object_open_as_tree(&tree, repo, id);
12827 nentries = got_object_tree_get_nentries(tree);
12828 for (i = 0; i < nentries; i++) {
12829 struct got_tree_entry *te;
12831 if (sigint_received || sigpipe_received)
12833 te = got_object_tree_get_entry(tree, i);
12834 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
12837 fprintf(outfile, "%s %.7o %s\n", id_str,
12838 got_tree_entry_get_mode(te),
12839 got_tree_entry_get_name(te));
12843 got_object_tree_close(tree);
12847 static const struct got_error *
12848 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
12850 const struct got_error *err;
12851 struct got_commit_object *commit;
12852 const struct got_object_id_queue *parent_ids;
12853 struct got_object_qid *pid;
12854 char *id_str = NULL;
12855 const char *logmsg = NULL;
12858 err = got_object_open_as_commit(&commit, repo, id);
12862 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
12866 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
12867 parent_ids = got_object_commit_get_parent_ids(commit);
12868 fprintf(outfile, "numparents %d\n",
12869 got_object_commit_get_nparents(commit));
12870 STAILQ_FOREACH(pid, parent_ids, entry) {
12872 err = got_object_id_str(&pid_str, &pid->id);
12875 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
12878 got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
12879 got_object_commit_get_author_gmtoff(commit));
12880 fprintf(outfile, "%s%s %lld %s\n", GOT_COMMIT_LABEL_AUTHOR,
12881 got_object_commit_get_author(commit),
12882 (long long)got_object_commit_get_author_time(commit),
12885 got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
12886 got_object_commit_get_committer_gmtoff(commit));
12887 fprintf(outfile, "%s%s %lld %s\n", GOT_COMMIT_LABEL_COMMITTER,
12888 got_object_commit_get_committer(commit),
12889 (long long)got_object_commit_get_committer_time(commit),
12892 logmsg = got_object_commit_get_logmsg_raw(commit);
12893 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
12894 fprintf(outfile, "%s", logmsg);
12897 got_object_commit_close(commit);
12901 static const struct got_error *
12902 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
12904 const struct got_error *err;
12905 struct got_tag_object *tag;
12906 char *id_str = NULL;
12907 const char *tagmsg = NULL;
12910 err = got_object_open_as_tag(&tag, repo, id);
12914 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
12918 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
12920 switch (got_object_tag_get_object_type(tag)) {
12921 case GOT_OBJ_TYPE_BLOB:
12922 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
12923 GOT_OBJ_LABEL_BLOB);
12925 case GOT_OBJ_TYPE_TREE:
12926 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
12927 GOT_OBJ_LABEL_TREE);
12929 case GOT_OBJ_TYPE_COMMIT:
12930 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
12931 GOT_OBJ_LABEL_COMMIT);
12933 case GOT_OBJ_TYPE_TAG:
12934 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
12935 GOT_OBJ_LABEL_TAG);
12941 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
12942 got_object_tag_get_name(tag));
12944 got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
12945 got_object_tag_get_tagger_gmtoff(tag));
12946 fprintf(outfile, "%s%s %lld %s\n", GOT_TAG_LABEL_TAGGER,
12947 got_object_tag_get_tagger(tag),
12948 (long long)got_object_tag_get_tagger_time(tag),
12951 tagmsg = got_object_tag_get_message(tag);
12952 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
12953 fprintf(outfile, "%s", tagmsg);
12956 got_object_tag_close(tag);
12960 static const struct got_error *
12961 cmd_cat(int argc, char *argv[])
12963 const struct got_error *error;
12964 struct got_repository *repo = NULL;
12965 struct got_worktree *worktree = NULL;
12966 char *cwd = NULL, *repo_path = NULL, *label = NULL;
12967 const char *commit_id_str = NULL;
12968 struct got_object_id *id = NULL, *commit_id = NULL;
12969 struct got_commit_object *commit = NULL;
12970 int ch, obj_type, i, force_path = 0;
12971 struct got_reflist_head refs;
12972 int *pack_fds = NULL;
12977 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
12982 while ((ch = getopt(argc, argv, "c:Pr:")) != -1) {
12985 commit_id_str = optarg;
12991 repo_path = realpath(optarg, NULL);
12992 if (repo_path == NULL)
12993 return got_error_from_errno2("realpath",
12995 got_path_strip_trailing_slashes(repo_path);
13006 cwd = getcwd(NULL, 0);
13008 error = got_error_from_errno("getcwd");
13012 error = got_repo_pack_fds_open(&pack_fds);
13016 if (repo_path == NULL) {
13017 error = got_worktree_open(&worktree, cwd);
13018 if (error && error->code != GOT_ERR_NOT_WORKTREE)
13021 repo_path = strdup(
13022 got_worktree_get_repo_path(worktree));
13023 if (repo_path == NULL) {
13024 error = got_error_from_errno("strdup");
13028 /* Release work tree lock. */
13029 got_worktree_close(worktree);
13034 if (repo_path == NULL) {
13035 repo_path = strdup(cwd);
13036 if (repo_path == NULL)
13037 return got_error_from_errno("strdup");
13040 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
13045 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
13049 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
13053 if (commit_id_str == NULL)
13054 commit_id_str = GOT_REF_HEAD;
13055 error = got_repo_match_object_id(&commit_id, NULL,
13056 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
13060 error = got_object_open_as_commit(&commit, repo, commit_id);
13064 for (i = 0; i < argc; i++) {
13066 error = got_object_id_by_path(&id, repo, commit,
13071 error = got_repo_match_object_id(&id, &label, argv[i],
13072 GOT_OBJ_TYPE_ANY, NULL /* do not resolve tags */,
13075 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
13076 error->code != GOT_ERR_NOT_REF)
13078 error = got_object_id_by_path(&id, repo,
13085 error = got_object_get_type(&obj_type, repo, id);
13089 switch (obj_type) {
13090 case GOT_OBJ_TYPE_BLOB:
13091 error = cat_blob(id, repo, stdout);
13093 case GOT_OBJ_TYPE_TREE:
13094 error = cat_tree(id, repo, stdout);
13096 case GOT_OBJ_TYPE_COMMIT:
13097 error = cat_commit(id, repo, stdout);
13099 case GOT_OBJ_TYPE_TAG:
13100 error = cat_tag(id, repo, stdout);
13103 error = got_error(GOT_ERR_OBJ_TYPE);
13118 got_object_commit_close(commit);
13120 got_worktree_close(worktree);
13122 const struct got_error *close_err = got_repo_close(repo);
13127 const struct got_error *pack_err =
13128 got_repo_pack_fds_close(pack_fds);
13133 got_ref_list_free(&refs);
13140 fprintf(stderr, "usage: %s info [path ...]\n",
13145 static const struct got_error *
13146 print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
13147 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
13148 struct got_object_id *commit_id)
13150 const struct got_error *err = NULL;
13151 char *id_str = NULL;
13153 struct tm mytm, *tm;
13154 struct got_pathlist_head *paths = arg;
13155 struct got_pathlist_entry *pe;
13158 * Clear error indication from any of the path arguments which
13159 * would cause this file index entry to be displayed.
13161 TAILQ_FOREACH(pe, paths, entry) {
13162 if (got_path_cmp(path, pe->path, strlen(path),
13163 pe->path_len) == 0 ||
13164 got_path_is_child(path, pe->path, pe->path_len))
13165 pe->data = NULL; /* no error */
13168 printf(GOT_COMMIT_SEP_STR);
13170 printf("symlink: %s\n", path);
13171 else if (S_ISREG(mode)) {
13172 printf("file: %s\n", path);
13173 printf("mode: %o\n", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
13174 } else if (S_ISDIR(mode))
13175 printf("directory: %s\n", path);
13177 printf("something: %s\n", path);
13179 tm = localtime_r(&mtime, &mytm);
13182 if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) == 0)
13183 return got_error(GOT_ERR_NO_SPACE);
13184 printf("timestamp: %s\n", datebuf);
13187 err = got_object_id_str(&id_str, blob_id);
13190 printf("based on blob: %s\n", id_str);
13194 if (staged_blob_id) {
13195 err = got_object_id_str(&id_str, staged_blob_id);
13198 printf("based on staged blob: %s\n", id_str);
13203 err = got_object_id_str(&id_str, commit_id);
13206 printf("based on commit: %s\n", id_str);
13213 static const struct got_error *
13214 cmd_info(int argc, char *argv[])
13216 const struct got_error *error = NULL;
13217 struct got_worktree *worktree = NULL;
13218 char *cwd = NULL, *id_str = NULL;
13219 struct got_pathlist_head paths;
13220 char *uuidstr = NULL;
13221 int ch, show_files = 0;
13223 TAILQ_INIT(&paths);
13225 while ((ch = getopt(argc, argv, "")) != -1) {
13237 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
13241 cwd = getcwd(NULL, 0);
13243 error = got_error_from_errno("getcwd");
13247 error = got_worktree_open(&worktree, cwd);
13249 if (error->code == GOT_ERR_NOT_WORKTREE)
13250 error = wrap_not_worktree_error(error, "info", cwd);
13255 /* Remove "wpath cpath proc exec sendfd" promises. */
13256 if (pledge("stdio rpath flock unveil", NULL) == -1)
13259 error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
13264 error = get_worktree_paths_from_argv(&paths, argc, argv,
13271 error = got_object_id_str(&id_str,
13272 got_worktree_get_base_commit_id(worktree));
13276 error = got_worktree_get_uuid(&uuidstr, worktree);
13280 printf("work tree: %s\n", got_worktree_get_root_path(worktree));
13281 printf("work tree base commit: %s\n", id_str);
13282 printf("work tree path prefix: %s\n",
13283 got_worktree_get_path_prefix(worktree));
13284 printf("work tree branch reference: %s\n",
13285 got_worktree_get_head_ref_name(worktree));
13286 printf("work tree UUID: %s\n", uuidstr);
13287 printf("repository: %s\n", got_worktree_get_repo_path(worktree));
13290 struct got_pathlist_entry *pe;
13291 TAILQ_FOREACH(pe, &paths, entry) {
13292 if (pe->path_len == 0)
13295 * Assume this path will fail. This will be corrected
13296 * in print_path_info() in case the path does suceeed.
13298 pe->data = (void *)got_error(GOT_ERR_BAD_PATH);
13300 error = got_worktree_path_info(worktree, &paths,
13301 print_path_info, &paths, check_cancelled, NULL);
13304 TAILQ_FOREACH(pe, &paths, entry) {
13305 if (pe->data != NULL) {
13306 const struct got_error *perr;
13309 error = got_error_fmt(perr->code, "%s",
13317 got_worktree_close(worktree);
13318 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);