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 got_pathlist_append(&wanted_branches,
2487 remote->fetch_branches[i], NULL);
2490 if (TAILQ_EMPTY(&wanted_refs)) {
2491 for (i = 0; i < remote->nfetch_refs; i++) {
2492 got_pathlist_append(&wanted_refs,
2493 remote->fetch_refs[i], NULL);
2497 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
2498 &repo_name, remote->fetch_url);
2502 if (strcmp(proto, "git") == 0) {
2504 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2505 "sendfd dns inet unveil", NULL) == -1)
2508 } else if (strcmp(proto, "git+ssh") == 0 ||
2509 strcmp(proto, "ssh") == 0) {
2511 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2512 "sendfd unveil", NULL) == -1)
2515 } else if (strcmp(proto, "http") == 0 ||
2516 strcmp(proto, "git+http") == 0) {
2517 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
2520 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
2524 error = got_dial_apply_unveil(proto);
2528 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
2532 if (verbosity >= 0) {
2533 printf("Connecting to \"%s\" %s://%s%s%s%s%s\n",
2534 remote->name, proto, host,
2535 port ? ":" : "", port ? port : "",
2536 *server_path == '/' ? "" : "/", server_path);
2539 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
2540 server_path, verbosity);
2544 fpa.last_scaled_size[0] = '\0';
2545 fpa.last_p_indexed = -1;
2546 fpa.last_p_resolved = -1;
2547 fpa.verbosity = verbosity;
2549 fpa.create_configs = 0;
2550 fpa.configs_created = 0;
2551 memset(&fpa.config_info, 0, sizeof(fpa.config_info));
2552 error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
2553 remote->mirror_references, fetch_all_branches, &wanted_branches,
2554 &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
2555 fetch_progress, &fpa);
2559 if (list_refs_only) {
2560 error = list_remote_refs(&symrefs, &refs);
2564 if (pack_hash == NULL) {
2566 printf("Already up-to-date\n");
2567 } else if (verbosity >= 0) {
2568 error = got_object_id_str(&id_str, pack_hash);
2571 printf("\nFetched %s.pack\n", id_str);
2576 /* Update references provided with the pack file. */
2577 TAILQ_FOREACH(pe, &refs, entry) {
2578 const char *refname = pe->path;
2579 struct got_object_id *id = pe->data;
2580 struct got_reference *ref;
2581 char *remote_refname;
2583 if (is_wanted_ref(&wanted_refs, refname) &&
2584 !remote->mirror_references) {
2585 error = update_wanted_ref(refname, id,
2586 remote->name, verbosity, repo);
2592 if (remote->mirror_references ||
2593 strncmp("refs/tags/", refname, 10) == 0) {
2594 error = got_ref_open(&ref, repo, refname, 1);
2596 if (error->code != GOT_ERR_NOT_REF)
2598 error = create_ref(refname, id, verbosity,
2603 error = update_ref(ref, id, replace_tags,
2605 unlock_err = got_ref_unlock(ref);
2606 if (unlock_err && error == NULL)
2612 } else if (strncmp("refs/heads/", refname, 11) == 0) {
2613 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2614 remote_name, refname + 11) == -1) {
2615 error = got_error_from_errno("asprintf");
2619 error = got_ref_open(&ref, repo, remote_refname, 1);
2621 if (error->code != GOT_ERR_NOT_REF)
2623 error = create_ref(remote_refname, id,
2628 error = update_ref(ref, id, replace_tags,
2630 unlock_err = got_ref_unlock(ref);
2631 if (unlock_err && error == NULL)
2638 /* Also create a local branch if none exists yet. */
2639 error = got_ref_open(&ref, repo, refname, 1);
2641 if (error->code != GOT_ERR_NOT_REF)
2643 error = create_ref(refname, id, verbosity,
2648 unlock_err = got_ref_unlock(ref);
2649 if (unlock_err && error == NULL)
2656 error = delete_missing_refs(&refs, &symrefs, remote,
2662 if (!remote->mirror_references) {
2663 /* Update remote HEAD reference if the server provided one. */
2664 TAILQ_FOREACH(pe, &symrefs, entry) {
2665 struct got_reference *target_ref;
2666 const char *refname = pe->path;
2667 const char *target = pe->data;
2668 char *remote_refname = NULL, *remote_target = NULL;
2670 if (strcmp(refname, GOT_REF_HEAD) != 0)
2673 if (strncmp("refs/heads/", target, 11) != 0)
2676 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2677 remote->name, refname) == -1) {
2678 error = got_error_from_errno("asprintf");
2681 if (asprintf(&remote_target, "refs/remotes/%s/%s",
2682 remote->name, target + 11) == -1) {
2683 error = got_error_from_errno("asprintf");
2684 free(remote_refname);
2688 error = got_ref_open(&target_ref, repo, remote_target,
2691 free(remote_refname);
2692 free(remote_target);
2693 if (error->code == GOT_ERR_NOT_REF) {
2699 error = update_symref(remote_refname, target_ref,
2701 free(remote_refname);
2702 free(remote_target);
2703 got_ref_close(target_ref);
2710 if (kill(fetchpid, SIGTERM) == -1)
2711 error = got_error_from_errno("kill");
2712 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
2713 error = got_error_from_errno("waitpid");
2715 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
2716 error = got_error_from_errno("close");
2718 const struct got_error *close_err = got_repo_close(repo);
2723 got_worktree_close(worktree);
2725 const struct got_error *pack_err =
2726 got_repo_pack_fds_close(pack_fds);
2730 got_pathlist_free(&refs, GOT_PATHLIST_FREE_ALL);
2731 got_pathlist_free(&symrefs, GOT_PATHLIST_FREE_ALL);
2732 got_pathlist_free(&wanted_branches, GOT_PATHLIST_FREE_NONE);
2733 got_pathlist_free(&wanted_refs, GOT_PATHLIST_FREE_NONE);
2748 usage_checkout(void)
2750 fprintf(stderr, "usage: %s checkout [-Eq] [-b branch] [-c commit] "
2751 "[-p path-prefix] repository-path [work-tree-path]\n",
2757 show_worktree_base_ref_warning(void)
2759 fprintf(stderr, "%s: warning: could not create a reference "
2760 "to the work tree's base commit; the commit could be "
2761 "garbage-collected by Git or 'gotadmin cleanup'; making the "
2762 "repository writable and running 'got update' will prevent this\n",
2766 struct got_checkout_progress_arg {
2767 const char *worktree_path;
2768 int had_base_commit_ref_error;
2772 static const struct got_error *
2773 checkout_progress(void *arg, unsigned char status, const char *path)
2775 struct got_checkout_progress_arg *a = arg;
2777 /* Base commit bump happens silently. */
2778 if (status == GOT_STATUS_BUMP_BASE)
2781 if (status == GOT_STATUS_BASE_REF_ERR) {
2782 a->had_base_commit_ref_error = 1;
2786 while (path[0] == '/')
2789 if (a->verbosity >= 0)
2790 printf("%c %s/%s\n", status, a->worktree_path, path);
2795 static const struct got_error *
2796 check_cancelled(void *arg)
2798 if (sigint_received || sigpipe_received)
2799 return got_error(GOT_ERR_CANCELLED);
2803 static const struct got_error *
2804 check_linear_ancestry(struct got_object_id *commit_id,
2805 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2806 struct got_repository *repo)
2808 const struct got_error *err = NULL;
2809 struct got_object_id *yca_id;
2811 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2812 commit_id, base_commit_id, 1, repo, check_cancelled, NULL);
2817 return got_error(GOT_ERR_ANCESTRY);
2820 * Require a straight line of history between the target commit
2821 * and the work tree's base commit.
2823 * Non-linear situations such as this require a rebase:
2825 * (commit) D F (base_commit)
2833 * 'got update' only handles linear cases:
2834 * Update forwards in time: A (base/yca) - B - C - D (commit)
2835 * Update backwards in time: D (base) - C - B - A (commit/yca)
2837 if (allow_forwards_in_time_only) {
2838 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2839 return got_error(GOT_ERR_ANCESTRY);
2840 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2841 got_object_id_cmp(base_commit_id, yca_id) != 0)
2842 return got_error(GOT_ERR_ANCESTRY);
2848 static const struct got_error *
2849 check_same_branch(struct got_object_id *commit_id,
2850 struct got_reference *head_ref, struct got_object_id *yca_id,
2851 struct got_repository *repo)
2853 const struct got_error *err = NULL;
2854 struct got_commit_graph *graph = NULL;
2855 struct got_object_id *head_commit_id = NULL;
2856 int is_same_branch = 0;
2858 err = got_ref_resolve(&head_commit_id, repo, head_ref);
2862 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
2866 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
2871 err = got_commit_graph_open(&graph, "/", 1);
2875 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2876 check_cancelled, NULL);
2881 struct got_object_id id;
2883 err = got_commit_graph_iter_next(&id, graph, repo,
2884 check_cancelled, NULL);
2886 if (err->code == GOT_ERR_ITER_COMPLETED)
2891 if (yca_id && got_object_id_cmp(&id, yca_id) == 0)
2893 if (got_object_id_cmp(&id, commit_id) == 0) {
2900 got_commit_graph_close(graph);
2901 free(head_commit_id);
2902 if (!err && !is_same_branch)
2903 err = got_error(GOT_ERR_ANCESTRY);
2907 static const struct got_error *
2908 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2910 static char msg[512];
2911 const char *branch_name;
2913 if (got_ref_is_symbolic(ref))
2914 branch_name = got_ref_get_symref_target(ref);
2916 branch_name = got_ref_get_name(ref);
2918 if (strncmp("refs/heads/", branch_name, 11) == 0)
2921 snprintf(msg, sizeof(msg),
2922 "target commit is not contained in branch '%s'; "
2923 "the branch to use must be specified with -b; "
2924 "if necessary a new branch can be created for "
2925 "this commit with 'got branch -c %s BRANCH_NAME'",
2926 branch_name, commit_id_str);
2928 return got_error_msg(GOT_ERR_ANCESTRY, msg);
2931 static const struct got_error *
2932 cmd_checkout(int argc, char *argv[])
2934 const struct got_error *error = NULL;
2935 struct got_repository *repo = NULL;
2936 struct got_reference *head_ref = NULL, *ref = NULL;
2937 struct got_worktree *worktree = NULL;
2938 char *repo_path = NULL;
2939 char *worktree_path = NULL;
2940 const char *path_prefix = "";
2941 const char *branch_name = GOT_REF_HEAD, *refname = NULL;
2942 char *commit_id_str = NULL;
2943 struct got_object_id *commit_id = NULL;
2945 int ch, same_path_prefix, allow_nonempty = 0, verbosity = 0;
2946 struct got_pathlist_head paths;
2947 struct got_checkout_progress_arg cpa;
2948 int *pack_fds = NULL;
2952 while ((ch = getopt(argc, argv, "b:c:Ep:q")) != -1) {
2955 branch_name = optarg;
2958 commit_id_str = strdup(optarg);
2959 if (commit_id_str == NULL)
2960 return got_error_from_errno("strdup");
2966 path_prefix = optarg;
2981 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2982 "unveil", NULL) == -1)
2986 char *base, *dotgit;
2988 repo_path = realpath(argv[0], NULL);
2989 if (repo_path == NULL)
2990 return got_error_from_errno2("realpath", argv[0]);
2991 cwd = getcwd(NULL, 0);
2993 error = got_error_from_errno("getcwd");
3000 error = got_path_basename(&base, path);
3003 dotgit = strstr(base, ".git");
3006 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
3007 error = got_error_from_errno("asprintf");
3012 } else if (argc == 2) {
3013 repo_path = realpath(argv[0], NULL);
3014 if (repo_path == NULL) {
3015 error = got_error_from_errno2("realpath", argv[0]);
3018 worktree_path = realpath(argv[1], NULL);
3019 if (worktree_path == NULL) {
3020 if (errno != ENOENT) {
3021 error = got_error_from_errno2("realpath",
3025 worktree_path = strdup(argv[1]);
3026 if (worktree_path == NULL) {
3027 error = got_error_from_errno("strdup");
3034 got_path_strip_trailing_slashes(repo_path);
3035 got_path_strip_trailing_slashes(worktree_path);
3037 error = got_repo_pack_fds_open(&pack_fds);
3041 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3045 /* Pre-create work tree path for unveil(2) */
3046 error = got_path_mkdir(worktree_path);
3048 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
3049 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
3051 if (!allow_nonempty &&
3052 !got_path_dir_is_empty(worktree_path)) {
3053 error = got_error_path(worktree_path,
3054 GOT_ERR_DIR_NOT_EMPTY);
3059 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
3063 error = got_ref_open(&head_ref, repo, branch_name, 0);
3067 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
3068 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
3071 error = got_worktree_open(&worktree, worktree_path);
3075 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
3079 if (!same_path_prefix) {
3080 error = got_error(GOT_ERR_PATH_PREFIX);
3084 if (commit_id_str) {
3085 struct got_reflist_head refs;
3087 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3091 error = got_repo_match_object_id(&commit_id, NULL,
3092 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3093 got_ref_list_free(&refs);
3096 error = check_linear_ancestry(commit_id,
3097 got_worktree_get_base_commit_id(worktree), 0, repo);
3098 if (error != NULL) {
3099 if (error->code == GOT_ERR_ANCESTRY) {
3100 error = checkout_ancestry_error(
3101 head_ref, commit_id_str);
3105 error = check_same_branch(commit_id, head_ref, NULL, repo);
3107 if (error->code == GOT_ERR_ANCESTRY) {
3108 error = checkout_ancestry_error(
3109 head_ref, commit_id_str);
3113 error = got_worktree_set_base_commit_id(worktree, repo,
3117 /* Expand potentially abbreviated commit ID string. */
3118 free(commit_id_str);
3119 error = got_object_id_str(&commit_id_str, commit_id);
3123 commit_id = got_object_id_dup(
3124 got_worktree_get_base_commit_id(worktree));
3125 if (commit_id == NULL) {
3126 error = got_error_from_errno("got_object_id_dup");
3129 error = got_object_id_str(&commit_id_str, commit_id);
3134 error = got_pathlist_append(&paths, "", NULL);
3137 cpa.worktree_path = worktree_path;
3138 cpa.had_base_commit_ref_error = 0;
3139 cpa.verbosity = verbosity;
3140 error = got_worktree_checkout_files(worktree, &paths, repo,
3141 checkout_progress, &cpa, check_cancelled, NULL);
3145 if (got_ref_is_symbolic(head_ref)) {
3146 error = got_ref_resolve_symbolic(&ref, repo, head_ref);
3149 refname = got_ref_get_name(ref);
3151 refname = got_ref_get_name(head_ref);
3152 printf("Checked out %s: %s\n", refname, commit_id_str);
3153 printf("Now shut up and hack\n");
3154 if (cpa.had_base_commit_ref_error)
3155 show_worktree_base_ref_warning();
3158 const struct got_error *pack_err =
3159 got_repo_pack_fds_close(pack_fds);
3164 got_ref_close(head_ref);
3167 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
3168 free(commit_id_str);
3171 free(worktree_path);
3176 struct got_update_progress_arg {
3188 print_update_progress_stats(struct got_update_progress_arg *upa)
3190 if (!upa->did_something)
3193 if (upa->conflicts > 0)
3194 printf("Files with new merge conflicts: %d\n", upa->conflicts);
3195 if (upa->obstructed > 0)
3196 printf("File paths obstructed by a non-regular file: %d\n",
3198 if (upa->not_updated > 0)
3199 printf("Files not updated because of existing merge "
3200 "conflicts: %d\n", upa->not_updated);
3204 * The meaning of some status codes differs between merge-style operations and
3205 * update operations. For example, the ! status code means "file was missing"
3206 * if changes were merged into the work tree, and "missing file was restored"
3207 * if the work tree was updated. This function should be used by any operation
3208 * which merges changes into the work tree without updating the work tree.
3211 print_merge_progress_stats(struct got_update_progress_arg *upa)
3213 if (!upa->did_something)
3216 if (upa->conflicts > 0)
3217 printf("Files with new merge conflicts: %d\n", upa->conflicts);
3218 if (upa->obstructed > 0)
3219 printf("File paths obstructed by a non-regular file: %d\n",
3221 if (upa->missing > 0)
3222 printf("Files which had incoming changes but could not be "
3223 "found in the work tree: %d\n", upa->missing);
3224 if (upa->not_deleted > 0)
3225 printf("Files not deleted due to differences in deleted "
3226 "content: %d\n", upa->not_deleted);
3227 if (upa->unversioned > 0)
3228 printf("Files not merged because an unversioned file was "
3229 "found in the work tree: %d\n", upa->unversioned);
3235 fprintf(stderr, "usage: %s update [-q] [-b branch] [-c commit] "
3236 "[path ...]\n", getprogname());
3240 static const struct got_error *
3241 update_progress(void *arg, unsigned char status, const char *path)
3243 struct got_update_progress_arg *upa = arg;
3245 if (status == GOT_STATUS_EXISTS ||
3246 status == GOT_STATUS_BASE_REF_ERR)
3249 upa->did_something = 1;
3251 /* Base commit bump happens silently. */
3252 if (status == GOT_STATUS_BUMP_BASE)
3255 if (status == GOT_STATUS_CONFLICT)
3257 if (status == GOT_STATUS_OBSTRUCTED)
3259 if (status == GOT_STATUS_CANNOT_UPDATE)
3261 if (status == GOT_STATUS_MISSING)
3263 if (status == GOT_STATUS_CANNOT_DELETE)
3265 if (status == GOT_STATUS_UNVERSIONED)
3268 while (path[0] == '/')
3270 if (upa->verbosity >= 0)
3271 printf("%c %s\n", status, path);
3276 static const struct got_error *
3277 switch_head_ref(struct got_reference *head_ref,
3278 struct got_object_id *commit_id, struct got_worktree *worktree,
3279 struct got_repository *repo)
3281 const struct got_error *err = NULL;
3283 int ref_has_moved = 0;
3285 /* Trivial case: switching between two different references. */
3286 if (strcmp(got_ref_get_name(head_ref),
3287 got_worktree_get_head_ref_name(worktree)) != 0) {
3288 printf("Switching work tree from %s to %s\n",
3289 got_worktree_get_head_ref_name(worktree),
3290 got_ref_get_name(head_ref));
3291 return got_worktree_set_head_ref(worktree, head_ref);
3294 err = check_linear_ancestry(commit_id,
3295 got_worktree_get_base_commit_id(worktree), 0, repo);
3297 if (err->code != GOT_ERR_ANCESTRY)
3304 /* Switching to a rebased branch with the same reference name. */
3305 err = got_object_id_str(&base_id_str,
3306 got_worktree_get_base_commit_id(worktree));
3309 printf("Reference %s now points at a different branch\n",
3310 got_worktree_get_head_ref_name(worktree));
3311 printf("Switching work tree from %s to %s\n", base_id_str,
3312 got_worktree_get_head_ref_name(worktree));
3316 static const struct got_error *
3317 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
3319 const struct got_error *err;
3322 err = got_worktree_rebase_in_progress(&in_progress, worktree);
3326 return got_error(GOT_ERR_REBASING);
3328 err = got_worktree_histedit_in_progress(&in_progress, worktree);
3332 return got_error(GOT_ERR_HISTEDIT_BUSY);
3337 static const struct got_error *
3338 check_merge_in_progress(struct got_worktree *worktree,
3339 struct got_repository *repo)
3341 const struct got_error *err;
3344 err = got_worktree_merge_in_progress(&in_progress, worktree, repo);
3348 return got_error(GOT_ERR_MERGE_BUSY);
3353 static const struct got_error *
3354 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
3355 char *argv[], struct got_worktree *worktree)
3357 const struct got_error *err = NULL;
3359 struct got_pathlist_entry *new;
3365 return got_error_from_errno("strdup");
3366 return got_pathlist_append(paths, path, NULL);
3369 for (i = 0; i < argc; i++) {
3370 err = got_worktree_resolve_path(&path, worktree, argv[i]);
3373 err = got_pathlist_insert(&new, paths, path, NULL);
3374 if (err || new == NULL /* duplicate */) {
3384 static const struct got_error *
3385 wrap_not_worktree_error(const struct got_error *orig_err,
3386 const char *cmdname, const char *path)
3388 const struct got_error *err;
3389 struct got_repository *repo;
3390 static char msg[512];
3391 int *pack_fds = NULL;
3393 err = got_repo_pack_fds_open(&pack_fds);
3397 err = got_repo_open(&repo, path, NULL, pack_fds);
3401 snprintf(msg, sizeof(msg),
3402 "'got %s' needs a work tree in addition to a git repository\n"
3403 "Work trees can be checked out from this Git repository with "
3405 "The got(1) manual page contains more information.", cmdname);
3406 err = got_error_msg(GOT_ERR_NOT_WORKTREE, msg);
3407 got_repo_close(repo);
3409 const struct got_error *pack_err =
3410 got_repo_pack_fds_close(pack_fds);
3417 static const struct got_error *
3418 cmd_update(int argc, char *argv[])
3420 const struct got_error *error = NULL;
3421 struct got_repository *repo = NULL;
3422 struct got_worktree *worktree = NULL;
3423 char *worktree_path = NULL;
3424 struct got_object_id *commit_id = NULL;
3425 char *commit_id_str = NULL;
3426 const char *branch_name = NULL;
3427 struct got_reference *head_ref = NULL;
3428 struct got_pathlist_head paths;
3429 struct got_pathlist_entry *pe;
3430 int ch, verbosity = 0;
3431 struct got_update_progress_arg upa;
3432 int *pack_fds = NULL;
3436 while ((ch = getopt(argc, argv, "b:c:q")) != -1) {
3439 branch_name = optarg;
3442 commit_id_str = strdup(optarg);
3443 if (commit_id_str == NULL)
3444 return got_error_from_errno("strdup");
3459 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3460 "unveil", NULL) == -1)
3463 worktree_path = getcwd(NULL, 0);
3464 if (worktree_path == NULL) {
3465 error = got_error_from_errno("getcwd");
3469 error = got_repo_pack_fds_open(&pack_fds);
3473 error = got_worktree_open(&worktree, worktree_path);
3475 if (error->code == GOT_ERR_NOT_WORKTREE)
3476 error = wrap_not_worktree_error(error, "update",
3481 error = check_rebase_or_histedit_in_progress(worktree);
3485 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3490 error = apply_unveil(got_repo_get_path(repo), 0,
3491 got_worktree_get_root_path(worktree));
3495 error = check_merge_in_progress(worktree, repo);
3499 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3503 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
3504 got_worktree_get_head_ref_name(worktree), 0);
3507 if (commit_id_str == NULL) {
3508 error = got_ref_resolve(&commit_id, repo, head_ref);
3511 error = got_object_id_str(&commit_id_str, commit_id);
3515 struct got_reflist_head refs;
3517 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3521 error = got_repo_match_object_id(&commit_id, NULL,
3522 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3523 got_ref_list_free(&refs);
3524 free(commit_id_str);
3525 commit_id_str = NULL;
3528 error = got_object_id_str(&commit_id_str, commit_id);
3534 struct got_object_id *head_commit_id;
3535 TAILQ_FOREACH(pe, &paths, entry) {
3536 if (pe->path_len == 0)
3538 error = got_error_msg(GOT_ERR_BAD_PATH,
3539 "switching between branches requires that "
3540 "the entire work tree gets updated");
3543 error = got_ref_resolve(&head_commit_id, repo, head_ref);
3546 error = check_linear_ancestry(commit_id, head_commit_id, 0,
3548 free(head_commit_id);
3551 error = check_same_branch(commit_id, head_ref, NULL, repo);
3554 error = switch_head_ref(head_ref, commit_id, worktree, repo);
3558 error = check_linear_ancestry(commit_id,
3559 got_worktree_get_base_commit_id(worktree), 0, repo);
3560 if (error != NULL) {
3561 if (error->code == GOT_ERR_ANCESTRY)
3562 error = got_error(GOT_ERR_BRANCH_MOVED);
3565 error = check_same_branch(commit_id, head_ref, NULL, repo);
3570 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
3572 error = got_worktree_set_base_commit_id(worktree, repo,
3578 memset(&upa, 0, sizeof(upa));
3579 upa.verbosity = verbosity;
3580 error = got_worktree_checkout_files(worktree, &paths, repo,
3581 update_progress, &upa, check_cancelled, NULL);
3585 if (upa.did_something) {
3586 printf("Updated to %s: %s\n",
3587 got_worktree_get_head_ref_name(worktree), commit_id_str);
3589 printf("Already up-to-date\n");
3591 print_update_progress_stats(&upa);
3594 const struct got_error *pack_err =
3595 got_repo_pack_fds_close(pack_fds);
3599 free(worktree_path);
3600 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
3602 free(commit_id_str);
3606 static const struct got_error *
3607 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
3608 const char *path, int diff_context, int ignore_whitespace,
3609 int force_text_diff, int show_diffstat, struct got_repository *repo,
3612 const struct got_error *err = NULL;
3613 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
3614 FILE *f1 = NULL, *f2 = NULL;
3615 int fd1 = -1, fd2 = -1;
3617 fd1 = got_opentempfd();
3619 return got_error_from_errno("got_opentempfd");
3620 fd2 = got_opentempfd();
3622 err = got_error_from_errno("got_opentempfd");
3627 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192,
3633 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192, fd2);
3637 f1 = got_opentemp();
3639 err = got_error_from_errno("got_opentemp");
3642 f2 = got_opentemp();
3644 err = got_error_from_errno("got_opentemp");
3648 while (path[0] == '/')
3650 err = got_diff_blob(NULL, NULL, blob1, blob2, f1, f2, path, path,
3651 GOT_DIFF_ALGORITHM_PATIENCE, diff_context, ignore_whitespace,
3652 force_text_diff, show_diffstat, NULL, outfile);
3654 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3655 err = got_error_from_errno("close");
3657 got_object_blob_close(blob1);
3658 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3659 err = got_error_from_errno("close");
3660 got_object_blob_close(blob2);
3661 if (f1 && fclose(f1) == EOF && err == NULL)
3662 err = got_error_from_errno("fclose");
3663 if (f2 && fclose(f2) == EOF && err == NULL)
3664 err = got_error_from_errno("fclose");
3668 static const struct got_error *
3669 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
3670 const char *path, int diff_context, int ignore_whitespace,
3671 int force_text_diff, struct got_repository *repo, FILE *outfile)
3673 const struct got_error *err = NULL;
3674 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3675 struct got_diff_blob_output_unidiff_arg arg;
3676 FILE *f1 = NULL, *f2 = NULL;
3677 int fd1 = -1, fd2 = -1;
3680 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3683 fd1 = got_opentempfd();
3685 err = got_error_from_errno("got_opentempfd");
3690 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3694 f1 = got_opentemp();
3696 err = got_error_from_errno("got_opentemp");
3700 f2 = got_opentemp();
3702 err = got_error_from_errno("got_opentemp");
3705 fd2 = got_opentempfd();
3707 err = got_error_from_errno("got_opentempfd");
3710 arg.diff_context = diff_context;
3711 arg.ignore_whitespace = ignore_whitespace;
3712 arg.force_text_diff = force_text_diff;
3713 arg.show_diffstat = 0;
3714 arg.diffstat = NULL;
3715 arg.diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
3716 arg.outfile = outfile;
3719 while (path[0] == '/')
3721 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, path, path, repo,
3722 got_diff_blob_output_unidiff, &arg, 1);
3725 got_object_tree_close(tree1);
3727 got_object_tree_close(tree2);
3728 if (f1 && fclose(f1) == EOF && err == NULL)
3729 err = got_error_from_errno("fclose");
3730 if (f2 && fclose(f2) == EOF && err == NULL)
3731 err = got_error_from_errno("fclose");
3732 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3733 err = got_error_from_errno("close");
3734 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3735 err = got_error_from_errno("close");
3739 static const struct got_error *
3740 get_changed_paths(struct got_pathlist_head *paths,
3741 struct got_commit_object *commit, struct got_repository *repo,
3742 struct got_diffstat_cb_arg *dsa)
3744 const struct got_error *err = NULL;
3745 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3746 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3747 struct got_object_qid *qid;
3748 got_diff_blob_cb cb = got_diff_tree_collect_changed_paths;
3749 FILE *f1 = NULL, *f2 = NULL;
3750 int fd1 = -1, fd2 = -1;
3753 cb = got_diff_tree_compute_diffstat;
3755 f1 = got_opentemp();
3757 err = got_error_from_errno("got_opentemp");
3760 f2 = got_opentemp();
3762 err = got_error_from_errno("got_opentemp");
3765 fd1 = got_opentempfd();
3767 err = got_error_from_errno("got_opentempfd");
3770 fd2 = got_opentempfd();
3772 err = got_error_from_errno("got_opentempfd");
3777 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3779 struct got_commit_object *pcommit;
3780 err = got_object_open_as_commit(&pcommit, repo,
3785 tree_id1 = got_object_id_dup(
3786 got_object_commit_get_tree_id(pcommit));
3787 if (tree_id1 == NULL) {
3788 got_object_commit_close(pcommit);
3789 return got_error_from_errno("got_object_id_dup");
3791 got_object_commit_close(pcommit);
3796 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3801 tree_id2 = got_object_commit_get_tree_id(commit);
3802 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3806 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3807 cb, dsa ? (void *)dsa : paths, dsa ? 1 : 0);
3810 got_object_tree_close(tree1);
3812 got_object_tree_close(tree2);
3813 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3814 err = got_error_from_errno("close");
3815 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3816 err = got_error_from_errno("close");
3817 if (f1 && fclose(f1) == EOF && err == NULL)
3818 err = got_error_from_errno("fclose");
3819 if (f2 && fclose(f2) == EOF && err == NULL)
3820 err = got_error_from_errno("fclose");
3825 static const struct got_error *
3826 print_patch(struct got_commit_object *commit, struct got_object_id *id,
3827 const char *path, int diff_context, struct got_repository *repo,
3830 const struct got_error *err = NULL;
3831 struct got_commit_object *pcommit = NULL;
3832 char *id_str1 = NULL, *id_str2 = NULL;
3833 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
3834 struct got_object_qid *qid;
3836 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3838 err = got_object_open_as_commit(&pcommit, repo,
3842 err = got_object_id_str(&id_str1, &qid->id);
3847 err = got_object_id_str(&id_str2, id);
3851 if (path && path[0] != '\0') {
3853 err = got_object_id_by_path(&obj_id2, repo, commit, path);
3857 err = got_object_id_by_path(&obj_id1, repo,
3860 if (err->code != GOT_ERR_NO_TREE_ENTRY) {
3866 err = got_object_get_type(&obj_type, repo, obj_id2);
3872 "diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3873 fprintf(outfile, "commit - %s\n",
3874 id_str1 ? id_str1 : "/dev/null");
3875 fprintf(outfile, "commit + %s\n", id_str2);
3877 case GOT_OBJ_TYPE_BLOB:
3878 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
3879 0, 0, 0, repo, outfile);
3881 case GOT_OBJ_TYPE_TREE:
3882 err = diff_trees(obj_id1, obj_id2, path, diff_context,
3883 0, 0, repo, outfile);
3886 err = got_error(GOT_ERR_OBJ_TYPE);
3892 obj_id2 = got_object_commit_get_tree_id(commit);
3894 obj_id1 = got_object_commit_get_tree_id(pcommit);
3896 "diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3897 fprintf(outfile, "commit - %s\n",
3898 id_str1 ? id_str1 : "/dev/null");
3899 fprintf(outfile, "commit + %s\n", id_str2);
3900 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, 0,
3907 got_object_commit_close(pcommit);
3912 get_datestr(time_t *time, char *datebuf)
3914 struct tm mytm, *tm;
3917 tm = gmtime_r(time, &mytm);
3920 s = asctime_r(tm, datebuf);
3923 p = strchr(s, '\n');
3929 static const struct got_error *
3930 match_commit(int *have_match, struct got_object_id *id,
3931 struct got_commit_object *commit, regex_t *regex)
3933 const struct got_error *err = NULL;
3934 regmatch_t regmatch;
3935 char *id_str = NULL, *logmsg = NULL;
3939 err = got_object_id_str(&id_str, id);
3943 err = got_object_commit_get_logmsg(&logmsg, commit);
3947 if (regexec(regex, got_object_commit_get_author(commit), 1,
3948 ®match, 0) == 0 ||
3949 regexec(regex, got_object_commit_get_committer(commit), 1,
3950 ®match, 0) == 0 ||
3951 regexec(regex, id_str, 1, ®match, 0) == 0 ||
3952 regexec(regex, logmsg, 1, ®match, 0) == 0)
3961 match_changed_paths(int *have_match, struct got_pathlist_head *changed_paths,
3964 regmatch_t regmatch;
3965 struct got_pathlist_entry *pe;
3969 TAILQ_FOREACH(pe, changed_paths, entry) {
3970 if (regexec(regex, pe->path, 1, ®match, 0) == 0) {
3977 static const struct got_error *
3978 match_patch(int *have_match, struct got_commit_object *commit,
3979 struct got_object_id *id, const char *path, int diff_context,
3980 struct got_repository *repo, regex_t *regex, FILE *f)
3982 const struct got_error *err = NULL;
3984 size_t linesize = 0;
3985 regmatch_t regmatch;
3989 err = got_opentemp_truncate(f);
3993 err = print_patch(commit, id, path, diff_context, repo, f);
3997 if (fseeko(f, 0L, SEEK_SET) == -1) {
3998 err = got_error_from_errno("fseeko");
4002 while (getline(&line, &linesize, f) != -1) {
4003 if (regexec(regex, line, 1, ®match, 0) == 0) {
4013 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
4015 static const struct got_error*
4016 build_refs_str(char **refs_str, struct got_reflist_head *refs,
4017 struct got_object_id *id, struct got_repository *repo,
4020 static const struct got_error *err = NULL;
4021 struct got_reflist_entry *re;
4027 TAILQ_FOREACH(re, refs, entry) {
4028 struct got_tag_object *tag = NULL;
4029 struct got_object_id *ref_id;
4032 name = got_ref_get_name(re->ref);
4033 if (strcmp(name, GOT_REF_HEAD) == 0)
4035 if (strncmp(name, "refs/", 5) == 0)
4037 if (strncmp(name, "got/", 4) == 0)
4039 if (strncmp(name, "heads/", 6) == 0)
4041 if (strncmp(name, "remotes/", 8) == 0) {
4045 s = strstr(name, "/" GOT_REF_HEAD);
4046 if (s != NULL && s[strlen(s)] == '\0')
4049 err = got_ref_resolve(&ref_id, repo, re->ref);
4052 if (strncmp(name, "tags/", 5) == 0) {
4053 err = got_object_open_as_tag(&tag, repo, ref_id);
4055 if (err->code != GOT_ERR_OBJ_TYPE) {
4059 /* Ref points at something other than a tag. */
4064 cmp = got_object_id_cmp(tag ?
4065 got_object_tag_get_object_id(tag) : ref_id, id);
4068 got_object_tag_close(tag);
4072 if (asprintf(refs_str, "%s%s%s", s ? s : "",
4073 s ? ", " : "", name) == -1) {
4074 err = got_error_from_errno("asprintf");
4085 static const struct got_error *
4086 print_commit_oneline(struct got_commit_object *commit, struct got_object_id *id,
4087 struct got_repository *repo, struct got_reflist_object_id_map *refs_idmap)
4089 const struct got_error *err = NULL;
4090 char *ref_str = NULL, *id_str = NULL, *logmsg0 = NULL;
4091 char *comma, *s, *nl;
4092 struct got_reflist_head *refs;
4093 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
4095 time_t committer_time;
4097 refs = got_reflist_object_id_map_lookup(refs_idmap, id);
4099 err = build_refs_str(&ref_str, refs, id, repo, 1);
4103 /* Display the first matching ref only. */
4104 if (ref_str && (comma = strchr(ref_str, ',')) != NULL)
4108 if (ref_str == NULL) {
4109 err = got_object_id_str(&id_str, id);
4114 committer_time = got_object_commit_get_committer_time(commit);
4115 if (gmtime_r(&committer_time, &tm) == NULL) {
4116 err = got_error_from_errno("gmtime_r");
4119 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0) {
4120 err = got_error(GOT_ERR_NO_SPACE);
4124 err = got_object_commit_get_logmsg(&logmsg0, commit);
4129 while (isspace((unsigned char)s[0]))
4132 nl = strchr(s, '\n');
4138 printf("%s%-7s %s\n", datebuf, ref_str, s);
4140 printf("%s%.7s %s\n", datebuf, id_str, s);
4142 if (fflush(stdout) != 0 && err == NULL)
4143 err = got_error_from_errno("fflush");
4151 static const struct got_error *
4152 print_diffstat(struct got_diffstat_cb_arg *dsa, struct got_pathlist_head *paths,
4155 struct got_pathlist_entry *pe;
4158 printf("%s\n", header);
4160 TAILQ_FOREACH(pe, paths, entry) {
4161 struct got_diff_changed_path *cp = pe->data;
4162 int pad = dsa->max_path_len - pe->path_len + 1;
4164 printf(" %c %s%*c | %*d+ %*d-\n", cp->status, pe->path, pad,
4165 ' ', dsa->add_cols + 1, cp->add, dsa->rm_cols + 1, cp->rm);
4167 printf("\n%d file%s changed, %d insertions(+), %d deletions(-)\n\n",
4168 dsa->nfiles, dsa->nfiles > 1 ? "s" : "", dsa->ins, dsa->del);
4170 if (fflush(stdout) != 0)
4171 return got_error_from_errno("fflush");
4176 static const struct got_error *
4177 print_commit(struct got_commit_object *commit, struct got_object_id *id,
4178 struct got_repository *repo, const char *path,
4179 struct got_pathlist_head *changed_paths, struct got_diffstat_cb_arg *dsa,
4180 int show_patch, int diff_context,
4181 struct got_reflist_object_id_map *refs_idmap, const char *custom_refs_str)
4183 const struct got_error *err = NULL;
4184 char *id_str, *datestr, *logmsg0, *logmsg, *line;
4186 time_t committer_time;
4187 const char *author, *committer;
4188 char *refs_str = NULL;
4190 err = got_object_id_str(&id_str, id);
4194 if (custom_refs_str == NULL) {
4195 struct got_reflist_head *refs;
4196 refs = got_reflist_object_id_map_lookup(refs_idmap, id);
4198 err = build_refs_str(&refs_str, refs, id, repo, 0);
4204 printf(GOT_COMMIT_SEP_STR);
4205 if (custom_refs_str)
4206 printf("commit %s (%s)\n", id_str, custom_refs_str);
4208 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4209 refs_str ? refs_str : "", refs_str ? ")" : "");
4214 printf("from: %s\n", got_object_commit_get_author(commit));
4215 author = got_object_commit_get_author(commit);
4216 committer = got_object_commit_get_committer(commit);
4217 if (strcmp(author, committer) != 0)
4218 printf("via: %s\n", committer);
4219 committer_time = got_object_commit_get_committer_time(commit);
4220 datestr = get_datestr(&committer_time, datebuf);
4222 printf("date: %s UTC\n", datestr);
4223 if (got_object_commit_get_nparents(commit) > 1) {
4224 const struct got_object_id_queue *parent_ids;
4225 struct got_object_qid *qid;
4227 parent_ids = got_object_commit_get_parent_ids(commit);
4228 STAILQ_FOREACH(qid, parent_ids, entry) {
4229 err = got_object_id_str(&id_str, &qid->id);
4232 printf("parent %d: %s\n", n++, id_str);
4238 err = got_object_commit_get_logmsg(&logmsg0, commit);
4244 line = strsep(&logmsg, "\n");
4246 printf(" %s\n", line);
4250 if (dsa && changed_paths) {
4251 err = print_diffstat(dsa, changed_paths, NULL);
4254 } else if (changed_paths) {
4255 struct got_pathlist_entry *pe;
4257 TAILQ_FOREACH(pe, changed_paths, entry) {
4258 struct got_diff_changed_path *cp = pe->data;
4260 printf(" %c %s\n", cp->status, pe->path);
4265 err = print_patch(commit, id, path, diff_context, repo, stdout);
4270 if (fflush(stdout) != 0 && err == NULL)
4271 err = got_error_from_errno("fflush");
4278 static const struct got_error *
4279 print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
4280 struct got_repository *repo, const char *path, int show_changed_paths,
4281 int show_diffstat, int show_patch, const char *search_pattern,
4282 int diff_context, int limit, int log_branches, int reverse_display_order,
4283 struct got_reflist_object_id_map *refs_idmap, int one_line,
4286 const struct got_error *err;
4287 struct got_commit_graph *graph;
4290 struct got_object_id_queue reversed_commits;
4291 struct got_object_qid *qid;
4292 struct got_commit_object *commit;
4293 struct got_pathlist_head changed_paths;
4295 STAILQ_INIT(&reversed_commits);
4296 TAILQ_INIT(&changed_paths);
4298 if (search_pattern && regcomp(®ex, search_pattern,
4299 REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
4300 return got_error_msg(GOT_ERR_REGEX, search_pattern);
4302 err = got_commit_graph_open(&graph, path, !log_branches);
4305 err = got_commit_graph_iter_start(graph, root_id, repo,
4306 check_cancelled, NULL);
4310 struct got_object_id id;
4311 struct got_diffstat_cb_arg dsa = { 0, 0, 0, 0, 0, 0,
4312 &changed_paths, 0, 0, GOT_DIFF_ALGORITHM_PATIENCE };
4314 if (sigint_received || sigpipe_received)
4317 err = got_commit_graph_iter_next(&id, graph, repo,
4318 check_cancelled, NULL);
4320 if (err->code == GOT_ERR_ITER_COMPLETED)
4325 err = got_object_open_as_commit(&commit, repo, &id);
4329 if ((show_changed_paths || show_diffstat) &&
4330 !reverse_display_order) {
4331 err = get_changed_paths(&changed_paths, commit, repo,
4332 show_diffstat ? &dsa : NULL);
4337 if (search_pattern) {
4338 err = match_commit(&have_match, &id, commit, ®ex);
4340 got_object_commit_close(commit);
4343 if (have_match == 0 && show_changed_paths)
4344 match_changed_paths(&have_match,
4345 &changed_paths, ®ex);
4346 if (have_match == 0 && show_patch) {
4347 err = match_patch(&have_match, commit, &id,
4348 path, diff_context, repo, ®ex,
4353 if (have_match == 0) {
4354 got_object_commit_close(commit);
4355 got_pathlist_free(&changed_paths,
4356 GOT_PATHLIST_FREE_ALL);
4361 if (reverse_display_order) {
4362 err = got_object_qid_alloc(&qid, &id);
4365 STAILQ_INSERT_HEAD(&reversed_commits, qid, entry);
4366 got_object_commit_close(commit);
4369 err = print_commit_oneline(commit, &id,
4372 err = print_commit(commit, &id, repo, path,
4373 (show_changed_paths || show_diffstat) ?
4374 &changed_paths : NULL,
4375 show_diffstat ? &dsa : NULL, show_patch,
4376 diff_context, refs_idmap, NULL);
4377 got_object_commit_close(commit);
4381 if ((limit && --limit == 0) ||
4382 (end_id && got_object_id_cmp(&id, end_id) == 0))
4385 got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
4387 if (reverse_display_order) {
4388 STAILQ_FOREACH(qid, &reversed_commits, entry) {
4389 struct got_diffstat_cb_arg dsa = { 0, 0, 0, 0, 0, 0,
4390 &changed_paths, 0, 0, GOT_DIFF_ALGORITHM_PATIENCE };
4392 err = got_object_open_as_commit(&commit, repo,
4396 if (show_changed_paths || show_diffstat) {
4397 err = get_changed_paths(&changed_paths,
4398 commit, repo, show_diffstat ? &dsa : NULL);
4403 err = print_commit_oneline(commit, &qid->id,
4406 err = print_commit(commit, &qid->id, repo, path,
4407 (show_changed_paths || show_diffstat) ?
4408 &changed_paths : NULL,
4409 show_diffstat ? &dsa : NULL, show_patch,
4410 diff_context, refs_idmap, NULL);
4411 got_object_commit_close(commit);
4414 got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
4418 while (!STAILQ_EMPTY(&reversed_commits)) {
4419 qid = STAILQ_FIRST(&reversed_commits);
4420 STAILQ_REMOVE_HEAD(&reversed_commits, entry);
4421 got_object_qid_free(qid);
4423 got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
4426 got_commit_graph_close(graph);
4433 fprintf(stderr, "usage: %s log [-bdPpRs] [-C number] [-c commit] "
4434 "[-l N] [-r repository-path] [-S search-pattern] [-x commit] "
4435 "[path]\n", getprogname());
4440 get_default_log_limit(void)
4442 const char *got_default_log_limit;
4446 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
4447 if (got_default_log_limit == NULL)
4449 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
4455 static const struct got_error *
4456 cmd_log(int argc, char *argv[])
4458 const struct got_error *error;
4459 struct got_repository *repo = NULL;
4460 struct got_worktree *worktree = NULL;
4461 struct got_object_id *start_id = NULL, *end_id = NULL;
4462 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
4463 const char *start_commit = NULL, *end_commit = NULL;
4464 const char *search_pattern = NULL;
4465 int diff_context = -1, ch;
4466 int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
4467 int show_diffstat = 0, reverse_display_order = 0, one_line = 0;
4469 struct got_reflist_head refs;
4470 struct got_reflist_object_id_map *refs_idmap = NULL;
4471 FILE *tmpfile = NULL;
4472 int *pack_fds = NULL;
4477 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4483 limit = get_default_log_limit();
4485 while ((ch = getopt(argc, argv, "bC:c:dl:PpRr:S:sx:")) != -1) {
4491 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4494 errx(1, "number of context lines is %s: %s",
4498 start_commit = optarg;
4504 limit = strtonum(optarg, 0, INT_MAX, &errstr);
4506 errx(1, "number of commits is %s: %s",
4510 show_changed_paths = 1;
4516 reverse_display_order = 1;
4519 repo_path = realpath(optarg, NULL);
4520 if (repo_path == NULL)
4521 return got_error_from_errno2("realpath",
4523 got_path_strip_trailing_slashes(repo_path);
4526 search_pattern = optarg;
4532 end_commit = optarg;
4543 if (diff_context == -1)
4545 else if (!show_patch)
4546 errx(1, "-C requires -p");
4548 if (one_line && (show_patch || show_changed_paths || show_diffstat))
4549 errx(1, "cannot use -s with -d, -p or -P");
4551 cwd = getcwd(NULL, 0);
4553 error = got_error_from_errno("getcwd");
4557 error = got_repo_pack_fds_open(&pack_fds);
4561 if (repo_path == NULL) {
4562 error = got_worktree_open(&worktree, cwd);
4563 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4570 error = got_worktree_resolve_path(&path, worktree,
4575 path = strdup(argv[0]);
4577 error = got_error_from_errno("strdup");
4581 } else if (argc != 0)
4584 if (repo_path == NULL) {
4585 repo_path = worktree ?
4586 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
4588 if (repo_path == NULL) {
4589 error = got_error_from_errno("strdup");
4593 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4597 error = apply_unveil(got_repo_get_path(repo), 1,
4598 worktree ? got_worktree_get_root_path(worktree) : NULL);
4602 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4606 error = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
4610 if (start_commit == NULL) {
4611 struct got_reference *head_ref;
4612 struct got_commit_object *commit = NULL;
4613 error = got_ref_open(&head_ref, repo,
4614 worktree ? got_worktree_get_head_ref_name(worktree)
4618 error = got_ref_resolve(&start_id, repo, head_ref);
4619 got_ref_close(head_ref);
4622 error = got_object_open_as_commit(&commit, repo,
4626 got_object_commit_close(commit);
4628 error = got_repo_match_object_id(&start_id, NULL,
4629 start_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4633 if (end_commit != NULL) {
4634 error = got_repo_match_object_id(&end_id, NULL,
4635 end_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4642 * If a path was specified on the command line it was resolved
4643 * to a path in the work tree above. Prepend the work tree's
4644 * path prefix to obtain the corresponding in-repository path.
4648 prefix = got_worktree_get_path_prefix(worktree);
4649 if (asprintf(&in_repo_path, "%s%s%s", prefix,
4650 (path[0] != '\0') ? "/" : "", path) == -1) {
4651 error = got_error_from_errno("asprintf");
4656 error = got_repo_map_path(&in_repo_path, repo,
4662 path = in_repo_path;
4666 /* Release work tree lock. */
4667 got_worktree_close(worktree);
4671 if (search_pattern && show_patch) {
4672 tmpfile = got_opentemp();
4673 if (tmpfile == NULL) {
4674 error = got_error_from_errno("got_opentemp");
4679 error = print_commits(start_id, end_id, repo, path ? path : "",
4680 show_changed_paths, show_diffstat, show_patch, search_pattern,
4681 diff_context, limit, log_branches, reverse_display_order,
4682 refs_idmap, one_line, tmpfile);
4688 got_worktree_close(worktree);
4690 const struct got_error *close_err = got_repo_close(repo);
4695 const struct got_error *pack_err =
4696 got_repo_pack_fds_close(pack_fds);
4701 got_reflist_object_id_map_free(refs_idmap);
4702 if (tmpfile && fclose(tmpfile) == EOF && error == NULL)
4703 error = got_error_from_errno("fclose");
4704 got_ref_list_free(&refs);
4711 fprintf(stderr, "usage: %s diff [-adPsw] [-C number] [-c commit] "
4712 "[-r repository-path] [object1 object2 | path ...]\n",
4717 struct print_diff_arg {
4718 struct got_repository *repo;
4719 struct got_worktree *worktree;
4720 struct got_diffstat_cb_arg *diffstat;
4725 enum got_diff_algorithm diff_algo;
4726 int ignore_whitespace;
4727 int force_text_diff;
4735 * Create a file which contains the target path of a symlink so we can feed
4736 * it as content to the diff engine.
4738 static const struct got_error *
4739 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
4740 const char *abspath)
4742 const struct got_error *err = NULL;
4743 char target_path[PATH_MAX];
4744 ssize_t target_len, outlen;
4749 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
4750 if (target_len == -1)
4751 return got_error_from_errno2("readlinkat", abspath);
4753 target_len = readlink(abspath, target_path, PATH_MAX);
4754 if (target_len == -1)
4755 return got_error_from_errno2("readlink", abspath);
4758 *fd = got_opentempfd();
4760 return got_error_from_errno("got_opentempfd");
4762 outlen = write(*fd, target_path, target_len);
4764 err = got_error_from_errno("got_opentempfd");
4768 if (lseek(*fd, 0, SEEK_SET) == -1) {
4769 err = got_error_from_errno2("lseek", abspath);
4780 static const struct got_error *
4781 print_diff(void *arg, unsigned char status, unsigned char staged_status,
4782 const char *path, struct got_object_id *blob_id,
4783 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4784 int dirfd, const char *de_name)
4786 struct print_diff_arg *a = arg;
4787 const struct got_error *err = NULL;
4788 struct got_blob_object *blob1 = NULL;
4789 int fd = -1, fd1 = -1, fd2 = -1;
4791 char *abspath = NULL, *label1 = NULL;
4796 memset(&sb, 0, sizeof(sb));
4798 if (a->diff_staged) {
4799 if (staged_status != GOT_STATUS_MODIFY &&
4800 staged_status != GOT_STATUS_ADD &&
4801 staged_status != GOT_STATUS_DELETE)
4804 if (staged_status == GOT_STATUS_DELETE)
4806 if (status == GOT_STATUS_NONEXISTENT)
4807 return got_error_set_errno(ENOENT, path);
4808 if (status != GOT_STATUS_MODIFY &&
4809 status != GOT_STATUS_ADD &&
4810 status != GOT_STATUS_DELETE &&
4811 status != GOT_STATUS_CONFLICT)
4815 err = got_opentemp_truncate(a->f1);
4817 return got_error_from_errno("got_opentemp_truncate");
4818 err = got_opentemp_truncate(a->f2);
4820 return got_error_from_errno("got_opentemp_truncate");
4822 if (!a->header_shown) {
4823 if (fprintf(a->outfile, "diff %s%s\n",
4824 a->diff_staged ? "-s " : "",
4825 got_worktree_get_root_path(a->worktree)) < 0) {
4826 err = got_error_from_errno("fprintf");
4829 if (fprintf(a->outfile, "commit - %s\n", a->id_str) < 0) {
4830 err = got_error_from_errno("fprintf");
4833 if (fprintf(a->outfile, "path + %s%s\n",
4834 got_worktree_get_root_path(a->worktree),
4835 a->diff_staged ? " (staged changes)" : "") < 0) {
4836 err = got_error_from_errno("fprintf");
4839 a->header_shown = 1;
4842 if (a->diff_staged) {
4843 const char *label1 = NULL, *label2 = NULL;
4844 switch (staged_status) {
4845 case GOT_STATUS_MODIFY:
4849 case GOT_STATUS_ADD:
4852 case GOT_STATUS_DELETE:
4856 return got_error(GOT_ERR_FILE_STATUS);
4858 fd1 = got_opentempfd();
4860 err = got_error_from_errno("got_opentempfd");
4863 fd2 = got_opentempfd();
4865 err = got_error_from_errno("got_opentempfd");
4868 err = got_diff_objects_as_blobs(NULL, NULL, a->f1, a->f2,
4869 fd1, fd2, blob_id, staged_blob_id, label1, label2,
4870 a->diff_algo, a->diff_context, a->ignore_whitespace,
4871 a->force_text_diff, a->show_diffstat, a->diffstat, a->repo,
4876 fd1 = got_opentempfd();
4878 err = got_error_from_errno("got_opentempfd");
4882 if (staged_status == GOT_STATUS_ADD ||
4883 staged_status == GOT_STATUS_MODIFY) {
4885 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
4889 err = got_object_id_str(&id_str, staged_blob_id);
4892 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
4893 err = got_error_from_errno("asprintf");
4898 } else if (status != GOT_STATUS_ADD) {
4899 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192,
4905 if (status != GOT_STATUS_DELETE) {
4906 if (asprintf(&abspath, "%s/%s",
4907 got_worktree_get_root_path(a->worktree), path) == -1) {
4908 err = got_error_from_errno("asprintf");
4913 fd = openat(dirfd, de_name,
4914 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4916 if (!got_err_open_nofollow_on_symlink()) {
4917 err = got_error_from_errno2("openat",
4921 err = get_symlink_target_file(&fd, dirfd,
4927 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4929 if (!got_err_open_nofollow_on_symlink()) {
4930 err = got_error_from_errno2("open",
4934 err = get_symlink_target_file(&fd, dirfd,
4940 if (fstatat(fd, abspath, &sb, AT_SYMLINK_NOFOLLOW) == -1) {
4941 err = got_error_from_errno2("fstatat", abspath);
4944 f2 = fdopen(fd, "r");
4946 err = got_error_from_errno2("fdopen", abspath);
4954 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
4960 err = got_diff_blob_file(blob1, a->f1, size1, label1, f2 ? f2 : a->f2,
4961 f2_exists, &sb, path, GOT_DIFF_ALGORITHM_PATIENCE, a->diff_context,
4962 a->ignore_whitespace, a->force_text_diff, a->show_diffstat,
4963 a->diffstat, a->outfile);
4965 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
4966 err = got_error_from_errno("close");
4967 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4968 err = got_error_from_errno("close");
4970 got_object_blob_close(blob1);
4971 if (fd != -1 && close(fd) == -1 && err == NULL)
4972 err = got_error_from_errno("close");
4973 if (f2 && fclose(f2) == EOF && err == NULL)
4974 err = got_error_from_errno("fclose");
4979 static const struct got_error *
4985 if (fseeko(f, 0L, SEEK_SET) == -1)
4986 return got_error_from_errno("fseek");
4989 r = fread(buf, 1, sizeof(buf), f);
4992 return got_error_from_errno("fread");
4996 if (fwrite(buf, 1, r, stdout) != r)
4997 return got_ferror(stdout, GOT_ERR_IO);
5003 static const struct got_error *
5004 cmd_diff(int argc, char *argv[])
5006 const struct got_error *error;
5007 struct got_repository *repo = NULL;
5008 struct got_worktree *worktree = NULL;
5009 char *cwd = NULL, *repo_path = NULL;
5010 const char *commit_args[2] = { NULL, NULL };
5011 int ncommit_args = 0;
5012 struct got_object_id *ids[2] = { NULL, NULL };
5013 char *labels[2] = { NULL, NULL };
5014 int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
5015 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch, i;
5016 int force_text_diff = 0, force_path = 0, rflag = 0, show_diffstat = 0;
5018 struct got_reflist_head refs;
5019 struct got_pathlist_head diffstat_paths, paths;
5020 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
5021 int fd1 = -1, fd2 = -1;
5022 int *pack_fds = NULL;
5023 struct got_diffstat_cb_arg dsa;
5025 memset(&dsa, 0, sizeof(dsa));
5029 TAILQ_INIT(&diffstat_paths);
5032 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5037 while ((ch = getopt(argc, argv, "aC:c:dPr:sw")) != -1) {
5040 force_text_diff = 1;
5043 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
5046 errx(1, "number of context lines is %s: %s",
5050 if (ncommit_args >= 2)
5051 errx(1, "too many -c options used");
5052 commit_args[ncommit_args++] = optarg;
5061 repo_path = realpath(optarg, NULL);
5062 if (repo_path == NULL)
5063 return got_error_from_errno2("realpath",
5065 got_path_strip_trailing_slashes(repo_path);
5072 ignore_whitespace = 1;
5083 cwd = getcwd(NULL, 0);
5085 error = got_error_from_errno("getcwd");
5089 error = got_repo_pack_fds_open(&pack_fds);
5093 if (repo_path == NULL) {
5094 error = got_worktree_open(&worktree, cwd);
5095 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5101 strdup(got_worktree_get_repo_path(worktree));
5102 if (repo_path == NULL) {
5103 error = got_error_from_errno("strdup");
5107 repo_path = strdup(cwd);
5108 if (repo_path == NULL) {
5109 error = got_error_from_errno("strdup");
5115 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5120 if (show_diffstat) {
5121 dsa.paths = &diffstat_paths;
5122 dsa.force_text = force_text_diff;
5123 dsa.ignore_ws = ignore_whitespace;
5124 dsa.diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
5127 if (rflag || worktree == NULL || ncommit_args > 0) {
5129 error = got_error_msg(GOT_ERR_NOT_IMPL,
5130 "-P option can only be used when diffing "
5135 error = got_error_msg(GOT_ERR_NOT_IMPL,
5136 "-s option can only be used when diffing "
5142 error = apply_unveil(got_repo_get_path(repo), 1,
5143 worktree ? got_worktree_get_root_path(worktree) : NULL);
5147 if ((!force_path && argc == 2) || ncommit_args > 0) {
5148 int obj_type = (ncommit_args > 0 ?
5149 GOT_OBJ_TYPE_COMMIT : GOT_OBJ_TYPE_ANY);
5150 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5154 for (i = 0; i < (ncommit_args > 0 ? ncommit_args : argc); i++) {
5156 if (ncommit_args > 0)
5157 arg = commit_args[i];
5160 error = got_repo_match_object_id(&ids[i], &labels[i],
5161 arg, obj_type, &refs, repo);
5163 if (error->code != GOT_ERR_NOT_REF &&
5164 error->code != GOT_ERR_NO_OBJ)
5166 if (ncommit_args > 0)
5174 f1 = got_opentemp();
5176 error = got_error_from_errno("got_opentemp");
5180 f2 = got_opentemp();
5182 error = got_error_from_errno("got_opentemp");
5186 outfile = got_opentemp();
5187 if (outfile == NULL) {
5188 error = got_error_from_errno("got_opentemp");
5192 if (ncommit_args == 0 && (ids[0] == NULL || ids[1] == NULL)) {
5193 struct print_diff_arg arg;
5196 if (worktree == NULL) {
5197 if (argc == 2 && ids[0] == NULL) {
5198 error = got_error_path(argv[0], GOT_ERR_NO_OBJ);
5200 } else if (argc == 2 && ids[1] == NULL) {
5201 error = got_error_path(argv[1], GOT_ERR_NO_OBJ);
5203 } else if (argc > 0) {
5204 error = got_error_fmt(GOT_ERR_NOT_WORKTREE,
5205 "%s", "specified paths cannot be resolved");
5208 error = got_error(GOT_ERR_NOT_WORKTREE);
5213 error = get_worktree_paths_from_argv(&paths, argc, argv,
5218 error = got_object_id_str(&id_str,
5219 got_worktree_get_base_commit_id(worktree));
5223 arg.worktree = worktree;
5224 arg.diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
5225 arg.diff_context = diff_context;
5226 arg.id_str = id_str;
5227 arg.header_shown = 0;
5228 arg.diff_staged = diff_staged;
5229 arg.ignore_whitespace = ignore_whitespace;
5230 arg.force_text_diff = force_text_diff;
5231 arg.show_diffstat = show_diffstat;
5232 arg.diffstat = &dsa;
5235 arg.outfile = outfile;
5237 error = got_worktree_status(worktree, &paths, repo, 0,
5238 print_diff, &arg, check_cancelled, NULL);
5243 if (show_diffstat && dsa.nfiles > 0) {
5246 if (asprintf(&header, "diffstat %s%s",
5247 diff_staged ? "-s " : "",
5248 got_worktree_get_root_path(worktree)) == -1)
5251 error = print_diffstat(&dsa, &diffstat_paths, header);
5257 error = printfile(outfile);
5261 if (ncommit_args == 1) {
5262 struct got_commit_object *commit;
5263 error = got_object_open_as_commit(&commit, repo, ids[0]);
5267 labels[1] = labels[0];
5269 if (got_object_commit_get_nparents(commit) > 0) {
5270 const struct got_object_id_queue *pids;
5271 struct got_object_qid *pid;
5272 pids = got_object_commit_get_parent_ids(commit);
5273 pid = STAILQ_FIRST(pids);
5274 ids[0] = got_object_id_dup(&pid->id);
5275 if (ids[0] == NULL) {
5276 error = got_error_from_errno(
5277 "got_object_id_dup");
5278 got_object_commit_close(commit);
5281 error = got_object_id_str(&labels[0], ids[0]);
5283 got_object_commit_close(commit);
5288 labels[0] = strdup("/dev/null");
5289 if (labels[0] == NULL) {
5290 error = got_error_from_errno("strdup");
5291 got_object_commit_close(commit);
5296 got_object_commit_close(commit);
5299 if (ncommit_args == 0 && argc > 2) {
5300 error = got_error_msg(GOT_ERR_BAD_PATH,
5301 "path arguments cannot be used when diffing two objects");
5306 error = got_object_get_type(&type1, repo, ids[0]);
5311 error = got_object_get_type(&type2, repo, ids[1]);
5314 if (type1 != GOT_OBJ_TYPE_ANY && type1 != type2) {
5315 error = got_error(GOT_ERR_OBJ_TYPE);
5318 if (type1 == GOT_OBJ_TYPE_BLOB && argc > 2) {
5319 error = got_error_msg(GOT_ERR_OBJ_TYPE,
5320 "path arguments cannot be used when diffing blobs");
5324 for (i = 0; ncommit_args > 0 && i < argc; i++) {
5326 struct got_pathlist_entry *new;
5330 error = got_worktree_resolve_path(&p, worktree,
5334 prefix = got_worktree_get_path_prefix(worktree);
5335 while (prefix[0] == '/')
5337 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5338 (p[0] != '\0' && prefix[0] != '\0') ? "/" : "",
5340 error = got_error_from_errno("asprintf");
5346 char *mapped_path, *s;
5347 error = got_repo_map_path(&mapped_path, repo, argv[i]);
5353 in_repo_path = strdup(s);
5354 if (in_repo_path == NULL) {
5355 error = got_error_from_errno("asprintf");
5362 error = got_pathlist_insert(&new, &paths, in_repo_path, NULL);
5363 if (error || new == NULL /* duplicate */)
5370 /* Release work tree lock. */
5371 got_worktree_close(worktree);
5375 fd1 = got_opentempfd();
5377 error = got_error_from_errno("got_opentempfd");
5381 fd2 = got_opentempfd();
5383 error = got_error_from_errno("got_opentempfd");
5387 switch (type1 == GOT_OBJ_TYPE_ANY ? type2 : type1) {
5388 case GOT_OBJ_TYPE_BLOB:
5389 error = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5390 fd1, fd2, ids[0], ids[1], NULL, NULL,
5391 GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
5392 ignore_whitespace, force_text_diff, show_diffstat,
5393 show_diffstat ? &dsa : NULL, repo, outfile);
5395 case GOT_OBJ_TYPE_TREE:
5396 error = got_diff_objects_as_trees(NULL, NULL, f1, f2, fd1, fd2,
5397 ids[0], ids[1], &paths, "", "",
5398 GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
5399 ignore_whitespace, force_text_diff, show_diffstat,
5400 show_diffstat ? &dsa : NULL, repo, outfile);
5402 case GOT_OBJ_TYPE_COMMIT:
5403 fprintf(outfile, "diff %s %s\n", labels[0], labels[1]);
5404 error = got_diff_objects_as_commits(NULL, NULL, f1, f2,
5405 fd1, fd2, ids[0], ids[1], &paths,
5406 GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
5407 ignore_whitespace, force_text_diff, show_diffstat,
5408 show_diffstat ? &dsa : NULL, repo, outfile);
5411 error = got_error(GOT_ERR_OBJ_TYPE);
5416 if (show_diffstat && dsa.nfiles > 0) {
5417 char *header = NULL;
5419 if (asprintf(&header, "diffstat %s %s",
5420 labels[0], labels[1]) == -1)
5423 error = print_diffstat(&dsa, &diffstat_paths, header);
5429 error = printfile(outfile);
5437 got_worktree_close(worktree);
5439 const struct got_error *close_err = got_repo_close(repo);
5444 const struct got_error *pack_err =
5445 got_repo_pack_fds_close(pack_fds);
5449 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
5450 got_pathlist_free(&diffstat_paths, GOT_PATHLIST_FREE_ALL);
5451 got_ref_list_free(&refs);
5452 if (outfile && fclose(outfile) == EOF && error == NULL)
5453 error = got_error_from_errno("fclose");
5454 if (f1 && fclose(f1) == EOF && error == NULL)
5455 error = got_error_from_errno("fclose");
5456 if (f2 && fclose(f2) == EOF && error == NULL)
5457 error = got_error_from_errno("fclose");
5458 if (fd1 != -1 && close(fd1) == -1 && error == NULL)
5459 error = got_error_from_errno("close");
5460 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
5461 error = got_error_from_errno("close");
5469 "usage: %s blame [-c commit] [-r repository-path] path\n",
5478 char datebuf[11]; /* YYYY-MM-DD + NUL */
5481 struct blame_cb_args {
5482 struct blame_line *lines;
5486 off_t *line_offsets;
5488 struct got_repository *repo;
5491 static const struct got_error *
5492 blame_cb(void *arg, int nlines, int lineno,
5493 struct got_commit_object *commit, struct got_object_id *id)
5495 const struct got_error *err = NULL;
5496 struct blame_cb_args *a = arg;
5497 struct blame_line *bline;
5499 size_t linesize = 0;
5502 time_t committer_time;
5504 if (nlines != a->nlines ||
5505 (lineno != -1 && lineno < 1) || lineno > a->nlines)
5506 return got_error(GOT_ERR_RANGE);
5508 if (sigint_received)
5509 return got_error(GOT_ERR_ITER_COMPLETED);
5512 return NULL; /* no change in this commit */
5514 /* Annotate this line. */
5515 bline = &a->lines[lineno - 1];
5516 if (bline->annotated)
5518 err = got_object_id_str(&bline->id_str, id);
5522 bline->committer = strdup(got_object_commit_get_committer(commit));
5523 if (bline->committer == NULL) {
5524 err = got_error_from_errno("strdup");
5528 committer_time = got_object_commit_get_committer_time(commit);
5529 if (gmtime_r(&committer_time, &tm) == NULL)
5530 return got_error_from_errno("gmtime_r");
5531 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
5533 err = got_error(GOT_ERR_NO_SPACE);
5536 bline->annotated = 1;
5538 /* Print lines annotated so far. */
5539 bline = &a->lines[a->lineno_cur - 1];
5540 if (!bline->annotated)
5543 offset = a->line_offsets[a->lineno_cur - 1];
5544 if (fseeko(a->f, offset, SEEK_SET) == -1) {
5545 err = got_error_from_errno("fseeko");
5549 while (a->lineno_cur <= a->nlines && bline->annotated) {
5550 char *smallerthan, *at, *nl, *committer;
5553 if (getline(&line, &linesize, a->f) == -1) {
5555 err = got_error_from_errno("getline");
5559 committer = bline->committer;
5560 smallerthan = strchr(committer, '<');
5561 if (smallerthan && smallerthan[1] != '\0')
5562 committer = smallerthan + 1;
5563 at = strchr(committer, '@');
5566 len = strlen(committer);
5568 committer[8] = '\0';
5570 nl = strchr(line, '\n');
5573 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
5574 bline->id_str, bline->datebuf, committer, line);
5577 bline = &a->lines[a->lineno_cur - 1];
5584 static const struct got_error *
5585 cmd_blame(int argc, char *argv[])
5587 const struct got_error *error;
5588 struct got_repository *repo = NULL;
5589 struct got_worktree *worktree = NULL;
5590 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5591 char *link_target = NULL;
5592 struct got_object_id *obj_id = NULL;
5593 struct got_object_id *commit_id = NULL;
5594 struct got_commit_object *commit = NULL;
5595 struct got_blob_object *blob = NULL;
5596 char *commit_id_str = NULL;
5597 struct blame_cb_args bca;
5598 int ch, obj_type, i, fd1 = -1, fd2 = -1, fd3 = -1;
5600 int *pack_fds = NULL;
5601 FILE *f1 = NULL, *f2 = NULL;
5603 fd1 = got_opentempfd();
5605 return got_error_from_errno("got_opentempfd");
5607 memset(&bca, 0, sizeof(bca));
5610 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5615 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5618 commit_id_str = optarg;
5621 repo_path = realpath(optarg, NULL);
5622 if (repo_path == NULL)
5623 return got_error_from_errno2("realpath",
5625 got_path_strip_trailing_slashes(repo_path);
5641 cwd = getcwd(NULL, 0);
5643 error = got_error_from_errno("getcwd");
5647 error = got_repo_pack_fds_open(&pack_fds);
5651 if (repo_path == NULL) {
5652 error = got_worktree_open(&worktree, cwd);
5653 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5659 strdup(got_worktree_get_repo_path(worktree));
5660 if (repo_path == NULL) {
5661 error = got_error_from_errno("strdup");
5666 repo_path = strdup(cwd);
5667 if (repo_path == NULL) {
5668 error = got_error_from_errno("strdup");
5674 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5679 const char *prefix = got_worktree_get_path_prefix(worktree);
5682 error = got_worktree_resolve_path(&p, worktree, path);
5685 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5686 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
5688 error = got_error_from_errno("asprintf");
5693 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5695 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5698 error = got_repo_map_path(&in_repo_path, repo, path);
5703 if (commit_id_str == NULL) {
5704 struct got_reference *head_ref;
5705 error = got_ref_open(&head_ref, repo, worktree ?
5706 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5709 error = got_ref_resolve(&commit_id, repo, head_ref);
5710 got_ref_close(head_ref);
5714 struct got_reflist_head refs;
5716 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5720 error = got_repo_match_object_id(&commit_id, NULL,
5721 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5722 got_ref_list_free(&refs);
5728 /* Release work tree lock. */
5729 got_worktree_close(worktree);
5733 error = got_object_open_as_commit(&commit, repo, commit_id);
5737 error = got_object_resolve_symlinks(&link_target, in_repo_path,
5742 error = got_object_id_by_path(&obj_id, repo, commit,
5743 link_target ? link_target : in_repo_path);
5747 error = got_object_get_type(&obj_type, repo, obj_id);
5751 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5752 error = got_error_path(link_target ? link_target : in_repo_path,
5757 error = got_object_open_as_blob(&blob, repo, obj_id, 8192, fd1);
5760 bca.f = got_opentemp();
5761 if (bca.f == NULL) {
5762 error = got_error_from_errno("got_opentemp");
5765 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
5766 &bca.line_offsets, bca.f, blob);
5767 if (error || bca.nlines == 0)
5770 /* Don't include \n at EOF in the blame line count. */
5771 if (bca.line_offsets[bca.nlines - 1] == filesize)
5774 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
5775 if (bca.lines == NULL) {
5776 error = got_error_from_errno("calloc");
5780 bca.nlines_prec = 0;
5788 fd2 = got_opentempfd();
5790 error = got_error_from_errno("got_opentempfd");
5793 fd3 = got_opentempfd();
5795 error = got_error_from_errno("got_opentempfd");
5798 f1 = got_opentemp();
5800 error = got_error_from_errno("got_opentemp");
5803 f2 = got_opentemp();
5805 error = got_error_from_errno("got_opentemp");
5808 error = got_blame(link_target ? link_target : in_repo_path, commit_id,
5809 repo, GOT_DIFF_ALGORITHM_PATIENCE, blame_cb, &bca,
5810 check_cancelled, NULL, fd2, fd3, f1, f2);
5819 got_object_commit_close(commit);
5821 if (fd1 != -1 && close(fd1) == -1 && error == NULL)
5822 error = got_error_from_errno("close");
5823 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
5824 error = got_error_from_errno("close");
5825 if (fd3 != -1 && close(fd3) == -1 && error == NULL)
5826 error = got_error_from_errno("close");
5827 if (f1 && fclose(f1) == EOF && error == NULL)
5828 error = got_error_from_errno("fclose");
5829 if (f2 && fclose(f2) == EOF && error == NULL)
5830 error = got_error_from_errno("fclose");
5833 got_object_blob_close(blob);
5835 got_worktree_close(worktree);
5837 const struct got_error *close_err = got_repo_close(repo);
5842 const struct got_error *pack_err =
5843 got_repo_pack_fds_close(pack_fds);
5848 for (i = 0; i < bca.nlines; i++) {
5849 struct blame_line *bline = &bca.lines[i];
5850 free(bline->id_str);
5851 free(bline->committer);
5855 free(bca.line_offsets);
5856 if (bca.f && fclose(bca.f) == EOF && error == NULL)
5857 error = got_error_from_errno("fclose");
5864 fprintf(stderr, "usage: %s tree [-iR] [-c commit] [-r repository-path] "
5865 "[path]\n", getprogname());
5869 static const struct got_error *
5870 print_entry(struct got_tree_entry *te, const char *id, const char *path,
5871 const char *root_path, struct got_repository *repo)
5873 const struct got_error *err = NULL;
5874 int is_root_path = (strcmp(path, root_path) == 0);
5875 const char *modestr = "";
5876 mode_t mode = got_tree_entry_get_mode(te);
5877 char *link_target = NULL;
5879 path += strlen(root_path);
5880 while (path[0] == '/')
5883 if (got_object_tree_entry_is_submodule(te))
5885 else if (S_ISLNK(mode)) {
5888 err = got_tree_entry_get_symlink_target(&link_target, te, repo);
5891 for (i = 0; i < strlen(link_target); i++) {
5892 if (!isprint((unsigned char)link_target[i]))
5893 link_target[i] = '?';
5898 else if (S_ISDIR(mode))
5900 else if (mode & S_IXUSR)
5903 printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
5904 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
5905 link_target ? " -> ": "", link_target ? link_target : "");
5911 static const struct got_error *
5912 print_tree(const char *path, struct got_commit_object *commit,
5913 int show_ids, int recurse, const char *root_path,
5914 struct got_repository *repo)
5916 const struct got_error *err = NULL;
5917 struct got_object_id *tree_id = NULL;
5918 struct got_tree_object *tree = NULL;
5921 err = got_object_id_by_path(&tree_id, repo, commit, path);
5925 err = got_object_open_as_tree(&tree, repo, tree_id);
5928 nentries = got_object_tree_get_nentries(tree);
5929 for (i = 0; i < nentries; i++) {
5930 struct got_tree_entry *te;
5933 if (sigint_received || sigpipe_received)
5936 te = got_object_tree_get_entry(tree, i);
5939 err = got_object_id_str(&id_str,
5940 got_tree_entry_get_id(te));
5943 if (asprintf(&id, "%s ", id_str) == -1) {
5944 err = got_error_from_errno("asprintf");
5950 err = print_entry(te, id, path, root_path, repo);
5955 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
5957 if (asprintf(&child_path, "%s%s%s", path,
5958 path[0] == '/' && path[1] == '\0' ? "" : "/",
5959 got_tree_entry_get_name(te)) == -1) {
5960 err = got_error_from_errno("asprintf");
5963 err = print_tree(child_path, commit, show_ids, 1,
5972 got_object_tree_close(tree);
5977 static const struct got_error *
5978 cmd_tree(int argc, char *argv[])
5980 const struct got_error *error;
5981 struct got_repository *repo = NULL;
5982 struct got_worktree *worktree = NULL;
5983 const char *path, *refname = NULL;
5984 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5985 struct got_object_id *commit_id = NULL;
5986 struct got_commit_object *commit = NULL;
5987 char *commit_id_str = NULL;
5988 int show_ids = 0, recurse = 0;
5990 int *pack_fds = NULL;
5993 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5998 while ((ch = getopt(argc, argv, "c:iRr:")) != -1) {
6001 commit_id_str = optarg;
6010 repo_path = realpath(optarg, NULL);
6011 if (repo_path == NULL)
6012 return got_error_from_errno2("realpath",
6014 got_path_strip_trailing_slashes(repo_path);
6032 cwd = getcwd(NULL, 0);
6034 error = got_error_from_errno("getcwd");
6038 error = got_repo_pack_fds_open(&pack_fds);
6042 if (repo_path == NULL) {
6043 error = got_worktree_open(&worktree, cwd);
6044 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6050 strdup(got_worktree_get_repo_path(worktree));
6051 if (repo_path == NULL)
6052 error = got_error_from_errno("strdup");
6056 repo_path = strdup(cwd);
6057 if (repo_path == NULL) {
6058 error = got_error_from_errno("strdup");
6064 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6069 const char *prefix = got_worktree_get_path_prefix(worktree);
6074 error = got_worktree_resolve_path(&p, worktree, path);
6077 if (asprintf(&in_repo_path, "%s%s%s", prefix,
6078 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
6080 error = got_error_from_errno("asprintf");
6085 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6089 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6094 error = got_repo_map_path(&in_repo_path, repo, path);
6099 if (commit_id_str == NULL) {
6100 struct got_reference *head_ref;
6102 refname = got_worktree_get_head_ref_name(worktree);
6104 refname = GOT_REF_HEAD;
6105 error = got_ref_open(&head_ref, repo, refname, 0);
6108 error = got_ref_resolve(&commit_id, repo, head_ref);
6109 got_ref_close(head_ref);
6113 struct got_reflist_head refs;
6115 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
6119 error = got_repo_match_object_id(&commit_id, NULL,
6120 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
6121 got_ref_list_free(&refs);
6127 /* Release work tree lock. */
6128 got_worktree_close(worktree);
6132 error = got_object_open_as_commit(&commit, repo, commit_id);
6136 error = print_tree(in_repo_path, commit, show_ids, recurse,
6137 in_repo_path, repo);
6144 got_object_commit_close(commit);
6146 got_worktree_close(worktree);
6148 const struct got_error *close_err = got_repo_close(repo);
6153 const struct got_error *pack_err =
6154 got_repo_pack_fds_close(pack_fds);
6164 fprintf(stderr, "usage: %s status [-I] [-S status-codes] "
6165 "[-s status-codes] [path ...]\n", getprogname());
6169 struct got_status_arg {
6174 static const struct got_error *
6175 print_status(void *arg, unsigned char status, unsigned char staged_status,
6176 const char *path, struct got_object_id *blob_id,
6177 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6178 int dirfd, const char *de_name)
6180 struct got_status_arg *st = arg;
6182 if (status == staged_status && (status == GOT_STATUS_DELETE))
6183 status = GOT_STATUS_NO_CHANGE;
6184 if (st != NULL && st->status_codes) {
6185 size_t ncodes = strlen(st->status_codes);
6188 for (i = 0; i < ncodes ; i++) {
6190 if (status == st->status_codes[i] ||
6191 staged_status == st->status_codes[i]) {
6196 if (status == st->status_codes[i] ||
6197 staged_status == st->status_codes[i])
6202 if (st->suppress && j == 0)
6209 printf("%c%c %s\n", status, staged_status, path);
6213 static const struct got_error *
6214 cmd_status(int argc, char *argv[])
6216 const struct got_error *error = NULL;
6217 struct got_repository *repo = NULL;
6218 struct got_worktree *worktree = NULL;
6219 struct got_status_arg st;
6221 struct got_pathlist_head paths;
6222 int ch, i, no_ignores = 0;
6223 int *pack_fds = NULL;
6227 memset(&st, 0, sizeof(st));
6228 st.status_codes = NULL;
6231 while ((ch = getopt(argc, argv, "IS:s:")) != -1) {
6237 if (st.status_codes != NULL && st.suppress == 0)
6238 option_conflict('S', 's');
6242 for (i = 0; i < strlen(optarg); i++) {
6243 switch (optarg[i]) {
6244 case GOT_STATUS_MODIFY:
6245 case GOT_STATUS_ADD:
6246 case GOT_STATUS_DELETE:
6247 case GOT_STATUS_CONFLICT:
6248 case GOT_STATUS_MISSING:
6249 case GOT_STATUS_OBSTRUCTED:
6250 case GOT_STATUS_UNVERSIONED:
6251 case GOT_STATUS_MODE_CHANGE:
6252 case GOT_STATUS_NONEXISTENT:
6255 errx(1, "invalid status code '%c'",
6259 if (ch == 's' && st.suppress)
6260 option_conflict('s', 'S');
6261 st.status_codes = optarg;
6273 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6277 cwd = getcwd(NULL, 0);
6279 error = got_error_from_errno("getcwd");
6283 error = got_repo_pack_fds_open(&pack_fds);
6287 error = got_worktree_open(&worktree, cwd);
6289 if (error->code == GOT_ERR_NOT_WORKTREE)
6290 error = wrap_not_worktree_error(error, "status", cwd);
6294 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6299 error = apply_unveil(got_repo_get_path(repo), 1,
6300 got_worktree_get_root_path(worktree));
6304 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6308 error = got_worktree_status(worktree, &paths, repo, no_ignores,
6309 print_status, &st, check_cancelled, NULL);
6312 const struct got_error *pack_err =
6313 got_repo_pack_fds_close(pack_fds);
6318 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
6326 fprintf(stderr, "usage: %s ref [-dlt] [-c object] [-r repository-path] "
6327 "[-s reference] [name]\n", getprogname());
6331 static const struct got_error *
6332 list_refs(struct got_repository *repo, const char *refname, int sort_by_time)
6334 static const struct got_error *err = NULL;
6335 struct got_reflist_head refs;
6336 struct got_reflist_entry *re;
6339 err = got_ref_list(&refs, repo, refname, sort_by_time ?
6340 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6345 TAILQ_FOREACH(re, &refs, entry) {
6347 refstr = got_ref_to_str(re->ref);
6348 if (refstr == NULL) {
6349 err = got_error_from_errno("got_ref_to_str");
6352 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
6356 got_ref_list_free(&refs);
6360 static const struct got_error *
6361 delete_ref_by_name(struct got_repository *repo, const char *refname)
6363 const struct got_error *err;
6364 struct got_reference *ref;
6366 err = got_ref_open(&ref, repo, refname, 0);
6370 err = delete_ref(repo, ref);
6375 static const struct got_error *
6376 add_ref(struct got_repository *repo, const char *refname, const char *target)
6378 const struct got_error *err = NULL;
6379 struct got_object_id *id = NULL;
6380 struct got_reference *ref = NULL;
6381 struct got_reflist_head refs;
6384 * Don't let the user create a reference name with a leading '-'.
6385 * While technically a valid reference name, this case is usually
6386 * an unintended typo.
6388 if (refname[0] == '-')
6389 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
6392 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
6395 err = got_repo_match_object_id(&id, NULL, target, GOT_OBJ_TYPE_ANY,
6397 got_ref_list_free(&refs);
6401 err = got_ref_alloc(&ref, refname, id);
6405 err = got_ref_write(ref, repo);
6413 static const struct got_error *
6414 add_symref(struct got_repository *repo, const char *refname, const char *target)
6416 const struct got_error *err = NULL;
6417 struct got_reference *ref = NULL;
6418 struct got_reference *target_ref = NULL;
6421 * Don't let the user create a reference name with a leading '-'.
6422 * While technically a valid reference name, this case is usually
6423 * an unintended typo.
6425 if (refname[0] == '-')
6426 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
6428 err = got_ref_open(&target_ref, repo, target, 0);
6432 err = got_ref_alloc_symref(&ref, refname, target_ref);
6436 err = got_ref_write(ref, repo);
6439 got_ref_close(target_ref);
6445 static const struct got_error *
6446 cmd_ref(int argc, char *argv[])
6448 const struct got_error *error = NULL;
6449 struct got_repository *repo = NULL;
6450 struct got_worktree *worktree = NULL;
6451 char *cwd = NULL, *repo_path = NULL;
6452 int ch, do_list = 0, do_delete = 0, sort_by_time = 0;
6453 const char *obj_arg = NULL, *symref_target= NULL;
6454 char *refname = NULL;
6455 int *pack_fds = NULL;
6457 while ((ch = getopt(argc, argv, "c:dlr:s:t")) != -1) {
6469 repo_path = realpath(optarg, NULL);
6470 if (repo_path == NULL)
6471 return got_error_from_errno2("realpath",
6473 got_path_strip_trailing_slashes(repo_path);
6476 symref_target = optarg;
6487 if (obj_arg && do_list)
6488 option_conflict('c', 'l');
6489 if (obj_arg && do_delete)
6490 option_conflict('c', 'd');
6491 if (obj_arg && symref_target)
6492 option_conflict('c', 's');
6493 if (symref_target && do_delete)
6494 option_conflict('s', 'd');
6495 if (symref_target && do_list)
6496 option_conflict('s', 'l');
6497 if (do_delete && do_list)
6498 option_conflict('d', 'l');
6499 if (sort_by_time && !do_list)
6500 errx(1, "-t option requires -l option");
6506 if (argc != 0 && argc != 1)
6509 refname = strdup(argv[0]);
6510 if (refname == NULL) {
6511 error = got_error_from_errno("strdup");
6518 refname = strdup(argv[0]);
6519 if (refname == NULL) {
6520 error = got_error_from_errno("strdup");
6526 got_path_strip_trailing_slashes(refname);
6529 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6530 "sendfd unveil", NULL) == -1)
6533 cwd = getcwd(NULL, 0);
6535 error = got_error_from_errno("getcwd");
6539 error = got_repo_pack_fds_open(&pack_fds);
6543 if (repo_path == NULL) {
6544 error = got_worktree_open(&worktree, cwd);
6545 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6551 strdup(got_worktree_get_repo_path(worktree));
6552 if (repo_path == NULL)
6553 error = got_error_from_errno("strdup");
6557 repo_path = strdup(cwd);
6558 if (repo_path == NULL) {
6559 error = got_error_from_errno("strdup");
6565 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6571 /* Remove "cpath" promise. */
6572 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6578 error = apply_unveil(got_repo_get_path(repo), do_list,
6579 worktree ? got_worktree_get_root_path(worktree) : NULL);
6584 error = list_refs(repo, refname, sort_by_time);
6586 error = delete_ref_by_name(repo, refname);
6587 else if (symref_target)
6588 error = add_symref(repo, refname, symref_target);
6590 if (obj_arg == NULL)
6592 error = add_ref(repo, refname, obj_arg);
6597 const struct got_error *close_err = got_repo_close(repo);
6602 got_worktree_close(worktree);
6604 const struct got_error *pack_err =
6605 got_repo_pack_fds_close(pack_fds);
6617 fprintf(stderr, "usage: %s branch [-lnt] [-c commit] [-d name] "
6618 "[-r repository-path] [name]\n", getprogname());
6622 static const struct got_error *
6623 list_branch(struct got_repository *repo, struct got_worktree *worktree,
6624 struct got_reference *ref)
6626 const struct got_error *err = NULL;
6627 const char *refname, *marker = " ";
6630 refname = got_ref_get_name(ref);
6631 if (worktree && strcmp(refname,
6632 got_worktree_get_head_ref_name(worktree)) == 0) {
6633 struct got_object_id *id = NULL;
6635 err = got_ref_resolve(&id, repo, ref);
6638 if (got_object_id_cmp(id,
6639 got_worktree_get_base_commit_id(worktree)) == 0)
6646 if (strncmp(refname, "refs/heads/", 11) == 0)
6648 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
6650 if (strncmp(refname, "refs/remotes/", 13) == 0)
6653 refstr = got_ref_to_str(ref);
6655 return got_error_from_errno("got_ref_to_str");
6657 printf("%s%s: %s\n", marker, refname, refstr);
6662 static const struct got_error *
6663 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
6665 const char *refname;
6667 if (worktree == NULL)
6668 return got_error(GOT_ERR_NOT_WORKTREE);
6670 refname = got_worktree_get_head_ref_name(worktree);
6672 if (strncmp(refname, "refs/heads/", 11) == 0)
6674 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
6677 printf("%s\n", refname);
6682 static const struct got_error *
6683 list_branches(struct got_repository *repo, struct got_worktree *worktree,
6686 static const struct got_error *err = NULL;
6687 struct got_reflist_head refs;
6688 struct got_reflist_entry *re;
6689 struct got_reference *temp_ref = NULL;
6690 int rebase_in_progress, histedit_in_progress;
6695 err = got_worktree_rebase_in_progress(&rebase_in_progress,
6700 err = got_worktree_histedit_in_progress(&histedit_in_progress,
6705 if (rebase_in_progress || histedit_in_progress) {
6706 err = got_ref_open(&temp_ref, repo,
6707 got_worktree_get_head_ref_name(worktree), 0);
6710 list_branch(repo, worktree, temp_ref);
6711 got_ref_close(temp_ref);
6715 err = got_ref_list(&refs, repo, "refs/heads", sort_by_time ?
6716 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6721 TAILQ_FOREACH(re, &refs, entry)
6722 list_branch(repo, worktree, re->ref);
6724 got_ref_list_free(&refs);
6726 err = got_ref_list(&refs, repo, "refs/remotes", sort_by_time ?
6727 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6732 TAILQ_FOREACH(re, &refs, entry)
6733 list_branch(repo, worktree, re->ref);
6735 got_ref_list_free(&refs);
6740 static const struct got_error *
6741 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
6742 const char *branch_name)
6744 const struct got_error *err = NULL;
6745 struct got_reference *ref = NULL;
6746 char *refname, *remote_refname = NULL;
6748 if (strncmp(branch_name, "refs/", 5) == 0)
6750 if (strncmp(branch_name, "heads/", 6) == 0)
6752 else if (strncmp(branch_name, "remotes/", 8) == 0)
6755 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
6756 return got_error_from_errno("asprintf");
6758 if (asprintf(&remote_refname, "refs/remotes/%s",
6759 branch_name) == -1) {
6760 err = got_error_from_errno("asprintf");
6764 err = got_ref_open(&ref, repo, refname, 0);
6766 const struct got_error *err2;
6767 if (err->code != GOT_ERR_NOT_REF)
6770 * Keep 'err' intact such that if neither branch exists
6771 * we report "refs/heads" rather than "refs/remotes" in
6772 * our error message.
6774 err2 = got_ref_open(&ref, repo, remote_refname, 0);
6781 strcmp(got_worktree_get_head_ref_name(worktree),
6782 got_ref_get_name(ref)) == 0) {
6783 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6784 "will not delete this work tree's current branch");
6788 err = delete_ref(repo, ref);
6793 free(remote_refname);
6797 static const struct got_error *
6798 add_branch(struct got_repository *repo, const char *branch_name,
6799 struct got_object_id *base_commit_id)
6801 const struct got_error *err = NULL;
6802 struct got_reference *ref = NULL;
6803 char *refname = NULL;
6806 * Don't let the user create a branch name with a leading '-'.
6807 * While technically a valid reference name, this case is usually
6808 * an unintended typo.
6810 if (branch_name[0] == '-')
6811 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
6813 if (strncmp(branch_name, "refs/heads/", 11) == 0)
6816 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
6817 err = got_error_from_errno("asprintf");
6821 err = got_ref_open(&ref, repo, refname, 0);
6823 err = got_error(GOT_ERR_BRANCH_EXISTS);
6825 } else if (err->code != GOT_ERR_NOT_REF)
6828 err = got_ref_alloc(&ref, refname, base_commit_id);
6832 err = got_ref_write(ref, repo);
6840 static const struct got_error *
6841 cmd_branch(int argc, char *argv[])
6843 const struct got_error *error = NULL;
6844 struct got_repository *repo = NULL;
6845 struct got_worktree *worktree = NULL;
6846 char *cwd = NULL, *repo_path = NULL;
6847 int ch, do_list = 0, do_show = 0, do_update = 1, sort_by_time = 0;
6848 const char *delref = NULL, *commit_id_arg = NULL;
6849 struct got_reference *ref = NULL;
6850 struct got_pathlist_head paths;
6851 struct got_object_id *commit_id = NULL;
6852 char *commit_id_str = NULL;
6853 int *pack_fds = NULL;
6857 while ((ch = getopt(argc, argv, "c:d:lnr:t")) != -1) {
6860 commit_id_arg = optarg;
6872 repo_path = realpath(optarg, NULL);
6873 if (repo_path == NULL)
6874 return got_error_from_errno2("realpath",
6876 got_path_strip_trailing_slashes(repo_path);
6887 if (do_list && delref)
6888 option_conflict('l', 'd');
6889 if (sort_by_time && !do_list)
6890 errx(1, "-t option requires -l option");
6895 if (!do_list && !delref && argc == 0)
6898 if ((do_list || delref || do_show) && commit_id_arg != NULL)
6899 errx(1, "-c option can only be used when creating a branch");
6901 if (do_list || delref) {
6904 } else if (!do_show && argc != 1)
6908 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6909 "sendfd unveil", NULL) == -1)
6912 cwd = getcwd(NULL, 0);
6914 error = got_error_from_errno("getcwd");
6918 error = got_repo_pack_fds_open(&pack_fds);
6922 if (repo_path == NULL) {
6923 error = got_worktree_open(&worktree, cwd);
6924 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6930 strdup(got_worktree_get_repo_path(worktree));
6931 if (repo_path == NULL)
6932 error = got_error_from_errno("strdup");
6936 repo_path = strdup(cwd);
6937 if (repo_path == NULL) {
6938 error = got_error_from_errno("strdup");
6944 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6949 if (do_list || do_show) {
6950 /* Remove "cpath" promise. */
6951 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6957 error = apply_unveil(got_repo_get_path(repo), do_list,
6958 worktree ? got_worktree_get_root_path(worktree) : NULL);
6963 error = show_current_branch(repo, worktree);
6965 error = list_branches(repo, worktree, sort_by_time);
6967 error = delete_branch(repo, worktree, delref);
6969 struct got_reflist_head refs;
6971 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
6975 if (commit_id_arg == NULL)
6976 commit_id_arg = worktree ?
6977 got_worktree_get_head_ref_name(worktree) :
6979 error = got_repo_match_object_id(&commit_id, NULL,
6980 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &refs, repo);
6981 got_ref_list_free(&refs);
6984 error = add_branch(repo, argv[0], commit_id);
6987 if (worktree && do_update) {
6988 struct got_update_progress_arg upa;
6989 char *branch_refname = NULL;
6991 error = got_object_id_str(&commit_id_str, commit_id);
6994 error = get_worktree_paths_from_argv(&paths, 0, NULL,
6998 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
7000 error = got_error_from_errno("asprintf");
7003 error = got_ref_open(&ref, repo, branch_refname, 0);
7004 free(branch_refname);
7007 error = switch_head_ref(ref, commit_id, worktree,
7011 error = got_worktree_set_base_commit_id(worktree, repo,
7015 memset(&upa, 0, sizeof(upa));
7016 error = got_worktree_checkout_files(worktree, &paths,
7017 repo, update_progress, &upa, check_cancelled,
7021 if (upa.did_something) {
7022 printf("Updated to %s: %s\n",
7023 got_worktree_get_head_ref_name(worktree),
7026 print_update_progress_stats(&upa);
7033 const struct got_error *close_err = got_repo_close(repo);
7038 got_worktree_close(worktree);
7040 const struct got_error *pack_err =
7041 got_repo_pack_fds_close(pack_fds);
7048 free(commit_id_str);
7049 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
7057 fprintf(stderr, "usage: %s tag [-lVv] [-c commit] [-m message] "
7058 "[-r repository-path] [-s signer-id] name\n", getprogname());
7063 static const struct got_error *
7064 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
7066 const struct got_error *err = NULL;
7067 struct got_reflist_entry *re, *se, *new;
7068 struct got_object_id *re_id, *se_id;
7069 struct got_tag_object *re_tag, *se_tag;
7070 time_t re_time, se_time;
7072 STAILQ_FOREACH(re, tags, entry) {
7073 se = STAILQ_FIRST(sorted);
7075 err = got_reflist_entry_dup(&new, re);
7078 STAILQ_INSERT_HEAD(sorted, new, entry);
7081 err = got_ref_resolve(&re_id, repo, re->ref);
7084 err = got_object_open_as_tag(&re_tag, repo, re_id);
7088 re_time = got_object_tag_get_tagger_time(re_tag);
7089 got_object_tag_close(re_tag);
7093 err = got_ref_resolve(&se_id, repo, re->ref);
7096 err = got_object_open_as_tag(&se_tag, repo, se_id);
7100 se_time = got_object_tag_get_tagger_time(se_tag);
7101 got_object_tag_close(se_tag);
7103 if (se_time > re_time) {
7104 err = got_reflist_entry_dup(&new, re);
7107 STAILQ_INSERT_AFTER(sorted, se, new, entry);
7110 se = STAILQ_NEXT(se, entry);
7119 static const struct got_error *
7120 get_tag_refname(char **refname, const char *tag_name)
7122 const struct got_error *err;
7124 if (strncmp("refs/tags/", tag_name, 10) == 0) {
7125 *refname = strdup(tag_name);
7126 if (*refname == NULL)
7127 return got_error_from_errno("strdup");
7128 } else if (asprintf(refname, "refs/tags/%s", tag_name) == -1) {
7129 err = got_error_from_errno("asprintf");
7137 static const struct got_error *
7138 list_tags(struct got_repository *repo, const char *tag_name, int verify_tags,
7139 const char *allowed_signers, const char *revoked_signers, int verbosity)
7141 static const struct got_error *err = NULL;
7142 struct got_reflist_head refs;
7143 struct got_reflist_entry *re;
7144 char *wanted_refname = NULL;
7149 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
7154 struct got_reference *ref;
7155 err = get_tag_refname(&wanted_refname, tag_name);
7158 /* Wanted tag reference should exist. */
7159 err = got_ref_open(&ref, repo, wanted_refname, 0);
7165 TAILQ_FOREACH(re, &refs, entry) {
7166 const char *refname;
7167 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
7169 const char *tagger, *ssh_sig = NULL;
7170 char *sig_msg = NULL;
7172 struct got_object_id *id;
7173 struct got_tag_object *tag;
7174 struct got_commit_object *commit = NULL;
7176 refname = got_ref_get_name(re->ref);
7177 if (strncmp(refname, "refs/tags/", 10) != 0 ||
7178 (wanted_refname && strcmp(refname, wanted_refname) != 0))
7181 refstr = got_ref_to_str(re->ref);
7182 if (refstr == NULL) {
7183 err = got_error_from_errno("got_ref_to_str");
7187 err = got_ref_resolve(&id, repo, re->ref);
7190 err = got_object_open_as_tag(&tag, repo, id);
7192 if (err->code != GOT_ERR_OBJ_TYPE) {
7196 /* "lightweight" tag */
7197 err = got_object_open_as_commit(&commit, repo, id);
7202 tagger = got_object_commit_get_committer(commit);
7204 got_object_commit_get_committer_time(commit);
7205 err = got_object_id_str(&id_str, id);
7211 tagger = got_object_tag_get_tagger(tag);
7212 tagger_time = got_object_tag_get_tagger_time(tag);
7213 err = got_object_id_str(&id_str,
7214 got_object_tag_get_object_id(tag));
7219 if (tag && verify_tags) {
7220 ssh_sig = got_sigs_get_tagmsg_ssh_signature(
7221 got_object_tag_get_message(tag));
7222 if (ssh_sig && allowed_signers == NULL) {
7223 err = got_error_msg(
7224 GOT_ERR_VERIFY_TAG_SIGNATURE,
7225 "SSH signature verification requires "
7226 "setting allowed_signers in "
7232 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
7234 printf("from: %s\n", tagger);
7235 datestr = get_datestr(&tagger_time, datebuf);
7237 printf("date: %s UTC\n", datestr);
7239 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
7241 switch (got_object_tag_get_object_type(tag)) {
7242 case GOT_OBJ_TYPE_BLOB:
7243 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
7246 case GOT_OBJ_TYPE_TREE:
7247 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
7250 case GOT_OBJ_TYPE_COMMIT:
7251 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
7254 case GOT_OBJ_TYPE_TAG:
7255 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
7265 err = got_sigs_verify_tag_ssh(&sig_msg, tag, ssh_sig,
7266 allowed_signers, revoked_signers, verbosity);
7267 if (err && err->code == GOT_ERR_BAD_TAG_SIGNATURE)
7271 printf("signature: %s", sig_msg);
7277 err = got_object_commit_get_logmsg(&tagmsg0, commit);
7280 got_object_commit_close(commit);
7282 tagmsg0 = strdup(got_object_tag_get_message(tag));
7283 got_object_tag_close(tag);
7284 if (tagmsg0 == NULL) {
7285 err = got_error_from_errno("strdup");
7292 line = strsep(&tagmsg, "\n");
7294 printf(" %s\n", line);
7299 got_ref_list_free(&refs);
7300 free(wanted_refname);
7302 if (err == NULL && bad_sigs)
7303 err = got_error(GOT_ERR_BAD_TAG_SIGNATURE);
7307 static const struct got_error *
7308 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
7309 const char *tag_name, const char *repo_path)
7311 const struct got_error *err = NULL;
7312 char *template = NULL, *initial_content = NULL;
7313 char *editor = NULL;
7314 int initial_content_len;
7317 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
7318 err = got_error_from_errno("asprintf");
7322 initial_content_len = asprintf(&initial_content,
7323 "\n# tagging commit %s as %s\n",
7324 commit_id_str, tag_name);
7325 if (initial_content_len == -1) {
7326 err = got_error_from_errno("asprintf");
7330 err = got_opentemp_named_fd(tagmsg_path, &fd, template, "");
7334 if (write(fd, initial_content, initial_content_len) == -1) {
7335 err = got_error_from_errno2("write", *tagmsg_path);
7339 err = get_editor(&editor);
7342 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content,
7343 initial_content_len, 1);
7345 free(initial_content);
7349 if (fd != -1 && close(fd) == -1 && err == NULL)
7350 err = got_error_from_errno2("close", *tagmsg_path);
7359 static const struct got_error *
7360 add_tag(struct got_repository *repo, const char *tagger,
7361 const char *tag_name, const char *commit_arg, const char *tagmsg_arg,
7362 const char *signer_id, int verbosity)
7364 const struct got_error *err = NULL;
7365 struct got_object_id *commit_id = NULL, *tag_id = NULL;
7366 char *label = NULL, *commit_id_str = NULL;
7367 struct got_reference *ref = NULL;
7368 char *refname = NULL, *tagmsg = NULL;
7369 char *tagmsg_path = NULL, *tag_id_str = NULL;
7370 int preserve_tagmsg = 0;
7371 struct got_reflist_head refs;
7376 * Don't let the user create a tag name with a leading '-'.
7377 * While technically a valid reference name, this case is usually
7378 * an unintended typo.
7380 if (tag_name[0] == '-')
7381 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
7383 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
7387 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
7388 GOT_OBJ_TYPE_COMMIT, &refs, repo);
7392 err = got_object_id_str(&commit_id_str, commit_id);
7396 err = get_tag_refname(&refname, tag_name);
7399 if (strncmp("refs/tags/", tag_name, 10) == 0)
7402 err = got_ref_open(&ref, repo, refname, 0);
7404 err = got_error(GOT_ERR_TAG_EXISTS);
7406 } else if (err->code != GOT_ERR_NOT_REF)
7409 if (tagmsg_arg == NULL) {
7410 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
7411 tag_name, got_repo_get_path(repo));
7413 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
7414 tagmsg_path != NULL)
7415 preserve_tagmsg = 1;
7418 /* Editor is done; we can now apply unveil(2) */
7419 err = got_sigs_apply_unveil();
7422 err = apply_unveil(got_repo_get_path(repo), 0, NULL);
7427 err = got_object_tag_create(&tag_id, tag_name, commit_id,
7428 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, signer_id, repo,
7432 preserve_tagmsg = 1;
7436 err = got_ref_alloc(&ref, refname, tag_id);
7439 preserve_tagmsg = 1;
7443 err = got_ref_write(ref, repo);
7446 preserve_tagmsg = 1;
7450 err = got_object_id_str(&tag_id_str, tag_id);
7453 preserve_tagmsg = 1;
7456 printf("Created tag %s\n", tag_id_str);
7458 if (preserve_tagmsg) {
7459 fprintf(stderr, "%s: tag message preserved in %s\n",
7460 getprogname(), tagmsg_path);
7461 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
7462 err = got_error_from_errno2("unlink", tagmsg_path);
7467 free(commit_id_str);
7471 got_ref_list_free(&refs);
7475 static const struct got_error *
7476 cmd_tag(int argc, char *argv[])
7478 const struct got_error *error = NULL;
7479 struct got_repository *repo = NULL;
7480 struct got_worktree *worktree = NULL;
7481 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
7482 char *gitconfig_path = NULL, *tagger = NULL;
7483 char *allowed_signers = NULL, *revoked_signers = NULL;
7484 char *signer_id = NULL;
7485 const char *tag_name = NULL, *commit_id_arg = NULL, *tagmsg = NULL;
7486 int ch, do_list = 0, verify_tags = 0, verbosity = 0;
7487 int *pack_fds = NULL;
7489 while ((ch = getopt(argc, argv, "c:lm:r:s:Vv")) != -1) {
7492 commit_id_arg = optarg;
7501 repo_path = realpath(optarg, NULL);
7502 if (repo_path == NULL) {
7503 error = got_error_from_errno2("realpath",
7507 got_path_strip_trailing_slashes(repo_path);
7510 signer_id = strdup(optarg);
7511 if (signer_id == NULL) {
7512 error = got_error_from_errno("strdup");
7522 else if (verbosity < 3)
7534 if (do_list || verify_tags) {
7535 if (commit_id_arg != NULL)
7537 "-c option can only be used when creating a tag");
7540 option_conflict('l', 'm');
7542 option_conflict('V', 'm');
7546 option_conflict('l', 's');
7548 option_conflict('V', 's');
7552 } else if (argc != 1)
7559 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
7560 "sendfd unveil", NULL) == -1)
7563 cwd = getcwd(NULL, 0);
7565 error = got_error_from_errno("getcwd");
7569 error = got_repo_pack_fds_open(&pack_fds);
7573 if (repo_path == NULL) {
7574 error = got_worktree_open(&worktree, cwd);
7575 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7581 strdup(got_worktree_get_repo_path(worktree));
7582 if (repo_path == NULL)
7583 error = got_error_from_errno("strdup");
7587 repo_path = strdup(cwd);
7588 if (repo_path == NULL) {
7589 error = got_error_from_errno("strdup");
7595 if (do_list || verify_tags) {
7596 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7599 error = get_allowed_signers(&allowed_signers, repo, worktree);
7602 error = get_revoked_signers(&revoked_signers, repo, worktree);
7606 /* Release work tree lock. */
7607 got_worktree_close(worktree);
7612 * Remove "cpath" promise unless needed for signature tmpfile
7616 got_sigs_apply_unveil();
7619 if (pledge("stdio rpath wpath flock proc exec sendfd "
7620 "unveil", NULL) == -1)
7624 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7627 error = list_tags(repo, tag_name, verify_tags, allowed_signers,
7628 revoked_signers, verbosity);
7630 error = get_gitconfig_path(&gitconfig_path);
7633 error = got_repo_open(&repo, repo_path, gitconfig_path,
7638 error = get_author(&tagger, repo, worktree);
7641 if (signer_id == NULL) {
7642 error = get_signer_id(&signer_id, repo, worktree);
7649 error = got_sigs_apply_unveil();
7653 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
7658 if (commit_id_arg == NULL) {
7659 struct got_reference *head_ref;
7660 struct got_object_id *commit_id;
7661 error = got_ref_open(&head_ref, repo,
7662 worktree ? got_worktree_get_head_ref_name(worktree)
7666 error = got_ref_resolve(&commit_id, repo, head_ref);
7667 got_ref_close(head_ref);
7670 error = got_object_id_str(&commit_id_str, commit_id);
7677 /* Release work tree lock. */
7678 got_worktree_close(worktree);
7682 error = add_tag(repo, tagger, tag_name,
7683 commit_id_str ? commit_id_str : commit_id_arg, tagmsg,
7684 signer_id, verbosity);
7688 const struct got_error *close_err = got_repo_close(repo);
7693 got_worktree_close(worktree);
7695 const struct got_error *pack_err =
7696 got_repo_pack_fds_close(pack_fds);
7702 free(gitconfig_path);
7703 free(commit_id_str);
7705 free(allowed_signers);
7706 free(revoked_signers);
7714 fprintf(stderr, "usage: %s add [-IR] path ...\n", getprogname());
7718 static const struct got_error *
7719 add_progress(void *arg, unsigned char status, const char *path)
7721 while (path[0] == '/')
7723 printf("%c %s\n", status, path);
7727 static const struct got_error *
7728 cmd_add(int argc, char *argv[])
7730 const struct got_error *error = NULL;
7731 struct got_repository *repo = NULL;
7732 struct got_worktree *worktree = NULL;
7734 struct got_pathlist_head paths;
7735 struct got_pathlist_entry *pe;
7736 int ch, can_recurse = 0, no_ignores = 0;
7737 int *pack_fds = NULL;
7741 while ((ch = getopt(argc, argv, "IR")) != -1) {
7759 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7766 cwd = getcwd(NULL, 0);
7768 error = got_error_from_errno("getcwd");
7772 error = got_repo_pack_fds_open(&pack_fds);
7776 error = got_worktree_open(&worktree, cwd);
7778 if (error->code == GOT_ERR_NOT_WORKTREE)
7779 error = wrap_not_worktree_error(error, "add", cwd);
7783 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7788 error = apply_unveil(got_repo_get_path(repo), 1,
7789 got_worktree_get_root_path(worktree));
7793 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7800 TAILQ_FOREACH(pe, &paths, entry) {
7801 if (asprintf(&ondisk_path, "%s/%s",
7802 got_worktree_get_root_path(worktree),
7804 error = got_error_from_errno("asprintf");
7807 if (lstat(ondisk_path, &sb) == -1) {
7808 if (errno == ENOENT) {
7812 error = got_error_from_errno2("lstat",
7818 if (S_ISDIR(sb.st_mode)) {
7819 error = got_error_msg(GOT_ERR_BAD_PATH,
7820 "adding directories requires -R option");
7826 error = got_worktree_schedule_add(worktree, &paths, add_progress,
7827 NULL, repo, no_ignores);
7830 const struct got_error *close_err = got_repo_close(repo);
7835 got_worktree_close(worktree);
7837 const struct got_error *pack_err =
7838 got_repo_pack_fds_close(pack_fds);
7842 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
7850 fprintf(stderr, "usage: %s remove [-fkR] [-s status-codes] path ...\n",
7855 static const struct got_error *
7856 print_remove_status(void *arg, unsigned char status,
7857 unsigned char staged_status, const char *path)
7859 while (path[0] == '/')
7861 if (status == GOT_STATUS_NONEXISTENT)
7863 if (status == staged_status && (status == GOT_STATUS_DELETE))
7864 status = GOT_STATUS_NO_CHANGE;
7865 printf("%c%c %s\n", status, staged_status, path);
7869 static const struct got_error *
7870 cmd_remove(int argc, char *argv[])
7872 const struct got_error *error = NULL;
7873 struct got_worktree *worktree = NULL;
7874 struct got_repository *repo = NULL;
7875 const char *status_codes = NULL;
7877 struct got_pathlist_head paths;
7878 struct got_pathlist_entry *pe;
7879 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0, i;
7880 int ignore_missing_paths = 0;
7881 int *pack_fds = NULL;
7885 while ((ch = getopt(argc, argv, "fkRs:")) != -1) {
7888 delete_local_mods = 1;
7889 ignore_missing_paths = 1;
7898 for (i = 0; i < strlen(optarg); i++) {
7899 switch (optarg[i]) {
7900 case GOT_STATUS_MODIFY:
7901 delete_local_mods = 1;
7903 case GOT_STATUS_MISSING:
7904 ignore_missing_paths = 1;
7907 errx(1, "invalid status code '%c'",
7911 status_codes = optarg;
7923 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7930 cwd = getcwd(NULL, 0);
7932 error = got_error_from_errno("getcwd");
7936 error = got_repo_pack_fds_open(&pack_fds);
7940 error = got_worktree_open(&worktree, cwd);
7942 if (error->code == GOT_ERR_NOT_WORKTREE)
7943 error = wrap_not_worktree_error(error, "remove", cwd);
7947 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7952 error = apply_unveil(got_repo_get_path(repo), 1,
7953 got_worktree_get_root_path(worktree));
7957 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7964 TAILQ_FOREACH(pe, &paths, entry) {
7965 if (asprintf(&ondisk_path, "%s/%s",
7966 got_worktree_get_root_path(worktree),
7968 error = got_error_from_errno("asprintf");
7971 if (lstat(ondisk_path, &sb) == -1) {
7972 if (errno == ENOENT) {
7976 error = got_error_from_errno2("lstat",
7982 if (S_ISDIR(sb.st_mode)) {
7983 error = got_error_msg(GOT_ERR_BAD_PATH,
7984 "removing directories requires -R option");
7990 error = got_worktree_schedule_delete(worktree, &paths,
7991 delete_local_mods, status_codes, print_remove_status, NULL,
7992 repo, keep_on_disk, ignore_missing_paths);
7995 const struct got_error *close_err = got_repo_close(repo);
8000 got_worktree_close(worktree);
8002 const struct got_error *pack_err =
8003 got_repo_pack_fds_close(pack_fds);
8007 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
8015 fprintf(stderr, "usage: %s patch [-nR] [-c commit] [-p strip-count] "
8016 "[patchfile]\n", getprogname());
8020 static const struct got_error *
8021 patch_from_stdin(int *patchfd)
8023 const struct got_error *err = NULL;
8026 sig_t sighup, sigint, sigquit;
8028 *patchfd = got_opentempfd();
8030 return got_error_from_errno("got_opentempfd");
8032 sighup = signal(SIGHUP, SIG_DFL);
8033 sigint = signal(SIGINT, SIG_DFL);
8034 sigquit = signal(SIGQUIT, SIG_DFL);
8037 r = read(0, buf, sizeof(buf));
8039 err = got_error_from_errno("read");
8044 if (write(*patchfd, buf, r) == -1) {
8045 err = got_error_from_errno("write");
8050 signal(SIGHUP, sighup);
8051 signal(SIGINT, sigint);
8052 signal(SIGQUIT, sigquit);
8054 if (err == NULL && lseek(*patchfd, 0, SEEK_SET) == -1)
8055 err = got_error_from_errno("lseek");
8065 static const struct got_error *
8066 patch_progress(void *arg, const char *old, const char *new,
8067 unsigned char status, const struct got_error *error, int old_from,
8068 int old_lines, int new_from, int new_lines, int offset,
8069 int ws_mangled, const struct got_error *hunk_err)
8071 const char *path = new == NULL ? old : new;
8073 while (*path == '/')
8077 printf("%c %s\n", status, path);
8080 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
8082 if (offset != 0 || hunk_err != NULL || ws_mangled) {
8083 printf("@@ -%d,%d +%d,%d @@ ", old_from,
8084 old_lines, new_from, new_lines);
8085 if (hunk_err != NULL)
8086 printf("%s\n", hunk_err->msg);
8087 else if (offset != 0)
8088 printf("applied with offset %d\n", offset);
8090 printf("hunk contains mangled whitespace\n");
8096 static const struct got_error *
8097 cmd_patch(int argc, char *argv[])
8099 const struct got_error *error = NULL, *close_error = NULL;
8100 struct got_worktree *worktree = NULL;
8101 struct got_repository *repo = NULL;
8102 struct got_reflist_head refs;
8103 struct got_object_id *commit_id = NULL;
8104 const char *commit_id_str = NULL;
8108 int ch, nop = 0, strip = -1, reverse = 0;
8110 int *pack_fds = NULL;
8115 if (pledge("stdio rpath wpath cpath fattr proc exec sendfd flock "
8116 "unveil", NULL) == -1)
8120 while ((ch = getopt(argc, argv, "c:np:R")) != -1) {
8123 commit_id_str = optarg;
8129 strip = strtonum(optarg, 0, INT_MAX, &errstr);
8131 errx(1, "pathname strip count is %s: %s",
8147 error = patch_from_stdin(&patchfd);
8150 } else if (argc == 1) {
8151 patchfd = open(argv[0], O_RDONLY);
8152 if (patchfd == -1) {
8153 error = got_error_from_errno2("open", argv[0]);
8156 if (fstat(patchfd, &sb) == -1) {
8157 error = got_error_from_errno2("fstat", argv[0]);
8160 if (!S_ISREG(sb.st_mode)) {
8161 error = got_error_path(argv[0], GOT_ERR_BAD_FILETYPE);
8167 if ((cwd = getcwd(NULL, 0)) == NULL) {
8168 error = got_error_from_errno("getcwd");
8172 error = got_repo_pack_fds_open(&pack_fds);
8176 error = got_worktree_open(&worktree, cwd);
8180 const char *repo_path = got_worktree_get_repo_path(worktree);
8181 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8185 error = apply_unveil(got_repo_get_path(repo), 0,
8186 got_worktree_get_root_path(worktree));
8190 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
8194 if (commit_id_str != NULL) {
8195 error = got_repo_match_object_id(&commit_id, NULL,
8196 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
8201 error = got_patch(patchfd, worktree, repo, nop, strip, reverse,
8202 commit_id, &patch_progress, NULL, check_cancelled, NULL);
8205 got_ref_list_free(&refs);
8208 close_error = got_repo_close(repo);
8210 error = close_error;
8212 if (worktree != NULL) {
8213 close_error = got_worktree_close(worktree);
8215 error = close_error;
8218 const struct got_error *pack_err =
8219 got_repo_pack_fds_close(pack_fds);
8230 fprintf(stderr, "usage: %s revert [-pR] [-F response-script] path ...\n",
8235 static const struct got_error *
8236 revert_progress(void *arg, unsigned char status, const char *path)
8238 if (status == GOT_STATUS_UNVERSIONED)
8241 while (path[0] == '/')
8243 printf("%c %s\n", status, path);
8247 struct choose_patch_arg {
8248 FILE *patch_script_file;
8252 static const struct got_error *
8253 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
8254 int nchanges, const char *action)
8256 const struct got_error *err;
8258 size_t linesize = 0;
8262 case GOT_STATUS_ADD:
8263 printf("A %s\n%s this addition? [y/n] ", path, action);
8265 case GOT_STATUS_DELETE:
8266 printf("D %s\n%s this deletion? [y/n] ", path, action);
8268 case GOT_STATUS_MODIFY:
8269 if (fseek(patch_file, 0L, SEEK_SET) == -1)
8270 return got_error_from_errno("fseek");
8271 printf(GOT_COMMIT_SEP_STR);
8272 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
8274 if (linelen == -1 && ferror(patch_file)) {
8275 err = got_error_from_errno("getline");
8280 printf(GOT_COMMIT_SEP_STR);
8281 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
8282 path, n, nchanges, action);
8285 return got_error_path(path, GOT_ERR_FILE_STATUS);
8292 static const struct got_error *
8293 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
8294 FILE *patch_file, int n, int nchanges)
8296 const struct got_error *err = NULL;
8298 size_t linesize = 0;
8301 struct choose_patch_arg *a = arg;
8303 *choice = GOT_PATCH_CHOICE_NONE;
8305 if (a->patch_script_file) {
8307 err = show_change(status, path, patch_file, n, nchanges,
8311 linelen = getline(&line, &linesize, a->patch_script_file);
8312 if (linelen == -1) {
8313 if (ferror(a->patch_script_file))
8314 return got_error_from_errno("getline");
8317 nl = strchr(line, '\n');
8320 if (strcmp(line, "y") == 0) {
8321 *choice = GOT_PATCH_CHOICE_YES;
8323 } else if (strcmp(line, "n") == 0) {
8324 *choice = GOT_PATCH_CHOICE_NO;
8326 } else if (strcmp(line, "q") == 0 &&
8327 status == GOT_STATUS_MODIFY) {
8328 *choice = GOT_PATCH_CHOICE_QUIT;
8331 printf("invalid response '%s'\n", line);
8336 while (resp != 'y' && resp != 'n' && resp != 'q') {
8337 err = show_change(status, path, patch_file, n, nchanges,
8344 if (status == GOT_STATUS_MODIFY) {
8345 if (resp != 'y' && resp != 'n' && resp != 'q') {
8346 printf("invalid response '%c'\n", resp);
8349 } else if (resp != 'y' && resp != 'n') {
8350 printf("invalid response '%c'\n", resp);
8356 *choice = GOT_PATCH_CHOICE_YES;
8357 else if (resp == 'n')
8358 *choice = GOT_PATCH_CHOICE_NO;
8359 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
8360 *choice = GOT_PATCH_CHOICE_QUIT;
8365 static const struct got_error *
8366 cmd_revert(int argc, char *argv[])
8368 const struct got_error *error = NULL;
8369 struct got_worktree *worktree = NULL;
8370 struct got_repository *repo = NULL;
8371 char *cwd = NULL, *path = NULL;
8372 struct got_pathlist_head paths;
8373 struct got_pathlist_entry *pe;
8374 int ch, can_recurse = 0, pflag = 0;
8375 FILE *patch_script_file = NULL;
8376 const char *patch_script_path = NULL;
8377 struct choose_patch_arg cpa;
8378 int *pack_fds = NULL;
8382 while ((ch = getopt(argc, argv, "F:pR")) != -1) {
8385 patch_script_path = optarg;
8403 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8404 "unveil", NULL) == -1)
8409 if (patch_script_path && !pflag)
8410 errx(1, "-F option can only be used together with -p option");
8412 cwd = getcwd(NULL, 0);
8414 error = got_error_from_errno("getcwd");
8418 error = got_repo_pack_fds_open(&pack_fds);
8422 error = got_worktree_open(&worktree, cwd);
8424 if (error->code == GOT_ERR_NOT_WORKTREE)
8425 error = wrap_not_worktree_error(error, "revert", cwd);
8429 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8434 if (patch_script_path) {
8435 patch_script_file = fopen(patch_script_path, "re");
8436 if (patch_script_file == NULL) {
8437 error = got_error_from_errno2("fopen",
8442 error = apply_unveil(got_repo_get_path(repo), 1,
8443 got_worktree_get_root_path(worktree));
8447 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8454 TAILQ_FOREACH(pe, &paths, entry) {
8455 if (asprintf(&ondisk_path, "%s/%s",
8456 got_worktree_get_root_path(worktree),
8458 error = got_error_from_errno("asprintf");
8461 if (lstat(ondisk_path, &sb) == -1) {
8462 if (errno == ENOENT) {
8466 error = got_error_from_errno2("lstat",
8472 if (S_ISDIR(sb.st_mode)) {
8473 error = got_error_msg(GOT_ERR_BAD_PATH,
8474 "reverting directories requires -R option");
8480 cpa.patch_script_file = patch_script_file;
8481 cpa.action = "revert";
8482 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
8483 pflag ? choose_patch : NULL, &cpa, repo);
8485 if (patch_script_file && fclose(patch_script_file) == EOF &&
8487 error = got_error_from_errno2("fclose", patch_script_path);
8489 const struct got_error *close_err = got_repo_close(repo);
8494 got_worktree_close(worktree);
8496 const struct got_error *pack_err =
8497 got_repo_pack_fds_close(pack_fds);
8509 fprintf(stderr, "usage: %s commit [-NS] [-A author] [-F path] "
8510 "[-m message] [path ...]\n", getprogname());
8514 struct collect_commit_logmsg_arg {
8515 const char *cmdline_log;
8516 const char *prepared_log;
8517 int non_interactive;
8519 const char *worktree_path;
8520 const char *branch_name;
8521 const char *repo_path;
8526 static const struct got_error *
8527 read_prepared_logmsg(char **logmsg, const char *path)
8529 const struct got_error *err = NULL;
8535 memset(&sb, 0, sizeof(sb));
8537 f = fopen(path, "re");
8539 return got_error_from_errno2("fopen", path);
8541 if (fstat(fileno(f), &sb) == -1) {
8542 err = got_error_from_errno2("fstat", path);
8545 if (sb.st_size == 0) {
8546 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
8550 *logmsg = malloc(sb.st_size + 1);
8551 if (*logmsg == NULL) {
8552 err = got_error_from_errno("malloc");
8556 r = fread(*logmsg, 1, sb.st_size, f);
8557 if (r != sb.st_size) {
8559 err = got_error_from_errno2("fread", path);
8561 err = got_error(GOT_ERR_IO);
8564 (*logmsg)[sb.st_size] = '\0';
8566 if (fclose(f) == EOF && err == NULL)
8567 err = got_error_from_errno2("fclose", path);
8575 static const struct got_error *
8576 collect_commit_logmsg(struct got_pathlist_head *commitable_paths,
8577 const char *diff_path, char **logmsg, void *arg)
8579 char *initial_content = NULL;
8580 struct got_pathlist_entry *pe;
8581 const struct got_error *err = NULL;
8582 char *template = NULL;
8583 struct collect_commit_logmsg_arg *a = arg;
8584 int initial_content_len;
8588 /* if a message was specified on the command line, just use it */
8589 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
8590 len = strlen(a->cmdline_log) + 1;
8591 *logmsg = malloc(len + 1);
8592 if (*logmsg == NULL)
8593 return got_error_from_errno("malloc");
8594 strlcpy(*logmsg, a->cmdline_log, len);
8596 } else if (a->prepared_log != NULL && a->non_interactive)
8597 return read_prepared_logmsg(logmsg, a->prepared_log);
8599 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
8600 return got_error_from_errno("asprintf");
8602 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template, "");
8606 if (a->prepared_log) {
8608 err = read_prepared_logmsg(&msg, a->prepared_log);
8611 if (write(fd, msg, strlen(msg)) == -1) {
8612 err = got_error_from_errno2("write", a->logmsg_path);
8619 initial_content_len = asprintf(&initial_content,
8620 "\n# changes to be committed on branch %s:\n",
8622 if (initial_content_len == -1) {
8623 err = got_error_from_errno("asprintf");
8627 if (write(fd, initial_content, initial_content_len) == -1) {
8628 err = got_error_from_errno2("write", a->logmsg_path);
8632 TAILQ_FOREACH(pe, commitable_paths, entry) {
8633 struct got_commitable *ct = pe->data;
8634 dprintf(fd, "# %c %s\n",
8635 got_commitable_get_status(ct),
8636 got_commitable_get_path(ct));
8640 dprintf(fd, "# detailed changes can be viewed in %s\n",
8644 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content,
8645 initial_content_len, a->prepared_log ? 0 : 1);
8647 free(initial_content);
8650 if (fd != -1 && close(fd) == -1 && err == NULL)
8651 err = got_error_from_errno2("close", a->logmsg_path);
8653 /* Editor is done; we can now apply unveil(2) */
8655 err = apply_unveil(a->repo_path, 0, a->worktree_path);
8663 static const struct got_error *
8664 cmd_commit(int argc, char *argv[])
8666 const struct got_error *error = NULL;
8667 struct got_worktree *worktree = NULL;
8668 struct got_repository *repo = NULL;
8669 char *cwd = NULL, *id_str = NULL;
8670 struct got_object_id *id = NULL;
8671 const char *logmsg = NULL;
8672 char *prepared_logmsg = NULL;
8673 struct collect_commit_logmsg_arg cl_arg;
8674 const char *author = NULL;
8675 char *gitconfig_path = NULL, *editor = NULL, *committer = NULL;
8676 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
8677 int allow_bad_symlinks = 0, non_interactive = 0, merge_in_progress = 0;
8679 struct got_pathlist_head paths;
8680 int *pack_fds = NULL;
8683 cl_arg.logmsg_path = NULL;
8685 while ((ch = getopt(argc, argv, "A:F:m:NnS")) != -1) {
8689 error = valid_author(author);
8695 option_conflict('F', 'm');
8696 prepared_logmsg = realpath(optarg, NULL);
8697 if (prepared_logmsg == NULL)
8698 return got_error_from_errno2("realpath",
8702 if (prepared_logmsg)
8703 option_conflict('m', 'F');
8707 non_interactive = 1;
8713 allow_bad_symlinks = 1;
8725 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8726 "unveil", NULL) == -1)
8729 cwd = getcwd(NULL, 0);
8731 error = got_error_from_errno("getcwd");
8735 error = got_repo_pack_fds_open(&pack_fds);
8739 error = got_worktree_open(&worktree, cwd);
8741 if (error->code == GOT_ERR_NOT_WORKTREE)
8742 error = wrap_not_worktree_error(error, "commit", cwd);
8746 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
8749 if (rebase_in_progress) {
8750 error = got_error(GOT_ERR_REBASING);
8754 error = got_worktree_histedit_in_progress(&histedit_in_progress,
8759 error = get_gitconfig_path(&gitconfig_path);
8762 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8763 gitconfig_path, pack_fds);
8767 error = got_worktree_merge_in_progress(&merge_in_progress, worktree, repo);
8770 if (merge_in_progress) {
8771 error = got_error(GOT_ERR_MERGE_BUSY);
8775 error = get_author(&committer, repo, worktree);
8779 if (author != NULL && !strcmp(committer, author)) {
8780 error = got_error(GOT_ERR_COMMIT_REDUNDANT_AUTHOR);
8788 * unveil(2) traverses exec(2); if an editor is used we have
8789 * to apply unveil after the log message has been written.
8791 if (logmsg == NULL || strlen(logmsg) == 0)
8792 error = get_editor(&editor);
8794 error = apply_unveil(got_repo_get_path(repo), 0,
8795 got_worktree_get_root_path(worktree));
8799 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8803 cl_arg.editor = editor;
8804 cl_arg.cmdline_log = logmsg;
8805 cl_arg.prepared_log = prepared_logmsg;
8806 cl_arg.non_interactive = non_interactive;
8807 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
8808 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
8809 if (!histedit_in_progress) {
8810 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
8811 error = got_error(GOT_ERR_COMMIT_BRANCH);
8814 cl_arg.branch_name += 11;
8816 cl_arg.repo_path = got_repo_get_path(repo);
8817 error = got_worktree_commit(&id, worktree, &paths, author, committer,
8818 allow_bad_symlinks, show_diff, collect_commit_logmsg, &cl_arg,
8819 print_status, NULL, repo);
8821 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
8822 cl_arg.logmsg_path != NULL)
8823 preserve_logmsg = 1;
8827 error = got_object_id_str(&id_str, id);
8830 printf("Created commit %s\n", id_str);
8832 if (preserve_logmsg) {
8833 fprintf(stderr, "%s: log message preserved in %s\n",
8834 getprogname(), cl_arg.logmsg_path);
8835 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
8837 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
8838 free(cl_arg.logmsg_path);
8840 const struct got_error *close_err = got_repo_close(repo);
8845 got_worktree_close(worktree);
8847 const struct got_error *pack_err =
8848 got_repo_pack_fds_close(pack_fds);
8854 free(gitconfig_path);
8857 free(prepared_logmsg);
8864 fprintf(stderr, "usage: %s send [-afqTv] [-b branch] [-d branch] "
8865 "[-r repository-path] [-t tag] [remote-repository]\n",
8871 print_load_info(int print_colored, int print_found, int print_trees,
8872 int ncolored, int nfound, int ntrees)
8874 if (print_colored) {
8875 printf("%d commit%s colored", ncolored,
8876 ncolored == 1 ? "" : "s");
8879 printf("%s%d object%s found",
8880 ncolored > 0 ? "; " : "",
8881 nfound, nfound == 1 ? "" : "s");
8884 printf("; %d tree%s scanned", ntrees,
8885 ntrees == 1 ? "" : "s");
8889 struct got_send_progress_arg {
8890 char last_scaled_packsize[FMT_SCALED_STRSIZE];
8897 int last_nobj_total;
8901 int printed_something;
8903 struct got_pathlist_head *delete_branches;
8906 static const struct got_error *
8907 send_progress(void *arg, int ncolored, int nfound, int ntrees,
8908 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
8909 int nobj_written, off_t bytes_sent, const char *refname,
8910 const char *errmsg, int success)
8912 struct got_send_progress_arg *a = arg;
8913 char scaled_packsize[FMT_SCALED_STRSIZE];
8914 char scaled_sent[FMT_SCALED_STRSIZE];
8915 int p_deltify = 0, p_written = 0, p_sent = 0;
8916 int print_colored = 0, print_found = 0, print_trees = 0;
8917 int print_searching = 0, print_total = 0;
8918 int print_deltify = 0, print_written = 0, print_sent = 0;
8920 if (a->verbosity < 0)
8924 const char *status = success ? "accepted" : "rejected";
8927 struct got_pathlist_entry *pe;
8928 TAILQ_FOREACH(pe, a->delete_branches, entry) {
8929 const char *branchname = pe->path;
8930 if (got_path_cmp(branchname, refname,
8931 strlen(branchname), strlen(refname)) == 0) {
8933 a->sent_something = 1;
8939 if (a->printed_something)
8941 printf("Server has %s %s", status, refname);
8943 printf(": %s", errmsg);
8944 a->printed_something = 1;
8948 if (a->last_ncolored != ncolored) {
8950 a->last_ncolored = ncolored;
8953 if (a->last_nfound != nfound) {
8956 a->last_nfound = nfound;
8959 if (a->last_ntrees != ntrees) {
8963 a->last_ntrees = ntrees;
8966 if ((print_colored || print_found || print_trees) &&
8969 print_load_info(print_colored, print_found, print_trees,
8970 ncolored, nfound, ntrees);
8971 a->printed_something = 1;
8974 } else if (!a->loading_done) {
8976 print_load_info(1, 1, 1, ncolored, nfound, ntrees);
8978 a->loading_done = 1;
8981 if (fmt_scaled(packfile_size, scaled_packsize) == -1)
8982 return got_error_from_errno("fmt_scaled");
8983 if (fmt_scaled(bytes_sent, scaled_sent) == -1)
8984 return got_error_from_errno("fmt_scaled");
8986 if (a->last_ncommits != ncommits) {
8987 print_searching = 1;
8988 a->last_ncommits = ncommits;
8991 if (a->last_nobj_total != nobj_total) {
8992 print_searching = 1;
8994 a->last_nobj_total = nobj_total;
8997 if (packfile_size > 0 && (a->last_scaled_packsize[0] == '\0' ||
8998 strcmp(scaled_packsize, a->last_scaled_packsize)) != 0) {
8999 if (strlcpy(a->last_scaled_packsize, scaled_packsize,
9000 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
9001 return got_error(GOT_ERR_NO_SPACE);
9004 if (nobj_deltify > 0 || nobj_written > 0) {
9005 if (nobj_deltify > 0) {
9006 p_deltify = (nobj_deltify * 100) / nobj_total;
9007 if (p_deltify != a->last_p_deltify) {
9008 a->last_p_deltify = p_deltify;
9009 print_searching = 1;
9014 if (nobj_written > 0) {
9015 p_written = (nobj_written * 100) / nobj_total;
9016 if (p_written != a->last_p_written) {
9017 a->last_p_written = p_written;
9018 print_searching = 1;
9026 if (bytes_sent > 0) {
9027 p_sent = (bytes_sent * 100) / packfile_size;
9028 if (p_sent != a->last_p_sent) {
9029 a->last_p_sent = p_sent;
9030 print_searching = 1;
9036 a->sent_something = 1;
9039 if (print_searching || print_total || print_deltify || print_written ||
9042 if (print_searching)
9043 printf("packing %d reference%s", ncommits,
9044 ncommits == 1 ? "" : "s");
9046 printf("; %d object%s", nobj_total,
9047 nobj_total == 1 ? "" : "s");
9049 printf("; deltify: %d%%", p_deltify);
9051 printf("; uploading pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
9052 scaled_packsize, p_sent);
9053 else if (print_written)
9054 printf("; writing pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
9055 scaled_packsize, p_written);
9056 if (print_searching || print_total || print_deltify ||
9057 print_written || print_sent) {
9058 a->printed_something = 1;
9064 static const struct got_error *
9065 cmd_send(int argc, char *argv[])
9067 const struct got_error *error = NULL;
9068 char *cwd = NULL, *repo_path = NULL;
9069 const char *remote_name;
9070 char *proto = NULL, *host = NULL, *port = NULL;
9071 char *repo_name = NULL, *server_path = NULL;
9072 const struct got_remote_repo *remotes, *remote = NULL;
9073 int nremotes, nbranches = 0, ndelete_branches = 0;
9074 struct got_repository *repo = NULL;
9075 struct got_worktree *worktree = NULL;
9076 const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
9077 struct got_pathlist_head branches;
9078 struct got_pathlist_head tags;
9079 struct got_reflist_head all_branches;
9080 struct got_reflist_head all_tags;
9081 struct got_pathlist_head delete_args;
9082 struct got_pathlist_head delete_branches;
9083 struct got_reflist_entry *re;
9084 struct got_pathlist_entry *pe;
9085 int i, ch, sendfd = -1, sendstatus;
9087 struct got_send_progress_arg spa;
9088 int verbosity = 0, overwrite_refs = 0;
9089 int send_all_branches = 0, send_all_tags = 0;
9090 struct got_reference *ref = NULL;
9091 int *pack_fds = NULL;
9093 TAILQ_INIT(&branches);
9095 TAILQ_INIT(&all_branches);
9096 TAILQ_INIT(&all_tags);
9097 TAILQ_INIT(&delete_args);
9098 TAILQ_INIT(&delete_branches);
9100 while ((ch = getopt(argc, argv, "ab:d:fqr:Tt:v")) != -1) {
9103 send_all_branches = 1;
9106 error = got_pathlist_append(&branches, optarg, NULL);
9112 error = got_pathlist_append(&delete_args, optarg, NULL);
9123 repo_path = realpath(optarg, NULL);
9124 if (repo_path == NULL)
9125 return got_error_from_errno2("realpath",
9127 got_path_strip_trailing_slashes(repo_path);
9133 error = got_pathlist_append(&tags, optarg, NULL);
9140 else if (verbosity < 3)
9151 if (send_all_branches && !TAILQ_EMPTY(&branches))
9152 option_conflict('a', 'b');
9153 if (send_all_tags && !TAILQ_EMPTY(&tags))
9154 option_conflict('T', 't');
9158 remote_name = GOT_SEND_DEFAULT_REMOTE_NAME;
9160 remote_name = argv[0];
9164 cwd = getcwd(NULL, 0);
9166 error = got_error_from_errno("getcwd");
9170 error = got_repo_pack_fds_open(&pack_fds);
9174 if (repo_path == NULL) {
9175 error = got_worktree_open(&worktree, cwd);
9176 if (error && error->code != GOT_ERR_NOT_WORKTREE)
9182 strdup(got_worktree_get_repo_path(worktree));
9183 if (repo_path == NULL)
9184 error = got_error_from_errno("strdup");
9188 repo_path = strdup(cwd);
9189 if (repo_path == NULL) {
9190 error = got_error_from_errno("strdup");
9196 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
9201 worktree_conf = got_worktree_get_gotconfig(worktree);
9202 if (worktree_conf) {
9203 got_gotconfig_get_remotes(&nremotes, &remotes,
9205 for (i = 0; i < nremotes; i++) {
9206 if (strcmp(remotes[i].name, remote_name) == 0) {
9207 remote = &remotes[i];
9213 if (remote == NULL) {
9214 repo_conf = got_repo_get_gotconfig(repo);
9216 got_gotconfig_get_remotes(&nremotes, &remotes,
9218 for (i = 0; i < nremotes; i++) {
9219 if (strcmp(remotes[i].name, remote_name) == 0) {
9220 remote = &remotes[i];
9226 if (remote == NULL) {
9227 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
9228 for (i = 0; i < nremotes; i++) {
9229 if (strcmp(remotes[i].name, remote_name) == 0) {
9230 remote = &remotes[i];
9235 if (remote == NULL) {
9236 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
9240 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
9241 &repo_name, remote->send_url);
9245 if (strcmp(proto, "git") == 0) {
9247 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
9248 "sendfd dns inet unveil", NULL) == -1)
9251 } else if (strcmp(proto, "git+ssh") == 0 ||
9252 strcmp(proto, "ssh") == 0) {
9254 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
9255 "sendfd unveil", NULL) == -1)
9258 } else if (strcmp(proto, "http") == 0 ||
9259 strcmp(proto, "git+http") == 0) {
9260 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
9263 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
9267 error = got_dial_apply_unveil(proto);
9271 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
9275 if (send_all_branches) {
9276 error = got_ref_list(&all_branches, repo, "refs/heads",
9277 got_ref_cmp_by_name, NULL);
9280 TAILQ_FOREACH(re, &all_branches, entry) {
9281 const char *branchname = got_ref_get_name(re->ref);
9282 error = got_pathlist_append(&branches,
9288 } else if (nbranches == 0) {
9289 for (i = 0; i < remote->nsend_branches; i++) {
9290 got_pathlist_append(&branches,
9291 remote->send_branches[i], NULL);
9295 if (send_all_tags) {
9296 error = got_ref_list(&all_tags, repo, "refs/tags",
9297 got_ref_cmp_by_name, NULL);
9300 TAILQ_FOREACH(re, &all_tags, entry) {
9301 const char *tagname = got_ref_get_name(re->ref);
9302 error = got_pathlist_append(&tags,
9310 * To prevent accidents only branches in refs/heads/ can be deleted
9311 * with 'got send -d'.
9312 * Deleting anything else requires local repository access or Git.
9314 TAILQ_FOREACH(pe, &delete_args, entry) {
9315 const char *branchname = pe->path;
9317 struct got_pathlist_entry *new;
9318 if (strncmp(branchname, "refs/heads/", 11) == 0) {
9319 s = strdup(branchname);
9321 error = got_error_from_errno("strdup");
9325 if (asprintf(&s, "refs/heads/%s", branchname) == -1) {
9326 error = got_error_from_errno("asprintf");
9330 error = got_pathlist_insert(&new, &delete_branches, s, NULL);
9331 if (error || new == NULL /* duplicate */)
9338 if (nbranches == 0 && ndelete_branches == 0) {
9339 struct got_reference *head_ref;
9341 error = got_ref_open(&head_ref, repo,
9342 got_worktree_get_head_ref_name(worktree), 0);
9344 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
9347 if (got_ref_is_symbolic(head_ref)) {
9348 error = got_ref_resolve_symbolic(&ref, repo, head_ref);
9349 got_ref_close(head_ref);
9354 error = got_pathlist_append(&branches, got_ref_get_name(ref),
9361 if (verbosity >= 0) {
9362 printf("Connecting to \"%s\" %s://%s%s%s%s%s\n",
9363 remote->name, proto, host,
9364 port ? ":" : "", port ? port : "",
9365 *server_path == '/' ? "" : "/", server_path);
9368 error = got_send_connect(&sendpid, &sendfd, proto, host, port,
9369 server_path, verbosity);
9373 memset(&spa, 0, sizeof(spa));
9374 spa.last_scaled_packsize[0] = '\0';
9375 spa.last_p_deltify = -1;
9376 spa.last_p_written = -1;
9377 spa.verbosity = verbosity;
9378 spa.delete_branches = &delete_branches;
9379 error = got_send_pack(remote_name, &branches, &tags, &delete_branches,
9380 verbosity, overwrite_refs, sendfd, repo, send_progress, &spa,
9381 check_cancelled, NULL);
9382 if (spa.printed_something)
9386 if (!spa.sent_something && verbosity >= 0)
9387 printf("Already up-to-date\n");
9390 if (kill(sendpid, SIGTERM) == -1)
9391 error = got_error_from_errno("kill");
9392 if (waitpid(sendpid, &sendstatus, 0) == -1 && error == NULL)
9393 error = got_error_from_errno("waitpid");
9395 if (sendfd != -1 && close(sendfd) == -1 && error == NULL)
9396 error = got_error_from_errno("close");
9398 const struct got_error *close_err = got_repo_close(repo);
9403 got_worktree_close(worktree);
9405 const struct got_error *pack_err =
9406 got_repo_pack_fds_close(pack_fds);
9412 got_pathlist_free(&branches, GOT_PATHLIST_FREE_NONE);
9413 got_pathlist_free(&tags, GOT_PATHLIST_FREE_NONE);
9414 got_ref_list_free(&all_branches);
9415 got_ref_list_free(&all_tags);
9416 got_pathlist_free(&delete_args, GOT_PATHLIST_FREE_NONE);
9417 got_pathlist_free(&delete_branches, GOT_PATHLIST_FREE_PATH);
9429 usage_cherrypick(void)
9431 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
9435 static const struct got_error *
9436 cmd_cherrypick(int argc, char *argv[])
9438 const struct got_error *error = NULL;
9439 struct got_worktree *worktree = NULL;
9440 struct got_repository *repo = NULL;
9441 char *cwd = NULL, *commit_id_str = NULL;
9442 struct got_object_id *commit_id = NULL;
9443 struct got_commit_object *commit = NULL;
9444 struct got_object_qid *pid;
9446 struct got_update_progress_arg upa;
9447 int *pack_fds = NULL;
9449 while ((ch = getopt(argc, argv, "")) != -1) {
9461 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9462 "unveil", NULL) == -1)
9468 cwd = getcwd(NULL, 0);
9470 error = got_error_from_errno("getcwd");
9474 error = got_repo_pack_fds_open(&pack_fds);
9478 error = got_worktree_open(&worktree, cwd);
9480 if (error->code == GOT_ERR_NOT_WORKTREE)
9481 error = wrap_not_worktree_error(error, "cherrypick",
9486 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9491 error = apply_unveil(got_repo_get_path(repo), 0,
9492 got_worktree_get_root_path(worktree));
9496 error = got_repo_match_object_id(&commit_id, NULL, argv[0],
9497 GOT_OBJ_TYPE_COMMIT, NULL, repo);
9500 error = got_object_id_str(&commit_id_str, commit_id);
9504 error = got_object_open_as_commit(&commit, repo, commit_id);
9507 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
9508 memset(&upa, 0, sizeof(upa));
9509 error = got_worktree_merge_files(worktree, pid ? &pid->id : NULL,
9510 commit_id, repo, update_progress, &upa, check_cancelled,
9515 if (upa.did_something)
9516 printf("Merged commit %s\n", commit_id_str);
9517 print_merge_progress_stats(&upa);
9520 got_object_commit_close(commit);
9521 free(commit_id_str);
9523 got_worktree_close(worktree);
9525 const struct got_error *close_err = got_repo_close(repo);
9530 const struct got_error *pack_err =
9531 got_repo_pack_fds_close(pack_fds);
9542 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
9546 static const struct got_error *
9547 cmd_backout(int argc, char *argv[])
9549 const struct got_error *error = NULL;
9550 struct got_worktree *worktree = NULL;
9551 struct got_repository *repo = NULL;
9552 char *cwd = NULL, *commit_id_str = NULL;
9553 struct got_object_id *commit_id = NULL;
9554 struct got_commit_object *commit = NULL;
9555 struct got_object_qid *pid;
9557 struct got_update_progress_arg upa;
9558 int *pack_fds = NULL;
9560 while ((ch = getopt(argc, argv, "")) != -1) {
9572 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9573 "unveil", NULL) == -1)
9579 cwd = getcwd(NULL, 0);
9581 error = got_error_from_errno("getcwd");
9585 error = got_repo_pack_fds_open(&pack_fds);
9589 error = got_worktree_open(&worktree, cwd);
9591 if (error->code == GOT_ERR_NOT_WORKTREE)
9592 error = wrap_not_worktree_error(error, "backout", cwd);
9596 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9601 error = apply_unveil(got_repo_get_path(repo), 0,
9602 got_worktree_get_root_path(worktree));
9606 error = got_repo_match_object_id(&commit_id, NULL, argv[0],
9607 GOT_OBJ_TYPE_COMMIT, NULL, repo);
9610 error = got_object_id_str(&commit_id_str, commit_id);
9614 error = got_object_open_as_commit(&commit, repo, commit_id);
9617 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
9619 error = got_error(GOT_ERR_ROOT_COMMIT);
9623 memset(&upa, 0, sizeof(upa));
9624 error = got_worktree_merge_files(worktree, commit_id, &pid->id,
9625 repo, update_progress, &upa, check_cancelled, NULL);
9629 if (upa.did_something)
9630 printf("Backed out commit %s\n", commit_id_str);
9631 print_merge_progress_stats(&upa);
9634 got_object_commit_close(commit);
9635 free(commit_id_str);
9637 got_worktree_close(worktree);
9639 const struct got_error *close_err = got_repo_close(repo);
9644 const struct got_error *pack_err =
9645 got_repo_pack_fds_close(pack_fds);
9655 fprintf(stderr, "usage: %s rebase [-aclX] [branch]\n", getprogname());
9660 trim_logmsg(char *logmsg, int limit)
9665 len = strlen(logmsg);
9669 nl = strchr(logmsg, '\n');
9674 static const struct got_error *
9675 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
9677 const struct got_error *err;
9678 char *logmsg0 = NULL;
9681 err = got_object_commit_get_logmsg(&logmsg0, commit);
9686 while (isspace((unsigned char)s[0]))
9689 *logmsg = strdup(s);
9690 if (*logmsg == NULL) {
9691 err = got_error_from_errno("strdup");
9695 trim_logmsg(*logmsg, limit);
9701 static const struct got_error *
9702 show_rebase_merge_conflict(struct got_object_id *id,
9703 struct got_repository *repo)
9705 const struct got_error *err;
9706 struct got_commit_object *commit = NULL;
9707 char *id_str = NULL, *logmsg = NULL;
9709 err = got_object_open_as_commit(&commit, repo, id);
9713 err = got_object_id_str(&id_str, id);
9719 err = get_short_logmsg(&logmsg, 42, commit);
9723 printf("%s -> merge conflict: %s\n", id_str, logmsg);
9726 got_object_commit_close(commit);
9731 static const struct got_error *
9732 show_rebase_progress(struct got_commit_object *commit,
9733 struct got_object_id *old_id, struct got_object_id *new_id)
9735 const struct got_error *err;
9736 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
9738 err = got_object_id_str(&old_id_str, old_id);
9743 err = got_object_id_str(&new_id_str, new_id);
9748 old_id_str[12] = '\0';
9750 new_id_str[12] = '\0';
9752 err = get_short_logmsg(&logmsg, 42, commit);
9756 printf("%s -> %s: %s\n", old_id_str,
9757 new_id_str ? new_id_str : "no-op change", logmsg);
9765 static const struct got_error *
9766 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
9767 struct got_reference *branch, struct got_reference *new_base_branch,
9768 struct got_reference *tmp_branch, struct got_repository *repo,
9771 printf("Switching work tree to %s\n", got_ref_get_name(branch));
9772 return got_worktree_rebase_complete(worktree, fileindex,
9773 new_base_branch, tmp_branch, branch, repo, create_backup);
9776 static const struct got_error *
9777 rebase_commit(struct got_pathlist_head *merged_paths,
9778 struct got_worktree *worktree, struct got_fileindex *fileindex,
9779 struct got_reference *tmp_branch, const char *committer,
9780 struct got_object_id *commit_id, struct got_repository *repo)
9782 const struct got_error *error;
9783 struct got_commit_object *commit;
9784 struct got_object_id *new_commit_id;
9786 error = got_object_open_as_commit(&commit, repo, commit_id);
9790 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
9791 worktree, fileindex, tmp_branch, committer, commit, commit_id,
9794 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
9796 error = show_rebase_progress(commit, commit_id, NULL);
9798 error = show_rebase_progress(commit, commit_id, new_commit_id);
9799 free(new_commit_id);
9802 got_object_commit_close(commit);
9806 struct check_path_prefix_arg {
9807 const char *path_prefix;
9812 static const struct got_error *
9813 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
9814 struct got_blob_object *blob2, FILE *f1, FILE *f2,
9815 struct got_object_id *id1, struct got_object_id *id2,
9816 const char *path1, const char *path2,
9817 mode_t mode1, mode_t mode2, struct got_repository *repo)
9819 struct check_path_prefix_arg *a = arg;
9821 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
9822 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
9823 return got_error(a->errcode);
9828 static const struct got_error *
9829 check_path_prefix(struct got_object_id *parent_id,
9830 struct got_object_id *commit_id, const char *path_prefix,
9831 int errcode, struct got_repository *repo)
9833 const struct got_error *err;
9834 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
9835 struct got_commit_object *commit = NULL, *parent_commit = NULL;
9836 struct check_path_prefix_arg cpp_arg;
9838 if (got_path_is_root_dir(path_prefix))
9841 err = got_object_open_as_commit(&commit, repo, commit_id);
9845 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
9849 err = got_object_open_as_tree(&tree1, repo,
9850 got_object_commit_get_tree_id(parent_commit));
9854 err = got_object_open_as_tree(&tree2, repo,
9855 got_object_commit_get_tree_id(commit));
9859 cpp_arg.path_prefix = path_prefix;
9860 while (cpp_arg.path_prefix[0] == '/')
9861 cpp_arg.path_prefix++;
9862 cpp_arg.len = strlen(cpp_arg.path_prefix);
9863 cpp_arg.errcode = errcode;
9864 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
9865 check_path_prefix_in_diff, &cpp_arg, 0);
9868 got_object_tree_close(tree1);
9870 got_object_tree_close(tree2);
9872 got_object_commit_close(commit);
9874 got_object_commit_close(parent_commit);
9878 static const struct got_error *
9879 collect_commits(struct got_object_id_queue *commits,
9880 struct got_object_id *initial_commit_id,
9881 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
9882 const char *path_prefix, int path_prefix_errcode,
9883 struct got_repository *repo)
9885 const struct got_error *err = NULL;
9886 struct got_commit_graph *graph = NULL;
9887 struct got_object_id parent_id, commit_id;
9888 struct got_object_qid *qid;
9890 err = got_commit_graph_open(&graph, "/", 1);
9894 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
9895 check_cancelled, NULL);
9899 memcpy(&commit_id, initial_commit_id, sizeof(commit_id));
9900 while (got_object_id_cmp(&commit_id, iter_stop_id) != 0) {
9901 err = got_commit_graph_iter_next(&parent_id, graph, repo,
9902 check_cancelled, NULL);
9904 if (err->code == GOT_ERR_ITER_COMPLETED) {
9905 err = got_error_msg(GOT_ERR_ANCESTRY,
9906 "ran out of commits to rebase before "
9907 "youngest common ancestor commit has "
9912 err = check_path_prefix(&parent_id, &commit_id,
9913 path_prefix, path_prefix_errcode, repo);
9917 err = got_object_qid_alloc(&qid, &commit_id);
9920 STAILQ_INSERT_HEAD(commits, qid, entry);
9922 memcpy(&commit_id, &parent_id, sizeof(commit_id));
9926 got_commit_graph_close(graph);
9930 static const struct got_error *
9931 get_commit_brief_str(char **brief_str, struct got_commit_object *commit)
9933 const struct got_error *err = NULL;
9934 time_t committer_time;
9936 char datebuf[11]; /* YYYY-MM-DD + NUL */
9937 char *author0 = NULL, *author, *smallerthan;
9938 char *logmsg0 = NULL, *logmsg, *newline;
9940 committer_time = got_object_commit_get_committer_time(commit);
9941 if (gmtime_r(&committer_time, &tm) == NULL)
9942 return got_error_from_errno("gmtime_r");
9943 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d", &tm) == 0)
9944 return got_error(GOT_ERR_NO_SPACE);
9946 author0 = strdup(got_object_commit_get_author(commit));
9947 if (author0 == NULL)
9948 return got_error_from_errno("strdup");
9950 smallerthan = strchr(author, '<');
9951 if (smallerthan && smallerthan[1] != '\0')
9952 author = smallerthan + 1;
9953 author[strcspn(author, "@>")] = '\0';
9955 err = got_object_commit_get_logmsg(&logmsg0, commit);
9959 while (*logmsg == '\n')
9961 newline = strchr(logmsg, '\n');
9965 if (asprintf(brief_str, "%s %s %s",
9966 datebuf, author, logmsg) == -1)
9967 err = got_error_from_errno("asprintf");
9974 static const struct got_error *
9975 delete_backup_ref(struct got_reference *ref, struct got_object_id *id,
9976 struct got_repository *repo)
9978 const struct got_error *err;
9981 err = got_object_id_str(&id_str, id);
9985 err = got_ref_delete(ref, repo);
9989 printf("Deleted %s: %s\n", got_ref_get_name(ref), id_str);
9995 static const struct got_error *
9996 print_backup_ref(const char *branch_name, const char *new_id_str,
9997 struct got_object_id *old_commit_id, struct got_commit_object *old_commit,
9998 struct got_reflist_object_id_map *refs_idmap,
9999 struct got_repository *repo)
10001 const struct got_error *err = NULL;
10002 struct got_reflist_head *refs;
10003 char *refs_str = NULL;
10004 struct got_object_id *new_commit_id = NULL;
10005 struct got_commit_object *new_commit = NULL;
10006 char *new_commit_brief_str = NULL;
10007 struct got_object_id *yca_id = NULL;
10008 struct got_commit_object *yca_commit = NULL;
10009 char *yca_id_str = NULL, *yca_brief_str = NULL;
10010 char *custom_refs_str;
10012 if (asprintf(&custom_refs_str, "formerly %s", branch_name) == -1)
10013 return got_error_from_errno("asprintf");
10015 err = print_commit(old_commit, old_commit_id, repo, NULL, NULL, NULL,
10016 0, 0, refs_idmap, custom_refs_str);
10020 err = got_object_resolve_id_str(&new_commit_id, repo, new_id_str);
10024 refs = got_reflist_object_id_map_lookup(refs_idmap, new_commit_id);
10026 err = build_refs_str(&refs_str, refs, new_commit_id, repo, 0);
10031 err = got_object_open_as_commit(&new_commit, repo, new_commit_id);
10035 err = get_commit_brief_str(&new_commit_brief_str, new_commit);
10039 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
10040 old_commit_id, new_commit_id, 1, repo, check_cancelled, NULL);
10044 printf("has become commit %s%s%s%s\n %s\n", new_id_str,
10045 refs_str ? " (" : "", refs_str ? refs_str : "",
10046 refs_str ? ")" : "", new_commit_brief_str);
10047 if (yca_id && got_object_id_cmp(yca_id, new_commit_id) != 0 &&
10048 got_object_id_cmp(yca_id, old_commit_id) != 0) {
10052 err = got_object_open_as_commit(&yca_commit, repo, yca_id);
10056 err = get_commit_brief_str(&yca_brief_str, yca_commit);
10060 err = got_object_id_str(&yca_id_str, yca_id);
10064 refs = got_reflist_object_id_map_lookup(refs_idmap, yca_id);
10066 err = build_refs_str(&refs_str, refs, yca_id, repo, 0);
10070 printf("history forked at %s%s%s%s\n %s\n",
10072 refs_str ? " (" : "", refs_str ? refs_str : "",
10073 refs_str ? ")" : "", yca_brief_str);
10076 free(custom_refs_str);
10077 free(new_commit_id);
10081 free(yca_brief_str);
10083 got_object_commit_close(new_commit);
10085 got_object_commit_close(yca_commit);
10090 static const struct got_error *
10091 process_backup_refs(const char *backup_ref_prefix,
10092 const char *wanted_branch_name,
10093 int delete, struct got_repository *repo)
10095 const struct got_error *err;
10096 struct got_reflist_head refs, backup_refs;
10097 struct got_reflist_entry *re;
10098 const size_t backup_ref_prefix_len = strlen(backup_ref_prefix);
10099 struct got_object_id *old_commit_id = NULL;
10100 char *branch_name = NULL;
10101 struct got_commit_object *old_commit = NULL;
10102 struct got_reflist_object_id_map *refs_idmap = NULL;
10103 int wanted_branch_found = 0;
10106 TAILQ_INIT(&backup_refs);
10108 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
10112 err = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
10116 if (wanted_branch_name) {
10117 if (strncmp(wanted_branch_name, "refs/heads/", 11) == 0)
10118 wanted_branch_name += 11;
10121 err = got_ref_list(&backup_refs, repo, backup_ref_prefix,
10122 got_ref_cmp_by_commit_timestamp_descending, repo);
10126 TAILQ_FOREACH(re, &backup_refs, entry) {
10127 const char *refname = got_ref_get_name(re->ref);
10130 err = check_cancelled(NULL);
10134 err = got_ref_resolve(&old_commit_id, repo, re->ref);
10138 err = got_object_open_as_commit(&old_commit, repo,
10143 if (strncmp(backup_ref_prefix, refname,
10144 backup_ref_prefix_len) == 0)
10145 refname += backup_ref_prefix_len;
10147 while (refname[0] == '/')
10150 branch_name = strdup(refname);
10151 if (branch_name == NULL) {
10152 err = got_error_from_errno("strdup");
10155 slash = strrchr(branch_name, '/');
10158 refname += strlen(branch_name) + 1;
10161 if (wanted_branch_name == NULL ||
10162 strcmp(wanted_branch_name, branch_name) == 0) {
10163 wanted_branch_found = 1;
10165 err = delete_backup_ref(re->ref,
10166 old_commit_id, repo);
10168 err = print_backup_ref(branch_name, refname,
10169 old_commit_id, old_commit, refs_idmap,
10176 free(old_commit_id);
10177 old_commit_id = NULL;
10179 branch_name = NULL;
10180 got_object_commit_close(old_commit);
10184 if (wanted_branch_name && !wanted_branch_found) {
10185 err = got_error_fmt(GOT_ERR_NOT_REF,
10186 "%s/%s/", backup_ref_prefix, wanted_branch_name);
10190 got_reflist_object_id_map_free(refs_idmap);
10191 got_ref_list_free(&refs);
10192 got_ref_list_free(&backup_refs);
10193 free(old_commit_id);
10196 got_object_commit_close(old_commit);
10200 static const struct got_error *
10201 abort_progress(void *arg, unsigned char status, const char *path)
10204 * Unversioned files should not clutter progress output when
10205 * an operation is aborted.
10207 if (status == GOT_STATUS_UNVERSIONED)
10210 return update_progress(arg, status, path);
10213 static const struct got_error *
10214 cmd_rebase(int argc, char *argv[])
10216 const struct got_error *error = NULL;
10217 struct got_worktree *worktree = NULL;
10218 struct got_repository *repo = NULL;
10219 struct got_fileindex *fileindex = NULL;
10220 char *cwd = NULL, *committer = NULL, *gitconfig_path = NULL;
10221 struct got_reference *branch = NULL;
10222 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
10223 struct got_object_id *commit_id = NULL, *parent_id = NULL;
10224 struct got_object_id *resume_commit_id = NULL;
10225 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
10226 struct got_object_id *head_commit_id = NULL;
10227 struct got_reference *head_ref = NULL;
10228 struct got_commit_object *commit = NULL;
10229 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
10230 int histedit_in_progress = 0, merge_in_progress = 0;
10231 int create_backup = 1, list_backups = 0, delete_backups = 0;
10232 struct got_object_id_queue commits;
10233 struct got_pathlist_head merged_paths;
10234 const struct got_object_id_queue *parent_ids;
10235 struct got_object_qid *qid, *pid;
10236 struct got_update_progress_arg upa;
10237 int *pack_fds = NULL;
10239 STAILQ_INIT(&commits);
10240 TAILQ_INIT(&merged_paths);
10241 memset(&upa, 0, sizeof(upa));
10243 while ((ch = getopt(argc, argv, "aclX")) != -1) {
10249 continue_rebase = 1;
10255 delete_backups = 1;
10267 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10268 "unveil", NULL) == -1)
10271 if (list_backups) {
10273 option_conflict('l', 'a');
10274 if (continue_rebase)
10275 option_conflict('l', 'c');
10276 if (delete_backups)
10277 option_conflict('l', 'X');
10278 if (argc != 0 && argc != 1)
10280 } else if (delete_backups) {
10282 option_conflict('X', 'a');
10283 if (continue_rebase)
10284 option_conflict('X', 'c');
10286 option_conflict('l', 'X');
10287 if (argc != 0 && argc != 1)
10290 if (abort_rebase && continue_rebase)
10292 else if (abort_rebase || continue_rebase) {
10295 } else if (argc != 1)
10299 cwd = getcwd(NULL, 0);
10301 error = got_error_from_errno("getcwd");
10305 error = got_repo_pack_fds_open(&pack_fds);
10309 error = got_worktree_open(&worktree, cwd);
10311 if (list_backups || delete_backups) {
10312 if (error->code != GOT_ERR_NOT_WORKTREE)
10315 if (error->code == GOT_ERR_NOT_WORKTREE)
10316 error = wrap_not_worktree_error(error,
10322 error = get_gitconfig_path(&gitconfig_path);
10325 error = got_repo_open(&repo,
10326 worktree ? got_worktree_get_repo_path(worktree) : cwd,
10327 gitconfig_path, pack_fds);
10331 error = get_author(&committer, repo, worktree);
10332 if (error && error->code != GOT_ERR_COMMIT_NO_AUTHOR)
10335 error = apply_unveil(got_repo_get_path(repo), 0,
10336 worktree ? got_worktree_get_root_path(worktree) : NULL);
10340 if (list_backups || delete_backups) {
10341 error = process_backup_refs(
10342 GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
10343 argc == 1 ? argv[0] : NULL, delete_backups, repo);
10344 goto done; /* nothing else to do */
10347 error = got_worktree_histedit_in_progress(&histedit_in_progress,
10351 if (histedit_in_progress) {
10352 error = got_error(GOT_ERR_HISTEDIT_BUSY);
10356 error = got_worktree_merge_in_progress(&merge_in_progress,
10360 if (merge_in_progress) {
10361 error = got_error(GOT_ERR_MERGE_BUSY);
10365 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
10369 if (abort_rebase) {
10370 if (!rebase_in_progress) {
10371 error = got_error(GOT_ERR_NOT_REBASING);
10374 error = got_worktree_rebase_continue(&resume_commit_id,
10375 &new_base_branch, &tmp_branch, &branch, &fileindex,
10379 printf("Switching work tree to %s\n",
10380 got_ref_get_symref_target(new_base_branch));
10381 error = got_worktree_rebase_abort(worktree, fileindex, repo,
10382 new_base_branch, abort_progress, &upa);
10385 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
10386 print_merge_progress_stats(&upa);
10387 goto done; /* nothing else to do */
10390 if (continue_rebase) {
10391 if (!rebase_in_progress) {
10392 error = got_error(GOT_ERR_NOT_REBASING);
10395 error = got_worktree_rebase_continue(&resume_commit_id,
10396 &new_base_branch, &tmp_branch, &branch, &fileindex,
10401 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
10402 committer, resume_commit_id, repo);
10406 yca_id = got_object_id_dup(resume_commit_id);
10407 if (yca_id == NULL) {
10408 error = got_error_from_errno("got_object_id_dup");
10412 error = got_ref_open(&branch, repo, argv[0], 0);
10415 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
10416 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
10417 "will not rebase a branch which lives outside "
10418 "the \"refs/heads/\" reference namespace");
10423 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
10427 if (!continue_rebase) {
10428 struct got_object_id *base_commit_id;
10430 error = got_ref_open(&head_ref, repo,
10431 got_worktree_get_head_ref_name(worktree), 0);
10434 error = got_ref_resolve(&head_commit_id, repo, head_ref);
10437 base_commit_id = got_worktree_get_base_commit_id(worktree);
10438 if (got_object_id_cmp(base_commit_id, head_commit_id) != 0) {
10439 error = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
10443 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
10444 base_commit_id, branch_head_commit_id, 1, repo,
10445 check_cancelled, NULL);
10448 if (yca_id == NULL) {
10449 error = got_error_msg(GOT_ERR_ANCESTRY,
10450 "specified branch shares no common ancestry "
10451 "with work tree's branch");
10455 error = check_same_branch(base_commit_id, branch, yca_id, repo);
10457 if (error->code != GOT_ERR_ANCESTRY)
10461 struct got_pathlist_head paths;
10462 printf("%s is already based on %s\n",
10463 got_ref_get_name(branch),
10464 got_worktree_get_head_ref_name(worktree));
10465 error = switch_head_ref(branch, branch_head_commit_id,
10469 error = got_worktree_set_base_commit_id(worktree, repo,
10470 branch_head_commit_id);
10473 TAILQ_INIT(&paths);
10474 error = got_pathlist_append(&paths, "", NULL);
10477 error = got_worktree_checkout_files(worktree,
10478 &paths, repo, update_progress, &upa,
10479 check_cancelled, NULL);
10480 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
10483 if (upa.did_something) {
10485 error = got_object_id_str(&id_str,
10486 branch_head_commit_id);
10489 printf("Updated to %s: %s\n",
10490 got_worktree_get_head_ref_name(worktree),
10494 printf("Already up-to-date\n");
10495 print_update_progress_stats(&upa);
10500 commit_id = branch_head_commit_id;
10501 error = got_object_open_as_commit(&commit, repo, commit_id);
10505 parent_ids = got_object_commit_get_parent_ids(commit);
10506 pid = STAILQ_FIRST(parent_ids);
10508 error = got_error(GOT_ERR_EMPTY_REBASE);
10511 error = collect_commits(&commits, commit_id, &pid->id,
10512 yca_id, got_worktree_get_path_prefix(worktree),
10513 GOT_ERR_REBASE_PATH, repo);
10514 got_object_commit_close(commit);
10519 if (!continue_rebase) {
10520 error = got_worktree_rebase_prepare(&new_base_branch,
10521 &tmp_branch, &fileindex, worktree, branch, repo);
10526 if (STAILQ_EMPTY(&commits)) {
10527 if (continue_rebase) {
10528 error = rebase_complete(worktree, fileindex,
10529 branch, new_base_branch, tmp_branch, repo,
10533 /* Fast-forward the reference of the branch. */
10534 struct got_object_id *new_head_commit_id;
10536 error = got_ref_resolve(&new_head_commit_id, repo,
10540 error = got_object_id_str(&id_str, new_head_commit_id);
10543 printf("Forwarding %s to commit %s\n",
10544 got_ref_get_name(branch), id_str);
10546 error = got_ref_change_ref(branch,
10547 new_head_commit_id);
10550 /* No backup needed since objects did not change. */
10556 STAILQ_FOREACH(qid, &commits, entry) {
10558 commit_id = &qid->id;
10559 parent_id = pid ? &pid->id : yca_id;
10562 memset(&upa, 0, sizeof(upa));
10563 error = got_worktree_rebase_merge_files(&merged_paths,
10564 worktree, fileindex, parent_id, commit_id, repo,
10565 update_progress, &upa, check_cancelled, NULL);
10569 print_merge_progress_stats(&upa);
10570 if (upa.conflicts > 0 || upa.missing > 0 ||
10571 upa.not_deleted > 0 || upa.unversioned > 0) {
10572 if (upa.conflicts > 0) {
10573 error = show_rebase_merge_conflict(&qid->id,
10578 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
10582 error = rebase_commit(&merged_paths, worktree, fileindex,
10583 tmp_branch, committer, commit_id, repo);
10584 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
10589 if (upa.conflicts > 0 || upa.missing > 0 ||
10590 upa.not_deleted > 0 || upa.unversioned > 0) {
10591 error = got_worktree_rebase_postpone(worktree, fileindex);
10594 if (upa.conflicts > 0 && upa.missing == 0 &&
10595 upa.not_deleted == 0 && upa.unversioned == 0) {
10596 error = got_error_msg(GOT_ERR_CONFLICTS,
10597 "conflicts must be resolved before rebasing "
10599 } else if (upa.conflicts > 0) {
10600 error = got_error_msg(GOT_ERR_CONFLICTS,
10601 "conflicts must be resolved before rebasing "
10602 "can continue; changes destined for some "
10603 "files were not yet merged and should be "
10604 "merged manually if required before the "
10605 "rebase operation is continued");
10607 error = got_error_msg(GOT_ERR_CONFLICTS,
10608 "changes destined for some files were not "
10609 "yet merged and should be merged manually "
10610 "if required before the rebase operation "
10614 error = rebase_complete(worktree, fileindex, branch,
10615 new_base_branch, tmp_branch, repo, create_backup);
10619 free(gitconfig_path);
10620 got_object_id_queue_free(&commits);
10621 free(branch_head_commit_id);
10622 free(resume_commit_id);
10623 free(head_commit_id);
10626 got_object_commit_close(commit);
10628 got_ref_close(branch);
10629 if (new_base_branch)
10630 got_ref_close(new_base_branch);
10632 got_ref_close(tmp_branch);
10634 got_ref_close(head_ref);
10636 got_worktree_close(worktree);
10638 const struct got_error *close_err = got_repo_close(repo);
10643 const struct got_error *pack_err =
10644 got_repo_pack_fds_close(pack_fds);
10652 usage_histedit(void)
10654 fprintf(stderr, "usage: %s histedit [-aceflmX] [-F histedit-script] "
10655 "[branch]\n", getprogname());
10659 #define GOT_HISTEDIT_PICK 'p'
10660 #define GOT_HISTEDIT_EDIT 'e'
10661 #define GOT_HISTEDIT_FOLD 'f'
10662 #define GOT_HISTEDIT_DROP 'd'
10663 #define GOT_HISTEDIT_MESG 'm'
10665 static const struct got_histedit_cmd {
10666 unsigned char code;
10669 } got_histedit_cmds[] = {
10670 { GOT_HISTEDIT_PICK, "pick", "use commit" },
10671 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
10672 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
10674 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
10675 { GOT_HISTEDIT_MESG, "mesg",
10676 "single-line log message for commit above (open editor if empty)" },
10679 struct got_histedit_list_entry {
10680 TAILQ_ENTRY(got_histedit_list_entry) entry;
10681 struct got_object_id *commit_id;
10682 const struct got_histedit_cmd *cmd;
10685 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
10687 static const struct got_error *
10688 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
10689 FILE *f, struct got_repository *repo)
10691 const struct got_error *err = NULL;
10692 char *logmsg = NULL, *id_str = NULL;
10693 struct got_commit_object *commit = NULL;
10696 err = got_object_open_as_commit(&commit, repo, commit_id);
10700 err = get_short_logmsg(&logmsg, 34, commit);
10704 err = got_object_id_str(&id_str, commit_id);
10708 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
10710 err = got_ferror(f, GOT_ERR_IO);
10713 got_object_commit_close(commit);
10719 static const struct got_error *
10720 histedit_write_commit_list(struct got_object_id_queue *commits,
10721 FILE *f, int edit_logmsg_only, int fold_only, int edit_only,
10722 struct got_repository *repo)
10724 const struct got_error *err = NULL;
10725 struct got_object_qid *qid;
10726 const char *histedit_cmd = NULL;
10728 if (STAILQ_EMPTY(commits))
10729 return got_error(GOT_ERR_EMPTY_HISTEDIT);
10731 STAILQ_FOREACH(qid, commits, entry) {
10732 histedit_cmd = got_histedit_cmds[0].name;
10734 histedit_cmd = "edit";
10735 else if (fold_only && STAILQ_NEXT(qid, entry) != NULL)
10736 histedit_cmd = "fold";
10737 err = histedit_write_commit(&qid->id, histedit_cmd, f, repo);
10740 if (edit_logmsg_only) {
10741 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
10743 err = got_ferror(f, GOT_ERR_IO);
10752 static const struct got_error *
10753 write_cmd_list(FILE *f, const char *branch_name,
10754 struct got_object_id_queue *commits)
10756 const struct got_error *err = NULL;
10760 struct got_object_qid *qid;
10762 qid = STAILQ_FIRST(commits);
10763 err = got_object_id_str(&id_str, &qid->id);
10768 "# Editing the history of branch '%s' starting at\n"
10770 "# Commits will be processed in order from top to "
10771 "bottom of this file.\n", branch_name, id_str);
10773 err = got_ferror(f, GOT_ERR_IO);
10777 n = fprintf(f, "# Available histedit commands:\n");
10779 err = got_ferror(f, GOT_ERR_IO);
10783 for (i = 0; i < nitems(got_histedit_cmds); i++) {
10784 const struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
10785 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
10788 err = got_ferror(f, GOT_ERR_IO);
10797 static const struct got_error *
10798 histedit_syntax_error(int lineno)
10800 static char msg[42];
10803 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
10805 if (ret < 0 || (size_t)ret >= sizeof(msg))
10806 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
10808 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
10811 static const struct got_error *
10812 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
10813 char *logmsg, struct got_repository *repo)
10815 const struct got_error *err;
10816 struct got_commit_object *folded_commit = NULL;
10817 char *id_str, *folded_logmsg = NULL;
10819 err = got_object_id_str(&id_str, hle->commit_id);
10823 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
10827 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
10830 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
10831 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
10832 folded_logmsg) == -1) {
10833 err = got_error_from_errno("asprintf");
10837 got_object_commit_close(folded_commit);
10839 free(folded_logmsg);
10843 static struct got_histedit_list_entry *
10844 get_folded_commits(struct got_histedit_list_entry *hle)
10846 struct got_histedit_list_entry *prev, *folded = NULL;
10848 prev = TAILQ_PREV(hle, got_histedit_list, entry);
10849 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
10850 prev->cmd->code == GOT_HISTEDIT_DROP)) {
10851 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
10853 prev = TAILQ_PREV(prev, got_histedit_list, entry);
10859 static const struct got_error *
10860 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
10861 struct got_repository *repo)
10863 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
10864 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
10865 const struct got_error *err = NULL;
10866 struct got_commit_object *commit = NULL;
10869 struct got_histedit_list_entry *folded = NULL;
10871 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
10875 folded = get_folded_commits(hle);
10877 while (folded != hle) {
10878 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
10879 folded = TAILQ_NEXT(folded, entry);
10882 err = append_folded_commit_msg(&new_msg, folded,
10888 folded = TAILQ_NEXT(folded, entry);
10892 err = got_object_id_str(&id_str, hle->commit_id);
10895 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
10898 logmsg_len = asprintf(&new_msg,
10899 "%s\n# original log message of commit %s: %s",
10900 logmsg ? logmsg : "", id_str, orig_logmsg);
10901 if (logmsg_len == -1) {
10902 err = got_error_from_errno("asprintf");
10908 err = got_object_id_str(&id_str, hle->commit_id);
10912 err = got_opentemp_named_fd(&logmsg_path, &fd,
10913 GOT_TMPDIR_STR "/got-logmsg", "");
10917 write(fd, logmsg, logmsg_len);
10920 err = get_editor(&editor);
10924 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg,
10927 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
10930 hle->logmsg = strdup(new_msg);
10931 if (hle->logmsg == NULL)
10932 err = got_error_from_errno("strdup");
10935 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
10936 err = got_error_from_errno2("unlink", logmsg_path);
10942 got_object_commit_close(commit);
10946 static const struct got_error *
10947 histedit_parse_list(struct got_histedit_list *histedit_cmds,
10948 FILE *f, struct got_repository *repo)
10950 const struct got_error *err = NULL;
10951 char *line = NULL, *p, *end;
10954 int lineno = 0, lastcmd = -1;
10955 const struct got_histedit_cmd *cmd;
10956 struct got_object_id *commit_id = NULL;
10957 struct got_histedit_list_entry *hle = NULL;
10960 len = getline(&line, &size, f);
10962 const struct got_error *getline_err;
10965 getline_err = got_error_from_errno("getline");
10966 err = got_ferror(f, getline_err->code);
10971 while (isspace((unsigned char)p[0]))
10973 if (p[0] == '#' || p[0] == '\0') {
10979 for (i = 0; i < nitems(got_histedit_cmds); i++) {
10980 cmd = &got_histedit_cmds[i];
10981 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
10982 isspace((unsigned char)p[strlen(cmd->name)])) {
10983 p += strlen(cmd->name);
10986 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
10991 if (i == nitems(got_histedit_cmds)) {
10992 err = histedit_syntax_error(lineno);
10995 while (isspace((unsigned char)p[0]))
10997 if (cmd->code == GOT_HISTEDIT_MESG) {
10998 if (lastcmd != GOT_HISTEDIT_PICK &&
10999 lastcmd != GOT_HISTEDIT_EDIT) {
11000 err = got_error(GOT_ERR_HISTEDIT_CMD);
11003 if (p[0] == '\0') {
11004 err = histedit_edit_logmsg(hle, repo);
11008 hle->logmsg = strdup(p);
11009 if (hle->logmsg == NULL) {
11010 err = got_error_from_errno("strdup");
11016 lastcmd = cmd->code;
11020 while (end[0] && !isspace((unsigned char)end[0]))
11024 err = got_object_resolve_id_str(&commit_id, repo, p);
11026 /* override error code */
11027 err = histedit_syntax_error(lineno);
11031 hle = malloc(sizeof(*hle));
11033 err = got_error_from_errno("malloc");
11037 hle->commit_id = commit_id;
11038 hle->logmsg = NULL;
11042 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
11043 lastcmd = cmd->code;
11051 static const struct got_error *
11052 histedit_check_script(struct got_histedit_list *histedit_cmds,
11053 struct got_object_id_queue *commits, struct got_repository *repo)
11055 const struct got_error *err = NULL;
11056 struct got_object_qid *qid;
11057 struct got_histedit_list_entry *hle;
11058 static char msg[92];
11061 if (TAILQ_EMPTY(histedit_cmds))
11062 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
11063 "histedit script contains no commands");
11064 if (STAILQ_EMPTY(commits))
11065 return got_error(GOT_ERR_EMPTY_HISTEDIT);
11067 TAILQ_FOREACH(hle, histedit_cmds, entry) {
11068 struct got_histedit_list_entry *hle2;
11069 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
11072 if (got_object_id_cmp(hle->commit_id,
11073 hle2->commit_id) != 0)
11075 err = got_object_id_str(&id_str, hle->commit_id);
11078 snprintf(msg, sizeof(msg), "commit %s is listed "
11079 "more than once in histedit script", id_str);
11081 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
11085 STAILQ_FOREACH(qid, commits, entry) {
11086 TAILQ_FOREACH(hle, histedit_cmds, entry) {
11087 if (got_object_id_cmp(&qid->id, hle->commit_id) == 0)
11091 err = got_object_id_str(&id_str, &qid->id);
11094 snprintf(msg, sizeof(msg),
11095 "commit %s missing from histedit script", id_str);
11097 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
11101 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
11102 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
11103 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
11104 "last commit in histedit script cannot be folded");
11109 static const struct got_error *
11110 histedit_run_editor(struct got_histedit_list *histedit_cmds,
11111 const char *path, struct got_object_id_queue *commits,
11112 struct got_repository *repo)
11114 const struct got_error *err = NULL;
11118 err = get_editor(&editor);
11122 if (spawn_editor(editor, path) == -1) {
11123 err = got_error_from_errno("failed spawning editor");
11127 f = fopen(path, "re");
11129 err = got_error_from_errno("fopen");
11132 err = histedit_parse_list(histedit_cmds, f, repo);
11136 err = histedit_check_script(histedit_cmds, commits, repo);
11138 if (f && fclose(f) == EOF && err == NULL)
11139 err = got_error_from_errno("fclose");
11144 static const struct got_error *
11145 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
11146 struct got_object_id_queue *, const char *, const char *,
11147 struct got_repository *);
11149 static const struct got_error *
11150 histedit_edit_script(struct got_histedit_list *histedit_cmds,
11151 struct got_object_id_queue *commits, const char *branch_name,
11152 int edit_logmsg_only, int fold_only, int edit_only,
11153 struct got_repository *repo)
11155 const struct got_error *err;
11159 err = got_opentemp_named(&path, &f, "got-histedit", "");
11163 err = write_cmd_list(f, branch_name, commits);
11167 err = histedit_write_commit_list(commits, f, edit_logmsg_only,
11168 fold_only, edit_only, repo);
11172 if (edit_logmsg_only || fold_only || edit_only) {
11174 err = histedit_parse_list(histedit_cmds, f, repo);
11176 if (fclose(f) == EOF) {
11177 err = got_error_from_errno("fclose");
11181 err = histedit_run_editor(histedit_cmds, path, commits, repo);
11183 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
11184 err->code != GOT_ERR_HISTEDIT_CMD)
11186 err = histedit_edit_list_retry(histedit_cmds, err,
11187 commits, path, branch_name, repo);
11191 if (f && fclose(f) == EOF && err == NULL)
11192 err = got_error_from_errno("fclose");
11193 if (path && unlink(path) != 0 && err == NULL)
11194 err = got_error_from_errno2("unlink", path);
11199 static const struct got_error *
11200 histedit_save_list(struct got_histedit_list *histedit_cmds,
11201 struct got_worktree *worktree, struct got_repository *repo)
11203 const struct got_error *err = NULL;
11206 struct got_histedit_list_entry *hle;
11207 struct got_commit_object *commit = NULL;
11209 err = got_worktree_get_histedit_script_path(&path, worktree);
11213 f = fopen(path, "we");
11215 err = got_error_from_errno2("fopen", path);
11218 TAILQ_FOREACH(hle, histedit_cmds, entry) {
11219 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
11225 int n = fprintf(f, "%c %s\n",
11226 GOT_HISTEDIT_MESG, hle->logmsg);
11228 err = got_ferror(f, GOT_ERR_IO);
11234 if (f && fclose(f) == EOF && err == NULL)
11235 err = got_error_from_errno("fclose");
11238 got_object_commit_close(commit);
11243 histedit_free_list(struct got_histedit_list *histedit_cmds)
11245 struct got_histedit_list_entry *hle;
11247 while ((hle = TAILQ_FIRST(histedit_cmds))) {
11248 TAILQ_REMOVE(histedit_cmds, hle, entry);
11253 static const struct got_error *
11254 histedit_load_list(struct got_histedit_list *histedit_cmds,
11255 const char *path, struct got_repository *repo)
11257 const struct got_error *err = NULL;
11260 f = fopen(path, "re");
11262 err = got_error_from_errno2("fopen", path);
11266 err = histedit_parse_list(histedit_cmds, f, repo);
11268 if (f && fclose(f) == EOF && err == NULL)
11269 err = got_error_from_errno("fclose");
11273 static const struct got_error *
11274 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
11275 const struct got_error *edit_err, struct got_object_id_queue *commits,
11276 const char *path, const char *branch_name, struct got_repository *repo)
11278 const struct got_error *err = NULL, *prev_err = edit_err;
11281 while (resp != 'c' && resp != 'r' && resp != 'a') {
11282 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
11283 "or (a)bort: ", getprogname(), prev_err->msg);
11288 histedit_free_list(histedit_cmds);
11289 err = histedit_run_editor(histedit_cmds, path, commits,
11292 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
11293 err->code != GOT_ERR_HISTEDIT_CMD)
11300 } else if (resp == 'r') {
11301 histedit_free_list(histedit_cmds);
11302 err = histedit_edit_script(histedit_cmds,
11303 commits, branch_name, 0, 0, 0, repo);
11305 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
11306 err->code != GOT_ERR_HISTEDIT_CMD)
11313 } else if (resp == 'a') {
11314 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
11317 printf("invalid response '%c'\n", resp);
11323 static const struct got_error *
11324 histedit_complete(struct got_worktree *worktree,
11325 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
11326 struct got_reference *branch, struct got_repository *repo)
11328 printf("Switching work tree to %s\n",
11329 got_ref_get_symref_target(branch));
11330 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
11334 static const struct got_error *
11335 show_histedit_progress(struct got_commit_object *commit,
11336 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
11338 const struct got_error *err;
11339 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
11341 err = got_object_id_str(&old_id_str, hle->commit_id);
11346 err = got_object_id_str(&new_id_str, new_id);
11351 old_id_str[12] = '\0';
11353 new_id_str[12] = '\0';
11356 logmsg = strdup(hle->logmsg);
11357 if (logmsg == NULL) {
11358 err = got_error_from_errno("strdup");
11361 trim_logmsg(logmsg, 42);
11363 err = get_short_logmsg(&logmsg, 42, commit);
11368 switch (hle->cmd->code) {
11369 case GOT_HISTEDIT_PICK:
11370 case GOT_HISTEDIT_EDIT:
11371 printf("%s -> %s: %s\n", old_id_str,
11372 new_id_str ? new_id_str : "no-op change", logmsg);
11374 case GOT_HISTEDIT_DROP:
11375 case GOT_HISTEDIT_FOLD:
11376 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
11388 static const struct got_error *
11389 histedit_commit(struct got_pathlist_head *merged_paths,
11390 struct got_worktree *worktree, struct got_fileindex *fileindex,
11391 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
11392 const char *committer, struct got_repository *repo)
11394 const struct got_error *err;
11395 struct got_commit_object *commit;
11396 struct got_object_id *new_commit_id;
11398 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
11399 && hle->logmsg == NULL) {
11400 err = histedit_edit_logmsg(hle, repo);
11405 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
11409 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
11410 worktree, fileindex, tmp_branch, committer, commit, hle->commit_id,
11411 hle->logmsg, repo);
11413 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
11415 err = show_histedit_progress(commit, hle, NULL);
11417 err = show_histedit_progress(commit, hle, new_commit_id);
11418 free(new_commit_id);
11421 got_object_commit_close(commit);
11425 static const struct got_error *
11426 histedit_skip_commit(struct got_histedit_list_entry *hle,
11427 struct got_worktree *worktree, struct got_repository *repo)
11429 const struct got_error *error;
11430 struct got_commit_object *commit;
11432 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
11437 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
11441 error = show_histedit_progress(commit, hle, NULL);
11442 got_object_commit_close(commit);
11446 static const struct got_error *
11447 check_local_changes(void *arg, unsigned char status,
11448 unsigned char staged_status, const char *path,
11449 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
11450 struct got_object_id *commit_id, int dirfd, const char *de_name)
11452 int *have_local_changes = arg;
11455 case GOT_STATUS_ADD:
11456 case GOT_STATUS_DELETE:
11457 case GOT_STATUS_MODIFY:
11458 case GOT_STATUS_CONFLICT:
11459 *have_local_changes = 1;
11460 return got_error(GOT_ERR_CANCELLED);
11465 switch (staged_status) {
11466 case GOT_STATUS_ADD:
11467 case GOT_STATUS_DELETE:
11468 case GOT_STATUS_MODIFY:
11469 *have_local_changes = 1;
11470 return got_error(GOT_ERR_CANCELLED);
11478 static const struct got_error *
11479 cmd_histedit(int argc, char *argv[])
11481 const struct got_error *error = NULL;
11482 struct got_worktree *worktree = NULL;
11483 struct got_fileindex *fileindex = NULL;
11484 struct got_repository *repo = NULL;
11485 char *cwd = NULL, *committer = NULL, *gitconfig_path = NULL;
11486 struct got_reference *branch = NULL;
11487 struct got_reference *tmp_branch = NULL;
11488 struct got_object_id *resume_commit_id = NULL;
11489 struct got_object_id *base_commit_id = NULL;
11490 struct got_object_id *head_commit_id = NULL;
11491 struct got_commit_object *commit = NULL;
11492 int ch, rebase_in_progress = 0, merge_in_progress = 0;
11493 struct got_update_progress_arg upa;
11494 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
11495 int edit_logmsg_only = 0, fold_only = 0, edit_only = 0;
11496 int list_backups = 0, delete_backups = 0;
11497 const char *edit_script_path = NULL;
11498 struct got_object_id_queue commits;
11499 struct got_pathlist_head merged_paths;
11500 const struct got_object_id_queue *parent_ids;
11501 struct got_object_qid *pid;
11502 struct got_histedit_list histedit_cmds;
11503 struct got_histedit_list_entry *hle;
11504 int *pack_fds = NULL;
11506 STAILQ_INIT(&commits);
11507 TAILQ_INIT(&histedit_cmds);
11508 TAILQ_INIT(&merged_paths);
11509 memset(&upa, 0, sizeof(upa));
11511 while ((ch = getopt(argc, argv, "aceF:flmX")) != -1) {
11523 edit_script_path = optarg;
11532 edit_logmsg_only = 1;
11535 delete_backups = 1;
11547 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
11548 "unveil", NULL) == -1)
11551 if (abort_edit && continue_edit)
11552 option_conflict('a', 'c');
11553 if (edit_script_path && edit_logmsg_only)
11554 option_conflict('F', 'm');
11555 if (abort_edit && edit_logmsg_only)
11556 option_conflict('a', 'm');
11557 if (continue_edit && edit_logmsg_only)
11558 option_conflict('c', 'm');
11559 if (abort_edit && fold_only)
11560 option_conflict('a', 'f');
11561 if (continue_edit && fold_only)
11562 option_conflict('c', 'f');
11563 if (fold_only && edit_logmsg_only)
11564 option_conflict('f', 'm');
11565 if (edit_script_path && fold_only)
11566 option_conflict('F', 'f');
11567 if (abort_edit && edit_only)
11568 option_conflict('a', 'e');
11569 if (continue_edit && edit_only)
11570 option_conflict('c', 'e');
11571 if (edit_only && edit_logmsg_only)
11572 option_conflict('e', 'm');
11573 if (edit_script_path && edit_only)
11574 option_conflict('F', 'e');
11575 if (list_backups) {
11577 option_conflict('l', 'a');
11579 option_conflict('l', 'c');
11580 if (edit_script_path)
11581 option_conflict('l', 'F');
11582 if (edit_logmsg_only)
11583 option_conflict('l', 'm');
11585 option_conflict('l', 'f');
11587 option_conflict('l', 'e');
11588 if (delete_backups)
11589 option_conflict('l', 'X');
11590 if (argc != 0 && argc != 1)
11592 } else if (delete_backups) {
11594 option_conflict('X', 'a');
11596 option_conflict('X', 'c');
11597 if (edit_script_path)
11598 option_conflict('X', 'F');
11599 if (edit_logmsg_only)
11600 option_conflict('X', 'm');
11602 option_conflict('X', 'f');
11604 option_conflict('X', 'e');
11606 option_conflict('X', 'l');
11607 if (argc != 0 && argc != 1)
11609 } else if (argc != 0)
11613 * This command cannot apply unveil(2) in all cases because the
11614 * user may choose to run an editor to edit the histedit script
11615 * and to edit individual commit log messages.
11616 * unveil(2) traverses exec(2); if an editor is used we have to
11617 * apply unveil after edit script and log messages have been written.
11618 * XXX TODO: Make use of unveil(2) where possible.
11621 cwd = getcwd(NULL, 0);
11623 error = got_error_from_errno("getcwd");
11627 error = got_repo_pack_fds_open(&pack_fds);
11631 error = got_worktree_open(&worktree, cwd);
11633 if (list_backups || delete_backups) {
11634 if (error->code != GOT_ERR_NOT_WORKTREE)
11637 if (error->code == GOT_ERR_NOT_WORKTREE)
11638 error = wrap_not_worktree_error(error,
11644 if (list_backups || delete_backups) {
11645 error = got_repo_open(&repo,
11646 worktree ? got_worktree_get_repo_path(worktree) : cwd,
11650 error = apply_unveil(got_repo_get_path(repo), 0,
11651 worktree ? got_worktree_get_root_path(worktree) : NULL);
11654 error = process_backup_refs(
11655 GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
11656 argc == 1 ? argv[0] : NULL, delete_backups, repo);
11657 goto done; /* nothing else to do */
11660 error = get_gitconfig_path(&gitconfig_path);
11663 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
11664 gitconfig_path, pack_fds);
11668 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
11671 if (rebase_in_progress) {
11672 error = got_error(GOT_ERR_REBASING);
11676 error = got_worktree_merge_in_progress(&merge_in_progress, worktree,
11680 if (merge_in_progress) {
11681 error = got_error(GOT_ERR_MERGE_BUSY);
11685 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
11689 if (edit_in_progress && edit_logmsg_only) {
11690 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
11691 "histedit operation is in progress in this "
11692 "work tree and must be continued or aborted "
11693 "before the -m option can be used");
11696 if (edit_in_progress && fold_only) {
11697 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
11698 "histedit operation is in progress in this "
11699 "work tree and must be continued or aborted "
11700 "before the -f option can be used");
11703 if (edit_in_progress && edit_only) {
11704 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
11705 "histedit operation is in progress in this "
11706 "work tree and must be continued or aborted "
11707 "before the -e option can be used");
11711 if (edit_in_progress && abort_edit) {
11712 error = got_worktree_histedit_continue(&resume_commit_id,
11713 &tmp_branch, &branch, &base_commit_id, &fileindex,
11717 printf("Switching work tree to %s\n",
11718 got_ref_get_symref_target(branch));
11719 error = got_worktree_histedit_abort(worktree, fileindex, repo,
11720 branch, base_commit_id, abort_progress, &upa);
11723 printf("Histedit of %s aborted\n",
11724 got_ref_get_symref_target(branch));
11725 print_merge_progress_stats(&upa);
11726 goto done; /* nothing else to do */
11727 } else if (abort_edit) {
11728 error = got_error(GOT_ERR_NOT_HISTEDIT);
11732 error = get_author(&committer, repo, worktree);
11736 if (continue_edit) {
11739 if (!edit_in_progress) {
11740 error = got_error(GOT_ERR_NOT_HISTEDIT);
11744 error = got_worktree_get_histedit_script_path(&path, worktree);
11748 error = histedit_load_list(&histedit_cmds, path, repo);
11753 error = got_worktree_histedit_continue(&resume_commit_id,
11754 &tmp_branch, &branch, &base_commit_id, &fileindex,
11759 error = got_ref_resolve(&head_commit_id, repo, branch);
11763 error = got_object_open_as_commit(&commit, repo,
11767 parent_ids = got_object_commit_get_parent_ids(commit);
11768 pid = STAILQ_FIRST(parent_ids);
11770 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
11773 error = collect_commits(&commits, head_commit_id, &pid->id,
11774 base_commit_id, got_worktree_get_path_prefix(worktree),
11775 GOT_ERR_HISTEDIT_PATH, repo);
11776 got_object_commit_close(commit);
11781 if (edit_in_progress) {
11782 error = got_error(GOT_ERR_HISTEDIT_BUSY);
11786 error = got_ref_open(&branch, repo,
11787 got_worktree_get_head_ref_name(worktree), 0);
11791 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
11792 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
11793 "will not edit commit history of a branch outside "
11794 "the \"refs/heads/\" reference namespace");
11798 error = got_ref_resolve(&head_commit_id, repo, branch);
11799 got_ref_close(branch);
11804 error = got_object_open_as_commit(&commit, repo,
11808 parent_ids = got_object_commit_get_parent_ids(commit);
11809 pid = STAILQ_FIRST(parent_ids);
11811 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
11814 error = collect_commits(&commits, head_commit_id, &pid->id,
11815 got_worktree_get_base_commit_id(worktree),
11816 got_worktree_get_path_prefix(worktree),
11817 GOT_ERR_HISTEDIT_PATH, repo);
11818 got_object_commit_close(commit);
11823 if (STAILQ_EMPTY(&commits)) {
11824 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
11828 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
11829 &base_commit_id, &fileindex, worktree, repo);
11833 if (edit_script_path) {
11834 error = histedit_load_list(&histedit_cmds,
11835 edit_script_path, repo);
11837 got_worktree_histedit_abort(worktree, fileindex,
11838 repo, branch, base_commit_id,
11839 abort_progress, &upa);
11840 print_merge_progress_stats(&upa);
11844 const char *branch_name;
11845 branch_name = got_ref_get_symref_target(branch);
11846 if (strncmp(branch_name, "refs/heads/", 11) == 0)
11848 error = histedit_edit_script(&histedit_cmds, &commits,
11849 branch_name, edit_logmsg_only, fold_only,
11852 got_worktree_histedit_abort(worktree, fileindex,
11853 repo, branch, base_commit_id,
11854 abort_progress, &upa);
11855 print_merge_progress_stats(&upa);
11861 error = histedit_save_list(&histedit_cmds, worktree,
11864 got_worktree_histedit_abort(worktree, fileindex,
11865 repo, branch, base_commit_id,
11866 abort_progress, &upa);
11867 print_merge_progress_stats(&upa);
11873 error = histedit_check_script(&histedit_cmds, &commits, repo);
11877 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
11878 if (resume_commit_id) {
11879 if (got_object_id_cmp(hle->commit_id,
11880 resume_commit_id) != 0)
11883 resume_commit_id = NULL;
11884 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
11885 hle->cmd->code == GOT_HISTEDIT_FOLD) {
11886 error = histedit_skip_commit(hle, worktree,
11891 struct got_pathlist_head paths;
11892 int have_changes = 0;
11894 TAILQ_INIT(&paths);
11895 error = got_pathlist_append(&paths, "", NULL);
11898 error = got_worktree_status(worktree, &paths,
11899 repo, 0, check_local_changes, &have_changes,
11900 check_cancelled, NULL);
11901 got_pathlist_free(&paths,
11902 GOT_PATHLIST_FREE_NONE);
11904 if (error->code != GOT_ERR_CANCELLED)
11906 if (sigint_received || sigpipe_received)
11909 if (have_changes) {
11910 error = histedit_commit(NULL, worktree,
11911 fileindex, tmp_branch, hle,
11916 error = got_object_open_as_commit(
11917 &commit, repo, hle->commit_id);
11920 error = show_histedit_progress(commit,
11922 got_object_commit_close(commit);
11931 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
11932 error = histedit_skip_commit(hle, worktree, repo);
11938 error = got_object_open_as_commit(&commit, repo,
11942 parent_ids = got_object_commit_get_parent_ids(commit);
11943 pid = STAILQ_FIRST(parent_ids);
11945 error = got_worktree_histedit_merge_files(&merged_paths,
11946 worktree, fileindex, &pid->id, hle->commit_id, repo,
11947 update_progress, &upa, check_cancelled, NULL);
11950 got_object_commit_close(commit);
11953 print_merge_progress_stats(&upa);
11954 if (upa.conflicts > 0 || upa.missing > 0 ||
11955 upa.not_deleted > 0 || upa.unversioned > 0) {
11956 if (upa.conflicts > 0) {
11957 error = show_rebase_merge_conflict(
11958 hle->commit_id, repo);
11962 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
11966 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
11968 error = got_object_id_str(&id_str, hle->commit_id);
11971 printf("Stopping histedit for amending commit %s\n",
11974 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
11975 error = got_worktree_histedit_postpone(worktree,
11980 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
11981 error = histedit_skip_commit(hle, worktree, repo);
11987 error = histedit_commit(&merged_paths, worktree, fileindex,
11988 tmp_branch, hle, committer, repo);
11989 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
11994 if (upa.conflicts > 0 || upa.missing > 0 ||
11995 upa.not_deleted > 0 || upa.unversioned > 0) {
11996 error = got_worktree_histedit_postpone(worktree, fileindex);
11999 if (upa.conflicts > 0 && upa.missing == 0 &&
12000 upa.not_deleted == 0 && upa.unversioned == 0) {
12001 error = got_error_msg(GOT_ERR_CONFLICTS,
12002 "conflicts must be resolved before histedit "
12004 } else if (upa.conflicts > 0) {
12005 error = got_error_msg(GOT_ERR_CONFLICTS,
12006 "conflicts must be resolved before histedit "
12007 "can continue; changes destined for some "
12008 "files were not yet merged and should be "
12009 "merged manually if required before the "
12010 "histedit operation is continued");
12012 error = got_error_msg(GOT_ERR_CONFLICTS,
12013 "changes destined for some files were not "
12014 "yet merged and should be merged manually "
12015 "if required before the histedit operation "
12019 error = histedit_complete(worktree, fileindex, tmp_branch,
12024 free(gitconfig_path);
12025 got_object_id_queue_free(&commits);
12026 histedit_free_list(&histedit_cmds);
12027 free(head_commit_id);
12028 free(base_commit_id);
12029 free(resume_commit_id);
12031 got_object_commit_close(commit);
12033 got_ref_close(branch);
12035 got_ref_close(tmp_branch);
12037 got_worktree_close(worktree);
12039 const struct got_error *close_err = got_repo_close(repo);
12044 const struct got_error *pack_err =
12045 got_repo_pack_fds_close(pack_fds);
12053 usage_integrate(void)
12055 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
12059 static const struct got_error *
12060 cmd_integrate(int argc, char *argv[])
12062 const struct got_error *error = NULL;
12063 struct got_repository *repo = NULL;
12064 struct got_worktree *worktree = NULL;
12065 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
12066 const char *branch_arg = NULL;
12067 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
12068 struct got_fileindex *fileindex = NULL;
12069 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
12071 struct got_update_progress_arg upa;
12072 int *pack_fds = NULL;
12074 while ((ch = getopt(argc, argv, "")) != -1) {
12087 branch_arg = argv[0];
12089 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
12090 "unveil", NULL) == -1)
12093 cwd = getcwd(NULL, 0);
12095 error = got_error_from_errno("getcwd");
12099 error = got_repo_pack_fds_open(&pack_fds);
12103 error = got_worktree_open(&worktree, cwd);
12105 if (error->code == GOT_ERR_NOT_WORKTREE)
12106 error = wrap_not_worktree_error(error, "integrate",
12111 error = check_rebase_or_histedit_in_progress(worktree);
12115 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
12120 error = apply_unveil(got_repo_get_path(repo), 0,
12121 got_worktree_get_root_path(worktree));
12125 error = check_merge_in_progress(worktree, repo);
12129 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
12130 error = got_error_from_errno("asprintf");
12134 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
12135 &base_branch_ref, worktree, refname, repo);
12139 refname = strdup(got_ref_get_name(branch_ref));
12140 if (refname == NULL) {
12141 error = got_error_from_errno("strdup");
12142 got_worktree_integrate_abort(worktree, fileindex, repo,
12143 branch_ref, base_branch_ref);
12146 base_refname = strdup(got_ref_get_name(base_branch_ref));
12147 if (base_refname == NULL) {
12148 error = got_error_from_errno("strdup");
12149 got_worktree_integrate_abort(worktree, fileindex, repo,
12150 branch_ref, base_branch_ref);
12153 if (strncmp(base_refname, "refs/heads/", 11) != 0) {
12154 error = got_error(GOT_ERR_INTEGRATE_BRANCH);
12155 got_worktree_integrate_abort(worktree, fileindex, repo,
12156 branch_ref, base_branch_ref);
12160 error = got_ref_resolve(&commit_id, repo, branch_ref);
12164 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
12168 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
12169 error = got_error_msg(GOT_ERR_SAME_BRANCH,
12170 "specified branch has already been integrated");
12171 got_worktree_integrate_abort(worktree, fileindex, repo,
12172 branch_ref, base_branch_ref);
12176 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
12178 if (error->code == GOT_ERR_ANCESTRY)
12179 error = got_error(GOT_ERR_REBASE_REQUIRED);
12180 got_worktree_integrate_abort(worktree, fileindex, repo,
12181 branch_ref, base_branch_ref);
12185 memset(&upa, 0, sizeof(upa));
12186 error = got_worktree_integrate_continue(worktree, fileindex, repo,
12187 branch_ref, base_branch_ref, update_progress, &upa,
12188 check_cancelled, NULL);
12192 printf("Integrated %s into %s\n", refname, base_refname);
12193 print_update_progress_stats(&upa);
12196 const struct got_error *close_err = got_repo_close(repo);
12201 got_worktree_close(worktree);
12203 const struct got_error *pack_err =
12204 got_repo_pack_fds_close(pack_fds);
12209 free(base_commit_id);
12212 free(base_refname);
12219 fprintf(stderr, "usage: %s merge [-acn] [branch]\n", getprogname());
12223 static const struct got_error *
12224 cmd_merge(int argc, char *argv[])
12226 const struct got_error *error = NULL;
12227 struct got_worktree *worktree = NULL;
12228 struct got_repository *repo = NULL;
12229 struct got_fileindex *fileindex = NULL;
12230 char *cwd = NULL, *id_str = NULL, *author = NULL;
12231 struct got_reference *branch = NULL, *wt_branch = NULL;
12232 struct got_object_id *branch_tip = NULL, *yca_id = NULL;
12233 struct got_object_id *wt_branch_tip = NULL;
12234 int ch, merge_in_progress = 0, abort_merge = 0, continue_merge = 0;
12235 int interrupt_merge = 0;
12236 struct got_update_progress_arg upa;
12237 struct got_object_id *merge_commit_id = NULL;
12238 char *branch_name = NULL;
12239 int *pack_fds = NULL;
12241 memset(&upa, 0, sizeof(upa));
12243 while ((ch = getopt(argc, argv, "acn")) != -1) {
12249 continue_merge = 1;
12252 interrupt_merge = 1;
12264 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
12265 "unveil", NULL) == -1)
12269 if (abort_merge && continue_merge)
12270 option_conflict('a', 'c');
12271 if (abort_merge || continue_merge) {
12274 } else if (argc != 1)
12277 cwd = getcwd(NULL, 0);
12279 error = got_error_from_errno("getcwd");
12283 error = got_repo_pack_fds_open(&pack_fds);
12287 error = got_worktree_open(&worktree, cwd);
12289 if (error->code == GOT_ERR_NOT_WORKTREE)
12290 error = wrap_not_worktree_error(error,
12295 error = got_repo_open(&repo,
12296 worktree ? got_worktree_get_repo_path(worktree) : cwd, NULL,
12301 error = apply_unveil(got_repo_get_path(repo), 0,
12302 worktree ? got_worktree_get_root_path(worktree) : NULL);
12306 error = check_rebase_or_histedit_in_progress(worktree);
12310 error = got_worktree_merge_in_progress(&merge_in_progress, worktree,
12316 if (!merge_in_progress) {
12317 error = got_error(GOT_ERR_NOT_MERGING);
12320 error = got_worktree_merge_continue(&branch_name,
12321 &branch_tip, &fileindex, worktree, repo);
12324 error = got_worktree_merge_abort(worktree, fileindex, repo,
12325 abort_progress, &upa);
12328 printf("Merge of %s aborted\n", branch_name);
12329 goto done; /* nothing else to do */
12332 error = get_author(&author, repo, worktree);
12336 if (continue_merge) {
12337 if (!merge_in_progress) {
12338 error = got_error(GOT_ERR_NOT_MERGING);
12341 error = got_worktree_merge_continue(&branch_name,
12342 &branch_tip, &fileindex, worktree, repo);
12346 error = got_ref_open(&branch, repo, argv[0], 0);
12349 branch_name = strdup(got_ref_get_name(branch));
12350 if (branch_name == NULL) {
12351 error = got_error_from_errno("strdup");
12354 error = got_ref_resolve(&branch_tip, repo, branch);
12359 error = got_ref_open(&wt_branch, repo,
12360 got_worktree_get_head_ref_name(worktree), 0);
12363 error = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
12366 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
12367 wt_branch_tip, branch_tip, 0, repo,
12368 check_cancelled, NULL);
12369 if (error && error->code != GOT_ERR_ANCESTRY)
12372 if (!continue_merge) {
12373 error = check_path_prefix(wt_branch_tip, branch_tip,
12374 got_worktree_get_path_prefix(worktree),
12375 GOT_ERR_MERGE_PATH, repo);
12379 error = check_same_branch(wt_branch_tip, branch,
12382 if (error->code != GOT_ERR_ANCESTRY)
12386 static char msg[512];
12387 snprintf(msg, sizeof(msg),
12388 "cannot create a merge commit because "
12389 "%s is based on %s; %s can be integrated "
12390 "with 'got integrate' instead", branch_name,
12391 got_worktree_get_head_ref_name(worktree),
12393 error = got_error_msg(GOT_ERR_SAME_BRANCH, msg);
12397 error = got_worktree_merge_prepare(&fileindex, worktree,
12402 error = got_worktree_merge_branch(worktree, fileindex,
12403 yca_id, branch_tip, repo, update_progress, &upa,
12404 check_cancelled, NULL);
12407 print_merge_progress_stats(&upa);
12408 if (!upa.did_something) {
12409 error = got_worktree_merge_abort(worktree, fileindex,
12410 repo, abort_progress, &upa);
12413 printf("Already up-to-date\n");
12418 if (interrupt_merge) {
12419 error = got_worktree_merge_postpone(worktree, fileindex);
12422 printf("Merge of %s interrupted on request\n", branch_name);
12423 } else if (upa.conflicts > 0 || upa.missing > 0 ||
12424 upa.not_deleted > 0 || upa.unversioned > 0) {
12425 error = got_worktree_merge_postpone(worktree, fileindex);
12428 if (upa.conflicts > 0 && upa.missing == 0 &&
12429 upa.not_deleted == 0 && upa.unversioned == 0) {
12430 error = got_error_msg(GOT_ERR_CONFLICTS,
12431 "conflicts must be resolved before merging "
12433 } else if (upa.conflicts > 0) {
12434 error = got_error_msg(GOT_ERR_CONFLICTS,
12435 "conflicts must be resolved before merging "
12436 "can continue; changes destined for some "
12437 "files were not yet merged and "
12438 "should be merged manually if required before the "
12439 "merge operation is continued");
12441 error = got_error_msg(GOT_ERR_CONFLICTS,
12442 "changes destined for some "
12443 "files were not yet merged and should be "
12444 "merged manually if required before the "
12445 "merge operation is continued");
12449 error = got_worktree_merge_commit(&merge_commit_id, worktree,
12450 fileindex, author, NULL, 1, branch_tip, branch_name,
12451 repo, continue_merge ? print_status : NULL, NULL);
12454 error = got_worktree_merge_complete(worktree, fileindex, repo);
12457 error = got_object_id_str(&id_str, merge_commit_id);
12460 printf("Merged %s into %s: %s\n", branch_name,
12461 got_worktree_get_head_ref_name(worktree),
12467 free(merge_commit_id);
12473 got_ref_close(branch);
12475 got_ref_close(wt_branch);
12477 got_worktree_close(worktree);
12479 const struct got_error *close_err = got_repo_close(repo);
12484 const struct got_error *pack_err =
12485 got_repo_pack_fds_close(pack_fds);
12495 fprintf(stderr, "usage: %s stage [-lpS] [-F response-script] "
12496 "[path ...]\n", getprogname());
12500 static const struct got_error *
12501 print_stage(void *arg, unsigned char status, unsigned char staged_status,
12502 const char *path, struct got_object_id *blob_id,
12503 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
12504 int dirfd, const char *de_name)
12506 const struct got_error *err = NULL;
12507 char *id_str = NULL;
12509 if (staged_status != GOT_STATUS_ADD &&
12510 staged_status != GOT_STATUS_MODIFY &&
12511 staged_status != GOT_STATUS_DELETE)
12514 if (staged_status == GOT_STATUS_ADD ||
12515 staged_status == GOT_STATUS_MODIFY)
12516 err = got_object_id_str(&id_str, staged_blob_id);
12518 err = got_object_id_str(&id_str, blob_id);
12522 printf("%s %c %s\n", id_str, staged_status, path);
12527 static const struct got_error *
12528 cmd_stage(int argc, char *argv[])
12530 const struct got_error *error = NULL;
12531 struct got_repository *repo = NULL;
12532 struct got_worktree *worktree = NULL;
12534 struct got_pathlist_head paths;
12535 int ch, list_stage = 0, pflag = 0, allow_bad_symlinks = 0;
12536 FILE *patch_script_file = NULL;
12537 const char *patch_script_path = NULL;
12538 struct choose_patch_arg cpa;
12539 int *pack_fds = NULL;
12541 TAILQ_INIT(&paths);
12543 while ((ch = getopt(argc, argv, "F:lpS")) != -1) {
12546 patch_script_path = optarg;
12555 allow_bad_symlinks = 1;
12567 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
12568 "unveil", NULL) == -1)
12571 if (list_stage && (pflag || patch_script_path))
12572 errx(1, "-l option cannot be used with other options");
12573 if (patch_script_path && !pflag)
12574 errx(1, "-F option can only be used together with -p option");
12576 cwd = getcwd(NULL, 0);
12578 error = got_error_from_errno("getcwd");
12582 error = got_repo_pack_fds_open(&pack_fds);
12586 error = got_worktree_open(&worktree, cwd);
12588 if (error->code == GOT_ERR_NOT_WORKTREE)
12589 error = wrap_not_worktree_error(error, "stage", cwd);
12593 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
12598 if (patch_script_path) {
12599 patch_script_file = fopen(patch_script_path, "re");
12600 if (patch_script_file == NULL) {
12601 error = got_error_from_errno2("fopen",
12602 patch_script_path);
12606 error = apply_unveil(got_repo_get_path(repo), 0,
12607 got_worktree_get_root_path(worktree));
12611 error = check_merge_in_progress(worktree, repo);
12615 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
12620 error = got_worktree_status(worktree, &paths, repo, 0,
12621 print_stage, NULL, check_cancelled, NULL);
12623 cpa.patch_script_file = patch_script_file;
12624 cpa.action = "stage";
12625 error = got_worktree_stage(worktree, &paths,
12626 pflag ? NULL : print_status, NULL,
12627 pflag ? choose_patch : NULL, &cpa,
12628 allow_bad_symlinks, repo);
12631 if (patch_script_file && fclose(patch_script_file) == EOF &&
12633 error = got_error_from_errno2("fclose", patch_script_path);
12635 const struct got_error *close_err = got_repo_close(repo);
12640 got_worktree_close(worktree);
12642 const struct got_error *pack_err =
12643 got_repo_pack_fds_close(pack_fds);
12647 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
12653 usage_unstage(void)
12655 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
12656 "[path ...]\n", getprogname());
12661 static const struct got_error *
12662 cmd_unstage(int argc, char *argv[])
12664 const struct got_error *error = NULL;
12665 struct got_repository *repo = NULL;
12666 struct got_worktree *worktree = NULL;
12668 struct got_pathlist_head paths;
12670 struct got_update_progress_arg upa;
12671 FILE *patch_script_file = NULL;
12672 const char *patch_script_path = NULL;
12673 struct choose_patch_arg cpa;
12674 int *pack_fds = NULL;
12676 TAILQ_INIT(&paths);
12678 while ((ch = getopt(argc, argv, "F:p")) != -1) {
12681 patch_script_path = optarg;
12696 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
12697 "unveil", NULL) == -1)
12700 if (patch_script_path && !pflag)
12701 errx(1, "-F option can only be used together with -p option");
12703 cwd = getcwd(NULL, 0);
12705 error = got_error_from_errno("getcwd");
12709 error = got_repo_pack_fds_open(&pack_fds);
12713 error = got_worktree_open(&worktree, cwd);
12715 if (error->code == GOT_ERR_NOT_WORKTREE)
12716 error = wrap_not_worktree_error(error, "unstage", cwd);
12720 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
12725 if (patch_script_path) {
12726 patch_script_file = fopen(patch_script_path, "re");
12727 if (patch_script_file == NULL) {
12728 error = got_error_from_errno2("fopen",
12729 patch_script_path);
12734 error = apply_unveil(got_repo_get_path(repo), 0,
12735 got_worktree_get_root_path(worktree));
12739 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
12743 cpa.patch_script_file = patch_script_file;
12744 cpa.action = "unstage";
12745 memset(&upa, 0, sizeof(upa));
12746 error = got_worktree_unstage(worktree, &paths, update_progress,
12747 &upa, pflag ? choose_patch : NULL, &cpa, repo);
12749 print_merge_progress_stats(&upa);
12751 if (patch_script_file && fclose(patch_script_file) == EOF &&
12753 error = got_error_from_errno2("fclose", patch_script_path);
12755 const struct got_error *close_err = got_repo_close(repo);
12760 got_worktree_close(worktree);
12762 const struct got_error *pack_err =
12763 got_repo_pack_fds_close(pack_fds);
12767 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
12775 fprintf(stderr, "usage: %s cat [-P] [-c commit] [-r repository-path] "
12776 "arg ...\n", getprogname());
12780 static const struct got_error *
12781 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
12783 const struct got_error *err;
12784 struct got_blob_object *blob;
12787 fd = got_opentempfd();
12789 return got_error_from_errno("got_opentempfd");
12791 err = got_object_open_as_blob(&blob, repo, id, 8192, fd);
12795 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
12797 if (fd != -1 && close(fd) == -1 && err == NULL)
12798 err = got_error_from_errno("close");
12800 got_object_blob_close(blob);
12804 static const struct got_error *
12805 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
12807 const struct got_error *err;
12808 struct got_tree_object *tree;
12811 err = got_object_open_as_tree(&tree, repo, id);
12815 nentries = got_object_tree_get_nentries(tree);
12816 for (i = 0; i < nentries; i++) {
12817 struct got_tree_entry *te;
12819 if (sigint_received || sigpipe_received)
12821 te = got_object_tree_get_entry(tree, i);
12822 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
12825 fprintf(outfile, "%s %.7o %s\n", id_str,
12826 got_tree_entry_get_mode(te),
12827 got_tree_entry_get_name(te));
12831 got_object_tree_close(tree);
12835 static const struct got_error *
12836 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
12838 const struct got_error *err;
12839 struct got_commit_object *commit;
12840 const struct got_object_id_queue *parent_ids;
12841 struct got_object_qid *pid;
12842 char *id_str = NULL;
12843 const char *logmsg = NULL;
12846 err = got_object_open_as_commit(&commit, repo, id);
12850 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
12854 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
12855 parent_ids = got_object_commit_get_parent_ids(commit);
12856 fprintf(outfile, "numparents %d\n",
12857 got_object_commit_get_nparents(commit));
12858 STAILQ_FOREACH(pid, parent_ids, entry) {
12860 err = got_object_id_str(&pid_str, &pid->id);
12863 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
12866 got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
12867 got_object_commit_get_author_gmtoff(commit));
12868 fprintf(outfile, "%s%s %lld %s\n", GOT_COMMIT_LABEL_AUTHOR,
12869 got_object_commit_get_author(commit),
12870 (long long)got_object_commit_get_author_time(commit),
12873 got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
12874 got_object_commit_get_committer_gmtoff(commit));
12875 fprintf(outfile, "%s%s %lld %s\n", GOT_COMMIT_LABEL_COMMITTER,
12876 got_object_commit_get_committer(commit),
12877 (long long)got_object_commit_get_committer_time(commit),
12880 logmsg = got_object_commit_get_logmsg_raw(commit);
12881 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
12882 fprintf(outfile, "%s", logmsg);
12885 got_object_commit_close(commit);
12889 static const struct got_error *
12890 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
12892 const struct got_error *err;
12893 struct got_tag_object *tag;
12894 char *id_str = NULL;
12895 const char *tagmsg = NULL;
12898 err = got_object_open_as_tag(&tag, repo, id);
12902 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
12906 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
12908 switch (got_object_tag_get_object_type(tag)) {
12909 case GOT_OBJ_TYPE_BLOB:
12910 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
12911 GOT_OBJ_LABEL_BLOB);
12913 case GOT_OBJ_TYPE_TREE:
12914 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
12915 GOT_OBJ_LABEL_TREE);
12917 case GOT_OBJ_TYPE_COMMIT:
12918 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
12919 GOT_OBJ_LABEL_COMMIT);
12921 case GOT_OBJ_TYPE_TAG:
12922 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
12923 GOT_OBJ_LABEL_TAG);
12929 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
12930 got_object_tag_get_name(tag));
12932 got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
12933 got_object_tag_get_tagger_gmtoff(tag));
12934 fprintf(outfile, "%s%s %lld %s\n", GOT_TAG_LABEL_TAGGER,
12935 got_object_tag_get_tagger(tag),
12936 (long long)got_object_tag_get_tagger_time(tag),
12939 tagmsg = got_object_tag_get_message(tag);
12940 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
12941 fprintf(outfile, "%s", tagmsg);
12944 got_object_tag_close(tag);
12948 static const struct got_error *
12949 cmd_cat(int argc, char *argv[])
12951 const struct got_error *error;
12952 struct got_repository *repo = NULL;
12953 struct got_worktree *worktree = NULL;
12954 char *cwd = NULL, *repo_path = NULL, *label = NULL;
12955 const char *commit_id_str = NULL;
12956 struct got_object_id *id = NULL, *commit_id = NULL;
12957 struct got_commit_object *commit = NULL;
12958 int ch, obj_type, i, force_path = 0;
12959 struct got_reflist_head refs;
12960 int *pack_fds = NULL;
12965 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
12970 while ((ch = getopt(argc, argv, "c:Pr:")) != -1) {
12973 commit_id_str = optarg;
12979 repo_path = realpath(optarg, NULL);
12980 if (repo_path == NULL)
12981 return got_error_from_errno2("realpath",
12983 got_path_strip_trailing_slashes(repo_path);
12994 cwd = getcwd(NULL, 0);
12996 error = got_error_from_errno("getcwd");
13000 error = got_repo_pack_fds_open(&pack_fds);
13004 if (repo_path == NULL) {
13005 error = got_worktree_open(&worktree, cwd);
13006 if (error && error->code != GOT_ERR_NOT_WORKTREE)
13009 repo_path = strdup(
13010 got_worktree_get_repo_path(worktree));
13011 if (repo_path == NULL) {
13012 error = got_error_from_errno("strdup");
13016 /* Release work tree lock. */
13017 got_worktree_close(worktree);
13022 if (repo_path == NULL) {
13023 repo_path = strdup(cwd);
13024 if (repo_path == NULL)
13025 return got_error_from_errno("strdup");
13028 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
13033 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
13037 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
13041 if (commit_id_str == NULL)
13042 commit_id_str = GOT_REF_HEAD;
13043 error = got_repo_match_object_id(&commit_id, NULL,
13044 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
13048 error = got_object_open_as_commit(&commit, repo, commit_id);
13052 for (i = 0; i < argc; i++) {
13054 error = got_object_id_by_path(&id, repo, commit,
13059 error = got_repo_match_object_id(&id, &label, argv[i],
13060 GOT_OBJ_TYPE_ANY, NULL /* do not resolve tags */,
13063 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
13064 error->code != GOT_ERR_NOT_REF)
13066 error = got_object_id_by_path(&id, repo,
13073 error = got_object_get_type(&obj_type, repo, id);
13077 switch (obj_type) {
13078 case GOT_OBJ_TYPE_BLOB:
13079 error = cat_blob(id, repo, stdout);
13081 case GOT_OBJ_TYPE_TREE:
13082 error = cat_tree(id, repo, stdout);
13084 case GOT_OBJ_TYPE_COMMIT:
13085 error = cat_commit(id, repo, stdout);
13087 case GOT_OBJ_TYPE_TAG:
13088 error = cat_tag(id, repo, stdout);
13091 error = got_error(GOT_ERR_OBJ_TYPE);
13106 got_object_commit_close(commit);
13108 got_worktree_close(worktree);
13110 const struct got_error *close_err = got_repo_close(repo);
13115 const struct got_error *pack_err =
13116 got_repo_pack_fds_close(pack_fds);
13121 got_ref_list_free(&refs);
13128 fprintf(stderr, "usage: %s info [path ...]\n",
13133 static const struct got_error *
13134 print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
13135 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
13136 struct got_object_id *commit_id)
13138 const struct got_error *err = NULL;
13139 char *id_str = NULL;
13141 struct tm mytm, *tm;
13142 struct got_pathlist_head *paths = arg;
13143 struct got_pathlist_entry *pe;
13146 * Clear error indication from any of the path arguments which
13147 * would cause this file index entry to be displayed.
13149 TAILQ_FOREACH(pe, paths, entry) {
13150 if (got_path_cmp(path, pe->path, strlen(path),
13151 pe->path_len) == 0 ||
13152 got_path_is_child(path, pe->path, pe->path_len))
13153 pe->data = NULL; /* no error */
13156 printf(GOT_COMMIT_SEP_STR);
13158 printf("symlink: %s\n", path);
13159 else if (S_ISREG(mode)) {
13160 printf("file: %s\n", path);
13161 printf("mode: %o\n", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
13162 } else if (S_ISDIR(mode))
13163 printf("directory: %s\n", path);
13165 printf("something: %s\n", path);
13167 tm = localtime_r(&mtime, &mytm);
13170 if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) == 0)
13171 return got_error(GOT_ERR_NO_SPACE);
13172 printf("timestamp: %s\n", datebuf);
13175 err = got_object_id_str(&id_str, blob_id);
13178 printf("based on blob: %s\n", id_str);
13182 if (staged_blob_id) {
13183 err = got_object_id_str(&id_str, staged_blob_id);
13186 printf("based on staged blob: %s\n", id_str);
13191 err = got_object_id_str(&id_str, commit_id);
13194 printf("based on commit: %s\n", id_str);
13201 static const struct got_error *
13202 cmd_info(int argc, char *argv[])
13204 const struct got_error *error = NULL;
13205 struct got_worktree *worktree = NULL;
13206 char *cwd = NULL, *id_str = NULL;
13207 struct got_pathlist_head paths;
13208 char *uuidstr = NULL;
13209 int ch, show_files = 0;
13211 TAILQ_INIT(&paths);
13213 while ((ch = getopt(argc, argv, "")) != -1) {
13225 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
13229 cwd = getcwd(NULL, 0);
13231 error = got_error_from_errno("getcwd");
13235 error = got_worktree_open(&worktree, cwd);
13237 if (error->code == GOT_ERR_NOT_WORKTREE)
13238 error = wrap_not_worktree_error(error, "info", cwd);
13243 /* Remove "wpath cpath proc exec sendfd" promises. */
13244 if (pledge("stdio rpath flock unveil", NULL) == -1)
13247 error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
13252 error = get_worktree_paths_from_argv(&paths, argc, argv,
13259 error = got_object_id_str(&id_str,
13260 got_worktree_get_base_commit_id(worktree));
13264 error = got_worktree_get_uuid(&uuidstr, worktree);
13268 printf("work tree: %s\n", got_worktree_get_root_path(worktree));
13269 printf("work tree base commit: %s\n", id_str);
13270 printf("work tree path prefix: %s\n",
13271 got_worktree_get_path_prefix(worktree));
13272 printf("work tree branch reference: %s\n",
13273 got_worktree_get_head_ref_name(worktree));
13274 printf("work tree UUID: %s\n", uuidstr);
13275 printf("repository: %s\n", got_worktree_get_repo_path(worktree));
13278 struct got_pathlist_entry *pe;
13279 TAILQ_FOREACH(pe, &paths, entry) {
13280 if (pe->path_len == 0)
13283 * Assume this path will fail. This will be corrected
13284 * in print_path_info() in case the path does suceeed.
13286 pe->data = (void *)got_error(GOT_ERR_BAD_PATH);
13288 error = got_worktree_path_info(worktree, &paths,
13289 print_path_info, &paths, check_cancelled, NULL);
13292 TAILQ_FOREACH(pe, &paths, entry) {
13293 if (pe->data != NULL) {
13294 const struct got_error *perr;
13297 error = got_error_fmt(perr->code, "%s",
13305 got_worktree_close(worktree);
13306 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);