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 "got_compat.h"
21 #include <sys/queue.h>
23 #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"
64 #include "got_keyword.h"
67 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
70 #ifndef GOT_DEFAULT_EDITOR
71 #define GOT_DEFAULT_EDITOR "/usr/bin/vi"
74 static volatile sig_atomic_t sigint_received;
75 static volatile sig_atomic_t sigpipe_received;
78 catch_sigint(int signo)
84 catch_sigpipe(int signo)
92 const struct got_error *(*cmd_main)(int, char *[]);
93 void (*cmd_usage)(void);
94 const char *cmd_alias;
97 __dead static void usage(int, int);
98 __dead static void usage_init(void);
99 __dead static void usage_import(void);
100 __dead static void usage_clone(void);
101 __dead static void usage_fetch(void);
102 __dead static void usage_checkout(void);
103 __dead static void usage_update(void);
104 __dead static void usage_log(void);
105 __dead static void usage_diff(void);
106 __dead static void usage_blame(void);
107 __dead static void usage_tree(void);
108 __dead static void usage_status(void);
109 __dead static void usage_ref(void);
110 __dead static void usage_branch(void);
111 __dead static void usage_tag(void);
112 __dead static void usage_add(void);
113 __dead static void usage_remove(void);
114 __dead static void usage_patch(void);
115 __dead static void usage_revert(void);
116 __dead static void usage_commit(void);
117 __dead static void usage_send(void);
118 __dead static void usage_cherrypick(void);
119 __dead static void usage_backout(void);
120 __dead static void usage_rebase(void);
121 __dead static void usage_histedit(void);
122 __dead static void usage_integrate(void);
123 __dead static void usage_merge(void);
124 __dead static void usage_stage(void);
125 __dead static void usage_unstage(void);
126 __dead static void usage_cat(void);
127 __dead static void usage_info(void);
129 static const struct got_error* cmd_init(int, char *[]);
130 static const struct got_error* cmd_import(int, char *[]);
131 static const struct got_error* cmd_clone(int, char *[]);
132 static const struct got_error* cmd_fetch(int, char *[]);
133 static const struct got_error* cmd_checkout(int, char *[]);
134 static const struct got_error* cmd_update(int, char *[]);
135 static const struct got_error* cmd_log(int, char *[]);
136 static const struct got_error* cmd_diff(int, char *[]);
137 static const struct got_error* cmd_blame(int, char *[]);
138 static const struct got_error* cmd_tree(int, char *[]);
139 static const struct got_error* cmd_status(int, char *[]);
140 static const struct got_error* cmd_ref(int, char *[]);
141 static const struct got_error* cmd_branch(int, char *[]);
142 static const struct got_error* cmd_tag(int, char *[]);
143 static const struct got_error* cmd_add(int, char *[]);
144 static const struct got_error* cmd_remove(int, char *[]);
145 static const struct got_error* cmd_patch(int, char *[]);
146 static const struct got_error* cmd_revert(int, char *[]);
147 static const struct got_error* cmd_commit(int, char *[]);
148 static const struct got_error* cmd_send(int, char *[]);
149 static const struct got_error* cmd_cherrypick(int, char *[]);
150 static const struct got_error* cmd_backout(int, char *[]);
151 static const struct got_error* cmd_rebase(int, char *[]);
152 static const struct got_error* cmd_histedit(int, char *[]);
153 static const struct got_error* cmd_integrate(int, char *[]);
154 static const struct got_error* cmd_merge(int, char *[]);
155 static const struct got_error* cmd_stage(int, char *[]);
156 static const struct got_error* cmd_unstage(int, char *[]);
157 static const struct got_error* cmd_cat(int, char *[]);
158 static const struct got_error* cmd_info(int, char *[]);
160 static const struct got_cmd got_commands[] = {
161 { "init", cmd_init, usage_init, "" },
162 { "import", cmd_import, usage_import, "im" },
163 { "clone", cmd_clone, usage_clone, "cl" },
164 { "fetch", cmd_fetch, usage_fetch, "fe" },
165 { "checkout", cmd_checkout, usage_checkout, "co" },
166 { "update", cmd_update, usage_update, "up" },
167 { "log", cmd_log, usage_log, "" },
168 { "diff", cmd_diff, usage_diff, "di" },
169 { "blame", cmd_blame, usage_blame, "bl" },
170 { "tree", cmd_tree, usage_tree, "tr" },
171 { "status", cmd_status, usage_status, "st" },
172 { "ref", cmd_ref, usage_ref, "" },
173 { "branch", cmd_branch, usage_branch, "br" },
174 { "tag", cmd_tag, usage_tag, "" },
175 { "add", cmd_add, usage_add, "" },
176 { "remove", cmd_remove, usage_remove, "rm" },
177 { "patch", cmd_patch, usage_patch, "pa" },
178 { "revert", cmd_revert, usage_revert, "rv" },
179 { "commit", cmd_commit, usage_commit, "ci" },
180 { "send", cmd_send, usage_send, "se" },
181 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
182 { "backout", cmd_backout, usage_backout, "bo" },
183 { "rebase", cmd_rebase, usage_rebase, "rb" },
184 { "histedit", cmd_histedit, usage_histedit, "he" },
185 { "integrate", cmd_integrate, usage_integrate,"ig" },
186 { "merge", cmd_merge, usage_merge, "mg" },
187 { "stage", cmd_stage, usage_stage, "sg" },
188 { "unstage", cmd_unstage, usage_unstage, "ug" },
189 { "cat", cmd_cat, usage_cat, "" },
190 { "info", cmd_info, usage_info, "" },
194 list_commands(FILE *fp)
198 fprintf(fp, "commands:");
199 for (i = 0; i < nitems(got_commands); i++) {
200 const struct got_cmd *cmd = &got_commands[i];
201 fprintf(fp, " %s", cmd->cmd_name);
207 option_conflict(char a, char b)
209 errx(1, "-%c and -%c options are mutually exclusive", a, b);
213 main(int argc, char *argv[])
215 const struct got_cmd *cmd;
218 int hflag = 0, Vflag = 0;
219 static const struct option longopts[] = {
220 { "version", no_argument, NULL, 'V' },
224 setlocale(LC_CTYPE, "");
226 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
246 got_version_print_str();
251 usage(hflag, hflag ? 0 : 1);
253 signal(SIGINT, catch_sigint);
254 signal(SIGPIPE, catch_sigpipe);
256 for (i = 0; i < nitems(got_commands); i++) {
257 const struct got_error *error;
259 cmd = &got_commands[i];
261 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
262 strcmp(cmd->cmd_alias, argv[0]) != 0)
268 error = cmd->cmd_main(argc, argv);
269 if (error && error->code != GOT_ERR_CANCELLED &&
270 error->code != GOT_ERR_PRIVSEP_EXIT &&
271 !(sigpipe_received &&
272 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
274 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
276 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
283 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
284 list_commands(stderr);
289 usage(int hflag, int status)
291 FILE *fp = (status == 0) ? stdout : stderr;
293 fprintf(fp, "usage: %s [-hV] command [arg ...]\n",
300 static const struct got_error *
301 get_editor(char **abspath)
303 const struct got_error *err = NULL;
308 editor = getenv("VISUAL");
310 editor = getenv("EDITOR");
313 err = got_path_find_prog(abspath, editor);
318 if (*abspath == NULL) {
319 *abspath = strdup(GOT_DEFAULT_EDITOR);
320 if (*abspath == NULL)
321 return got_error_from_errno("strdup");
327 static const struct got_error *
328 apply_unveil(const char *repo_path, int repo_read_only,
329 const char *worktree_path)
331 const struct got_error *err;
334 if (unveil("gmon.out", "rwc") != 0)
335 return got_error_from_errno2("unveil", "gmon.out");
337 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
338 return got_error_from_errno2("unveil", repo_path);
340 if (worktree_path && unveil(worktree_path, "rwc") != 0)
341 return got_error_from_errno2("unveil", worktree_path);
343 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
344 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
346 err = got_privsep_unveil_exec_helpers();
350 if (unveil(NULL, NULL) != 0)
351 return got_error_from_errno("unveil");
359 fprintf(stderr, "usage: %s init [-A hashing-algorithm] [-b branch]"
360 " repository-path\n",
365 static const struct got_error *
366 cmd_init(int argc, char *argv[])
368 const struct got_error *error = NULL;
369 const char *head_name = NULL;
370 char *repo_path = NULL;
371 enum got_hash_algorithm algo = GOT_HASH_SHA1;
374 while ((ch = getopt(argc, argv, "A:b:")) != -1) {
377 if (!strcmp(optarg, "sha1"))
378 algo = GOT_HASH_SHA1;
379 else if (!strcmp(optarg, "sha256"))
380 algo = GOT_HASH_SHA256;
382 return got_error_path(optarg,
383 GOT_ERR_OBJECT_FORMAT);
398 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
404 repo_path = strdup(argv[0]);
405 if (repo_path == NULL)
406 return got_error_from_errno("strdup");
408 got_path_strip_trailing_slashes(repo_path);
410 error = got_path_mkdir(repo_path);
412 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
415 error = apply_unveil(repo_path, 0, NULL);
419 error = got_repo_init(repo_path, head_name, algo);
428 fprintf(stderr, "usage: %s import [-b branch] [-I pattern] [-m message] "
429 "[-r repository-path] directory\n", getprogname());
434 spawn_editor(const char *editor, const char *file)
437 sig_t sighup, sigint, sigquit;
440 sighup = signal(SIGHUP, SIG_IGN);
441 sigint = signal(SIGINT, SIG_IGN);
442 sigquit = signal(SIGQUIT, SIG_IGN);
444 switch (pid = fork()) {
448 execl(editor, editor, file, (char *)NULL);
452 while (waitpid(pid, &st, 0) == -1)
457 (void)signal(SIGHUP, sighup);
458 (void)signal(SIGINT, sigint);
459 (void)signal(SIGQUIT, sigquit);
461 if (!WIFEXITED(st)) {
466 return WEXITSTATUS(st);
469 static const struct got_error *
470 read_logmsg(char **logmsg, size_t *len, FILE *fp, size_t filesize)
472 const struct got_error *err = NULL;
479 if (fseeko(fp, 0L, SEEK_SET) == -1)
480 return got_error_from_errno("fseeko");
482 *logmsg = malloc(filesize + 1);
484 return got_error_from_errno("malloc");
487 while (getline(&line, &linesize, fp) != -1) {
488 if (line[0] == '#' || (*len == 0 && line[0] == '\n'))
489 continue; /* remove comments and leading empty lines */
490 *len = strlcat(*logmsg, line, filesize + 1);
491 if (*len >= filesize + 1) {
492 err = got_error(GOT_ERR_NO_SPACE);
497 err = got_ferror(fp, GOT_ERR_IO);
501 while (*len > 0 && (*logmsg)[*len - 1] == '\n') {
502 (*logmsg)[*len - 1] = '\0';
515 static const struct got_error *
516 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
517 const char *initial_content, size_t initial_content_len,
518 int require_modification)
520 const struct got_error *err = NULL;
527 if (stat(logmsg_path, &st) == -1)
528 return got_error_from_errno2("stat", logmsg_path);
530 if (spawn_editor(editor, logmsg_path) == -1)
531 return got_error_from_errno("failed spawning editor");
533 if (require_modification) {
534 struct timespec timeout;
538 nanosleep(&timeout, NULL);
541 if (stat(logmsg_path, &st2) == -1)
542 return got_error_from_errno2("stat", logmsg_path);
544 if (require_modification && st.st_size == st2.st_size &&
545 timespeccmp(&st.st_mtim, &st2.st_mtim, ==))
546 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
547 "no changes made to commit message, aborting");
549 fp = fopen(logmsg_path, "re");
551 err = got_error_from_errno("fopen");
555 /* strip comments and leading/trailing newlines */
556 err = read_logmsg(logmsg, &logmsg_len, fp, st2.st_size);
559 if (logmsg_len == 0) {
560 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
561 "commit message cannot be empty, aborting");
565 if (fp && fclose(fp) == EOF && err == NULL)
566 err = got_error_from_errno("fclose");
574 static const struct got_error *
575 collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
576 const char *path_dir, const char *branch_name)
578 char *initial_content = NULL;
579 const struct got_error *err = NULL;
580 int initial_content_len;
583 initial_content_len = asprintf(&initial_content,
584 "\n# %s to be imported to branch %s\n", path_dir,
586 if (initial_content_len == -1)
587 return got_error_from_errno("asprintf");
589 err = got_opentemp_named_fd(logmsg_path, &fd,
590 GOT_TMPDIR_STR "/got-importmsg", "");
594 if (write(fd, initial_content, initial_content_len) == -1) {
595 err = got_error_from_errno2("write", *logmsg_path);
598 if (close(fd) == -1) {
599 err = got_error_from_errno2("close", *logmsg_path);
604 err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content,
605 initial_content_len, 1);
607 if (fd != -1 && close(fd) == -1 && err == NULL)
608 err = got_error_from_errno2("close", *logmsg_path);
609 free(initial_content);
617 static const struct got_error *
618 import_progress(void *arg, const char *path)
620 printf("A %s\n", path);
624 static const struct got_error *
625 valid_author(const char *author)
627 const char *email = author;
630 * Git' expects the author (or committer) to be in the form
631 * "name <email>", which are mostly free form (see the
632 * "committer" description in git-fast-import(1)). We're only
633 * doing this to avoid git's object parser breaking on commits
637 while (*author && *author != '\n' && *author != '<' && *author != '>')
639 if (author != email && *author == '<' && *(author - 1) != ' ')
640 return got_error_fmt(GOT_ERR_COMMIT_BAD_AUTHOR, "%s: space "
641 "between author name and email required", email);
642 if (*author++ != '<')
643 return got_error_fmt(GOT_ERR_COMMIT_NO_EMAIL, "%s", email);
644 while (*author && *author != '\n' && *author != '<' && *author != '>')
646 if (strcmp(author, ">") != 0)
647 return got_error_fmt(GOT_ERR_COMMIT_NO_EMAIL, "%s", email);
651 static const struct got_error *
652 get_author(char **author, struct got_repository *repo,
653 struct got_worktree *worktree)
655 const struct got_error *err = NULL;
656 const char *got_author = NULL, *name, *email;
657 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
662 worktree_conf = got_worktree_get_gotconfig(worktree);
663 repo_conf = got_repo_get_gotconfig(repo);
666 * Priority of potential author information sources, from most
667 * significant to least significant:
668 * 1) work tree's .got/got.conf file
669 * 2) repository's got.conf file
670 * 3) repository's git config file
671 * 4) environment variables
672 * 5) global git config files (in user's home directory or /etc)
676 got_author = got_gotconfig_get_author(worktree_conf);
677 if (got_author == NULL)
678 got_author = got_gotconfig_get_author(repo_conf);
679 if (got_author == NULL) {
680 name = got_repo_get_gitconfig_author_name(repo);
681 email = got_repo_get_gitconfig_author_email(repo);
683 if (asprintf(author, "%s <%s>", name, email) == -1)
684 return got_error_from_errno("asprintf");
688 got_author = getenv("GOT_AUTHOR");
689 if (got_author == NULL) {
690 name = got_repo_get_global_gitconfig_author_name(repo);
691 email = got_repo_get_global_gitconfig_author_email(
694 if (asprintf(author, "%s <%s>", name, email)
696 return got_error_from_errno("asprintf");
699 /* TODO: Look up user in password database? */
700 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
704 *author = strdup(got_author);
706 return got_error_from_errno("strdup");
708 err = valid_author(*author);
716 static const struct got_error *
717 get_allowed_signers(char **allowed_signers, struct got_repository *repo,
718 struct got_worktree *worktree)
720 const char *got_allowed_signers = NULL;
721 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
723 *allowed_signers = NULL;
726 worktree_conf = got_worktree_get_gotconfig(worktree);
727 repo_conf = got_repo_get_gotconfig(repo);
730 * Priority of potential author information sources, from most
731 * significant to least significant:
732 * 1) work tree's .got/got.conf file
733 * 2) repository's got.conf file
737 got_allowed_signers = got_gotconfig_get_allowed_signers_file(
739 if (got_allowed_signers == NULL)
740 got_allowed_signers = got_gotconfig_get_allowed_signers_file(
743 if (got_allowed_signers) {
744 *allowed_signers = strdup(got_allowed_signers);
745 if (*allowed_signers == NULL)
746 return got_error_from_errno("strdup");
751 static const struct got_error *
752 get_revoked_signers(char **revoked_signers, struct got_repository *repo,
753 struct got_worktree *worktree)
755 const char *got_revoked_signers = NULL;
756 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
758 *revoked_signers = NULL;
761 worktree_conf = got_worktree_get_gotconfig(worktree);
762 repo_conf = got_repo_get_gotconfig(repo);
765 * Priority of potential author information sources, from most
766 * significant to least significant:
767 * 1) work tree's .got/got.conf file
768 * 2) repository's got.conf file
772 got_revoked_signers = got_gotconfig_get_revoked_signers_file(
774 if (got_revoked_signers == NULL)
775 got_revoked_signers = got_gotconfig_get_revoked_signers_file(
778 if (got_revoked_signers) {
779 *revoked_signers = strdup(got_revoked_signers);
780 if (*revoked_signers == NULL)
781 return got_error_from_errno("strdup");
787 get_signer_id(struct got_repository *repo, struct got_worktree *worktree)
789 const char *got_signer_id = NULL;
790 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
793 worktree_conf = got_worktree_get_gotconfig(worktree);
794 repo_conf = got_repo_get_gotconfig(repo);
797 * Priority of potential author information sources, from most
798 * significant to least significant:
799 * 1) work tree's .got/got.conf file
800 * 2) repository's got.conf file
804 got_signer_id = got_gotconfig_get_signer_id(worktree_conf);
805 if (got_signer_id == NULL)
806 got_signer_id = got_gotconfig_get_signer_id(repo_conf);
808 return got_signer_id;
811 static const struct got_error *
812 get_gitconfig_path(char **gitconfig_path)
814 const char *homedir = getenv("HOME");
816 *gitconfig_path = NULL;
818 if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
819 return got_error_from_errno("asprintf");
825 static const struct got_error *
826 cmd_import(int argc, char *argv[])
828 const struct got_error *error = NULL;
829 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
830 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
831 const char *branch_name = NULL;
832 char *id_str = NULL, *logmsg_path = NULL;
833 char refname[PATH_MAX] = "refs/heads/";
834 struct got_repository *repo = NULL;
835 struct got_reference *branch_ref = NULL, *head_ref = NULL;
836 struct got_object_id *new_commit_id = NULL;
838 struct got_pathlist_head ignores;
839 struct got_pathlist_entry *pe;
840 int preserve_logmsg = 0;
841 int *pack_fds = NULL;
843 TAILQ_INIT(&ignores);
846 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
852 while ((ch = getopt(argc, argv, "b:I:m:r:")) != -1) {
855 branch_name = optarg;
858 if (optarg[0] == '\0')
860 error = got_pathlist_insert(&pe, &ignores, optarg,
866 logmsg = strdup(optarg);
867 if (logmsg == NULL) {
868 error = got_error_from_errno("strdup");
873 repo_path = realpath(optarg, NULL);
874 if (repo_path == NULL) {
875 error = got_error_from_errno2("realpath",
892 if (repo_path == NULL) {
893 repo_path = getcwd(NULL, 0);
894 if (repo_path == NULL)
895 return got_error_from_errno("getcwd");
897 got_path_strip_trailing_slashes(repo_path);
898 error = get_gitconfig_path(&gitconfig_path);
901 error = got_repo_pack_fds_open(&pack_fds);
904 error = got_repo_open(&repo, repo_path, gitconfig_path, pack_fds);
908 path_dir = realpath(argv[0], NULL);
909 if (path_dir == NULL) {
910 error = got_error_from_errno2("realpath", argv[0]);
913 got_path_strip_trailing_slashes(path_dir);
915 error = get_editor(&editor);
919 if (unveil(path_dir, "r") != 0) {
920 error = got_error_from_errno2("unveil", path_dir);
923 if (unveil(editor, "x") != 0) {
924 error = got_error_from_errno2("unveil", editor);
927 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
931 error = get_author(&author, repo, NULL);
936 * Don't let the user create a branch name with a leading '-'.
937 * While technically a valid reference name, this case is usually
938 * an unintended typo.
940 if (branch_name && branch_name[0] == '-')
941 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
943 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
944 if (error && error->code != GOT_ERR_NOT_REF)
948 n = strlcat(refname, branch_name, sizeof(refname));
949 else if (head_ref && got_ref_is_symbolic(head_ref))
950 n = strlcpy(refname, got_ref_get_symref_target(head_ref),
953 n = strlcat(refname, "main", sizeof(refname));
954 if (n >= sizeof(refname)) {
955 error = got_error(GOT_ERR_NO_SPACE);
959 error = got_ref_open(&branch_ref, repo, refname, 0);
961 if (error->code != GOT_ERR_NOT_REF)
964 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
965 "import target branch already exists");
969 if (logmsg == NULL || *logmsg == '\0') {
971 error = collect_import_msg(&logmsg, &logmsg_path, editor,
974 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
981 error = got_repo_import(&new_commit_id, path_dir, logmsg,
982 author, &ignores, repo, import_progress, NULL);
989 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
996 error = got_ref_write(branch_ref, repo);
1003 error = got_object_id_str(&id_str, new_commit_id);
1006 preserve_logmsg = 1;
1010 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1012 if (error->code != GOT_ERR_NOT_REF) {
1014 preserve_logmsg = 1;
1018 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
1022 preserve_logmsg = 1;
1026 error = got_ref_write(head_ref, repo);
1029 preserve_logmsg = 1;
1034 printf("Created branch %s with commit %s\n",
1035 got_ref_get_name(branch_ref), id_str);
1038 const struct got_error *pack_err =
1039 got_repo_pack_fds_close(pack_fds);
1044 const struct got_error *close_err = got_repo_close(repo);
1048 if (preserve_logmsg) {
1049 fprintf(stderr, "%s: log message preserved in %s\n",
1050 getprogname(), logmsg_path);
1051 } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
1052 error = got_error_from_errno2("unlink", logmsg_path);
1057 free(new_commit_id);
1060 free(gitconfig_path);
1062 got_ref_close(branch_ref);
1064 got_ref_close(head_ref);
1071 fprintf(stderr, "usage: %s clone [-almqv] [-b branch] [-R reference] "
1072 "repository-URL [directory]\n", getprogname());
1076 struct got_fetch_progress_arg {
1077 char last_scaled_size[FMT_SCALED_STRSIZE];
1079 int last_p_resolved;
1082 struct got_repository *repo;
1085 int configs_created;
1087 struct got_pathlist_head *symrefs;
1088 struct got_pathlist_head *wanted_branches;
1089 struct got_pathlist_head *wanted_refs;
1093 const char *remote_repo_path;
1094 const char *git_url;
1095 int fetch_all_branches;
1096 int mirror_references;
1100 /* XXX forward declaration */
1101 static const struct got_error *
1102 create_config_files(const char *proto, const char *host, const char *port,
1103 const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1104 int mirror_references, struct got_pathlist_head *symrefs,
1105 struct got_pathlist_head *wanted_branches,
1106 struct got_pathlist_head *wanted_refs, struct got_repository *repo);
1108 static const struct got_error *
1109 fetch_progress(void *arg, const char *message, off_t packfile_size,
1110 int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
1112 const struct got_error *err = NULL;
1113 struct got_fetch_progress_arg *a = arg;
1114 char scaled_size[FMT_SCALED_STRSIZE];
1115 int p_indexed, p_resolved;
1116 int print_size = 0, print_indexed = 0, print_resolved = 0;
1119 * In order to allow a failed clone to be resumed with 'got fetch'
1120 * we try to create configuration files as soon as possible.
1121 * Once the server has sent information about its default branch
1122 * we have all required information.
1124 if (a->create_configs && !a->configs_created &&
1125 !TAILQ_EMPTY(a->config_info.symrefs)) {
1126 err = create_config_files(a->config_info.proto,
1127 a->config_info.host, a->config_info.port,
1128 a->config_info.remote_repo_path,
1129 a->config_info.git_url,
1130 a->config_info.fetch_all_branches,
1131 a->config_info.mirror_references,
1132 a->config_info.symrefs,
1133 a->config_info.wanted_branches,
1134 a->config_info.wanted_refs, a->repo);
1137 a->configs_created = 1;
1140 if (a->verbosity < 0)
1143 if (message && message[0] != '\0') {
1144 printf("\rserver: %s", message);
1149 if (packfile_size > 0 || nobj_indexed > 0) {
1150 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
1151 (a->last_scaled_size[0] == '\0' ||
1152 strcmp(scaled_size, a->last_scaled_size)) != 0) {
1154 if (strlcpy(a->last_scaled_size, scaled_size,
1155 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
1156 return got_error(GOT_ERR_NO_SPACE);
1158 if (nobj_indexed > 0) {
1159 p_indexed = (nobj_indexed * 100) / nobj_total;
1160 if (p_indexed != a->last_p_indexed) {
1161 a->last_p_indexed = p_indexed;
1166 if (nobj_resolved > 0) {
1167 p_resolved = (nobj_resolved * 100) /
1168 (nobj_total - nobj_loose);
1169 if (p_resolved != a->last_p_resolved) {
1170 a->last_p_resolved = p_resolved;
1178 if (print_size || print_indexed || print_resolved)
1181 printf("%*s fetched", FMT_SCALED_STRSIZE - 2, scaled_size);
1183 printf("; indexing %d%%", p_indexed);
1185 printf("; resolving deltas %d%%", p_resolved);
1186 if (print_size || print_indexed || print_resolved)
1192 static const struct got_error *
1193 create_symref(const char *refname, struct got_reference *target_ref,
1194 int verbosity, struct got_repository *repo)
1196 const struct got_error *err;
1197 struct got_reference *head_symref;
1199 err = got_ref_alloc_symref(&head_symref, refname, target_ref);
1203 err = got_ref_write(head_symref, repo);
1204 if (err == NULL && verbosity > 0) {
1205 printf("Created reference %s: %s\n", GOT_REF_HEAD,
1206 got_ref_get_name(target_ref));
1208 got_ref_close(head_symref);
1212 static const struct got_error *
1213 list_remote_refs(struct got_pathlist_head *symrefs,
1214 struct got_pathlist_head *refs)
1216 const struct got_error *err;
1217 struct got_pathlist_entry *pe;
1219 TAILQ_FOREACH(pe, symrefs, entry) {
1220 const char *refname = pe->path;
1221 const char *targetref = pe->data;
1223 printf("%s: %s\n", refname, targetref);
1226 TAILQ_FOREACH(pe, refs, entry) {
1227 const char *refname = pe->path;
1228 struct got_object_id *id = pe->data;
1231 err = got_object_id_str(&id_str, id);
1234 printf("%s: %s\n", refname, id_str);
1241 static const struct got_error *
1242 create_ref(const char *refname, struct got_object_id *id,
1243 int verbosity, struct got_repository *repo)
1245 const struct got_error *err = NULL;
1246 struct got_reference *ref;
1249 err = got_object_id_str(&id_str, id);
1253 err = got_ref_alloc(&ref, refname, id);
1257 err = got_ref_write(ref, repo);
1260 if (err == NULL && verbosity >= 0)
1261 printf("Created reference %s: %s\n", refname, id_str);
1268 match_wanted_ref(const char *refname, const char *wanted_ref)
1270 if (strncmp(refname, "refs/", 5) != 0)
1275 * Prevent fetching of references that won't make any
1276 * sense outside of the remote repository's context.
1278 if (strncmp(refname, "got/", 4) == 0)
1280 if (strncmp(refname, "remotes/", 8) == 0)
1283 if (strncmp(wanted_ref, "refs/", 5) == 0)
1286 /* Allow prefix match. */
1287 if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
1290 /* Allow exact match. */
1291 return (strcmp(refname, wanted_ref) == 0);
1295 is_wanted_ref(struct got_pathlist_head *wanted_refs, const char *refname)
1297 struct got_pathlist_entry *pe;
1299 TAILQ_FOREACH(pe, wanted_refs, entry) {
1300 if (match_wanted_ref(refname, pe->path))
1307 static const struct got_error *
1308 create_wanted_ref(const char *refname, struct got_object_id *id,
1309 const char *remote_repo_name, int verbosity, struct got_repository *repo)
1311 const struct got_error *err;
1312 char *remote_refname;
1314 if (strncmp("refs/", refname, 5) == 0)
1317 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1318 remote_repo_name, refname) == -1)
1319 return got_error_from_errno("asprintf");
1321 err = create_ref(remote_refname, id, verbosity, repo);
1322 free(remote_refname);
1326 static const struct got_error *
1327 create_gotconfig(const char *proto, const char *host, const char *port,
1328 const char *remote_repo_path, const char *default_branch,
1329 int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1330 struct got_pathlist_head *wanted_refs, int mirror_references,
1331 struct got_repository *repo)
1333 const struct got_error *err = NULL;
1334 char *gotconfig_path = NULL;
1335 char *gotconfig = NULL;
1336 FILE *gotconfig_file = NULL;
1337 const char *branchname = NULL;
1338 char *branches = NULL, *refs = NULL;
1341 if (!fetch_all_branches && !TAILQ_EMPTY(wanted_branches)) {
1342 struct got_pathlist_entry *pe;
1343 TAILQ_FOREACH(pe, wanted_branches, entry) {
1345 branchname = pe->path;
1346 if (strncmp(branchname, "refs/heads/", 11) == 0)
1348 if (asprintf(&s, "%s\"%s\" ",
1349 branches ? branches : "", branchname) == -1) {
1350 err = got_error_from_errno("asprintf");
1356 } else if (!fetch_all_branches && default_branch) {
1357 branchname = default_branch;
1358 if (strncmp(branchname, "refs/heads/", 11) == 0)
1360 if (asprintf(&branches, "\"%s\" ", branchname) == -1) {
1361 err = got_error_from_errno("asprintf");
1365 if (!TAILQ_EMPTY(wanted_refs)) {
1366 struct got_pathlist_entry *pe;
1367 TAILQ_FOREACH(pe, wanted_refs, entry) {
1369 const char *refname = pe->path;
1370 if (strncmp(refname, "refs/", 5) == 0)
1372 if (asprintf(&s, "%s\"%s\" ",
1373 refs ? refs : "", refname) == -1) {
1374 err = got_error_from_errno("asprintf");
1382 /* Create got.conf(5). */
1383 gotconfig_path = got_repo_get_path_gotconfig(repo);
1384 if (gotconfig_path == NULL) {
1385 err = got_error_from_errno("got_repo_get_path_gotconfig");
1388 gotconfig_file = fopen(gotconfig_path, "ae");
1389 if (gotconfig_file == NULL) {
1390 err = got_error_from_errno2("fopen", gotconfig_path);
1393 if (asprintf(&gotconfig,
1398 "\trepository \"%s\"\n"
1404 GOT_FETCH_DEFAULT_REMOTE_NAME, host, proto,
1405 port ? "\tport " : "", port ? port : "", port ? "\n" : "",
1406 remote_repo_path, branches ? "\tbranch { " : "",
1407 branches ? branches : "", branches ? "}\n" : "",
1408 refs ? "\treference { " : "", refs ? refs : "", refs ? "}\n" : "",
1409 mirror_references ? "\tmirror_references yes\n" : "",
1410 fetch_all_branches ? "\tfetch_all_branches yes\n" : "") == -1) {
1411 err = got_error_from_errno("asprintf");
1414 n = fwrite(gotconfig, 1, strlen(gotconfig), gotconfig_file);
1415 if (n != strlen(gotconfig)) {
1416 err = got_ferror(gotconfig_file, GOT_ERR_IO);
1421 if (gotconfig_file && fclose(gotconfig_file) == EOF && err == NULL)
1422 err = got_error_from_errno2("fclose", gotconfig_path);
1423 free(gotconfig_path);
1428 static const struct got_error *
1429 create_gitconfig(const char *git_url, const char *default_branch,
1430 int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1431 struct got_pathlist_head *wanted_refs, int mirror_references,
1432 struct got_repository *repo)
1434 const struct got_error *err = NULL;
1435 char *gitconfig_path = NULL;
1436 char *gitconfig = NULL;
1437 FILE *gitconfig_file = NULL;
1438 char *branches = NULL, *refs = NULL;
1439 const char *branchname;
1442 /* Create a config file Git can understand. */
1443 gitconfig_path = got_repo_get_path_gitconfig(repo);
1444 if (gitconfig_path == NULL) {
1445 err = got_error_from_errno("got_repo_get_path_gitconfig");
1448 gitconfig_file = fopen(gitconfig_path, "ae");
1449 if (gitconfig_file == NULL) {
1450 err = got_error_from_errno2("fopen", gitconfig_path);
1453 if (fetch_all_branches) {
1454 if (mirror_references) {
1455 if (asprintf(&branches,
1456 "\tfetch = refs/heads/*:refs/heads/*\n") == -1) {
1457 err = got_error_from_errno("asprintf");
1460 } else if (asprintf(&branches,
1461 "\tfetch = refs/heads/*:refs/remotes/%s/*\n",
1462 GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1463 err = got_error_from_errno("asprintf");
1466 } else if (!TAILQ_EMPTY(wanted_branches)) {
1467 struct got_pathlist_entry *pe;
1468 TAILQ_FOREACH(pe, wanted_branches, entry) {
1470 branchname = pe->path;
1471 if (strncmp(branchname, "refs/heads/", 11) == 0)
1473 if (mirror_references) {
1475 "%s\tfetch = refs/heads/%s:refs/heads/%s\n",
1476 branches ? branches : "",
1477 branchname, branchname) == -1) {
1478 err = got_error_from_errno("asprintf");
1481 } else if (asprintf(&s,
1482 "%s\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1483 branches ? branches : "",
1484 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1485 branchname) == -1) {
1486 err = got_error_from_errno("asprintf");
1494 * If the server specified a default branch, use just that one.
1495 * Otherwise fall back to fetching all branches on next fetch.
1497 if (default_branch) {
1498 branchname = default_branch;
1499 if (strncmp(branchname, "refs/heads/", 11) == 0)
1502 branchname = "*"; /* fall back to all branches */
1503 if (mirror_references) {
1504 if (asprintf(&branches,
1505 "\tfetch = refs/heads/%s:refs/heads/%s\n",
1506 branchname, branchname) == -1) {
1507 err = got_error_from_errno("asprintf");
1510 } else if (asprintf(&branches,
1511 "\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1512 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1513 branchname) == -1) {
1514 err = got_error_from_errno("asprintf");
1518 if (!TAILQ_EMPTY(wanted_refs)) {
1519 struct got_pathlist_entry *pe;
1520 TAILQ_FOREACH(pe, wanted_refs, entry) {
1522 const char *refname = pe->path;
1523 if (strncmp(refname, "refs/", 5) == 0)
1525 if (mirror_references) {
1527 "%s\tfetch = refs/%s:refs/%s\n",
1528 refs ? refs : "", refname, refname) == -1) {
1529 err = got_error_from_errno("asprintf");
1532 } else if (asprintf(&s,
1533 "%s\tfetch = refs/%s:refs/remotes/%s/%s\n",
1535 refname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1537 err = got_error_from_errno("asprintf");
1545 if (asprintf(&gitconfig,
1550 "\tfetch = refs/tags/*:refs/tags/*\n",
1551 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url, branches ? branches : "",
1552 refs ? refs : "") == -1) {
1553 err = got_error_from_errno("asprintf");
1556 n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1557 if (n != strlen(gitconfig)) {
1558 err = got_ferror(gitconfig_file, GOT_ERR_IO);
1562 if (gitconfig_file && fclose(gitconfig_file) == EOF && err == NULL)
1563 err = got_error_from_errno2("fclose", gitconfig_path);
1564 free(gitconfig_path);
1569 static const struct got_error *
1570 create_config_files(const char *proto, const char *host, const char *port,
1571 const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1572 int mirror_references, struct got_pathlist_head *symrefs,
1573 struct got_pathlist_head *wanted_branches,
1574 struct got_pathlist_head *wanted_refs, struct got_repository *repo)
1576 const struct got_error *err = NULL;
1577 const char *default_branch = NULL;
1578 struct got_pathlist_entry *pe;
1581 * If we asked for a set of wanted branches then use the first
1584 if (!TAILQ_EMPTY(wanted_branches)) {
1585 pe = TAILQ_FIRST(wanted_branches);
1586 default_branch = pe->path;
1588 /* First HEAD ref listed by server is the default branch. */
1589 TAILQ_FOREACH(pe, symrefs, entry) {
1590 const char *refname = pe->path;
1591 const char *target = pe->data;
1593 if (strcmp(refname, GOT_REF_HEAD) != 0)
1596 default_branch = target;
1601 /* Create got.conf(5). */
1602 err = create_gotconfig(proto, host, port, remote_repo_path,
1603 default_branch, fetch_all_branches, wanted_branches,
1604 wanted_refs, mirror_references, repo);
1608 /* Create a config file Git can understand. */
1609 return create_gitconfig(git_url, default_branch, fetch_all_branches,
1610 wanted_branches, wanted_refs, mirror_references, repo);
1613 static const struct got_error *
1614 cmd_clone(int argc, char *argv[])
1616 const struct got_error *error = NULL;
1617 const char *uri, *dirname;
1618 char *proto, *host, *port, *repo_name, *server_path;
1619 char *default_destdir = NULL, *id_str = NULL;
1620 const char *repo_path;
1621 struct got_repository *repo = NULL;
1622 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1623 struct got_pathlist_entry *pe;
1624 struct got_object_id *pack_hash = NULL;
1625 int ch, fetchfd = -1, fetchstatus;
1626 pid_t fetchpid = -1;
1627 struct got_fetch_progress_arg fpa;
1628 char *git_url = NULL;
1629 int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
1630 int bflag = 0, list_refs_only = 0;
1631 int *pack_fds = NULL;
1634 TAILQ_INIT(&symrefs);
1635 TAILQ_INIT(&wanted_branches);
1636 TAILQ_INIT(&wanted_refs);
1638 while ((ch = getopt(argc, argv, "ab:lmqR:v")) != -1) {
1641 fetch_all_branches = 1;
1644 error = got_pathlist_append(&wanted_branches,
1654 mirror_references = 1;
1660 error = got_pathlist_append(&wanted_refs,
1668 else if (verbosity < 3)
1679 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1680 option_conflict('a', 'b');
1681 if (list_refs_only) {
1682 if (!TAILQ_EMPTY(&wanted_branches))
1683 option_conflict('l', 'b');
1684 if (fetch_all_branches)
1685 option_conflict('l', 'a');
1686 if (mirror_references)
1687 option_conflict('l', 'm');
1688 if (!TAILQ_EMPTY(&wanted_refs))
1689 option_conflict('l', 'R');
1701 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
1706 if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1707 host, port ? ":" : "", port ? port : "",
1708 server_path[0] != '/' ? "/" : "", server_path) == -1) {
1709 error = got_error_from_errno("asprintf");
1713 if (strcmp(proto, "git") == 0) {
1715 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1716 "sendfd dns inet unveil", NULL) == -1)
1719 } else if (strcmp(proto, "git+ssh") == 0 ||
1720 strcmp(proto, "ssh") == 0 ||
1721 strcmp(proto, "git+http") == 0 ||
1722 strcmp(proto, "http") == 0 ||
1723 strcmp(proto, "git+https") == 0 ||
1724 strcmp(proto, "https") == 0) {
1726 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1727 "sendfd unveil", NULL) == -1)
1731 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1734 if (dirname == NULL) {
1735 if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1736 error = got_error_from_errno("asprintf");
1739 repo_path = default_destdir;
1741 repo_path = dirname;
1743 if (!list_refs_only) {
1744 error = got_path_mkdir(repo_path);
1746 (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1747 !(error->code == GOT_ERR_ERRNO && errno == EEXIST)))
1749 if (!got_path_dir_is_empty(repo_path)) {
1750 error = got_error_path(repo_path,
1751 GOT_ERR_DIR_NOT_EMPTY);
1756 error = got_dial_apply_unveil(proto);
1760 error = apply_unveil(repo_path, 0, NULL);
1765 printf("Connecting to %s\n", git_url);
1767 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1768 server_path, verbosity);
1773 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd",
1777 if (!list_refs_only) {
1778 error = got_repo_init(repo_path, NULL, GOT_HASH_SHA1);
1781 error = got_repo_pack_fds_open(&pack_fds);
1784 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
1789 fpa.last_scaled_size[0] = '\0';
1790 fpa.last_p_indexed = -1;
1791 fpa.last_p_resolved = -1;
1792 fpa.verbosity = verbosity;
1793 fpa.create_configs = 1;
1794 fpa.configs_created = 0;
1796 fpa.config_info.symrefs = &symrefs;
1797 fpa.config_info.wanted_branches = &wanted_branches;
1798 fpa.config_info.wanted_refs = &wanted_refs;
1799 fpa.config_info.proto = proto;
1800 fpa.config_info.host = host;
1801 fpa.config_info.port = port;
1802 fpa.config_info.remote_repo_path = server_path;
1803 fpa.config_info.git_url = git_url;
1804 fpa.config_info.fetch_all_branches = fetch_all_branches;
1805 fpa.config_info.mirror_references = mirror_references;
1806 error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1807 GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1808 fetch_all_branches, &wanted_branches, &wanted_refs,
1809 list_refs_only, verbosity, fetchfd, repo, NULL, NULL, bflag,
1810 fetch_progress, &fpa);
1814 if (list_refs_only) {
1815 error = list_remote_refs(&symrefs, &refs);
1819 if (pack_hash == NULL) {
1820 error = got_error_fmt(GOT_ERR_FETCH_FAILED, "%s",
1821 "server sent an empty pack file");
1824 error = got_object_id_str(&id_str, pack_hash);
1828 printf("\nFetched %s.pack\n", id_str);
1831 /* Set up references provided with the pack file. */
1832 TAILQ_FOREACH(pe, &refs, entry) {
1833 const char *refname = pe->path;
1834 struct got_object_id *id = pe->data;
1835 char *remote_refname;
1837 if (is_wanted_ref(&wanted_refs, refname) &&
1838 !mirror_references) {
1839 error = create_wanted_ref(refname, id,
1840 GOT_FETCH_DEFAULT_REMOTE_NAME,
1841 verbosity - 1, repo);
1847 error = create_ref(refname, id, verbosity - 1, repo);
1851 if (mirror_references)
1854 if (strncmp("refs/heads/", refname, 11) != 0)
1857 if (asprintf(&remote_refname,
1858 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1859 refname + 11) == -1) {
1860 error = got_error_from_errno("asprintf");
1863 error = create_ref(remote_refname, id, verbosity - 1, repo);
1864 free(remote_refname);
1869 /* Set the HEAD reference if the server provided one. */
1870 TAILQ_FOREACH(pe, &symrefs, entry) {
1871 struct got_reference *target_ref;
1872 const char *refname = pe->path;
1873 const char *target = pe->data;
1874 char *remote_refname = NULL, *remote_target = NULL;
1876 if (strcmp(refname, GOT_REF_HEAD) != 0)
1879 error = got_ref_open(&target_ref, repo, target, 0);
1881 if (error->code == GOT_ERR_NOT_REF) {
1888 error = create_symref(refname, target_ref, verbosity, repo);
1889 got_ref_close(target_ref);
1893 if (mirror_references)
1896 if (strncmp("refs/heads/", target, 11) != 0)
1899 if (asprintf(&remote_refname,
1900 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1902 error = got_error_from_errno("asprintf");
1905 if (asprintf(&remote_target,
1906 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1907 target + 11) == -1) {
1908 error = got_error_from_errno("asprintf");
1909 free(remote_refname);
1912 error = got_ref_open(&target_ref, repo, remote_target, 0);
1914 free(remote_refname);
1915 free(remote_target);
1916 if (error->code == GOT_ERR_NOT_REF) {
1922 error = create_symref(remote_refname, target_ref,
1923 verbosity - 1, repo);
1924 free(remote_refname);
1925 free(remote_target);
1926 got_ref_close(target_ref);
1932 * We failed to set the HEAD reference. If we asked for
1933 * a set of wanted branches use the first of one of those
1934 * which could be fetched instead.
1936 TAILQ_FOREACH(pe, &wanted_branches, entry) {
1937 const char *target = pe->path;
1938 struct got_reference *target_ref;
1940 error = got_ref_open(&target_ref, repo, target, 0);
1942 if (error->code == GOT_ERR_NOT_REF) {
1949 error = create_symref(GOT_REF_HEAD, target_ref,
1951 got_ref_close(target_ref);
1957 if (!fpa.configs_created && pe != NULL) {
1958 error = create_config_files(fpa.config_info.proto,
1959 fpa.config_info.host, fpa.config_info.port,
1960 fpa.config_info.remote_repo_path,
1961 fpa.config_info.git_url,
1962 fpa.config_info.fetch_all_branches,
1963 fpa.config_info.mirror_references,
1964 fpa.config_info.symrefs,
1965 fpa.config_info.wanted_branches,
1966 fpa.config_info.wanted_refs, fpa.repo);
1973 printf("Created %s repository '%s'\n",
1974 mirror_references ? "mirrored" : "cloned", repo_path);
1977 const struct got_error *pack_err =
1978 got_repo_pack_fds_close(pack_fds);
1983 if (kill(fetchpid, SIGTERM) == -1)
1984 error = got_error_from_errno("kill");
1985 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1986 error = got_error_from_errno("waitpid");
1988 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1989 error = got_error_from_errno("close");
1991 const struct got_error *close_err = got_repo_close(repo);
1995 got_pathlist_free(&refs, GOT_PATHLIST_FREE_ALL);
1996 got_pathlist_free(&symrefs, GOT_PATHLIST_FREE_ALL);
1997 got_pathlist_free(&wanted_branches, GOT_PATHLIST_FREE_NONE);
1998 got_pathlist_free(&wanted_refs, GOT_PATHLIST_FREE_NONE);
2005 free(default_destdir);
2010 static const struct got_error *
2011 update_ref(struct got_reference *ref, struct got_object_id *new_id,
2012 int replace_tags, int verbosity, struct got_repository *repo)
2014 const struct got_error *err = NULL;
2015 char *new_id_str = NULL;
2016 struct got_object_id *old_id = NULL;
2018 err = got_object_id_str(&new_id_str, new_id);
2022 if (!replace_tags &&
2023 strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
2024 err = got_ref_resolve(&old_id, repo, ref);
2027 if (got_object_id_cmp(old_id, new_id) == 0)
2029 if (verbosity >= 0) {
2030 printf("Rejecting update of existing tag %s: %s\n",
2031 got_ref_get_name(ref), new_id_str);
2036 if (got_ref_is_symbolic(ref)) {
2037 if (verbosity >= 0) {
2038 printf("Replacing reference %s: %s\n",
2039 got_ref_get_name(ref),
2040 got_ref_get_symref_target(ref));
2042 err = got_ref_change_symref_to_ref(ref, new_id);
2045 err = got_ref_write(ref, repo);
2049 err = got_ref_resolve(&old_id, repo, ref);
2052 if (got_object_id_cmp(old_id, new_id) == 0)
2055 err = got_ref_change_ref(ref, new_id);
2058 err = got_ref_write(ref, repo);
2064 printf("Updated %s: %s\n", got_ref_get_name(ref),
2072 static const struct got_error *
2073 update_symref(const char *refname, struct got_reference *target_ref,
2074 int verbosity, struct got_repository *repo)
2076 const struct got_error *err = NULL, *unlock_err;
2077 struct got_reference *symref;
2078 int symref_is_locked = 0;
2080 err = got_ref_open(&symref, repo, refname, 1);
2082 if (err->code != GOT_ERR_NOT_REF)
2084 err = got_ref_alloc_symref(&symref, refname, target_ref);
2088 err = got_ref_write(symref, repo);
2093 printf("Created reference %s: %s\n",
2094 got_ref_get_name(symref),
2095 got_ref_get_symref_target(symref));
2097 symref_is_locked = 1;
2099 if (strcmp(got_ref_get_symref_target(symref),
2100 got_ref_get_name(target_ref)) == 0)
2103 err = got_ref_change_symref(symref,
2104 got_ref_get_name(target_ref));
2108 err = got_ref_write(symref, repo);
2113 printf("Updated %s: %s\n", got_ref_get_name(symref),
2114 got_ref_get_symref_target(symref));
2118 if (symref_is_locked) {
2119 unlock_err = got_ref_unlock(symref);
2120 if (unlock_err && err == NULL)
2123 got_ref_close(symref);
2130 fprintf(stderr, "usage: %s fetch [-adlqtvX] [-b branch] "
2131 "[-R reference] [-r repository-path] [remote-repository]\n",
2136 static const struct got_error *
2137 delete_missing_ref(struct got_reference *ref,
2138 int verbosity, struct got_repository *repo)
2140 const struct got_error *err = NULL;
2141 struct got_object_id *id = NULL;
2142 char *id_str = NULL;
2144 if (got_ref_is_symbolic(ref)) {
2145 err = got_ref_delete(ref, repo);
2148 if (verbosity >= 0) {
2149 printf("Deleted %s: %s\n",
2150 got_ref_get_name(ref),
2151 got_ref_get_symref_target(ref));
2154 err = got_ref_resolve(&id, repo, ref);
2157 err = got_object_id_str(&id_str, id);
2161 err = got_ref_delete(ref, repo);
2164 if (verbosity >= 0) {
2165 printf("Deleted %s: %s\n",
2166 got_ref_get_name(ref), id_str);
2175 static const struct got_error *
2176 delete_missing_refs(struct got_pathlist_head *their_refs,
2177 struct got_pathlist_head *their_symrefs,
2178 const struct got_remote_repo *remote,
2179 int verbosity, struct got_repository *repo)
2181 const struct got_error *err = NULL, *unlock_err;
2182 struct got_reflist_head my_refs;
2183 struct got_reflist_entry *re;
2184 struct got_pathlist_entry *pe;
2185 char *remote_namespace = NULL;
2186 char *local_refname = NULL;
2188 TAILQ_INIT(&my_refs);
2190 if (asprintf(&remote_namespace, "refs/remotes/%s/", remote->name)
2192 return got_error_from_errno("asprintf");
2194 err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
2198 TAILQ_FOREACH(re, &my_refs, entry) {
2199 const char *refname = got_ref_get_name(re->ref);
2200 const char *their_refname;
2202 if (remote->mirror_references) {
2203 their_refname = refname;
2205 if (strncmp(refname, remote_namespace,
2206 strlen(remote_namespace)) == 0) {
2207 if (strcmp(refname + strlen(remote_namespace),
2210 if (asprintf(&local_refname, "refs/heads/%s",
2211 refname + strlen(remote_namespace)) == -1) {
2212 err = got_error_from_errno("asprintf");
2215 } else if (strncmp(refname, "refs/tags/", 10) != 0)
2218 their_refname = local_refname;
2221 TAILQ_FOREACH(pe, their_refs, entry) {
2222 if (strcmp(their_refname, pe->path) == 0)
2228 TAILQ_FOREACH(pe, their_symrefs, entry) {
2229 if (strcmp(their_refname, pe->path) == 0)
2235 err = delete_missing_ref(re->ref, verbosity, repo);
2239 if (local_refname) {
2240 struct got_reference *ref;
2241 err = got_ref_open(&ref, repo, local_refname, 1);
2243 if (err->code != GOT_ERR_NOT_REF)
2245 free(local_refname);
2246 local_refname = NULL;
2249 err = delete_missing_ref(ref, verbosity, repo);
2252 unlock_err = got_ref_unlock(ref);
2254 if (unlock_err && err == NULL) {
2259 free(local_refname);
2260 local_refname = NULL;
2264 got_ref_list_free(&my_refs);
2265 free(remote_namespace);
2266 free(local_refname);
2270 static const struct got_error *
2271 update_wanted_ref(const char *refname, struct got_object_id *id,
2272 const char *remote_repo_name, int verbosity, struct got_repository *repo)
2274 const struct got_error *err, *unlock_err;
2275 char *remote_refname;
2276 struct got_reference *ref;
2278 if (strncmp("refs/", refname, 5) == 0)
2281 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2282 remote_repo_name, refname) == -1)
2283 return got_error_from_errno("asprintf");
2285 err = got_ref_open(&ref, repo, remote_refname, 1);
2287 if (err->code != GOT_ERR_NOT_REF)
2289 err = create_ref(remote_refname, id, verbosity, repo);
2291 err = update_ref(ref, id, 0, verbosity, repo);
2292 unlock_err = got_ref_unlock(ref);
2293 if (unlock_err && err == NULL)
2298 free(remote_refname);
2302 static const struct got_error *
2303 delete_ref(struct got_repository *repo, struct got_reference *ref)
2305 const struct got_error *err = NULL;
2306 struct got_object_id *id = NULL;
2307 char *id_str = NULL;
2310 if (got_ref_is_symbolic(ref)) {
2311 target = got_ref_get_symref_target(ref);
2313 err = got_ref_resolve(&id, repo, ref);
2316 err = got_object_id_str(&id_str, id);
2322 err = got_ref_delete(ref, repo);
2326 printf("Deleted %s: %s\n", got_ref_get_name(ref), target);
2333 static const struct got_error *
2334 delete_refs_for_remote(struct got_repository *repo, const char *remote_name)
2336 const struct got_error *err = NULL;
2337 struct got_reflist_head refs;
2338 struct got_reflist_entry *re;
2343 if (asprintf(&prefix, "refs/remotes/%s", remote_name) == -1) {
2344 err = got_error_from_errno("asprintf");
2347 err = got_ref_list(&refs, repo, prefix, got_ref_cmp_by_name, NULL);
2351 TAILQ_FOREACH(re, &refs, entry)
2352 delete_ref(repo, re->ref);
2354 got_ref_list_free(&refs);
2358 static const struct got_error *
2359 cmd_fetch(int argc, char *argv[])
2361 const struct got_error *error = NULL, *unlock_err;
2362 char *cwd = NULL, *repo_path = NULL;
2363 const char *remote_name;
2364 char *proto = NULL, *host = NULL, *port = NULL;
2365 char *repo_name = NULL, *server_path = NULL;
2366 const struct got_remote_repo *remotes;
2367 struct got_remote_repo *remote = NULL;
2369 char *id_str = NULL;
2370 struct got_repository *repo = NULL;
2371 struct got_worktree *worktree = NULL;
2372 const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
2373 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
2374 char *head_refname = NULL;
2375 struct got_pathlist_entry *pe;
2376 struct got_reflist_head remote_refs;
2377 struct got_reflist_entry *re;
2378 struct got_object_id *pack_hash = NULL;
2379 int i, ch, fetchfd = -1, fetchstatus;
2380 pid_t fetchpid = -1;
2381 struct got_fetch_progress_arg fpa;
2382 int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
2383 int delete_refs = 0, replace_tags = 0, delete_remote = 0;
2384 int *pack_fds = NULL, have_bflag = 0;
2385 const char *remote_head = NULL, *worktree_branch = NULL;
2388 TAILQ_INIT(&symrefs);
2389 TAILQ_INIT(&remote_refs);
2390 TAILQ_INIT(&wanted_branches);
2391 TAILQ_INIT(&wanted_refs);
2393 while ((ch = getopt(argc, argv, "ab:dlqR:r:tvX")) != -1) {
2396 fetch_all_branches = 1;
2399 error = got_pathlist_append(&wanted_branches,
2415 error = got_pathlist_append(&wanted_refs,
2421 repo_path = realpath(optarg, NULL);
2422 if (repo_path == NULL)
2423 return got_error_from_errno2("realpath",
2425 got_path_strip_trailing_slashes(repo_path);
2433 else if (verbosity < 3)
2447 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
2448 option_conflict('a', 'b');
2449 if (list_refs_only) {
2450 if (!TAILQ_EMPTY(&wanted_branches))
2451 option_conflict('l', 'b');
2452 if (fetch_all_branches)
2453 option_conflict('l', 'a');
2455 option_conflict('l', 'd');
2457 option_conflict('l', 'X');
2459 if (delete_remote) {
2460 if (fetch_all_branches)
2461 option_conflict('X', 'a');
2462 if (!TAILQ_EMPTY(&wanted_branches))
2463 option_conflict('X', 'b');
2465 option_conflict('X', 'd');
2467 option_conflict('X', 't');
2468 if (!TAILQ_EMPTY(&wanted_refs))
2469 option_conflict('X', 'R');
2474 errx(1, "-X option requires a remote name");
2475 remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
2476 } else if (argc == 1)
2477 remote_name = argv[0];
2481 cwd = getcwd(NULL, 0);
2483 error = got_error_from_errno("getcwd");
2487 error = got_repo_pack_fds_open(&pack_fds);
2491 if (repo_path == NULL) {
2492 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
2493 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2499 strdup(got_worktree_get_repo_path(worktree));
2500 if (repo_path == NULL)
2501 error = got_error_from_errno("strdup");
2505 repo_path = strdup(cwd);
2506 if (repo_path == NULL) {
2507 error = got_error_from_errno("strdup");
2513 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
2517 if (delete_remote) {
2518 error = delete_refs_for_remote(repo, remote_name);
2519 goto done; /* nothing else to do */
2523 worktree_conf = got_worktree_get_gotconfig(worktree);
2524 if (worktree_conf) {
2525 got_gotconfig_get_remotes(&nremotes, &remotes,
2527 for (i = 0; i < nremotes; i++) {
2528 if (strcmp(remotes[i].name, remote_name) == 0) {
2529 error = got_repo_remote_repo_dup(&remote,
2538 if (remote == NULL) {
2539 repo_conf = got_repo_get_gotconfig(repo);
2541 got_gotconfig_get_remotes(&nremotes, &remotes,
2543 for (i = 0; i < nremotes; i++) {
2544 if (strcmp(remotes[i].name, remote_name) == 0) {
2545 error = got_repo_remote_repo_dup(&remote,
2554 if (remote == NULL) {
2555 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
2556 for (i = 0; i < nremotes; i++) {
2557 if (strcmp(remotes[i].name, remote_name) == 0) {
2558 error = got_repo_remote_repo_dup(&remote,
2566 if (remote == NULL) {
2567 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
2571 if (TAILQ_EMPTY(&wanted_branches)) {
2572 if (!fetch_all_branches)
2573 fetch_all_branches = remote->fetch_all_branches;
2574 for (i = 0; i < remote->nfetch_branches; i++) {
2575 error = got_pathlist_append(&wanted_branches,
2576 remote->fetch_branches[i], NULL);
2581 if (TAILQ_EMPTY(&wanted_refs)) {
2582 for (i = 0; i < remote->nfetch_refs; i++) {
2583 error = got_pathlist_append(&wanted_refs,
2584 remote->fetch_refs[i], NULL);
2590 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
2591 &repo_name, remote->fetch_url);
2595 if (strcmp(proto, "git") == 0) {
2597 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2598 "sendfd dns inet unveil", NULL) == -1)
2601 } else if (strcmp(proto, "git+ssh") == 0 ||
2602 strcmp(proto, "ssh") == 0 ||
2603 strcmp(proto, "git+http") == 0 ||
2604 strcmp(proto, "http") == 0 ||
2605 strcmp(proto, "git+https") == 0 ||
2606 strcmp(proto, "https") == 0) {
2608 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2609 "sendfd unveil", NULL) == -1)
2613 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
2617 error = got_dial_apply_unveil(proto);
2621 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
2626 head_refname = strdup(got_worktree_get_head_ref_name(worktree));
2627 if (head_refname == NULL) {
2628 error = got_error_from_errno("strdup");
2632 /* Release work tree lock. */
2633 got_worktree_close(worktree);
2637 if (verbosity >= 0) {
2638 printf("Connecting to \"%s\" %s://%s%s%s%s%s\n",
2639 remote->name, proto, host,
2640 port ? ":" : "", port ? port : "",
2641 *server_path == '/' ? "" : "/", server_path);
2644 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
2645 server_path, verbosity);
2649 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd",
2655 * If set, get this remote's HEAD ref target so
2656 * if it has changed on the server we can fetch it.
2658 error = got_ref_list(&remote_refs, repo, "refs/remotes",
2659 got_ref_cmp_by_name, repo);
2663 TAILQ_FOREACH(re, &remote_refs, entry) {
2664 const char *remote_refname, *remote_target;
2665 size_t remote_name_len;
2667 if (!got_ref_is_symbolic(re->ref))
2670 remote_name_len = strlen(remote->name);
2671 remote_refname = got_ref_get_name(re->ref);
2673 /* we only want refs/remotes/$remote->name/HEAD */
2674 if (strncmp(remote_refname + 13, remote->name,
2675 remote_name_len) != 0)
2678 if (strcmp(remote_refname + remote_name_len + 14,
2683 * Take the name itself because we already
2684 * only match with refs/heads/ in fetch_pack().
2686 remote_target = got_ref_get_symref_target(re->ref);
2687 remote_head = remote_target + remote_name_len + 14;
2692 strncmp(head_refname, "refs/heads/", 11) == 0)
2693 worktree_branch = head_refname;
2696 fpa.last_scaled_size[0] = '\0';
2697 fpa.last_p_indexed = -1;
2698 fpa.last_p_resolved = -1;
2699 fpa.verbosity = verbosity;
2701 fpa.create_configs = 0;
2702 fpa.configs_created = 0;
2703 memset(&fpa.config_info, 0, sizeof(fpa.config_info));
2705 error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
2706 remote->mirror_references, fetch_all_branches, &wanted_branches,
2707 &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
2708 worktree_branch, remote_head, have_bflag, fetch_progress, &fpa);
2712 if (list_refs_only) {
2713 error = list_remote_refs(&symrefs, &refs);
2717 if (pack_hash == NULL) {
2719 printf("Already up-to-date\n");
2720 } else if (verbosity >= 0) {
2721 error = got_object_id_str(&id_str, pack_hash);
2724 printf("\nFetched %s.pack\n", id_str);
2729 /* Update references provided with the pack file. */
2730 TAILQ_FOREACH(pe, &refs, entry) {
2731 const char *refname = pe->path;
2732 struct got_object_id *id = pe->data;
2733 struct got_reference *ref;
2734 char *remote_refname;
2736 if (is_wanted_ref(&wanted_refs, refname) &&
2737 !remote->mirror_references) {
2738 error = update_wanted_ref(refname, id,
2739 remote->name, verbosity, repo);
2745 if (remote->mirror_references ||
2746 strncmp("refs/tags/", refname, 10) == 0) {
2747 error = got_ref_open(&ref, repo, refname, 1);
2749 if (error->code != GOT_ERR_NOT_REF)
2751 error = create_ref(refname, id, verbosity,
2756 error = update_ref(ref, id, replace_tags,
2758 unlock_err = got_ref_unlock(ref);
2759 if (unlock_err && error == NULL)
2765 } else if (strncmp("refs/heads/", refname, 11) == 0) {
2766 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2767 remote_name, refname + 11) == -1) {
2768 error = got_error_from_errno("asprintf");
2772 error = got_ref_open(&ref, repo, remote_refname, 1);
2774 if (error->code != GOT_ERR_NOT_REF)
2776 error = create_ref(remote_refname, id,
2781 error = update_ref(ref, id, replace_tags,
2783 unlock_err = got_ref_unlock(ref);
2784 if (unlock_err && error == NULL)
2791 /* Also create a local branch if none exists yet. */
2792 error = got_ref_open(&ref, repo, refname, 1);
2794 if (error->code != GOT_ERR_NOT_REF)
2796 error = create_ref(refname, id, verbosity,
2801 unlock_err = got_ref_unlock(ref);
2802 if (unlock_err && error == NULL)
2809 error = delete_missing_refs(&refs, &symrefs, remote,
2815 if (!remote->mirror_references) {
2816 /* Update remote HEAD reference if the server provided one. */
2817 TAILQ_FOREACH(pe, &symrefs, entry) {
2818 struct got_reference *target_ref;
2819 const char *refname = pe->path;
2820 const char *target = pe->data;
2821 char *remote_refname = NULL, *remote_target = NULL;
2823 if (strcmp(refname, GOT_REF_HEAD) != 0)
2826 if (strncmp("refs/heads/", target, 11) != 0)
2829 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2830 remote->name, refname) == -1) {
2831 error = got_error_from_errno("asprintf");
2834 if (asprintf(&remote_target, "refs/remotes/%s/%s",
2835 remote->name, target + 11) == -1) {
2836 error = got_error_from_errno("asprintf");
2837 free(remote_refname);
2841 error = got_ref_open(&target_ref, repo, remote_target,
2844 free(remote_refname);
2845 free(remote_target);
2846 if (error->code == GOT_ERR_NOT_REF) {
2852 error = update_symref(remote_refname, target_ref,
2854 free(remote_refname);
2855 free(remote_target);
2856 got_ref_close(target_ref);
2863 if (kill(fetchpid, SIGTERM) == -1)
2864 error = got_error_from_errno("kill");
2865 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
2866 error = got_error_from_errno("waitpid");
2868 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
2869 error = got_error_from_errno("close");
2871 const struct got_error *close_err = got_repo_close(repo);
2876 got_worktree_close(worktree);
2878 const struct got_error *pack_err =
2879 got_repo_pack_fds_close(pack_fds);
2883 got_pathlist_free(&refs, GOT_PATHLIST_FREE_ALL);
2884 got_pathlist_free(&symrefs, GOT_PATHLIST_FREE_ALL);
2885 got_pathlist_free(&wanted_branches, GOT_PATHLIST_FREE_NONE);
2886 got_pathlist_free(&wanted_refs, GOT_PATHLIST_FREE_NONE);
2887 got_ref_list_free(&remote_refs);
2888 got_repo_free_remote_repo_data(remote);
2905 usage_checkout(void)
2907 fprintf(stderr, "usage: %s checkout [-Eq] [-b branch] [-c commit] "
2908 "[-p path-prefix] repository-path [work-tree-path]\n",
2914 show_worktree_base_ref_warning(void)
2916 fprintf(stderr, "%s: warning: could not create a reference "
2917 "to the work tree's base commit; the commit could be "
2918 "garbage-collected by Git or 'gotadmin cleanup'; making the "
2919 "repository writable and running 'got update' will prevent this\n",
2923 struct got_checkout_progress_arg {
2924 const char *worktree_path;
2925 int had_base_commit_ref_error;
2929 static const struct got_error *
2930 checkout_progress(void *arg, unsigned char status, const char *path)
2932 struct got_checkout_progress_arg *a = arg;
2934 /* Base commit bump happens silently. */
2935 if (status == GOT_STATUS_BUMP_BASE)
2938 if (status == GOT_STATUS_BASE_REF_ERR) {
2939 a->had_base_commit_ref_error = 1;
2943 while (path[0] == '/')
2946 if (a->verbosity >= 0)
2947 printf("%c %s/%s\n", status, a->worktree_path, path);
2952 static const struct got_error *
2953 check_cancelled(void *arg)
2955 if (sigint_received || sigpipe_received)
2956 return got_error(GOT_ERR_CANCELLED);
2960 static const struct got_error *
2961 check_linear_ancestry(struct got_object_id *commit_id,
2962 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2963 struct got_repository *repo)
2965 const struct got_error *err = NULL;
2966 struct got_object_id *yca_id;
2968 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2969 commit_id, base_commit_id, 1, 0, repo, check_cancelled, NULL);
2974 return got_error(GOT_ERR_ANCESTRY);
2977 * Require a straight line of history between the target commit
2978 * and the work tree's base commit.
2980 * Non-linear situations such as this require a rebase:
2982 * (commit) D F (base_commit)
2990 * 'got update' only handles linear cases:
2991 * Update forwards in time: A (base/yca) - B - C - D (commit)
2992 * Update backwards in time: D (base) - C - B - A (commit/yca)
2994 if (allow_forwards_in_time_only) {
2995 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2996 return got_error(GOT_ERR_ANCESTRY);
2997 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2998 got_object_id_cmp(base_commit_id, yca_id) != 0)
2999 return got_error(GOT_ERR_ANCESTRY);
3005 static const struct got_error *
3006 check_same_branch(struct got_object_id *commit_id,
3007 struct got_reference *head_ref, struct got_repository *repo)
3009 const struct got_error *err = NULL;
3010 struct got_commit_graph *graph = NULL;
3011 struct got_object_id *head_commit_id = NULL;
3013 err = got_ref_resolve(&head_commit_id, repo, head_ref);
3017 if (got_object_id_cmp(head_commit_id, commit_id) == 0)
3020 err = got_commit_graph_open(&graph, "/", 1);
3024 err = got_commit_graph_bfsort(graph, head_commit_id, repo,
3025 check_cancelled, NULL);
3030 struct got_object_id id;
3032 err = got_commit_graph_iter_next(&id, graph, repo,
3033 check_cancelled, NULL);
3035 if (err->code == GOT_ERR_ITER_COMPLETED)
3036 err = got_error(GOT_ERR_ANCESTRY);
3040 if (got_object_id_cmp(&id, commit_id) == 0)
3045 got_commit_graph_close(graph);
3046 free(head_commit_id);
3050 static const struct got_error *
3051 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
3053 static char msg[512];
3054 const char *branch_name;
3056 if (got_ref_is_symbolic(ref))
3057 branch_name = got_ref_get_symref_target(ref);
3059 branch_name = got_ref_get_name(ref);
3061 if (strncmp("refs/heads/", branch_name, 11) == 0)
3064 snprintf(msg, sizeof(msg),
3065 "target commit is not contained in branch '%s'; "
3066 "the branch to use must be specified with -b; "
3067 "if necessary a new branch can be created for "
3068 "this commit with 'got branch -c %s BRANCH_NAME'",
3069 branch_name, commit_id_str);
3071 return got_error_msg(GOT_ERR_ANCESTRY, msg);
3074 static const struct got_error *
3075 cmd_checkout(int argc, char *argv[])
3077 const struct got_error *close_err, *error = NULL;
3078 struct got_repository *repo = NULL;
3079 struct got_reference *head_ref = NULL, *ref = NULL;
3080 struct got_worktree *worktree = NULL;
3081 char *repo_path = NULL;
3082 char *worktree_path = NULL;
3083 const char *path_prefix = "";
3084 const char *branch_name = GOT_REF_HEAD, *refname = NULL;
3085 char *commit_id_str = NULL, *keyword_idstr = NULL;
3086 struct got_object_id *commit_id = NULL;
3088 int ch, same_path_prefix, allow_nonempty = 0, verbosity = 0;
3089 struct got_pathlist_head paths;
3090 struct got_checkout_progress_arg cpa;
3091 int *pack_fds = NULL;
3096 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3097 "unveil", NULL) == -1)
3101 while ((ch = getopt(argc, argv, "b:c:Ep:q")) != -1) {
3104 branch_name = optarg;
3107 commit_id_str = strdup(optarg);
3108 if (commit_id_str == NULL)
3109 return got_error_from_errno("strdup");
3115 path_prefix = optarg;
3130 char *base, *dotgit;
3132 repo_path = realpath(argv[0], NULL);
3133 if (repo_path == NULL)
3134 return got_error_from_errno2("realpath", argv[0]);
3135 cwd = getcwd(NULL, 0);
3137 error = got_error_from_errno("getcwd");
3144 error = got_path_basename(&base, path);
3147 dotgit = strstr(base, ".git");
3150 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
3151 error = got_error_from_errno("asprintf");
3156 } else if (argc == 2) {
3157 repo_path = realpath(argv[0], NULL);
3158 if (repo_path == NULL) {
3159 error = got_error_from_errno2("realpath", argv[0]);
3162 worktree_path = realpath(argv[1], NULL);
3163 if (worktree_path == NULL) {
3164 if (errno != ENOENT) {
3165 error = got_error_from_errno2("realpath",
3169 worktree_path = strdup(argv[1]);
3170 if (worktree_path == NULL) {
3171 error = got_error_from_errno("strdup");
3178 got_path_strip_trailing_slashes(repo_path);
3179 got_path_strip_trailing_slashes(worktree_path);
3181 if (got_path_is_child(worktree_path, repo_path, strlen(repo_path)) ||
3182 got_path_is_child(repo_path, worktree_path,
3183 strlen(worktree_path))) {
3184 error = got_error_fmt(GOT_ERR_BAD_PATH,
3185 "work tree and repository paths may not overlap: %s",
3190 error = got_repo_pack_fds_open(&pack_fds);
3194 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3198 /* Pre-create work tree path for unveil(2) */
3199 error = got_path_mkdir(worktree_path);
3201 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
3202 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
3204 if (!allow_nonempty &&
3205 !got_path_dir_is_empty(worktree_path)) {
3206 error = got_error_path(worktree_path,
3207 GOT_ERR_DIR_NOT_EMPTY);
3212 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
3216 error = got_ref_open(&head_ref, repo, branch_name, 0);
3220 error = got_worktree_init(worktree_path, head_ref, path_prefix,
3221 GOT_WORKTREE_GOT_DIR, repo);
3222 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
3225 error = got_worktree_open(&worktree, worktree_path,
3226 GOT_WORKTREE_GOT_DIR);
3230 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
3234 if (!same_path_prefix) {
3235 error = got_error(GOT_ERR_PATH_PREFIX);
3239 if (commit_id_str) {
3240 struct got_reflist_head refs;
3242 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3247 error = got_keyword_to_idstr(&keyword_idstr, commit_id_str,
3251 if (keyword_idstr != NULL) {
3252 free(commit_id_str);
3253 commit_id_str = keyword_idstr;
3256 error = got_repo_match_object_id(&commit_id, NULL,
3257 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3258 got_ref_list_free(&refs);
3261 error = check_linear_ancestry(commit_id,
3262 got_worktree_get_base_commit_id(worktree), 0, repo);
3263 if (error != NULL) {
3264 if (error->code == GOT_ERR_ANCESTRY) {
3265 error = checkout_ancestry_error(
3266 head_ref, commit_id_str);
3270 error = check_same_branch(commit_id, head_ref, repo);
3272 if (error->code == GOT_ERR_ANCESTRY) {
3273 error = checkout_ancestry_error(
3274 head_ref, commit_id_str);
3278 error = got_worktree_set_base_commit_id(worktree, repo,
3282 /* Expand potentially abbreviated commit ID string. */
3283 free(commit_id_str);
3284 error = got_object_id_str(&commit_id_str, commit_id);
3288 commit_id = got_object_id_dup(
3289 got_worktree_get_base_commit_id(worktree));
3290 if (commit_id == NULL) {
3291 error = got_error_from_errno("got_object_id_dup");
3294 error = got_object_id_str(&commit_id_str, commit_id);
3299 error = got_pathlist_append(&paths, "", NULL);
3302 cpa.worktree_path = worktree_path;
3303 cpa.had_base_commit_ref_error = 0;
3304 cpa.verbosity = verbosity;
3305 error = got_worktree_checkout_files(worktree, &paths, repo,
3306 checkout_progress, &cpa, check_cancelled, NULL);
3310 if (got_ref_is_symbolic(head_ref)) {
3311 error = got_ref_resolve_symbolic(&ref, repo, head_ref);
3314 refname = got_ref_get_name(ref);
3316 refname = got_ref_get_name(head_ref);
3317 printf("Checked out %s: %s\n", refname, commit_id_str);
3318 printf("Now shut up and hack\n");
3319 if (cpa.had_base_commit_ref_error)
3320 show_worktree_base_ref_warning();
3323 const struct got_error *pack_err =
3324 got_repo_pack_fds_close(pack_fds);
3329 got_ref_close(head_ref);
3333 close_err = got_repo_close(repo);
3337 if (worktree != NULL) {
3338 close_err = got_worktree_close(worktree);
3342 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
3343 free(commit_id_str);
3346 free(worktree_path);
3351 struct got_update_progress_arg {
3363 print_update_progress_stats(struct got_update_progress_arg *upa)
3365 if (!upa->did_something)
3368 if (upa->conflicts > 0)
3369 printf("Files with new merge conflicts: %d\n", upa->conflicts);
3370 if (upa->obstructed > 0)
3371 printf("File paths obstructed by a non-regular file: %d\n",
3373 if (upa->not_updated > 0)
3374 printf("Files not updated because of existing merge "
3375 "conflicts: %d\n", upa->not_updated);
3379 * The meaning of some status codes differs between merge-style operations and
3380 * update operations. For example, the ! status code means "file was missing"
3381 * if changes were merged into the work tree, and "missing file was restored"
3382 * if the work tree was updated. This function should be used by any operation
3383 * which merges changes into the work tree without updating the work tree.
3386 print_merge_progress_stats(struct got_update_progress_arg *upa)
3388 if (!upa->did_something)
3391 if (upa->conflicts > 0)
3392 printf("Files with new merge conflicts: %d\n", upa->conflicts);
3393 if (upa->obstructed > 0)
3394 printf("File paths obstructed by a non-regular file: %d\n",
3396 if (upa->missing > 0)
3397 printf("Files which had incoming changes but could not be "
3398 "found in the work tree: %d\n", upa->missing);
3399 if (upa->not_deleted > 0)
3400 printf("Files not deleted due to differences in deleted "
3401 "content: %d\n", upa->not_deleted);
3402 if (upa->unversioned > 0)
3403 printf("Files not merged because an unversioned file was "
3404 "found in the work tree: %d\n", upa->unversioned);
3410 fprintf(stderr, "usage: %s update [-q] [-b branch] [-c commit] "
3411 "[path ...]\n", getprogname());
3415 static const struct got_error *
3416 update_progress(void *arg, unsigned char status, const char *path)
3418 struct got_update_progress_arg *upa = arg;
3420 if (status == GOT_STATUS_EXISTS ||
3421 status == GOT_STATUS_BASE_REF_ERR)
3424 upa->did_something = 1;
3426 /* Base commit bump happens silently. */
3427 if (status == GOT_STATUS_BUMP_BASE)
3430 if (status == GOT_STATUS_CONFLICT)
3432 if (status == GOT_STATUS_OBSTRUCTED)
3434 if (status == GOT_STATUS_CANNOT_UPDATE)
3436 if (status == GOT_STATUS_MISSING)
3438 if (status == GOT_STATUS_CANNOT_DELETE)
3440 if (status == GOT_STATUS_UNVERSIONED)
3443 while (path[0] == '/')
3445 if (upa->verbosity >= 0)
3446 printf("%c %s\n", status, path);
3451 static const struct got_error *
3452 switch_head_ref(struct got_reference *head_ref,
3453 struct got_object_id *commit_id, struct got_worktree *worktree,
3454 struct got_repository *repo)
3456 const struct got_error *err = NULL;
3458 int ref_has_moved = 0;
3460 /* Trivial case: switching between two different references. */
3461 if (strcmp(got_ref_get_name(head_ref),
3462 got_worktree_get_head_ref_name(worktree)) != 0) {
3463 printf("Switching work tree from %s to %s\n",
3464 got_worktree_get_head_ref_name(worktree),
3465 got_ref_get_name(head_ref));
3466 return got_worktree_set_head_ref(worktree, head_ref);
3469 err = check_linear_ancestry(commit_id,
3470 got_worktree_get_base_commit_id(worktree), 0, repo);
3472 if (err->code != GOT_ERR_ANCESTRY)
3479 /* Switching to a rebased branch with the same reference name. */
3480 err = got_object_id_str(&base_id_str,
3481 got_worktree_get_base_commit_id(worktree));
3484 printf("Reference %s now points at a different branch\n",
3485 got_worktree_get_head_ref_name(worktree));
3486 printf("Switching work tree from %s to %s\n", base_id_str,
3487 got_worktree_get_head_ref_name(worktree));
3491 static const struct got_error *
3492 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
3494 const struct got_error *err;
3497 err = got_worktree_rebase_in_progress(&in_progress, worktree);
3501 return got_error(GOT_ERR_REBASING);
3503 err = got_worktree_histedit_in_progress(&in_progress, worktree);
3507 return got_error(GOT_ERR_HISTEDIT_BUSY);
3512 static const struct got_error *
3513 check_merge_in_progress(struct got_worktree *worktree,
3514 struct got_repository *repo)
3516 const struct got_error *err;
3519 err = got_worktree_merge_in_progress(&in_progress, worktree, repo);
3523 return got_error(GOT_ERR_MERGE_BUSY);
3528 static const struct got_error *
3529 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
3530 char *argv[], struct got_worktree *worktree)
3532 const struct got_error *err = NULL;
3534 struct got_pathlist_entry *new;
3540 return got_error_from_errno("strdup");
3541 return got_pathlist_append(paths, path, NULL);
3544 for (i = 0; i < argc; i++) {
3545 err = got_worktree_resolve_path(&path, worktree, argv[i]);
3548 err = got_pathlist_insert(&new, paths, path, NULL);
3549 if (err || new == NULL /* duplicate */) {
3559 static const struct got_error *
3560 wrap_not_worktree_error(const struct got_error *orig_err,
3561 const char *cmdname, const char *path)
3563 const struct got_error *err;
3564 struct got_repository *repo;
3565 static char msg[512];
3566 int *pack_fds = NULL;
3568 err = got_repo_pack_fds_open(&pack_fds);
3572 err = got_repo_open(&repo, path, NULL, pack_fds);
3576 snprintf(msg, sizeof(msg),
3577 "'got %s' needs a work tree in addition to a git repository\n"
3578 "Work trees can be checked out from this Git repository with "
3580 "The got(1) manual page contains more information.", cmdname);
3581 err = got_error_msg(GOT_ERR_NOT_WORKTREE, msg);
3583 const struct got_error *close_err = got_repo_close(repo);
3588 const struct got_error *pack_err =
3589 got_repo_pack_fds_close(pack_fds);
3596 static const struct got_error *
3597 cmd_update(int argc, char *argv[])
3599 const struct got_error *close_err, *error = NULL;
3600 struct got_repository *repo = NULL;
3601 struct got_worktree *worktree = NULL;
3602 char *worktree_path = NULL;
3603 struct got_object_id *commit_id = NULL;
3604 char *commit_id_str = NULL;
3605 const char *branch_name = NULL;
3606 struct got_reference *head_ref = NULL;
3607 struct got_pathlist_head paths;
3608 struct got_pathlist_entry *pe;
3609 int ch, verbosity = 0;
3610 struct got_update_progress_arg upa;
3611 int *pack_fds = NULL;
3616 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3617 "unveil", NULL) == -1)
3621 while ((ch = getopt(argc, argv, "b:c:q")) != -1) {
3624 branch_name = optarg;
3627 commit_id_str = strdup(optarg);
3628 if (commit_id_str == NULL)
3629 return got_error_from_errno("strdup");
3643 worktree_path = getcwd(NULL, 0);
3644 if (worktree_path == NULL) {
3645 error = got_error_from_errno("getcwd");
3649 error = got_repo_pack_fds_open(&pack_fds);
3653 error = got_worktree_open(&worktree, worktree_path,
3654 GOT_WORKTREE_GOT_DIR);
3656 if (error->code == GOT_ERR_NOT_WORKTREE)
3657 error = wrap_not_worktree_error(error, "update",
3662 error = check_rebase_or_histedit_in_progress(worktree);
3666 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3671 error = apply_unveil(got_repo_get_path(repo), 0,
3672 got_worktree_get_root_path(worktree));
3676 error = check_merge_in_progress(worktree, repo);
3680 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3684 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
3685 got_worktree_get_head_ref_name(worktree), 0);
3688 if (commit_id_str == NULL) {
3689 error = got_ref_resolve(&commit_id, repo, head_ref);
3692 error = got_object_id_str(&commit_id_str, commit_id);
3696 struct got_reflist_head refs;
3697 char *keyword_idstr = NULL;
3701 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3706 error = got_keyword_to_idstr(&keyword_idstr, commit_id_str,
3710 if (keyword_idstr != NULL) {
3711 free(commit_id_str);
3712 commit_id_str = keyword_idstr;
3715 error = got_repo_match_object_id(&commit_id, NULL,
3716 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3717 got_ref_list_free(&refs);
3718 free(commit_id_str);
3719 commit_id_str = NULL;
3722 error = got_object_id_str(&commit_id_str, commit_id);
3728 struct got_object_id *head_commit_id;
3729 TAILQ_FOREACH(pe, &paths, entry) {
3730 if (pe->path_len == 0)
3732 error = got_error_msg(GOT_ERR_BAD_PATH,
3733 "switching between branches requires that "
3734 "the entire work tree gets updated");
3737 error = got_ref_resolve(&head_commit_id, repo, head_ref);
3740 error = check_linear_ancestry(commit_id, head_commit_id, 0,
3742 free(head_commit_id);
3745 error = check_same_branch(commit_id, head_ref, repo);
3748 error = switch_head_ref(head_ref, commit_id, worktree, repo);
3752 error = check_linear_ancestry(commit_id,
3753 got_worktree_get_base_commit_id(worktree), 0, repo);
3754 if (error != NULL) {
3755 if (error->code == GOT_ERR_ANCESTRY)
3756 error = got_error(GOT_ERR_BRANCH_MOVED);
3759 error = check_same_branch(commit_id, head_ref, repo);
3764 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
3766 error = got_worktree_set_base_commit_id(worktree, repo,
3772 memset(&upa, 0, sizeof(upa));
3773 upa.verbosity = verbosity;
3774 error = got_worktree_checkout_files(worktree, &paths, repo,
3775 update_progress, &upa, check_cancelled, NULL);
3779 if (upa.did_something) {
3780 printf("Updated to %s: %s\n",
3781 got_worktree_get_head_ref_name(worktree), commit_id_str);
3783 printf("Already up-to-date\n");
3785 print_update_progress_stats(&upa);
3788 const struct got_error *pack_err =
3789 got_repo_pack_fds_close(pack_fds);
3794 close_err = got_repo_close(repo);
3798 if (worktree != NULL) {
3799 close_err = got_worktree_close(worktree);
3803 if (head_ref != NULL)
3804 got_ref_close(head_ref);
3805 free(worktree_path);
3806 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
3808 free(commit_id_str);
3812 static const struct got_error *
3813 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
3814 const char *path, int diff_context, int ignore_whitespace,
3815 int force_text_diff, struct got_diffstat_cb_arg *dsa,
3816 struct got_repository *repo, FILE *outfile)
3818 const struct got_error *err = NULL;
3819 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
3820 FILE *f1 = NULL, *f2 = NULL;
3821 int fd1 = -1, fd2 = -1;
3823 fd1 = got_opentempfd();
3825 return got_error_from_errno("got_opentempfd");
3826 fd2 = got_opentempfd();
3828 err = got_error_from_errno("got_opentempfd");
3833 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192,
3839 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192, fd2);
3843 f1 = got_opentemp();
3845 err = got_error_from_errno("got_opentemp");
3848 f2 = got_opentemp();
3850 err = got_error_from_errno("got_opentemp");
3854 while (path[0] == '/')
3856 err = got_diff_blob(NULL, NULL, blob1, blob2, f1, f2, path, path,
3857 GOT_DIFF_ALGORITHM_PATIENCE, diff_context, ignore_whitespace,
3858 force_text_diff, dsa, outfile);
3860 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3861 err = got_error_from_errno("close");
3863 got_object_blob_close(blob1);
3864 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3865 err = got_error_from_errno("close");
3867 got_object_blob_close(blob2);
3868 if (f1 && fclose(f1) == EOF && err == NULL)
3869 err = got_error_from_errno("fclose");
3870 if (f2 && fclose(f2) == EOF && err == NULL)
3871 err = got_error_from_errno("fclose");
3875 static const struct got_error *
3876 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
3877 const char *path, int diff_context, int ignore_whitespace,
3878 int force_text_diff, struct got_diffstat_cb_arg *dsa,
3879 struct got_repository *repo, FILE *outfile)
3881 const struct got_error *err = NULL;
3882 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3883 struct got_diff_blob_output_unidiff_arg arg;
3884 FILE *f1 = NULL, *f2 = NULL;
3885 int fd1 = -1, fd2 = -1;
3888 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3891 fd1 = got_opentempfd();
3893 err = got_error_from_errno("got_opentempfd");
3898 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3902 f1 = got_opentemp();
3904 err = got_error_from_errno("got_opentemp");
3908 f2 = got_opentemp();
3910 err = got_error_from_errno("got_opentemp");
3913 fd2 = got_opentempfd();
3915 err = got_error_from_errno("got_opentempfd");
3918 arg.diff_context = diff_context;
3919 arg.ignore_whitespace = ignore_whitespace;
3920 arg.force_text_diff = force_text_diff;
3922 arg.diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
3923 arg.outfile = outfile;
3926 while (path[0] == '/')
3928 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, path, path, repo,
3929 got_diff_blob_output_unidiff, &arg, 1);
3932 got_object_tree_close(tree1);
3934 got_object_tree_close(tree2);
3935 if (f1 && fclose(f1) == EOF && err == NULL)
3936 err = got_error_from_errno("fclose");
3937 if (f2 && fclose(f2) == EOF && err == NULL)
3938 err = got_error_from_errno("fclose");
3939 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3940 err = got_error_from_errno("close");
3941 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3942 err = got_error_from_errno("close");
3946 static const struct got_error *
3947 get_changed_paths(struct got_pathlist_head *paths,
3948 struct got_commit_object *commit, struct got_repository *repo,
3949 struct got_diffstat_cb_arg *dsa)
3951 const struct got_error *err = NULL;
3952 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3953 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3954 struct got_object_qid *qid;
3955 got_diff_blob_cb cb = got_diff_tree_collect_changed_paths;
3956 FILE *f1 = NULL, *f2 = NULL;
3957 int fd1 = -1, fd2 = -1;
3960 cb = got_diff_tree_compute_diffstat;
3962 f1 = got_opentemp();
3964 err = got_error_from_errno("got_opentemp");
3967 f2 = got_opentemp();
3969 err = got_error_from_errno("got_opentemp");
3972 fd1 = got_opentempfd();
3974 err = got_error_from_errno("got_opentempfd");
3977 fd2 = got_opentempfd();
3979 err = got_error_from_errno("got_opentempfd");
3984 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3986 struct got_commit_object *pcommit;
3987 err = got_object_open_as_commit(&pcommit, repo,
3992 tree_id1 = got_object_id_dup(
3993 got_object_commit_get_tree_id(pcommit));
3994 if (tree_id1 == NULL) {
3995 got_object_commit_close(pcommit);
3996 return got_error_from_errno("got_object_id_dup");
3998 got_object_commit_close(pcommit);
4003 err = got_object_open_as_tree(&tree1, repo, tree_id1);
4008 tree_id2 = got_object_commit_get_tree_id(commit);
4009 err = got_object_open_as_tree(&tree2, repo, tree_id2);
4013 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
4014 cb, dsa ? (void *)dsa : paths, dsa ? 1 : 0);
4017 got_object_tree_close(tree1);
4019 got_object_tree_close(tree2);
4020 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
4021 err = got_error_from_errno("close");
4022 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4023 err = got_error_from_errno("close");
4024 if (f1 && fclose(f1) == EOF && err == NULL)
4025 err = got_error_from_errno("fclose");
4026 if (f2 && fclose(f2) == EOF && err == NULL)
4027 err = got_error_from_errno("fclose");
4032 static const struct got_error *
4033 print_patch(struct got_commit_object *commit, struct got_object_id *id,
4034 const char *path, int diff_context, struct got_diffstat_cb_arg *dsa,
4035 struct got_repository *repo, FILE *outfile)
4037 const struct got_error *err = NULL;
4038 struct got_commit_object *pcommit = NULL;
4039 char *id_str1 = NULL, *id_str2 = NULL;
4040 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
4041 struct got_object_qid *qid;
4043 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
4045 err = got_object_open_as_commit(&pcommit, repo,
4049 err = got_object_id_str(&id_str1, &qid->id);
4054 err = got_object_id_str(&id_str2, id);
4058 if (path && path[0] != '\0') {
4060 err = got_object_id_by_path(&obj_id2, repo, commit, path);
4064 err = got_object_id_by_path(&obj_id1, repo,
4067 if (err->code != GOT_ERR_NO_TREE_ENTRY) {
4073 err = got_object_get_type(&obj_type, repo, obj_id2);
4079 "diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
4080 fprintf(outfile, "commit - %s\n",
4081 id_str1 ? id_str1 : "/dev/null");
4082 fprintf(outfile, "commit + %s\n", id_str2);
4084 case GOT_OBJ_TYPE_BLOB:
4085 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
4086 0, 0, dsa, repo, outfile);
4088 case GOT_OBJ_TYPE_TREE:
4089 err = diff_trees(obj_id1, obj_id2, path, diff_context,
4090 0, 0, dsa, repo, outfile);
4093 err = got_error(GOT_ERR_OBJ_TYPE);
4099 obj_id2 = got_object_commit_get_tree_id(commit);
4101 obj_id1 = got_object_commit_get_tree_id(pcommit);
4103 "diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
4104 fprintf(outfile, "commit - %s\n",
4105 id_str1 ? id_str1 : "/dev/null");
4106 fprintf(outfile, "commit + %s\n", id_str2);
4107 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, 0,
4108 dsa, repo, outfile);
4114 got_object_commit_close(pcommit);
4119 get_datestr(time_t *time, char *datebuf)
4121 struct tm mytm, *tm;
4124 tm = gmtime_r(time, &mytm);
4127 s = asctime_r(tm, datebuf);
4130 p = strchr(s, '\n');
4136 static const struct got_error *
4137 match_commit(int *have_match, struct got_object_id *id,
4138 struct got_commit_object *commit, regex_t *regex)
4140 const struct got_error *err = NULL;
4141 regmatch_t regmatch;
4142 char *id_str = NULL, *logmsg = NULL;
4146 err = got_object_id_str(&id_str, id);
4150 err = got_object_commit_get_logmsg(&logmsg, commit);
4154 if (regexec(regex, got_object_commit_get_author(commit), 1,
4155 ®match, 0) == 0 ||
4156 regexec(regex, got_object_commit_get_committer(commit), 1,
4157 ®match, 0) == 0 ||
4158 regexec(regex, id_str, 1, ®match, 0) == 0 ||
4159 regexec(regex, logmsg, 1, ®match, 0) == 0)
4168 match_changed_paths(int *have_match, struct got_pathlist_head *changed_paths,
4171 regmatch_t regmatch;
4172 struct got_pathlist_entry *pe;
4176 TAILQ_FOREACH(pe, changed_paths, entry) {
4177 if (regexec(regex, pe->path, 1, ®match, 0) == 0) {
4184 static const struct got_error *
4185 match_patch(int *have_match, struct got_commit_object *commit,
4186 struct got_object_id *id, const char *path, int diff_context,
4187 struct got_repository *repo, regex_t *regex, FILE *f)
4189 const struct got_error *err = NULL;
4191 size_t linesize = 0;
4192 regmatch_t regmatch;
4196 err = got_opentemp_truncate(f);
4200 err = print_patch(commit, id, path, diff_context, NULL, repo, f);
4204 if (fseeko(f, 0L, SEEK_SET) == -1) {
4205 err = got_error_from_errno("fseeko");
4209 while (getline(&line, &linesize, f) != -1) {
4210 if (regexec(regex, line, 1, ®match, 0) == 0) {
4220 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
4222 static const struct got_error*
4223 build_refs_str(char **refs_str, struct got_reflist_head *refs,
4224 struct got_object_id *id, struct got_repository *repo,
4227 static const struct got_error *err = NULL;
4228 struct got_reflist_entry *re;
4234 TAILQ_FOREACH(re, refs, entry) {
4235 struct got_tag_object *tag = NULL;
4236 struct got_object_id *ref_id;
4239 name = got_ref_get_name(re->ref);
4240 if (strcmp(name, GOT_REF_HEAD) == 0)
4242 if (strncmp(name, "refs/", 5) == 0)
4244 if (strncmp(name, "got/", 4) == 0)
4246 if (strncmp(name, "heads/", 6) == 0)
4248 if (strncmp(name, "remotes/", 8) == 0) {
4252 s = strstr(name, "/" GOT_REF_HEAD);
4253 if (s != NULL && strcmp(s, "/" GOT_REF_HEAD) == 0)
4256 err = got_ref_resolve(&ref_id, repo, re->ref);
4259 if (strncmp(name, "tags/", 5) == 0) {
4260 err = got_object_open_as_tag(&tag, repo, ref_id);
4262 if (err->code != GOT_ERR_OBJ_TYPE) {
4266 /* Ref points at something other than a tag. */
4271 cmp = got_object_id_cmp(tag ?
4272 got_object_tag_get_object_id(tag) : ref_id, id);
4275 got_object_tag_close(tag);
4279 if (asprintf(refs_str, "%s%s%s", s ? s : "",
4280 s ? ", " : "", name) == -1) {
4281 err = got_error_from_errno("asprintf");
4292 static const struct got_error *
4293 print_commit_oneline(struct got_commit_object *commit, struct got_object_id *id,
4294 struct got_repository *repo, struct got_reflist_object_id_map *refs_idmap)
4296 const struct got_error *err = NULL;
4297 char *ref_str = NULL, *id_str = NULL, *logmsg0 = NULL;
4298 char *comma, *s, *nl;
4299 struct got_reflist_head *refs;
4300 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
4302 time_t committer_time;
4304 refs = got_reflist_object_id_map_lookup(refs_idmap, id);
4306 err = build_refs_str(&ref_str, refs, id, repo, 1);
4310 /* Display the first matching ref only. */
4311 if (ref_str && (comma = strchr(ref_str, ',')) != NULL)
4315 if (ref_str == NULL) {
4316 err = got_object_id_str(&id_str, id);
4321 committer_time = got_object_commit_get_committer_time(commit);
4322 if (gmtime_r(&committer_time, &tm) == NULL) {
4323 err = got_error_from_errno("gmtime_r");
4326 if (strftime(datebuf, sizeof(datebuf), "%F ", &tm) == 0) {
4327 err = got_error(GOT_ERR_NO_SPACE);
4331 err = got_object_commit_get_logmsg(&logmsg0, commit);
4336 while (isspace((unsigned char)s[0]))
4339 nl = strchr(s, '\n');
4345 printf("%s%-7s %s\n", datebuf, ref_str, s);
4347 printf("%s%.7s %s\n", datebuf, id_str, s);
4349 if (fflush(stdout) != 0 && err == NULL)
4350 err = got_error_from_errno("fflush");
4358 static const struct got_error *
4359 print_diffstat(struct got_diffstat_cb_arg *dsa, const char *header)
4361 struct got_pathlist_entry *pe;
4364 printf("%s\n", header);
4366 TAILQ_FOREACH(pe, dsa->paths, entry) {
4367 struct got_diff_changed_path *cp = pe->data;
4368 int pad = dsa->max_path_len - pe->path_len + 1;
4370 printf(" %c %s%*c | %*d+ %*d-\n", cp->status, pe->path, pad,
4371 ' ', dsa->add_cols + 1, cp->add, dsa->rm_cols + 1, cp->rm);
4373 printf("\n%d file%s changed, %d insertion%s(+), %d deletion%s(-)\n\n",
4374 dsa->nfiles, dsa->nfiles > 1 ? "s" : "", dsa->ins,
4375 dsa->ins != 1 ? "s" : "", dsa->del, dsa->del != 1 ? "s" : "");
4377 if (fflush(stdout) != 0)
4378 return got_error_from_errno("fflush");
4383 static const struct got_error *
4389 if (fseeko(f, 0L, SEEK_SET) == -1)
4390 return got_error_from_errno("fseek");
4393 r = fread(buf, 1, sizeof(buf), f);
4396 return got_error_from_errno("fread");
4400 if (fwrite(buf, 1, r, stdout) != r)
4401 return got_ferror(stdout, GOT_ERR_IO);
4407 static const struct got_error *
4408 print_commit(struct got_commit_object *commit, struct got_object_id *id,
4409 struct got_repository *repo, const char *path,
4410 struct got_pathlist_head *changed_paths,
4411 struct got_diffstat_cb_arg *diffstat, int show_patch, int diff_context,
4412 struct got_reflist_object_id_map *refs_idmap, const char *custom_refs_str,
4415 const struct got_error *err = NULL;
4417 char *id_str, *datestr, *logmsg0, *logmsg, *line;
4419 time_t committer_time;
4420 const char *author, *committer;
4421 char *refs_str = NULL;
4423 err = got_object_id_str(&id_str, id);
4427 if (custom_refs_str == NULL) {
4428 struct got_reflist_head *refs;
4429 refs = got_reflist_object_id_map_lookup(refs_idmap, id);
4431 err = build_refs_str(&refs_str, refs, id, repo, 0);
4437 printf(GOT_COMMIT_SEP_STR);
4438 if (custom_refs_str)
4439 printf("%s %s (%s)\n", prefix ? prefix : "commit", id_str,
4442 printf("%s %s%s%s%s\n", prefix ? prefix : "commit", id_str,
4443 refs_str ? " (" : "", refs_str ? refs_str : "",
4444 refs_str ? ")" : "");
4449 printf("from: %s\n", got_object_commit_get_author(commit));
4450 author = got_object_commit_get_author(commit);
4451 committer = got_object_commit_get_committer(commit);
4452 if (strcmp(author, committer) != 0)
4453 printf("via: %s\n", committer);
4454 committer_time = got_object_commit_get_committer_time(commit);
4455 datestr = get_datestr(&committer_time, datebuf);
4457 printf("date: %s UTC\n", datestr);
4458 if (got_object_commit_get_nparents(commit) > 1) {
4459 const struct got_object_id_queue *parent_ids;
4460 struct got_object_qid *qid;
4462 parent_ids = got_object_commit_get_parent_ids(commit);
4463 STAILQ_FOREACH(qid, parent_ids, entry) {
4464 err = got_object_id_str(&id_str, &qid->id);
4467 printf("parent %d: %s\n", n++, id_str);
4473 err = got_object_commit_get_logmsg(&logmsg0, commit);
4479 line = strsep(&logmsg, "\n");
4481 printf(" %s\n", line);
4485 if (changed_paths && diffstat == NULL) {
4486 struct got_pathlist_entry *pe;
4488 TAILQ_FOREACH(pe, changed_paths, entry) {
4489 struct got_diff_changed_path *cp = pe->data;
4491 printf(" %c %s\n", cp->status, pe->path);
4499 err = got_error_from_errno("got_opentemp");
4504 err = print_patch(commit, id, path, diff_context, diffstat,
4505 repo, diffstat == NULL ? stdout : f);
4510 err = print_diffstat(diffstat, NULL);
4522 if (fflush(stdout) != 0 && err == NULL)
4523 err = got_error_from_errno("fflush");
4525 if (f && fclose(f) == EOF && err == NULL)
4526 err = got_error_from_errno("fclose");
4532 static const struct got_error *
4533 print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
4534 struct got_repository *repo, const char *path, int show_changed_paths,
4535 int show_diffstat, int show_patch, const char *search_pattern,
4536 int diff_context, int limit, int log_branches, int reverse_display_order,
4537 struct got_reflist_object_id_map *refs_idmap, int one_line, int toposort,
4540 const struct got_error *err;
4541 struct got_commit_graph *graph;
4544 struct got_object_id_queue reversed_commits;
4545 struct got_object_qid *qid;
4546 struct got_commit_object *commit;
4547 struct got_pathlist_head changed_paths;
4549 STAILQ_INIT(&reversed_commits);
4550 TAILQ_INIT(&changed_paths);
4552 if (search_pattern && regcomp(®ex, search_pattern,
4553 REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
4554 return got_error_msg(GOT_ERR_REGEX, search_pattern);
4556 err = got_commit_graph_open(&graph, path, !log_branches);
4559 if (log_branches && toposort) {
4560 err = got_commit_graph_toposort(graph, root_id, repo,
4561 check_cancelled, NULL);
4563 err = got_commit_graph_bfsort(graph, root_id, repo,
4564 check_cancelled, NULL);
4569 struct got_object_id id;
4570 struct got_diffstat_cb_arg dsa = { 0, 0, 0, 0, 0, 0,
4571 &changed_paths, 0, 0, GOT_DIFF_ALGORITHM_PATIENCE };
4573 if (sigint_received || sigpipe_received)
4576 err = got_commit_graph_iter_next(&id, graph, repo,
4577 check_cancelled, NULL);
4579 if (err->code == GOT_ERR_ITER_COMPLETED)
4584 err = got_object_open_as_commit(&commit, repo, &id);
4588 if (((show_changed_paths && !show_diffstat) ||
4589 (show_diffstat && !show_patch))
4590 && !reverse_display_order) {
4591 err = get_changed_paths(&changed_paths, commit, repo,
4592 show_diffstat ? &dsa : NULL);
4597 if (search_pattern) {
4598 err = match_commit(&have_match, &id, commit, ®ex);
4600 got_object_commit_close(commit);
4603 if (have_match == 0 && show_changed_paths)
4604 match_changed_paths(&have_match,
4605 &changed_paths, ®ex);
4606 if (have_match == 0 && show_patch) {
4607 err = match_patch(&have_match, commit, &id,
4608 path, diff_context, repo, ®ex, tmpfile);
4612 if (have_match == 0) {
4613 got_object_commit_close(commit);
4614 got_pathlist_free(&changed_paths,
4615 GOT_PATHLIST_FREE_ALL);
4620 if (reverse_display_order) {
4621 err = got_object_qid_alloc(&qid, &id);
4624 STAILQ_INSERT_HEAD(&reversed_commits, qid, entry);
4625 got_object_commit_close(commit);
4628 err = print_commit_oneline(commit, &id,
4631 err = print_commit(commit, &id, repo, path,
4632 (show_changed_paths || show_diffstat) ?
4633 &changed_paths : NULL,
4634 show_diffstat ? &dsa : NULL, show_patch,
4635 diff_context, refs_idmap, NULL, NULL);
4636 got_object_commit_close(commit);
4640 if ((limit && --limit == 0) ||
4641 (end_id && got_object_id_cmp(&id, end_id) == 0))
4644 got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
4646 if (reverse_display_order) {
4647 STAILQ_FOREACH(qid, &reversed_commits, entry) {
4648 struct got_diffstat_cb_arg dsa = { 0, 0, 0, 0, 0, 0,
4649 &changed_paths, 0, 0, GOT_DIFF_ALGORITHM_PATIENCE };
4651 err = got_object_open_as_commit(&commit, repo,
4655 if ((show_changed_paths && !show_diffstat) ||
4656 (show_diffstat && !show_patch)) {
4657 err = get_changed_paths(&changed_paths, commit,
4658 repo, show_diffstat ? &dsa : NULL);
4663 err = print_commit_oneline(commit, &qid->id,
4666 err = print_commit(commit, &qid->id, repo, path,
4667 (show_changed_paths || show_diffstat) ?
4668 &changed_paths : NULL,
4669 show_diffstat ? &dsa : NULL, show_patch,
4670 diff_context, refs_idmap, NULL, NULL);
4671 got_object_commit_close(commit);
4674 got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
4678 while (!STAILQ_EMPTY(&reversed_commits)) {
4679 qid = STAILQ_FIRST(&reversed_commits);
4680 STAILQ_REMOVE_HEAD(&reversed_commits, entry);
4681 got_object_qid_free(qid);
4683 got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
4686 got_commit_graph_close(graph);
4693 fprintf(stderr, "usage: %s log [-bdPpRst] [-C number] [-c commit] "
4694 "[-l N] [-r repository-path] [-S search-pattern] [-x commit] "
4695 "[path]\n", getprogname());
4700 get_default_log_limit(void)
4702 const char *got_default_log_limit;
4706 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
4707 if (got_default_log_limit == NULL)
4709 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
4715 static const struct got_error *
4716 cmd_log(int argc, char *argv[])
4718 const struct got_error *error;
4719 struct got_repository *repo = NULL;
4720 struct got_worktree *worktree = NULL;
4721 struct got_object_id *start_id = NULL, *end_id = NULL;
4722 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
4723 char *keyword_idstr = NULL;
4724 const char *start_commit = NULL, *end_commit = NULL;
4725 const char *search_pattern = NULL;
4726 int diff_context = -1, ch;
4727 int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
4728 int show_diffstat = 0, reverse_display_order = 0, one_line = 0;
4731 struct got_reflist_head refs;
4732 struct got_reflist_object_id_map *refs_idmap = NULL;
4733 FILE *tmpfile = NULL;
4734 int *pack_fds = NULL;
4739 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4745 limit = get_default_log_limit();
4747 while ((ch = getopt(argc, argv, "bC:c:dl:PpRr:S:stx:")) != -1) {
4753 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4756 errx(1, "number of context lines is %s: %s",
4760 start_commit = optarg;
4766 limit = strtonum(optarg, 0, INT_MAX, &errstr);
4768 errx(1, "number of commits is %s: %s",
4772 show_changed_paths = 1;
4778 reverse_display_order = 1;
4781 repo_path = realpath(optarg, NULL);
4782 if (repo_path == NULL)
4783 return got_error_from_errno2("realpath",
4785 got_path_strip_trailing_slashes(repo_path);
4788 search_pattern = optarg;
4797 end_commit = optarg;
4808 if (diff_context == -1)
4810 else if (!show_patch)
4811 errx(1, "-C requires -p");
4813 if (one_line && (show_patch || show_changed_paths || show_diffstat))
4814 errx(1, "cannot use -s with -d, -p or -P");
4816 cwd = getcwd(NULL, 0);
4818 error = got_error_from_errno("getcwd");
4822 error = got_repo_pack_fds_open(&pack_fds);
4826 if (repo_path == NULL) {
4827 error = got_worktree_open(&worktree, cwd,
4828 GOT_WORKTREE_GOT_DIR);
4829 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4836 error = got_worktree_resolve_path(&path, worktree,
4841 path = strdup(argv[0]);
4843 error = got_error_from_errno("strdup");
4847 } else if (argc != 0)
4850 if (repo_path == NULL) {
4851 repo_path = worktree ?
4852 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
4854 if (repo_path == NULL) {
4855 error = got_error_from_errno("strdup");
4859 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4863 error = apply_unveil(got_repo_get_path(repo), 1,
4864 worktree ? got_worktree_get_root_path(worktree) : NULL);
4868 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4872 error = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
4876 if (start_commit == NULL) {
4877 struct got_reference *head_ref;
4878 struct got_commit_object *commit = NULL;
4879 error = got_ref_open(&head_ref, repo,
4880 worktree ? got_worktree_get_head_ref_name(worktree)
4884 error = got_ref_resolve(&start_id, repo, head_ref);
4885 got_ref_close(head_ref);
4888 error = got_object_open_as_commit(&commit, repo,
4892 got_object_commit_close(commit);
4894 error = got_keyword_to_idstr(&keyword_idstr, start_commit,
4898 if (keyword_idstr != NULL)
4899 start_commit = keyword_idstr;
4901 error = got_repo_match_object_id(&start_id, NULL,
4902 start_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4906 if (end_commit != NULL) {
4907 error = got_keyword_to_idstr(&keyword_idstr, end_commit,
4911 if (keyword_idstr != NULL)
4912 end_commit = keyword_idstr;
4914 error = got_repo_match_object_id(&end_id, NULL,
4915 end_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4922 * If a path was specified on the command line it was resolved
4923 * to a path in the work tree above. Prepend the work tree's
4924 * path prefix to obtain the corresponding in-repository path.
4928 prefix = got_worktree_get_path_prefix(worktree);
4929 if (asprintf(&in_repo_path, "%s%s%s", prefix,
4930 (path[0] != '\0') ? "/" : "", path) == -1) {
4931 error = got_error_from_errno("asprintf");
4936 error = got_repo_map_path(&in_repo_path, repo,
4942 path = in_repo_path;
4946 /* Release work tree lock. */
4947 got_worktree_close(worktree);
4951 if (search_pattern && show_patch) {
4952 tmpfile = got_opentemp();
4953 if (tmpfile == NULL) {
4954 error = got_error_from_errno("got_opentemp");
4959 error = print_commits(start_id, end_id, repo, path ? path : "",
4960 show_changed_paths, show_diffstat, show_patch, search_pattern,
4961 diff_context, limit, log_branches, reverse_display_order,
4962 refs_idmap, one_line, toposort, tmpfile);
4969 free(keyword_idstr);
4971 got_worktree_close(worktree);
4973 const struct got_error *close_err = got_repo_close(repo);
4978 const struct got_error *pack_err =
4979 got_repo_pack_fds_close(pack_fds);
4984 got_reflist_object_id_map_free(refs_idmap);
4985 if (tmpfile && fclose(tmpfile) == EOF && error == NULL)
4986 error = got_error_from_errno("fclose");
4987 got_ref_list_free(&refs);
4994 fprintf(stderr, "usage: %s diff [-adPsw] [-C number] [-c commit] "
4995 "[-r repository-path] [object1 object2 | path ...]\n",
5000 struct print_diff_arg {
5001 struct got_repository *repo;
5002 struct got_worktree *worktree;
5003 struct got_diffstat_cb_arg *diffstat;
5008 enum got_diff_algorithm diff_algo;
5009 int ignore_whitespace;
5010 int force_text_diff;
5017 * Create a file which contains the target path of a symlink so we can feed
5018 * it as content to the diff engine.
5020 static const struct got_error *
5021 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
5022 const char *abspath)
5024 const struct got_error *err = NULL;
5025 char target_path[PATH_MAX];
5026 ssize_t target_len, outlen;
5031 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
5032 if (target_len == -1)
5033 return got_error_from_errno2("readlinkat", abspath);
5035 target_len = readlink(abspath, target_path, PATH_MAX);
5036 if (target_len == -1)
5037 return got_error_from_errno2("readlink", abspath);
5040 *fd = got_opentempfd();
5042 return got_error_from_errno("got_opentempfd");
5044 outlen = write(*fd, target_path, target_len);
5046 err = got_error_from_errno("got_opentempfd");
5050 if (lseek(*fd, 0, SEEK_SET) == -1) {
5051 err = got_error_from_errno2("lseek", abspath);
5062 static const struct got_error *
5063 print_diff(void *arg, unsigned char status, unsigned char staged_status,
5064 const char *path, struct got_object_id *blob_id,
5065 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5066 int dirfd, const char *de_name)
5068 struct print_diff_arg *a = arg;
5069 const struct got_error *err = NULL;
5070 struct got_blob_object *blob1 = NULL;
5071 int fd = -1, fd1 = -1, fd2 = -1;
5073 char *abspath = NULL, *label1 = NULL;
5078 memset(&sb, 0, sizeof(sb));
5080 if (a->diff_staged) {
5081 if (staged_status != GOT_STATUS_MODIFY &&
5082 staged_status != GOT_STATUS_ADD &&
5083 staged_status != GOT_STATUS_DELETE)
5086 if (staged_status == GOT_STATUS_DELETE)
5088 if (status == GOT_STATUS_NONEXISTENT)
5089 return got_error_set_errno(ENOENT, path);
5090 if (status != GOT_STATUS_MODIFY &&
5091 status != GOT_STATUS_ADD &&
5092 status != GOT_STATUS_DELETE &&
5093 status != GOT_STATUS_CONFLICT)
5097 err = got_opentemp_truncate(a->f1);
5099 return got_error_from_errno("got_opentemp_truncate");
5100 err = got_opentemp_truncate(a->f2);
5102 return got_error_from_errno("got_opentemp_truncate");
5104 if (!a->header_shown) {
5105 if (fprintf(a->outfile, "diff %s%s\n",
5106 a->diff_staged ? "-s " : "",
5107 got_worktree_get_root_path(a->worktree)) < 0) {
5108 err = got_error_from_errno("fprintf");
5111 if (fprintf(a->outfile, "commit - %s\n", a->id_str) < 0) {
5112 err = got_error_from_errno("fprintf");
5115 if (fprintf(a->outfile, "path + %s%s\n",
5116 got_worktree_get_root_path(a->worktree),
5117 a->diff_staged ? " (staged changes)" : "") < 0) {
5118 err = got_error_from_errno("fprintf");
5121 a->header_shown = 1;
5124 if (a->diff_staged) {
5125 const char *label1 = NULL, *label2 = NULL;
5126 switch (staged_status) {
5127 case GOT_STATUS_MODIFY:
5131 case GOT_STATUS_ADD:
5134 case GOT_STATUS_DELETE:
5138 return got_error(GOT_ERR_FILE_STATUS);
5140 fd1 = got_opentempfd();
5142 err = got_error_from_errno("got_opentempfd");
5145 fd2 = got_opentempfd();
5147 err = got_error_from_errno("got_opentempfd");
5150 err = got_diff_objects_as_blobs(NULL, NULL, a->f1, a->f2,
5151 fd1, fd2, blob_id, staged_blob_id, label1, label2,
5152 a->diff_algo, a->diff_context, a->ignore_whitespace,
5153 a->force_text_diff, a->diffstat, a->repo, a->outfile);
5157 fd1 = got_opentempfd();
5159 err = got_error_from_errno("got_opentempfd");
5163 if (staged_status == GOT_STATUS_ADD ||
5164 staged_status == GOT_STATUS_MODIFY) {
5166 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
5170 err = got_object_id_str(&id_str, staged_blob_id);
5173 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
5174 err = got_error_from_errno("asprintf");
5179 } else if (status != GOT_STATUS_ADD) {
5180 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192,
5186 if (status != GOT_STATUS_DELETE) {
5187 if (asprintf(&abspath, "%s/%s",
5188 got_worktree_get_root_path(a->worktree), path) == -1) {
5189 err = got_error_from_errno("asprintf");
5194 fd = openat(dirfd, de_name,
5195 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5197 if (!got_err_open_nofollow_on_symlink()) {
5198 err = got_error_from_errno2("openat",
5202 err = get_symlink_target_file(&fd, dirfd,
5208 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5210 if (!got_err_open_nofollow_on_symlink()) {
5211 err = got_error_from_errno2("open",
5215 err = get_symlink_target_file(&fd, dirfd,
5221 if (fstatat(fd, abspath, &sb, AT_SYMLINK_NOFOLLOW) == -1) {
5222 err = got_error_from_errno2("fstatat", abspath);
5225 f2 = fdopen(fd, "r");
5227 err = got_error_from_errno2("fdopen", abspath);
5235 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5241 err = got_diff_blob_file(blob1, a->f1, size1, label1, f2 ? f2 : a->f2,
5242 f2_exists, &sb, path, GOT_DIFF_ALGORITHM_PATIENCE, a->diff_context,
5243 a->ignore_whitespace, a->force_text_diff, a->diffstat, a->outfile);
5245 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5246 err = got_error_from_errno("close");
5247 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5248 err = got_error_from_errno("close");
5250 got_object_blob_close(blob1);
5251 if (fd != -1 && close(fd) == -1 && err == NULL)
5252 err = got_error_from_errno("close");
5253 if (f2 && fclose(f2) == EOF && err == NULL)
5254 err = got_error_from_errno("fclose");
5259 static const struct got_error *
5260 cmd_diff(int argc, char *argv[])
5262 const struct got_error *error;
5263 struct got_repository *repo = NULL;
5264 struct got_worktree *worktree = NULL;
5265 char *cwd = NULL, *repo_path = NULL;
5266 const char *commit_args[2] = { NULL, NULL };
5267 int ncommit_args = 0;
5268 struct got_object_id *ids[2] = { NULL, NULL };
5269 char *labels[2] = { NULL, NULL };
5270 int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
5271 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch, i;
5272 int force_text_diff = 0, force_path = 0, rflag = 0, show_diffstat = 0;
5274 struct got_reflist_head refs;
5275 struct got_pathlist_head diffstat_paths, paths;
5276 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
5277 int fd1 = -1, fd2 = -1;
5278 int *pack_fds = NULL;
5279 struct got_diffstat_cb_arg dsa;
5281 memset(&dsa, 0, sizeof(dsa));
5285 TAILQ_INIT(&diffstat_paths);
5288 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5293 while ((ch = getopt(argc, argv, "aC:c:dPr:sw")) != -1) {
5296 force_text_diff = 1;
5299 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
5302 errx(1, "number of context lines is %s: %s",
5306 if (ncommit_args >= 2)
5307 errx(1, "too many -c options used");
5308 commit_args[ncommit_args++] = optarg;
5317 repo_path = realpath(optarg, NULL);
5318 if (repo_path == NULL)
5319 return got_error_from_errno2("realpath",
5321 got_path_strip_trailing_slashes(repo_path);
5328 ignore_whitespace = 1;
5339 cwd = getcwd(NULL, 0);
5341 error = got_error_from_errno("getcwd");
5345 error = got_repo_pack_fds_open(&pack_fds);
5349 if (repo_path == NULL) {
5350 error = got_worktree_open(&worktree, cwd,
5351 GOT_WORKTREE_GOT_DIR);
5352 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5358 strdup(got_worktree_get_repo_path(worktree));
5359 if (repo_path == NULL) {
5360 error = got_error_from_errno("strdup");
5364 repo_path = strdup(cwd);
5365 if (repo_path == NULL) {
5366 error = got_error_from_errno("strdup");
5372 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5377 if (show_diffstat) {
5378 dsa.paths = &diffstat_paths;
5379 dsa.force_text = force_text_diff;
5380 dsa.ignore_ws = ignore_whitespace;
5381 dsa.diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
5384 if (rflag || worktree == NULL || ncommit_args > 0) {
5386 error = got_error_msg(GOT_ERR_NOT_IMPL,
5387 "-P option can only be used when diffing "
5392 error = got_error_msg(GOT_ERR_NOT_IMPL,
5393 "-s option can only be used when diffing "
5399 error = apply_unveil(got_repo_get_path(repo), 1,
5400 worktree ? got_worktree_get_root_path(worktree) : NULL);
5404 if ((!force_path && argc == 2) || ncommit_args > 0) {
5405 int obj_type = (ncommit_args > 0 ?
5406 GOT_OBJ_TYPE_COMMIT : GOT_OBJ_TYPE_ANY);
5407 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5411 for (i = 0; i < (ncommit_args > 0 ? ncommit_args : argc); i++) {
5413 char *keyword_idstr = NULL;
5415 if (ncommit_args > 0)
5416 arg = commit_args[i];
5420 error = got_keyword_to_idstr(&keyword_idstr, arg,
5424 if (keyword_idstr != NULL)
5425 arg = keyword_idstr;
5427 error = got_repo_match_object_id(&ids[i], &labels[i],
5428 arg, obj_type, &refs, repo);
5429 free(keyword_idstr);
5431 if (error->code != GOT_ERR_NOT_REF &&
5432 error->code != GOT_ERR_NO_OBJ)
5434 if (ncommit_args > 0)
5442 f1 = got_opentemp();
5444 error = got_error_from_errno("got_opentemp");
5448 f2 = got_opentemp();
5450 error = got_error_from_errno("got_opentemp");
5454 outfile = got_opentemp();
5455 if (outfile == NULL) {
5456 error = got_error_from_errno("got_opentemp");
5460 if (ncommit_args == 0 && (ids[0] == NULL || ids[1] == NULL)) {
5461 struct print_diff_arg arg;
5464 if (worktree == NULL) {
5465 if (argc == 2 && ids[0] == NULL) {
5466 error = got_error_path(argv[0], GOT_ERR_NO_OBJ);
5468 } else if (argc == 2 && ids[1] == NULL) {
5469 error = got_error_path(argv[1], GOT_ERR_NO_OBJ);
5471 } else if (argc > 0) {
5472 error = got_error_fmt(GOT_ERR_NOT_WORKTREE,
5473 "%s", "specified paths cannot be resolved");
5476 error = got_error(GOT_ERR_NOT_WORKTREE);
5481 error = get_worktree_paths_from_argv(&paths, argc, argv,
5486 error = got_object_id_str(&id_str,
5487 got_worktree_get_base_commit_id(worktree));
5491 arg.worktree = worktree;
5492 arg.diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
5493 arg.diff_context = diff_context;
5494 arg.id_str = id_str;
5495 arg.header_shown = 0;
5496 arg.diff_staged = diff_staged;
5497 arg.ignore_whitespace = ignore_whitespace;
5498 arg.force_text_diff = force_text_diff;
5499 arg.diffstat = show_diffstat ? &dsa : NULL;
5502 arg.outfile = outfile;
5504 error = got_worktree_status(worktree, &paths, repo, 0,
5505 print_diff, &arg, check_cancelled, NULL);
5510 if (show_diffstat && dsa.nfiles > 0) {
5513 if (asprintf(&header, "diffstat %s%s",
5514 diff_staged ? "-s " : "",
5515 got_worktree_get_root_path(worktree)) == -1) {
5516 error = got_error_from_errno("asprintf");
5520 error = print_diffstat(&dsa, header);
5526 error = printfile(outfile);
5530 if (ncommit_args == 1) {
5531 struct got_commit_object *commit;
5532 error = got_object_open_as_commit(&commit, repo, ids[0]);
5536 labels[1] = labels[0];
5538 if (got_object_commit_get_nparents(commit) > 0) {
5539 const struct got_object_id_queue *pids;
5540 struct got_object_qid *pid;
5541 pids = got_object_commit_get_parent_ids(commit);
5542 pid = STAILQ_FIRST(pids);
5543 ids[0] = got_object_id_dup(&pid->id);
5544 if (ids[0] == NULL) {
5545 error = got_error_from_errno(
5546 "got_object_id_dup");
5547 got_object_commit_close(commit);
5550 error = got_object_id_str(&labels[0], ids[0]);
5552 got_object_commit_close(commit);
5557 labels[0] = strdup("/dev/null");
5558 if (labels[0] == NULL) {
5559 error = got_error_from_errno("strdup");
5560 got_object_commit_close(commit);
5565 got_object_commit_close(commit);
5568 if (ncommit_args == 0 && argc > 2) {
5569 error = got_error_msg(GOT_ERR_BAD_PATH,
5570 "path arguments cannot be used when diffing two objects");
5575 error = got_object_get_type(&type1, repo, ids[0]);
5580 error = got_object_get_type(&type2, repo, ids[1]);
5583 if (type1 != GOT_OBJ_TYPE_ANY && type1 != type2) {
5584 error = got_error(GOT_ERR_OBJ_TYPE);
5587 if (type1 == GOT_OBJ_TYPE_BLOB && argc > 2) {
5588 error = got_error_msg(GOT_ERR_OBJ_TYPE,
5589 "path arguments cannot be used when diffing blobs");
5593 for (i = 0; ncommit_args > 0 && i < argc; i++) {
5595 struct got_pathlist_entry *new;
5599 error = got_worktree_resolve_path(&p, worktree,
5603 prefix = got_worktree_get_path_prefix(worktree);
5604 while (prefix[0] == '/')
5606 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5607 (p[0] != '\0' && prefix[0] != '\0') ? "/" : "",
5609 error = got_error_from_errno("asprintf");
5615 char *mapped_path, *s;
5616 error = got_repo_map_path(&mapped_path, repo, argv[i]);
5622 in_repo_path = strdup(s);
5623 if (in_repo_path == NULL) {
5624 error = got_error_from_errno("asprintf");
5631 error = got_pathlist_insert(&new, &paths, in_repo_path, NULL);
5632 if (error || new == NULL /* duplicate */)
5639 /* Release work tree lock. */
5640 got_worktree_close(worktree);
5644 fd1 = got_opentempfd();
5646 error = got_error_from_errno("got_opentempfd");
5650 fd2 = got_opentempfd();
5652 error = got_error_from_errno("got_opentempfd");
5656 switch (type1 == GOT_OBJ_TYPE_ANY ? type2 : type1) {
5657 case GOT_OBJ_TYPE_BLOB:
5658 error = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5659 fd1, fd2, ids[0], ids[1], NULL, NULL,
5660 GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
5661 ignore_whitespace, force_text_diff,
5662 show_diffstat ? &dsa : NULL, repo, outfile);
5664 case GOT_OBJ_TYPE_TREE:
5665 error = got_diff_objects_as_trees(NULL, NULL, f1, f2, fd1, fd2,
5666 ids[0], ids[1], &paths, "", "",
5667 GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
5668 ignore_whitespace, force_text_diff,
5669 show_diffstat ? &dsa : NULL, repo, outfile);
5671 case GOT_OBJ_TYPE_COMMIT:
5672 fprintf(outfile, "diff %s %s\n", labels[0], labels[1]);
5673 error = got_diff_objects_as_commits(NULL, NULL, f1, f2,
5674 fd1, fd2, ids[0], ids[1], &paths,
5675 GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
5676 ignore_whitespace, force_text_diff,
5677 show_diffstat ? &dsa : NULL, repo, outfile);
5680 error = got_error(GOT_ERR_OBJ_TYPE);
5685 if (show_diffstat && dsa.nfiles > 0) {
5686 char *header = NULL;
5688 if (asprintf(&header, "diffstat %s %s",
5689 labels[0], labels[1]) == -1) {
5690 error = got_error_from_errno("asprintf");
5694 error = print_diffstat(&dsa, header);
5700 error = printfile(outfile);
5709 got_worktree_close(worktree);
5711 const struct got_error *close_err = got_repo_close(repo);
5716 const struct got_error *pack_err =
5717 got_repo_pack_fds_close(pack_fds);
5721 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
5722 got_pathlist_free(&diffstat_paths, GOT_PATHLIST_FREE_ALL);
5723 got_ref_list_free(&refs);
5724 if (outfile && fclose(outfile) == EOF && error == NULL)
5725 error = got_error_from_errno("fclose");
5726 if (f1 && fclose(f1) == EOF && error == NULL)
5727 error = got_error_from_errno("fclose");
5728 if (f2 && fclose(f2) == EOF && error == NULL)
5729 error = got_error_from_errno("fclose");
5730 if (fd1 != -1 && close(fd1) == -1 && error == NULL)
5731 error = got_error_from_errno("close");
5732 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
5733 error = got_error_from_errno("close");
5741 "usage: %s blame [-c commit] [-r repository-path] path\n",
5750 char datebuf[11]; /* YYYY-MM-DD + NUL */
5753 struct blame_cb_args {
5754 struct blame_line *lines;
5758 off_t *line_offsets;
5760 struct got_repository *repo;
5763 static const struct got_error *
5764 blame_cb(void *arg, int nlines, int lineno,
5765 struct got_commit_object *commit, struct got_object_id *id)
5767 const struct got_error *err = NULL;
5768 struct blame_cb_args *a = arg;
5769 struct blame_line *bline;
5771 size_t linesize = 0;
5774 time_t committer_time;
5776 if (nlines != a->nlines ||
5777 (lineno != -1 && lineno < 1) || lineno > a->nlines)
5778 return got_error(GOT_ERR_RANGE);
5780 if (sigint_received)
5781 return got_error(GOT_ERR_ITER_COMPLETED);
5784 return NULL; /* no change in this commit */
5786 /* Annotate this line. */
5787 bline = &a->lines[lineno - 1];
5788 if (bline->annotated)
5790 err = got_object_id_str(&bline->id_str, id);
5794 bline->committer = strdup(got_object_commit_get_committer(commit));
5795 if (bline->committer == NULL) {
5796 err = got_error_from_errno("strdup");
5800 committer_time = got_object_commit_get_committer_time(commit);
5801 if (gmtime_r(&committer_time, &tm) == NULL)
5802 return got_error_from_errno("gmtime_r");
5803 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%F", &tm) == 0) {
5804 err = got_error(GOT_ERR_NO_SPACE);
5807 bline->annotated = 1;
5809 /* Print lines annotated so far. */
5810 bline = &a->lines[a->lineno_cur - 1];
5811 if (!bline->annotated)
5814 offset = a->line_offsets[a->lineno_cur - 1];
5815 if (fseeko(a->f, offset, SEEK_SET) == -1) {
5816 err = got_error_from_errno("fseeko");
5820 while (a->lineno_cur <= a->nlines && bline->annotated) {
5821 char *smallerthan, *at, *nl, *committer;
5824 if (getline(&line, &linesize, a->f) == -1) {
5826 err = got_error_from_errno("getline");
5830 committer = bline->committer;
5831 smallerthan = strchr(committer, '<');
5832 if (smallerthan && smallerthan[1] != '\0')
5833 committer = smallerthan + 1;
5834 at = strchr(committer, '@');
5837 len = strlen(committer);
5839 committer[8] = '\0';
5841 nl = strchr(line, '\n');
5844 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
5845 bline->id_str, bline->datebuf, committer, line);
5848 bline = &a->lines[a->lineno_cur - 1];
5855 static const struct got_error *
5856 cmd_blame(int argc, char *argv[])
5858 const struct got_error *error;
5859 struct got_repository *repo = NULL;
5860 struct got_worktree *worktree = NULL;
5861 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5862 char *link_target = NULL;
5863 struct got_object_id *obj_id = NULL;
5864 struct got_object_id *commit_id = NULL;
5865 struct got_commit_object *commit = NULL;
5866 struct got_blob_object *blob = NULL;
5867 char *commit_id_str = NULL, *keyword_idstr = NULL;
5868 struct blame_cb_args bca;
5869 int ch, obj_type, i, fd1 = -1, fd2 = -1, fd3 = -1;
5871 int *pack_fds = NULL;
5872 FILE *f1 = NULL, *f2 = NULL;
5874 fd1 = got_opentempfd();
5876 return got_error_from_errno("got_opentempfd");
5878 memset(&bca, 0, sizeof(bca));
5881 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5886 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5889 commit_id_str = optarg;
5892 repo_path = realpath(optarg, NULL);
5893 if (repo_path == NULL)
5894 return got_error_from_errno2("realpath",
5896 got_path_strip_trailing_slashes(repo_path);
5912 cwd = getcwd(NULL, 0);
5914 error = got_error_from_errno("getcwd");
5918 error = got_repo_pack_fds_open(&pack_fds);
5922 if (repo_path == NULL) {
5923 error = got_worktree_open(&worktree, cwd,
5924 GOT_WORKTREE_GOT_DIR);
5925 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5931 strdup(got_worktree_get_repo_path(worktree));
5932 if (repo_path == NULL) {
5933 error = got_error_from_errno("strdup");
5938 repo_path = strdup(cwd);
5939 if (repo_path == NULL) {
5940 error = got_error_from_errno("strdup");
5946 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5951 const char *prefix = got_worktree_get_path_prefix(worktree);
5954 error = got_worktree_resolve_path(&p, worktree, path);
5957 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5958 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
5960 error = got_error_from_errno("asprintf");
5965 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5967 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5970 error = got_repo_map_path(&in_repo_path, repo, path);
5975 if (commit_id_str == NULL) {
5976 struct got_reference *head_ref;
5977 error = got_ref_open(&head_ref, repo, worktree ?
5978 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5981 error = got_ref_resolve(&commit_id, repo, head_ref);
5982 got_ref_close(head_ref);
5986 struct got_reflist_head refs;
5989 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5994 error = got_keyword_to_idstr(&keyword_idstr, commit_id_str,
5998 if (keyword_idstr != NULL)
5999 commit_id_str = keyword_idstr;
6001 error = got_repo_match_object_id(&commit_id, NULL,
6002 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
6003 got_ref_list_free(&refs);
6009 /* Release work tree lock. */
6010 got_worktree_close(worktree);
6014 error = got_object_open_as_commit(&commit, repo, commit_id);
6018 error = got_object_resolve_symlinks(&link_target, in_repo_path,
6023 error = got_object_id_by_path(&obj_id, repo, commit,
6024 link_target ? link_target : in_repo_path);
6028 error = got_object_get_type(&obj_type, repo, obj_id);
6032 if (obj_type != GOT_OBJ_TYPE_BLOB) {
6033 error = got_error_path(link_target ? link_target : in_repo_path,
6038 error = got_object_open_as_blob(&blob, repo, obj_id, 8192, fd1);
6041 bca.f = got_opentemp();
6042 if (bca.f == NULL) {
6043 error = got_error_from_errno("got_opentemp");
6046 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
6047 &bca.line_offsets, bca.f, blob);
6048 if (error || bca.nlines == 0)
6051 /* Don't include \n at EOF in the blame line count. */
6052 if (bca.line_offsets[bca.nlines - 1] == filesize)
6055 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
6056 if (bca.lines == NULL) {
6057 error = got_error_from_errno("calloc");
6061 bca.nlines_prec = 0;
6069 fd2 = got_opentempfd();
6071 error = got_error_from_errno("got_opentempfd");
6074 fd3 = got_opentempfd();
6076 error = got_error_from_errno("got_opentempfd");
6079 f1 = got_opentemp();
6081 error = got_error_from_errno("got_opentemp");
6084 f2 = got_opentemp();
6086 error = got_error_from_errno("got_opentemp");
6089 error = got_blame(link_target ? link_target : in_repo_path, commit_id,
6090 repo, GOT_DIFF_ALGORITHM_PATIENCE, blame_cb, &bca,
6091 check_cancelled, NULL, fd2, fd3, f1, f2);
6093 free(keyword_idstr);
6101 got_object_commit_close(commit);
6103 if (fd1 != -1 && close(fd1) == -1 && error == NULL)
6104 error = got_error_from_errno("close");
6105 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
6106 error = got_error_from_errno("close");
6107 if (fd3 != -1 && close(fd3) == -1 && error == NULL)
6108 error = got_error_from_errno("close");
6109 if (f1 && fclose(f1) == EOF && error == NULL)
6110 error = got_error_from_errno("fclose");
6111 if (f2 && fclose(f2) == EOF && error == NULL)
6112 error = got_error_from_errno("fclose");
6115 got_object_blob_close(blob);
6117 got_worktree_close(worktree);
6119 const struct got_error *close_err = got_repo_close(repo);
6124 const struct got_error *pack_err =
6125 got_repo_pack_fds_close(pack_fds);
6130 for (i = 0; i < bca.nlines; i++) {
6131 struct blame_line *bline = &bca.lines[i];
6132 free(bline->id_str);
6133 free(bline->committer);
6137 free(bca.line_offsets);
6138 if (bca.f && fclose(bca.f) == EOF && error == NULL)
6139 error = got_error_from_errno("fclose");
6146 fprintf(stderr, "usage: %s tree [-iR] [-c commit] [-r repository-path] "
6147 "[path]\n", getprogname());
6151 static const struct got_error *
6152 print_entry(struct got_tree_entry *te, const char *id, const char *path,
6153 const char *root_path, struct got_repository *repo)
6155 const struct got_error *err = NULL;
6156 int is_root_path = (strcmp(path, root_path) == 0);
6157 const char *modestr = "";
6158 mode_t mode = got_tree_entry_get_mode(te);
6159 char *link_target = NULL;
6161 path += strlen(root_path);
6162 while (path[0] == '/')
6165 if (got_object_tree_entry_is_submodule(te))
6167 else if (S_ISLNK(mode)) {
6170 err = got_tree_entry_get_symlink_target(&link_target, te, repo);
6173 for (i = 0; link_target[i] != '\0'; i++) {
6174 if (!isprint((unsigned char)link_target[i]))
6175 link_target[i] = '?';
6180 else if (S_ISDIR(mode))
6182 else if (mode & S_IXUSR)
6185 printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
6186 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
6187 link_target ? " -> ": "", link_target ? link_target : "");
6193 static const struct got_error *
6194 print_tree(const char *path, struct got_commit_object *commit,
6195 int show_ids, int recurse, const char *root_path,
6196 struct got_repository *repo)
6198 const struct got_error *err = NULL;
6199 struct got_object_id *tree_id = NULL;
6200 struct got_tree_object *tree = NULL;
6203 err = got_object_id_by_path(&tree_id, repo, commit, path);
6207 err = got_object_open_as_tree(&tree, repo, tree_id);
6210 nentries = got_object_tree_get_nentries(tree);
6211 for (i = 0; i < nentries; i++) {
6212 struct got_tree_entry *te;
6215 if (sigint_received || sigpipe_received)
6218 te = got_object_tree_get_entry(tree, i);
6221 err = got_object_id_str(&id_str,
6222 got_tree_entry_get_id(te));
6225 if (asprintf(&id, "%s ", id_str) == -1) {
6226 err = got_error_from_errno("asprintf");
6232 err = print_entry(te, id, path, root_path, repo);
6237 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
6239 if (asprintf(&child_path, "%s%s%s", path,
6240 path[0] == '/' && path[1] == '\0' ? "" : "/",
6241 got_tree_entry_get_name(te)) == -1) {
6242 err = got_error_from_errno("asprintf");
6245 err = print_tree(child_path, commit, show_ids, 1,
6254 got_object_tree_close(tree);
6259 static const struct got_error *
6260 cmd_tree(int argc, char *argv[])
6262 const struct got_error *error;
6263 struct got_repository *repo = NULL;
6264 struct got_worktree *worktree = NULL;
6265 const char *path, *refname = NULL;
6266 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6267 struct got_object_id *commit_id = NULL;
6268 struct got_commit_object *commit = NULL;
6269 char *commit_id_str = NULL, *keyword_idstr = NULL;
6270 int show_ids = 0, recurse = 0;
6272 int *pack_fds = NULL;
6275 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6280 while ((ch = getopt(argc, argv, "c:iRr:")) != -1) {
6283 commit_id_str = optarg;
6292 repo_path = realpath(optarg, NULL);
6293 if (repo_path == NULL)
6294 return got_error_from_errno2("realpath",
6296 got_path_strip_trailing_slashes(repo_path);
6314 cwd = getcwd(NULL, 0);
6316 error = got_error_from_errno("getcwd");
6320 error = got_repo_pack_fds_open(&pack_fds);
6324 if (repo_path == NULL) {
6325 error = got_worktree_open(&worktree, cwd,
6326 GOT_WORKTREE_GOT_DIR);
6327 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6333 strdup(got_worktree_get_repo_path(worktree));
6334 if (repo_path == NULL)
6335 error = got_error_from_errno("strdup");
6339 repo_path = strdup(cwd);
6340 if (repo_path == NULL) {
6341 error = got_error_from_errno("strdup");
6347 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6352 const char *prefix = got_worktree_get_path_prefix(worktree);
6355 if (path == NULL || got_path_is_root_dir(path))
6357 error = got_worktree_resolve_path(&p, worktree, path);
6360 if (asprintf(&in_repo_path, "%s%s%s", prefix,
6361 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
6363 error = got_error_from_errno("asprintf");
6368 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6372 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6377 error = got_repo_map_path(&in_repo_path, repo, path);
6382 if (commit_id_str == NULL) {
6383 struct got_reference *head_ref;
6385 refname = got_worktree_get_head_ref_name(worktree);
6387 refname = GOT_REF_HEAD;
6388 error = got_ref_open(&head_ref, repo, refname, 0);
6391 error = got_ref_resolve(&commit_id, repo, head_ref);
6392 got_ref_close(head_ref);
6396 struct got_reflist_head refs;
6399 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
6404 error = got_keyword_to_idstr(&keyword_idstr, commit_id_str,
6408 if (keyword_idstr != NULL)
6409 commit_id_str = keyword_idstr;
6411 error = got_repo_match_object_id(&commit_id, NULL,
6412 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
6413 got_ref_list_free(&refs);
6419 /* Release work tree lock. */
6420 got_worktree_close(worktree);
6424 error = got_object_open_as_commit(&commit, repo, commit_id);
6428 error = print_tree(in_repo_path, commit, show_ids, recurse,
6429 in_repo_path, repo);
6431 free(keyword_idstr);
6437 got_object_commit_close(commit);
6439 got_worktree_close(worktree);
6441 const struct got_error *close_err = got_repo_close(repo);
6446 const struct got_error *pack_err =
6447 got_repo_pack_fds_close(pack_fds);
6457 fprintf(stderr, "usage: %s status [-I] [-S status-codes] "
6458 "[-s status-codes] [path ...]\n", getprogname());
6462 struct got_status_arg {
6467 static const struct got_error *
6468 print_status(void *arg, unsigned char status, unsigned char staged_status,
6469 const char *path, struct got_object_id *blob_id,
6470 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6471 int dirfd, const char *de_name)
6473 struct got_status_arg *st = arg;
6475 if (status == staged_status && (status == GOT_STATUS_DELETE))
6476 status = GOT_STATUS_NO_CHANGE;
6477 if (st != NULL && st->status_codes) {
6478 size_t ncodes = strlen(st->status_codes);
6481 for (i = 0; i < ncodes ; i++) {
6483 if (status == st->status_codes[i] ||
6484 staged_status == st->status_codes[i]) {
6489 if (status == st->status_codes[i] ||
6490 staged_status == st->status_codes[i])
6495 if (st->suppress && j == 0)
6502 printf("%c%c %s\n", status, staged_status, path);
6506 static const struct got_error *
6507 show_operation_in_progress(struct got_worktree *worktree,
6508 struct got_repository *repo)
6510 const struct got_error *err;
6511 char *new_base_branch_name = NULL;
6512 char *branch_name = NULL;
6513 int rebase_in_progress, histedit_in_progress, merge_in_progress;
6515 err = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6518 if (rebase_in_progress) {
6519 err = got_worktree_rebase_info(&new_base_branch_name,
6520 &branch_name, worktree, repo);
6523 printf("Work tree is rebasing %s onto %s\n",
6524 branch_name, new_base_branch_name);
6527 err = got_worktree_histedit_in_progress(&histedit_in_progress,
6531 if (histedit_in_progress) {
6532 err = got_worktree_histedit_info(&branch_name, worktree, repo);
6535 printf("Work tree is editing the history of %s\n", branch_name);
6538 err = got_worktree_merge_in_progress(&merge_in_progress,
6542 if (merge_in_progress) {
6543 err = got_worktree_merge_info(&branch_name, worktree,
6547 printf("Work tree is merging %s into %s\n", branch_name,
6548 got_worktree_get_head_ref_name(worktree));
6551 free(new_base_branch_name);
6556 static const struct got_error *
6557 cmd_status(int argc, char *argv[])
6559 const struct got_error *close_err, *error = NULL;
6560 struct got_repository *repo = NULL;
6561 struct got_worktree *worktree = NULL;
6562 struct got_status_arg st;
6564 struct got_pathlist_head paths;
6565 int ch, i, no_ignores = 0;
6566 int *pack_fds = NULL;
6570 memset(&st, 0, sizeof(st));
6571 st.status_codes = NULL;
6575 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6580 while ((ch = getopt(argc, argv, "IS:s:")) != -1) {
6586 if (st.status_codes != NULL && st.suppress == 0)
6587 option_conflict('S', 's');
6591 for (i = 0; optarg[i] != '\0'; i++) {
6592 switch (optarg[i]) {
6593 case GOT_STATUS_MODIFY:
6594 case GOT_STATUS_ADD:
6595 case GOT_STATUS_DELETE:
6596 case GOT_STATUS_CONFLICT:
6597 case GOT_STATUS_MISSING:
6598 case GOT_STATUS_OBSTRUCTED:
6599 case GOT_STATUS_UNVERSIONED:
6600 case GOT_STATUS_MODE_CHANGE:
6601 case GOT_STATUS_NONEXISTENT:
6604 errx(1, "invalid status code '%c'",
6608 if (ch == 's' && st.suppress)
6609 option_conflict('s', 'S');
6610 st.status_codes = optarg;
6621 cwd = getcwd(NULL, 0);
6623 error = got_error_from_errno("getcwd");
6627 error = got_repo_pack_fds_open(&pack_fds);
6631 error = got_worktree_open(&worktree, cwd,
6632 GOT_WORKTREE_GOT_DIR);
6634 if (error->code == GOT_ERR_NOT_WORKTREE)
6635 error = wrap_not_worktree_error(error, "status", cwd);
6639 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6644 error = apply_unveil(got_repo_get_path(repo), 1,
6645 got_worktree_get_root_path(worktree));
6649 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6653 error = got_worktree_status(worktree, &paths, repo, no_ignores,
6654 print_status, &st, check_cancelled, NULL);
6658 error = show_operation_in_progress(worktree, repo);
6661 const struct got_error *pack_err =
6662 got_repo_pack_fds_close(pack_fds);
6667 close_err = got_repo_close(repo);
6671 if (worktree != NULL) {
6672 close_err = got_worktree_close(worktree);
6677 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
6685 fprintf(stderr, "usage: %s ref [-dlt] [-c object] [-r repository-path] "
6686 "[-s reference] [name]\n", getprogname());
6690 static const struct got_error *
6691 list_refs(struct got_repository *repo, const char *refname, int sort_by_time)
6693 static const struct got_error *err = NULL;
6694 struct got_reflist_head refs;
6695 struct got_reflist_entry *re;
6698 err = got_ref_list(&refs, repo, refname, sort_by_time ?
6699 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6704 TAILQ_FOREACH(re, &refs, entry) {
6706 refstr = got_ref_to_str(re->ref);
6707 if (refstr == NULL) {
6708 err = got_error_from_errno("got_ref_to_str");
6711 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
6715 got_ref_list_free(&refs);
6719 static const struct got_error *
6720 delete_ref_by_name(struct got_repository *repo, const char *refname)
6722 const struct got_error *err;
6723 struct got_reference *ref;
6725 err = got_ref_open(&ref, repo, refname, 0);
6729 err = delete_ref(repo, ref);
6734 static const struct got_error *
6735 add_ref(struct got_repository *repo, const char *refname, const char *target)
6737 const struct got_error *err = NULL;
6738 struct got_object_id *id = NULL;
6739 struct got_reference *ref = NULL;
6740 struct got_reflist_head refs;
6743 * Don't let the user create a reference name with a leading '-'.
6744 * While technically a valid reference name, this case is usually
6745 * an unintended typo.
6747 if (refname[0] == '-')
6748 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
6751 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
6754 err = got_repo_match_object_id(&id, NULL, target, GOT_OBJ_TYPE_ANY,
6756 got_ref_list_free(&refs);
6760 err = got_ref_alloc(&ref, refname, id);
6764 err = got_ref_write(ref, repo);
6772 static const struct got_error *
6773 add_symref(struct got_repository *repo, const char *refname, const char *target)
6775 const struct got_error *err = NULL;
6776 struct got_reference *ref = NULL;
6777 struct got_reference *target_ref = NULL;
6780 * Don't let the user create a reference name with a leading '-'.
6781 * While technically a valid reference name, this case is usually
6782 * an unintended typo.
6784 if (refname[0] == '-')
6785 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
6787 err = got_ref_open(&target_ref, repo, target, 0);
6791 err = got_ref_alloc_symref(&ref, refname, target_ref);
6795 err = got_ref_write(ref, repo);
6798 got_ref_close(target_ref);
6804 static const struct got_error *
6805 cmd_ref(int argc, char *argv[])
6807 const struct got_error *error = NULL;
6808 struct got_repository *repo = NULL;
6809 struct got_worktree *worktree = NULL;
6810 char *cwd = NULL, *repo_path = NULL;
6811 int ch, do_list = 0, do_delete = 0, sort_by_time = 0;
6812 const char *obj_arg = NULL, *symref_target= NULL;
6813 char *refname = NULL, *keyword_idstr = NULL;
6814 int *pack_fds = NULL;
6817 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6818 "sendfd unveil", NULL) == -1)
6822 while ((ch = getopt(argc, argv, "c:dlr:s:t")) != -1) {
6834 repo_path = realpath(optarg, NULL);
6835 if (repo_path == NULL)
6836 return got_error_from_errno2("realpath",
6838 got_path_strip_trailing_slashes(repo_path);
6841 symref_target = optarg;
6852 if (obj_arg && do_list)
6853 option_conflict('c', 'l');
6854 if (obj_arg && do_delete)
6855 option_conflict('c', 'd');
6856 if (obj_arg && symref_target)
6857 option_conflict('c', 's');
6858 if (symref_target && do_delete)
6859 option_conflict('s', 'd');
6860 if (symref_target && do_list)
6861 option_conflict('s', 'l');
6862 if (do_delete && do_list)
6863 option_conflict('d', 'l');
6864 if (sort_by_time && !do_list)
6865 errx(1, "-t option requires -l option");
6871 if (argc != 0 && argc != 1)
6874 refname = strdup(argv[0]);
6875 if (refname == NULL) {
6876 error = got_error_from_errno("strdup");
6883 refname = strdup(argv[0]);
6884 if (refname == NULL) {
6885 error = got_error_from_errno("strdup");
6891 got_path_strip_trailing_slashes(refname);
6893 cwd = getcwd(NULL, 0);
6895 error = got_error_from_errno("getcwd");
6899 error = got_repo_pack_fds_open(&pack_fds);
6903 if (repo_path == NULL) {
6904 error = got_worktree_open(&worktree, cwd,
6905 GOT_WORKTREE_GOT_DIR);
6906 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6912 strdup(got_worktree_get_repo_path(worktree));
6913 if (repo_path == NULL)
6914 error = got_error_from_errno("strdup");
6918 repo_path = strdup(cwd);
6919 if (repo_path == NULL) {
6920 error = got_error_from_errno("strdup");
6926 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6932 /* Remove "cpath" promise. */
6933 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6939 error = apply_unveil(got_repo_get_path(repo), do_list,
6940 worktree ? got_worktree_get_root_path(worktree) : NULL);
6945 error = list_refs(repo, refname, sort_by_time);
6947 error = delete_ref_by_name(repo, refname);
6948 else if (symref_target)
6949 error = add_symref(repo, refname, symref_target);
6951 if (obj_arg == NULL)
6954 error = got_keyword_to_idstr(&keyword_idstr, obj_arg,
6958 if (keyword_idstr != NULL)
6959 obj_arg = keyword_idstr;
6961 error = add_ref(repo, refname, obj_arg);
6966 const struct got_error *close_err = got_repo_close(repo);
6971 got_worktree_close(worktree);
6973 const struct got_error *pack_err =
6974 got_repo_pack_fds_close(pack_fds);
6980 free(keyword_idstr);
6987 fprintf(stderr, "usage: %s branch [-lnt] [-c commit] [-d name] "
6988 "[-r repository-path] [name]\n", getprogname());
6992 static const struct got_error *
6993 list_branch(struct got_repository *repo, struct got_worktree *worktree,
6994 struct got_reference *ref)
6996 const struct got_error *err = NULL;
6997 const char *refname;
7001 refname = got_ref_get_name(ref);
7002 if (worktree && strcmp(refname,
7003 got_worktree_get_head_ref_name(worktree)) == 0) {
7004 err = got_worktree_get_state(&marker, repo, worktree,
7005 check_cancelled, NULL);
7010 if (strncmp(refname, "refs/heads/", 11) == 0)
7012 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
7014 if (strncmp(refname, "refs/remotes/", 13) == 0)
7017 refstr = got_ref_to_str(ref);
7019 return got_error_from_errno("got_ref_to_str");
7021 printf("%c %s: %s\n", marker, refname, refstr);
7026 static const struct got_error *
7027 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
7029 const char *refname;
7031 if (worktree == NULL)
7032 return got_error(GOT_ERR_NOT_WORKTREE);
7034 refname = got_worktree_get_head_ref_name(worktree);
7036 if (strncmp(refname, "refs/heads/", 11) == 0)
7038 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
7041 printf("%s\n", refname);
7046 static const struct got_error *
7047 list_branches(struct got_repository *repo, struct got_worktree *worktree,
7050 static const struct got_error *err = NULL;
7051 struct got_reflist_head refs;
7052 struct got_reflist_entry *re;
7053 struct got_reference *temp_ref = NULL;
7054 int rebase_in_progress, histedit_in_progress;
7059 err = got_worktree_rebase_in_progress(&rebase_in_progress,
7064 err = got_worktree_histedit_in_progress(&histedit_in_progress,
7069 if (rebase_in_progress || histedit_in_progress) {
7070 err = got_ref_open(&temp_ref, repo,
7071 got_worktree_get_head_ref_name(worktree), 0);
7074 list_branch(repo, worktree, temp_ref);
7075 got_ref_close(temp_ref);
7079 err = got_ref_list(&refs, repo, "refs/heads", sort_by_time ?
7080 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
7085 TAILQ_FOREACH(re, &refs, entry)
7086 list_branch(repo, worktree, re->ref);
7088 got_ref_list_free(&refs);
7090 err = got_ref_list(&refs, repo, "refs/remotes", sort_by_time ?
7091 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
7096 TAILQ_FOREACH(re, &refs, entry)
7097 list_branch(repo, worktree, re->ref);
7099 got_ref_list_free(&refs);
7104 static const struct got_error *
7105 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
7106 const char *branch_name)
7108 const struct got_error *err = NULL;
7109 struct got_reference *ref = NULL;
7110 char *refname, *remote_refname = NULL;
7112 if (strncmp(branch_name, "refs/", 5) == 0)
7114 if (strncmp(branch_name, "heads/", 6) == 0)
7116 else if (strncmp(branch_name, "remotes/", 8) == 0)
7119 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
7120 return got_error_from_errno("asprintf");
7122 if (asprintf(&remote_refname, "refs/remotes/%s",
7123 branch_name) == -1) {
7124 err = got_error_from_errno("asprintf");
7128 err = got_ref_open(&ref, repo, refname, 0);
7130 const struct got_error *err2;
7131 if (err->code != GOT_ERR_NOT_REF)
7134 * Keep 'err' intact such that if neither branch exists
7135 * we report "refs/heads" rather than "refs/remotes" in
7136 * our error message.
7138 err2 = got_ref_open(&ref, repo, remote_refname, 0);
7145 strcmp(got_worktree_get_head_ref_name(worktree),
7146 got_ref_get_name(ref)) == 0) {
7147 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7148 "will not delete this work tree's current branch");
7152 err = delete_ref(repo, ref);
7157 free(remote_refname);
7161 static const struct got_error *
7162 add_branch(struct got_repository *repo, const char *branch_name,
7163 struct got_object_id *base_commit_id)
7165 const struct got_error *err = NULL;
7166 struct got_reference *ref = NULL;
7167 char *refname = NULL;
7170 * Don't let the user create a branch name with a leading '-'.
7171 * While technically a valid reference name, this case is usually
7172 * an unintended typo.
7174 if (branch_name[0] == '-')
7175 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
7177 if (strncmp(branch_name, "refs/heads/", 11) == 0)
7180 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
7181 err = got_error_from_errno("asprintf");
7185 err = got_ref_open(&ref, repo, refname, 0);
7187 err = got_error(GOT_ERR_BRANCH_EXISTS);
7189 } else if (err->code != GOT_ERR_NOT_REF)
7192 err = got_ref_alloc(&ref, refname, base_commit_id);
7196 err = got_ref_write(ref, repo);
7204 static const struct got_error *
7205 cmd_branch(int argc, char *argv[])
7207 const struct got_error *error = NULL;
7208 struct got_repository *repo = NULL;
7209 struct got_worktree *worktree = NULL;
7210 char *cwd = NULL, *repo_path = NULL;
7211 int ch, do_list = 0, do_show = 0, do_update = 1, sort_by_time = 0;
7212 const char *delref = NULL, *commit_id_arg = NULL;
7213 struct got_reference *ref = NULL;
7214 struct got_pathlist_head paths;
7215 struct got_object_id *commit_id = NULL;
7216 char *commit_id_str = NULL, *keyword_idstr = NULL;;
7217 int *pack_fds = NULL;
7222 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
7223 "sendfd unveil", NULL) == -1)
7227 while ((ch = getopt(argc, argv, "c:d:lnr:t")) != -1) {
7230 commit_id_arg = optarg;
7242 repo_path = realpath(optarg, NULL);
7243 if (repo_path == NULL)
7244 return got_error_from_errno2("realpath",
7246 got_path_strip_trailing_slashes(repo_path);
7257 if (do_list && delref)
7258 option_conflict('l', 'd');
7259 if (sort_by_time && !do_list)
7260 errx(1, "-t option requires -l option");
7265 if (!do_list && !delref && argc == 0)
7268 if ((do_list || delref || do_show) && commit_id_arg != NULL)
7269 errx(1, "-c option can only be used when creating a branch");
7271 if (do_list || delref) {
7274 } else if (!do_show && argc != 1)
7277 cwd = getcwd(NULL, 0);
7279 error = got_error_from_errno("getcwd");
7283 error = got_repo_pack_fds_open(&pack_fds);
7287 if (repo_path == NULL) {
7288 error = got_worktree_open(&worktree, cwd,
7289 GOT_WORKTREE_GOT_DIR);
7290 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7296 strdup(got_worktree_get_repo_path(worktree));
7297 if (repo_path == NULL)
7298 error = got_error_from_errno("strdup");
7302 repo_path = strdup(cwd);
7303 if (repo_path == NULL) {
7304 error = got_error_from_errno("strdup");
7310 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7315 if (do_list || do_show) {
7316 /* Remove "cpath" promise. */
7317 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
7323 error = apply_unveil(got_repo_get_path(repo), do_list,
7324 worktree ? got_worktree_get_root_path(worktree) : NULL);
7329 error = show_current_branch(repo, worktree);
7331 error = list_branches(repo, worktree, sort_by_time);
7333 error = delete_branch(repo, worktree, delref);
7335 struct got_reflist_head refs;
7337 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
7341 if (commit_id_arg == NULL)
7342 commit_id_arg = worktree ?
7343 got_worktree_get_head_ref_name(worktree) :
7346 error = got_keyword_to_idstr(&keyword_idstr,
7347 commit_id_arg, repo, worktree);
7350 if (keyword_idstr != NULL)
7351 commit_id_arg = keyword_idstr;
7353 error = got_repo_match_object_id(&commit_id, NULL,
7354 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &refs, repo);
7355 got_ref_list_free(&refs);
7358 error = add_branch(repo, argv[0], commit_id);
7361 if (worktree && do_update) {
7362 struct got_update_progress_arg upa;
7363 char *branch_refname = NULL;
7365 error = got_object_id_str(&commit_id_str, commit_id);
7368 error = get_worktree_paths_from_argv(&paths, 0, NULL,
7372 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
7374 error = got_error_from_errno("asprintf");
7377 error = got_ref_open(&ref, repo, branch_refname, 0);
7378 free(branch_refname);
7381 error = switch_head_ref(ref, commit_id, worktree,
7385 error = got_worktree_set_base_commit_id(worktree, repo,
7389 memset(&upa, 0, sizeof(upa));
7390 error = got_worktree_checkout_files(worktree, &paths,
7391 repo, update_progress, &upa, check_cancelled,
7395 if (upa.did_something) {
7396 printf("Updated to %s: %s\n",
7397 got_worktree_get_head_ref_name(worktree),
7400 print_update_progress_stats(&upa);
7404 free(keyword_idstr);
7408 const struct got_error *close_err = got_repo_close(repo);
7413 got_worktree_close(worktree);
7415 const struct got_error *pack_err =
7416 got_repo_pack_fds_close(pack_fds);
7423 free(commit_id_str);
7424 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
7432 fprintf(stderr, "usage: %s tag [-lVv] [-c commit] [-m message] "
7433 "[-r repository-path] [-s signer-id] name\n", getprogname());
7438 static const struct got_error *
7439 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
7441 const struct got_error *err = NULL;
7442 struct got_reflist_entry *re, *se, *new;
7443 struct got_object_id *re_id, *se_id;
7444 struct got_tag_object *re_tag, *se_tag;
7445 time_t re_time, se_time;
7447 STAILQ_FOREACH(re, tags, entry) {
7448 se = STAILQ_FIRST(sorted);
7450 err = got_reflist_entry_dup(&new, re);
7453 STAILQ_INSERT_HEAD(sorted, new, entry);
7456 err = got_ref_resolve(&re_id, repo, re->ref);
7459 err = got_object_open_as_tag(&re_tag, repo, re_id);
7463 re_time = got_object_tag_get_tagger_time(re_tag);
7464 got_object_tag_close(re_tag);
7468 err = got_ref_resolve(&se_id, repo, re->ref);
7471 err = got_object_open_as_tag(&se_tag, repo, se_id);
7475 se_time = got_object_tag_get_tagger_time(se_tag);
7476 got_object_tag_close(se_tag);
7478 if (se_time > re_time) {
7479 err = got_reflist_entry_dup(&new, re);
7482 STAILQ_INSERT_AFTER(sorted, se, new, entry);
7485 se = STAILQ_NEXT(se, entry);
7494 static const struct got_error *
7495 get_tag_refname(char **refname, const char *tag_name)
7497 const struct got_error *err;
7499 if (strncmp("refs/tags/", tag_name, 10) == 0) {
7500 *refname = strdup(tag_name);
7501 if (*refname == NULL)
7502 return got_error_from_errno("strdup");
7503 } else if (asprintf(refname, "refs/tags/%s", tag_name) == -1) {
7504 err = got_error_from_errno("asprintf");
7512 static const struct got_error *
7513 list_tags(struct got_repository *repo, const char *tag_name, int verify_tags,
7514 const char *allowed_signers, const char *revoked_signers, int verbosity)
7516 static const struct got_error *err = NULL;
7517 struct got_reflist_head refs;
7518 struct got_reflist_entry *re;
7519 char *wanted_refname = NULL;
7524 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
7529 struct got_reference *ref;
7530 err = get_tag_refname(&wanted_refname, tag_name);
7533 /* Wanted tag reference should exist. */
7534 err = got_ref_open(&ref, repo, wanted_refname, 0);
7540 TAILQ_FOREACH(re, &refs, entry) {
7541 const char *refname;
7542 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
7544 const char *tagger, *ssh_sig = NULL;
7545 char *sig_msg = NULL;
7547 struct got_object_id *id;
7548 struct got_tag_object *tag;
7549 struct got_commit_object *commit = NULL;
7551 refname = got_ref_get_name(re->ref);
7552 if (strncmp(refname, "refs/tags/", 10) != 0 ||
7553 (wanted_refname && strcmp(refname, wanted_refname) != 0))
7556 refstr = got_ref_to_str(re->ref);
7557 if (refstr == NULL) {
7558 err = got_error_from_errno("got_ref_to_str");
7562 err = got_ref_resolve(&id, repo, re->ref);
7565 err = got_object_open_as_tag(&tag, repo, id);
7567 if (err->code != GOT_ERR_OBJ_TYPE) {
7571 /* "lightweight" tag */
7572 err = got_object_open_as_commit(&commit, repo, id);
7577 tagger = got_object_commit_get_committer(commit);
7579 got_object_commit_get_committer_time(commit);
7580 err = got_object_id_str(&id_str, id);
7586 tagger = got_object_tag_get_tagger(tag);
7587 tagger_time = got_object_tag_get_tagger_time(tag);
7588 err = got_object_id_str(&id_str,
7589 got_object_tag_get_object_id(tag));
7594 if (tag && verify_tags) {
7595 ssh_sig = got_sigs_get_tagmsg_ssh_signature(
7596 got_object_tag_get_message(tag));
7597 if (ssh_sig && allowed_signers == NULL) {
7598 err = got_error_msg(
7599 GOT_ERR_VERIFY_TAG_SIGNATURE,
7600 "SSH signature verification requires "
7601 "setting allowed_signers in "
7607 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
7609 printf("from: %s\n", tagger);
7610 datestr = get_datestr(&tagger_time, datebuf);
7612 printf("date: %s UTC\n", datestr);
7614 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
7616 switch (got_object_tag_get_object_type(tag)) {
7617 case GOT_OBJ_TYPE_BLOB:
7618 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
7621 case GOT_OBJ_TYPE_TREE:
7622 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
7625 case GOT_OBJ_TYPE_COMMIT:
7626 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
7629 case GOT_OBJ_TYPE_TAG:
7630 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
7640 err = got_sigs_verify_tag_ssh(&sig_msg, tag, ssh_sig,
7641 allowed_signers, revoked_signers, verbosity);
7642 if (err && err->code == GOT_ERR_BAD_TAG_SIGNATURE)
7646 printf("signature: %s", sig_msg);
7652 err = got_object_commit_get_logmsg(&tagmsg0, commit);
7655 got_object_commit_close(commit);
7657 tagmsg0 = strdup(got_object_tag_get_message(tag));
7658 got_object_tag_close(tag);
7659 if (tagmsg0 == NULL) {
7660 err = got_error_from_errno("strdup");
7667 line = strsep(&tagmsg, "\n");
7669 printf(" %s\n", line);
7674 got_ref_list_free(&refs);
7675 free(wanted_refname);
7677 if (err == NULL && bad_sigs)
7678 err = got_error(GOT_ERR_BAD_TAG_SIGNATURE);
7682 static const struct got_error *
7683 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
7684 const char *tag_name, const char *editor, const char *repo_path)
7686 const struct got_error *err = NULL;
7687 char *template = NULL, *initial_content = NULL;
7688 int initial_content_len;
7691 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
7692 err = got_error_from_errno("asprintf");
7696 initial_content_len = asprintf(&initial_content,
7697 "\n# tagging commit %s as %s\n",
7698 commit_id_str, tag_name);
7699 if (initial_content_len == -1) {
7700 err = got_error_from_errno("asprintf");
7704 err = got_opentemp_named_fd(tagmsg_path, &fd, template, "");
7708 if (write(fd, initial_content, initial_content_len) == -1) {
7709 err = got_error_from_errno2("write", *tagmsg_path);
7712 if (close(fd) == -1) {
7713 err = got_error_from_errno2("close", *tagmsg_path);
7718 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content,
7719 initial_content_len, 1);
7721 free(initial_content);
7724 if (fd != -1 && close(fd) == -1 && err == NULL)
7725 err = got_error_from_errno2("close", *tagmsg_path);
7734 static const struct got_error *
7735 add_tag(struct got_repository *repo, const char *tagger,
7736 const char *tag_name, const char *commit_arg, const char *tagmsg_arg,
7737 const char *signer_id, const char *editor, int verbosity)
7739 const struct got_error *err = NULL;
7740 struct got_object_id *commit_id = NULL, *tag_id = NULL;
7741 char *label = NULL, *commit_id_str = NULL;
7742 struct got_reference *ref = NULL;
7743 char *refname = NULL, *tagmsg = NULL;
7744 char *tagmsg_path = NULL, *tag_id_str = NULL;
7745 int preserve_tagmsg = 0;
7746 struct got_reflist_head refs;
7751 * Don't let the user create a tag name with a leading '-'.
7752 * While technically a valid reference name, this case is usually
7753 * an unintended typo.
7755 if (tag_name[0] == '-')
7756 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
7758 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
7762 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
7763 GOT_OBJ_TYPE_COMMIT, &refs, repo);
7767 err = got_object_id_str(&commit_id_str, commit_id);
7771 err = get_tag_refname(&refname, tag_name);
7774 if (strncmp("refs/tags/", tag_name, 10) == 0)
7777 err = got_ref_open(&ref, repo, refname, 0);
7779 err = got_error(GOT_ERR_TAG_EXISTS);
7781 } else if (err->code != GOT_ERR_NOT_REF)
7784 if (tagmsg_arg == NULL) {
7785 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
7786 tag_name, editor, got_repo_get_path(repo));
7788 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
7789 tagmsg_path != NULL)
7790 preserve_tagmsg = 1;
7795 err = got_object_tag_create(&tag_id, tag_name, commit_id,
7796 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, signer_id, repo,
7800 preserve_tagmsg = 1;
7804 err = got_ref_alloc(&ref, refname, tag_id);
7807 preserve_tagmsg = 1;
7811 err = got_ref_write(ref, repo);
7814 preserve_tagmsg = 1;
7818 err = got_object_id_str(&tag_id_str, tag_id);
7821 preserve_tagmsg = 1;
7824 printf("Created tag %s\n", tag_id_str);
7826 if (preserve_tagmsg) {
7827 fprintf(stderr, "%s: tag message preserved in %s\n",
7828 getprogname(), tagmsg_path);
7829 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
7830 err = got_error_from_errno2("unlink", tagmsg_path);
7835 free(commit_id_str);
7839 got_ref_list_free(&refs);
7843 static const struct got_error *
7844 cmd_tag(int argc, char *argv[])
7846 const struct got_error *error = NULL;
7847 struct got_repository *repo = NULL;
7848 struct got_worktree *worktree = NULL;
7849 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
7850 char *gitconfig_path = NULL, *tagger = NULL, *keyword_idstr = NULL;
7851 char *allowed_signers = NULL, *revoked_signers = NULL, *editor = NULL;
7852 const char *signer_id = NULL;
7853 const char *tag_name = NULL, *commit_id_arg = NULL, *tagmsg = NULL;
7854 int ch, do_list = 0, verify_tags = 0, verbosity = 0;
7855 int *pack_fds = NULL;
7858 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
7859 "sendfd unveil", NULL) == -1)
7863 while ((ch = getopt(argc, argv, "c:lm:r:s:Vv")) != -1) {
7866 commit_id_arg = optarg;
7875 repo_path = realpath(optarg, NULL);
7876 if (repo_path == NULL) {
7877 error = got_error_from_errno2("realpath",
7881 got_path_strip_trailing_slashes(repo_path);
7892 else if (verbosity < 3)
7904 if (do_list || verify_tags) {
7905 if (commit_id_arg != NULL)
7907 "-c option can only be used when creating a tag");
7910 option_conflict('l', 'm');
7912 option_conflict('V', 'm');
7916 option_conflict('l', 's');
7918 option_conflict('V', 's');
7922 } else if (argc != 1)
7928 cwd = getcwd(NULL, 0);
7930 error = got_error_from_errno("getcwd");
7934 error = got_repo_pack_fds_open(&pack_fds);
7938 if (repo_path == NULL) {
7939 error = got_worktree_open(&worktree, cwd,
7940 GOT_WORKTREE_GOT_DIR);
7941 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7947 strdup(got_worktree_get_repo_path(worktree));
7948 if (repo_path == NULL)
7949 error = got_error_from_errno("strdup");
7953 repo_path = strdup(cwd);
7954 if (repo_path == NULL) {
7955 error = got_error_from_errno("strdup");
7961 if (do_list || verify_tags) {
7962 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7965 error = get_allowed_signers(&allowed_signers, repo, worktree);
7968 error = get_revoked_signers(&revoked_signers, repo, worktree);
7972 /* Release work tree lock. */
7973 got_worktree_close(worktree);
7978 * Remove "cpath" promise unless needed for signature tmpfile
7982 got_sigs_apply_unveil();
7985 if (pledge("stdio rpath wpath flock proc exec sendfd "
7986 "unveil", NULL) == -1)
7990 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7993 error = list_tags(repo, tag_name, verify_tags, allowed_signers,
7994 revoked_signers, verbosity);
7996 error = get_gitconfig_path(&gitconfig_path);
7999 error = got_repo_open(&repo, repo_path, gitconfig_path,
8004 error = get_author(&tagger, repo, worktree);
8007 if (signer_id == NULL)
8008 signer_id = get_signer_id(repo, worktree);
8010 if (tagmsg == NULL) {
8011 error = get_editor(&editor);
8014 if (unveil(editor, "x") != 0) {
8015 error = got_error_from_errno2("unveil", editor);
8020 error = got_sigs_apply_unveil();
8024 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
8028 if (commit_id_arg == NULL) {
8029 struct got_reference *head_ref;
8030 struct got_object_id *commit_id;
8031 error = got_ref_open(&head_ref, repo,
8032 worktree ? got_worktree_get_head_ref_name(worktree)
8036 error = got_ref_resolve(&commit_id, repo, head_ref);
8037 got_ref_close(head_ref);
8040 error = got_object_id_str(&commit_id_str, commit_id);
8045 error = got_keyword_to_idstr(&keyword_idstr,
8046 commit_id_arg, repo, worktree);
8049 commit_id_str = keyword_idstr;
8053 /* Release work tree lock. */
8054 got_worktree_close(worktree);
8058 error = add_tag(repo, tagger, tag_name,
8059 commit_id_str ? commit_id_str : commit_id_arg, tagmsg,
8060 signer_id, editor, verbosity);
8064 const struct got_error *close_err = got_repo_close(repo);
8069 got_worktree_close(worktree);
8071 const struct got_error *pack_err =
8072 got_repo_pack_fds_close(pack_fds);
8079 free(gitconfig_path);
8080 free(commit_id_str);
8082 free(allowed_signers);
8083 free(revoked_signers);
8090 fprintf(stderr, "usage: %s add [-IR] path ...\n", getprogname());