2 * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Copyright (c) 2020 Ori Bernstein <ori@openbsd.org>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/queue.h>
21 #include <sys/types.h>
44 #include "got_version.h"
45 #include "got_error.h"
46 #include "got_object.h"
47 #include "got_reference.h"
48 #include "got_repository.h"
50 #include "got_cancel.h"
51 #include "got_worktree.h"
53 #include "got_commit_graph.h"
54 #include "got_fetch.h"
56 #include "got_blame.h"
57 #include "got_privsep.h"
58 #include "got_opentemp.h"
59 #include "got_gotconfig.h"
61 #include "got_patch.h"
66 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
69 static volatile sig_atomic_t sigint_received;
70 static volatile sig_atomic_t sigpipe_received;
73 catch_sigint(int signo)
79 catch_sigpipe(int signo)
87 const struct got_error *(*cmd_main)(int, char *[]);
88 void (*cmd_usage)(void);
89 const char *cmd_alias;
92 __dead static void usage(int, int);
93 __dead static void usage_import(void);
94 __dead static void usage_clone(void);
95 __dead static void usage_fetch(void);
96 __dead static void usage_checkout(void);
97 __dead static void usage_update(void);
98 __dead static void usage_log(void);
99 __dead static void usage_diff(void);
100 __dead static void usage_blame(void);
101 __dead static void usage_tree(void);
102 __dead static void usage_status(void);
103 __dead static void usage_ref(void);
104 __dead static void usage_branch(void);
105 __dead static void usage_tag(void);
106 __dead static void usage_add(void);
107 __dead static void usage_remove(void);
108 __dead static void usage_patch(void);
109 __dead static void usage_revert(void);
110 __dead static void usage_commit(void);
111 __dead static void usage_send(void);
112 __dead static void usage_cherrypick(void);
113 __dead static void usage_backout(void);
114 __dead static void usage_rebase(void);
115 __dead static void usage_histedit(void);
116 __dead static void usage_integrate(void);
117 __dead static void usage_merge(void);
118 __dead static void usage_stage(void);
119 __dead static void usage_unstage(void);
120 __dead static void usage_cat(void);
121 __dead static void usage_info(void);
123 static const struct got_error* cmd_import(int, char *[]);
124 static const struct got_error* cmd_clone(int, char *[]);
125 static const struct got_error* cmd_fetch(int, char *[]);
126 static const struct got_error* cmd_checkout(int, char *[]);
127 static const struct got_error* cmd_update(int, char *[]);
128 static const struct got_error* cmd_log(int, char *[]);
129 static const struct got_error* cmd_diff(int, char *[]);
130 static const struct got_error* cmd_blame(int, char *[]);
131 static const struct got_error* cmd_tree(int, char *[]);
132 static const struct got_error* cmd_status(int, char *[]);
133 static const struct got_error* cmd_ref(int, char *[]);
134 static const struct got_error* cmd_branch(int, char *[]);
135 static const struct got_error* cmd_tag(int, char *[]);
136 static const struct got_error* cmd_add(int, char *[]);
137 static const struct got_error* cmd_remove(int, char *[]);
138 static const struct got_error* cmd_patch(int, char *[]);
139 static const struct got_error* cmd_revert(int, char *[]);
140 static const struct got_error* cmd_commit(int, char *[]);
141 static const struct got_error* cmd_send(int, char *[]);
142 static const struct got_error* cmd_cherrypick(int, char *[]);
143 static const struct got_error* cmd_backout(int, char *[]);
144 static const struct got_error* cmd_rebase(int, char *[]);
145 static const struct got_error* cmd_histedit(int, char *[]);
146 static const struct got_error* cmd_integrate(int, char *[]);
147 static const struct got_error* cmd_merge(int, char *[]);
148 static const struct got_error* cmd_stage(int, char *[]);
149 static const struct got_error* cmd_unstage(int, char *[]);
150 static const struct got_error* cmd_cat(int, char *[]);
151 static const struct got_error* cmd_info(int, char *[]);
153 static const struct got_cmd got_commands[] = {
154 { "import", cmd_import, usage_import, "im" },
155 { "clone", cmd_clone, usage_clone, "cl" },
156 { "fetch", cmd_fetch, usage_fetch, "fe" },
157 { "checkout", cmd_checkout, usage_checkout, "co" },
158 { "update", cmd_update, usage_update, "up" },
159 { "log", cmd_log, usage_log, "" },
160 { "diff", cmd_diff, usage_diff, "di" },
161 { "blame", cmd_blame, usage_blame, "bl" },
162 { "tree", cmd_tree, usage_tree, "tr" },
163 { "status", cmd_status, usage_status, "st" },
164 { "ref", cmd_ref, usage_ref, "" },
165 { "branch", cmd_branch, usage_branch, "br" },
166 { "tag", cmd_tag, usage_tag, "" },
167 { "add", cmd_add, usage_add, "" },
168 { "remove", cmd_remove, usage_remove, "rm" },
169 { "patch", cmd_patch, usage_patch, "pa" },
170 { "revert", cmd_revert, usage_revert, "rv" },
171 { "commit", cmd_commit, usage_commit, "ci" },
172 { "send", cmd_send, usage_send, "se" },
173 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
174 { "backout", cmd_backout, usage_backout, "bo" },
175 { "rebase", cmd_rebase, usage_rebase, "rb" },
176 { "histedit", cmd_histedit, usage_histedit, "he" },
177 { "integrate", cmd_integrate, usage_integrate,"ig" },
178 { "merge", cmd_merge, usage_merge, "mg" },
179 { "stage", cmd_stage, usage_stage, "sg" },
180 { "unstage", cmd_unstage, usage_unstage, "ug" },
181 { "cat", cmd_cat, usage_cat, "" },
182 { "info", cmd_info, usage_info, "" },
186 list_commands(FILE *fp)
190 fprintf(fp, "commands:");
191 for (i = 0; i < nitems(got_commands); i++) {
192 const struct got_cmd *cmd = &got_commands[i];
193 fprintf(fp, " %s", cmd->cmd_name);
199 option_conflict(char a, char b)
201 errx(1, "-%c and -%c options are mutually exclusive", a, b);
205 main(int argc, char *argv[])
207 const struct got_cmd *cmd;
210 int hflag = 0, Vflag = 0;
211 static const struct option longopts[] = {
212 { "version", no_argument, NULL, 'V' },
216 setlocale(LC_CTYPE, "");
218 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
238 got_version_print_str();
243 usage(hflag, hflag ? 0 : 1);
245 signal(SIGINT, catch_sigint);
246 signal(SIGPIPE, catch_sigpipe);
248 for (i = 0; i < nitems(got_commands); i++) {
249 const struct got_error *error;
251 cmd = &got_commands[i];
253 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
254 strcmp(cmd->cmd_alias, argv[0]) != 0)
260 error = cmd->cmd_main(argc, argv);
261 if (error && error->code != GOT_ERR_CANCELLED &&
262 error->code != GOT_ERR_PRIVSEP_EXIT &&
263 !(sigpipe_received &&
264 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
266 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
267 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
274 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
275 list_commands(stderr);
280 usage(int hflag, int status)
282 FILE *fp = (status == 0) ? stdout : stderr;
284 fprintf(fp, "usage: %s [-hV] command [arg ...]\n",
291 static const struct got_error *
292 get_editor(char **abspath)
294 const struct got_error *err = NULL;
299 editor = getenv("VISUAL");
301 editor = getenv("EDITOR");
304 err = got_path_find_prog(abspath, editor);
309 if (*abspath == NULL) {
310 *abspath = strdup("/bin/ed");
311 if (*abspath == NULL)
312 return got_error_from_errno("strdup");
318 static const struct got_error *
319 apply_unveil(const char *repo_path, int repo_read_only,
320 const char *worktree_path)
322 const struct got_error *err;
325 if (unveil("gmon.out", "rwc") != 0)
326 return got_error_from_errno2("unveil", "gmon.out");
328 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
329 return got_error_from_errno2("unveil", repo_path);
331 if (worktree_path && unveil(worktree_path, "rwc") != 0)
332 return got_error_from_errno2("unveil", worktree_path);
334 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
335 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
337 err = got_privsep_unveil_exec_helpers();
341 if (unveil(NULL, NULL) != 0)
342 return got_error_from_errno("unveil");
350 fprintf(stderr, "usage: %s import [-b branch] [-I pattern] [-m message] "
351 "[-r repository-path] directory\n", getprogname());
356 spawn_editor(const char *editor, const char *file)
359 sig_t sighup, sigint, sigquit;
362 sighup = signal(SIGHUP, SIG_IGN);
363 sigint = signal(SIGINT, SIG_IGN);
364 sigquit = signal(SIGQUIT, SIG_IGN);
366 switch (pid = fork()) {
370 execl(editor, editor, file, (char *)NULL);
374 while (waitpid(pid, &st, 0) == -1)
379 (void)signal(SIGHUP, sighup);
380 (void)signal(SIGINT, sigint);
381 (void)signal(SIGQUIT, sigquit);
383 if (!WIFEXITED(st)) {
388 return WEXITSTATUS(st);
391 static const struct got_error *
392 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
393 const char *initial_content, size_t initial_content_len,
394 int require_modification)
396 const struct got_error *err = NULL;
401 size_t len, logmsg_len;
402 char *initial_content_stripped = NULL, *buf = NULL, *s;
406 if (stat(logmsg_path, &st) == -1)
407 return got_error_from_errno2("stat", logmsg_path);
409 if (spawn_editor(editor, logmsg_path) == -1)
410 return got_error_from_errno("failed spawning editor");
412 if (stat(logmsg_path, &st2) == -1)
413 return got_error_from_errno("stat");
415 if (require_modification &&
416 st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
417 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
418 "no changes made to commit message, aborting");
421 * Set up a stripped version of the initial content without comments
422 * and blank lines. We need this in order to check if the message
423 * has in fact been edited.
425 initial_content_stripped = malloc(initial_content_len + 1);
426 if (initial_content_stripped == NULL)
427 return got_error_from_errno("malloc");
428 initial_content_stripped[0] = '\0';
430 buf = strdup(initial_content);
432 err = got_error_from_errno("strdup");
437 while ((line = strsep(&s, "\n")) != NULL) {
438 if ((line[0] == '#' || (len == 0 && line[0] == '\n')))
439 continue; /* remove comments and leading empty lines */
440 len = strlcat(initial_content_stripped, line,
441 initial_content_len + 1);
442 if (len >= initial_content_len + 1) {
443 err = got_error(GOT_ERR_NO_SPACE);
447 while (len > 0 && initial_content_stripped[len - 1] == '\n') {
448 initial_content_stripped[len - 1] = '\0';
452 logmsg_len = st2.st_size;
453 *logmsg = malloc(logmsg_len + 1);
455 return got_error_from_errno("malloc");
458 fp = fopen(logmsg_path, "re");
460 err = got_error_from_errno("fopen");
465 while (getline(&line, &linesize, fp) != -1) {
466 if ((line[0] == '#' || (len == 0 && line[0] == '\n')))
467 continue; /* remove comments and leading empty lines */
468 len = strlcat(*logmsg, line, logmsg_len + 1);
469 if (len >= logmsg_len + 1) {
470 err = got_error(GOT_ERR_NO_SPACE);
476 err = got_ferror(fp, GOT_ERR_IO);
479 while (len > 0 && (*logmsg)[len - 1] == '\n') {
480 (*logmsg)[len - 1] = '\0';
485 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
486 "commit message cannot be empty, aborting");
489 if (require_modification &&
490 strcmp(*logmsg, initial_content_stripped) == 0)
491 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
492 "no changes made to commit message, aborting");
494 free(initial_content_stripped);
496 if (fp && fclose(fp) == EOF && err == NULL)
497 err = got_error_from_errno("fclose");
505 static const struct got_error *
506 collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
507 const char *path_dir, const char *branch_name)
509 char *initial_content = NULL;
510 const struct got_error *err = NULL;
511 int initial_content_len;
514 initial_content_len = asprintf(&initial_content,
515 "\n# %s to be imported to branch %s\n", path_dir,
517 if (initial_content_len == -1)
518 return got_error_from_errno("asprintf");
520 err = got_opentemp_named_fd(logmsg_path, &fd,
521 GOT_TMPDIR_STR "/got-importmsg");
525 if (write(fd, initial_content, initial_content_len) == -1) {
526 err = got_error_from_errno2("write", *logmsg_path);
530 err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content,
531 initial_content_len, 1);
533 if (fd != -1 && close(fd) == -1 && err == NULL)
534 err = got_error_from_errno2("close", *logmsg_path);
535 free(initial_content);
543 static const struct got_error *
544 import_progress(void *arg, const char *path)
546 printf("A %s\n", path);
550 static const struct got_error *
551 valid_author(const char *author)
553 const char *email = author;
556 * Git' expects the author (or committer) to be in the form
557 * "name <email>", which are mostly free form (see the
558 * "committer" description in git-fast-import(1)). We're only
559 * doing this to avoid git's object parser breaking on commits
563 while (*author && *author != '\n' && *author != '<' && *author != '>')
565 if (author != email && *author == '<' && *(author - 1) != ' ')
566 return got_error_fmt(GOT_ERR_COMMIT_BAD_AUTHOR, "%s: space "
567 "between author name and email required", email);
568 if (*author++ != '<')
569 return got_error_fmt(GOT_ERR_COMMIT_NO_EMAIL, "%s", email);
570 while (*author && *author != '\n' && *author != '<' && *author != '>')
572 if (strcmp(author, ">") != 0)
573 return got_error_fmt(GOT_ERR_COMMIT_NO_EMAIL, "%s", email);
577 static const struct got_error *
578 get_author(char **author, struct got_repository *repo,
579 struct got_worktree *worktree)
581 const struct got_error *err = NULL;
582 const char *got_author = NULL, *name, *email;
583 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
588 worktree_conf = got_worktree_get_gotconfig(worktree);
589 repo_conf = got_repo_get_gotconfig(repo);
592 * Priority of potential author information sources, from most
593 * significant to least significant:
594 * 1) work tree's .got/got.conf file
595 * 2) repository's got.conf file
596 * 3) repository's git config file
597 * 4) environment variables
598 * 5) global git config files (in user's home directory or /etc)
602 got_author = got_gotconfig_get_author(worktree_conf);
603 if (got_author == NULL)
604 got_author = got_gotconfig_get_author(repo_conf);
605 if (got_author == NULL) {
606 name = got_repo_get_gitconfig_author_name(repo);
607 email = got_repo_get_gitconfig_author_email(repo);
609 if (asprintf(author, "%s <%s>", name, email) == -1)
610 return got_error_from_errno("asprintf");
614 got_author = getenv("GOT_AUTHOR");
615 if (got_author == NULL) {
616 name = got_repo_get_global_gitconfig_author_name(repo);
617 email = got_repo_get_global_gitconfig_author_email(
620 if (asprintf(author, "%s <%s>", name, email)
622 return got_error_from_errno("asprintf");
625 /* TODO: Look up user in password database? */
626 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
630 *author = strdup(got_author);
632 return got_error_from_errno("strdup");
634 err = valid_author(*author);
642 static const struct got_error *
643 get_allowed_signers(char **allowed_signers, struct got_repository *repo,
644 struct got_worktree *worktree)
646 const char *got_allowed_signers = NULL;
647 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
649 *allowed_signers = NULL;
652 worktree_conf = got_worktree_get_gotconfig(worktree);
653 repo_conf = got_repo_get_gotconfig(repo);
656 * Priority of potential author information sources, from most
657 * significant to least significant:
658 * 1) work tree's .got/got.conf file
659 * 2) repository's got.conf file
663 got_allowed_signers = got_gotconfig_get_allowed_signers_file(
665 if (got_allowed_signers == NULL)
666 got_allowed_signers = got_gotconfig_get_allowed_signers_file(
669 if (got_allowed_signers) {
670 *allowed_signers = strdup(got_allowed_signers);
671 if (*allowed_signers == NULL)
672 return got_error_from_errno("strdup");
677 static const struct got_error *
678 get_revoked_signers(char **revoked_signers, struct got_repository *repo,
679 struct got_worktree *worktree)
681 const char *got_revoked_signers = NULL;
682 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
684 *revoked_signers = NULL;
687 worktree_conf = got_worktree_get_gotconfig(worktree);
688 repo_conf = got_repo_get_gotconfig(repo);
691 * Priority of potential author information sources, from most
692 * significant to least significant:
693 * 1) work tree's .got/got.conf file
694 * 2) repository's got.conf file
698 got_revoked_signers = got_gotconfig_get_revoked_signers_file(
700 if (got_revoked_signers == NULL)
701 got_revoked_signers = got_gotconfig_get_revoked_signers_file(
704 if (got_revoked_signers) {
705 *revoked_signers = strdup(got_revoked_signers);
706 if (*revoked_signers == NULL)
707 return got_error_from_errno("strdup");
712 static const struct got_error *
713 get_signer_id(char **signer_id, struct got_repository *repo,
714 struct got_worktree *worktree)
716 const char *got_signer_id = NULL;
717 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
722 worktree_conf = got_worktree_get_gotconfig(worktree);
723 repo_conf = got_repo_get_gotconfig(repo);
726 * Priority of potential author information sources, from most
727 * significant to least significant:
728 * 1) work tree's .got/got.conf file
729 * 2) repository's got.conf file
733 got_signer_id = got_gotconfig_get_signer_id(worktree_conf);
734 if (got_signer_id == NULL)
735 got_signer_id = got_gotconfig_get_signer_id(repo_conf);
738 *signer_id = strdup(got_signer_id);
739 if (*signer_id == NULL)
740 return got_error_from_errno("strdup");
745 static const struct got_error *
746 get_gitconfig_path(char **gitconfig_path)
748 const char *homedir = getenv("HOME");
750 *gitconfig_path = NULL;
752 if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
753 return got_error_from_errno("asprintf");
759 static const struct got_error *
760 cmd_import(int argc, char *argv[])
762 const struct got_error *error = NULL;
763 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
764 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
765 const char *branch_name = NULL;
766 char *id_str = NULL, *logmsg_path = NULL;
767 char refname[PATH_MAX] = "refs/heads/";
768 struct got_repository *repo = NULL;
769 struct got_reference *branch_ref = NULL, *head_ref = NULL;
770 struct got_object_id *new_commit_id = NULL;
772 struct got_pathlist_head ignores;
773 struct got_pathlist_entry *pe;
774 int preserve_logmsg = 0;
775 int *pack_fds = NULL;
777 TAILQ_INIT(&ignores);
779 while ((ch = getopt(argc, argv, "b:I:m:r:")) != -1) {
782 branch_name = optarg;
785 if (optarg[0] == '\0')
787 error = got_pathlist_insert(&pe, &ignores, optarg,
793 logmsg = strdup(optarg);
794 if (logmsg == NULL) {
795 error = got_error_from_errno("strdup");
800 repo_path = realpath(optarg, NULL);
801 if (repo_path == NULL) {
802 error = got_error_from_errno2("realpath",
817 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
825 if (repo_path == NULL) {
826 repo_path = getcwd(NULL, 0);
827 if (repo_path == NULL)
828 return got_error_from_errno("getcwd");
830 got_path_strip_trailing_slashes(repo_path);
831 error = get_gitconfig_path(&gitconfig_path);
834 error = got_repo_pack_fds_open(&pack_fds);
837 error = got_repo_open(&repo, repo_path, gitconfig_path, pack_fds);
841 error = get_author(&author, repo, NULL);
846 * Don't let the user create a branch name with a leading '-'.
847 * While technically a valid reference name, this case is usually
848 * an unintended typo.
850 if (branch_name && branch_name[0] == '-')
851 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
853 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
854 if (error && error->code != GOT_ERR_NOT_REF)
858 n = strlcat(refname, branch_name, sizeof(refname));
859 else if (head_ref && got_ref_is_symbolic(head_ref))
860 n = strlcpy(refname, got_ref_get_symref_target(head_ref),
863 n = strlcat(refname, "main", sizeof(refname));
864 if (n >= sizeof(refname)) {
865 error = got_error(GOT_ERR_NO_SPACE);
869 error = got_ref_open(&branch_ref, repo, refname, 0);
871 if (error->code != GOT_ERR_NOT_REF)
874 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
875 "import target branch already exists");
879 path_dir = realpath(argv[0], NULL);
880 if (path_dir == NULL) {
881 error = got_error_from_errno2("realpath", argv[0]);
884 got_path_strip_trailing_slashes(path_dir);
887 * unveil(2) traverses exec(2); if an editor is used we have
888 * to apply unveil after the log message has been written.
890 if (logmsg == NULL || strlen(logmsg) == 0) {
891 error = get_editor(&editor);
895 error = collect_import_msg(&logmsg, &logmsg_path, editor,
898 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
905 if (unveil(path_dir, "r") != 0) {
906 error = got_error_from_errno2("unveil", path_dir);
912 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
919 error = got_repo_import(&new_commit_id, path_dir, logmsg,
920 author, &ignores, repo, import_progress, NULL);
927 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
934 error = got_ref_write(branch_ref, repo);
941 error = got_object_id_str(&id_str, new_commit_id);
948 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
950 if (error->code != GOT_ERR_NOT_REF) {
956 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
964 error = got_ref_write(head_ref, repo);
972 printf("Created branch %s with commit %s\n",
973 got_ref_get_name(branch_ref), id_str);
976 const struct got_error *pack_err =
977 got_repo_pack_fds_close(pack_fds);
981 if (preserve_logmsg) {
982 fprintf(stderr, "%s: log message preserved in %s\n",
983 getprogname(), logmsg_path);
984 } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
985 error = got_error_from_errno2("unlink", logmsg_path);
993 free(gitconfig_path);
995 got_ref_close(branch_ref);
997 got_ref_close(head_ref);
1004 fprintf(stderr, "usage: %s clone [-almqv] [-b branch] [-R reference] "
1005 "repository-URL [directory]\n", getprogname());
1009 struct got_fetch_progress_arg {
1010 char last_scaled_size[FMT_SCALED_STRSIZE];
1012 int last_p_resolved;
1015 struct got_repository *repo;
1018 int configs_created;
1020 struct got_pathlist_head *symrefs;
1021 struct got_pathlist_head *wanted_branches;
1022 struct got_pathlist_head *wanted_refs;
1026 const char *remote_repo_path;
1027 const char *git_url;
1028 int fetch_all_branches;
1029 int mirror_references;
1033 /* XXX forward declaration */
1034 static const struct got_error *
1035 create_config_files(const char *proto, const char *host, const char *port,
1036 const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1037 int mirror_references, struct got_pathlist_head *symrefs,
1038 struct got_pathlist_head *wanted_branches,
1039 struct got_pathlist_head *wanted_refs, struct got_repository *repo);
1041 static const struct got_error *
1042 fetch_progress(void *arg, const char *message, off_t packfile_size,
1043 int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
1045 const struct got_error *err = NULL;
1046 struct got_fetch_progress_arg *a = arg;
1047 char scaled_size[FMT_SCALED_STRSIZE];
1048 int p_indexed, p_resolved;
1049 int print_size = 0, print_indexed = 0, print_resolved = 0;
1052 * In order to allow a failed clone to be resumed with 'got fetch'
1053 * we try to create configuration files as soon as possible.
1054 * Once the server has sent information about its default branch
1055 * we have all required information.
1057 if (a->create_configs && !a->configs_created &&
1058 !TAILQ_EMPTY(a->config_info.symrefs)) {
1059 err = create_config_files(a->config_info.proto,
1060 a->config_info.host, a->config_info.port,
1061 a->config_info.remote_repo_path,
1062 a->config_info.git_url,
1063 a->config_info.fetch_all_branches,
1064 a->config_info.mirror_references,
1065 a->config_info.symrefs,
1066 a->config_info.wanted_branches,
1067 a->config_info.wanted_refs, a->repo);
1070 a->configs_created = 1;
1073 if (a->verbosity < 0)
1076 if (message && message[0] != '\0') {
1077 printf("\rserver: %s", message);
1082 if (packfile_size > 0 || nobj_indexed > 0) {
1083 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
1084 (a->last_scaled_size[0] == '\0' ||
1085 strcmp(scaled_size, a->last_scaled_size)) != 0) {
1087 if (strlcpy(a->last_scaled_size, scaled_size,
1088 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
1089 return got_error(GOT_ERR_NO_SPACE);
1091 if (nobj_indexed > 0) {
1092 p_indexed = (nobj_indexed * 100) / nobj_total;
1093 if (p_indexed != a->last_p_indexed) {
1094 a->last_p_indexed = p_indexed;
1099 if (nobj_resolved > 0) {
1100 p_resolved = (nobj_resolved * 100) /
1101 (nobj_total - nobj_loose);
1102 if (p_resolved != a->last_p_resolved) {
1103 a->last_p_resolved = p_resolved;
1111 if (print_size || print_indexed || print_resolved)
1114 printf("%*s fetched", FMT_SCALED_STRSIZE - 2, scaled_size);
1116 printf("; indexing %d%%", p_indexed);
1118 printf("; resolving deltas %d%%", p_resolved);
1119 if (print_size || print_indexed || print_resolved)
1125 static const struct got_error *
1126 create_symref(const char *refname, struct got_reference *target_ref,
1127 int verbosity, struct got_repository *repo)
1129 const struct got_error *err;
1130 struct got_reference *head_symref;
1132 err = got_ref_alloc_symref(&head_symref, refname, target_ref);
1136 err = got_ref_write(head_symref, repo);
1137 if (err == NULL && verbosity > 0) {
1138 printf("Created reference %s: %s\n", GOT_REF_HEAD,
1139 got_ref_get_name(target_ref));
1141 got_ref_close(head_symref);
1145 static const struct got_error *
1146 list_remote_refs(struct got_pathlist_head *symrefs,
1147 struct got_pathlist_head *refs)
1149 const struct got_error *err;
1150 struct got_pathlist_entry *pe;
1152 TAILQ_FOREACH(pe, symrefs, entry) {
1153 const char *refname = pe->path;
1154 const char *targetref = pe->data;
1156 printf("%s: %s\n", refname, targetref);
1159 TAILQ_FOREACH(pe, refs, entry) {
1160 const char *refname = pe->path;
1161 struct got_object_id *id = pe->data;
1164 err = got_object_id_str(&id_str, id);
1167 printf("%s: %s\n", refname, id_str);
1174 static const struct got_error *
1175 create_ref(const char *refname, struct got_object_id *id,
1176 int verbosity, struct got_repository *repo)
1178 const struct got_error *err = NULL;
1179 struct got_reference *ref;
1182 err = got_object_id_str(&id_str, id);
1186 err = got_ref_alloc(&ref, refname, id);
1190 err = got_ref_write(ref, repo);
1193 if (err == NULL && verbosity >= 0)
1194 printf("Created reference %s: %s\n", refname, id_str);
1201 match_wanted_ref(const char *refname, const char *wanted_ref)
1203 if (strncmp(refname, "refs/", 5) != 0)
1208 * Prevent fetching of references that won't make any
1209 * sense outside of the remote repository's context.
1211 if (strncmp(refname, "got/", 4) == 0)
1213 if (strncmp(refname, "remotes/", 8) == 0)
1216 if (strncmp(wanted_ref, "refs/", 5) == 0)
1219 /* Allow prefix match. */
1220 if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
1223 /* Allow exact match. */
1224 return (strcmp(refname, wanted_ref) == 0);
1228 is_wanted_ref(struct got_pathlist_head *wanted_refs, const char *refname)
1230 struct got_pathlist_entry *pe;
1232 TAILQ_FOREACH(pe, wanted_refs, entry) {
1233 if (match_wanted_ref(refname, pe->path))
1240 static const struct got_error *
1241 create_wanted_ref(const char *refname, struct got_object_id *id,
1242 const char *remote_repo_name, int verbosity, struct got_repository *repo)
1244 const struct got_error *err;
1245 char *remote_refname;
1247 if (strncmp("refs/", refname, 5) == 0)
1250 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1251 remote_repo_name, refname) == -1)
1252 return got_error_from_errno("asprintf");
1254 err = create_ref(remote_refname, id, verbosity, repo);
1255 free(remote_refname);
1259 static const struct got_error *
1260 create_gotconfig(const char *proto, const char *host, const char *port,
1261 const char *remote_repo_path, const char *default_branch,
1262 int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1263 struct got_pathlist_head *wanted_refs, int mirror_references,
1264 struct got_repository *repo)
1266 const struct got_error *err = NULL;
1267 char *gotconfig_path = NULL;
1268 char *gotconfig = NULL;
1269 FILE *gotconfig_file = NULL;
1270 const char *branchname = NULL;
1271 char *branches = NULL, *refs = NULL;
1274 if (!fetch_all_branches && !TAILQ_EMPTY(wanted_branches)) {
1275 struct got_pathlist_entry *pe;
1276 TAILQ_FOREACH(pe, wanted_branches, entry) {
1278 branchname = pe->path;
1279 if (strncmp(branchname, "refs/heads/", 11) == 0)
1281 if (asprintf(&s, "%s\"%s\" ",
1282 branches ? branches : "", branchname) == -1) {
1283 err = got_error_from_errno("asprintf");
1289 } else if (!fetch_all_branches && default_branch) {
1290 branchname = default_branch;
1291 if (strncmp(branchname, "refs/heads/", 11) == 0)
1293 if (asprintf(&branches, "\"%s\" ", branchname) == -1) {
1294 err = got_error_from_errno("asprintf");
1298 if (!TAILQ_EMPTY(wanted_refs)) {
1299 struct got_pathlist_entry *pe;
1300 TAILQ_FOREACH(pe, wanted_refs, entry) {
1302 const char *refname = pe->path;
1303 if (strncmp(refname, "refs/", 5) == 0)
1305 if (asprintf(&s, "%s\"%s\" ",
1306 refs ? refs : "", refname) == -1) {
1307 err = got_error_from_errno("asprintf");
1315 /* Create got.conf(5). */
1316 gotconfig_path = got_repo_get_path_gotconfig(repo);
1317 if (gotconfig_path == NULL) {
1318 err = got_error_from_errno("got_repo_get_path_gotconfig");
1321 gotconfig_file = fopen(gotconfig_path, "ae");
1322 if (gotconfig_file == NULL) {
1323 err = got_error_from_errno2("fopen", gotconfig_path);
1326 if (asprintf(&gotconfig,
1331 "\trepository \"%s\"\n"
1337 GOT_FETCH_DEFAULT_REMOTE_NAME, host, proto,
1338 port ? "\tport " : "", port ? port : "", port ? "\n" : "",
1339 remote_repo_path, branches ? "\tbranch { " : "",
1340 branches ? branches : "", branches ? "}\n" : "",
1341 refs ? "\treference { " : "", refs ? refs : "", refs ? "}\n" : "",
1342 mirror_references ? "\tmirror_references yes\n" : "",
1343 fetch_all_branches ? "\tfetch_all_branches yes\n" : "") == -1) {
1344 err = got_error_from_errno("asprintf");
1347 n = fwrite(gotconfig, 1, strlen(gotconfig), gotconfig_file);
1348 if (n != strlen(gotconfig)) {
1349 err = got_ferror(gotconfig_file, GOT_ERR_IO);
1354 if (gotconfig_file && fclose(gotconfig_file) == EOF && err == NULL)
1355 err = got_error_from_errno2("fclose", gotconfig_path);
1356 free(gotconfig_path);
1361 static const struct got_error *
1362 create_gitconfig(const char *git_url, const char *default_branch,
1363 int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1364 struct got_pathlist_head *wanted_refs, int mirror_references,
1365 struct got_repository *repo)
1367 const struct got_error *err = NULL;
1368 char *gitconfig_path = NULL;
1369 char *gitconfig = NULL;
1370 FILE *gitconfig_file = NULL;
1371 char *branches = NULL, *refs = NULL;
1372 const char *branchname;
1375 /* Create a config file Git can understand. */
1376 gitconfig_path = got_repo_get_path_gitconfig(repo);
1377 if (gitconfig_path == NULL) {
1378 err = got_error_from_errno("got_repo_get_path_gitconfig");
1381 gitconfig_file = fopen(gitconfig_path, "ae");
1382 if (gitconfig_file == NULL) {
1383 err = got_error_from_errno2("fopen", gitconfig_path);
1386 if (fetch_all_branches) {
1387 if (mirror_references) {
1388 if (asprintf(&branches,
1389 "\tfetch = refs/heads/*:refs/heads/*\n") == -1) {
1390 err = got_error_from_errno("asprintf");
1393 } else if (asprintf(&branches,
1394 "\tfetch = refs/heads/*:refs/remotes/%s/*\n",
1395 GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1396 err = got_error_from_errno("asprintf");
1399 } else if (!TAILQ_EMPTY(wanted_branches)) {
1400 struct got_pathlist_entry *pe;
1401 TAILQ_FOREACH(pe, wanted_branches, entry) {
1403 branchname = pe->path;
1404 if (strncmp(branchname, "refs/heads/", 11) == 0)
1406 if (mirror_references) {
1408 "%s\tfetch = refs/heads/%s:refs/heads/%s\n",
1409 branches ? branches : "",
1410 branchname, branchname) == -1) {
1411 err = got_error_from_errno("asprintf");
1414 } else if (asprintf(&s,
1415 "%s\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1416 branches ? branches : "",
1417 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1418 branchname) == -1) {
1419 err = got_error_from_errno("asprintf");
1427 * If the server specified a default branch, use just that one.
1428 * Otherwise fall back to fetching all branches on next fetch.
1430 if (default_branch) {
1431 branchname = default_branch;
1432 if (strncmp(branchname, "refs/heads/", 11) == 0)
1435 branchname = "*"; /* fall back to all branches */
1436 if (mirror_references) {
1437 if (asprintf(&branches,
1438 "\tfetch = refs/heads/%s:refs/heads/%s\n",
1439 branchname, branchname) == -1) {
1440 err = got_error_from_errno("asprintf");
1443 } else if (asprintf(&branches,
1444 "\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1445 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1446 branchname) == -1) {
1447 err = got_error_from_errno("asprintf");
1451 if (!TAILQ_EMPTY(wanted_refs)) {
1452 struct got_pathlist_entry *pe;
1453 TAILQ_FOREACH(pe, wanted_refs, entry) {
1455 const char *refname = pe->path;
1456 if (strncmp(refname, "refs/", 5) == 0)
1458 if (mirror_references) {
1460 "%s\tfetch = refs/%s:refs/%s\n",
1461 refs ? refs : "", refname, refname) == -1) {
1462 err = got_error_from_errno("asprintf");
1465 } else if (asprintf(&s,
1466 "%s\tfetch = refs/%s:refs/remotes/%s/%s\n",
1468 refname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1470 err = got_error_from_errno("asprintf");
1478 if (asprintf(&gitconfig,
1483 "\tfetch = refs/tags/*:refs/tags/*\n",
1484 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url, branches ? branches : "",
1485 refs ? refs : "") == -1) {
1486 err = got_error_from_errno("asprintf");
1489 n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1490 if (n != strlen(gitconfig)) {
1491 err = got_ferror(gitconfig_file, GOT_ERR_IO);
1495 if (gitconfig_file && fclose(gitconfig_file) == EOF && err == NULL)
1496 err = got_error_from_errno2("fclose", gitconfig_path);
1497 free(gitconfig_path);
1502 static const struct got_error *
1503 create_config_files(const char *proto, const char *host, const char *port,
1504 const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1505 int mirror_references, struct got_pathlist_head *symrefs,
1506 struct got_pathlist_head *wanted_branches,
1507 struct got_pathlist_head *wanted_refs, struct got_repository *repo)
1509 const struct got_error *err = NULL;
1510 const char *default_branch = NULL;
1511 struct got_pathlist_entry *pe;
1514 * If we asked for a set of wanted branches then use the first
1517 if (!TAILQ_EMPTY(wanted_branches)) {
1518 pe = TAILQ_FIRST(wanted_branches);
1519 default_branch = pe->path;
1521 /* First HEAD ref listed by server is the default branch. */
1522 TAILQ_FOREACH(pe, symrefs, entry) {
1523 const char *refname = pe->path;
1524 const char *target = pe->data;
1526 if (strcmp(refname, GOT_REF_HEAD) != 0)
1529 default_branch = target;
1534 /* Create got.conf(5). */
1535 err = create_gotconfig(proto, host, port, remote_repo_path,
1536 default_branch, fetch_all_branches, wanted_branches,
1537 wanted_refs, mirror_references, repo);
1541 /* Create a config file Git can understand. */
1542 return create_gitconfig(git_url, default_branch, fetch_all_branches,
1543 wanted_branches, wanted_refs, mirror_references, repo);
1546 static const struct got_error *
1547 cmd_clone(int argc, char *argv[])
1549 const struct got_error *error = NULL;
1550 const char *uri, *dirname;
1551 char *proto, *host, *port, *repo_name, *server_path;
1552 char *default_destdir = NULL, *id_str = NULL;
1553 const char *repo_path;
1554 struct got_repository *repo = NULL;
1555 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1556 struct got_pathlist_entry *pe;
1557 struct got_object_id *pack_hash = NULL;
1558 int ch, fetchfd = -1, fetchstatus;
1559 pid_t fetchpid = -1;
1560 struct got_fetch_progress_arg fpa;
1561 char *git_url = NULL;
1562 int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
1563 int list_refs_only = 0;
1564 int *pack_fds = NULL;
1567 TAILQ_INIT(&symrefs);
1568 TAILQ_INIT(&wanted_branches);
1569 TAILQ_INIT(&wanted_refs);
1571 while ((ch = getopt(argc, argv, "ab:lmqR:v")) != -1) {
1574 fetch_all_branches = 1;
1577 error = got_pathlist_append(&wanted_branches,
1586 mirror_references = 1;
1592 error = got_pathlist_append(&wanted_refs,
1600 else if (verbosity < 3)
1611 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1612 option_conflict('a', 'b');
1613 if (list_refs_only) {
1614 if (!TAILQ_EMPTY(&wanted_branches))
1615 option_conflict('l', 'b');
1616 if (fetch_all_branches)
1617 option_conflict('l', 'a');
1618 if (mirror_references)
1619 option_conflict('l', 'm');
1620 if (!TAILQ_EMPTY(&wanted_refs))
1621 option_conflict('l', 'R');
1633 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
1638 if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1639 host, port ? ":" : "", port ? port : "",
1640 server_path[0] != '/' ? "/" : "", server_path) == -1) {
1641 error = got_error_from_errno("asprintf");
1645 if (strcmp(proto, "git") == 0) {
1647 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1648 "sendfd dns inet unveil", NULL) == -1)
1651 } else if (strcmp(proto, "git+ssh") == 0 ||
1652 strcmp(proto, "ssh") == 0) {
1654 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1655 "sendfd unveil", NULL) == -1)
1658 } else if (strcmp(proto, "http") == 0 ||
1659 strcmp(proto, "git+http") == 0) {
1660 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1663 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1666 if (dirname == NULL) {
1667 if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1668 error = got_error_from_errno("asprintf");
1671 repo_path = default_destdir;
1673 repo_path = dirname;
1675 if (!list_refs_only) {
1676 error = got_path_mkdir(repo_path);
1678 (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1679 !(error->code == GOT_ERR_ERRNO && errno == EEXIST)))
1681 if (!got_path_dir_is_empty(repo_path)) {
1682 error = got_error_path(repo_path,
1683 GOT_ERR_DIR_NOT_EMPTY);
1688 error = got_dial_apply_unveil(proto);
1692 error = apply_unveil(repo_path, 0, NULL);
1697 printf("Connecting to %s%s%s\n", host,
1698 port ? ":" : "", port ? port : "");
1700 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1701 server_path, verbosity);
1705 if (!list_refs_only) {
1706 error = got_repo_init(repo_path, NULL);
1709 error = got_repo_pack_fds_open(&pack_fds);
1712 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
1717 fpa.last_scaled_size[0] = '\0';
1718 fpa.last_p_indexed = -1;
1719 fpa.last_p_resolved = -1;
1720 fpa.verbosity = verbosity;
1721 fpa.create_configs = 1;
1722 fpa.configs_created = 0;
1724 fpa.config_info.symrefs = &symrefs;
1725 fpa.config_info.wanted_branches = &wanted_branches;
1726 fpa.config_info.wanted_refs = &wanted_refs;
1727 fpa.config_info.proto = proto;
1728 fpa.config_info.host = host;
1729 fpa.config_info.port = port;
1730 fpa.config_info.remote_repo_path = server_path;
1731 fpa.config_info.git_url = git_url;
1732 fpa.config_info.fetch_all_branches = fetch_all_branches;
1733 fpa.config_info.mirror_references = mirror_references;
1734 error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1735 GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1736 fetch_all_branches, &wanted_branches, &wanted_refs,
1737 list_refs_only, verbosity, fetchfd, repo,
1738 fetch_progress, &fpa);
1742 if (list_refs_only) {
1743 error = list_remote_refs(&symrefs, &refs);
1747 if (pack_hash == NULL) {
1748 error = got_error_fmt(GOT_ERR_FETCH_FAILED, "%s",
1749 "server sent an empty pack file");
1752 error = got_object_id_str(&id_str, pack_hash);
1756 printf("\nFetched %s.pack\n", id_str);
1759 /* Set up references provided with the pack file. */
1760 TAILQ_FOREACH(pe, &refs, entry) {
1761 const char *refname = pe->path;
1762 struct got_object_id *id = pe->data;
1763 char *remote_refname;
1765 if (is_wanted_ref(&wanted_refs, refname) &&
1766 !mirror_references) {
1767 error = create_wanted_ref(refname, id,
1768 GOT_FETCH_DEFAULT_REMOTE_NAME,
1769 verbosity - 1, repo);
1775 error = create_ref(refname, id, verbosity - 1, repo);
1779 if (mirror_references)
1782 if (strncmp("refs/heads/", refname, 11) != 0)
1785 if (asprintf(&remote_refname,
1786 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1787 refname + 11) == -1) {
1788 error = got_error_from_errno("asprintf");
1791 error = create_ref(remote_refname, id, verbosity - 1, repo);
1792 free(remote_refname);
1797 /* Set the HEAD reference if the server provided one. */
1798 TAILQ_FOREACH(pe, &symrefs, entry) {
1799 struct got_reference *target_ref;
1800 const char *refname = pe->path;
1801 const char *target = pe->data;
1802 char *remote_refname = NULL, *remote_target = NULL;
1804 if (strcmp(refname, GOT_REF_HEAD) != 0)
1807 error = got_ref_open(&target_ref, repo, target, 0);
1809 if (error->code == GOT_ERR_NOT_REF) {
1816 error = create_symref(refname, target_ref, verbosity, repo);
1817 got_ref_close(target_ref);
1821 if (mirror_references)
1824 if (strncmp("refs/heads/", target, 11) != 0)
1827 if (asprintf(&remote_refname,
1828 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1830 error = got_error_from_errno("asprintf");
1833 if (asprintf(&remote_target,
1834 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1835 target + 11) == -1) {
1836 error = got_error_from_errno("asprintf");
1837 free(remote_refname);
1840 error = got_ref_open(&target_ref, repo, remote_target, 0);
1842 free(remote_refname);
1843 free(remote_target);
1844 if (error->code == GOT_ERR_NOT_REF) {
1850 error = create_symref(remote_refname, target_ref,
1851 verbosity - 1, repo);
1852 free(remote_refname);
1853 free(remote_target);
1854 got_ref_close(target_ref);
1860 * We failed to set the HEAD reference. If we asked for
1861 * a set of wanted branches use the first of one of those
1862 * which could be fetched instead.
1864 TAILQ_FOREACH(pe, &wanted_branches, entry) {
1865 const char *target = pe->path;
1866 struct got_reference *target_ref;
1868 error = got_ref_open(&target_ref, repo, target, 0);
1870 if (error->code == GOT_ERR_NOT_REF) {
1877 error = create_symref(GOT_REF_HEAD, target_ref,
1879 got_ref_close(target_ref);
1887 printf("Created %s repository '%s'\n",
1888 mirror_references ? "mirrored" : "cloned", repo_path);
1891 const struct got_error *pack_err =
1892 got_repo_pack_fds_close(pack_fds);
1897 if (kill(fetchpid, SIGTERM) == -1)
1898 error = got_error_from_errno("kill");
1899 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1900 error = got_error_from_errno("waitpid");
1902 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1903 error = got_error_from_errno("close");
1905 const struct got_error *close_err = got_repo_close(repo);
1909 TAILQ_FOREACH(pe, &refs, entry) {
1910 free((void *)pe->path);
1913 got_pathlist_free(&refs);
1914 TAILQ_FOREACH(pe, &symrefs, entry) {
1915 free((void *)pe->path);
1918 got_pathlist_free(&symrefs);
1919 got_pathlist_free(&wanted_branches);
1920 got_pathlist_free(&wanted_refs);
1927 free(default_destdir);
1932 static const struct got_error *
1933 update_ref(struct got_reference *ref, struct got_object_id *new_id,
1934 int replace_tags, int verbosity, struct got_repository *repo)
1936 const struct got_error *err = NULL;
1937 char *new_id_str = NULL;
1938 struct got_object_id *old_id = NULL;
1940 err = got_object_id_str(&new_id_str, new_id);
1944 if (!replace_tags &&
1945 strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1946 err = got_ref_resolve(&old_id, repo, ref);
1949 if (got_object_id_cmp(old_id, new_id) == 0)
1951 if (verbosity >= 0) {
1952 printf("Rejecting update of existing tag %s: %s\n",
1953 got_ref_get_name(ref), new_id_str);
1958 if (got_ref_is_symbolic(ref)) {
1959 if (verbosity >= 0) {
1960 printf("Replacing reference %s: %s\n",
1961 got_ref_get_name(ref),
1962 got_ref_get_symref_target(ref));
1964 err = got_ref_change_symref_to_ref(ref, new_id);
1967 err = got_ref_write(ref, repo);
1971 err = got_ref_resolve(&old_id, repo, ref);
1974 if (got_object_id_cmp(old_id, new_id) == 0)
1977 err = got_ref_change_ref(ref, new_id);
1980 err = got_ref_write(ref, repo);
1986 printf("Updated %s: %s\n", got_ref_get_name(ref),
1994 static const struct got_error *
1995 update_symref(const char *refname, struct got_reference *target_ref,
1996 int verbosity, struct got_repository *repo)
1998 const struct got_error *err = NULL, *unlock_err;
1999 struct got_reference *symref;
2000 int symref_is_locked = 0;
2002 err = got_ref_open(&symref, repo, refname, 1);
2004 if (err->code != GOT_ERR_NOT_REF)
2006 err = got_ref_alloc_symref(&symref, refname, target_ref);
2010 err = got_ref_write(symref, repo);
2015 printf("Created reference %s: %s\n",
2016 got_ref_get_name(symref),
2017 got_ref_get_symref_target(symref));
2019 symref_is_locked = 1;
2021 if (strcmp(got_ref_get_symref_target(symref),
2022 got_ref_get_name(target_ref)) == 0)
2025 err = got_ref_change_symref(symref,
2026 got_ref_get_name(target_ref));
2030 err = got_ref_write(symref, repo);
2035 printf("Updated %s: %s\n", got_ref_get_name(symref),
2036 got_ref_get_symref_target(symref));
2040 if (symref_is_locked) {
2041 unlock_err = got_ref_unlock(symref);
2042 if (unlock_err && err == NULL)
2045 got_ref_close(symref);
2052 fprintf(stderr, "usage: %s fetch [-adlqtvX] [-b branch] "
2053 "[-R reference] [-r repository-path] [remote-repository]\n",
2058 static const struct got_error *
2059 delete_missing_ref(struct got_reference *ref,
2060 int verbosity, struct got_repository *repo)
2062 const struct got_error *err = NULL;
2063 struct got_object_id *id = NULL;
2064 char *id_str = NULL;
2066 if (got_ref_is_symbolic(ref)) {
2067 err = got_ref_delete(ref, repo);
2070 if (verbosity >= 0) {
2071 printf("Deleted %s: %s\n",
2072 got_ref_get_name(ref),
2073 got_ref_get_symref_target(ref));
2076 err = got_ref_resolve(&id, repo, ref);
2079 err = got_object_id_str(&id_str, id);
2083 err = got_ref_delete(ref, repo);
2086 if (verbosity >= 0) {
2087 printf("Deleted %s: %s\n",
2088 got_ref_get_name(ref), id_str);
2097 static const struct got_error *
2098 delete_missing_refs(struct got_pathlist_head *their_refs,
2099 struct got_pathlist_head *their_symrefs,
2100 const struct got_remote_repo *remote,
2101 int verbosity, struct got_repository *repo)
2103 const struct got_error *err = NULL, *unlock_err;
2104 struct got_reflist_head my_refs;
2105 struct got_reflist_entry *re;
2106 struct got_pathlist_entry *pe;
2107 char *remote_namespace = NULL;
2108 char *local_refname = NULL;
2110 TAILQ_INIT(&my_refs);
2112 if (asprintf(&remote_namespace, "refs/remotes/%s/", remote->name)
2114 return got_error_from_errno("asprintf");
2116 err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
2120 TAILQ_FOREACH(re, &my_refs, entry) {
2121 const char *refname = got_ref_get_name(re->ref);
2122 const char *their_refname;
2124 if (remote->mirror_references) {
2125 their_refname = refname;
2127 if (strncmp(refname, remote_namespace,
2128 strlen(remote_namespace)) == 0) {
2129 if (strcmp(refname + strlen(remote_namespace),
2132 if (asprintf(&local_refname, "refs/heads/%s",
2133 refname + strlen(remote_namespace)) == -1) {
2134 err = got_error_from_errno("asprintf");
2137 } else if (strncmp(refname, "refs/tags/", 10) != 0)
2140 their_refname = local_refname;
2143 TAILQ_FOREACH(pe, their_refs, entry) {
2144 if (strcmp(their_refname, pe->path) == 0)
2150 TAILQ_FOREACH(pe, their_symrefs, entry) {
2151 if (strcmp(their_refname, pe->path) == 0)
2157 err = delete_missing_ref(re->ref, verbosity, repo);
2161 if (local_refname) {
2162 struct got_reference *ref;
2163 err = got_ref_open(&ref, repo, local_refname, 1);
2165 if (err->code != GOT_ERR_NOT_REF)
2167 free(local_refname);
2168 local_refname = NULL;
2171 err = delete_missing_ref(ref, verbosity, repo);
2174 unlock_err = got_ref_unlock(ref);
2176 if (unlock_err && err == NULL) {
2181 free(local_refname);
2182 local_refname = NULL;
2186 got_ref_list_free(&my_refs);
2187 free(remote_namespace);
2188 free(local_refname);
2192 static const struct got_error *
2193 update_wanted_ref(const char *refname, struct got_object_id *id,
2194 const char *remote_repo_name, int verbosity, struct got_repository *repo)
2196 const struct got_error *err, *unlock_err;
2197 char *remote_refname;
2198 struct got_reference *ref;
2200 if (strncmp("refs/", refname, 5) == 0)
2203 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2204 remote_repo_name, refname) == -1)
2205 return got_error_from_errno("asprintf");
2207 err = got_ref_open(&ref, repo, remote_refname, 1);
2209 if (err->code != GOT_ERR_NOT_REF)
2211 err = create_ref(remote_refname, id, verbosity, repo);
2213 err = update_ref(ref, id, 0, verbosity, repo);
2214 unlock_err = got_ref_unlock(ref);
2215 if (unlock_err && err == NULL)
2220 free(remote_refname);
2224 static const struct got_error *
2225 delete_ref(struct got_repository *repo, struct got_reference *ref)
2227 const struct got_error *err = NULL;
2228 struct got_object_id *id = NULL;
2229 char *id_str = NULL;
2232 if (got_ref_is_symbolic(ref)) {
2233 target = got_ref_get_symref_target(ref);
2235 err = got_ref_resolve(&id, repo, ref);
2238 err = got_object_id_str(&id_str, id);
2244 err = got_ref_delete(ref, repo);
2248 printf("Deleted %s: %s\n", got_ref_get_name(ref), target);
2255 static const struct got_error *
2256 delete_refs_for_remote(struct got_repository *repo, const char *remote_name)
2258 const struct got_error *err = NULL;
2259 struct got_reflist_head refs;
2260 struct got_reflist_entry *re;
2265 if (asprintf(&prefix, "refs/remotes/%s", remote_name) == -1) {
2266 err = got_error_from_errno("asprintf");
2269 err = got_ref_list(&refs, repo, prefix, got_ref_cmp_by_name, NULL);
2273 TAILQ_FOREACH(re, &refs, entry)
2274 delete_ref(repo, re->ref);
2276 got_ref_list_free(&refs);
2280 static const struct got_error *
2281 cmd_fetch(int argc, char *argv[])
2283 const struct got_error *error = NULL, *unlock_err;
2284 char *cwd = NULL, *repo_path = NULL;
2285 const char *remote_name;
2286 char *proto = NULL, *host = NULL, *port = NULL;
2287 char *repo_name = NULL, *server_path = NULL;
2288 const struct got_remote_repo *remotes, *remote = NULL;
2290 char *id_str = NULL;
2291 struct got_repository *repo = NULL;
2292 struct got_worktree *worktree = NULL;
2293 const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
2294 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
2295 struct got_pathlist_entry *pe;
2296 struct got_object_id *pack_hash = NULL;
2297 int i, ch, fetchfd = -1, fetchstatus;
2298 pid_t fetchpid = -1;
2299 struct got_fetch_progress_arg fpa;
2300 int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
2301 int delete_refs = 0, replace_tags = 0, delete_remote = 0;
2302 int *pack_fds = NULL;
2305 TAILQ_INIT(&symrefs);
2306 TAILQ_INIT(&wanted_branches);
2307 TAILQ_INIT(&wanted_refs);
2309 while ((ch = getopt(argc, argv, "ab:dlqR:r:tvX")) != -1) {
2312 fetch_all_branches = 1;
2315 error = got_pathlist_append(&wanted_branches,
2330 error = got_pathlist_append(&wanted_refs,
2336 repo_path = realpath(optarg, NULL);
2337 if (repo_path == NULL)
2338 return got_error_from_errno2("realpath",
2340 got_path_strip_trailing_slashes(repo_path);
2348 else if (verbosity < 3)
2362 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
2363 option_conflict('a', 'b');
2364 if (list_refs_only) {
2365 if (!TAILQ_EMPTY(&wanted_branches))
2366 option_conflict('l', 'b');
2367 if (fetch_all_branches)
2368 option_conflict('l', 'a');
2370 option_conflict('l', 'd');
2372 option_conflict('l', 'X');
2374 if (delete_remote) {
2375 if (fetch_all_branches)
2376 option_conflict('X', 'a');
2377 if (!TAILQ_EMPTY(&wanted_branches))
2378 option_conflict('X', 'b');
2380 option_conflict('X', 'd');
2382 option_conflict('X', 't');
2383 if (!TAILQ_EMPTY(&wanted_refs))
2384 option_conflict('X', 'R');
2389 errx(1, "-X option requires a remote name");
2390 remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
2391 } else if (argc == 1)
2392 remote_name = argv[0];
2396 cwd = getcwd(NULL, 0);
2398 error = got_error_from_errno("getcwd");
2402 error = got_repo_pack_fds_open(&pack_fds);
2406 if (repo_path == NULL) {
2407 error = got_worktree_open(&worktree, cwd);
2408 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2414 strdup(got_worktree_get_repo_path(worktree));
2415 if (repo_path == NULL)
2416 error = got_error_from_errno("strdup");
2420 repo_path = strdup(cwd);
2421 if (repo_path == NULL) {
2422 error = got_error_from_errno("strdup");
2428 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
2432 if (delete_remote) {
2433 error = delete_refs_for_remote(repo, remote_name);
2434 goto done; /* nothing else to do */
2438 worktree_conf = got_worktree_get_gotconfig(worktree);
2439 if (worktree_conf) {
2440 got_gotconfig_get_remotes(&nremotes, &remotes,
2442 for (i = 0; i < nremotes; i++) {
2443 if (strcmp(remotes[i].name, remote_name) == 0) {
2444 remote = &remotes[i];
2450 if (remote == NULL) {
2451 repo_conf = got_repo_get_gotconfig(repo);
2453 got_gotconfig_get_remotes(&nremotes, &remotes,
2455 for (i = 0; i < nremotes; i++) {
2456 if (strcmp(remotes[i].name, remote_name) == 0) {
2457 remote = &remotes[i];
2463 if (remote == NULL) {
2464 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
2465 for (i = 0; i < nremotes; i++) {
2466 if (strcmp(remotes[i].name, remote_name) == 0) {
2467 remote = &remotes[i];
2472 if (remote == NULL) {
2473 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
2477 if (TAILQ_EMPTY(&wanted_branches)) {
2478 if (!fetch_all_branches)
2479 fetch_all_branches = remote->fetch_all_branches;
2480 for (i = 0; i < remote->nfetch_branches; i++) {
2481 got_pathlist_append(&wanted_branches,
2482 remote->fetch_branches[i], NULL);
2485 if (TAILQ_EMPTY(&wanted_refs)) {
2486 for (i = 0; i < remote->nfetch_refs; i++) {
2487 got_pathlist_append(&wanted_refs,
2488 remote->fetch_refs[i], NULL);
2492 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
2493 &repo_name, remote->fetch_url);
2497 if (strcmp(proto, "git") == 0) {
2499 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2500 "sendfd dns inet unveil", NULL) == -1)
2503 } else if (strcmp(proto, "git+ssh") == 0 ||
2504 strcmp(proto, "ssh") == 0) {
2506 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2507 "sendfd unveil", NULL) == -1)
2510 } else if (strcmp(proto, "http") == 0 ||
2511 strcmp(proto, "git+http") == 0) {
2512 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
2515 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
2519 error = got_dial_apply_unveil(proto);
2523 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
2528 printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
2529 port ? ":" : "", port ? port : "");
2531 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
2532 server_path, verbosity);
2536 fpa.last_scaled_size[0] = '\0';
2537 fpa.last_p_indexed = -1;
2538 fpa.last_p_resolved = -1;
2539 fpa.verbosity = verbosity;
2541 fpa.create_configs = 0;
2542 fpa.configs_created = 0;
2543 memset(&fpa.config_info, 0, sizeof(fpa.config_info));
2544 error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
2545 remote->mirror_references, fetch_all_branches, &wanted_branches,
2546 &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
2547 fetch_progress, &fpa);
2551 if (list_refs_only) {
2552 error = list_remote_refs(&symrefs, &refs);
2556 if (pack_hash == NULL) {
2558 printf("Already up-to-date\n");
2559 } else if (verbosity >= 0) {
2560 error = got_object_id_str(&id_str, pack_hash);
2563 printf("\nFetched %s.pack\n", id_str);
2568 /* Update references provided with the pack file. */
2569 TAILQ_FOREACH(pe, &refs, entry) {
2570 const char *refname = pe->path;
2571 struct got_object_id *id = pe->data;
2572 struct got_reference *ref;
2573 char *remote_refname;
2575 if (is_wanted_ref(&wanted_refs, refname) &&
2576 !remote->mirror_references) {
2577 error = update_wanted_ref(refname, id,
2578 remote->name, verbosity, repo);
2584 if (remote->mirror_references ||
2585 strncmp("refs/tags/", refname, 10) == 0) {
2586 error = got_ref_open(&ref, repo, refname, 1);
2588 if (error->code != GOT_ERR_NOT_REF)
2590 error = create_ref(refname, id, verbosity,
2595 error = update_ref(ref, id, replace_tags,
2597 unlock_err = got_ref_unlock(ref);
2598 if (unlock_err && error == NULL)
2604 } else if (strncmp("refs/heads/", refname, 11) == 0) {
2605 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2606 remote_name, refname + 11) == -1) {
2607 error = got_error_from_errno("asprintf");
2611 error = got_ref_open(&ref, repo, remote_refname, 1);
2613 if (error->code != GOT_ERR_NOT_REF)
2615 error = create_ref(remote_refname, id,
2620 error = update_ref(ref, id, replace_tags,
2622 unlock_err = got_ref_unlock(ref);
2623 if (unlock_err && error == NULL)
2630 /* Also create a local branch if none exists yet. */
2631 error = got_ref_open(&ref, repo, refname, 1);
2633 if (error->code != GOT_ERR_NOT_REF)
2635 error = create_ref(refname, id, verbosity,
2640 unlock_err = got_ref_unlock(ref);
2641 if (unlock_err && error == NULL)
2648 error = delete_missing_refs(&refs, &symrefs, remote,
2654 if (!remote->mirror_references) {
2655 /* Update remote HEAD reference if the server provided one. */
2656 TAILQ_FOREACH(pe, &symrefs, entry) {
2657 struct got_reference *target_ref;
2658 const char *refname = pe->path;
2659 const char *target = pe->data;
2660 char *remote_refname = NULL, *remote_target = NULL;
2662 if (strcmp(refname, GOT_REF_HEAD) != 0)
2665 if (strncmp("refs/heads/", target, 11) != 0)
2668 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2669 remote->name, refname) == -1) {
2670 error = got_error_from_errno("asprintf");
2673 if (asprintf(&remote_target, "refs/remotes/%s/%s",
2674 remote->name, target + 11) == -1) {
2675 error = got_error_from_errno("asprintf");
2676 free(remote_refname);
2680 error = got_ref_open(&target_ref, repo, remote_target,
2683 free(remote_refname);
2684 free(remote_target);
2685 if (error->code == GOT_ERR_NOT_REF) {
2691 error = update_symref(remote_refname, target_ref,
2693 free(remote_refname);
2694 free(remote_target);
2695 got_ref_close(target_ref);
2702 if (kill(fetchpid, SIGTERM) == -1)
2703 error = got_error_from_errno("kill");
2704 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
2705 error = got_error_from_errno("waitpid");
2707 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
2708 error = got_error_from_errno("close");
2710 const struct got_error *close_err = got_repo_close(repo);
2715 got_worktree_close(worktree);
2717 const struct got_error *pack_err =
2718 got_repo_pack_fds_close(pack_fds);
2722 TAILQ_FOREACH(pe, &refs, entry) {
2723 free((void *)pe->path);
2726 got_pathlist_free(&refs);
2727 TAILQ_FOREACH(pe, &symrefs, entry) {
2728 free((void *)pe->path);
2731 got_pathlist_free(&symrefs);
2732 got_pathlist_free(&wanted_branches);
2733 got_pathlist_free(&wanted_refs);
2748 usage_checkout(void)
2750 fprintf(stderr, "usage: %s checkout [-Eq] [-b branch] [-c commit] "
2751 "[-p path-prefix] repository-path [work-tree-path]\n",
2757 show_worktree_base_ref_warning(void)
2759 fprintf(stderr, "%s: warning: could not create a reference "
2760 "to the work tree's base commit; the commit could be "
2761 "garbage-collected by Git or 'gotadmin cleanup'; making the "
2762 "repository writable and running 'got update' will prevent this\n",
2766 struct got_checkout_progress_arg {
2767 const char *worktree_path;
2768 int had_base_commit_ref_error;
2772 static const struct got_error *
2773 checkout_progress(void *arg, unsigned char status, const char *path)
2775 struct got_checkout_progress_arg *a = arg;
2777 /* Base commit bump happens silently. */
2778 if (status == GOT_STATUS_BUMP_BASE)
2781 if (status == GOT_STATUS_BASE_REF_ERR) {
2782 a->had_base_commit_ref_error = 1;
2786 while (path[0] == '/')
2789 if (a->verbosity >= 0)
2790 printf("%c %s/%s\n", status, a->worktree_path, path);
2795 static const struct got_error *
2796 check_cancelled(void *arg)
2798 if (sigint_received || sigpipe_received)
2799 return got_error(GOT_ERR_CANCELLED);
2803 static const struct got_error *
2804 check_linear_ancestry(struct got_object_id *commit_id,
2805 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2806 struct got_repository *repo)
2808 const struct got_error *err = NULL;
2809 struct got_object_id *yca_id;
2811 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2812 commit_id, base_commit_id, 1, repo, check_cancelled, NULL);
2817 return got_error(GOT_ERR_ANCESTRY);
2820 * Require a straight line of history between the target commit
2821 * and the work tree's base commit.
2823 * Non-linear situations such as this require a rebase:
2825 * (commit) D F (base_commit)
2833 * 'got update' only handles linear cases:
2834 * Update forwards in time: A (base/yca) - B - C - D (commit)
2835 * Update backwards in time: D (base) - C - B - A (commit/yca)
2837 if (allow_forwards_in_time_only) {
2838 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2839 return got_error(GOT_ERR_ANCESTRY);
2840 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2841 got_object_id_cmp(base_commit_id, yca_id) != 0)
2842 return got_error(GOT_ERR_ANCESTRY);
2848 static const struct got_error *
2849 check_same_branch(struct got_object_id *commit_id,
2850 struct got_reference *head_ref, struct got_object_id *yca_id,
2851 struct got_repository *repo)
2853 const struct got_error *err = NULL;
2854 struct got_commit_graph *graph = NULL;
2855 struct got_object_id *head_commit_id = NULL;
2856 int is_same_branch = 0;
2858 err = got_ref_resolve(&head_commit_id, repo, head_ref);
2862 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
2866 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
2871 err = got_commit_graph_open(&graph, "/", 1);
2875 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2876 check_cancelled, NULL);
2881 struct got_object_id id;
2883 err = got_commit_graph_iter_next(&id, graph, repo,
2884 check_cancelled, NULL);
2886 if (err->code == GOT_ERR_ITER_COMPLETED)
2891 if (yca_id && got_object_id_cmp(&id, yca_id) == 0)
2893 if (got_object_id_cmp(&id, commit_id) == 0) {
2900 got_commit_graph_close(graph);
2901 free(head_commit_id);
2902 if (!err && !is_same_branch)
2903 err = got_error(GOT_ERR_ANCESTRY);
2907 static const struct got_error *
2908 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2910 static char msg[512];
2911 const char *branch_name;
2913 if (got_ref_is_symbolic(ref))
2914 branch_name = got_ref_get_symref_target(ref);
2916 branch_name = got_ref_get_name(ref);
2918 if (strncmp("refs/heads/", branch_name, 11) == 0)
2921 snprintf(msg, sizeof(msg),
2922 "target commit is not contained in branch '%s'; "
2923 "the branch to use must be specified with -b; "
2924 "if necessary a new branch can be created for "
2925 "this commit with 'got branch -c %s BRANCH_NAME'",
2926 branch_name, commit_id_str);
2928 return got_error_msg(GOT_ERR_ANCESTRY, msg);
2931 static const struct got_error *
2932 cmd_checkout(int argc, char *argv[])
2934 const struct got_error *error = NULL;
2935 struct got_repository *repo = NULL;
2936 struct got_reference *head_ref = NULL, *ref = NULL;
2937 struct got_worktree *worktree = NULL;
2938 char *repo_path = NULL;
2939 char *worktree_path = NULL;
2940 const char *path_prefix = "";
2941 const char *branch_name = GOT_REF_HEAD, *refname = NULL;
2942 char *commit_id_str = NULL;
2943 struct got_object_id *commit_id = NULL;
2945 int ch, same_path_prefix, allow_nonempty = 0, verbosity = 0;
2946 struct got_pathlist_head paths;
2947 struct got_checkout_progress_arg cpa;
2948 int *pack_fds = NULL;
2952 while ((ch = getopt(argc, argv, "b:c:Ep:q")) != -1) {
2955 branch_name = optarg;
2958 commit_id_str = strdup(optarg);
2959 if (commit_id_str == NULL)
2960 return got_error_from_errno("strdup");
2966 path_prefix = optarg;
2981 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2982 "unveil", NULL) == -1)
2986 char *base, *dotgit;
2988 repo_path = realpath(argv[0], NULL);
2989 if (repo_path == NULL)
2990 return got_error_from_errno2("realpath", argv[0]);
2991 cwd = getcwd(NULL, 0);
2993 error = got_error_from_errno("getcwd");
3000 error = got_path_basename(&base, path);
3003 dotgit = strstr(base, ".git");
3006 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
3007 error = got_error_from_errno("asprintf");
3012 } else if (argc == 2) {
3013 repo_path = realpath(argv[0], NULL);
3014 if (repo_path == NULL) {
3015 error = got_error_from_errno2("realpath", argv[0]);
3018 worktree_path = realpath(argv[1], NULL);
3019 if (worktree_path == NULL) {
3020 if (errno != ENOENT) {
3021 error = got_error_from_errno2("realpath",
3025 worktree_path = strdup(argv[1]);
3026 if (worktree_path == NULL) {
3027 error = got_error_from_errno("strdup");
3034 got_path_strip_trailing_slashes(repo_path);
3035 got_path_strip_trailing_slashes(worktree_path);
3037 error = got_repo_pack_fds_open(&pack_fds);
3041 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3045 /* Pre-create work tree path for unveil(2) */
3046 error = got_path_mkdir(worktree_path);
3048 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
3049 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
3051 if (!allow_nonempty &&
3052 !got_path_dir_is_empty(worktree_path)) {
3053 error = got_error_path(worktree_path,
3054 GOT_ERR_DIR_NOT_EMPTY);
3059 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
3063 error = got_ref_open(&head_ref, repo, branch_name, 0);
3067 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
3068 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
3071 error = got_worktree_open(&worktree, worktree_path);
3075 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
3079 if (!same_path_prefix) {
3080 error = got_error(GOT_ERR_PATH_PREFIX);
3084 if (commit_id_str) {
3085 struct got_reflist_head refs;
3087 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3091 error = got_repo_match_object_id(&commit_id, NULL,
3092 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3093 got_ref_list_free(&refs);
3096 error = check_linear_ancestry(commit_id,
3097 got_worktree_get_base_commit_id(worktree), 0, repo);
3098 if (error != NULL) {
3099 if (error->code == GOT_ERR_ANCESTRY) {
3100 error = checkout_ancestry_error(
3101 head_ref, commit_id_str);
3105 error = check_same_branch(commit_id, head_ref, NULL, repo);
3107 if (error->code == GOT_ERR_ANCESTRY) {
3108 error = checkout_ancestry_error(
3109 head_ref, commit_id_str);
3113 error = got_worktree_set_base_commit_id(worktree, repo,
3117 /* Expand potentially abbreviated commit ID string. */
3118 free(commit_id_str);
3119 error = got_object_id_str(&commit_id_str, commit_id);
3123 commit_id = got_object_id_dup(
3124 got_worktree_get_base_commit_id(worktree));
3125 if (commit_id == NULL) {
3126 error = got_error_from_errno("got_object_id_dup");
3129 error = got_object_id_str(&commit_id_str, commit_id);
3134 error = got_pathlist_append(&paths, "", NULL);
3137 cpa.worktree_path = worktree_path;
3138 cpa.had_base_commit_ref_error = 0;
3139 cpa.verbosity = verbosity;
3140 error = got_worktree_checkout_files(worktree, &paths, repo,
3141 checkout_progress, &cpa, check_cancelled, NULL);
3145 if (got_ref_is_symbolic(head_ref)) {
3146 error = got_ref_resolve_symbolic(&ref, repo, head_ref);
3149 refname = got_ref_get_name(ref);
3151 refname = got_ref_get_name(head_ref);
3152 printf("Checked out %s: %s\n", refname, commit_id_str);
3153 printf("Now shut up and hack\n");
3154 if (cpa.had_base_commit_ref_error)
3155 show_worktree_base_ref_warning();
3158 const struct got_error *pack_err =
3159 got_repo_pack_fds_close(pack_fds);
3164 got_ref_close(head_ref);
3167 got_pathlist_free(&paths);
3168 free(commit_id_str);
3171 free(worktree_path);
3176 struct got_update_progress_arg {
3188 print_update_progress_stats(struct got_update_progress_arg *upa)
3190 if (!upa->did_something)
3193 if (upa->conflicts > 0)
3194 printf("Files with new merge conflicts: %d\n", upa->conflicts);
3195 if (upa->obstructed > 0)
3196 printf("File paths obstructed by a non-regular file: %d\n",
3198 if (upa->not_updated > 0)
3199 printf("Files not updated because of existing merge "
3200 "conflicts: %d\n", upa->not_updated);
3204 * The meaning of some status codes differs between merge-style operations and
3205 * update operations. For example, the ! status code means "file was missing"
3206 * if changes were merged into the work tree, and "missing file was restored"
3207 * if the work tree was updated. This function should be used by any operation
3208 * which merges changes into the work tree without updating the work tree.
3211 print_merge_progress_stats(struct got_update_progress_arg *upa)
3213 if (!upa->did_something)
3216 if (upa->conflicts > 0)
3217 printf("Files with new merge conflicts: %d\n", upa->conflicts);
3218 if (upa->obstructed > 0)
3219 printf("File paths obstructed by a non-regular file: %d\n",
3221 if (upa->missing > 0)
3222 printf("Files which had incoming changes but could not be "
3223 "found in the work tree: %d\n", upa->missing);
3224 if (upa->not_deleted > 0)
3225 printf("Files not deleted due to differences in deleted "
3226 "content: %d\n", upa->not_deleted);
3227 if (upa->unversioned > 0)
3228 printf("Files not merged because an unversioned file was "
3229 "found in the work tree: %d\n", upa->unversioned);
3235 fprintf(stderr, "usage: %s update [-q] [-b branch] [-c commit] "
3236 "[path ...]\n", getprogname());
3240 static const struct got_error *
3241 update_progress(void *arg, unsigned char status, const char *path)
3243 struct got_update_progress_arg *upa = arg;
3245 if (status == GOT_STATUS_EXISTS ||
3246 status == GOT_STATUS_BASE_REF_ERR)
3249 upa->did_something = 1;
3251 /* Base commit bump happens silently. */
3252 if (status == GOT_STATUS_BUMP_BASE)
3255 if (status == GOT_STATUS_CONFLICT)
3257 if (status == GOT_STATUS_OBSTRUCTED)
3259 if (status == GOT_STATUS_CANNOT_UPDATE)
3261 if (status == GOT_STATUS_MISSING)
3263 if (status == GOT_STATUS_CANNOT_DELETE)
3265 if (status == GOT_STATUS_UNVERSIONED)
3268 while (path[0] == '/')
3270 if (upa->verbosity >= 0)
3271 printf("%c %s\n", status, path);
3276 static const struct got_error *
3277 switch_head_ref(struct got_reference *head_ref,
3278 struct got_object_id *commit_id, struct got_worktree *worktree,
3279 struct got_repository *repo)
3281 const struct got_error *err = NULL;
3283 int ref_has_moved = 0;
3285 /* Trivial case: switching between two different references. */
3286 if (strcmp(got_ref_get_name(head_ref),
3287 got_worktree_get_head_ref_name(worktree)) != 0) {
3288 printf("Switching work tree from %s to %s\n",
3289 got_worktree_get_head_ref_name(worktree),
3290 got_ref_get_name(head_ref));
3291 return got_worktree_set_head_ref(worktree, head_ref);
3294 err = check_linear_ancestry(commit_id,
3295 got_worktree_get_base_commit_id(worktree), 0, repo);
3297 if (err->code != GOT_ERR_ANCESTRY)
3304 /* Switching to a rebased branch with the same reference name. */
3305 err = got_object_id_str(&base_id_str,
3306 got_worktree_get_base_commit_id(worktree));
3309 printf("Reference %s now points at a different branch\n",
3310 got_worktree_get_head_ref_name(worktree));
3311 printf("Switching work tree from %s to %s\n", base_id_str,
3312 got_worktree_get_head_ref_name(worktree));
3316 static const struct got_error *
3317 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
3319 const struct got_error *err;
3322 err = got_worktree_rebase_in_progress(&in_progress, worktree);
3326 return got_error(GOT_ERR_REBASING);
3328 err = got_worktree_histedit_in_progress(&in_progress, worktree);
3332 return got_error(GOT_ERR_HISTEDIT_BUSY);
3337 static const struct got_error *
3338 check_merge_in_progress(struct got_worktree *worktree,
3339 struct got_repository *repo)
3341 const struct got_error *err;
3344 err = got_worktree_merge_in_progress(&in_progress, worktree, repo);
3348 return got_error(GOT_ERR_MERGE_BUSY);
3353 static const struct got_error *
3354 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
3355 char *argv[], struct got_worktree *worktree)
3357 const struct got_error *err = NULL;
3359 struct got_pathlist_entry *new;
3365 return got_error_from_errno("strdup");
3366 return got_pathlist_append(paths, path, NULL);
3369 for (i = 0; i < argc; i++) {
3370 err = got_worktree_resolve_path(&path, worktree, argv[i]);
3373 err = got_pathlist_insert(&new, paths, path, NULL);
3374 if (err || new == NULL /* duplicate */) {
3384 static const struct got_error *
3385 wrap_not_worktree_error(const struct got_error *orig_err,
3386 const char *cmdname, const char *path)
3388 const struct got_error *err;
3389 struct got_repository *repo;
3390 static char msg[512];
3391 int *pack_fds = NULL;
3393 err = got_repo_pack_fds_open(&pack_fds);
3397 err = got_repo_open(&repo, path, NULL, pack_fds);
3401 snprintf(msg, sizeof(msg),
3402 "'got %s' needs a work tree in addition to a git repository\n"
3403 "Work trees can be checked out from this Git repository with "
3405 "The got(1) manual page contains more information.", cmdname);
3406 err = got_error_msg(GOT_ERR_NOT_WORKTREE, msg);
3407 got_repo_close(repo);
3409 const struct got_error *pack_err =
3410 got_repo_pack_fds_close(pack_fds);
3417 static const struct got_error *
3418 cmd_update(int argc, char *argv[])
3420 const struct got_error *error = NULL;
3421 struct got_repository *repo = NULL;
3422 struct got_worktree *worktree = NULL;
3423 char *worktree_path = NULL;
3424 struct got_object_id *commit_id = NULL;
3425 char *commit_id_str = NULL;
3426 const char *branch_name = NULL;
3427 struct got_reference *head_ref = NULL;
3428 struct got_pathlist_head paths;
3429 struct got_pathlist_entry *pe;
3430 int ch, verbosity = 0;
3431 struct got_update_progress_arg upa;
3432 int *pack_fds = NULL;
3436 while ((ch = getopt(argc, argv, "b:c:q")) != -1) {
3439 branch_name = optarg;
3442 commit_id_str = strdup(optarg);
3443 if (commit_id_str == NULL)
3444 return got_error_from_errno("strdup");
3459 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3460 "unveil", NULL) == -1)
3463 worktree_path = getcwd(NULL, 0);
3464 if (worktree_path == NULL) {
3465 error = got_error_from_errno("getcwd");
3469 error = got_repo_pack_fds_open(&pack_fds);
3473 error = got_worktree_open(&worktree, worktree_path);
3475 if (error->code == GOT_ERR_NOT_WORKTREE)
3476 error = wrap_not_worktree_error(error, "update",
3481 error = check_rebase_or_histedit_in_progress(worktree);
3485 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3490 error = apply_unveil(got_repo_get_path(repo), 0,
3491 got_worktree_get_root_path(worktree));
3495 error = check_merge_in_progress(worktree, repo);
3499 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3503 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
3504 got_worktree_get_head_ref_name(worktree), 0);
3507 if (commit_id_str == NULL) {
3508 error = got_ref_resolve(&commit_id, repo, head_ref);
3511 error = got_object_id_str(&commit_id_str, commit_id);
3515 struct got_reflist_head refs;
3517 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3521 error = got_repo_match_object_id(&commit_id, NULL,
3522 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3523 got_ref_list_free(&refs);
3524 free(commit_id_str);
3525 commit_id_str = NULL;
3528 error = got_object_id_str(&commit_id_str, commit_id);
3534 struct got_object_id *head_commit_id;
3535 TAILQ_FOREACH(pe, &paths, entry) {
3536 if (pe->path_len == 0)
3538 error = got_error_msg(GOT_ERR_BAD_PATH,
3539 "switching between branches requires that "
3540 "the entire work tree gets updated");
3543 error = got_ref_resolve(&head_commit_id, repo, head_ref);
3546 error = check_linear_ancestry(commit_id, head_commit_id, 0,
3548 free(head_commit_id);
3551 error = check_same_branch(commit_id, head_ref, NULL, repo);
3554 error = switch_head_ref(head_ref, commit_id, worktree, repo);
3558 error = check_linear_ancestry(commit_id,
3559 got_worktree_get_base_commit_id(worktree), 0, repo);
3560 if (error != NULL) {
3561 if (error->code == GOT_ERR_ANCESTRY)
3562 error = got_error(GOT_ERR_BRANCH_MOVED);
3565 error = check_same_branch(commit_id, head_ref, NULL, repo);
3570 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
3572 error = got_worktree_set_base_commit_id(worktree, repo,
3578 memset(&upa, 0, sizeof(upa));
3579 upa.verbosity = verbosity;
3580 error = got_worktree_checkout_files(worktree, &paths, repo,
3581 update_progress, &upa, check_cancelled, NULL);
3585 if (upa.did_something) {
3586 printf("Updated to %s: %s\n",
3587 got_worktree_get_head_ref_name(worktree), commit_id_str);
3589 printf("Already up-to-date\n");
3591 print_update_progress_stats(&upa);
3594 const struct got_error *pack_err =
3595 got_repo_pack_fds_close(pack_fds);
3599 free(worktree_path);
3600 TAILQ_FOREACH(pe, &paths, entry)
3601 free((char *)pe->path);
3602 got_pathlist_free(&paths);
3604 free(commit_id_str);
3608 static const struct got_error *
3609 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
3610 const char *path, int diff_context, int ignore_whitespace,
3611 int force_text_diff, struct got_repository *repo, FILE *outfile)
3613 const struct got_error *err = NULL;
3614 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
3615 FILE *f1 = NULL, *f2 = NULL;
3616 int fd1 = -1, fd2 = -1;
3618 fd1 = got_opentempfd();
3620 return got_error_from_errno("got_opentempfd");
3621 fd2 = got_opentempfd();
3623 err = got_error_from_errno("got_opentempfd");
3628 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192,
3634 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192, fd2);
3638 f1 = got_opentemp();
3640 err = got_error_from_errno("got_opentemp");
3643 f2 = got_opentemp();
3645 err = got_error_from_errno("got_opentemp");
3649 while (path[0] == '/')
3651 err = got_diff_blob(NULL, NULL, blob1, blob2, f1, f2, path, path,
3652 GOT_DIFF_ALGORITHM_PATIENCE, diff_context, ignore_whitespace,
3653 force_text_diff, outfile);
3655 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3656 err = got_error_from_errno("close");
3658 got_object_blob_close(blob1);
3659 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3660 err = got_error_from_errno("close");
3661 got_object_blob_close(blob2);
3662 if (f1 && fclose(f1) == EOF && err == NULL)
3663 err = got_error_from_errno("fclose");
3664 if (f2 && fclose(f2) == EOF && err == NULL)
3665 err = got_error_from_errno("fclose");
3669 static const struct got_error *
3670 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
3671 const char *path, int diff_context, int ignore_whitespace,
3672 int force_text_diff, struct got_repository *repo, FILE *outfile)
3674 const struct got_error *err = NULL;
3675 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3676 struct got_diff_blob_output_unidiff_arg arg;
3677 FILE *f1 = NULL, *f2 = NULL;
3678 int fd1 = -1, fd2 = -1;
3681 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3684 fd1 = got_opentempfd();
3686 err = got_error_from_errno("got_opentempfd");
3691 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3695 f1 = got_opentemp();
3697 err = got_error_from_errno("got_opentemp");
3701 f2 = got_opentemp();
3703 err = got_error_from_errno("got_opentemp");
3706 fd2 = got_opentempfd();
3708 err = got_error_from_errno("got_opentempfd");
3711 arg.diff_context = diff_context;
3712 arg.ignore_whitespace = ignore_whitespace;
3713 arg.force_text_diff = force_text_diff;
3714 arg.diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
3715 arg.outfile = outfile;
3718 while (path[0] == '/')
3720 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, path, path, repo,
3721 got_diff_blob_output_unidiff, &arg, 1);
3724 got_object_tree_close(tree1);
3726 got_object_tree_close(tree2);
3727 if (f1 && fclose(f1) == EOF && err == NULL)
3728 err = got_error_from_errno("fclose");
3729 if (f2 && fclose(f2) == EOF && err == NULL)
3730 err = got_error_from_errno("fclose");
3731 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3732 err = got_error_from_errno("close");
3733 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3734 err = got_error_from_errno("close");
3738 static const struct got_error *
3739 get_changed_paths(struct got_pathlist_head *paths,
3740 struct got_commit_object *commit, struct got_repository *repo)
3742 const struct got_error *err = NULL;
3743 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3744 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3745 struct got_object_qid *qid;
3747 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3749 struct got_commit_object *pcommit;
3750 err = got_object_open_as_commit(&pcommit, repo,
3755 tree_id1 = got_object_id_dup(
3756 got_object_commit_get_tree_id(pcommit));
3757 if (tree_id1 == NULL) {
3758 got_object_commit_close(pcommit);
3759 return got_error_from_errno("got_object_id_dup");
3761 got_object_commit_close(pcommit);
3766 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3771 tree_id2 = got_object_commit_get_tree_id(commit);
3772 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3776 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
3777 got_diff_tree_collect_changed_paths, paths, 0);
3780 got_object_tree_close(tree1);
3782 got_object_tree_close(tree2);
3787 static const struct got_error *
3788 print_patch(struct got_commit_object *commit, struct got_object_id *id,
3789 const char *path, int diff_context, struct got_repository *repo,
3792 const struct got_error *err = NULL;
3793 struct got_commit_object *pcommit = NULL;
3794 char *id_str1 = NULL, *id_str2 = NULL;
3795 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
3796 struct got_object_qid *qid;
3798 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3800 err = got_object_open_as_commit(&pcommit, repo,
3804 err = got_object_id_str(&id_str1, &qid->id);
3809 err = got_object_id_str(&id_str2, id);
3813 if (path && path[0] != '\0') {
3815 err = got_object_id_by_path(&obj_id2, repo, commit, path);
3819 err = got_object_id_by_path(&obj_id1, repo,
3822 if (err->code != GOT_ERR_NO_TREE_ENTRY) {
3828 err = got_object_get_type(&obj_type, repo, obj_id2);
3834 "diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3835 fprintf(outfile, "commit - %s\n",
3836 id_str1 ? id_str1 : "/dev/null");
3837 fprintf(outfile, "commit + %s\n", id_str2);
3839 case GOT_OBJ_TYPE_BLOB:
3840 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
3841 0, 0, repo, outfile);
3843 case GOT_OBJ_TYPE_TREE:
3844 err = diff_trees(obj_id1, obj_id2, path, diff_context,
3845 0, 0, repo, outfile);
3848 err = got_error(GOT_ERR_OBJ_TYPE);
3854 obj_id2 = got_object_commit_get_tree_id(commit);
3856 obj_id1 = got_object_commit_get_tree_id(pcommit);
3858 "diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3859 fprintf(outfile, "commit - %s\n",
3860 id_str1 ? id_str1 : "/dev/null");
3861 fprintf(outfile, "commit + %s\n", id_str2);
3862 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, 0,
3869 got_object_commit_close(pcommit);
3874 get_datestr(time_t *time, char *datebuf)
3876 struct tm mytm, *tm;
3879 tm = gmtime_r(time, &mytm);
3882 s = asctime_r(tm, datebuf);
3885 p = strchr(s, '\n');
3891 static const struct got_error *
3892 match_commit(int *have_match, struct got_object_id *id,
3893 struct got_commit_object *commit, regex_t *regex)
3895 const struct got_error *err = NULL;
3896 regmatch_t regmatch;
3897 char *id_str = NULL, *logmsg = NULL;
3901 err = got_object_id_str(&id_str, id);
3905 err = got_object_commit_get_logmsg(&logmsg, commit);
3909 if (regexec(regex, got_object_commit_get_author(commit), 1,
3910 ®match, 0) == 0 ||
3911 regexec(regex, got_object_commit_get_committer(commit), 1,
3912 ®match, 0) == 0 ||
3913 regexec(regex, id_str, 1, ®match, 0) == 0 ||
3914 regexec(regex, logmsg, 1, ®match, 0) == 0)
3923 match_changed_paths(int *have_match, struct got_pathlist_head *changed_paths,
3926 regmatch_t regmatch;
3927 struct got_pathlist_entry *pe;
3931 TAILQ_FOREACH(pe, changed_paths, entry) {
3932 if (regexec(regex, pe->path, 1, ®match, 0) == 0) {
3939 static const struct got_error *
3940 match_patch(int *have_match, struct got_commit_object *commit,
3941 struct got_object_id *id, const char *path, int diff_context,
3942 struct got_repository *repo, regex_t *regex, FILE *f)
3944 const struct got_error *err = NULL;
3946 size_t linesize = 0;
3947 regmatch_t regmatch;
3951 err = got_opentemp_truncate(f);
3955 err = print_patch(commit, id, path, diff_context, repo, f);
3959 if (fseeko(f, 0L, SEEK_SET) == -1) {
3960 err = got_error_from_errno("fseeko");
3964 while (getline(&line, &linesize, f) != -1) {
3965 if (regexec(regex, line, 1, ®match, 0) == 0) {
3975 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
3977 static const struct got_error*
3978 build_refs_str(char **refs_str, struct got_reflist_head *refs,
3979 struct got_object_id *id, struct got_repository *repo,
3982 static const struct got_error *err = NULL;
3983 struct got_reflist_entry *re;
3989 TAILQ_FOREACH(re, refs, entry) {
3990 struct got_tag_object *tag = NULL;
3991 struct got_object_id *ref_id;
3994 name = got_ref_get_name(re->ref);
3995 if (strcmp(name, GOT_REF_HEAD) == 0)
3997 if (strncmp(name, "refs/", 5) == 0)
3999 if (strncmp(name, "got/", 4) == 0)
4001 if (strncmp(name, "heads/", 6) == 0)
4003 if (strncmp(name, "remotes/", 8) == 0) {
4007 s = strstr(name, "/" GOT_REF_HEAD);
4008 if (s != NULL && s[strlen(s)] == '\0')
4011 err = got_ref_resolve(&ref_id, repo, re->ref);
4014 if (strncmp(name, "tags/", 5) == 0) {
4015 err = got_object_open_as_tag(&tag, repo, ref_id);
4017 if (err->code != GOT_ERR_OBJ_TYPE) {
4021 /* Ref points at something other than a tag. */
4026 cmp = got_object_id_cmp(tag ?
4027 got_object_tag_get_object_id(tag) : ref_id, id);
4030 got_object_tag_close(tag);
4034 if (asprintf(refs_str, "%s%s%s", s ? s : "",
4035 s ? ", " : "", name) == -1) {
4036 err = got_error_from_errno("asprintf");
4047 static const struct got_error *
4048 print_commit_oneline(struct got_commit_object *commit, struct got_object_id *id,
4049 struct got_repository *repo, struct got_reflist_object_id_map *refs_idmap)
4051 const struct got_error *err = NULL;
4052 char *ref_str = NULL, *id_str = NULL, *logmsg0 = NULL;
4053 char *comma, *s, *nl;
4054 struct got_reflist_head *refs;
4055 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
4057 time_t committer_time;
4059 refs = got_reflist_object_id_map_lookup(refs_idmap, id);
4061 err = build_refs_str(&ref_str, refs, id, repo, 1);
4065 /* Display the first matching ref only. */
4066 if (ref_str && (comma = strchr(ref_str, ',')) != NULL)
4070 if (ref_str == NULL) {
4071 err = got_object_id_str(&id_str, id);
4076 committer_time = got_object_commit_get_committer_time(commit);
4077 if (gmtime_r(&committer_time, &tm) == NULL) {
4078 err = got_error_from_errno("gmtime_r");
4081 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0) {
4082 err = got_error(GOT_ERR_NO_SPACE);
4086 err = got_object_commit_get_logmsg(&logmsg0, commit);
4091 while (isspace((unsigned char)s[0]))
4094 nl = strchr(s, '\n');
4100 printf("%s%-7s %s\n", datebuf, ref_str, s);
4102 printf("%s%.7s %s\n", datebuf, id_str, s);
4104 if (fflush(stdout) != 0 && err == NULL)
4105 err = got_error_from_errno("fflush");
4113 static const struct got_error *
4114 print_commit(struct got_commit_object *commit, struct got_object_id *id,
4115 struct got_repository *repo, const char *path,
4116 struct got_pathlist_head *changed_paths, int show_patch,
4117 int diff_context, struct got_reflist_object_id_map *refs_idmap,
4118 const char *custom_refs_str)
4120 const struct got_error *err = NULL;
4121 char *id_str, *datestr, *logmsg0, *logmsg, *line;
4123 time_t committer_time;
4124 const char *author, *committer;
4125 char *refs_str = NULL;
4127 err = got_object_id_str(&id_str, id);
4131 if (custom_refs_str == NULL) {
4132 struct got_reflist_head *refs;
4133 refs = got_reflist_object_id_map_lookup(refs_idmap, id);
4135 err = build_refs_str(&refs_str, refs, id, repo, 0);
4141 printf(GOT_COMMIT_SEP_STR);
4142 if (custom_refs_str)
4143 printf("commit %s (%s)\n", id_str, custom_refs_str);
4145 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4146 refs_str ? refs_str : "", refs_str ? ")" : "");
4151 printf("from: %s\n", got_object_commit_get_author(commit));
4152 committer_time = got_object_commit_get_committer_time(commit);
4153 datestr = get_datestr(&committer_time, datebuf);
4155 printf("date: %s UTC\n", datestr);
4156 author = got_object_commit_get_author(commit);
4157 committer = got_object_commit_get_committer(commit);
4158 if (strcmp(author, committer) != 0)
4159 printf("via: %s\n", committer);
4160 if (got_object_commit_get_nparents(commit) > 1) {
4161 const struct got_object_id_queue *parent_ids;
4162 struct got_object_qid *qid;
4164 parent_ids = got_object_commit_get_parent_ids(commit);
4165 STAILQ_FOREACH(qid, parent_ids, entry) {
4166 err = got_object_id_str(&id_str, &qid->id);
4169 printf("parent %d: %s\n", n++, id_str);
4175 err = got_object_commit_get_logmsg(&logmsg0, commit);
4181 line = strsep(&logmsg, "\n");
4183 printf(" %s\n", line);
4187 if (changed_paths) {
4188 struct got_pathlist_entry *pe;
4189 TAILQ_FOREACH(pe, changed_paths, entry) {
4190 struct got_diff_changed_path *cp = pe->data;
4191 printf(" %c %s\n", cp->status, pe->path);
4196 err = print_patch(commit, id, path, diff_context, repo, stdout);
4201 if (fflush(stdout) != 0 && err == NULL)
4202 err = got_error_from_errno("fflush");
4209 static const struct got_error *
4210 print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
4211 struct got_repository *repo, const char *path, int show_changed_paths,
4212 int show_patch, const char *search_pattern, int diff_context, int limit,
4213 int log_branches, int reverse_display_order,
4214 struct got_reflist_object_id_map *refs_idmap, int one_line,
4217 const struct got_error *err;
4218 struct got_commit_graph *graph;
4221 struct got_object_id_queue reversed_commits;
4222 struct got_object_qid *qid;
4223 struct got_commit_object *commit;
4224 struct got_pathlist_head changed_paths;
4225 struct got_pathlist_entry *pe;
4227 STAILQ_INIT(&reversed_commits);
4228 TAILQ_INIT(&changed_paths);
4230 if (search_pattern && regcomp(®ex, search_pattern,
4231 REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
4232 return got_error_msg(GOT_ERR_REGEX, search_pattern);
4234 err = got_commit_graph_open(&graph, path, !log_branches);
4237 err = got_commit_graph_iter_start(graph, root_id, repo,
4238 check_cancelled, NULL);
4242 struct got_object_id id;
4244 if (sigint_received || sigpipe_received)
4247 err = got_commit_graph_iter_next(&id, graph, repo,
4248 check_cancelled, NULL);
4250 if (err->code == GOT_ERR_ITER_COMPLETED)
4255 err = got_object_open_as_commit(&commit, repo, &id);
4259 if (show_changed_paths && !reverse_display_order) {
4260 err = get_changed_paths(&changed_paths, commit, repo);
4265 if (search_pattern) {
4266 err = match_commit(&have_match, &id, commit, ®ex);
4268 got_object_commit_close(commit);
4271 if (have_match == 0 && show_changed_paths)
4272 match_changed_paths(&have_match,
4273 &changed_paths, ®ex);
4274 if (have_match == 0 && show_patch) {
4275 err = match_patch(&have_match, commit, &id,
4276 path, diff_context, repo, ®ex,
4281 if (have_match == 0) {
4282 got_object_commit_close(commit);
4283 TAILQ_FOREACH(pe, &changed_paths, entry) {
4284 free((char *)pe->path);
4287 got_pathlist_free(&changed_paths);
4292 if (reverse_display_order) {
4293 err = got_object_qid_alloc(&qid, &id);
4296 STAILQ_INSERT_HEAD(&reversed_commits, qid, entry);
4297 got_object_commit_close(commit);
4300 err = print_commit_oneline(commit, &id,
4303 err = print_commit(commit, &id, repo, path,
4304 show_changed_paths ? &changed_paths : NULL,
4305 show_patch, diff_context, refs_idmap, NULL);
4306 got_object_commit_close(commit);
4310 if ((limit && --limit == 0) ||
4311 (end_id && got_object_id_cmp(&id, end_id) == 0))
4314 TAILQ_FOREACH(pe, &changed_paths, entry) {
4315 free((char *)pe->path);
4318 got_pathlist_free(&changed_paths);
4320 if (reverse_display_order) {
4321 STAILQ_FOREACH(qid, &reversed_commits, entry) {
4322 err = got_object_open_as_commit(&commit, repo,
4326 if (show_changed_paths) {
4327 err = get_changed_paths(&changed_paths,
4333 err = print_commit_oneline(commit, &qid->id,
4336 err = print_commit(commit, &qid->id, repo, path,
4337 show_changed_paths ? &changed_paths : NULL,
4338 show_patch, diff_context, refs_idmap, NULL);
4339 got_object_commit_close(commit);
4342 TAILQ_FOREACH(pe, &changed_paths, entry) {
4343 free((char *)pe->path);
4346 got_pathlist_free(&changed_paths);
4350 while (!STAILQ_EMPTY(&reversed_commits)) {
4351 qid = STAILQ_FIRST(&reversed_commits);
4352 STAILQ_REMOVE_HEAD(&reversed_commits, entry);
4353 got_object_qid_free(qid);
4355 TAILQ_FOREACH(pe, &changed_paths, entry) {
4356 free((char *)pe->path);
4359 got_pathlist_free(&changed_paths);
4362 got_commit_graph_close(graph);
4369 fprintf(stderr, "usage: %s log [-bPpRs] [-C number] [-c commit] [-l N] "
4370 "[-r repository-path] [-S search-pattern] [-x commit] [path]\n",
4376 get_default_log_limit(void)
4378 const char *got_default_log_limit;
4382 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
4383 if (got_default_log_limit == NULL)
4385 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
4391 static const struct got_error *
4392 cmd_log(int argc, char *argv[])
4394 const struct got_error *error;
4395 struct got_repository *repo = NULL;
4396 struct got_worktree *worktree = NULL;
4397 struct got_object_id *start_id = NULL, *end_id = NULL;
4398 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
4399 const char *start_commit = NULL, *end_commit = NULL;
4400 const char *search_pattern = NULL;
4401 int diff_context = -1, ch;
4402 int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
4403 int reverse_display_order = 0, one_line = 0;
4405 struct got_reflist_head refs;
4406 struct got_reflist_object_id_map *refs_idmap = NULL;
4407 FILE *tmpfile = NULL;
4408 int *pack_fds = NULL;
4413 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4419 limit = get_default_log_limit();
4421 while ((ch = getopt(argc, argv, "bC:c:l:PpRr:S:sx:")) != -1) {
4427 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4430 errx(1, "number of context lines is %s: %s",
4434 start_commit = optarg;
4437 limit = strtonum(optarg, 0, INT_MAX, &errstr);
4439 errx(1, "number of commits is %s: %s",
4443 show_changed_paths = 1;
4449 reverse_display_order = 1;
4452 repo_path = realpath(optarg, NULL);
4453 if (repo_path == NULL)
4454 return got_error_from_errno2("realpath",
4456 got_path_strip_trailing_slashes(repo_path);
4459 search_pattern = optarg;
4465 end_commit = optarg;
4476 if (diff_context == -1)
4478 else if (!show_patch)
4479 errx(1, "-C requires -p");
4481 if (one_line && (show_patch || show_changed_paths))
4482 errx(1, "cannot use -s with -p or -P");
4484 cwd = getcwd(NULL, 0);
4486 error = got_error_from_errno("getcwd");
4490 error = got_repo_pack_fds_open(&pack_fds);
4494 if (repo_path == NULL) {
4495 error = got_worktree_open(&worktree, cwd);
4496 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4503 error = got_worktree_resolve_path(&path, worktree,
4508 path = strdup(argv[0]);
4510 error = got_error_from_errno("strdup");
4514 } else if (argc != 0)
4517 if (repo_path == NULL) {
4518 repo_path = worktree ?
4519 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
4521 if (repo_path == NULL) {
4522 error = got_error_from_errno("strdup");
4526 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4530 error = apply_unveil(got_repo_get_path(repo), 1,
4531 worktree ? got_worktree_get_root_path(worktree) : NULL);
4535 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4539 error = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
4543 if (start_commit == NULL) {
4544 struct got_reference *head_ref;
4545 struct got_commit_object *commit = NULL;
4546 error = got_ref_open(&head_ref, repo,
4547 worktree ? got_worktree_get_head_ref_name(worktree)
4551 error = got_ref_resolve(&start_id, repo, head_ref);
4552 got_ref_close(head_ref);
4555 error = got_object_open_as_commit(&commit, repo,
4559 got_object_commit_close(commit);
4561 error = got_repo_match_object_id(&start_id, NULL,
4562 start_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4566 if (end_commit != NULL) {
4567 error = got_repo_match_object_id(&end_id, NULL,
4568 end_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4575 * If a path was specified on the command line it was resolved
4576 * to a path in the work tree above. Prepend the work tree's
4577 * path prefix to obtain the corresponding in-repository path.
4581 prefix = got_worktree_get_path_prefix(worktree);
4582 if (asprintf(&in_repo_path, "%s%s%s", prefix,
4583 (path[0] != '\0') ? "/" : "", path) == -1) {
4584 error = got_error_from_errno("asprintf");
4589 error = got_repo_map_path(&in_repo_path, repo,
4595 path = in_repo_path;
4599 /* Release work tree lock. */
4600 got_worktree_close(worktree);
4604 if (search_pattern && show_patch) {
4605 tmpfile = got_opentemp();
4606 if (tmpfile == NULL) {
4607 error = got_error_from_errno("got_opentemp");
4612 error = print_commits(start_id, end_id, repo, path ? path : "",
4613 show_changed_paths, show_patch, search_pattern, diff_context,
4614 limit, log_branches, reverse_display_order, refs_idmap, one_line,
4621 got_worktree_close(worktree);
4623 const struct got_error *close_err = got_repo_close(repo);
4628 const struct got_error *pack_err =
4629 got_repo_pack_fds_close(pack_fds);
4634 got_reflist_object_id_map_free(refs_idmap);
4635 if (tmpfile && fclose(tmpfile) == EOF && error == NULL)
4636 error = got_error_from_errno("fclose");
4637 got_ref_list_free(&refs);
4644 fprintf(stderr, "usage: %s diff [-aPsw] [-C number] [-c commit] "
4645 "[-r repository-path] [object1 object2 | path ...]\n",
4650 struct print_diff_arg {
4651 struct got_repository *repo;
4652 struct got_worktree *worktree;
4657 enum got_diff_algorithm diff_algo;
4658 int ignore_whitespace;
4659 int force_text_diff;
4665 * Create a file which contains the target path of a symlink so we can feed
4666 * it as content to the diff engine.
4668 static const struct got_error *
4669 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
4670 const char *abspath)
4672 const struct got_error *err = NULL;
4673 char target_path[PATH_MAX];
4674 ssize_t target_len, outlen;
4679 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
4680 if (target_len == -1)
4681 return got_error_from_errno2("readlinkat", abspath);
4683 target_len = readlink(abspath, target_path, PATH_MAX);
4684 if (target_len == -1)
4685 return got_error_from_errno2("readlink", abspath);
4688 *fd = got_opentempfd();
4690 return got_error_from_errno("got_opentempfd");
4692 outlen = write(*fd, target_path, target_len);
4694 err = got_error_from_errno("got_opentempfd");
4698 if (lseek(*fd, 0, SEEK_SET) == -1) {
4699 err = got_error_from_errno2("lseek", abspath);
4710 static const struct got_error *
4711 print_diff(void *arg, unsigned char status, unsigned char staged_status,
4712 const char *path, struct got_object_id *blob_id,
4713 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4714 int dirfd, const char *de_name)
4716 struct print_diff_arg *a = arg;
4717 const struct got_error *err = NULL;
4718 struct got_blob_object *blob1 = NULL;
4719 int fd = -1, fd1 = -1, fd2 = -1;
4721 char *abspath = NULL, *label1 = NULL;
4726 memset(&sb, 0, sizeof(sb));
4728 if (a->diff_staged) {
4729 if (staged_status != GOT_STATUS_MODIFY &&
4730 staged_status != GOT_STATUS_ADD &&
4731 staged_status != GOT_STATUS_DELETE)
4734 if (staged_status == GOT_STATUS_DELETE)
4736 if (status == GOT_STATUS_NONEXISTENT)
4737 return got_error_set_errno(ENOENT, path);
4738 if (status != GOT_STATUS_MODIFY &&
4739 status != GOT_STATUS_ADD &&
4740 status != GOT_STATUS_DELETE &&
4741 status != GOT_STATUS_CONFLICT)
4745 err = got_opentemp_truncate(a->f1);
4747 return got_error_from_errno("got_opentemp_truncate");
4748 err = got_opentemp_truncate(a->f2);
4750 return got_error_from_errno("got_opentemp_truncate");
4752 if (!a->header_shown) {
4753 printf("diff %s%s\n", a->diff_staged ? "-s " : "",
4754 got_worktree_get_root_path(a->worktree));
4755 printf("commit - %s\n", a->id_str);
4756 printf("path + %s%s\n",
4757 got_worktree_get_root_path(a->worktree),
4758 a->diff_staged ? " (staged changes)" : "");
4759 a->header_shown = 1;
4762 if (a->diff_staged) {
4763 const char *label1 = NULL, *label2 = NULL;
4764 switch (staged_status) {
4765 case GOT_STATUS_MODIFY:
4769 case GOT_STATUS_ADD:
4772 case GOT_STATUS_DELETE:
4776 return got_error(GOT_ERR_FILE_STATUS);
4778 fd1 = got_opentempfd();
4780 err = got_error_from_errno("got_opentempfd");
4783 fd2 = got_opentempfd();
4785 err = got_error_from_errno("got_opentempfd");
4788 err = got_diff_objects_as_blobs(NULL, NULL, a->f1, a->f2,
4789 fd1, fd2, blob_id, staged_blob_id, label1, label2,
4790 a->diff_algo, a->diff_context, a->ignore_whitespace,
4791 a->force_text_diff, a->repo, stdout);
4795 fd1 = got_opentempfd();
4797 err = got_error_from_errno("got_opentempfd");
4801 if (staged_status == GOT_STATUS_ADD ||
4802 staged_status == GOT_STATUS_MODIFY) {
4804 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
4808 err = got_object_id_str(&id_str, staged_blob_id);
4811 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
4812 err = got_error_from_errno("asprintf");
4817 } else if (status != GOT_STATUS_ADD) {
4818 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192,
4824 if (status != GOT_STATUS_DELETE) {
4825 if (asprintf(&abspath, "%s/%s",
4826 got_worktree_get_root_path(a->worktree), path) == -1) {
4827 err = got_error_from_errno("asprintf");
4832 fd = openat(dirfd, de_name,
4833 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4835 if (!got_err_open_nofollow_on_symlink()) {
4836 err = got_error_from_errno2("openat",
4840 err = get_symlink_target_file(&fd, dirfd,
4846 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4848 if (!got_err_open_nofollow_on_symlink()) {
4849 err = got_error_from_errno2("open",
4853 err = get_symlink_target_file(&fd, dirfd,
4859 if (fstatat(fd, abspath, &sb, AT_SYMLINK_NOFOLLOW) == -1) {
4860 err = got_error_from_errno2("fstatat", abspath);
4863 f2 = fdopen(fd, "r");
4865 err = got_error_from_errno2("fdopen", abspath);
4873 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
4879 err = got_diff_blob_file(blob1, a->f1, size1, label1, f2 ? f2 : a->f2,
4880 f2_exists, &sb, path, GOT_DIFF_ALGORITHM_PATIENCE, a->diff_context,
4881 a->ignore_whitespace, a->force_text_diff, stdout);
4883 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
4884 err = got_error_from_errno("close");
4885 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4886 err = got_error_from_errno("close");
4888 got_object_blob_close(blob1);
4889 if (fd != -1 && close(fd) == -1 && err == NULL)
4890 err = got_error_from_errno("close");
4891 if (f2 && fclose(f2) == EOF && err == NULL)
4892 err = got_error_from_errno("fclose");
4897 static const struct got_error *
4898 cmd_diff(int argc, char *argv[])
4900 const struct got_error *error;
4901 struct got_repository *repo = NULL;
4902 struct got_worktree *worktree = NULL;
4903 char *cwd = NULL, *repo_path = NULL;
4904 const char *commit_args[2] = { NULL, NULL };
4905 int ncommit_args = 0;
4906 struct got_object_id *ids[2] = { NULL, NULL };
4907 char *labels[2] = { NULL, NULL };
4908 int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
4909 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch, i;
4910 int force_text_diff = 0, force_path = 0, rflag = 0;
4912 struct got_reflist_head refs;
4913 struct got_pathlist_head paths;
4914 struct got_pathlist_entry *pe;
4915 FILE *f1 = NULL, *f2 = NULL;
4916 int fd1 = -1, fd2 = -1;
4917 int *pack_fds = NULL;
4923 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4928 while ((ch = getopt(argc, argv, "aC:c:Pr:sw")) != -1) {
4931 force_text_diff = 1;
4934 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4937 errx(1, "number of context lines is %s: %s",
4941 if (ncommit_args >= 2)
4942 errx(1, "too many -c options used");
4943 commit_args[ncommit_args++] = optarg;
4949 repo_path = realpath(optarg, NULL);
4950 if (repo_path == NULL)
4951 return got_error_from_errno2("realpath",
4953 got_path_strip_trailing_slashes(repo_path);
4960 ignore_whitespace = 1;
4971 cwd = getcwd(NULL, 0);
4973 error = got_error_from_errno("getcwd");
4977 error = got_repo_pack_fds_open(&pack_fds);
4981 if (repo_path == NULL) {
4982 error = got_worktree_open(&worktree, cwd);
4983 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4989 strdup(got_worktree_get_repo_path(worktree));
4990 if (repo_path == NULL) {
4991 error = got_error_from_errno("strdup");
4995 repo_path = strdup(cwd);
4996 if (repo_path == NULL) {
4997 error = got_error_from_errno("strdup");
5003 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5008 if (rflag || worktree == NULL || ncommit_args > 0) {
5010 error = got_error_msg(GOT_ERR_NOT_IMPL,
5011 "-P option can only be used when diffing "
5016 error = got_error_msg(GOT_ERR_NOT_IMPL,
5017 "-s option can only be used when diffing "
5023 error = apply_unveil(got_repo_get_path(repo), 1,
5024 worktree ? got_worktree_get_root_path(worktree) : NULL);
5028 if ((!force_path && argc == 2) || ncommit_args > 0) {
5029 int obj_type = (ncommit_args > 0 ?
5030 GOT_OBJ_TYPE_COMMIT : GOT_OBJ_TYPE_ANY);
5031 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5035 for (i = 0; i < (ncommit_args > 0 ? ncommit_args : argc); i++) {
5037 if (ncommit_args > 0)
5038 arg = commit_args[i];
5041 error = got_repo_match_object_id(&ids[i], &labels[i],
5042 arg, obj_type, &refs, repo);
5044 if (error->code != GOT_ERR_NOT_REF &&
5045 error->code != GOT_ERR_NO_OBJ)
5047 if (ncommit_args > 0)
5055 f1 = got_opentemp();
5057 error = got_error_from_errno("got_opentemp");
5061 f2 = got_opentemp();
5063 error = got_error_from_errno("got_opentemp");
5067 if (ncommit_args == 0 && (ids[0] == NULL || ids[1] == NULL)) {
5068 struct print_diff_arg arg;
5071 if (worktree == NULL) {
5072 if (argc == 2 && ids[0] == NULL) {
5073 error = got_error_path(argv[0], GOT_ERR_NO_OBJ);
5075 } else if (argc == 2 && ids[1] == NULL) {
5076 error = got_error_path(argv[1], GOT_ERR_NO_OBJ);
5078 } else if (argc > 0) {
5079 error = got_error_fmt(GOT_ERR_NOT_WORKTREE,
5080 "%s", "specified paths cannot be resolved");
5083 error = got_error(GOT_ERR_NOT_WORKTREE);
5088 error = get_worktree_paths_from_argv(&paths, argc, argv,
5093 error = got_object_id_str(&id_str,
5094 got_worktree_get_base_commit_id(worktree));
5098 arg.worktree = worktree;
5099 arg.diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
5100 arg.diff_context = diff_context;
5101 arg.id_str = id_str;
5102 arg.header_shown = 0;
5103 arg.diff_staged = diff_staged;
5104 arg.ignore_whitespace = ignore_whitespace;
5105 arg.force_text_diff = force_text_diff;
5109 error = got_worktree_status(worktree, &paths, repo, 0,
5110 print_diff, &arg, check_cancelled, NULL);
5115 if (ncommit_args == 1) {
5116 struct got_commit_object *commit;
5117 error = got_object_open_as_commit(&commit, repo, ids[0]);
5121 labels[1] = labels[0];
5123 if (got_object_commit_get_nparents(commit) > 0) {
5124 const struct got_object_id_queue *pids;
5125 struct got_object_qid *pid;
5126 pids = got_object_commit_get_parent_ids(commit);
5127 pid = STAILQ_FIRST(pids);
5128 ids[0] = got_object_id_dup(&pid->id);
5129 if (ids[0] == NULL) {
5130 error = got_error_from_errno(
5131 "got_object_id_dup");
5132 got_object_commit_close(commit);
5135 error = got_object_id_str(&labels[0], ids[0]);
5137 got_object_commit_close(commit);
5142 labels[0] = strdup("/dev/null");
5143 if (labels[0] == NULL) {
5144 error = got_error_from_errno("strdup");
5145 got_object_commit_close(commit);
5150 got_object_commit_close(commit);
5153 if (ncommit_args == 0 && argc > 2) {
5154 error = got_error_msg(GOT_ERR_BAD_PATH,
5155 "path arguments cannot be used when diffing two objects");
5160 error = got_object_get_type(&type1, repo, ids[0]);
5165 error = got_object_get_type(&type2, repo, ids[1]);
5168 if (type1 != GOT_OBJ_TYPE_ANY && type1 != type2) {
5169 error = got_error(GOT_ERR_OBJ_TYPE);
5172 if (type1 == GOT_OBJ_TYPE_BLOB && argc > 2) {
5173 error = got_error_msg(GOT_ERR_OBJ_TYPE,
5174 "path arguments cannot be used when diffing blobs");
5178 for (i = 0; ncommit_args > 0 && i < argc; i++) {
5180 struct got_pathlist_entry *new;
5184 error = got_worktree_resolve_path(&p, worktree,
5188 prefix = got_worktree_get_path_prefix(worktree);
5189 while (prefix[0] == '/')
5191 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5192 (p[0] != '\0' && prefix[0] != '\0') ? "/" : "",
5194 error = got_error_from_errno("asprintf");
5200 char *mapped_path, *s;
5201 error = got_repo_map_path(&mapped_path, repo, argv[i]);
5207 in_repo_path = strdup(s);
5208 if (in_repo_path == NULL) {
5209 error = got_error_from_errno("asprintf");
5216 error = got_pathlist_insert(&new, &paths, in_repo_path, NULL);
5217 if (error || new == NULL /* duplicate */)
5224 /* Release work tree lock. */
5225 got_worktree_close(worktree);
5229 fd1 = got_opentempfd();
5231 error = got_error_from_errno("got_opentempfd");
5235 fd2 = got_opentempfd();
5237 error = got_error_from_errno("got_opentempfd");
5241 switch (type1 == GOT_OBJ_TYPE_ANY ? type2 : type1) {
5242 case GOT_OBJ_TYPE_BLOB:
5243 error = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5244 fd1, fd2, ids[0], ids[1], NULL, NULL,
5245 GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
5246 ignore_whitespace, force_text_diff, repo, stdout);
5248 case GOT_OBJ_TYPE_TREE:
5249 error = got_diff_objects_as_trees(NULL, NULL, f1, f2, fd1, fd2,
5250 ids[0], ids[1], &paths, "", "",
5251 GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
5252 ignore_whitespace, force_text_diff, repo, stdout);
5254 case GOT_OBJ_TYPE_COMMIT:
5255 printf("diff %s %s\n", labels[0], labels[1]);
5256 error = got_diff_objects_as_commits(NULL, NULL, f1, f2,
5257 fd1, fd2, ids[0], ids[1], &paths,
5258 GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
5259 ignore_whitespace, force_text_diff, repo, stdout);
5262 error = got_error(GOT_ERR_OBJ_TYPE);
5270 got_worktree_close(worktree);
5272 const struct got_error *close_err = got_repo_close(repo);
5277 const struct got_error *pack_err =
5278 got_repo_pack_fds_close(pack_fds);
5282 TAILQ_FOREACH(pe, &paths, entry)
5283 free((char *)pe->path);
5284 got_pathlist_free(&paths);
5285 got_ref_list_free(&refs);
5286 if (f1 && fclose(f1) == EOF && error == NULL)
5287 error = got_error_from_errno("fclose");
5288 if (f2 && fclose(f2) == EOF && error == NULL)
5289 error = got_error_from_errno("fclose");
5290 if (fd1 != -1 && close(fd1) == -1 && error == NULL)
5291 error = got_error_from_errno("close");
5292 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
5293 error = got_error_from_errno("close");
5301 "usage: %s blame [-c commit] [-r repository-path] path\n",
5310 char datebuf[11]; /* YYYY-MM-DD + NUL */
5313 struct blame_cb_args {
5314 struct blame_line *lines;
5318 off_t *line_offsets;
5320 struct got_repository *repo;
5323 static const struct got_error *
5324 blame_cb(void *arg, int nlines, int lineno,
5325 struct got_commit_object *commit, struct got_object_id *id)
5327 const struct got_error *err = NULL;
5328 struct blame_cb_args *a = arg;
5329 struct blame_line *bline;
5331 size_t linesize = 0;
5334 time_t committer_time;
5336 if (nlines != a->nlines ||
5337 (lineno != -1 && lineno < 1) || lineno > a->nlines)
5338 return got_error(GOT_ERR_RANGE);
5340 if (sigint_received)
5341 return got_error(GOT_ERR_ITER_COMPLETED);
5344 return NULL; /* no change in this commit */
5346 /* Annotate this line. */
5347 bline = &a->lines[lineno - 1];
5348 if (bline->annotated)
5350 err = got_object_id_str(&bline->id_str, id);
5354 bline->committer = strdup(got_object_commit_get_committer(commit));
5355 if (bline->committer == NULL) {
5356 err = got_error_from_errno("strdup");
5360 committer_time = got_object_commit_get_committer_time(commit);
5361 if (gmtime_r(&committer_time, &tm) == NULL)
5362 return got_error_from_errno("gmtime_r");
5363 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
5365 err = got_error(GOT_ERR_NO_SPACE);
5368 bline->annotated = 1;
5370 /* Print lines annotated so far. */
5371 bline = &a->lines[a->lineno_cur - 1];
5372 if (!bline->annotated)
5375 offset = a->line_offsets[a->lineno_cur - 1];
5376 if (fseeko(a->f, offset, SEEK_SET) == -1) {
5377 err = got_error_from_errno("fseeko");
5381 while (a->lineno_cur <= a->nlines && bline->annotated) {
5382 char *smallerthan, *at, *nl, *committer;
5385 if (getline(&line, &linesize, a->f) == -1) {
5387 err = got_error_from_errno("getline");
5391 committer = bline->committer;
5392 smallerthan = strchr(committer, '<');
5393 if (smallerthan && smallerthan[1] != '\0')
5394 committer = smallerthan + 1;
5395 at = strchr(committer, '@');
5398 len = strlen(committer);
5400 committer[8] = '\0';
5402 nl = strchr(line, '\n');
5405 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
5406 bline->id_str, bline->datebuf, committer, line);
5409 bline = &a->lines[a->lineno_cur - 1];
5416 static const struct got_error *
5417 cmd_blame(int argc, char *argv[])
5419 const struct got_error *error;
5420 struct got_repository *repo = NULL;
5421 struct got_worktree *worktree = NULL;
5422 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5423 char *link_target = NULL;
5424 struct got_object_id *obj_id = NULL;
5425 struct got_object_id *commit_id = NULL;
5426 struct got_commit_object *commit = NULL;
5427 struct got_blob_object *blob = NULL;
5428 char *commit_id_str = NULL;
5429 struct blame_cb_args bca;
5430 int ch, obj_type, i, fd1 = -1, fd2 = -1, fd3 = -1;
5432 int *pack_fds = NULL;
5433 FILE *f1 = NULL, *f2 = NULL;
5435 fd1 = got_opentempfd();
5437 return got_error_from_errno("got_opentempfd");
5439 memset(&bca, 0, sizeof(bca));
5442 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5447 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5450 commit_id_str = optarg;
5453 repo_path = realpath(optarg, NULL);
5454 if (repo_path == NULL)
5455 return got_error_from_errno2("realpath",
5457 got_path_strip_trailing_slashes(repo_path);
5473 cwd = getcwd(NULL, 0);
5475 error = got_error_from_errno("getcwd");
5479 error = got_repo_pack_fds_open(&pack_fds);
5483 if (repo_path == NULL) {
5484 error = got_worktree_open(&worktree, cwd);
5485 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5491 strdup(got_worktree_get_repo_path(worktree));
5492 if (repo_path == NULL) {
5493 error = got_error_from_errno("strdup");
5498 repo_path = strdup(cwd);
5499 if (repo_path == NULL) {
5500 error = got_error_from_errno("strdup");
5506 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5511 const char *prefix = got_worktree_get_path_prefix(worktree);
5514 error = got_worktree_resolve_path(&p, worktree, path);
5517 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5518 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
5520 error = got_error_from_errno("asprintf");
5525 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5527 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5530 error = got_repo_map_path(&in_repo_path, repo, path);
5535 if (commit_id_str == NULL) {
5536 struct got_reference *head_ref;
5537 error = got_ref_open(&head_ref, repo, worktree ?
5538 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5541 error = got_ref_resolve(&commit_id, repo, head_ref);
5542 got_ref_close(head_ref);
5546 struct got_reflist_head refs;
5548 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5552 error = got_repo_match_object_id(&commit_id, NULL,
5553 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5554 got_ref_list_free(&refs);
5560 /* Release work tree lock. */
5561 got_worktree_close(worktree);
5565 error = got_object_open_as_commit(&commit, repo, commit_id);
5569 error = got_object_resolve_symlinks(&link_target, in_repo_path,
5574 error = got_object_id_by_path(&obj_id, repo, commit,
5575 link_target ? link_target : in_repo_path);
5579 error = got_object_get_type(&obj_type, repo, obj_id);
5583 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5584 error = got_error_path(link_target ? link_target : in_repo_path,
5589 error = got_object_open_as_blob(&blob, repo, obj_id, 8192, fd1);
5592 bca.f = got_opentemp();
5593 if (bca.f == NULL) {
5594 error = got_error_from_errno("got_opentemp");
5597 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
5598 &bca.line_offsets, bca.f, blob);
5599 if (error || bca.nlines == 0)
5602 /* Don't include \n at EOF in the blame line count. */
5603 if (bca.line_offsets[bca.nlines - 1] == filesize)
5606 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
5607 if (bca.lines == NULL) {
5608 error = got_error_from_errno("calloc");
5612 bca.nlines_prec = 0;
5620 fd2 = got_opentempfd();
5622 error = got_error_from_errno("got_opentempfd");
5625 fd3 = got_opentempfd();
5627 error = got_error_from_errno("got_opentempfd");
5630 f1 = got_opentemp();
5632 error = got_error_from_errno("got_opentemp");
5635 f2 = got_opentemp();
5637 error = got_error_from_errno("got_opentemp");
5640 error = got_blame(link_target ? link_target : in_repo_path, commit_id,
5641 repo, GOT_DIFF_ALGORITHM_PATIENCE, blame_cb, &bca,
5642 check_cancelled, NULL, fd2, fd3, f1, f2);
5651 got_object_commit_close(commit);
5653 if (fd1 != -1 && close(fd1) == -1 && error == NULL)
5654 error = got_error_from_errno("close");
5655 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
5656 error = got_error_from_errno("close");
5657 if (fd3 != -1 && close(fd3) == -1 && error == NULL)
5658 error = got_error_from_errno("close");
5659 if (f1 && fclose(f1) == EOF && error == NULL)
5660 error = got_error_from_errno("fclose");
5661 if (f2 && fclose(f2) == EOF && error == NULL)
5662 error = got_error_from_errno("fclose");
5665 got_object_blob_close(blob);
5667 got_worktree_close(worktree);
5669 const struct got_error *close_err = got_repo_close(repo);
5674 const struct got_error *pack_err =
5675 got_repo_pack_fds_close(pack_fds);
5680 for (i = 0; i < bca.nlines; i++) {
5681 struct blame_line *bline = &bca.lines[i];
5682 free(bline->id_str);
5683 free(bline->committer);
5687 free(bca.line_offsets);
5688 if (bca.f && fclose(bca.f) == EOF && error == NULL)
5689 error = got_error_from_errno("fclose");
5696 fprintf(stderr, "usage: %s tree [-iR] [-c commit] [-r repository-path] "
5697 "[path]\n", getprogname());
5701 static const struct got_error *
5702 print_entry(struct got_tree_entry *te, const char *id, const char *path,
5703 const char *root_path, struct got_repository *repo)
5705 const struct got_error *err = NULL;
5706 int is_root_path = (strcmp(path, root_path) == 0);
5707 const char *modestr = "";
5708 mode_t mode = got_tree_entry_get_mode(te);
5709 char *link_target = NULL;
5711 path += strlen(root_path);
5712 while (path[0] == '/')
5715 if (got_object_tree_entry_is_submodule(te))
5717 else if (S_ISLNK(mode)) {
5720 err = got_tree_entry_get_symlink_target(&link_target, te, repo);
5723 for (i = 0; i < strlen(link_target); i++) {
5724 if (!isprint((unsigned char)link_target[i]))
5725 link_target[i] = '?';
5730 else if (S_ISDIR(mode))
5732 else if (mode & S_IXUSR)
5735 printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
5736 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
5737 link_target ? " -> ": "", link_target ? link_target : "");
5743 static const struct got_error *
5744 print_tree(const char *path, struct got_commit_object *commit,
5745 int show_ids, int recurse, const char *root_path,
5746 struct got_repository *repo)
5748 const struct got_error *err = NULL;
5749 struct got_object_id *tree_id = NULL;
5750 struct got_tree_object *tree = NULL;
5753 err = got_object_id_by_path(&tree_id, repo, commit, path);
5757 err = got_object_open_as_tree(&tree, repo, tree_id);
5760 nentries = got_object_tree_get_nentries(tree);
5761 for (i = 0; i < nentries; i++) {
5762 struct got_tree_entry *te;
5765 if (sigint_received || sigpipe_received)
5768 te = got_object_tree_get_entry(tree, i);
5771 err = got_object_id_str(&id_str,
5772 got_tree_entry_get_id(te));
5775 if (asprintf(&id, "%s ", id_str) == -1) {
5776 err = got_error_from_errno("asprintf");
5782 err = print_entry(te, id, path, root_path, repo);
5787 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
5789 if (asprintf(&child_path, "%s%s%s", path,
5790 path[0] == '/' && path[1] == '\0' ? "" : "/",
5791 got_tree_entry_get_name(te)) == -1) {
5792 err = got_error_from_errno("asprintf");
5795 err = print_tree(child_path, commit, show_ids, 1,
5804 got_object_tree_close(tree);
5809 static const struct got_error *
5810 cmd_tree(int argc, char *argv[])
5812 const struct got_error *error;
5813 struct got_repository *repo = NULL;
5814 struct got_worktree *worktree = NULL;
5815 const char *path, *refname = NULL;
5816 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5817 struct got_object_id *commit_id = NULL;
5818 struct got_commit_object *commit = NULL;
5819 char *commit_id_str = NULL;
5820 int show_ids = 0, recurse = 0;
5822 int *pack_fds = NULL;
5825 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5830 while ((ch = getopt(argc, argv, "c:iRr:")) != -1) {
5833 commit_id_str = optarg;
5842 repo_path = realpath(optarg, NULL);
5843 if (repo_path == NULL)
5844 return got_error_from_errno2("realpath",
5846 got_path_strip_trailing_slashes(repo_path);
5864 cwd = getcwd(NULL, 0);
5866 error = got_error_from_errno("getcwd");
5870 error = got_repo_pack_fds_open(&pack_fds);
5874 if (repo_path == NULL) {
5875 error = got_worktree_open(&worktree, cwd);
5876 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5882 strdup(got_worktree_get_repo_path(worktree));
5883 if (repo_path == NULL)
5884 error = got_error_from_errno("strdup");
5888 repo_path = strdup(cwd);
5889 if (repo_path == NULL) {
5890 error = got_error_from_errno("strdup");
5896 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5901 const char *prefix = got_worktree_get_path_prefix(worktree);
5906 error = got_worktree_resolve_path(&p, worktree, path);
5909 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5910 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
5912 error = got_error_from_errno("asprintf");
5917 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5921 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5926 error = got_repo_map_path(&in_repo_path, repo, path);
5931 if (commit_id_str == NULL) {
5932 struct got_reference *head_ref;
5934 refname = got_worktree_get_head_ref_name(worktree);
5936 refname = GOT_REF_HEAD;
5937 error = got_ref_open(&head_ref, repo, refname, 0);
5940 error = got_ref_resolve(&commit_id, repo, head_ref);
5941 got_ref_close(head_ref);
5945 struct got_reflist_head refs;
5947 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5951 error = got_repo_match_object_id(&commit_id, NULL,
5952 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5953 got_ref_list_free(&refs);
5959 /* Release work tree lock. */
5960 got_worktree_close(worktree);
5964 error = got_object_open_as_commit(&commit, repo, commit_id);
5968 error = print_tree(in_repo_path, commit, show_ids, recurse,
5969 in_repo_path, repo);
5976 got_object_commit_close(commit);
5978 got_worktree_close(worktree);
5980 const struct got_error *close_err = got_repo_close(repo);
5985 const struct got_error *pack_err =
5986 got_repo_pack_fds_close(pack_fds);
5996 fprintf(stderr, "usage: %s status [-I] [-S status-codes] "
5997 "[-s status-codes] [path ...]\n", getprogname());
6001 struct got_status_arg {
6006 static const struct got_error *
6007 print_status(void *arg, unsigned char status, unsigned char staged_status,
6008 const char *path, struct got_object_id *blob_id,
6009 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6010 int dirfd, const char *de_name)
6012 struct got_status_arg *st = arg;
6014 if (status == staged_status && (status == GOT_STATUS_DELETE))
6015 status = GOT_STATUS_NO_CHANGE;
6016 if (st != NULL && st->status_codes) {
6017 size_t ncodes = strlen(st->status_codes);
6020 for (i = 0; i < ncodes ; i++) {
6022 if (status == st->status_codes[i] ||
6023 staged_status == st->status_codes[i]) {
6028 if (status == st->status_codes[i] ||
6029 staged_status == st->status_codes[i])
6034 if (st->suppress && j == 0)
6041 printf("%c%c %s\n", status, staged_status, path);
6045 static const struct got_error *
6046 cmd_status(int argc, char *argv[])
6048 const struct got_error *error = NULL;
6049 struct got_repository *repo = NULL;
6050 struct got_worktree *worktree = NULL;
6051 struct got_status_arg st;
6053 struct got_pathlist_head paths;
6054 struct got_pathlist_entry *pe;
6055 int ch, i, no_ignores = 0;
6056 int *pack_fds = NULL;
6060 memset(&st, 0, sizeof(st));
6061 st.status_codes = NULL;
6064 while ((ch = getopt(argc, argv, "IS:s:")) != -1) {
6070 if (st.status_codes != NULL && st.suppress == 0)
6071 option_conflict('S', 's');
6075 for (i = 0; i < strlen(optarg); i++) {
6076 switch (optarg[i]) {
6077 case GOT_STATUS_MODIFY:
6078 case GOT_STATUS_ADD:
6079 case GOT_STATUS_DELETE:
6080 case GOT_STATUS_CONFLICT:
6081 case GOT_STATUS_MISSING:
6082 case GOT_STATUS_OBSTRUCTED:
6083 case GOT_STATUS_UNVERSIONED:
6084 case GOT_STATUS_MODE_CHANGE:
6085 case GOT_STATUS_NONEXISTENT:
6088 errx(1, "invalid status code '%c'",
6092 if (ch == 's' && st.suppress)
6093 option_conflict('s', 'S');
6094 st.status_codes = optarg;
6106 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6110 cwd = getcwd(NULL, 0);
6112 error = got_error_from_errno("getcwd");
6116 error = got_repo_pack_fds_open(&pack_fds);
6120 error = got_worktree_open(&worktree, cwd);
6122 if (error->code == GOT_ERR_NOT_WORKTREE)
6123 error = wrap_not_worktree_error(error, "status", cwd);
6127 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6132 error = apply_unveil(got_repo_get_path(repo), 1,
6133 got_worktree_get_root_path(worktree));
6137 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6141 error = got_worktree_status(worktree, &paths, repo, no_ignores,
6142 print_status, &st, check_cancelled, NULL);
6145 const struct got_error *pack_err =
6146 got_repo_pack_fds_close(pack_fds);
6151 TAILQ_FOREACH(pe, &paths, entry)
6152 free((char *)pe->path);
6153 got_pathlist_free(&paths);
6161 fprintf(stderr, "usage: %s ref [-dlt] [-c object] [-r repository-path] "
6162 "[-s reference] [name]\n", getprogname());
6166 static const struct got_error *
6167 list_refs(struct got_repository *repo, const char *refname, int sort_by_time)
6169 static const struct got_error *err = NULL;
6170 struct got_reflist_head refs;
6171 struct got_reflist_entry *re;
6174 err = got_ref_list(&refs, repo, refname, sort_by_time ?
6175 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6180 TAILQ_FOREACH(re, &refs, entry) {
6182 refstr = got_ref_to_str(re->ref);
6183 if (refstr == NULL) {
6184 err = got_error_from_errno("got_ref_to_str");
6187 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
6191 got_ref_list_free(&refs);
6195 static const struct got_error *
6196 delete_ref_by_name(struct got_repository *repo, const char *refname)
6198 const struct got_error *err;
6199 struct got_reference *ref;
6201 err = got_ref_open(&ref, repo, refname, 0);
6205 err = delete_ref(repo, ref);
6210 static const struct got_error *
6211 add_ref(struct got_repository *repo, const char *refname, const char *target)
6213 const struct got_error *err = NULL;
6214 struct got_object_id *id = NULL;
6215 struct got_reference *ref = NULL;
6216 struct got_reflist_head refs;
6219 * Don't let the user create a reference name with a leading '-'.
6220 * While technically a valid reference name, this case is usually
6221 * an unintended typo.
6223 if (refname[0] == '-')
6224 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
6227 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
6230 err = got_repo_match_object_id(&id, NULL, target, GOT_OBJ_TYPE_ANY,
6232 got_ref_list_free(&refs);
6236 err = got_ref_alloc(&ref, refname, id);
6240 err = got_ref_write(ref, repo);
6248 static const struct got_error *
6249 add_symref(struct got_repository *repo, const char *refname, const char *target)
6251 const struct got_error *err = NULL;
6252 struct got_reference *ref = NULL;
6253 struct got_reference *target_ref = NULL;
6256 * Don't let the user create a reference name with a leading '-'.
6257 * While technically a valid reference name, this case is usually
6258 * an unintended typo.
6260 if (refname[0] == '-')
6261 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
6263 err = got_ref_open(&target_ref, repo, target, 0);
6267 err = got_ref_alloc_symref(&ref, refname, target_ref);
6271 err = got_ref_write(ref, repo);
6274 got_ref_close(target_ref);
6280 static const struct got_error *
6281 cmd_ref(int argc, char *argv[])
6283 const struct got_error *error = NULL;
6284 struct got_repository *repo = NULL;
6285 struct got_worktree *worktree = NULL;
6286 char *cwd = NULL, *repo_path = NULL;
6287 int ch, do_list = 0, do_delete = 0, sort_by_time = 0;
6288 const char *obj_arg = NULL, *symref_target= NULL;
6289 char *refname = NULL;
6290 int *pack_fds = NULL;
6292 while ((ch = getopt(argc, argv, "c:dlr:s:t")) != -1) {
6304 repo_path = realpath(optarg, NULL);
6305 if (repo_path == NULL)
6306 return got_error_from_errno2("realpath",
6308 got_path_strip_trailing_slashes(repo_path);
6311 symref_target = optarg;
6322 if (obj_arg && do_list)
6323 option_conflict('c', 'l');
6324 if (obj_arg && do_delete)
6325 option_conflict('c', 'd');
6326 if (obj_arg && symref_target)
6327 option_conflict('c', 's');
6328 if (symref_target && do_delete)
6329 option_conflict('s', 'd');
6330 if (symref_target && do_list)
6331 option_conflict('s', 'l');
6332 if (do_delete && do_list)
6333 option_conflict('d', 'l');
6334 if (sort_by_time && !do_list)
6335 errx(1, "-t option requires -l option");
6341 if (argc != 0 && argc != 1)
6344 refname = strdup(argv[0]);
6345 if (refname == NULL) {
6346 error = got_error_from_errno("strdup");
6353 refname = strdup(argv[0]);
6354 if (refname == NULL) {
6355 error = got_error_from_errno("strdup");
6361 got_path_strip_trailing_slashes(refname);
6364 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6365 "sendfd unveil", NULL) == -1)
6368 cwd = getcwd(NULL, 0);
6370 error = got_error_from_errno("getcwd");
6374 error = got_repo_pack_fds_open(&pack_fds);
6378 if (repo_path == NULL) {
6379 error = got_worktree_open(&worktree, cwd);
6380 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6386 strdup(got_worktree_get_repo_path(worktree));
6387 if (repo_path == NULL)
6388 error = got_error_from_errno("strdup");
6392 repo_path = strdup(cwd);
6393 if (repo_path == NULL) {
6394 error = got_error_from_errno("strdup");
6400 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6406 /* Remove "cpath" promise. */
6407 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6413 error = apply_unveil(got_repo_get_path(repo), do_list,
6414 worktree ? got_worktree_get_root_path(worktree) : NULL);
6419 error = list_refs(repo, refname, sort_by_time);
6421 error = delete_ref_by_name(repo, refname);
6422 else if (symref_target)
6423 error = add_symref(repo, refname, symref_target);
6425 if (obj_arg == NULL)
6427 error = add_ref(repo, refname, obj_arg);
6432 const struct got_error *close_err = got_repo_close(repo);
6437 got_worktree_close(worktree);
6439 const struct got_error *pack_err =
6440 got_repo_pack_fds_close(pack_fds);
6452 fprintf(stderr, "usage: %s branch [-lnt] [-c commit] [-d name] "
6453 "[-r repository-path] [name]\n", getprogname());
6457 static const struct got_error *
6458 list_branch(struct got_repository *repo, struct got_worktree *worktree,
6459 struct got_reference *ref)
6461 const struct got_error *err = NULL;
6462 const char *refname, *marker = " ";
6465 refname = got_ref_get_name(ref);
6466 if (worktree && strcmp(refname,
6467 got_worktree_get_head_ref_name(worktree)) == 0) {
6468 struct got_object_id *id = NULL;
6470 err = got_ref_resolve(&id, repo, ref);
6473 if (got_object_id_cmp(id,
6474 got_worktree_get_base_commit_id(worktree)) == 0)
6481 if (strncmp(refname, "refs/heads/", 11) == 0)
6483 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
6485 if (strncmp(refname, "refs/remotes/", 13) == 0)
6488 refstr = got_ref_to_str(ref);
6490 return got_error_from_errno("got_ref_to_str");
6492 printf("%s%s: %s\n", marker, refname, refstr);
6497 static const struct got_error *
6498 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
6500 const char *refname;
6502 if (worktree == NULL)
6503 return got_error(GOT_ERR_NOT_WORKTREE);
6505 refname = got_worktree_get_head_ref_name(worktree);
6507 if (strncmp(refname, "refs/heads/", 11) == 0)
6509 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
6512 printf("%s\n", refname);
6517 static const struct got_error *
6518 list_branches(struct got_repository *repo, struct got_worktree *worktree,
6521 static const struct got_error *err = NULL;
6522 struct got_reflist_head refs;
6523 struct got_reflist_entry *re;
6524 struct got_reference *temp_ref = NULL;
6525 int rebase_in_progress, histedit_in_progress;
6530 err = got_worktree_rebase_in_progress(&rebase_in_progress,
6535 err = got_worktree_histedit_in_progress(&histedit_in_progress,
6540 if (rebase_in_progress || histedit_in_progress) {
6541 err = got_ref_open(&temp_ref, repo,
6542 got_worktree_get_head_ref_name(worktree), 0);
6545 list_branch(repo, worktree, temp_ref);
6546 got_ref_close(temp_ref);
6550 err = got_ref_list(&refs, repo, "refs/heads", sort_by_time ?
6551 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6556 TAILQ_FOREACH(re, &refs, entry)
6557 list_branch(repo, worktree, re->ref);
6559 got_ref_list_free(&refs);
6561 err = got_ref_list(&refs, repo, "refs/remotes", sort_by_time ?
6562 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6567 TAILQ_FOREACH(re, &refs, entry)
6568 list_branch(repo, worktree, re->ref);
6570 got_ref_list_free(&refs);
6575 static const struct got_error *
6576 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
6577 const char *branch_name)
6579 const struct got_error *err = NULL;
6580 struct got_reference *ref = NULL;
6581 char *refname, *remote_refname = NULL;
6583 if (strncmp(branch_name, "refs/", 5) == 0)
6585 if (strncmp(branch_name, "heads/", 6) == 0)
6587 else if (strncmp(branch_name, "remotes/", 8) == 0)
6590 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
6591 return got_error_from_errno("asprintf");
6593 if (asprintf(&remote_refname, "refs/remotes/%s",
6594 branch_name) == -1) {
6595 err = got_error_from_errno("asprintf");
6599 err = got_ref_open(&ref, repo, refname, 0);
6601 const struct got_error *err2;
6602 if (err->code != GOT_ERR_NOT_REF)
6605 * Keep 'err' intact such that if neither branch exists
6606 * we report "refs/heads" rather than "refs/remotes" in
6607 * our error message.
6609 err2 = got_ref_open(&ref, repo, remote_refname, 0);
6616 strcmp(got_worktree_get_head_ref_name(worktree),
6617 got_ref_get_name(ref)) == 0) {
6618 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6619 "will not delete this work tree's current branch");
6623 err = delete_ref(repo, ref);
6628 free(remote_refname);
6632 static const struct got_error *
6633 add_branch(struct got_repository *repo, const char *branch_name,
6634 struct got_object_id *base_commit_id)
6636 const struct got_error *err = NULL;
6637 struct got_reference *ref = NULL;
6638 char *base_refname = NULL, *refname = NULL;
6641 * Don't let the user create a branch name with a leading '-'.
6642 * While technically a valid reference name, this case is usually
6643 * an unintended typo.
6645 if (branch_name[0] == '-')
6646 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
6648 if (strncmp(branch_name, "refs/heads/", 11) == 0)
6651 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
6652 err = got_error_from_errno("asprintf");
6656 err = got_ref_open(&ref, repo, refname, 0);
6658 err = got_error(GOT_ERR_BRANCH_EXISTS);
6660 } else if (err->code != GOT_ERR_NOT_REF)
6663 err = got_ref_alloc(&ref, refname, base_commit_id);
6667 err = got_ref_write(ref, repo);
6676 static const struct got_error *
6677 cmd_branch(int argc, char *argv[])
6679 const struct got_error *error = NULL;
6680 struct got_repository *repo = NULL;
6681 struct got_worktree *worktree = NULL;
6682 char *cwd = NULL, *repo_path = NULL;
6683 int ch, do_list = 0, do_show = 0, do_update = 1, sort_by_time = 0;
6684 const char *delref = NULL, *commit_id_arg = NULL;
6685 struct got_reference *ref = NULL;
6686 struct got_pathlist_head paths;
6687 struct got_pathlist_entry *pe;
6688 struct got_object_id *commit_id = NULL;
6689 char *commit_id_str = NULL;
6690 int *pack_fds = NULL;
6694 while ((ch = getopt(argc, argv, "c:d:lnr:t")) != -1) {
6697 commit_id_arg = optarg;
6709 repo_path = realpath(optarg, NULL);
6710 if (repo_path == NULL)
6711 return got_error_from_errno2("realpath",
6713 got_path_strip_trailing_slashes(repo_path);
6724 if (do_list && delref)
6725 option_conflict('l', 'd');
6726 if (sort_by_time && !do_list)
6727 errx(1, "-t option requires -l option");
6732 if (!do_list && !delref && argc == 0)
6735 if ((do_list || delref || do_show) && commit_id_arg != NULL)
6736 errx(1, "-c option can only be used when creating a branch");
6738 if (do_list || delref) {
6741 } else if (!do_show && argc != 1)
6745 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6746 "sendfd unveil", NULL) == -1)
6749 cwd = getcwd(NULL, 0);
6751 error = got_error_from_errno("getcwd");
6755 error = got_repo_pack_fds_open(&pack_fds);
6759 if (repo_path == NULL) {
6760 error = got_worktree_open(&worktree, cwd);
6761 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6767 strdup(got_worktree_get_repo_path(worktree));
6768 if (repo_path == NULL)
6769 error = got_error_from_errno("strdup");
6773 repo_path = strdup(cwd);
6774 if (repo_path == NULL) {
6775 error = got_error_from_errno("strdup");
6781 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6786 if (do_list || do_show) {
6787 /* Remove "cpath" promise. */
6788 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6794 error = apply_unveil(got_repo_get_path(repo), do_list,
6795 worktree ? got_worktree_get_root_path(worktree) : NULL);
6800 error = show_current_branch(repo, worktree);
6802 error = list_branches(repo, worktree, sort_by_time);
6804 error = delete_branch(repo, worktree, delref);
6806 struct got_reflist_head refs;
6808 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
6812 if (commit_id_arg == NULL)
6813 commit_id_arg = worktree ?
6814 got_worktree_get_head_ref_name(worktree) :
6816 error = got_repo_match_object_id(&commit_id, NULL,
6817 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &refs, repo);
6818 got_ref_list_free(&refs);
6821 error = add_branch(repo, argv[0], commit_id);
6824 if (worktree && do_update) {
6825 struct got_update_progress_arg upa;
6826 char *branch_refname = NULL;
6828 error = got_object_id_str(&commit_id_str, commit_id);
6831 error = get_worktree_paths_from_argv(&paths, 0, NULL,
6835 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
6837 error = got_error_from_errno("asprintf");
6840 error = got_ref_open(&ref, repo, branch_refname, 0);
6841 free(branch_refname);
6844 error = switch_head_ref(ref, commit_id, worktree,
6848 error = got_worktree_set_base_commit_id(worktree, repo,
6852 memset(&upa, 0, sizeof(upa));
6853 error = got_worktree_checkout_files(worktree, &paths,
6854 repo, update_progress, &upa, check_cancelled,
6858 if (upa.did_something) {
6859 printf("Updated to %s: %s\n",
6860 got_worktree_get_head_ref_name(worktree),
6863 print_update_progress_stats(&upa);
6870 const struct got_error *close_err = got_repo_close(repo);
6875 got_worktree_close(worktree);
6877 const struct got_error *pack_err =
6878 got_repo_pack_fds_close(pack_fds);
6885 free(commit_id_str);
6886 TAILQ_FOREACH(pe, &paths, entry)
6887 free((char *)pe->path);
6888 got_pathlist_free(&paths);
6896 fprintf(stderr, "usage: %s tag [-lVv] [-c commit] [-m message] "
6897 "[-r repository-path] [-s signer-id] name\n", getprogname());
6902 static const struct got_error *
6903 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
6905 const struct got_error *err = NULL;
6906 struct got_reflist_entry *re, *se, *new;
6907 struct got_object_id *re_id, *se_id;
6908 struct got_tag_object *re_tag, *se_tag;
6909 time_t re_time, se_time;
6911 STAILQ_FOREACH(re, tags, entry) {
6912 se = STAILQ_FIRST(sorted);
6914 err = got_reflist_entry_dup(&new, re);
6917 STAILQ_INSERT_HEAD(sorted, new, entry);
6920 err = got_ref_resolve(&re_id, repo, re->ref);
6923 err = got_object_open_as_tag(&re_tag, repo, re_id);
6927 re_time = got_object_tag_get_tagger_time(re_tag);
6928 got_object_tag_close(re_tag);
6932 err = got_ref_resolve(&se_id, repo, re->ref);
6935 err = got_object_open_as_tag(&se_tag, repo, se_id);
6939 se_time = got_object_tag_get_tagger_time(se_tag);
6940 got_object_tag_close(se_tag);
6942 if (se_time > re_time) {
6943 err = got_reflist_entry_dup(&new, re);
6946 STAILQ_INSERT_AFTER(sorted, se, new, entry);
6949 se = STAILQ_NEXT(se, entry);
6958 static const struct got_error *
6959 get_tag_refname(char **refname, const char *tag_name)
6961 const struct got_error *err;
6963 if (strncmp("refs/tags/", tag_name, 10) == 0) {
6964 *refname = strdup(tag_name);
6965 if (*refname == NULL)
6966 return got_error_from_errno("strdup");
6967 } else if (asprintf(refname, "refs/tags/%s", tag_name) == -1) {
6968 err = got_error_from_errno("asprintf");
6976 static const struct got_error *
6977 list_tags(struct got_repository *repo, const char *tag_name, int verify_tags,
6978 const char *allowed_signers, const char *revoked_signers, int verbosity)
6980 static const struct got_error *err = NULL;
6981 struct got_reflist_head refs;
6982 struct got_reflist_entry *re;
6983 char *wanted_refname = NULL;
6988 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
6993 struct got_reference *ref;
6994 err = get_tag_refname(&wanted_refname, tag_name);
6997 /* Wanted tag reference should exist. */
6998 err = got_ref_open(&ref, repo, wanted_refname, 0);
7004 TAILQ_FOREACH(re, &refs, entry) {
7005 const char *refname;
7006 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
7008 const char *tagger, *ssh_sig = NULL;
7009 char *sig_msg = NULL;
7011 struct got_object_id *id;
7012 struct got_tag_object *tag;
7013 struct got_commit_object *commit = NULL;
7015 refname = got_ref_get_name(re->ref);
7016 if (strncmp(refname, "refs/tags/", 10) != 0 ||
7017 (wanted_refname && strcmp(refname, wanted_refname) != 0))
7020 refstr = got_ref_to_str(re->ref);
7021 if (refstr == NULL) {
7022 err = got_error_from_errno("got_ref_to_str");
7026 err = got_ref_resolve(&id, repo, re->ref);
7029 err = got_object_open_as_tag(&tag, repo, id);
7031 if (err->code != GOT_ERR_OBJ_TYPE) {
7035 /* "lightweight" tag */
7036 err = got_object_open_as_commit(&commit, repo, id);
7041 tagger = got_object_commit_get_committer(commit);
7043 got_object_commit_get_committer_time(commit);
7044 err = got_object_id_str(&id_str, id);
7050 tagger = got_object_tag_get_tagger(tag);
7051 tagger_time = got_object_tag_get_tagger_time(tag);
7052 err = got_object_id_str(&id_str,
7053 got_object_tag_get_object_id(tag));
7058 if (tag && verify_tags) {
7059 ssh_sig = got_sigs_get_tagmsg_ssh_signature(
7060 got_object_tag_get_message(tag));
7061 if (ssh_sig && allowed_signers == NULL) {
7062 err = got_error_msg(
7063 GOT_ERR_VERIFY_TAG_SIGNATURE,
7064 "SSH signature verification requires "
7065 "setting allowed_signers in "
7071 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
7073 printf("from: %s\n", tagger);
7074 datestr = get_datestr(&tagger_time, datebuf);
7076 printf("date: %s UTC\n", datestr);
7078 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
7080 switch (got_object_tag_get_object_type(tag)) {
7081 case GOT_OBJ_TYPE_BLOB:
7082 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
7085 case GOT_OBJ_TYPE_TREE:
7086 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
7089 case GOT_OBJ_TYPE_COMMIT:
7090 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
7093 case GOT_OBJ_TYPE_TAG:
7094 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
7104 err = got_sigs_verify_tag_ssh(&sig_msg, tag, ssh_sig,
7105 allowed_signers, revoked_signers, verbosity);
7106 if (err && err->code == GOT_ERR_BAD_TAG_SIGNATURE)
7110 printf("signature: %s", sig_msg);
7116 err = got_object_commit_get_logmsg(&tagmsg0, commit);
7119 got_object_commit_close(commit);
7121 tagmsg0 = strdup(got_object_tag_get_message(tag));
7122 got_object_tag_close(tag);
7123 if (tagmsg0 == NULL) {
7124 err = got_error_from_errno("strdup");
7131 line = strsep(&tagmsg, "\n");
7133 printf(" %s\n", line);
7138 got_ref_list_free(&refs);
7139 free(wanted_refname);
7141 if (err == NULL && bad_sigs)
7142 err = got_error(GOT_ERR_BAD_TAG_SIGNATURE);
7146 static const struct got_error *
7147 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
7148 const char *tag_name, const char *repo_path)
7150 const struct got_error *err = NULL;
7151 char *template = NULL, *initial_content = NULL;
7152 char *editor = NULL;
7153 int initial_content_len;
7156 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
7157 err = got_error_from_errno("asprintf");
7161 initial_content_len = asprintf(&initial_content,
7162 "\n# tagging commit %s as %s\n",
7163 commit_id_str, tag_name);
7164 if (initial_content_len == -1) {
7165 err = got_error_from_errno("asprintf");
7169 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
7173 if (write(fd, initial_content, initial_content_len) == -1) {
7174 err = got_error_from_errno2("write", *tagmsg_path);
7178 err = get_editor(&editor);
7181 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content,
7182 initial_content_len, 1);
7184 free(initial_content);
7188 if (fd != -1 && close(fd) == -1 && err == NULL)
7189 err = got_error_from_errno2("close", *tagmsg_path);
7198 static const struct got_error *
7199 add_tag(struct got_repository *repo, const char *tagger,
7200 const char *tag_name, const char *commit_arg, const char *tagmsg_arg,
7201 const char *signer_id, int verbosity)
7203 const struct got_error *err = NULL;
7204 struct got_object_id *commit_id = NULL, *tag_id = NULL;
7205 char *label = NULL, *commit_id_str = NULL;
7206 struct got_reference *ref = NULL;
7207 char *refname = NULL, *tagmsg = NULL;
7208 char *tagmsg_path = NULL, *tag_id_str = NULL;
7209 int preserve_tagmsg = 0;
7210 struct got_reflist_head refs;
7215 * Don't let the user create a tag name with a leading '-'.
7216 * While technically a valid reference name, this case is usually
7217 * an unintended typo.
7219 if (tag_name[0] == '-')
7220 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
7222 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
7226 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
7227 GOT_OBJ_TYPE_COMMIT, &refs, repo);
7231 err = got_object_id_str(&commit_id_str, commit_id);
7235 err = get_tag_refname(&refname, tag_name);
7238 if (strncmp("refs/tags/", tag_name, 10) == 0)
7241 err = got_ref_open(&ref, repo, refname, 0);
7243 err = got_error(GOT_ERR_TAG_EXISTS);
7245 } else if (err->code != GOT_ERR_NOT_REF)
7248 if (tagmsg_arg == NULL) {
7249 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
7250 tag_name, got_repo_get_path(repo));
7252 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
7253 tagmsg_path != NULL)
7254 preserve_tagmsg = 1;
7257 /* Editor is done; we can now apply unveil(2) */
7258 err = got_sigs_apply_unveil();
7261 err = apply_unveil(got_repo_get_path(repo), 0, NULL);
7266 err = got_object_tag_create(&tag_id, tag_name, commit_id,
7267 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, signer_id, repo,
7271 preserve_tagmsg = 1;
7275 err = got_ref_alloc(&ref, refname, tag_id);
7278 preserve_tagmsg = 1;
7282 err = got_ref_write(ref, repo);
7285 preserve_tagmsg = 1;
7289 err = got_object_id_str(&tag_id_str, tag_id);
7292 preserve_tagmsg = 1;
7295 printf("Created tag %s\n", tag_id_str);
7297 if (preserve_tagmsg) {
7298 fprintf(stderr, "%s: tag message preserved in %s\n",
7299 getprogname(), tagmsg_path);
7300 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
7301 err = got_error_from_errno2("unlink", tagmsg_path);
7306 free(commit_id_str);
7310 got_ref_list_free(&refs);
7314 static const struct got_error *
7315 cmd_tag(int argc, char *argv[])
7317 const struct got_error *error = NULL;
7318 struct got_repository *repo = NULL;
7319 struct got_worktree *worktree = NULL;
7320 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
7321 char *gitconfig_path = NULL, *tagger = NULL;
7322 char *allowed_signers = NULL, *revoked_signers = NULL;
7323 char *signer_id = NULL;
7324 const char *tag_name = NULL, *commit_id_arg = NULL, *tagmsg = NULL;
7325 int ch, do_list = 0, verify_tags = 0, verbosity = 0;
7326 int *pack_fds = NULL;
7328 while ((ch = getopt(argc, argv, "c:lm:r:s:Vv")) != -1) {
7331 commit_id_arg = optarg;
7340 repo_path = realpath(optarg, NULL);
7341 if (repo_path == NULL) {
7342 error = got_error_from_errno2("realpath",
7346 got_path_strip_trailing_slashes(repo_path);
7349 signer_id = strdup(optarg);
7350 if (signer_id == NULL) {
7351 error = got_error_from_errno("strdup");
7361 else if (verbosity < 3)
7373 if (do_list || verify_tags) {
7374 if (commit_id_arg != NULL)
7376 "-c option can only be used when creating a tag");
7379 option_conflict('l', 'm');
7381 option_conflict('V', 'm');
7385 option_conflict('l', 's');
7387 option_conflict('V', 's');
7391 } else if (argc != 1)
7398 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
7399 "sendfd unveil", NULL) == -1)
7402 cwd = getcwd(NULL, 0);
7404 error = got_error_from_errno("getcwd");
7408 error = got_repo_pack_fds_open(&pack_fds);
7412 if (repo_path == NULL) {
7413 error = got_worktree_open(&worktree, cwd);
7414 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7420 strdup(got_worktree_get_repo_path(worktree));
7421 if (repo_path == NULL)
7422 error = got_error_from_errno("strdup");
7426 repo_path = strdup(cwd);
7427 if (repo_path == NULL) {
7428 error = got_error_from_errno("strdup");
7434 if (do_list || verify_tags) {
7435 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7438 error = get_allowed_signers(&allowed_signers, repo, worktree);
7441 error = get_revoked_signers(&revoked_signers, repo, worktree);
7445 /* Release work tree lock. */
7446 got_worktree_close(worktree);
7451 * Remove "cpath" promise unless needed for signature tmpfile
7455 got_sigs_apply_unveil();
7458 if (pledge("stdio rpath wpath flock proc exec sendfd "
7459 "unveil", NULL) == -1)
7463 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7466 error = list_tags(repo, tag_name, verify_tags, allowed_signers,
7467 revoked_signers, verbosity);
7469 error = get_gitconfig_path(&gitconfig_path);
7472 error = got_repo_open(&repo, repo_path, gitconfig_path,
7477 error = get_author(&tagger, repo, worktree);
7480 if (signer_id == NULL) {
7481 error = get_signer_id(&signer_id, repo, worktree);
7488 error = got_sigs_apply_unveil();
7492 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
7497 if (commit_id_arg == NULL) {
7498 struct got_reference *head_ref;
7499 struct got_object_id *commit_id;
7500 error = got_ref_open(&head_ref, repo,
7501 worktree ? got_worktree_get_head_ref_name(worktree)
7505 error = got_ref_resolve(&commit_id, repo, head_ref);
7506 got_ref_close(head_ref);
7509 error = got_object_id_str(&commit_id_str, commit_id);
7516 /* Release work tree lock. */
7517 got_worktree_close(worktree);
7521 error = add_tag(repo, tagger, tag_name,
7522 commit_id_str ? commit_id_str : commit_id_arg, tagmsg,
7523 signer_id, verbosity);
7527 const struct got_error *close_err = got_repo_close(repo);
7532 got_worktree_close(worktree);
7534 const struct got_error *pack_err =
7535 got_repo_pack_fds_close(pack_fds);
7541 free(gitconfig_path);
7542 free(commit_id_str);
7544 free(allowed_signers);
7545 free(revoked_signers);
7553 fprintf(stderr, "usage: %s add [-IR] path ...\n", getprogname());
7557 static const struct got_error *
7558 add_progress(void *arg, unsigned char status, const char *path)
7560 while (path[0] == '/')
7562 printf("%c %s\n", status, path);
7566 static const struct got_error *
7567 cmd_add(int argc, char *argv[])
7569 const struct got_error *error = NULL;
7570 struct got_repository *repo = NULL;
7571 struct got_worktree *worktree = NULL;
7573 struct got_pathlist_head paths;
7574 struct got_pathlist_entry *pe;
7575 int ch, can_recurse = 0, no_ignores = 0;
7576 int *pack_fds = NULL;
7580 while ((ch = getopt(argc, argv, "IR")) != -1) {
7598 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7605 cwd = getcwd(NULL, 0);
7607 error = got_error_from_errno("getcwd");
7611 error = got_repo_pack_fds_open(&pack_fds);
7615 error = got_worktree_open(&worktree, cwd);
7617 if (error->code == GOT_ERR_NOT_WORKTREE)
7618 error = wrap_not_worktree_error(error, "add", cwd);
7622 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7627 error = apply_unveil(got_repo_get_path(repo), 1,
7628 got_worktree_get_root_path(worktree));
7632 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7639 TAILQ_FOREACH(pe, &paths, entry) {
7640 if (asprintf(&ondisk_path, "%s/%s",
7641 got_worktree_get_root_path(worktree),
7643 error = got_error_from_errno("asprintf");
7646 if (lstat(ondisk_path, &sb) == -1) {
7647 if (errno == ENOENT) {
7651 error = got_error_from_errno2("lstat",
7657 if (S_ISDIR(sb.st_mode)) {
7658 error = got_error_msg(GOT_ERR_BAD_PATH,
7659 "adding directories requires -R option");
7665 error = got_worktree_schedule_add(worktree, &paths, add_progress,
7666 NULL, repo, no_ignores);
7669 const struct got_error *close_err = got_repo_close(repo);
7674 got_worktree_close(worktree);
7676 const struct got_error *pack_err =
7677 got_repo_pack_fds_close(pack_fds);
7681 TAILQ_FOREACH(pe, &paths, entry)
7682 free((char *)pe->path);
7683 got_pathlist_free(&paths);
7691 fprintf(stderr, "usage: %s remove [-fkR] [-s status-codes] path ...\n",
7696 static const struct got_error *
7697 print_remove_status(void *arg, unsigned char status,
7698 unsigned char staged_status, const char *path)
7700 while (path[0] == '/')
7702 if (status == GOT_STATUS_NONEXISTENT)
7704 if (status == staged_status && (status == GOT_STATUS_DELETE))
7705 status = GOT_STATUS_NO_CHANGE;
7706 printf("%c%c %s\n", status, staged_status, path);
7710 static const struct got_error *
7711 cmd_remove(int argc, char *argv[])
7713 const struct got_error *error = NULL;
7714 struct got_worktree *worktree = NULL;
7715 struct got_repository *repo = NULL;
7716 const char *status_codes = NULL;
7718 struct got_pathlist_head paths;
7719 struct got_pathlist_entry *pe;
7720 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0, i;
7721 int ignore_missing_paths = 0;
7722 int *pack_fds = NULL;
7726 while ((ch = getopt(argc, argv, "fkRs:")) != -1) {
7729 delete_local_mods = 1;
7730 ignore_missing_paths = 1;
7739 for (i = 0; i < strlen(optarg); i++) {
7740 switch (optarg[i]) {
7741 case GOT_STATUS_MODIFY:
7742 delete_local_mods = 1;
7744 case GOT_STATUS_MISSING:
7745 ignore_missing_paths = 1;
7748 errx(1, "invalid status code '%c'",
7752 status_codes = optarg;
7764 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7771 cwd = getcwd(NULL, 0);
7773 error = got_error_from_errno("getcwd");
7777 error = got_repo_pack_fds_open(&pack_fds);
7781 error = got_worktree_open(&worktree, cwd);
7783 if (error->code == GOT_ERR_NOT_WORKTREE)
7784 error = wrap_not_worktree_error(error, "remove", cwd);
7788 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7793 error = apply_unveil(got_repo_get_path(repo), 1,
7794 got_worktree_get_root_path(worktree));
7798 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7805 TAILQ_FOREACH(pe, &paths, entry) {
7806 if (asprintf(&ondisk_path, "%s/%s",
7807 got_worktree_get_root_path(worktree),
7809 error = got_error_from_errno("asprintf");
7812 if (lstat(ondisk_path, &sb) == -1) {
7813 if (errno == ENOENT) {
7817 error = got_error_from_errno2("lstat",
7823 if (S_ISDIR(sb.st_mode)) {
7824 error = got_error_msg(GOT_ERR_BAD_PATH,
7825 "removing directories requires -R option");
7831 error = got_worktree_schedule_delete(worktree, &paths,
7832 delete_local_mods, status_codes, print_remove_status, NULL,
7833 repo, keep_on_disk, ignore_missing_paths);
7836 const struct got_error *close_err = got_repo_close(repo);
7841 got_worktree_close(worktree);
7843 const struct got_error *pack_err =
7844 got_repo_pack_fds_close(pack_fds);
7848 TAILQ_FOREACH(pe, &paths, entry)
7849 free((char *)pe->path);
7850 got_pathlist_free(&paths);
7858 fprintf(stderr, "usage: %s patch [-nR] [-c commit] [-p strip-count] "
7859 "[patchfile]\n", getprogname());
7863 static const struct got_error *
7864 patch_from_stdin(int *patchfd)
7866 const struct got_error *err = NULL;
7869 sig_t sighup, sigint, sigquit;
7871 *patchfd = got_opentempfd();
7873 return got_error_from_errno("got_opentempfd");
7875 sighup = signal(SIGHUP, SIG_DFL);
7876 sigint = signal(SIGINT, SIG_DFL);
7877 sigquit = signal(SIGQUIT, SIG_DFL);
7880 r = read(0, buf, sizeof(buf));
7882 err = got_error_from_errno("read");
7887 if (write(*patchfd, buf, r) == -1) {
7888 err = got_error_from_errno("write");
7893 signal(SIGHUP, sighup);
7894 signal(SIGINT, sigint);
7895 signal(SIGQUIT, sigquit);
7897 if (err == NULL && lseek(*patchfd, 0, SEEK_SET) == -1)
7898 err = got_error_from_errno("lseek");
7908 static const struct got_error *
7909 patch_progress(void *arg, const char *old, const char *new,
7910 unsigned char status, const struct got_error *error, int old_from,
7911 int old_lines, int new_from, int new_lines, int offset,
7912 int ws_mangled, const struct got_error *hunk_err)
7914 const char *path = new == NULL ? old : new;
7916 while (*path == '/')
7920 printf("%c %s\n", status, path);
7923 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
7925 if (offset != 0 || hunk_err != NULL || ws_mangled) {
7926 printf("@@ -%d,%d +%d,%d @@ ", old_from,
7927 old_lines, new_from, new_lines);
7928 if (hunk_err != NULL)
7929 printf("%s\n", hunk_err->msg);
7930 else if (offset != 0)
7931 printf("applied with offset %d\n", offset);
7933 printf("hunk contains mangled whitespace\n");
7939 static const struct got_error *
7940 cmd_patch(int argc, char *argv[])
7942 const struct got_error *error = NULL, *close_error = NULL;
7943 struct got_worktree *worktree = NULL;
7944 struct got_repository *repo = NULL;
7945 struct got_reflist_head refs;
7946 struct got_object_id *commit_id = NULL;
7947 const char *commit_id_str = NULL;
7951 int ch, nop = 0, strip = -1, reverse = 0;
7953 int *pack_fds = NULL;
7958 if (pledge("stdio rpath wpath cpath fattr proc exec sendfd flock "
7959 "unveil", NULL) == -1)
7963 while ((ch = getopt(argc, argv, "c:np:R")) != -1) {
7966 commit_id_str = optarg;
7972 strip = strtonum(optarg, 0, INT_MAX, &errstr);
7974 errx(1, "pathname strip count is %s: %s",
7990 error = patch_from_stdin(&patchfd);
7993 } else if (argc == 1) {
7994 patchfd = open(argv[0], O_RDONLY);
7995 if (patchfd == -1) {
7996 error = got_error_from_errno2("open", argv[0]);
7999 if (fstat(patchfd, &sb) == -1) {
8000 error = got_error_from_errno2("fstat", argv[0]);
8003 if (!S_ISREG(sb.st_mode)) {
8004 error = got_error_path(argv[0], GOT_ERR_BAD_FILETYPE);
8010 if ((cwd = getcwd(NULL, 0)) == NULL) {
8011 error = got_error_from_errno("getcwd");
8015 error = got_repo_pack_fds_open(&pack_fds);
8019 error = got_worktree_open(&worktree, cwd);
8023 const char *repo_path = got_worktree_get_repo_path(worktree);
8024 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8028 error = apply_unveil(got_repo_get_path(repo), 0,
8029 got_worktree_get_root_path(worktree));
8033 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
8037 if (commit_id_str != NULL) {
8038 error = got_repo_match_object_id(&commit_id, NULL,
8039 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
8044 error = got_patch(patchfd, worktree, repo, nop, strip, reverse,
8045 commit_id, &patch_progress, NULL, check_cancelled, NULL);
8048 got_ref_list_free(&refs);
8051 close_error = got_repo_close(repo);
8053 error = close_error;
8055 if (worktree != NULL) {
8056 close_error = got_worktree_close(worktree);
8058 error = close_error;
8061 const struct got_error *pack_err =
8062 got_repo_pack_fds_close(pack_fds);
8073 fprintf(stderr, "usage: %s revert [-pR] [-F response-script] path ...\n",
8078 static const struct got_error *
8079 revert_progress(void *arg, unsigned char status, const char *path)
8081 if (status == GOT_STATUS_UNVERSIONED)
8084 while (path[0] == '/')
8086 printf("%c %s\n", status, path);
8090 struct choose_patch_arg {
8091 FILE *patch_script_file;
8095 static const struct got_error *
8096 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
8097 int nchanges, const char *action)
8099 const struct got_error *err;
8101 size_t linesize = 0;
8105 case GOT_STATUS_ADD:
8106 printf("A %s\n%s this addition? [y/n] ", path, action);
8108 case GOT_STATUS_DELETE:
8109 printf("D %s\n%s this deletion? [y/n] ", path, action);
8111 case GOT_STATUS_MODIFY:
8112 if (fseek(patch_file, 0L, SEEK_SET) == -1)
8113 return got_error_from_errno("fseek");
8114 printf(GOT_COMMIT_SEP_STR);
8115 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
8117 if (linelen == -1 && ferror(patch_file)) {
8118 err = got_error_from_errno("getline");
8123 printf(GOT_COMMIT_SEP_STR);
8124 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
8125 path, n, nchanges, action);
8128 return got_error_path(path, GOT_ERR_FILE_STATUS);
8135 static const struct got_error *
8136 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
8137 FILE *patch_file, int n, int nchanges)
8139 const struct got_error *err = NULL;
8141 size_t linesize = 0;
8144 struct choose_patch_arg *a = arg;
8146 *choice = GOT_PATCH_CHOICE_NONE;
8148 if (a->patch_script_file) {
8150 err = show_change(status, path, patch_file, n, nchanges,
8154 linelen = getline(&line, &linesize, a->patch_script_file);
8155 if (linelen == -1) {
8156 if (ferror(a->patch_script_file))
8157 return got_error_from_errno("getline");
8160 nl = strchr(line, '\n');
8163 if (strcmp(line, "y") == 0) {
8164 *choice = GOT_PATCH_CHOICE_YES;
8166 } else if (strcmp(line, "n") == 0) {
8167 *choice = GOT_PATCH_CHOICE_NO;
8169 } else if (strcmp(line, "q") == 0 &&
8170 status == GOT_STATUS_MODIFY) {
8171 *choice = GOT_PATCH_CHOICE_QUIT;
8174 printf("invalid response '%s'\n", line);
8179 while (resp != 'y' && resp != 'n' && resp != 'q') {
8180 err = show_change(status, path, patch_file, n, nchanges,
8187 if (status == GOT_STATUS_MODIFY) {
8188 if (resp != 'y' && resp != 'n' && resp != 'q') {
8189 printf("invalid response '%c'\n", resp);
8192 } else if (resp != 'y' && resp != 'n') {
8193 printf("invalid response '%c'\n", resp);
8199 *choice = GOT_PATCH_CHOICE_YES;
8200 else if (resp == 'n')
8201 *choice = GOT_PATCH_CHOICE_NO;
8202 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
8203 *choice = GOT_PATCH_CHOICE_QUIT;
8208 static const struct got_error *
8209 cmd_revert(int argc, char *argv[])
8211 const struct got_error *error = NULL;
8212 struct got_worktree *worktree = NULL;
8213 struct got_repository *repo = NULL;
8214 char *cwd = NULL, *path = NULL;
8215 struct got_pathlist_head paths;
8216 struct got_pathlist_entry *pe;
8217 int ch, can_recurse = 0, pflag = 0;
8218 FILE *patch_script_file = NULL;
8219 const char *patch_script_path = NULL;
8220 struct choose_patch_arg cpa;
8221 int *pack_fds = NULL;
8225 while ((ch = getopt(argc, argv, "F:pR")) != -1) {
8228 patch_script_path = optarg;
8246 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8247 "unveil", NULL) == -1)
8252 if (patch_script_path && !pflag)
8253 errx(1, "-F option can only be used together with -p option");
8255 cwd = getcwd(NULL, 0);
8257 error = got_error_from_errno("getcwd");
8261 error = got_repo_pack_fds_open(&pack_fds);
8265 error = got_worktree_open(&worktree, cwd);
8267 if (error->code == GOT_ERR_NOT_WORKTREE)
8268 error = wrap_not_worktree_error(error, "revert", cwd);
8272 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8277 if (patch_script_path) {
8278 patch_script_file = fopen(patch_script_path, "re");
8279 if (patch_script_file == NULL) {
8280 error = got_error_from_errno2("fopen",
8285 error = apply_unveil(got_repo_get_path(repo), 1,
8286 got_worktree_get_root_path(worktree));
8290 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8297 TAILQ_FOREACH(pe, &paths, entry) {
8298 if (asprintf(&ondisk_path, "%s/%s",
8299 got_worktree_get_root_path(worktree),
8301 error = got_error_from_errno("asprintf");
8304 if (lstat(ondisk_path, &sb) == -1) {
8305 if (errno == ENOENT) {
8309 error = got_error_from_errno2("lstat",
8315 if (S_ISDIR(sb.st_mode)) {
8316 error = got_error_msg(GOT_ERR_BAD_PATH,
8317 "reverting directories requires -R option");
8323 cpa.patch_script_file = patch_script_file;
8324 cpa.action = "revert";
8325 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
8326 pflag ? choose_patch : NULL, &cpa, repo);
8328 if (patch_script_file && fclose(patch_script_file) == EOF &&
8330 error = got_error_from_errno2("fclose", patch_script_path);
8332 const struct got_error *close_err = got_repo_close(repo);
8337 got_worktree_close(worktree);
8339 const struct got_error *pack_err =
8340 got_repo_pack_fds_close(pack_fds);
8352 fprintf(stderr, "usage: %s commit [-NS] [-A author] [-F path] "
8353 "[-m message] [path ...]\n", getprogname());
8357 struct collect_commit_logmsg_arg {
8358 const char *cmdline_log;
8359 const char *prepared_log;
8360 int non_interactive;
8362 const char *worktree_path;
8363 const char *branch_name;
8364 const char *repo_path;
8369 static const struct got_error *
8370 read_prepared_logmsg(char **logmsg, const char *path)
8372 const struct got_error *err = NULL;
8378 memset(&sb, 0, sizeof(sb));
8380 f = fopen(path, "re");
8382 return got_error_from_errno2("fopen", path);
8384 if (fstat(fileno(f), &sb) == -1) {
8385 err = got_error_from_errno2("fstat", path);
8388 if (sb.st_size == 0) {
8389 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
8393 *logmsg = malloc(sb.st_size + 1);
8394 if (*logmsg == NULL) {
8395 err = got_error_from_errno("malloc");
8399 r = fread(*logmsg, 1, sb.st_size, f);
8400 if (r != sb.st_size) {
8402 err = got_error_from_errno2("fread", path);
8404 err = got_error(GOT_ERR_IO);
8407 (*logmsg)[sb.st_size] = '\0';
8409 if (fclose(f) == EOF && err == NULL)
8410 err = got_error_from_errno2("fclose", path);
8418 static const struct got_error *
8419 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
8422 char *initial_content = NULL;
8423 struct got_pathlist_entry *pe;
8424 const struct got_error *err = NULL;
8425 char *template = NULL;
8426 struct collect_commit_logmsg_arg *a = arg;
8427 int initial_content_len;
8431 /* if a message was specified on the command line, just use it */
8432 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
8433 len = strlen(a->cmdline_log) + 1;
8434 *logmsg = malloc(len + 1);
8435 if (*logmsg == NULL)
8436 return got_error_from_errno("malloc");
8437 strlcpy(*logmsg, a->cmdline_log, len);
8439 } else if (a->prepared_log != NULL && a->non_interactive)
8440 return read_prepared_logmsg(logmsg, a->prepared_log);
8442 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
8443 return got_error_from_errno("asprintf");
8445 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
8449 if (a->prepared_log) {
8451 err = read_prepared_logmsg(&msg, a->prepared_log);
8454 if (write(fd, msg, strlen(msg)) == -1) {
8455 err = got_error_from_errno2("write", a->logmsg_path);
8462 initial_content_len = asprintf(&initial_content,
8463 "\n# changes to be committed on branch %s:\n",
8465 if (initial_content_len == -1) {
8466 err = got_error_from_errno("asprintf");
8470 if (write(fd, initial_content, initial_content_len) == -1) {
8471 err = got_error_from_errno2("write", a->logmsg_path);
8475 TAILQ_FOREACH(pe, commitable_paths, entry) {
8476 struct got_commitable *ct = pe->data;
8477 dprintf(fd, "# %c %s\n",
8478 got_commitable_get_status(ct),
8479 got_commitable_get_path(ct));
8482 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content,
8483 initial_content_len, a->prepared_log ? 0 : 1);
8485 free(initial_content);
8488 if (fd != -1 && close(fd) == -1 && err == NULL)
8489 err = got_error_from_errno2("close", a->logmsg_path);
8491 /* Editor is done; we can now apply unveil(2) */
8493 err = apply_unveil(a->repo_path, 0, a->worktree_path);
8501 static const struct got_error *
8502 cmd_commit(int argc, char *argv[])
8504 const struct got_error *error = NULL;
8505 struct got_worktree *worktree = NULL;
8506 struct got_repository *repo = NULL;
8507 char *cwd = NULL, *id_str = NULL;
8508 struct got_object_id *id = NULL;
8509 const char *logmsg = NULL;
8510 char *prepared_logmsg = NULL;
8511 struct collect_commit_logmsg_arg cl_arg;
8512 const char *author = NULL;
8513 char *gitconfig_path = NULL, *editor = NULL, *committer = NULL;
8514 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
8515 int allow_bad_symlinks = 0, non_interactive = 0, merge_in_progress = 0;
8516 struct got_pathlist_head paths;
8517 int *pack_fds = NULL;
8520 cl_arg.logmsg_path = NULL;
8522 while ((ch = getopt(argc, argv, "A:F:m:NS")) != -1) {
8526 error = valid_author(author);
8532 option_conflict('F', 'm');
8533 prepared_logmsg = realpath(optarg, NULL);
8534 if (prepared_logmsg == NULL)
8535 return got_error_from_errno2("realpath",
8539 if (prepared_logmsg)
8540 option_conflict('m', 'F');
8544 non_interactive = 1;
8547 allow_bad_symlinks = 1;
8559 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8560 "unveil", NULL) == -1)
8563 cwd = getcwd(NULL, 0);
8565 error = got_error_from_errno("getcwd");
8569 error = got_repo_pack_fds_open(&pack_fds);
8573 error = got_worktree_open(&worktree, cwd);
8575 if (error->code == GOT_ERR_NOT_WORKTREE)
8576 error = wrap_not_worktree_error(error, "commit", cwd);
8580 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
8583 if (rebase_in_progress) {
8584 error = got_error(GOT_ERR_REBASING);
8588 error = got_worktree_histedit_in_progress(&histedit_in_progress,
8593 error = get_gitconfig_path(&gitconfig_path);
8596 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8597 gitconfig_path, pack_fds);
8601 error = got_worktree_merge_in_progress(&merge_in_progress, worktree, repo);
8604 if (merge_in_progress) {
8605 error = got_error(GOT_ERR_MERGE_BUSY);
8609 error = get_author(&committer, repo, worktree);
8613 if (author != NULL && !strcmp(committer, author)) {
8614 error = got_error(GOT_ERR_COMMIT_REDUNDANT_AUTHOR);
8622 * unveil(2) traverses exec(2); if an editor is used we have
8623 * to apply unveil after the log message has been written.
8625 if (logmsg == NULL || strlen(logmsg) == 0)
8626 error = get_editor(&editor);
8628 error = apply_unveil(got_repo_get_path(repo), 0,
8629 got_worktree_get_root_path(worktree));
8633 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8637 cl_arg.editor = editor;
8638 cl_arg.cmdline_log = logmsg;
8639 cl_arg.prepared_log = prepared_logmsg;
8640 cl_arg.non_interactive = non_interactive;
8641 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
8642 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
8643 if (!histedit_in_progress) {
8644 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
8645 error = got_error(GOT_ERR_COMMIT_BRANCH);
8648 cl_arg.branch_name += 11;
8650 cl_arg.repo_path = got_repo_get_path(repo);
8651 error = got_worktree_commit(&id, worktree, &paths, author, committer,
8652 allow_bad_symlinks, collect_commit_logmsg, &cl_arg,
8653 print_status, NULL, repo);
8655 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
8656 cl_arg.logmsg_path != NULL)
8657 preserve_logmsg = 1;
8661 error = got_object_id_str(&id_str, id);
8664 printf("Created commit %s\n", id_str);
8666 if (preserve_logmsg) {
8667 fprintf(stderr, "%s: log message preserved in %s\n",
8668 getprogname(), cl_arg.logmsg_path);
8669 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
8671 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
8672 free(cl_arg.logmsg_path);
8674 const struct got_error *close_err = got_repo_close(repo);
8679 got_worktree_close(worktree);
8681 const struct got_error *pack_err =
8682 got_repo_pack_fds_close(pack_fds);
8688 free(gitconfig_path);
8691 free(prepared_logmsg);
8698 fprintf(stderr, "usage: %s send [-afqTv] [-b branch] [-d branch] "
8699 "[-r repository-path] [-t tag] [remote-repository]\n",
8705 print_load_info(int print_colored, int print_found, int print_trees,
8706 int ncolored, int nfound, int ntrees)
8708 if (print_colored) {
8709 printf("%d commit%s colored", ncolored,
8710 ncolored == 1 ? "" : "s");
8713 printf("%s%d object%s found",
8714 ncolored > 0 ? "; " : "",
8715 nfound, nfound == 1 ? "" : "s");
8718 printf("; %d tree%s scanned", ntrees,
8719 ntrees == 1 ? "" : "s");
8723 struct got_send_progress_arg {
8724 char last_scaled_packsize[FMT_SCALED_STRSIZE];
8731 int last_nobj_total;
8735 int printed_something;
8737 struct got_pathlist_head *delete_branches;
8740 static const struct got_error *
8741 send_progress(void *arg, int ncolored, int nfound, int ntrees,
8742 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
8743 int nobj_written, off_t bytes_sent, const char *refname, int success)
8745 struct got_send_progress_arg *a = arg;
8746 char scaled_packsize[FMT_SCALED_STRSIZE];
8747 char scaled_sent[FMT_SCALED_STRSIZE];
8748 int p_deltify = 0, p_written = 0, p_sent = 0;
8749 int print_colored = 0, print_found = 0, print_trees = 0;
8750 int print_searching = 0, print_total = 0;
8751 int print_deltify = 0, print_written = 0, print_sent = 0;
8753 if (a->verbosity < 0)
8757 const char *status = success ? "accepted" : "rejected";
8760 struct got_pathlist_entry *pe;
8761 TAILQ_FOREACH(pe, a->delete_branches, entry) {
8762 const char *branchname = pe->path;
8763 if (got_path_cmp(branchname, refname,
8764 strlen(branchname), strlen(refname)) == 0) {
8766 a->sent_something = 1;
8772 if (a->printed_something)
8774 printf("Server has %s %s", status, refname);
8775 a->printed_something = 1;
8779 if (a->last_ncolored != ncolored) {
8781 a->last_ncolored = ncolored;
8784 if (a->last_nfound != nfound) {
8787 a->last_nfound = nfound;
8790 if (a->last_ntrees != ntrees) {
8794 a->last_ntrees = ntrees;
8797 if ((print_colored || print_found || print_trees) &&
8800 print_load_info(print_colored, print_found, print_trees,
8801 ncolored, nfound, ntrees);
8802 a->printed_something = 1;
8805 } else if (!a->loading_done) {
8807 print_load_info(1, 1, 1, ncolored, nfound, ntrees);
8809 a->loading_done = 1;
8812 if (fmt_scaled(packfile_size, scaled_packsize) == -1)
8813 return got_error_from_errno("fmt_scaled");
8814 if (fmt_scaled(bytes_sent, scaled_sent) == -1)
8815 return got_error_from_errno("fmt_scaled");
8817 if (a->last_ncommits != ncommits) {
8818 print_searching = 1;
8819 a->last_ncommits = ncommits;
8822 if (a->last_nobj_total != nobj_total) {
8823 print_searching = 1;
8825 a->last_nobj_total = nobj_total;
8828 if (packfile_size > 0 && (a->last_scaled_packsize[0] == '\0' ||
8829 strcmp(scaled_packsize, a->last_scaled_packsize)) != 0) {
8830 if (strlcpy(a->last_scaled_packsize, scaled_packsize,
8831 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
8832 return got_error(GOT_ERR_NO_SPACE);
8835 if (nobj_deltify > 0 || nobj_written > 0) {
8836 if (nobj_deltify > 0) {
8837 p_deltify = (nobj_deltify * 100) / nobj_total;
8838 if (p_deltify != a->last_p_deltify) {
8839 a->last_p_deltify = p_deltify;
8840 print_searching = 1;
8845 if (nobj_written > 0) {
8846 p_written = (nobj_written * 100) / nobj_total;
8847 if (p_written != a->last_p_written) {
8848 a->last_p_written = p_written;
8849 print_searching = 1;
8857 if (bytes_sent > 0) {
8858 p_sent = (bytes_sent * 100) / packfile_size;
8859 if (p_sent != a->last_p_sent) {
8860 a->last_p_sent = p_sent;
8861 print_searching = 1;
8867 a->sent_something = 1;
8870 if (print_searching || print_total || print_deltify || print_written ||
8873 if (print_searching)
8874 printf("packing %d reference%s", ncommits,
8875 ncommits == 1 ? "" : "s");
8877 printf("; %d object%s", nobj_total,
8878 nobj_total == 1 ? "" : "s");
8880 printf("; deltify: %d%%", p_deltify);
8882 printf("; uploading pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
8883 scaled_packsize, p_sent);
8884 else if (print_written)
8885 printf("; writing pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
8886 scaled_packsize, p_written);
8887 if (print_searching || print_total || print_deltify ||
8888 print_written || print_sent) {
8889 a->printed_something = 1;
8895 static const struct got_error *
8896 cmd_send(int argc, char *argv[])
8898 const struct got_error *error = NULL;
8899 char *cwd = NULL, *repo_path = NULL;
8900 const char *remote_name;
8901 char *proto = NULL, *host = NULL, *port = NULL;
8902 char *repo_name = NULL, *server_path = NULL;
8903 const struct got_remote_repo *remotes, *remote = NULL;
8904 int nremotes, nbranches = 0, ndelete_branches = 0;
8905 struct got_repository *repo = NULL;
8906 struct got_worktree *worktree = NULL;
8907 const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
8908 struct got_pathlist_head branches;
8909 struct got_pathlist_head tags;
8910 struct got_reflist_head all_branches;
8911 struct got_reflist_head all_tags;
8912 struct got_pathlist_head delete_args;
8913 struct got_pathlist_head delete_branches;
8914 struct got_reflist_entry *re;
8915 struct got_pathlist_entry *pe;
8916 int i, ch, sendfd = -1, sendstatus;
8918 struct got_send_progress_arg spa;
8919 int verbosity = 0, overwrite_refs = 0;
8920 int send_all_branches = 0, send_all_tags = 0;
8921 struct got_reference *ref = NULL;
8922 int *pack_fds = NULL;
8924 TAILQ_INIT(&branches);
8926 TAILQ_INIT(&all_branches);
8927 TAILQ_INIT(&all_tags);
8928 TAILQ_INIT(&delete_args);
8929 TAILQ_INIT(&delete_branches);
8931 while ((ch = getopt(argc, argv, "ab:d:fqr:Tt:v")) != -1) {
8934 send_all_branches = 1;
8937 error = got_pathlist_append(&branches, optarg, NULL);
8943 error = got_pathlist_append(&delete_args, optarg, NULL);
8954 repo_path = realpath(optarg, NULL);
8955 if (repo_path == NULL)
8956 return got_error_from_errno2("realpath",
8958 got_path_strip_trailing_slashes(repo_path);
8964 error = got_pathlist_append(&tags, optarg, NULL);
8971 else if (verbosity < 3)
8982 if (send_all_branches && !TAILQ_EMPTY(&branches))
8983 option_conflict('a', 'b');
8984 if (send_all_tags && !TAILQ_EMPTY(&tags))
8985 option_conflict('T', 't');
8989 remote_name = GOT_SEND_DEFAULT_REMOTE_NAME;
8991 remote_name = argv[0];
8995 cwd = getcwd(NULL, 0);
8997 error = got_error_from_errno("getcwd");
9001 error = got_repo_pack_fds_open(&pack_fds);
9005 if (repo_path == NULL) {
9006 error = got_worktree_open(&worktree, cwd);
9007 if (error && error->code != GOT_ERR_NOT_WORKTREE)
9013 strdup(got_worktree_get_repo_path(worktree));
9014 if (repo_path == NULL)
9015 error = got_error_from_errno("strdup");
9019 repo_path = strdup(cwd);
9020 if (repo_path == NULL) {
9021 error = got_error_from_errno("strdup");
9027 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
9032 worktree_conf = got_worktree_get_gotconfig(worktree);
9033 if (worktree_conf) {
9034 got_gotconfig_get_remotes(&nremotes, &remotes,
9036 for (i = 0; i < nremotes; i++) {
9037 if (strcmp(remotes[i].name, remote_name) == 0) {
9038 remote = &remotes[i];
9044 if (remote == NULL) {
9045 repo_conf = got_repo_get_gotconfig(repo);
9047 got_gotconfig_get_remotes(&nremotes, &remotes,
9049 for (i = 0; i < nremotes; i++) {
9050 if (strcmp(remotes[i].name, remote_name) == 0) {
9051 remote = &remotes[i];
9057 if (remote == NULL) {
9058 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
9059 for (i = 0; i < nremotes; i++) {
9060 if (strcmp(remotes[i].name, remote_name) == 0) {
9061 remote = &remotes[i];
9066 if (remote == NULL) {
9067 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
9071 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
9072 &repo_name, remote->send_url);
9076 if (strcmp(proto, "git") == 0) {
9078 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
9079 "sendfd dns inet unveil", NULL) == -1)
9082 } else if (strcmp(proto, "git+ssh") == 0 ||
9083 strcmp(proto, "ssh") == 0) {
9085 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
9086 "sendfd unveil", NULL) == -1)
9089 } else if (strcmp(proto, "http") == 0 ||
9090 strcmp(proto, "git+http") == 0) {
9091 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
9094 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
9098 error = got_dial_apply_unveil(proto);
9102 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
9106 if (send_all_branches) {
9107 error = got_ref_list(&all_branches, repo, "refs/heads",
9108 got_ref_cmp_by_name, NULL);
9111 TAILQ_FOREACH(re, &all_branches, entry) {
9112 const char *branchname = got_ref_get_name(re->ref);
9113 error = got_pathlist_append(&branches,
9119 } else if (nbranches == 0) {
9120 for (i = 0; i < remote->nsend_branches; i++) {
9121 got_pathlist_append(&branches,
9122 remote->send_branches[i], NULL);
9126 if (send_all_tags) {
9127 error = got_ref_list(&all_tags, repo, "refs/tags",
9128 got_ref_cmp_by_name, NULL);
9131 TAILQ_FOREACH(re, &all_tags, entry) {
9132 const char *tagname = got_ref_get_name(re->ref);
9133 error = got_pathlist_append(&tags,
9141 * To prevent accidents only branches in refs/heads/ can be deleted
9142 * with 'got send -d'.
9143 * Deleting anything else requires local repository access or Git.
9145 TAILQ_FOREACH(pe, &delete_args, entry) {
9146 const char *branchname = pe->path;
9148 struct got_pathlist_entry *new;
9149 if (strncmp(branchname, "refs/heads/", 11) == 0) {
9150 s = strdup(branchname);
9152 error = got_error_from_errno("strdup");
9156 if (asprintf(&s, "refs/heads/%s", branchname) == -1) {
9157 error = got_error_from_errno("asprintf");
9161 error = got_pathlist_insert(&new, &delete_branches, s, NULL);
9162 if (error || new == NULL /* duplicate */)
9169 if (nbranches == 0 && ndelete_branches == 0) {
9170 struct got_reference *head_ref;
9172 error = got_ref_open(&head_ref, repo,
9173 got_worktree_get_head_ref_name(worktree), 0);
9175 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
9178 if (got_ref_is_symbolic(head_ref)) {
9179 error = got_ref_resolve_symbolic(&ref, repo, head_ref);
9180 got_ref_close(head_ref);
9185 error = got_pathlist_append(&branches, got_ref_get_name(ref),
9193 printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
9194 port ? ":" : "", port ? port : "");
9196 error = got_send_connect(&sendpid, &sendfd, proto, host, port,
9197 server_path, verbosity);
9201 memset(&spa, 0, sizeof(spa));
9202 spa.last_scaled_packsize[0] = '\0';
9203 spa.last_p_deltify = -1;
9204 spa.last_p_written = -1;
9205 spa.verbosity = verbosity;
9206 spa.delete_branches = &delete_branches;
9207 error = got_send_pack(remote_name, &branches, &tags, &delete_branches,
9208 verbosity, overwrite_refs, sendfd, repo, send_progress, &spa,
9209 check_cancelled, NULL);
9210 if (spa.printed_something)
9214 if (!spa.sent_something && verbosity >= 0)
9215 printf("Already up-to-date\n");
9218 if (kill(sendpid, SIGTERM) == -1)
9219 error = got_error_from_errno("kill");
9220 if (waitpid(sendpid, &sendstatus, 0) == -1 && error == NULL)
9221 error = got_error_from_errno("waitpid");
9223 if (sendfd != -1 && close(sendfd) == -1 && error == NULL)
9224 error = got_error_from_errno("close");
9226 const struct got_error *close_err = got_repo_close(repo);
9231 got_worktree_close(worktree);
9233 const struct got_error *pack_err =
9234 got_repo_pack_fds_close(pack_fds);
9240 got_pathlist_free(&branches);
9241 got_pathlist_free(&tags);
9242 got_ref_list_free(&all_branches);
9243 got_ref_list_free(&all_tags);
9244 got_pathlist_free(&delete_args);
9245 TAILQ_FOREACH(pe, &delete_branches, entry)
9246 free((char *)pe->path);
9247 got_pathlist_free(&delete_branches);
9259 usage_cherrypick(void)
9261 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
9265 static const struct got_error *
9266 cmd_cherrypick(int argc, char *argv[])
9268 const struct got_error *error = NULL;
9269 struct got_worktree *worktree = NULL;
9270 struct got_repository *repo = NULL;
9271 char *cwd = NULL, *commit_id_str = NULL;
9272 struct got_object_id *commit_id = NULL;
9273 struct got_commit_object *commit = NULL;
9274 struct got_object_qid *pid;
9276 struct got_update_progress_arg upa;
9277 int *pack_fds = NULL;
9279 while ((ch = getopt(argc, argv, "")) != -1) {
9291 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9292 "unveil", NULL) == -1)
9298 cwd = getcwd(NULL, 0);
9300 error = got_error_from_errno("getcwd");
9304 error = got_repo_pack_fds_open(&pack_fds);
9308 error = got_worktree_open(&worktree, cwd);
9310 if (error->code == GOT_ERR_NOT_WORKTREE)
9311 error = wrap_not_worktree_error(error, "cherrypick",
9316 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9321 error = apply_unveil(got_repo_get_path(repo), 0,
9322 got_worktree_get_root_path(worktree));
9326 error = got_repo_match_object_id(&commit_id, NULL, argv[0],
9327 GOT_OBJ_TYPE_COMMIT, NULL, repo);
9330 error = got_object_id_str(&commit_id_str, commit_id);
9334 error = got_object_open_as_commit(&commit, repo, commit_id);
9337 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
9338 memset(&upa, 0, sizeof(upa));
9339 error = got_worktree_merge_files(worktree, pid ? &pid->id : NULL,
9340 commit_id, repo, update_progress, &upa, check_cancelled,
9345 if (upa.did_something)
9346 printf("Merged commit %s\n", commit_id_str);
9347 print_merge_progress_stats(&upa);
9350 got_object_commit_close(commit);
9351 free(commit_id_str);
9353 got_worktree_close(worktree);
9355 const struct got_error *close_err = got_repo_close(repo);
9360 const struct got_error *pack_err =
9361 got_repo_pack_fds_close(pack_fds);
9372 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
9376 static const struct got_error *
9377 cmd_backout(int argc, char *argv[])
9379 const struct got_error *error = NULL;
9380 struct got_worktree *worktree = NULL;
9381 struct got_repository *repo = NULL;
9382 char *cwd = NULL, *commit_id_str = NULL;
9383 struct got_object_id *commit_id = NULL;
9384 struct got_commit_object *commit = NULL;
9385 struct got_object_qid *pid;
9387 struct got_update_progress_arg upa;
9388 int *pack_fds = NULL;
9390 while ((ch = getopt(argc, argv, "")) != -1) {
9402 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9403 "unveil", NULL) == -1)
9409 cwd = getcwd(NULL, 0);
9411 error = got_error_from_errno("getcwd");
9415 error = got_repo_pack_fds_open(&pack_fds);
9419 error = got_worktree_open(&worktree, cwd);
9421 if (error->code == GOT_ERR_NOT_WORKTREE)
9422 error = wrap_not_worktree_error(error, "backout", cwd);
9426 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9431 error = apply_unveil(got_repo_get_path(repo), 0,
9432 got_worktree_get_root_path(worktree));
9436 error = got_repo_match_object_id(&commit_id, NULL, argv[0],
9437 GOT_OBJ_TYPE_COMMIT, NULL, repo);
9440 error = got_object_id_str(&commit_id_str, commit_id);
9444 error = got_object_open_as_commit(&commit, repo, commit_id);
9447 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
9449 error = got_error(GOT_ERR_ROOT_COMMIT);
9453 memset(&upa, 0, sizeof(upa));
9454 error = got_worktree_merge_files(worktree, commit_id, &pid->id,
9455 repo, update_progress, &upa, check_cancelled, NULL);
9459 if (upa.did_something)
9460 printf("Backed out commit %s\n", commit_id_str);
9461 print_merge_progress_stats(&upa);
9464 got_object_commit_close(commit);
9465 free(commit_id_str);
9467 got_worktree_close(worktree);
9469 const struct got_error *close_err = got_repo_close(repo);
9474 const struct got_error *pack_err =
9475 got_repo_pack_fds_close(pack_fds);
9485 fprintf(stderr, "usage: %s rebase [-aclX] [branch]\n", getprogname());
9490 trim_logmsg(char *logmsg, int limit)
9495 len = strlen(logmsg);
9499 nl = strchr(logmsg, '\n');
9504 static const struct got_error *
9505 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
9507 const struct got_error *err;
9508 char *logmsg0 = NULL;
9511 err = got_object_commit_get_logmsg(&logmsg0, commit);
9516 while (isspace((unsigned char)s[0]))
9519 *logmsg = strdup(s);
9520 if (*logmsg == NULL) {
9521 err = got_error_from_errno("strdup");
9525 trim_logmsg(*logmsg, limit);
9531 static const struct got_error *
9532 show_rebase_merge_conflict(struct got_object_id *id,
9533 struct got_repository *repo)
9535 const struct got_error *err;
9536 struct got_commit_object *commit = NULL;
9537 char *id_str = NULL, *logmsg = NULL;
9539 err = got_object_open_as_commit(&commit, repo, id);
9543 err = got_object_id_str(&id_str, id);
9549 err = get_short_logmsg(&logmsg, 42, commit);
9553 printf("%s -> merge conflict: %s\n", id_str, logmsg);
9556 got_object_commit_close(commit);
9561 static const struct got_error *
9562 show_rebase_progress(struct got_commit_object *commit,
9563 struct got_object_id *old_id, struct got_object_id *new_id)
9565 const struct got_error *err;
9566 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
9568 err = got_object_id_str(&old_id_str, old_id);
9573 err = got_object_id_str(&new_id_str, new_id);
9578 old_id_str[12] = '\0';
9580 new_id_str[12] = '\0';
9582 err = get_short_logmsg(&logmsg, 42, commit);
9586 printf("%s -> %s: %s\n", old_id_str,
9587 new_id_str ? new_id_str : "no-op change", logmsg);
9595 static const struct got_error *
9596 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
9597 struct got_reference *branch, struct got_reference *new_base_branch,
9598 struct got_reference *tmp_branch, struct got_repository *repo,
9601 printf("Switching work tree to %s\n", got_ref_get_name(branch));
9602 return got_worktree_rebase_complete(worktree, fileindex,
9603 new_base_branch, tmp_branch, branch, repo, create_backup);
9606 static const struct got_error *
9607 rebase_commit(struct got_pathlist_head *merged_paths,
9608 struct got_worktree *worktree, struct got_fileindex *fileindex,
9609 struct got_reference *tmp_branch, const char *committer,
9610 struct got_object_id *commit_id, struct got_repository *repo)
9612 const struct got_error *error;
9613 struct got_commit_object *commit;
9614 struct got_object_id *new_commit_id;
9616 error = got_object_open_as_commit(&commit, repo, commit_id);
9620 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
9621 worktree, fileindex, tmp_branch, committer, commit, commit_id,
9624 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
9626 error = show_rebase_progress(commit, commit_id, NULL);
9628 error = show_rebase_progress(commit, commit_id, new_commit_id);
9629 free(new_commit_id);
9632 got_object_commit_close(commit);
9636 struct check_path_prefix_arg {
9637 const char *path_prefix;
9642 static const struct got_error *
9643 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
9644 struct got_blob_object *blob2, FILE *f1, FILE *f2,
9645 struct got_object_id *id1, struct got_object_id *id2,
9646 const char *path1, const char *path2,
9647 mode_t mode1, mode_t mode2, struct got_repository *repo)
9649 struct check_path_prefix_arg *a = arg;
9651 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
9652 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
9653 return got_error(a->errcode);
9658 static const struct got_error *
9659 check_path_prefix(struct got_object_id *parent_id,
9660 struct got_object_id *commit_id, const char *path_prefix,
9661 int errcode, struct got_repository *repo)
9663 const struct got_error *err;
9664 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
9665 struct got_commit_object *commit = NULL, *parent_commit = NULL;
9666 struct check_path_prefix_arg cpp_arg;
9668 if (got_path_is_root_dir(path_prefix))
9671 err = got_object_open_as_commit(&commit, repo, commit_id);
9675 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
9679 err = got_object_open_as_tree(&tree1, repo,
9680 got_object_commit_get_tree_id(parent_commit));
9684 err = got_object_open_as_tree(&tree2, repo,
9685 got_object_commit_get_tree_id(commit));
9689 cpp_arg.path_prefix = path_prefix;
9690 while (cpp_arg.path_prefix[0] == '/')
9691 cpp_arg.path_prefix++;
9692 cpp_arg.len = strlen(cpp_arg.path_prefix);
9693 cpp_arg.errcode = errcode;
9694 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
9695 check_path_prefix_in_diff, &cpp_arg, 0);
9698 got_object_tree_close(tree1);
9700 got_object_tree_close(tree2);
9702 got_object_commit_close(commit);
9704 got_object_commit_close(parent_commit);
9708 static const struct got_error *
9709 collect_commits(struct got_object_id_queue *commits,
9710 struct got_object_id *initial_commit_id,
9711 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
9712 const char *path_prefix, int path_prefix_errcode,
9713 struct got_repository *repo)
9715 const struct got_error *err = NULL;
9716 struct got_commit_graph *graph = NULL;
9717 struct got_object_id parent_id, commit_id;
9718 struct got_object_qid *qid;
9720 err = got_commit_graph_open(&graph, "/", 1);
9724 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
9725 check_cancelled, NULL);
9729 memcpy(&commit_id, initial_commit_id, sizeof(commit_id));
9730 while (got_object_id_cmp(&commit_id, iter_stop_id) != 0) {
9731 err = got_commit_graph_iter_next(&parent_id, graph, repo,
9732 check_cancelled, NULL);
9734 if (err->code == GOT_ERR_ITER_COMPLETED) {
9735 err = got_error_msg(GOT_ERR_ANCESTRY,
9736 "ran out of commits to rebase before "
9737 "youngest common ancestor commit has "
9742 err = check_path_prefix(&parent_id, &commit_id,
9743 path_prefix, path_prefix_errcode, repo);
9747 err = got_object_qid_alloc(&qid, &commit_id);
9750 STAILQ_INSERT_HEAD(commits, qid, entry);
9752 memcpy(&commit_id, &parent_id, sizeof(commit_id));
9756 got_commit_graph_close(graph);
9760 static const struct got_error *
9761 get_commit_brief_str(char **brief_str, struct got_commit_object *commit)
9763 const struct got_error *err = NULL;
9764 time_t committer_time;
9766 char datebuf[11]; /* YYYY-MM-DD + NUL */
9767 char *author0 = NULL, *author, *smallerthan;
9768 char *logmsg0 = NULL, *logmsg, *newline;
9770 committer_time = got_object_commit_get_committer_time(commit);
9771 if (gmtime_r(&committer_time, &tm) == NULL)
9772 return got_error_from_errno("gmtime_r");
9773 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d", &tm) == 0)
9774 return got_error(GOT_ERR_NO_SPACE);
9776 author0 = strdup(got_object_commit_get_author(commit));
9777 if (author0 == NULL)
9778 return got_error_from_errno("strdup");
9780 smallerthan = strchr(author, '<');
9781 if (smallerthan && smallerthan[1] != '\0')
9782 author = smallerthan + 1;
9783 author[strcspn(author, "@>")] = '\0';
9785 err = got_object_commit_get_logmsg(&logmsg0, commit);
9789 while (*logmsg == '\n')
9791 newline = strchr(logmsg, '\n');
9795 if (asprintf(brief_str, "%s %s %s",
9796 datebuf, author, logmsg) == -1)
9797 err = got_error_from_errno("asprintf");
9804 static const struct got_error *
9805 delete_backup_ref(struct got_reference *ref, struct got_object_id *id,
9806 struct got_repository *repo)
9808 const struct got_error *err;
9811 err = got_object_id_str(&id_str, id);
9815 err = got_ref_delete(ref, repo);
9819 printf("Deleted %s: %s\n", got_ref_get_name(ref), id_str);
9825 static const struct got_error *
9826 print_backup_ref(const char *branch_name, const char *new_id_str,
9827 struct got_object_id *old_commit_id, struct got_commit_object *old_commit,
9828 struct got_reflist_object_id_map *refs_idmap,
9829 struct got_repository *repo)
9831 const struct got_error *err = NULL;
9832 struct got_reflist_head *refs;
9833 char *refs_str = NULL;
9834 struct got_object_id *new_commit_id = NULL;
9835 struct got_commit_object *new_commit = NULL;
9836 char *new_commit_brief_str = NULL;
9837 struct got_object_id *yca_id = NULL;
9838 struct got_commit_object *yca_commit = NULL;
9839 char *yca_id_str = NULL, *yca_brief_str = NULL;
9840 char *custom_refs_str;
9842 if (asprintf(&custom_refs_str, "formerly %s", branch_name) == -1)
9843 return got_error_from_errno("asprintf");
9845 err = print_commit(old_commit, old_commit_id, repo, NULL, NULL,
9846 0, 0, refs_idmap, custom_refs_str);
9850 err = got_object_resolve_id_str(&new_commit_id, repo, new_id_str);
9854 refs = got_reflist_object_id_map_lookup(refs_idmap, new_commit_id);
9856 err = build_refs_str(&refs_str, refs, new_commit_id, repo, 0);
9861 err = got_object_open_as_commit(&new_commit, repo, new_commit_id);
9865 err = get_commit_brief_str(&new_commit_brief_str, new_commit);
9869 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
9870 old_commit_id, new_commit_id, 1, repo, check_cancelled, NULL);
9874 printf("has become commit %s%s%s%s\n %s\n", new_id_str,
9875 refs_str ? " (" : "", refs_str ? refs_str : "",
9876 refs_str ? ")" : "", new_commit_brief_str);
9877 if (yca_id && got_object_id_cmp(yca_id, new_commit_id) != 0 &&
9878 got_object_id_cmp(yca_id, old_commit_id) != 0) {
9882 err = got_object_open_as_commit(&yca_commit, repo, yca_id);
9886 err = get_commit_brief_str(&yca_brief_str, yca_commit);
9890 err = got_object_id_str(&yca_id_str, yca_id);
9894 refs = got_reflist_object_id_map_lookup(refs_idmap, yca_id);
9896 err = build_refs_str(&refs_str, refs, yca_id, repo, 0);
9900 printf("history forked at %s%s%s%s\n %s\n",
9902 refs_str ? " (" : "", refs_str ? refs_str : "",
9903 refs_str ? ")" : "", yca_brief_str);
9906 free(custom_refs_str);
9907 free(new_commit_id);
9911 free(yca_brief_str);
9913 got_object_commit_close(new_commit);
9915 got_object_commit_close(yca_commit);
9920 static const struct got_error *
9921 process_backup_refs(const char *backup_ref_prefix,
9922 const char *wanted_branch_name,
9923 int delete, struct got_repository *repo)
9925 const struct got_error *err;
9926 struct got_reflist_head refs, backup_refs;
9927 struct got_reflist_entry *re;
9928 const size_t backup_ref_prefix_len = strlen(backup_ref_prefix);
9929 struct got_object_id *old_commit_id = NULL;
9930 char *branch_name = NULL;
9931 struct got_commit_object *old_commit = NULL;
9932 struct got_reflist_object_id_map *refs_idmap = NULL;
9933 int wanted_branch_found = 0;
9936 TAILQ_INIT(&backup_refs);
9938 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
9942 err = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
9946 if (wanted_branch_name) {
9947 if (strncmp(wanted_branch_name, "refs/heads/", 11) == 0)
9948 wanted_branch_name += 11;
9951 err = got_ref_list(&backup_refs, repo, backup_ref_prefix,
9952 got_ref_cmp_by_commit_timestamp_descending, repo);
9956 TAILQ_FOREACH(re, &backup_refs, entry) {
9957 const char *refname = got_ref_get_name(re->ref);
9960 err = check_cancelled(NULL);
9964 err = got_ref_resolve(&old_commit_id, repo, re->ref);
9968 err = got_object_open_as_commit(&old_commit, repo,
9973 if (strncmp(backup_ref_prefix, refname,
9974 backup_ref_prefix_len) == 0)
9975 refname += backup_ref_prefix_len;
9977 while (refname[0] == '/')
9980 branch_name = strdup(refname);
9981 if (branch_name == NULL) {
9982 err = got_error_from_errno("strdup");
9985 slash = strrchr(branch_name, '/');
9988 refname += strlen(branch_name) + 1;
9991 if (wanted_branch_name == NULL ||
9992 strcmp(wanted_branch_name, branch_name) == 0) {
9993 wanted_branch_found = 1;
9995 err = delete_backup_ref(re->ref,
9996 old_commit_id, repo);
9998 err = print_backup_ref(branch_name, refname,
9999 old_commit_id, old_commit, refs_idmap,
10006 free(old_commit_id);
10007 old_commit_id = NULL;
10009 branch_name = NULL;
10010 got_object_commit_close(old_commit);
10014 if (wanted_branch_name && !wanted_branch_found) {
10015 err = got_error_fmt(GOT_ERR_NOT_REF,
10016 "%s/%s/", backup_ref_prefix, wanted_branch_name);
10020 got_reflist_object_id_map_free(refs_idmap);
10021 got_ref_list_free(&refs);
10022 got_ref_list_free(&backup_refs);
10023 free(old_commit_id);
10026 got_object_commit_close(old_commit);
10030 static const struct got_error *
10031 abort_progress(void *arg, unsigned char status, const char *path)
10034 * Unversioned files should not clutter progress output when
10035 * an operation is aborted.
10037 if (status == GOT_STATUS_UNVERSIONED)
10040 return update_progress(arg, status, path);
10043 static const struct got_error *
10044 cmd_rebase(int argc, char *argv[])
10046 const struct got_error *error = NULL;
10047 struct got_worktree *worktree = NULL;
10048 struct got_repository *repo = NULL;
10049 struct got_fileindex *fileindex = NULL;
10050 char *cwd = NULL, *committer = NULL, *gitconfig_path = NULL;
10051 struct got_reference *branch = NULL;
10052 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
10053 struct got_object_id *commit_id = NULL, *parent_id = NULL;
10054 struct got_object_id *resume_commit_id = NULL;
10055 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
10056 struct got_commit_object *commit = NULL;
10057 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
10058 int histedit_in_progress = 0, merge_in_progress = 0;
10059 int create_backup = 1, list_backups = 0, delete_backups = 0;
10060 struct got_object_id_queue commits;
10061 struct got_pathlist_head merged_paths;
10062 const struct got_object_id_queue *parent_ids;
10063 struct got_object_qid *qid, *pid;
10064 struct got_update_progress_arg upa;
10065 int *pack_fds = NULL;
10067 STAILQ_INIT(&commits);
10068 TAILQ_INIT(&merged_paths);
10069 memset(&upa, 0, sizeof(upa));
10071 while ((ch = getopt(argc, argv, "aclX")) != -1) {
10077 continue_rebase = 1;
10083 delete_backups = 1;
10095 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10096 "unveil", NULL) == -1)
10099 if (list_backups) {
10101 option_conflict('l', 'a');
10102 if (continue_rebase)
10103 option_conflict('l', 'c');
10104 if (delete_backups)
10105 option_conflict('l', 'X');
10106 if (argc != 0 && argc != 1)
10108 } else if (delete_backups) {
10110 option_conflict('X', 'a');
10111 if (continue_rebase)
10112 option_conflict('X', 'c');
10114 option_conflict('l', 'X');
10115 if (argc != 0 && argc != 1)
10118 if (abort_rebase && continue_rebase)
10120 else if (abort_rebase || continue_rebase) {
10123 } else if (argc != 1)
10127 cwd = getcwd(NULL, 0);
10129 error = got_error_from_errno("getcwd");
10133 error = got_repo_pack_fds_open(&pack_fds);
10137 error = got_worktree_open(&worktree, cwd);
10139 if (list_backups || delete_backups) {
10140 if (error->code != GOT_ERR_NOT_WORKTREE)
10143 if (error->code == GOT_ERR_NOT_WORKTREE)
10144 error = wrap_not_worktree_error(error,
10150 error = get_gitconfig_path(&gitconfig_path);
10153 error = got_repo_open(&repo,
10154 worktree ? got_worktree_get_repo_path(worktree) : cwd,
10155 gitconfig_path, pack_fds);
10159 error = get_author(&committer, repo, worktree);
10160 if (error && error->code != GOT_ERR_COMMIT_NO_AUTHOR)
10163 error = apply_unveil(got_repo_get_path(repo), 0,
10164 worktree ? got_worktree_get_root_path(worktree) : NULL);
10168 if (list_backups || delete_backups) {
10169 error = process_backup_refs(
10170 GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
10171 argc == 1 ? argv[0] : NULL, delete_backups, repo);
10172 goto done; /* nothing else to do */
10175 error = got_worktree_histedit_in_progress(&histedit_in_progress,
10179 if (histedit_in_progress) {
10180 error = got_error(GOT_ERR_HISTEDIT_BUSY);
10184 error = got_worktree_merge_in_progress(&merge_in_progress,
10188 if (merge_in_progress) {
10189 error = got_error(GOT_ERR_MERGE_BUSY);
10193 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
10197 if (abort_rebase) {
10198 if (!rebase_in_progress) {
10199 error = got_error(GOT_ERR_NOT_REBASING);
10202 error = got_worktree_rebase_continue(&resume_commit_id,
10203 &new_base_branch, &tmp_branch, &branch, &fileindex,
10207 printf("Switching work tree to %s\n",
10208 got_ref_get_symref_target(new_base_branch));
10209 error = got_worktree_rebase_abort(worktree, fileindex, repo,
10210 new_base_branch, abort_progress, &upa);
10213 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
10214 print_merge_progress_stats(&upa);
10215 goto done; /* nothing else to do */
10218 if (continue_rebase) {
10219 if (!rebase_in_progress) {
10220 error = got_error(GOT_ERR_NOT_REBASING);
10223 error = got_worktree_rebase_continue(&resume_commit_id,
10224 &new_base_branch, &tmp_branch, &branch, &fileindex,
10229 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
10230 committer, resume_commit_id, repo);
10234 yca_id = got_object_id_dup(resume_commit_id);
10235 if (yca_id == NULL) {
10236 error = got_error_from_errno("got_object_id_dup");
10240 error = got_ref_open(&branch, repo, argv[0], 0);
10243 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
10244 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
10245 "will not rebase a branch which lives outside "
10246 "the \"refs/heads/\" reference namespace");
10251 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
10255 if (!continue_rebase) {
10256 struct got_object_id *base_commit_id;
10258 base_commit_id = got_worktree_get_base_commit_id(worktree);
10259 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
10260 base_commit_id, branch_head_commit_id, 1, repo,
10261 check_cancelled, NULL);
10264 if (yca_id == NULL) {
10265 error = got_error_msg(GOT_ERR_ANCESTRY,
10266 "specified branch shares no common ancestry "
10267 "with work tree's branch");
10271 error = check_same_branch(base_commit_id, branch, yca_id, repo);
10273 if (error->code != GOT_ERR_ANCESTRY)
10277 struct got_pathlist_head paths;
10278 printf("%s is already based on %s\n",
10279 got_ref_get_name(branch),
10280 got_worktree_get_head_ref_name(worktree));
10281 error = switch_head_ref(branch, branch_head_commit_id,
10285 error = got_worktree_set_base_commit_id(worktree, repo,
10286 branch_head_commit_id);
10289 TAILQ_INIT(&paths);
10290 error = got_pathlist_append(&paths, "", NULL);
10293 error = got_worktree_checkout_files(worktree,
10294 &paths, repo, update_progress, &upa,
10295 check_cancelled, NULL);
10296 got_pathlist_free(&paths);
10299 if (upa.did_something) {
10301 error = got_object_id_str(&id_str,
10302 branch_head_commit_id);
10305 printf("Updated to %s: %s\n",
10306 got_worktree_get_head_ref_name(worktree),
10310 printf("Already up-to-date\n");
10311 print_update_progress_stats(&upa);
10316 commit_id = branch_head_commit_id;
10317 error = got_object_open_as_commit(&commit, repo, commit_id);
10321 parent_ids = got_object_commit_get_parent_ids(commit);
10322 pid = STAILQ_FIRST(parent_ids);
10324 error = got_error(GOT_ERR_EMPTY_REBASE);
10327 error = collect_commits(&commits, commit_id, &pid->id,
10328 yca_id, got_worktree_get_path_prefix(worktree),
10329 GOT_ERR_REBASE_PATH, repo);
10330 got_object_commit_close(commit);
10335 if (!continue_rebase) {
10336 error = got_worktree_rebase_prepare(&new_base_branch,
10337 &tmp_branch, &fileindex, worktree, branch, repo);
10342 if (STAILQ_EMPTY(&commits)) {
10343 if (continue_rebase) {
10344 error = rebase_complete(worktree, fileindex,
10345 branch, new_base_branch, tmp_branch, repo,
10349 /* Fast-forward the reference of the branch. */
10350 struct got_object_id *new_head_commit_id;
10352 error = got_ref_resolve(&new_head_commit_id, repo,
10356 error = got_object_id_str(&id_str, new_head_commit_id);
10359 printf("Forwarding %s to commit %s\n",
10360 got_ref_get_name(branch), id_str);
10362 error = got_ref_change_ref(branch,
10363 new_head_commit_id);
10366 /* No backup needed since objects did not change. */
10372 STAILQ_FOREACH(qid, &commits, entry) {
10374 commit_id = &qid->id;
10375 parent_id = pid ? &pid->id : yca_id;
10378 memset(&upa, 0, sizeof(upa));
10379 error = got_worktree_rebase_merge_files(&merged_paths,
10380 worktree, fileindex, parent_id, commit_id, repo,
10381 update_progress, &upa, check_cancelled, NULL);
10385 print_merge_progress_stats(&upa);
10386 if (upa.conflicts > 0 || upa.missing > 0 ||
10387 upa.not_deleted > 0 || upa.unversioned > 0) {
10388 if (upa.conflicts > 0) {
10389 error = show_rebase_merge_conflict(&qid->id,
10394 got_worktree_rebase_pathlist_free(&merged_paths);
10398 error = rebase_commit(&merged_paths, worktree, fileindex,
10399 tmp_branch, committer, commit_id, repo);
10400 got_worktree_rebase_pathlist_free(&merged_paths);
10405 if (upa.conflicts > 0 || upa.missing > 0 ||
10406 upa.not_deleted > 0 || upa.unversioned > 0) {
10407 error = got_worktree_rebase_postpone(worktree, fileindex);
10410 if (upa.conflicts > 0 && upa.missing == 0 &&
10411 upa.not_deleted == 0 && upa.unversioned == 0) {
10412 error = got_error_msg(GOT_ERR_CONFLICTS,
10413 "conflicts must be resolved before rebasing "
10415 } else if (upa.conflicts > 0) {
10416 error = got_error_msg(GOT_ERR_CONFLICTS,
10417 "conflicts must be resolved before rebasing "
10418 "can continue; changes destined for some "
10419 "files were not yet merged and should be "
10420 "merged manually if required before the "
10421 "rebase operation is continued");
10423 error = got_error_msg(GOT_ERR_CONFLICTS,
10424 "changes destined for some files were not "
10425 "yet merged and should be merged manually "
10426 "if required before the rebase operation "
10430 error = rebase_complete(worktree, fileindex, branch,
10431 new_base_branch, tmp_branch, repo, create_backup);
10435 free(gitconfig_path);
10436 got_object_id_queue_free(&commits);
10437 free(branch_head_commit_id);
10438 free(resume_commit_id);
10441 got_object_commit_close(commit);
10443 got_ref_close(branch);
10444 if (new_base_branch)
10445 got_ref_close(new_base_branch);
10447 got_ref_close(tmp_branch);
10449 got_worktree_close(worktree);
10451 const struct got_error *close_err = got_repo_close(repo);
10456 const struct got_error *pack_err =
10457 got_repo_pack_fds_close(pack_fds);
10465 usage_histedit(void)
10467 fprintf(stderr, "usage: %s histedit [-aceflmX] [-F histedit-script] "
10468 "[branch]\n", getprogname());
10472 #define GOT_HISTEDIT_PICK 'p'
10473 #define GOT_HISTEDIT_EDIT 'e'
10474 #define GOT_HISTEDIT_FOLD 'f'
10475 #define GOT_HISTEDIT_DROP 'd'
10476 #define GOT_HISTEDIT_MESG 'm'
10478 static const struct got_histedit_cmd {
10479 unsigned char code;
10482 } got_histedit_cmds[] = {
10483 { GOT_HISTEDIT_PICK, "pick", "use commit" },
10484 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
10485 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
10487 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
10488 { GOT_HISTEDIT_MESG, "mesg",
10489 "single-line log message for commit above (open editor if empty)" },
10492 struct got_histedit_list_entry {
10493 TAILQ_ENTRY(got_histedit_list_entry) entry;
10494 struct got_object_id *commit_id;
10495 const struct got_histedit_cmd *cmd;
10498 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
10500 static const struct got_error *
10501 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
10502 FILE *f, struct got_repository *repo)
10504 const struct got_error *err = NULL;
10505 char *logmsg = NULL, *id_str = NULL;
10506 struct got_commit_object *commit = NULL;
10509 err = got_object_open_as_commit(&commit, repo, commit_id);
10513 err = get_short_logmsg(&logmsg, 34, commit);
10517 err = got_object_id_str(&id_str, commit_id);
10521 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
10523 err = got_ferror(f, GOT_ERR_IO);
10526 got_object_commit_close(commit);
10532 static const struct got_error *
10533 histedit_write_commit_list(struct got_object_id_queue *commits,
10534 FILE *f, int edit_logmsg_only, int fold_only, int edit_only,
10535 struct got_repository *repo)
10537 const struct got_error *err = NULL;
10538 struct got_object_qid *qid;
10539 const char *histedit_cmd = NULL;
10541 if (STAILQ_EMPTY(commits))
10542 return got_error(GOT_ERR_EMPTY_HISTEDIT);
10544 STAILQ_FOREACH(qid, commits, entry) {
10545 histedit_cmd = got_histedit_cmds[0].name;
10547 histedit_cmd = "edit";
10548 else if (fold_only && STAILQ_NEXT(qid, entry) != NULL)
10549 histedit_cmd = "fold";
10550 err = histedit_write_commit(&qid->id, histedit_cmd, f, repo);
10553 if (edit_logmsg_only) {
10554 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
10556 err = got_ferror(f, GOT_ERR_IO);
10565 static const struct got_error *
10566 write_cmd_list(FILE *f, const char *branch_name,
10567 struct got_object_id_queue *commits)
10569 const struct got_error *err = NULL;
10573 struct got_object_qid *qid;
10575 qid = STAILQ_FIRST(commits);
10576 err = got_object_id_str(&id_str, &qid->id);
10581 "# Editing the history of branch '%s' starting at\n"
10583 "# Commits will be processed in order from top to "
10584 "bottom of this file.\n", branch_name, id_str);
10586 err = got_ferror(f, GOT_ERR_IO);
10590 n = fprintf(f, "# Available histedit commands:\n");
10592 err = got_ferror(f, GOT_ERR_IO);
10596 for (i = 0; i < nitems(got_histedit_cmds); i++) {
10597 const struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
10598 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
10601 err = got_ferror(f, GOT_ERR_IO);
10610 static const struct got_error *
10611 histedit_syntax_error(int lineno)
10613 static char msg[42];
10616 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
10618 if (ret < 0 || (size_t)ret >= sizeof(msg))
10619 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
10621 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
10624 static const struct got_error *
10625 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
10626 char *logmsg, struct got_repository *repo)
10628 const struct got_error *err;
10629 struct got_commit_object *folded_commit = NULL;
10630 char *id_str, *folded_logmsg = NULL;
10632 err = got_object_id_str(&id_str, hle->commit_id);
10636 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
10640 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
10643 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
10644 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
10645 folded_logmsg) == -1) {
10646 err = got_error_from_errno("asprintf");
10650 got_object_commit_close(folded_commit);
10652 free(folded_logmsg);
10656 static struct got_histedit_list_entry *
10657 get_folded_commits(struct got_histedit_list_entry *hle)
10659 struct got_histedit_list_entry *prev, *folded = NULL;
10661 prev = TAILQ_PREV(hle, got_histedit_list, entry);
10662 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
10663 prev->cmd->code == GOT_HISTEDIT_DROP)) {
10664 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
10666 prev = TAILQ_PREV(prev, got_histedit_list, entry);
10672 static const struct got_error *
10673 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
10674 struct got_repository *repo)
10676 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
10677 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
10678 const struct got_error *err = NULL;
10679 struct got_commit_object *commit = NULL;
10682 struct got_histedit_list_entry *folded = NULL;
10684 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
10688 folded = get_folded_commits(hle);
10690 while (folded != hle) {
10691 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
10692 folded = TAILQ_NEXT(folded, entry);
10695 err = append_folded_commit_msg(&new_msg, folded,
10701 folded = TAILQ_NEXT(folded, entry);
10705 err = got_object_id_str(&id_str, hle->commit_id);
10708 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
10711 logmsg_len = asprintf(&new_msg,
10712 "%s\n# original log message of commit %s: %s",
10713 logmsg ? logmsg : "", id_str, orig_logmsg);
10714 if (logmsg_len == -1) {
10715 err = got_error_from_errno("asprintf");
10721 err = got_object_id_str(&id_str, hle->commit_id);
10725 err = got_opentemp_named_fd(&logmsg_path, &fd,
10726 GOT_TMPDIR_STR "/got-logmsg");
10730 write(fd, logmsg, logmsg_len);
10733 err = get_editor(&editor);
10737 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg,
10740 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
10743 hle->logmsg = strdup(new_msg);
10744 if (hle->logmsg == NULL)
10745 err = got_error_from_errno("strdup");
10748 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
10749 err = got_error_from_errno2("unlink", logmsg_path);
10755 got_object_commit_close(commit);
10759 static const struct got_error *
10760 histedit_parse_list(struct got_histedit_list *histedit_cmds,
10761 FILE *f, struct got_repository *repo)
10763 const struct got_error *err = NULL;
10764 char *line = NULL, *p, *end;
10767 int lineno = 0, lastcmd = -1;
10768 const struct got_histedit_cmd *cmd;
10769 struct got_object_id *commit_id = NULL;
10770 struct got_histedit_list_entry *hle = NULL;
10773 len = getline(&line, &size, f);
10775 const struct got_error *getline_err;
10778 getline_err = got_error_from_errno("getline");
10779 err = got_ferror(f, getline_err->code);
10784 while (isspace((unsigned char)p[0]))
10786 if (p[0] == '#' || p[0] == '\0') {
10792 for (i = 0; i < nitems(got_histedit_cmds); i++) {
10793 cmd = &got_histedit_cmds[i];
10794 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
10795 isspace((unsigned char)p[strlen(cmd->name)])) {
10796 p += strlen(cmd->name);
10799 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
10804 if (i == nitems(got_histedit_cmds)) {
10805 err = histedit_syntax_error(lineno);
10808 while (isspace((unsigned char)p[0]))
10810 if (cmd->code == GOT_HISTEDIT_MESG) {
10811 if (lastcmd != GOT_HISTEDIT_PICK &&
10812 lastcmd != GOT_HISTEDIT_EDIT) {
10813 err = got_error(GOT_ERR_HISTEDIT_CMD);
10816 if (p[0] == '\0') {
10817 err = histedit_edit_logmsg(hle, repo);
10821 hle->logmsg = strdup(p);
10822 if (hle->logmsg == NULL) {
10823 err = got_error_from_errno("strdup");
10829 lastcmd = cmd->code;
10833 while (end[0] && !isspace((unsigned char)end[0]))
10837 err = got_object_resolve_id_str(&commit_id, repo, p);
10839 /* override error code */
10840 err = histedit_syntax_error(lineno);
10844 hle = malloc(sizeof(*hle));
10846 err = got_error_from_errno("malloc");
10850 hle->commit_id = commit_id;
10851 hle->logmsg = NULL;
10855 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
10856 lastcmd = cmd->code;
10864 static const struct got_error *
10865 histedit_check_script(struct got_histedit_list *histedit_cmds,
10866 struct got_object_id_queue *commits, struct got_repository *repo)
10868 const struct got_error *err = NULL;
10869 struct got_object_qid *qid;
10870 struct got_histedit_list_entry *hle;
10871 static char msg[92];
10874 if (TAILQ_EMPTY(histedit_cmds))
10875 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
10876 "histedit script contains no commands");
10877 if (STAILQ_EMPTY(commits))
10878 return got_error(GOT_ERR_EMPTY_HISTEDIT);
10880 TAILQ_FOREACH(hle, histedit_cmds, entry) {
10881 struct got_histedit_list_entry *hle2;
10882 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
10885 if (got_object_id_cmp(hle->commit_id,
10886 hle2->commit_id) != 0)
10888 err = got_object_id_str(&id_str, hle->commit_id);
10891 snprintf(msg, sizeof(msg), "commit %s is listed "
10892 "more than once in histedit script", id_str);
10894 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
10898 STAILQ_FOREACH(qid, commits, entry) {
10899 TAILQ_FOREACH(hle, histedit_cmds, entry) {
10900 if (got_object_id_cmp(&qid->id, hle->commit_id) == 0)
10904 err = got_object_id_str(&id_str, &qid->id);
10907 snprintf(msg, sizeof(msg),
10908 "commit %s missing from histedit script", id_str);
10910 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
10914 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
10915 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
10916 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
10917 "last commit in histedit script cannot be folded");
10922 static const struct got_error *
10923 histedit_run_editor(struct got_histedit_list *histedit_cmds,
10924 const char *path, struct got_object_id_queue *commits,
10925 struct got_repository *repo)
10927 const struct got_error *err = NULL;
10931 err = get_editor(&editor);
10935 if (spawn_editor(editor, path) == -1) {
10936 err = got_error_from_errno("failed spawning editor");
10940 f = fopen(path, "re");
10942 err = got_error_from_errno("fopen");
10945 err = histedit_parse_list(histedit_cmds, f, repo);
10949 err = histedit_check_script(histedit_cmds, commits, repo);
10951 if (f && fclose(f) == EOF && err == NULL)
10952 err = got_error_from_errno("fclose");
10957 static const struct got_error *
10958 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
10959 struct got_object_id_queue *, const char *, const char *,
10960 struct got_repository *);
10962 static const struct got_error *
10963 histedit_edit_script(struct got_histedit_list *histedit_cmds,
10964 struct got_object_id_queue *commits, const char *branch_name,
10965 int edit_logmsg_only, int fold_only, int edit_only,
10966 struct got_repository *repo)
10968 const struct got_error *err;
10972 err = got_opentemp_named(&path, &f, "got-histedit");
10976 err = write_cmd_list(f, branch_name, commits);
10980 err = histedit_write_commit_list(commits, f, edit_logmsg_only,
10981 fold_only, edit_only, repo);
10985 if (edit_logmsg_only || fold_only || edit_only) {
10987 err = histedit_parse_list(histedit_cmds, f, repo);
10989 if (fclose(f) == EOF) {
10990 err = got_error_from_errno("fclose");
10994 err = histedit_run_editor(histedit_cmds, path, commits, repo);
10996 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
10997 err->code != GOT_ERR_HISTEDIT_CMD)
10999 err = histedit_edit_list_retry(histedit_cmds, err,
11000 commits, path, branch_name, repo);
11004 if (f && fclose(f) == EOF && err == NULL)
11005 err = got_error_from_errno("fclose");
11006 if (path && unlink(path) != 0 && err == NULL)
11007 err = got_error_from_errno2("unlink", path);
11012 static const struct got_error *
11013 histedit_save_list(struct got_histedit_list *histedit_cmds,
11014 struct got_worktree *worktree, struct got_repository *repo)
11016 const struct got_error *err = NULL;
11019 struct got_histedit_list_entry *hle;
11020 struct got_commit_object *commit = NULL;
11022 err = got_worktree_get_histedit_script_path(&path, worktree);
11026 f = fopen(path, "we");
11028 err = got_error_from_errno2("fopen", path);
11031 TAILQ_FOREACH(hle, histedit_cmds, entry) {
11032 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
11038 int n = fprintf(f, "%c %s\n",
11039 GOT_HISTEDIT_MESG, hle->logmsg);
11041 err = got_ferror(f, GOT_ERR_IO);
11047 if (f && fclose(f) == EOF && err == NULL)
11048 err = got_error_from_errno("fclose");
11051 got_object_commit_close(commit);
11056 histedit_free_list(struct got_histedit_list *histedit_cmds)
11058 struct got_histedit_list_entry *hle;
11060 while ((hle = TAILQ_FIRST(histedit_cmds))) {
11061 TAILQ_REMOVE(histedit_cmds, hle, entry);
11066 static const struct got_error *
11067 histedit_load_list(struct got_histedit_list *histedit_cmds,
11068 const char *path, struct got_repository *repo)
11070 const struct got_error *err = NULL;
11073 f = fopen(path, "re");
11075 err = got_error_from_errno2("fopen", path);
11079 err = histedit_parse_list(histedit_cmds, f, repo);
11081 if (f && fclose(f) == EOF && err == NULL)
11082 err = got_error_from_errno("fclose");
11086 static const struct got_error *
11087 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
11088 const struct got_error *edit_err, struct got_object_id_queue *commits,
11089 const char *path, const char *branch_name, struct got_repository *repo)
11091 const struct got_error *err = NULL, *prev_err = edit_err;
11094 while (resp != 'c' && resp != 'r' && resp != 'a') {
11095 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
11096 "or (a)bort: ", getprogname(), prev_err->msg);
11101 histedit_free_list(histedit_cmds);
11102 err = histedit_run_editor(histedit_cmds, path, commits,
11105 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
11106 err->code != GOT_ERR_HISTEDIT_CMD)
11113 } else if (resp == 'r') {
11114 histedit_free_list(histedit_cmds);
11115 err = histedit_edit_script(histedit_cmds,
11116 commits, branch_name, 0, 0, 0, repo);
11118 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
11119 err->code != GOT_ERR_HISTEDIT_CMD)
11126 } else if (resp == 'a') {
11127 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
11130 printf("invalid response '%c'\n", resp);
11136 static const struct got_error *
11137 histedit_complete(struct got_worktree *worktree,
11138 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
11139 struct got_reference *branch, struct got_repository *repo)
11141 printf("Switching work tree to %s\n",
11142 got_ref_get_symref_target(branch));
11143 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
11147 static const struct got_error *
11148 show_histedit_progress(struct got_commit_object *commit,
11149 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
11151 const struct got_error *err;
11152 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
11154 err = got_object_id_str(&old_id_str, hle->commit_id);
11159 err = got_object_id_str(&new_id_str, new_id);
11164 old_id_str[12] = '\0';
11166 new_id_str[12] = '\0';
11169 logmsg = strdup(hle->logmsg);
11170 if (logmsg == NULL) {
11171 err = got_error_from_errno("strdup");
11174 trim_logmsg(logmsg, 42);
11176 err = get_short_logmsg(&logmsg, 42, commit);
11181 switch (hle->cmd->code) {
11182 case GOT_HISTEDIT_PICK:
11183 case GOT_HISTEDIT_EDIT:
11184 printf("%s -> %s: %s\n", old_id_str,
11185 new_id_str ? new_id_str : "no-op change", logmsg);
11187 case GOT_HISTEDIT_DROP:
11188 case GOT_HISTEDIT_FOLD:
11189 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
11201 static const struct got_error *
11202 histedit_commit(struct got_pathlist_head *merged_paths,
11203 struct got_worktree *worktree, struct got_fileindex *fileindex,
11204 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
11205 const char *committer, struct got_repository *repo)
11207 const struct got_error *err;
11208 struct got_commit_object *commit;
11209 struct got_object_id *new_commit_id;
11211 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
11212 && hle->logmsg == NULL) {
11213 err = histedit_edit_logmsg(hle, repo);
11218 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
11222 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
11223 worktree, fileindex, tmp_branch, committer, commit, hle->commit_id,
11224 hle->logmsg, repo);
11226 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
11228 err = show_histedit_progress(commit, hle, NULL);
11230 err = show_histedit_progress(commit, hle, new_commit_id);
11231 free(new_commit_id);
11234 got_object_commit_close(commit);
11238 static const struct got_error *
11239 histedit_skip_commit(struct got_histedit_list_entry *hle,
11240 struct got_worktree *worktree, struct got_repository *repo)
11242 const struct got_error *error;
11243 struct got_commit_object *commit;
11245 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
11250 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
11254 error = show_histedit_progress(commit, hle, NULL);
11255 got_object_commit_close(commit);
11259 static const struct got_error *
11260 check_local_changes(void *arg, unsigned char status,
11261 unsigned char staged_status, const char *path,
11262 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
11263 struct got_object_id *commit_id, int dirfd, const char *de_name)
11265 int *have_local_changes = arg;
11268 case GOT_STATUS_ADD:
11269 case GOT_STATUS_DELETE:
11270 case GOT_STATUS_MODIFY:
11271 case GOT_STATUS_CONFLICT:
11272 *have_local_changes = 1;
11273 return got_error(GOT_ERR_CANCELLED);
11278 switch (staged_status) {
11279 case GOT_STATUS_ADD:
11280 case GOT_STATUS_DELETE:
11281 case GOT_STATUS_MODIFY:
11282 *have_local_changes = 1;
11283 return got_error(GOT_ERR_CANCELLED);
11291 static const struct got_error *
11292 cmd_histedit(int argc, char *argv[])
11294 const struct got_error *error = NULL;
11295 struct got_worktree *worktree = NULL;
11296 struct got_fileindex *fileindex = NULL;
11297 struct got_repository *repo = NULL;
11298 char *cwd = NULL, *committer = NULL, *gitconfig_path = NULL;
11299 struct got_reference *branch = NULL;
11300 struct got_reference *tmp_branch = NULL;
11301 struct got_object_id *resume_commit_id = NULL;
11302 struct got_object_id *base_commit_id = NULL;
11303 struct got_object_id *head_commit_id = NULL;
11304 struct got_commit_object *commit = NULL;
11305 int ch, rebase_in_progress = 0, merge_in_progress = 0;
11306 struct got_update_progress_arg upa;
11307 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
11308 int edit_logmsg_only = 0, fold_only = 0, edit_only = 0;
11309 int list_backups = 0, delete_backups = 0;
11310 const char *edit_script_path = NULL;
11311 struct got_object_id_queue commits;
11312 struct got_pathlist_head merged_paths;
11313 const struct got_object_id_queue *parent_ids;
11314 struct got_object_qid *pid;
11315 struct got_histedit_list histedit_cmds;
11316 struct got_histedit_list_entry *hle;
11317 int *pack_fds = NULL;
11319 STAILQ_INIT(&commits);
11320 TAILQ_INIT(&histedit_cmds);
11321 TAILQ_INIT(&merged_paths);
11322 memset(&upa, 0, sizeof(upa));
11324 while ((ch = getopt(argc, argv, "aceF:flmX")) != -1) {
11336 edit_script_path = optarg;
11345 edit_logmsg_only = 1;
11348 delete_backups = 1;
11360 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
11361 "unveil", NULL) == -1)
11364 if (abort_edit && continue_edit)
11365 option_conflict('a', 'c');
11366 if (edit_script_path && edit_logmsg_only)
11367 option_conflict('F', 'm');
11368 if (abort_edit && edit_logmsg_only)
11369 option_conflict('a', 'm');
11370 if (continue_edit && edit_logmsg_only)
11371 option_conflict('c', 'm');
11372 if (abort_edit && fold_only)
11373 option_conflict('a', 'f');
11374 if (continue_edit && fold_only)
11375 option_conflict('c', 'f');
11376 if (fold_only && edit_logmsg_only)
11377 option_conflict('f', 'm');
11378 if (edit_script_path && fold_only)
11379 option_conflict('F', 'f');
11380 if (abort_edit && edit_only)
11381 option_conflict('a', 'e');
11382 if (continue_edit && edit_only)
11383 option_conflict('c', 'e');
11384 if (edit_only && edit_logmsg_only)
11385 option_conflict('e', 'm');
11386 if (edit_script_path && edit_only)
11387 option_conflict('F', 'e');
11388 if (list_backups) {
11390 option_conflict('l', 'a');
11392 option_conflict('l', 'c');
11393 if (edit_script_path)
11394 option_conflict('l', 'F');
11395 if (edit_logmsg_only)
11396 option_conflict('l', 'm');
11398 option_conflict('l', 'f');
11400 option_conflict('l', 'e');
11401 if (delete_backups)
11402 option_conflict('l', 'X');
11403 if (argc != 0 && argc != 1)
11405 } else if (delete_backups) {
11407 option_conflict('X', 'a');
11409 option_conflict('X', 'c');
11410 if (edit_script_path)
11411 option_conflict('X', 'F');
11412 if (edit_logmsg_only)
11413 option_conflict('X', 'm');
11415 option_conflict('X', 'f');
11417 option_conflict('X', 'e');
11419 option_conflict('X', 'l');
11420 if (argc != 0 && argc != 1)
11422 } else if (argc != 0)
11426 * This command cannot apply unveil(2) in all cases because the
11427 * user may choose to run an editor to edit the histedit script
11428 * and to edit individual commit log messages.
11429 * unveil(2) traverses exec(2); if an editor is used we have to
11430 * apply unveil after edit script and log messages have been written.
11431 * XXX TODO: Make use of unveil(2) where possible.
11434 cwd = getcwd(NULL, 0);
11436 error = got_error_from_errno("getcwd");
11440 error = got_repo_pack_fds_open(&pack_fds);
11444 error = got_worktree_open(&worktree, cwd);
11446 if (list_backups || delete_backups) {
11447 if (error->code != GOT_ERR_NOT_WORKTREE)
11450 if (error->code == GOT_ERR_NOT_WORKTREE)
11451 error = wrap_not_worktree_error(error,
11457 if (list_backups || delete_backups) {
11458 error = got_repo_open(&repo,
11459 worktree ? got_worktree_get_repo_path(worktree) : cwd,
11463 error = apply_unveil(got_repo_get_path(repo), 0,
11464 worktree ? got_worktree_get_root_path(worktree) : NULL);
11467 error = process_backup_refs(
11468 GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
11469 argc == 1 ? argv[0] : NULL, delete_backups, repo);
11470 goto done; /* nothing else to do */
11473 error = get_gitconfig_path(&gitconfig_path);
11476 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
11477 gitconfig_path, pack_fds);
11481 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
11484 if (rebase_in_progress) {
11485 error = got_error(GOT_ERR_REBASING);
11489 error = got_worktree_merge_in_progress(&merge_in_progress, worktree,
11493 if (merge_in_progress) {
11494 error = got_error(GOT_ERR_MERGE_BUSY);
11498 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
11502 if (edit_in_progress && edit_logmsg_only) {
11503 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
11504 "histedit operation is in progress in this "
11505 "work tree and must be continued or aborted "
11506 "before the -m option can be used");
11509 if (edit_in_progress && fold_only) {
11510 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
11511 "histedit operation is in progress in this "
11512 "work tree and must be continued or aborted "
11513 "before the -f option can be used");
11516 if (edit_in_progress && edit_only) {
11517 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
11518 "histedit operation is in progress in this "
11519 "work tree and must be continued or aborted "
11520 "before the -e option can be used");
11524 if (edit_in_progress && abort_edit) {
11525 error = got_worktree_histedit_continue(&resume_commit_id,
11526 &tmp_branch, &branch, &base_commit_id, &fileindex,
11530 printf("Switching work tree to %s\n",
11531 got_ref_get_symref_target(branch));
11532 error = got_worktree_histedit_abort(worktree, fileindex, repo,
11533 branch, base_commit_id, abort_progress, &upa);
11536 printf("Histedit of %s aborted\n",
11537 got_ref_get_symref_target(branch));
11538 print_merge_progress_stats(&upa);
11539 goto done; /* nothing else to do */
11540 } else if (abort_edit) {
11541 error = got_error(GOT_ERR_NOT_HISTEDIT);
11545 error = get_author(&committer, repo, worktree);
11549 if (continue_edit) {
11552 if (!edit_in_progress) {
11553 error = got_error(GOT_ERR_NOT_HISTEDIT);
11557 error = got_worktree_get_histedit_script_path(&path, worktree);
11561 error = histedit_load_list(&histedit_cmds, path, repo);
11566 error = got_worktree_histedit_continue(&resume_commit_id,
11567 &tmp_branch, &branch, &base_commit_id, &fileindex,
11572 error = got_ref_resolve(&head_commit_id, repo, branch);
11576 error = got_object_open_as_commit(&commit, repo,
11580 parent_ids = got_object_commit_get_parent_ids(commit);
11581 pid = STAILQ_FIRST(parent_ids);
11583 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
11586 error = collect_commits(&commits, head_commit_id, &pid->id,
11587 base_commit_id, got_worktree_get_path_prefix(worktree),
11588 GOT_ERR_HISTEDIT_PATH, repo);
11589 got_object_commit_close(commit);
11594 if (edit_in_progress) {
11595 error = got_error(GOT_ERR_HISTEDIT_BUSY);
11599 error = got_ref_open(&branch, repo,
11600 got_worktree_get_head_ref_name(worktree), 0);
11604 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
11605 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
11606 "will not edit commit history of a branch outside "
11607 "the \"refs/heads/\" reference namespace");
11611 error = got_ref_resolve(&head_commit_id, repo, branch);
11612 got_ref_close(branch);
11617 error = got_object_open_as_commit(&commit, repo,
11621 parent_ids = got_object_commit_get_parent_ids(commit);
11622 pid = STAILQ_FIRST(parent_ids);
11624 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
11627 error = collect_commits(&commits, head_commit_id, &pid->id,
11628 got_worktree_get_base_commit_id(worktree),
11629 got_worktree_get_path_prefix(worktree),
11630 GOT_ERR_HISTEDIT_PATH, repo);
11631 got_object_commit_close(commit);
11636 if (STAILQ_EMPTY(&commits)) {
11637 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
11641 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
11642 &base_commit_id, &fileindex, worktree, repo);
11646 if (edit_script_path) {
11647 error = histedit_load_list(&histedit_cmds,
11648 edit_script_path, repo);
11650 got_worktree_histedit_abort(worktree, fileindex,
11651 repo, branch, base_commit_id,
11652 abort_progress, &upa);
11653 print_merge_progress_stats(&upa);
11657 const char *branch_name;
11658 branch_name = got_ref_get_symref_target(branch);
11659 if (strncmp(branch_name, "refs/heads/", 11) == 0)
11661 error = histedit_edit_script(&histedit_cmds, &commits,
11662 branch_name, edit_logmsg_only, fold_only,
11665 got_worktree_histedit_abort(worktree, fileindex,
11666 repo, branch, base_commit_id,
11667 abort_progress, &upa);
11668 print_merge_progress_stats(&upa);
11674 error = histedit_save_list(&histedit_cmds, worktree,
11677 got_worktree_histedit_abort(worktree, fileindex,
11678 repo, branch, base_commit_id,
11679 abort_progress, &upa);
11680 print_merge_progress_stats(&upa);
11686 error = histedit_check_script(&histedit_cmds, &commits, repo);
11690 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
11691 if (resume_commit_id) {
11692 if (got_object_id_cmp(hle->commit_id,
11693 resume_commit_id) != 0)
11696 resume_commit_id = NULL;
11697 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
11698 hle->cmd->code == GOT_HISTEDIT_FOLD) {
11699 error = histedit_skip_commit(hle, worktree,
11704 struct got_pathlist_head paths;
11705 int have_changes = 0;
11707 TAILQ_INIT(&paths);
11708 error = got_pathlist_append(&paths, "", NULL);
11711 error = got_worktree_status(worktree, &paths,
11712 repo, 0, check_local_changes, &have_changes,
11713 check_cancelled, NULL);
11714 got_pathlist_free(&paths);
11716 if (error->code != GOT_ERR_CANCELLED)
11718 if (sigint_received || sigpipe_received)
11721 if (have_changes) {
11722 error = histedit_commit(NULL, worktree,
11723 fileindex, tmp_branch, hle,
11728 error = got_object_open_as_commit(
11729 &commit, repo, hle->commit_id);
11732 error = show_histedit_progress(commit,
11734 got_object_commit_close(commit);
11743 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
11744 error = histedit_skip_commit(hle, worktree, repo);
11750 error = got_object_open_as_commit(&commit, repo,
11754 parent_ids = got_object_commit_get_parent_ids(commit);
11755 pid = STAILQ_FIRST(parent_ids);
11757 error = got_worktree_histedit_merge_files(&merged_paths,
11758 worktree, fileindex, &pid->id, hle->commit_id, repo,
11759 update_progress, &upa, check_cancelled, NULL);
11762 got_object_commit_close(commit);
11765 print_merge_progress_stats(&upa);
11766 if (upa.conflicts > 0 || upa.missing > 0 ||
11767 upa.not_deleted > 0 || upa.unversioned > 0) {
11768 if (upa.conflicts > 0) {
11769 error = show_rebase_merge_conflict(
11770 hle->commit_id, repo);
11774 got_worktree_rebase_pathlist_free(&merged_paths);
11778 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
11780 error = got_object_id_str(&id_str, hle->commit_id);
11783 printf("Stopping histedit for amending commit %s\n",
11786 got_worktree_rebase_pathlist_free(&merged_paths);
11787 error = got_worktree_histedit_postpone(worktree,
11792 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
11793 error = histedit_skip_commit(hle, worktree, repo);
11799 error = histedit_commit(&merged_paths, worktree, fileindex,
11800 tmp_branch, hle, committer, repo);
11801 got_worktree_rebase_pathlist_free(&merged_paths);
11806 if (upa.conflicts > 0 || upa.missing > 0 ||
11807 upa.not_deleted > 0 || upa.unversioned > 0) {
11808 error = got_worktree_histedit_postpone(worktree, fileindex);
11811 if (upa.conflicts > 0 && upa.missing == 0 &&
11812 upa.not_deleted == 0 && upa.unversioned == 0) {
11813 error = got_error_msg(GOT_ERR_CONFLICTS,
11814 "conflicts must be resolved before histedit "
11816 } else if (upa.conflicts > 0) {
11817 error = got_error_msg(GOT_ERR_CONFLICTS,
11818 "conflicts must be resolved before histedit "
11819 "can continue; changes destined for some "
11820 "files were not yet merged and should be "
11821 "merged manually if required before the "
11822 "histedit operation is continued");
11824 error = got_error_msg(GOT_ERR_CONFLICTS,
11825 "changes destined for some files were not "
11826 "yet merged and should be merged manually "
11827 "if required before the histedit operation "
11831 error = histedit_complete(worktree, fileindex, tmp_branch,
11836 free(gitconfig_path);
11837 got_object_id_queue_free(&commits);
11838 histedit_free_list(&histedit_cmds);
11839 free(head_commit_id);
11840 free(base_commit_id);
11841 free(resume_commit_id);
11843 got_object_commit_close(commit);
11845 got_ref_close(branch);
11847 got_ref_close(tmp_branch);
11849 got_worktree_close(worktree);
11851 const struct got_error *close_err = got_repo_close(repo);
11856 const struct got_error *pack_err =
11857 got_repo_pack_fds_close(pack_fds);
11865 usage_integrate(void)
11867 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
11871 static const struct got_error *
11872 cmd_integrate(int argc, char *argv[])
11874 const struct got_error *error = NULL;
11875 struct got_repository *repo = NULL;
11876 struct got_worktree *worktree = NULL;
11877 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
11878 const char *branch_arg = NULL;
11879 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
11880 struct got_fileindex *fileindex = NULL;
11881 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
11883 struct got_update_progress_arg upa;
11884 int *pack_fds = NULL;
11886 while ((ch = getopt(argc, argv, "")) != -1) {
11899 branch_arg = argv[0];
11901 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
11902 "unveil", NULL) == -1)
11905 cwd = getcwd(NULL, 0);
11907 error = got_error_from_errno("getcwd");
11911 error = got_repo_pack_fds_open(&pack_fds);
11915 error = got_worktree_open(&worktree, cwd);
11917 if (error->code == GOT_ERR_NOT_WORKTREE)
11918 error = wrap_not_worktree_error(error, "integrate",
11923 error = check_rebase_or_histedit_in_progress(worktree);
11927 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
11932 error = apply_unveil(got_repo_get_path(repo), 0,
11933 got_worktree_get_root_path(worktree));
11937 error = check_merge_in_progress(worktree, repo);
11941 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
11942 error = got_error_from_errno("asprintf");
11946 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
11947 &base_branch_ref, worktree, refname, repo);
11951 refname = strdup(got_ref_get_name(branch_ref));
11952 if (refname == NULL) {
11953 error = got_error_from_errno("strdup");
11954 got_worktree_integrate_abort(worktree, fileindex, repo,
11955 branch_ref, base_branch_ref);
11958 base_refname = strdup(got_ref_get_name(base_branch_ref));
11959 if (base_refname == NULL) {
11960 error = got_error_from_errno("strdup");
11961 got_worktree_integrate_abort(worktree, fileindex, repo,
11962 branch_ref, base_branch_ref);
11965 if (strncmp(base_refname, "refs/heads/", 11) != 0) {
11966 error = got_error(GOT_ERR_INTEGRATE_BRANCH);
11967 got_worktree_integrate_abort(worktree, fileindex, repo,
11968 branch_ref, base_branch_ref);
11972 error = got_ref_resolve(&commit_id, repo, branch_ref);
11976 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
11980 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
11981 error = got_error_msg(GOT_ERR_SAME_BRANCH,
11982 "specified branch has already been integrated");
11983 got_worktree_integrate_abort(worktree, fileindex, repo,
11984 branch_ref, base_branch_ref);
11988 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
11990 if (error->code == GOT_ERR_ANCESTRY)
11991 error = got_error(GOT_ERR_REBASE_REQUIRED);
11992 got_worktree_integrate_abort(worktree, fileindex, repo,
11993 branch_ref, base_branch_ref);
11997 memset(&upa, 0, sizeof(upa));
11998 error = got_worktree_integrate_continue(worktree, fileindex, repo,
11999 branch_ref, base_branch_ref, update_progress, &upa,
12000 check_cancelled, NULL);
12004 printf("Integrated %s into %s\n", refname, base_refname);
12005 print_update_progress_stats(&upa);
12008 const struct got_error *close_err = got_repo_close(repo);
12013 got_worktree_close(worktree);
12015 const struct got_error *pack_err =
12016 got_repo_pack_fds_close(pack_fds);
12021 free(base_commit_id);
12024 free(base_refname);
12031 fprintf(stderr, "usage: %s merge [-acn] [branch]\n", getprogname());
12035 static const struct got_error *
12036 cmd_merge(int argc, char *argv[])
12038 const struct got_error *error = NULL;
12039 struct got_worktree *worktree = NULL;
12040 struct got_repository *repo = NULL;
12041 struct got_fileindex *fileindex = NULL;
12042 char *cwd = NULL, *id_str = NULL, *author = NULL;
12043 struct got_reference *branch = NULL, *wt_branch = NULL;
12044 struct got_object_id *branch_tip = NULL, *yca_id = NULL;
12045 struct got_object_id *wt_branch_tip = NULL;
12046 int ch, merge_in_progress = 0, abort_merge = 0, continue_merge = 0;
12047 int interrupt_merge = 0;
12048 struct got_update_progress_arg upa;
12049 struct got_object_id *merge_commit_id = NULL;
12050 char *branch_name = NULL;
12051 int *pack_fds = NULL;
12053 memset(&upa, 0, sizeof(upa));
12055 while ((ch = getopt(argc, argv, "acn")) != -1) {
12061 continue_merge = 1;
12064 interrupt_merge = 1;
12076 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
12077 "unveil", NULL) == -1)
12081 if (abort_merge && continue_merge)
12082 option_conflict('a', 'c');
12083 if (abort_merge || continue_merge) {
12086 } else if (argc != 1)
12089 cwd = getcwd(NULL, 0);
12091 error = got_error_from_errno("getcwd");
12095 error = got_repo_pack_fds_open(&pack_fds);
12099 error = got_worktree_open(&worktree, cwd);
12101 if (error->code == GOT_ERR_NOT_WORKTREE)
12102 error = wrap_not_worktree_error(error,
12107 error = got_repo_open(&repo,
12108 worktree ? got_worktree_get_repo_path(worktree) : cwd, NULL,
12113 error = apply_unveil(got_repo_get_path(repo), 0,
12114 worktree ? got_worktree_get_root_path(worktree) : NULL);
12118 error = check_rebase_or_histedit_in_progress(worktree);
12122 error = got_worktree_merge_in_progress(&merge_in_progress, worktree,
12128 if (!merge_in_progress) {
12129 error = got_error(GOT_ERR_NOT_MERGING);
12132 error = got_worktree_merge_continue(&branch_name,
12133 &branch_tip, &fileindex, worktree, repo);
12136 error = got_worktree_merge_abort(worktree, fileindex, repo,
12137 abort_progress, &upa);
12140 printf("Merge of %s aborted\n", branch_name);
12141 goto done; /* nothing else to do */
12144 error = get_author(&author, repo, worktree);
12148 if (continue_merge) {
12149 if (!merge_in_progress) {
12150 error = got_error(GOT_ERR_NOT_MERGING);
12153 error = got_worktree_merge_continue(&branch_name,
12154 &branch_tip, &fileindex, worktree, repo);
12158 error = got_ref_open(&branch, repo, argv[0], 0);
12161 branch_name = strdup(got_ref_get_name(branch));
12162 if (branch_name == NULL) {
12163 error = got_error_from_errno("strdup");
12166 error = got_ref_resolve(&branch_tip, repo, branch);
12171 error = got_ref_open(&wt_branch, repo,
12172 got_worktree_get_head_ref_name(worktree), 0);
12175 error = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
12178 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
12179 wt_branch_tip, branch_tip, 0, repo,
12180 check_cancelled, NULL);
12181 if (error && error->code != GOT_ERR_ANCESTRY)
12184 if (!continue_merge) {
12185 error = check_path_prefix(wt_branch_tip, branch_tip,
12186 got_worktree_get_path_prefix(worktree),
12187 GOT_ERR_MERGE_PATH, repo);
12191 error = check_same_branch(wt_branch_tip, branch,
12194 if (error->code != GOT_ERR_ANCESTRY)
12198 static char msg[512];
12199 snprintf(msg, sizeof(msg),
12200 "cannot create a merge commit because "
12201 "%s is based on %s; %s can be integrated "
12202 "with 'got integrate' instead", branch_name,
12203 got_worktree_get_head_ref_name(worktree),
12205 error = got_error_msg(GOT_ERR_SAME_BRANCH, msg);
12209 error = got_worktree_merge_prepare(&fileindex, worktree,
12214 error = got_worktree_merge_branch(worktree, fileindex,
12215 yca_id, branch_tip, repo, update_progress, &upa,
12216 check_cancelled, NULL);
12219 print_merge_progress_stats(&upa);
12220 if (!upa.did_something) {
12221 error = got_worktree_merge_abort(worktree, fileindex,
12222 repo, abort_progress, &upa);
12225 printf("Already up-to-date\n");
12230 if (interrupt_merge) {
12231 error = got_worktree_merge_postpone(worktree, fileindex);
12234 printf("Merge of %s interrupted on request\n", branch_name);
12235 } else if (upa.conflicts > 0 || upa.missing > 0 ||
12236 upa.not_deleted > 0 || upa.unversioned > 0) {
12237 error = got_worktree_merge_postpone(worktree, fileindex);
12240 if (upa.conflicts > 0 && upa.missing == 0 &&
12241 upa.not_deleted == 0 && upa.unversioned == 0) {
12242 error = got_error_msg(GOT_ERR_CONFLICTS,
12243 "conflicts must be resolved before merging "
12245 } else if (upa.conflicts > 0) {
12246 error = got_error_msg(GOT_ERR_CONFLICTS,
12247 "conflicts must be resolved before merging "
12248 "can continue; changes destined for some "
12249 "files were not yet merged and "
12250 "should be merged manually if required before the "
12251 "merge operation is continued");
12253 error = got_error_msg(GOT_ERR_CONFLICTS,
12254 "changes destined for some "
12255 "files were not yet merged and should be "
12256 "merged manually if required before the "
12257 "merge operation is continued");
12261 error = got_worktree_merge_commit(&merge_commit_id, worktree,
12262 fileindex, author, NULL, 1, branch_tip, branch_name,
12263 repo, continue_merge ? print_status : NULL, NULL);
12266 error = got_worktree_merge_complete(worktree, fileindex, repo);
12269 error = got_object_id_str(&id_str, merge_commit_id);
12272 printf("Merged %s into %s: %s\n", branch_name,
12273 got_worktree_get_head_ref_name(worktree),
12279 free(merge_commit_id);
12285 got_ref_close(branch);
12287 got_ref_close(wt_branch);
12289 got_worktree_close(worktree);
12291 const struct got_error *close_err = got_repo_close(repo);
12296 const struct got_error *pack_err =
12297 got_repo_pack_fds_close(pack_fds);
12307 fprintf(stderr, "usage: %s stage [-lpS] [-F response-script] "
12308 "[path ...]\n", getprogname());
12312 static const struct got_error *
12313 print_stage(void *arg, unsigned char status, unsigned char staged_status,
12314 const char *path, struct got_object_id *blob_id,
12315 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
12316 int dirfd, const char *de_name)
12318 const struct got_error *err = NULL;
12319 char *id_str = NULL;
12321 if (staged_status != GOT_STATUS_ADD &&
12322 staged_status != GOT_STATUS_MODIFY &&
12323 staged_status != GOT_STATUS_DELETE)
12326 if (staged_status == GOT_STATUS_ADD ||
12327 staged_status == GOT_STATUS_MODIFY)
12328 err = got_object_id_str(&id_str, staged_blob_id);
12330 err = got_object_id_str(&id_str, blob_id);
12334 printf("%s %c %s\n", id_str, staged_status, path);
12339 static const struct got_error *
12340 cmd_stage(int argc, char *argv[])
12342 const struct got_error *error = NULL;
12343 struct got_repository *repo = NULL;
12344 struct got_worktree *worktree = NULL;
12346 struct got_pathlist_head paths;
12347 struct got_pathlist_entry *pe;
12348 int ch, list_stage = 0, pflag = 0, allow_bad_symlinks = 0;
12349 FILE *patch_script_file = NULL;
12350 const char *patch_script_path = NULL;
12351 struct choose_patch_arg cpa;
12352 int *pack_fds = NULL;
12354 TAILQ_INIT(&paths);
12356 while ((ch = getopt(argc, argv, "F:lpS")) != -1) {
12359 patch_script_path = optarg;
12368 allow_bad_symlinks = 1;
12380 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
12381 "unveil", NULL) == -1)
12384 if (list_stage && (pflag || patch_script_path))
12385 errx(1, "-l option cannot be used with other options");
12386 if (patch_script_path && !pflag)
12387 errx(1, "-F option can only be used together with -p option");
12389 cwd = getcwd(NULL, 0);
12391 error = got_error_from_errno("getcwd");
12395 error = got_repo_pack_fds_open(&pack_fds);
12399 error = got_worktree_open(&worktree, cwd);
12401 if (error->code == GOT_ERR_NOT_WORKTREE)
12402 error = wrap_not_worktree_error(error, "stage", cwd);
12406 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
12411 if (patch_script_path) {
12412 patch_script_file = fopen(patch_script_path, "re");
12413 if (patch_script_file == NULL) {
12414 error = got_error_from_errno2("fopen",
12415 patch_script_path);
12419 error = apply_unveil(got_repo_get_path(repo), 0,
12420 got_worktree_get_root_path(worktree));
12424 error = check_merge_in_progress(worktree, repo);
12428 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
12433 error = got_worktree_status(worktree, &paths, repo, 0,
12434 print_stage, NULL, check_cancelled, NULL);
12436 cpa.patch_script_file = patch_script_file;
12437 cpa.action = "stage";
12438 error = got_worktree_stage(worktree, &paths,
12439 pflag ? NULL : print_status, NULL,
12440 pflag ? choose_patch : NULL, &cpa,
12441 allow_bad_symlinks, repo);
12444 if (patch_script_file && fclose(patch_script_file) == EOF &&
12446 error = got_error_from_errno2("fclose", patch_script_path);
12448 const struct got_error *close_err = got_repo_close(repo);
12453 got_worktree_close(worktree);
12455 const struct got_error *pack_err =
12456 got_repo_pack_fds_close(pack_fds);
12460 TAILQ_FOREACH(pe, &paths, entry)
12461 free((char *)pe->path);
12462 got_pathlist_free(&paths);
12468 usage_unstage(void)
12470 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
12471 "[path ...]\n", getprogname());
12476 static const struct got_error *
12477 cmd_unstage(int argc, char *argv[])
12479 const struct got_error *error = NULL;
12480 struct got_repository *repo = NULL;
12481 struct got_worktree *worktree = NULL;
12483 struct got_pathlist_head paths;
12484 struct got_pathlist_entry *pe;
12486 struct got_update_progress_arg upa;
12487 FILE *patch_script_file = NULL;
12488 const char *patch_script_path = NULL;
12489 struct choose_patch_arg cpa;
12490 int *pack_fds = NULL;
12492 TAILQ_INIT(&paths);
12494 while ((ch = getopt(argc, argv, "F:p")) != -1) {
12497 patch_script_path = optarg;
12512 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
12513 "unveil", NULL) == -1)
12516 if (patch_script_path && !pflag)
12517 errx(1, "-F option can only be used together with -p option");
12519 cwd = getcwd(NULL, 0);
12521 error = got_error_from_errno("getcwd");
12525 error = got_repo_pack_fds_open(&pack_fds);
12529 error = got_worktree_open(&worktree, cwd);
12531 if (error->code == GOT_ERR_NOT_WORKTREE)
12532 error = wrap_not_worktree_error(error, "unstage", cwd);
12536 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
12541 if (patch_script_path) {
12542 patch_script_file = fopen(patch_script_path, "re");
12543 if (patch_script_file == NULL) {
12544 error = got_error_from_errno2("fopen",
12545 patch_script_path);
12550 error = apply_unveil(got_repo_get_path(repo), 0,
12551 got_worktree_get_root_path(worktree));
12555 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
12559 cpa.patch_script_file = patch_script_file;
12560 cpa.action = "unstage";
12561 memset(&upa, 0, sizeof(upa));
12562 error = got_worktree_unstage(worktree, &paths, update_progress,
12563 &upa, pflag ? choose_patch : NULL, &cpa, repo);
12565 print_merge_progress_stats(&upa);
12567 if (patch_script_file && fclose(patch_script_file) == EOF &&
12569 error = got_error_from_errno2("fclose", patch_script_path);
12571 const struct got_error *close_err = got_repo_close(repo);
12576 got_worktree_close(worktree);
12578 const struct got_error *pack_err =
12579 got_repo_pack_fds_close(pack_fds);
12583 TAILQ_FOREACH(pe, &paths, entry)
12584 free((char *)pe->path);
12585 got_pathlist_free(&paths);
12593 fprintf(stderr, "usage: %s cat [-P] [-c commit] [-r repository-path] "
12594 "arg ...\n", getprogname());
12598 static const struct got_error *
12599 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
12601 const struct got_error *err;
12602 struct got_blob_object *blob;
12605 fd = got_opentempfd();
12607 return got_error_from_errno("got_opentempfd");
12609 err = got_object_open_as_blob(&blob, repo, id, 8192, fd);
12613 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
12615 if (fd != -1 && close(fd) == -1 && err == NULL)
12616 err = got_error_from_errno("close");
12618 got_object_blob_close(blob);
12622 static const struct got_error *
12623 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
12625 const struct got_error *err;
12626 struct got_tree_object *tree;
12629 err = got_object_open_as_tree(&tree, repo, id);
12633 nentries = got_object_tree_get_nentries(tree);
12634 for (i = 0; i < nentries; i++) {
12635 struct got_tree_entry *te;
12637 if (sigint_received || sigpipe_received)
12639 te = got_object_tree_get_entry(tree, i);
12640 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
12643 fprintf(outfile, "%s %.7o %s\n", id_str,
12644 got_tree_entry_get_mode(te),
12645 got_tree_entry_get_name(te));
12649 got_object_tree_close(tree);
12653 static const struct got_error *
12654 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
12656 const struct got_error *err;
12657 struct got_commit_object *commit;
12658 const struct got_object_id_queue *parent_ids;
12659 struct got_object_qid *pid;
12660 char *id_str = NULL;
12661 const char *logmsg = NULL;
12664 err = got_object_open_as_commit(&commit, repo, id);
12668 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
12672 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
12673 parent_ids = got_object_commit_get_parent_ids(commit);
12674 fprintf(outfile, "numparents %d\n",
12675 got_object_commit_get_nparents(commit));
12676 STAILQ_FOREACH(pid, parent_ids, entry) {
12678 err = got_object_id_str(&pid_str, &pid->id);
12681 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
12684 got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
12685 got_object_commit_get_author_gmtoff(commit));
12686 fprintf(outfile, "%s%s %lld %s\n", GOT_COMMIT_LABEL_AUTHOR,
12687 got_object_commit_get_author(commit),
12688 (long long)got_object_commit_get_author_time(commit),
12691 got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
12692 got_object_commit_get_committer_gmtoff(commit));
12693 fprintf(outfile, "%s%s %lld %s\n", GOT_COMMIT_LABEL_COMMITTER,
12694 got_object_commit_get_committer(commit),
12695 (long long)got_object_commit_get_committer_time(commit),
12698 logmsg = got_object_commit_get_logmsg_raw(commit);
12699 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
12700 fprintf(outfile, "%s", logmsg);
12703 got_object_commit_close(commit);
12707 static const struct got_error *
12708 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
12710 const struct got_error *err;
12711 struct got_tag_object *tag;
12712 char *id_str = NULL;
12713 const char *tagmsg = NULL;
12716 err = got_object_open_as_tag(&tag, repo, id);
12720 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
12724 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
12726 switch (got_object_tag_get_object_type(tag)) {
12727 case GOT_OBJ_TYPE_BLOB:
12728 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
12729 GOT_OBJ_LABEL_BLOB);
12731 case GOT_OBJ_TYPE_TREE:
12732 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
12733 GOT_OBJ_LABEL_TREE);
12735 case GOT_OBJ_TYPE_COMMIT:
12736 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
12737 GOT_OBJ_LABEL_COMMIT);
12739 case GOT_OBJ_TYPE_TAG:
12740 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
12741 GOT_OBJ_LABEL_TAG);
12747 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
12748 got_object_tag_get_name(tag));
12750 got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
12751 got_object_tag_get_tagger_gmtoff(tag));
12752 fprintf(outfile, "%s%s %lld %s\n", GOT_TAG_LABEL_TAGGER,
12753 got_object_tag_get_tagger(tag),
12754 (long long)got_object_tag_get_tagger_time(tag),
12757 tagmsg = got_object_tag_get_message(tag);
12758 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
12759 fprintf(outfile, "%s", tagmsg);
12762 got_object_tag_close(tag);
12766 static const struct got_error *
12767 cmd_cat(int argc, char *argv[])
12769 const struct got_error *error;
12770 struct got_repository *repo = NULL;
12771 struct got_worktree *worktree = NULL;
12772 char *cwd = NULL, *repo_path = NULL, *label = NULL;
12773 const char *commit_id_str = NULL;
12774 struct got_object_id *id = NULL, *commit_id = NULL;
12775 struct got_commit_object *commit = NULL;
12776 int ch, obj_type, i, force_path = 0;
12777 struct got_reflist_head refs;
12778 int *pack_fds = NULL;
12783 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
12788 while ((ch = getopt(argc, argv, "c:Pr:")) != -1) {
12791 commit_id_str = optarg;
12797 repo_path = realpath(optarg, NULL);
12798 if (repo_path == NULL)
12799 return got_error_from_errno2("realpath",
12801 got_path_strip_trailing_slashes(repo_path);
12812 cwd = getcwd(NULL, 0);
12814 error = got_error_from_errno("getcwd");
12818 error = got_repo_pack_fds_open(&pack_fds);
12822 if (repo_path == NULL) {
12823 error = got_worktree_open(&worktree, cwd);
12824 if (error && error->code != GOT_ERR_NOT_WORKTREE)
12827 repo_path = strdup(
12828 got_worktree_get_repo_path(worktree));
12829 if (repo_path == NULL) {
12830 error = got_error_from_errno("strdup");
12834 /* Release work tree lock. */
12835 got_worktree_close(worktree);
12840 if (repo_path == NULL) {
12841 repo_path = strdup(cwd);
12842 if (repo_path == NULL)
12843 return got_error_from_errno("strdup");
12846 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
12851 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
12855 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
12859 if (commit_id_str == NULL)
12860 commit_id_str = GOT_REF_HEAD;
12861 error = got_repo_match_object_id(&commit_id, NULL,
12862 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
12866 error = got_object_open_as_commit(&commit, repo, commit_id);
12870 for (i = 0; i < argc; i++) {
12872 error = got_object_id_by_path(&id, repo, commit,
12877 error = got_repo_match_object_id(&id, &label, argv[i],
12878 GOT_OBJ_TYPE_ANY, NULL /* do not resolve tags */,
12881 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
12882 error->code != GOT_ERR_NOT_REF)
12884 error = got_object_id_by_path(&id, repo,
12891 error = got_object_get_type(&obj_type, repo, id);
12895 switch (obj_type) {
12896 case GOT_OBJ_TYPE_BLOB:
12897 error = cat_blob(id, repo, stdout);
12899 case GOT_OBJ_TYPE_TREE:
12900 error = cat_tree(id, repo, stdout);
12902 case GOT_OBJ_TYPE_COMMIT:
12903 error = cat_commit(id, repo, stdout);
12905 case GOT_OBJ_TYPE_TAG:
12906 error = cat_tag(id, repo, stdout);
12909 error = got_error(GOT_ERR_OBJ_TYPE);
12924 got_object_commit_close(commit);
12926 got_worktree_close(worktree);
12928 const struct got_error *close_err = got_repo_close(repo);
12933 const struct got_error *pack_err =
12934 got_repo_pack_fds_close(pack_fds);
12939 got_ref_list_free(&refs);
12946 fprintf(stderr, "usage: %s info [path ...]\n",
12951 static const struct got_error *
12952 print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
12953 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
12954 struct got_object_id *commit_id)
12956 const struct got_error *err = NULL;
12957 char *id_str = NULL;
12959 struct tm mytm, *tm;
12960 struct got_pathlist_head *paths = arg;
12961 struct got_pathlist_entry *pe;
12964 * Clear error indication from any of the path arguments which
12965 * would cause this file index entry to be displayed.
12967 TAILQ_FOREACH(pe, paths, entry) {
12968 if (got_path_cmp(path, pe->path, strlen(path),
12969 pe->path_len) == 0 ||
12970 got_path_is_child(path, pe->path, pe->path_len))
12971 pe->data = NULL; /* no error */
12974 printf(GOT_COMMIT_SEP_STR);
12976 printf("symlink: %s\n", path);
12977 else if (S_ISREG(mode)) {
12978 printf("file: %s\n", path);
12979 printf("mode: %o\n", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
12980 } else if (S_ISDIR(mode))
12981 printf("directory: %s\n", path);
12983 printf("something: %s\n", path);
12985 tm = localtime_r(&mtime, &mytm);
12988 if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) == 0)
12989 return got_error(GOT_ERR_NO_SPACE);
12990 printf("timestamp: %s\n", datebuf);
12993 err = got_object_id_str(&id_str, blob_id);
12996 printf("based on blob: %s\n", id_str);
13000 if (staged_blob_id) {
13001 err = got_object_id_str(&id_str, staged_blob_id);
13004 printf("based on staged blob: %s\n", id_str);
13009 err = got_object_id_str(&id_str, commit_id);
13012 printf("based on commit: %s\n", id_str);
13019 static const struct got_error *
13020 cmd_info(int argc, char *argv[])
13022 const struct got_error *error = NULL;
13023 struct got_worktree *worktree = NULL;
13024 char *cwd = NULL, *id_str = NULL;
13025 struct got_pathlist_head paths;
13026 struct got_pathlist_entry *pe;
13027 char *uuidstr = NULL;
13028 int ch, show_files = 0;
13030 TAILQ_INIT(&paths);
13032 while ((ch = getopt(argc, argv, "")) != -1) {
13044 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
13048 cwd = getcwd(NULL, 0);
13050 error = got_error_from_errno("getcwd");
13054 error = got_worktree_open(&worktree, cwd);
13056 if (error->code == GOT_ERR_NOT_WORKTREE)
13057 error = wrap_not_worktree_error(error, "info", cwd);
13062 /* Remove "wpath cpath proc exec sendfd" promises. */
13063 if (pledge("stdio rpath flock unveil", NULL) == -1)
13066 error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
13071 error = get_worktree_paths_from_argv(&paths, argc, argv,
13078 error = got_object_id_str(&id_str,
13079 got_worktree_get_base_commit_id(worktree));
13083 error = got_worktree_get_uuid(&uuidstr, worktree);
13087 printf("work tree: %s\n", got_worktree_get_root_path(worktree));
13088 printf("work tree base commit: %s\n", id_str);
13089 printf("work tree path prefix: %s\n",
13090 got_worktree_get_path_prefix(worktree));
13091 printf("work tree branch reference: %s\n",
13092 got_worktree_get_head_ref_name(worktree));
13093 printf("work tree UUID: %s\n", uuidstr);
13094 printf("repository: %s\n", got_worktree_get_repo_path(worktree));
13097 struct got_pathlist_entry *pe;
13098 TAILQ_FOREACH(pe, &paths, entry) {
13099 if (pe->path_len == 0)
13102 * Assume this path will fail. This will be corrected
13103 * in print_path_info() in case the path does suceeed.
13105 pe->data = (void *)got_error(GOT_ERR_BAD_PATH);
13107 error = got_worktree_path_info(worktree, &paths,
13108 print_path_info, &paths, check_cancelled, NULL);
13111 TAILQ_FOREACH(pe, &paths, entry) {
13112 if (pe->data != NULL) {
13113 const struct got_error *perr;
13116 error = got_error_fmt(perr->code, "%s",
13124 got_worktree_close(worktree);
13125 TAILQ_FOREACH(pe, &paths, entry)
13126 free((char *)pe->path);
13127 got_pathlist_free(&paths);