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.
20 #include <sys/types.h>
41 #include "got_compat.h"
43 #include "got_version.h"
44 #include "got_error.h"
45 #include "got_object.h"
46 #include "got_reference.h"
47 #include "got_repository.h"
49 #include "got_cancel.h"
50 #include "got_worktree.h"
52 #include "got_commit_graph.h"
53 #include "got_fetch.h"
55 #include "got_blame.h"
56 #include "got_privsep.h"
57 #include "got_opentemp.h"
58 #include "got_gotconfig.h"
60 #include "got_patch.h"
65 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
68 static volatile sig_atomic_t sigint_received;
69 static volatile sig_atomic_t sigpipe_received;
72 catch_sigint(int signo)
78 catch_sigpipe(int signo)
86 const struct got_error *(*cmd_main)(int, char *[]);
87 void (*cmd_usage)(void);
88 const char *cmd_alias;
91 __dead static void usage(int, int);
92 __dead static void usage_import(void);
93 __dead static void usage_clone(void);
94 __dead static void usage_fetch(void);
95 __dead static void usage_checkout(void);
96 __dead static void usage_update(void);
97 __dead static void usage_log(void);
98 __dead static void usage_diff(void);
99 __dead static void usage_blame(void);
100 __dead static void usage_tree(void);
101 __dead static void usage_status(void);
102 __dead static void usage_ref(void);
103 __dead static void usage_branch(void);
104 __dead static void usage_tag(void);
105 __dead static void usage_add(void);
106 __dead static void usage_remove(void);
107 __dead static void usage_patch(void);
108 __dead static void usage_revert(void);
109 __dead static void usage_commit(void);
110 __dead static void usage_send(void);
111 __dead static void usage_cherrypick(void);
112 __dead static void usage_backout(void);
113 __dead static void usage_rebase(void);
114 __dead static void usage_histedit(void);
115 __dead static void usage_integrate(void);
116 __dead static void usage_merge(void);
117 __dead static void usage_stage(void);
118 __dead static void usage_unstage(void);
119 __dead static void usage_cat(void);
120 __dead static void usage_info(void);
122 static const struct got_error* cmd_import(int, char *[]);
123 static const struct got_error* cmd_clone(int, char *[]);
124 static const struct got_error* cmd_fetch(int, char *[]);
125 static const struct got_error* cmd_checkout(int, char *[]);
126 static const struct got_error* cmd_update(int, char *[]);
127 static const struct got_error* cmd_log(int, char *[]);
128 static const struct got_error* cmd_diff(int, char *[]);
129 static const struct got_error* cmd_blame(int, char *[]);
130 static const struct got_error* cmd_tree(int, char *[]);
131 static const struct got_error* cmd_status(int, char *[]);
132 static const struct got_error* cmd_ref(int, char *[]);
133 static const struct got_error* cmd_branch(int, char *[]);
134 static const struct got_error* cmd_tag(int, char *[]);
135 static const struct got_error* cmd_add(int, char *[]);
136 static const struct got_error* cmd_remove(int, char *[]);
137 static const struct got_error* cmd_patch(int, char *[]);
138 static const struct got_error* cmd_revert(int, char *[]);
139 static const struct got_error* cmd_commit(int, char *[]);
140 static const struct got_error* cmd_send(int, char *[]);
141 static const struct got_error* cmd_cherrypick(int, char *[]);
142 static const struct got_error* cmd_backout(int, char *[]);
143 static const struct got_error* cmd_rebase(int, char *[]);
144 static const struct got_error* cmd_histedit(int, char *[]);
145 static const struct got_error* cmd_integrate(int, char *[]);
146 static const struct got_error* cmd_merge(int, char *[]);
147 static const struct got_error* cmd_stage(int, char *[]);
148 static const struct got_error* cmd_unstage(int, char *[]);
149 static const struct got_error* cmd_cat(int, char *[]);
150 static const struct got_error* cmd_info(int, char *[]);
152 static const struct got_cmd got_commands[] = {
153 { "import", cmd_import, usage_import, "im" },
154 { "clone", cmd_clone, usage_clone, "cl" },
155 { "fetch", cmd_fetch, usage_fetch, "fe" },
156 { "checkout", cmd_checkout, usage_checkout, "co" },
157 { "update", cmd_update, usage_update, "up" },
158 { "log", cmd_log, usage_log, "" },
159 { "diff", cmd_diff, usage_diff, "di" },
160 { "blame", cmd_blame, usage_blame, "bl" },
161 { "tree", cmd_tree, usage_tree, "tr" },
162 { "status", cmd_status, usage_status, "st" },
163 { "ref", cmd_ref, usage_ref, "" },
164 { "branch", cmd_branch, usage_branch, "br" },
165 { "tag", cmd_tag, usage_tag, "" },
166 { "add", cmd_add, usage_add, "" },
167 { "remove", cmd_remove, usage_remove, "rm" },
168 { "patch", cmd_patch, usage_patch, "pa" },
169 { "revert", cmd_revert, usage_revert, "rv" },
170 { "commit", cmd_commit, usage_commit, "ci" },
171 { "send", cmd_send, usage_send, "se" },
172 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
173 { "backout", cmd_backout, usage_backout, "bo" },
174 { "rebase", cmd_rebase, usage_rebase, "rb" },
175 { "histedit", cmd_histedit, usage_histedit, "he" },
176 { "integrate", cmd_integrate, usage_integrate,"ig" },
177 { "merge", cmd_merge, usage_merge, "mg" },
178 { "stage", cmd_stage, usage_stage, "sg" },
179 { "unstage", cmd_unstage, usage_unstage, "ug" },
180 { "cat", cmd_cat, usage_cat, "" },
181 { "info", cmd_info, usage_info, "" },
185 list_commands(FILE *fp)
189 fprintf(fp, "commands:");
190 for (i = 0; i < nitems(got_commands); i++) {
191 const struct got_cmd *cmd = &got_commands[i];
192 fprintf(fp, " %s", cmd->cmd_name);
198 option_conflict(char a, char b)
200 errx(1, "-%c and -%c options are mutually exclusive", a, b);
204 main(int argc, char *argv[])
206 const struct got_cmd *cmd;
209 int hflag = 0, Vflag = 0;
210 static const struct option longopts[] = {
211 { "version", no_argument, NULL, 'V' },
215 setlocale(LC_CTYPE, "");
217 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
237 got_version_print_str();
242 usage(hflag, hflag ? 0 : 1);
244 signal(SIGINT, catch_sigint);
245 signal(SIGPIPE, catch_sigpipe);
247 for (i = 0; i < nitems(got_commands); i++) {
248 const struct got_error *error;
250 cmd = &got_commands[i];
252 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
253 strcmp(cmd->cmd_alias, argv[0]) != 0)
259 error = cmd->cmd_main(argc, argv);
260 if (error && error->code != GOT_ERR_CANCELLED &&
261 error->code != GOT_ERR_PRIVSEP_EXIT &&
262 !(sigpipe_received &&
263 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
265 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
266 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
273 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
274 list_commands(stderr);
279 usage(int hflag, int status)
281 FILE *fp = (status == 0) ? stdout : stderr;
283 fprintf(fp, "usage: %s [-h] [-V | --version] command [arg ...]\n",
290 static const struct got_error *
291 get_editor(char **abspath)
293 const struct got_error *err = NULL;
298 editor = getenv("VISUAL");
300 editor = getenv("EDITOR");
303 err = got_path_find_prog(abspath, editor);
308 if (*abspath == NULL) {
309 *abspath = strdup("/bin/ed");
310 if (*abspath == NULL)
311 return got_error_from_errno("strdup");
317 static const struct got_error *
318 apply_unveil(const char *repo_path, int repo_read_only,
319 const char *worktree_path)
321 const struct got_error *err;
324 if (unveil("gmon.out", "rwc") != 0)
325 return got_error_from_errno2("unveil", "gmon.out");
327 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
328 return got_error_from_errno2("unveil", repo_path);
330 if (worktree_path && unveil(worktree_path, "rwc") != 0)
331 return got_error_from_errno2("unveil", worktree_path);
333 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
334 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
336 err = got_privsep_unveil_exec_helpers();
340 if (unveil(NULL, NULL) != 0)
341 return got_error_from_errno("unveil");
349 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
350 "[-r repository-path] [-I pattern] path\n", getprogname());
355 spawn_editor(const char *editor, const char *file)
358 sig_t sighup, sigint, sigquit;
361 sighup = signal(SIGHUP, SIG_IGN);
362 sigint = signal(SIGINT, SIG_IGN);
363 sigquit = signal(SIGQUIT, SIG_IGN);
365 switch (pid = fork()) {
369 execl(editor, editor, file, (char *)NULL);
373 while (waitpid(pid, &st, 0) == -1)
378 (void)signal(SIGHUP, sighup);
379 (void)signal(SIGINT, sigint);
380 (void)signal(SIGQUIT, sigquit);
382 if (!WIFEXITED(st)) {
387 return WEXITSTATUS(st);
390 static const struct got_error *
391 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
392 const char *initial_content, size_t initial_content_len,
393 int require_modification)
395 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 ((linelen = 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);
551 valid_author(const char *author)
554 * Really dumb email address check; we're only doing this to
555 * avoid git's object parser breaking on commits we create.
557 while (*author && *author != '<')
561 while (*author && *author != '@')
565 while (*author && *author != '>')
567 return *author == '>';
570 static const struct got_error *
571 get_author(char **author, struct got_repository *repo,
572 struct got_worktree *worktree)
574 const struct got_error *err = NULL;
575 const char *got_author = NULL, *name, *email;
576 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
581 worktree_conf = got_worktree_get_gotconfig(worktree);
582 repo_conf = got_repo_get_gotconfig(repo);
585 * Priority of potential author information sources, from most
586 * significant to least significant:
587 * 1) work tree's .got/got.conf file
588 * 2) repository's got.conf file
589 * 3) repository's git config file
590 * 4) environment variables
591 * 5) global git config files (in user's home directory or /etc)
595 got_author = got_gotconfig_get_author(worktree_conf);
596 if (got_author == NULL)
597 got_author = got_gotconfig_get_author(repo_conf);
598 if (got_author == NULL) {
599 name = got_repo_get_gitconfig_author_name(repo);
600 email = got_repo_get_gitconfig_author_email(repo);
602 if (asprintf(author, "%s <%s>", name, email) == -1)
603 return got_error_from_errno("asprintf");
607 got_author = getenv("GOT_AUTHOR");
608 if (got_author == NULL) {
609 name = got_repo_get_global_gitconfig_author_name(repo);
610 email = got_repo_get_global_gitconfig_author_email(
613 if (asprintf(author, "%s <%s>", name, email)
615 return got_error_from_errno("asprintf");
618 /* TODO: Look up user in password database? */
619 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
623 *author = strdup(got_author);
625 return got_error_from_errno("strdup");
627 if (!valid_author(*author)) {
628 err = got_error_fmt(GOT_ERR_COMMIT_NO_EMAIL, "%s", *author);
635 static const struct got_error *
636 get_allowed_signers(char **allowed_signers, struct got_repository *repo,
637 struct got_worktree *worktree)
639 const char *got_allowed_signers = NULL;
640 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
642 *allowed_signers = NULL;
645 worktree_conf = got_worktree_get_gotconfig(worktree);
646 repo_conf = got_repo_get_gotconfig(repo);
649 * Priority of potential author information sources, from most
650 * significant to least significant:
651 * 1) work tree's .got/got.conf file
652 * 2) repository's got.conf file
656 got_allowed_signers = got_gotconfig_get_allowed_signers_file(
658 if (got_allowed_signers == NULL)
659 got_allowed_signers = got_gotconfig_get_allowed_signers_file(
662 if (got_allowed_signers) {
663 *allowed_signers = strdup(got_allowed_signers);
664 if (*allowed_signers == NULL)
665 return got_error_from_errno("strdup");
670 static const struct got_error *
671 get_revoked_signers(char **revoked_signers, struct got_repository *repo,
672 struct got_worktree *worktree)
674 const char *got_revoked_signers = NULL;
675 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
677 *revoked_signers = NULL;
680 worktree_conf = got_worktree_get_gotconfig(worktree);
681 repo_conf = got_repo_get_gotconfig(repo);
684 * Priority of potential author information sources, from most
685 * significant to least significant:
686 * 1) work tree's .got/got.conf file
687 * 2) repository's got.conf file
691 got_revoked_signers = got_gotconfig_get_revoked_signers_file(
693 if (got_revoked_signers == NULL)
694 got_revoked_signers = got_gotconfig_get_revoked_signers_file(
697 if (got_revoked_signers) {
698 *revoked_signers = strdup(got_revoked_signers);
699 if (*revoked_signers == NULL)
700 return got_error_from_errno("strdup");
705 static const struct got_error *
706 get_signer_id(char **signer_id, struct got_repository *repo,
707 struct got_worktree *worktree)
709 const char *got_signer_id = NULL;
710 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
715 worktree_conf = got_worktree_get_gotconfig(worktree);
716 repo_conf = got_repo_get_gotconfig(repo);
719 * Priority of potential author information sources, from most
720 * significant to least significant:
721 * 1) work tree's .got/got.conf file
722 * 2) repository's got.conf file
726 got_signer_id = got_gotconfig_get_signer_id(worktree_conf);
727 if (got_signer_id == NULL)
728 got_signer_id = got_gotconfig_get_signer_id(repo_conf);
731 *signer_id = strdup(got_signer_id);
732 if (*signer_id == NULL)
733 return got_error_from_errno("strdup");
738 static const struct got_error *
739 get_gitconfig_path(char **gitconfig_path)
741 const char *homedir = getenv("HOME");
743 *gitconfig_path = NULL;
745 if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
746 return got_error_from_errno("asprintf");
752 static const struct got_error *
753 cmd_import(int argc, char *argv[])
755 const struct got_error *error = NULL;
756 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
757 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
758 const char *branch_name = "main";
759 char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
760 struct got_repository *repo = NULL;
761 struct got_reference *branch_ref = NULL, *head_ref = NULL;
762 struct got_object_id *new_commit_id = NULL;
764 struct got_pathlist_head ignores;
765 struct got_pathlist_entry *pe;
766 int preserve_logmsg = 0;
767 int *pack_fds = NULL;
769 TAILQ_INIT(&ignores);
771 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
774 branch_name = optarg;
777 logmsg = strdup(optarg);
778 if (logmsg == NULL) {
779 error = got_error_from_errno("strdup");
784 repo_path = realpath(optarg, NULL);
785 if (repo_path == NULL) {
786 error = got_error_from_errno2("realpath",
792 if (optarg[0] == '\0')
794 error = got_pathlist_insert(&pe, &ignores, optarg,
809 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
817 if (repo_path == NULL) {
818 repo_path = getcwd(NULL, 0);
819 if (repo_path == NULL)
820 return got_error_from_errno("getcwd");
822 got_path_strip_trailing_slashes(repo_path);
823 error = get_gitconfig_path(&gitconfig_path);
826 error = got_repo_pack_fds_open(&pack_fds);
829 error = got_repo_open(&repo, repo_path, gitconfig_path, pack_fds);
833 error = get_author(&author, repo, NULL);
838 * Don't let the user create a branch name with a leading '-'.
839 * While technically a valid reference name, this case is usually
840 * an unintended typo.
842 if (branch_name[0] == '-')
843 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
845 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
846 error = got_error_from_errno("asprintf");
850 error = got_ref_open(&branch_ref, repo, refname, 0);
852 if (error->code != GOT_ERR_NOT_REF)
855 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
856 "import target branch already exists");
860 path_dir = realpath(argv[0], NULL);
861 if (path_dir == NULL) {
862 error = got_error_from_errno2("realpath", argv[0]);
865 got_path_strip_trailing_slashes(path_dir);
868 * unveil(2) traverses exec(2); if an editor is used we have
869 * to apply unveil after the log message has been written.
871 if (logmsg == NULL || strlen(logmsg) == 0) {
872 error = get_editor(&editor);
876 error = collect_import_msg(&logmsg, &logmsg_path, editor,
879 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
886 if (unveil(path_dir, "r") != 0) {
887 error = got_error_from_errno2("unveil", path_dir);
893 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
900 error = got_repo_import(&new_commit_id, path_dir, logmsg,
901 author, &ignores, repo, import_progress, NULL);
908 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
915 error = got_ref_write(branch_ref, repo);
922 error = got_object_id_str(&id_str, new_commit_id);
929 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
931 if (error->code != GOT_ERR_NOT_REF) {
937 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
945 error = got_ref_write(head_ref, repo);
953 printf("Created branch %s with commit %s\n",
954 got_ref_get_name(branch_ref), id_str);
957 const struct got_error *pack_err =
958 got_repo_pack_fds_close(pack_fds);
962 if (preserve_logmsg) {
963 fprintf(stderr, "%s: log message preserved in %s\n",
964 getprogname(), logmsg_path);
965 } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
966 error = got_error_from_errno2("unlink", logmsg_path);
975 free(gitconfig_path);
977 got_ref_close(branch_ref);
979 got_ref_close(head_ref);
986 fprintf(stderr, "usage: %s clone [-a] [-b branch] [-l] [-m] [-q] [-v] "
987 "[-R reference] repository-url [directory]\n", getprogname());
991 struct got_fetch_progress_arg {
992 char last_scaled_size[FMT_SCALED_STRSIZE];
997 struct got_repository *repo;
1000 int configs_created;
1002 struct got_pathlist_head *symrefs;
1003 struct got_pathlist_head *wanted_branches;
1004 struct got_pathlist_head *wanted_refs;
1008 const char *remote_repo_path;
1009 const char *git_url;
1010 int fetch_all_branches;
1011 int mirror_references;
1015 /* XXX forward declaration */
1016 static const struct got_error *
1017 create_config_files(const char *proto, const char *host, const char *port,
1018 const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1019 int mirror_references, struct got_pathlist_head *symrefs,
1020 struct got_pathlist_head *wanted_branches,
1021 struct got_pathlist_head *wanted_refs, struct got_repository *repo);
1023 static const struct got_error *
1024 fetch_progress(void *arg, const char *message, off_t packfile_size,
1025 int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
1027 const struct got_error *err = NULL;
1028 struct got_fetch_progress_arg *a = arg;
1029 char scaled_size[FMT_SCALED_STRSIZE];
1030 int p_indexed, p_resolved;
1031 int print_size = 0, print_indexed = 0, print_resolved = 0;
1034 * In order to allow a failed clone to be resumed with 'got fetch'
1035 * we try to create configuration files as soon as possible.
1036 * Once the server has sent information about its default branch
1037 * we have all required information.
1039 if (a->create_configs && !a->configs_created &&
1040 !TAILQ_EMPTY(a->config_info.symrefs)) {
1041 err = create_config_files(a->config_info.proto,
1042 a->config_info.host, a->config_info.port,
1043 a->config_info.remote_repo_path,
1044 a->config_info.git_url,
1045 a->config_info.fetch_all_branches,
1046 a->config_info.mirror_references,
1047 a->config_info.symrefs,
1048 a->config_info.wanted_branches,
1049 a->config_info.wanted_refs, a->repo);
1052 a->configs_created = 1;
1055 if (a->verbosity < 0)
1058 if (message && message[0] != '\0') {
1059 printf("\rserver: %s", message);
1064 if (packfile_size > 0 || nobj_indexed > 0) {
1065 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
1066 (a->last_scaled_size[0] == '\0' ||
1067 strcmp(scaled_size, a->last_scaled_size)) != 0) {
1069 if (strlcpy(a->last_scaled_size, scaled_size,
1070 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
1071 return got_error(GOT_ERR_NO_SPACE);
1073 if (nobj_indexed > 0) {
1074 p_indexed = (nobj_indexed * 100) / nobj_total;
1075 if (p_indexed != a->last_p_indexed) {
1076 a->last_p_indexed = p_indexed;
1081 if (nobj_resolved > 0) {
1082 p_resolved = (nobj_resolved * 100) /
1083 (nobj_total - nobj_loose);
1084 if (p_resolved != a->last_p_resolved) {
1085 a->last_p_resolved = p_resolved;
1093 if (print_size || print_indexed || print_resolved)
1096 printf("%*s fetched", FMT_SCALED_STRSIZE - 2, scaled_size);
1098 printf("; indexing %d%%", p_indexed);
1100 printf("; resolving deltas %d%%", p_resolved);
1101 if (print_size || print_indexed || print_resolved)
1107 static const struct got_error *
1108 create_symref(const char *refname, struct got_reference *target_ref,
1109 int verbosity, struct got_repository *repo)
1111 const struct got_error *err;
1112 struct got_reference *head_symref;
1114 err = got_ref_alloc_symref(&head_symref, refname, target_ref);
1118 err = got_ref_write(head_symref, repo);
1119 if (err == NULL && verbosity > 0) {
1120 printf("Created reference %s: %s\n", GOT_REF_HEAD,
1121 got_ref_get_name(target_ref));
1123 got_ref_close(head_symref);
1127 static const struct got_error *
1128 list_remote_refs(struct got_pathlist_head *symrefs,
1129 struct got_pathlist_head *refs)
1131 const struct got_error *err;
1132 struct got_pathlist_entry *pe;
1134 TAILQ_FOREACH(pe, symrefs, entry) {
1135 const char *refname = pe->path;
1136 const char *targetref = pe->data;
1138 printf("%s: %s\n", refname, targetref);
1141 TAILQ_FOREACH(pe, refs, entry) {
1142 const char *refname = pe->path;
1143 struct got_object_id *id = pe->data;
1146 err = got_object_id_str(&id_str, id);
1149 printf("%s: %s\n", refname, id_str);
1156 static const struct got_error *
1157 create_ref(const char *refname, struct got_object_id *id,
1158 int verbosity, struct got_repository *repo)
1160 const struct got_error *err = NULL;
1161 struct got_reference *ref;
1164 err = got_object_id_str(&id_str, id);
1168 err = got_ref_alloc(&ref, refname, id);
1172 err = got_ref_write(ref, repo);
1175 if (err == NULL && verbosity >= 0)
1176 printf("Created reference %s: %s\n", refname, id_str);
1183 match_wanted_ref(const char *refname, const char *wanted_ref)
1185 if (strncmp(refname, "refs/", 5) != 0)
1190 * Prevent fetching of references that won't make any
1191 * sense outside of the remote repository's context.
1193 if (strncmp(refname, "got/", 4) == 0)
1195 if (strncmp(refname, "remotes/", 8) == 0)
1198 if (strncmp(wanted_ref, "refs/", 5) == 0)
1201 /* Allow prefix match. */
1202 if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
1205 /* Allow exact match. */
1206 return (strcmp(refname, wanted_ref) == 0);
1210 is_wanted_ref(struct got_pathlist_head *wanted_refs, const char *refname)
1212 struct got_pathlist_entry *pe;
1214 TAILQ_FOREACH(pe, wanted_refs, entry) {
1215 if (match_wanted_ref(refname, pe->path))
1222 static const struct got_error *
1223 create_wanted_ref(const char *refname, struct got_object_id *id,
1224 const char *remote_repo_name, int verbosity, struct got_repository *repo)
1226 const struct got_error *err;
1227 char *remote_refname;
1229 if (strncmp("refs/", refname, 5) == 0)
1232 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1233 remote_repo_name, refname) == -1)
1234 return got_error_from_errno("asprintf");
1236 err = create_ref(remote_refname, id, verbosity, repo);
1237 free(remote_refname);
1241 static const struct got_error *
1242 create_gotconfig(const char *proto, const char *host, const char *port,
1243 const char *remote_repo_path, const char *default_branch,
1244 int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1245 struct got_pathlist_head *wanted_refs, int mirror_references,
1246 struct got_repository *repo)
1248 const struct got_error *err = NULL;
1249 char *gotconfig_path = NULL;
1250 char *gotconfig = NULL;
1251 FILE *gotconfig_file = NULL;
1252 const char *branchname = NULL;
1253 char *branches = NULL, *refs = NULL;
1256 if (!fetch_all_branches && !TAILQ_EMPTY(wanted_branches)) {
1257 struct got_pathlist_entry *pe;
1258 TAILQ_FOREACH(pe, wanted_branches, entry) {
1260 branchname = pe->path;
1261 if (strncmp(branchname, "refs/heads/", 11) == 0)
1263 if (asprintf(&s, "%s\"%s\" ",
1264 branches ? branches : "", branchname) == -1) {
1265 err = got_error_from_errno("asprintf");
1271 } else if (!fetch_all_branches && default_branch) {
1272 branchname = default_branch;
1273 if (strncmp(branchname, "refs/heads/", 11) == 0)
1275 if (asprintf(&branches, "\"%s\" ", branchname) == -1) {
1276 err = got_error_from_errno("asprintf");
1280 if (!TAILQ_EMPTY(wanted_refs)) {
1281 struct got_pathlist_entry *pe;
1282 TAILQ_FOREACH(pe, wanted_refs, entry) {
1284 const char *refname = pe->path;
1285 if (strncmp(refname, "refs/", 5) == 0)
1287 if (asprintf(&s, "%s\"%s\" ",
1288 refs ? refs : "", refname) == -1) {
1289 err = got_error_from_errno("asprintf");
1297 /* Create got.conf(5). */
1298 gotconfig_path = got_repo_get_path_gotconfig(repo);
1299 if (gotconfig_path == NULL) {
1300 err = got_error_from_errno("got_repo_get_path_gotconfig");
1303 gotconfig_file = fopen(gotconfig_path, "ae");
1304 if (gotconfig_file == NULL) {
1305 err = got_error_from_errno2("fopen", gotconfig_path);
1308 if (asprintf(&gotconfig,
1313 "\trepository \"%s\"\n"
1319 GOT_FETCH_DEFAULT_REMOTE_NAME, host, proto,
1320 port ? "\tport " : "", port ? port : "", port ? "\n" : "",
1321 remote_repo_path, branches ? "\tbranch { " : "",
1322 branches ? branches : "", branches ? "}\n" : "",
1323 refs ? "\treference { " : "", refs ? refs : "", refs ? "}\n" : "",
1324 mirror_references ? "\tmirror_references yes\n" : "",
1325 fetch_all_branches ? "\tfetch_all_branches yes\n" : "") == -1) {
1326 err = got_error_from_errno("asprintf");
1329 n = fwrite(gotconfig, 1, strlen(gotconfig), gotconfig_file);
1330 if (n != strlen(gotconfig)) {
1331 err = got_ferror(gotconfig_file, GOT_ERR_IO);
1336 if (gotconfig_file && fclose(gotconfig_file) == EOF && err == NULL)
1337 err = got_error_from_errno2("fclose", gotconfig_path);
1338 free(gotconfig_path);
1343 static const struct got_error *
1344 create_gitconfig(const char *git_url, const char *default_branch,
1345 int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1346 struct got_pathlist_head *wanted_refs, int mirror_references,
1347 struct got_repository *repo)
1349 const struct got_error *err = NULL;
1350 char *gitconfig_path = NULL;
1351 char *gitconfig = NULL;
1352 FILE *gitconfig_file = NULL;
1353 char *branches = NULL, *refs = NULL;
1354 const char *branchname;
1357 /* Create a config file Git can understand. */
1358 gitconfig_path = got_repo_get_path_gitconfig(repo);
1359 if (gitconfig_path == NULL) {
1360 err = got_error_from_errno("got_repo_get_path_gitconfig");
1363 gitconfig_file = fopen(gitconfig_path, "ae");
1364 if (gitconfig_file == NULL) {
1365 err = got_error_from_errno2("fopen", gitconfig_path);
1368 if (fetch_all_branches) {
1369 if (mirror_references) {
1370 if (asprintf(&branches,
1371 "\tfetch = refs/heads/*:refs/heads/*\n") == -1) {
1372 err = got_error_from_errno("asprintf");
1375 } else if (asprintf(&branches,
1376 "\tfetch = refs/heads/*:refs/remotes/%s/*\n",
1377 GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1378 err = got_error_from_errno("asprintf");
1381 } else if (!TAILQ_EMPTY(wanted_branches)) {
1382 struct got_pathlist_entry *pe;
1383 TAILQ_FOREACH(pe, wanted_branches, entry) {
1385 branchname = pe->path;
1386 if (strncmp(branchname, "refs/heads/", 11) == 0)
1388 if (mirror_references) {
1390 "%s\tfetch = refs/heads/%s:refs/heads/%s\n",
1391 branches ? branches : "",
1392 branchname, branchname) == -1) {
1393 err = got_error_from_errno("asprintf");
1396 } else if (asprintf(&s,
1397 "%s\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1398 branches ? branches : "",
1399 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1400 branchname) == -1) {
1401 err = got_error_from_errno("asprintf");
1409 * If the server specified a default branch, use just that one.
1410 * Otherwise fall back to fetching all branches on next fetch.
1412 if (default_branch) {
1413 branchname = default_branch;
1414 if (strncmp(branchname, "refs/heads/", 11) == 0)
1417 branchname = "*"; /* fall back to all branches */
1418 if (mirror_references) {
1419 if (asprintf(&branches,
1420 "\tfetch = refs/heads/%s:refs/heads/%s\n",
1421 branchname, branchname) == -1) {
1422 err = got_error_from_errno("asprintf");
1425 } else if (asprintf(&branches,
1426 "\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1427 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1428 branchname) == -1) {
1429 err = got_error_from_errno("asprintf");
1433 if (!TAILQ_EMPTY(wanted_refs)) {
1434 struct got_pathlist_entry *pe;
1435 TAILQ_FOREACH(pe, wanted_refs, entry) {
1437 const char *refname = pe->path;
1438 if (strncmp(refname, "refs/", 5) == 0)
1440 if (mirror_references) {
1442 "%s\tfetch = refs/%s:refs/%s\n",
1443 refs ? refs : "", refname, refname) == -1) {
1444 err = got_error_from_errno("asprintf");
1447 } else if (asprintf(&s,
1448 "%s\tfetch = refs/%s:refs/remotes/%s/%s\n",
1450 refname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1452 err = got_error_from_errno("asprintf");
1460 if (asprintf(&gitconfig,
1465 "\tfetch = refs/tags/*:refs/tags/*\n",
1466 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url, branches ? branches : "",
1467 refs ? refs : "") == -1) {
1468 err = got_error_from_errno("asprintf");
1471 n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1472 if (n != strlen(gitconfig)) {
1473 err = got_ferror(gitconfig_file, GOT_ERR_IO);
1477 if (gitconfig_file && fclose(gitconfig_file) == EOF && err == NULL)
1478 err = got_error_from_errno2("fclose", gitconfig_path);
1479 free(gitconfig_path);
1484 static const struct got_error *
1485 create_config_files(const char *proto, const char *host, const char *port,
1486 const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1487 int mirror_references, struct got_pathlist_head *symrefs,
1488 struct got_pathlist_head *wanted_branches,
1489 struct got_pathlist_head *wanted_refs, struct got_repository *repo)
1491 const struct got_error *err = NULL;
1492 const char *default_branch = NULL;
1493 struct got_pathlist_entry *pe;
1496 * If we asked for a set of wanted branches then use the first
1499 if (!TAILQ_EMPTY(wanted_branches)) {
1500 pe = TAILQ_FIRST(wanted_branches);
1501 default_branch = pe->path;
1503 /* First HEAD ref listed by server is the default branch. */
1504 TAILQ_FOREACH(pe, symrefs, entry) {
1505 const char *refname = pe->path;
1506 const char *target = pe->data;
1508 if (strcmp(refname, GOT_REF_HEAD) != 0)
1511 default_branch = target;
1516 /* Create got.conf(5). */
1517 err = create_gotconfig(proto, host, port, remote_repo_path,
1518 default_branch, fetch_all_branches, wanted_branches,
1519 wanted_refs, mirror_references, repo);
1523 /* Create a config file Git can understand. */
1524 return create_gitconfig(git_url, default_branch, fetch_all_branches,
1525 wanted_branches, wanted_refs, mirror_references, repo);
1528 static const struct got_error *
1529 cmd_clone(int argc, char *argv[])
1531 const struct got_error *error = NULL;
1532 const char *uri, *dirname;
1533 char *proto, *host, *port, *repo_name, *server_path;
1534 char *default_destdir = NULL, *id_str = NULL;
1535 const char *repo_path;
1536 struct got_repository *repo = NULL;
1537 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1538 struct got_pathlist_entry *pe;
1539 struct got_object_id *pack_hash = NULL;
1540 int ch, fetchfd = -1, fetchstatus;
1541 pid_t fetchpid = -1;
1542 struct got_fetch_progress_arg fpa;
1543 char *git_url = NULL;
1544 int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
1545 int list_refs_only = 0;
1546 int *pack_fds = NULL;
1549 TAILQ_INIT(&symrefs);
1550 TAILQ_INIT(&wanted_branches);
1551 TAILQ_INIT(&wanted_refs);
1553 while ((ch = getopt(argc, argv, "ab:lmvqR:")) != -1) {
1556 fetch_all_branches = 1;
1559 error = got_pathlist_append(&wanted_branches,
1568 mirror_references = 1;
1573 else if (verbosity < 3)
1580 error = got_pathlist_append(&wanted_refs,
1593 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1594 option_conflict('a', 'b');
1595 if (list_refs_only) {
1596 if (!TAILQ_EMPTY(&wanted_branches))
1597 option_conflict('l', 'b');
1598 if (fetch_all_branches)
1599 option_conflict('l', 'a');
1600 if (mirror_references)
1601 option_conflict('l', 'm');
1602 if (!TAILQ_EMPTY(&wanted_refs))
1603 option_conflict('l', 'R');
1615 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
1620 if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1621 host, port ? ":" : "", port ? port : "",
1622 server_path[0] != '/' ? "/" : "", server_path) == -1) {
1623 error = got_error_from_errno("asprintf");
1627 if (strcmp(proto, "git") == 0) {
1629 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1630 "sendfd dns inet unveil", NULL) == -1)
1633 } else if (strcmp(proto, "git+ssh") == 0 ||
1634 strcmp(proto, "ssh") == 0) {
1636 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1637 "sendfd unveil", NULL) == -1)
1640 } else if (strcmp(proto, "http") == 0 ||
1641 strcmp(proto, "git+http") == 0) {
1642 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1645 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1648 if (dirname == NULL) {
1649 if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1650 error = got_error_from_errno("asprintf");
1653 repo_path = default_destdir;
1655 repo_path = dirname;
1657 if (!list_refs_only) {
1658 error = got_path_mkdir(repo_path);
1660 (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1661 !(error->code == GOT_ERR_ERRNO && errno == EEXIST)))
1663 if (!got_path_dir_is_empty(repo_path)) {
1664 error = got_error_path(repo_path,
1665 GOT_ERR_DIR_NOT_EMPTY);
1670 error = got_dial_apply_unveil(proto);
1674 error = apply_unveil(repo_path, 0, NULL);
1679 printf("Connecting to %s%s%s\n", host,
1680 port ? ":" : "", port ? port : "");
1682 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1683 server_path, verbosity);
1687 if (!list_refs_only) {
1688 error = got_repo_init(repo_path);
1691 error = got_repo_pack_fds_open(&pack_fds);
1694 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
1699 fpa.last_scaled_size[0] = '\0';
1700 fpa.last_p_indexed = -1;
1701 fpa.last_p_resolved = -1;
1702 fpa.verbosity = verbosity;
1703 fpa.create_configs = 1;
1704 fpa.configs_created = 0;
1706 fpa.config_info.symrefs = &symrefs;
1707 fpa.config_info.wanted_branches = &wanted_branches;
1708 fpa.config_info.wanted_refs = &wanted_refs;
1709 fpa.config_info.proto = proto;
1710 fpa.config_info.host = host;
1711 fpa.config_info.port = port;
1712 fpa.config_info.remote_repo_path = server_path;
1713 fpa.config_info.git_url = git_url;
1714 fpa.config_info.fetch_all_branches = fetch_all_branches;
1715 fpa.config_info.mirror_references = mirror_references;
1716 error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1717 GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1718 fetch_all_branches, &wanted_branches, &wanted_refs,
1719 list_refs_only, verbosity, fetchfd, repo,
1720 fetch_progress, &fpa);
1724 if (list_refs_only) {
1725 error = list_remote_refs(&symrefs, &refs);
1729 if (pack_hash == NULL) {
1730 error = got_error_fmt(GOT_ERR_FETCH_FAILED, "%s",
1731 "server sent an empty pack file");
1734 error = got_object_id_str(&id_str, pack_hash);
1738 printf("\nFetched %s.pack\n", id_str);
1741 /* Set up references provided with the pack file. */
1742 TAILQ_FOREACH(pe, &refs, entry) {
1743 const char *refname = pe->path;
1744 struct got_object_id *id = pe->data;
1745 char *remote_refname;
1747 if (is_wanted_ref(&wanted_refs, refname) &&
1748 !mirror_references) {
1749 error = create_wanted_ref(refname, id,
1750 GOT_FETCH_DEFAULT_REMOTE_NAME,
1751 verbosity - 1, repo);
1757 error = create_ref(refname, id, verbosity - 1, repo);
1761 if (mirror_references)
1764 if (strncmp("refs/heads/", refname, 11) != 0)
1767 if (asprintf(&remote_refname,
1768 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1769 refname + 11) == -1) {
1770 error = got_error_from_errno("asprintf");
1773 error = create_ref(remote_refname, id, verbosity - 1, repo);
1774 free(remote_refname);
1779 /* Set the HEAD reference if the server provided one. */
1780 TAILQ_FOREACH(pe, &symrefs, entry) {
1781 struct got_reference *target_ref;
1782 const char *refname = pe->path;
1783 const char *target = pe->data;
1784 char *remote_refname = NULL, *remote_target = NULL;
1786 if (strcmp(refname, GOT_REF_HEAD) != 0)
1789 error = got_ref_open(&target_ref, repo, target, 0);
1791 if (error->code == GOT_ERR_NOT_REF) {
1798 error = create_symref(refname, target_ref, verbosity, repo);
1799 got_ref_close(target_ref);
1803 if (mirror_references)
1806 if (strncmp("refs/heads/", target, 11) != 0)
1809 if (asprintf(&remote_refname,
1810 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1812 error = got_error_from_errno("asprintf");
1815 if (asprintf(&remote_target,
1816 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1817 target + 11) == -1) {
1818 error = got_error_from_errno("asprintf");
1819 free(remote_refname);
1822 error = got_ref_open(&target_ref, repo, remote_target, 0);
1824 free(remote_refname);
1825 free(remote_target);
1826 if (error->code == GOT_ERR_NOT_REF) {
1832 error = create_symref(remote_refname, target_ref,
1833 verbosity - 1, repo);
1834 free(remote_refname);
1835 free(remote_target);
1836 got_ref_close(target_ref);
1842 * We failed to set the HEAD reference. If we asked for
1843 * a set of wanted branches use the first of one of those
1844 * which could be fetched instead.
1846 TAILQ_FOREACH(pe, &wanted_branches, entry) {
1847 const char *target = pe->path;
1848 struct got_reference *target_ref;
1850 error = got_ref_open(&target_ref, repo, target, 0);
1852 if (error->code == GOT_ERR_NOT_REF) {
1859 error = create_symref(GOT_REF_HEAD, target_ref,
1861 got_ref_close(target_ref);
1869 printf("Created %s repository '%s'\n",
1870 mirror_references ? "mirrored" : "cloned", repo_path);
1873 const struct got_error *pack_err =
1874 got_repo_pack_fds_close(pack_fds);
1879 if (kill(fetchpid, SIGTERM) == -1)
1880 error = got_error_from_errno("kill");
1881 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1882 error = got_error_from_errno("waitpid");
1884 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1885 error = got_error_from_errno("close");
1887 const struct got_error *close_err = got_repo_close(repo);
1891 TAILQ_FOREACH(pe, &refs, entry) {
1892 free((void *)pe->path);
1895 got_pathlist_free(&refs);
1896 TAILQ_FOREACH(pe, &symrefs, entry) {
1897 free((void *)pe->path);
1900 got_pathlist_free(&symrefs);
1901 got_pathlist_free(&wanted_branches);
1902 got_pathlist_free(&wanted_refs);
1909 free(default_destdir);
1914 static const struct got_error *
1915 update_ref(struct got_reference *ref, struct got_object_id *new_id,
1916 int replace_tags, int verbosity, struct got_repository *repo)
1918 const struct got_error *err = NULL;
1919 char *new_id_str = NULL;
1920 struct got_object_id *old_id = NULL;
1922 err = got_object_id_str(&new_id_str, new_id);
1926 if (!replace_tags &&
1927 strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1928 err = got_ref_resolve(&old_id, repo, ref);
1931 if (got_object_id_cmp(old_id, new_id) == 0)
1933 if (verbosity >= 0) {
1934 printf("Rejecting update of existing tag %s: %s\n",
1935 got_ref_get_name(ref), new_id_str);
1940 if (got_ref_is_symbolic(ref)) {
1941 if (verbosity >= 0) {
1942 printf("Replacing reference %s: %s\n",
1943 got_ref_get_name(ref),
1944 got_ref_get_symref_target(ref));
1946 err = got_ref_change_symref_to_ref(ref, new_id);
1949 err = got_ref_write(ref, repo);
1953 err = got_ref_resolve(&old_id, repo, ref);
1956 if (got_object_id_cmp(old_id, new_id) == 0)
1959 err = got_ref_change_ref(ref, new_id);
1962 err = got_ref_write(ref, repo);
1968 printf("Updated %s: %s\n", got_ref_get_name(ref),
1976 static const struct got_error *
1977 update_symref(const char *refname, struct got_reference *target_ref,
1978 int verbosity, struct got_repository *repo)
1980 const struct got_error *err = NULL, *unlock_err;
1981 struct got_reference *symref;
1982 int symref_is_locked = 0;
1984 err = got_ref_open(&symref, repo, refname, 1);
1986 if (err->code != GOT_ERR_NOT_REF)
1988 err = got_ref_alloc_symref(&symref, refname, target_ref);
1992 err = got_ref_write(symref, repo);
1997 printf("Created reference %s: %s\n",
1998 got_ref_get_name(symref),
1999 got_ref_get_symref_target(symref));
2001 symref_is_locked = 1;
2003 if (strcmp(got_ref_get_symref_target(symref),
2004 got_ref_get_name(target_ref)) == 0)
2007 err = got_ref_change_symref(symref,
2008 got_ref_get_name(target_ref));
2012 err = got_ref_write(symref, repo);
2017 printf("Updated %s: %s\n", got_ref_get_name(symref),
2018 got_ref_get_symref_target(symref));
2022 if (symref_is_locked) {
2023 unlock_err = got_ref_unlock(symref);
2024 if (unlock_err && err == NULL)
2027 got_ref_close(symref);
2034 fprintf(stderr, "usage: %s fetch [-a] [-b branch] [-d] [-l] "
2035 "[-r repository-path] [-t] [-q] [-v] [-R reference] [-X] "
2036 "[remote-repository-name]\n",
2041 static const struct got_error *
2042 delete_missing_ref(struct got_reference *ref,
2043 int verbosity, struct got_repository *repo)
2045 const struct got_error *err = NULL;
2046 struct got_object_id *id = NULL;
2047 char *id_str = NULL;
2049 if (got_ref_is_symbolic(ref)) {
2050 err = got_ref_delete(ref, repo);
2053 if (verbosity >= 0) {
2054 printf("Deleted %s: %s\n",
2055 got_ref_get_name(ref),
2056 got_ref_get_symref_target(ref));
2059 err = got_ref_resolve(&id, repo, ref);
2062 err = got_object_id_str(&id_str, id);
2066 err = got_ref_delete(ref, repo);
2069 if (verbosity >= 0) {
2070 printf("Deleted %s: %s\n",
2071 got_ref_get_name(ref), id_str);
2080 static const struct got_error *
2081 delete_missing_refs(struct got_pathlist_head *their_refs,
2082 struct got_pathlist_head *their_symrefs,
2083 const struct got_remote_repo *remote,
2084 int verbosity, struct got_repository *repo)
2086 const struct got_error *err = NULL, *unlock_err;
2087 struct got_reflist_head my_refs;
2088 struct got_reflist_entry *re;
2089 struct got_pathlist_entry *pe;
2090 char *remote_namespace = NULL;
2091 char *local_refname = NULL;
2093 TAILQ_INIT(&my_refs);
2095 if (asprintf(&remote_namespace, "refs/remotes/%s/", remote->name)
2097 return got_error_from_errno("asprintf");
2099 err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
2103 TAILQ_FOREACH(re, &my_refs, entry) {
2104 const char *refname = got_ref_get_name(re->ref);
2105 const char *their_refname;
2107 if (remote->mirror_references) {
2108 their_refname = refname;
2110 if (strncmp(refname, remote_namespace,
2111 strlen(remote_namespace)) == 0) {
2112 if (strcmp(refname + strlen(remote_namespace),
2115 if (asprintf(&local_refname, "refs/heads/%s",
2116 refname + strlen(remote_namespace)) == -1) {
2117 err = got_error_from_errno("asprintf");
2120 } else if (strncmp(refname, "refs/tags/", 10) != 0)
2123 their_refname = local_refname;
2126 TAILQ_FOREACH(pe, their_refs, entry) {
2127 if (strcmp(their_refname, pe->path) == 0)
2133 TAILQ_FOREACH(pe, their_symrefs, entry) {
2134 if (strcmp(their_refname, pe->path) == 0)
2140 err = delete_missing_ref(re->ref, verbosity, repo);
2144 if (local_refname) {
2145 struct got_reference *ref;
2146 err = got_ref_open(&ref, repo, local_refname, 1);
2148 if (err->code != GOT_ERR_NOT_REF)
2150 free(local_refname);
2151 local_refname = NULL;
2154 err = delete_missing_ref(ref, verbosity, repo);
2157 unlock_err = got_ref_unlock(ref);
2159 if (unlock_err && err == NULL) {
2164 free(local_refname);
2165 local_refname = NULL;
2169 free(remote_namespace);
2170 free(local_refname);
2174 static const struct got_error *
2175 update_wanted_ref(const char *refname, struct got_object_id *id,
2176 const char *remote_repo_name, int verbosity, struct got_repository *repo)
2178 const struct got_error *err, *unlock_err;
2179 char *remote_refname;
2180 struct got_reference *ref;
2182 if (strncmp("refs/", refname, 5) == 0)
2185 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2186 remote_repo_name, refname) == -1)
2187 return got_error_from_errno("asprintf");
2189 err = got_ref_open(&ref, repo, remote_refname, 1);
2191 if (err->code != GOT_ERR_NOT_REF)
2193 err = create_ref(remote_refname, id, verbosity, repo);
2195 err = update_ref(ref, id, 0, verbosity, repo);
2196 unlock_err = got_ref_unlock(ref);
2197 if (unlock_err && err == NULL)
2202 free(remote_refname);
2206 static const struct got_error *
2207 delete_ref(struct got_repository *repo, struct got_reference *ref)
2209 const struct got_error *err = NULL;
2210 struct got_object_id *id = NULL;
2211 char *id_str = NULL;
2214 if (got_ref_is_symbolic(ref)) {
2215 target = got_ref_get_symref_target(ref);
2217 err = got_ref_resolve(&id, repo, ref);
2220 err = got_object_id_str(&id_str, id);
2226 err = got_ref_delete(ref, repo);
2230 printf("Deleted %s: %s\n", got_ref_get_name(ref), target);
2237 static const struct got_error *
2238 delete_refs_for_remote(struct got_repository *repo, const char *remote_name)
2240 const struct got_error *err = NULL;
2241 struct got_reflist_head refs;
2242 struct got_reflist_entry *re;
2247 if (asprintf(&prefix, "refs/remotes/%s", remote_name) == -1) {
2248 err = got_error_from_errno("asprintf");
2251 err = got_ref_list(&refs, repo, prefix, got_ref_cmp_by_name, NULL);
2255 TAILQ_FOREACH(re, &refs, entry)
2256 delete_ref(repo, re->ref);
2258 got_ref_list_free(&refs);
2262 static const struct got_error *
2263 cmd_fetch(int argc, char *argv[])
2265 const struct got_error *error = NULL, *unlock_err;
2266 char *cwd = NULL, *repo_path = NULL;
2267 const char *remote_name;
2268 char *proto = NULL, *host = NULL, *port = NULL;
2269 char *repo_name = NULL, *server_path = NULL;
2270 const struct got_remote_repo *remotes, *remote = NULL;
2272 char *id_str = NULL;
2273 struct got_repository *repo = NULL;
2274 struct got_worktree *worktree = NULL;
2275 const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
2276 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
2277 struct got_pathlist_entry *pe;
2278 struct got_object_id *pack_hash = NULL;
2279 int i, ch, fetchfd = -1, fetchstatus;
2280 pid_t fetchpid = -1;
2281 struct got_fetch_progress_arg fpa;
2282 int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
2283 int delete_refs = 0, replace_tags = 0, delete_remote = 0;
2284 int *pack_fds = NULL;
2287 TAILQ_INIT(&symrefs);
2288 TAILQ_INIT(&wanted_branches);
2289 TAILQ_INIT(&wanted_refs);
2291 while ((ch = getopt(argc, argv, "ab:dlr:tvqR:X")) != -1) {
2294 fetch_all_branches = 1;
2297 error = got_pathlist_append(&wanted_branches,
2309 repo_path = realpath(optarg, NULL);
2310 if (repo_path == NULL)
2311 return got_error_from_errno2("realpath",
2313 got_path_strip_trailing_slashes(repo_path);
2321 else if (verbosity < 3)
2328 error = got_pathlist_append(&wanted_refs,
2344 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
2345 option_conflict('a', 'b');
2346 if (list_refs_only) {
2347 if (!TAILQ_EMPTY(&wanted_branches))
2348 option_conflict('l', 'b');
2349 if (fetch_all_branches)
2350 option_conflict('l', 'a');
2352 option_conflict('l', 'd');
2354 option_conflict('l', 'X');
2356 if (delete_remote) {
2357 if (fetch_all_branches)
2358 option_conflict('X', 'a');
2359 if (!TAILQ_EMPTY(&wanted_branches))
2360 option_conflict('X', 'b');
2362 option_conflict('X', 'd');
2364 option_conflict('X', 't');
2365 if (!TAILQ_EMPTY(&wanted_refs))
2366 option_conflict('X', 'R');
2371 errx(1, "-X option requires a remote name");
2372 remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
2373 } else if (argc == 1)
2374 remote_name = argv[0];
2378 cwd = getcwd(NULL, 0);
2380 error = got_error_from_errno("getcwd");
2384 error = got_repo_pack_fds_open(&pack_fds);
2388 if (repo_path == NULL) {
2389 error = got_worktree_open(&worktree, cwd);
2390 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2396 strdup(got_worktree_get_repo_path(worktree));
2397 if (repo_path == NULL)
2398 error = got_error_from_errno("strdup");
2402 repo_path = strdup(cwd);
2403 if (repo_path == NULL) {
2404 error = got_error_from_errno("strdup");
2410 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
2414 if (delete_remote) {
2415 error = delete_refs_for_remote(repo, remote_name);
2416 goto done; /* nothing else to do */
2420 worktree_conf = got_worktree_get_gotconfig(worktree);
2421 if (worktree_conf) {
2422 got_gotconfig_get_remotes(&nremotes, &remotes,
2424 for (i = 0; i < nremotes; i++) {
2425 if (strcmp(remotes[i].name, remote_name) == 0) {
2426 remote = &remotes[i];
2432 if (remote == NULL) {
2433 repo_conf = got_repo_get_gotconfig(repo);
2435 got_gotconfig_get_remotes(&nremotes, &remotes,
2437 for (i = 0; i < nremotes; i++) {
2438 if (strcmp(remotes[i].name, remote_name) == 0) {
2439 remote = &remotes[i];
2445 if (remote == NULL) {
2446 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
2447 for (i = 0; i < nremotes; i++) {
2448 if (strcmp(remotes[i].name, remote_name) == 0) {
2449 remote = &remotes[i];
2454 if (remote == NULL) {
2455 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
2459 if (TAILQ_EMPTY(&wanted_branches)) {
2460 if (!fetch_all_branches)
2461 fetch_all_branches = remote->fetch_all_branches;
2462 for (i = 0; i < remote->nfetch_branches; i++) {
2463 got_pathlist_append(&wanted_branches,
2464 remote->fetch_branches[i], NULL);
2467 if (TAILQ_EMPTY(&wanted_refs)) {
2468 for (i = 0; i < remote->nfetch_refs; i++) {
2469 got_pathlist_append(&wanted_refs,
2470 remote->fetch_refs[i], NULL);
2474 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
2475 &repo_name, remote->fetch_url);
2479 if (strcmp(proto, "git") == 0) {
2481 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2482 "sendfd dns inet unveil", NULL) == -1)
2485 } else if (strcmp(proto, "git+ssh") == 0 ||
2486 strcmp(proto, "ssh") == 0) {
2488 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2489 "sendfd unveil", NULL) == -1)
2492 } else if (strcmp(proto, "http") == 0 ||
2493 strcmp(proto, "git+http") == 0) {
2494 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
2497 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
2501 error = got_dial_apply_unveil(proto);
2505 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
2510 printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
2511 port ? ":" : "", port ? port : "");
2513 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
2514 server_path, verbosity);
2518 fpa.last_scaled_size[0] = '\0';
2519 fpa.last_p_indexed = -1;
2520 fpa.last_p_resolved = -1;
2521 fpa.verbosity = verbosity;
2523 fpa.create_configs = 0;
2524 fpa.configs_created = 0;
2525 memset(&fpa.config_info, 0, sizeof(fpa.config_info));
2526 error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
2527 remote->mirror_references, fetch_all_branches, &wanted_branches,
2528 &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
2529 fetch_progress, &fpa);
2533 if (list_refs_only) {
2534 error = list_remote_refs(&symrefs, &refs);
2538 if (pack_hash == NULL) {
2540 printf("Already up-to-date\n");
2541 } else if (verbosity >= 0) {
2542 error = got_object_id_str(&id_str, pack_hash);
2545 printf("\nFetched %s.pack\n", id_str);
2550 /* Update references provided with the pack file. */
2551 TAILQ_FOREACH(pe, &refs, entry) {
2552 const char *refname = pe->path;
2553 struct got_object_id *id = pe->data;
2554 struct got_reference *ref;
2555 char *remote_refname;
2557 if (is_wanted_ref(&wanted_refs, refname) &&
2558 !remote->mirror_references) {
2559 error = update_wanted_ref(refname, id,
2560 remote->name, verbosity, repo);
2566 if (remote->mirror_references ||
2567 strncmp("refs/tags/", refname, 10) == 0) {
2568 error = got_ref_open(&ref, repo, refname, 1);
2570 if (error->code != GOT_ERR_NOT_REF)
2572 error = create_ref(refname, id, verbosity,
2577 error = update_ref(ref, id, replace_tags,
2579 unlock_err = got_ref_unlock(ref);
2580 if (unlock_err && error == NULL)
2586 } else if (strncmp("refs/heads/", refname, 11) == 0) {
2587 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2588 remote_name, refname + 11) == -1) {
2589 error = got_error_from_errno("asprintf");
2593 error = got_ref_open(&ref, repo, remote_refname, 1);
2595 if (error->code != GOT_ERR_NOT_REF)
2597 error = create_ref(remote_refname, id,
2602 error = update_ref(ref, id, replace_tags,
2604 unlock_err = got_ref_unlock(ref);
2605 if (unlock_err && error == NULL)
2612 /* Also create a local branch if none exists yet. */
2613 error = got_ref_open(&ref, repo, refname, 1);
2615 if (error->code != GOT_ERR_NOT_REF)
2617 error = create_ref(refname, id, verbosity,
2622 unlock_err = got_ref_unlock(ref);
2623 if (unlock_err && error == NULL)
2630 error = delete_missing_refs(&refs, &symrefs, remote,
2636 if (!remote->mirror_references) {
2637 /* Update remote HEAD reference if the server provided one. */
2638 TAILQ_FOREACH(pe, &symrefs, entry) {
2639 struct got_reference *target_ref;
2640 const char *refname = pe->path;
2641 const char *target = pe->data;
2642 char *remote_refname = NULL, *remote_target = NULL;
2644 if (strcmp(refname, GOT_REF_HEAD) != 0)
2647 if (strncmp("refs/heads/", target, 11) != 0)
2650 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2651 remote->name, refname) == -1) {
2652 error = got_error_from_errno("asprintf");
2655 if (asprintf(&remote_target, "refs/remotes/%s/%s",
2656 remote->name, target + 11) == -1) {
2657 error = got_error_from_errno("asprintf");
2658 free(remote_refname);
2662 error = got_ref_open(&target_ref, repo, remote_target,
2665 free(remote_refname);
2666 free(remote_target);
2667 if (error->code == GOT_ERR_NOT_REF) {
2673 error = update_symref(remote_refname, target_ref,
2675 free(remote_refname);
2676 free(remote_target);
2677 got_ref_close(target_ref);
2684 if (kill(fetchpid, SIGTERM) == -1)
2685 error = got_error_from_errno("kill");
2686 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
2687 error = got_error_from_errno("waitpid");
2689 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
2690 error = got_error_from_errno("close");
2692 const struct got_error *close_err = got_repo_close(repo);
2697 got_worktree_close(worktree);
2699 const struct got_error *pack_err =
2700 got_repo_pack_fds_close(pack_fds);
2704 TAILQ_FOREACH(pe, &refs, entry) {
2705 free((void *)pe->path);
2708 got_pathlist_free(&refs);
2709 TAILQ_FOREACH(pe, &symrefs, entry) {
2710 free((void *)pe->path);
2713 got_pathlist_free(&symrefs);
2714 got_pathlist_free(&wanted_branches);
2715 got_pathlist_free(&wanted_refs);
2730 usage_checkout(void)
2732 fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
2733 "[-p prefix] [-q] repository-path [worktree-path]\n",
2739 show_worktree_base_ref_warning(void)
2741 fprintf(stderr, "%s: warning: could not create a reference "
2742 "to the work tree's base commit; the commit could be "
2743 "garbage-collected by Git or 'gotadmin cleanup'; making the "
2744 "repository writable and running 'got update' will prevent this\n",
2748 struct got_checkout_progress_arg {
2749 const char *worktree_path;
2750 int had_base_commit_ref_error;
2754 static const struct got_error *
2755 checkout_progress(void *arg, unsigned char status, const char *path)
2757 struct got_checkout_progress_arg *a = arg;
2759 /* Base commit bump happens silently. */
2760 if (status == GOT_STATUS_BUMP_BASE)
2763 if (status == GOT_STATUS_BASE_REF_ERR) {
2764 a->had_base_commit_ref_error = 1;
2768 while (path[0] == '/')
2771 if (a->verbosity >= 0)
2772 printf("%c %s/%s\n", status, a->worktree_path, path);
2777 static const struct got_error *
2778 check_cancelled(void *arg)
2780 if (sigint_received || sigpipe_received)
2781 return got_error(GOT_ERR_CANCELLED);
2785 static const struct got_error *
2786 check_linear_ancestry(struct got_object_id *commit_id,
2787 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2788 struct got_repository *repo)
2790 const struct got_error *err = NULL;
2791 struct got_object_id *yca_id;
2793 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2794 commit_id, base_commit_id, 1, repo, check_cancelled, NULL);
2799 return got_error(GOT_ERR_ANCESTRY);
2802 * Require a straight line of history between the target commit
2803 * and the work tree's base commit.
2805 * Non-linear situations such as this require a rebase:
2807 * (commit) D F (base_commit)
2815 * 'got update' only handles linear cases:
2816 * Update forwards in time: A (base/yca) - B - C - D (commit)
2817 * Update backwards in time: D (base) - C - B - A (commit/yca)
2819 if (allow_forwards_in_time_only) {
2820 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2821 return got_error(GOT_ERR_ANCESTRY);
2822 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2823 got_object_id_cmp(base_commit_id, yca_id) != 0)
2824 return got_error(GOT_ERR_ANCESTRY);
2830 static const struct got_error *
2831 check_same_branch(struct got_object_id *commit_id,
2832 struct got_reference *head_ref, struct got_object_id *yca_id,
2833 struct got_repository *repo)
2835 const struct got_error *err = NULL;
2836 struct got_commit_graph *graph = NULL;
2837 struct got_object_id *head_commit_id = NULL;
2838 int is_same_branch = 0;
2840 err = got_ref_resolve(&head_commit_id, repo, head_ref);
2844 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
2848 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
2853 err = got_commit_graph_open(&graph, "/", 1);
2857 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2858 check_cancelled, NULL);
2863 struct got_object_id *id;
2864 err = got_commit_graph_iter_next(&id, graph, repo,
2865 check_cancelled, NULL);
2867 if (err->code == GOT_ERR_ITER_COMPLETED)
2873 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
2875 if (got_object_id_cmp(id, commit_id) == 0) {
2883 got_commit_graph_close(graph);
2884 free(head_commit_id);
2885 if (!err && !is_same_branch)
2886 err = got_error(GOT_ERR_ANCESTRY);
2890 static const struct got_error *
2891 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2893 static char msg[512];
2894 const char *branch_name;
2896 if (got_ref_is_symbolic(ref))
2897 branch_name = got_ref_get_symref_target(ref);
2899 branch_name = got_ref_get_name(ref);
2901 if (strncmp("refs/heads/", branch_name, 11) == 0)
2904 snprintf(msg, sizeof(msg),
2905 "target commit is not contained in branch '%s'; "
2906 "the branch to use must be specified with -b; "
2907 "if necessary a new branch can be created for "
2908 "this commit with 'got branch -c %s BRANCH_NAME'",
2909 branch_name, commit_id_str);
2911 return got_error_msg(GOT_ERR_ANCESTRY, msg);
2914 static const struct got_error *
2915 cmd_checkout(int argc, char *argv[])
2917 const struct got_error *error = NULL;
2918 struct got_repository *repo = NULL;
2919 struct got_reference *head_ref = NULL, *ref = NULL;
2920 struct got_worktree *worktree = NULL;
2921 char *repo_path = NULL;
2922 char *worktree_path = NULL;
2923 const char *path_prefix = "";
2924 const char *branch_name = GOT_REF_HEAD, *refname = NULL;
2925 char *commit_id_str = NULL;
2926 struct got_object_id *commit_id = NULL;
2928 int ch, same_path_prefix, allow_nonempty = 0, verbosity = 0;
2929 struct got_pathlist_head paths;
2930 struct got_checkout_progress_arg cpa;
2931 int *pack_fds = NULL;
2935 while ((ch = getopt(argc, argv, "b:c:Ep:q")) != -1) {
2938 branch_name = optarg;
2941 commit_id_str = strdup(optarg);
2942 if (commit_id_str == NULL)
2943 return got_error_from_errno("strdup");
2949 path_prefix = optarg;
2964 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2965 "unveil", NULL) == -1)
2969 char *base, *dotgit;
2971 repo_path = realpath(argv[0], NULL);
2972 if (repo_path == NULL)
2973 return got_error_from_errno2("realpath", argv[0]);
2974 cwd = getcwd(NULL, 0);
2976 error = got_error_from_errno("getcwd");
2983 error = got_path_basename(&base, path);
2986 dotgit = strstr(base, ".git");
2989 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
2990 error = got_error_from_errno("asprintf");
2995 } else if (argc == 2) {
2996 repo_path = realpath(argv[0], NULL);
2997 if (repo_path == NULL) {
2998 error = got_error_from_errno2("realpath", argv[0]);
3001 worktree_path = realpath(argv[1], NULL);
3002 if (worktree_path == NULL) {
3003 if (errno != ENOENT) {
3004 error = got_error_from_errno2("realpath",
3008 worktree_path = strdup(argv[1]);
3009 if (worktree_path == NULL) {
3010 error = got_error_from_errno("strdup");
3017 got_path_strip_trailing_slashes(repo_path);
3018 got_path_strip_trailing_slashes(worktree_path);
3020 error = got_repo_pack_fds_open(&pack_fds);
3024 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3028 /* Pre-create work tree path for unveil(2) */
3029 error = got_path_mkdir(worktree_path);
3031 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
3032 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
3034 if (!allow_nonempty &&
3035 !got_path_dir_is_empty(worktree_path)) {
3036 error = got_error_path(worktree_path,
3037 GOT_ERR_DIR_NOT_EMPTY);
3042 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
3046 error = got_ref_open(&head_ref, repo, branch_name, 0);
3050 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
3051 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
3054 error = got_worktree_open(&worktree, worktree_path);
3058 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
3062 if (!same_path_prefix) {
3063 error = got_error(GOT_ERR_PATH_PREFIX);
3067 if (commit_id_str) {
3068 struct got_reflist_head refs;
3070 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3074 error = got_repo_match_object_id(&commit_id, NULL,
3075 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3076 got_ref_list_free(&refs);
3079 error = check_linear_ancestry(commit_id,
3080 got_worktree_get_base_commit_id(worktree), 0, repo);
3081 if (error != NULL) {
3082 if (error->code == GOT_ERR_ANCESTRY) {
3083 error = checkout_ancestry_error(
3084 head_ref, commit_id_str);
3088 error = check_same_branch(commit_id, head_ref, NULL, repo);
3090 if (error->code == GOT_ERR_ANCESTRY) {
3091 error = checkout_ancestry_error(
3092 head_ref, commit_id_str);
3096 error = got_worktree_set_base_commit_id(worktree, repo,
3100 /* Expand potentially abbreviated commit ID string. */
3101 free(commit_id_str);
3102 error = got_object_id_str(&commit_id_str, commit_id);
3106 commit_id = got_object_id_dup(
3107 got_worktree_get_base_commit_id(worktree));
3108 if (commit_id == NULL) {
3109 error = got_error_from_errno("got_object_id_dup");
3112 error = got_object_id_str(&commit_id_str, commit_id);
3117 error = got_pathlist_append(&paths, "", NULL);
3120 cpa.worktree_path = worktree_path;
3121 cpa.had_base_commit_ref_error = 0;
3122 cpa.verbosity = verbosity;
3123 error = got_worktree_checkout_files(worktree, &paths, repo,
3124 checkout_progress, &cpa, check_cancelled, NULL);
3128 if (got_ref_is_symbolic(head_ref)) {
3129 error = got_ref_resolve_symbolic(&ref, repo, head_ref);
3132 refname = got_ref_get_name(ref);
3134 refname = got_ref_get_name(head_ref);
3135 printf("Checked out %s: %s\n", refname, commit_id_str);
3136 printf("Now shut up and hack\n");
3137 if (cpa.had_base_commit_ref_error)
3138 show_worktree_base_ref_warning();
3141 const struct got_error *pack_err =
3142 got_repo_pack_fds_close(pack_fds);
3147 got_ref_close(head_ref);
3150 got_pathlist_free(&paths);
3151 free(commit_id_str);
3154 free(worktree_path);
3159 struct got_update_progress_arg {
3171 print_update_progress_stats(struct got_update_progress_arg *upa)
3173 if (!upa->did_something)
3176 if (upa->conflicts > 0)
3177 printf("Files with new merge conflicts: %d\n", upa->conflicts);
3178 if (upa->obstructed > 0)
3179 printf("File paths obstructed by a non-regular file: %d\n",
3181 if (upa->not_updated > 0)
3182 printf("Files not updated because of existing merge "
3183 "conflicts: %d\n", upa->not_updated);
3187 * The meaning of some status codes differs between merge-style operations and
3188 * update operations. For example, the ! status code means "file was missing"
3189 * if changes were merged into the work tree, and "missing file was restored"
3190 * if the work tree was updated. This function should be used by any operation
3191 * which merges changes into the work tree without updating the work tree.
3194 print_merge_progress_stats(struct got_update_progress_arg *upa)
3196 if (!upa->did_something)
3199 if (upa->conflicts > 0)
3200 printf("Files with new merge conflicts: %d\n", upa->conflicts);
3201 if (upa->obstructed > 0)
3202 printf("File paths obstructed by a non-regular file: %d\n",
3204 if (upa->missing > 0)
3205 printf("Files which had incoming changes but could not be "
3206 "found in the work tree: %d\n", upa->missing);
3207 if (upa->not_deleted > 0)
3208 printf("Files not deleted due to differences in deleted "
3209 "content: %d\n", upa->not_deleted);
3210 if (upa->unversioned > 0)
3211 printf("Files not merged because an unversioned file was "
3212 "found in the work tree: %d\n", upa->unversioned);
3218 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [-q] "
3224 static const struct got_error *
3225 update_progress(void *arg, unsigned char status, const char *path)
3227 struct got_update_progress_arg *upa = arg;
3229 if (status == GOT_STATUS_EXISTS ||
3230 status == GOT_STATUS_BASE_REF_ERR)
3233 upa->did_something = 1;
3235 /* Base commit bump happens silently. */
3236 if (status == GOT_STATUS_BUMP_BASE)
3239 if (status == GOT_STATUS_CONFLICT)
3241 if (status == GOT_STATUS_OBSTRUCTED)
3243 if (status == GOT_STATUS_CANNOT_UPDATE)
3245 if (status == GOT_STATUS_MISSING)
3247 if (status == GOT_STATUS_CANNOT_DELETE)
3249 if (status == GOT_STATUS_UNVERSIONED)
3252 while (path[0] == '/')
3254 if (upa->verbosity >= 0)
3255 printf("%c %s\n", status, path);
3260 static const struct got_error *
3261 switch_head_ref(struct got_reference *head_ref,
3262 struct got_object_id *commit_id, struct got_worktree *worktree,
3263 struct got_repository *repo)
3265 const struct got_error *err = NULL;
3267 int ref_has_moved = 0;
3269 /* Trivial case: switching between two different references. */
3270 if (strcmp(got_ref_get_name(head_ref),
3271 got_worktree_get_head_ref_name(worktree)) != 0) {
3272 printf("Switching work tree from %s to %s\n",
3273 got_worktree_get_head_ref_name(worktree),
3274 got_ref_get_name(head_ref));
3275 return got_worktree_set_head_ref(worktree, head_ref);
3278 err = check_linear_ancestry(commit_id,
3279 got_worktree_get_base_commit_id(worktree), 0, repo);
3281 if (err->code != GOT_ERR_ANCESTRY)
3288 /* Switching to a rebased branch with the same reference name. */
3289 err = got_object_id_str(&base_id_str,
3290 got_worktree_get_base_commit_id(worktree));
3293 printf("Reference %s now points at a different branch\n",
3294 got_worktree_get_head_ref_name(worktree));
3295 printf("Switching work tree from %s to %s\n", base_id_str,
3296 got_worktree_get_head_ref_name(worktree));
3300 static const struct got_error *
3301 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
3303 const struct got_error *err;
3306 err = got_worktree_rebase_in_progress(&in_progress, worktree);
3310 return got_error(GOT_ERR_REBASING);
3312 err = got_worktree_histedit_in_progress(&in_progress, worktree);
3316 return got_error(GOT_ERR_HISTEDIT_BUSY);
3321 static const struct got_error *
3322 check_merge_in_progress(struct got_worktree *worktree,
3323 struct got_repository *repo)
3325 const struct got_error *err;
3328 err = got_worktree_merge_in_progress(&in_progress, worktree, repo);
3332 return got_error(GOT_ERR_MERGE_BUSY);
3337 static const struct got_error *
3338 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
3339 char *argv[], struct got_worktree *worktree)
3341 const struct got_error *err = NULL;
3343 struct got_pathlist_entry *new;
3349 return got_error_from_errno("strdup");
3350 return got_pathlist_append(paths, path, NULL);
3353 for (i = 0; i < argc; i++) {
3354 err = got_worktree_resolve_path(&path, worktree, argv[i]);
3357 err = got_pathlist_insert(&new, paths, path, NULL);
3358 if (err || new == NULL /* duplicate */) {
3368 static const struct got_error *
3369 wrap_not_worktree_error(const struct got_error *orig_err,
3370 const char *cmdname, const char *path)
3372 const struct got_error *err;
3373 struct got_repository *repo;
3374 static char msg[512];
3375 int *pack_fds = NULL;
3377 err = got_repo_pack_fds_open(&pack_fds);
3381 err = got_repo_open(&repo, path, NULL, pack_fds);
3385 snprintf(msg, sizeof(msg),
3386 "'got %s' needs a work tree in addition to a git repository\n"
3387 "Work trees can be checked out from this Git repository with "
3389 "The got(1) manual page contains more information.", cmdname);
3390 err = got_error_msg(GOT_ERR_NOT_WORKTREE, msg);
3391 got_repo_close(repo);
3393 const struct got_error *pack_err =
3394 got_repo_pack_fds_close(pack_fds);
3401 static const struct got_error *
3402 cmd_update(int argc, char *argv[])
3404 const struct got_error *error = NULL;
3405 struct got_repository *repo = NULL;
3406 struct got_worktree *worktree = NULL;
3407 char *worktree_path = NULL;
3408 struct got_object_id *commit_id = NULL;
3409 char *commit_id_str = NULL;
3410 const char *branch_name = NULL;
3411 struct got_reference *head_ref = NULL;
3412 struct got_pathlist_head paths;
3413 struct got_pathlist_entry *pe;
3414 int ch, verbosity = 0;
3415 struct got_update_progress_arg upa;
3416 int *pack_fds = NULL;
3420 while ((ch = getopt(argc, argv, "b:c:q")) != -1) {
3423 branch_name = optarg;
3426 commit_id_str = strdup(optarg);
3427 if (commit_id_str == NULL)
3428 return got_error_from_errno("strdup");
3443 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3444 "unveil", NULL) == -1)
3447 worktree_path = getcwd(NULL, 0);
3448 if (worktree_path == NULL) {
3449 error = got_error_from_errno("getcwd");
3453 error = got_repo_pack_fds_open(&pack_fds);
3457 error = got_worktree_open(&worktree, worktree_path);
3459 if (error->code == GOT_ERR_NOT_WORKTREE)
3460 error = wrap_not_worktree_error(error, "update",
3465 error = check_rebase_or_histedit_in_progress(worktree);
3469 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3474 error = apply_unveil(got_repo_get_path(repo), 0,
3475 got_worktree_get_root_path(worktree));
3479 error = check_merge_in_progress(worktree, repo);
3483 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3487 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
3488 got_worktree_get_head_ref_name(worktree), 0);
3491 if (commit_id_str == NULL) {
3492 error = got_ref_resolve(&commit_id, repo, head_ref);
3495 error = got_object_id_str(&commit_id_str, commit_id);
3499 struct got_reflist_head refs;
3501 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3505 error = got_repo_match_object_id(&commit_id, NULL,
3506 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3507 got_ref_list_free(&refs);
3508 free(commit_id_str);
3509 commit_id_str = NULL;
3512 error = got_object_id_str(&commit_id_str, commit_id);
3518 struct got_object_id *head_commit_id;
3519 TAILQ_FOREACH(pe, &paths, entry) {
3520 if (pe->path_len == 0)
3522 error = got_error_msg(GOT_ERR_BAD_PATH,
3523 "switching between branches requires that "
3524 "the entire work tree gets updated");
3527 error = got_ref_resolve(&head_commit_id, repo, head_ref);
3530 error = check_linear_ancestry(commit_id, head_commit_id, 0,
3532 free(head_commit_id);
3535 error = check_same_branch(commit_id, head_ref, NULL, repo);
3538 error = switch_head_ref(head_ref, commit_id, worktree, repo);
3542 error = check_linear_ancestry(commit_id,
3543 got_worktree_get_base_commit_id(worktree), 0, repo);
3544 if (error != NULL) {
3545 if (error->code == GOT_ERR_ANCESTRY)
3546 error = got_error(GOT_ERR_BRANCH_MOVED);
3549 error = check_same_branch(commit_id, head_ref, NULL, repo);
3554 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
3556 error = got_worktree_set_base_commit_id(worktree, repo,
3562 memset(&upa, 0, sizeof(upa));
3563 upa.verbosity = verbosity;
3564 error = got_worktree_checkout_files(worktree, &paths, repo,
3565 update_progress, &upa, check_cancelled, NULL);
3569 if (upa.did_something) {
3570 printf("Updated to %s: %s\n",
3571 got_worktree_get_head_ref_name(worktree), commit_id_str);
3573 printf("Already up-to-date\n");
3575 print_update_progress_stats(&upa);
3578 const struct got_error *pack_err =
3579 got_repo_pack_fds_close(pack_fds);
3583 free(worktree_path);
3584 TAILQ_FOREACH(pe, &paths, entry)
3585 free((char *)pe->path);
3586 got_pathlist_free(&paths);
3588 free(commit_id_str);
3592 static const struct got_error *
3593 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
3594 const char *path, int diff_context, int ignore_whitespace,
3595 int force_text_diff, struct got_repository *repo, FILE *outfile)
3597 const struct got_error *err = NULL;
3598 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
3599 FILE *f1 = NULL, *f2 = NULL;
3600 int fd1 = -1, fd2 = -1;
3602 fd1 = got_opentempfd();
3604 return got_error_from_errno("got_opentempfd");
3605 fd2 = got_opentempfd();
3607 err = got_error_from_errno("got_opentempfd");
3612 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192,
3618 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192, fd2);
3622 f1 = got_opentemp();
3624 err = got_error_from_errno("got_opentemp");
3627 f2 = got_opentemp();
3629 err = got_error_from_errno("got_opentemp");
3633 while (path[0] == '/')
3635 err = got_diff_blob(NULL, NULL, blob1, blob2, f1, f2, path, path,
3636 GOT_DIFF_ALGORITHM_PATIENCE, diff_context, ignore_whitespace,
3637 force_text_diff, outfile);
3639 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3640 err = got_error_from_errno("close");
3642 got_object_blob_close(blob1);
3643 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3644 err = got_error_from_errno("close");
3645 got_object_blob_close(blob2);
3646 if (f1 && fclose(f1) == EOF && err == NULL)
3647 err = got_error_from_errno("fclose");
3648 if (f2 && fclose(f2) == EOF && err == NULL)
3649 err = got_error_from_errno("fclose");
3653 static const struct got_error *
3654 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
3655 const char *path, int diff_context, int ignore_whitespace,
3656 int force_text_diff, struct got_repository *repo, FILE *outfile)
3658 const struct got_error *err = NULL;
3659 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3660 struct got_diff_blob_output_unidiff_arg arg;
3661 FILE *f1 = NULL, *f2 = NULL;
3662 int fd1 = -1, fd2 = -1;
3665 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3668 fd1 = got_opentempfd();
3670 err = got_error_from_errno("got_opentempfd");
3675 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3679 f1 = got_opentemp();
3681 err = got_error_from_errno("got_opentemp");
3685 f2 = got_opentemp();
3687 err = got_error_from_errno("got_opentemp");
3690 fd2 = got_opentempfd();
3692 err = got_error_from_errno("got_opentempfd");
3695 arg.diff_context = diff_context;
3696 arg.ignore_whitespace = ignore_whitespace;
3697 arg.force_text_diff = force_text_diff;
3698 arg.diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
3699 arg.outfile = outfile;
3700 arg.line_offsets = NULL;
3702 while (path[0] == '/')
3704 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, path, path, repo,
3705 got_diff_blob_output_unidiff, &arg, 1);
3708 got_object_tree_close(tree1);
3710 got_object_tree_close(tree2);
3711 if (f1 && fclose(f1) == EOF && err == NULL)
3712 err = got_error_from_errno("fclose");
3713 if (f2 && fclose(f2) == EOF && err == NULL)
3714 err = got_error_from_errno("fclose");
3715 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3716 err = got_error_from_errno("close");
3717 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3718 err = got_error_from_errno("close");
3722 static const struct got_error *
3723 get_changed_paths(struct got_pathlist_head *paths,
3724 struct got_commit_object *commit, struct got_repository *repo)
3726 const struct got_error *err = NULL;
3727 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3728 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3729 struct got_object_qid *qid;
3731 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3733 struct got_commit_object *pcommit;
3734 err = got_object_open_as_commit(&pcommit, repo,
3739 tree_id1 = got_object_id_dup(
3740 got_object_commit_get_tree_id(pcommit));
3741 if (tree_id1 == NULL) {
3742 got_object_commit_close(pcommit);
3743 return got_error_from_errno("got_object_id_dup");
3745 got_object_commit_close(pcommit);
3750 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3755 tree_id2 = got_object_commit_get_tree_id(commit);
3756 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3760 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
3761 got_diff_tree_collect_changed_paths, paths, 0);
3764 got_object_tree_close(tree1);
3766 got_object_tree_close(tree2);
3771 static const struct got_error *
3772 print_patch(struct got_commit_object *commit, struct got_object_id *id,
3773 const char *path, int diff_context, struct got_repository *repo,
3776 const struct got_error *err = NULL;
3777 struct got_commit_object *pcommit = NULL;
3778 char *id_str1 = NULL, *id_str2 = NULL;
3779 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
3780 struct got_object_qid *qid;
3782 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3784 err = got_object_open_as_commit(&pcommit, repo,
3788 err = got_object_id_str(&id_str1, &qid->id);
3793 err = got_object_id_str(&id_str2, id);
3797 if (path && path[0] != '\0') {
3799 err = got_object_id_by_path(&obj_id2, repo, commit, path);
3803 err = got_object_id_by_path(&obj_id1, repo,
3806 if (err->code != GOT_ERR_NO_TREE_ENTRY) {
3812 err = got_object_get_type(&obj_type, repo, obj_id2);
3818 "diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3819 fprintf(outfile, "commit - %s\n",
3820 id_str1 ? id_str1 : "/dev/null");
3821 fprintf(outfile, "commit + %s\n", id_str2);
3823 case GOT_OBJ_TYPE_BLOB:
3824 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
3825 0, 0, repo, outfile);
3827 case GOT_OBJ_TYPE_TREE:
3828 err = diff_trees(obj_id1, obj_id2, path, diff_context,
3829 0, 0, repo, outfile);
3832 err = got_error(GOT_ERR_OBJ_TYPE);
3838 obj_id2 = got_object_commit_get_tree_id(commit);
3840 obj_id1 = got_object_commit_get_tree_id(pcommit);
3842 "diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3843 fprintf(outfile, "commit - %s\n",
3844 id_str1 ? id_str1 : "/dev/null");
3845 fprintf(outfile, "commit + %s\n", id_str2);
3846 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, 0,
3853 got_object_commit_close(pcommit);
3858 get_datestr(time_t *time, char *datebuf)
3860 struct tm mytm, *tm;
3863 tm = gmtime_r(time, &mytm);
3866 s = asctime_r(tm, datebuf);
3869 p = strchr(s, '\n');
3875 static const struct got_error *
3876 match_commit(int *have_match, struct got_object_id *id,
3877 struct got_commit_object *commit, regex_t *regex)
3879 const struct got_error *err = NULL;
3880 regmatch_t regmatch;
3881 char *id_str = NULL, *logmsg = NULL;
3885 err = got_object_id_str(&id_str, id);
3889 err = got_object_commit_get_logmsg(&logmsg, commit);
3893 if (regexec(regex, got_object_commit_get_author(commit), 1,
3894 ®match, 0) == 0 ||
3895 regexec(regex, got_object_commit_get_committer(commit), 1,
3896 ®match, 0) == 0 ||
3897 regexec(regex, id_str, 1, ®match, 0) == 0 ||
3898 regexec(regex, logmsg, 1, ®match, 0) == 0)
3907 match_changed_paths(int *have_match, struct got_pathlist_head *changed_paths,
3910 regmatch_t regmatch;
3911 struct got_pathlist_entry *pe;
3915 TAILQ_FOREACH(pe, changed_paths, entry) {
3916 if (regexec(regex, pe->path, 1, ®match, 0) == 0) {
3923 static const struct got_error *
3924 match_patch(int *have_match, struct got_commit_object *commit,
3925 struct got_object_id *id, const char *path, int diff_context,
3926 struct got_repository *repo, regex_t *regex, FILE *f)
3928 const struct got_error *err = NULL;
3930 size_t linesize = 0;
3932 regmatch_t regmatch;
3936 err = got_opentemp_truncate(f);
3940 err = print_patch(commit, id, path, diff_context, repo, f);
3944 if (fseeko(f, 0L, SEEK_SET) == -1) {
3945 err = got_error_from_errno("fseeko");
3949 while ((linelen = getline(&line, &linesize, f)) != -1) {
3950 if (regexec(regex, line, 1, ®match, 0) == 0) {
3960 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
3962 static const struct got_error*
3963 build_refs_str(char **refs_str, struct got_reflist_head *refs,
3964 struct got_object_id *id, struct got_repository *repo,
3967 static const struct got_error *err = NULL;
3968 struct got_reflist_entry *re;
3974 TAILQ_FOREACH(re, refs, entry) {
3975 struct got_tag_object *tag = NULL;
3976 struct got_object_id *ref_id;
3979 name = got_ref_get_name(re->ref);
3980 if (strcmp(name, GOT_REF_HEAD) == 0)
3982 if (strncmp(name, "refs/", 5) == 0)
3984 if (strncmp(name, "got/", 4) == 0)
3986 if (strncmp(name, "heads/", 6) == 0)
3988 if (strncmp(name, "remotes/", 8) == 0) {
3992 s = strstr(name, "/" GOT_REF_HEAD);
3993 if (s != NULL && s[strlen(s)] == '\0')
3996 err = got_ref_resolve(&ref_id, repo, re->ref);
3999 if (strncmp(name, "tags/", 5) == 0) {
4000 err = got_object_open_as_tag(&tag, repo, ref_id);
4002 if (err->code != GOT_ERR_OBJ_TYPE) {
4006 /* Ref points at something other than a tag. */
4011 cmp = got_object_id_cmp(tag ?
4012 got_object_tag_get_object_id(tag) : ref_id, id);
4015 got_object_tag_close(tag);
4019 if (asprintf(refs_str, "%s%s%s", s ? s : "",
4020 s ? ", " : "", name) == -1) {
4021 err = got_error_from_errno("asprintf");
4032 static const struct got_error *
4033 print_commit_oneline(struct got_commit_object *commit, struct got_object_id *id,
4034 struct got_repository *repo, struct got_reflist_object_id_map *refs_idmap)
4036 const struct got_error *err = NULL;
4037 char *ref_str = NULL, *id_str = NULL, *logmsg0 = NULL;
4038 char *comma, *s, *nl;
4039 struct got_reflist_head *refs;
4040 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
4042 time_t committer_time;
4044 refs = got_reflist_object_id_map_lookup(refs_idmap, id);
4046 err = build_refs_str(&ref_str, refs, id, repo, 1);
4050 /* Display the first matching ref only. */
4051 if (ref_str && (comma = strchr(ref_str, ',')) != NULL)
4055 if (ref_str == NULL) {
4056 err = got_object_id_str(&id_str, id);
4061 committer_time = got_object_commit_get_committer_time(commit);
4062 if (gmtime_r(&committer_time, &tm) == NULL) {
4063 err = got_error_from_errno("gmtime_r");
4066 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0) {
4067 err = got_error(GOT_ERR_NO_SPACE);
4071 err = got_object_commit_get_logmsg(&logmsg0, commit);
4076 while (isspace((unsigned char)s[0]))
4079 nl = strchr(s, '\n');
4085 printf("%s%-7s %s\n", datebuf, ref_str, s);
4087 printf("%s%.7s %s\n", datebuf, id_str, s);
4089 if (fflush(stdout) != 0 && err == NULL)
4090 err = got_error_from_errno("fflush");
4098 static const struct got_error *
4099 print_commit(struct got_commit_object *commit, struct got_object_id *id,
4100 struct got_repository *repo, const char *path,
4101 struct got_pathlist_head *changed_paths, int show_patch,
4102 int diff_context, struct got_reflist_object_id_map *refs_idmap,
4103 const char *custom_refs_str)
4105 const struct got_error *err = NULL;
4106 char *id_str, *datestr, *logmsg0, *logmsg, *line;
4108 time_t committer_time;
4109 const char *author, *committer;
4110 char *refs_str = NULL;
4112 err = got_object_id_str(&id_str, id);
4116 if (custom_refs_str == NULL) {
4117 struct got_reflist_head *refs;
4118 refs = got_reflist_object_id_map_lookup(refs_idmap, id);
4120 err = build_refs_str(&refs_str, refs, id, repo, 0);
4126 printf(GOT_COMMIT_SEP_STR);
4127 if (custom_refs_str)
4128 printf("commit %s (%s)\n", id_str, custom_refs_str);
4130 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4131 refs_str ? refs_str : "", refs_str ? ")" : "");
4136 printf("from: %s\n", got_object_commit_get_author(commit));
4137 committer_time = got_object_commit_get_committer_time(commit);
4138 datestr = get_datestr(&committer_time, datebuf);
4140 printf("date: %s UTC\n", datestr);
4141 author = got_object_commit_get_author(commit);
4142 committer = got_object_commit_get_committer(commit);
4143 if (strcmp(author, committer) != 0)
4144 printf("via: %s\n", committer);
4145 if (got_object_commit_get_nparents(commit) > 1) {
4146 const struct got_object_id_queue *parent_ids;
4147 struct got_object_qid *qid;
4149 parent_ids = got_object_commit_get_parent_ids(commit);
4150 STAILQ_FOREACH(qid, parent_ids, entry) {
4151 err = got_object_id_str(&id_str, &qid->id);
4154 printf("parent %d: %s\n", n++, id_str);
4160 err = got_object_commit_get_logmsg(&logmsg0, commit);
4166 line = strsep(&logmsg, "\n");
4168 printf(" %s\n", line);
4172 if (changed_paths) {
4173 struct got_pathlist_entry *pe;
4174 TAILQ_FOREACH(pe, changed_paths, entry) {
4175 struct got_diff_changed_path *cp = pe->data;
4176 printf(" %c %s\n", cp->status, pe->path);
4181 err = print_patch(commit, id, path, diff_context, repo, stdout);
4186 if (fflush(stdout) != 0 && err == NULL)
4187 err = got_error_from_errno("fflush");
4194 static const struct got_error *
4195 print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
4196 struct got_repository *repo, const char *path, int show_changed_paths,
4197 int show_patch, const char *search_pattern, int diff_context, int limit,
4198 int log_branches, int reverse_display_order,
4199 struct got_reflist_object_id_map *refs_idmap, int one_line,
4202 const struct got_error *err;
4203 struct got_commit_graph *graph;
4206 struct got_object_id_queue reversed_commits;
4207 struct got_object_qid *qid;
4208 struct got_commit_object *commit;
4209 struct got_pathlist_head changed_paths;
4210 struct got_pathlist_entry *pe;
4212 STAILQ_INIT(&reversed_commits);
4213 TAILQ_INIT(&changed_paths);
4215 if (search_pattern && regcomp(®ex, search_pattern,
4216 REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
4217 return got_error_msg(GOT_ERR_REGEX, search_pattern);
4219 err = got_commit_graph_open(&graph, path, !log_branches);
4222 err = got_commit_graph_iter_start(graph, root_id, repo,
4223 check_cancelled, NULL);
4227 struct got_object_id *id;
4229 if (sigint_received || sigpipe_received)
4232 err = got_commit_graph_iter_next(&id, graph, repo,
4233 check_cancelled, NULL);
4235 if (err->code == GOT_ERR_ITER_COMPLETED)
4242 err = got_object_open_as_commit(&commit, repo, id);
4246 if (show_changed_paths && !reverse_display_order) {
4247 err = get_changed_paths(&changed_paths, commit, repo);
4252 if (search_pattern) {
4253 err = match_commit(&have_match, id, commit, ®ex);
4255 got_object_commit_close(commit);
4258 if (have_match == 0 && show_changed_paths)
4259 match_changed_paths(&have_match,
4260 &changed_paths, ®ex);
4261 if (have_match == 0 && show_patch) {
4262 err = match_patch(&have_match, commit, id,
4263 path, diff_context, repo, ®ex,
4268 if (have_match == 0) {
4269 got_object_commit_close(commit);
4270 TAILQ_FOREACH(pe, &changed_paths, entry) {
4271 free((char *)pe->path);
4274 got_pathlist_free(&changed_paths);
4279 if (reverse_display_order) {
4280 err = got_object_qid_alloc(&qid, id);
4283 STAILQ_INSERT_HEAD(&reversed_commits, qid, entry);
4284 got_object_commit_close(commit);
4287 err = print_commit_oneline(commit, id,
4290 err = print_commit(commit, id, repo, path,
4291 show_changed_paths ? &changed_paths : NULL,
4292 show_patch, diff_context, refs_idmap, NULL);
4293 got_object_commit_close(commit);
4297 if ((limit && --limit == 0) ||
4298 (end_id && got_object_id_cmp(id, end_id) == 0))
4301 TAILQ_FOREACH(pe, &changed_paths, entry) {
4302 free((char *)pe->path);
4305 got_pathlist_free(&changed_paths);
4307 if (reverse_display_order) {
4308 STAILQ_FOREACH(qid, &reversed_commits, entry) {
4309 err = got_object_open_as_commit(&commit, repo,
4313 if (show_changed_paths) {
4314 err = get_changed_paths(&changed_paths,
4320 err = print_commit_oneline(commit, &qid->id,
4323 err = print_commit(commit, &qid->id, repo, path,
4324 show_changed_paths ? &changed_paths : NULL,
4325 show_patch, diff_context, refs_idmap, NULL);
4326 got_object_commit_close(commit);
4329 TAILQ_FOREACH(pe, &changed_paths, entry) {
4330 free((char *)pe->path);
4333 got_pathlist_free(&changed_paths);
4337 while (!STAILQ_EMPTY(&reversed_commits)) {
4338 qid = STAILQ_FIRST(&reversed_commits);
4339 STAILQ_REMOVE_HEAD(&reversed_commits, entry);
4340 got_object_qid_free(qid);
4342 TAILQ_FOREACH(pe, &changed_paths, entry) {
4343 free((char *)pe->path);
4346 got_pathlist_free(&changed_paths);
4349 got_commit_graph_close(graph);
4356 fprintf(stderr, "usage: %s log [-b] [-p] [-P] [-s] [-c commit] "
4357 "[-C number] [ -l N ] [-x commit] [-S search-pattern] "
4358 "[-r repository-path] [-R] [path]\n", getprogname());
4363 get_default_log_limit(void)
4365 const char *got_default_log_limit;
4369 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
4370 if (got_default_log_limit == NULL)
4372 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
4378 static const struct got_error *
4379 cmd_log(int argc, char *argv[])
4381 const struct got_error *error;
4382 struct got_repository *repo = NULL;
4383 struct got_worktree *worktree = NULL;
4384 struct got_object_id *start_id = NULL, *end_id = NULL;
4385 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
4386 const char *start_commit = NULL, *end_commit = NULL;
4387 const char *search_pattern = NULL;
4388 int diff_context = -1, ch;
4389 int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
4390 int reverse_display_order = 0, one_line = 0;
4392 struct got_reflist_head refs;
4393 struct got_reflist_object_id_map *refs_idmap = NULL;
4394 FILE *tmpfile = NULL;
4395 int *pack_fds = NULL;
4400 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4406 limit = get_default_log_limit();
4408 while ((ch = getopt(argc, argv, "bpPc:C:l:r:RsS:x:")) != -1) {
4414 show_changed_paths = 1;
4417 start_commit = optarg;
4420 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4423 errx(1, "number of context lines is %s: %s",
4427 limit = strtonum(optarg, 0, INT_MAX, &errstr);
4429 errx(1, "number of commits is %s: %s",
4436 repo_path = realpath(optarg, NULL);
4437 if (repo_path == NULL)
4438 return got_error_from_errno2("realpath",
4440 got_path_strip_trailing_slashes(repo_path);
4443 reverse_display_order = 1;
4449 search_pattern = optarg;
4452 end_commit = optarg;
4463 if (diff_context == -1)
4465 else if (!show_patch)
4466 errx(1, "-C requires -p");
4468 if (one_line && (show_patch || show_changed_paths))
4469 errx(1, "cannot use -s with -p or -P");
4471 cwd = getcwd(NULL, 0);
4473 error = got_error_from_errno("getcwd");
4477 error = got_repo_pack_fds_open(&pack_fds);
4481 if (repo_path == NULL) {
4482 error = got_worktree_open(&worktree, cwd);
4483 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4490 error = got_worktree_resolve_path(&path, worktree,
4495 path = strdup(argv[0]);
4497 error = got_error_from_errno("strdup");
4501 } else if (argc != 0)
4504 if (repo_path == NULL) {
4505 repo_path = worktree ?
4506 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
4508 if (repo_path == NULL) {
4509 error = got_error_from_errno("strdup");
4513 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4517 error = apply_unveil(got_repo_get_path(repo), 1,
4518 worktree ? got_worktree_get_root_path(worktree) : NULL);
4522 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4526 error = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
4530 if (start_commit == NULL) {
4531 struct got_reference *head_ref;
4532 struct got_commit_object *commit = NULL;
4533 error = got_ref_open(&head_ref, repo,
4534 worktree ? got_worktree_get_head_ref_name(worktree)
4538 error = got_ref_resolve(&start_id, repo, head_ref);
4539 got_ref_close(head_ref);
4542 error = got_object_open_as_commit(&commit, repo,
4546 got_object_commit_close(commit);
4548 error = got_repo_match_object_id(&start_id, NULL,
4549 start_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4553 if (end_commit != NULL) {
4554 error = got_repo_match_object_id(&end_id, NULL,
4555 end_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4562 * If a path was specified on the command line it was resolved
4563 * to a path in the work tree above. Prepend the work tree's
4564 * path prefix to obtain the corresponding in-repository path.
4568 prefix = got_worktree_get_path_prefix(worktree);
4569 if (asprintf(&in_repo_path, "%s%s%s", prefix,
4570 (path[0] != '\0') ? "/" : "", path) == -1) {
4571 error = got_error_from_errno("asprintf");
4576 error = got_repo_map_path(&in_repo_path, repo,
4582 path = in_repo_path;
4586 /* Release work tree lock. */
4587 got_worktree_close(worktree);
4591 if (search_pattern && show_patch) {
4592 tmpfile = got_opentemp();
4593 if (tmpfile == NULL) {
4594 error = got_error_from_errno("got_opentemp");
4599 error = print_commits(start_id, end_id, repo, path ? path : "",
4600 show_changed_paths, show_patch, search_pattern, diff_context,
4601 limit, log_branches, reverse_display_order, refs_idmap, one_line,
4608 got_worktree_close(worktree);
4610 const struct got_error *close_err = got_repo_close(repo);
4615 const struct got_error *pack_err =
4616 got_repo_pack_fds_close(pack_fds);
4621 got_reflist_object_id_map_free(refs_idmap);
4622 if (tmpfile && fclose(tmpfile) == EOF && error == NULL)
4623 error = got_error_from_errno("fclose");
4624 got_ref_list_free(&refs);
4631 fprintf(stderr, "usage: %s diff [-a] [-c commit] [-C number] "
4632 "[-r repository-path] [-s] [-w] [-P] "
4633 "[object1 object2 | path ...]\n", getprogname());
4637 struct print_diff_arg {
4638 struct got_repository *repo;
4639 struct got_worktree *worktree;
4644 enum got_diff_algorithm diff_algo;
4645 int ignore_whitespace;
4646 int force_text_diff;
4652 * Create a file which contains the target path of a symlink so we can feed
4653 * it as content to the diff engine.
4655 static const struct got_error *
4656 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
4657 const char *abspath)
4659 const struct got_error *err = NULL;
4660 char target_path[PATH_MAX];
4661 ssize_t target_len, outlen;
4666 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
4667 if (target_len == -1)
4668 return got_error_from_errno2("readlinkat", abspath);
4670 target_len = readlink(abspath, target_path, PATH_MAX);
4671 if (target_len == -1)
4672 return got_error_from_errno2("readlink", abspath);
4675 *fd = got_opentempfd();
4677 return got_error_from_errno("got_opentempfd");
4679 outlen = write(*fd, target_path, target_len);
4681 err = got_error_from_errno("got_opentempfd");
4685 if (lseek(*fd, 0, SEEK_SET) == -1) {
4686 err = got_error_from_errno2("lseek", abspath);
4697 static const struct got_error *
4698 print_diff(void *arg, unsigned char status, unsigned char staged_status,
4699 const char *path, struct got_object_id *blob_id,
4700 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4701 int dirfd, const char *de_name)
4703 struct print_diff_arg *a = arg;
4704 const struct got_error *err = NULL;
4705 struct got_blob_object *blob1 = NULL;
4706 int fd = -1, fd1 = -1, fd2 = -1;
4708 char *abspath = NULL, *label1 = NULL;
4713 if (a->diff_staged) {
4714 if (staged_status != GOT_STATUS_MODIFY &&
4715 staged_status != GOT_STATUS_ADD &&
4716 staged_status != GOT_STATUS_DELETE)
4719 if (staged_status == GOT_STATUS_DELETE)
4721 if (status == GOT_STATUS_NONEXISTENT)
4722 return got_error_set_errno(ENOENT, path);
4723 if (status != GOT_STATUS_MODIFY &&
4724 status != GOT_STATUS_ADD &&
4725 status != GOT_STATUS_DELETE &&
4726 status != GOT_STATUS_CONFLICT)
4730 err = got_opentemp_truncate(a->f1);
4732 return got_error_from_errno("got_opentemp_truncate");
4733 err = got_opentemp_truncate(a->f2);
4735 return got_error_from_errno("got_opentemp_truncate");
4737 if (!a->header_shown) {
4738 printf("diff %s%s\n", a->diff_staged ? "-s " : "",
4739 got_worktree_get_root_path(a->worktree));
4740 printf("commit - %s\n", a->id_str);
4741 printf("path + %s%s\n",
4742 got_worktree_get_root_path(a->worktree),
4743 a->diff_staged ? " (staged changes)" : "");
4744 a->header_shown = 1;
4747 if (a->diff_staged) {
4748 const char *label1 = NULL, *label2 = NULL;
4749 switch (staged_status) {
4750 case GOT_STATUS_MODIFY:
4754 case GOT_STATUS_ADD:
4757 case GOT_STATUS_DELETE:
4761 return got_error(GOT_ERR_FILE_STATUS);
4763 fd1 = got_opentempfd();
4765 err = got_error_from_errno("got_opentempfd");
4768 fd2 = got_opentempfd();
4770 err = got_error_from_errno("got_opentempfd");
4773 err = got_diff_objects_as_blobs(NULL, NULL, a->f1, a->f2,
4774 fd1, fd2, blob_id, staged_blob_id, label1, label2,
4775 a->diff_algo, a->diff_context, a->ignore_whitespace,
4776 a->force_text_diff, a->repo, stdout);
4780 fd1 = got_opentempfd();
4782 err = got_error_from_errno("got_opentempfd");
4786 if (staged_status == GOT_STATUS_ADD ||
4787 staged_status == GOT_STATUS_MODIFY) {
4789 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
4793 err = got_object_id_str(&id_str, staged_blob_id);
4796 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
4797 err = got_error_from_errno("asprintf");
4802 } else if (status != GOT_STATUS_ADD) {
4803 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192,
4809 if (status != GOT_STATUS_DELETE) {
4810 if (asprintf(&abspath, "%s/%s",
4811 got_worktree_get_root_path(a->worktree), path) == -1) {
4812 err = got_error_from_errno("asprintf");
4817 fd = openat(dirfd, de_name,
4818 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4820 if (!got_err_open_nofollow_on_symlink()) {
4821 err = got_error_from_errno2("openat",
4825 err = get_symlink_target_file(&fd, dirfd,
4831 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4833 if (!got_err_open_nofollow_on_symlink()) {
4834 err = got_error_from_errno2("open",
4838 err = get_symlink_target_file(&fd, dirfd,
4844 if (fstat(fd, &sb) == -1) {
4845 err = got_error_from_errno2("fstat", abspath);
4848 f2 = fdopen(fd, "r");
4850 err = got_error_from_errno2("fdopen", abspath);
4860 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
4866 err = got_diff_blob_file(blob1, a->f1, size1, label1, f2 ? f2 : a->f2,
4867 f2_exists, sb.st_size, path, GOT_DIFF_ALGORITHM_PATIENCE,
4868 a->diff_context, a->ignore_whitespace, a->force_text_diff, stdout);
4870 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
4871 err = got_error_from_errno("close");
4872 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4873 err = got_error_from_errno("close");
4875 got_object_blob_close(blob1);
4876 if (fd != -1 && close(fd) == -1 && err == NULL)
4877 err = got_error_from_errno("close");
4878 if (f2 && fclose(f2) == EOF && err == NULL)
4879 err = got_error_from_errno("fclose");
4884 static const struct got_error *
4885 cmd_diff(int argc, char *argv[])
4887 const struct got_error *error;
4888 struct got_repository *repo = NULL;
4889 struct got_worktree *worktree = NULL;
4890 char *cwd = NULL, *repo_path = NULL;
4891 const char *commit_args[2] = { NULL, NULL };
4892 int ncommit_args = 0;
4893 struct got_object_id *ids[2] = { NULL, NULL };
4894 char *labels[2] = { NULL, NULL };
4895 int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
4896 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch, i;
4897 int force_text_diff = 0, force_path = 0, rflag = 0;
4899 struct got_reflist_head refs;
4900 struct got_pathlist_head paths;
4901 struct got_pathlist_entry *pe;
4902 FILE *f1 = NULL, *f2 = NULL;
4903 int fd1 = -1, fd2 = -1;
4904 int *pack_fds = NULL;
4910 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4915 while ((ch = getopt(argc, argv, "ac:C:r:swP")) != -1) {
4918 force_text_diff = 1;
4921 if (ncommit_args >= 2)
4922 errx(1, "too many -c options used");
4923 commit_args[ncommit_args++] = optarg;
4926 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4929 errx(1, "number of context lines is %s: %s",
4933 repo_path = realpath(optarg, NULL);
4934 if (repo_path == NULL)
4935 return got_error_from_errno2("realpath",
4937 got_path_strip_trailing_slashes(repo_path);
4944 ignore_whitespace = 1;
4958 cwd = getcwd(NULL, 0);
4960 error = got_error_from_errno("getcwd");
4964 error = got_repo_pack_fds_open(&pack_fds);
4968 if (repo_path == NULL) {
4969 error = got_worktree_open(&worktree, cwd);
4970 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4976 strdup(got_worktree_get_repo_path(worktree));
4977 if (repo_path == NULL) {
4978 error = got_error_from_errno("strdup");
4982 repo_path = strdup(cwd);
4983 if (repo_path == NULL) {
4984 error = got_error_from_errno("strdup");
4990 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4995 if (rflag || worktree == NULL || ncommit_args > 0) {
4997 error = got_error_msg(GOT_ERR_NOT_IMPL,
4998 "-P option can only be used when diffing "
5003 error = got_error_msg(GOT_ERR_NOT_IMPL,
5004 "-s option can only be used when diffing "
5010 error = apply_unveil(got_repo_get_path(repo), 1,
5011 worktree ? got_worktree_get_root_path(worktree) : NULL);
5015 if ((!force_path && argc == 2) || ncommit_args > 0) {
5016 int obj_type = (ncommit_args > 0 ?
5017 GOT_OBJ_TYPE_COMMIT : GOT_OBJ_TYPE_ANY);
5018 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5022 for (i = 0; i < (ncommit_args > 0 ? ncommit_args : argc); i++) {
5024 if (ncommit_args > 0)
5025 arg = commit_args[i];
5028 error = got_repo_match_object_id(&ids[i], &labels[i],
5029 arg, obj_type, &refs, repo);
5031 if (error->code != GOT_ERR_NOT_REF &&
5032 error->code != GOT_ERR_NO_OBJ)
5034 if (ncommit_args > 0)
5042 f1 = got_opentemp();
5044 error = got_error_from_errno("got_opentemp");
5048 f2 = got_opentemp();
5050 error = got_error_from_errno("got_opentemp");
5054 if (ncommit_args == 0 && (ids[0] == NULL || ids[1] == NULL)) {
5055 struct print_diff_arg arg;
5058 if (worktree == NULL) {
5059 if (argc == 2 && ids[0] == NULL) {
5060 error = got_error_path(argv[0], GOT_ERR_NO_OBJ);
5062 } else if (argc == 2 && ids[1] == NULL) {
5063 error = got_error_path(argv[1], GOT_ERR_NO_OBJ);
5065 } else if (argc > 0) {
5066 error = got_error_fmt(GOT_ERR_NOT_WORKTREE,
5067 "%s", "specified paths cannot be resolved");
5070 error = got_error(GOT_ERR_NOT_WORKTREE);
5075 error = get_worktree_paths_from_argv(&paths, argc, argv,
5080 error = got_object_id_str(&id_str,
5081 got_worktree_get_base_commit_id(worktree));
5085 arg.worktree = worktree;
5086 arg.diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
5087 arg.diff_context = diff_context;
5088 arg.id_str = id_str;
5089 arg.header_shown = 0;
5090 arg.diff_staged = diff_staged;
5091 arg.ignore_whitespace = ignore_whitespace;
5092 arg.force_text_diff = force_text_diff;
5096 error = got_worktree_status(worktree, &paths, repo, 0,
5097 print_diff, &arg, check_cancelled, NULL);
5102 if (ncommit_args == 1) {
5103 struct got_commit_object *commit;
5104 error = got_object_open_as_commit(&commit, repo, ids[0]);
5108 labels[1] = labels[0];
5110 if (got_object_commit_get_nparents(commit) > 0) {
5111 const struct got_object_id_queue *pids;
5112 struct got_object_qid *pid;
5113 pids = got_object_commit_get_parent_ids(commit);
5114 pid = STAILQ_FIRST(pids);
5115 ids[0] = got_object_id_dup(&pid->id);
5116 if (ids[0] == NULL) {
5117 error = got_error_from_errno(
5118 "got_object_id_dup");
5119 got_object_commit_close(commit);
5122 error = got_object_id_str(&labels[0], ids[0]);
5124 got_object_commit_close(commit);
5129 labels[0] = strdup("/dev/null");
5130 if (labels[0] == NULL) {
5131 error = got_error_from_errno("strdup");
5132 got_object_commit_close(commit);
5137 got_object_commit_close(commit);
5140 if (ncommit_args == 0 && argc > 2) {
5141 error = got_error_msg(GOT_ERR_BAD_PATH,
5142 "path arguments cannot be used when diffing two objects");
5147 error = got_object_get_type(&type1, repo, ids[0]);
5152 error = got_object_get_type(&type2, repo, ids[1]);
5155 if (type1 != GOT_OBJ_TYPE_ANY && type1 != type2) {
5156 error = got_error(GOT_ERR_OBJ_TYPE);
5159 if (type1 == GOT_OBJ_TYPE_BLOB && argc > 0) {
5160 error = got_error_msg(GOT_ERR_OBJ_TYPE,
5161 "path arguments cannot be used when diffing blobs");
5165 for (i = 0; ncommit_args > 0 && i < argc; i++) {
5167 struct got_pathlist_entry *new;
5171 error = got_worktree_resolve_path(&p, worktree,
5175 prefix = got_worktree_get_path_prefix(worktree);
5176 while (prefix[0] == '/')
5178 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5179 (p[0] != '\0' && prefix[0] != '\0') ? "/" : "",
5181 error = got_error_from_errno("asprintf");
5187 char *mapped_path, *s;
5188 error = got_repo_map_path(&mapped_path, repo, argv[i]);
5194 in_repo_path = strdup(s);
5195 if (in_repo_path == NULL) {
5196 error = got_error_from_errno("asprintf");
5203 error = got_pathlist_insert(&new, &paths, in_repo_path, NULL);
5204 if (error || new == NULL /* duplicate */)
5211 /* Release work tree lock. */
5212 got_worktree_close(worktree);
5216 fd1 = got_opentempfd();
5218 error = got_error_from_errno("got_opentempfd");
5222 fd2 = got_opentempfd();
5224 error = got_error_from_errno("got_opentempfd");
5228 switch (type1 == GOT_OBJ_TYPE_ANY ? type2 : type1) {
5229 case GOT_OBJ_TYPE_BLOB:
5230 error = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5231 fd1, fd2, ids[0], ids[1], NULL, NULL,
5232 GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
5233 ignore_whitespace, force_text_diff, repo, stdout);
5235 case GOT_OBJ_TYPE_TREE:
5236 error = got_diff_objects_as_trees(NULL, NULL, f1, f2, fd1, fd2,
5237 ids[0], ids[1], &paths, "", "",
5238 GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
5239 ignore_whitespace, force_text_diff, repo, stdout);
5241 case GOT_OBJ_TYPE_COMMIT:
5242 printf("diff %s %s\n", labels[0], labels[1]);
5243 error = got_diff_objects_as_commits(NULL, NULL, f1, f2,
5244 fd1, fd2, ids[0], ids[1], &paths,
5245 GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
5246 ignore_whitespace, force_text_diff, repo, stdout);
5249 error = got_error(GOT_ERR_OBJ_TYPE);
5257 got_worktree_close(worktree);
5259 const struct got_error *close_err = got_repo_close(repo);
5264 const struct got_error *pack_err =
5265 got_repo_pack_fds_close(pack_fds);
5269 TAILQ_FOREACH(pe, &paths, entry)
5270 free((char *)pe->path);
5271 got_pathlist_free(&paths);
5272 got_ref_list_free(&refs);
5273 if (f1 && fclose(f1) == EOF && error == NULL)
5274 error = got_error_from_errno("fclose");
5275 if (f2 && fclose(f2) == EOF && error == NULL)
5276 error = got_error_from_errno("fclose");
5277 if (fd1 != -1 && close(fd1) == -1 && error == NULL)
5278 error = got_error_from_errno("close");
5279 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
5280 error = got_error_from_errno("close");
5288 "usage: %s blame [-c commit] [-r repository-path] path\n",
5297 char datebuf[11]; /* YYYY-MM-DD + NUL */
5300 struct blame_cb_args {
5301 struct blame_line *lines;
5305 off_t *line_offsets;
5307 struct got_repository *repo;
5310 static const struct got_error *
5311 blame_cb(void *arg, int nlines, int lineno,
5312 struct got_commit_object *commit, struct got_object_id *id)
5314 const struct got_error *err = NULL;
5315 struct blame_cb_args *a = arg;
5316 struct blame_line *bline;
5318 size_t linesize = 0;
5321 time_t committer_time;
5323 if (nlines != a->nlines ||
5324 (lineno != -1 && lineno < 1) || lineno > a->nlines)
5325 return got_error(GOT_ERR_RANGE);
5327 if (sigint_received)
5328 return got_error(GOT_ERR_ITER_COMPLETED);
5331 return NULL; /* no change in this commit */
5333 /* Annotate this line. */
5334 bline = &a->lines[lineno - 1];
5335 if (bline->annotated)
5337 err = got_object_id_str(&bline->id_str, id);
5341 bline->committer = strdup(got_object_commit_get_committer(commit));
5342 if (bline->committer == NULL) {
5343 err = got_error_from_errno("strdup");
5347 committer_time = got_object_commit_get_committer_time(commit);
5348 if (gmtime_r(&committer_time, &tm) == NULL)
5349 return got_error_from_errno("gmtime_r");
5350 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
5352 err = got_error(GOT_ERR_NO_SPACE);
5355 bline->annotated = 1;
5357 /* Print lines annotated so far. */
5358 bline = &a->lines[a->lineno_cur - 1];
5359 if (!bline->annotated)
5362 offset = a->line_offsets[a->lineno_cur - 1];
5363 if (fseeko(a->f, offset, SEEK_SET) == -1) {
5364 err = got_error_from_errno("fseeko");
5368 while (bline->annotated) {
5369 char *smallerthan, *at, *nl, *committer;
5372 if (getline(&line, &linesize, a->f) == -1) {
5374 err = got_error_from_errno("getline");
5378 committer = bline->committer;
5379 smallerthan = strchr(committer, '<');
5380 if (smallerthan && smallerthan[1] != '\0')
5381 committer = smallerthan + 1;
5382 at = strchr(committer, '@');
5385 len = strlen(committer);
5387 committer[8] = '\0';
5389 nl = strchr(line, '\n');
5392 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
5393 bline->id_str, bline->datebuf, committer, line);
5396 bline = &a->lines[a->lineno_cur - 1];
5403 static const struct got_error *
5404 cmd_blame(int argc, char *argv[])
5406 const struct got_error *error;
5407 struct got_repository *repo = NULL;
5408 struct got_worktree *worktree = NULL;
5409 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5410 char *link_target = NULL;
5411 struct got_object_id *obj_id = NULL;
5412 struct got_object_id *commit_id = NULL;
5413 struct got_commit_object *commit = NULL;
5414 struct got_blob_object *blob = NULL;
5415 char *commit_id_str = NULL;
5416 struct blame_cb_args bca;
5417 int ch, obj_type, i, fd1 = -1, fd2 = -1, fd3 = -1;
5419 int *pack_fds = NULL;
5420 FILE *f1 = NULL, *f2 = NULL;
5422 fd1 = got_opentempfd();
5424 return got_error_from_errno("got_opentempfd");
5426 memset(&bca, 0, sizeof(bca));
5429 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5434 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5437 commit_id_str = optarg;
5440 repo_path = realpath(optarg, NULL);
5441 if (repo_path == NULL)
5442 return got_error_from_errno2("realpath",
5444 got_path_strip_trailing_slashes(repo_path);
5460 cwd = getcwd(NULL, 0);
5462 error = got_error_from_errno("getcwd");
5466 error = got_repo_pack_fds_open(&pack_fds);
5470 if (repo_path == NULL) {
5471 error = got_worktree_open(&worktree, cwd);
5472 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5478 strdup(got_worktree_get_repo_path(worktree));
5479 if (repo_path == NULL) {
5480 error = got_error_from_errno("strdup");
5485 repo_path = strdup(cwd);
5486 if (repo_path == NULL) {
5487 error = got_error_from_errno("strdup");
5493 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5498 const char *prefix = got_worktree_get_path_prefix(worktree);
5501 error = got_worktree_resolve_path(&p, worktree, path);
5504 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5505 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
5507 error = got_error_from_errno("asprintf");
5512 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5514 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5517 error = got_repo_map_path(&in_repo_path, repo, path);
5522 if (commit_id_str == NULL) {
5523 struct got_reference *head_ref;
5524 error = got_ref_open(&head_ref, repo, worktree ?
5525 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5528 error = got_ref_resolve(&commit_id, repo, head_ref);
5529 got_ref_close(head_ref);
5533 struct got_reflist_head refs;
5535 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5539 error = got_repo_match_object_id(&commit_id, NULL,
5540 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5541 got_ref_list_free(&refs);
5547 /* Release work tree lock. */
5548 got_worktree_close(worktree);
5552 error = got_object_open_as_commit(&commit, repo, commit_id);
5556 error = got_object_resolve_symlinks(&link_target, in_repo_path,
5561 error = got_object_id_by_path(&obj_id, repo, commit,
5562 link_target ? link_target : in_repo_path);
5566 error = got_object_get_type(&obj_type, repo, obj_id);
5570 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5571 error = got_error_path(link_target ? link_target : in_repo_path,
5576 error = got_object_open_as_blob(&blob, repo, obj_id, 8192, fd1);
5579 bca.f = got_opentemp();
5580 if (bca.f == NULL) {
5581 error = got_error_from_errno("got_opentemp");
5584 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
5585 &bca.line_offsets, bca.f, blob);
5586 if (error || bca.nlines == 0)
5589 /* Don't include \n at EOF in the blame line count. */
5590 if (bca.line_offsets[bca.nlines - 1] == filesize)
5593 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
5594 if (bca.lines == NULL) {
5595 error = got_error_from_errno("calloc");
5599 bca.nlines_prec = 0;
5607 fd2 = got_opentempfd();
5609 error = got_error_from_errno("got_opentempfd");
5612 fd3 = got_opentempfd();
5614 error = got_error_from_errno("got_opentempfd");
5617 f1 = got_opentemp();
5619 error = got_error_from_errno("got_opentemp");
5622 f2 = got_opentemp();
5624 error = got_error_from_errno("got_opentemp");
5627 error = got_blame(link_target ? link_target : in_repo_path, commit_id,
5628 repo, GOT_DIFF_ALGORITHM_PATIENCE, blame_cb, &bca,
5629 check_cancelled, NULL, fd2, fd3, f1, f2);
5638 got_object_commit_close(commit);
5640 if (fd1 != -1 && close(fd1) == -1 && error == NULL)
5641 error = got_error_from_errno("close");
5642 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
5643 error = got_error_from_errno("close");
5644 if (fd3 != -1 && close(fd3) == -1 && error == NULL)
5645 error = got_error_from_errno("close");
5646 if (f1 && fclose(f1) == EOF && error == NULL)
5647 error = got_error_from_errno("fclose");
5648 if (f2 && fclose(f2) == EOF && error == NULL)
5649 error = got_error_from_errno("fclose");
5652 got_object_blob_close(blob);
5654 got_worktree_close(worktree);
5656 const struct got_error *close_err = got_repo_close(repo);
5661 const struct got_error *pack_err =
5662 got_repo_pack_fds_close(pack_fds);
5667 for (i = 0; i < bca.nlines; i++) {
5668 struct blame_line *bline = &bca.lines[i];
5669 free(bline->id_str);
5670 free(bline->committer);
5674 free(bca.line_offsets);
5675 if (bca.f && fclose(bca.f) == EOF && error == NULL)
5676 error = got_error_from_errno("fclose");
5684 "usage: %s tree [-c commit] [-r repository-path] [-iR] [path]\n",
5689 static const struct got_error *
5690 print_entry(struct got_tree_entry *te, const char *id, const char *path,
5691 const char *root_path, struct got_repository *repo)
5693 const struct got_error *err = NULL;
5694 int is_root_path = (strcmp(path, root_path) == 0);
5695 const char *modestr = "";
5696 mode_t mode = got_tree_entry_get_mode(te);
5697 char *link_target = NULL;
5699 path += strlen(root_path);
5700 while (path[0] == '/')
5703 if (got_object_tree_entry_is_submodule(te))
5705 else if (S_ISLNK(mode)) {
5708 err = got_tree_entry_get_symlink_target(&link_target, te, repo);
5711 for (i = 0; i < strlen(link_target); i++) {
5712 if (!isprint((unsigned char)link_target[i]))
5713 link_target[i] = '?';
5718 else if (S_ISDIR(mode))
5720 else if (mode & S_IXUSR)
5723 printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
5724 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
5725 link_target ? " -> ": "", link_target ? link_target : "");
5731 static const struct got_error *
5732 print_tree(const char *path, struct got_commit_object *commit,
5733 int show_ids, int recurse, const char *root_path,
5734 struct got_repository *repo)
5736 const struct got_error *err = NULL;
5737 struct got_object_id *tree_id = NULL;
5738 struct got_tree_object *tree = NULL;
5741 err = got_object_id_by_path(&tree_id, repo, commit, path);
5745 err = got_object_open_as_tree(&tree, repo, tree_id);
5748 nentries = got_object_tree_get_nentries(tree);
5749 for (i = 0; i < nentries; i++) {
5750 struct got_tree_entry *te;
5753 if (sigint_received || sigpipe_received)
5756 te = got_object_tree_get_entry(tree, i);
5759 err = got_object_id_str(&id_str,
5760 got_tree_entry_get_id(te));
5763 if (asprintf(&id, "%s ", id_str) == -1) {
5764 err = got_error_from_errno("asprintf");
5770 err = print_entry(te, id, path, root_path, repo);
5775 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
5777 if (asprintf(&child_path, "%s%s%s", path,
5778 path[0] == '/' && path[1] == '\0' ? "" : "/",
5779 got_tree_entry_get_name(te)) == -1) {
5780 err = got_error_from_errno("asprintf");
5783 err = print_tree(child_path, commit, show_ids, 1,
5792 got_object_tree_close(tree);
5797 static const struct got_error *
5798 cmd_tree(int argc, char *argv[])
5800 const struct got_error *error;
5801 struct got_repository *repo = NULL;
5802 struct got_worktree *worktree = NULL;
5803 const char *path, *refname = NULL;
5804 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5805 struct got_object_id *commit_id = NULL;
5806 struct got_commit_object *commit = NULL;
5807 char *commit_id_str = NULL;
5808 int show_ids = 0, recurse = 0;
5810 int *pack_fds = NULL;
5813 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5818 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
5821 commit_id_str = optarg;
5824 repo_path = realpath(optarg, NULL);
5825 if (repo_path == NULL)
5826 return got_error_from_errno2("realpath",
5828 got_path_strip_trailing_slashes(repo_path);
5852 cwd = getcwd(NULL, 0);
5854 error = got_error_from_errno("getcwd");
5858 error = got_repo_pack_fds_open(&pack_fds);
5862 if (repo_path == NULL) {
5863 error = got_worktree_open(&worktree, cwd);
5864 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5870 strdup(got_worktree_get_repo_path(worktree));
5871 if (repo_path == NULL)
5872 error = got_error_from_errno("strdup");
5876 repo_path = strdup(cwd);
5877 if (repo_path == NULL) {
5878 error = got_error_from_errno("strdup");
5884 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5889 const char *prefix = got_worktree_get_path_prefix(worktree);
5894 error = got_worktree_resolve_path(&p, worktree, path);
5897 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5898 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
5900 error = got_error_from_errno("asprintf");
5905 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5909 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5914 error = got_repo_map_path(&in_repo_path, repo, path);
5919 if (commit_id_str == NULL) {
5920 struct got_reference *head_ref;
5922 refname = got_worktree_get_head_ref_name(worktree);
5924 refname = GOT_REF_HEAD;
5925 error = got_ref_open(&head_ref, repo, refname, 0);
5928 error = got_ref_resolve(&commit_id, repo, head_ref);
5929 got_ref_close(head_ref);
5933 struct got_reflist_head refs;
5935 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5939 error = got_repo_match_object_id(&commit_id, NULL,
5940 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5941 got_ref_list_free(&refs);
5947 /* Release work tree lock. */
5948 got_worktree_close(worktree);
5952 error = got_object_open_as_commit(&commit, repo, commit_id);
5956 error = print_tree(in_repo_path, commit, show_ids, recurse,
5957 in_repo_path, repo);
5964 got_object_commit_close(commit);
5966 got_worktree_close(worktree);
5968 const struct got_error *close_err = got_repo_close(repo);
5973 const struct got_error *pack_err =
5974 got_repo_pack_fds_close(pack_fds);
5984 fprintf(stderr, "usage: %s status [-I] [-s status-codes ] "
5985 "[-S status-codes] [path ...]\n", getprogname());
5989 struct got_status_arg {
5994 static const struct got_error *
5995 print_status(void *arg, unsigned char status, unsigned char staged_status,
5996 const char *path, struct got_object_id *blob_id,
5997 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5998 int dirfd, const char *de_name)
6000 struct got_status_arg *st = arg;
6002 if (status == staged_status && (status == GOT_STATUS_DELETE))
6003 status = GOT_STATUS_NO_CHANGE;
6004 if (st != NULL && st->status_codes) {
6005 size_t ncodes = strlen(st->status_codes);
6008 for (i = 0; i < ncodes ; i++) {
6010 if (status == st->status_codes[i] ||
6011 staged_status == st->status_codes[i]) {
6016 if (status == st->status_codes[i] ||
6017 staged_status == st->status_codes[i])
6022 if (st->suppress && j == 0)
6029 printf("%c%c %s\n", status, staged_status, path);
6033 static const struct got_error *
6034 cmd_status(int argc, char *argv[])
6036 const struct got_error *error = NULL;
6037 struct got_repository *repo = NULL;
6038 struct got_worktree *worktree = NULL;
6039 struct got_status_arg st;
6041 struct got_pathlist_head paths;
6042 struct got_pathlist_entry *pe;
6043 int ch, i, no_ignores = 0;
6044 int *pack_fds = NULL;
6048 memset(&st, 0, sizeof(st));
6049 st.status_codes = NULL;
6052 while ((ch = getopt(argc, argv, "Is:S:")) != -1) {
6058 if (st.status_codes != NULL && st.suppress == 0)
6059 option_conflict('S', 's');
6063 for (i = 0; i < strlen(optarg); i++) {
6064 switch (optarg[i]) {
6065 case GOT_STATUS_MODIFY:
6066 case GOT_STATUS_ADD:
6067 case GOT_STATUS_DELETE:
6068 case GOT_STATUS_CONFLICT:
6069 case GOT_STATUS_MISSING:
6070 case GOT_STATUS_OBSTRUCTED:
6071 case GOT_STATUS_UNVERSIONED:
6072 case GOT_STATUS_MODE_CHANGE:
6073 case GOT_STATUS_NONEXISTENT:
6076 errx(1, "invalid status code '%c'",
6080 if (ch == 's' && st.suppress)
6081 option_conflict('s', 'S');
6082 st.status_codes = optarg;
6094 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6098 cwd = getcwd(NULL, 0);
6100 error = got_error_from_errno("getcwd");
6104 error = got_repo_pack_fds_open(&pack_fds);
6108 error = got_worktree_open(&worktree, cwd);
6110 if (error->code == GOT_ERR_NOT_WORKTREE)
6111 error = wrap_not_worktree_error(error, "status", cwd);
6115 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6120 error = apply_unveil(got_repo_get_path(repo), 1,
6121 got_worktree_get_root_path(worktree));
6125 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6129 error = got_worktree_status(worktree, &paths, repo, no_ignores,
6130 print_status, &st, check_cancelled, NULL);
6133 const struct got_error *pack_err =
6134 got_repo_pack_fds_close(pack_fds);
6139 TAILQ_FOREACH(pe, &paths, entry)
6140 free((char *)pe->path);
6141 got_pathlist_free(&paths);
6150 "usage: %s ref [-r repository] [-l] [-t] [-c object] "
6151 "[-s reference] [-d] [name]\n",
6156 static const struct got_error *
6157 list_refs(struct got_repository *repo, const char *refname, int sort_by_time)
6159 static const struct got_error *err = NULL;
6160 struct got_reflist_head refs;
6161 struct got_reflist_entry *re;
6164 err = got_ref_list(&refs, repo, refname, sort_by_time ?
6165 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6170 TAILQ_FOREACH(re, &refs, entry) {
6172 refstr = got_ref_to_str(re->ref);
6173 if (refstr == NULL) {
6174 err = got_error_from_errno("got_ref_to_str");
6177 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
6181 got_ref_list_free(&refs);
6185 static const struct got_error *
6186 delete_ref_by_name(struct got_repository *repo, const char *refname)
6188 const struct got_error *err;
6189 struct got_reference *ref;
6191 err = got_ref_open(&ref, repo, refname, 0);
6195 err = delete_ref(repo, ref);
6200 static const struct got_error *
6201 add_ref(struct got_repository *repo, const char *refname, const char *target)
6203 const struct got_error *err = NULL;
6204 struct got_object_id *id = NULL;
6205 struct got_reference *ref = NULL;
6206 struct got_reflist_head refs;
6209 * Don't let the user create a reference name with a leading '-'.
6210 * While technically a valid reference name, this case is usually
6211 * an unintended typo.
6213 if (refname[0] == '-')
6214 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
6217 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
6220 err = got_repo_match_object_id(&id, NULL, target, GOT_OBJ_TYPE_ANY,
6222 got_ref_list_free(&refs);
6226 err = got_ref_alloc(&ref, refname, id);
6230 err = got_ref_write(ref, repo);
6238 static const struct got_error *
6239 add_symref(struct got_repository *repo, const char *refname, const char *target)
6241 const struct got_error *err = NULL;
6242 struct got_reference *ref = NULL;
6243 struct got_reference *target_ref = NULL;
6246 * Don't let the user create a reference name with a leading '-'.
6247 * While technically a valid reference name, this case is usually
6248 * an unintended typo.
6250 if (refname[0] == '-')
6251 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
6253 err = got_ref_open(&target_ref, repo, target, 0);
6257 err = got_ref_alloc_symref(&ref, refname, target_ref);
6261 err = got_ref_write(ref, repo);
6264 got_ref_close(target_ref);
6270 static const struct got_error *
6271 cmd_ref(int argc, char *argv[])
6273 const struct got_error *error = NULL;
6274 struct got_repository *repo = NULL;
6275 struct got_worktree *worktree = NULL;
6276 char *cwd = NULL, *repo_path = NULL;
6277 int ch, do_list = 0, do_delete = 0, sort_by_time = 0;
6278 const char *obj_arg = NULL, *symref_target= NULL;
6279 char *refname = NULL;
6280 int *pack_fds = NULL;
6282 while ((ch = getopt(argc, argv, "c:dr:ls:t")) != -1) {
6291 repo_path = realpath(optarg, NULL);
6292 if (repo_path == NULL)
6293 return got_error_from_errno2("realpath",
6295 got_path_strip_trailing_slashes(repo_path);
6301 symref_target = optarg;
6312 if (obj_arg && do_list)
6313 option_conflict('c', 'l');
6314 if (obj_arg && do_delete)
6315 option_conflict('c', 'd');
6316 if (obj_arg && symref_target)
6317 option_conflict('c', 's');
6318 if (symref_target && do_delete)
6319 option_conflict('s', 'd');
6320 if (symref_target && do_list)
6321 option_conflict('s', 'l');
6322 if (do_delete && do_list)
6323 option_conflict('d', 'l');
6324 if (sort_by_time && !do_list)
6325 errx(1, "-t option requires -l option");
6331 if (argc != 0 && argc != 1)
6334 refname = strdup(argv[0]);
6335 if (refname == NULL) {
6336 error = got_error_from_errno("strdup");
6343 refname = strdup(argv[0]);
6344 if (refname == NULL) {
6345 error = got_error_from_errno("strdup");
6351 got_path_strip_trailing_slashes(refname);
6354 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6355 "sendfd unveil", NULL) == -1)
6358 cwd = getcwd(NULL, 0);
6360 error = got_error_from_errno("getcwd");
6364 error = got_repo_pack_fds_open(&pack_fds);
6368 if (repo_path == NULL) {
6369 error = got_worktree_open(&worktree, cwd);
6370 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6376 strdup(got_worktree_get_repo_path(worktree));
6377 if (repo_path == NULL)
6378 error = got_error_from_errno("strdup");
6382 repo_path = strdup(cwd);
6383 if (repo_path == NULL) {
6384 error = got_error_from_errno("strdup");
6390 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6396 /* Remove "cpath" promise. */
6397 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6403 error = apply_unveil(got_repo_get_path(repo), do_list,
6404 worktree ? got_worktree_get_root_path(worktree) : NULL);
6409 error = list_refs(repo, refname, sort_by_time);
6411 error = delete_ref_by_name(repo, refname);
6412 else if (symref_target)
6413 error = add_symref(repo, refname, symref_target);
6415 if (obj_arg == NULL)
6417 error = add_ref(repo, refname, obj_arg);
6422 const struct got_error *close_err = got_repo_close(repo);
6427 got_worktree_close(worktree);
6429 const struct got_error *pack_err =
6430 got_repo_pack_fds_close(pack_fds);
6443 "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-t] "
6444 "[-n] [name]\n", getprogname());
6448 static const struct got_error *
6449 list_branch(struct got_repository *repo, struct got_worktree *worktree,
6450 struct got_reference *ref)
6452 const struct got_error *err = NULL;
6453 const char *refname, *marker = " ";
6456 refname = got_ref_get_name(ref);
6457 if (worktree && strcmp(refname,
6458 got_worktree_get_head_ref_name(worktree)) == 0) {
6459 struct got_object_id *id = NULL;
6461 err = got_ref_resolve(&id, repo, ref);
6464 if (got_object_id_cmp(id,
6465 got_worktree_get_base_commit_id(worktree)) == 0)
6472 if (strncmp(refname, "refs/heads/", 11) == 0)
6474 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
6476 if (strncmp(refname, "refs/remotes/", 13) == 0)
6479 refstr = got_ref_to_str(ref);
6481 return got_error_from_errno("got_ref_to_str");
6483 printf("%s%s: %s\n", marker, refname, refstr);
6488 static const struct got_error *
6489 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
6491 const char *refname;
6493 if (worktree == NULL)
6494 return got_error(GOT_ERR_NOT_WORKTREE);
6496 refname = got_worktree_get_head_ref_name(worktree);
6498 if (strncmp(refname, "refs/heads/", 11) == 0)
6500 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
6503 printf("%s\n", refname);
6508 static const struct got_error *
6509 list_branches(struct got_repository *repo, struct got_worktree *worktree,
6512 static const struct got_error *err = NULL;
6513 struct got_reflist_head refs;
6514 struct got_reflist_entry *re;
6515 struct got_reference *temp_ref = NULL;
6516 int rebase_in_progress, histedit_in_progress;
6521 err = got_worktree_rebase_in_progress(&rebase_in_progress,
6526 err = got_worktree_histedit_in_progress(&histedit_in_progress,
6531 if (rebase_in_progress || histedit_in_progress) {
6532 err = got_ref_open(&temp_ref, repo,
6533 got_worktree_get_head_ref_name(worktree), 0);
6536 list_branch(repo, worktree, temp_ref);
6537 got_ref_close(temp_ref);
6541 err = got_ref_list(&refs, repo, "refs/heads", sort_by_time ?
6542 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6547 TAILQ_FOREACH(re, &refs, entry)
6548 list_branch(repo, worktree, re->ref);
6550 got_ref_list_free(&refs);
6552 err = got_ref_list(&refs, repo, "refs/remotes", sort_by_time ?
6553 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6558 TAILQ_FOREACH(re, &refs, entry)
6559 list_branch(repo, worktree, re->ref);
6561 got_ref_list_free(&refs);
6566 static const struct got_error *
6567 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
6568 const char *branch_name)
6570 const struct got_error *err = NULL;
6571 struct got_reference *ref = NULL;
6572 char *refname, *remote_refname = NULL;
6574 if (strncmp(branch_name, "refs/", 5) == 0)
6576 if (strncmp(branch_name, "heads/", 6) == 0)
6578 else if (strncmp(branch_name, "remotes/", 8) == 0)
6581 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
6582 return got_error_from_errno("asprintf");
6584 if (asprintf(&remote_refname, "refs/remotes/%s",
6585 branch_name) == -1) {
6586 err = got_error_from_errno("asprintf");
6590 err = got_ref_open(&ref, repo, refname, 0);
6592 const struct got_error *err2;
6593 if (err->code != GOT_ERR_NOT_REF)
6596 * Keep 'err' intact such that if neither branch exists
6597 * we report "refs/heads" rather than "refs/remotes" in
6598 * our error message.
6600 err2 = got_ref_open(&ref, repo, remote_refname, 0);
6607 strcmp(got_worktree_get_head_ref_name(worktree),
6608 got_ref_get_name(ref)) == 0) {
6609 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6610 "will not delete this work tree's current branch");
6614 err = delete_ref(repo, ref);
6619 free(remote_refname);
6623 static const struct got_error *
6624 add_branch(struct got_repository *repo, const char *branch_name,
6625 struct got_object_id *base_commit_id)
6627 const struct got_error *err = NULL;
6628 struct got_reference *ref = NULL;
6629 char *base_refname = NULL, *refname = NULL;
6632 * Don't let the user create a branch name with a leading '-'.
6633 * While technically a valid reference name, this case is usually
6634 * an unintended typo.
6636 if (branch_name[0] == '-')
6637 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
6639 if (strncmp(branch_name, "refs/heads/", 11) == 0)
6642 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
6643 err = got_error_from_errno("asprintf");
6647 err = got_ref_open(&ref, repo, refname, 0);
6649 err = got_error(GOT_ERR_BRANCH_EXISTS);
6651 } else if (err->code != GOT_ERR_NOT_REF)
6654 err = got_ref_alloc(&ref, refname, base_commit_id);
6658 err = got_ref_write(ref, repo);
6667 static const struct got_error *
6668 cmd_branch(int argc, char *argv[])
6670 const struct got_error *error = NULL;
6671 struct got_repository *repo = NULL;
6672 struct got_worktree *worktree = NULL;
6673 char *cwd = NULL, *repo_path = NULL;
6674 int ch, do_list = 0, do_show = 0, do_update = 1, sort_by_time = 0;
6675 const char *delref = NULL, *commit_id_arg = NULL;
6676 struct got_reference *ref = NULL;
6677 struct got_pathlist_head paths;
6678 struct got_pathlist_entry *pe;
6679 struct got_object_id *commit_id = NULL;
6680 char *commit_id_str = NULL;
6681 int *pack_fds = NULL;
6685 while ((ch = getopt(argc, argv, "c:d:r:lnt")) != -1) {
6688 commit_id_arg = optarg;
6694 repo_path = realpath(optarg, NULL);
6695 if (repo_path == NULL)
6696 return got_error_from_errno2("realpath",
6698 got_path_strip_trailing_slashes(repo_path);
6715 if (do_list && delref)
6716 option_conflict('l', 'd');
6717 if (sort_by_time && !do_list)
6718 errx(1, "-t option requires -l option");
6723 if (!do_list && !delref && argc == 0)
6726 if ((do_list || delref || do_show) && commit_id_arg != NULL)
6727 errx(1, "-c option can only be used when creating a branch");
6729 if (do_list || delref) {
6732 } else if (!do_show && argc != 1)
6736 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6737 "sendfd unveil", NULL) == -1)
6740 cwd = getcwd(NULL, 0);
6742 error = got_error_from_errno("getcwd");
6746 error = got_repo_pack_fds_open(&pack_fds);
6750 if (repo_path == NULL) {
6751 error = got_worktree_open(&worktree, cwd);
6752 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6758 strdup(got_worktree_get_repo_path(worktree));
6759 if (repo_path == NULL)
6760 error = got_error_from_errno("strdup");
6764 repo_path = strdup(cwd);
6765 if (repo_path == NULL) {
6766 error = got_error_from_errno("strdup");
6772 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6777 if (do_list || do_show) {
6778 /* Remove "cpath" promise. */
6779 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6785 error = apply_unveil(got_repo_get_path(repo), do_list,
6786 worktree ? got_worktree_get_root_path(worktree) : NULL);
6791 error = show_current_branch(repo, worktree);
6793 error = list_branches(repo, worktree, sort_by_time);
6795 error = delete_branch(repo, worktree, delref);
6797 struct got_reflist_head refs;
6799 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
6803 if (commit_id_arg == NULL)
6804 commit_id_arg = worktree ?
6805 got_worktree_get_head_ref_name(worktree) :
6807 error = got_repo_match_object_id(&commit_id, NULL,
6808 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &refs, repo);
6809 got_ref_list_free(&refs);
6812 error = add_branch(repo, argv[0], commit_id);
6815 if (worktree && do_update) {
6816 struct got_update_progress_arg upa;
6817 char *branch_refname = NULL;
6819 error = got_object_id_str(&commit_id_str, commit_id);
6822 error = get_worktree_paths_from_argv(&paths, 0, NULL,
6826 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
6828 error = got_error_from_errno("asprintf");
6831 error = got_ref_open(&ref, repo, branch_refname, 0);
6832 free(branch_refname);
6835 error = switch_head_ref(ref, commit_id, worktree,
6839 error = got_worktree_set_base_commit_id(worktree, repo,
6843 memset(&upa, 0, sizeof(upa));
6844 error = got_worktree_checkout_files(worktree, &paths,
6845 repo, update_progress, &upa, check_cancelled,
6849 if (upa.did_something) {
6850 printf("Updated to %s: %s\n",
6851 got_worktree_get_head_ref_name(worktree),
6854 print_update_progress_stats(&upa);
6861 const struct got_error *close_err = got_repo_close(repo);
6866 got_worktree_close(worktree);
6868 const struct got_error *pack_err =
6869 got_repo_pack_fds_close(pack_fds);
6876 free(commit_id_str);
6877 TAILQ_FOREACH(pe, &paths, entry)
6878 free((char *)pe->path);
6879 got_pathlist_free(&paths);
6888 "usage: %s tag [-c commit] [-r repository] [-l] "
6889 "[-m message] [-s signer-id] [-V] name\n",
6895 static const struct got_error *
6896 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
6898 const struct got_error *err = NULL;
6899 struct got_reflist_entry *re, *se, *new;
6900 struct got_object_id *re_id, *se_id;
6901 struct got_tag_object *re_tag, *se_tag;
6902 time_t re_time, se_time;
6904 STAILQ_FOREACH(re, tags, entry) {
6905 se = STAILQ_FIRST(sorted);
6907 err = got_reflist_entry_dup(&new, re);
6910 STAILQ_INSERT_HEAD(sorted, new, entry);
6913 err = got_ref_resolve(&re_id, repo, re->ref);
6916 err = got_object_open_as_tag(&re_tag, repo, re_id);
6920 re_time = got_object_tag_get_tagger_time(re_tag);
6921 got_object_tag_close(re_tag);
6925 err = got_ref_resolve(&se_id, repo, re->ref);
6928 err = got_object_open_as_tag(&se_tag, repo, se_id);
6932 se_time = got_object_tag_get_tagger_time(se_tag);
6933 got_object_tag_close(se_tag);
6935 if (se_time > re_time) {
6936 err = got_reflist_entry_dup(&new, re);
6939 STAILQ_INSERT_AFTER(sorted, se, new, entry);
6942 se = STAILQ_NEXT(se, entry);
6951 static const struct got_error *
6952 get_tag_refname(char **refname, const char *tag_name)
6954 const struct got_error *err;
6956 if (strncmp("refs/tags/", tag_name, 10) == 0) {
6957 *refname = strdup(tag_name);
6958 if (*refname == NULL)
6959 return got_error_from_errno("strdup");
6960 } else if (asprintf(refname, "refs/tags/%s", tag_name) == -1) {
6961 err = got_error_from_errno("asprintf");
6969 static const struct got_error *
6970 list_tags(struct got_repository *repo, const char *tag_name, int verify_tags,
6971 const char *allowed_signers, const char *revoked_signers, int verbosity)
6973 static const struct got_error *err = NULL;
6974 struct got_reflist_head refs;
6975 struct got_reflist_entry *re;
6976 char *wanted_refname = NULL;
6981 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
6986 struct got_reference *ref;
6987 err = get_tag_refname(&wanted_refname, tag_name);
6990 /* Wanted tag reference should exist. */
6991 err = got_ref_open(&ref, repo, wanted_refname, 0);
6997 TAILQ_FOREACH(re, &refs, entry) {
6998 const char *refname;
6999 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
7001 const char *tagger, *ssh_sig = NULL;
7002 char *sig_msg = NULL;
7004 struct got_object_id *id;
7005 struct got_tag_object *tag;
7006 struct got_commit_object *commit = NULL;
7008 refname = got_ref_get_name(re->ref);
7009 if (strncmp(refname, "refs/tags/", 10) != 0 ||
7010 (wanted_refname && strcmp(refname, wanted_refname) != 0))
7013 refstr = got_ref_to_str(re->ref);
7014 if (refstr == NULL) {
7015 err = got_error_from_errno("got_ref_to_str");
7019 err = got_ref_resolve(&id, repo, re->ref);
7022 err = got_object_open_as_tag(&tag, repo, id);
7024 if (err->code != GOT_ERR_OBJ_TYPE) {
7028 /* "lightweight" tag */
7029 err = got_object_open_as_commit(&commit, repo, id);
7034 tagger = got_object_commit_get_committer(commit);
7036 got_object_commit_get_committer_time(commit);
7037 err = got_object_id_str(&id_str, id);
7043 tagger = got_object_tag_get_tagger(tag);
7044 tagger_time = got_object_tag_get_tagger_time(tag);
7045 err = got_object_id_str(&id_str,
7046 got_object_tag_get_object_id(tag));
7052 ssh_sig = got_sigs_get_tagmsg_ssh_signature(
7053 got_object_tag_get_message(tag));
7054 if (ssh_sig && allowed_signers == NULL) {
7055 err = got_error_msg(
7056 GOT_ERR_VERIFY_TAG_SIGNATURE,
7057 "SSH signature verification requires "
7058 "setting allowed_signers in "
7064 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
7066 printf("from: %s\n", tagger);
7067 datestr = get_datestr(&tagger_time, datebuf);
7069 printf("date: %s UTC\n", datestr);
7071 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
7073 switch (got_object_tag_get_object_type(tag)) {
7074 case GOT_OBJ_TYPE_BLOB:
7075 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
7078 case GOT_OBJ_TYPE_TREE:
7079 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
7082 case GOT_OBJ_TYPE_COMMIT:
7083 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
7086 case GOT_OBJ_TYPE_TAG:
7087 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
7097 err = got_sigs_verify_tag_ssh(&sig_msg, tag, ssh_sig,
7098 allowed_signers, revoked_signers, verbosity);
7099 if (err && err->code == GOT_ERR_BAD_TAG_SIGNATURE)
7103 printf("signature: %s", sig_msg);
7109 err = got_object_commit_get_logmsg(&tagmsg0, commit);
7112 got_object_commit_close(commit);
7114 tagmsg0 = strdup(got_object_tag_get_message(tag));
7115 got_object_tag_close(tag);
7116 if (tagmsg0 == NULL) {
7117 err = got_error_from_errno("strdup");
7124 line = strsep(&tagmsg, "\n");
7126 printf(" %s\n", line);
7131 got_ref_list_free(&refs);
7132 free(wanted_refname);
7134 if (err == NULL && bad_sigs)
7135 err = got_error(GOT_ERR_BAD_TAG_SIGNATURE);
7139 static const struct got_error *
7140 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
7141 const char *tag_name, const char *repo_path)
7143 const struct got_error *err = NULL;
7144 char *template = NULL, *initial_content = NULL;
7145 char *editor = NULL;
7146 int initial_content_len;
7149 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
7150 err = got_error_from_errno("asprintf");
7154 initial_content_len = asprintf(&initial_content,
7155 "\n# tagging commit %s as %s\n",
7156 commit_id_str, tag_name);
7157 if (initial_content_len == -1) {
7158 err = got_error_from_errno("asprintf");
7162 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
7166 if (write(fd, initial_content, initial_content_len) == -1) {
7167 err = got_error_from_errno2("write", *tagmsg_path);
7171 err = get_editor(&editor);
7174 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content,
7175 initial_content_len, 1);
7177 free(initial_content);
7181 if (fd != -1 && close(fd) == -1 && err == NULL)
7182 err = got_error_from_errno2("close", *tagmsg_path);
7191 static const struct got_error *
7192 add_tag(struct got_repository *repo, const char *tagger,
7193 const char *tag_name, const char *commit_arg, const char *tagmsg_arg,
7194 const char *signer_id, int verbosity)
7196 const struct got_error *err = NULL;
7197 struct got_object_id *commit_id = NULL, *tag_id = NULL;
7198 char *label = NULL, *commit_id_str = NULL;
7199 struct got_reference *ref = NULL;
7200 char *refname = NULL, *tagmsg = NULL;
7201 char *tagmsg_path = NULL, *tag_id_str = NULL;
7202 int preserve_tagmsg = 0;
7203 struct got_reflist_head refs;
7208 * Don't let the user create a tag name with a leading '-'.
7209 * While technically a valid reference name, this case is usually
7210 * an unintended typo.
7212 if (tag_name[0] == '-')
7213 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
7215 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
7219 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
7220 GOT_OBJ_TYPE_COMMIT, &refs, repo);
7224 err = got_object_id_str(&commit_id_str, commit_id);
7228 err = get_tag_refname(&refname, tag_name);
7231 if (strncmp("refs/tags/", tag_name, 10) == 0)
7234 err = got_ref_open(&ref, repo, refname, 0);
7236 err = got_error(GOT_ERR_TAG_EXISTS);
7238 } else if (err->code != GOT_ERR_NOT_REF)
7241 if (tagmsg_arg == NULL) {
7242 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
7243 tag_name, got_repo_get_path(repo));
7245 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
7246 tagmsg_path != NULL)
7247 preserve_tagmsg = 1;
7250 /* Editor is done; we can now apply unveil(2) */
7251 err = got_sigs_apply_unveil();
7254 err = apply_unveil(got_repo_get_path(repo), 0, NULL);
7259 err = got_object_tag_create(&tag_id, tag_name, commit_id,
7260 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, signer_id, repo,
7264 preserve_tagmsg = 1;
7268 err = got_ref_alloc(&ref, refname, tag_id);
7271 preserve_tagmsg = 1;
7275 err = got_ref_write(ref, repo);
7278 preserve_tagmsg = 1;
7282 err = got_object_id_str(&tag_id_str, tag_id);
7285 preserve_tagmsg = 1;
7288 printf("Created tag %s\n", tag_id_str);
7290 if (preserve_tagmsg) {
7291 fprintf(stderr, "%s: tag message preserved in %s\n",
7292 getprogname(), tagmsg_path);
7293 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
7294 err = got_error_from_errno2("unlink", tagmsg_path);
7299 free(commit_id_str);
7303 got_ref_list_free(&refs);
7307 static const struct got_error *
7308 cmd_tag(int argc, char *argv[])
7310 const struct got_error *error = NULL;
7311 struct got_repository *repo = NULL;
7312 struct got_worktree *worktree = NULL;
7313 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
7314 char *gitconfig_path = NULL, *tagger = NULL;
7315 char *allowed_signers = NULL, *revoked_signers = NULL;
7316 char *signer_id = NULL;
7317 const char *tag_name = NULL, *commit_id_arg = NULL, *tagmsg = NULL;
7318 int ch, do_list = 0, verify_tags = 0, verbosity = 0;
7319 int *pack_fds = NULL;
7321 while ((ch = getopt(argc, argv, "c:m:r:ls:Vv")) != -1) {
7324 commit_id_arg = optarg;
7330 repo_path = realpath(optarg, NULL);
7331 if (repo_path == NULL) {
7332 error = got_error_from_errno2("realpath",
7336 got_path_strip_trailing_slashes(repo_path);
7342 signer_id = strdup(optarg);
7343 if (signer_id == NULL) {
7344 error = got_error_from_errno("strdup");
7354 else if (verbosity < 3)
7366 if (do_list || verify_tags) {
7367 if (commit_id_arg != NULL)
7369 "-c option can only be used when creating a tag");
7372 option_conflict('l', 'm');
7374 option_conflict('V', 'm');
7378 option_conflict('l', 's');
7380 option_conflict('V', 's');
7384 } else if (argc != 1)
7391 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
7392 "sendfd unveil", NULL) == -1)
7395 cwd = getcwd(NULL, 0);
7397 error = got_error_from_errno("getcwd");
7401 error = got_repo_pack_fds_open(&pack_fds);
7405 if (repo_path == NULL) {
7406 error = got_worktree_open(&worktree, cwd);
7407 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7413 strdup(got_worktree_get_repo_path(worktree));
7414 if (repo_path == NULL)
7415 error = got_error_from_errno("strdup");
7419 repo_path = strdup(cwd);
7420 if (repo_path == NULL) {
7421 error = got_error_from_errno("strdup");
7427 if (do_list || verify_tags) {
7428 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7431 error = get_allowed_signers(&allowed_signers, repo, worktree);
7434 error = get_revoked_signers(&revoked_signers, repo, worktree);
7438 /* Release work tree lock. */
7439 got_worktree_close(worktree);
7444 * Remove "cpath" promise unless needed for signature tmpfile
7448 got_sigs_apply_unveil();
7451 if (pledge("stdio rpath wpath flock proc exec sendfd "
7452 "unveil", NULL) == -1)
7456 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7459 error = list_tags(repo, tag_name, verify_tags, allowed_signers,
7460 revoked_signers, verbosity);
7462 error = get_gitconfig_path(&gitconfig_path);
7465 error = got_repo_open(&repo, repo_path, gitconfig_path,
7470 error = get_author(&tagger, repo, worktree);
7473 if (signer_id == NULL) {
7474 error = get_signer_id(&signer_id, repo, worktree);
7479 /* Release work tree lock. */
7480 got_worktree_close(worktree);
7486 error = got_sigs_apply_unveil();
7490 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
7495 if (commit_id_arg == NULL) {
7496 struct got_reference *head_ref;
7497 struct got_object_id *commit_id;
7498 error = got_ref_open(&head_ref, repo,
7499 worktree ? got_worktree_get_head_ref_name(worktree)
7503 error = got_ref_resolve(&commit_id, repo, head_ref);
7504 got_ref_close(head_ref);
7507 error = got_object_id_str(&commit_id_str, commit_id);
7513 error = add_tag(repo, tagger, tag_name,
7514 commit_id_str ? commit_id_str : commit_id_arg, tagmsg,
7515 signer_id, verbosity);
7519 const struct got_error *close_err = got_repo_close(repo);
7524 got_worktree_close(worktree);
7526 const struct got_error *pack_err =
7527 got_repo_pack_fds_close(pack_fds);
7533 free(gitconfig_path);
7534 free(commit_id_str);
7536 free(allowed_signers);
7537 free(revoked_signers);
7545 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
7550 static const struct got_error *
7551 add_progress(void *arg, unsigned char status, const char *path)
7553 while (path[0] == '/')
7555 printf("%c %s\n", status, path);
7559 static const struct got_error *
7560 cmd_add(int argc, char *argv[])
7562 const struct got_error *error = NULL;
7563 struct got_repository *repo = NULL;
7564 struct got_worktree *worktree = NULL;
7566 struct got_pathlist_head paths;
7567 struct got_pathlist_entry *pe;
7568 int ch, can_recurse = 0, no_ignores = 0;
7569 int *pack_fds = NULL;
7573 while ((ch = getopt(argc, argv, "IR")) != -1) {
7591 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7598 cwd = getcwd(NULL, 0);
7600 error = got_error_from_errno("getcwd");
7604 error = got_repo_pack_fds_open(&pack_fds);
7608 error = got_worktree_open(&worktree, cwd);
7610 if (error->code == GOT_ERR_NOT_WORKTREE)
7611 error = wrap_not_worktree_error(error, "add", cwd);
7615 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7620 error = apply_unveil(got_repo_get_path(repo), 1,
7621 got_worktree_get_root_path(worktree));
7625 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7632 TAILQ_FOREACH(pe, &paths, entry) {
7633 if (asprintf(&ondisk_path, "%s/%s",
7634 got_worktree_get_root_path(worktree),
7636 error = got_error_from_errno("asprintf");
7639 if (lstat(ondisk_path, &sb) == -1) {
7640 if (errno == ENOENT) {
7644 error = got_error_from_errno2("lstat",
7650 if (S_ISDIR(sb.st_mode)) {
7651 error = got_error_msg(GOT_ERR_BAD_PATH,
7652 "adding directories requires -R option");
7658 error = got_worktree_schedule_add(worktree, &paths, add_progress,
7659 NULL, repo, no_ignores);
7662 const struct got_error *close_err = got_repo_close(repo);
7667 got_worktree_close(worktree);
7669 const struct got_error *pack_err =
7670 got_repo_pack_fds_close(pack_fds);
7674 TAILQ_FOREACH(pe, &paths, entry)
7675 free((char *)pe->path);
7676 got_pathlist_free(&paths);
7684 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] [-s status-codes] "
7685 "path ...\n", getprogname());
7689 static const struct got_error *
7690 print_remove_status(void *arg, unsigned char status,
7691 unsigned char staged_status, const char *path)
7693 while (path[0] == '/')
7695 if (status == GOT_STATUS_NONEXISTENT)
7697 if (status == staged_status && (status == GOT_STATUS_DELETE))
7698 status = GOT_STATUS_NO_CHANGE;
7699 printf("%c%c %s\n", status, staged_status, path);
7703 static const struct got_error *
7704 cmd_remove(int argc, char *argv[])
7706 const struct got_error *error = NULL;
7707 struct got_worktree *worktree = NULL;
7708 struct got_repository *repo = NULL;
7709 const char *status_codes = NULL;
7711 struct got_pathlist_head paths;
7712 struct got_pathlist_entry *pe;
7713 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0, i;
7714 int ignore_missing_paths = 0;
7715 int *pack_fds = NULL;
7719 while ((ch = getopt(argc, argv, "fkRs:")) != -1) {
7722 delete_local_mods = 1;
7723 ignore_missing_paths = 1;
7732 for (i = 0; i < strlen(optarg); i++) {
7733 switch (optarg[i]) {
7734 case GOT_STATUS_MODIFY:
7735 delete_local_mods = 1;
7737 case GOT_STATUS_MISSING:
7738 ignore_missing_paths = 1;
7741 errx(1, "invalid status code '%c'",
7745 status_codes = optarg;
7757 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7764 cwd = getcwd(NULL, 0);
7766 error = got_error_from_errno("getcwd");
7770 error = got_repo_pack_fds_open(&pack_fds);
7774 error = got_worktree_open(&worktree, cwd);
7776 if (error->code == GOT_ERR_NOT_WORKTREE)
7777 error = wrap_not_worktree_error(error, "remove", cwd);
7781 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7786 error = apply_unveil(got_repo_get_path(repo), 1,
7787 got_worktree_get_root_path(worktree));
7791 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7798 TAILQ_FOREACH(pe, &paths, entry) {
7799 if (asprintf(&ondisk_path, "%s/%s",
7800 got_worktree_get_root_path(worktree),
7802 error = got_error_from_errno("asprintf");
7805 if (lstat(ondisk_path, &sb) == -1) {
7806 if (errno == ENOENT) {
7810 error = got_error_from_errno2("lstat",
7816 if (S_ISDIR(sb.st_mode)) {
7817 error = got_error_msg(GOT_ERR_BAD_PATH,
7818 "removing directories requires -R option");
7824 error = got_worktree_schedule_delete(worktree, &paths,
7825 delete_local_mods, status_codes, print_remove_status, NULL,
7826 repo, keep_on_disk, ignore_missing_paths);
7829 const struct got_error *close_err = got_repo_close(repo);
7834 got_worktree_close(worktree);
7836 const struct got_error *pack_err =
7837 got_repo_pack_fds_close(pack_fds);
7841 TAILQ_FOREACH(pe, &paths, entry)
7842 free((char *)pe->path);
7843 got_pathlist_free(&paths);
7851 fprintf(stderr, "usage: %s patch [-n] [-p strip-count] "
7852 "[-R] [patchfile]\n", getprogname());
7856 static const struct got_error *
7857 patch_from_stdin(int *patchfd)
7859 const struct got_error *err = NULL;
7861 char *path, buf[BUFSIZ];
7862 sig_t sighup, sigint, sigquit;
7864 err = got_opentemp_named_fd(&path, patchfd,
7865 GOT_TMPDIR_STR "/got-patch");
7871 sighup = signal(SIGHUP, SIG_DFL);
7872 sigint = signal(SIGINT, SIG_DFL);
7873 sigquit = signal(SIGQUIT, SIG_DFL);
7876 r = read(0, buf, sizeof(buf));
7878 err = got_error_from_errno("read");
7883 if (write(*patchfd, buf, r) == -1) {
7884 err = got_error_from_errno("write");
7889 signal(SIGHUP, sighup);
7890 signal(SIGINT, sigint);
7891 signal(SIGQUIT, sigquit);
7893 if (err == NULL && lseek(*patchfd, 0, SEEK_SET) == -1)
7894 err = got_error_from_errno("lseek");
7904 static const struct got_error *
7905 patch_progress(void *arg, const char *old, const char *new,
7906 unsigned char status, const struct got_error *error, int old_from,
7907 int old_lines, int new_from, int new_lines, int offset,
7908 int ws_mangled, const struct got_error *hunk_err)
7910 const char *path = new == NULL ? old : new;
7912 while (*path == '/')
7916 printf("%c %s\n", status, path);
7919 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
7921 if (offset != 0 || hunk_err != NULL || ws_mangled) {
7922 printf("@@ -%d,%d +%d,%d @@ ", old_from,
7923 old_lines, new_from, new_lines);
7924 if (hunk_err != NULL)
7925 printf("%s\n", hunk_err->msg);
7926 else if (offset != 0)
7927 printf("applied with offset %d\n", offset);
7929 printf("hunk contains mangled whitespace\n");
7935 static const struct got_error *
7936 cmd_patch(int argc, char *argv[])
7938 const struct got_error *error = NULL, *close_error = NULL;
7939 struct got_worktree *worktree = NULL;
7940 struct got_repository *repo = NULL;
7943 int ch, nop = 0, strip = -1, reverse = 0;
7945 int *pack_fds = NULL;
7947 while ((ch = getopt(argc, argv, "np:R")) != -1) {
7953 strip = strtonum(optarg, 0, INT_MAX, &errstr);
7955 errx(1, "pathname strip count is %s: %s",
7971 error = patch_from_stdin(&patchfd);
7974 } else if (argc == 1) {
7975 patchfd = open(argv[0], O_RDONLY);
7976 if (patchfd == -1) {
7977 error = got_error_from_errno2("open", argv[0]);
7983 if ((cwd = getcwd(NULL, 0)) == NULL) {
7984 error = got_error_from_errno("getcwd");
7988 error = got_repo_pack_fds_open(&pack_fds);
7992 error = got_worktree_open(&worktree, cwd);
7996 const char *repo_path = got_worktree_get_repo_path(worktree);
7997 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8001 error = apply_unveil(got_repo_get_path(repo), 0,
8002 got_worktree_get_root_path(worktree));
8007 if (pledge("stdio rpath wpath cpath fattr proc exec sendfd flock",
8012 error = got_patch(patchfd, worktree, repo, nop, strip, reverse,
8013 &patch_progress, NULL, check_cancelled, NULL);
8017 close_error = got_repo_close(repo);
8019 error = close_error;
8021 if (worktree != NULL) {
8022 close_error = got_worktree_close(worktree);
8024 error = close_error;
8027 const struct got_error *pack_err =
8028 got_repo_pack_fds_close(pack_fds);
8039 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
8040 "path ...\n", getprogname());
8044 static const struct got_error *
8045 revert_progress(void *arg, unsigned char status, const char *path)
8047 if (status == GOT_STATUS_UNVERSIONED)
8050 while (path[0] == '/')
8052 printf("%c %s\n", status, path);
8056 struct choose_patch_arg {
8057 FILE *patch_script_file;
8061 static const struct got_error *
8062 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
8063 int nchanges, const char *action)
8065 const struct got_error *err;
8067 size_t linesize = 0;
8071 case GOT_STATUS_ADD:
8072 printf("A %s\n%s this addition? [y/n] ", path, action);
8074 case GOT_STATUS_DELETE:
8075 printf("D %s\n%s this deletion? [y/n] ", path, action);
8077 case GOT_STATUS_MODIFY:
8078 if (fseek(patch_file, 0L, SEEK_SET) == -1)
8079 return got_error_from_errno("fseek");
8080 printf(GOT_COMMIT_SEP_STR);
8081 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
8083 if (linelen == -1 && ferror(patch_file)) {
8084 err = got_error_from_errno("getline");
8089 printf(GOT_COMMIT_SEP_STR);
8090 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
8091 path, n, nchanges, action);
8094 return got_error_path(path, GOT_ERR_FILE_STATUS);
8100 static const struct got_error *
8101 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
8102 FILE *patch_file, int n, int nchanges)
8104 const struct got_error *err = NULL;
8106 size_t linesize = 0;
8109 struct choose_patch_arg *a = arg;
8111 *choice = GOT_PATCH_CHOICE_NONE;
8113 if (a->patch_script_file) {
8115 err = show_change(status, path, patch_file, n, nchanges,
8119 linelen = getline(&line, &linesize, a->patch_script_file);
8120 if (linelen == -1) {
8121 if (ferror(a->patch_script_file))
8122 return got_error_from_errno("getline");
8125 nl = strchr(line, '\n');
8128 if (strcmp(line, "y") == 0) {
8129 *choice = GOT_PATCH_CHOICE_YES;
8131 } else if (strcmp(line, "n") == 0) {
8132 *choice = GOT_PATCH_CHOICE_NO;
8134 } else if (strcmp(line, "q") == 0 &&
8135 status == GOT_STATUS_MODIFY) {
8136 *choice = GOT_PATCH_CHOICE_QUIT;
8139 printf("invalid response '%s'\n", line);
8144 while (resp != 'y' && resp != 'n' && resp != 'q') {
8145 err = show_change(status, path, patch_file, n, nchanges,
8152 if (status == GOT_STATUS_MODIFY) {
8153 if (resp != 'y' && resp != 'n' && resp != 'q') {
8154 printf("invalid response '%c'\n", resp);
8157 } else if (resp != 'y' && resp != 'n') {
8158 printf("invalid response '%c'\n", resp);
8164 *choice = GOT_PATCH_CHOICE_YES;
8165 else if (resp == 'n')
8166 *choice = GOT_PATCH_CHOICE_NO;
8167 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
8168 *choice = GOT_PATCH_CHOICE_QUIT;
8173 static const struct got_error *
8174 cmd_revert(int argc, char *argv[])
8176 const struct got_error *error = NULL;
8177 struct got_worktree *worktree = NULL;
8178 struct got_repository *repo = NULL;
8179 char *cwd = NULL, *path = NULL;
8180 struct got_pathlist_head paths;
8181 struct got_pathlist_entry *pe;
8182 int ch, can_recurse = 0, pflag = 0;
8183 FILE *patch_script_file = NULL;
8184 const char *patch_script_path = NULL;
8185 struct choose_patch_arg cpa;
8186 int *pack_fds = NULL;
8190 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
8196 patch_script_path = optarg;
8211 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8212 "unveil", NULL) == -1)
8217 if (patch_script_path && !pflag)
8218 errx(1, "-F option can only be used together with -p option");
8220 cwd = getcwd(NULL, 0);
8222 error = got_error_from_errno("getcwd");
8226 error = got_repo_pack_fds_open(&pack_fds);
8230 error = got_worktree_open(&worktree, cwd);
8232 if (error->code == GOT_ERR_NOT_WORKTREE)
8233 error = wrap_not_worktree_error(error, "revert", cwd);
8237 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8242 if (patch_script_path) {
8243 patch_script_file = fopen(patch_script_path, "re");
8244 if (patch_script_file == NULL) {
8245 error = got_error_from_errno2("fopen",
8250 error = apply_unveil(got_repo_get_path(repo), 1,
8251 got_worktree_get_root_path(worktree));
8255 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8262 TAILQ_FOREACH(pe, &paths, entry) {
8263 if (asprintf(&ondisk_path, "%s/%s",
8264 got_worktree_get_root_path(worktree),
8266 error = got_error_from_errno("asprintf");
8269 if (lstat(ondisk_path, &sb) == -1) {
8270 if (errno == ENOENT) {
8274 error = got_error_from_errno2("lstat",
8280 if (S_ISDIR(sb.st_mode)) {
8281 error = got_error_msg(GOT_ERR_BAD_PATH,
8282 "reverting directories requires -R option");
8288 cpa.patch_script_file = patch_script_file;
8289 cpa.action = "revert";
8290 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
8291 pflag ? choose_patch : NULL, &cpa, repo);
8293 if (patch_script_file && fclose(patch_script_file) == EOF &&
8295 error = got_error_from_errno2("fclose", patch_script_path);
8297 const struct got_error *close_err = got_repo_close(repo);
8302 got_worktree_close(worktree);
8304 const struct got_error *pack_err =
8305 got_repo_pack_fds_close(pack_fds);
8317 fprintf(stderr, "usage: %s commit [-F path] [-m msg] [-N] [-S] "
8318 "[path ...]\n", getprogname());
8322 struct collect_commit_logmsg_arg {
8323 const char *cmdline_log;
8324 const char *prepared_log;
8325 int non_interactive;
8327 const char *worktree_path;
8328 const char *branch_name;
8329 const char *repo_path;
8334 static const struct got_error *
8335 read_prepared_logmsg(char **logmsg, const char *path)
8337 const struct got_error *err = NULL;
8343 memset(&sb, 0, sizeof(sb));
8345 f = fopen(path, "re");
8347 return got_error_from_errno2("fopen", path);
8349 if (fstat(fileno(f), &sb) == -1) {
8350 err = got_error_from_errno2("fstat", path);
8353 if (sb.st_size == 0) {
8354 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
8358 *logmsg = malloc(sb.st_size + 1);
8359 if (*logmsg == NULL) {
8360 err = got_error_from_errno("malloc");
8364 r = fread(*logmsg, 1, sb.st_size, f);
8365 if (r != sb.st_size) {
8367 err = got_error_from_errno2("fread", path);
8369 err = got_error(GOT_ERR_IO);
8372 (*logmsg)[sb.st_size] = '\0';
8374 if (fclose(f) == EOF && err == NULL)
8375 err = got_error_from_errno2("fclose", path);
8384 static const struct got_error *
8385 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
8388 char *initial_content = NULL;
8389 struct got_pathlist_entry *pe;
8390 const struct got_error *err = NULL;
8391 char *template = NULL;
8392 struct collect_commit_logmsg_arg *a = arg;
8393 int initial_content_len;
8397 /* if a message was specified on the command line, just use it */
8398 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
8399 len = strlen(a->cmdline_log) + 1;
8400 *logmsg = malloc(len + 1);
8401 if (*logmsg == NULL)
8402 return got_error_from_errno("malloc");
8403 strlcpy(*logmsg, a->cmdline_log, len);
8405 } else if (a->prepared_log != NULL && a->non_interactive)
8406 return read_prepared_logmsg(logmsg, a->prepared_log);
8408 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
8409 return got_error_from_errno("asprintf");
8411 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
8415 if (a->prepared_log) {
8417 err = read_prepared_logmsg(&msg, a->prepared_log);
8420 if (write(fd, msg, strlen(msg)) == -1) {
8421 err = got_error_from_errno2("write", a->logmsg_path);
8428 initial_content_len = asprintf(&initial_content,
8429 "\n# changes to be committed on branch %s:\n",
8431 if (initial_content_len == -1) {
8432 err = got_error_from_errno("asprintf");
8436 if (write(fd, initial_content, initial_content_len) == -1) {
8437 err = got_error_from_errno2("write", a->logmsg_path);
8441 TAILQ_FOREACH(pe, commitable_paths, entry) {
8442 struct got_commitable *ct = pe->data;
8443 dprintf(fd, "# %c %s\n",
8444 got_commitable_get_status(ct),
8445 got_commitable_get_path(ct));
8448 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content,
8449 initial_content_len, a->prepared_log ? 0 : 1);
8451 free(initial_content);
8454 if (fd != -1 && close(fd) == -1 && err == NULL)
8455 err = got_error_from_errno2("close", a->logmsg_path);
8457 /* Editor is done; we can now apply unveil(2) */
8459 err = apply_unveil(a->repo_path, 0, a->worktree_path);
8467 static const struct got_error *
8468 cmd_commit(int argc, char *argv[])
8470 const struct got_error *error = NULL;
8471 struct got_worktree *worktree = NULL;
8472 struct got_repository *repo = NULL;
8473 char *cwd = NULL, *id_str = NULL;
8474 struct got_object_id *id = NULL;
8475 const char *logmsg = NULL;
8476 char *prepared_logmsg = NULL;
8477 struct collect_commit_logmsg_arg cl_arg;
8478 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
8479 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
8480 int allow_bad_symlinks = 0, non_interactive = 0, merge_in_progress = 0;
8481 struct got_pathlist_head paths;
8482 int *pack_fds = NULL;
8485 cl_arg.logmsg_path = NULL;
8487 while ((ch = getopt(argc, argv, "F:m:NS")) != -1) {
8491 option_conflict('F', 'm');
8492 prepared_logmsg = realpath(optarg, NULL);
8493 if (prepared_logmsg == NULL)
8494 return got_error_from_errno2("realpath",
8498 if (prepared_logmsg)
8499 option_conflict('m', 'F');
8503 non_interactive = 1;
8506 allow_bad_symlinks = 1;
8518 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8519 "unveil", NULL) == -1)
8522 cwd = getcwd(NULL, 0);
8524 error = got_error_from_errno("getcwd");
8528 error = got_repo_pack_fds_open(&pack_fds);
8532 error = got_worktree_open(&worktree, cwd);
8534 if (error->code == GOT_ERR_NOT_WORKTREE)
8535 error = wrap_not_worktree_error(error, "commit", cwd);
8539 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
8542 if (rebase_in_progress) {
8543 error = got_error(GOT_ERR_REBASING);
8547 error = got_worktree_histedit_in_progress(&histedit_in_progress,
8552 error = get_gitconfig_path(&gitconfig_path);
8555 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8556 gitconfig_path, pack_fds);
8560 error = got_worktree_merge_in_progress(&merge_in_progress, worktree, repo);
8563 if (merge_in_progress) {
8564 error = got_error(GOT_ERR_MERGE_BUSY);
8568 error = get_author(&author, repo, worktree);
8573 * unveil(2) traverses exec(2); if an editor is used we have
8574 * to apply unveil after the log message has been written.
8576 if (logmsg == NULL || strlen(logmsg) == 0)
8577 error = get_editor(&editor);
8579 error = apply_unveil(got_repo_get_path(repo), 0,
8580 got_worktree_get_root_path(worktree));
8584 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8588 cl_arg.editor = editor;
8589 cl_arg.cmdline_log = logmsg;
8590 cl_arg.prepared_log = prepared_logmsg;
8591 cl_arg.non_interactive = non_interactive;
8592 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
8593 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
8594 if (!histedit_in_progress) {
8595 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
8596 error = got_error(GOT_ERR_COMMIT_BRANCH);
8599 cl_arg.branch_name += 11;
8601 cl_arg.repo_path = got_repo_get_path(repo);
8602 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
8603 allow_bad_symlinks, collect_commit_logmsg, &cl_arg,
8604 print_status, NULL, repo);
8606 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
8607 cl_arg.logmsg_path != NULL)
8608 preserve_logmsg = 1;
8612 error = got_object_id_str(&id_str, id);
8615 printf("Created commit %s\n", id_str);
8617 if (preserve_logmsg) {
8618 fprintf(stderr, "%s: log message preserved in %s\n",
8619 getprogname(), cl_arg.logmsg_path);
8620 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
8622 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
8623 free(cl_arg.logmsg_path);
8625 const struct got_error *close_err = got_repo_close(repo);
8630 got_worktree_close(worktree);
8632 const struct got_error *pack_err =
8633 got_repo_pack_fds_close(pack_fds);
8639 free(gitconfig_path);
8642 free(prepared_logmsg);
8649 fprintf(stderr, "usage: %s send [-a] [-b branch] [-d branch] [-f] "
8650 "[-r repository-path] [-t tag] [-T] [-q] [-v] "
8651 "[remote-repository]\n", getprogname());
8656 print_load_info(int print_colored, int print_found, int print_trees,
8657 int ncolored, int nfound, int ntrees)
8659 if (print_colored) {
8660 printf("%d commit%s colored", ncolored,
8661 ncolored == 1 ? "" : "s");
8664 printf("%s%d object%s found",
8665 ncolored > 0 ? "; " : "",
8666 nfound, nfound == 1 ? "" : "s");
8669 printf("; %d tree%s scanned", ntrees,
8670 ntrees == 1 ? "" : "s");
8674 struct got_send_progress_arg {
8675 char last_scaled_packsize[FMT_SCALED_STRSIZE];
8682 int last_nobj_total;
8686 int printed_something;
8688 struct got_pathlist_head *delete_branches;
8691 static const struct got_error *
8692 send_progress(void *arg, int ncolored, int nfound, int ntrees,
8693 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
8694 int nobj_written, off_t bytes_sent, const char *refname, int success)
8696 struct got_send_progress_arg *a = arg;
8697 char scaled_packsize[FMT_SCALED_STRSIZE];
8698 char scaled_sent[FMT_SCALED_STRSIZE];
8699 int p_deltify = 0, p_written = 0, p_sent = 0;
8700 int print_colored = 0, print_found = 0, print_trees = 0;
8701 int print_searching = 0, print_total = 0;
8702 int print_deltify = 0, print_written = 0, print_sent = 0;
8704 if (a->verbosity < 0)
8708 const char *status = success ? "accepted" : "rejected";
8711 struct got_pathlist_entry *pe;
8712 TAILQ_FOREACH(pe, a->delete_branches, entry) {
8713 const char *branchname = pe->path;
8714 if (got_path_cmp(branchname, refname,
8715 strlen(branchname), strlen(refname)) == 0) {
8717 a->sent_something = 1;
8723 if (a->printed_something)
8725 printf("Server has %s %s", status, refname);
8726 a->printed_something = 1;
8730 if (a->last_ncolored != ncolored) {
8732 a->last_ncolored = ncolored;
8735 if (a->last_nfound != nfound) {
8738 a->last_nfound = nfound;
8741 if (a->last_ntrees != ntrees) {
8745 a->last_ntrees = ntrees;
8748 if ((print_colored || print_found || print_trees) &&
8751 print_load_info(print_colored, print_found, print_trees,
8752 ncolored, nfound, ntrees);
8753 a->printed_something = 1;
8756 } else if (!a->loading_done) {
8758 print_load_info(1, 1, 1, ncolored, nfound, ntrees);
8760 a->loading_done = 1;
8763 if (fmt_scaled(packfile_size, scaled_packsize) == -1)
8764 return got_error_from_errno("fmt_scaled");
8765 if (fmt_scaled(bytes_sent, scaled_sent) == -1)
8766 return got_error_from_errno("fmt_scaled");
8768 if (a->last_ncommits != ncommits) {
8769 print_searching = 1;
8770 a->last_ncommits = ncommits;
8773 if (a->last_nobj_total != nobj_total) {
8774 print_searching = 1;
8776 a->last_nobj_total = nobj_total;
8779 if (packfile_size > 0 && (a->last_scaled_packsize[0] == '\0' ||
8780 strcmp(scaled_packsize, a->last_scaled_packsize)) != 0) {
8781 if (strlcpy(a->last_scaled_packsize, scaled_packsize,
8782 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
8783 return got_error(GOT_ERR_NO_SPACE);
8786 if (nobj_deltify > 0 || nobj_written > 0) {
8787 if (nobj_deltify > 0) {
8788 p_deltify = (nobj_deltify * 100) / nobj_total;
8789 if (p_deltify != a->last_p_deltify) {
8790 a->last_p_deltify = p_deltify;
8791 print_searching = 1;
8796 if (nobj_written > 0) {
8797 p_written = (nobj_written * 100) / nobj_total;
8798 if (p_written != a->last_p_written) {
8799 a->last_p_written = p_written;
8800 print_searching = 1;
8808 if (bytes_sent > 0) {
8809 p_sent = (bytes_sent * 100) / packfile_size;
8810 if (p_sent != a->last_p_sent) {
8811 a->last_p_sent = p_sent;
8812 print_searching = 1;
8818 a->sent_something = 1;
8821 if (print_searching || print_total || print_deltify || print_written ||
8824 if (print_searching)
8825 printf("packing %d reference%s", ncommits,
8826 ncommits == 1 ? "" : "s");
8828 printf("; %d object%s", nobj_total,
8829 nobj_total == 1 ? "" : "s");
8831 printf("; deltify: %d%%", p_deltify);
8833 printf("; uploading pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
8834 scaled_packsize, p_sent);
8835 else if (print_written)
8836 printf("; writing pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
8837 scaled_packsize, p_written);
8838 if (print_searching || print_total || print_deltify ||
8839 print_written || print_sent) {
8840 a->printed_something = 1;
8846 static const struct got_error *
8847 cmd_send(int argc, char *argv[])
8849 const struct got_error *error = NULL;
8850 char *cwd = NULL, *repo_path = NULL;
8851 const char *remote_name;
8852 char *proto = NULL, *host = NULL, *port = NULL;
8853 char *repo_name = NULL, *server_path = NULL;
8854 const struct got_remote_repo *remotes, *remote = NULL;
8855 int nremotes, nbranches = 0, ntags = 0, ndelete_branches = 0;
8856 struct got_repository *repo = NULL;
8857 struct got_worktree *worktree = NULL;
8858 const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
8859 struct got_pathlist_head branches;
8860 struct got_pathlist_head tags;
8861 struct got_reflist_head all_branches;
8862 struct got_reflist_head all_tags;
8863 struct got_pathlist_head delete_args;
8864 struct got_pathlist_head delete_branches;
8865 struct got_reflist_entry *re;
8866 struct got_pathlist_entry *pe;
8867 int i, ch, sendfd = -1, sendstatus;
8869 struct got_send_progress_arg spa;
8870 int verbosity = 0, overwrite_refs = 0;
8871 int send_all_branches = 0, send_all_tags = 0;
8872 struct got_reference *ref = NULL;
8873 int *pack_fds = NULL;
8875 TAILQ_INIT(&branches);
8877 TAILQ_INIT(&all_branches);
8878 TAILQ_INIT(&all_tags);
8879 TAILQ_INIT(&delete_args);
8880 TAILQ_INIT(&delete_branches);
8882 while ((ch = getopt(argc, argv, "ab:d:fr:t:Tvq")) != -1) {
8885 send_all_branches = 1;
8888 error = got_pathlist_append(&branches, optarg, NULL);
8894 error = got_pathlist_append(&delete_args, optarg, NULL);
8902 repo_path = realpath(optarg, NULL);
8903 if (repo_path == NULL)
8904 return got_error_from_errno2("realpath",
8906 got_path_strip_trailing_slashes(repo_path);
8909 error = got_pathlist_append(&tags, optarg, NULL);
8920 else if (verbosity < 3)
8934 if (send_all_branches && !TAILQ_EMPTY(&branches))
8935 option_conflict('a', 'b');
8936 if (send_all_tags && !TAILQ_EMPTY(&tags))
8937 option_conflict('T', 't');
8941 remote_name = GOT_SEND_DEFAULT_REMOTE_NAME;
8943 remote_name = argv[0];
8947 cwd = getcwd(NULL, 0);
8949 error = got_error_from_errno("getcwd");
8953 error = got_repo_pack_fds_open(&pack_fds);
8957 if (repo_path == NULL) {
8958 error = got_worktree_open(&worktree, cwd);
8959 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8965 strdup(got_worktree_get_repo_path(worktree));
8966 if (repo_path == NULL)
8967 error = got_error_from_errno("strdup");
8971 repo_path = strdup(cwd);
8972 if (repo_path == NULL) {
8973 error = got_error_from_errno("strdup");
8979 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8984 worktree_conf = got_worktree_get_gotconfig(worktree);
8985 if (worktree_conf) {
8986 got_gotconfig_get_remotes(&nremotes, &remotes,
8988 for (i = 0; i < nremotes; i++) {
8989 if (strcmp(remotes[i].name, remote_name) == 0) {
8990 remote = &remotes[i];
8996 if (remote == NULL) {
8997 repo_conf = got_repo_get_gotconfig(repo);
8999 got_gotconfig_get_remotes(&nremotes, &remotes,
9001 for (i = 0; i < nremotes; i++) {
9002 if (strcmp(remotes[i].name, remote_name) == 0) {
9003 remote = &remotes[i];
9009 if (remote == NULL) {
9010 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
9011 for (i = 0; i < nremotes; i++) {
9012 if (strcmp(remotes[i].name, remote_name) == 0) {
9013 remote = &remotes[i];
9018 if (remote == NULL) {
9019 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
9023 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
9024 &repo_name, remote->send_url);
9028 if (strcmp(proto, "git") == 0) {
9030 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
9031 "sendfd dns inet unveil", NULL) == -1)
9034 } else if (strcmp(proto, "git+ssh") == 0 ||
9035 strcmp(proto, "ssh") == 0) {
9037 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
9038 "sendfd unveil", NULL) == -1)
9041 } else if (strcmp(proto, "http") == 0 ||
9042 strcmp(proto, "git+http") == 0) {
9043 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
9046 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
9050 error = got_dial_apply_unveil(proto);
9054 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
9058 if (send_all_branches) {
9059 error = got_ref_list(&all_branches, repo, "refs/heads",
9060 got_ref_cmp_by_name, NULL);
9063 TAILQ_FOREACH(re, &all_branches, entry) {
9064 const char *branchname = got_ref_get_name(re->ref);
9065 error = got_pathlist_append(&branches,
9071 } else if (nbranches == 0) {
9072 for (i = 0; i < remote->nsend_branches; i++) {
9073 got_pathlist_append(&branches,
9074 remote->send_branches[i], NULL);
9078 if (send_all_tags) {
9079 error = got_ref_list(&all_tags, repo, "refs/tags",
9080 got_ref_cmp_by_name, NULL);
9083 TAILQ_FOREACH(re, &all_tags, entry) {
9084 const char *tagname = got_ref_get_name(re->ref);
9085 error = got_pathlist_append(&tags,
9094 * To prevent accidents only branches in refs/heads/ can be deleted
9095 * with 'got send -d'.
9096 * Deleting anything else requires local repository access or Git.
9098 TAILQ_FOREACH(pe, &delete_args, entry) {
9099 const char *branchname = pe->path;
9101 struct got_pathlist_entry *new;
9102 if (strncmp(branchname, "refs/heads/", 11) == 0) {
9103 s = strdup(branchname);
9105 error = got_error_from_errno("strdup");
9109 if (asprintf(&s, "refs/heads/%s", branchname) == -1) {
9110 error = got_error_from_errno("asprintf");
9114 error = got_pathlist_insert(&new, &delete_branches, s, NULL);
9115 if (error || new == NULL /* duplicate */)
9122 if (nbranches == 0 && ndelete_branches == 0) {
9123 struct got_reference *head_ref;
9125 error = got_ref_open(&head_ref, repo,
9126 got_worktree_get_head_ref_name(worktree), 0);
9128 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
9131 if (got_ref_is_symbolic(head_ref)) {
9132 error = got_ref_resolve_symbolic(&ref, repo, head_ref);
9133 got_ref_close(head_ref);
9138 error = got_pathlist_append(&branches, got_ref_get_name(ref),
9146 printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
9147 port ? ":" : "", port ? port : "");
9149 error = got_send_connect(&sendpid, &sendfd, proto, host, port,
9150 server_path, verbosity);
9154 memset(&spa, 0, sizeof(spa));
9155 spa.last_scaled_packsize[0] = '\0';
9156 spa.last_p_deltify = -1;
9157 spa.last_p_written = -1;
9158 spa.verbosity = verbosity;
9159 spa.delete_branches = &delete_branches;
9160 error = got_send_pack(remote_name, &branches, &tags, &delete_branches,
9161 verbosity, overwrite_refs, sendfd, repo, send_progress, &spa,
9162 check_cancelled, NULL);
9163 if (spa.printed_something)
9167 if (!spa.sent_something && verbosity >= 0)
9168 printf("Already up-to-date\n");
9171 if (kill(sendpid, SIGTERM) == -1)
9172 error = got_error_from_errno("kill");
9173 if (waitpid(sendpid, &sendstatus, 0) == -1 && error == NULL)
9174 error = got_error_from_errno("waitpid");
9176 if (sendfd != -1 && close(sendfd) == -1 && error == NULL)
9177 error = got_error_from_errno("close");
9179 const struct got_error *close_err = got_repo_close(repo);
9184 got_worktree_close(worktree);
9186 const struct got_error *pack_err =
9187 got_repo_pack_fds_close(pack_fds);
9193 got_pathlist_free(&branches);
9194 got_pathlist_free(&tags);
9195 got_ref_list_free(&all_branches);
9196 got_ref_list_free(&all_tags);
9197 got_pathlist_free(&delete_args);
9198 TAILQ_FOREACH(pe, &delete_branches, entry)
9199 free((char *)pe->path);
9200 got_pathlist_free(&delete_branches);
9212 usage_cherrypick(void)
9214 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
9218 static const struct got_error *
9219 cmd_cherrypick(int argc, char *argv[])
9221 const struct got_error *error = NULL;
9222 struct got_worktree *worktree = NULL;
9223 struct got_repository *repo = NULL;
9224 char *cwd = NULL, *commit_id_str = NULL;
9225 struct got_object_id *commit_id = NULL;
9226 struct got_commit_object *commit = NULL;
9227 struct got_object_qid *pid;
9229 struct got_update_progress_arg upa;
9230 int *pack_fds = NULL;
9232 while ((ch = getopt(argc, argv, "")) != -1) {
9244 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9245 "unveil", NULL) == -1)
9251 cwd = getcwd(NULL, 0);
9253 error = got_error_from_errno("getcwd");
9257 error = got_repo_pack_fds_open(&pack_fds);
9261 error = got_worktree_open(&worktree, cwd);
9263 if (error->code == GOT_ERR_NOT_WORKTREE)
9264 error = wrap_not_worktree_error(error, "cherrypick",
9269 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9274 error = apply_unveil(got_repo_get_path(repo), 0,
9275 got_worktree_get_root_path(worktree));
9279 error = got_repo_match_object_id(&commit_id, NULL, argv[0],
9280 GOT_OBJ_TYPE_COMMIT, NULL, repo);
9283 error = got_object_id_str(&commit_id_str, commit_id);
9287 error = got_object_open_as_commit(&commit, repo, commit_id);
9290 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
9291 memset(&upa, 0, sizeof(upa));
9292 error = got_worktree_merge_files(worktree, pid ? &pid->id : NULL,
9293 commit_id, repo, update_progress, &upa, check_cancelled,
9298 if (upa.did_something)
9299 printf("Merged commit %s\n", commit_id_str);
9300 print_merge_progress_stats(&upa);
9303 got_object_commit_close(commit);
9304 free(commit_id_str);
9306 got_worktree_close(worktree);
9308 const struct got_error *close_err = got_repo_close(repo);
9313 const struct got_error *pack_err =
9314 got_repo_pack_fds_close(pack_fds);
9325 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
9329 static const struct got_error *
9330 cmd_backout(int argc, char *argv[])
9332 const struct got_error *error = NULL;
9333 struct got_worktree *worktree = NULL;
9334 struct got_repository *repo = NULL;
9335 char *cwd = NULL, *commit_id_str = NULL;
9336 struct got_object_id *commit_id = NULL;
9337 struct got_commit_object *commit = NULL;
9338 struct got_object_qid *pid;
9340 struct got_update_progress_arg upa;
9341 int *pack_fds = NULL;
9343 while ((ch = getopt(argc, argv, "")) != -1) {
9355 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9356 "unveil", NULL) == -1)
9362 cwd = getcwd(NULL, 0);
9364 error = got_error_from_errno("getcwd");
9368 error = got_repo_pack_fds_open(&pack_fds);
9372 error = got_worktree_open(&worktree, cwd);
9374 if (error->code == GOT_ERR_NOT_WORKTREE)
9375 error = wrap_not_worktree_error(error, "backout", cwd);
9379 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9384 error = apply_unveil(got_repo_get_path(repo), 0,
9385 got_worktree_get_root_path(worktree));
9389 error = got_repo_match_object_id(&commit_id, NULL, argv[0],
9390 GOT_OBJ_TYPE_COMMIT, NULL, repo);
9393 error = got_object_id_str(&commit_id_str, commit_id);
9397 error = got_object_open_as_commit(&commit, repo, commit_id);
9400 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
9402 error = got_error(GOT_ERR_ROOT_COMMIT);
9406 memset(&upa, 0, sizeof(upa));
9407 error = got_worktree_merge_files(worktree, commit_id, &pid->id,
9408 repo, update_progress, &upa, check_cancelled, NULL);
9412 if (upa.did_something)
9413 printf("Backed out commit %s\n", commit_id_str);
9414 print_merge_progress_stats(&upa);
9417 got_object_commit_close(commit);
9418 free(commit_id_str);
9420 got_worktree_close(worktree);
9422 const struct got_error *close_err = got_repo_close(repo);
9427 const struct got_error *pack_err =
9428 got_repo_pack_fds_close(pack_fds);
9438 fprintf(stderr, "usage: %s rebase [-a] [-c] [-l] [-X] [branch]\n",
9444 trim_logmsg(char *logmsg, int limit)
9449 len = strlen(logmsg);
9453 nl = strchr(logmsg, '\n');
9458 static const struct got_error *
9459 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
9461 const struct got_error *err;
9462 char *logmsg0 = NULL;
9465 err = got_object_commit_get_logmsg(&logmsg0, commit);
9470 while (isspace((unsigned char)s[0]))
9473 *logmsg = strdup(s);
9474 if (*logmsg == NULL) {
9475 err = got_error_from_errno("strdup");
9479 trim_logmsg(*logmsg, limit);
9485 static const struct got_error *
9486 show_rebase_merge_conflict(struct got_object_id *id,
9487 struct got_repository *repo)
9489 const struct got_error *err;
9490 struct got_commit_object *commit = NULL;
9491 char *id_str = NULL, *logmsg = NULL;
9493 err = got_object_open_as_commit(&commit, repo, id);
9497 err = got_object_id_str(&id_str, id);
9503 err = get_short_logmsg(&logmsg, 42, commit);
9507 printf("%s -> merge conflict: %s\n", id_str, logmsg);
9510 got_object_commit_close(commit);
9515 static const struct got_error *
9516 show_rebase_progress(struct got_commit_object *commit,
9517 struct got_object_id *old_id, struct got_object_id *new_id)
9519 const struct got_error *err;
9520 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
9522 err = got_object_id_str(&old_id_str, old_id);
9527 err = got_object_id_str(&new_id_str, new_id);
9532 old_id_str[12] = '\0';
9534 new_id_str[12] = '\0';
9536 err = get_short_logmsg(&logmsg, 42, commit);
9540 printf("%s -> %s: %s\n", old_id_str,
9541 new_id_str ? new_id_str : "no-op change", logmsg);
9549 static const struct got_error *
9550 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
9551 struct got_reference *branch, struct got_reference *new_base_branch,
9552 struct got_reference *tmp_branch, struct got_repository *repo,
9555 printf("Switching work tree to %s\n", got_ref_get_name(branch));
9556 return got_worktree_rebase_complete(worktree, fileindex,
9557 new_base_branch, tmp_branch, branch, repo, create_backup);
9560 static const struct got_error *
9561 rebase_commit(struct got_pathlist_head *merged_paths,
9562 struct got_worktree *worktree, struct got_fileindex *fileindex,
9563 struct got_reference *tmp_branch,
9564 struct got_object_id *commit_id, struct got_repository *repo)
9566 const struct got_error *error;
9567 struct got_commit_object *commit;
9568 struct got_object_id *new_commit_id;
9570 error = got_object_open_as_commit(&commit, repo, commit_id);
9574 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
9575 worktree, fileindex, tmp_branch, commit, commit_id, repo);
9577 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
9579 error = show_rebase_progress(commit, commit_id, NULL);
9581 error = show_rebase_progress(commit, commit_id, new_commit_id);
9582 free(new_commit_id);
9585 got_object_commit_close(commit);
9589 struct check_path_prefix_arg {
9590 const char *path_prefix;
9595 static const struct got_error *
9596 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
9597 struct got_blob_object *blob2, FILE *f1, FILE *f2,
9598 struct got_object_id *id1, struct got_object_id *id2,
9599 const char *path1, const char *path2,
9600 mode_t mode1, mode_t mode2, struct got_repository *repo)
9602 struct check_path_prefix_arg *a = arg;
9604 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
9605 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
9606 return got_error(a->errcode);
9611 static const struct got_error *
9612 check_path_prefix(struct got_object_id *parent_id,
9613 struct got_object_id *commit_id, const char *path_prefix,
9614 int errcode, struct got_repository *repo)
9616 const struct got_error *err;
9617 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
9618 struct got_commit_object *commit = NULL, *parent_commit = NULL;
9619 struct check_path_prefix_arg cpp_arg;
9621 if (got_path_is_root_dir(path_prefix))
9624 err = got_object_open_as_commit(&commit, repo, commit_id);
9628 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
9632 err = got_object_open_as_tree(&tree1, repo,
9633 got_object_commit_get_tree_id(parent_commit));
9637 err = got_object_open_as_tree(&tree2, repo,
9638 got_object_commit_get_tree_id(commit));
9642 cpp_arg.path_prefix = path_prefix;
9643 while (cpp_arg.path_prefix[0] == '/')
9644 cpp_arg.path_prefix++;
9645 cpp_arg.len = strlen(cpp_arg.path_prefix);
9646 cpp_arg.errcode = errcode;
9647 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
9648 check_path_prefix_in_diff, &cpp_arg, 0);
9651 got_object_tree_close(tree1);
9653 got_object_tree_close(tree2);
9655 got_object_commit_close(commit);
9657 got_object_commit_close(parent_commit);
9661 static const struct got_error *
9662 collect_commits(struct got_object_id_queue *commits,
9663 struct got_object_id *initial_commit_id,
9664 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
9665 const char *path_prefix, int path_prefix_errcode,
9666 struct got_repository *repo)
9668 const struct got_error *err = NULL;
9669 struct got_commit_graph *graph = NULL;
9670 struct got_object_id *parent_id = NULL;
9671 struct got_object_qid *qid;
9672 struct got_object_id *commit_id = initial_commit_id;
9674 err = got_commit_graph_open(&graph, "/", 1);
9678 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
9679 check_cancelled, NULL);
9682 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
9683 err = got_commit_graph_iter_next(&parent_id, graph, repo,
9684 check_cancelled, NULL);
9686 if (err->code == GOT_ERR_ITER_COMPLETED) {
9687 err = got_error_msg(GOT_ERR_ANCESTRY,
9688 "ran out of commits to rebase before "
9689 "youngest common ancestor commit has "
9694 err = check_path_prefix(parent_id, commit_id,
9695 path_prefix, path_prefix_errcode, repo);
9699 err = got_object_qid_alloc(&qid, commit_id);
9702 STAILQ_INSERT_HEAD(commits, qid, entry);
9703 commit_id = parent_id;
9707 got_commit_graph_close(graph);
9711 static const struct got_error *
9712 get_commit_brief_str(char **brief_str, struct got_commit_object *commit)
9714 const struct got_error *err = NULL;
9715 time_t committer_time;
9717 char datebuf[11]; /* YYYY-MM-DD + NUL */
9718 char *author0 = NULL, *author, *smallerthan;
9719 char *logmsg0 = NULL, *logmsg, *newline;
9721 committer_time = got_object_commit_get_committer_time(commit);
9722 if (gmtime_r(&committer_time, &tm) == NULL)
9723 return got_error_from_errno("gmtime_r");
9724 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d", &tm) == 0)
9725 return got_error(GOT_ERR_NO_SPACE);
9727 author0 = strdup(got_object_commit_get_author(commit));
9728 if (author0 == NULL)
9729 return got_error_from_errno("strdup");
9731 smallerthan = strchr(author, '<');
9732 if (smallerthan && smallerthan[1] != '\0')
9733 author = smallerthan + 1;
9734 author[strcspn(author, "@>")] = '\0';
9736 err = got_object_commit_get_logmsg(&logmsg0, commit);
9740 while (*logmsg == '\n')
9742 newline = strchr(logmsg, '\n');
9746 if (asprintf(brief_str, "%s %s %s",
9747 datebuf, author, logmsg) == -1)
9748 err = got_error_from_errno("asprintf");
9755 static const struct got_error *
9756 delete_backup_ref(struct got_reference *ref, struct got_object_id *id,
9757 struct got_repository *repo)
9759 const struct got_error *err;
9762 err = got_object_id_str(&id_str, id);
9766 err = got_ref_delete(ref, repo);
9770 printf("Deleted %s: %s\n", got_ref_get_name(ref), id_str);
9776 static const struct got_error *
9777 print_backup_ref(const char *branch_name, const char *new_id_str,
9778 struct got_object_id *old_commit_id, struct got_commit_object *old_commit,
9779 struct got_reflist_object_id_map *refs_idmap,
9780 struct got_repository *repo)
9782 const struct got_error *err = NULL;
9783 struct got_reflist_head *refs;
9784 char *refs_str = NULL;
9785 struct got_object_id *new_commit_id = NULL;
9786 struct got_commit_object *new_commit = NULL;
9787 char *new_commit_brief_str = NULL;
9788 struct got_object_id *yca_id = NULL;
9789 struct got_commit_object *yca_commit = NULL;
9790 char *yca_id_str = NULL, *yca_brief_str = NULL;
9791 char *custom_refs_str;
9793 if (asprintf(&custom_refs_str, "formerly %s", branch_name) == -1)
9794 return got_error_from_errno("asprintf");
9796 err = print_commit(old_commit, old_commit_id, repo, NULL, NULL,
9797 0, 0, refs_idmap, custom_refs_str);
9801 err = got_object_resolve_id_str(&new_commit_id, repo, new_id_str);
9805 refs = got_reflist_object_id_map_lookup(refs_idmap, new_commit_id);
9807 err = build_refs_str(&refs_str, refs, new_commit_id, repo, 0);
9812 err = got_object_open_as_commit(&new_commit, repo, new_commit_id);
9816 err = get_commit_brief_str(&new_commit_brief_str, new_commit);
9820 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
9821 old_commit_id, new_commit_id, 1, repo, check_cancelled, NULL);
9825 printf("has become commit %s%s%s%s\n %s\n", new_id_str,
9826 refs_str ? " (" : "", refs_str ? refs_str : "",
9827 refs_str ? ")" : "", new_commit_brief_str);
9828 if (yca_id && got_object_id_cmp(yca_id, new_commit_id) != 0 &&
9829 got_object_id_cmp(yca_id, old_commit_id) != 0) {
9833 err = got_object_open_as_commit(&yca_commit, repo, yca_id);
9837 err = get_commit_brief_str(&yca_brief_str, yca_commit);
9841 err = got_object_id_str(&yca_id_str, yca_id);
9845 refs = got_reflist_object_id_map_lookup(refs_idmap, yca_id);
9847 err = build_refs_str(&refs_str, refs, yca_id, repo, 0);
9851 printf("history forked at %s%s%s%s\n %s\n",
9853 refs_str ? " (" : "", refs_str ? refs_str : "",
9854 refs_str ? ")" : "", yca_brief_str);
9857 free(custom_refs_str);
9858 free(new_commit_id);
9862 free(yca_brief_str);
9864 got_object_commit_close(new_commit);
9866 got_object_commit_close(yca_commit);
9871 static const struct got_error *
9872 process_backup_refs(const char *backup_ref_prefix,
9873 const char *wanted_branch_name,
9874 int delete, struct got_repository *repo)
9876 const struct got_error *err;
9877 struct got_reflist_head refs, backup_refs;
9878 struct got_reflist_entry *re;
9879 const size_t backup_ref_prefix_len = strlen(backup_ref_prefix);
9880 struct got_object_id *old_commit_id = NULL;
9881 char *branch_name = NULL;
9882 struct got_commit_object *old_commit = NULL;
9883 struct got_reflist_object_id_map *refs_idmap = NULL;
9884 int wanted_branch_found = 0;
9887 TAILQ_INIT(&backup_refs);
9889 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
9893 err = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
9897 if (wanted_branch_name) {
9898 if (strncmp(wanted_branch_name, "refs/heads/", 11) == 0)
9899 wanted_branch_name += 11;
9902 err = got_ref_list(&backup_refs, repo, backup_ref_prefix,
9903 got_ref_cmp_by_commit_timestamp_descending, repo);
9907 TAILQ_FOREACH(re, &backup_refs, entry) {
9908 const char *refname = got_ref_get_name(re->ref);
9911 err = check_cancelled(NULL);
9915 err = got_ref_resolve(&old_commit_id, repo, re->ref);
9919 err = got_object_open_as_commit(&old_commit, repo,
9924 if (strncmp(backup_ref_prefix, refname,
9925 backup_ref_prefix_len) == 0)
9926 refname += backup_ref_prefix_len;
9928 while (refname[0] == '/')
9931 branch_name = strdup(refname);
9932 if (branch_name == NULL) {
9933 err = got_error_from_errno("strdup");
9936 slash = strrchr(branch_name, '/');
9939 refname += strlen(branch_name) + 1;
9942 if (wanted_branch_name == NULL ||
9943 strcmp(wanted_branch_name, branch_name) == 0) {
9944 wanted_branch_found = 1;
9946 err = delete_backup_ref(re->ref,
9947 old_commit_id, repo);
9949 err = print_backup_ref(branch_name, refname,
9950 old_commit_id, old_commit, refs_idmap,
9957 free(old_commit_id);
9958 old_commit_id = NULL;
9961 got_object_commit_close(old_commit);
9965 if (wanted_branch_name && !wanted_branch_found) {
9966 err = got_error_fmt(GOT_ERR_NOT_REF,
9967 "%s/%s/", backup_ref_prefix, wanted_branch_name);
9971 got_reflist_object_id_map_free(refs_idmap);
9972 got_ref_list_free(&refs);
9973 got_ref_list_free(&backup_refs);
9974 free(old_commit_id);
9977 got_object_commit_close(old_commit);
9981 static const struct got_error *
9982 abort_progress(void *arg, unsigned char status, const char *path)
9985 * Unversioned files should not clutter progress output when
9986 * an operation is aborted.
9988 if (status == GOT_STATUS_UNVERSIONED)
9991 return update_progress(arg, status, path);
9994 static const struct got_error *
9995 cmd_rebase(int argc, char *argv[])
9997 const struct got_error *error = NULL;
9998 struct got_worktree *worktree = NULL;
9999 struct got_repository *repo = NULL;
10000 struct got_fileindex *fileindex = NULL;
10002 struct got_reference *branch = NULL;
10003 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
10004 struct got_object_id *commit_id = NULL, *parent_id = NULL;
10005 struct got_object_id *resume_commit_id = NULL;
10006 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
10007 struct got_commit_object *commit = NULL;
10008 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
10009 int histedit_in_progress = 0, merge_in_progress = 0;
10010 int create_backup = 1, list_backups = 0, delete_backups = 0;
10011 struct got_object_id_queue commits;
10012 struct got_pathlist_head merged_paths;
10013 const struct got_object_id_queue *parent_ids;
10014 struct got_object_qid *qid, *pid;
10015 struct got_update_progress_arg upa;
10016 int *pack_fds = NULL;
10018 STAILQ_INIT(&commits);
10019 TAILQ_INIT(&merged_paths);
10020 memset(&upa, 0, sizeof(upa));
10022 while ((ch = getopt(argc, argv, "aclX")) != -1) {
10028 continue_rebase = 1;
10034 delete_backups = 1;
10046 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10047 "unveil", NULL) == -1)
10050 if (list_backups) {
10052 option_conflict('l', 'a');
10053 if (continue_rebase)
10054 option_conflict('l', 'c');
10055 if (delete_backups)
10056 option_conflict('l', 'X');
10057 if (argc != 0 && argc != 1)
10059 } else if (delete_backups) {
10061 option_conflict('X', 'a');
10062 if (continue_rebase)
10063 option_conflict('X', 'c');
10065 option_conflict('l', 'X');
10066 if (argc != 0 && argc != 1)
10069 if (abort_rebase && continue_rebase)
10071 else if (abort_rebase || continue_rebase) {
10074 } else if (argc != 1)
10078 cwd = getcwd(NULL, 0);
10080 error = got_error_from_errno("getcwd");
10084 error = got_repo_pack_fds_open(&pack_fds);
10088 error = got_worktree_open(&worktree, cwd);
10090 if (list_backups || delete_backups) {
10091 if (error->code != GOT_ERR_NOT_WORKTREE)
10094 if (error->code == GOT_ERR_NOT_WORKTREE)
10095 error = wrap_not_worktree_error(error,
10101 error = got_repo_open(&repo,
10102 worktree ? got_worktree_get_repo_path(worktree) : cwd, NULL,
10107 error = apply_unveil(got_repo_get_path(repo), 0,
10108 worktree ? got_worktree_get_root_path(worktree) : NULL);
10112 if (list_backups || delete_backups) {
10113 error = process_backup_refs(
10114 GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
10115 argc == 1 ? argv[0] : NULL, delete_backups, repo);
10116 goto done; /* nothing else to do */
10119 error = got_worktree_histedit_in_progress(&histedit_in_progress,
10123 if (histedit_in_progress) {
10124 error = got_error(GOT_ERR_HISTEDIT_BUSY);
10128 error = got_worktree_merge_in_progress(&merge_in_progress,
10132 if (merge_in_progress) {
10133 error = got_error(GOT_ERR_MERGE_BUSY);
10137 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
10141 if (abort_rebase) {
10142 if (!rebase_in_progress) {
10143 error = got_error(GOT_ERR_NOT_REBASING);
10146 error = got_worktree_rebase_continue(&resume_commit_id,
10147 &new_base_branch, &tmp_branch, &branch, &fileindex,
10151 printf("Switching work tree to %s\n",
10152 got_ref_get_symref_target(new_base_branch));
10153 error = got_worktree_rebase_abort(worktree, fileindex, repo,
10154 new_base_branch, abort_progress, &upa);
10157 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
10158 print_merge_progress_stats(&upa);
10159 goto done; /* nothing else to do */
10162 if (continue_rebase) {
10163 if (!rebase_in_progress) {
10164 error = got_error(GOT_ERR_NOT_REBASING);
10167 error = got_worktree_rebase_continue(&resume_commit_id,
10168 &new_base_branch, &tmp_branch, &branch, &fileindex,
10173 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
10174 resume_commit_id, repo);
10178 yca_id = got_object_id_dup(resume_commit_id);
10179 if (yca_id == NULL) {
10180 error = got_error_from_errno("got_object_id_dup");
10184 error = got_ref_open(&branch, repo, argv[0], 0);
10189 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
10193 if (!continue_rebase) {
10194 struct got_object_id *base_commit_id;
10196 base_commit_id = got_worktree_get_base_commit_id(worktree);
10197 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
10198 base_commit_id, branch_head_commit_id, 1, repo,
10199 check_cancelled, NULL);
10202 if (yca_id == NULL) {
10203 error = got_error_msg(GOT_ERR_ANCESTRY,
10204 "specified branch shares no common ancestry "
10205 "with work tree's branch");
10209 error = check_same_branch(base_commit_id, branch, yca_id, repo);
10211 if (error->code != GOT_ERR_ANCESTRY)
10215 struct got_pathlist_head paths;
10216 printf("%s is already based on %s\n",
10217 got_ref_get_name(branch),
10218 got_worktree_get_head_ref_name(worktree));
10219 error = switch_head_ref(branch, branch_head_commit_id,
10223 error = got_worktree_set_base_commit_id(worktree, repo,
10224 branch_head_commit_id);
10227 TAILQ_INIT(&paths);
10228 error = got_pathlist_append(&paths, "", NULL);
10231 error = got_worktree_checkout_files(worktree,
10232 &paths, repo, update_progress, &upa,
10233 check_cancelled, NULL);
10234 got_pathlist_free(&paths);
10237 if (upa.did_something) {
10239 error = got_object_id_str(&id_str,
10240 branch_head_commit_id);
10243 printf("Updated to %s: %s\n",
10244 got_worktree_get_head_ref_name(worktree),
10248 printf("Already up-to-date\n");
10249 print_update_progress_stats(&upa);
10254 commit_id = branch_head_commit_id;
10255 error = got_object_open_as_commit(&commit, repo, commit_id);
10259 parent_ids = got_object_commit_get_parent_ids(commit);
10260 pid = STAILQ_FIRST(parent_ids);
10262 error = got_error(GOT_ERR_EMPTY_REBASE);
10265 error = collect_commits(&commits, commit_id, &pid->id,
10266 yca_id, got_worktree_get_path_prefix(worktree),
10267 GOT_ERR_REBASE_PATH, repo);
10268 got_object_commit_close(commit);
10273 if (!continue_rebase) {
10274 error = got_worktree_rebase_prepare(&new_base_branch,
10275 &tmp_branch, &fileindex, worktree, branch, repo);
10280 if (STAILQ_EMPTY(&commits)) {
10281 if (continue_rebase) {
10282 error = rebase_complete(worktree, fileindex,
10283 branch, new_base_branch, tmp_branch, repo,
10287 /* Fast-forward the reference of the branch. */
10288 struct got_object_id *new_head_commit_id;
10290 error = got_ref_resolve(&new_head_commit_id, repo,
10294 error = got_object_id_str(&id_str, new_head_commit_id);
10295 printf("Forwarding %s to commit %s\n",
10296 got_ref_get_name(branch), id_str);
10298 error = got_ref_change_ref(branch,
10299 new_head_commit_id);
10302 /* No backup needed since objects did not change. */
10308 STAILQ_FOREACH(qid, &commits, entry) {
10310 commit_id = &qid->id;
10311 parent_id = pid ? &pid->id : yca_id;
10314 memset(&upa, 0, sizeof(upa));
10315 error = got_worktree_rebase_merge_files(&merged_paths,
10316 worktree, fileindex, parent_id, commit_id, repo,
10317 update_progress, &upa, check_cancelled, NULL);
10321 print_merge_progress_stats(&upa);
10322 if (upa.conflicts > 0 || upa.missing > 0 ||
10323 upa.not_deleted > 0 || upa.unversioned > 0) {
10324 if (upa.conflicts > 0) {
10325 error = show_rebase_merge_conflict(&qid->id,
10330 got_worktree_rebase_pathlist_free(&merged_paths);
10334 error = rebase_commit(&merged_paths, worktree, fileindex,
10335 tmp_branch, commit_id, repo);
10336 got_worktree_rebase_pathlist_free(&merged_paths);
10341 if (upa.conflicts > 0 || upa.missing > 0 ||
10342 upa.not_deleted > 0 || upa.unversioned > 0) {
10343 error = got_worktree_rebase_postpone(worktree, fileindex);
10346 if (upa.conflicts > 0 && upa.missing == 0 &&
10347 upa.not_deleted == 0 && upa.unversioned == 0) {
10348 error = got_error_msg(GOT_ERR_CONFLICTS,
10349 "conflicts must be resolved before rebasing "
10351 } else if (upa.conflicts > 0) {
10352 error = got_error_msg(GOT_ERR_CONFLICTS,
10353 "conflicts must be resolved before rebasing "
10354 "can continue; changes destined for some "
10355 "files were not yet merged and should be "
10356 "merged manually if required before the "
10357 "rebase operation is continued");
10359 error = got_error_msg(GOT_ERR_CONFLICTS,
10360 "changes destined for some files were not "
10361 "yet merged and should be merged manually "
10362 "if required before the rebase operation "
10366 error = rebase_complete(worktree, fileindex, branch,
10367 new_base_branch, tmp_branch, repo, create_backup);
10369 got_object_id_queue_free(&commits);
10370 free(branch_head_commit_id);
10371 free(resume_commit_id);
10374 got_object_commit_close(commit);
10376 got_ref_close(branch);
10377 if (new_base_branch)
10378 got_ref_close(new_base_branch);
10380 got_ref_close(tmp_branch);
10382 got_worktree_close(worktree);
10384 const struct got_error *close_err = got_repo_close(repo);
10389 const struct got_error *pack_err =
10390 got_repo_pack_fds_close(pack_fds);
10398 usage_histedit(void)
10400 fprintf(stderr, "usage: %s histedit [-a] [-c] [-e] [-f] "
10401 "[-F histedit-script] [-m] [-l] [-X] [branch]\n",
10406 #define GOT_HISTEDIT_PICK 'p'
10407 #define GOT_HISTEDIT_EDIT 'e'
10408 #define GOT_HISTEDIT_FOLD 'f'
10409 #define GOT_HISTEDIT_DROP 'd'
10410 #define GOT_HISTEDIT_MESG 'm'
10412 static const struct got_histedit_cmd {
10413 unsigned char code;
10416 } got_histedit_cmds[] = {
10417 { GOT_HISTEDIT_PICK, "pick", "use commit" },
10418 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
10419 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
10421 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
10422 { GOT_HISTEDIT_MESG, "mesg",
10423 "single-line log message for commit above (open editor if empty)" },
10426 struct got_histedit_list_entry {
10427 TAILQ_ENTRY(got_histedit_list_entry) entry;
10428 struct got_object_id *commit_id;
10429 const struct got_histedit_cmd *cmd;
10432 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
10434 static const struct got_error *
10435 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
10436 FILE *f, struct got_repository *repo)
10438 const struct got_error *err = NULL;
10439 char *logmsg = NULL, *id_str = NULL;
10440 struct got_commit_object *commit = NULL;
10443 err = got_object_open_as_commit(&commit, repo, commit_id);
10447 err = get_short_logmsg(&logmsg, 34, commit);
10451 err = got_object_id_str(&id_str, commit_id);
10455 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
10457 err = got_ferror(f, GOT_ERR_IO);
10460 got_object_commit_close(commit);
10466 static const struct got_error *
10467 histedit_write_commit_list(struct got_object_id_queue *commits,
10468 FILE *f, int edit_logmsg_only, int fold_only, int edit_only,
10469 struct got_repository *repo)
10471 const struct got_error *err = NULL;
10472 struct got_object_qid *qid;
10473 const char *histedit_cmd = NULL;
10475 if (STAILQ_EMPTY(commits))
10476 return got_error(GOT_ERR_EMPTY_HISTEDIT);
10478 STAILQ_FOREACH(qid, commits, entry) {
10479 histedit_cmd = got_histedit_cmds[0].name;
10481 histedit_cmd = "edit";
10482 else if (fold_only && STAILQ_NEXT(qid, entry) != NULL)
10483 histedit_cmd = "fold";
10484 err = histedit_write_commit(&qid->id, histedit_cmd, f, repo);
10487 if (edit_logmsg_only) {
10488 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
10490 err = got_ferror(f, GOT_ERR_IO);
10499 static const struct got_error *
10500 write_cmd_list(FILE *f, const char *branch_name,
10501 struct got_object_id_queue *commits)
10503 const struct got_error *err = NULL;
10507 struct got_object_qid *qid;
10509 qid = STAILQ_FIRST(commits);
10510 err = got_object_id_str(&id_str, &qid->id);
10515 "# Editing the history of branch '%s' starting at\n"
10517 "# Commits will be processed in order from top to "
10518 "bottom of this file.\n", branch_name, id_str);
10520 err = got_ferror(f, GOT_ERR_IO);
10524 n = fprintf(f, "# Available histedit commands:\n");
10526 err = got_ferror(f, GOT_ERR_IO);
10530 for (i = 0; i < nitems(got_histedit_cmds); i++) {
10531 const struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
10532 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
10535 err = got_ferror(f, GOT_ERR_IO);
10544 static const struct got_error *
10545 histedit_syntax_error(int lineno)
10547 static char msg[42];
10550 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
10552 if (ret == -1 || ret >= sizeof(msg))
10553 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
10555 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
10558 static const struct got_error *
10559 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
10560 char *logmsg, struct got_repository *repo)
10562 const struct got_error *err;
10563 struct got_commit_object *folded_commit = NULL;
10564 char *id_str, *folded_logmsg = NULL;
10566 err = got_object_id_str(&id_str, hle->commit_id);
10570 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
10574 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
10577 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
10578 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
10579 folded_logmsg) == -1) {
10580 err = got_error_from_errno("asprintf");
10584 got_object_commit_close(folded_commit);
10586 free(folded_logmsg);
10590 static struct got_histedit_list_entry *
10591 get_folded_commits(struct got_histedit_list_entry *hle)
10593 struct got_histedit_list_entry *prev, *folded = NULL;
10595 prev = TAILQ_PREV(hle, got_histedit_list, entry);
10596 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
10597 prev->cmd->code == GOT_HISTEDIT_DROP)) {
10598 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
10600 prev = TAILQ_PREV(prev, got_histedit_list, entry);
10606 static const struct got_error *
10607 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
10608 struct got_repository *repo)
10610 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
10611 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
10612 const struct got_error *err = NULL;
10613 struct got_commit_object *commit = NULL;
10616 struct got_histedit_list_entry *folded = NULL;
10618 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
10622 folded = get_folded_commits(hle);
10624 while (folded != hle) {
10625 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
10626 folded = TAILQ_NEXT(folded, entry);
10629 err = append_folded_commit_msg(&new_msg, folded,
10635 folded = TAILQ_NEXT(folded, entry);
10639 err = got_object_id_str(&id_str, hle->commit_id);
10642 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
10645 logmsg_len = asprintf(&new_msg,
10646 "%s\n# original log message of commit %s: %s",
10647 logmsg ? logmsg : "", id_str, orig_logmsg);
10648 if (logmsg_len == -1) {
10649 err = got_error_from_errno("asprintf");
10655 err = got_object_id_str(&id_str, hle->commit_id);
10659 err = got_opentemp_named_fd(&logmsg_path, &fd,
10660 GOT_TMPDIR_STR "/got-logmsg");
10664 write(fd, logmsg, logmsg_len);
10667 err = get_editor(&editor);
10671 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg,
10674 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
10677 hle->logmsg = strdup(new_msg);
10678 if (hle->logmsg == NULL)
10679 err = got_error_from_errno("strdup");
10682 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
10683 err = got_error_from_errno2("unlink", logmsg_path);
10689 got_object_commit_close(commit);
10693 static const struct got_error *
10694 histedit_parse_list(struct got_histedit_list *histedit_cmds,
10695 FILE *f, struct got_repository *repo)
10697 const struct got_error *err = NULL;
10698 char *line = NULL, *p, *end;
10701 int lineno = 0, lastcmd = -1;
10702 const struct got_histedit_cmd *cmd;
10703 struct got_object_id *commit_id = NULL;
10704 struct got_histedit_list_entry *hle = NULL;
10707 len = getline(&line, &size, f);
10709 const struct got_error *getline_err;
10712 getline_err = got_error_from_errno("getline");
10713 err = got_ferror(f, getline_err->code);
10718 while (isspace((unsigned char)p[0]))
10720 if (p[0] == '#' || p[0] == '\0') {
10726 for (i = 0; i < nitems(got_histedit_cmds); i++) {
10727 cmd = &got_histedit_cmds[i];
10728 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
10729 isspace((unsigned char)p[strlen(cmd->name)])) {
10730 p += strlen(cmd->name);
10733 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
10738 if (i == nitems(got_histedit_cmds)) {
10739 err = histedit_syntax_error(lineno);
10742 while (isspace((unsigned char)p[0]))
10744 if (cmd->code == GOT_HISTEDIT_MESG) {
10745 if (lastcmd != GOT_HISTEDIT_PICK &&
10746 lastcmd != GOT_HISTEDIT_EDIT) {
10747 err = got_error(GOT_ERR_HISTEDIT_CMD);
10750 if (p[0] == '\0') {
10751 err = histedit_edit_logmsg(hle, repo);
10755 hle->logmsg = strdup(p);
10756 if (hle->logmsg == NULL) {
10757 err = got_error_from_errno("strdup");
10763 lastcmd = cmd->code;
10767 while (end[0] && !isspace((unsigned char)end[0]))
10771 err = got_object_resolve_id_str(&commit_id, repo, p);
10773 /* override error code */
10774 err = histedit_syntax_error(lineno);
10778 hle = malloc(sizeof(*hle));
10780 err = got_error_from_errno("malloc");
10784 hle->commit_id = commit_id;
10785 hle->logmsg = NULL;
10789 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
10790 lastcmd = cmd->code;
10798 static const struct got_error *
10799 histedit_check_script(struct got_histedit_list *histedit_cmds,
10800 struct got_object_id_queue *commits, struct got_repository *repo)
10802 const struct got_error *err = NULL;
10803 struct got_object_qid *qid;
10804 struct got_histedit_list_entry *hle;
10805 static char msg[92];
10808 if (TAILQ_EMPTY(histedit_cmds))
10809 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
10810 "histedit script contains no commands");
10811 if (STAILQ_EMPTY(commits))
10812 return got_error(GOT_ERR_EMPTY_HISTEDIT);
10814 TAILQ_FOREACH(hle, histedit_cmds, entry) {
10815 struct got_histedit_list_entry *hle2;
10816 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
10819 if (got_object_id_cmp(hle->commit_id,
10820 hle2->commit_id) != 0)
10822 err = got_object_id_str(&id_str, hle->commit_id);
10825 snprintf(msg, sizeof(msg), "commit %s is listed "
10826 "more than once in histedit script", id_str);
10828 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
10832 STAILQ_FOREACH(qid, commits, entry) {
10833 TAILQ_FOREACH(hle, histedit_cmds, entry) {
10834 if (got_object_id_cmp(&qid->id, hle->commit_id) == 0)
10838 err = got_object_id_str(&id_str, &qid->id);
10841 snprintf(msg, sizeof(msg),
10842 "commit %s missing from histedit script", id_str);
10844 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
10848 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
10849 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
10850 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
10851 "last commit in histedit script cannot be folded");
10856 static const struct got_error *
10857 histedit_run_editor(struct got_histedit_list *histedit_cmds,
10858 const char *path, struct got_object_id_queue *commits,
10859 struct got_repository *repo)
10861 const struct got_error *err = NULL;
10865 err = get_editor(&editor);
10869 if (spawn_editor(editor, path) == -1) {
10870 err = got_error_from_errno("failed spawning editor");
10874 f = fopen(path, "re");
10876 err = got_error_from_errno("fopen");
10879 err = histedit_parse_list(histedit_cmds, f, repo);
10883 err = histedit_check_script(histedit_cmds, commits, repo);
10885 if (f && fclose(f) == EOF && err == NULL)
10886 err = got_error_from_errno("fclose");
10891 static const struct got_error *
10892 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
10893 struct got_object_id_queue *, const char *, const char *,
10894 struct got_repository *);
10896 static const struct got_error *
10897 histedit_edit_script(struct got_histedit_list *histedit_cmds,
10898 struct got_object_id_queue *commits, const char *branch_name,
10899 int edit_logmsg_only, int fold_only, int edit_only,
10900 struct got_repository *repo)
10902 const struct got_error *err;
10906 err = got_opentemp_named(&path, &f, "got-histedit");
10910 err = write_cmd_list(f, branch_name, commits);
10914 err = histedit_write_commit_list(commits, f, edit_logmsg_only,
10915 fold_only, edit_only, repo);
10919 if (edit_logmsg_only || fold_only || edit_only) {
10921 err = histedit_parse_list(histedit_cmds, f, repo);
10923 if (fclose(f) == EOF) {
10924 err = got_error_from_errno("fclose");
10928 err = histedit_run_editor(histedit_cmds, path, commits, repo);
10930 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
10931 err->code != GOT_ERR_HISTEDIT_CMD)
10933 err = histedit_edit_list_retry(histedit_cmds, err,
10934 commits, path, branch_name, repo);
10938 if (f && fclose(f) == EOF && err == NULL)
10939 err = got_error_from_errno("fclose");
10940 if (path && unlink(path) != 0 && err == NULL)
10941 err = got_error_from_errno2("unlink", path);
10946 static const struct got_error *
10947 histedit_save_list(struct got_histedit_list *histedit_cmds,
10948 struct got_worktree *worktree, struct got_repository *repo)
10950 const struct got_error *err = NULL;
10953 struct got_histedit_list_entry *hle;
10954 struct got_commit_object *commit = NULL;
10956 err = got_worktree_get_histedit_script_path(&path, worktree);
10960 f = fopen(path, "we");
10962 err = got_error_from_errno2("fopen", path);
10965 TAILQ_FOREACH(hle, histedit_cmds, entry) {
10966 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
10972 int n = fprintf(f, "%c %s\n",
10973 GOT_HISTEDIT_MESG, hle->logmsg);
10975 err = got_ferror(f, GOT_ERR_IO);
10981 if (f && fclose(f) == EOF && err == NULL)
10982 err = got_error_from_errno("fclose");
10985 got_object_commit_close(commit);
10990 histedit_free_list(struct got_histedit_list *histedit_cmds)
10992 struct got_histedit_list_entry *hle;
10994 while ((hle = TAILQ_FIRST(histedit_cmds))) {
10995 TAILQ_REMOVE(histedit_cmds, hle, entry);
11000 static const struct got_error *
11001 histedit_load_list(struct got_histedit_list *histedit_cmds,
11002 const char *path, struct got_repository *repo)
11004 const struct got_error *err = NULL;
11007 f = fopen(path, "re");
11009 err = got_error_from_errno2("fopen", path);
11013 err = histedit_parse_list(histedit_cmds, f, repo);
11015 if (f && fclose(f) == EOF && err == NULL)
11016 err = got_error_from_errno("fclose");
11020 static const struct got_error *
11021 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
11022 const struct got_error *edit_err, struct got_object_id_queue *commits,
11023 const char *path, const char *branch_name, struct got_repository *repo)
11025 const struct got_error *err = NULL, *prev_err = edit_err;
11028 while (resp != 'c' && resp != 'r' && resp != 'a') {
11029 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
11030 "or (a)bort: ", getprogname(), prev_err->msg);
11035 histedit_free_list(histedit_cmds);
11036 err = histedit_run_editor(histedit_cmds, path, commits,
11039 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
11040 err->code != GOT_ERR_HISTEDIT_CMD)
11047 } else if (resp == 'r') {
11048 histedit_free_list(histedit_cmds);
11049 err = histedit_edit_script(histedit_cmds,
11050 commits, branch_name, 0, 0, 0, repo);
11052 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
11053 err->code != GOT_ERR_HISTEDIT_CMD)
11060 } else if (resp == 'a') {
11061 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
11064 printf("invalid response '%c'\n", resp);
11070 static const struct got_error *
11071 histedit_complete(struct got_worktree *worktree,
11072 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
11073 struct got_reference *branch, struct got_repository *repo)
11075 printf("Switching work tree to %s\n",
11076 got_ref_get_symref_target(branch));
11077 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
11081 static const struct got_error *
11082 show_histedit_progress(struct got_commit_object *commit,
11083 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
11085 const struct got_error *err;
11086 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
11088 err = got_object_id_str(&old_id_str, hle->commit_id);
11093 err = got_object_id_str(&new_id_str, new_id);
11098 old_id_str[12] = '\0';
11100 new_id_str[12] = '\0';
11103 logmsg = strdup(hle->logmsg);
11104 if (logmsg == NULL) {
11105 err = got_error_from_errno("strdup");
11108 trim_logmsg(logmsg, 42);
11110 err = get_short_logmsg(&logmsg, 42, commit);
11115 switch (hle->cmd->code) {
11116 case GOT_HISTEDIT_PICK:
11117 case GOT_HISTEDIT_EDIT:
11118 printf("%s -> %s: %s\n", old_id_str,
11119 new_id_str ? new_id_str : "no-op change", logmsg);
11121 case GOT_HISTEDIT_DROP:
11122 case GOT_HISTEDIT_FOLD:
11123 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
11135 static const struct got_error *
11136 histedit_commit(struct got_pathlist_head *merged_paths,
11137 struct got_worktree *worktree, struct got_fileindex *fileindex,
11138 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
11139 struct got_repository *repo)
11141 const struct got_error *err;
11142 struct got_commit_object *commit;
11143 struct got_object_id *new_commit_id;
11145 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
11146 && hle->logmsg == NULL) {
11147 err = histedit_edit_logmsg(hle, repo);
11152 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
11156 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
11157 worktree, fileindex, tmp_branch, commit, hle->commit_id,
11158 hle->logmsg, repo);
11160 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
11162 err = show_histedit_progress(commit, hle, NULL);
11164 err = show_histedit_progress(commit, hle, new_commit_id);
11165 free(new_commit_id);
11168 got_object_commit_close(commit);
11172 static const struct got_error *
11173 histedit_skip_commit(struct got_histedit_list_entry *hle,
11174 struct got_worktree *worktree, struct got_repository *repo)
11176 const struct got_error *error;
11177 struct got_commit_object *commit;
11179 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
11184 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
11188 error = show_histedit_progress(commit, hle, NULL);
11189 got_object_commit_close(commit);
11193 static const struct got_error *
11194 check_local_changes(void *arg, unsigned char status,
11195 unsigned char staged_status, const char *path,
11196 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
11197 struct got_object_id *commit_id, int dirfd, const char *de_name)
11199 int *have_local_changes = arg;
11202 case GOT_STATUS_ADD:
11203 case GOT_STATUS_DELETE:
11204 case GOT_STATUS_MODIFY:
11205 case GOT_STATUS_CONFLICT:
11206 *have_local_changes = 1;
11207 return got_error(GOT_ERR_CANCELLED);
11212 switch (staged_status) {
11213 case GOT_STATUS_ADD:
11214 case GOT_STATUS_DELETE:
11215 case GOT_STATUS_MODIFY:
11216 *have_local_changes = 1;
11217 return got_error(GOT_ERR_CANCELLED);
11225 static const struct got_error *
11226 cmd_histedit(int argc, char *argv[])
11228 const struct got_error *error = NULL;
11229 struct got_worktree *worktree = NULL;
11230 struct got_fileindex *fileindex = NULL;
11231 struct got_repository *repo = NULL;
11233 struct got_reference *branch = NULL;
11234 struct got_reference *tmp_branch = NULL;
11235 struct got_object_id *resume_commit_id = NULL;
11236 struct got_object_id *base_commit_id = NULL;
11237 struct got_object_id *head_commit_id = NULL;
11238 struct got_commit_object *commit = NULL;
11239 int ch, rebase_in_progress = 0, merge_in_progress = 0;
11240 struct got_update_progress_arg upa;
11241 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
11242 int edit_logmsg_only = 0, fold_only = 0, edit_only = 0;
11243 int list_backups = 0, delete_backups = 0;
11244 const char *edit_script_path = NULL;
11245 struct got_object_id_queue commits;
11246 struct got_pathlist_head merged_paths;
11247 const struct got_object_id_queue *parent_ids;
11248 struct got_object_qid *pid;
11249 struct got_histedit_list histedit_cmds;
11250 struct got_histedit_list_entry *hle;
11251 int *pack_fds = NULL;
11253 STAILQ_INIT(&commits);
11254 TAILQ_INIT(&histedit_cmds);
11255 TAILQ_INIT(&merged_paths);
11256 memset(&upa, 0, sizeof(upa));
11258 while ((ch = getopt(argc, argv, "acefF:mlX")) != -1) {
11273 edit_script_path = optarg;
11276 edit_logmsg_only = 1;
11282 delete_backups = 1;
11294 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
11295 "unveil", NULL) == -1)
11298 if (abort_edit && continue_edit)
11299 option_conflict('a', 'c');
11300 if (edit_script_path && edit_logmsg_only)
11301 option_conflict('F', 'm');
11302 if (abort_edit && edit_logmsg_only)
11303 option_conflict('a', 'm');
11304 if (continue_edit && edit_logmsg_only)
11305 option_conflict('c', 'm');
11306 if (abort_edit && fold_only)
11307 option_conflict('a', 'f');
11308 if (continue_edit && fold_only)
11309 option_conflict('c', 'f');
11310 if (fold_only && edit_logmsg_only)
11311 option_conflict('f', 'm');
11312 if (edit_script_path && fold_only)
11313 option_conflict('F', 'f');
11314 if (abort_edit && edit_only)
11315 option_conflict('a', 'e');
11316 if (continue_edit && edit_only)
11317 option_conflict('c', 'e');
11318 if (edit_only && edit_logmsg_only)
11319 option_conflict('e', 'm');
11320 if (edit_script_path && edit_only)
11321 option_conflict('F', 'e');
11322 if (list_backups) {
11324 option_conflict('l', 'a');
11326 option_conflict('l', 'c');
11327 if (edit_script_path)
11328 option_conflict('l', 'F');
11329 if (edit_logmsg_only)
11330 option_conflict('l', 'm');
11332 option_conflict('l', 'f');
11334 option_conflict('l', 'e');
11335 if (delete_backups)
11336 option_conflict('l', 'X');
11337 if (argc != 0 && argc != 1)
11339 } else if (delete_backups) {
11341 option_conflict('X', 'a');
11343 option_conflict('X', 'c');
11344 if (edit_script_path)
11345 option_conflict('X', 'F');
11346 if (edit_logmsg_only)
11347 option_conflict('X', 'm');
11349 option_conflict('X', 'f');
11351 option_conflict('X', 'e');
11353 option_conflict('X', 'l');
11354 if (argc != 0 && argc != 1)
11356 } else if (argc != 0)
11360 * This command cannot apply unveil(2) in all cases because the
11361 * user may choose to run an editor to edit the histedit script
11362 * and to edit individual commit log messages.
11363 * unveil(2) traverses exec(2); if an editor is used we have to
11364 * apply unveil after edit script and log messages have been written.
11365 * XXX TODO: Make use of unveil(2) where possible.
11368 cwd = getcwd(NULL, 0);
11370 error = got_error_from_errno("getcwd");
11374 error = got_repo_pack_fds_open(&pack_fds);
11378 error = got_worktree_open(&worktree, cwd);
11380 if (list_backups || delete_backups) {
11381 if (error->code != GOT_ERR_NOT_WORKTREE)
11384 if (error->code == GOT_ERR_NOT_WORKTREE)
11385 error = wrap_not_worktree_error(error,
11391 if (list_backups || delete_backups) {
11392 error = got_repo_open(&repo,
11393 worktree ? got_worktree_get_repo_path(worktree) : cwd,
11397 error = apply_unveil(got_repo_get_path(repo), 0,
11398 worktree ? got_worktree_get_root_path(worktree) : NULL);
11401 error = process_backup_refs(
11402 GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
11403 argc == 1 ? argv[0] : NULL, delete_backups, repo);
11404 goto done; /* nothing else to do */
11407 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
11412 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
11415 if (rebase_in_progress) {
11416 error = got_error(GOT_ERR_REBASING);
11420 error = got_worktree_merge_in_progress(&merge_in_progress, worktree,
11424 if (merge_in_progress) {
11425 error = got_error(GOT_ERR_MERGE_BUSY);
11429 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
11433 if (edit_in_progress && edit_logmsg_only) {
11434 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
11435 "histedit operation is in progress in this "
11436 "work tree and must be continued or aborted "
11437 "before the -m option can be used");
11440 if (edit_in_progress && fold_only) {
11441 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
11442 "histedit operation is in progress in this "
11443 "work tree and must be continued or aborted "
11444 "before the -f option can be used");
11447 if (edit_in_progress && edit_only) {
11448 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
11449 "histedit operation is in progress in this "
11450 "work tree and must be continued or aborted "
11451 "before the -e option can be used");
11455 if (edit_in_progress && abort_edit) {
11456 error = got_worktree_histedit_continue(&resume_commit_id,
11457 &tmp_branch, &branch, &base_commit_id, &fileindex,
11461 printf("Switching work tree to %s\n",
11462 got_ref_get_symref_target(branch));
11463 error = got_worktree_histedit_abort(worktree, fileindex, repo,
11464 branch, base_commit_id, abort_progress, &upa);
11467 printf("Histedit of %s aborted\n",
11468 got_ref_get_symref_target(branch));
11469 print_merge_progress_stats(&upa);
11470 goto done; /* nothing else to do */
11471 } else if (abort_edit) {
11472 error = got_error(GOT_ERR_NOT_HISTEDIT);
11476 if (continue_edit) {
11479 if (!edit_in_progress) {
11480 error = got_error(GOT_ERR_NOT_HISTEDIT);
11484 error = got_worktree_get_histedit_script_path(&path, worktree);
11488 error = histedit_load_list(&histedit_cmds, path, repo);
11493 error = got_worktree_histedit_continue(&resume_commit_id,
11494 &tmp_branch, &branch, &base_commit_id, &fileindex,
11499 error = got_ref_resolve(&head_commit_id, repo, branch);
11503 error = got_object_open_as_commit(&commit, repo,
11507 parent_ids = got_object_commit_get_parent_ids(commit);
11508 pid = STAILQ_FIRST(parent_ids);
11510 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
11513 error = collect_commits(&commits, head_commit_id, &pid->id,
11514 base_commit_id, got_worktree_get_path_prefix(worktree),
11515 GOT_ERR_HISTEDIT_PATH, repo);
11516 got_object_commit_close(commit);
11521 if (edit_in_progress) {
11522 error = got_error(GOT_ERR_HISTEDIT_BUSY);
11526 error = got_ref_open(&branch, repo,
11527 got_worktree_get_head_ref_name(worktree), 0);
11531 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
11532 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
11533 "will not edit commit history of a branch outside "
11534 "the \"refs/heads/\" reference namespace");
11538 error = got_ref_resolve(&head_commit_id, repo, branch);
11539 got_ref_close(branch);
11544 error = got_object_open_as_commit(&commit, repo,
11548 parent_ids = got_object_commit_get_parent_ids(commit);
11549 pid = STAILQ_FIRST(parent_ids);
11551 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
11554 error = collect_commits(&commits, head_commit_id, &pid->id,
11555 got_worktree_get_base_commit_id(worktree),
11556 got_worktree_get_path_prefix(worktree),
11557 GOT_ERR_HISTEDIT_PATH, repo);
11558 got_object_commit_close(commit);
11563 if (STAILQ_EMPTY(&commits)) {
11564 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
11568 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
11569 &base_commit_id, &fileindex, worktree, repo);
11573 if (edit_script_path) {
11574 error = histedit_load_list(&histedit_cmds,
11575 edit_script_path, repo);
11577 got_worktree_histedit_abort(worktree, fileindex,
11578 repo, branch, base_commit_id,
11579 abort_progress, &upa);
11580 print_merge_progress_stats(&upa);
11584 const char *branch_name;
11585 branch_name = got_ref_get_symref_target(branch);
11586 if (strncmp(branch_name, "refs/heads/", 11) == 0)
11588 error = histedit_edit_script(&histedit_cmds, &commits,
11589 branch_name, edit_logmsg_only, fold_only,
11592 got_worktree_histedit_abort(worktree, fileindex,
11593 repo, branch, base_commit_id,
11594 abort_progress, &upa);
11595 print_merge_progress_stats(&upa);
11601 error = histedit_save_list(&histedit_cmds, worktree,
11604 got_worktree_histedit_abort(worktree, fileindex,
11605 repo, branch, base_commit_id,
11606 abort_progress, &upa);
11607 print_merge_progress_stats(&upa);
11613 error = histedit_check_script(&histedit_cmds, &commits, repo);
11617 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
11618 if (resume_commit_id) {
11619 if (got_object_id_cmp(hle->commit_id,
11620 resume_commit_id) != 0)
11623 resume_commit_id = NULL;
11624 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
11625 hle->cmd->code == GOT_HISTEDIT_FOLD) {
11626 error = histedit_skip_commit(hle, worktree,
11631 struct got_pathlist_head paths;
11632 int have_changes = 0;
11634 TAILQ_INIT(&paths);
11635 error = got_pathlist_append(&paths, "", NULL);
11638 error = got_worktree_status(worktree, &paths,
11639 repo, 0, check_local_changes, &have_changes,
11640 check_cancelled, NULL);
11641 got_pathlist_free(&paths);
11643 if (error->code != GOT_ERR_CANCELLED)
11645 if (sigint_received || sigpipe_received)
11648 if (have_changes) {
11649 error = histedit_commit(NULL, worktree,
11650 fileindex, tmp_branch, hle, repo);
11654 error = got_object_open_as_commit(
11655 &commit, repo, hle->commit_id);
11658 error = show_histedit_progress(commit,
11660 got_object_commit_close(commit);
11669 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
11670 error = histedit_skip_commit(hle, worktree, repo);
11676 error = got_object_open_as_commit(&commit, repo,
11680 parent_ids = got_object_commit_get_parent_ids(commit);
11681 pid = STAILQ_FIRST(parent_ids);
11683 error = got_worktree_histedit_merge_files(&merged_paths,
11684 worktree, fileindex, &pid->id, hle->commit_id, repo,
11685 update_progress, &upa, check_cancelled, NULL);
11688 got_object_commit_close(commit);
11691 print_merge_progress_stats(&upa);
11692 if (upa.conflicts > 0 || upa.missing > 0 ||
11693 upa.not_deleted > 0 || upa.unversioned > 0) {
11694 if (upa.conflicts > 0) {
11695 error = show_rebase_merge_conflict(
11696 hle->commit_id, repo);
11700 got_worktree_rebase_pathlist_free(&merged_paths);
11704 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
11706 error = got_object_id_str(&id_str, hle->commit_id);
11709 printf("Stopping histedit for amending commit %s\n",
11712 got_worktree_rebase_pathlist_free(&merged_paths);
11713 error = got_worktree_histedit_postpone(worktree,
11718 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
11719 error = histedit_skip_commit(hle, worktree, repo);
11725 error = histedit_commit(&merged_paths, worktree, fileindex,
11726 tmp_branch, hle, repo);
11727 got_worktree_rebase_pathlist_free(&merged_paths);
11732 if (upa.conflicts > 0 || upa.missing > 0 ||
11733 upa.not_deleted > 0 || upa.unversioned > 0) {
11734 error = got_worktree_histedit_postpone(worktree, fileindex);
11737 if (upa.conflicts > 0 && upa.missing == 0 &&
11738 upa.not_deleted == 0 && upa.unversioned == 0) {
11739 error = got_error_msg(GOT_ERR_CONFLICTS,
11740 "conflicts must be resolved before histedit "
11742 } else if (upa.conflicts > 0) {
11743 error = got_error_msg(GOT_ERR_CONFLICTS,
11744 "conflicts must be resolved before histedit "
11745 "can continue; changes destined for some "
11746 "files were not yet merged and should be "
11747 "merged manually if required before the "
11748 "histedit operation is continued");
11750 error = got_error_msg(GOT_ERR_CONFLICTS,
11751 "changes destined for some files were not "
11752 "yet merged and should be merged manually "
11753 "if required before the histedit operation "
11757 error = histedit_complete(worktree, fileindex, tmp_branch,
11760 got_object_id_queue_free(&commits);
11761 histedit_free_list(&histedit_cmds);
11762 free(head_commit_id);
11763 free(base_commit_id);
11764 free(resume_commit_id);
11766 got_object_commit_close(commit);
11768 got_ref_close(branch);
11770 got_ref_close(tmp_branch);
11772 got_worktree_close(worktree);
11774 const struct got_error *close_err = got_repo_close(repo);
11779 const struct got_error *pack_err =
11780 got_repo_pack_fds_close(pack_fds);
11788 usage_integrate(void)
11790 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
11794 static const struct got_error *
11795 cmd_integrate(int argc, char *argv[])
11797 const struct got_error *error = NULL;
11798 struct got_repository *repo = NULL;
11799 struct got_worktree *worktree = NULL;
11800 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
11801 const char *branch_arg = NULL;
11802 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
11803 struct got_fileindex *fileindex = NULL;
11804 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
11806 struct got_update_progress_arg upa;
11807 int *pack_fds = NULL;
11809 while ((ch = getopt(argc, argv, "")) != -1) {
11822 branch_arg = argv[0];
11824 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
11825 "unveil", NULL) == -1)
11828 cwd = getcwd(NULL, 0);
11830 error = got_error_from_errno("getcwd");
11834 error = got_repo_pack_fds_open(&pack_fds);
11838 error = got_worktree_open(&worktree, cwd);
11840 if (error->code == GOT_ERR_NOT_WORKTREE)
11841 error = wrap_not_worktree_error(error, "integrate",
11846 error = check_rebase_or_histedit_in_progress(worktree);
11850 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
11855 error = apply_unveil(got_repo_get_path(repo), 0,
11856 got_worktree_get_root_path(worktree));
11860 error = check_merge_in_progress(worktree, repo);
11864 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
11865 error = got_error_from_errno("asprintf");
11869 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
11870 &base_branch_ref, worktree, refname, repo);
11874 refname = strdup(got_ref_get_name(branch_ref));
11875 if (refname == NULL) {
11876 error = got_error_from_errno("strdup");
11877 got_worktree_integrate_abort(worktree, fileindex, repo,
11878 branch_ref, base_branch_ref);
11881 base_refname = strdup(got_ref_get_name(base_branch_ref));
11882 if (base_refname == NULL) {
11883 error = got_error_from_errno("strdup");
11884 got_worktree_integrate_abort(worktree, fileindex, repo,
11885 branch_ref, base_branch_ref);
11889 error = got_ref_resolve(&commit_id, repo, branch_ref);
11893 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
11897 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
11898 error = got_error_msg(GOT_ERR_SAME_BRANCH,
11899 "specified branch has already been integrated");
11900 got_worktree_integrate_abort(worktree, fileindex, repo,
11901 branch_ref, base_branch_ref);
11905 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
11907 if (error->code == GOT_ERR_ANCESTRY)
11908 error = got_error(GOT_ERR_REBASE_REQUIRED);
11909 got_worktree_integrate_abort(worktree, fileindex, repo,
11910 branch_ref, base_branch_ref);
11914 memset(&upa, 0, sizeof(upa));
11915 error = got_worktree_integrate_continue(worktree, fileindex, repo,
11916 branch_ref, base_branch_ref, update_progress, &upa,
11917 check_cancelled, NULL);
11921 printf("Integrated %s into %s\n", refname, base_refname);
11922 print_update_progress_stats(&upa);
11925 const struct got_error *close_err = got_repo_close(repo);
11930 got_worktree_close(worktree);
11932 const struct got_error *pack_err =
11933 got_repo_pack_fds_close(pack_fds);
11938 free(base_commit_id);
11941 free(base_refname);
11948 fprintf(stderr, "usage: %s merge [-a] [-c] [-n] [branch]\n",
11953 static const struct got_error *
11954 cmd_merge(int argc, char *argv[])
11956 const struct got_error *error = NULL;
11957 struct got_worktree *worktree = NULL;
11958 struct got_repository *repo = NULL;
11959 struct got_fileindex *fileindex = NULL;
11960 char *cwd = NULL, *id_str = NULL, *author = NULL;
11961 struct got_reference *branch = NULL, *wt_branch = NULL;
11962 struct got_object_id *branch_tip = NULL, *yca_id = NULL;
11963 struct got_object_id *wt_branch_tip = NULL;
11964 int ch, merge_in_progress = 0, abort_merge = 0, continue_merge = 0;
11965 int interrupt_merge = 0;
11966 struct got_update_progress_arg upa;
11967 struct got_object_id *merge_commit_id = NULL;
11968 char *branch_name = NULL;
11969 int *pack_fds = NULL;
11971 memset(&upa, 0, sizeof(upa));
11973 while ((ch = getopt(argc, argv, "acn")) != -1) {
11979 continue_merge = 1;
11982 interrupt_merge = 1;
11994 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
11995 "unveil", NULL) == -1)
11999 if (abort_merge && continue_merge)
12000 option_conflict('a', 'c');
12001 if (abort_merge || continue_merge) {
12004 } else if (argc != 1)
12007 cwd = getcwd(NULL, 0);
12009 error = got_error_from_errno("getcwd");
12013 error = got_repo_pack_fds_open(&pack_fds);
12017 error = got_worktree_open(&worktree, cwd);
12019 if (error->code == GOT_ERR_NOT_WORKTREE)
12020 error = wrap_not_worktree_error(error,
12025 error = got_repo_open(&repo,
12026 worktree ? got_worktree_get_repo_path(worktree) : cwd, NULL,
12031 error = apply_unveil(got_repo_get_path(repo), 0,
12032 worktree ? got_worktree_get_root_path(worktree) : NULL);
12036 error = check_rebase_or_histedit_in_progress(worktree);
12040 error = got_worktree_merge_in_progress(&merge_in_progress, worktree,
12046 if (!merge_in_progress) {
12047 error = got_error(GOT_ERR_NOT_MERGING);
12050 error = got_worktree_merge_continue(&branch_name,
12051 &branch_tip, &fileindex, worktree, repo);
12054 error = got_worktree_merge_abort(worktree, fileindex, repo,
12055 abort_progress, &upa);
12058 printf("Merge of %s aborted\n", branch_name);
12059 goto done; /* nothing else to do */
12062 error = get_author(&author, repo, worktree);
12066 if (continue_merge) {
12067 if (!merge_in_progress) {
12068 error = got_error(GOT_ERR_NOT_MERGING);
12071 error = got_worktree_merge_continue(&branch_name,
12072 &branch_tip, &fileindex, worktree, repo);
12076 error = got_ref_open(&branch, repo, argv[0], 0);
12079 branch_name = strdup(got_ref_get_name(branch));
12080 if (branch_name == NULL) {
12081 error = got_error_from_errno("strdup");
12084 error = got_ref_resolve(&branch_tip, repo, branch);
12089 error = got_ref_open(&wt_branch, repo,
12090 got_worktree_get_head_ref_name(worktree), 0);
12093 error = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
12096 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
12097 wt_branch_tip, branch_tip, 0, repo,
12098 check_cancelled, NULL);
12099 if (error && error->code != GOT_ERR_ANCESTRY)
12102 if (!continue_merge) {
12103 error = check_path_prefix(wt_branch_tip, branch_tip,
12104 got_worktree_get_path_prefix(worktree),
12105 GOT_ERR_MERGE_PATH, repo);
12109 error = check_same_branch(wt_branch_tip, branch,
12112 if (error->code != GOT_ERR_ANCESTRY)
12116 static char msg[512];
12117 snprintf(msg, sizeof(msg),
12118 "cannot create a merge commit because "
12119 "%s is based on %s; %s can be integrated "
12120 "with 'got integrate' instead", branch_name,
12121 got_worktree_get_head_ref_name(worktree),
12123 error = got_error_msg(GOT_ERR_SAME_BRANCH, msg);
12127 error = got_worktree_merge_prepare(&fileindex, worktree,
12132 error = got_worktree_merge_branch(worktree, fileindex,
12133 yca_id, branch_tip, repo, update_progress, &upa,
12134 check_cancelled, NULL);
12137 print_merge_progress_stats(&upa);
12138 if (!upa.did_something) {
12139 error = got_worktree_merge_abort(worktree, fileindex,
12140 repo, abort_progress, &upa);
12143 printf("Already up-to-date\n");
12148 if (interrupt_merge) {
12149 error = got_worktree_merge_postpone(worktree, fileindex);
12152 printf("Merge of %s interrupted on request\n", branch_name);
12153 } else if (upa.conflicts > 0 || upa.missing > 0 ||
12154 upa.not_deleted > 0 || upa.unversioned > 0) {
12155 error = got_worktree_merge_postpone(worktree, fileindex);
12158 if (upa.conflicts > 0 && upa.missing == 0 &&
12159 upa.not_deleted == 0 && upa.unversioned == 0) {
12160 error = got_error_msg(GOT_ERR_CONFLICTS,
12161 "conflicts must be resolved before merging "
12163 } else if (upa.conflicts > 0) {
12164 error = got_error_msg(GOT_ERR_CONFLICTS,
12165 "conflicts must be resolved before merging "
12166 "can continue; changes destined for some "
12167 "files were not yet merged and "
12168 "should be merged manually if required before the "
12169 "merge operation is continued");
12171 error = got_error_msg(GOT_ERR_CONFLICTS,
12172 "changes destined for some "
12173 "files were not yet merged and should be "
12174 "merged manually if required before the "
12175 "merge operation is continued");
12179 error = got_worktree_merge_commit(&merge_commit_id, worktree,
12180 fileindex, author, NULL, 1, branch_tip, branch_name,
12181 repo, continue_merge ? print_status : NULL, NULL);
12184 error = got_worktree_merge_complete(worktree, fileindex, repo);
12187 error = got_object_id_str(&id_str, merge_commit_id);
12190 printf("Merged %s into %s: %s\n", branch_name,
12191 got_worktree_get_head_ref_name(worktree),
12197 free(merge_commit_id);
12203 got_ref_close(branch);
12205 got_ref_close(wt_branch);
12207 got_worktree_close(worktree);
12209 const struct got_error *close_err = got_repo_close(repo);
12214 const struct got_error *pack_err =
12215 got_repo_pack_fds_close(pack_fds);
12225 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
12226 "[-S] [file-path ...]\n",
12231 static const struct got_error *
12232 print_stage(void *arg, unsigned char status, unsigned char staged_status,
12233 const char *path, struct got_object_id *blob_id,
12234 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
12235 int dirfd, const char *de_name)
12237 const struct got_error *err = NULL;
12238 char *id_str = NULL;
12240 if (staged_status != GOT_STATUS_ADD &&
12241 staged_status != GOT_STATUS_MODIFY &&
12242 staged_status != GOT_STATUS_DELETE)
12245 if (staged_status == GOT_STATUS_ADD ||
12246 staged_status == GOT_STATUS_MODIFY)
12247 err = got_object_id_str(&id_str, staged_blob_id);
12249 err = got_object_id_str(&id_str, blob_id);
12253 printf("%s %c %s\n", id_str, staged_status, path);
12258 static const struct got_error *
12259 cmd_stage(int argc, char *argv[])
12261 const struct got_error *error = NULL;
12262 struct got_repository *repo = NULL;
12263 struct got_worktree *worktree = NULL;
12265 struct got_pathlist_head paths;
12266 struct got_pathlist_entry *pe;
12267 int ch, list_stage = 0, pflag = 0, allow_bad_symlinks = 0;
12268 FILE *patch_script_file = NULL;
12269 const char *patch_script_path = NULL;
12270 struct choose_patch_arg cpa;
12271 int *pack_fds = NULL;
12273 TAILQ_INIT(&paths);
12275 while ((ch = getopt(argc, argv, "lpF:S")) != -1) {
12284 patch_script_path = optarg;
12287 allow_bad_symlinks = 1;
12299 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
12300 "unveil", NULL) == -1)
12303 if (list_stage && (pflag || patch_script_path))
12304 errx(1, "-l option cannot be used with other options");
12305 if (patch_script_path && !pflag)
12306 errx(1, "-F option can only be used together with -p option");
12308 cwd = getcwd(NULL, 0);
12310 error = got_error_from_errno("getcwd");
12314 error = got_repo_pack_fds_open(&pack_fds);
12318 error = got_worktree_open(&worktree, cwd);
12320 if (error->code == GOT_ERR_NOT_WORKTREE)
12321 error = wrap_not_worktree_error(error, "stage", cwd);
12325 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
12330 if (patch_script_path) {
12331 patch_script_file = fopen(patch_script_path, "re");
12332 if (patch_script_file == NULL) {
12333 error = got_error_from_errno2("fopen",
12334 patch_script_path);
12338 error = apply_unveil(got_repo_get_path(repo), 0,
12339 got_worktree_get_root_path(worktree));
12343 error = check_merge_in_progress(worktree, repo);
12347 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
12352 error = got_worktree_status(worktree, &paths, repo, 0,
12353 print_stage, NULL, check_cancelled, NULL);
12355 cpa.patch_script_file = patch_script_file;
12356 cpa.action = "stage";
12357 error = got_worktree_stage(worktree, &paths,
12358 pflag ? NULL : print_status, NULL,
12359 pflag ? choose_patch : NULL, &cpa,
12360 allow_bad_symlinks, repo);
12363 if (patch_script_file && fclose(patch_script_file) == EOF &&
12365 error = got_error_from_errno2("fclose", patch_script_path);
12367 const struct got_error *close_err = got_repo_close(repo);
12372 got_worktree_close(worktree);
12374 const struct got_error *pack_err =
12375 got_repo_pack_fds_close(pack_fds);
12379 TAILQ_FOREACH(pe, &paths, entry)
12380 free((char *)pe->path);
12381 got_pathlist_free(&paths);
12387 usage_unstage(void)
12389 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
12390 "[file-path ...]\n",
12396 static const struct got_error *
12397 cmd_unstage(int argc, char *argv[])
12399 const struct got_error *error = NULL;
12400 struct got_repository *repo = NULL;
12401 struct got_worktree *worktree = NULL;
12403 struct got_pathlist_head paths;
12404 struct got_pathlist_entry *pe;
12406 struct got_update_progress_arg upa;
12407 FILE *patch_script_file = NULL;
12408 const char *patch_script_path = NULL;
12409 struct choose_patch_arg cpa;
12410 int *pack_fds = NULL;
12412 TAILQ_INIT(&paths);
12414 while ((ch = getopt(argc, argv, "pF:")) != -1) {
12420 patch_script_path = optarg;
12432 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
12433 "unveil", NULL) == -1)
12436 if (patch_script_path && !pflag)
12437 errx(1, "-F option can only be used together with -p option");
12439 cwd = getcwd(NULL, 0);
12441 error = got_error_from_errno("getcwd");
12445 error = got_repo_pack_fds_open(&pack_fds);
12449 error = got_worktree_open(&worktree, cwd);
12451 if (error->code == GOT_ERR_NOT_WORKTREE)
12452 error = wrap_not_worktree_error(error, "unstage", cwd);
12456 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
12461 if (patch_script_path) {
12462 patch_script_file = fopen(patch_script_path, "re");
12463 if (patch_script_file == NULL) {
12464 error = got_error_from_errno2("fopen",
12465 patch_script_path);
12470 error = apply_unveil(got_repo_get_path(repo), 0,
12471 got_worktree_get_root_path(worktree));
12475 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
12479 cpa.patch_script_file = patch_script_file;
12480 cpa.action = "unstage";
12481 memset(&upa, 0, sizeof(upa));
12482 error = got_worktree_unstage(worktree, &paths, update_progress,
12483 &upa, pflag ? choose_patch : NULL, &cpa, repo);
12485 print_merge_progress_stats(&upa);
12487 if (patch_script_file && fclose(patch_script_file) == EOF &&
12489 error = got_error_from_errno2("fclose", patch_script_path);
12491 const struct got_error *close_err = got_repo_close(repo);
12496 got_worktree_close(worktree);
12498 const struct got_error *pack_err =
12499 got_repo_pack_fds_close(pack_fds);
12503 TAILQ_FOREACH(pe, &paths, entry)
12504 free((char *)pe->path);
12505 got_pathlist_free(&paths);
12513 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
12514 "arg1 [arg2 ...]\n", getprogname());
12518 static const struct got_error *
12519 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
12521 const struct got_error *err;
12522 struct got_blob_object *blob;
12525 fd = got_opentempfd();
12527 return got_error_from_errno("got_opentempfd");
12529 err = got_object_open_as_blob(&blob, repo, id, 8192, fd);
12533 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
12535 if (fd != -1 && close(fd) == -1 && err == NULL)
12536 err = got_error_from_errno("close");
12538 got_object_blob_close(blob);
12542 static const struct got_error *
12543 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
12545 const struct got_error *err;
12546 struct got_tree_object *tree;
12549 err = got_object_open_as_tree(&tree, repo, id);
12553 nentries = got_object_tree_get_nentries(tree);
12554 for (i = 0; i < nentries; i++) {
12555 struct got_tree_entry *te;
12557 if (sigint_received || sigpipe_received)
12559 te = got_object_tree_get_entry(tree, i);
12560 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
12563 fprintf(outfile, "%s %.7o %s\n", id_str,
12564 got_tree_entry_get_mode(te),
12565 got_tree_entry_get_name(te));
12569 got_object_tree_close(tree);
12573 static const struct got_error *
12574 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
12576 const struct got_error *err;
12577 struct got_commit_object *commit;
12578 const struct got_object_id_queue *parent_ids;
12579 struct got_object_qid *pid;
12580 char *id_str = NULL;
12581 const char *logmsg = NULL;
12584 err = got_object_open_as_commit(&commit, repo, id);
12588 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
12592 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
12593 parent_ids = got_object_commit_get_parent_ids(commit);
12594 fprintf(outfile, "numparents %d\n",
12595 got_object_commit_get_nparents(commit));
12596 STAILQ_FOREACH(pid, parent_ids, entry) {
12598 err = got_object_id_str(&pid_str, &pid->id);
12601 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
12604 got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
12605 got_object_commit_get_author_gmtoff(commit));
12606 fprintf(outfile, "%s%s %lld %s\n", GOT_COMMIT_LABEL_AUTHOR,
12607 got_object_commit_get_author(commit),
12608 (long long)got_object_commit_get_author_time(commit),
12611 got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
12612 got_object_commit_get_committer_gmtoff(commit));
12613 fprintf(outfile, "%s%s %lld %s\n", GOT_COMMIT_LABEL_COMMITTER,
12614 got_object_commit_get_author(commit),
12615 (long long)got_object_commit_get_committer_time(commit),
12618 logmsg = got_object_commit_get_logmsg_raw(commit);
12619 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
12620 fprintf(outfile, "%s", logmsg);
12623 got_object_commit_close(commit);
12627 static const struct got_error *
12628 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
12630 const struct got_error *err;
12631 struct got_tag_object *tag;
12632 char *id_str = NULL;
12633 const char *tagmsg = NULL;
12636 err = got_object_open_as_tag(&tag, repo, id);
12640 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
12644 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
12646 switch (got_object_tag_get_object_type(tag)) {
12647 case GOT_OBJ_TYPE_BLOB:
12648 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
12649 GOT_OBJ_LABEL_BLOB);
12651 case GOT_OBJ_TYPE_TREE:
12652 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
12653 GOT_OBJ_LABEL_TREE);
12655 case GOT_OBJ_TYPE_COMMIT:
12656 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
12657 GOT_OBJ_LABEL_COMMIT);
12659 case GOT_OBJ_TYPE_TAG:
12660 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
12661 GOT_OBJ_LABEL_TAG);
12667 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
12668 got_object_tag_get_name(tag));
12670 got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
12671 got_object_tag_get_tagger_gmtoff(tag));
12672 fprintf(outfile, "%s%s %lld %s\n", GOT_TAG_LABEL_TAGGER,
12673 got_object_tag_get_tagger(tag),
12674 (long long)got_object_tag_get_tagger_time(tag),
12677 tagmsg = got_object_tag_get_message(tag);
12678 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
12679 fprintf(outfile, "%s", tagmsg);
12682 got_object_tag_close(tag);
12686 static const struct got_error *
12687 cmd_cat(int argc, char *argv[])
12689 const struct got_error *error;
12690 struct got_repository *repo = NULL;
12691 struct got_worktree *worktree = NULL;
12692 char *cwd = NULL, *repo_path = NULL, *label = NULL;
12693 const char *commit_id_str = NULL;
12694 struct got_object_id *id = NULL, *commit_id = NULL;
12695 struct got_commit_object *commit = NULL;
12696 int ch, obj_type, i, force_path = 0;
12697 struct got_reflist_head refs;
12698 int *pack_fds = NULL;
12703 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
12708 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
12711 commit_id_str = optarg;
12714 repo_path = realpath(optarg, NULL);
12715 if (repo_path == NULL)
12716 return got_error_from_errno2("realpath",
12718 got_path_strip_trailing_slashes(repo_path);
12732 cwd = getcwd(NULL, 0);
12734 error = got_error_from_errno("getcwd");
12738 error = got_repo_pack_fds_open(&pack_fds);
12742 if (repo_path == NULL) {
12743 error = got_worktree_open(&worktree, cwd);
12744 if (error && error->code != GOT_ERR_NOT_WORKTREE)
12747 repo_path = strdup(
12748 got_worktree_get_repo_path(worktree));
12749 if (repo_path == NULL) {
12750 error = got_error_from_errno("strdup");
12754 /* Release work tree lock. */
12755 got_worktree_close(worktree);
12760 if (repo_path == NULL) {
12761 repo_path = strdup(cwd);
12762 if (repo_path == NULL)
12763 return got_error_from_errno("strdup");
12766 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
12771 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
12775 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
12779 if (commit_id_str == NULL)
12780 commit_id_str = GOT_REF_HEAD;
12781 error = got_repo_match_object_id(&commit_id, NULL,
12782 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
12786 error = got_object_open_as_commit(&commit, repo, commit_id);
12790 for (i = 0; i < argc; i++) {
12792 error = got_object_id_by_path(&id, repo, commit,
12797 error = got_repo_match_object_id(&id, &label, argv[i],
12798 GOT_OBJ_TYPE_ANY, NULL /* do not resolve tags */,
12801 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
12802 error->code != GOT_ERR_NOT_REF)
12804 error = got_object_id_by_path(&id, repo,
12811 error = got_object_get_type(&obj_type, repo, id);
12815 switch (obj_type) {
12816 case GOT_OBJ_TYPE_BLOB:
12817 error = cat_blob(id, repo, stdout);
12819 case GOT_OBJ_TYPE_TREE:
12820 error = cat_tree(id, repo, stdout);
12822 case GOT_OBJ_TYPE_COMMIT:
12823 error = cat_commit(id, repo, stdout);
12825 case GOT_OBJ_TYPE_TAG:
12826 error = cat_tag(id, repo, stdout);
12829 error = got_error(GOT_ERR_OBJ_TYPE);
12844 got_object_commit_close(commit);
12846 got_worktree_close(worktree);
12848 const struct got_error *close_err = got_repo_close(repo);
12853 const struct got_error *pack_err =
12854 got_repo_pack_fds_close(pack_fds);
12859 got_ref_list_free(&refs);
12866 fprintf(stderr, "usage: %s info [path ...]\n",
12871 static const struct got_error *
12872 print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
12873 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
12874 struct got_object_id *commit_id)
12876 const struct got_error *err = NULL;
12877 char *id_str = NULL;
12879 struct tm mytm, *tm;
12880 struct got_pathlist_head *paths = arg;
12881 struct got_pathlist_entry *pe;
12884 * Clear error indication from any of the path arguments which
12885 * would cause this file index entry to be displayed.
12887 TAILQ_FOREACH(pe, paths, entry) {
12888 if (got_path_cmp(path, pe->path, strlen(path),
12889 pe->path_len) == 0 ||
12890 got_path_is_child(path, pe->path, pe->path_len))
12891 pe->data = NULL; /* no error */
12894 printf(GOT_COMMIT_SEP_STR);
12896 printf("symlink: %s\n", path);
12897 else if (S_ISREG(mode)) {
12898 printf("file: %s\n", path);
12899 printf("mode: %o\n", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
12900 } else if (S_ISDIR(mode))
12901 printf("directory: %s\n", path);
12903 printf("something: %s\n", path);
12905 tm = localtime_r(&mtime, &mytm);
12908 if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) == 0)
12909 return got_error(GOT_ERR_NO_SPACE);
12910 printf("timestamp: %s\n", datebuf);
12913 err = got_object_id_str(&id_str, blob_id);
12916 printf("based on blob: %s\n", id_str);
12920 if (staged_blob_id) {
12921 err = got_object_id_str(&id_str, staged_blob_id);
12924 printf("based on staged blob: %s\n", id_str);
12929 err = got_object_id_str(&id_str, commit_id);
12932 printf("based on commit: %s\n", id_str);
12939 static const struct got_error *
12940 cmd_info(int argc, char *argv[])
12942 const struct got_error *error = NULL;
12943 struct got_worktree *worktree = NULL;
12944 char *cwd = NULL, *id_str = NULL;
12945 struct got_pathlist_head paths;
12946 struct got_pathlist_entry *pe;
12947 char *uuidstr = NULL;
12948 int ch, show_files = 0;
12949 int *pack_fds = NULL;
12951 TAILQ_INIT(&paths);
12953 while ((ch = getopt(argc, argv, "")) != -1) {
12965 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
12969 cwd = getcwd(NULL, 0);
12971 error = got_error_from_errno("getcwd");
12975 error = got_repo_pack_fds_open(&pack_fds);
12979 error = got_worktree_open(&worktree, cwd);
12981 if (error->code == GOT_ERR_NOT_WORKTREE)
12982 error = wrap_not_worktree_error(error, "info", cwd);
12987 /* Remove "cpath" promise. */
12988 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
12992 error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
12997 error = get_worktree_paths_from_argv(&paths, argc, argv,
13004 error = got_object_id_str(&id_str,
13005 got_worktree_get_base_commit_id(worktree));
13009 error = got_worktree_get_uuid(&uuidstr, worktree);
13013 printf("work tree: %s\n", got_worktree_get_root_path(worktree));
13014 printf("work tree base commit: %s\n", id_str);
13015 printf("work tree path prefix: %s\n",
13016 got_worktree_get_path_prefix(worktree));
13017 printf("work tree branch reference: %s\n",
13018 got_worktree_get_head_ref_name(worktree));
13019 printf("work tree UUID: %s\n", uuidstr);
13020 printf("repository: %s\n", got_worktree_get_repo_path(worktree));
13023 struct got_pathlist_entry *pe;
13024 TAILQ_FOREACH(pe, &paths, entry) {
13025 if (pe->path_len == 0)
13028 * Assume this path will fail. This will be corrected
13029 * in print_path_info() in case the path does suceeed.
13031 pe->data = (void *)got_error_path(pe->path,
13034 error = got_worktree_path_info(worktree, &paths,
13035 print_path_info, &paths, check_cancelled, NULL);
13038 TAILQ_FOREACH(pe, &paths, entry) {
13039 if (pe->data != NULL) {
13040 error = pe->data; /* bad path */
13047 const struct got_error *pack_err =
13048 got_repo_pack_fds_close(pack_fds);
13052 TAILQ_FOREACH(pe, &paths, entry)
13053 free((char *)pe->path);
13054 got_pathlist_free(&paths);