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");
712 static const struct got_error *
713 get_signer_id(char **signer_id, struct got_repository *repo,
714 struct got_worktree *worktree)
716 const char *got_signer_id = NULL;
717 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
722 worktree_conf = got_worktree_get_gotconfig(worktree);
723 repo_conf = got_repo_get_gotconfig(repo);
726 * Priority of potential author information sources, from most
727 * significant to least significant:
728 * 1) work tree's .got/got.conf file
729 * 2) repository's got.conf file
733 got_signer_id = got_gotconfig_get_signer_id(worktree_conf);
734 if (got_signer_id == NULL)
735 got_signer_id = got_gotconfig_get_signer_id(repo_conf);
738 *signer_id = strdup(got_signer_id);
739 if (*signer_id == NULL)
740 return got_error_from_errno("strdup");
745 static const struct got_error *
746 get_gitconfig_path(char **gitconfig_path)
748 const char *homedir = getenv("HOME");
750 *gitconfig_path = NULL;
752 if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
753 return got_error_from_errno("asprintf");
759 static const struct got_error *
760 cmd_import(int argc, char *argv[])
762 const struct got_error *error = NULL;
763 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
764 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
765 const char *branch_name = NULL;
766 char *id_str = NULL, *logmsg_path = NULL;
767 char refname[PATH_MAX] = "refs/heads/";
768 struct got_repository *repo = NULL;
769 struct got_reference *branch_ref = NULL, *head_ref = NULL;
770 struct got_object_id *new_commit_id = NULL;
772 struct got_pathlist_head ignores;
773 struct got_pathlist_entry *pe;
774 int preserve_logmsg = 0;
775 int *pack_fds = NULL;
777 TAILQ_INIT(&ignores);
779 while ((ch = getopt(argc, argv, "b:I:m:r:")) != -1) {
782 branch_name = optarg;
785 if (optarg[0] == '\0')
787 error = got_pathlist_insert(&pe, &ignores, optarg,
793 logmsg = strdup(optarg);
794 if (logmsg == NULL) {
795 error = got_error_from_errno("strdup");
800 repo_path = realpath(optarg, NULL);
801 if (repo_path == NULL) {
802 error = got_error_from_errno2("realpath",
817 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
825 if (repo_path == NULL) {
826 repo_path = getcwd(NULL, 0);
827 if (repo_path == NULL)
828 return got_error_from_errno("getcwd");
830 got_path_strip_trailing_slashes(repo_path);
831 error = get_gitconfig_path(&gitconfig_path);
834 error = got_repo_pack_fds_open(&pack_fds);
837 error = got_repo_open(&repo, repo_path, gitconfig_path, pack_fds);
841 error = get_author(&author, repo, NULL);
846 * Don't let the user create a branch name with a leading '-'.
847 * While technically a valid reference name, this case is usually
848 * an unintended typo.
850 if (branch_name && branch_name[0] == '-')
851 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
853 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
854 if (error && error->code != GOT_ERR_NOT_REF)
858 n = strlcat(refname, branch_name, sizeof(refname));
859 else if (head_ref && got_ref_is_symbolic(head_ref))
860 n = strlcpy(refname, got_ref_get_symref_target(head_ref),
863 n = strlcat(refname, "main", sizeof(refname));
864 if (n >= sizeof(refname)) {
865 error = got_error(GOT_ERR_NO_SPACE);
869 error = got_ref_open(&branch_ref, repo, refname, 0);
871 if (error->code != GOT_ERR_NOT_REF)
874 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
875 "import target branch already exists");
879 path_dir = realpath(argv[0], NULL);
880 if (path_dir == NULL) {
881 error = got_error_from_errno2("realpath", argv[0]);
884 got_path_strip_trailing_slashes(path_dir);
887 * unveil(2) traverses exec(2); if an editor is used we have
888 * to apply unveil after the log message has been written.
890 if (logmsg == NULL || strlen(logmsg) == 0) {
891 error = get_editor(&editor);
895 error = collect_import_msg(&logmsg, &logmsg_path, editor,
898 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
905 if (unveil(path_dir, "r") != 0) {
906 error = got_error_from_errno2("unveil", path_dir);
912 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
919 error = got_repo_import(&new_commit_id, path_dir, logmsg,
920 author, &ignores, repo, import_progress, NULL);
927 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
934 error = got_ref_write(branch_ref, repo);
941 error = got_object_id_str(&id_str, new_commit_id);
948 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
950 if (error->code != GOT_ERR_NOT_REF) {
956 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
964 error = got_ref_write(head_ref, repo);
972 printf("Created branch %s with commit %s\n",
973 got_ref_get_name(branch_ref), id_str);
976 const struct got_error *pack_err =
977 got_repo_pack_fds_close(pack_fds);
981 if (preserve_logmsg) {
982 fprintf(stderr, "%s: log message preserved in %s\n",
983 getprogname(), logmsg_path);
984 } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
985 error = got_error_from_errno2("unlink", logmsg_path);
993 free(gitconfig_path);
995 got_ref_close(branch_ref);
997 got_ref_close(head_ref);
1004 fprintf(stderr, "usage: %s clone [-almqv] [-b branch] [-R reference] "
1005 "repository-URL [directory]\n", getprogname());
1009 struct got_fetch_progress_arg {
1010 char last_scaled_size[FMT_SCALED_STRSIZE];
1012 int last_p_resolved;
1015 struct got_repository *repo;
1018 int configs_created;
1020 struct got_pathlist_head *symrefs;
1021 struct got_pathlist_head *wanted_branches;
1022 struct got_pathlist_head *wanted_refs;
1026 const char *remote_repo_path;
1027 const char *git_url;
1028 int fetch_all_branches;
1029 int mirror_references;
1033 /* XXX forward declaration */
1034 static const struct got_error *
1035 create_config_files(const char *proto, const char *host, const char *port,
1036 const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1037 int mirror_references, struct got_pathlist_head *symrefs,
1038 struct got_pathlist_head *wanted_branches,
1039 struct got_pathlist_head *wanted_refs, struct got_repository *repo);
1041 static const struct got_error *
1042 fetch_progress(void *arg, const char *message, off_t packfile_size,
1043 int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
1045 const struct got_error *err = NULL;
1046 struct got_fetch_progress_arg *a = arg;
1047 char scaled_size[FMT_SCALED_STRSIZE];
1048 int p_indexed, p_resolved;
1049 int print_size = 0, print_indexed = 0, print_resolved = 0;
1052 * In order to allow a failed clone to be resumed with 'got fetch'
1053 * we try to create configuration files as soon as possible.
1054 * Once the server has sent information about its default branch
1055 * we have all required information.
1057 if (a->create_configs && !a->configs_created &&
1058 !TAILQ_EMPTY(a->config_info.symrefs)) {
1059 err = create_config_files(a->config_info.proto,
1060 a->config_info.host, a->config_info.port,
1061 a->config_info.remote_repo_path,
1062 a->config_info.git_url,
1063 a->config_info.fetch_all_branches,
1064 a->config_info.mirror_references,
1065 a->config_info.symrefs,
1066 a->config_info.wanted_branches,
1067 a->config_info.wanted_refs, a->repo);
1070 a->configs_created = 1;
1073 if (a->verbosity < 0)
1076 if (message && message[0] != '\0') {
1077 printf("\rserver: %s", message);
1082 if (packfile_size > 0 || nobj_indexed > 0) {
1083 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
1084 (a->last_scaled_size[0] == '\0' ||
1085 strcmp(scaled_size, a->last_scaled_size)) != 0) {
1087 if (strlcpy(a->last_scaled_size, scaled_size,
1088 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
1089 return got_error(GOT_ERR_NO_SPACE);
1091 if (nobj_indexed > 0) {
1092 p_indexed = (nobj_indexed * 100) / nobj_total;
1093 if (p_indexed != a->last_p_indexed) {
1094 a->last_p_indexed = p_indexed;
1099 if (nobj_resolved > 0) {
1100 p_resolved = (nobj_resolved * 100) /
1101 (nobj_total - nobj_loose);
1102 if (p_resolved != a->last_p_resolved) {
1103 a->last_p_resolved = p_resolved;
1111 if (print_size || print_indexed || print_resolved)
1114 printf("%*s fetched", FMT_SCALED_STRSIZE - 2, scaled_size);
1116 printf("; indexing %d%%", p_indexed);
1118 printf("; resolving deltas %d%%", p_resolved);
1119 if (print_size || print_indexed || print_resolved)
1125 static const struct got_error *
1126 create_symref(const char *refname, struct got_reference *target_ref,
1127 int verbosity, struct got_repository *repo)
1129 const struct got_error *err;
1130 struct got_reference *head_symref;
1132 err = got_ref_alloc_symref(&head_symref, refname, target_ref);
1136 err = got_ref_write(head_symref, repo);
1137 if (err == NULL && verbosity > 0) {
1138 printf("Created reference %s: %s\n", GOT_REF_HEAD,
1139 got_ref_get_name(target_ref));
1141 got_ref_close(head_symref);
1145 static const struct got_error *
1146 list_remote_refs(struct got_pathlist_head *symrefs,
1147 struct got_pathlist_head *refs)
1149 const struct got_error *err;
1150 struct got_pathlist_entry *pe;
1152 TAILQ_FOREACH(pe, symrefs, entry) {
1153 const char *refname = pe->path;
1154 const char *targetref = pe->data;
1156 printf("%s: %s\n", refname, targetref);
1159 TAILQ_FOREACH(pe, refs, entry) {
1160 const char *refname = pe->path;
1161 struct got_object_id *id = pe->data;
1164 err = got_object_id_str(&id_str, id);
1167 printf("%s: %s\n", refname, id_str);
1174 static const struct got_error *
1175 create_ref(const char *refname, struct got_object_id *id,
1176 int verbosity, struct got_repository *repo)
1178 const struct got_error *err = NULL;
1179 struct got_reference *ref;
1182 err = got_object_id_str(&id_str, id);
1186 err = got_ref_alloc(&ref, refname, id);
1190 err = got_ref_write(ref, repo);
1193 if (err == NULL && verbosity >= 0)
1194 printf("Created reference %s: %s\n", refname, id_str);
1201 match_wanted_ref(const char *refname, const char *wanted_ref)
1203 if (strncmp(refname, "refs/", 5) != 0)
1208 * Prevent fetching of references that won't make any
1209 * sense outside of the remote repository's context.
1211 if (strncmp(refname, "got/", 4) == 0)
1213 if (strncmp(refname, "remotes/", 8) == 0)
1216 if (strncmp(wanted_ref, "refs/", 5) == 0)
1219 /* Allow prefix match. */
1220 if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
1223 /* Allow exact match. */
1224 return (strcmp(refname, wanted_ref) == 0);
1228 is_wanted_ref(struct got_pathlist_head *wanted_refs, const char *refname)
1230 struct got_pathlist_entry *pe;
1232 TAILQ_FOREACH(pe, wanted_refs, entry) {
1233 if (match_wanted_ref(refname, pe->path))
1240 static const struct got_error *
1241 create_wanted_ref(const char *refname, struct got_object_id *id,
1242 const char *remote_repo_name, int verbosity, struct got_repository *repo)
1244 const struct got_error *err;
1245 char *remote_refname;
1247 if (strncmp("refs/", refname, 5) == 0)
1250 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1251 remote_repo_name, refname) == -1)
1252 return got_error_from_errno("asprintf");
1254 err = create_ref(remote_refname, id, verbosity, repo);
1255 free(remote_refname);
1259 static const struct got_error *
1260 create_gotconfig(const char *proto, const char *host, const char *port,
1261 const char *remote_repo_path, const char *default_branch,
1262 int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1263 struct got_pathlist_head *wanted_refs, int mirror_references,
1264 struct got_repository *repo)
1266 const struct got_error *err = NULL;
1267 char *gotconfig_path = NULL;
1268 char *gotconfig = NULL;
1269 FILE *gotconfig_file = NULL;
1270 const char *branchname = NULL;
1271 char *branches = NULL, *refs = NULL;
1274 if (!fetch_all_branches && !TAILQ_EMPTY(wanted_branches)) {
1275 struct got_pathlist_entry *pe;
1276 TAILQ_FOREACH(pe, wanted_branches, entry) {
1278 branchname = pe->path;
1279 if (strncmp(branchname, "refs/heads/", 11) == 0)
1281 if (asprintf(&s, "%s\"%s\" ",
1282 branches ? branches : "", branchname) == -1) {
1283 err = got_error_from_errno("asprintf");
1289 } else if (!fetch_all_branches && default_branch) {
1290 branchname = default_branch;
1291 if (strncmp(branchname, "refs/heads/", 11) == 0)
1293 if (asprintf(&branches, "\"%s\" ", branchname) == -1) {
1294 err = got_error_from_errno("asprintf");
1298 if (!TAILQ_EMPTY(wanted_refs)) {
1299 struct got_pathlist_entry *pe;
1300 TAILQ_FOREACH(pe, wanted_refs, entry) {
1302 const char *refname = pe->path;
1303 if (strncmp(refname, "refs/", 5) == 0)
1305 if (asprintf(&s, "%s\"%s\" ",
1306 refs ? refs : "", refname) == -1) {
1307 err = got_error_from_errno("asprintf");
1315 /* Create got.conf(5). */
1316 gotconfig_path = got_repo_get_path_gotconfig(repo);
1317 if (gotconfig_path == NULL) {
1318 err = got_error_from_errno("got_repo_get_path_gotconfig");
1321 gotconfig_file = fopen(gotconfig_path, "ae");
1322 if (gotconfig_file == NULL) {
1323 err = got_error_from_errno2("fopen", gotconfig_path);
1326 if (asprintf(&gotconfig,
1331 "\trepository \"%s\"\n"
1337 GOT_FETCH_DEFAULT_REMOTE_NAME, host, proto,
1338 port ? "\tport " : "", port ? port : "", port ? "\n" : "",
1339 remote_repo_path, branches ? "\tbranch { " : "",
1340 branches ? branches : "", branches ? "}\n" : "",
1341 refs ? "\treference { " : "", refs ? refs : "", refs ? "}\n" : "",
1342 mirror_references ? "\tmirror_references yes\n" : "",
1343 fetch_all_branches ? "\tfetch_all_branches yes\n" : "") == -1) {
1344 err = got_error_from_errno("asprintf");
1347 n = fwrite(gotconfig, 1, strlen(gotconfig), gotconfig_file);
1348 if (n != strlen(gotconfig)) {
1349 err = got_ferror(gotconfig_file, GOT_ERR_IO);
1354 if (gotconfig_file && fclose(gotconfig_file) == EOF && err == NULL)
1355 err = got_error_from_errno2("fclose", gotconfig_path);
1356 free(gotconfig_path);
1361 static const struct got_error *
1362 create_gitconfig(const char *git_url, const char *default_branch,
1363 int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1364 struct got_pathlist_head *wanted_refs, int mirror_references,
1365 struct got_repository *repo)
1367 const struct got_error *err = NULL;
1368 char *gitconfig_path = NULL;
1369 char *gitconfig = NULL;
1370 FILE *gitconfig_file = NULL;
1371 char *branches = NULL, *refs = NULL;
1372 const char *branchname;
1375 /* Create a config file Git can understand. */
1376 gitconfig_path = got_repo_get_path_gitconfig(repo);
1377 if (gitconfig_path == NULL) {
1378 err = got_error_from_errno("got_repo_get_path_gitconfig");
1381 gitconfig_file = fopen(gitconfig_path, "ae");
1382 if (gitconfig_file == NULL) {
1383 err = got_error_from_errno2("fopen", gitconfig_path);
1386 if (fetch_all_branches) {
1387 if (mirror_references) {
1388 if (asprintf(&branches,
1389 "\tfetch = refs/heads/*:refs/heads/*\n") == -1) {
1390 err = got_error_from_errno("asprintf");
1393 } else if (asprintf(&branches,
1394 "\tfetch = refs/heads/*:refs/remotes/%s/*\n",
1395 GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1396 err = got_error_from_errno("asprintf");
1399 } else if (!TAILQ_EMPTY(wanted_branches)) {
1400 struct got_pathlist_entry *pe;
1401 TAILQ_FOREACH(pe, wanted_branches, entry) {
1403 branchname = pe->path;
1404 if (strncmp(branchname, "refs/heads/", 11) == 0)
1406 if (mirror_references) {
1408 "%s\tfetch = refs/heads/%s:refs/heads/%s\n",
1409 branches ? branches : "",
1410 branchname, branchname) == -1) {
1411 err = got_error_from_errno("asprintf");
1414 } else if (asprintf(&s,
1415 "%s\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1416 branches ? branches : "",
1417 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1418 branchname) == -1) {
1419 err = got_error_from_errno("asprintf");
1427 * If the server specified a default branch, use just that one.
1428 * Otherwise fall back to fetching all branches on next fetch.
1430 if (default_branch) {
1431 branchname = default_branch;
1432 if (strncmp(branchname, "refs/heads/", 11) == 0)
1435 branchname = "*"; /* fall back to all branches */
1436 if (mirror_references) {
1437 if (asprintf(&branches,
1438 "\tfetch = refs/heads/%s:refs/heads/%s\n",
1439 branchname, branchname) == -1) {
1440 err = got_error_from_errno("asprintf");
1443 } else if (asprintf(&branches,
1444 "\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1445 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1446 branchname) == -1) {
1447 err = got_error_from_errno("asprintf");
1451 if (!TAILQ_EMPTY(wanted_refs)) {
1452 struct got_pathlist_entry *pe;
1453 TAILQ_FOREACH(pe, wanted_refs, entry) {
1455 const char *refname = pe->path;
1456 if (strncmp(refname, "refs/", 5) == 0)
1458 if (mirror_references) {
1460 "%s\tfetch = refs/%s:refs/%s\n",
1461 refs ? refs : "", refname, refname) == -1) {
1462 err = got_error_from_errno("asprintf");
1465 } else if (asprintf(&s,
1466 "%s\tfetch = refs/%s:refs/remotes/%s/%s\n",
1468 refname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1470 err = got_error_from_errno("asprintf");
1478 if (asprintf(&gitconfig,
1483 "\tfetch = refs/tags/*:refs/tags/*\n",
1484 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url, branches ? branches : "",
1485 refs ? refs : "") == -1) {
1486 err = got_error_from_errno("asprintf");
1489 n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1490 if (n != strlen(gitconfig)) {
1491 err = got_ferror(gitconfig_file, GOT_ERR_IO);
1495 if (gitconfig_file && fclose(gitconfig_file) == EOF && err == NULL)
1496 err = got_error_from_errno2("fclose", gitconfig_path);
1497 free(gitconfig_path);
1502 static const struct got_error *
1503 create_config_files(const char *proto, const char *host, const char *port,
1504 const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1505 int mirror_references, struct got_pathlist_head *symrefs,
1506 struct got_pathlist_head *wanted_branches,
1507 struct got_pathlist_head *wanted_refs, struct got_repository *repo)
1509 const struct got_error *err = NULL;
1510 const char *default_branch = NULL;
1511 struct got_pathlist_entry *pe;
1514 * If we asked for a set of wanted branches then use the first
1517 if (!TAILQ_EMPTY(wanted_branches)) {
1518 pe = TAILQ_FIRST(wanted_branches);
1519 default_branch = pe->path;
1521 /* First HEAD ref listed by server is the default branch. */
1522 TAILQ_FOREACH(pe, symrefs, entry) {
1523 const char *refname = pe->path;
1524 const char *target = pe->data;
1526 if (strcmp(refname, GOT_REF_HEAD) != 0)
1529 default_branch = target;
1534 /* Create got.conf(5). */
1535 err = create_gotconfig(proto, host, port, remote_repo_path,
1536 default_branch, fetch_all_branches, wanted_branches,
1537 wanted_refs, mirror_references, repo);
1541 /* Create a config file Git can understand. */
1542 return create_gitconfig(git_url, default_branch, fetch_all_branches,
1543 wanted_branches, wanted_refs, mirror_references, repo);
1546 static const struct got_error *
1547 cmd_clone(int argc, char *argv[])
1549 const struct got_error *error = NULL;
1550 const char *uri, *dirname;
1551 char *proto, *host, *port, *repo_name, *server_path;
1552 char *default_destdir = NULL, *id_str = NULL;
1553 const char *repo_path;
1554 struct got_repository *repo = NULL;
1555 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1556 struct got_pathlist_entry *pe;
1557 struct got_object_id *pack_hash = NULL;
1558 int ch, fetchfd = -1, fetchstatus;
1559 pid_t fetchpid = -1;
1560 struct got_fetch_progress_arg fpa;
1561 char *git_url = NULL;
1562 int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
1563 int list_refs_only = 0;
1564 int *pack_fds = NULL;
1567 TAILQ_INIT(&symrefs);
1568 TAILQ_INIT(&wanted_branches);
1569 TAILQ_INIT(&wanted_refs);
1571 while ((ch = getopt(argc, argv, "ab:lmqR:v")) != -1) {
1574 fetch_all_branches = 1;
1577 error = got_pathlist_append(&wanted_branches,
1586 mirror_references = 1;
1592 error = got_pathlist_append(&wanted_refs,
1600 else if (verbosity < 3)
1611 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1612 option_conflict('a', 'b');
1613 if (list_refs_only) {
1614 if (!TAILQ_EMPTY(&wanted_branches))
1615 option_conflict('l', 'b');
1616 if (fetch_all_branches)
1617 option_conflict('l', 'a');
1618 if (mirror_references)
1619 option_conflict('l', 'm');
1620 if (!TAILQ_EMPTY(&wanted_refs))
1621 option_conflict('l', 'R');
1633 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
1638 if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1639 host, port ? ":" : "", port ? port : "",
1640 server_path[0] != '/' ? "/" : "", server_path) == -1) {
1641 error = got_error_from_errno("asprintf");
1645 if (strcmp(proto, "git") == 0) {
1647 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1648 "sendfd dns inet unveil", NULL) == -1)
1651 } else if (strcmp(proto, "git+ssh") == 0 ||
1652 strcmp(proto, "ssh") == 0) {
1654 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1655 "sendfd unveil", NULL) == -1)
1658 } else if (strcmp(proto, "http") == 0 ||
1659 strcmp(proto, "git+http") == 0) {
1660 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1663 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1666 if (dirname == NULL) {
1667 if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1668 error = got_error_from_errno("asprintf");
1671 repo_path = default_destdir;
1673 repo_path = dirname;
1675 if (!list_refs_only) {
1676 error = got_path_mkdir(repo_path);
1678 (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1679 !(error->code == GOT_ERR_ERRNO && errno == EEXIST)))
1681 if (!got_path_dir_is_empty(repo_path)) {
1682 error = got_error_path(repo_path,
1683 GOT_ERR_DIR_NOT_EMPTY);
1688 error = got_dial_apply_unveil(proto);
1692 error = apply_unveil(repo_path, 0, NULL);
1697 printf("Connecting to %s\n", git_url);
1699 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1700 server_path, verbosity);
1704 if (!list_refs_only) {
1705 error = got_repo_init(repo_path, NULL);
1708 error = got_repo_pack_fds_open(&pack_fds);
1711 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
1716 fpa.last_scaled_size[0] = '\0';
1717 fpa.last_p_indexed = -1;
1718 fpa.last_p_resolved = -1;
1719 fpa.verbosity = verbosity;
1720 fpa.create_configs = 1;
1721 fpa.configs_created = 0;
1723 fpa.config_info.symrefs = &symrefs;
1724 fpa.config_info.wanted_branches = &wanted_branches;
1725 fpa.config_info.wanted_refs = &wanted_refs;
1726 fpa.config_info.proto = proto;
1727 fpa.config_info.host = host;
1728 fpa.config_info.port = port;
1729 fpa.config_info.remote_repo_path = server_path;
1730 fpa.config_info.git_url = git_url;
1731 fpa.config_info.fetch_all_branches = fetch_all_branches;
1732 fpa.config_info.mirror_references = mirror_references;
1733 error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1734 GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1735 fetch_all_branches, &wanted_branches, &wanted_refs,
1736 list_refs_only, verbosity, fetchfd, repo,
1737 fetch_progress, &fpa);
1741 if (list_refs_only) {
1742 error = list_remote_refs(&symrefs, &refs);
1746 if (pack_hash == NULL) {
1747 error = got_error_fmt(GOT_ERR_FETCH_FAILED, "%s",
1748 "server sent an empty pack file");
1751 error = got_object_id_str(&id_str, pack_hash);
1755 printf("\nFetched %s.pack\n", id_str);
1758 /* Set up references provided with the pack file. */
1759 TAILQ_FOREACH(pe, &refs, entry) {
1760 const char *refname = pe->path;
1761 struct got_object_id *id = pe->data;
1762 char *remote_refname;
1764 if (is_wanted_ref(&wanted_refs, refname) &&
1765 !mirror_references) {
1766 error = create_wanted_ref(refname, id,
1767 GOT_FETCH_DEFAULT_REMOTE_NAME,
1768 verbosity - 1, repo);
1774 error = create_ref(refname, id, verbosity - 1, repo);
1778 if (mirror_references)
1781 if (strncmp("refs/heads/", refname, 11) != 0)
1784 if (asprintf(&remote_refname,
1785 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1786 refname + 11) == -1) {
1787 error = got_error_from_errno("asprintf");
1790 error = create_ref(remote_refname, id, verbosity - 1, repo);
1791 free(remote_refname);
1796 /* Set the HEAD reference if the server provided one. */
1797 TAILQ_FOREACH(pe, &symrefs, entry) {
1798 struct got_reference *target_ref;
1799 const char *refname = pe->path;
1800 const char *target = pe->data;
1801 char *remote_refname = NULL, *remote_target = NULL;
1803 if (strcmp(refname, GOT_REF_HEAD) != 0)
1806 error = got_ref_open(&target_ref, repo, target, 0);
1808 if (error->code == GOT_ERR_NOT_REF) {
1815 error = create_symref(refname, target_ref, verbosity, repo);
1816 got_ref_close(target_ref);
1820 if (mirror_references)
1823 if (strncmp("refs/heads/", target, 11) != 0)
1826 if (asprintf(&remote_refname,
1827 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1829 error = got_error_from_errno("asprintf");
1832 if (asprintf(&remote_target,
1833 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1834 target + 11) == -1) {
1835 error = got_error_from_errno("asprintf");
1836 free(remote_refname);
1839 error = got_ref_open(&target_ref, repo, remote_target, 0);
1841 free(remote_refname);
1842 free(remote_target);
1843 if (error->code == GOT_ERR_NOT_REF) {
1849 error = create_symref(remote_refname, target_ref,
1850 verbosity - 1, repo);
1851 free(remote_refname);
1852 free(remote_target);
1853 got_ref_close(target_ref);
1859 * We failed to set the HEAD reference. If we asked for
1860 * a set of wanted branches use the first of one of those
1861 * which could be fetched instead.
1863 TAILQ_FOREACH(pe, &wanted_branches, entry) {
1864 const char *target = pe->path;
1865 struct got_reference *target_ref;
1867 error = got_ref_open(&target_ref, repo, target, 0);
1869 if (error->code == GOT_ERR_NOT_REF) {
1876 error = create_symref(GOT_REF_HEAD, target_ref,
1878 got_ref_close(target_ref);
1884 if (!fpa.configs_created && pe != NULL) {
1885 error = create_config_files(fpa.config_info.proto,
1886 fpa.config_info.host, fpa.config_info.port,
1887 fpa.config_info.remote_repo_path,
1888 fpa.config_info.git_url,
1889 fpa.config_info.fetch_all_branches,
1890 fpa.config_info.mirror_references,
1891 fpa.config_info.symrefs,
1892 fpa.config_info.wanted_branches,
1893 fpa.config_info.wanted_refs, fpa.repo);
1900 printf("Created %s repository '%s'\n",
1901 mirror_references ? "mirrored" : "cloned", repo_path);
1904 const struct got_error *pack_err =
1905 got_repo_pack_fds_close(pack_fds);
1910 if (kill(fetchpid, SIGTERM) == -1)
1911 error = got_error_from_errno("kill");
1912 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1913 error = got_error_from_errno("waitpid");
1915 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1916 error = got_error_from_errno("close");
1918 const struct got_error *close_err = got_repo_close(repo);
1922 got_pathlist_free(&refs, GOT_PATHLIST_FREE_ALL);
1923 got_pathlist_free(&symrefs, GOT_PATHLIST_FREE_ALL);
1924 got_pathlist_free(&wanted_branches, GOT_PATHLIST_FREE_NONE);
1925 got_pathlist_free(&wanted_refs, GOT_PATHLIST_FREE_NONE);
1932 free(default_destdir);
1937 static const struct got_error *
1938 update_ref(struct got_reference *ref, struct got_object_id *new_id,
1939 int replace_tags, int verbosity, struct got_repository *repo)
1941 const struct got_error *err = NULL;
1942 char *new_id_str = NULL;
1943 struct got_object_id *old_id = NULL;
1945 err = got_object_id_str(&new_id_str, new_id);
1949 if (!replace_tags &&
1950 strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1951 err = got_ref_resolve(&old_id, repo, ref);
1954 if (got_object_id_cmp(old_id, new_id) == 0)
1956 if (verbosity >= 0) {
1957 printf("Rejecting update of existing tag %s: %s\n",
1958 got_ref_get_name(ref), new_id_str);
1963 if (got_ref_is_symbolic(ref)) {
1964 if (verbosity >= 0) {
1965 printf("Replacing reference %s: %s\n",
1966 got_ref_get_name(ref),
1967 got_ref_get_symref_target(ref));
1969 err = got_ref_change_symref_to_ref(ref, new_id);
1972 err = got_ref_write(ref, repo);
1976 err = got_ref_resolve(&old_id, repo, ref);
1979 if (got_object_id_cmp(old_id, new_id) == 0)
1982 err = got_ref_change_ref(ref, new_id);
1985 err = got_ref_write(ref, repo);
1991 printf("Updated %s: %s\n", got_ref_get_name(ref),
1999 static const struct got_error *
2000 update_symref(const char *refname, struct got_reference *target_ref,
2001 int verbosity, struct got_repository *repo)
2003 const struct got_error *err = NULL, *unlock_err;
2004 struct got_reference *symref;
2005 int symref_is_locked = 0;
2007 err = got_ref_open(&symref, repo, refname, 1);
2009 if (err->code != GOT_ERR_NOT_REF)
2011 err = got_ref_alloc_symref(&symref, refname, target_ref);
2015 err = got_ref_write(symref, repo);
2020 printf("Created reference %s: %s\n",
2021 got_ref_get_name(symref),
2022 got_ref_get_symref_target(symref));
2024 symref_is_locked = 1;
2026 if (strcmp(got_ref_get_symref_target(symref),
2027 got_ref_get_name(target_ref)) == 0)
2030 err = got_ref_change_symref(symref,
2031 got_ref_get_name(target_ref));
2035 err = got_ref_write(symref, repo);
2040 printf("Updated %s: %s\n", got_ref_get_name(symref),
2041 got_ref_get_symref_target(symref));
2045 if (symref_is_locked) {
2046 unlock_err = got_ref_unlock(symref);
2047 if (unlock_err && err == NULL)
2050 got_ref_close(symref);
2057 fprintf(stderr, "usage: %s fetch [-adlqtvX] [-b branch] "
2058 "[-R reference] [-r repository-path] [remote-repository]\n",
2063 static const struct got_error *
2064 delete_missing_ref(struct got_reference *ref,
2065 int verbosity, struct got_repository *repo)
2067 const struct got_error *err = NULL;
2068 struct got_object_id *id = NULL;
2069 char *id_str = NULL;
2071 if (got_ref_is_symbolic(ref)) {
2072 err = got_ref_delete(ref, repo);
2075 if (verbosity >= 0) {
2076 printf("Deleted %s: %s\n",
2077 got_ref_get_name(ref),
2078 got_ref_get_symref_target(ref));
2081 err = got_ref_resolve(&id, repo, ref);
2084 err = got_object_id_str(&id_str, id);
2088 err = got_ref_delete(ref, repo);
2091 if (verbosity >= 0) {
2092 printf("Deleted %s: %s\n",
2093 got_ref_get_name(ref), id_str);
2102 static const struct got_error *
2103 delete_missing_refs(struct got_pathlist_head *their_refs,
2104 struct got_pathlist_head *their_symrefs,
2105 const struct got_remote_repo *remote,
2106 int verbosity, struct got_repository *repo)
2108 const struct got_error *err = NULL, *unlock_err;
2109 struct got_reflist_head my_refs;
2110 struct got_reflist_entry *re;
2111 struct got_pathlist_entry *pe;
2112 char *remote_namespace = NULL;
2113 char *local_refname = NULL;
2115 TAILQ_INIT(&my_refs);
2117 if (asprintf(&remote_namespace, "refs/remotes/%s/", remote->name)
2119 return got_error_from_errno("asprintf");
2121 err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
2125 TAILQ_FOREACH(re, &my_refs, entry) {
2126 const char *refname = got_ref_get_name(re->ref);
2127 const char *their_refname;
2129 if (remote->mirror_references) {
2130 their_refname = refname;
2132 if (strncmp(refname, remote_namespace,
2133 strlen(remote_namespace)) == 0) {
2134 if (strcmp(refname + strlen(remote_namespace),
2137 if (asprintf(&local_refname, "refs/heads/%s",
2138 refname + strlen(remote_namespace)) == -1) {
2139 err = got_error_from_errno("asprintf");
2142 } else if (strncmp(refname, "refs/tags/", 10) != 0)
2145 their_refname = local_refname;
2148 TAILQ_FOREACH(pe, their_refs, entry) {
2149 if (strcmp(their_refname, pe->path) == 0)
2155 TAILQ_FOREACH(pe, their_symrefs, entry) {
2156 if (strcmp(their_refname, pe->path) == 0)
2162 err = delete_missing_ref(re->ref, verbosity, repo);
2166 if (local_refname) {
2167 struct got_reference *ref;
2168 err = got_ref_open(&ref, repo, local_refname, 1);
2170 if (err->code != GOT_ERR_NOT_REF)
2172 free(local_refname);
2173 local_refname = NULL;
2176 err = delete_missing_ref(ref, verbosity, repo);
2179 unlock_err = got_ref_unlock(ref);
2181 if (unlock_err && err == NULL) {
2186 free(local_refname);
2187 local_refname = NULL;
2191 got_ref_list_free(&my_refs);
2192 free(remote_namespace);
2193 free(local_refname);
2197 static const struct got_error *
2198 update_wanted_ref(const char *refname, struct got_object_id *id,
2199 const char *remote_repo_name, int verbosity, struct got_repository *repo)
2201 const struct got_error *err, *unlock_err;
2202 char *remote_refname;
2203 struct got_reference *ref;
2205 if (strncmp("refs/", refname, 5) == 0)
2208 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2209 remote_repo_name, refname) == -1)
2210 return got_error_from_errno("asprintf");
2212 err = got_ref_open(&ref, repo, remote_refname, 1);
2214 if (err->code != GOT_ERR_NOT_REF)
2216 err = create_ref(remote_refname, id, verbosity, repo);
2218 err = update_ref(ref, id, 0, verbosity, repo);
2219 unlock_err = got_ref_unlock(ref);
2220 if (unlock_err && err == NULL)
2225 free(remote_refname);
2229 static const struct got_error *
2230 delete_ref(struct got_repository *repo, struct got_reference *ref)
2232 const struct got_error *err = NULL;
2233 struct got_object_id *id = NULL;
2234 char *id_str = NULL;
2237 if (got_ref_is_symbolic(ref)) {
2238 target = got_ref_get_symref_target(ref);
2240 err = got_ref_resolve(&id, repo, ref);
2243 err = got_object_id_str(&id_str, id);
2249 err = got_ref_delete(ref, repo);
2253 printf("Deleted %s: %s\n", got_ref_get_name(ref), target);
2260 static const struct got_error *
2261 delete_refs_for_remote(struct got_repository *repo, const char *remote_name)
2263 const struct got_error *err = NULL;
2264 struct got_reflist_head refs;
2265 struct got_reflist_entry *re;
2270 if (asprintf(&prefix, "refs/remotes/%s", remote_name) == -1) {
2271 err = got_error_from_errno("asprintf");
2274 err = got_ref_list(&refs, repo, prefix, got_ref_cmp_by_name, NULL);
2278 TAILQ_FOREACH(re, &refs, entry)
2279 delete_ref(repo, re->ref);
2281 got_ref_list_free(&refs);
2285 static const struct got_error *
2286 cmd_fetch(int argc, char *argv[])
2288 const struct got_error *error = NULL, *unlock_err;
2289 char *cwd = NULL, *repo_path = NULL;
2290 const char *remote_name;
2291 char *proto = NULL, *host = NULL, *port = NULL;
2292 char *repo_name = NULL, *server_path = NULL;
2293 const struct got_remote_repo *remotes, *remote = NULL;
2295 char *id_str = NULL;
2296 struct got_repository *repo = NULL;
2297 struct got_worktree *worktree = NULL;
2298 const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
2299 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
2300 struct got_pathlist_entry *pe;
2301 struct got_object_id *pack_hash = NULL;
2302 int i, ch, fetchfd = -1, fetchstatus;
2303 pid_t fetchpid = -1;
2304 struct got_fetch_progress_arg fpa;
2305 int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
2306 int delete_refs = 0, replace_tags = 0, delete_remote = 0;
2307 int *pack_fds = NULL;
2310 TAILQ_INIT(&symrefs);
2311 TAILQ_INIT(&wanted_branches);
2312 TAILQ_INIT(&wanted_refs);
2314 while ((ch = getopt(argc, argv, "ab:dlqR:r:tvX")) != -1) {
2317 fetch_all_branches = 1;
2320 error = got_pathlist_append(&wanted_branches,
2335 error = got_pathlist_append(&wanted_refs,
2341 repo_path = realpath(optarg, NULL);
2342 if (repo_path == NULL)
2343 return got_error_from_errno2("realpath",
2345 got_path_strip_trailing_slashes(repo_path);
2353 else if (verbosity < 3)
2367 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
2368 option_conflict('a', 'b');
2369 if (list_refs_only) {
2370 if (!TAILQ_EMPTY(&wanted_branches))
2371 option_conflict('l', 'b');
2372 if (fetch_all_branches)
2373 option_conflict('l', 'a');
2375 option_conflict('l', 'd');
2377 option_conflict('l', 'X');
2379 if (delete_remote) {
2380 if (fetch_all_branches)
2381 option_conflict('X', 'a');
2382 if (!TAILQ_EMPTY(&wanted_branches))
2383 option_conflict('X', 'b');
2385 option_conflict('X', 'd');
2387 option_conflict('X', 't');
2388 if (!TAILQ_EMPTY(&wanted_refs))
2389 option_conflict('X', 'R');
2394 errx(1, "-X option requires a remote name");
2395 remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
2396 } else if (argc == 1)
2397 remote_name = argv[0];
2401 cwd = getcwd(NULL, 0);
2403 error = got_error_from_errno("getcwd");
2407 error = got_repo_pack_fds_open(&pack_fds);
2411 if (repo_path == NULL) {
2412 error = got_worktree_open(&worktree, cwd);
2413 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2419 strdup(got_worktree_get_repo_path(worktree));
2420 if (repo_path == NULL)
2421 error = got_error_from_errno("strdup");
2425 repo_path = strdup(cwd);
2426 if (repo_path == NULL) {
2427 error = got_error_from_errno("strdup");
2433 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
2437 if (delete_remote) {
2438 error = delete_refs_for_remote(repo, remote_name);
2439 goto done; /* nothing else to do */
2443 worktree_conf = got_worktree_get_gotconfig(worktree);
2444 if (worktree_conf) {
2445 got_gotconfig_get_remotes(&nremotes, &remotes,
2447 for (i = 0; i < nremotes; i++) {
2448 if (strcmp(remotes[i].name, remote_name) == 0) {
2449 remote = &remotes[i];
2455 if (remote == NULL) {
2456 repo_conf = got_repo_get_gotconfig(repo);
2458 got_gotconfig_get_remotes(&nremotes, &remotes,
2460 for (i = 0; i < nremotes; i++) {
2461 if (strcmp(remotes[i].name, remote_name) == 0) {
2462 remote = &remotes[i];
2468 if (remote == NULL) {
2469 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
2470 for (i = 0; i < nremotes; i++) {
2471 if (strcmp(remotes[i].name, remote_name) == 0) {
2472 remote = &remotes[i];
2477 if (remote == NULL) {
2478 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
2482 if (TAILQ_EMPTY(&wanted_branches)) {
2483 if (!fetch_all_branches)
2484 fetch_all_branches = remote->fetch_all_branches;
2485 for (i = 0; i < remote->nfetch_branches; i++) {
2486 error = got_pathlist_append(&wanted_branches,
2487 remote->fetch_branches[i], NULL);
2492 if (TAILQ_EMPTY(&wanted_refs)) {
2493 for (i = 0; i < remote->nfetch_refs; i++) {
2494 error = got_pathlist_append(&wanted_refs,
2495 remote->fetch_refs[i], NULL);
2501 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
2502 &repo_name, remote->fetch_url);
2506 if (strcmp(proto, "git") == 0) {
2508 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2509 "sendfd dns inet unveil", NULL) == -1)
2512 } else if (strcmp(proto, "git+ssh") == 0 ||
2513 strcmp(proto, "ssh") == 0) {
2515 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2516 "sendfd unveil", NULL) == -1)
2519 } else if (strcmp(proto, "http") == 0 ||
2520 strcmp(proto, "git+http") == 0) {
2521 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
2524 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
2528 error = got_dial_apply_unveil(proto);
2532 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
2536 if (verbosity >= 0) {
2537 printf("Connecting to \"%s\" %s://%s%s%s%s%s\n",
2538 remote->name, proto, host,
2539 port ? ":" : "", port ? port : "",
2540 *server_path == '/' ? "" : "/", server_path);
2543 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
2544 server_path, verbosity);
2548 fpa.last_scaled_size[0] = '\0';
2549 fpa.last_p_indexed = -1;
2550 fpa.last_p_resolved = -1;
2551 fpa.verbosity = verbosity;
2553 fpa.create_configs = 0;
2554 fpa.configs_created = 0;
2555 memset(&fpa.config_info, 0, sizeof(fpa.config_info));
2556 error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
2557 remote->mirror_references, fetch_all_branches, &wanted_branches,
2558 &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
2559 fetch_progress, &fpa);
2563 if (list_refs_only) {
2564 error = list_remote_refs(&symrefs, &refs);
2568 if (pack_hash == NULL) {
2570 printf("Already up-to-date\n");
2571 } else if (verbosity >= 0) {
2572 error = got_object_id_str(&id_str, pack_hash);
2575 printf("\nFetched %s.pack\n", id_str);
2580 /* Update references provided with the pack file. */
2581 TAILQ_FOREACH(pe, &refs, entry) {
2582 const char *refname = pe->path;
2583 struct got_object_id *id = pe->data;
2584 struct got_reference *ref;
2585 char *remote_refname;
2587 if (is_wanted_ref(&wanted_refs, refname) &&
2588 !remote->mirror_references) {
2589 error = update_wanted_ref(refname, id,
2590 remote->name, verbosity, repo);
2596 if (remote->mirror_references ||
2597 strncmp("refs/tags/", refname, 10) == 0) {
2598 error = got_ref_open(&ref, repo, refname, 1);
2600 if (error->code != GOT_ERR_NOT_REF)
2602 error = create_ref(refname, id, verbosity,
2607 error = update_ref(ref, id, replace_tags,
2609 unlock_err = got_ref_unlock(ref);
2610 if (unlock_err && error == NULL)
2616 } else if (strncmp("refs/heads/", refname, 11) == 0) {
2617 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2618 remote_name, refname + 11) == -1) {
2619 error = got_error_from_errno("asprintf");
2623 error = got_ref_open(&ref, repo, remote_refname, 1);
2625 if (error->code != GOT_ERR_NOT_REF)
2627 error = create_ref(remote_refname, id,
2632 error = update_ref(ref, id, replace_tags,
2634 unlock_err = got_ref_unlock(ref);
2635 if (unlock_err && error == NULL)
2642 /* Also create a local branch if none exists yet. */
2643 error = got_ref_open(&ref, repo, refname, 1);
2645 if (error->code != GOT_ERR_NOT_REF)
2647 error = create_ref(refname, id, verbosity,
2652 unlock_err = got_ref_unlock(ref);
2653 if (unlock_err && error == NULL)
2660 error = delete_missing_refs(&refs, &symrefs, remote,
2666 if (!remote->mirror_references) {
2667 /* Update remote HEAD reference if the server provided one. */
2668 TAILQ_FOREACH(pe, &symrefs, entry) {
2669 struct got_reference *target_ref;
2670 const char *refname = pe->path;
2671 const char *target = pe->data;
2672 char *remote_refname = NULL, *remote_target = NULL;
2674 if (strcmp(refname, GOT_REF_HEAD) != 0)
2677 if (strncmp("refs/heads/", target, 11) != 0)
2680 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2681 remote->name, refname) == -1) {
2682 error = got_error_from_errno("asprintf");
2685 if (asprintf(&remote_target, "refs/remotes/%s/%s",
2686 remote->name, target + 11) == -1) {
2687 error = got_error_from_errno("asprintf");
2688 free(remote_refname);
2692 error = got_ref_open(&target_ref, repo, remote_target,
2695 free(remote_refname);
2696 free(remote_target);
2697 if (error->code == GOT_ERR_NOT_REF) {
2703 error = update_symref(remote_refname, target_ref,
2705 free(remote_refname);
2706 free(remote_target);
2707 got_ref_close(target_ref);
2714 if (kill(fetchpid, SIGTERM) == -1)
2715 error = got_error_from_errno("kill");
2716 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
2717 error = got_error_from_errno("waitpid");
2719 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
2720 error = got_error_from_errno("close");
2722 const struct got_error *close_err = got_repo_close(repo);
2727 got_worktree_close(worktree);
2729 const struct got_error *pack_err =
2730 got_repo_pack_fds_close(pack_fds);
2734 got_pathlist_free(&refs, GOT_PATHLIST_FREE_ALL);
2735 got_pathlist_free(&symrefs, GOT_PATHLIST_FREE_ALL);
2736 got_pathlist_free(&wanted_branches, GOT_PATHLIST_FREE_NONE);
2737 got_pathlist_free(&wanted_refs, GOT_PATHLIST_FREE_NONE);
2752 usage_checkout(void)
2754 fprintf(stderr, "usage: %s checkout [-Eq] [-b branch] [-c commit] "
2755 "[-p path-prefix] repository-path [work-tree-path]\n",
2761 show_worktree_base_ref_warning(void)
2763 fprintf(stderr, "%s: warning: could not create a reference "
2764 "to the work tree's base commit; the commit could be "
2765 "garbage-collected by Git or 'gotadmin cleanup'; making the "
2766 "repository writable and running 'got update' will prevent this\n",
2770 struct got_checkout_progress_arg {
2771 const char *worktree_path;
2772 int had_base_commit_ref_error;
2776 static const struct got_error *
2777 checkout_progress(void *arg, unsigned char status, const char *path)
2779 struct got_checkout_progress_arg *a = arg;
2781 /* Base commit bump happens silently. */
2782 if (status == GOT_STATUS_BUMP_BASE)
2785 if (status == GOT_STATUS_BASE_REF_ERR) {
2786 a->had_base_commit_ref_error = 1;
2790 while (path[0] == '/')
2793 if (a->verbosity >= 0)
2794 printf("%c %s/%s\n", status, a->worktree_path, path);
2799 static const struct got_error *
2800 check_cancelled(void *arg)
2802 if (sigint_received || sigpipe_received)
2803 return got_error(GOT_ERR_CANCELLED);
2807 static const struct got_error *
2808 check_linear_ancestry(struct got_object_id *commit_id,
2809 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2810 struct got_repository *repo)
2812 const struct got_error *err = NULL;
2813 struct got_object_id *yca_id;
2815 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2816 commit_id, base_commit_id, 1, repo, check_cancelled, NULL);
2821 return got_error(GOT_ERR_ANCESTRY);
2824 * Require a straight line of history between the target commit
2825 * and the work tree's base commit.
2827 * Non-linear situations such as this require a rebase:
2829 * (commit) D F (base_commit)
2837 * 'got update' only handles linear cases:
2838 * Update forwards in time: A (base/yca) - B - C - D (commit)
2839 * Update backwards in time: D (base) - C - B - A (commit/yca)
2841 if (allow_forwards_in_time_only) {
2842 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2843 return got_error(GOT_ERR_ANCESTRY);
2844 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2845 got_object_id_cmp(base_commit_id, yca_id) != 0)
2846 return got_error(GOT_ERR_ANCESTRY);
2852 static const struct got_error *
2853 check_same_branch(struct got_object_id *commit_id,
2854 struct got_reference *head_ref, struct got_object_id *yca_id,
2855 struct got_repository *repo)
2857 const struct got_error *err = NULL;
2858 struct got_commit_graph *graph = NULL;
2859 struct got_object_id *head_commit_id = NULL;
2860 int is_same_branch = 0;
2862 err = got_ref_resolve(&head_commit_id, repo, head_ref);
2866 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
2870 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
2875 err = got_commit_graph_open(&graph, "/", 1);
2879 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2880 check_cancelled, NULL);
2885 struct got_object_id id;
2887 err = got_commit_graph_iter_next(&id, graph, repo,
2888 check_cancelled, NULL);
2890 if (err->code == GOT_ERR_ITER_COMPLETED)
2895 if (yca_id && got_object_id_cmp(&id, yca_id) == 0)
2897 if (got_object_id_cmp(&id, commit_id) == 0) {
2904 got_commit_graph_close(graph);
2905 free(head_commit_id);
2906 if (!err && !is_same_branch)
2907 err = got_error(GOT_ERR_ANCESTRY);
2911 static const struct got_error *
2912 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2914 static char msg[512];
2915 const char *branch_name;
2917 if (got_ref_is_symbolic(ref))
2918 branch_name = got_ref_get_symref_target(ref);
2920 branch_name = got_ref_get_name(ref);
2922 if (strncmp("refs/heads/", branch_name, 11) == 0)
2925 snprintf(msg, sizeof(msg),
2926 "target commit is not contained in branch '%s'; "
2927 "the branch to use must be specified with -b; "
2928 "if necessary a new branch can be created for "
2929 "this commit with 'got branch -c %s BRANCH_NAME'",
2930 branch_name, commit_id_str);
2932 return got_error_msg(GOT_ERR_ANCESTRY, msg);
2935 static const struct got_error *
2936 cmd_checkout(int argc, char *argv[])
2938 const struct got_error *error = NULL;
2939 struct got_repository *repo = NULL;
2940 struct got_reference *head_ref = NULL, *ref = NULL;
2941 struct got_worktree *worktree = NULL;
2942 char *repo_path = NULL;
2943 char *worktree_path = NULL;
2944 const char *path_prefix = "";
2945 const char *branch_name = GOT_REF_HEAD, *refname = NULL;
2946 char *commit_id_str = NULL;
2947 struct got_object_id *commit_id = NULL;
2949 int ch, same_path_prefix, allow_nonempty = 0, verbosity = 0;
2950 struct got_pathlist_head paths;
2951 struct got_checkout_progress_arg cpa;
2952 int *pack_fds = NULL;
2956 while ((ch = getopt(argc, argv, "b:c:Ep:q")) != -1) {
2959 branch_name = optarg;
2962 commit_id_str = strdup(optarg);
2963 if (commit_id_str == NULL)
2964 return got_error_from_errno("strdup");
2970 path_prefix = optarg;
2985 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2986 "unveil", NULL) == -1)
2990 char *base, *dotgit;
2992 repo_path = realpath(argv[0], NULL);
2993 if (repo_path == NULL)
2994 return got_error_from_errno2("realpath", argv[0]);
2995 cwd = getcwd(NULL, 0);
2997 error = got_error_from_errno("getcwd");
3004 error = got_path_basename(&base, path);
3007 dotgit = strstr(base, ".git");
3010 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
3011 error = got_error_from_errno("asprintf");
3016 } else if (argc == 2) {
3017 repo_path = realpath(argv[0], NULL);
3018 if (repo_path == NULL) {
3019 error = got_error_from_errno2("realpath", argv[0]);
3022 worktree_path = realpath(argv[1], NULL);
3023 if (worktree_path == NULL) {
3024 if (errno != ENOENT) {
3025 error = got_error_from_errno2("realpath",
3029 worktree_path = strdup(argv[1]);
3030 if (worktree_path == NULL) {
3031 error = got_error_from_errno("strdup");
3038 got_path_strip_trailing_slashes(repo_path);
3039 got_path_strip_trailing_slashes(worktree_path);
3041 error = got_repo_pack_fds_open(&pack_fds);
3045 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3049 /* Pre-create work tree path for unveil(2) */
3050 error = got_path_mkdir(worktree_path);
3052 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
3053 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
3055 if (!allow_nonempty &&
3056 !got_path_dir_is_empty(worktree_path)) {
3057 error = got_error_path(worktree_path,
3058 GOT_ERR_DIR_NOT_EMPTY);
3063 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
3067 error = got_ref_open(&head_ref, repo, branch_name, 0);
3071 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
3072 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
3075 error = got_worktree_open(&worktree, worktree_path);
3079 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
3083 if (!same_path_prefix) {
3084 error = got_error(GOT_ERR_PATH_PREFIX);
3088 if (commit_id_str) {
3089 struct got_reflist_head refs;
3091 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3095 error = got_repo_match_object_id(&commit_id, NULL,
3096 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3097 got_ref_list_free(&refs);
3100 error = check_linear_ancestry(commit_id,
3101 got_worktree_get_base_commit_id(worktree), 0, repo);
3102 if (error != NULL) {
3103 if (error->code == GOT_ERR_ANCESTRY) {
3104 error = checkout_ancestry_error(
3105 head_ref, commit_id_str);
3109 error = check_same_branch(commit_id, head_ref, NULL, repo);
3111 if (error->code == GOT_ERR_ANCESTRY) {
3112 error = checkout_ancestry_error(
3113 head_ref, commit_id_str);
3117 error = got_worktree_set_base_commit_id(worktree, repo,
3121 /* Expand potentially abbreviated commit ID string. */
3122 free(commit_id_str);
3123 error = got_object_id_str(&commit_id_str, commit_id);
3127 commit_id = got_object_id_dup(
3128 got_worktree_get_base_commit_id(worktree));
3129 if (commit_id == NULL) {
3130 error = got_error_from_errno("got_object_id_dup");
3133 error = got_object_id_str(&commit_id_str, commit_id);
3138 error = got_pathlist_append(&paths, "", NULL);
3141 cpa.worktree_path = worktree_path;
3142 cpa.had_base_commit_ref_error = 0;
3143 cpa.verbosity = verbosity;
3144 error = got_worktree_checkout_files(worktree, &paths, repo,
3145 checkout_progress, &cpa, check_cancelled, NULL);
3149 if (got_ref_is_symbolic(head_ref)) {
3150 error = got_ref_resolve_symbolic(&ref, repo, head_ref);
3153 refname = got_ref_get_name(ref);
3155 refname = got_ref_get_name(head_ref);
3156 printf("Checked out %s: %s\n", refname, commit_id_str);
3157 printf("Now shut up and hack\n");
3158 if (cpa.had_base_commit_ref_error)
3159 show_worktree_base_ref_warning();
3162 const struct got_error *pack_err =
3163 got_repo_pack_fds_close(pack_fds);
3168 got_ref_close(head_ref);
3171 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
3172 free(commit_id_str);
3175 free(worktree_path);
3180 struct got_update_progress_arg {
3192 print_update_progress_stats(struct got_update_progress_arg *upa)
3194 if (!upa->did_something)
3197 if (upa->conflicts > 0)
3198 printf("Files with new merge conflicts: %d\n", upa->conflicts);
3199 if (upa->obstructed > 0)
3200 printf("File paths obstructed by a non-regular file: %d\n",
3202 if (upa->not_updated > 0)
3203 printf("Files not updated because of existing merge "
3204 "conflicts: %d\n", upa->not_updated);
3208 * The meaning of some status codes differs between merge-style operations and
3209 * update operations. For example, the ! status code means "file was missing"
3210 * if changes were merged into the work tree, and "missing file was restored"
3211 * if the work tree was updated. This function should be used by any operation
3212 * which merges changes into the work tree without updating the work tree.
3215 print_merge_progress_stats(struct got_update_progress_arg *upa)
3217 if (!upa->did_something)
3220 if (upa->conflicts > 0)
3221 printf("Files with new merge conflicts: %d\n", upa->conflicts);
3222 if (upa->obstructed > 0)
3223 printf("File paths obstructed by a non-regular file: %d\n",
3225 if (upa->missing > 0)
3226 printf("Files which had incoming changes but could not be "
3227 "found in the work tree: %d\n", upa->missing);
3228 if (upa->not_deleted > 0)
3229 printf("Files not deleted due to differences in deleted "
3230 "content: %d\n", upa->not_deleted);
3231 if (upa->unversioned > 0)
3232 printf("Files not merged because an unversioned file was "
3233 "found in the work tree: %d\n", upa->unversioned);
3239 fprintf(stderr, "usage: %s update [-q] [-b branch] [-c commit] "
3240 "[path ...]\n", getprogname());
3244 static const struct got_error *
3245 update_progress(void *arg, unsigned char status, const char *path)
3247 struct got_update_progress_arg *upa = arg;
3249 if (status == GOT_STATUS_EXISTS ||
3250 status == GOT_STATUS_BASE_REF_ERR)
3253 upa->did_something = 1;
3255 /* Base commit bump happens silently. */
3256 if (status == GOT_STATUS_BUMP_BASE)
3259 if (status == GOT_STATUS_CONFLICT)
3261 if (status == GOT_STATUS_OBSTRUCTED)
3263 if (status == GOT_STATUS_CANNOT_UPDATE)
3265 if (status == GOT_STATUS_MISSING)
3267 if (status == GOT_STATUS_CANNOT_DELETE)
3269 if (status == GOT_STATUS_UNVERSIONED)
3272 while (path[0] == '/')
3274 if (upa->verbosity >= 0)
3275 printf("%c %s\n", status, path);
3280 static const struct got_error *
3281 switch_head_ref(struct got_reference *head_ref,
3282 struct got_object_id *commit_id, struct got_worktree *worktree,
3283 struct got_repository *repo)
3285 const struct got_error *err = NULL;
3287 int ref_has_moved = 0;
3289 /* Trivial case: switching between two different references. */
3290 if (strcmp(got_ref_get_name(head_ref),
3291 got_worktree_get_head_ref_name(worktree)) != 0) {
3292 printf("Switching work tree from %s to %s\n",
3293 got_worktree_get_head_ref_name(worktree),
3294 got_ref_get_name(head_ref));
3295 return got_worktree_set_head_ref(worktree, head_ref);
3298 err = check_linear_ancestry(commit_id,
3299 got_worktree_get_base_commit_id(worktree), 0, repo);
3301 if (err->code != GOT_ERR_ANCESTRY)
3308 /* Switching to a rebased branch with the same reference name. */
3309 err = got_object_id_str(&base_id_str,
3310 got_worktree_get_base_commit_id(worktree));
3313 printf("Reference %s now points at a different branch\n",
3314 got_worktree_get_head_ref_name(worktree));
3315 printf("Switching work tree from %s to %s\n", base_id_str,
3316 got_worktree_get_head_ref_name(worktree));
3320 static const struct got_error *
3321 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
3323 const struct got_error *err;
3326 err = got_worktree_rebase_in_progress(&in_progress, worktree);
3330 return got_error(GOT_ERR_REBASING);
3332 err = got_worktree_histedit_in_progress(&in_progress, worktree);
3336 return got_error(GOT_ERR_HISTEDIT_BUSY);
3341 static const struct got_error *
3342 check_merge_in_progress(struct got_worktree *worktree,
3343 struct got_repository *repo)
3345 const struct got_error *err;
3348 err = got_worktree_merge_in_progress(&in_progress, worktree, repo);
3352 return got_error(GOT_ERR_MERGE_BUSY);
3357 static const struct got_error *
3358 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
3359 char *argv[], struct got_worktree *worktree)
3361 const struct got_error *err = NULL;
3363 struct got_pathlist_entry *new;
3369 return got_error_from_errno("strdup");
3370 return got_pathlist_append(paths, path, NULL);
3373 for (i = 0; i < argc; i++) {
3374 err = got_worktree_resolve_path(&path, worktree, argv[i]);
3377 err = got_pathlist_insert(&new, paths, path, NULL);
3378 if (err || new == NULL /* duplicate */) {
3388 static const struct got_error *
3389 wrap_not_worktree_error(const struct got_error *orig_err,
3390 const char *cmdname, const char *path)
3392 const struct got_error *err;
3393 struct got_repository *repo;
3394 static char msg[512];
3395 int *pack_fds = NULL;
3397 err = got_repo_pack_fds_open(&pack_fds);
3401 err = got_repo_open(&repo, path, NULL, pack_fds);
3405 snprintf(msg, sizeof(msg),
3406 "'got %s' needs a work tree in addition to a git repository\n"
3407 "Work trees can be checked out from this Git repository with "
3409 "The got(1) manual page contains more information.", cmdname);
3410 err = got_error_msg(GOT_ERR_NOT_WORKTREE, msg);
3411 got_repo_close(repo);
3413 const struct got_error *pack_err =
3414 got_repo_pack_fds_close(pack_fds);
3421 static const struct got_error *
3422 cmd_update(int argc, char *argv[])
3424 const struct got_error *error = NULL;
3425 struct got_repository *repo = NULL;
3426 struct got_worktree *worktree = NULL;
3427 char *worktree_path = NULL;
3428 struct got_object_id *commit_id = NULL;
3429 char *commit_id_str = NULL;
3430 const char *branch_name = NULL;
3431 struct got_reference *head_ref = NULL;
3432 struct got_pathlist_head paths;
3433 struct got_pathlist_entry *pe;
3434 int ch, verbosity = 0;
3435 struct got_update_progress_arg upa;
3436 int *pack_fds = NULL;
3440 while ((ch = getopt(argc, argv, "b:c:q")) != -1) {
3443 branch_name = optarg;
3446 commit_id_str = strdup(optarg);
3447 if (commit_id_str == NULL)
3448 return got_error_from_errno("strdup");
3463 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3464 "unveil", NULL) == -1)
3467 worktree_path = getcwd(NULL, 0);
3468 if (worktree_path == NULL) {
3469 error = got_error_from_errno("getcwd");
3473 error = got_repo_pack_fds_open(&pack_fds);
3477 error = got_worktree_open(&worktree, worktree_path);
3479 if (error->code == GOT_ERR_NOT_WORKTREE)
3480 error = wrap_not_worktree_error(error, "update",
3485 error = check_rebase_or_histedit_in_progress(worktree);
3489 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3494 error = apply_unveil(got_repo_get_path(repo), 0,
3495 got_worktree_get_root_path(worktree));
3499 error = check_merge_in_progress(worktree, repo);
3503 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3507 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
3508 got_worktree_get_head_ref_name(worktree), 0);
3511 if (commit_id_str == NULL) {
3512 error = got_ref_resolve(&commit_id, repo, head_ref);
3515 error = got_object_id_str(&commit_id_str, commit_id);
3519 struct got_reflist_head refs;
3521 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3525 error = got_repo_match_object_id(&commit_id, NULL,
3526 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3527 got_ref_list_free(&refs);
3528 free(commit_id_str);
3529 commit_id_str = NULL;
3532 error = got_object_id_str(&commit_id_str, commit_id);
3538 struct got_object_id *head_commit_id;
3539 TAILQ_FOREACH(pe, &paths, entry) {
3540 if (pe->path_len == 0)
3542 error = got_error_msg(GOT_ERR_BAD_PATH,
3543 "switching between branches requires that "
3544 "the entire work tree gets updated");
3547 error = got_ref_resolve(&head_commit_id, repo, head_ref);
3550 error = check_linear_ancestry(commit_id, head_commit_id, 0,
3552 free(head_commit_id);
3555 error = check_same_branch(commit_id, head_ref, NULL, repo);
3558 error = switch_head_ref(head_ref, commit_id, worktree, repo);
3562 error = check_linear_ancestry(commit_id,
3563 got_worktree_get_base_commit_id(worktree), 0, repo);
3564 if (error != NULL) {
3565 if (error->code == GOT_ERR_ANCESTRY)
3566 error = got_error(GOT_ERR_BRANCH_MOVED);
3569 error = check_same_branch(commit_id, head_ref, NULL, repo);
3574 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
3576 error = got_worktree_set_base_commit_id(worktree, repo,
3582 memset(&upa, 0, sizeof(upa));
3583 upa.verbosity = verbosity;
3584 error = got_worktree_checkout_files(worktree, &paths, repo,
3585 update_progress, &upa, check_cancelled, NULL);
3589 if (upa.did_something) {
3590 printf("Updated to %s: %s\n",
3591 got_worktree_get_head_ref_name(worktree), commit_id_str);
3593 printf("Already up-to-date\n");
3595 print_update_progress_stats(&upa);
3598 const struct got_error *pack_err =
3599 got_repo_pack_fds_close(pack_fds);
3603 free(worktree_path);
3604 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
3606 free(commit_id_str);
3610 static const struct got_error *
3611 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
3612 const char *path, int diff_context, int ignore_whitespace,
3613 int force_text_diff, struct got_diffstat_cb_arg *dsa,
3614 struct got_repository *repo, FILE *outfile)
3616 const struct got_error *err = NULL;
3617 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
3618 FILE *f1 = NULL, *f2 = NULL;
3619 int fd1 = -1, fd2 = -1;
3621 fd1 = got_opentempfd();
3623 return got_error_from_errno("got_opentempfd");
3624 fd2 = got_opentempfd();
3626 err = got_error_from_errno("got_opentempfd");
3631 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192,
3637 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192, fd2);
3641 f1 = got_opentemp();
3643 err = got_error_from_errno("got_opentemp");
3646 f2 = got_opentemp();
3648 err = got_error_from_errno("got_opentemp");
3652 while (path[0] == '/')
3654 err = got_diff_blob(NULL, NULL, blob1, blob2, f1, f2, path, path,
3655 GOT_DIFF_ALGORITHM_PATIENCE, diff_context, ignore_whitespace,
3656 force_text_diff, dsa, outfile);
3658 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3659 err = got_error_from_errno("close");
3661 got_object_blob_close(blob1);
3662 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3663 err = got_error_from_errno("close");
3664 got_object_blob_close(blob2);
3665 if (f1 && fclose(f1) == EOF && err == NULL)
3666 err = got_error_from_errno("fclose");
3667 if (f2 && fclose(f2) == EOF && err == NULL)
3668 err = got_error_from_errno("fclose");
3672 static const struct got_error *
3673 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
3674 const char *path, int diff_context, int ignore_whitespace,
3675 int force_text_diff, struct got_diffstat_cb_arg *dsa,
3676 struct got_repository *repo, FILE *outfile)
3678 const struct got_error *err = NULL;
3679 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3680 struct got_diff_blob_output_unidiff_arg arg;
3681 FILE *f1 = NULL, *f2 = NULL;
3682 int fd1 = -1, fd2 = -1;
3685 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3688 fd1 = got_opentempfd();
3690 err = got_error_from_errno("got_opentempfd");
3695 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3699 f1 = got_opentemp();
3701 err = got_error_from_errno("got_opentemp");
3705 f2 = got_opentemp();
3707 err = got_error_from_errno("got_opentemp");
3710 fd2 = got_opentempfd();
3712 err = got_error_from_errno("got_opentempfd");
3715 arg.diff_context = diff_context;
3716 arg.ignore_whitespace = ignore_whitespace;
3717 arg.force_text_diff = force_text_diff;
3719 arg.diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
3720 arg.outfile = outfile;
3723 while (path[0] == '/')
3725 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, path, path, repo,
3726 got_diff_blob_output_unidiff, &arg, 1);
3729 got_object_tree_close(tree1);
3731 got_object_tree_close(tree2);
3732 if (f1 && fclose(f1) == EOF && err == NULL)
3733 err = got_error_from_errno("fclose");
3734 if (f2 && fclose(f2) == EOF && err == NULL)
3735 err = got_error_from_errno("fclose");
3736 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3737 err = got_error_from_errno("close");
3738 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3739 err = got_error_from_errno("close");
3743 static const struct got_error *
3744 get_changed_paths(struct got_pathlist_head *paths,
3745 struct got_commit_object *commit, struct got_repository *repo,
3746 struct got_diffstat_cb_arg *dsa)
3748 const struct got_error *err = NULL;
3749 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3750 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3751 struct got_object_qid *qid;
3752 got_diff_blob_cb cb = got_diff_tree_collect_changed_paths;
3753 FILE *f1 = NULL, *f2 = NULL;
3754 int fd1 = -1, fd2 = -1;
3757 cb = got_diff_tree_compute_diffstat;
3759 f1 = got_opentemp();
3761 err = got_error_from_errno("got_opentemp");
3764 f2 = got_opentemp();
3766 err = got_error_from_errno("got_opentemp");
3769 fd1 = got_opentempfd();
3771 err = got_error_from_errno("got_opentempfd");
3774 fd2 = got_opentempfd();
3776 err = got_error_from_errno("got_opentempfd");
3781 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3783 struct got_commit_object *pcommit;
3784 err = got_object_open_as_commit(&pcommit, repo,
3789 tree_id1 = got_object_id_dup(
3790 got_object_commit_get_tree_id(pcommit));
3791 if (tree_id1 == NULL) {
3792 got_object_commit_close(pcommit);
3793 return got_error_from_errno("got_object_id_dup");
3795 got_object_commit_close(pcommit);
3800 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3805 tree_id2 = got_object_commit_get_tree_id(commit);
3806 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3810 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3811 cb, dsa ? (void *)dsa : paths, dsa ? 1 : 0);
3814 got_object_tree_close(tree1);
3816 got_object_tree_close(tree2);
3817 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3818 err = got_error_from_errno("close");
3819 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3820 err = got_error_from_errno("close");
3821 if (f1 && fclose(f1) == EOF && err == NULL)
3822 err = got_error_from_errno("fclose");
3823 if (f2 && fclose(f2) == EOF && err == NULL)
3824 err = got_error_from_errno("fclose");
3829 static const struct got_error *
3830 print_patch(struct got_commit_object *commit, struct got_object_id *id,
3831 const char *path, int diff_context, struct got_diffstat_cb_arg *dsa,
3832 struct got_repository *repo, FILE *outfile)
3834 const struct got_error *err = NULL;
3835 struct got_commit_object *pcommit = NULL;
3836 char *id_str1 = NULL, *id_str2 = NULL;
3837 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
3838 struct got_object_qid *qid;
3840 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3842 err = got_object_open_as_commit(&pcommit, repo,
3846 err = got_object_id_str(&id_str1, &qid->id);
3851 err = got_object_id_str(&id_str2, id);
3855 if (path && path[0] != '\0') {
3857 err = got_object_id_by_path(&obj_id2, repo, commit, path);
3861 err = got_object_id_by_path(&obj_id1, repo,
3864 if (err->code != GOT_ERR_NO_TREE_ENTRY) {
3870 err = got_object_get_type(&obj_type, repo, obj_id2);
3876 "diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3877 fprintf(outfile, "commit - %s\n",
3878 id_str1 ? id_str1 : "/dev/null");
3879 fprintf(outfile, "commit + %s\n", id_str2);
3881 case GOT_OBJ_TYPE_BLOB:
3882 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
3883 0, 0, dsa, repo, outfile);
3885 case GOT_OBJ_TYPE_TREE:
3886 err = diff_trees(obj_id1, obj_id2, path, diff_context,
3887 0, 0, dsa, repo, outfile);
3890 err = got_error(GOT_ERR_OBJ_TYPE);
3896 obj_id2 = got_object_commit_get_tree_id(commit);
3898 obj_id1 = got_object_commit_get_tree_id(pcommit);
3900 "diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3901 fprintf(outfile, "commit - %s\n",
3902 id_str1 ? id_str1 : "/dev/null");
3903 fprintf(outfile, "commit + %s\n", id_str2);
3904 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, 0,
3905 dsa, repo, outfile);
3911 got_object_commit_close(pcommit);
3916 get_datestr(time_t *time, char *datebuf)
3918 struct tm mytm, *tm;
3921 tm = gmtime_r(time, &mytm);
3924 s = asctime_r(tm, datebuf);
3927 p = strchr(s, '\n');
3933 static const struct got_error *
3934 match_commit(int *have_match, struct got_object_id *id,
3935 struct got_commit_object *commit, regex_t *regex)
3937 const struct got_error *err = NULL;
3938 regmatch_t regmatch;
3939 char *id_str = NULL, *logmsg = NULL;
3943 err = got_object_id_str(&id_str, id);
3947 err = got_object_commit_get_logmsg(&logmsg, commit);
3951 if (regexec(regex, got_object_commit_get_author(commit), 1,
3952 ®match, 0) == 0 ||
3953 regexec(regex, got_object_commit_get_committer(commit), 1,
3954 ®match, 0) == 0 ||
3955 regexec(regex, id_str, 1, ®match, 0) == 0 ||
3956 regexec(regex, logmsg, 1, ®match, 0) == 0)
3965 match_changed_paths(int *have_match, struct got_pathlist_head *changed_paths,
3968 regmatch_t regmatch;
3969 struct got_pathlist_entry *pe;
3973 TAILQ_FOREACH(pe, changed_paths, entry) {
3974 if (regexec(regex, pe->path, 1, ®match, 0) == 0) {
3981 static const struct got_error *
3982 match_patch(int *have_match, struct got_commit_object *commit,
3983 struct got_object_id *id, const char *path, int diff_context,
3984 struct got_repository *repo, regex_t *regex, FILE *f)
3986 const struct got_error *err = NULL;
3988 size_t linesize = 0;
3989 regmatch_t regmatch;
3993 err = got_opentemp_truncate(f);
3997 err = print_patch(commit, id, path, diff_context, NULL, repo, f);
4001 if (fseeko(f, 0L, SEEK_SET) == -1) {
4002 err = got_error_from_errno("fseeko");
4006 while (getline(&line, &linesize, f) != -1) {
4007 if (regexec(regex, line, 1, ®match, 0) == 0) {
4017 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
4019 static const struct got_error*
4020 build_refs_str(char **refs_str, struct got_reflist_head *refs,
4021 struct got_object_id *id, struct got_repository *repo,
4024 static const struct got_error *err = NULL;
4025 struct got_reflist_entry *re;
4031 TAILQ_FOREACH(re, refs, entry) {
4032 struct got_tag_object *tag = NULL;
4033 struct got_object_id *ref_id;
4036 name = got_ref_get_name(re->ref);
4037 if (strcmp(name, GOT_REF_HEAD) == 0)
4039 if (strncmp(name, "refs/", 5) == 0)
4041 if (strncmp(name, "got/", 4) == 0)
4043 if (strncmp(name, "heads/", 6) == 0)
4045 if (strncmp(name, "remotes/", 8) == 0) {
4049 s = strstr(name, "/" GOT_REF_HEAD);
4050 if (s != NULL && s[strlen(s)] == '\0')
4053 err = got_ref_resolve(&ref_id, repo, re->ref);
4056 if (strncmp(name, "tags/", 5) == 0) {
4057 err = got_object_open_as_tag(&tag, repo, ref_id);
4059 if (err->code != GOT_ERR_OBJ_TYPE) {
4063 /* Ref points at something other than a tag. */
4068 cmp = got_object_id_cmp(tag ?
4069 got_object_tag_get_object_id(tag) : ref_id, id);
4072 got_object_tag_close(tag);
4076 if (asprintf(refs_str, "%s%s%s", s ? s : "",
4077 s ? ", " : "", name) == -1) {
4078 err = got_error_from_errno("asprintf");
4089 static const struct got_error *
4090 print_commit_oneline(struct got_commit_object *commit, struct got_object_id *id,
4091 struct got_repository *repo, struct got_reflist_object_id_map *refs_idmap)
4093 const struct got_error *err = NULL;
4094 char *ref_str = NULL, *id_str = NULL, *logmsg0 = NULL;
4095 char *comma, *s, *nl;
4096 struct got_reflist_head *refs;
4097 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
4099 time_t committer_time;
4101 refs = got_reflist_object_id_map_lookup(refs_idmap, id);
4103 err = build_refs_str(&ref_str, refs, id, repo, 1);
4107 /* Display the first matching ref only. */
4108 if (ref_str && (comma = strchr(ref_str, ',')) != NULL)
4112 if (ref_str == NULL) {
4113 err = got_object_id_str(&id_str, id);
4118 committer_time = got_object_commit_get_committer_time(commit);
4119 if (gmtime_r(&committer_time, &tm) == NULL) {
4120 err = got_error_from_errno("gmtime_r");
4123 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0) {
4124 err = got_error(GOT_ERR_NO_SPACE);
4128 err = got_object_commit_get_logmsg(&logmsg0, commit);
4133 while (isspace((unsigned char)s[0]))
4136 nl = strchr(s, '\n');
4142 printf("%s%-7s %s\n", datebuf, ref_str, s);
4144 printf("%s%.7s %s\n", datebuf, id_str, s);
4146 if (fflush(stdout) != 0 && err == NULL)
4147 err = got_error_from_errno("fflush");
4155 static const struct got_error *
4156 print_diffstat(struct got_diffstat_cb_arg *dsa, const char *header)
4158 struct got_pathlist_entry *pe;
4161 printf("%s\n", header);
4163 TAILQ_FOREACH(pe, dsa->paths, entry) {
4164 struct got_diff_changed_path *cp = pe->data;
4165 int pad = dsa->max_path_len - pe->path_len + 1;
4167 printf(" %c %s%*c | %*d+ %*d-\n", cp->status, pe->path, pad,
4168 ' ', dsa->add_cols + 1, cp->add, dsa->rm_cols + 1, cp->rm);
4170 printf("\n%d file%s changed, %d insertion%s(+), %d deletion%s(-)\n\n",
4171 dsa->nfiles, dsa->nfiles > 1 ? "s" : "", dsa->ins,
4172 dsa->ins != 1 ? "s" : "", dsa->del, dsa->del != 1 ? "s" : "");
4174 if (fflush(stdout) != 0)
4175 return got_error_from_errno("fflush");
4180 static const struct got_error *
4186 if (fseeko(f, 0L, SEEK_SET) == -1)
4187 return got_error_from_errno("fseek");
4190 r = fread(buf, 1, sizeof(buf), f);
4193 return got_error_from_errno("fread");
4197 if (fwrite(buf, 1, r, stdout) != r)
4198 return got_ferror(stdout, GOT_ERR_IO);
4204 static const struct got_error *
4205 print_commit(struct got_commit_object *commit, struct got_object_id *id,
4206 struct got_repository *repo, const char *path,
4207 struct got_pathlist_head *changed_paths,
4208 struct got_diffstat_cb_arg *diffstat, int show_patch, int diff_context,
4209 struct got_reflist_object_id_map *refs_idmap, const char *custom_refs_str)
4211 const struct got_error *err = NULL;
4213 char *id_str, *datestr, *logmsg0, *logmsg, *line;
4215 time_t committer_time;
4216 const char *author, *committer;
4217 char *refs_str = NULL;
4219 err = got_object_id_str(&id_str, id);
4223 if (custom_refs_str == NULL) {
4224 struct got_reflist_head *refs;
4225 refs = got_reflist_object_id_map_lookup(refs_idmap, id);
4227 err = build_refs_str(&refs_str, refs, id, repo, 0);
4233 printf(GOT_COMMIT_SEP_STR);
4234 if (custom_refs_str)
4235 printf("commit %s (%s)\n", id_str, custom_refs_str);
4237 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4238 refs_str ? refs_str : "", refs_str ? ")" : "");
4243 printf("from: %s\n", got_object_commit_get_author(commit));
4244 author = got_object_commit_get_author(commit);
4245 committer = got_object_commit_get_committer(commit);
4246 if (strcmp(author, committer) != 0)
4247 printf("via: %s\n", committer);
4248 committer_time = got_object_commit_get_committer_time(commit);
4249 datestr = get_datestr(&committer_time, datebuf);
4251 printf("date: %s UTC\n", datestr);
4252 if (got_object_commit_get_nparents(commit) > 1) {
4253 const struct got_object_id_queue *parent_ids;
4254 struct got_object_qid *qid;
4256 parent_ids = got_object_commit_get_parent_ids(commit);
4257 STAILQ_FOREACH(qid, parent_ids, entry) {
4258 err = got_object_id_str(&id_str, &qid->id);
4261 printf("parent %d: %s\n", n++, id_str);
4267 err = got_object_commit_get_logmsg(&logmsg0, commit);
4273 line = strsep(&logmsg, "\n");
4275 printf(" %s\n", line);
4279 if (changed_paths && diffstat == NULL) {
4280 struct got_pathlist_entry *pe;
4282 TAILQ_FOREACH(pe, changed_paths, entry) {
4283 struct got_diff_changed_path *cp = pe->data;
4285 printf(" %c %s\n", cp->status, pe->path);
4293 err = got_error_from_errno("got_opentemp");
4298 err = print_patch(commit, id, path, diff_context, diffstat,
4299 repo, diffstat == NULL ? stdout : f);
4304 err = print_diffstat(diffstat, NULL);
4316 if (fflush(stdout) != 0 && err == NULL)
4317 err = got_error_from_errno("fflush");
4319 if (f && fclose(f) == EOF && err == NULL)
4320 err = got_error_from_errno("fclose");
4326 static const struct got_error *
4327 print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
4328 struct got_repository *repo, const char *path, int show_changed_paths,
4329 int show_diffstat, int show_patch, const char *search_pattern,
4330 int diff_context, int limit, int log_branches, int reverse_display_order,
4331 struct got_reflist_object_id_map *refs_idmap, int one_line,
4334 const struct got_error *err;
4335 struct got_commit_graph *graph;
4338 struct got_object_id_queue reversed_commits;
4339 struct got_object_qid *qid;
4340 struct got_commit_object *commit;
4341 struct got_pathlist_head changed_paths;
4343 STAILQ_INIT(&reversed_commits);
4344 TAILQ_INIT(&changed_paths);
4346 if (search_pattern && regcomp(®ex, search_pattern,
4347 REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
4348 return got_error_msg(GOT_ERR_REGEX, search_pattern);
4350 err = got_commit_graph_open(&graph, path, !log_branches);
4353 err = got_commit_graph_iter_start(graph, root_id, repo,
4354 check_cancelled, NULL);
4358 struct got_object_id id;
4359 struct got_diffstat_cb_arg dsa = { 0, 0, 0, 0, 0, 0,
4360 &changed_paths, 0, 0, GOT_DIFF_ALGORITHM_PATIENCE };
4362 if (sigint_received || sigpipe_received)
4365 err = got_commit_graph_iter_next(&id, graph, repo,
4366 check_cancelled, NULL);
4368 if (err->code == GOT_ERR_ITER_COMPLETED)
4373 err = got_object_open_as_commit(&commit, repo, &id);
4377 if ((show_changed_paths || (show_diffstat && !show_patch))
4378 && !reverse_display_order) {
4379 err = get_changed_paths(&changed_paths, commit, repo,
4380 show_diffstat ? &dsa : NULL);
4385 if (search_pattern) {
4386 err = match_commit(&have_match, &id, commit, ®ex);
4388 got_object_commit_close(commit);
4391 if (have_match == 0 && show_changed_paths)
4392 match_changed_paths(&have_match,
4393 &changed_paths, ®ex);
4394 if (have_match == 0 && show_patch) {
4395 err = match_patch(&have_match, commit, &id,
4396 path, diff_context, repo, ®ex, tmpfile);
4400 if (have_match == 0) {
4401 got_object_commit_close(commit);
4402 got_pathlist_free(&changed_paths,
4403 GOT_PATHLIST_FREE_ALL);
4408 if (reverse_display_order) {
4409 err = got_object_qid_alloc(&qid, &id);
4412 STAILQ_INSERT_HEAD(&reversed_commits, qid, entry);
4413 got_object_commit_close(commit);
4416 err = print_commit_oneline(commit, &id,
4419 err = print_commit(commit, &id, repo, path,
4420 (show_changed_paths || show_diffstat) ?
4421 &changed_paths : NULL,
4422 show_diffstat ? &dsa : NULL, show_patch,
4423 diff_context, refs_idmap, NULL);
4424 got_object_commit_close(commit);
4428 if ((limit && --limit == 0) ||
4429 (end_id && got_object_id_cmp(&id, end_id) == 0))
4432 got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
4434 if (reverse_display_order) {
4435 STAILQ_FOREACH(qid, &reversed_commits, entry) {
4436 struct got_diffstat_cb_arg dsa = { 0, 0, 0, 0, 0, 0,
4437 &changed_paths, 0, 0, GOT_DIFF_ALGORITHM_PATIENCE };
4439 err = got_object_open_as_commit(&commit, repo,
4443 if (show_changed_paths ||
4444 (show_diffstat && !show_patch)) {
4445 err = get_changed_paths(&changed_paths, commit,
4446 repo, show_diffstat ? &dsa : NULL);
4451 err = print_commit_oneline(commit, &qid->id,
4454 err = print_commit(commit, &qid->id, repo, path,
4455 (show_changed_paths || show_diffstat) ?
4456 &changed_paths : NULL,
4457 show_diffstat ? &dsa : NULL, show_patch,
4458 diff_context, refs_idmap, NULL);
4459 got_object_commit_close(commit);
4462 got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
4466 while (!STAILQ_EMPTY(&reversed_commits)) {
4467 qid = STAILQ_FIRST(&reversed_commits);
4468 STAILQ_REMOVE_HEAD(&reversed_commits, entry);
4469 got_object_qid_free(qid);
4471 got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
4474 got_commit_graph_close(graph);
4481 fprintf(stderr, "usage: %s log [-bdPpRs] [-C number] [-c commit] "
4482 "[-l N] [-r repository-path] [-S search-pattern] [-x commit] "
4483 "[path]\n", getprogname());
4488 get_default_log_limit(void)
4490 const char *got_default_log_limit;
4494 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
4495 if (got_default_log_limit == NULL)
4497 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
4503 static const struct got_error *
4504 cmd_log(int argc, char *argv[])
4506 const struct got_error *error;
4507 struct got_repository *repo = NULL;
4508 struct got_worktree *worktree = NULL;
4509 struct got_object_id *start_id = NULL, *end_id = NULL;
4510 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
4511 const char *start_commit = NULL, *end_commit = NULL;
4512 const char *search_pattern = NULL;
4513 int diff_context = -1, ch;
4514 int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
4515 int show_diffstat = 0, reverse_display_order = 0, one_line = 0;
4517 struct got_reflist_head refs;
4518 struct got_reflist_object_id_map *refs_idmap = NULL;
4519 FILE *tmpfile = NULL;
4520 int *pack_fds = NULL;
4525 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4531 limit = get_default_log_limit();
4533 while ((ch = getopt(argc, argv, "bC:c:dl:PpRr:S:sx:")) != -1) {
4539 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4542 errx(1, "number of context lines is %s: %s",
4546 start_commit = optarg;
4552 limit = strtonum(optarg, 0, INT_MAX, &errstr);
4554 errx(1, "number of commits is %s: %s",
4558 show_changed_paths = 1;
4564 reverse_display_order = 1;
4567 repo_path = realpath(optarg, NULL);
4568 if (repo_path == NULL)
4569 return got_error_from_errno2("realpath",
4571 got_path_strip_trailing_slashes(repo_path);
4574 search_pattern = optarg;
4580 end_commit = optarg;
4591 if (diff_context == -1)
4593 else if (!show_patch)
4594 errx(1, "-C requires -p");
4596 if (one_line && (show_patch || show_changed_paths || show_diffstat))
4597 errx(1, "cannot use -s with -d, -p or -P");
4599 cwd = getcwd(NULL, 0);
4601 error = got_error_from_errno("getcwd");
4605 error = got_repo_pack_fds_open(&pack_fds);
4609 if (repo_path == NULL) {
4610 error = got_worktree_open(&worktree, cwd);
4611 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4618 error = got_worktree_resolve_path(&path, worktree,
4623 path = strdup(argv[0]);
4625 error = got_error_from_errno("strdup");
4629 } else if (argc != 0)
4632 if (repo_path == NULL) {
4633 repo_path = worktree ?
4634 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
4636 if (repo_path == NULL) {
4637 error = got_error_from_errno("strdup");
4641 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4645 error = apply_unveil(got_repo_get_path(repo), 1,
4646 worktree ? got_worktree_get_root_path(worktree) : NULL);
4650 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4654 error = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
4658 if (start_commit == NULL) {
4659 struct got_reference *head_ref;
4660 struct got_commit_object *commit = NULL;
4661 error = got_ref_open(&head_ref, repo,
4662 worktree ? got_worktree_get_head_ref_name(worktree)
4666 error = got_ref_resolve(&start_id, repo, head_ref);
4667 got_ref_close(head_ref);
4670 error = got_object_open_as_commit(&commit, repo,
4674 got_object_commit_close(commit);
4676 error = got_repo_match_object_id(&start_id, NULL,
4677 start_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4681 if (end_commit != NULL) {
4682 error = got_repo_match_object_id(&end_id, NULL,
4683 end_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4690 * If a path was specified on the command line it was resolved
4691 * to a path in the work tree above. Prepend the work tree's
4692 * path prefix to obtain the corresponding in-repository path.
4696 prefix = got_worktree_get_path_prefix(worktree);
4697 if (asprintf(&in_repo_path, "%s%s%s", prefix,
4698 (path[0] != '\0') ? "/" : "", path) == -1) {
4699 error = got_error_from_errno("asprintf");
4704 error = got_repo_map_path(&in_repo_path, repo,
4710 path = in_repo_path;
4714 /* Release work tree lock. */
4715 got_worktree_close(worktree);
4719 if (search_pattern && show_patch) {
4720 tmpfile = got_opentemp();
4721 if (tmpfile == NULL) {
4722 error = got_error_from_errno("got_opentemp");
4727 error = print_commits(start_id, end_id, repo, path ? path : "",
4728 show_changed_paths, show_diffstat, show_patch, search_pattern,
4729 diff_context, limit, log_branches, reverse_display_order,
4730 refs_idmap, one_line, tmpfile);
4736 got_worktree_close(worktree);
4738 const struct got_error *close_err = got_repo_close(repo);
4743 const struct got_error *pack_err =
4744 got_repo_pack_fds_close(pack_fds);
4749 got_reflist_object_id_map_free(refs_idmap);
4750 if (tmpfile && fclose(tmpfile) == EOF && error == NULL)
4751 error = got_error_from_errno("fclose");
4752 got_ref_list_free(&refs);
4759 fprintf(stderr, "usage: %s diff [-adPsw] [-C number] [-c commit] "
4760 "[-r repository-path] [object1 object2 | path ...]\n",
4765 struct print_diff_arg {
4766 struct got_repository *repo;
4767 struct got_worktree *worktree;
4768 struct got_diffstat_cb_arg *diffstat;
4773 enum got_diff_algorithm diff_algo;
4774 int ignore_whitespace;
4775 int force_text_diff;
4782 * Create a file which contains the target path of a symlink so we can feed
4783 * it as content to the diff engine.
4785 static const struct got_error *
4786 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
4787 const char *abspath)
4789 const struct got_error *err = NULL;
4790 char target_path[PATH_MAX];
4791 ssize_t target_len, outlen;
4796 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
4797 if (target_len == -1)
4798 return got_error_from_errno2("readlinkat", abspath);
4800 target_len = readlink(abspath, target_path, PATH_MAX);
4801 if (target_len == -1)
4802 return got_error_from_errno2("readlink", abspath);
4805 *fd = got_opentempfd();
4807 return got_error_from_errno("got_opentempfd");
4809 outlen = write(*fd, target_path, target_len);
4811 err = got_error_from_errno("got_opentempfd");
4815 if (lseek(*fd, 0, SEEK_SET) == -1) {
4816 err = got_error_from_errno2("lseek", abspath);
4827 static const struct got_error *
4828 print_diff(void *arg, unsigned char status, unsigned char staged_status,
4829 const char *path, struct got_object_id *blob_id,
4830 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4831 int dirfd, const char *de_name)
4833 struct print_diff_arg *a = arg;
4834 const struct got_error *err = NULL;
4835 struct got_blob_object *blob1 = NULL;
4836 int fd = -1, fd1 = -1, fd2 = -1;
4838 char *abspath = NULL, *label1 = NULL;
4843 memset(&sb, 0, sizeof(sb));
4845 if (a->diff_staged) {
4846 if (staged_status != GOT_STATUS_MODIFY &&
4847 staged_status != GOT_STATUS_ADD &&
4848 staged_status != GOT_STATUS_DELETE)
4851 if (staged_status == GOT_STATUS_DELETE)
4853 if (status == GOT_STATUS_NONEXISTENT)
4854 return got_error_set_errno(ENOENT, path);
4855 if (status != GOT_STATUS_MODIFY &&
4856 status != GOT_STATUS_ADD &&
4857 status != GOT_STATUS_DELETE &&
4858 status != GOT_STATUS_CONFLICT)
4862 err = got_opentemp_truncate(a->f1);
4864 return got_error_from_errno("got_opentemp_truncate");
4865 err = got_opentemp_truncate(a->f2);
4867 return got_error_from_errno("got_opentemp_truncate");
4869 if (!a->header_shown) {
4870 if (fprintf(a->outfile, "diff %s%s\n",
4871 a->diff_staged ? "-s " : "",
4872 got_worktree_get_root_path(a->worktree)) < 0) {
4873 err = got_error_from_errno("fprintf");
4876 if (fprintf(a->outfile, "commit - %s\n", a->id_str) < 0) {
4877 err = got_error_from_errno("fprintf");
4880 if (fprintf(a->outfile, "path + %s%s\n",
4881 got_worktree_get_root_path(a->worktree),
4882 a->diff_staged ? " (staged changes)" : "") < 0) {
4883 err = got_error_from_errno("fprintf");
4886 a->header_shown = 1;
4889 if (a->diff_staged) {
4890 const char *label1 = NULL, *label2 = NULL;
4891 switch (staged_status) {
4892 case GOT_STATUS_MODIFY:
4896 case GOT_STATUS_ADD:
4899 case GOT_STATUS_DELETE:
4903 return got_error(GOT_ERR_FILE_STATUS);
4905 fd1 = got_opentempfd();
4907 err = got_error_from_errno("got_opentempfd");
4910 fd2 = got_opentempfd();
4912 err = got_error_from_errno("got_opentempfd");
4915 err = got_diff_objects_as_blobs(NULL, NULL, a->f1, a->f2,
4916 fd1, fd2, blob_id, staged_blob_id, label1, label2,
4917 a->diff_algo, a->diff_context, a->ignore_whitespace,
4918 a->force_text_diff, a->diffstat, a->repo, a->outfile);
4922 fd1 = got_opentempfd();
4924 err = got_error_from_errno("got_opentempfd");
4928 if (staged_status == GOT_STATUS_ADD ||
4929 staged_status == GOT_STATUS_MODIFY) {
4931 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
4935 err = got_object_id_str(&id_str, staged_blob_id);
4938 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
4939 err = got_error_from_errno("asprintf");
4944 } else if (status != GOT_STATUS_ADD) {
4945 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192,
4951 if (status != GOT_STATUS_DELETE) {
4952 if (asprintf(&abspath, "%s/%s",
4953 got_worktree_get_root_path(a->worktree), path) == -1) {
4954 err = got_error_from_errno("asprintf");
4959 fd = openat(dirfd, de_name,
4960 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4962 if (!got_err_open_nofollow_on_symlink()) {
4963 err = got_error_from_errno2("openat",
4967 err = get_symlink_target_file(&fd, dirfd,
4973 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4975 if (!got_err_open_nofollow_on_symlink()) {
4976 err = got_error_from_errno2("open",
4980 err = get_symlink_target_file(&fd, dirfd,
4986 if (fstatat(fd, abspath, &sb, AT_SYMLINK_NOFOLLOW) == -1) {
4987 err = got_error_from_errno2("fstatat", abspath);
4990 f2 = fdopen(fd, "r");
4992 err = got_error_from_errno2("fdopen", abspath);
5000 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5006 err = got_diff_blob_file(blob1, a->f1, size1, label1, f2 ? f2 : a->f2,
5007 f2_exists, &sb, path, GOT_DIFF_ALGORITHM_PATIENCE, a->diff_context,
5008 a->ignore_whitespace, a->force_text_diff, a->diffstat, a->outfile);
5010 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5011 err = got_error_from_errno("close");
5012 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5013 err = got_error_from_errno("close");
5015 got_object_blob_close(blob1);
5016 if (fd != -1 && close(fd) == -1 && err == NULL)
5017 err = got_error_from_errno("close");
5018 if (f2 && fclose(f2) == EOF && err == NULL)
5019 err = got_error_from_errno("fclose");
5024 static const struct got_error *
5025 cmd_diff(int argc, char *argv[])
5027 const struct got_error *error;
5028 struct got_repository *repo = NULL;
5029 struct got_worktree *worktree = NULL;
5030 char *cwd = NULL, *repo_path = NULL;
5031 const char *commit_args[2] = { NULL, NULL };
5032 int ncommit_args = 0;
5033 struct got_object_id *ids[2] = { NULL, NULL };
5034 char *labels[2] = { NULL, NULL };
5035 int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
5036 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch, i;
5037 int force_text_diff = 0, force_path = 0, rflag = 0, show_diffstat = 0;
5039 struct got_reflist_head refs;
5040 struct got_pathlist_head diffstat_paths, paths;
5041 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
5042 int fd1 = -1, fd2 = -1;
5043 int *pack_fds = NULL;
5044 struct got_diffstat_cb_arg dsa;
5046 memset(&dsa, 0, sizeof(dsa));
5050 TAILQ_INIT(&diffstat_paths);
5053 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5058 while ((ch = getopt(argc, argv, "aC:c:dPr:sw")) != -1) {
5061 force_text_diff = 1;
5064 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
5067 errx(1, "number of context lines is %s: %s",
5071 if (ncommit_args >= 2)
5072 errx(1, "too many -c options used");
5073 commit_args[ncommit_args++] = optarg;
5082 repo_path = realpath(optarg, NULL);
5083 if (repo_path == NULL)
5084 return got_error_from_errno2("realpath",
5086 got_path_strip_trailing_slashes(repo_path);
5093 ignore_whitespace = 1;
5104 cwd = getcwd(NULL, 0);
5106 error = got_error_from_errno("getcwd");
5110 error = got_repo_pack_fds_open(&pack_fds);
5114 if (repo_path == NULL) {
5115 error = got_worktree_open(&worktree, cwd);
5116 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5122 strdup(got_worktree_get_repo_path(worktree));
5123 if (repo_path == NULL) {
5124 error = got_error_from_errno("strdup");
5128 repo_path = strdup(cwd);
5129 if (repo_path == NULL) {
5130 error = got_error_from_errno("strdup");
5136 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5141 if (show_diffstat) {
5142 dsa.paths = &diffstat_paths;
5143 dsa.force_text = force_text_diff;
5144 dsa.ignore_ws = ignore_whitespace;
5145 dsa.diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
5148 if (rflag || worktree == NULL || ncommit_args > 0) {
5150 error = got_error_msg(GOT_ERR_NOT_IMPL,
5151 "-P option can only be used when diffing "
5156 error = got_error_msg(GOT_ERR_NOT_IMPL,
5157 "-s option can only be used when diffing "
5163 error = apply_unveil(got_repo_get_path(repo), 1,
5164 worktree ? got_worktree_get_root_path(worktree) : NULL);
5168 if ((!force_path && argc == 2) || ncommit_args > 0) {
5169 int obj_type = (ncommit_args > 0 ?
5170 GOT_OBJ_TYPE_COMMIT : GOT_OBJ_TYPE_ANY);
5171 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5175 for (i = 0; i < (ncommit_args > 0 ? ncommit_args : argc); i++) {
5177 if (ncommit_args > 0)
5178 arg = commit_args[i];
5181 error = got_repo_match_object_id(&ids[i], &labels[i],
5182 arg, obj_type, &refs, repo);
5184 if (error->code != GOT_ERR_NOT_REF &&
5185 error->code != GOT_ERR_NO_OBJ)
5187 if (ncommit_args > 0)
5195 f1 = got_opentemp();
5197 error = got_error_from_errno("got_opentemp");
5201 f2 = got_opentemp();
5203 error = got_error_from_errno("got_opentemp");
5207 outfile = got_opentemp();
5208 if (outfile == NULL) {
5209 error = got_error_from_errno("got_opentemp");
5213 if (ncommit_args == 0 && (ids[0] == NULL || ids[1] == NULL)) {
5214 struct print_diff_arg arg;
5217 if (worktree == NULL) {
5218 if (argc == 2 && ids[0] == NULL) {
5219 error = got_error_path(argv[0], GOT_ERR_NO_OBJ);
5221 } else if (argc == 2 && ids[1] == NULL) {
5222 error = got_error_path(argv[1], GOT_ERR_NO_OBJ);
5224 } else if (argc > 0) {
5225 error = got_error_fmt(GOT_ERR_NOT_WORKTREE,
5226 "%s", "specified paths cannot be resolved");
5229 error = got_error(GOT_ERR_NOT_WORKTREE);
5234 error = get_worktree_paths_from_argv(&paths, argc, argv,
5239 error = got_object_id_str(&id_str,
5240 got_worktree_get_base_commit_id(worktree));
5244 arg.worktree = worktree;
5245 arg.diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
5246 arg.diff_context = diff_context;
5247 arg.id_str = id_str;
5248 arg.header_shown = 0;
5249 arg.diff_staged = diff_staged;
5250 arg.ignore_whitespace = ignore_whitespace;
5251 arg.force_text_diff = force_text_diff;
5252 arg.diffstat = show_diffstat ? &dsa : NULL;
5255 arg.outfile = outfile;
5257 error = got_worktree_status(worktree, &paths, repo, 0,
5258 print_diff, &arg, check_cancelled, NULL);
5263 if (show_diffstat && dsa.nfiles > 0) {
5266 if (asprintf(&header, "diffstat %s%s",
5267 diff_staged ? "-s " : "",
5268 got_worktree_get_root_path(worktree)) == -1) {
5269 error = got_error_from_errno("asprintf");
5273 error = print_diffstat(&dsa, header);
5279 error = printfile(outfile);
5283 if (ncommit_args == 1) {
5284 struct got_commit_object *commit;
5285 error = got_object_open_as_commit(&commit, repo, ids[0]);
5289 labels[1] = labels[0];
5291 if (got_object_commit_get_nparents(commit) > 0) {
5292 const struct got_object_id_queue *pids;
5293 struct got_object_qid *pid;
5294 pids = got_object_commit_get_parent_ids(commit);
5295 pid = STAILQ_FIRST(pids);
5296 ids[0] = got_object_id_dup(&pid->id);
5297 if (ids[0] == NULL) {
5298 error = got_error_from_errno(
5299 "got_object_id_dup");
5300 got_object_commit_close(commit);
5303 error = got_object_id_str(&labels[0], ids[0]);
5305 got_object_commit_close(commit);
5310 labels[0] = strdup("/dev/null");
5311 if (labels[0] == NULL) {
5312 error = got_error_from_errno("strdup");
5313 got_object_commit_close(commit);
5318 got_object_commit_close(commit);
5321 if (ncommit_args == 0 && argc > 2) {
5322 error = got_error_msg(GOT_ERR_BAD_PATH,
5323 "path arguments cannot be used when diffing two objects");
5328 error = got_object_get_type(&type1, repo, ids[0]);
5333 error = got_object_get_type(&type2, repo, ids[1]);
5336 if (type1 != GOT_OBJ_TYPE_ANY && type1 != type2) {
5337 error = got_error(GOT_ERR_OBJ_TYPE);
5340 if (type1 == GOT_OBJ_TYPE_BLOB && argc > 2) {
5341 error = got_error_msg(GOT_ERR_OBJ_TYPE,
5342 "path arguments cannot be used when diffing blobs");
5346 for (i = 0; ncommit_args > 0 && i < argc; i++) {
5348 struct got_pathlist_entry *new;
5352 error = got_worktree_resolve_path(&p, worktree,
5356 prefix = got_worktree_get_path_prefix(worktree);
5357 while (prefix[0] == '/')
5359 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5360 (p[0] != '\0' && prefix[0] != '\0') ? "/" : "",
5362 error = got_error_from_errno("asprintf");
5368 char *mapped_path, *s;
5369 error = got_repo_map_path(&mapped_path, repo, argv[i]);
5375 in_repo_path = strdup(s);
5376 if (in_repo_path == NULL) {
5377 error = got_error_from_errno("asprintf");
5384 error = got_pathlist_insert(&new, &paths, in_repo_path, NULL);
5385 if (error || new == NULL /* duplicate */)
5392 /* Release work tree lock. */
5393 got_worktree_close(worktree);
5397 fd1 = got_opentempfd();
5399 error = got_error_from_errno("got_opentempfd");
5403 fd2 = got_opentempfd();
5405 error = got_error_from_errno("got_opentempfd");
5409 switch (type1 == GOT_OBJ_TYPE_ANY ? type2 : type1) {
5410 case GOT_OBJ_TYPE_BLOB:
5411 error = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5412 fd1, fd2, ids[0], ids[1], NULL, NULL,
5413 GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
5414 ignore_whitespace, force_text_diff,
5415 show_diffstat ? &dsa : NULL, repo, outfile);
5417 case GOT_OBJ_TYPE_TREE:
5418 error = got_diff_objects_as_trees(NULL, NULL, f1, f2, fd1, fd2,
5419 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);
5424 case GOT_OBJ_TYPE_COMMIT:
5425 fprintf(outfile, "diff %s %s\n", labels[0], labels[1]);
5426 error = got_diff_objects_as_commits(NULL, NULL, f1, f2,
5427 fd1, fd2, ids[0], ids[1], &paths,
5428 GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
5429 ignore_whitespace, force_text_diff,
5430 show_diffstat ? &dsa : NULL, repo, outfile);
5433 error = got_error(GOT_ERR_OBJ_TYPE);
5438 if (show_diffstat && dsa.nfiles > 0) {
5439 char *header = NULL;
5441 if (asprintf(&header, "diffstat %s %s",
5442 labels[0], labels[1]) == -1) {
5443 error = got_error_from_errno("asprintf");
5447 error = print_diffstat(&dsa, header);
5453 error = printfile(outfile);
5461 got_worktree_close(worktree);
5463 const struct got_error *close_err = got_repo_close(repo);
5468 const struct got_error *pack_err =
5469 got_repo_pack_fds_close(pack_fds);
5473 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
5474 got_pathlist_free(&diffstat_paths, GOT_PATHLIST_FREE_ALL);
5475 got_ref_list_free(&refs);
5476 if (outfile && fclose(outfile) == EOF && error == NULL)
5477 error = got_error_from_errno("fclose");
5478 if (f1 && fclose(f1) == EOF && error == NULL)
5479 error = got_error_from_errno("fclose");
5480 if (f2 && fclose(f2) == EOF && error == NULL)
5481 error = got_error_from_errno("fclose");
5482 if (fd1 != -1 && close(fd1) == -1 && error == NULL)
5483 error = got_error_from_errno("close");
5484 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
5485 error = got_error_from_errno("close");
5493 "usage: %s blame [-c commit] [-r repository-path] path\n",
5502 char datebuf[11]; /* YYYY-MM-DD + NUL */
5505 struct blame_cb_args {
5506 struct blame_line *lines;
5510 off_t *line_offsets;
5512 struct got_repository *repo;
5515 static const struct got_error *
5516 blame_cb(void *arg, int nlines, int lineno,
5517 struct got_commit_object *commit, struct got_object_id *id)
5519 const struct got_error *err = NULL;
5520 struct blame_cb_args *a = arg;
5521 struct blame_line *bline;
5523 size_t linesize = 0;
5526 time_t committer_time;
5528 if (nlines != a->nlines ||
5529 (lineno != -1 && lineno < 1) || lineno > a->nlines)
5530 return got_error(GOT_ERR_RANGE);
5532 if (sigint_received)
5533 return got_error(GOT_ERR_ITER_COMPLETED);
5536 return NULL; /* no change in this commit */
5538 /* Annotate this line. */
5539 bline = &a->lines[lineno - 1];
5540 if (bline->annotated)
5542 err = got_object_id_str(&bline->id_str, id);
5546 bline->committer = strdup(got_object_commit_get_committer(commit));
5547 if (bline->committer == NULL) {
5548 err = got_error_from_errno("strdup");
5552 committer_time = got_object_commit_get_committer_time(commit);
5553 if (gmtime_r(&committer_time, &tm) == NULL)
5554 return got_error_from_errno("gmtime_r");
5555 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
5557 err = got_error(GOT_ERR_NO_SPACE);
5560 bline->annotated = 1;
5562 /* Print lines annotated so far. */
5563 bline = &a->lines[a->lineno_cur - 1];
5564 if (!bline->annotated)
5567 offset = a->line_offsets[a->lineno_cur - 1];
5568 if (fseeko(a->f, offset, SEEK_SET) == -1) {
5569 err = got_error_from_errno("fseeko");
5573 while (a->lineno_cur <= a->nlines && bline->annotated) {
5574 char *smallerthan, *at, *nl, *committer;
5577 if (getline(&line, &linesize, a->f) == -1) {
5579 err = got_error_from_errno("getline");
5583 committer = bline->committer;
5584 smallerthan = strchr(committer, '<');
5585 if (smallerthan && smallerthan[1] != '\0')
5586 committer = smallerthan + 1;
5587 at = strchr(committer, '@');
5590 len = strlen(committer);
5592 committer[8] = '\0';
5594 nl = strchr(line, '\n');
5597 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
5598 bline->id_str, bline->datebuf, committer, line);
5601 bline = &a->lines[a->lineno_cur - 1];
5608 static const struct got_error *
5609 cmd_blame(int argc, char *argv[])
5611 const struct got_error *error;
5612 struct got_repository *repo = NULL;
5613 struct got_worktree *worktree = NULL;
5614 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5615 char *link_target = NULL;
5616 struct got_object_id *obj_id = NULL;
5617 struct got_object_id *commit_id = NULL;
5618 struct got_commit_object *commit = NULL;
5619 struct got_blob_object *blob = NULL;
5620 char *commit_id_str = NULL;
5621 struct blame_cb_args bca;
5622 int ch, obj_type, i, fd1 = -1, fd2 = -1, fd3 = -1;
5624 int *pack_fds = NULL;
5625 FILE *f1 = NULL, *f2 = NULL;
5627 fd1 = got_opentempfd();
5629 return got_error_from_errno("got_opentempfd");
5631 memset(&bca, 0, sizeof(bca));
5634 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5639 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5642 commit_id_str = optarg;
5645 repo_path = realpath(optarg, NULL);
5646 if (repo_path == NULL)
5647 return got_error_from_errno2("realpath",
5649 got_path_strip_trailing_slashes(repo_path);
5665 cwd = getcwd(NULL, 0);
5667 error = got_error_from_errno("getcwd");
5671 error = got_repo_pack_fds_open(&pack_fds);
5675 if (repo_path == NULL) {
5676 error = got_worktree_open(&worktree, cwd);
5677 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5683 strdup(got_worktree_get_repo_path(worktree));
5684 if (repo_path == NULL) {
5685 error = got_error_from_errno("strdup");
5690 repo_path = strdup(cwd);
5691 if (repo_path == NULL) {
5692 error = got_error_from_errno("strdup");
5698 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5703 const char *prefix = got_worktree_get_path_prefix(worktree);
5706 error = got_worktree_resolve_path(&p, worktree, path);
5709 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5710 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
5712 error = got_error_from_errno("asprintf");
5717 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5719 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5722 error = got_repo_map_path(&in_repo_path, repo, path);
5727 if (commit_id_str == NULL) {
5728 struct got_reference *head_ref;
5729 error = got_ref_open(&head_ref, repo, worktree ?
5730 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5733 error = got_ref_resolve(&commit_id, repo, head_ref);
5734 got_ref_close(head_ref);
5738 struct got_reflist_head refs;
5740 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5744 error = got_repo_match_object_id(&commit_id, NULL,
5745 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5746 got_ref_list_free(&refs);
5752 /* Release work tree lock. */
5753 got_worktree_close(worktree);
5757 error = got_object_open_as_commit(&commit, repo, commit_id);
5761 error = got_object_resolve_symlinks(&link_target, in_repo_path,
5766 error = got_object_id_by_path(&obj_id, repo, commit,
5767 link_target ? link_target : in_repo_path);
5771 error = got_object_get_type(&obj_type, repo, obj_id);
5775 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5776 error = got_error_path(link_target ? link_target : in_repo_path,
5781 error = got_object_open_as_blob(&blob, repo, obj_id, 8192, fd1);
5784 bca.f = got_opentemp();
5785 if (bca.f == NULL) {
5786 error = got_error_from_errno("got_opentemp");
5789 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
5790 &bca.line_offsets, bca.f, blob);
5791 if (error || bca.nlines == 0)
5794 /* Don't include \n at EOF in the blame line count. */
5795 if (bca.line_offsets[bca.nlines - 1] == filesize)
5798 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
5799 if (bca.lines == NULL) {
5800 error = got_error_from_errno("calloc");
5804 bca.nlines_prec = 0;
5812 fd2 = got_opentempfd();
5814 error = got_error_from_errno("got_opentempfd");
5817 fd3 = got_opentempfd();
5819 error = got_error_from_errno("got_opentempfd");
5822 f1 = got_opentemp();
5824 error = got_error_from_errno("got_opentemp");
5827 f2 = got_opentemp();
5829 error = got_error_from_errno("got_opentemp");
5832 error = got_blame(link_target ? link_target : in_repo_path, commit_id,
5833 repo, GOT_DIFF_ALGORITHM_PATIENCE, blame_cb, &bca,
5834 check_cancelled, NULL, fd2, fd3, f1, f2);
5843 got_object_commit_close(commit);
5845 if (fd1 != -1 && close(fd1) == -1 && error == NULL)
5846 error = got_error_from_errno("close");
5847 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
5848 error = got_error_from_errno("close");
5849 if (fd3 != -1 && close(fd3) == -1 && error == NULL)
5850 error = got_error_from_errno("close");
5851 if (f1 && fclose(f1) == EOF && error == NULL)
5852 error = got_error_from_errno("fclose");
5853 if (f2 && fclose(f2) == EOF && error == NULL)
5854 error = got_error_from_errno("fclose");
5857 got_object_blob_close(blob);
5859 got_worktree_close(worktree);
5861 const struct got_error *close_err = got_repo_close(repo);
5866 const struct got_error *pack_err =
5867 got_repo_pack_fds_close(pack_fds);
5872 for (i = 0; i < bca.nlines; i++) {
5873 struct blame_line *bline = &bca.lines[i];
5874 free(bline->id_str);
5875 free(bline->committer);
5879 free(bca.line_offsets);
5880 if (bca.f && fclose(bca.f) == EOF && error == NULL)
5881 error = got_error_from_errno("fclose");
5888 fprintf(stderr, "usage: %s tree [-iR] [-c commit] [-r repository-path] "
5889 "[path]\n", getprogname());
5893 static const struct got_error *
5894 print_entry(struct got_tree_entry *te, const char *id, const char *path,
5895 const char *root_path, struct got_repository *repo)
5897 const struct got_error *err = NULL;
5898 int is_root_path = (strcmp(path, root_path) == 0);
5899 const char *modestr = "";
5900 mode_t mode = got_tree_entry_get_mode(te);
5901 char *link_target = NULL;
5903 path += strlen(root_path);
5904 while (path[0] == '/')
5907 if (got_object_tree_entry_is_submodule(te))
5909 else if (S_ISLNK(mode)) {
5912 err = got_tree_entry_get_symlink_target(&link_target, te, repo);
5915 for (i = 0; i < strlen(link_target); i++) {
5916 if (!isprint((unsigned char)link_target[i]))
5917 link_target[i] = '?';
5922 else if (S_ISDIR(mode))
5924 else if (mode & S_IXUSR)
5927 printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
5928 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
5929 link_target ? " -> ": "", link_target ? link_target : "");
5935 static const struct got_error *
5936 print_tree(const char *path, struct got_commit_object *commit,
5937 int show_ids, int recurse, const char *root_path,
5938 struct got_repository *repo)
5940 const struct got_error *err = NULL;
5941 struct got_object_id *tree_id = NULL;
5942 struct got_tree_object *tree = NULL;
5945 err = got_object_id_by_path(&tree_id, repo, commit, path);
5949 err = got_object_open_as_tree(&tree, repo, tree_id);
5952 nentries = got_object_tree_get_nentries(tree);
5953 for (i = 0; i < nentries; i++) {
5954 struct got_tree_entry *te;
5957 if (sigint_received || sigpipe_received)
5960 te = got_object_tree_get_entry(tree, i);
5963 err = got_object_id_str(&id_str,
5964 got_tree_entry_get_id(te));
5967 if (asprintf(&id, "%s ", id_str) == -1) {
5968 err = got_error_from_errno("asprintf");
5974 err = print_entry(te, id, path, root_path, repo);
5979 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
5981 if (asprintf(&child_path, "%s%s%s", path,
5982 path[0] == '/' && path[1] == '\0' ? "" : "/",
5983 got_tree_entry_get_name(te)) == -1) {
5984 err = got_error_from_errno("asprintf");
5987 err = print_tree(child_path, commit, show_ids, 1,
5996 got_object_tree_close(tree);
6001 static const struct got_error *
6002 cmd_tree(int argc, char *argv[])
6004 const struct got_error *error;
6005 struct got_repository *repo = NULL;
6006 struct got_worktree *worktree = NULL;
6007 const char *path, *refname = NULL;
6008 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6009 struct got_object_id *commit_id = NULL;
6010 struct got_commit_object *commit = NULL;
6011 char *commit_id_str = NULL;
6012 int show_ids = 0, recurse = 0;
6014 int *pack_fds = NULL;
6017 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6022 while ((ch = getopt(argc, argv, "c:iRr:")) != -1) {
6025 commit_id_str = optarg;
6034 repo_path = realpath(optarg, NULL);
6035 if (repo_path == NULL)
6036 return got_error_from_errno2("realpath",
6038 got_path_strip_trailing_slashes(repo_path);
6056 cwd = getcwd(NULL, 0);
6058 error = got_error_from_errno("getcwd");
6062 error = got_repo_pack_fds_open(&pack_fds);
6066 if (repo_path == NULL) {
6067 error = got_worktree_open(&worktree, cwd);
6068 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6074 strdup(got_worktree_get_repo_path(worktree));
6075 if (repo_path == NULL)
6076 error = got_error_from_errno("strdup");
6080 repo_path = strdup(cwd);
6081 if (repo_path == NULL) {
6082 error = got_error_from_errno("strdup");
6088 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6093 const char *prefix = got_worktree_get_path_prefix(worktree);
6098 error = got_worktree_resolve_path(&p, worktree, path);
6101 if (asprintf(&in_repo_path, "%s%s%s", prefix,
6102 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
6104 error = got_error_from_errno("asprintf");
6109 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6113 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6118 error = got_repo_map_path(&in_repo_path, repo, path);
6123 if (commit_id_str == NULL) {
6124 struct got_reference *head_ref;
6126 refname = got_worktree_get_head_ref_name(worktree);
6128 refname = GOT_REF_HEAD;
6129 error = got_ref_open(&head_ref, repo, refname, 0);
6132 error = got_ref_resolve(&commit_id, repo, head_ref);
6133 got_ref_close(head_ref);
6137 struct got_reflist_head refs;
6139 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
6143 error = got_repo_match_object_id(&commit_id, NULL,
6144 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
6145 got_ref_list_free(&refs);
6151 /* Release work tree lock. */
6152 got_worktree_close(worktree);
6156 error = got_object_open_as_commit(&commit, repo, commit_id);
6160 error = print_tree(in_repo_path, commit, show_ids, recurse,
6161 in_repo_path, repo);
6168 got_object_commit_close(commit);
6170 got_worktree_close(worktree);
6172 const struct got_error *close_err = got_repo_close(repo);
6177 const struct got_error *pack_err =
6178 got_repo_pack_fds_close(pack_fds);
6188 fprintf(stderr, "usage: %s status [-I] [-S status-codes] "
6189 "[-s status-codes] [path ...]\n", getprogname());
6193 struct got_status_arg {
6198 static const struct got_error *
6199 print_status(void *arg, unsigned char status, unsigned char staged_status,
6200 const char *path, struct got_object_id *blob_id,
6201 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6202 int dirfd, const char *de_name)
6204 struct got_status_arg *st = arg;
6206 if (status == staged_status && (status == GOT_STATUS_DELETE))
6207 status = GOT_STATUS_NO_CHANGE;
6208 if (st != NULL && st->status_codes) {
6209 size_t ncodes = strlen(st->status_codes);
6212 for (i = 0; i < ncodes ; i++) {
6214 if (status == st->status_codes[i] ||
6215 staged_status == st->status_codes[i]) {
6220 if (status == st->status_codes[i] ||
6221 staged_status == st->status_codes[i])
6226 if (st->suppress && j == 0)
6233 printf("%c%c %s\n", status, staged_status, path);
6237 static const struct got_error *
6238 cmd_status(int argc, char *argv[])
6240 const struct got_error *error = NULL;
6241 struct got_repository *repo = NULL;
6242 struct got_worktree *worktree = NULL;
6243 struct got_status_arg st;
6245 struct got_pathlist_head paths;
6246 int ch, i, no_ignores = 0;
6247 int *pack_fds = NULL;
6251 memset(&st, 0, sizeof(st));
6252 st.status_codes = NULL;
6255 while ((ch = getopt(argc, argv, "IS:s:")) != -1) {
6261 if (st.status_codes != NULL && st.suppress == 0)
6262 option_conflict('S', 's');
6266 for (i = 0; i < strlen(optarg); i++) {
6267 switch (optarg[i]) {
6268 case GOT_STATUS_MODIFY:
6269 case GOT_STATUS_ADD:
6270 case GOT_STATUS_DELETE:
6271 case GOT_STATUS_CONFLICT:
6272 case GOT_STATUS_MISSING:
6273 case GOT_STATUS_OBSTRUCTED:
6274 case GOT_STATUS_UNVERSIONED:
6275 case GOT_STATUS_MODE_CHANGE:
6276 case GOT_STATUS_NONEXISTENT:
6279 errx(1, "invalid status code '%c'",
6283 if (ch == 's' && st.suppress)
6284 option_conflict('s', 'S');
6285 st.status_codes = optarg;
6297 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6301 cwd = getcwd(NULL, 0);
6303 error = got_error_from_errno("getcwd");
6307 error = got_repo_pack_fds_open(&pack_fds);
6311 error = got_worktree_open(&worktree, cwd);
6313 if (error->code == GOT_ERR_NOT_WORKTREE)
6314 error = wrap_not_worktree_error(error, "status", cwd);
6318 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6323 error = apply_unveil(got_repo_get_path(repo), 1,
6324 got_worktree_get_root_path(worktree));
6328 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6332 error = got_worktree_status(worktree, &paths, repo, no_ignores,
6333 print_status, &st, check_cancelled, NULL);
6336 const struct got_error *pack_err =
6337 got_repo_pack_fds_close(pack_fds);
6342 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
6350 fprintf(stderr, "usage: %s ref [-dlt] [-c object] [-r repository-path] "
6351 "[-s reference] [name]\n", getprogname());
6355 static const struct got_error *
6356 list_refs(struct got_repository *repo, const char *refname, int sort_by_time)
6358 static const struct got_error *err = NULL;
6359 struct got_reflist_head refs;
6360 struct got_reflist_entry *re;
6363 err = got_ref_list(&refs, repo, refname, sort_by_time ?
6364 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6369 TAILQ_FOREACH(re, &refs, entry) {
6371 refstr = got_ref_to_str(re->ref);
6372 if (refstr == NULL) {
6373 err = got_error_from_errno("got_ref_to_str");
6376 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
6380 got_ref_list_free(&refs);
6384 static const struct got_error *
6385 delete_ref_by_name(struct got_repository *repo, const char *refname)
6387 const struct got_error *err;
6388 struct got_reference *ref;
6390 err = got_ref_open(&ref, repo, refname, 0);
6394 err = delete_ref(repo, ref);
6399 static const struct got_error *
6400 add_ref(struct got_repository *repo, const char *refname, const char *target)
6402 const struct got_error *err = NULL;
6403 struct got_object_id *id = NULL;
6404 struct got_reference *ref = NULL;
6405 struct got_reflist_head refs;
6408 * Don't let the user create a reference name with a leading '-'.
6409 * While technically a valid reference name, this case is usually
6410 * an unintended typo.
6412 if (refname[0] == '-')
6413 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
6416 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
6419 err = got_repo_match_object_id(&id, NULL, target, GOT_OBJ_TYPE_ANY,
6421 got_ref_list_free(&refs);
6425 err = got_ref_alloc(&ref, refname, id);
6429 err = got_ref_write(ref, repo);
6437 static const struct got_error *
6438 add_symref(struct got_repository *repo, const char *refname, const char *target)
6440 const struct got_error *err = NULL;
6441 struct got_reference *ref = NULL;
6442 struct got_reference *target_ref = NULL;
6445 * Don't let the user create a reference name with a leading '-'.
6446 * While technically a valid reference name, this case is usually
6447 * an unintended typo.
6449 if (refname[0] == '-')
6450 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
6452 err = got_ref_open(&target_ref, repo, target, 0);
6456 err = got_ref_alloc_symref(&ref, refname, target_ref);
6460 err = got_ref_write(ref, repo);
6463 got_ref_close(target_ref);
6469 static const struct got_error *
6470 cmd_ref(int argc, char *argv[])
6472 const struct got_error *error = NULL;
6473 struct got_repository *repo = NULL;
6474 struct got_worktree *worktree = NULL;
6475 char *cwd = NULL, *repo_path = NULL;
6476 int ch, do_list = 0, do_delete = 0, sort_by_time = 0;
6477 const char *obj_arg = NULL, *symref_target= NULL;
6478 char *refname = NULL;
6479 int *pack_fds = NULL;
6481 while ((ch = getopt(argc, argv, "c:dlr:s:t")) != -1) {
6493 repo_path = realpath(optarg, NULL);
6494 if (repo_path == NULL)
6495 return got_error_from_errno2("realpath",
6497 got_path_strip_trailing_slashes(repo_path);
6500 symref_target = optarg;
6511 if (obj_arg && do_list)
6512 option_conflict('c', 'l');
6513 if (obj_arg && do_delete)
6514 option_conflict('c', 'd');
6515 if (obj_arg && symref_target)
6516 option_conflict('c', 's');
6517 if (symref_target && do_delete)
6518 option_conflict('s', 'd');
6519 if (symref_target && do_list)
6520 option_conflict('s', 'l');
6521 if (do_delete && do_list)
6522 option_conflict('d', 'l');
6523 if (sort_by_time && !do_list)
6524 errx(1, "-t option requires -l option");
6530 if (argc != 0 && argc != 1)
6533 refname = strdup(argv[0]);
6534 if (refname == NULL) {
6535 error = got_error_from_errno("strdup");
6542 refname = strdup(argv[0]);
6543 if (refname == NULL) {
6544 error = got_error_from_errno("strdup");
6550 got_path_strip_trailing_slashes(refname);
6553 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6554 "sendfd unveil", NULL) == -1)
6557 cwd = getcwd(NULL, 0);
6559 error = got_error_from_errno("getcwd");
6563 error = got_repo_pack_fds_open(&pack_fds);
6567 if (repo_path == NULL) {
6568 error = got_worktree_open(&worktree, cwd);
6569 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6575 strdup(got_worktree_get_repo_path(worktree));
6576 if (repo_path == NULL)
6577 error = got_error_from_errno("strdup");
6581 repo_path = strdup(cwd);
6582 if (repo_path == NULL) {
6583 error = got_error_from_errno("strdup");
6589 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6595 /* Remove "cpath" promise. */
6596 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6602 error = apply_unveil(got_repo_get_path(repo), do_list,
6603 worktree ? got_worktree_get_root_path(worktree) : NULL);
6608 error = list_refs(repo, refname, sort_by_time);
6610 error = delete_ref_by_name(repo, refname);
6611 else if (symref_target)
6612 error = add_symref(repo, refname, symref_target);
6614 if (obj_arg == NULL)
6616 error = add_ref(repo, refname, obj_arg);
6621 const struct got_error *close_err = got_repo_close(repo);
6626 got_worktree_close(worktree);
6628 const struct got_error *pack_err =
6629 got_repo_pack_fds_close(pack_fds);
6641 fprintf(stderr, "usage: %s branch [-lnt] [-c commit] [-d name] "
6642 "[-r repository-path] [name]\n", getprogname());
6646 static const struct got_error *
6647 list_branch(struct got_repository *repo, struct got_worktree *worktree,
6648 struct got_reference *ref)
6650 const struct got_error *err = NULL;
6651 const char *refname, *marker = " ";
6654 refname = got_ref_get_name(ref);
6655 if (worktree && strcmp(refname,
6656 got_worktree_get_head_ref_name(worktree)) == 0) {
6657 struct got_object_id *id = NULL;
6659 err = got_ref_resolve(&id, repo, ref);
6662 if (got_object_id_cmp(id,
6663 got_worktree_get_base_commit_id(worktree)) == 0)
6670 if (strncmp(refname, "refs/heads/", 11) == 0)
6672 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
6674 if (strncmp(refname, "refs/remotes/", 13) == 0)
6677 refstr = got_ref_to_str(ref);
6679 return got_error_from_errno("got_ref_to_str");
6681 printf("%s%s: %s\n", marker, refname, refstr);
6686 static const struct got_error *
6687 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
6689 const char *refname;
6691 if (worktree == NULL)
6692 return got_error(GOT_ERR_NOT_WORKTREE);
6694 refname = got_worktree_get_head_ref_name(worktree);
6696 if (strncmp(refname, "refs/heads/", 11) == 0)
6698 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
6701 printf("%s\n", refname);
6706 static const struct got_error *
6707 list_branches(struct got_repository *repo, struct got_worktree *worktree,
6710 static const struct got_error *err = NULL;
6711 struct got_reflist_head refs;
6712 struct got_reflist_entry *re;
6713 struct got_reference *temp_ref = NULL;
6714 int rebase_in_progress, histedit_in_progress;
6719 err = got_worktree_rebase_in_progress(&rebase_in_progress,
6724 err = got_worktree_histedit_in_progress(&histedit_in_progress,
6729 if (rebase_in_progress || histedit_in_progress) {
6730 err = got_ref_open(&temp_ref, repo,
6731 got_worktree_get_head_ref_name(worktree), 0);
6734 list_branch(repo, worktree, temp_ref);
6735 got_ref_close(temp_ref);
6739 err = got_ref_list(&refs, repo, "refs/heads", sort_by_time ?
6740 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6745 TAILQ_FOREACH(re, &refs, entry)
6746 list_branch(repo, worktree, re->ref);
6748 got_ref_list_free(&refs);
6750 err = got_ref_list(&refs, repo, "refs/remotes", sort_by_time ?
6751 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6756 TAILQ_FOREACH(re, &refs, entry)
6757 list_branch(repo, worktree, re->ref);
6759 got_ref_list_free(&refs);
6764 static const struct got_error *
6765 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
6766 const char *branch_name)
6768 const struct got_error *err = NULL;
6769 struct got_reference *ref = NULL;
6770 char *refname, *remote_refname = NULL;
6772 if (strncmp(branch_name, "refs/", 5) == 0)
6774 if (strncmp(branch_name, "heads/", 6) == 0)
6776 else if (strncmp(branch_name, "remotes/", 8) == 0)
6779 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
6780 return got_error_from_errno("asprintf");
6782 if (asprintf(&remote_refname, "refs/remotes/%s",
6783 branch_name) == -1) {
6784 err = got_error_from_errno("asprintf");
6788 err = got_ref_open(&ref, repo, refname, 0);
6790 const struct got_error *err2;
6791 if (err->code != GOT_ERR_NOT_REF)
6794 * Keep 'err' intact such that if neither branch exists
6795 * we report "refs/heads" rather than "refs/remotes" in
6796 * our error message.
6798 err2 = got_ref_open(&ref, repo, remote_refname, 0);
6805 strcmp(got_worktree_get_head_ref_name(worktree),
6806 got_ref_get_name(ref)) == 0) {
6807 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6808 "will not delete this work tree's current branch");
6812 err = delete_ref(repo, ref);
6817 free(remote_refname);
6821 static const struct got_error *
6822 add_branch(struct got_repository *repo, const char *branch_name,
6823 struct got_object_id *base_commit_id)
6825 const struct got_error *err = NULL;
6826 struct got_reference *ref = NULL;
6827 char *refname = NULL;
6830 * Don't let the user create a branch name with a leading '-'.
6831 * While technically a valid reference name, this case is usually
6832 * an unintended typo.
6834 if (branch_name[0] == '-')
6835 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
6837 if (strncmp(branch_name, "refs/heads/", 11) == 0)
6840 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
6841 err = got_error_from_errno("asprintf");
6845 err = got_ref_open(&ref, repo, refname, 0);
6847 err = got_error(GOT_ERR_BRANCH_EXISTS);
6849 } else if (err->code != GOT_ERR_NOT_REF)
6852 err = got_ref_alloc(&ref, refname, base_commit_id);
6856 err = got_ref_write(ref, repo);
6864 static const struct got_error *
6865 cmd_branch(int argc, char *argv[])
6867 const struct got_error *error = NULL;
6868 struct got_repository *repo = NULL;
6869 struct got_worktree *worktree = NULL;
6870 char *cwd = NULL, *repo_path = NULL;
6871 int ch, do_list = 0, do_show = 0, do_update = 1, sort_by_time = 0;
6872 const char *delref = NULL, *commit_id_arg = NULL;
6873 struct got_reference *ref = NULL;
6874 struct got_pathlist_head paths;
6875 struct got_object_id *commit_id = NULL;
6876 char *commit_id_str = NULL;
6877 int *pack_fds = NULL;
6881 while ((ch = getopt(argc, argv, "c:d:lnr:t")) != -1) {
6884 commit_id_arg = optarg;
6896 repo_path = realpath(optarg, NULL);
6897 if (repo_path == NULL)
6898 return got_error_from_errno2("realpath",
6900 got_path_strip_trailing_slashes(repo_path);
6911 if (do_list && delref)
6912 option_conflict('l', 'd');
6913 if (sort_by_time && !do_list)
6914 errx(1, "-t option requires -l option");
6919 if (!do_list && !delref && argc == 0)
6922 if ((do_list || delref || do_show) && commit_id_arg != NULL)
6923 errx(1, "-c option can only be used when creating a branch");
6925 if (do_list || delref) {
6928 } else if (!do_show && argc != 1)
6932 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6933 "sendfd unveil", NULL) == -1)
6936 cwd = getcwd(NULL, 0);
6938 error = got_error_from_errno("getcwd");
6942 error = got_repo_pack_fds_open(&pack_fds);
6946 if (repo_path == NULL) {
6947 error = got_worktree_open(&worktree, cwd);
6948 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6954 strdup(got_worktree_get_repo_path(worktree));
6955 if (repo_path == NULL)
6956 error = got_error_from_errno("strdup");
6960 repo_path = strdup(cwd);
6961 if (repo_path == NULL) {
6962 error = got_error_from_errno("strdup");
6968 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6973 if (do_list || do_show) {
6974 /* Remove "cpath" promise. */
6975 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6981 error = apply_unveil(got_repo_get_path(repo), do_list,
6982 worktree ? got_worktree_get_root_path(worktree) : NULL);
6987 error = show_current_branch(repo, worktree);
6989 error = list_branches(repo, worktree, sort_by_time);
6991 error = delete_branch(repo, worktree, delref);
6993 struct got_reflist_head refs;
6995 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
6999 if (commit_id_arg == NULL)
7000 commit_id_arg = worktree ?
7001 got_worktree_get_head_ref_name(worktree) :
7003 error = got_repo_match_object_id(&commit_id, NULL,
7004 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &refs, repo);
7005 got_ref_list_free(&refs);
7008 error = add_branch(repo, argv[0], commit_id);
7011 if (worktree && do_update) {
7012 struct got_update_progress_arg upa;
7013 char *branch_refname = NULL;
7015 error = got_object_id_str(&commit_id_str, commit_id);
7018 error = get_worktree_paths_from_argv(&paths, 0, NULL,
7022 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
7024 error = got_error_from_errno("asprintf");
7027 error = got_ref_open(&ref, repo, branch_refname, 0);
7028 free(branch_refname);
7031 error = switch_head_ref(ref, commit_id, worktree,
7035 error = got_worktree_set_base_commit_id(worktree, repo,
7039 memset(&upa, 0, sizeof(upa));
7040 error = got_worktree_checkout_files(worktree, &paths,
7041 repo, update_progress, &upa, check_cancelled,
7045 if (upa.did_something) {
7046 printf("Updated to %s: %s\n",
7047 got_worktree_get_head_ref_name(worktree),
7050 print_update_progress_stats(&upa);
7057 const struct got_error *close_err = got_repo_close(repo);
7062 got_worktree_close(worktree);
7064 const struct got_error *pack_err =
7065 got_repo_pack_fds_close(pack_fds);
7072 free(commit_id_str);
7073 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
7081 fprintf(stderr, "usage: %s tag [-lVv] [-c commit] [-m message] "
7082 "[-r repository-path] [-s signer-id] name\n", getprogname());
7087 static const struct got_error *
7088 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
7090 const struct got_error *err = NULL;
7091 struct got_reflist_entry *re, *se, *new;
7092 struct got_object_id *re_id, *se_id;
7093 struct got_tag_object *re_tag, *se_tag;
7094 time_t re_time, se_time;
7096 STAILQ_FOREACH(re, tags, entry) {
7097 se = STAILQ_FIRST(sorted);
7099 err = got_reflist_entry_dup(&new, re);
7102 STAILQ_INSERT_HEAD(sorted, new, entry);
7105 err = got_ref_resolve(&re_id, repo, re->ref);
7108 err = got_object_open_as_tag(&re_tag, repo, re_id);
7112 re_time = got_object_tag_get_tagger_time(re_tag);
7113 got_object_tag_close(re_tag);
7117 err = got_ref_resolve(&se_id, repo, re->ref);
7120 err = got_object_open_as_tag(&se_tag, repo, se_id);
7124 se_time = got_object_tag_get_tagger_time(se_tag);
7125 got_object_tag_close(se_tag);
7127 if (se_time > re_time) {
7128 err = got_reflist_entry_dup(&new, re);
7131 STAILQ_INSERT_AFTER(sorted, se, new, entry);
7134 se = STAILQ_NEXT(se, entry);
7143 static const struct got_error *
7144 get_tag_refname(char **refname, const char *tag_name)
7146 const struct got_error *err;
7148 if (strncmp("refs/tags/", tag_name, 10) == 0) {
7149 *refname = strdup(tag_name);
7150 if (*refname == NULL)
7151 return got_error_from_errno("strdup");
7152 } else if (asprintf(refname, "refs/tags/%s", tag_name) == -1) {
7153 err = got_error_from_errno("asprintf");
7161 static const struct got_error *
7162 list_tags(struct got_repository *repo, const char *tag_name, int verify_tags,
7163 const char *allowed_signers, const char *revoked_signers, int verbosity)
7165 static const struct got_error *err = NULL;
7166 struct got_reflist_head refs;
7167 struct got_reflist_entry *re;
7168 char *wanted_refname = NULL;
7173 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
7178 struct got_reference *ref;
7179 err = get_tag_refname(&wanted_refname, tag_name);
7182 /* Wanted tag reference should exist. */
7183 err = got_ref_open(&ref, repo, wanted_refname, 0);
7189 TAILQ_FOREACH(re, &refs, entry) {
7190 const char *refname;
7191 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
7193 const char *tagger, *ssh_sig = NULL;
7194 char *sig_msg = NULL;
7196 struct got_object_id *id;
7197 struct got_tag_object *tag;
7198 struct got_commit_object *commit = NULL;
7200 refname = got_ref_get_name(re->ref);
7201 if (strncmp(refname, "refs/tags/", 10) != 0 ||
7202 (wanted_refname && strcmp(refname, wanted_refname) != 0))
7205 refstr = got_ref_to_str(re->ref);
7206 if (refstr == NULL) {
7207 err = got_error_from_errno("got_ref_to_str");
7211 err = got_ref_resolve(&id, repo, re->ref);
7214 err = got_object_open_as_tag(&tag, repo, id);
7216 if (err->code != GOT_ERR_OBJ_TYPE) {
7220 /* "lightweight" tag */
7221 err = got_object_open_as_commit(&commit, repo, id);
7226 tagger = got_object_commit_get_committer(commit);
7228 got_object_commit_get_committer_time(commit);
7229 err = got_object_id_str(&id_str, id);
7235 tagger = got_object_tag_get_tagger(tag);
7236 tagger_time = got_object_tag_get_tagger_time(tag);
7237 err = got_object_id_str(&id_str,
7238 got_object_tag_get_object_id(tag));
7243 if (tag && verify_tags) {
7244 ssh_sig = got_sigs_get_tagmsg_ssh_signature(
7245 got_object_tag_get_message(tag));
7246 if (ssh_sig && allowed_signers == NULL) {
7247 err = got_error_msg(
7248 GOT_ERR_VERIFY_TAG_SIGNATURE,
7249 "SSH signature verification requires "
7250 "setting allowed_signers in "
7256 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
7258 printf("from: %s\n", tagger);
7259 datestr = get_datestr(&tagger_time, datebuf);
7261 printf("date: %s UTC\n", datestr);
7263 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
7265 switch (got_object_tag_get_object_type(tag)) {
7266 case GOT_OBJ_TYPE_BLOB:
7267 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
7270 case GOT_OBJ_TYPE_TREE:
7271 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
7274 case GOT_OBJ_TYPE_COMMIT:
7275 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
7278 case GOT_OBJ_TYPE_TAG:
7279 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
7289 err = got_sigs_verify_tag_ssh(&sig_msg, tag, ssh_sig,
7290 allowed_signers, revoked_signers, verbosity);
7291 if (err && err->code == GOT_ERR_BAD_TAG_SIGNATURE)
7295 printf("signature: %s", sig_msg);
7301 err = got_object_commit_get_logmsg(&tagmsg0, commit);
7304 got_object_commit_close(commit);
7306 tagmsg0 = strdup(got_object_tag_get_message(tag));
7307 got_object_tag_close(tag);
7308 if (tagmsg0 == NULL) {
7309 err = got_error_from_errno("strdup");
7316 line = strsep(&tagmsg, "\n");
7318 printf(" %s\n", line);
7323 got_ref_list_free(&refs);
7324 free(wanted_refname);
7326 if (err == NULL && bad_sigs)
7327 err = got_error(GOT_ERR_BAD_TAG_SIGNATURE);
7331 static const struct got_error *
7332 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
7333 const char *tag_name, const char *repo_path)
7335 const struct got_error *err = NULL;
7336 char *template = NULL, *initial_content = NULL;
7337 char *editor = NULL;
7338 int initial_content_len;
7341 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
7342 err = got_error_from_errno("asprintf");
7346 initial_content_len = asprintf(&initial_content,
7347 "\n# tagging commit %s as %s\n",
7348 commit_id_str, tag_name);
7349 if (initial_content_len == -1) {
7350 err = got_error_from_errno("asprintf");
7354 err = got_opentemp_named_fd(tagmsg_path, &fd, template, "");
7358 if (write(fd, initial_content, initial_content_len) == -1) {
7359 err = got_error_from_errno2("write", *tagmsg_path);
7363 err = get_editor(&editor);
7366 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content,
7367 initial_content_len, 1);
7369 free(initial_content);
7373 if (fd != -1 && close(fd) == -1 && err == NULL)
7374 err = got_error_from_errno2("close", *tagmsg_path);
7383 static const struct got_error *
7384 add_tag(struct got_repository *repo, const char *tagger,
7385 const char *tag_name, const char *commit_arg, const char *tagmsg_arg,
7386 const char *signer_id, int verbosity)
7388 const struct got_error *err = NULL;
7389 struct got_object_id *commit_id = NULL, *tag_id = NULL;
7390 char *label = NULL, *commit_id_str = NULL;
7391 struct got_reference *ref = NULL;
7392 char *refname = NULL, *tagmsg = NULL;
7393 char *tagmsg_path = NULL, *tag_id_str = NULL;
7394 int preserve_tagmsg = 0;
7395 struct got_reflist_head refs;
7400 * Don't let the user create a tag name with a leading '-'.
7401 * While technically a valid reference name, this case is usually
7402 * an unintended typo.
7404 if (tag_name[0] == '-')
7405 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
7407 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
7411 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
7412 GOT_OBJ_TYPE_COMMIT, &refs, repo);
7416 err = got_object_id_str(&commit_id_str, commit_id);
7420 err = get_tag_refname(&refname, tag_name);
7423 if (strncmp("refs/tags/", tag_name, 10) == 0)
7426 err = got_ref_open(&ref, repo, refname, 0);
7428 err = got_error(GOT_ERR_TAG_EXISTS);
7430 } else if (err->code != GOT_ERR_NOT_REF)
7433 if (tagmsg_arg == NULL) {
7434 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
7435 tag_name, got_repo_get_path(repo));
7437 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
7438 tagmsg_path != NULL)
7439 preserve_tagmsg = 1;
7442 /* Editor is done; we can now apply unveil(2) */
7443 err = got_sigs_apply_unveil();
7446 err = apply_unveil(got_repo_get_path(repo), 0, NULL);
7451 err = got_object_tag_create(&tag_id, tag_name, commit_id,
7452 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, signer_id, repo,
7456 preserve_tagmsg = 1;
7460 err = got_ref_alloc(&ref, refname, tag_id);
7463 preserve_tagmsg = 1;
7467 err = got_ref_write(ref, repo);
7470 preserve_tagmsg = 1;
7474 err = got_object_id_str(&tag_id_str, tag_id);
7477 preserve_tagmsg = 1;
7480 printf("Created tag %s\n", tag_id_str);
7482 if (preserve_tagmsg) {
7483 fprintf(stderr, "%s: tag message preserved in %s\n",
7484 getprogname(), tagmsg_path);
7485 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
7486 err = got_error_from_errno2("unlink", tagmsg_path);
7491 free(commit_id_str);
7495 got_ref_list_free(&refs);
7499 static const struct got_error *
7500 cmd_tag(int argc, char *argv[])
7502 const struct got_error *error = NULL;
7503 struct got_repository *repo = NULL;
7504 struct got_worktree *worktree = NULL;
7505 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
7506 char *gitconfig_path = NULL, *tagger = NULL;
7507 char *allowed_signers = NULL, *revoked_signers = NULL;
7508 char *signer_id = NULL;
7509 const char *tag_name = NULL, *commit_id_arg = NULL, *tagmsg = NULL;
7510 int ch, do_list = 0, verify_tags = 0, verbosity = 0;
7511 int *pack_fds = NULL;
7513 while ((ch = getopt(argc, argv, "c:lm:r:s:Vv")) != -1) {
7516 commit_id_arg = optarg;
7525 repo_path = realpath(optarg, NULL);
7526 if (repo_path == NULL) {
7527 error = got_error_from_errno2("realpath",
7531 got_path_strip_trailing_slashes(repo_path);
7534 signer_id = strdup(optarg);
7535 if (signer_id == NULL) {
7536 error = got_error_from_errno("strdup");
7546 else if (verbosity < 3)
7558 if (do_list || verify_tags) {
7559 if (commit_id_arg != NULL)
7561 "-c option can only be used when creating a tag");
7564 option_conflict('l', 'm');
7566 option_conflict('V', 'm');
7570 option_conflict('l', 's');
7572 option_conflict('V', 's');
7576 } else if (argc != 1)
7583 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
7584 "sendfd unveil", NULL) == -1)
7587 cwd = getcwd(NULL, 0);
7589 error = got_error_from_errno("getcwd");
7593 error = got_repo_pack_fds_open(&pack_fds);
7597 if (repo_path == NULL) {
7598 error = got_worktree_open(&worktree, cwd);
7599 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7605 strdup(got_worktree_get_repo_path(worktree));
7606 if (repo_path == NULL)
7607 error = got_error_from_errno("strdup");
7611 repo_path = strdup(cwd);
7612 if (repo_path == NULL) {
7613 error = got_error_from_errno("strdup");
7619 if (do_list || verify_tags) {
7620 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7623 error = get_allowed_signers(&allowed_signers, repo, worktree);
7626 error = get_revoked_signers(&revoked_signers, repo, worktree);
7630 /* Release work tree lock. */
7631 got_worktree_close(worktree);
7636 * Remove "cpath" promise unless needed for signature tmpfile
7640 got_sigs_apply_unveil();
7643 if (pledge("stdio rpath wpath flock proc exec sendfd "
7644 "unveil", NULL) == -1)
7648 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7651 error = list_tags(repo, tag_name, verify_tags, allowed_signers,
7652 revoked_signers, verbosity);
7654 error = get_gitconfig_path(&gitconfig_path);
7657 error = got_repo_open(&repo, repo_path, gitconfig_path,
7662 error = get_author(&tagger, repo, worktree);
7665 if (signer_id == NULL) {
7666 error = get_signer_id(&signer_id, repo, worktree);
7673 error = got_sigs_apply_unveil();
7677 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
7682 if (commit_id_arg == NULL) {
7683 struct got_reference *head_ref;
7684 struct got_object_id *commit_id;
7685 error = got_ref_open(&head_ref, repo,
7686 worktree ? got_worktree_get_head_ref_name(worktree)
7690 error = got_ref_resolve(&commit_id, repo, head_ref);
7691 got_ref_close(head_ref);
7694 error = got_object_id_str(&commit_id_str, commit_id);
7701 /* Release work tree lock. */
7702 got_worktree_close(worktree);
7706 error = add_tag(repo, tagger, tag_name,
7707 commit_id_str ? commit_id_str : commit_id_arg, tagmsg,
7708 signer_id, verbosity);
7712 const struct got_error *close_err = got_repo_close(repo);
7717 got_worktree_close(worktree);
7719 const struct got_error *pack_err =
7720 got_repo_pack_fds_close(pack_fds);
7726 free(gitconfig_path);
7727 free(commit_id_str);
7729 free(allowed_signers);
7730 free(revoked_signers);
7738 fprintf(stderr, "usage: %s add [-IR] path ...\n", getprogname());
7742 static const struct got_error *
7743 add_progress(void *arg, unsigned char status, const char *path)
7745 while (path[0] == '/')
7747 printf("%c %s\n", status, path);
7751 static const struct got_error *
7752 cmd_add(int argc, char *argv[])
7754 const struct got_error *error = NULL;
7755 struct got_repository *repo = NULL;
7756 struct got_worktree *worktree = NULL;
7758 struct got_pathlist_head paths;
7759 struct got_pathlist_entry *pe;
7760 int ch, can_recurse = 0, no_ignores = 0;
7761 int *pack_fds = NULL;
7765 while ((ch = getopt(argc, argv, "IR")) != -1) {
7783 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7790 cwd = getcwd(NULL, 0);
7792 error = got_error_from_errno("getcwd");
7796 error = got_repo_pack_fds_open(&pack_fds);
7800 error = got_worktree_open(&worktree, cwd);
7802 if (error->code == GOT_ERR_NOT_WORKTREE)
7803 error = wrap_not_worktree_error(error, "add", cwd);
7807 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7812 error = apply_unveil(got_repo_get_path(repo), 1,
7813 got_worktree_get_root_path(worktree));
7817 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7824 TAILQ_FOREACH(pe, &paths, entry) {
7825 if (asprintf(&ondisk_path, "%s/%s",
7826 got_worktree_get_root_path(worktree),
7828 error = got_error_from_errno("asprintf");
7831 if (lstat(ondisk_path, &sb) == -1) {
7832 if (errno == ENOENT) {
7836 error = got_error_from_errno2("lstat",
7842 if (S_ISDIR(sb.st_mode)) {
7843 error = got_error_msg(GOT_ERR_BAD_PATH,
7844 "adding directories requires -R option");
7850 error = got_worktree_schedule_add(worktree, &paths, add_progress,
7851 NULL, repo, no_ignores);
7854 const struct got_error *close_err = got_repo_close(repo);
7859 got_worktree_close(worktree);
7861 const struct got_error *pack_err =
7862 got_repo_pack_fds_close(pack_fds);
7866 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
7874 fprintf(stderr, "usage: %s remove [-fkR] [-s status-codes] path ...\n",
7879 static const struct got_error *
7880 print_remove_status(void *arg, unsigned char status,
7881 unsigned char staged_status, const char *path)
7883 while (path[0] == '/')
7885 if (status == GOT_STATUS_NONEXISTENT)
7887 if (status == staged_status && (status == GOT_STATUS_DELETE))
7888 status = GOT_STATUS_NO_CHANGE;
7889 printf("%c%c %s\n", status, staged_status, path);
7893 static const struct got_error *
7894 cmd_remove(int argc, char *argv[])
7896 const struct got_error *error = NULL;
7897 struct got_worktree *worktree = NULL;
7898 struct got_repository *repo = NULL;
7899 const char *status_codes = NULL;
7901 struct got_pathlist_head paths;
7902 struct got_pathlist_entry *pe;
7903 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0, i;
7904 int ignore_missing_paths = 0;
7905 int *pack_fds = NULL;
7909 while ((ch = getopt(argc, argv, "fkRs:")) != -1) {
7912 delete_local_mods = 1;
7913 ignore_missing_paths = 1;
7922 for (i = 0; i < strlen(optarg); i++) {
7923 switch (optarg[i]) {
7924 case GOT_STATUS_MODIFY:
7925 delete_local_mods = 1;
7927 case GOT_STATUS_MISSING:
7928 ignore_missing_paths = 1;
7931 errx(1, "invalid status code '%c'",
7935 status_codes = optarg;
7947 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7954 cwd = getcwd(NULL, 0);
7956 error = got_error_from_errno("getcwd");
7960 error = got_repo_pack_fds_open(&pack_fds);
7964 error = got_worktree_open(&worktree, cwd);
7966 if (error->code == GOT_ERR_NOT_WORKTREE)
7967 error = wrap_not_worktree_error(error, "remove", cwd);
7971 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7976 error = apply_unveil(got_repo_get_path(repo), 1,
7977 got_worktree_get_root_path(worktree));
7981 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7988 TAILQ_FOREACH(pe, &paths, entry) {
7989 if (asprintf(&ondisk_path, "%s/%s",
7990 got_worktree_get_root_path(worktree),
7992 error = got_error_from_errno("asprintf");
7995 if (lstat(ondisk_path, &sb) == -1) {
7996 if (errno == ENOENT) {
8000 error = got_error_from_errno2("lstat",
8006 if (S_ISDIR(sb.st_mode)) {
8007 error = got_error_msg(GOT_ERR_BAD_PATH,
8008 "removing directories requires -R option");
8014 error = got_worktree_schedule_delete(worktree, &paths,
8015 delete_local_mods, status_codes, print_remove_status, NULL,
8016 repo, keep_on_disk, ignore_missing_paths);
8019 const struct got_error *close_err = got_repo_close(repo);
8024 got_worktree_close(worktree);
8026 const struct got_error *pack_err =
8027 got_repo_pack_fds_close(pack_fds);
8031 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
8039 fprintf(stderr, "usage: %s patch [-nR] [-c commit] [-p strip-count] "
8040 "[patchfile]\n", getprogname());
8044 static const struct got_error *
8045 patch_from_stdin(int *patchfd)
8047 const struct got_error *err = NULL;
8050 sig_t sighup, sigint, sigquit;
8052 *patchfd = got_opentempfd();
8054 return got_error_from_errno("got_opentempfd");
8056 sighup = signal(SIGHUP, SIG_DFL);
8057 sigint = signal(SIGINT, SIG_DFL);
8058 sigquit = signal(SIGQUIT, SIG_DFL);
8061 r = read(0, buf, sizeof(buf));
8063 err = got_error_from_errno("read");
8068 if (write(*patchfd, buf, r) == -1) {
8069 err = got_error_from_errno("write");
8074 signal(SIGHUP, sighup);
8075 signal(SIGINT, sigint);
8076 signal(SIGQUIT, sigquit);
8078 if (err == NULL && lseek(*patchfd, 0, SEEK_SET) == -1)
8079 err = got_error_from_errno("lseek");
8089 static const struct got_error *
8090 patch_progress(void *arg, const char *old, const char *new,
8091 unsigned char status, const struct got_error *error, int old_from,
8092 int old_lines, int new_from, int new_lines, int offset,
8093 int ws_mangled, const struct got_error *hunk_err)
8095 const char *path = new == NULL ? old : new;
8097 while (*path == '/')
8101 printf("%c %s\n", status, path);
8104 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
8106 if (offset != 0 || hunk_err != NULL || ws_mangled) {
8107 printf("@@ -%d,%d +%d,%d @@ ", old_from,
8108 old_lines, new_from, new_lines);
8109 if (hunk_err != NULL)
8110 printf("%s\n", hunk_err->msg);
8111 else if (offset != 0)
8112 printf("applied with offset %d\n", offset);
8114 printf("hunk contains mangled whitespace\n");
8120 static const struct got_error *
8121 cmd_patch(int argc, char *argv[])
8123 const struct got_error *error = NULL, *close_error = NULL;
8124 struct got_worktree *worktree = NULL;
8125 struct got_repository *repo = NULL;
8126 struct got_reflist_head refs;
8127 struct got_object_id *commit_id = NULL;
8128 const char *commit_id_str = NULL;
8132 int ch, nop = 0, strip = -1, reverse = 0;
8134 int *pack_fds = NULL;
8139 if (pledge("stdio rpath wpath cpath fattr proc exec sendfd flock "
8140 "unveil", NULL) == -1)
8144 while ((ch = getopt(argc, argv, "c:np:R")) != -1) {
8147 commit_id_str = optarg;
8153 strip = strtonum(optarg, 0, INT_MAX, &errstr);
8155 errx(1, "pathname strip count is %s: %s",
8171 error = patch_from_stdin(&patchfd);
8174 } else if (argc == 1) {
8175 patchfd = open(argv[0], O_RDONLY);
8176 if (patchfd == -1) {
8177 error = got_error_from_errno2("open", argv[0]);
8180 if (fstat(patchfd, &sb) == -1) {
8181 error = got_error_from_errno2("fstat", argv[0]);
8184 if (!S_ISREG(sb.st_mode)) {
8185 error = got_error_path(argv[0], GOT_ERR_BAD_FILETYPE);
8191 if ((cwd = getcwd(NULL, 0)) == NULL) {
8192 error = got_error_from_errno("getcwd");
8196 error = got_repo_pack_fds_open(&pack_fds);
8200 error = got_worktree_open(&worktree, cwd);
8204 const char *repo_path = got_worktree_get_repo_path(worktree);
8205 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8209 error = apply_unveil(got_repo_get_path(repo), 0,
8210 got_worktree_get_root_path(worktree));
8214 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
8218 if (commit_id_str != NULL) {
8219 error = got_repo_match_object_id(&commit_id, NULL,
8220 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
8225 error = got_patch(patchfd, worktree, repo, nop, strip, reverse,
8226 commit_id, &patch_progress, NULL, check_cancelled, NULL);
8229 got_ref_list_free(&refs);
8232 close_error = got_repo_close(repo);
8234 error = close_error;
8236 if (worktree != NULL) {
8237 close_error = got_worktree_close(worktree);
8239 error = close_error;
8242 const struct got_error *pack_err =
8243 got_repo_pack_fds_close(pack_fds);
8254 fprintf(stderr, "usage: %s revert [-pR] [-F response-script] path ...\n",
8259 static const struct got_error *
8260 revert_progress(void *arg, unsigned char status, const char *path)
8262 if (status == GOT_STATUS_UNVERSIONED)
8265 while (path[0] == '/')
8267 printf("%c %s\n", status, path);
8271 struct choose_patch_arg {
8272 FILE *patch_script_file;
8276 static const struct got_error *
8277 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
8278 int nchanges, const char *action)
8280 const struct got_error *err;
8282 size_t linesize = 0;
8286 case GOT_STATUS_ADD:
8287 printf("A %s\n%s this addition? [y/n] ", path, action);
8289 case GOT_STATUS_DELETE:
8290 printf("D %s\n%s this deletion? [y/n] ", path, action);
8292 case GOT_STATUS_MODIFY:
8293 if (fseek(patch_file, 0L, SEEK_SET) == -1)
8294 return got_error_from_errno("fseek");
8295 printf(GOT_COMMIT_SEP_STR);
8296 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
8298 if (linelen == -1 && ferror(patch_file)) {
8299 err = got_error_from_errno("getline");
8304 printf(GOT_COMMIT_SEP_STR);
8305 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
8306 path, n, nchanges, action);
8309 return got_error_path(path, GOT_ERR_FILE_STATUS);
8316 static const struct got_error *
8317 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
8318 FILE *patch_file, int n, int nchanges)
8320 const struct got_error *err = NULL;
8322 size_t linesize = 0;
8325 struct choose_patch_arg *a = arg;
8327 *choice = GOT_PATCH_CHOICE_NONE;
8329 if (a->patch_script_file) {
8331 err = show_change(status, path, patch_file, n, nchanges,
8335 linelen = getline(&line, &linesize, a->patch_script_file);
8336 if (linelen == -1) {
8337 if (ferror(a->patch_script_file))
8338 return got_error_from_errno("getline");
8341 nl = strchr(line, '\n');
8344 if (strcmp(line, "y") == 0) {
8345 *choice = GOT_PATCH_CHOICE_YES;
8347 } else if (strcmp(line, "n") == 0) {
8348 *choice = GOT_PATCH_CHOICE_NO;
8350 } else if (strcmp(line, "q") == 0 &&
8351 status == GOT_STATUS_MODIFY) {
8352 *choice = GOT_PATCH_CHOICE_QUIT;
8355 printf("invalid response '%s'\n", line);
8360 while (resp != 'y' && resp != 'n' && resp != 'q') {
8361 err = show_change(status, path, patch_file, n, nchanges,
8368 if (status == GOT_STATUS_MODIFY) {
8369 if (resp != 'y' && resp != 'n' && resp != 'q') {
8370 printf("invalid response '%c'\n", resp);
8373 } else if (resp != 'y' && resp != 'n') {
8374 printf("invalid response '%c'\n", resp);
8380 *choice = GOT_PATCH_CHOICE_YES;
8381 else if (resp == 'n')
8382 *choice = GOT_PATCH_CHOICE_NO;
8383 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
8384 *choice = GOT_PATCH_CHOICE_QUIT;
8389 static const struct got_error *
8390 cmd_revert(int argc, char *argv[])
8392 const struct got_error *error = NULL;
8393 struct got_worktree *worktree = NULL;
8394 struct got_repository *repo = NULL;
8395 char *cwd = NULL, *path = NULL;
8396 struct got_pathlist_head paths;
8397 struct got_pathlist_entry *pe;
8398 int ch, can_recurse = 0, pflag = 0;
8399 FILE *patch_script_file = NULL;
8400 const char *patch_script_path = NULL;
8401 struct choose_patch_arg cpa;
8402 int *pack_fds = NULL;
8406 while ((ch = getopt(argc, argv, "F:pR")) != -1) {
8409 patch_script_path = optarg;
8427 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8428 "unveil", NULL) == -1)
8433 if (patch_script_path && !pflag)
8434 errx(1, "-F option can only be used together with -p option");
8436 cwd = getcwd(NULL, 0);
8438 error = got_error_from_errno("getcwd");
8442 error = got_repo_pack_fds_open(&pack_fds);
8446 error = got_worktree_open(&worktree, cwd);
8448 if (error->code == GOT_ERR_NOT_WORKTREE)
8449 error = wrap_not_worktree_error(error, "revert", cwd);
8453 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8458 if (patch_script_path) {
8459 patch_script_file = fopen(patch_script_path, "re");
8460 if (patch_script_file == NULL) {
8461 error = got_error_from_errno2("fopen",
8466 error = apply_unveil(got_repo_get_path(repo), 1,
8467 got_worktree_get_root_path(worktree));
8471 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8478 TAILQ_FOREACH(pe, &paths, entry) {
8479 if (asprintf(&ondisk_path, "%s/%s",
8480 got_worktree_get_root_path(worktree),
8482 error = got_error_from_errno("asprintf");
8485 if (lstat(ondisk_path, &sb) == -1) {
8486 if (errno == ENOENT) {
8490 error = got_error_from_errno2("lstat",
8496 if (S_ISDIR(sb.st_mode)) {
8497 error = got_error_msg(GOT_ERR_BAD_PATH,
8498 "reverting directories requires -R option");
8504 cpa.patch_script_file = patch_script_file;
8505 cpa.action = "revert";
8506 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
8507 pflag ? choose_patch : NULL, &cpa, repo);
8509 if (patch_script_file && fclose(patch_script_file) == EOF &&
8511 error = got_error_from_errno2("fclose", patch_script_path);
8513 const struct got_error *close_err = got_repo_close(repo);
8518 got_worktree_close(worktree);
8520 const struct got_error *pack_err =
8521 got_repo_pack_fds_close(pack_fds);
8533 fprintf(stderr, "usage: %s commit [-NS] [-A author] [-F path] "
8534 "[-m message] [path ...]\n", getprogname());
8538 struct collect_commit_logmsg_arg {
8539 const char *cmdline_log;
8540 const char *prepared_log;
8541 int non_interactive;
8543 const char *worktree_path;
8544 const char *branch_name;
8545 const char *repo_path;
8550 static const struct got_error *
8551 read_prepared_logmsg(char **logmsg, const char *path)
8553 const struct got_error *err = NULL;
8559 memset(&sb, 0, sizeof(sb));
8561 f = fopen(path, "re");
8563 return got_error_from_errno2("fopen", path);
8565 if (fstat(fileno(f), &sb) == -1) {
8566 err = got_error_from_errno2("fstat", path);
8569 if (sb.st_size == 0) {
8570 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
8574 *logmsg = malloc(sb.st_size + 1);
8575 if (*logmsg == NULL) {
8576 err = got_error_from_errno("malloc");
8580 r = fread(*logmsg, 1, sb.st_size, f);
8581 if (r != sb.st_size) {
8583 err = got_error_from_errno2("fread", path);
8585 err = got_error(GOT_ERR_IO);
8588 (*logmsg)[sb.st_size] = '\0';
8590 if (fclose(f) == EOF && err == NULL)
8591 err = got_error_from_errno2("fclose", path);
8599 static const struct got_error *
8600 collect_commit_logmsg(struct got_pathlist_head *commitable_paths,
8601 const char *diff_path, char **logmsg, void *arg)
8603 char *initial_content = NULL;
8604 struct got_pathlist_entry *pe;
8605 const struct got_error *err = NULL;
8606 char *template = NULL;
8607 struct collect_commit_logmsg_arg *a = arg;
8608 int initial_content_len;
8612 /* if a message was specified on the command line, just use it */
8613 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
8614 len = strlen(a->cmdline_log) + 1;
8615 *logmsg = malloc(len + 1);
8616 if (*logmsg == NULL)
8617 return got_error_from_errno("malloc");
8618 strlcpy(*logmsg, a->cmdline_log, len);
8620 } else if (a->prepared_log != NULL && a->non_interactive)
8621 return read_prepared_logmsg(logmsg, a->prepared_log);
8623 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
8624 return got_error_from_errno("asprintf");
8626 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template, "");
8630 if (a->prepared_log) {
8632 err = read_prepared_logmsg(&msg, a->prepared_log);
8635 if (write(fd, msg, strlen(msg)) == -1) {
8636 err = got_error_from_errno2("write", a->logmsg_path);
8643 initial_content_len = asprintf(&initial_content,
8644 "\n# changes to be committed on branch %s:\n",
8646 if (initial_content_len == -1) {
8647 err = got_error_from_errno("asprintf");
8651 if (write(fd, initial_content, initial_content_len) == -1) {
8652 err = got_error_from_errno2("write", a->logmsg_path);
8656 TAILQ_FOREACH(pe, commitable_paths, entry) {
8657 struct got_commitable *ct = pe->data;
8658 dprintf(fd, "# %c %s\n",
8659 got_commitable_get_status(ct),
8660 got_commitable_get_path(ct));
8664 dprintf(fd, "# detailed changes can be viewed in %s\n",
8668 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content,
8669 initial_content_len, a->prepared_log ? 0 : 1);
8671 free(initial_content);
8674 if (fd != -1 && close(fd) == -1 && err == NULL)
8675 err = got_error_from_errno2("close", a->logmsg_path);
8677 /* Editor is done; we can now apply unveil(2) */
8679 err = apply_unveil(a->repo_path, 0, a->worktree_path);
8687 static const struct got_error *
8688 cmd_commit(int argc, char *argv[])
8690 const struct got_error *error = NULL;
8691 struct got_worktree *worktree = NULL;
8692 struct got_repository *repo = NULL;
8693 char *cwd = NULL, *id_str = NULL;
8694 struct got_object_id *id = NULL;
8695 const char *logmsg = NULL;
8696 char *prepared_logmsg = NULL;
8697 struct collect_commit_logmsg_arg cl_arg;
8698 const char *author = NULL;
8699 char *gitconfig_path = NULL, *editor = NULL, *committer = NULL;
8700 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
8701 int allow_bad_symlinks = 0, non_interactive = 0, merge_in_progress = 0;
8703 struct got_pathlist_head paths;
8704 int *pack_fds = NULL;
8707 cl_arg.logmsg_path = NULL;
8709 while ((ch = getopt(argc, argv, "A:F:m:NnS")) != -1) {
8713 error = valid_author(author);
8719 option_conflict('F', 'm');
8720 prepared_logmsg = realpath(optarg, NULL);
8721 if (prepared_logmsg == NULL)
8722 return got_error_from_errno2("realpath",
8726 if (prepared_logmsg)
8727 option_conflict('m', 'F');
8731 non_interactive = 1;
8737 allow_bad_symlinks = 1;
8749 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8750 "unveil", NULL) == -1)
8753 cwd = getcwd(NULL, 0);
8755 error = got_error_from_errno("getcwd");
8759 error = got_repo_pack_fds_open(&pack_fds);
8763 error = got_worktree_open(&worktree, cwd);
8765 if (error->code == GOT_ERR_NOT_WORKTREE)
8766 error = wrap_not_worktree_error(error, "commit", cwd);
8770 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
8773 if (rebase_in_progress) {
8774 error = got_error(GOT_ERR_REBASING);
8778 error = got_worktree_histedit_in_progress(&histedit_in_progress,
8783 error = get_gitconfig_path(&gitconfig_path);
8786 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8787 gitconfig_path, pack_fds);
8791 error = got_worktree_merge_in_progress(&merge_in_progress, worktree, repo);
8794 if (merge_in_progress) {
8795 error = got_error(GOT_ERR_MERGE_BUSY);
8799 error = get_author(&committer, repo, worktree);
8803 if (author != NULL && !strcmp(committer, author)) {
8804 error = got_error(GOT_ERR_COMMIT_REDUNDANT_AUTHOR);
8812 * unveil(2) traverses exec(2); if an editor is used we have
8813 * to apply unveil after the log message has been written.
8815 if (logmsg == NULL || strlen(logmsg) == 0)
8816 error = get_editor(&editor);
8818 error = apply_unveil(got_repo_get_path(repo), 0,
8819 got_worktree_get_root_path(worktree));
8823 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8827 cl_arg.editor = editor;
8828 cl_arg.cmdline_log = logmsg;
8829 cl_arg.prepared_log = prepared_logmsg;
8830 cl_arg.non_interactive = non_interactive;
8831 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
8832 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
8833 if (!histedit_in_progress) {
8834 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
8835 error = got_error(GOT_ERR_COMMIT_BRANCH);
8838 cl_arg.branch_name += 11;
8840 cl_arg.repo_path = got_repo_get_path(repo);
8841 error = got_worktree_commit(&id, worktree, &paths, author, committer,
8842 allow_bad_symlinks, show_diff, collect_commit_logmsg, &cl_arg,
8843 print_status, NULL, repo);
8845 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
8846 cl_arg.logmsg_path != NULL)
8847 preserve_logmsg = 1;
8851 error = got_object_id_str(&id_str, id);
8854 printf("Created commit %s\n", id_str);
8856 if (preserve_logmsg) {
8857 fprintf(stderr, "%s: log message preserved in %s\n",
8858 getprogname(), cl_arg.logmsg_path);
8859 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
8861 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
8862 free(cl_arg.logmsg_path);
8864 const struct got_error *close_err = got_repo_close(repo);
8869 got_worktree_close(worktree);
8871 const struct got_error *pack_err =
8872 got_repo_pack_fds_close(pack_fds);
8878 free(gitconfig_path);
8881 free(prepared_logmsg);
8888 fprintf(stderr, "usage: %s send [-afqTv] [-b branch] [-d branch] "
8889 "[-r repository-path] [-t tag] [remote-repository]\n",
8895 print_load_info(int print_colored, int print_found, int print_trees,
8896 int ncolored, int nfound, int ntrees)
8898 if (print_colored) {
8899 printf("%d commit%s colored", ncolored,
8900 ncolored == 1 ? "" : "s");
8903 printf("%s%d object%s found",
8904 ncolored > 0 ? "; " : "",
8905 nfound, nfound == 1 ? "" : "s");
8908 printf("; %d tree%s scanned", ntrees,
8909 ntrees == 1 ? "" : "s");
8913 struct got_send_progress_arg {
8914 char last_scaled_packsize[FMT_SCALED_STRSIZE];
8921 int last_nobj_total;
8925 int printed_something;
8927 struct got_pathlist_head *delete_branches;
8930 static const struct got_error *
8931 send_progress(void *arg, int ncolored, int nfound, int ntrees,
8932 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
8933 int nobj_written, off_t bytes_sent, const char *refname,
8934 const char *errmsg, int success)
8936 struct got_send_progress_arg *a = arg;
8937 char scaled_packsize[FMT_SCALED_STRSIZE];
8938 char scaled_sent[FMT_SCALED_STRSIZE];
8939 int p_deltify = 0, p_written = 0, p_sent = 0;
8940 int print_colored = 0, print_found = 0, print_trees = 0;
8941 int print_searching = 0, print_total = 0;
8942 int print_deltify = 0, print_written = 0, print_sent = 0;
8944 if (a->verbosity < 0)
8948 const char *status = success ? "accepted" : "rejected";
8951 struct got_pathlist_entry *pe;
8952 TAILQ_FOREACH(pe, a->delete_branches, entry) {
8953 const char *branchname = pe->path;
8954 if (got_path_cmp(branchname, refname,
8955 strlen(branchname), strlen(refname)) == 0) {
8957 a->sent_something = 1;
8963 if (a->printed_something)
8965 printf("Server has %s %s", status, refname);
8967 printf(": %s", errmsg);
8968 a->printed_something = 1;
8972 if (a->last_ncolored != ncolored) {
8974 a->last_ncolored = ncolored;
8977 if (a->last_nfound != nfound) {
8980 a->last_nfound = nfound;
8983 if (a->last_ntrees != ntrees) {
8987 a->last_ntrees = ntrees;
8990 if ((print_colored || print_found || print_trees) &&
8993 print_load_info(print_colored, print_found, print_trees,
8994 ncolored, nfound, ntrees);
8995 a->printed_something = 1;
8998 } else if (!a->loading_done) {
9000 print_load_info(1, 1, 1, ncolored, nfound, ntrees);
9002 a->loading_done = 1;
9005 if (fmt_scaled(packfile_size, scaled_packsize) == -1)
9006 return got_error_from_errno("fmt_scaled");
9007 if (fmt_scaled(bytes_sent, scaled_sent) == -1)
9008 return got_error_from_errno("fmt_scaled");
9010 if (a->last_ncommits != ncommits) {
9011 print_searching = 1;
9012 a->last_ncommits = ncommits;
9015 if (a->last_nobj_total != nobj_total) {
9016 print_searching = 1;
9018 a->last_nobj_total = nobj_total;
9021 if (packfile_size > 0 && (a->last_scaled_packsize[0] == '\0' ||
9022 strcmp(scaled_packsize, a->last_scaled_packsize)) != 0) {
9023 if (strlcpy(a->last_scaled_packsize, scaled_packsize,
9024 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
9025 return got_error(GOT_ERR_NO_SPACE);
9028 if (nobj_deltify > 0 || nobj_written > 0) {
9029 if (nobj_deltify > 0) {
9030 p_deltify = (nobj_deltify * 100) / nobj_total;
9031 if (p_deltify != a->last_p_deltify) {
9032 a->last_p_deltify = p_deltify;
9033 print_searching = 1;
9038 if (nobj_written > 0) {
9039 p_written = (nobj_written * 100) / nobj_total;
9040 if (p_written != a->last_p_written) {
9041 a->last_p_written = p_written;
9042 print_searching = 1;
9050 if (bytes_sent > 0) {
9051 p_sent = (bytes_sent * 100) / packfile_size;
9052 if (p_sent != a->last_p_sent) {
9053 a->last_p_sent = p_sent;
9054 print_searching = 1;
9060 a->sent_something = 1;
9063 if (print_searching || print_total || print_deltify || print_written ||
9066 if (print_searching)
9067 printf("packing %d reference%s", ncommits,
9068 ncommits == 1 ? "" : "s");
9070 printf("; %d object%s", nobj_total,
9071 nobj_total == 1 ? "" : "s");
9073 printf("; deltify: %d%%", p_deltify);
9075 printf("; uploading pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
9076 scaled_packsize, p_sent);
9077 else if (print_written)
9078 printf("; writing pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
9079 scaled_packsize, p_written);
9080 if (print_searching || print_total || print_deltify ||
9081 print_written || print_sent) {
9082 a->printed_something = 1;
9088 static const struct got_error *
9089 cmd_send(int argc, char *argv[])
9091 const struct got_error *error = NULL;
9092 char *cwd = NULL, *repo_path = NULL;
9093 const char *remote_name;
9094 char *proto = NULL, *host = NULL, *port = NULL;
9095 char *repo_name = NULL, *server_path = NULL;
9096 const struct got_remote_repo *remotes, *remote = NULL;
9097 int nremotes, nbranches = 0, ndelete_branches = 0;
9098 struct got_repository *repo = NULL;
9099 struct got_worktree *worktree = NULL;
9100 const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
9101 struct got_pathlist_head branches;
9102 struct got_pathlist_head tags;
9103 struct got_reflist_head all_branches;
9104 struct got_reflist_head all_tags;
9105 struct got_pathlist_head delete_args;
9106 struct got_pathlist_head delete_branches;
9107 struct got_reflist_entry *re;
9108 struct got_pathlist_entry *pe;
9109 int i, ch, sendfd = -1, sendstatus;
9111 struct got_send_progress_arg spa;
9112 int verbosity = 0, overwrite_refs = 0;
9113 int send_all_branches = 0, send_all_tags = 0;
9114 struct got_reference *ref = NULL;
9115 int *pack_fds = NULL;
9117 TAILQ_INIT(&branches);
9119 TAILQ_INIT(&all_branches);
9120 TAILQ_INIT(&all_tags);
9121 TAILQ_INIT(&delete_args);
9122 TAILQ_INIT(&delete_branches);
9124 while ((ch = getopt(argc, argv, "ab:d:fqr:Tt:v")) != -1) {
9127 send_all_branches = 1;
9130 error = got_pathlist_append(&branches, optarg, NULL);
9136 error = got_pathlist_append(&delete_args, optarg, NULL);
9147 repo_path = realpath(optarg, NULL);
9148 if (repo_path == NULL)
9149 return got_error_from_errno2("realpath",
9151 got_path_strip_trailing_slashes(repo_path);
9157 error = got_pathlist_append(&tags, optarg, NULL);
9164 else if (verbosity < 3)
9175 if (send_all_branches && !TAILQ_EMPTY(&branches))
9176 option_conflict('a', 'b');
9177 if (send_all_tags && !TAILQ_EMPTY(&tags))
9178 option_conflict('T', 't');
9182 remote_name = GOT_SEND_DEFAULT_REMOTE_NAME;
9184 remote_name = argv[0];
9188 cwd = getcwd(NULL, 0);
9190 error = got_error_from_errno("getcwd");
9194 error = got_repo_pack_fds_open(&pack_fds);
9198 if (repo_path == NULL) {
9199 error = got_worktree_open(&worktree, cwd);
9200 if (error && error->code != GOT_ERR_NOT_WORKTREE)
9206 strdup(got_worktree_get_repo_path(worktree));
9207 if (repo_path == NULL)
9208 error = got_error_from_errno("strdup");
9212 repo_path = strdup(cwd);
9213 if (repo_path == NULL) {
9214 error = got_error_from_errno("strdup");
9220 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
9225 worktree_conf = got_worktree_get_gotconfig(worktree);
9226 if (worktree_conf) {
9227 got_gotconfig_get_remotes(&nremotes, &remotes,
9229 for (i = 0; i < nremotes; i++) {
9230 if (strcmp(remotes[i].name, remote_name) == 0) {
9231 remote = &remotes[i];
9237 if (remote == NULL) {
9238 repo_conf = got_repo_get_gotconfig(repo);
9240 got_gotconfig_get_remotes(&nremotes, &remotes,
9242 for (i = 0; i < nremotes; i++) {
9243 if (strcmp(remotes[i].name, remote_name) == 0) {
9244 remote = &remotes[i];
9250 if (remote == NULL) {
9251 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
9252 for (i = 0; i < nremotes; i++) {
9253 if (strcmp(remotes[i].name, remote_name) == 0) {
9254 remote = &remotes[i];
9259 if (remote == NULL) {
9260 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
9264 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
9265 &repo_name, remote->send_url);
9269 if (strcmp(proto, "git") == 0) {
9271 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
9272 "sendfd dns inet unveil", NULL) == -1)
9275 } else if (strcmp(proto, "git+ssh") == 0 ||
9276 strcmp(proto, "ssh") == 0) {
9278 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
9279 "sendfd unveil", NULL) == -1)
9282 } else if (strcmp(proto, "http") == 0 ||
9283 strcmp(proto, "git+http") == 0) {
9284 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
9287 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
9291 error = got_dial_apply_unveil(proto);
9295 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
9299 if (send_all_branches) {
9300 error = got_ref_list(&all_branches, repo, "refs/heads",
9301 got_ref_cmp_by_name, NULL);
9304 TAILQ_FOREACH(re, &all_branches, entry) {
9305 const char *branchname = got_ref_get_name(re->ref);
9306 error = got_pathlist_append(&branches,
9312 } else if (nbranches == 0) {
9313 for (i = 0; i < remote->nsend_branches; i++) {
9314 error = got_pathlist_append(&branches,
9315 remote->send_branches[i], NULL);
9321 if (send_all_tags) {
9322 error = got_ref_list(&all_tags, repo, "refs/tags",
9323 got_ref_cmp_by_name, NULL);
9326 TAILQ_FOREACH(re, &all_tags, entry) {
9327 const char *tagname = got_ref_get_name(re->ref);
9328 error = got_pathlist_append(&tags,
9336 * To prevent accidents only branches in refs/heads/ can be deleted
9337 * with 'got send -d'.
9338 * Deleting anything else requires local repository access or Git.
9340 TAILQ_FOREACH(pe, &delete_args, entry) {
9341 const char *branchname = pe->path;
9343 struct got_pathlist_entry *new;
9344 if (strncmp(branchname, "refs/heads/", 11) == 0) {
9345 s = strdup(branchname);
9347 error = got_error_from_errno("strdup");
9351 if (asprintf(&s, "refs/heads/%s", branchname) == -1) {
9352 error = got_error_from_errno("asprintf");
9356 error = got_pathlist_insert(&new, &delete_branches, s, NULL);
9357 if (error || new == NULL /* duplicate */)
9364 if (nbranches == 0 && ndelete_branches == 0) {
9365 struct got_reference *head_ref;
9367 error = got_ref_open(&head_ref, repo,
9368 got_worktree_get_head_ref_name(worktree), 0);
9370 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
9373 if (got_ref_is_symbolic(head_ref)) {
9374 error = got_ref_resolve_symbolic(&ref, repo, head_ref);
9375 got_ref_close(head_ref);
9380 error = got_pathlist_append(&branches, got_ref_get_name(ref),
9387 if (verbosity >= 0) {
9388 printf("Connecting to \"%s\" %s://%s%s%s%s%s\n",
9389 remote->name, proto, host,
9390 port ? ":" : "", port ? port : "",
9391 *server_path == '/' ? "" : "/", server_path);
9394 error = got_send_connect(&sendpid, &sendfd, proto, host, port,
9395 server_path, verbosity);
9399 memset(&spa, 0, sizeof(spa));
9400 spa.last_scaled_packsize[0] = '\0';
9401 spa.last_p_deltify = -1;
9402 spa.last_p_written = -1;
9403 spa.verbosity = verbosity;
9404 spa.delete_branches = &delete_branches;
9405 error = got_send_pack(remote_name, &branches, &tags, &delete_branches,
9406 verbosity, overwrite_refs, sendfd, repo, send_progress, &spa,
9407 check_cancelled, NULL);
9408 if (spa.printed_something)
9412 if (!spa.sent_something && verbosity >= 0)
9413 printf("Already up-to-date\n");
9416 if (kill(sendpid, SIGTERM) == -1)
9417 error = got_error_from_errno("kill");
9418 if (waitpid(sendpid, &sendstatus, 0) == -1 && error == NULL)
9419 error = got_error_from_errno("waitpid");
9421 if (sendfd != -1 && close(sendfd) == -1 && error == NULL)
9422 error = got_error_from_errno("close");
9424 const struct got_error *close_err = got_repo_close(repo);
9429 got_worktree_close(worktree);
9431 const struct got_error *pack_err =
9432 got_repo_pack_fds_close(pack_fds);
9438 got_pathlist_free(&branches, GOT_PATHLIST_FREE_NONE);
9439 got_pathlist_free(&tags, GOT_PATHLIST_FREE_NONE);
9440 got_ref_list_free(&all_branches);
9441 got_ref_list_free(&all_tags);
9442 got_pathlist_free(&delete_args, GOT_PATHLIST_FREE_NONE);
9443 got_pathlist_free(&delete_branches, GOT_PATHLIST_FREE_PATH);
9455 usage_cherrypick(void)
9457 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
9461 static const struct got_error *
9462 cmd_cherrypick(int argc, char *argv[])
9464 const struct got_error *error = NULL;
9465 struct got_worktree *worktree = NULL;
9466 struct got_repository *repo = NULL;
9467 char *cwd = NULL, *commit_id_str = NULL;
9468 struct got_object_id *commit_id = NULL;
9469 struct got_commit_object *commit = NULL;
9470 struct got_object_qid *pid;
9472 struct got_update_progress_arg upa;
9473 int *pack_fds = NULL;
9475 while ((ch = getopt(argc, argv, "")) != -1) {
9487 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9488 "unveil", NULL) == -1)
9494 cwd = getcwd(NULL, 0);
9496 error = got_error_from_errno("getcwd");
9500 error = got_repo_pack_fds_open(&pack_fds);
9504 error = got_worktree_open(&worktree, cwd);
9506 if (error->code == GOT_ERR_NOT_WORKTREE)
9507 error = wrap_not_worktree_error(error, "cherrypick",
9512 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9517 error = apply_unveil(got_repo_get_path(repo), 0,
9518 got_worktree_get_root_path(worktree));
9522 error = got_repo_match_object_id(&commit_id, NULL, argv[0],
9523 GOT_OBJ_TYPE_COMMIT, NULL, repo);
9526 error = got_object_id_str(&commit_id_str, commit_id);
9530 error = got_object_open_as_commit(&commit, repo, commit_id);
9533 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
9534 memset(&upa, 0, sizeof(upa));
9535 error = got_worktree_merge_files(worktree, pid ? &pid->id : NULL,
9536 commit_id, repo, update_progress, &upa, check_cancelled,
9541 if (upa.did_something)
9542 printf("Merged commit %s\n", commit_id_str);
9543 print_merge_progress_stats(&upa);
9546 got_object_commit_close(commit);
9547 free(commit_id_str);
9549 got_worktree_close(worktree);
9551 const struct got_error *close_err = got_repo_close(repo);
9556 const struct got_error *pack_err =
9557 got_repo_pack_fds_close(pack_fds);
9568 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
9572 static const struct got_error *
9573 cmd_backout(int argc, char *argv[])
9575 const struct got_error *error = NULL;
9576 struct got_worktree *worktree = NULL;
9577 struct got_repository *repo = NULL;
9578 char *cwd = NULL, *commit_id_str = NULL;
9579 struct got_object_id *commit_id = NULL;
9580 struct got_commit_object *commit = NULL;
9581 struct got_object_qid *pid;
9583 struct got_update_progress_arg upa;
9584 int *pack_fds = NULL;
9586 while ((ch = getopt(argc, argv, "")) != -1) {
9598 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9599 "unveil", NULL) == -1)
9605 cwd = getcwd(NULL, 0);
9607 error = got_error_from_errno("getcwd");
9611 error = got_repo_pack_fds_open(&pack_fds);
9615 error = got_worktree_open(&worktree, cwd);
9617 if (error->code == GOT_ERR_NOT_WORKTREE)
9618 error = wrap_not_worktree_error(error, "backout", cwd);
9622 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9627 error = apply_unveil(got_repo_get_path(repo), 0,
9628 got_worktree_get_root_path(worktree));
9632 error = got_repo_match_object_id(&commit_id, NULL, argv[0],
9633 GOT_OBJ_TYPE_COMMIT, NULL, repo);
9636 error = got_object_id_str(&commit_id_str, commit_id);
9640 error = got_object_open_as_commit(&commit, repo, commit_id);
9643 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
9645 error = got_error(GOT_ERR_ROOT_COMMIT);
9649 memset(&upa, 0, sizeof(upa));
9650 error = got_worktree_merge_files(worktree, commit_id, &pid->id,
9651 repo, update_progress, &upa, check_cancelled, NULL);
9655 if (upa.did_something)
9656 printf("Backed out commit %s\n", commit_id_str);
9657 print_merge_progress_stats(&upa);
9660 got_object_commit_close(commit);
9661 free(commit_id_str);
9663 got_worktree_close(worktree);
9665 const struct got_error *close_err = got_repo_close(repo);
9670 const struct got_error *pack_err =
9671 got_repo_pack_fds_close(pack_fds);
9681 fprintf(stderr, "usage: %s rebase [-aclX] [branch]\n", getprogname());
9686 trim_logmsg(char *logmsg, int limit)
9691 len = strlen(logmsg);
9695 nl = strchr(logmsg, '\n');
9700 static const struct got_error *
9701 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
9703 const struct got_error *err;
9704 char *logmsg0 = NULL;
9707 err = got_object_commit_get_logmsg(&logmsg0, commit);
9712 while (isspace((unsigned char)s[0]))
9715 *logmsg = strdup(s);
9716 if (*logmsg == NULL) {
9717 err = got_error_from_errno("strdup");
9721 trim_logmsg(*logmsg, limit);
9727 static const struct got_error *
9728 show_rebase_merge_conflict(struct got_object_id *id,
9729 struct got_repository *repo)
9731 const struct got_error *err;
9732 struct got_commit_object *commit = NULL;
9733 char *id_str = NULL, *logmsg = NULL;
9735 err = got_object_open_as_commit(&commit, repo, id);
9739 err = got_object_id_str(&id_str, id);
9745 err = get_short_logmsg(&logmsg, 42, commit);
9749 printf("%s -> merge conflict: %s\n", id_str, logmsg);
9752 got_object_commit_close(commit);
9757 static const struct got_error *
9758 show_rebase_progress(struct got_commit_object *commit,
9759 struct got_object_id *old_id, struct got_object_id *new_id)
9761 const struct got_error *err;
9762 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
9764 err = got_object_id_str(&old_id_str, old_id);
9769 err = got_object_id_str(&new_id_str, new_id);
9774 old_id_str[12] = '\0';
9776 new_id_str[12] = '\0';
9778 err = get_short_logmsg(&logmsg, 42, commit);
9782 printf("%s -> %s: %s\n", old_id_str,
9783 new_id_str ? new_id_str : "no-op change", logmsg);
9791 static const struct got_error *
9792 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
9793 struct got_reference *branch, struct got_reference *new_base_branch,
9794 struct got_reference *tmp_branch, struct got_repository *repo,
9797 printf("Switching work tree to %s\n", got_ref_get_name(branch));
9798 return got_worktree_rebase_complete(worktree, fileindex,
9799 new_base_branch, tmp_branch, branch, repo, create_backup);
9802 static const struct got_error *
9803 rebase_commit(struct got_pathlist_head *merged_paths,
9804 struct got_worktree *worktree, struct got_fileindex *fileindex,
9805 struct got_reference *tmp_branch, const char *committer,
9806 struct got_object_id *commit_id, struct got_repository *repo)
9808 const struct got_error *error;
9809 struct got_commit_object *commit;
9810 struct got_object_id *new_commit_id;
9812 error = got_object_open_as_commit(&commit, repo, commit_id);
9816 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
9817 worktree, fileindex, tmp_branch, committer, commit, commit_id,
9820 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
9822 error = show_rebase_progress(commit, commit_id, NULL);
9824 error = show_rebase_progress(commit, commit_id, new_commit_id);
9825 free(new_commit_id);
9828 got_object_commit_close(commit);
9832 struct check_path_prefix_arg {
9833 const char *path_prefix;
9838 static const struct got_error *
9839 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
9840 struct got_blob_object *blob2, FILE *f1, FILE *f2,
9841 struct got_object_id *id1, struct got_object_id *id2,
9842 const char *path1, const char *path2,
9843 mode_t mode1, mode_t mode2, struct got_repository *repo)
9845 struct check_path_prefix_arg *a = arg;
9847 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
9848 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
9849 return got_error(a->errcode);
9854 static const struct got_error *
9855 check_path_prefix(struct got_object_id *parent_id,
9856 struct got_object_id *commit_id, const char *path_prefix,
9857 int errcode, struct got_repository *repo)
9859 const struct got_error *err;
9860 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
9861 struct got_commit_object *commit = NULL, *parent_commit = NULL;
9862 struct check_path_prefix_arg cpp_arg;
9864 if (got_path_is_root_dir(path_prefix))
9867 err = got_object_open_as_commit(&commit, repo, commit_id);
9871 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
9875 err = got_object_open_as_tree(&tree1, repo,
9876 got_object_commit_get_tree_id(parent_commit));
9880 err = got_object_open_as_tree(&tree2, repo,
9881 got_object_commit_get_tree_id(commit));
9885 cpp_arg.path_prefix = path_prefix;
9886 while (cpp_arg.path_prefix[0] == '/')
9887 cpp_arg.path_prefix++;
9888 cpp_arg.len = strlen(cpp_arg.path_prefix);
9889 cpp_arg.errcode = errcode;
9890 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
9891 check_path_prefix_in_diff, &cpp_arg, 0);
9894 got_object_tree_close(tree1);
9896 got_object_tree_close(tree2);
9898 got_object_commit_close(commit);
9900 got_object_commit_close(parent_commit);
9904 static const struct got_error *
9905 collect_commits(struct got_object_id_queue *commits,
9906 struct got_object_id *initial_commit_id,
9907 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
9908 const char *path_prefix, int path_prefix_errcode,
9909 struct got_repository *repo)
9911 const struct got_error *err = NULL;
9912 struct got_commit_graph *graph = NULL;
9913 struct got_object_id parent_id, commit_id;
9914 struct got_object_qid *qid;
9916 err = got_commit_graph_open(&graph, "/", 1);
9920 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
9921 check_cancelled, NULL);
9925 memcpy(&commit_id, initial_commit_id, sizeof(commit_id));
9926 while (got_object_id_cmp(&commit_id, iter_stop_id) != 0) {
9927 err = got_commit_graph_iter_next(&parent_id, graph, repo,
9928 check_cancelled, NULL);
9930 if (err->code == GOT_ERR_ITER_COMPLETED) {
9931 err = got_error_msg(GOT_ERR_ANCESTRY,
9932 "ran out of commits to rebase before "
9933 "youngest common ancestor commit has "
9938 err = check_path_prefix(&parent_id, &commit_id,
9939 path_prefix, path_prefix_errcode, repo);
9943 err = got_object_qid_alloc(&qid, &commit_id);
9946 STAILQ_INSERT_HEAD(commits, qid, entry);
9948 memcpy(&commit_id, &parent_id, sizeof(commit_id));
9952 got_commit_graph_close(graph);
9956 static const struct got_error *
9957 get_commit_brief_str(char **brief_str, struct got_commit_object *commit)
9959 const struct got_error *err = NULL;
9960 time_t committer_time;
9962 char datebuf[11]; /* YYYY-MM-DD + NUL */
9963 char *author0 = NULL, *author, *smallerthan;
9964 char *logmsg0 = NULL, *logmsg, *newline;
9966 committer_time = got_object_commit_get_committer_time(commit);
9967 if (gmtime_r(&committer_time, &tm) == NULL)
9968 return got_error_from_errno("gmtime_r");
9969 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d", &tm) == 0)
9970 return got_error(GOT_ERR_NO_SPACE);
9972 author0 = strdup(got_object_commit_get_author(commit));
9973 if (author0 == NULL)
9974 return got_error_from_errno("strdup");
9976 smallerthan = strchr(author, '<');
9977 if (smallerthan && smallerthan[1] != '\0')
9978 author = smallerthan + 1;
9979 author[strcspn(author, "@>")] = '\0';
9981 err = got_object_commit_get_logmsg(&logmsg0, commit);
9985 while (*logmsg == '\n')
9987 newline = strchr(logmsg, '\n');
9991 if (asprintf(brief_str, "%s %s %s",
9992 datebuf, author, logmsg) == -1)
9993 err = got_error_from_errno("asprintf");
10000 static const struct got_error *
10001 delete_backup_ref(struct got_reference *ref, struct got_object_id *id,
10002 struct got_repository *repo)
10004 const struct got_error *err;
10007 err = got_object_id_str(&id_str, id);
10011 err = got_ref_delete(ref, repo);
10015 printf("Deleted %s: %s\n", got_ref_get_name(ref), id_str);
10021 static const struct got_error *
10022 print_backup_ref(const char *branch_name, const char *new_id_str,
10023 struct got_object_id *old_commit_id, struct got_commit_object *old_commit,
10024 struct got_reflist_object_id_map *refs_idmap,
10025 struct got_repository *repo)
10027 const struct got_error *err = NULL;
10028 struct got_reflist_head *refs;
10029 char *refs_str = NULL;
10030 struct got_object_id *new_commit_id = NULL;
10031 struct got_commit_object *new_commit = NULL;
10032 char *new_commit_brief_str = NULL;
10033 struct got_object_id *yca_id = NULL;
10034 struct got_commit_object *yca_commit = NULL;
10035 char *yca_id_str = NULL, *yca_brief_str = NULL;
10036 char *custom_refs_str;
10038 if (asprintf(&custom_refs_str, "formerly %s", branch_name) == -1)
10039 return got_error_from_errno("asprintf");
10041 err = print_commit(old_commit, old_commit_id, repo, NULL, NULL, NULL,
10042 0, 0, refs_idmap, custom_refs_str);
10046 err = got_object_resolve_id_str(&new_commit_id, repo, new_id_str);
10050 refs = got_reflist_object_id_map_lookup(refs_idmap, new_commit_id);
10052 err = build_refs_str(&refs_str, refs, new_commit_id, repo, 0);
10057 err = got_object_open_as_commit(&new_commit, repo, new_commit_id);
10061 err = get_commit_brief_str(&new_commit_brief_str, new_commit);
10065 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
10066 old_commit_id, new_commit_id, 1, repo, check_cancelled, NULL);
10070 printf("has become commit %s%s%s%s\n %s\n", new_id_str,
10071 refs_str ? " (" : "", refs_str ? refs_str : "",
10072 refs_str ? ")" : "", new_commit_brief_str);
10073 if (yca_id && got_object_id_cmp(yca_id, new_commit_id) != 0 &&
10074 got_object_id_cmp(yca_id, old_commit_id) != 0) {
10078 err = got_object_open_as_commit(&yca_commit, repo, yca_id);
10082 err = get_commit_brief_str(&yca_brief_str, yca_commit);
10086 err = got_object_id_str(&yca_id_str, yca_id);
10090 refs = got_reflist_object_id_map_lookup(refs_idmap, yca_id);
10092 err = build_refs_str(&refs_str, refs, yca_id, repo, 0);
10096 printf("history forked at %s%s%s%s\n %s\n",
10098 refs_str ? " (" : "", refs_str ? refs_str : "",
10099 refs_str ? ")" : "", yca_brief_str);
10102 free(custom_refs_str);
10103 free(new_commit_id);
10107 free(yca_brief_str);
10109 got_object_commit_close(new_commit);
10111 got_object_commit_close(yca_commit);
10116 static const struct got_error *
10117 process_backup_refs(const char *backup_ref_prefix,
10118 const char *wanted_branch_name,
10119 int delete, struct got_repository *repo)
10121 const struct got_error *err;
10122 struct got_reflist_head refs, backup_refs;
10123 struct got_reflist_entry *re;
10124 const size_t backup_ref_prefix_len = strlen(backup_ref_prefix);
10125 struct got_object_id *old_commit_id = NULL;
10126 char *branch_name = NULL;
10127 struct got_commit_object *old_commit = NULL;
10128 struct got_reflist_object_id_map *refs_idmap = NULL;
10129 int wanted_branch_found = 0;
10132 TAILQ_INIT(&backup_refs);
10134 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
10138 err = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
10142 if (wanted_branch_name) {
10143 if (strncmp(wanted_branch_name, "refs/heads/", 11) == 0)
10144 wanted_branch_name += 11;
10147 err = got_ref_list(&backup_refs, repo, backup_ref_prefix,
10148 got_ref_cmp_by_commit_timestamp_descending, repo);
10152 TAILQ_FOREACH(re, &backup_refs, entry) {
10153 const char *refname = got_ref_get_name(re->ref);
10156 err = check_cancelled(NULL);
10160 err = got_ref_resolve(&old_commit_id, repo, re->ref);
10164 err = got_object_open_as_commit(&old_commit, repo,
10169 if (strncmp(backup_ref_prefix, refname,
10170 backup_ref_prefix_len) == 0)
10171 refname += backup_ref_prefix_len;
10173 while (refname[0] == '/')
10176 branch_name = strdup(refname);
10177 if (branch_name == NULL) {
10178 err = got_error_from_errno("strdup");
10181 slash = strrchr(branch_name, '/');
10184 refname += strlen(branch_name) + 1;
10187 if (wanted_branch_name == NULL ||
10188 strcmp(wanted_branch_name, branch_name) == 0) {
10189 wanted_branch_found = 1;
10191 err = delete_backup_ref(re->ref,
10192 old_commit_id, repo);
10194 err = print_backup_ref(branch_name, refname,
10195 old_commit_id, old_commit, refs_idmap,
10202 free(old_commit_id);
10203 old_commit_id = NULL;
10205 branch_name = NULL;
10206 got_object_commit_close(old_commit);
10210 if (wanted_branch_name && !wanted_branch_found) {
10211 err = got_error_fmt(GOT_ERR_NOT_REF,
10212 "%s/%s/", backup_ref_prefix, wanted_branch_name);
10216 got_reflist_object_id_map_free(refs_idmap);
10217 got_ref_list_free(&refs);
10218 got_ref_list_free(&backup_refs);
10219 free(old_commit_id);
10222 got_object_commit_close(old_commit);
10226 static const struct got_error *
10227 abort_progress(void *arg, unsigned char status, const char *path)
10230 * Unversioned files should not clutter progress output when
10231 * an operation is aborted.
10233 if (status == GOT_STATUS_UNVERSIONED)
10236 return update_progress(arg, status, path);
10239 static const struct got_error *
10240 cmd_rebase(int argc, char *argv[])
10242 const struct got_error *error = NULL;
10243 struct got_worktree *worktree = NULL;
10244 struct got_repository *repo = NULL;
10245 struct got_fileindex *fileindex = NULL;
10246 char *cwd = NULL, *committer = NULL, *gitconfig_path = NULL;
10247 struct got_reference *branch = NULL;
10248 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
10249 struct got_object_id *commit_id = NULL, *parent_id = NULL;
10250 struct got_object_id *resume_commit_id = NULL;
10251 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
10252 struct got_object_id *head_commit_id = NULL;
10253 struct got_reference *head_ref = NULL;
10254 struct got_commit_object *commit = NULL;
10255 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
10256 int histedit_in_progress = 0, merge_in_progress = 0;
10257 int create_backup = 1, list_backups = 0, delete_backups = 0;
10258 struct got_object_id_queue commits;
10259 struct got_pathlist_head merged_paths;
10260 const struct got_object_id_queue *parent_ids;
10261 struct got_object_qid *qid, *pid;
10262 struct got_update_progress_arg upa;
10263 int *pack_fds = NULL;
10265 STAILQ_INIT(&commits);
10266 TAILQ_INIT(&merged_paths);
10267 memset(&upa, 0, sizeof(upa));
10269 while ((ch = getopt(argc, argv, "aclX")) != -1) {
10275 continue_rebase = 1;
10281 delete_backups = 1;
10293 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10294 "unveil", NULL) == -1)
10297 if (list_backups) {
10299 option_conflict('l', 'a');
10300 if (continue_rebase)
10301 option_conflict('l', 'c');
10302 if (delete_backups)
10303 option_conflict('l', 'X');
10304 if (argc != 0 && argc != 1)
10306 } else if (delete_backups) {
10308 option_conflict('X', 'a');
10309 if (continue_rebase)
10310 option_conflict('X', 'c');
10312 option_conflict('l', 'X');
10313 if (argc != 0 && argc != 1)
10316 if (abort_rebase && continue_rebase)
10318 else if (abort_rebase || continue_rebase) {
10321 } else if (argc != 1)
10325 cwd = getcwd(NULL, 0);
10327 error = got_error_from_errno("getcwd");
10331 error = got_repo_pack_fds_open(&pack_fds);
10335 error = got_worktree_open(&worktree, cwd);
10337 if (list_backups || delete_backups) {
10338 if (error->code != GOT_ERR_NOT_WORKTREE)
10341 if (error->code == GOT_ERR_NOT_WORKTREE)
10342 error = wrap_not_worktree_error(error,
10348 error = get_gitconfig_path(&gitconfig_path);
10351 error = got_repo_open(&repo,
10352 worktree ? got_worktree_get_repo_path(worktree) : cwd,
10353 gitconfig_path, pack_fds);
10357 error = get_author(&committer, repo, worktree);
10358 if (error && error->code != GOT_ERR_COMMIT_NO_AUTHOR)
10361 error = apply_unveil(got_repo_get_path(repo), 0,
10362 worktree ? got_worktree_get_root_path(worktree) : NULL);
10366 if (list_backups || delete_backups) {
10367 error = process_backup_refs(
10368 GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
10369 argc == 1 ? argv[0] : NULL, delete_backups, repo);
10370 goto done; /* nothing else to do */
10373 error = got_worktree_histedit_in_progress(&histedit_in_progress,
10377 if (histedit_in_progress) {
10378 error = got_error(GOT_ERR_HISTEDIT_BUSY);
10382 error = got_worktree_merge_in_progress(&merge_in_progress,
10386 if (merge_in_progress) {
10387 error = got_error(GOT_ERR_MERGE_BUSY);
10391 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
10395 if (abort_rebase) {
10396 if (!rebase_in_progress) {
10397 error = got_error(GOT_ERR_NOT_REBASING);
10400 error = got_worktree_rebase_continue(&resume_commit_id,
10401 &new_base_branch, &tmp_branch, &branch, &fileindex,
10405 printf("Switching work tree to %s\n",
10406 got_ref_get_symref_target(new_base_branch));
10407 error = got_worktree_rebase_abort(worktree, fileindex, repo,
10408 new_base_branch, abort_progress, &upa);
10411 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
10412 print_merge_progress_stats(&upa);
10413 goto done; /* nothing else to do */
10416 if (continue_rebase) {
10417 if (!rebase_in_progress) {
10418 error = got_error(GOT_ERR_NOT_REBASING);
10421 error = got_worktree_rebase_continue(&resume_commit_id,
10422 &new_base_branch, &tmp_branch, &branch, &fileindex,
10427 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
10428 committer, resume_commit_id, repo);
10432 yca_id = got_object_id_dup(resume_commit_id);
10433 if (yca_id == NULL) {
10434 error = got_error_from_errno("got_object_id_dup");
10438 error = got_ref_open(&branch, repo, argv[0], 0);
10441 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
10442 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
10443 "will not rebase a branch which lives outside "
10444 "the \"refs/heads/\" reference namespace");
10449 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
10453 if (!continue_rebase) {
10454 struct got_object_id *base_commit_id;
10456 error = got_ref_open(&head_ref, repo,
10457 got_worktree_get_head_ref_name(worktree), 0);
10460 error = got_ref_resolve(&head_commit_id, repo, head_ref);
10463 base_commit_id = got_worktree_get_base_commit_id(worktree);
10464 if (got_object_id_cmp(base_commit_id, head_commit_id) != 0) {
10465 error = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
10469 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
10470 base_commit_id, branch_head_commit_id, 1, repo,
10471 check_cancelled, NULL);
10474 if (yca_id == NULL) {
10475 error = got_error_msg(GOT_ERR_ANCESTRY,
10476 "specified branch shares no common ancestry "
10477 "with work tree's branch");
10481 error = check_same_branch(base_commit_id, branch, yca_id, repo);
10483 if (error->code != GOT_ERR_ANCESTRY)
10487 struct got_pathlist_head paths;
10488 printf("%s is already based on %s\n",
10489 got_ref_get_name(branch),
10490 got_worktree_get_head_ref_name(worktree));
10491 error = switch_head_ref(branch, branch_head_commit_id,
10495 error = got_worktree_set_base_commit_id(worktree, repo,
10496 branch_head_commit_id);
10499 TAILQ_INIT(&paths);
10500 error = got_pathlist_append(&paths, "", NULL);
10503 error = got_worktree_checkout_files(worktree,
10504 &paths, repo, update_progress, &upa,
10505 check_cancelled, NULL);
10506 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
10509 if (upa.did_something) {
10511 error = got_object_id_str(&id_str,
10512 branch_head_commit_id);
10515 printf("Updated to %s: %s\n",
10516 got_worktree_get_head_ref_name(worktree),
10520 printf("Already up-to-date\n");
10521 print_update_progress_stats(&upa);
10526 commit_id = branch_head_commit_id;
10527 error = got_object_open_as_commit(&commit, repo, commit_id);
10531 parent_ids = got_object_commit_get_parent_ids(commit);
10532 pid = STAILQ_FIRST(parent_ids);
10534 error = got_error(GOT_ERR_EMPTY_REBASE);
10537 error = collect_commits(&commits, commit_id, &pid->id,
10538 yca_id, got_worktree_get_path_prefix(worktree),
10539 GOT_ERR_REBASE_PATH, repo);
10540 got_object_commit_close(commit);
10545 if (!continue_rebase) {
10546 error = got_worktree_rebase_prepare(&new_base_branch,
10547 &tmp_branch, &fileindex, worktree, branch, repo);
10552 if (STAILQ_EMPTY(&commits)) {
10553 if (continue_rebase) {
10554 error = rebase_complete(worktree, fileindex,
10555 branch, new_base_branch, tmp_branch, repo,
10559 /* Fast-forward the reference of the branch. */
10560 struct got_object_id *new_head_commit_id;
10562 error = got_ref_resolve(&new_head_commit_id, repo,
10566 error = got_object_id_str(&id_str, new_head_commit_id);
10569 printf("Forwarding %s to commit %s\n",
10570 got_ref_get_name(branch), id_str);
10572 error = got_ref_change_ref(branch,
10573 new_head_commit_id);
10576 /* No backup needed since objects did not change. */
10582 STAILQ_FOREACH(qid, &commits, entry) {
10584 commit_id = &qid->id;
10585 parent_id = pid ? &pid->id : yca_id;
10588 memset(&upa, 0, sizeof(upa));
10589 error = got_worktree_rebase_merge_files(&merged_paths,
10590 worktree, fileindex, parent_id, commit_id, repo,
10591 update_progress, &upa, check_cancelled, NULL);
10595 print_merge_progress_stats(&upa);
10596 if (upa.conflicts > 0 || upa.missing > 0 ||
10597 upa.not_deleted > 0 || upa.unversioned > 0) {
10598 if (upa.conflicts > 0) {
10599 error = show_rebase_merge_conflict(&qid->id,
10604 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
10608 error = rebase_commit(&merged_paths, worktree, fileindex,
10609 tmp_branch, committer, commit_id, repo);
10610 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
10615 if (upa.conflicts > 0 || upa.missing > 0 ||
10616 upa.not_deleted > 0 || upa.unversioned > 0) {
10617 error = got_worktree_rebase_postpone(worktree, fileindex);
10620 if (upa.conflicts > 0 && upa.missing == 0 &&
10621 upa.not_deleted == 0 && upa.unversioned == 0) {
10622 error = got_error_msg(GOT_ERR_CONFLICTS,
10623 "conflicts must be resolved before rebasing "
10625 } else if (upa.conflicts > 0) {
10626 error = got_error_msg(GOT_ERR_CONFLICTS,
10627 "conflicts must be resolved before rebasing "
10628 "can continue; changes destined for some "
10629 "files were not yet merged and should be "
10630 "merged manually if required before the "
10631 "rebase operation is continued");
10633 error = got_error_msg(GOT_ERR_CONFLICTS,
10634 "changes destined for some files were not "
10635 "yet merged and should be merged manually "
10636 "if required before the rebase operation "
10640 error = rebase_complete(worktree, fileindex, branch,
10641 new_base_branch, tmp_branch, repo, create_backup);
10645 free(gitconfig_path);
10646 got_object_id_queue_free(&commits);
10647 free(branch_head_commit_id);
10648 free(resume_commit_id);
10649 free(head_commit_id);
10652 got_object_commit_close(commit);
10654 got_ref_close(branch);
10655 if (new_base_branch)
10656 got_ref_close(new_base_branch);
10658 got_ref_close(tmp_branch);
10660 got_ref_close(head_ref);
10662 got_worktree_close(worktree);
10664 const struct got_error *close_err = got_repo_close(repo);
10669 const struct got_error *pack_err =
10670 got_repo_pack_fds_close(pack_fds);
10678 usage_histedit(void)
10680 fprintf(stderr, "usage: %s histedit [-aceflmX] [-F histedit-script] "
10681 "[branch]\n", getprogname());
10685 #define GOT_HISTEDIT_PICK 'p'
10686 #define GOT_HISTEDIT_EDIT 'e'
10687 #define GOT_HISTEDIT_FOLD 'f'
10688 #define GOT_HISTEDIT_DROP 'd'
10689 #define GOT_HISTEDIT_MESG 'm'
10691 static const struct got_histedit_cmd {
10692 unsigned char code;
10695 } got_histedit_cmds[] = {
10696 { GOT_HISTEDIT_PICK, "pick", "use commit" },
10697 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
10698 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
10700 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
10701 { GOT_HISTEDIT_MESG, "mesg",
10702 "single-line log message for commit above (open editor if empty)" },
10705 struct got_histedit_list_entry {
10706 TAILQ_ENTRY(got_histedit_list_entry) entry;
10707 struct got_object_id *commit_id;
10708 const struct got_histedit_cmd *cmd;
10711 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
10713 static const struct got_error *
10714 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
10715 FILE *f, struct got_repository *repo)
10717 const struct got_error *err = NULL;
10718 char *logmsg = NULL, *id_str = NULL;
10719 struct got_commit_object *commit = NULL;
10722 err = got_object_open_as_commit(&commit, repo, commit_id);
10726 err = get_short_logmsg(&logmsg, 34, commit);
10730 err = got_object_id_str(&id_str, commit_id);
10734 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
10736 err = got_ferror(f, GOT_ERR_IO);
10739 got_object_commit_close(commit);
10745 static const struct got_error *
10746 histedit_write_commit_list(struct got_object_id_queue *commits,
10747 FILE *f, int edit_logmsg_only, int fold_only, int edit_only,
10748 struct got_repository *repo)
10750 const struct got_error *err = NULL;
10751 struct got_object_qid *qid;
10752 const char *histedit_cmd = NULL;
10754 if (STAILQ_EMPTY(commits))
10755 return got_error(GOT_ERR_EMPTY_HISTEDIT);
10757 STAILQ_FOREACH(qid, commits, entry) {
10758 histedit_cmd = got_histedit_cmds[0].name;
10760 histedit_cmd = "edit";
10761 else if (fold_only && STAILQ_NEXT(qid, entry) != NULL)
10762 histedit_cmd = "fold";
10763 err = histedit_write_commit(&qid->id, histedit_cmd, f, repo);
10766 if (edit_logmsg_only) {
10767 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
10769 err = got_ferror(f, GOT_ERR_IO);
10778 static const struct got_error *
10779 write_cmd_list(FILE *f, const char *branch_name,
10780 struct got_object_id_queue *commits)
10782 const struct got_error *err = NULL;
10786 struct got_object_qid *qid;
10788 qid = STAILQ_FIRST(commits);
10789 err = got_object_id_str(&id_str, &qid->id);
10794 "# Editing the history of branch '%s' starting at\n"
10796 "# Commits will be processed in order from top to "
10797 "bottom of this file.\n", branch_name, id_str);
10799 err = got_ferror(f, GOT_ERR_IO);
10803 n = fprintf(f, "# Available histedit commands:\n");
10805 err = got_ferror(f, GOT_ERR_IO);
10809 for (i = 0; i < nitems(got_histedit_cmds); i++) {
10810 const struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
10811 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
10814 err = got_ferror(f, GOT_ERR_IO);
10823 static const struct got_error *
10824 histedit_syntax_error(int lineno)
10826 static char msg[42];
10829 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
10831 if (ret < 0 || (size_t)ret >= sizeof(msg))
10832 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
10834 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
10837 static const struct got_error *
10838 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
10839 char *logmsg, struct got_repository *repo)
10841 const struct got_error *err;
10842 struct got_commit_object *folded_commit = NULL;
10843 char *id_str, *folded_logmsg = NULL;
10845 err = got_object_id_str(&id_str, hle->commit_id);
10849 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
10853 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
10856 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
10857 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
10858 folded_logmsg) == -1) {
10859 err = got_error_from_errno("asprintf");
10863 got_object_commit_close(folded_commit);
10865 free(folded_logmsg);
10869 static struct got_histedit_list_entry *
10870 get_folded_commits(struct got_histedit_list_entry *hle)
10872 struct got_histedit_list_entry *prev, *folded = NULL;
10874 prev = TAILQ_PREV(hle, got_histedit_list, entry);
10875 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
10876 prev->cmd->code == GOT_HISTEDIT_DROP)) {
10877 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
10879 prev = TAILQ_PREV(prev, got_histedit_list, entry);
10885 static const struct got_error *
10886 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
10887 struct got_repository *repo)
10889 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
10890 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
10891 const struct got_error *err = NULL;
10892 struct got_commit_object *commit = NULL;
10895 struct got_histedit_list_entry *folded = NULL;
10897 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
10901 folded = get_folded_commits(hle);
10903 while (folded != hle) {
10904 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
10905 folded = TAILQ_NEXT(folded, entry);
10908 err = append_folded_commit_msg(&new_msg, folded,
10914 folded = TAILQ_NEXT(folded, entry);
10918 err = got_object_id_str(&id_str, hle->commit_id);
10921 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
10924 logmsg_len = asprintf(&new_msg,
10925 "%s\n# original log message of commit %s: %s",
10926 logmsg ? logmsg : "", id_str, orig_logmsg);
10927 if (logmsg_len == -1) {
10928 err = got_error_from_errno("asprintf");
10934 err = got_object_id_str(&id_str, hle->commit_id);
10938 err = got_opentemp_named_fd(&logmsg_path, &fd,
10939 GOT_TMPDIR_STR "/got-logmsg", "");
10943 write(fd, logmsg, logmsg_len);
10946 err = get_editor(&editor);
10950 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg,
10953 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
10956 hle->logmsg = strdup(new_msg);
10957 if (hle->logmsg == NULL)
10958 err = got_error_from_errno("strdup");
10961 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
10962 err = got_error_from_errno2("unlink", logmsg_path);
10968 got_object_commit_close(commit);
10972 static const struct got_error *
10973 histedit_parse_list(struct got_histedit_list *histedit_cmds,
10974 FILE *f, struct got_repository *repo)
10976 const struct got_error *err = NULL;
10977 char *line = NULL, *p, *end;
10980 int lineno = 0, lastcmd = -1;
10981 const struct got_histedit_cmd *cmd;
10982 struct got_object_id *commit_id = NULL;
10983 struct got_histedit_list_entry *hle = NULL;
10986 len = getline(&line, &size, f);
10988 const struct got_error *getline_err;
10991 getline_err = got_error_from_errno("getline");
10992 err = got_ferror(f, getline_err->code);
10997 while (isspace((unsigned char)p[0]))
10999 if (p[0] == '#' || p[0] == '\0') {
11005 for (i = 0; i < nitems(got_histedit_cmds); i++) {
11006 cmd = &got_histedit_cmds[i];
11007 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
11008 isspace((unsigned char)p[strlen(cmd->name)])) {
11009 p += strlen(cmd->name);
11012 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
11017 if (i == nitems(got_histedit_cmds)) {
11018 err = histedit_syntax_error(lineno);
11021 while (isspace((unsigned char)p[0]))
11023 if (cmd->code == GOT_HISTEDIT_MESG) {
11024 if (lastcmd != GOT_HISTEDIT_PICK &&
11025 lastcmd != GOT_HISTEDIT_EDIT) {
11026 err = got_error(GOT_ERR_HISTEDIT_CMD);
11029 if (p[0] == '\0') {
11030 err = histedit_edit_logmsg(hle, repo);
11034 hle->logmsg = strdup(p);
11035 if (hle->logmsg == NULL) {
11036 err = got_error_from_errno("strdup");
11042 lastcmd = cmd->code;
11046 while (end[0] && !isspace((unsigned char)end[0]))
11050 err = got_object_resolve_id_str(&commit_id, repo, p);
11052 /* override error code */
11053 err = histedit_syntax_error(lineno);
11057 hle = malloc(sizeof(*hle));
11059 err = got_error_from_errno("malloc");
11063 hle->commit_id = commit_id;
11064 hle->logmsg = NULL;
11068 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
11069 lastcmd = cmd->code;
11077 static const struct got_error *
11078 histedit_check_script(struct got_histedit_list *histedit_cmds,
11079 struct got_object_id_queue *commits, struct got_repository *repo)
11081 const struct got_error *err = NULL;
11082 struct got_object_qid *qid;
11083 struct got_histedit_list_entry *hle;
11084 static char msg[92];
11087 if (TAILQ_EMPTY(histedit_cmds))
11088 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
11089 "histedit script contains no commands");
11090 if (STAILQ_EMPTY(commits))
11091 return got_error(GOT_ERR_EMPTY_HISTEDIT);
11093 TAILQ_FOREACH(hle, histedit_cmds, entry) {
11094 struct got_histedit_list_entry *hle2;
11095 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
11098 if (got_object_id_cmp(hle->commit_id,
11099 hle2->commit_id) != 0)
11101 err = got_object_id_str(&id_str, hle->commit_id);
11104 snprintf(msg, sizeof(msg), "commit %s is listed "
11105 "more than once in histedit script", id_str);
11107 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
11111 STAILQ_FOREACH(qid, commits, entry) {
11112 TAILQ_FOREACH(hle, histedit_cmds, entry) {
11113 if (got_object_id_cmp(&qid->id, hle->commit_id) == 0)
11117 err = got_object_id_str(&id_str, &qid->id);
11120 snprintf(msg, sizeof(msg),
11121 "commit %s missing from histedit script", id_str);
11123 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
11127 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
11128 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
11129 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
11130 "last commit in histedit script cannot be folded");
11135 static const struct got_error *
11136 histedit_run_editor(struct got_histedit_list *histedit_cmds,
11137 const char *path, struct got_object_id_queue *commits,
11138 struct got_repository *repo)
11140 const struct got_error *err = NULL;
11144 err = get_editor(&editor);
11148 if (spawn_editor(editor, path) == -1) {
11149 err = got_error_from_errno("failed spawning editor");
11153 f = fopen(path, "re");
11155 err = got_error_from_errno("fopen");
11158 err = histedit_parse_list(histedit_cmds, f, repo);
11162 err = histedit_check_script(histedit_cmds, commits, repo);
11164 if (f && fclose(f) == EOF && err == NULL)
11165 err = got_error_from_errno("fclose");
11170 static const struct got_error *
11171 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
11172 struct got_object_id_queue *, const char *, const char *,
11173 struct got_repository *);
11175 static const struct got_error *
11176 histedit_edit_script(struct got_histedit_list *histedit_cmds,
11177 struct got_object_id_queue *commits, const char *branch_name,
11178 int edit_logmsg_only, int fold_only, int edit_only,
11179 struct got_repository *repo)
11181 const struct got_error *err;
11185 err = got_opentemp_named(&path, &f, "got-histedit", "");
11189 err = write_cmd_list(f, branch_name, commits);
11193 err = histedit_write_commit_list(commits, f, edit_logmsg_only,
11194 fold_only, edit_only, repo);
11198 if (edit_logmsg_only || fold_only || edit_only) {
11200 err = histedit_parse_list(histedit_cmds, f, repo);
11202 if (fclose(f) == EOF) {
11203 err = got_error_from_errno("fclose");
11207 err = histedit_run_editor(histedit_cmds, path, commits, repo);
11209 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
11210 err->code != GOT_ERR_HISTEDIT_CMD)
11212 err = histedit_edit_list_retry(histedit_cmds, err,
11213 commits, path, branch_name, repo);
11217 if (f && fclose(f) == EOF && err == NULL)
11218 err = got_error_from_errno("fclose");
11219 if (path && unlink(path) != 0 && err == NULL)
11220 err = got_error_from_errno2("unlink", path);
11225 static const struct got_error *
11226 histedit_save_list(struct got_histedit_list *histedit_cmds,
11227 struct got_worktree *worktree, struct got_repository *repo)
11229 const struct got_error *err = NULL;
11232 struct got_histedit_list_entry *hle;
11233 struct got_commit_object *commit = NULL;
11235 err = got_worktree_get_histedit_script_path(&path, worktree);
11239 f = fopen(path, "we");
11241 err = got_error_from_errno2("fopen", path);
11244 TAILQ_FOREACH(hle, histedit_cmds, entry) {
11245 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
11251 int n = fprintf(f, "%c %s\n",
11252 GOT_HISTEDIT_MESG, hle->logmsg);
11254 err = got_ferror(f, GOT_ERR_IO);
11260 if (f && fclose(f) == EOF && err == NULL)
11261 err = got_error_from_errno("fclose");
11264 got_object_commit_close(commit);
11269 histedit_free_list(struct got_histedit_list *histedit_cmds)
11271 struct got_histedit_list_entry *hle;
11273 while ((hle = TAILQ_FIRST(histedit_cmds))) {
11274 TAILQ_REMOVE(histedit_cmds, hle, entry);
11279 static const struct got_error *
11280 histedit_load_list(struct got_histedit_list *histedit_cmds,
11281 const char *path, struct got_repository *repo)
11283 const struct got_error *err = NULL;
11286 f = fopen(path, "re");
11288 err = got_error_from_errno2("fopen", path);
11292 err = histedit_parse_list(histedit_cmds, f, repo);
11294 if (f && fclose(f) == EOF && err == NULL)
11295 err = got_error_from_errno("fclose");
11299 static const struct got_error *
11300 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
11301 const struct got_error *edit_err, struct got_object_id_queue *commits,
11302 const char *path, const char *branch_name, struct got_repository *repo)
11304 const struct got_error *err = NULL, *prev_err = edit_err;
11307 while (resp != 'c' && resp != 'r' && resp != 'a') {
11308 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
11309 "or (a)bort: ", getprogname(), prev_err->msg);
11314 histedit_free_list(histedit_cmds);
11315 err = histedit_run_editor(histedit_cmds, path, commits,
11318 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
11319 err->code != GOT_ERR_HISTEDIT_CMD)
11326 } else if (resp == 'r') {
11327 histedit_free_list(histedit_cmds);
11328 err = histedit_edit_script(histedit_cmds,
11329 commits, branch_name, 0, 0, 0, repo);
11331 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
11332 err->code != GOT_ERR_HISTEDIT_CMD)
11339 } else if (resp == 'a') {
11340 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
11343 printf("invalid response '%c'\n", resp);
11349 static const struct got_error *
11350 histedit_complete(struct got_worktree *worktree,
11351 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
11352 struct got_reference *branch, struct got_repository *repo)
11354 printf("Switching work tree to %s\n",
11355 got_ref_get_symref_target(branch));
11356 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
11360 static const struct got_error *
11361 show_histedit_progress(struct got_commit_object *commit,
11362 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
11364 const struct got_error *err;
11365 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
11367 err = got_object_id_str(&old_id_str, hle->commit_id);
11372 err = got_object_id_str(&new_id_str, new_id);
11377 old_id_str[12] = '\0';
11379 new_id_str[12] = '\0';
11382 logmsg = strdup(hle->logmsg);
11383 if (logmsg == NULL) {
11384 err = got_error_from_errno("strdup");
11387 trim_logmsg(logmsg, 42);
11389 err = get_short_logmsg(&logmsg, 42, commit);
11394 switch (hle->cmd->code) {
11395 case GOT_HISTEDIT_PICK:
11396 case GOT_HISTEDIT_EDIT:
11397 printf("%s -> %s: %s\n", old_id_str,
11398 new_id_str ? new_id_str : "no-op change", logmsg);
11400 case GOT_HISTEDIT_DROP:
11401 case GOT_HISTEDIT_FOLD:
11402 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
11414 static const struct got_error *
11415 histedit_commit(struct got_pathlist_head *merged_paths,
11416 struct got_worktree *worktree, struct got_fileindex *fileindex,
11417 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
11418 const char *committer, struct got_repository *repo)
11420 const struct got_error *err;
11421 struct got_commit_object *commit;
11422 struct got_object_id *new_commit_id;
11424 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
11425 && hle->logmsg == NULL) {
11426 err = histedit_edit_logmsg(hle, repo);
11431 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
11435 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
11436 worktree, fileindex, tmp_branch, committer, commit, hle->commit_id,
11437 hle->logmsg, repo);
11439 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
11441 err = show_histedit_progress(commit, hle, NULL);
11443 err = show_histedit_progress(commit, hle, new_commit_id);
11444 free(new_commit_id);
11447 got_object_commit_close(commit);
11451 static const struct got_error *
11452 histedit_skip_commit(struct got_histedit_list_entry *hle,
11453 struct got_worktree *worktree, struct got_repository *repo)
11455 const struct got_error *error;
11456 struct got_commit_object *commit;
11458 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
11463 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
11467 error = show_histedit_progress(commit, hle, NULL);
11468 got_object_commit_close(commit);
11472 static const struct got_error *
11473 check_local_changes(void *arg, unsigned char status,
11474 unsigned char staged_status, const char *path,
11475 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
11476 struct got_object_id *commit_id, int dirfd, const char *de_name)
11478 int *have_local_changes = arg;
11481 case GOT_STATUS_ADD:
11482 case GOT_STATUS_DELETE:
11483 case GOT_STATUS_MODIFY:
11484 case GOT_STATUS_CONFLICT:
11485 *have_local_changes = 1;
11486 return got_error(GOT_ERR_CANCELLED);
11491 switch (staged_status) {
11492 case GOT_STATUS_ADD:
11493 case GOT_STATUS_DELETE:
11494 case GOT_STATUS_MODIFY:
11495 *have_local_changes = 1;
11496 return got_error(GOT_ERR_CANCELLED);
11504 static const struct got_error *
11505 cmd_histedit(int argc, char *argv[])
11507 const struct got_error *error = NULL;
11508 struct got_worktree *worktree = NULL;
11509 struct got_fileindex *fileindex = NULL;
11510 struct got_repository *repo = NULL;
11511 char *cwd = NULL, *committer = NULL, *gitconfig_path = NULL;
11512 struct got_reference *branch = NULL;
11513 struct got_reference *tmp_branch = NULL;
11514 struct got_object_id *resume_commit_id = NULL;
11515 struct got_object_id *base_commit_id = NULL;
11516 struct got_object_id *head_commit_id = NULL;
11517 struct got_commit_object *commit = NULL;
11518 int ch, rebase_in_progress = 0, merge_in_progress = 0;
11519 struct got_update_progress_arg upa;
11520 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
11521 int edit_logmsg_only = 0, fold_only = 0, edit_only = 0;
11522 int list_backups = 0, delete_backups = 0;
11523 const char *edit_script_path = NULL;
11524 struct got_object_id_queue commits;
11525 struct got_pathlist_head merged_paths;
11526 const struct got_object_id_queue *parent_ids;
11527 struct got_object_qid *pid;
11528 struct got_histedit_list histedit_cmds;
11529 struct got_histedit_list_entry *hle;
11530 int *pack_fds = NULL;
11532 STAILQ_INIT(&commits);
11533 TAILQ_INIT(&histedit_cmds);
11534 TAILQ_INIT(&merged_paths);
11535 memset(&upa, 0, sizeof(upa));
11537 while ((ch = getopt(argc, argv, "aceF:flmX")) != -1) {
11549 edit_script_path = optarg;
11558 edit_logmsg_only = 1;
11561 delete_backups = 1;
11573 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
11574 "unveil", NULL) == -1)
11577 if (abort_edit && continue_edit)
11578 option_conflict('a', 'c');
11579 if (edit_script_path && edit_logmsg_only)
11580 option_conflict('F', 'm');
11581 if (abort_edit && edit_logmsg_only)
11582 option_conflict('a', 'm');
11583 if (continue_edit && edit_logmsg_only)
11584 option_conflict('c', 'm');
11585 if (abort_edit && fold_only)
11586 option_conflict('a', 'f');
11587 if (continue_edit && fold_only)
11588 option_conflict('c', 'f');
11589 if (fold_only && edit_logmsg_only)
11590 option_conflict('f', 'm');
11591 if (edit_script_path && fold_only)
11592 option_conflict('F', 'f');
11593 if (abort_edit && edit_only)
11594 option_conflict('a', 'e');
11595 if (continue_edit && edit_only)
11596 option_conflict('c', 'e');
11597 if (edit_only && edit_logmsg_only)
11598 option_conflict('e', 'm');
11599 if (edit_script_path && edit_only)
11600 option_conflict('F', 'e');
11601 if (list_backups) {
11603 option_conflict('l', 'a');
11605 option_conflict('l', 'c');
11606 if (edit_script_path)
11607 option_conflict('l', 'F');
11608 if (edit_logmsg_only)
11609 option_conflict('l', 'm');
11611 option_conflict('l', 'f');
11613 option_conflict('l', 'e');
11614 if (delete_backups)
11615 option_conflict('l', 'X');
11616 if (argc != 0 && argc != 1)
11618 } else if (delete_backups) {
11620 option_conflict('X', 'a');
11622 option_conflict('X', 'c');
11623 if (edit_script_path)
11624 option_conflict('X', 'F');
11625 if (edit_logmsg_only)
11626 option_conflict('X', 'm');
11628 option_conflict('X', 'f');
11630 option_conflict('X', 'e');
11632 option_conflict('X', 'l');
11633 if (argc != 0 && argc != 1)
11635 } else if (argc != 0)
11639 * This command cannot apply unveil(2) in all cases because the
11640 * user may choose to run an editor to edit the histedit script
11641 * and to edit individual commit log messages.
11642 * unveil(2) traverses exec(2); if an editor is used we have to
11643 * apply unveil after edit script and log messages have been written.
11644 * XXX TODO: Make use of unveil(2) where possible.
11647 cwd = getcwd(NULL, 0);
11649 error = got_error_from_errno("getcwd");
11653 error = got_repo_pack_fds_open(&pack_fds);
11657 error = got_worktree_open(&worktree, cwd);
11659 if (list_backups || delete_backups) {
11660 if (error->code != GOT_ERR_NOT_WORKTREE)
11663 if (error->code == GOT_ERR_NOT_WORKTREE)
11664 error = wrap_not_worktree_error(error,
11670 if (list_backups || delete_backups) {
11671 error = got_repo_open(&repo,
11672 worktree ? got_worktree_get_repo_path(worktree) : cwd,
11676 error = apply_unveil(got_repo_get_path(repo), 0,
11677 worktree ? got_worktree_get_root_path(worktree) : NULL);
11680 error = process_backup_refs(
11681 GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
11682 argc == 1 ? argv[0] : NULL, delete_backups, repo);
11683 goto done; /* nothing else to do */
11686 error = get_gitconfig_path(&gitconfig_path);
11689 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
11690 gitconfig_path, pack_fds);
11694 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
11697 if (rebase_in_progress) {
11698 error = got_error(GOT_ERR_REBASING);
11702 error = got_worktree_merge_in_progress(&merge_in_progress, worktree,
11706 if (merge_in_progress) {
11707 error = got_error(GOT_ERR_MERGE_BUSY);
11711 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
11715 if (edit_in_progress && edit_logmsg_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 -m option can be used");
11722 if (edit_in_progress && fold_only) {
11723 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
11724 "histedit operation is in progress in this "
11725 "work tree and must be continued or aborted "
11726 "before the -f option can be used");
11729 if (edit_in_progress && edit_only) {
11730 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
11731 "histedit operation is in progress in this "
11732 "work tree and must be continued or aborted "
11733 "before the -e option can be used");
11737 if (edit_in_progress && abort_edit) {
11738 error = got_worktree_histedit_continue(&resume_commit_id,
11739 &tmp_branch, &branch, &base_commit_id, &fileindex,
11743 printf("Switching work tree to %s\n",
11744 got_ref_get_symref_target(branch));
11745 error = got_worktree_histedit_abort(worktree, fileindex, repo,
11746 branch, base_commit_id, abort_progress, &upa);
11749 printf("Histedit of %s aborted\n",
11750 got_ref_get_symref_target(branch));
11751 print_merge_progress_stats(&upa);
11752 goto done; /* nothing else to do */
11753 } else if (abort_edit) {
11754 error = got_error(GOT_ERR_NOT_HISTEDIT);
11758 error = get_author(&committer, repo, worktree);
11762 if (continue_edit) {
11765 if (!edit_in_progress) {
11766 error = got_error(GOT_ERR_NOT_HISTEDIT);
11770 error = got_worktree_get_histedit_script_path(&path, worktree);
11774 error = histedit_load_list(&histedit_cmds, path, repo);
11779 error = got_worktree_histedit_continue(&resume_commit_id,
11780 &tmp_branch, &branch, &base_commit_id, &fileindex,
11785 error = got_ref_resolve(&head_commit_id, repo, branch);
11789 error = got_object_open_as_commit(&commit, repo,
11793 parent_ids = got_object_commit_get_parent_ids(commit);
11794 pid = STAILQ_FIRST(parent_ids);
11796 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
11799 error = collect_commits(&commits, head_commit_id, &pid->id,
11800 base_commit_id, got_worktree_get_path_prefix(worktree),
11801 GOT_ERR_HISTEDIT_PATH, repo);
11802 got_object_commit_close(commit);
11807 if (edit_in_progress) {
11808 error = got_error(GOT_ERR_HISTEDIT_BUSY);
11812 error = got_ref_open(&branch, repo,
11813 got_worktree_get_head_ref_name(worktree), 0);
11817 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
11818 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
11819 "will not edit commit history of a branch outside "
11820 "the \"refs/heads/\" reference namespace");
11824 error = got_ref_resolve(&head_commit_id, repo, branch);
11825 got_ref_close(branch);
11830 error = got_object_open_as_commit(&commit, repo,
11834 parent_ids = got_object_commit_get_parent_ids(commit);
11835 pid = STAILQ_FIRST(parent_ids);
11837 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
11840 error = collect_commits(&commits, head_commit_id, &pid->id,
11841 got_worktree_get_base_commit_id(worktree),
11842 got_worktree_get_path_prefix(worktree),
11843 GOT_ERR_HISTEDIT_PATH, repo);
11844 got_object_commit_close(commit);
11849 if (STAILQ_EMPTY(&commits)) {
11850 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
11854 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
11855 &base_commit_id, &fileindex, worktree, repo);
11859 if (edit_script_path) {
11860 error = histedit_load_list(&histedit_cmds,
11861 edit_script_path, repo);
11863 got_worktree_histedit_abort(worktree, fileindex,
11864 repo, branch, base_commit_id,
11865 abort_progress, &upa);
11866 print_merge_progress_stats(&upa);
11870 const char *branch_name;
11871 branch_name = got_ref_get_symref_target(branch);
11872 if (strncmp(branch_name, "refs/heads/", 11) == 0)
11874 error = histedit_edit_script(&histedit_cmds, &commits,
11875 branch_name, edit_logmsg_only, fold_only,
11878 got_worktree_histedit_abort(worktree, fileindex,
11879 repo, branch, base_commit_id,
11880 abort_progress, &upa);
11881 print_merge_progress_stats(&upa);
11887 error = histedit_save_list(&histedit_cmds, worktree,
11890 got_worktree_histedit_abort(worktree, fileindex,
11891 repo, branch, base_commit_id,
11892 abort_progress, &upa);
11893 print_merge_progress_stats(&upa);
11899 error = histedit_check_script(&histedit_cmds, &commits, repo);
11903 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
11904 if (resume_commit_id) {
11905 if (got_object_id_cmp(hle->commit_id,
11906 resume_commit_id) != 0)
11909 resume_commit_id = NULL;
11910 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
11911 hle->cmd->code == GOT_HISTEDIT_FOLD) {
11912 error = histedit_skip_commit(hle, worktree,
11917 struct got_pathlist_head paths;
11918 int have_changes = 0;
11920 TAILQ_INIT(&paths);
11921 error = got_pathlist_append(&paths, "", NULL);
11924 error = got_worktree_status(worktree, &paths,
11925 repo, 0, check_local_changes, &have_changes,
11926 check_cancelled, NULL);
11927 got_pathlist_free(&paths,
11928 GOT_PATHLIST_FREE_NONE);
11930 if (error->code != GOT_ERR_CANCELLED)
11932 if (sigint_received || sigpipe_received)
11935 if (have_changes) {
11936 error = histedit_commit(NULL, worktree,
11937 fileindex, tmp_branch, hle,
11942 error = got_object_open_as_commit(
11943 &commit, repo, hle->commit_id);
11946 error = show_histedit_progress(commit,
11948 got_object_commit_close(commit);
11957 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
11958 error = histedit_skip_commit(hle, worktree, repo);
11964 error = got_object_open_as_commit(&commit, repo,
11968 parent_ids = got_object_commit_get_parent_ids(commit);
11969 pid = STAILQ_FIRST(parent_ids);
11971 error = got_worktree_histedit_merge_files(&merged_paths,
11972 worktree, fileindex, &pid->id, hle->commit_id, repo,
11973 update_progress, &upa, check_cancelled, NULL);
11976 got_object_commit_close(commit);
11979 print_merge_progress_stats(&upa);
11980 if (upa.conflicts > 0 || upa.missing > 0 ||
11981 upa.not_deleted > 0 || upa.unversioned > 0) {
11982 if (upa.conflicts > 0) {
11983 error = show_rebase_merge_conflict(
11984 hle->commit_id, repo);
11988 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
11992 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
11994 error = got_object_id_str(&id_str, hle->commit_id);
11997 printf("Stopping histedit for amending commit %s\n",
12000 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
12001 error = got_worktree_histedit_postpone(worktree,
12006 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
12007 error = histedit_skip_commit(hle, worktree, repo);
12013 error = histedit_commit(&merged_paths, worktree, fileindex,
12014 tmp_branch, hle, committer, repo);
12015 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
12020 if (upa.conflicts > 0 || upa.missing > 0 ||
12021 upa.not_deleted > 0 || upa.unversioned > 0) {
12022 error = got_worktree_histedit_postpone(worktree, fileindex);
12025 if (upa.conflicts > 0 && upa.missing == 0 &&
12026 upa.not_deleted == 0 && upa.unversioned == 0) {
12027 error = got_error_msg(GOT_ERR_CONFLICTS,
12028 "conflicts must be resolved before histedit "
12030 } else if (upa.conflicts > 0) {
12031 error = got_error_msg(GOT_ERR_CONFLICTS,
12032 "conflicts must be resolved before histedit "
12033 "can continue; changes destined for some "
12034 "files were not yet merged and should be "
12035 "merged manually if required before the "
12036 "histedit operation is continued");
12038 error = got_error_msg(GOT_ERR_CONFLICTS,
12039 "changes destined for some files were not "
12040 "yet merged and should be merged manually "
12041 "if required before the histedit operation "
12045 error = histedit_complete(worktree, fileindex, tmp_branch,
12050 free(gitconfig_path);
12051 got_object_id_queue_free(&commits);
12052 histedit_free_list(&histedit_cmds);
12053 free(head_commit_id);
12054 free(base_commit_id);
12055 free(resume_commit_id);
12057 got_object_commit_close(commit);
12059 got_ref_close(branch);
12061 got_ref_close(tmp_branch);
12063 got_worktree_close(worktree);
12065 const struct got_error *close_err = got_repo_close(repo);
12070 const struct got_error *pack_err =
12071 got_repo_pack_fds_close(pack_fds);
12079 usage_integrate(void)
12081 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
12085 static const struct got_error *
12086 cmd_integrate(int argc, char *argv[])
12088 const struct got_error *error = NULL;
12089 struct got_repository *repo = NULL;
12090 struct got_worktree *worktree = NULL;
12091 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
12092 const char *branch_arg = NULL;
12093 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
12094 struct got_fileindex *fileindex = NULL;
12095 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
12097 struct got_update_progress_arg upa;
12098 int *pack_fds = NULL;
12100 while ((ch = getopt(argc, argv, "")) != -1) {
12113 branch_arg = argv[0];
12115 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
12116 "unveil", NULL) == -1)
12119 cwd = getcwd(NULL, 0);
12121 error = got_error_from_errno("getcwd");
12125 error = got_repo_pack_fds_open(&pack_fds);
12129 error = got_worktree_open(&worktree, cwd);
12131 if (error->code == GOT_ERR_NOT_WORKTREE)
12132 error = wrap_not_worktree_error(error, "integrate",
12137 error = check_rebase_or_histedit_in_progress(worktree);
12141 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
12146 error = apply_unveil(got_repo_get_path(repo), 0,
12147 got_worktree_get_root_path(worktree));
12151 error = check_merge_in_progress(worktree, repo);
12155 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
12156 error = got_error_from_errno("asprintf");
12160 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
12161 &base_branch_ref, worktree, refname, repo);
12165 refname = strdup(got_ref_get_name(branch_ref));
12166 if (refname == NULL) {
12167 error = got_error_from_errno("strdup");
12168 got_worktree_integrate_abort(worktree, fileindex, repo,
12169 branch_ref, base_branch_ref);
12172 base_refname = strdup(got_ref_get_name(base_branch_ref));
12173 if (base_refname == NULL) {
12174 error = got_error_from_errno("strdup");
12175 got_worktree_integrate_abort(worktree, fileindex, repo,
12176 branch_ref, base_branch_ref);
12179 if (strncmp(base_refname, "refs/heads/", 11) != 0) {
12180 error = got_error(GOT_ERR_INTEGRATE_BRANCH);
12181 got_worktree_integrate_abort(worktree, fileindex, repo,
12182 branch_ref, base_branch_ref);
12186 error = got_ref_resolve(&commit_id, repo, branch_ref);
12190 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
12194 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
12195 error = got_error_msg(GOT_ERR_SAME_BRANCH,
12196 "specified branch has already been integrated");
12197 got_worktree_integrate_abort(worktree, fileindex, repo,
12198 branch_ref, base_branch_ref);
12202 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
12204 if (error->code == GOT_ERR_ANCESTRY)
12205 error = got_error(GOT_ERR_REBASE_REQUIRED);
12206 got_worktree_integrate_abort(worktree, fileindex, repo,
12207 branch_ref, base_branch_ref);
12211 memset(&upa, 0, sizeof(upa));
12212 error = got_worktree_integrate_continue(worktree, fileindex, repo,
12213 branch_ref, base_branch_ref, update_progress, &upa,
12214 check_cancelled, NULL);
12218 printf("Integrated %s into %s\n", refname, base_refname);
12219 print_update_progress_stats(&upa);
12222 const struct got_error *close_err = got_repo_close(repo);
12227 got_worktree_close(worktree);
12229 const struct got_error *pack_err =
12230 got_repo_pack_fds_close(pack_fds);
12235 free(base_commit_id);
12238 free(base_refname);
12245 fprintf(stderr, "usage: %s merge [-acn] [branch]\n", getprogname());
12249 static const struct got_error *
12250 cmd_merge(int argc, char *argv[])
12252 const struct got_error *error = NULL;
12253 struct got_worktree *worktree = NULL;
12254 struct got_repository *repo = NULL;
12255 struct got_fileindex *fileindex = NULL;
12256 char *cwd = NULL, *id_str = NULL, *author = NULL;
12257 struct got_reference *branch = NULL, *wt_branch = NULL;
12258 struct got_object_id *branch_tip = NULL, *yca_id = NULL;
12259 struct got_object_id *wt_branch_tip = NULL;
12260 int ch, merge_in_progress = 0, abort_merge = 0, continue_merge = 0;
12261 int interrupt_merge = 0;
12262 struct got_update_progress_arg upa;
12263 struct got_object_id *merge_commit_id = NULL;
12264 char *branch_name = NULL;
12265 int *pack_fds = NULL;
12267 memset(&upa, 0, sizeof(upa));
12269 while ((ch = getopt(argc, argv, "acn")) != -1) {
12275 continue_merge = 1;
12278 interrupt_merge = 1;
12290 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
12291 "unveil", NULL) == -1)
12295 if (abort_merge && continue_merge)
12296 option_conflict('a', 'c');
12297 if (abort_merge || continue_merge) {
12300 } else if (argc != 1)
12303 cwd = getcwd(NULL, 0);
12305 error = got_error_from_errno("getcwd");
12309 error = got_repo_pack_fds_open(&pack_fds);
12313 error = got_worktree_open(&worktree, cwd);
12315 if (error->code == GOT_ERR_NOT_WORKTREE)
12316 error = wrap_not_worktree_error(error,
12321 error = got_repo_open(&repo,
12322 worktree ? got_worktree_get_repo_path(worktree) : cwd, NULL,
12327 error = apply_unveil(got_repo_get_path(repo), 0,
12328 worktree ? got_worktree_get_root_path(worktree) : NULL);
12332 error = check_rebase_or_histedit_in_progress(worktree);
12336 error = got_worktree_merge_in_progress(&merge_in_progress, worktree,
12342 if (!merge_in_progress) {
12343 error = got_error(GOT_ERR_NOT_MERGING);
12346 error = got_worktree_merge_continue(&branch_name,
12347 &branch_tip, &fileindex, worktree, repo);
12350 error = got_worktree_merge_abort(worktree, fileindex, repo,
12351 abort_progress, &upa);
12354 printf("Merge of %s aborted\n", branch_name);
12355 goto done; /* nothing else to do */
12358 error = get_author(&author, repo, worktree);
12362 if (continue_merge) {
12363 if (!merge_in_progress) {
12364 error = got_error(GOT_ERR_NOT_MERGING);
12367 error = got_worktree_merge_continue(&branch_name,
12368 &branch_tip, &fileindex, worktree, repo);
12372 error = got_ref_open(&branch, repo, argv[0], 0);
12375 branch_name = strdup(got_ref_get_name(branch));
12376 if (branch_name == NULL) {
12377 error = got_error_from_errno("strdup");
12380 error = got_ref_resolve(&branch_tip, repo, branch);
12385 error = got_ref_open(&wt_branch, repo,
12386 got_worktree_get_head_ref_name(worktree), 0);
12389 error = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
12392 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
12393 wt_branch_tip, branch_tip, 0, repo,
12394 check_cancelled, NULL);
12395 if (error && error->code != GOT_ERR_ANCESTRY)
12398 if (!continue_merge) {
12399 error = check_path_prefix(wt_branch_tip, branch_tip,
12400 got_worktree_get_path_prefix(worktree),
12401 GOT_ERR_MERGE_PATH, repo);
12405 error = check_same_branch(wt_branch_tip, branch,
12408 if (error->code != GOT_ERR_ANCESTRY)
12412 static char msg[512];
12413 snprintf(msg, sizeof(msg),
12414 "cannot create a merge commit because "
12415 "%s is based on %s; %s can be integrated "
12416 "with 'got integrate' instead", branch_name,
12417 got_worktree_get_head_ref_name(worktree),
12419 error = got_error_msg(GOT_ERR_SAME_BRANCH, msg);
12423 error = got_worktree_merge_prepare(&fileindex, worktree,
12428 error = got_worktree_merge_branch(worktree, fileindex,
12429 yca_id, branch_tip, repo, update_progress, &upa,
12430 check_cancelled, NULL);
12433 print_merge_progress_stats(&upa);
12434 if (!upa.did_something) {
12435 error = got_worktree_merge_abort(worktree, fileindex,
12436 repo, abort_progress, &upa);
12439 printf("Already up-to-date\n");
12444 if (interrupt_merge) {
12445 error = got_worktree_merge_postpone(worktree, fileindex);
12448 printf("Merge of %s interrupted on request\n", branch_name);
12449 } else if (upa.conflicts > 0 || upa.missing > 0 ||
12450 upa.not_deleted > 0 || upa.unversioned > 0) {
12451 error = got_worktree_merge_postpone(worktree, fileindex);
12454 if (upa.conflicts > 0 && upa.missing == 0 &&
12455 upa.not_deleted == 0 && upa.unversioned == 0) {
12456 error = got_error_msg(GOT_ERR_CONFLICTS,
12457 "conflicts must be resolved before merging "
12459 } else if (upa.conflicts > 0) {
12460 error = got_error_msg(GOT_ERR_CONFLICTS,
12461 "conflicts must be resolved before merging "
12462 "can continue; changes destined for some "
12463 "files were not yet merged and "
12464 "should be merged manually if required before the "
12465 "merge operation is continued");
12467 error = got_error_msg(GOT_ERR_CONFLICTS,
12468 "changes destined for some "
12469 "files were not yet merged and should be "
12470 "merged manually if required before the "
12471 "merge operation is continued");
12475 error = got_worktree_merge_commit(&merge_commit_id, worktree,
12476 fileindex, author, NULL, 1, branch_tip, branch_name,
12477 repo, continue_merge ? print_status : NULL, NULL);
12480 error = got_worktree_merge_complete(worktree, fileindex, repo);
12483 error = got_object_id_str(&id_str, merge_commit_id);
12486 printf("Merged %s into %s: %s\n", branch_name,
12487 got_worktree_get_head_ref_name(worktree),
12493 free(merge_commit_id);
12499 got_ref_close(branch);
12501 got_ref_close(wt_branch);
12503 got_worktree_close(worktree);
12505 const struct got_error *close_err = got_repo_close(repo);
12510 const struct got_error *pack_err =
12511 got_repo_pack_fds_close(pack_fds);
12521 fprintf(stderr, "usage: %s stage [-lpS] [-F response-script] "
12522 "[path ...]\n", getprogname());
12526 static const struct got_error *
12527 print_stage(void *arg, unsigned char status, unsigned char staged_status,
12528 const char *path, struct got_object_id *blob_id,
12529 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
12530 int dirfd, const char *de_name)
12532 const struct got_error *err = NULL;
12533 char *id_str = NULL;
12535 if (staged_status != GOT_STATUS_ADD &&
12536 staged_status != GOT_STATUS_MODIFY &&
12537 staged_status != GOT_STATUS_DELETE)
12540 if (staged_status == GOT_STATUS_ADD ||
12541 staged_status == GOT_STATUS_MODIFY)
12542 err = got_object_id_str(&id_str, staged_blob_id);
12544 err = got_object_id_str(&id_str, blob_id);
12548 printf("%s %c %s\n", id_str, staged_status, path);
12553 static const struct got_error *
12554 cmd_stage(int argc, char *argv[])
12556 const struct got_error *error = NULL;
12557 struct got_repository *repo = NULL;
12558 struct got_worktree *worktree = NULL;
12560 struct got_pathlist_head paths;
12561 int ch, list_stage = 0, pflag = 0, allow_bad_symlinks = 0;
12562 FILE *patch_script_file = NULL;
12563 const char *patch_script_path = NULL;
12564 struct choose_patch_arg cpa;
12565 int *pack_fds = NULL;
12567 TAILQ_INIT(&paths);
12569 while ((ch = getopt(argc, argv, "F:lpS")) != -1) {
12572 patch_script_path = optarg;
12581 allow_bad_symlinks = 1;
12593 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
12594 "unveil", NULL) == -1)
12597 if (list_stage && (pflag || patch_script_path))
12598 errx(1, "-l option cannot be used with other options");
12599 if (patch_script_path && !pflag)
12600 errx(1, "-F option can only be used together with -p option");
12602 cwd = getcwd(NULL, 0);
12604 error = got_error_from_errno("getcwd");
12608 error = got_repo_pack_fds_open(&pack_fds);
12612 error = got_worktree_open(&worktree, cwd);
12614 if (error->code == GOT_ERR_NOT_WORKTREE)
12615 error = wrap_not_worktree_error(error, "stage", cwd);
12619 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
12624 if (patch_script_path) {
12625 patch_script_file = fopen(patch_script_path, "re");
12626 if (patch_script_file == NULL) {
12627 error = got_error_from_errno2("fopen",
12628 patch_script_path);
12632 error = apply_unveil(got_repo_get_path(repo), 0,
12633 got_worktree_get_root_path(worktree));
12637 error = check_merge_in_progress(worktree, repo);
12641 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
12646 error = got_worktree_status(worktree, &paths, repo, 0,
12647 print_stage, NULL, check_cancelled, NULL);
12649 cpa.patch_script_file = patch_script_file;
12650 cpa.action = "stage";
12651 error = got_worktree_stage(worktree, &paths,
12652 pflag ? NULL : print_status, NULL,
12653 pflag ? choose_patch : NULL, &cpa,
12654 allow_bad_symlinks, repo);
12657 if (patch_script_file && fclose(patch_script_file) == EOF &&
12659 error = got_error_from_errno2("fclose", patch_script_path);
12661 const struct got_error *close_err = got_repo_close(repo);
12666 got_worktree_close(worktree);
12668 const struct got_error *pack_err =
12669 got_repo_pack_fds_close(pack_fds);
12673 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
12679 usage_unstage(void)
12681 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
12682 "[path ...]\n", getprogname());
12687 static const struct got_error *
12688 cmd_unstage(int argc, char *argv[])
12690 const struct got_error *error = NULL;
12691 struct got_repository *repo = NULL;
12692 struct got_worktree *worktree = NULL;
12694 struct got_pathlist_head paths;
12696 struct got_update_progress_arg upa;
12697 FILE *patch_script_file = NULL;
12698 const char *patch_script_path = NULL;
12699 struct choose_patch_arg cpa;
12700 int *pack_fds = NULL;
12702 TAILQ_INIT(&paths);
12704 while ((ch = getopt(argc, argv, "F:p")) != -1) {
12707 patch_script_path = optarg;
12722 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
12723 "unveil", NULL) == -1)
12726 if (patch_script_path && !pflag)
12727 errx(1, "-F option can only be used together with -p option");
12729 cwd = getcwd(NULL, 0);
12731 error = got_error_from_errno("getcwd");
12735 error = got_repo_pack_fds_open(&pack_fds);
12739 error = got_worktree_open(&worktree, cwd);
12741 if (error->code == GOT_ERR_NOT_WORKTREE)
12742 error = wrap_not_worktree_error(error, "unstage", cwd);
12746 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
12751 if (patch_script_path) {
12752 patch_script_file = fopen(patch_script_path, "re");
12753 if (patch_script_file == NULL) {
12754 error = got_error_from_errno2("fopen",
12755 patch_script_path);
12760 error = apply_unveil(got_repo_get_path(repo), 0,
12761 got_worktree_get_root_path(worktree));
12765 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
12769 cpa.patch_script_file = patch_script_file;
12770 cpa.action = "unstage";
12771 memset(&upa, 0, sizeof(upa));
12772 error = got_worktree_unstage(worktree, &paths, update_progress,
12773 &upa, pflag ? choose_patch : NULL, &cpa, repo);
12775 print_merge_progress_stats(&upa);
12777 if (patch_script_file && fclose(patch_script_file) == EOF &&
12779 error = got_error_from_errno2("fclose", patch_script_path);
12781 const struct got_error *close_err = got_repo_close(repo);
12786 got_worktree_close(worktree);
12788 const struct got_error *pack_err =
12789 got_repo_pack_fds_close(pack_fds);
12793 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
12801 fprintf(stderr, "usage: %s cat [-P] [-c commit] [-r repository-path] "
12802 "arg ...\n", getprogname());
12806 static const struct got_error *
12807 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
12809 const struct got_error *err;
12810 struct got_blob_object *blob;
12813 fd = got_opentempfd();
12815 return got_error_from_errno("got_opentempfd");
12817 err = got_object_open_as_blob(&blob, repo, id, 8192, fd);
12821 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
12823 if (fd != -1 && close(fd) == -1 && err == NULL)
12824 err = got_error_from_errno("close");
12826 got_object_blob_close(blob);
12830 static const struct got_error *
12831 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
12833 const struct got_error *err;
12834 struct got_tree_object *tree;
12837 err = got_object_open_as_tree(&tree, repo, id);
12841 nentries = got_object_tree_get_nentries(tree);
12842 for (i = 0; i < nentries; i++) {
12843 struct got_tree_entry *te;
12845 if (sigint_received || sigpipe_received)
12847 te = got_object_tree_get_entry(tree, i);
12848 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
12851 fprintf(outfile, "%s %.7o %s\n", id_str,
12852 got_tree_entry_get_mode(te),
12853 got_tree_entry_get_name(te));
12857 got_object_tree_close(tree);
12861 static const struct got_error *
12862 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
12864 const struct got_error *err;
12865 struct got_commit_object *commit;
12866 const struct got_object_id_queue *parent_ids;
12867 struct got_object_qid *pid;
12868 char *id_str = NULL;
12869 const char *logmsg = NULL;
12872 err = got_object_open_as_commit(&commit, repo, id);
12876 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
12880 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
12881 parent_ids = got_object_commit_get_parent_ids(commit);
12882 fprintf(outfile, "numparents %d\n",
12883 got_object_commit_get_nparents(commit));
12884 STAILQ_FOREACH(pid, parent_ids, entry) {
12886 err = got_object_id_str(&pid_str, &pid->id);
12889 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
12892 got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
12893 got_object_commit_get_author_gmtoff(commit));
12894 fprintf(outfile, "%s%s %lld %s\n", GOT_COMMIT_LABEL_AUTHOR,
12895 got_object_commit_get_author(commit),
12896 (long long)got_object_commit_get_author_time(commit),
12899 got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
12900 got_object_commit_get_committer_gmtoff(commit));
12901 fprintf(outfile, "%s%s %lld %s\n", GOT_COMMIT_LABEL_COMMITTER,
12902 got_object_commit_get_committer(commit),
12903 (long long)got_object_commit_get_committer_time(commit),
12906 logmsg = got_object_commit_get_logmsg_raw(commit);
12907 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
12908 fprintf(outfile, "%s", logmsg);
12911 got_object_commit_close(commit);
12915 static const struct got_error *
12916 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
12918 const struct got_error *err;
12919 struct got_tag_object *tag;
12920 char *id_str = NULL;
12921 const char *tagmsg = NULL;
12924 err = got_object_open_as_tag(&tag, repo, id);
12928 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
12932 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
12934 switch (got_object_tag_get_object_type(tag)) {
12935 case GOT_OBJ_TYPE_BLOB:
12936 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
12937 GOT_OBJ_LABEL_BLOB);
12939 case GOT_OBJ_TYPE_TREE:
12940 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
12941 GOT_OBJ_LABEL_TREE);
12943 case GOT_OBJ_TYPE_COMMIT:
12944 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
12945 GOT_OBJ_LABEL_COMMIT);
12947 case GOT_OBJ_TYPE_TAG:
12948 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
12949 GOT_OBJ_LABEL_TAG);
12955 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
12956 got_object_tag_get_name(tag));
12958 got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
12959 got_object_tag_get_tagger_gmtoff(tag));
12960 fprintf(outfile, "%s%s %lld %s\n", GOT_TAG_LABEL_TAGGER,
12961 got_object_tag_get_tagger(tag),
12962 (long long)got_object_tag_get_tagger_time(tag),
12965 tagmsg = got_object_tag_get_message(tag);
12966 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
12967 fprintf(outfile, "%s", tagmsg);
12970 got_object_tag_close(tag);
12974 static const struct got_error *
12975 cmd_cat(int argc, char *argv[])
12977 const struct got_error *error;
12978 struct got_repository *repo = NULL;
12979 struct got_worktree *worktree = NULL;
12980 char *cwd = NULL, *repo_path = NULL, *label = NULL;
12981 const char *commit_id_str = NULL;
12982 struct got_object_id *id = NULL, *commit_id = NULL;
12983 struct got_commit_object *commit = NULL;
12984 int ch, obj_type, i, force_path = 0;
12985 struct got_reflist_head refs;
12986 int *pack_fds = NULL;
12991 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
12996 while ((ch = getopt(argc, argv, "c:Pr:")) != -1) {
12999 commit_id_str = optarg;
13005 repo_path = realpath(optarg, NULL);
13006 if (repo_path == NULL)
13007 return got_error_from_errno2("realpath",
13009 got_path_strip_trailing_slashes(repo_path);
13020 cwd = getcwd(NULL, 0);
13022 error = got_error_from_errno("getcwd");
13026 error = got_repo_pack_fds_open(&pack_fds);
13030 if (repo_path == NULL) {
13031 error = got_worktree_open(&worktree, cwd);
13032 if (error && error->code != GOT_ERR_NOT_WORKTREE)
13035 repo_path = strdup(
13036 got_worktree_get_repo_path(worktree));
13037 if (repo_path == NULL) {
13038 error = got_error_from_errno("strdup");
13042 /* Release work tree lock. */
13043 got_worktree_close(worktree);
13048 if (repo_path == NULL) {
13049 repo_path = strdup(cwd);
13050 if (repo_path == NULL)
13051 return got_error_from_errno("strdup");
13054 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
13059 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
13063 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
13067 if (commit_id_str == NULL)
13068 commit_id_str = GOT_REF_HEAD;
13069 error = got_repo_match_object_id(&commit_id, NULL,
13070 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
13074 error = got_object_open_as_commit(&commit, repo, commit_id);
13078 for (i = 0; i < argc; i++) {
13080 error = got_object_id_by_path(&id, repo, commit,
13085 error = got_repo_match_object_id(&id, &label, argv[i],
13086 GOT_OBJ_TYPE_ANY, NULL /* do not resolve tags */,
13089 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
13090 error->code != GOT_ERR_NOT_REF)
13092 error = got_object_id_by_path(&id, repo,
13099 error = got_object_get_type(&obj_type, repo, id);
13103 switch (obj_type) {
13104 case GOT_OBJ_TYPE_BLOB:
13105 error = cat_blob(id, repo, stdout);
13107 case GOT_OBJ_TYPE_TREE:
13108 error = cat_tree(id, repo, stdout);
13110 case GOT_OBJ_TYPE_COMMIT:
13111 error = cat_commit(id, repo, stdout);
13113 case GOT_OBJ_TYPE_TAG:
13114 error = cat_tag(id, repo, stdout);
13117 error = got_error(GOT_ERR_OBJ_TYPE);
13132 got_object_commit_close(commit);
13134 got_worktree_close(worktree);
13136 const struct got_error *close_err = got_repo_close(repo);
13141 const struct got_error *pack_err =
13142 got_repo_pack_fds_close(pack_fds);
13147 got_ref_list_free(&refs);
13154 fprintf(stderr, "usage: %s info [path ...]\n",
13159 static const struct got_error *
13160 print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
13161 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
13162 struct got_object_id *commit_id)
13164 const struct got_error *err = NULL;
13165 char *id_str = NULL;
13167 struct tm mytm, *tm;
13168 struct got_pathlist_head *paths = arg;
13169 struct got_pathlist_entry *pe;
13172 * Clear error indication from any of the path arguments which
13173 * would cause this file index entry to be displayed.
13175 TAILQ_FOREACH(pe, paths, entry) {
13176 if (got_path_cmp(path, pe->path, strlen(path),
13177 pe->path_len) == 0 ||
13178 got_path_is_child(path, pe->path, pe->path_len))
13179 pe->data = NULL; /* no error */
13182 printf(GOT_COMMIT_SEP_STR);
13184 printf("symlink: %s\n", path);
13185 else if (S_ISREG(mode)) {
13186 printf("file: %s\n", path);
13187 printf("mode: %o\n", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
13188 } else if (S_ISDIR(mode))
13189 printf("directory: %s\n", path);
13191 printf("something: %s\n", path);
13193 tm = localtime_r(&mtime, &mytm);
13196 if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) == 0)
13197 return got_error(GOT_ERR_NO_SPACE);
13198 printf("timestamp: %s\n", datebuf);
13201 err = got_object_id_str(&id_str, blob_id);
13204 printf("based on blob: %s\n", id_str);
13208 if (staged_blob_id) {
13209 err = got_object_id_str(&id_str, staged_blob_id);
13212 printf("based on staged blob: %s\n", id_str);
13217 err = got_object_id_str(&id_str, commit_id);
13220 printf("based on commit: %s\n", id_str);
13227 static const struct got_error *
13228 cmd_info(int argc, char *argv[])
13230 const struct got_error *error = NULL;
13231 struct got_worktree *worktree = NULL;
13232 char *cwd = NULL, *id_str = NULL;
13233 struct got_pathlist_head paths;
13234 char *uuidstr = NULL;
13235 int ch, show_files = 0;
13237 TAILQ_INIT(&paths);
13239 while ((ch = getopt(argc, argv, "")) != -1) {
13251 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
13255 cwd = getcwd(NULL, 0);
13257 error = got_error_from_errno("getcwd");
13261 error = got_worktree_open(&worktree, cwd);
13263 if (error->code == GOT_ERR_NOT_WORKTREE)
13264 error = wrap_not_worktree_error(error, "info", cwd);
13269 /* Remove "wpath cpath proc exec sendfd" promises. */
13270 if (pledge("stdio rpath flock unveil", NULL) == -1)
13273 error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
13278 error = get_worktree_paths_from_argv(&paths, argc, argv,
13285 error = got_object_id_str(&id_str,
13286 got_worktree_get_base_commit_id(worktree));
13290 error = got_worktree_get_uuid(&uuidstr, worktree);
13294 printf("work tree: %s\n", got_worktree_get_root_path(worktree));
13295 printf("work tree base commit: %s\n", id_str);
13296 printf("work tree path prefix: %s\n",
13297 got_worktree_get_path_prefix(worktree));
13298 printf("work tree branch reference: %s\n",
13299 got_worktree_get_head_ref_name(worktree));
13300 printf("work tree UUID: %s\n", uuidstr);
13301 printf("repository: %s\n", got_worktree_get_repo_path(worktree));
13304 struct got_pathlist_entry *pe;
13305 TAILQ_FOREACH(pe, &paths, entry) {
13306 if (pe->path_len == 0)
13309 * Assume this path will fail. This will be corrected
13310 * in print_path_info() in case the path does suceeed.
13312 pe->data = (void *)got_error(GOT_ERR_BAD_PATH);
13314 error = got_worktree_path_info(worktree, &paths,
13315 print_path_info, &paths, check_cancelled, NULL);
13318 TAILQ_FOREACH(pe, &paths, entry) {
13319 if (pe->data != NULL) {
13320 const struct got_error *perr;
13323 error = got_error_fmt(perr->code, "%s",
13331 got_worktree_close(worktree);
13332 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);